Received: from list by lists.gnu.org with archive (Exim 4.71) id 1f8hW7-0004xv-Ow for mharc-grub-devel@gnu.org; Wed, 18 Apr 2018 03:28:35 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41966) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f8hW5-0004xX-M8 for grub-devel@gnu.org; Wed, 18 Apr 2018 03:28:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f8hW2-0000HS-Hy for grub-devel@gnu.org; Wed, 18 Apr 2018 03:28:33 -0400 Received: from prv3-mh.provo.novell.com ([137.65.250.26]:58793) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f8hW2-0000GL-7U for grub-devel@gnu.org; Wed, 18 Apr 2018 03:28:30 -0400 Received: from mazu (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (TLS encrypted); Wed, 18 Apr 2018 01:28:24 -0600 Date: Wed, 18 Apr 2018 15:28:20 +0800 From: Michael Chang To: Daniel Kiper Cc: grub-devel@gnu.org, phcoder@gmail.com Subject: Re: [PATCH] bufio: fix the next_buf calculation Message-ID: <20180418072820.tedbk7224yaec3fn@mazu> Mail-Followup-To: Daniel Kiper , grub-devel@gnu.org, phcoder@gmail.com References: <20180416100504.kbidvkerpritgigi@mazu> <20180417171134.GD21283@router-fw-old.local.net-space.pl> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180417171134.GD21283@router-fw-old.local.net-space.pl> User-Agent: NeoMutt/20170421 (1.8.2) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 137.65.250.26 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Apr 2018 07:28:34 -0000 On Tue, Apr 17, 2018 at 07:11:34PM +0200, Daniel Kiper wrote: > CC-ing Vladimir. > > On Mon, Apr 16, 2018 at 06:05:04PM +0800, Michael Chang wrote: > > The next_buf is the offset to the next cached block rounded to the size of > > bufio->block_size. However the calculation needs the block_size to be in power > > of 2 is not always valid. As an example, files with smaller size than > > block_size will have the block_size leveled to the size of file which can be > > set arbitrary value. > > > > This patch fixes the next_buf calculation to accept any integers. > > > > Signed-off-by: Michael Chang > > --- > > grub-core/io/bufio.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/grub-core/io/bufio.c b/grub-core/io/bufio.c > > index 22438277d..d0b0f71b6 100644 > > --- a/grub-core/io/bufio.c > > +++ b/grub-core/io/bufio.c > > @@ -132,7 +132,7 @@ grub_bufio_read (grub_file_t file, char *buf, grub_size_t len) > > return res; > > > > /* Need to read some more. */ > > - next_buf = (file->offset + res + len - 1) & ~((grub_off_t) bufio->block_size - 1); > > + next_buf = (grub_divmod64 (file->offset + res + len - 1, bufio->block_size, NULL)) * bufio->block_size; > > Should not you fix this in grub_bufio_open()? I think that > you should look for closest power of 2 in it. Of course here we can round up or down the bufio->block_size to meet power of 2. The down side of round-down is inefficient for small files as it can't cache entire file in one go. The round-up will have to allocate (slightly) larger than needed buffer to hold small files which is not a big deal ... If you insist, I can work a new patch to round up the block_size in grub_bufio_open, just let me know. Thanks, Michael > > Daniel