Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp631298pxb; Thu, 31 Mar 2022 13:26:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8xtGbPA8di2IhnLBnI6R5vrZ6hDu05HwvDjN82dWpIr3RBya8omr4I5FPlDdzHYdcEE3z X-Received: by 2002:a05:6a00:190a:b0:4fa:e4e9:7126 with SMTP id y10-20020a056a00190a00b004fae4e97126mr40982049pfi.65.1648758366901; Thu, 31 Mar 2022 13:26:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648758366; cv=none; d=google.com; s=arc-20160816; b=p79DMbYkq0hjcRDrXXR4MSeDbeDIuYcnKvRDc8j6+6Sv4+71Uj8KezsenMspidpnBM MgMrIWW+uqzRHnoXE6RqgD3ZnW+k2OXCBvtwhKxTYH8jbd8hiJPll7baDtlje5FbF6E+ jeDRRXaBckeh0STaLBZXERkG8+RF2oBIszx2I02guVd1KicgqvW/Pl0/9XNghi62v+hZ bLi7vMa3TEk1cnCbA2Vj6ASBbL5fkDiTn1GcXpJbgt/zXeMKlPUIKXGQEAkJzD5naJwp 5kq2mupUUq4Cwkk8yKkExHEgzpxkmcfPmDysgrO2ihswwjpVLMP5FZNfX3jteuVPdDFc Q6nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :from:references:cc:to:content-language:user-agent:mime-version:date :message-id:dkim-signature; bh=YsjSmrG2rvYs50asz/taC5lCuB0Ss1bBZU3bReqw5is=; b=uE3YMpWUiPftn5Gl9B1JjRAkAg6BeJFa6/DQBzanl4AzwdPLrd93Q3uPVYFauLIWT/ MJkpbtrYh1gU2Y9CfpJEMSZ8pc8VFM1zI4R+TJ5IjbxJpb+/N/auy0tPaksBhACWF09B Xrs6xgKs4gU/DTshSiklnHY9N9Y5G9G/VZlTOLCWZagIh0w5BwVrhMdqbqQ295/8I3r/ niMqCWcwZdKGITpJTLxiugRuvlNfYA3z/5z/XuUAFP3cIGBJJv1QuPMWmP9lLFYRc4eS 4py8vc65dKiiucCO+p6QaDVlh3IbZxynwz8Msx5WQ8QfEHzb2c/UmhcaMRcIOwplF+5W UQ3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PouxUIYk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j35-20020a635523000000b0038653b8ab83si537842pgb.204.2022.03.31.13.25.49; Thu, 31 Mar 2022 13:26:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PouxUIYk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232671AbiCaSFw (ORCPT + 99 others); Thu, 31 Mar 2022 14:05:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233302AbiCaSFv (ORCPT ); Thu, 31 Mar 2022 14:05:51 -0400 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3F2B5372F for ; Thu, 31 Mar 2022 11:04:01 -0700 (PDT) Received: by mail-pg1-x52f.google.com with SMTP id w21so398561pgm.7 for ; Thu, 31 Mar 2022 11:04:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=message-id:date:mime-version:user-agent:content-language:to:cc :references:from:subject:in-reply-to:content-transfer-encoding; bh=YsjSmrG2rvYs50asz/taC5lCuB0Ss1bBZU3bReqw5is=; b=PouxUIYkBVuDtFYYhzJKLOVGEietOc0cr9qJm2EvDslOhzSw9jON2ICmYF6t9N5/6d UeUa90pA/MozGMKak7kSYV0mXp43DAEGF2q7tXAYxPBwHknQABhfGpVQXvnSm8QSTt/n Eok9vzYTaPRKddP8OxP4oaKDkzdCIH7LL/RmyFAUWgF7jsxDViqNjjb7WbU/DZKVxRtG fN7/0u+gUYr79gPq5Rro0aSzY01+eXTZQ0BBw8iACDlZqN9rtqzBBl4iJe/qiOdvTHpe ywKFz8znw4luKvpXbOv6m6ae8EobDnAtXSAX89rWDWgXE8ZobRk7ujXauAwBCNcXwkU9 yg7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:cc:references:from:subject:in-reply-to :content-transfer-encoding; bh=YsjSmrG2rvYs50asz/taC5lCuB0Ss1bBZU3bReqw5is=; b=Eb0hizsVugYL294ELFyfqCggvPrzPfEPKNgQaluS3x0v6MTeBJHHmoSy6knIjU3UIK hssK3zc4Hd8LbLaCLJUnOZumZjHhlOR72Trfd7zE35Qby1IqcG/1DPXhNksSAk2SVFPk TZ6M0XIz17KsFafI6ax1r33ONZgnGa+zw7orY9LdMpaJdcs+bBkMmDKgTa9kP2KXCDec jeB1JXU2EqBPJruvc6sqZCxrN3/W8+c/tZvP5qW/KgUbE5E+UKhEAbVGZXdXl1ABILDh vnR8Iu0ffRQliHyyotd8ZCXbQbjdzc5YUO9Xd7AaSO2biNiYRgO+gRQ9O1kUK5QYzs4f PrMg== X-Gm-Message-State: AOAM53079MIbtBOlgwlkVkq8lDICgeMs+fDKxKOxvmqjoRLa8vdQalDw Y5Oc90fsSnsZPafSXf0cnJeb8Q== X-Received: by 2002:a65:6d15:0:b0:382:4e6d:dd0d with SMTP id bf21-20020a656d15000000b003824e6ddd0dmr11580133pgb.333.1648749841056; Thu, 31 Mar 2022 11:04:01 -0700 (PDT) Received: from [192.168.254.17] ([50.39.160.154]) by smtp.gmail.com with ESMTPSA id oc10-20020a17090b1c0a00b001c7510ed0c8sm11299522pjb.49.2022.03.31.11.03.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 31 Mar 2022 11:04:00 -0700 (PDT) Message-ID: <39b76458-17e6-4e04-15d8-1445d2067d0c@linaro.org> Date: Thu, 31 Mar 2022 11:03:59 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Content-Language: en-US To: Theodore Ts'o Cc: Andreas Dilger , Ritesh Harjani , linux-ext4@vger.kernel.org, stable@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+7a806094edd5d07ba029@syzkaller.appspotmail.com References: <20220315215439.269122-1-tadeusz.struk@linaro.org> From: Tadeusz Struk Subject: Re: [PATCH v2] ext4: check if offset+length is valid in fallocate In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 3/31/22 07:43, Theodore Ts'o wrote: > On Tue, Mar 15, 2022 at 02:54:39PM -0700, Tadeusz Struk wrote: >> @@ -3967,6 +3968,16 @@ int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length) >> offset; >> } >> >> + /* >> + * For punch hole the length + offset needs to be at least within >> + * one block before last >> + */ >> + max_length = sbi->s_bitmap_maxbytes - inode->i_sb->s_blocksize; >> + if (offset + length >= max_length) { >> + ret = -ENOSPC; >> + goto out_mutex; >> + } > > I wonder if we would be better off just simply capping length to > max_length? If length is set to some large value, such as LONG_MAX, > it's pretty clear what the intention should be, which is to simply do > the equivalent of truncating the file at offset. Perhaps we should > just do that? Don't think that would be the correct behavior. ftrucnate (or truncate) modify the file size, but fallocate with FALLOC_FL_PUNCH_HOLE should not. man 2 fallocate says: "... The FALLOC_FL_PUNCH_HOLE flag must be ORed with FALLOC_FL_KEEP_SIZE in mode; in other words, even when punching off the end of the file, the file size (as reported by stat(2)) does not change. " that is enforced by vfs: https://elixir.bootlin.com/linux/v5.17.1/source/fs/open.c#L245 > > That being said, we should be consistent with what other file systems > do when they are asked to punch a hole starting at offset and > extending out to LONG_MAX. For all the supported file systems, apart from ext4, only btrfs, gfs2, and xfs support fallocate and FALLOC_FL_PUNCH_HOLE mode. Looking at what they do is they round the length of the space to be freed i.e. offset + length to valid value and then perform the operation using the valid values. https://elixir.bootlin.com/linux/v5.17.1/source/fs/gfs2/bmap.c#L2424 https://elixir.bootlin.com/linux/v5.17.1/source/fs/btrfs/file.c#L2506 For ext4 this would mean that one could only deallocate space up to the one before last block. I will change this to do the same in the next version if that's ok with you. > > Also, if we are going to return an error, I don't think ENOSPC is the > correct error to be returning. I took it from man 2 fallocate, my first suspicion was that it crashed because the disk on my VM wasn't big enough. It was a bad choice. -- Thanks, Tadeusz