From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796148581; b=pJybH3Jk8nQAOS7f6LIy2J5g4kKNlgz7AONbh7YaxFfXP4OR7z0Qx8JLNoTdToT4KUB5 55Tso3BXA+ca334HZY2IuI5bnQAHr8HbCyV853jQuRFvNgwlZtjBJ7Z0LD7m5p1MONrKK Bt96GEbf3Z3VpL863xDqC1fODWj0yV+IHu5eT82P2yqRduf4U7ep9BFmjaDPR9TTB2QDg SPrKP1PZBiWp0GcZvRJffXGoZ+OfSwGiSYJWbEqqYlfOdHyA1QFB4COdambFqoLnHcvJX hdAhBADk6D2ohlvK5gbOa1vD/TsYNmvjp7JqVlG7mToI5Smid7nAL3eNthx5mQvXqUA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796148581; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References:Autocrypt: Content-Type:Content-Transfer-Encoding:User-Agent:MIME-Version; bh=RorxwOUnYDzD2ILGH8BoAPkjGLMCgK4/4cCToZBJMXs=; b=wDNu5MPMX80S51zPG6za5SC6NgZLNSiffGY/wA86Sa61nhtFEukt5cGNe+jyner5jT3O +ss0kLwlZfY/N6kYv87x4+7dz61Rtk/H3wkOExMXyKrGjDRWqYDNQqqDQA0MRO8Cl31CT jg32VCGuweZA4F7c289Q8A8ZBgDNtkY44o1zc3cRr4eSyIhe3Dt/XgAZbt3IVfN4AN96/ VhO+7oPZwYnUkJ1fe1XIzQzCABvlGMSf26CZRstcG8515cZ7AwbxKUISs+9GEiSA3l2JB GCRzIt9fCC6sajdPvx7f7ikLmLTszpGko7gPgzZG/jrZeKMIUcUqIa8Szo1RTGc5aPg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=dubeyko.com; spf=pass smtp.mailfrom=dubeyko.com; dkim=pass header.d=dubeyko-com.20260707.gappssmtp.com header.i=@dubeyko-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=V+BsQcRd; arc=none smtp.remote-ip=74.125.224.49 Received: from mail-yx1-f49.google.com (mail-yx1-f49.google.com [74.125.224.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A28B606A320 for ; Tue, 1 Dec 2026 18:09:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3A28B606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dubeyko.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=dubeyko.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dubeyko-com.20260707.gappssmtp.com header.i=@dubeyko-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=V+BsQcRd Received: by mail-yx1-f49.google.com with SMTP id 956f58d0204a3-67990a45e02so12599308d50.3 for ; Tue, 01 Dec 2026 10:09:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dubeyko-com.20260707.gappssmtp.com; s=20260707; t=1796148579; x=1796753379; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:content-type :autocrypt:references:in-reply-to:date:cc:to:from:subject:message-id :from:to:cc:subject:date:message-id:reply-to:content-type; bh=RorxwOUnYDzD2ILGH8BoAPkjGLMCgK4/4cCToZBJMXs=; b=V+BsQcRdNCyZAn8J5U0zbF7z272UyjS+UoPpAGHGvIn5FR0WnJGZPPFBPm14bEUW9B je3U1GL4Kw3XzF1Xa/u/8VTJuV/Hcg8Sef49ZMMHYlim/XlM8eaAy31HOSiOf7dl/p8q RKkUYzXQGZ4OfG+lRFqOYE5zfnchL5b3nAyZuVkhW49mw2XgKMhisUwY3pCLDT7eYAZy vFIxPrAj6J0VEyr4vzX01EntGMB57KdJgkFAb5Gm6jaCcuHYVbfQUPAxNOgsSOZufTcJ xFxw/rKLD4nclEcA9zYvifR/Ry3DhrFPiThEh2JvXjhDoGqVbTypRTO2GgYs29vaPIgJ Y4Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796148579; x=1796753379; h=mime-version:user-agent:content-transfer-encoding:content-type :autocrypt:references:in-reply-to:date:cc:to:from:subject:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=RorxwOUnYDzD2ILGH8BoAPkjGLMCgK4/4cCToZBJMXs=; b=gfdHSmXyepaj1PClYHJ2Pqrh8DGG1jRqOtIQYS00xUk9JEZoTreg57Es4v4OS40Mak IZd7J4fZ0srv3+y90l6rLyUD9nxxSEITp+dSu6DCmN27I+nNP2KT3auRC16FD6qM1xpA Tc8s4Op1OznPsWqy0D0QOQxCFshRVhjWi9cEVqAumQFJ2U7FrYNEN+/eBm4xaxfky60o Pi82IcFZU7sP2kwftlUImT9afeEoeObD0oDyhtJFsV531LhtJJqfaz7Uq9VsLQoOo12m tTcptXefKSeKXE13zUpEUf0cydblZpnfn27jn8a9eagyRivMQt7pRtk2Xw4sugKmpMRx 1ZoA== X-Forwarded-Encrypted: i=1; AL+hErEz6oW/5KvKiteOm1RQ0/T8sYZZ7u1piRido0XWHL5bixWkvYJoIKtP9bs7aKQnX9QPg/+EMz+MShTSqOc=@vger.kernel.org X-Gm-Message-State: APuU4Z+ju8tzaE5SRVhu3Mvg8x6/BynIi+jgRWTp44l7z0Ch4WkrBtQF bRh3Q6X/ct+lWZfJuvYPgzpExiaq3PxzNU+BTan2IcUJfjLvi+790lA/OZfrqvOC4SjhdtFYgtw AGnqrV8U= X-Gm-Gg: AZFdTZeEhsZUfPnkDOPVGXKkH5JrmOALhWR8OH70zYFJqzPxsMH3CDVWDuDwoiNG07e UofoFDGAeg2/Ey84HMfEFgYf5nKvQqWz76qXwZyouxgfBMNOou7Nbz2xCoJr9dYj32igrAdgSJu dVg8N6xoqxsLkyIA59oGqvdRm+NCjJQiHmnoyWvhBb6U/6gAaGK7JXOK158ybhzHeoU3fR3D4Vo 5XXpsSraTcsj7H5lv+2AHMS3tq9bf/jLu4HFs9R6VpCrKkcoBQoDZLk/Y+Zs4AVR1Br96IHvOPM JoMEa+dR4Pn/LdmrW62BwdZVNKi48qWRsWTUApZO3XvB6vXMnx1S7ZgOQT2Ri35ObW7HTkos8gN edZQ1dHm50md4u27TDywKSsbWIQzvPNR3kdmlwaYTGn0YVWHZU0Qzk8uKxTfBfvD7QPCkVeuvce wsXEHbTP+Ow1wa0hTz5jSMBhuOGNPpQHbVRPTWhRHlHZuY1Kksfrqp3VKB3USroevlbERUi4LHu MoqUr4r0u9ndFo0e2IUhBz94eQI17VA1C5cDqAtR+SSE04vSW/MztNuimRag0eZleoYkglqLHeQ ksP893o8XBMhMg+NCTgt3Pnf+UIGvCi0LvbWaMVCi0phKKgcOJEwFhd8KNHYdisbJojDSVNb+UU tWv69pbPzu9kOlRakV58kYvbfXKIhXNWazArvIvj3uV5kvjyvz3bRGG9ebtebiyzEwe17ApEnK4 cZ9JLP2cEaftRoEQ== X-Received: by 2002:a05:690e:1209:b0:67c:6a40:e316 with SMTP id 956f58d0204a3-67df43f6d63mr17613291d50.26.1796148578849; Tue, 01 Dec 2026 10:09:38 -0800 (PST) Received: from system76-laptop.attlocal.net ([2600:1700:6476:1430:6296:81e6:c:6227]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67df33105fesm17054602d50.7.2026.12.01.10.09.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 10:09:38 -0800 (PST) Message-ID: <285ced6b4cc04f25d367e6f4993aa223a662cfed.camel@dubeyko.com> Subject: Re: [PATCH] nilfs2: prevent stale B-tree node pages from lingering in page cache From: Viacheslav Dubeyko To: Ryusuke Konishi Cc: linux-nilfs , LKML Date: Tue, 01 Dec 2026 10:09:37 -0800 In-Reply-To: <20261201091832.3287-1-konishi.ryusuke@gmail.com> References: <20261201091832.3287-1-konishi.ryusuke@gmail.com> Autocrypt: addr=slava@dubeyko.com; prefer-encrypt=mutual; keydata=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 NFdvziOa4FgUBC4X71upsZIoZJeii2V/Jtc3B4g== Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2-9 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 On Tue, 2026-12-01 at 18:17 +0900, Ryusuke Konishi wrote: > When the block size differs from the page size, writing to files > leaves > unreferenced, stale B-tree node pages (folios) lingering in the B- > tree > node page cache until they are evicted by memory reclaim. >=20 > This is a design flaw stemming from the original interface of > nilfs_bmap_propagate().=C2=A0 For files other than the DAT, propagating > dirty > state to parent B-tree nodes updates the virtual block numbers of > those > nodes.=C2=A0 Because B-tree node buffers are indexed in the page cache by > their virtual block number, a node buffer is relocated to a different > page offset when its virtual block number changes.=C2=A0 When the block > size > is smaller than the page size, nilfs_btree_propagate() allocates a > new > buffer head at the new location, copies the node data, and releases > the > old buffer head via nilfs_btnode_commit_change_key().=C2=A0 However, the > callers up to nilfs_segctor_apply_buffers() hold a reference to the > original buffer head throughout the call chain, causing page > invalidation of the old buffer's folio to always fail even when it > was > the last valid buffer on that folio. >=20 > Fix this issue by transferring ownership of the buffer head reference > along the propagation call path: >=20 > - Change nilfs_bmap_propagate() and the bop_propagate operation to > =C2=A0 consume the caller's reference to the input buffer head, and add a= n > =C2=A0 output argument out_bh to return a reference to the resulting > buffer > =C2=A0 head (the replacement if relocated, or the original otherwise) whe= n > =C2=A0 needed. >=20 > - Pass the buffer head reference through > nilfs_segctor_apply_buffers(), > =C2=A0 the segment constructor collect() callbacks, > nilfs_bmap_propagate(), > =C2=A0 and nilfs_{btree,direct}_propagate() instead of retaining it in th= e > =C2=A0 callers, allowing nilfs_btnode_commit_change_key() to actively fre= e > =C2=A0 and invalidate the old buffer. >=20 > - Remove the redundant buffer head argument passed down to internal > =C2=A0 propagation helpers in btree.c, keeping reference handoff and > release > =C2=A0 consolidated in nilfs_btree_propagate() rather than leaking extra > =C2=A0 buffer references into lower-level routines. >=20 > Fixes: 17c76b0104e4 ("nilfs2: B-tree based block mapping") > Signed-off-by: Ryusuke Konishi > --- > Hi Viacheslav, >=20 > Could you please apply this for the next cycle? >=20 > This fixes an issue on filesystems with small block sizes (smaller > than the page size) where stale B-tree node buffers and their > containing > folios remain in the B-tree node page cache every time a B-tree node > is > updated, until memory reclaim eventually evicts them. >=20 > Thanks, > Ryusuke Konishi >=20 > =C2=A0fs/nilfs2/bmap.c=C2=A0=C2=A0=C2=A0 | 19 +++++++++++----- > =C2=A0fs/nilfs2/bmap.h=C2=A0=C2=A0=C2=A0 |=C2=A0 6 +++-- > =C2=A0fs/nilfs2/btree.c=C2=A0=C2=A0 | 54 +++++++++++++++++++++++++++++++-= ----------- > -- > =C2=A0fs/nilfs2/direct.c=C2=A0 | 21 ++++++++++++------ > =C2=A0fs/nilfs2/segment.c | 29 ++++++++++++++++-------- > =C2=A05 files changed, 89 insertions(+), 40 deletions(-) >=20 > diff --git a/fs/nilfs2/bmap.c b/fs/nilfs2/bmap.c > index 83f6ea30cc8b..5a3596e0aedf 100644 > --- a/fs/nilfs2/bmap.c > +++ b/fs/nilfs2/bmap.c > @@ -310,23 +310,30 @@ void nilfs_bmap_clear(struct nilfs_bmap *bmap) > =C2=A0 > =C2=A0/** > =C2=A0 * nilfs_bmap_propagate - propagate dirty state > - * @bmap: bmap > - * @bh: buffer head > + * @bmap:=C2=A0=C2=A0 bmap > + * @bh:=C2=A0=C2=A0=C2=A0=C2=A0 buffer head at the point of origin for p= ropagation > + * @out_bh: location to store the up-to-date buffer head (out) > =C2=A0 * > - * Description: nilfs_bmap_propagate() marks the buffers that > directly or > - * indirectly refer to the block specified by @bh dirty. > + * nilfs_bmap_propagate() marks as dirty any buffers that either > directly or > + * indirectly refer to the block of the buffer head @bh.=C2=A0 The block > may be > + * relocated in the page cache due to an update of its virtual block > number. > + * Regardless of success or failure, the caller's reference to @bh > is dropped. > + * On success, if @out_bh is not %NULL, a pointer to the latest > buffer head > + * (the new one if relocated, or @bh otherwise) is returned via > @out_bh with > + * a reference. > =C2=A0 * > =C2=A0 * Return: 0 on success, or one of the following negative error > codes on > =C2=A0 * failure: > =C2=A0 * * %-EIO - I/O error (including metadata corruption). > =C2=A0 * * %-ENOMEM - Insufficient memory available. > =C2=A0 */ > -int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head > *bh) > +int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head > *bh, > + struct buffer_head **out_bh) > =C2=A0{ > =C2=A0 int ret; > =C2=A0 > =C2=A0 down_write(&bmap->b_sem); > - ret =3D bmap->b_ops->bop_propagate(bmap, bh); > + ret =3D bmap->b_ops->bop_propagate(bmap, bh, out_bh); > =C2=A0 up_write(&bmap->b_sem); > =C2=A0 > =C2=A0 return nilfs_bmap_convert_error(bmap, __func__, ret); > diff --git a/fs/nilfs2/bmap.h b/fs/nilfs2/bmap.h > index 6493217b1a7f..26dd15a968f9 100644 > --- a/fs/nilfs2/bmap.h > +++ b/fs/nilfs2/bmap.h > @@ -67,7 +67,8 @@ struct nilfs_bmap_operations { > =C2=A0 int (*bop_delete)(struct nilfs_bmap *bmap, __u64 key, bool > deform); > =C2=A0 void (*bop_clear)(struct nilfs_bmap *); > =C2=A0 > - int (*bop_propagate)(struct nilfs_bmap *, struct buffer_head > *); > + int (*bop_propagate)(struct nilfs_bmap *bmap, struct > buffer_head *bh, > + =C2=A0=C2=A0=C2=A0=C2=A0 struct buffer_head **out_bh); > =C2=A0 void (*bop_lookup_dirty_buffers)(struct nilfs_bmap *, > =C2=A0 struct list_head *); > =C2=A0 > @@ -167,7 +168,8 @@ int nilfs_bmap_seek_key(struct nilfs_bmap *bmap, > __u64 start, __u64 *keyp); > =C2=A0int nilfs_bmap_last_key(struct nilfs_bmap *bmap, __u64 *keyp); > =C2=A0int nilfs_bmap_truncate(struct nilfs_bmap *bmap, __u64 key); > =C2=A0void nilfs_bmap_clear(struct nilfs_bmap *); > -int nilfs_bmap_propagate(struct nilfs_bmap *, struct buffer_head *); > +int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head > *bh, > + struct buffer_head **out_bh); > =C2=A0void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *, struct > list_head *); > =C2=A0int nilfs_bmap_assign(struct nilfs_bmap *, struct buffer_head **, > =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 unsigned long, union nilfs_binfo *= ); > diff --git a/fs/nilfs2/btree.c b/fs/nilfs2/btree.c > index 8c12b78c0e9d..552ae7199592 100644 > --- a/fs/nilfs2/btree.c > +++ b/fs/nilfs2/btree.c > @@ -1949,8 +1949,7 @@ int nilfs_btree_convert_and_insert(struct > nilfs_bmap *btree, > =C2=A0 > =C2=A0static int nilfs_btree_propagate_p(struct nilfs_bmap *btree, > =C2=A0 =C2=A0=C2=A0 struct nilfs_btree_path *path, > - =C2=A0=C2=A0 int level, > - =C2=A0=C2=A0 struct buffer_head *bh) > + =C2=A0=C2=A0 int level) > =C2=A0{ > =C2=A0 while ((++level < nilfs_btree_height(btree) - 1) && > =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 !buffer_dirty(path[level].bp_= bh)) > @@ -2068,7 +2067,6 @@ static int > nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree, > =C2=A0static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree= , > =C2=A0 =C2=A0=C2=A0 struct nilfs_btree_path > *path, > =C2=A0 =C2=A0=C2=A0 int minlevel, int > maxlevel, > - =C2=A0=C2=A0 struct buffer_head *bh, > =C2=A0 =C2=A0=C2=A0 struct inode *dat) > =C2=A0{ > =C2=A0 int level; > @@ -2082,7 +2080,7 @@ static void > nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree, > =C2=A0 > =C2=A0static int nilfs_btree_propagate_v(struct nilfs_bmap *btree, > =C2=A0 =C2=A0=C2=A0 struct nilfs_btree_path *path, > - =C2=A0=C2=A0 int level, struct buffer_head > *bh) > + =C2=A0=C2=A0 int level) > =C2=A0{ > =C2=A0 int maxlevel =3D 0, ret; > =C2=A0 struct nilfs_btree_node *parent; > @@ -2090,8 +2088,6 @@ static int nilfs_btree_propagate_v(struct > nilfs_bmap *btree, > =C2=A0 __u64 ptr; > =C2=A0 int ncmax; > =C2=A0 > - get_bh(bh); > - path[level].bp_bh =3D bh; > =C2=A0 ret =3D nilfs_btree_prepare_propagate_v(btree, path, level, > &maxlevel, > =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 dat); > =C2=A0 if (ret < 0) > @@ -2107,16 +2103,15 @@ static int nilfs_btree_propagate_v(struct > nilfs_bmap *btree, > =C2=A0 goto out; > =C2=A0 } > =C2=A0 > - nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, > bh, dat); > + nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, > dat); > =C2=A0 > =C2=A0 out: > - brelse(path[level].bp_bh); > - path[level].bp_bh =3D NULL; > =C2=A0 return ret; > =C2=A0} > =C2=A0 > =C2=A0static int nilfs_btree_propagate(struct nilfs_bmap *btree, > - struct buffer_head *bh) > + struct buffer_head *bh, > + struct buffer_head **out_bh) > =C2=A0{ > =C2=A0 struct nilfs_btree_path *path; > =C2=A0 struct nilfs_btree_node *node; > @@ -2126,8 +2121,10 @@ static int nilfs_btree_propagate(struct > nilfs_bmap *btree, > =C2=A0 WARN_ON(!buffer_dirty(bh)); > =C2=A0 > =C2=A0 path =3D nilfs_btree_alloc_path(); > - if (path =3D=3D NULL) > + if (unlikely(!path)) { > + brelse(bh); > =C2=A0 return -ENOMEM; > + } > =C2=A0 > =C2=A0 if (buffer_nilfs_node(bh)) { > =C2=A0 node =3D (struct nilfs_btree_node *)bh->b_data; > @@ -2150,20 +2147,45 @@ static int nilfs_btree_propagate(struct > nilfs_bmap *btree, > =C2=A0 goto out; > =C2=A0 } > =C2=A0 > - ret =3D NILFS_BMAP_USE_VBN(btree) ? > - nilfs_btree_propagate_v(btree, path, level, bh) : > - nilfs_btree_propagate_p(btree, path, level, bh); > + if (NILFS_BMAP_USE_VBN(btree)) { > + /* > + * Lend the buffer reference to path[level].bp_bh > during the > + * nilfs_btree_propagate_v() call and retrieve it > for output > + * afterward, as the buffer may be replaced due to > an update > + * for its virtual block number. > + */ > + path[level].bp_bh =3D bh; > + > + ret =3D nilfs_btree_propagate_v(btree, path, level); > + > + bh =3D path[level].bp_bh; > + path[level].bp_bh =3D NULL; > + } else { > + ret =3D nilfs_btree_propagate_p(btree, path, level); > + } > =C2=A0 > =C2=A0 out: > =C2=A0 nilfs_btree_free_path(path); > =C2=A0 > + if (out_bh && likely(!ret)) > + *out_bh =3D bh; > + else > + brelse(bh); > =C2=A0 return ret; > =C2=A0} > =C2=A0 > =C2=A0static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree, > - =C2=A0=C2=A0=C2=A0 struct buffer_head *bh) > + =C2=A0=C2=A0=C2=A0 struct buffer_head *bh, > + =C2=A0=C2=A0=C2=A0 struct buffer_head **out_bh) > =C2=A0{ > - return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh- > >b_blocknr); > + int ret =3D nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), > + =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bh->b_blocknr); > + > + if (out_bh && likely(!ret)) > + *out_bh =3D bh; > + else > + brelse(bh); > + return ret; > =C2=A0} > =C2=A0 > =C2=A0static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree, > diff --git a/fs/nilfs2/direct.c b/fs/nilfs2/direct.c > index 18a700db76ed..60211ee9aebe 100644 > --- a/fs/nilfs2/direct.c > +++ b/fs/nilfs2/direct.c > @@ -259,35 +259,37 @@ int nilfs_direct_delete_and_convert(struct > nilfs_bmap *bmap, > =C2=A0} > =C2=A0 > =C2=A0static int nilfs_direct_propagate(struct nilfs_bmap *bmap, > - =C2=A0 struct buffer_head *bh) > + =C2=A0 struct buffer_head *bh, > + =C2=A0 struct buffer_head **out_bh) > =C2=A0{ > =C2=A0 struct nilfs_palloc_req oldreq, newreq; > =C2=A0 struct inode *dat; > =C2=A0 __u64 key; > =C2=A0 __u64 ptr; > - int ret; > + int ret =3D 0; > =C2=A0 > =C2=A0 if (!NILFS_BMAP_USE_VBN(bmap)) > - return 0; > + goto out; > =C2=A0 > + ret =3D -EINVAL; > =C2=A0 if (WARN_ON_ONCE(buffer_nilfs_node(bh))) > - return -EINVAL; > + goto out; > =C2=A0 > =C2=A0 dat =3D nilfs_bmap_get_dat(bmap); > =C2=A0 key =3D nilfs_bmap_data_get_key(bmap, bh); > =C2=A0 if (WARN_ON_ONCE(key > NILFS_DIRECT_KEY_MAX)) > - return -EINVAL; > + goto out; > =C2=A0 > =C2=A0 ptr =3D nilfs_direct_get_ptr(bmap, key); > =C2=A0 if (ptr =3D=3D NILFS_BMAP_INVALID_PTR) > - return -EINVAL; > + goto out; > =C2=A0 > =C2=A0 if (!buffer_nilfs_volatile(bh)) { > =C2=A0 oldreq.pr_entry_nr =3D ptr; > =C2=A0 newreq.pr_entry_nr =3D ptr; > =C2=A0 ret =3D nilfs_dat_prepare_update(dat, &oldreq, > &newreq); > =C2=A0 if (ret < 0) > - return ret; > + goto out; > =C2=A0 nilfs_dat_commit_update(dat, &oldreq, &newreq, > =C2=A0 bmap->b_ptr_type =3D=3D > NILFS_BMAP_PTR_VS); > =C2=A0 set_buffer_nilfs_volatile(bh); > @@ -295,6 +297,11 @@ static int nilfs_direct_propagate(struct > nilfs_bmap *bmap, > =C2=A0 } else > =C2=A0 ret =3D nilfs_dat_mark_dirty(dat, ptr); > =C2=A0 > +out: > + if (out_bh && likely(!ret)) > + *out_bh =3D bh; > + else > + brelse(bh); > =C2=A0 return ret; > =C2=A0} > =C2=A0 > diff --git a/fs/nilfs2/segment.c b/fs/nilfs2/segment.c > index 5765959617fe..34016804af14 100644 > --- a/fs/nilfs2/segment.c > +++ b/fs/nilfs2/segment.c > @@ -586,16 +586,18 @@ static int nilfs_segctor_add_file_block(struct > nilfs_sc_info *sci, > =C2=A0static int nilfs_collect_file_data(struct nilfs_sc_info *sci, > =C2=A0 =C2=A0=C2=A0 struct buffer_head *bh, struct > inode *inode) > =C2=A0{ > + struct buffer_head *out_bh; > =C2=A0 int err; > =C2=A0 > - err =3D nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh); > + err =3D nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh, > &out_bh); > =C2=A0 if (err < 0) > =C2=A0 return err; > =C2=A0 > - err =3D nilfs_segctor_add_file_block(sci, bh, inode, > + err =3D nilfs_segctor_add_file_block(sci, out_bh, inode, > =C2=A0 =C2=A0=C2=A0 sizeof(struct > nilfs_binfo_v)); > =C2=A0 if (!err) > =C2=A0 sci->sc_datablk_cnt++; > + brelse(out_bh); > =C2=A0 return err; > =C2=A0} > =C2=A0 > @@ -603,15 +605,19 @@ static int nilfs_collect_file_node(struct > nilfs_sc_info *sci, > =C2=A0 =C2=A0=C2=A0 struct buffer_head *bh, > =C2=A0 =C2=A0=C2=A0 struct inode *inode) > =C2=A0{ > - return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh); > + return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh, > NULL); > =C2=A0} > =C2=A0 > =C2=A0static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci, > =C2=A0 =C2=A0=C2=A0 struct buffer_head *bh, > =C2=A0 =C2=A0=C2=A0 struct inode *inode) > =C2=A0{ > + int err; > + > =C2=A0 WARN_ON(!buffer_dirty(bh)); > - return nilfs_segctor_add_file_block(sci, bh, inode, > sizeof(__le64)); > + err =3D nilfs_segctor_add_file_block(sci, bh, inode, > sizeof(__le64)); > + brelse(bh); > + return err; > =C2=A0} > =C2=A0 > =C2=A0static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci, > @@ -643,24 +649,30 @@ static const struct nilfs_sc_operations > nilfs_sc_file_ops =3D { > =C2=A0static int nilfs_collect_dat_data(struct nilfs_sc_info *sci, > =C2=A0 =C2=A0 struct buffer_head *bh, struct > inode *inode) > =C2=A0{ > + struct buffer_head *out_bh; > =C2=A0 int err; > =C2=A0 > - err =3D nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh); > + err =3D nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh, > &out_bh); > =C2=A0 if (err < 0) > =C2=A0 return err; > =C2=A0 > - err =3D nilfs_segctor_add_file_block(sci, bh, inode, > sizeof(__le64)); > + err =3D nilfs_segctor_add_file_block(sci, out_bh, inode, > sizeof(__le64)); > =C2=A0 if (!err) > =C2=A0 sci->sc_datablk_cnt++; > + brelse(out_bh); > =C2=A0 return err; > =C2=A0} > =C2=A0 > =C2=A0static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci, > =C2=A0 =C2=A0 struct buffer_head *bh, struct > inode *inode) > =C2=A0{ > + int err; > + > =C2=A0 WARN_ON(!buffer_dirty(bh)); > - return nilfs_segctor_add_file_block(sci, bh, inode, > - =C2=A0=C2=A0=C2=A0 sizeof(struct > nilfs_binfo_dat)); > + err =3D nilfs_segctor_add_file_block(sci, bh, inode, > + =C2=A0=C2=A0 sizeof(struct > nilfs_binfo_dat)); > + brelse(bh); > + return err; > =C2=A0} > =C2=A0 > =C2=A0static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci, > @@ -1008,7 +1020,6 @@ static int nilfs_segctor_apply_buffers(struct > nilfs_sc_info *sci, > =C2=A0 list_for_each_entry_safe(bh, n, listp, > b_assoc_buffers) { > =C2=A0 list_del_init(&bh->b_assoc_buffers); > =C2=A0 err =3D collect(sci, bh, inode); > - brelse(bh); > =C2=A0 if (unlikely(err)) > =C2=A0 goto dispose_buffers; > =C2=A0 } Applied. Thanks, Slava.