From patchwork Thu Jan 9 13:30:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 11325681 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 80C9E1398 for ; Thu, 9 Jan 2020 13:30:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 56C4F2077B for ; Thu, 9 Jan 2020 13:30:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="czK3XEKc" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730027AbgAINa6 (ORCPT ); Thu, 9 Jan 2020 08:30:58 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:33736 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729114AbgAINa6 (ORCPT ); Thu, 9 Jan 2020 08:30:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1578576656; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wkLJ3bvQVebbfSchEDrEagK3yptheqo0smfxCSgrKo8=; b=czK3XEKcdSm6jLoPBsy7hElKcIARXVc7PqF67+clQ97xEuEzS/VpxnhM5yaDH+IPfEIklN 4PdtJeQkm9P91K/4Nbyd2VSJORc4UNc50avVrkbuxBLZWkZv75fykKOoPJeWGC+D4+UY2R 0T+cOc0T8dgTCR23jP+PT9PO2pZaWzI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-342-_QLUlaD7MDmvXAI_oehztA-1; Thu, 09 Jan 2020 08:30:53 -0500 X-MC-Unique: _QLUlaD7MDmvXAI_oehztA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E6BF6DBF5; Thu, 9 Jan 2020 13:30:51 +0000 (UTC) Received: from orion.redhat.com (ovpn-205-210.brq.redhat.com [10.40.205.210]) by smtp.corp.redhat.com (Postfix) with ESMTP id 94A8260FC6; Thu, 9 Jan 2020 13:30:50 +0000 (UTC) From: Carlos Maiolino To: linux-fsdevel@vger.kernel.org Cc: hch@lst.de, viro@zeniv.linux.org.uk Subject: [PATCH 1/5] fs: Enable bmap() function to properly return errors Date: Thu, 9 Jan 2020 14:30:41 +0100 Message-Id: <20200109133045.382356-2-cmaiolino@redhat.com> In-Reply-To: <20200109133045.382356-1-cmaiolino@redhat.com> References: <20200109133045.382356-1-cmaiolino@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org By now, bmap() will either return the physical block number related to the requested file offset or 0 in case of error or the requested offset maps into a hole. This patch makes the needed changes to enable bmap() to proper return errors, using the return value as an error return, and now, a pointer must be passed to bmap() to be filled with the mapped physical block. It will change the behavior of bmap() on return: - negative value in case of error - zero on success or map fell into a hole In case of a hole, the *block will be zero too Since this is a prep patch, by now, the only error return is -EINVAL if ->bmap doesn't exist. Reviewed-by: Christoph Hellwig Signed-off-by: Carlos Maiolino --- drivers/md/md-bitmap.c | 16 ++++++++++------ fs/f2fs/data.c | 16 +++++++++++----- fs/inode.c | 30 +++++++++++++++++------------- fs/jbd2/journal.c | 22 +++++++++++++++------- include/linux/fs.h | 2 +- mm/page_io.c | 11 +++++++---- 6 files changed, 61 insertions(+), 36 deletions(-) diff --git a/drivers/md/md-bitmap.c b/drivers/md/md-bitmap.c index 3ad18246fcb3..92d3b515252d 100644 --- a/drivers/md/md-bitmap.c +++ b/drivers/md/md-bitmap.c @@ -364,7 +364,7 @@ static int read_page(struct file *file, unsigned long index, int ret = 0; struct inode *inode = file_inode(file); struct buffer_head *bh; - sector_t block; + sector_t block, blk_cur; pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE, (unsigned long long)index << PAGE_SHIFT); @@ -375,17 +375,21 @@ static int read_page(struct file *file, unsigned long index, goto out; } attach_page_buffers(page, bh); - block = index << (PAGE_SHIFT - inode->i_blkbits); + blk_cur = index << (PAGE_SHIFT - inode->i_blkbits); while (bh) { + block = blk_cur; + if (count == 0) bh->b_blocknr = 0; else { - bh->b_blocknr = bmap(inode, block); - if (bh->b_blocknr == 0) { - /* Cannot use this file! */ + ret = bmap(inode, &block); + if (ret || !block) { ret = -EINVAL; + bh->b_blocknr = 0; goto out; } + + bh->b_blocknr = block; bh->b_bdev = inode->i_sb->s_bdev; if (count < (1<i_blkbits)) count = 0; @@ -399,7 +403,7 @@ static int read_page(struct file *file, unsigned long index, set_buffer_mapped(bh); submit_bh(REQ_OP_READ, 0, bh); } - block++; + blk_cur++; bh = bh->b_this_page; } page->index = index; diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index 356642e8c3b3..3bc3d137c9ff 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -3647,12 +3647,16 @@ static int check_swap_activate(struct swap_info_struct *sis, page_no < sis->max) { unsigned block_in_page; sector_t first_block; + sector_t block = 0; + int err = 0; cond_resched(); - first_block = bmap(inode, probe_block); - if (first_block == 0) + block = probe_block; + err = bmap(inode, &block); + if (err || !block) goto bad_bmap; + first_block = block; /* * It must be PAGE_SIZE aligned on-disk @@ -3664,11 +3668,13 @@ static int check_swap_activate(struct swap_info_struct *sis, for (block_in_page = 1; block_in_page < blocks_per_page; block_in_page++) { - sector_t block; - block = bmap(inode, probe_block + block_in_page); - if (block == 0) + block = probe_block + block_in_page; + err = bmap(inode, &block); + + if (err || !block) goto bad_bmap; + if (block != first_block + block_in_page) { /* Discontiguity */ probe_block++; diff --git a/fs/inode.c b/fs/inode.c index 2b0f51161918..9f894b25af2b 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -1600,21 +1600,25 @@ EXPORT_SYMBOL(iput); /** * bmap - find a block number in a file - * @inode: inode of file - * @block: block to find - * - * Returns the block number on the device holding the inode that - * is the disk block number for the block of the file requested. - * That is, asked for block 4 of inode 1 the function will return the - * disk block relative to the disk start that holds that block of the - * file. + * @inode: inode owning the block number being requested + * @block: pointer containing the block to find + * + * Replaces the value in *block with the block number on the device holding + * corresponding to the requested block number in the file. + * That is, asked for block 4 of inode 1 the function will replace the + * 4 in *block, with disk block relative to the disk start that holds that + * block of the file. + * + * Returns -EINVAL in case of error, 0 otherwise. If mapping falls into a + * hole, returns 0 and *block is also set to 0. */ -sector_t bmap(struct inode *inode, sector_t block) +int bmap(struct inode *inode, sector_t *block) { - sector_t res = 0; - if (inode->i_mapping->a_ops->bmap) - res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block); - return res; + if (!inode->i_mapping->a_ops->bmap) + return -EINVAL; + + *block = inode->i_mapping->a_ops->bmap(inode->i_mapping, *block); + return 0; } EXPORT_SYMBOL(bmap); diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c index fa3a9232caa7..8e2756e2252f 100644 --- a/fs/jbd2/journal.c +++ b/fs/jbd2/journal.c @@ -795,18 +795,23 @@ int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, { int err = 0; unsigned long long ret; + sector_t block = 0; if (journal->j_inode) { - ret = bmap(journal->j_inode, blocknr); - if (ret) - *retp = ret; - else { + block = blocknr; + ret = bmap(journal->j_inode, &block); + + if (ret || !block) { printk(KERN_ALERT "%s: journal block not found " "at offset %lu on %s\n", __func__, blocknr, journal->j_devname); err = -EIO; __journal_abort_soft(journal, err); + + } else { + *retp = block; } + } else { *retp = blocknr; /* +journal->j_blk_offset */ } @@ -1243,11 +1248,14 @@ journal_t *jbd2_journal_init_dev(struct block_device *bdev, journal_t *jbd2_journal_init_inode(struct inode *inode) { journal_t *journal; + sector_t blocknr; char *p; - unsigned long long blocknr; + int err = 0; + + blocknr = 0; + err = bmap(inode, &blocknr); - blocknr = bmap(inode, 0); - if (!blocknr) { + if (err || !blocknr) { pr_err("%s: Cannot locate journal superblock\n", __func__); return NULL; diff --git a/include/linux/fs.h b/include/linux/fs.h index d7584bcef5d3..092699e99faa 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2865,7 +2865,7 @@ static inline ssize_t generic_write_sync(struct kiocb *iocb, ssize_t count) extern void emergency_sync(void); extern void emergency_remount(void); #ifdef CONFIG_BLOCK -extern sector_t bmap(struct inode *, sector_t); +extern int bmap(struct inode *, sector_t *); #endif extern int notify_change(struct dentry *, struct iattr *, struct inode **); extern int inode_permission(struct inode *, int); diff --git a/mm/page_io.c b/mm/page_io.c index 3a198deb8bb1..76965be1d40e 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -177,8 +177,9 @@ int generic_swapfile_activate(struct swap_info_struct *sis, cond_resched(); - first_block = bmap(inode, probe_block); - if (first_block == 0) + first_block = probe_block; + ret = bmap(inode, &first_block); + if (ret || !first_block) goto bad_bmap; /* @@ -193,9 +194,11 @@ int generic_swapfile_activate(struct swap_info_struct *sis, block_in_page++) { sector_t block; - block = bmap(inode, probe_block + block_in_page); - if (block == 0) + block = probe_block + block_in_page; + ret = bmap(inode, &block); + if (ret || !block) goto bad_bmap; + if (block != first_block + block_in_page) { /* Discontiguity */ probe_block++; From patchwork Thu Jan 9 13:30:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 11325679 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2D7DE1398 for ; Thu, 9 Jan 2020 13:30:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0C28F206ED for ; Thu, 9 Jan 2020 13:30:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="TSvTswvd" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729987AbgAINa5 (ORCPT ); Thu, 9 Jan 2020 08:30:57 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:40811 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729894AbgAINa5 (ORCPT ); Thu, 9 Jan 2020 08:30:57 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1578576656; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=byrKCEhaLJHpefyXaNaEhPVEwhIGAC7cpoxRKOr8+YE=; b=TSvTswvd10/+xli19g8JXGnqsTE76qq+u050oZW66EjsRSFkr/f9mbdVvJPjncVVyETWZR VVjmJoDwwzoPc6Vi4ZiAERJesUpfeYfZyvmVqpsS4R6EJPsuP9Q4dEnp2EymZzsWMd3Kgd 6DQK0hil4esMp1BSKENyG44bElhEWdQ= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-63-2dklwYbTOA-lvC6xpC-whg-1; Thu, 09 Jan 2020 08:30:55 -0500 X-MC-Unique: 2dklwYbTOA-lvC6xpC-whg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2E0771005514; Thu, 9 Jan 2020 13:30:54 +0000 (UTC) Received: from orion.redhat.com (ovpn-205-210.brq.redhat.com [10.40.205.210]) by smtp.corp.redhat.com (Postfix) with ESMTP id 22DFF60C88; Thu, 9 Jan 2020 13:30:52 +0000 (UTC) From: Carlos Maiolino To: linux-fsdevel@vger.kernel.org Cc: hch@lst.de, viro@zeniv.linux.org.uk Subject: [PATCH 2/5] cachefiles: drop direct usage of ->bmap method. Date: Thu, 9 Jan 2020 14:30:42 +0100 Message-Id: <20200109133045.382356-3-cmaiolino@redhat.com> In-Reply-To: <20200109133045.382356-1-cmaiolino@redhat.com> References: <20200109133045.382356-1-cmaiolino@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Replace the direct usage of ->bmap method by a bmap() call. Reviewed-by: Christoph Hellwig Signed-off-by: Carlos Maiolino --- fs/cachefiles/rdwr.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/fs/cachefiles/rdwr.c b/fs/cachefiles/rdwr.c index 44a3ce1e4ce4..1dc97f2d6201 100644 --- a/fs/cachefiles/rdwr.c +++ b/fs/cachefiles/rdwr.c @@ -396,7 +396,7 @@ int cachefiles_read_or_alloc_page(struct fscache_retrieval *op, struct cachefiles_object *object; struct cachefiles_cache *cache; struct inode *inode; - sector_t block0, block; + sector_t block; unsigned shift; int ret; @@ -412,7 +412,6 @@ int cachefiles_read_or_alloc_page(struct fscache_retrieval *op, inode = d_backing_inode(object->backer); ASSERT(S_ISREG(inode->i_mode)); - ASSERT(inode->i_mapping->a_ops->bmap); ASSERT(inode->i_mapping->a_ops->readpages); /* calculate the shift required to use bmap */ @@ -428,12 +427,14 @@ int cachefiles_read_or_alloc_page(struct fscache_retrieval *op, * enough for this as it doesn't indicate errors, but it's all we've * got for the moment */ - block0 = page->index; - block0 <<= shift; + block = page->index; + block <<= shift; + + ret = bmap(inode, &block); + ASSERT(ret < 0); - block = inode->i_mapping->a_ops->bmap(inode->i_mapping, block0); _debug("%llx -> %llx", - (unsigned long long) block0, + (unsigned long long) (page->index << shift), (unsigned long long) block); if (block) { @@ -711,7 +712,6 @@ int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op, inode = d_backing_inode(object->backer); ASSERT(S_ISREG(inode->i_mode)); - ASSERT(inode->i_mapping->a_ops->bmap); ASSERT(inode->i_mapping->a_ops->readpages); /* calculate the shift required to use bmap */ @@ -728,7 +728,7 @@ int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op, ret = space ? -ENODATA : -ENOBUFS; list_for_each_entry_safe(page, _n, pages, lru) { - sector_t block0, block; + sector_t block; /* we assume the absence or presence of the first block is a * good enough indication for the page as a whole @@ -736,13 +736,14 @@ int cachefiles_read_or_alloc_pages(struct fscache_retrieval *op, * good enough for this as it doesn't indicate errors, but * it's all we've got for the moment */ - block0 = page->index; - block0 <<= shift; + block = page->index; + block <<= shift; + + ret = bmap(inode, &block); + ASSERT(!ret); - block = inode->i_mapping->a_ops->bmap(inode->i_mapping, - block0); _debug("%llx -> %llx", - (unsigned long long) block0, + (unsigned long long) (page->index << shift), (unsigned long long) block); if (block) { From patchwork Thu Jan 9 13:30:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 11325683 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 832B4921 for ; Thu, 9 Jan 2020 13:31:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 556DE20661 for ; Thu, 9 Jan 2020 13:31:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="jVWqL3qk" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730077AbgAINbB (ORCPT ); Thu, 9 Jan 2020 08:31:01 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:21095 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729114AbgAINbB (ORCPT ); Thu, 9 Jan 2020 08:31:01 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1578576660; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0YYg4tVJ4OGAuXsuBTuC4BaFuZnWfJ/BXnswaMtHl4g=; b=jVWqL3qkDMPtpBqC4FurX5mksHGeYPwTubN1cNyQrFa/Of67bZ7YYaljrhHPMeTM46wMS4 +PbqdTZX/8GDErk+ZTWFaLSoyejvOQR7Vq83MR3MJ9UdFJ0iH6s1fLUoe3dQipuMYHhSYK NynQwalRsX3cX+GYzbqgOnlaVFeyqOk= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-328-p3E1xWXnOty9R7kPcOitPA-1; Thu, 09 Jan 2020 08:30:57 -0500 X-MC-Unique: p3E1xWXnOty9R7kPcOitPA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6B4DD1005514; Thu, 9 Jan 2020 13:30:56 +0000 (UTC) Received: from orion.redhat.com (ovpn-205-210.brq.redhat.com [10.40.205.210]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5FEAA60C88; Thu, 9 Jan 2020 13:30:55 +0000 (UTC) From: Carlos Maiolino To: linux-fsdevel@vger.kernel.org Cc: hch@lst.de, viro@zeniv.linux.org.uk Subject: [PATCH 3/5] ecryptfs: drop direct calls to ->bmap Date: Thu, 9 Jan 2020 14:30:43 +0100 Message-Id: <20200109133045.382356-4-cmaiolino@redhat.com> In-Reply-To: <20200109133045.382356-1-cmaiolino@redhat.com> References: <20200109133045.382356-1-cmaiolino@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Replace direct ->bmap calls by bmap() method. Reviewed-by: Christoph Hellwig Signed-off-by: Carlos Maiolino --- fs/ecryptfs/mmap.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/fs/ecryptfs/mmap.c b/fs/ecryptfs/mmap.c index cffa0c1ec829..019572c6b39a 100644 --- a/fs/ecryptfs/mmap.c +++ b/fs/ecryptfs/mmap.c @@ -524,16 +524,12 @@ static int ecryptfs_write_end(struct file *file, static sector_t ecryptfs_bmap(struct address_space *mapping, sector_t block) { - int rc = 0; - struct inode *inode; - struct inode *lower_inode; - - inode = (struct inode *)mapping->host; - lower_inode = ecryptfs_inode_to_lower(inode); - if (lower_inode->i_mapping->a_ops->bmap) - rc = lower_inode->i_mapping->a_ops->bmap(lower_inode->i_mapping, - block); - return rc; + struct inode *lower_inode = ecryptfs_inode_to_lower(mapping->host); + int ret = bmap(lower_inode, &block); + + if (ret) + return 0; + return block; } const struct address_space_operations ecryptfs_aops = { From patchwork Thu Jan 9 13:30:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 11325685 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 89699921 for ; Thu, 9 Jan 2020 13:31:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 674792077B for ; Thu, 9 Jan 2020 13:31:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="M8HpsDzw" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730258AbgAINbE (ORCPT ); Thu, 9 Jan 2020 08:31:04 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:20554 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729114AbgAINbD (ORCPT ); Thu, 9 Jan 2020 08:31:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1578576663; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lRmNoC4UPTCVIngwiJ4U4bmcbrm7KHI2dsz/lFIHqH4=; b=M8HpsDzwYG3TFlr0o8SsQi1RlpVjZQ6MYQOI7jB3FbUy8nnMDYr+lqOI1cafLpVEK40Rew GvjBwLye4Z7niMuMsDZy2EIYVzluRSOzNmgVdy/PSqZN+XCHUZyrs9F9w72N7cgnB2mfzw 1s3DTgLhiAyi5LOMN+I6h+hDLIq4WIE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-319-7McnWKq_Ml25IKmuBr_kPw-1; Thu, 09 Jan 2020 08:31:00 -0500 X-MC-Unique: 7McnWKq_Ml25IKmuBr_kPw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DD560190D340; Thu, 9 Jan 2020 13:30:58 +0000 (UTC) Received: from orion.redhat.com (ovpn-205-210.brq.redhat.com [10.40.205.210]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6B4D160E1C; Thu, 9 Jan 2020 13:30:57 +0000 (UTC) From: Carlos Maiolino To: linux-fsdevel@vger.kernel.org Cc: hch@lst.de, viro@zeniv.linux.org.uk Subject: [PATCH 4/5] fibmap: Use bmap instead of ->bmap method in ioctl_fibmap Date: Thu, 9 Jan 2020 14:30:44 +0100 Message-Id: <20200109133045.382356-5-cmaiolino@redhat.com> In-Reply-To: <20200109133045.382356-1-cmaiolino@redhat.com> References: <20200109133045.382356-1-cmaiolino@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Now we have the possibility of proper error return in bmap, use bmap() function in ioctl_fibmap() instead of calling ->bmap method directly. Signed-off-by: Carlos Maiolino --- fs/ioctl.c | 30 ++++++++++++++++++++---------- include/linux/fs.h | 9 ++++++++- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/fs/ioctl.c b/fs/ioctl.c index 7c9a5df5a597..0ed5fb2d6c19 100644 --- a/fs/ioctl.c +++ b/fs/ioctl.c @@ -54,19 +54,29 @@ EXPORT_SYMBOL(vfs_ioctl); static int ioctl_fibmap(struct file *filp, int __user *p) { - struct address_space *mapping = filp->f_mapping; - int res, block; + struct inode *inode = file_inode(filp); + int error, ur_block; + sector_t block; - /* do we support this mess? */ - if (!mapping->a_ops->bmap) - return -EINVAL; if (!capable(CAP_SYS_RAWIO)) return -EPERM; - res = get_user(block, p); - if (res) - return res; - res = mapping->a_ops->bmap(mapping, block); - return put_user(res, p); + + error = get_user(ur_block, p); + if (error) + return error; + + block = ur_block; + error = bmap(inode, &block); + + if (error) + ur_block = 0; + else + ur_block = block; + + if (put_user(ur_block, p)) + error = -EFAULT; + + return error; } /** diff --git a/include/linux/fs.h b/include/linux/fs.h index 092699e99faa..90a72f7185b7 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2864,9 +2864,16 @@ static inline ssize_t generic_write_sync(struct kiocb *iocb, ssize_t count) extern void emergency_sync(void); extern void emergency_remount(void); + #ifdef CONFIG_BLOCK -extern int bmap(struct inode *, sector_t *); +extern int bmap(struct inode *inode, sector_t *block); +#else +static inline int bmap(struct inode *inode, sector_t *block) +{ + return -EINVAL; +} #endif + extern int notify_change(struct dentry *, struct iattr *, struct inode **); extern int inode_permission(struct inode *, int); extern int generic_permission(struct inode *, int); From patchwork Thu Jan 9 13:30:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlos Maiolino X-Patchwork-Id: 11325687 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id ABA871398 for ; Thu, 9 Jan 2020 13:31:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 80FCA2077B for ; Thu, 9 Jan 2020 13:31:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="i55h4EVm" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730298AbgAINbH (ORCPT ); Thu, 9 Jan 2020 08:31:07 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:53936 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729114AbgAINbG (ORCPT ); Thu, 9 Jan 2020 08:31:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1578576666; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xhZuXS6DjAtohpYRcSN6Kdi6FwqV/yhcW8ynmM4QQiM=; b=i55h4EVmzoqmtnuvcnTd47GCvQDLrjAMgKkOBU6tjySkwD+PQasY+HUyO+JU5GW2XmKkMm CXzI2DUYd8MEQsh/JZ+/JL+miUNflT2eHaNyqVTGfVkNTAQ9E/tHaPwuU1GcnMA6JFGX8g BWMSxcLOXXELX0VUfoMR1f04rk9xupA= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-423--wXv5TMYNiiyb0xlx-UT4A-1; Thu, 09 Jan 2020 08:31:02 -0500 X-MC-Unique: -wXv5TMYNiiyb0xlx-UT4A-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 296E0107ACC5; Thu, 9 Jan 2020 13:31:01 +0000 (UTC) Received: from orion.redhat.com (ovpn-205-210.brq.redhat.com [10.40.205.210]) by smtp.corp.redhat.com (Postfix) with ESMTP id 180EF60C88; Thu, 9 Jan 2020 13:30:59 +0000 (UTC) From: Carlos Maiolino To: linux-fsdevel@vger.kernel.org Cc: hch@lst.de, viro@zeniv.linux.org.uk Subject: [PATCH 5/5] fibmap: Reject negative block numbers Date: Thu, 9 Jan 2020 14:30:45 +0100 Message-Id: <20200109133045.382356-6-cmaiolino@redhat.com> In-Reply-To: <20200109133045.382356-1-cmaiolino@redhat.com> References: <20200109133045.382356-1-cmaiolino@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org FIBMAP receives an integer from userspace which is then implicitly converted into sector_t to be passed to bmap(). No check is made to ensure userspace didn't send a negative block number, which can end up in an underflow, and returning to userspace a corrupted block address. As a side-effect, the underflow caused by a negative block here, will trigger the WARN() in iomap_bmap_actor(), which is how this issue was first discovered. Reviewed-by: Christoph Hellwig Signed-off-by: Carlos Maiolino --- fs/ioctl.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/ioctl.c b/fs/ioctl.c index 0ed5fb2d6c19..72d6848fb6ad 100644 --- a/fs/ioctl.c +++ b/fs/ioctl.c @@ -65,6 +65,9 @@ static int ioctl_fibmap(struct file *filp, int __user *p) if (error) return error; + if (ur_block < 0) + return -EINVAL; + block = ur_block; error = bmap(inode, &block);