Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1549686pxb; Thu, 16 Sep 2021 09:42:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUy7Y1KbZ4WReTuM87rubuiXL4o8TrmNxYue04LdXVZCVBvpaHljmSOCbZjzla21pq9Ctq X-Received: by 2002:a05:6e02:1a23:: with SMTP id g3mr4602731ile.139.1631810572208; Thu, 16 Sep 2021 09:42:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631810572; cv=none; d=google.com; s=arc-20160816; b=zKG4jIv0kLPjFQPwU08dqb3a0k8VrpUXvwqjvQyISukP2sB1TVQeYOLfMw4LUNMJfF mDCoQijkp1ZXbhz9RmWRRWy4afHaAu//fyrwlT80l4KWO2eYIR+GRSi9CIlpl4lHf5lo nOP8en9ctHgeTRzmgvsuUmTt3GoIufXV5pKnUYVL2E9CPL/eREaWyyonN/zXHRHJq0jM WntWXeJqysMfFobFwpRiFqKU8jIWvOx9KgIuDOr2X4wYF+gioEMQFS2pPWnweevmSJVb sj5wvML97g1in6y78L5l85dqwRX1UBZraZ0uRmf3j0WMjryP6MBmuK0BQjO9wbVGu0v8 qXXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=hvs1knEG7duLX/Af6zMH9HwaP40ek7x5N8HSEW1xoNQ=; b=nUZx+2y3fYN6XTok+RCaGXn7VwNJQ1aEpRGajrj5IcMyuZwJNf0frp/hETfYe2Z6u6 R2bIwQ3vfU1Ib/jXQSNvWjF4Q2bWrQcWyfRHbBQfAC4t2D9gMGIaTrGMpMItqKfMhLzv crpT8IwYarr12zk6DgGutgb1hZaLqHpeGRqORDO1mW2Na1JNwXDsO5Wc6xZv1+XZ0qBZ g1OaR608cztc4A4y+22kiXrFZqKfeiysoa2rExrURRfbmR9t++pxSnwnWC/Y4wgKgQy4 xv6IOpmIfmuAzXEVI4jJelrtHMLYf7Blq6Sdsn4sSoioXsuBA8TzkvkiSukqqrUEjN8Y 8zJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0Zb8TQEL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h25si3165883ioj.39.2021.09.16.09.42.40; Thu, 16 Sep 2021 09:42:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=0Zb8TQEL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243917AbhIPQlK (ORCPT + 99 others); Thu, 16 Sep 2021 12:41:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:44326 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244205AbhIPQfA (ORCPT ); Thu, 16 Sep 2021 12:35:00 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7FF906197A; Thu, 16 Sep 2021 16:21:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631809263; bh=SSlOjnUmSWtJPnuxZ1cXgyAmRvG3KANBAym5dPZOjj0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0Zb8TQELqWYgPP4NAKzh6BC/hkkgJAOoxdEjvgecymyHEC2YnaUKNi7+etejOuDir UrggyvHNDQT6gHZxFyADvOHDXn627jkNt8LfpHW/GFNkjtQU0jMAAHfXn5ggfX7GRI f5APVQw6dz4EQLgZ4GicSScwE3lj/ARcTA65dSgo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yangtao Li , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 5.13 090/380] f2fs: reduce the scope of setting fsck tag when de->name_len is zero Date: Thu, 16 Sep 2021 17:57:27 +0200 Message-Id: <20210916155807.102250488@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210916155803.966362085@linuxfoundation.org> References: <20210916155803.966362085@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yangtao Li [ Upstream commit d4bf15a7ce172d186d400d606adf4f34a59130d6 ] I recently found a case where de->name_len is 0 in f2fs_fill_dentries() easily reproduced, and finally set the fsck flag. Thread A Thread B - f2fs_readdir - f2fs_read_inline_dir - ctx->pos = d.max - f2fs_add_dentry - f2fs_add_inline_entry - do_convert_inline_dir - f2fs_add_regular_entry - f2fs_readdir - f2fs_fill_dentries - set_sbi_flag(sbi, SBI_NEED_FSCK) Process A opens the folder, and has been reading without closing it. During this period, Process B created a file under the folder (occupying multiple f2fs_dir_entry, exceeding the d.max of the inline dir). After creation, process A uses the d.max of inline dir to read it again, and it will read that de->name_len is 0. And Chao pointed out that w/o inline conversion, the race condition still can happen as below: dir_entry1: A dir_entry2: B dir_entry3: C free slot: _ ctx->pos: ^ Thread A is traversing directory, ctx-pos moves to below position after readdir() by thread A: AAAABBBB___ ^ Then thread B delete dir_entry2, and create dir_entry3. Thread A calls readdir() to lookup dirents starting from middle of new dirent slots as below: AAAACCCCCC_ ^ In these scenarios, the file system is not damaged, and it's hard to avoid it. But we can bypass tagging FSCK flag if: a) bit_pos (:= ctx->pos % d->max) is non-zero and b) before bit_pos moves to first valid dir_entry. Fixes: ddf06b753a85 ("f2fs: fix to trigger fsck if dirent.name_len is zero") Signed-off-by: Yangtao Li [Chao: clean up description] Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/dir.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c index 96dcc4aca639..c821015c0a46 100644 --- a/fs/f2fs/dir.c +++ b/fs/f2fs/dir.c @@ -995,6 +995,7 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode); struct blk_plug plug; bool readdir_ra = sbi->readdir_ra == 1; + bool found_valid_dirent = false; int err = 0; bit_pos = ((unsigned long)ctx->pos % d->max); @@ -1009,13 +1010,15 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, de = &d->dentry[bit_pos]; if (de->name_len == 0) { + if (found_valid_dirent || !bit_pos) { + printk_ratelimited( + "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.", + KERN_WARNING, sbi->sb->s_id, + le32_to_cpu(de->ino)); + set_sbi_flag(sbi, SBI_NEED_FSCK); + } bit_pos++; ctx->pos = start_pos + bit_pos; - printk_ratelimited( - "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.", - KERN_WARNING, sbi->sb->s_id, - le32_to_cpu(de->ino)); - set_sbi_flag(sbi, SBI_NEED_FSCK); continue; } @@ -1058,6 +1061,7 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d, f2fs_ra_node_page(sbi, le32_to_cpu(de->ino)); ctx->pos = start_pos + bit_pos; + found_valid_dirent = true; } out: if (readdir_ra) -- 2.30.2