Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp686316ybx; Tue, 5 Nov 2019 04:03:33 -0800 (PST) X-Google-Smtp-Source: APXvYqwbtA4nllQRXpVkW26cMUCKGK5uflfl+h1ZQfQuFGqGLr0QHWs3H9cOBJNfXjAD1dCFFWCe X-Received: by 2002:a17:906:400f:: with SMTP id v15mr10836856ejj.126.1572955412773; Tue, 05 Nov 2019 04:03:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572955412; cv=none; d=google.com; s=arc-20160816; b=ql0BdWtqHvg4ZQxplhhK7gWfiBSoejMvcV3VlMTTkqMh5uXSPE1eJzPXL73ikukiMS 4jqKN+Sv6WeC++ycDJau8h7BmDsicIPcxkWKV6j5e3+Br0PQgysuOUnXewvROAaN65Ga FzhK16RR6KD7meIzjQrmWxwhp0EpPCboipRiGofrBkrmnBd344YEF4T7SaGIrfctJLyQ xozgFqgM0SharF/4lF43xfyJVWldl5mVl0ZIhgQVoOQqZ8ZdvmG8gJmgdrmqrnMatNrR qRuZIJicl4gBwte9tiOCBThiBNN74CHPcUmOHV5sHKob8LnedZ36H+47oAHF/G7vHYgJ c2NQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=Rm51/8A1FKCRPHUWGAek8qkPPsfDsFDO53iwbaAiymA=; b=hOJqnMEqQiwym+8pQk6jvQoxZ6dNX2ZeIyZ4HsthV6mh/7xwZBNfqRYkY+UJPvd7zW oLemezA9eBiA0I0fZK0f9siT0yQnq6nvwHlZxulBk3cuNr5rDzpgXeo5FGtpCwEpWN1G ATpqrIFkzQAMkZ2+3YMgrhagEUD4h+OP7b4GxDmWJyEJWvMzFbUrvw4Xm9DRa7V4teeF 78o1rHIPLcAGN/K1mBiGQSrNwzwf8ErN5o47rWy8M5FmILMbC3f9TeBLtamjVZtJ6dGR AcXxD6/dpd6c7tdUdvpYc2v43EZmjUVJwmALFw1y97/YliV4ktt2KY0ryLVO1NZuL0K5 oL7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mbobrowski-org.20150623.gappssmtp.com header.s=20150623 header.b=cgWsMGk3; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e25si10073291ede.335.2019.11.05.04.03.07; Tue, 05 Nov 2019 04:03:32 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@mbobrowski-org.20150623.gappssmtp.com header.s=20150623 header.b=cgWsMGk3; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726751AbfKEMAW (ORCPT + 99 others); Tue, 5 Nov 2019 07:00:22 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:42717 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730896AbfKEMAW (ORCPT ); Tue, 5 Nov 2019 07:00:22 -0500 Received: by mail-pl1-f193.google.com with SMTP id j12so7338716plt.9 for ; Tue, 05 Nov 2019 04:00:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mbobrowski-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=Rm51/8A1FKCRPHUWGAek8qkPPsfDsFDO53iwbaAiymA=; b=cgWsMGk3ooN4Zx1i2OmisZ2e3J13bSH2R7g3MeS32R8ie1nAI6BiB289EX4mM6VUgX nZn3CoVWzehrY3qLXzA2MFDhyOoZpqX7UWH7JgKnJZXbb2fBDh3yQDpgrPXYNQ0C2Qgf asH3QROVITNEOl+yWtHEJ7QqNk6UXK7Sip/58rnZl+VjEm0tXL23yiRh21W4Wgy2AeGc kokqVnSR8LTbx3W9IA61J4D1hM0orcTdn8N1WYTXpjAEDl3S18GJCXMg8QLCN8dI3HxT KZMjFYD6Uk9bOaInP6YA5H1dD+G5m3gbv2fihUB8e0zgPLKn5c2HVqN568tWL/zneYbp L6vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Rm51/8A1FKCRPHUWGAek8qkPPsfDsFDO53iwbaAiymA=; b=WvxXD+0B7fRPQk9QkvPkCuvw5hFT49QmZm16Ci3KxusFcgPY1DVAduHjvumjfMmTpW 6i3QXqsrUZ9C+OE/PhnAMxgv9KI05Dc5SZb0U70vi0LV42U6HkvOqr4SU8VCyHJ9FA7g C8KgqwWifkqLh436bNO73lvT4syNS9yKW2Wker4DJBhHtuGc8G9yhlzaBx/ypWTuA8Dg TDqtjE9cRta5DjRgN9youQhxYuZOeYkdbMOWZ5Z5U5oek0Bu37zN4PKuyNc99sVdlldf echWJsEvqCf+PlENJMB+KqLKGYcwGGnlNa1P+OA8IxNWnnj4uNnHgMr2u6ct0OajG/pV NuwA== X-Gm-Message-State: APjAAAWWHGx12kIKAN2eJ1FTkZ4EXwObHnQKqibKlVOw39KN7O6XJHzB Wjn3ASxb7Fe2caxNtbP3lLdN X-Received: by 2002:a17:902:7e45:: with SMTP id a5mr9164172pln.315.1572955220806; Tue, 05 Nov 2019 04:00:20 -0800 (PST) Received: from poseidon.bobrowski.net ([114.78.226.167]) by smtp.gmail.com with ESMTPSA id q34sm23038586pjb.15.2019.11.05.04.00.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Nov 2019 04:00:20 -0800 (PST) Date: Tue, 5 Nov 2019 23:00:14 +1100 From: Matthew Bobrowski To: tytso@mit.edu, jack@suse.cz, adilger.kernel@dilger.ca Cc: linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org, riteshh@linux.ibm.com Subject: [PATCH v7 05/11] ext4: split IOMAP_WRITE branch in ext4_iomap_begin() into helper Message-ID: <50eef383add1ea529651640574111076c55aca9f.1572949325.git.mbobrowski@mbobrowski.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org In preparation for porting across the ext4 direct I/O path over to the iomap infrastructure, split up the IOMAP_WRITE branch that's currently within ext4_iomap_begin() into a separate helper ext4_alloc_iomap(). This way, when we add in the necessary code for direct I/O, we don't end up with ext4_iomap_begin() becoming a monstrous twisty maze. Signed-off-by: Matthew Bobrowski Reviewed-by: Jan Kara Reviewed-by: Ritesh Harjani --- fs/ext4/inode.c | 113 ++++++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 52 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 9e1ac9fe816b..b540f2903faa 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -3493,6 +3493,63 @@ static void ext4_set_iomap(struct inode *inode, struct iomap *iomap, } } +static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, + unsigned int flags) +{ + handle_t *handle; + u8 blkbits = inode->i_blkbits; + int ret, dio_credits, retries = 0; + + /* + * Trim the mapping request to the maximum value that we can map at + * once for direct I/O. + */ + if (map->m_len > DIO_MAX_BLOCKS) + map->m_len = DIO_MAX_BLOCKS; + dio_credits = ext4_chunk_trans_blocks(inode, map->m_len); + +retry: + /* + * Either we allocate blocks and then don't get an unwritten extent, so + * in that case we have reserved enough credits. Or, the blocks are + * already allocated and unwritten. In that case, the extent conversion + * fits into the credits as well. + */ + handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits); + if (IS_ERR(handle)) + return PTR_ERR(handle); + + ret = ext4_map_blocks(handle, inode, map, EXT4_GET_BLOCKS_CREATE_ZERO); + if (ret < 0) + goto journal_stop; + + /* + * If we've allocated blocks beyond EOF, we need to ensure that they're + * truncated if we crash before updating the inode size metadata within + * ext4_iomap_end(). For faults, we don't need to do that (and cannot + * due to orphan list operations needing an inode_lock()). If we happen + * to instantiate blocks beyond EOF, it is because we race with a + * truncate operation, which already has added the inode onto the + * orphan list. + */ + if (!(flags & IOMAP_FAULT) && map->m_lblk + map->m_len > + (i_size_read(inode) + (1 << blkbits) - 1) >> blkbits) { + int err; + + err = ext4_orphan_add(handle, inode); + if (err < 0) + ret = err; + } + +journal_stop: + ext4_journal_stop(handle); + if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) + goto retry; + + return ret; +} + + static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, unsigned flags, struct iomap *iomap, struct iomap *srcmap) { @@ -3553,62 +3610,14 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, } } } else if (flags & IOMAP_WRITE) { - int dio_credits; - handle_t *handle; - int retries = 0; - - /* Trim mapping request to maximum we can map at once for DIO */ - if (map.m_len > DIO_MAX_BLOCKS) - map.m_len = DIO_MAX_BLOCKS; - dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); -retry: - /* - * Either we allocate blocks and then we don't get unwritten - * extent so we have reserved enough credits, or the blocks - * are already allocated and unwritten and in that case - * extent conversion fits in the credits as well. - */ - handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, - dio_credits); - if (IS_ERR(handle)) - return PTR_ERR(handle); - - ret = ext4_map_blocks(handle, inode, &map, - EXT4_GET_BLOCKS_CREATE_ZERO); - if (ret < 0) { - ext4_journal_stop(handle); - if (ret == -ENOSPC && - ext4_should_retry_alloc(inode->i_sb, &retries)) - goto retry; - return ret; - } - - /* - * If we added blocks beyond i_size, we need to make sure they - * will get truncated if we crash before updating i_size in - * ext4_iomap_end(). For faults we don't need to do that (and - * even cannot because for orphan list operations inode_lock is - * required) - if we happen to instantiate block beyond i_size, - * it is because we race with truncate which has already added - * the inode to the orphan list. - */ - if (!(flags & IOMAP_FAULT) && first_block + map.m_len > - (i_size_read(inode) + (1 << blkbits) - 1) >> blkbits) { - int err; - - err = ext4_orphan_add(handle, inode); - if (err < 0) { - ext4_journal_stop(handle); - return err; - } - } - ext4_journal_stop(handle); + ret = ext4_iomap_alloc(inode, &map, flags); } else { ret = ext4_map_blocks(NULL, inode, &map, 0); - if (ret < 0) - return ret; } + if (ret < 0) + return ret; + ext4_set_iomap(inode, iomap, &map, offset, length); if (delalloc && iomap->type == IOMAP_HOLE) iomap->type = IOMAP_DELALLOC; -- 2.20.1