Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp119784ybi; Wed, 29 May 2019 17:56:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqyNPkLNNEy6DbN99PP2zHz60AkDeSMz6JGmyBh3V8FnfSplNHDu2Z82J1+4BgFKS2BT/ftA X-Received: by 2002:a63:4d56:: with SMTP id n22mr968008pgl.307.1559177767484; Wed, 29 May 2019 17:56:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559177767; cv=none; d=google.com; s=arc-20160816; b=P6lUwRwOqEx7emVR0gCmUY1w2X4Zj5/g5P6jsdrfc3ghRawkvJ+TSSMpoOqArsjwT2 t2kRoeWLjjpdT6bFJC5qhZRftQ54jpL7MTUFOayv8eiUo5VPCPDdvIiJttpzJOoaIibO UF0IFe7iHR3nMnZu0RDwog+qdaeqOxRFroIl029Yd0tPNObfRyh7Z8kQ16s7MpVeVdeQ RptRPiCfjOR/SlMDuvxeZ9OBq6EgBNc66T7vWXAPadl/RvQ+3T8DN3FjOxNuPnAc1tpj xe8Lz1c4konKt/bTWXKfxbqJy9y8HDYOmcw+IcfBn5E3GvfPvUWgw+SBRCjZLhRx2UX6 Ilyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=pFDT61UYE6+362eUPJJuMi+49iBrHpoUZVksNkDJA2o=; b=CoflSCS38X0pE3HOGkwZH8uf+C1pW4Rk/ViKOsl+Gypga1D87ktigbD3sb4tWl9T9+ hZMYHmnxxnjTIoFXDtgAtjSDIY0WGGpE5/Ogce8iRLFw9rLCflOVIpaSjEXG+8xFhdUI /o2PPWuGnEizse6yL9W6YL7t742DJ/M6s04a/etw2/m36yBOUAT9ePt4sXpTVa6W1YsA sd6Txq1GvC7FzNLeGYMqmsuDYRwhVlX2uc/Xf4dqYLDlvvqO8pBs5nLYdRKnbVp8qnLk LKHfwiA+iW6KA2TUsk/QISVqC0cM9ewHndbVu9QTpsixaulfc/EaLhVIevKYvXd2RrIr QX7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tq0C+xLp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 192si1661046pfz.42.2019.05.29.17.55.51; Wed, 29 May 2019 17:56:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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=@google.com header.s=20161025 header.b=tq0C+xLp; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727263AbfE3Ax3 (ORCPT + 99 others); Wed, 29 May 2019 20:53:29 -0400 Received: from mail-qk1-f201.google.com ([209.85.222.201]:36101 "EHLO mail-qk1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727247AbfE3Ax0 (ORCPT ); Wed, 29 May 2019 20:53:26 -0400 Received: by mail-qk1-f201.google.com with SMTP id a12so3504786qkb.3 for ; Wed, 29 May 2019 17:53:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=pFDT61UYE6+362eUPJJuMi+49iBrHpoUZVksNkDJA2o=; b=tq0C+xLpxnScJrE7KGvLRNW1EvyW7w8fxZLYhMG38iqb6B65iu+pJNUwkSmSw9Wsl3 /9PNywbClVW9JA9DR/Dh1tSwsF6mMdgLrRvRMdWic+A+8I5sxRkeLEB1Ob2MAWWJm9jL 6ev53ORFgvokwK4cHk17Y7Ckh2RQmG74ba2tYgemlhSg7VkheKlADWDbsqwFIwOUSizD ZYBWe2VO+7gH+pPgHLcdOlhkOX4cS9lzTtfup83L3evOB0ATtVcM/T1mPq4hDcSUF5Uc u/Y86bsvfsu8KvdNmkA/2KSKxTBI0UsTGqIlC4MAGZr2YM/9PfAUtAnGcsXxF7iTVQkS YuCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=pFDT61UYE6+362eUPJJuMi+49iBrHpoUZVksNkDJA2o=; b=CicrLPPkhtqWT71Tm2ePAYq3Gmu5inF1gFKYJYK9J9dJ3pg4aSBpvE+LK0n59XBWri JXPCAHxyiFXlwxV2cpTs/8OqXsCj6iINes+P9Ija2980mYSEX9xhXAMU3ytNxh09wSlg hKlwYOtb9DZ5RiqyN4eHC6tiJcRox+lgGl16FqoIQJc6xqye3N62W5uQIR29lDlABt2m B/IX20Vqcv0YbdR3LtyfSsskkzeZAc0TlobZN48Plo+EdM49ZyDlt5w3KQ81b8IL5rtp xNZJ/wdU52hi08GAe76dsykCdDYiC9AMuFEJxMfx8CoXxn4DtR7Uqz9uLHAkLWvp+OMw LOgg== X-Gm-Message-State: APjAAAUQRBHZdsBVxkfoGsjcUIcnbiOg/l3FlkKcPvbP+H+C8mnd6UQj dd/RlCTrdzhrTK/WXbvR45gxEAHQMDg= X-Received: by 2002:a37:660b:: with SMTP id a11mr723548qkc.342.1559177606029; Wed, 29 May 2019 17:53:26 -0700 (PDT) Date: Wed, 29 May 2019 17:49:05 -0700 In-Reply-To: <20190530004906.261170-1-drosen@google.com> Message-Id: <20190530004906.261170-4-drosen@google.com> Mime-Version: 1.0 References: <20190530004906.261170-1-drosen@google.com> X-Mailer: git-send-email 2.22.0.rc1.257.g3120a18244-goog Subject: [PATCH v3 3/4] f2fs: Fix accounting for unusable blocks From: Daniel Rosenberg To: Jaegeuk Kim , Chao Yu , Jonathan Corbet , linux-f2fs-devel@lists.sourceforge.net Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, kernel-team@android.com, Daniel Rosenberg Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fixes possible underflows when dealing with unusable blocks. Signed-off-by: Daniel Rosenberg --- fs/f2fs/f2fs.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 9b3d9977cd1ef..a39cc4ffeb4b1 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -1769,8 +1769,12 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi, if (!__allow_reserved_blocks(sbi, inode, true)) avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks; - if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) - avail_user_block_count -= sbi->unusable_block_count; + if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { + if (avail_user_block_count > sbi->unusable_block_count) + avail_user_block_count = 0; + else + avail_user_block_count -= sbi->unusable_block_count; + } if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) { diff = sbi->total_valid_block_count - avail_user_block_count; if (diff > *count) @@ -1970,7 +1974,7 @@ static inline int inc_valid_node_count(struct f2fs_sb_info *sbi, struct inode *inode, bool is_inode) { block_t valid_block_count; - unsigned int valid_node_count; + unsigned int valid_node_count, user_block_count; int err; if (is_inode) { @@ -1997,10 +2001,11 @@ static inline int inc_valid_node_count(struct f2fs_sb_info *sbi, if (!__allow_reserved_blocks(sbi, inode, false)) valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks; + user_block_count = sbi->user_block_count; if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) - valid_block_count += sbi->unusable_block_count; + user_block_count -= sbi->unusable_block_count; - if (unlikely(valid_block_count > sbi->user_block_count)) { + if (unlikely(valid_block_count > user_block_count)) { spin_unlock(&sbi->stat_lock); goto enospc; } -- 2.22.0.rc1.257.g3120a18244-goog