Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2833867pxv; Mon, 12 Jul 2021 03:05:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzAbD+CAX6l46+q+q96rASDK2zLCuOu+x3XEDGYzCynl/JsLPd6xfgf8aQiVLXJkeHW808U X-Received: by 2002:a6b:4f08:: with SMTP id d8mr26541004iob.199.1626084339052; Mon, 12 Jul 2021 03:05:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626084339; cv=none; d=google.com; s=arc-20160816; b=LIUCpH1V5qf4EmgN2Vgh516IJMeAgzcRNBtOobYTQ/bgMA2vygN0ZOr0sIo+ZZBmhX JsBgTp3lzNOXBVGLQcQwnHjQLD08uLE9IZJSXx9XjEi+F4CLK0kKzqKPnSmBoTzR4Y0t 37G4B4EiFnBPkGyk5azjt/eh3Qbak4NmgrMS5Yw6Aml3uswfIEioWhbVQ2msLNXVqsX8 7EFqWh+qtOpGGlnvb6fkFgqOxt27h8GJvufziJ+m80DiHrgwKrGfmdRcu2YK3+VdDM/v 0+5FejMnMBjBnlPkwTyD+IEHcLfSG02O+uvJTB+3NRQBeeXsitsdArkDgojSyIzxH/iE 2z9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=72TECYyVTB6c3UAAynJPGSO1sx/0bH8V9M8BQvAZn04=; b=gfMeSNu1vDbMEiyRwVR2ORVK/u36oIIDRIOnduZyazNWln/ZtIOjqxYc4UVmrbHSrl C0bO2qjD3Ug4qTTbwYAlt2nnGDZ+xJxfWwVpnLy8JCVjeCTsKy/KkeLiIbKWBRK6unRa q5G6QG2rR6R/QokScMJcOe6VxQ2MairQPpKXR3mwuAS0sFznCqWkeQsefR6TxIfLDMox qFPJDEL4MMPhQI2K5jYuxc9TGhSGja64/2DujP41FtIhrW0jlRf6ggKPwiJS5RJs9Nbj YkaEEmYqxo48AYFoenhAhzoIGGyEM0MboooiIr1NW2zy+OBxBziJIZJzZ2mdG/+assbE jONQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UXwK3Jt3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n2si18628136jaj.36.2021.07.12.03.05.27; Mon, 12 Jul 2021 03:05:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UXwK3Jt3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347264AbhGLHen (ORCPT + 99 others); Mon, 12 Jul 2021 03:34:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:41186 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241961AbhGLHGJ (ORCPT ); Mon, 12 Jul 2021 03:06:09 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id ED5476143B; Mon, 12 Jul 2021 07:03:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626073400; bh=WPen+QcfEg5rD05vfZEKogtVnBkHoP+LMHTI+oXPI3s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UXwK3Jt3vL/4UsciCC46WDCAkwlpF8nVFDlrtElge/Uqyq7SXcKaToz3zkQ7V9fof Wlx88AUASSMYgxB61NIg7tHDCFQmUSFxm0ncZKCGt1zqh1rYPDCll4ZbtYkODfLLt1 PcEcnjkhgxkzhrlBjkR1h94eMeHbOuCcvWHNvY1M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, kernel test robot , Ronnie Sahlberg , Aurelien Aptel , "Paulo Alcantara (SUSE)" , Steve French , Sasha Levin Subject: [PATCH 5.12 230/700] cifs: improve fallocate emulation Date: Mon, 12 Jul 2021 08:05:13 +0200 Message-Id: <20210712060959.362105911@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060924.797321836@linuxfoundation.org> References: <20210712060924.797321836@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ronnie Sahlberg [ Upstream commit 966a3cb7c7db786452a87afdc3b48858fc4d4d6b ] RHBZ: 1866684 We don't have a real fallocate in the SMB2 protocol so we used to emulate fallocate by simply switching the file to become non-sparse. But as that could potantially consume a lot more data than we intended to fallocate (large sparse file and fallocating a thin slice in the middle) we would only do this IFF the fallocate request was for virtually the entire file. This patch improves this and starts allowing us to fallocate smaller chunks of a file by overwriting the region with 0, for the parts that are unallocated. The method used is to first query the server for FSCTL_QUERY_ALLOCATED_RANGES to find what is unallocated in the fallocate range and then to only overwrite-with-zero the unallocated ranges to fill in the holes. As overwriting-with-zero is different from just allocating blocks, and potentially much more expensive, we limit this to only allow fallocate ranges up to 1Mb in size. Reported-by: kernel test robot Signed-off-by: Ronnie Sahlberg Acked-by: Aurelien Aptel Acked-by: Paulo Alcantara (SUSE) Signed-off-by: Steve French Signed-off-by: Sasha Levin --- fs/cifs/smb2ops.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/fs/cifs/smb2ops.c b/fs/cifs/smb2ops.c index e9a530da4255..ea5e958fd6b0 100644 --- a/fs/cifs/smb2ops.c +++ b/fs/cifs/smb2ops.c @@ -3561,6 +3561,119 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon, return rc; } +static int smb3_simple_fallocate_write_range(unsigned int xid, + struct cifs_tcon *tcon, + struct cifsFileInfo *cfile, + loff_t off, loff_t len, + char *buf) +{ + struct cifs_io_parms io_parms = {0}; + int nbytes; + struct kvec iov[2]; + + io_parms.netfid = cfile->fid.netfid; + io_parms.pid = current->tgid; + io_parms.tcon = tcon; + io_parms.persistent_fid = cfile->fid.persistent_fid; + io_parms.volatile_fid = cfile->fid.volatile_fid; + io_parms.offset = off; + io_parms.length = len; + + /* iov[0] is reserved for smb header */ + iov[1].iov_base = buf; + iov[1].iov_len = io_parms.length; + return SMB2_write(xid, &io_parms, &nbytes, iov, 1); +} + +static int smb3_simple_fallocate_range(unsigned int xid, + struct cifs_tcon *tcon, + struct cifsFileInfo *cfile, + loff_t off, loff_t len) +{ + struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data; + u32 out_data_len; + char *buf = NULL; + loff_t l; + int rc; + + in_data.file_offset = cpu_to_le64(off); + in_data.length = cpu_to_le64(len); + rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid, + cfile->fid.volatile_fid, + FSCTL_QUERY_ALLOCATED_RANGES, true, + (char *)&in_data, sizeof(in_data), + 1024 * sizeof(struct file_allocated_range_buffer), + (char **)&out_data, &out_data_len); + if (rc) + goto out; + /* + * It is already all allocated + */ + if (out_data_len == 0) + goto out; + + buf = kzalloc(1024 * 1024, GFP_KERNEL); + if (buf == NULL) { + rc = -ENOMEM; + goto out; + } + + tmp_data = out_data; + while (len) { + /* + * The rest of the region is unmapped so write it all. + */ + if (out_data_len == 0) { + rc = smb3_simple_fallocate_write_range(xid, tcon, + cfile, off, len, buf); + goto out; + } + + if (out_data_len < sizeof(struct file_allocated_range_buffer)) { + rc = -EINVAL; + goto out; + } + + if (off < le64_to_cpu(tmp_data->file_offset)) { + /* + * We are at a hole. Write until the end of the region + * or until the next allocated data, + * whichever comes next. + */ + l = le64_to_cpu(tmp_data->file_offset) - off; + if (len < l) + l = len; + rc = smb3_simple_fallocate_write_range(xid, tcon, + cfile, off, l, buf); + if (rc) + goto out; + off = off + l; + len = len - l; + if (len == 0) + goto out; + } + /* + * We are at a section of allocated data, just skip forward + * until the end of the data or the end of the region + * we are supposed to fallocate, whichever comes first. + */ + l = le64_to_cpu(tmp_data->length); + if (len < l) + l = len; + off += l; + len -= l; + + tmp_data = &tmp_data[1]; + out_data_len -= sizeof(struct file_allocated_range_buffer); + } + + out: + kfree(out_data); + kfree(buf); + return rc; +} + + static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon, loff_t off, loff_t len, bool keep_size) { @@ -3621,6 +3734,26 @@ static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon, } if ((keep_size == true) || (i_size_read(inode) >= off + len)) { + /* + * At this point, we are trying to fallocate an internal + * regions of a sparse file. Since smb2 does not have a + * fallocate command we have two otions on how to emulate this. + * We can either turn the entire file to become non-sparse + * which we only do if the fallocate is for virtually + * the whole file, or we can overwrite the region with zeroes + * using SMB2_write, which could be prohibitevly expensive + * if len is large. + */ + /* + * We are only trying to fallocate a small region so + * just write it with zero. + */ + if (len <= 1024 * 1024) { + rc = smb3_simple_fallocate_range(xid, tcon, cfile, + off, len); + goto out; + } + /* * Check if falloc starts within first few pages of file * and ends within a few pages of the end of file to -- 2.30.2