From patchwork Mon Oct 7 14:19:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 13824759 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF9A21D8A1D; Mon, 7 Oct 2024 14:19:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310774; cv=none; b=KB84tRiIbuZFQmJ0iUND9ulSg0XDvY+OteLu3QUWSzktL9PZgAC/Pl3ns7/J9Fwa1C/Lz8ocCY9+mzKQGuef3RWckYLU1LW03HIN7KnHveYpBx+W958zN95hhP8WxmNCsRbr9zYG2L9ArrGf0aDbzZSyv7DtxWPYR3LUKdRoRTQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310774; c=relaxed/simple; bh=u5uN/v0hcCXxJzVjMecqx8XNsYDzzBB91/lRJbyg+lQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QgnvqW2DMIbCw4kovW7hw8QwrNGFDs8wK35NZeZJoR6T6f2cEyo25icq9tdvcDa0LJtLwzZBVMyuG5C9cvtDHlBo2AzuitD2xr/SLTITb/MgD/KtBv49w4kmypLp+iO8rpEJ0b6EcrCMp8+V7dvGOdJa78ylg9NrnSVKWjRvLRc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Pge6O+w0; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Pge6O+w0" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-42e7b7bef42so39740245e9.3; Mon, 07 Oct 2024 07:19:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728310771; x=1728915571; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vkU+cfiiNTUBWGlMXTLsIHnT7e0/Q3j2lLd4OH/mVZQ=; b=Pge6O+w0mnxdqtjIiXHPVkiZQUfxj6SX17OKYzSt3/rrzjlH7Io5CjeGoagdy8PuSx JIh2teeu3xHV6OCkr2o04eOb0wWM71w4fhJPTXWfEAPnu6jcpEIG0dfSGfQtHDjD5g45 06AxS9vK471H0cYVS7ia9vLID3WB0fPZnJ0oQaLtSav/I4SfVtRXGtxeV4zK1xVnxs0e i36xTKHDBHqODXPw+gnzlIrSI99sKJS0Cb5nqDhTZmQGnFoUeXLAXIxv85YeN7xkS7zl QVOG7G5Td1yDcfW7YsMDluss2+XsOYnl20ZBYp/G+ZiIV+l7m+icpv5iQhuUZdLZglgo nKUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728310771; x=1728915571; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vkU+cfiiNTUBWGlMXTLsIHnT7e0/Q3j2lLd4OH/mVZQ=; b=dKKTVU523hexg2iH+MsU3rDU2aAHlrA45+eqrWQi8bsX9EtxCevHoOlclmXSrBwPyr eLqaagyrknpGq84JuNSDNbRD5tWnzkXL7r/M6PiX3OTGcDmP6A/hHnOohFK51N2Sow1X g2wvMmKInYSZnkGMCk/3lfTH+M8J7PTtHHWppZxVAi0pCF4Zy/i6iA0aiK74CRR/7Rj0 CzA4okw5/uxnbWqc39zOzh4BYoeM9b/q0CkdNX+J9IDwfbGhohhsBg249QuH2Lr0ms9r Pogtgfh284zS/ZTZ9WPDpHz15/z4Ghqz5sy5dR9F6yc1XucoV79kqkL4fVqhRAxNWVjO 8XRw== X-Forwarded-Encrypted: i=1; AJvYcCVFjgGqRvESfU1jWjvVgq6orZfZV2XYN+0IaAJmCOcJoSqNK4pBoKd9e2lUwHacXhSZdt4W+GVp853EVCMxvA==@vger.kernel.org, AJvYcCWM+wbwgNg7bqgqFoJxHNegKqVfmfGpeupsPZw5gec1psN20tVUlZUPON1Hvp8TKtmY9O/zzsaSiHZfJGjF@vger.kernel.org X-Gm-Message-State: AOJu0YzO0hRXN+Uydl8VVqKGyuk8eX9oAUGCz/DHD4C4n23doBM4nQPF RULTVWNcZ+kB+efJwNWtE6Pih/kqS1BGe+G03IlupOCxjjP+5b/o X-Google-Smtp-Source: AGHT+IGKxDvdA09dWlHWXnHyK0wlYmqqaOmgsyBKkbb/tPoGFyLrB44dzfPBTJ4IFTFAZ29mEoADog== X-Received: by 2002:a05:600c:5124:b0:426:6f17:531 with SMTP id 5b1f17b1804b1-42f947a79b8mr2643175e9.13.1728310770701; Mon, 07 Oct 2024 07:19:30 -0700 (PDT) Received: from amir-ThinkPad-T480.arnhem.chello.nl (92-109-99-123.cable.dynamic.v4.ziggo.nl. [92.109.99.123]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d16970520sm5829396f8f.96.2024.10.07.07.19.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 07:19:29 -0700 (PDT) From: Amir Goldstein To: Miklos Szeredi , Al Viro Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org Subject: [PATCH v3 1/5] ovl: do not open non-data lower file for fsync Date: Mon, 7 Oct 2024 16:19:21 +0200 Message-Id: <20241007141925.327055-2-amir73il@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241007141925.327055-1-amir73il@gmail.com> References: <20241007141925.327055-1-amir73il@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 ovl_fsync() with !datasync opens a backing file from the top most dentry in the stack, checks if this dentry is non-upper and skips the fsync. In case of an overlay dentry stack with lower data and lower metadata above it, but without an upper metadata above it, the backing file is opened from the top most lower metadata dentry and never used. Refactor the helper ovl_real_fdget_meta() into ovl_real_fdget_path() and ovl_upper_fdget() where the latter returns an empty struct fd in that case to avoid the unneeded backing file open. Signed-off-by: Amir Goldstein --- fs/overlayfs/file.c | 71 +++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index 4504493b20be..f5d0498355d0 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -89,32 +89,19 @@ static int ovl_change_flags(struct file *file, unsigned int flags) return 0; } -static int ovl_real_fdget_meta(const struct file *file, struct fd *real, - bool allow_meta) +static int ovl_real_fdget_path(const struct file *file, struct fd *real, + struct path *realpath) { - struct dentry *dentry = file_dentry(file); struct file *realfile = file->private_data; - struct path realpath; - int err; real->word = (unsigned long)realfile; - if (allow_meta) { - ovl_path_real(dentry, &realpath); - } else { - /* lazy lookup and verify of lowerdata */ - err = ovl_verify_lowerdata(dentry); - if (err) - return err; - - ovl_path_realdata(dentry, &realpath); - } - if (!realpath.dentry) + if (WARN_ON_ONCE(!realpath->dentry)) return -EIO; /* Has it been copied up since we'd opened it? */ - if (unlikely(file_inode(realfile) != d_inode(realpath.dentry))) { - struct file *f = ovl_open_realfile(file, &realpath); + if (unlikely(file_inode(realfile) != d_inode(realpath->dentry))) { + struct file *f = ovl_open_realfile(file, realpath); if (IS_ERR(f)) return PTR_ERR(f); real->word = (unsigned long)f | FDPUT_FPUT; @@ -130,7 +117,11 @@ static int ovl_real_fdget_meta(const struct file *file, struct fd *real, static int ovl_real_fdget(const struct file *file, struct fd *real) { - if (d_is_dir(file_dentry(file))) { + struct dentry *dentry = file_dentry(file); + struct path realpath; + int err; + + if (d_is_dir(dentry)) { struct file *f = ovl_dir_real_file(file, false); if (IS_ERR(f)) return PTR_ERR(f); @@ -138,7 +129,33 @@ static int ovl_real_fdget(const struct file *file, struct fd *real) return 0; } - return ovl_real_fdget_meta(file, real, false); + /* lazy lookup and verify of lowerdata */ + err = ovl_verify_lowerdata(dentry); + if (err) + return err; + + ovl_path_realdata(dentry, &realpath); + + return ovl_real_fdget_path(file, real, &realpath); +} + +static int ovl_upper_fdget(const struct file *file, struct fd *real, bool data) +{ + struct dentry *dentry = file_dentry(file); + struct path realpath; + enum ovl_path_type type; + + if (data) + type = ovl_path_realdata(dentry, &realpath); + else + type = ovl_path_real(dentry, &realpath); + + real->word = 0; + /* Not interested in lower nor in upper meta if data was requested */ + if (!OVL_TYPE_UPPER(type) || (data && OVL_TYPE_MERGE(type))) + return 0; + + return ovl_real_fdget_path(file, real, &realpath); } static int ovl_open(struct inode *inode, struct file *file) @@ -394,16 +411,14 @@ static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) if (ret <= 0) return ret; - ret = ovl_real_fdget_meta(file, &real, !datasync); - if (ret) + /* Don't sync lower file for fear of receiving EROFS error */ + ret = ovl_upper_fdget(file, &real, datasync); + if (ret || fd_empty(real)) return ret; - /* Don't sync lower file for fear of receiving EROFS error */ - if (file_inode(fd_file(real)) == ovl_inode_upper(file_inode(file))) { - old_cred = ovl_override_creds(file_inode(file)->i_sb); - ret = vfs_fsync_range(fd_file(real), start, end, datasync); - revert_creds(old_cred); - } + old_cred = ovl_override_creds(file_inode(file)->i_sb); + ret = vfs_fsync_range(fd_file(real), start, end, datasync); + revert_creds(old_cred); fdput(real); From patchwork Mon Oct 7 14:19:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 13824760 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0BC71D54D4; Mon, 7 Oct 2024 14:19:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310775; cv=none; b=BkAZun6FT25zadg+RRvNc/rYDSh3QilGHu62hUyBPCM1XSWJBPz+6M1qeY1x8Axq0aa6f8uJuLEIifzDiMeQGawTnluleBrFW2JMMu5UeWLGF5dBMwqKxEzwgpcFT3rhf3fdUTGbzb5KMoQw9eWM8al84h5KfwR7vwThYEZFMvM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310775; c=relaxed/simple; bh=pubetNtaZBB6HQRWyxq/zdamFrw+VN9WayJdV93bV24=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aH46L05aDbKAtQ667VNxoW9P5PGwiunReasJrGEXQg+LbgiTg54ArhkV+NzdMfEVwg7GdJYHwTvkLJHc3Vnb+2XNYiKurH2wNiMg5lOdgI4p89HZvGgMjvRs/HljjOJMlU9dlzAnmadBqsa9TUyiVxg0M4es28Ess6y9+Q41YHs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Xpda9vS9; arc=none smtp.client-ip=209.85.221.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Xpda9vS9" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-37ccfada422so2744301f8f.2; Mon, 07 Oct 2024 07:19:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728310772; x=1728915572; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1GE/dxQSdo3FDX8cP0cmk86KhCkq//3GbFgRlty45nI=; b=Xpda9vS9xsHSJ2up4cJXswBYsDu+RPE9z00/jfyM7Xo8KLJLmnwW8ZIPI9U26sM4nZ 5dj2Ql6+RY5i3OEudTAu/Mj8c0ixW5QaQ9fDhIiE5XKhnEYEY54ttsmLkkqy9CxoGD72 UGZm8OmwXbyxSM/m1GbYJ73x8cgAxdPU+k6SoyvLmT0QDB9pm4MpU8mppmhMW6kqNm5Q lpR/XWW6oEkj+H8psausK3K5NFoZBtNYWne0loImx7StluzqTLZ5ht8KQf5Q7WMx5cFc lUVyZWBnKJXi4mfe69t31prC2LzSXeICu6R9eNyJuFINRzNL/tgBUS8/jadFf8DjtsBt pM2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728310772; x=1728915572; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1GE/dxQSdo3FDX8cP0cmk86KhCkq//3GbFgRlty45nI=; b=Fg3QeW51BCzzee98YfexjXosnRoLhhf5mBnjy7akeCj/y4HGEde6t3yXTDzUyMo1Mo miAX+fGL5hb+fKl6EHfNXhGSTDD75qd1b3lkSAuIOBEqrUBp7c6TkiNYajSXrOOL9d6P Y/Op2GcRJ3EZb0CUH3I3+d5fyl+eEcqgqFzddiMywPXxpTfppCr/3gTTJaBwppS5VDI6 rLIOw2dbBb2/sajCXjCOjCu7w/LQr9O51A/Jj7Zr7emq1pKIkVmPTsah2cgHVo2dR2wL xFyj/eAUaHYTrQzpg+/ly27KEjr8x1vMiDvVFaWzG4JL3upd1zLT9cMOtLx5tf+Qv6/U K0oQ== X-Forwarded-Encrypted: i=1; AJvYcCUJIyty0zM+ZyLx6TRrvnif8WOGO8/BKTLV1QJLjGtoDhBXHxiIz9pxMf+lMGUr4kH1w2Rl8vKGsYr5R07C@vger.kernel.org, AJvYcCWsdf7guNYZKqklnVyfSX95zA9kszqynpwmo/mfU57U9d693p/XoVzW/+zktSO4MvYTGhCKS7UNfRCG9vKtYQ==@vger.kernel.org X-Gm-Message-State: AOJu0Yx4KW30RJh8AI3t+BYkCblHqtPappkmQp+ias2VGrTIExY0DDOZ Fzy9mqVeOgZmk+FD32t6BitIpHW3QTlHr8JPr5H0fFFHj/vqH5/b X-Google-Smtp-Source: AGHT+IFO2GfE0BBrw7is/zr+2wdjYgBDzcLwPn8HYeouW1s4YXjUEqKhcSG1e6mvxD0XHBfNxfRagg== X-Received: by 2002:a5d:648a:0:b0:375:1b02:1e3c with SMTP id ffacd0b85a97d-37d0e8de99dmr10227229f8f.45.1728310771496; Mon, 07 Oct 2024 07:19:31 -0700 (PDT) Received: from amir-ThinkPad-T480.arnhem.chello.nl (92-109-99-123.cable.dynamic.v4.ziggo.nl. [92.109.99.123]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d16970520sm5829396f8f.96.2024.10.07.07.19.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 07:19:31 -0700 (PDT) From: Amir Goldstein To: Miklos Szeredi , Al Viro Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org Subject: [PATCH v3 2/5] ovl: allocate a container struct ovl_file for ovl private context Date: Mon, 7 Oct 2024 16:19:22 +0200 Message-Id: <20241007141925.327055-3-amir73il@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241007141925.327055-1-amir73il@gmail.com> References: <20241007141925.327055-1-amir73il@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Instead of using ->private_data to point at realfile directly, so that we can add more context per ovl open file. Signed-off-by: Amir Goldstein --- fs/overlayfs/file.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index f5d0498355d0..03bf6037b129 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -89,10 +89,15 @@ static int ovl_change_flags(struct file *file, unsigned int flags) return 0; } +struct ovl_file { + struct file *realfile; +}; + static int ovl_real_fdget_path(const struct file *file, struct fd *real, struct path *realpath) { - struct file *realfile = file->private_data; + struct ovl_file *of = file->private_data; + struct file *realfile = of->realfile; real->word = (unsigned long)realfile; @@ -163,6 +168,7 @@ static int ovl_open(struct inode *inode, struct file *file) struct dentry *dentry = file_dentry(file); struct file *realfile; struct path realpath; + struct ovl_file *of; int err; /* lazy lookup and verify lowerdata */ @@ -181,18 +187,28 @@ static int ovl_open(struct inode *inode, struct file *file) if (!realpath.dentry) return -EIO; + of = kzalloc(sizeof(struct ovl_file), GFP_KERNEL); + if (!of) + return -ENOMEM; + realfile = ovl_open_realfile(file, &realpath); - if (IS_ERR(realfile)) + if (IS_ERR(realfile)) { + kfree(of); return PTR_ERR(realfile); + } - file->private_data = realfile; + of->realfile = realfile; + file->private_data = of; return 0; } static int ovl_release(struct inode *inode, struct file *file) { - fput(file->private_data); + struct ovl_file *of = file->private_data; + + fput(of->realfile); + kfree(of); return 0; } @@ -427,14 +443,14 @@ static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) static int ovl_mmap(struct file *file, struct vm_area_struct *vma) { - struct file *realfile = file->private_data; + struct ovl_file *of = file->private_data; struct backing_file_ctx ctx = { .cred = ovl_creds(file_inode(file)->i_sb), .user_file = file, .accessed = ovl_file_accessed, }; - return backing_file_mmap(realfile, vma, &ctx); + return backing_file_mmap(of->realfile, vma, &ctx); } static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) From patchwork Mon Oct 7 14:19:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 13824761 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64BAF1D8DEC; Mon, 7 Oct 2024 14:19:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310776; cv=none; b=luCYsVZ4AnPYagwzms9EmaK+cRPKbtbbtZTZ7WR9ohILdxRMCR0Am3HkAu7j+2WFz5iIIpTpSEXO3fqtZXo2A3tvJZT6vqkBJ+vJyoshUNziQh8AUiaFrlEUHahN1Pa+ziKxeJre6+Me35vhCiZQ+4q8O+DhHVtx0pZNKHlGIGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310776; c=relaxed/simple; bh=J2P5IiHrUlxURdsqJ0nNZ62+0PHUNMZc9zVb0F22A1k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ae9mqQfYQnxHzUtaGDX2LY2F9gWFHgzI7vpExPS+yD6mIlj25Z9+B/TYwP+SS98kqg88idlMfXStVjenNi5dIcYii+KVYuN8m0TMcyENto2fG35mrRYmMEGKCwOSaks9ybzRZdd1Q5ZqA73hNeizZ/CC9C8zvH6+SYuQNrn8RuY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PTr8Hvce; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PTr8Hvce" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-42f6bec84b5so46681525e9.1; Mon, 07 Oct 2024 07:19:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728310773; x=1728915573; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=K5pl2ROicOhNAoVpZjAYZikcTP1+nppS1IdoYmg8e5Y=; b=PTr8HvcelcCWFRHa1GaqLjxSzRz9iwFzsgfUMkkM/HYijnW9zJqHIUxI5/bbnCAB5m ZFeC3ePXMIf1AJdA0WJEISh11FDjgd/GB3CIHSUQ6XaRDXau1AImixElyufoiXq5TY6h SuzMXFGOn8vwZrgmC/jtDasIgUt+DpkdyqKQxRr0LOVFad+GKyYu9GSCpcy0wDt9lMUH dLR9PgMssuG/5/1KwgMsOYErPuuFDEoVSLU/G8qFdXRKq4qgkyq3uGE3eja+xqWsgCwv b6G4F+mTgdkYi1n5Hcl3KmzBE18M5XYnddLHk7qV2wA4UJq3PlS5FK1f8TWs+JBWyytL bxSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728310773; x=1728915573; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=K5pl2ROicOhNAoVpZjAYZikcTP1+nppS1IdoYmg8e5Y=; b=Bq0eqG0vo9OEcDNFOBDDRgD42qMUSiUx9JMZg6EgSLJnMIEW7R0jkMIzhQfarna2zc SF1VaOVO8AtrMmIECoK2rSuhKTQZ3Y1J4csE92OkMfaYE82aHqmt58nRWcuQtCRxn3XG O6qxxs/g6MSTO+6+OX1x+ZTOdG6KkPECvpXmreY95IPAcUeofDUGr6UoRZD18YTBdPM/ jqn6MsMJxKM/hLS3ZhKOc9//d8crK7YwCyHhBCzA1tDivvKbLP5jRA2F6HhaVOPW9Rmu C9tPVsdTtEeAj3uNivlIcTe1R1nn4mPFWcaZEj5VA7ZLln70TWZFECHCV4aRUNCXQ+F5 J1Xg== X-Forwarded-Encrypted: i=1; AJvYcCVNYyrEZ5LkX2RPAp6sNPq5HcRLR3YiZYb+ZmlVDP/9/7J4P1maCyqutZmhNxw0+7Tg1mJ4GVSXTgYaeKfOOg==@vger.kernel.org, AJvYcCXN+S9FxPLyKRRBKqNLAVHHV8tlF7IilVf+WKvJBEpXTv9ztv0nsWX0aKF1THTtU84lEqgI7UVe5F0UkWiK@vger.kernel.org X-Gm-Message-State: AOJu0YzLRU/iibW/+BZW9MXCLnIJ/PXLeQeEE4u2jxD/J+FWMu3M7Gou O/g6mNcDu9JwySoX0g7DnpwdTC1B+ot19lKy27de+EFWjbEduGZy X-Google-Smtp-Source: AGHT+IEH16bvwIy00udG2k+69iE/yqFxITAt47WoPiPNyMmmaDYZm+128yrq5+B8sUVyqXHgRT0NlQ== X-Received: by 2002:adf:f588:0:b0:37c:ce58:5a1a with SMTP id ffacd0b85a97d-37d0e8e0415mr5918662f8f.54.1728310772238; Mon, 07 Oct 2024 07:19:32 -0700 (PDT) Received: from amir-ThinkPad-T480.arnhem.chello.nl (92-109-99-123.cable.dynamic.v4.ziggo.nl. [92.109.99.123]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d16970520sm5829396f8f.96.2024.10.07.07.19.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 07:19:31 -0700 (PDT) From: Amir Goldstein To: Miklos Szeredi , Al Viro Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org Subject: [PATCH v3 3/5] ovl: store upper real file in ovl_file struct Date: Mon, 7 Oct 2024 16:19:23 +0200 Message-Id: <20241007141925.327055-4-amir73il@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241007141925.327055-1-amir73il@gmail.com> References: <20241007141925.327055-1-amir73il@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When an overlayfs file is opened as lower and then the file is copied up, every operation on the overlayfs open file will open a temporary backing file to the upper dentry and close it at the end of the operation. Store the upper real file along side the original (lower) real file in ovl_file instead of opening a temporary upper file on every operation. Signed-off-by: Amir Goldstein --- fs/overlayfs/file.c | 49 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index 03bf6037b129..525bcddb49e5 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -91,32 +91,63 @@ static int ovl_change_flags(struct file *file, unsigned int flags) struct ovl_file { struct file *realfile; + struct file *upperfile; }; +static bool ovl_is_real_file(const struct file *realfile, + const struct path *realpath) +{ + return file_inode(realfile) == d_inode(realpath->dentry); +} + static int ovl_real_fdget_path(const struct file *file, struct fd *real, struct path *realpath) { struct ovl_file *of = file->private_data; struct file *realfile = of->realfile; - real->word = (unsigned long)realfile; + real->word = 0; if (WARN_ON_ONCE(!realpath->dentry)) return -EIO; - /* Has it been copied up since we'd opened it? */ - if (unlikely(file_inode(realfile) != d_inode(realpath->dentry))) { - struct file *f = ovl_open_realfile(file, realpath); - if (IS_ERR(f)) - return PTR_ERR(f); - real->word = (unsigned long)f | FDPUT_FPUT; - return 0; + /* + * If the realfile that we want is not where the data used to be at + * open time, either we'd been copied up, or it's an fsync of a + * metacopied file. We need the upperfile either way, so see if it + * is already opened and if it is not then open and store it. + */ + if (unlikely(!ovl_is_real_file(realfile, realpath))) { + struct file *upperfile = READ_ONCE(of->upperfile); + struct file *old; + + if (!upperfile) { /* Nobody opened upperfile yet */ + upperfile = ovl_open_realfile(file, realpath); + if (IS_ERR(upperfile)) + return PTR_ERR(upperfile); + + /* Store the upperfile for later */ + old = cmpxchg_release(&of->upperfile, NULL, upperfile); + if (old) { /* Someone opened upperfile before us */ + fput(upperfile); + upperfile = old; + } + } + /* + * Stored file must be from the right inode, unless someone's + * been corrupting the upper layer. + */ + if (WARN_ON_ONCE(!ovl_is_real_file(upperfile, realpath))) + return -EIO; + + realfile = upperfile; } /* Did the flags change since open? */ if (unlikely((file->f_flags ^ realfile->f_flags) & ~OVL_OPEN_FLAGS)) return ovl_change_flags(realfile, file->f_flags); + real->word = (unsigned long)realfile; return 0; } @@ -208,6 +239,8 @@ static int ovl_release(struct inode *inode, struct file *file) struct ovl_file *of = file->private_data; fput(of->realfile); + if (of->upperfile) + fput(of->upperfile); kfree(of); return 0; From patchwork Mon Oct 7 14:19:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 13824762 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30CE31D8DFB; Mon, 7 Oct 2024 14:19:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310777; cv=none; b=SOynv0UQwkslzEnSTwd2nUGZeqjLMH5nwCxvLZTMvRaHo42PEkA+Clc9jS5ZYE1jra+YiSfMk6YsRKSZxLZYSPdQm9KxJdTymO71LWc97khJAnfdxeVXhw5vs1/SmsxnvpP8tn0/fKsZgzOXqadtxW3syWMSgsV6WbvEV/UfWgs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310777; c=relaxed/simple; bh=ShLWCnLy37MvRFlXnqMMASukN/oTTXfwXOi0kGFCvYc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iHBbipuaEVv5REG/RCMzSS/IxNTTW6fzTlJtQ9erJFH9v+mppRDKAxBdHpuWbq5FXPGawXooz8UO7CCaiZKH42RkkMCi47fGomIrgjB5Fl1j3OvNh6Aq7Rix0Ujr1YD676il1QyZR3rJdghzvrmgly0npQCkrjO9mGNdXTY4g9w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=iVNLE7iJ; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iVNLE7iJ" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-37cdbcb139cso3535327f8f.1; Mon, 07 Oct 2024 07:19:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728310773; x=1728915573; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=w3aPZorWQGjj1ymVuXXU2EaIpRVEVNJmdpk026ZQD5M=; b=iVNLE7iJTsDfSBGqjZfBd7+o6ju6q+ZHiqY0IFazJMlpoJtWXFykHrbwnonYh/9s4e wdeZm+F3tp8M9gcPA9QBmKlQzB0sEishYKarVBRKFvJZFyGZYMEOtHBTEQsdNc582CCo 2M4nEPu1/TfW5uMwT4wZaX/xcfSEX+8n720tyG23kYPi8wXwUKNeGDVTbksb6qx5Nrvb l3vk1al0WAeNlX/CEKzsmaPFfRFeafKY70ClH79CBfRS+ejvg02Cr9vsIdgTKDCPx9Hq 5BNXu9DUrN1xmcDa0mrP38CgNsArTrjYwT1ZKCbK1huWeiupbbF/xiKJMNvvFpqyAwOo INHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728310773; x=1728915573; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=w3aPZorWQGjj1ymVuXXU2EaIpRVEVNJmdpk026ZQD5M=; b=aXJuP8COBF/HXbTEiRdC0MyrAAO9t2LTXuHC3YbtC8ecqTYxjTsXIxsmcpyt5Sue5z oaMkuzeQ+EhgD1NfVd+8zKhhC0uqopNNJ07cSLCNS3czmyqu+JZR4CA7VGPhqVsxW3/D 6OXmaWCXFVQAqoFGNPlRs6unpYnAvo9iCufcIrsoRvM2LQznyANGwuMspUWBCHs017By gFwLifUfj9VXxbp55ETguMNdYb4PPc2FcUzFEqsIEiMQgJE1S/1uEB1ljypGFQUcxgW2 8tsNYXUyDU2zRI55Vwcnjr94Y+DIavjWhD2oaD+kIanp3F0lCqfQKVSY8atOlXUjkWOH IgWA== X-Forwarded-Encrypted: i=1; AJvYcCVg5tc9KdS/siZLtYoSdD0lVrFV8ohQ5hOHLi2747/1X8gQvC7XX0T8cQIQNaLNnDH2+TWTWW5YgsFp1mvq@vger.kernel.org, AJvYcCXLHxFLafWpSobq/pRVwMFPgOwXVboLP8JhAl9V93AwDpz0xg0WGtDLfrsXNk40hn1CJ9I4xTWhqI7hNVfsSw==@vger.kernel.org X-Gm-Message-State: AOJu0YxM26KiLl7MrMlk1OBrujzsJAGAnIbO1wiEALQ/mTp1+xxQCJ/r t11ggNkVRfl112dQZk1aqNAggG3KoRPrMftY+ndmw3G5lMnE2Czx X-Google-Smtp-Source: AGHT+IFFCLNMa7ozCLRyuQwffwAl/1LKN9IarPzd4fugtBrwPtOc+bNIHnX9YVRivQldcUJnydmx/Q== X-Received: by 2002:adf:f987:0:b0:374:cbe8:6f43 with SMTP id ffacd0b85a97d-37d0e7902b2mr6953417f8f.33.1728310773065; Mon, 07 Oct 2024 07:19:33 -0700 (PDT) Received: from amir-ThinkPad-T480.arnhem.chello.nl (92-109-99-123.cable.dynamic.v4.ziggo.nl. [92.109.99.123]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d16970520sm5829396f8f.96.2024.10.07.07.19.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 07:19:32 -0700 (PDT) From: Amir Goldstein To: Miklos Szeredi , Al Viro Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org Subject: [PATCH v3 4/5] ovl: convert ovl_real_fdget_path() callers to ovl_real_file_path() Date: Mon, 7 Oct 2024 16:19:24 +0200 Message-Id: <20241007141925.327055-5-amir73il@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241007141925.327055-1-amir73il@gmail.com> References: <20241007141925.327055-1-amir73il@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Stop using struct fd to return a real file from ovl_real_fdget_path(), because we no longer return a temporary file object and the callers always get a borrowed file reference. Rename the helper to ovl_real_file_path(), return a borrowed reference of the real file that is referenced from the overlayfs file or an error. Signed-off-by: Amir Goldstein --- fs/overlayfs/file.c | 66 +++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index 525bcddb49e5..b6d6ccc39dad 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -100,16 +100,14 @@ static bool ovl_is_real_file(const struct file *realfile, return file_inode(realfile) == d_inode(realpath->dentry); } -static int ovl_real_fdget_path(const struct file *file, struct fd *real, - struct path *realpath) +static struct file *ovl_real_file_path(const struct file *file, + struct path *realpath) { struct ovl_file *of = file->private_data; struct file *realfile = of->realfile; - real->word = 0; - if (WARN_ON_ONCE(!realpath->dentry)) - return -EIO; + return ERR_PTR(-EIO); /* * If the realfile that we want is not where the data used to be at @@ -124,7 +122,7 @@ static int ovl_real_fdget_path(const struct file *file, struct fd *real, if (!upperfile) { /* Nobody opened upperfile yet */ upperfile = ovl_open_realfile(file, realpath); if (IS_ERR(upperfile)) - return PTR_ERR(upperfile); + return upperfile; /* Store the upperfile for later */ old = cmpxchg_release(&of->upperfile, NULL, upperfile); @@ -138,20 +136,23 @@ static int ovl_real_fdget_path(const struct file *file, struct fd *real, * been corrupting the upper layer. */ if (WARN_ON_ONCE(!ovl_is_real_file(upperfile, realpath))) - return -EIO; + return ERR_PTR(-EIO); realfile = upperfile; } /* Did the flags change since open? */ - if (unlikely((file->f_flags ^ realfile->f_flags) & ~OVL_OPEN_FLAGS)) - return ovl_change_flags(realfile, file->f_flags); + if (unlikely((file->f_flags ^ realfile->f_flags) & ~OVL_OPEN_FLAGS)) { + int err = ovl_change_flags(realfile, file->f_flags); - real->word = (unsigned long)realfile; - return 0; + if (err) + return ERR_PTR(err); + } + + return realfile; } -static int ovl_real_fdget(const struct file *file, struct fd *real) +static struct file *ovl_real_file(const struct file *file) { struct dentry *dentry = file_dentry(file); struct path realpath; @@ -159,23 +160,33 @@ static int ovl_real_fdget(const struct file *file, struct fd *real) if (d_is_dir(dentry)) { struct file *f = ovl_dir_real_file(file, false); - if (IS_ERR(f)) - return PTR_ERR(f); - real->word = (unsigned long)f; - return 0; + + if (WARN_ON_ONCE(!f)) + return ERR_PTR(-EIO); + return f; } /* lazy lookup and verify of lowerdata */ err = ovl_verify_lowerdata(dentry); if (err) - return err; + return ERR_PTR(err); ovl_path_realdata(dentry, &realpath); - return ovl_real_fdget_path(file, real, &realpath); + return ovl_real_file_path(file, &realpath); } -static int ovl_upper_fdget(const struct file *file, struct fd *real, bool data) +static int ovl_real_fdget(const struct file *file, struct fd *real) +{ + struct file *f = ovl_real_file(file); + + if (IS_ERR(f)) + return PTR_ERR(f); + real->word = (unsigned long)f; + return 0; +} + +static struct file *ovl_upper_file(const struct file *file, bool data) { struct dentry *dentry = file_dentry(file); struct path realpath; @@ -186,12 +197,11 @@ static int ovl_upper_fdget(const struct file *file, struct fd *real, bool data) else type = ovl_path_real(dentry, &realpath); - real->word = 0; /* Not interested in lower nor in upper meta if data was requested */ if (!OVL_TYPE_UPPER(type) || (data && OVL_TYPE_MERGE(type))) - return 0; + return NULL; - return ovl_real_fdget_path(file, real, &realpath); + return ovl_real_file_path(file, &realpath); } static int ovl_open(struct inode *inode, struct file *file) @@ -452,7 +462,7 @@ static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) { - struct fd real; + struct file *upperfile; const struct cred *old_cred; int ret; @@ -461,16 +471,14 @@ static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) return ret; /* Don't sync lower file for fear of receiving EROFS error */ - ret = ovl_upper_fdget(file, &real, datasync); - if (ret || fd_empty(real)) - return ret; + upperfile = ovl_upper_file(file, datasync); + if (IS_ERR_OR_NULL(upperfile)) + return PTR_ERR(upperfile); old_cred = ovl_override_creds(file_inode(file)->i_sb); - ret = vfs_fsync_range(fd_file(real), start, end, datasync); + ret = vfs_fsync_range(upperfile, start, end, datasync); revert_creds(old_cred); - fdput(real); - return ret; } From patchwork Mon Oct 7 14:19:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 13824763 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 299EB1D8E12; Mon, 7 Oct 2024 14:19:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310778; cv=none; b=qF6C7VtAfmGbVzoa7PnWWMhA7BmoDKto8YjFGksbcTccFqpgw/kP3cNXmZ0VIqqc8gtr27dpB9+1lKiDxNAOQoDBFsKvpWBwjPyMvIfXUmw03suhM8LeTjS1EYZJ35IyNgr1rUXJo0Rsw2nrYrvXMrpZ4pFmE3IQZWY7Pk+TfTI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310778; c=relaxed/simple; bh=uOc7LgbeTozMzxPISY+P0F6t7+XoC/59xEXd0gSKaTQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=loh5N7jCdbynkCAHbvrE1m+VyhuErZtzvJ2qa1gGofX13m5NTCMeDZVlqELFdlrBp6wTO+LuaeQqeZ5xfzKE/f+RDt8Mpic/e4qZdOoC0IpQfbjgjLeHbajrGq58xFk1mYtd8AaBHJtr5shVTVEOTeJaSPZroJ6hCD6GDjiK83g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QmkYRRRs; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QmkYRRRs" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-42cb5b3c57eso46123425e9.2; Mon, 07 Oct 2024 07:19:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728310774; x=1728915574; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=l6AatFEIpl32Sf0JO7MLVCai4CToQOaM2lvYJFVBW/M=; b=QmkYRRRsFzrt2HQ6U+KYe+6IjZCwrVa9nCAwH35FVYF7AnEjdkizx61RopG7Mzwa0c gvmltM0ua7VnjEcpW7a7YEF5x1oFYqMHzRUT11To+CRqqdJv11fsncihEXAMLy14E7Ai E/o4x825DXAHZbBzDcKR3vi3hcSYlJkmuxabUj/I0lIGynrV5bSTllOIxK7BJ8vTGSN/ iJowQk6BS7T429BbMX+PpxbXVAfMs4dCFw2tri8BYry5FWoSDjDz4ZqLe1U8xoov7tJP MIUOrLoOdvvTNv1eBS1h598/36FYl8LbXRaJnCDBCv2zvjSjCU+DYwVhorntX+RDQvZK MQAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728310774; x=1728915574; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=l6AatFEIpl32Sf0JO7MLVCai4CToQOaM2lvYJFVBW/M=; b=Q7wYwlL+pkw7dY9V+l2qn5qHvGVPOxj/U09UW6pZ4henPCGZxhsjygVIDpcZzS+0pK 7jo+o/hvyS9D+VfhMEAChszbg33YeMVAcb7vbuIhhevwc9zu2DdngfjiG80XCxZJ1rql o4iugHwe5YsPeknXzSce+C3kEh6gWQVq+hIMj2Ak+mBxFy4znLj+IPu3gCM5vc6Hqe7o vHMRIAuGo0lyeL9pbVmpcwfJPmof/h46+sSVMFWvwYZyAVjT4Lrlrz6Br4YeKcVIb1+6 npan76ynzr6tYWKLK8pJrIT9FdQEzJchmOiaJtOJJB2/WBnfvCxRQE/ho77T8OtjfBtw y+xQ== X-Forwarded-Encrypted: i=1; AJvYcCUhvdS6cHhBf9/PwEuDYzbJZi/RSYxbpEJaOUf8O0lkC/itAsoYsSd0Kvj/b5TTUgSZO6LubxP/07X8bQjw@vger.kernel.org, AJvYcCUvFRT1LhL4M9xtV4o+10vEL+owuqHLBQQkp56hsoPMvgY+FHHl53reEan3uKmXFVGs9g7yR0JQCMj4seyY/w==@vger.kernel.org X-Gm-Message-State: AOJu0YxP3IrZ5iUWtgsWhazUzve8ykjJpKredabQ2xG8eqoz1oSMRqrg b9Z0ZoizNgSy1AGqMWZikX+qSk7rgpFjWQKmkzijaT87qpUhl7z2 X-Google-Smtp-Source: AGHT+IEzrJ5onqsSRhtq2z/x8MzpBInyLvLW3tEHtP6swCEEnm2BRyl6KFRx2hW98rG8i5bC2Ajl+A== X-Received: by 2002:a05:600c:46c6:b0:42c:e0da:f15c with SMTP id 5b1f17b1804b1-42f85ab8695mr82976565e9.20.1728310773960; Mon, 07 Oct 2024 07:19:33 -0700 (PDT) Received: from amir-ThinkPad-T480.arnhem.chello.nl (92-109-99-123.cable.dynamic.v4.ziggo.nl. [92.109.99.123]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37d16970520sm5829396f8f.96.2024.10.07.07.19.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 07:19:33 -0700 (PDT) From: Amir Goldstein To: Miklos Szeredi , Al Viro Cc: Christian Brauner , linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org Subject: [PATCH v3 5/5] ovl: convert ovl_real_fdget() callers to ovl_real_file() Date: Mon, 7 Oct 2024 16:19:25 +0200 Message-Id: <20241007141925.327055-6-amir73il@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241007141925.327055-1-amir73il@gmail.com> References: <20241007141925.327055-1-amir73il@gmail.com> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Stop using struct fd to return a real file from ovl_real_fdget(), because we no longer return a temporary file object and the callers always get a borrowed file reference. Rename the helper to ovl_real_file(), return a borrowed reference of the real file that is referenced from the overlayfs file or an error. Signed-off-by: Amir Goldstein --- fs/overlayfs/file.c | 145 ++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 86 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index b6d6ccc39dad..b7bec2adb575 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -176,16 +176,6 @@ static struct file *ovl_real_file(const struct file *file) return ovl_real_file_path(file, &realpath); } -static int ovl_real_fdget(const struct file *file, struct fd *real) -{ - struct file *f = ovl_real_file(file); - - if (IS_ERR(f)) - return PTR_ERR(f); - real->word = (unsigned long)f; - return 0; -} - static struct file *ovl_upper_file(const struct file *file, bool data) { struct dentry *dentry = file_dentry(file); @@ -259,7 +249,7 @@ static int ovl_release(struct inode *inode, struct file *file) static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) { struct inode *inode = file_inode(file); - struct fd real; + struct file *realfile; const struct cred *old_cred; loff_t ret; @@ -275,9 +265,9 @@ static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, 0, 0); } - ret = ovl_real_fdget(file, &real); - if (ret) - return ret; + realfile = ovl_real_file(file); + if (IS_ERR(realfile)) + return PTR_ERR(realfile); /* * Overlay file f_pos is the master copy that is preserved @@ -287,17 +277,15 @@ static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) * files, so we use the real file to perform seeks. */ ovl_inode_lock(inode); - fd_file(real)->f_pos = file->f_pos; + realfile->f_pos = file->f_pos; old_cred = ovl_override_creds(inode->i_sb); - ret = vfs_llseek(fd_file(real), offset, whence); + ret = vfs_llseek(realfile, offset, whence); revert_creds(old_cred); - file->f_pos = fd_file(real)->f_pos; + file->f_pos = realfile->f_pos; ovl_inode_unlock(inode); - fdput(real); - return ret; } @@ -338,8 +326,7 @@ static void ovl_file_accessed(struct file *file) static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) { struct file *file = iocb->ki_filp; - struct fd real; - ssize_t ret; + struct file *realfile; struct backing_file_ctx ctx = { .cred = ovl_creds(file_inode(file)->i_sb), .user_file = file, @@ -349,22 +336,19 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) if (!iov_iter_count(iter)) return 0; - ret = ovl_real_fdget(file, &real); - if (ret) - return ret; - - ret = backing_file_read_iter(fd_file(real), iter, iocb, iocb->ki_flags, - &ctx); - fdput(real); + realfile = ovl_real_file(file); + if (IS_ERR(realfile)) + return PTR_ERR(realfile); - return ret; + return backing_file_read_iter(realfile, iter, iocb, iocb->ki_flags, + &ctx); } static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) { struct file *file = iocb->ki_filp; struct inode *inode = file_inode(file); - struct fd real; + struct file *realfile; ssize_t ret; int ifl = iocb->ki_flags; struct backing_file_ctx ctx = { @@ -380,8 +364,9 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) /* Update mode */ ovl_copyattr(inode); - ret = ovl_real_fdget(file, &real); - if (ret) + realfile = ovl_real_file(file); + ret = PTR_ERR(realfile); + if (IS_ERR(realfile)) goto out_unlock; if (!ovl_should_sync(OVL_FS(inode->i_sb))) @@ -392,8 +377,7 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) * this property in case it is set by the issuer. */ ifl &= ~IOCB_DIO_CALLER_COMP; - ret = backing_file_write_iter(fd_file(real), iter, iocb, ifl, &ctx); - fdput(real); + ret = backing_file_write_iter(realfile, iter, iocb, ifl, &ctx); out_unlock: inode_unlock(inode); @@ -405,28 +389,24 @@ static ssize_t ovl_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { - struct fd real; - ssize_t ret; + struct file *realfile; struct backing_file_ctx ctx = { .cred = ovl_creds(file_inode(in)->i_sb), .user_file = in, .accessed = ovl_file_accessed, }; - ret = ovl_real_fdget(in, &real); - if (ret) - return ret; - - ret = backing_file_splice_read(fd_file(real), ppos, pipe, len, flags, &ctx); - fdput(real); + realfile = ovl_real_file(in); + if (IS_ERR(realfile)) + return PTR_ERR(realfile); - return ret; + return backing_file_splice_read(realfile, ppos, pipe, len, flags, &ctx); } /* * Calling iter_file_splice_write() directly from overlay's f_op may deadlock * due to lock order inversion between pipe->mutex in iter_file_splice_write() - * and file_start_write(fd_file(real)) in ovl_write_iter(). + * and file_start_write(realfile) in ovl_write_iter(). * * So do everything ovl_write_iter() does and call iter_file_splice_write() on * the real file. @@ -434,7 +414,7 @@ static ssize_t ovl_splice_read(struct file *in, loff_t *ppos, static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags) { - struct fd real; + struct file *realfile; struct inode *inode = file_inode(out); ssize_t ret; struct backing_file_ctx ctx = { @@ -447,12 +427,12 @@ static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, /* Update mode */ ovl_copyattr(inode); - ret = ovl_real_fdget(out, &real); - if (ret) + realfile = ovl_real_file(out); + ret = PTR_ERR(realfile); + if (IS_ERR(realfile)) goto out_unlock; - ret = backing_file_splice_write(pipe, fd_file(real), ppos, len, flags, &ctx); - fdput(real); + ret = backing_file_splice_write(pipe, realfile, ppos, len, flags, &ctx); out_unlock: inode_unlock(inode); @@ -497,7 +477,7 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma) static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) { struct inode *inode = file_inode(file); - struct fd real; + struct file *realfile; const struct cred *old_cred; int ret; @@ -508,19 +488,18 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len if (ret) goto out_unlock; - ret = ovl_real_fdget(file, &real); - if (ret) + realfile = ovl_real_file(file); + ret = PTR_ERR(realfile); + if (IS_ERR(realfile)) goto out_unlock; old_cred = ovl_override_creds(file_inode(file)->i_sb); - ret = vfs_fallocate(fd_file(real), mode, offset, len); + ret = vfs_fallocate(realfile, mode, offset, len); revert_creds(old_cred); /* Update size */ ovl_file_modified(file); - fdput(real); - out_unlock: inode_unlock(inode); @@ -529,20 +508,18 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice) { - struct fd real; + struct file *realfile; const struct cred *old_cred; int ret; - ret = ovl_real_fdget(file, &real); - if (ret) - return ret; + realfile = ovl_real_file(file); + if (IS_ERR(realfile)) + return PTR_ERR(realfile); old_cred = ovl_override_creds(file_inode(file)->i_sb); - ret = vfs_fadvise(fd_file(real), offset, len, advice); + ret = vfs_fadvise(realfile, offset, len, advice); revert_creds(old_cred); - fdput(real); - return ret; } @@ -557,7 +534,7 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, loff_t len, unsigned int flags, enum ovl_copyop op) { struct inode *inode_out = file_inode(file_out); - struct fd real_in, real_out; + struct file *realfile_in, *realfile_out; const struct cred *old_cred; loff_t ret; @@ -570,31 +547,31 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, goto out_unlock; } - ret = ovl_real_fdget(file_out, &real_out); - if (ret) + realfile_out = ovl_real_file(file_out); + ret = PTR_ERR(realfile_out); + if (IS_ERR(realfile_out)) goto out_unlock; - ret = ovl_real_fdget(file_in, &real_in); - if (ret) { - fdput(real_out); + realfile_in = ovl_real_file(file_in); + ret = PTR_ERR(realfile_in); + if (IS_ERR(realfile_in)) goto out_unlock; - } old_cred = ovl_override_creds(file_inode(file_out)->i_sb); switch (op) { case OVL_COPY: - ret = vfs_copy_file_range(fd_file(real_in), pos_in, - fd_file(real_out), pos_out, len, flags); + ret = vfs_copy_file_range(realfile_in, pos_in, + realfile_out, pos_out, len, flags); break; case OVL_CLONE: - ret = vfs_clone_file_range(fd_file(real_in), pos_in, - fd_file(real_out), pos_out, len, flags); + ret = vfs_clone_file_range(realfile_in, pos_in, + realfile_out, pos_out, len, flags); break; case OVL_DEDUPE: - ret = vfs_dedupe_file_range_one(fd_file(real_in), pos_in, - fd_file(real_out), pos_out, len, + ret = vfs_dedupe_file_range_one(realfile_in, pos_in, + realfile_out, pos_out, len, flags); break; } @@ -603,9 +580,6 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, /* Update size */ ovl_file_modified(file_out); - fdput(real_in); - fdput(real_out); - out_unlock: inode_unlock(inode_out); @@ -649,20 +623,19 @@ static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in, static int ovl_flush(struct file *file, fl_owner_t id) { - struct fd real; + struct file *realfile; const struct cred *old_cred; - int err; + int err = 0; - err = ovl_real_fdget(file, &real); - if (err) - return err; + realfile = ovl_real_file(file); + if (IS_ERR(realfile)) + return PTR_ERR(realfile); - if (fd_file(real)->f_op->flush) { + if (realfile->f_op->flush) { old_cred = ovl_override_creds(file_inode(file)->i_sb); - err = fd_file(real)->f_op->flush(fd_file(real), id); + err = realfile->f_op->flush(realfile, id); revert_creds(old_cred); } - fdput(real); return err; }