Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2415847ybi; Sun, 9 Jun 2019 11:12:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqzSC+rgRDNdFY6hsOVrt5oYSEK0WIy9bquXK4XjFAanwaqEB2kT9tM4XW2VXR5mTEB4c/Uv X-Received: by 2002:a17:902:7448:: with SMTP id e8mr56520145plt.222.1560103927384; Sun, 09 Jun 2019 11:12:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560103927; cv=none; d=google.com; s=arc-20160816; b=bXVVMPFhysUt7fOyB5TbaQ95LxjMggpjo9oY/8HFdraVRjnaONrdr1RDmlPe5g9Irc PAEM2Ysl69SK07S6gmD0BS9EbUFSIZs3g95xmbGCE5NsmpRdpAOKn2WSjA99QtgTCCnO p6h1JHv13Ip7GgQI4wblA/gaOASkIMCrX3UGT5nx3tsyLLYnE1lFwBRUGFIpwgf26f2g wHuag1a6gsEbZNW4ipeo6wGMGtJWimSvBKzBaD8RLZ3Buh65CsdZjlN0uWFSyzn7Gxpl X352/YVwnUnRC0dLlaI0eygThQVfFHmL6puJ3lf1SSQKM9HtQwkEqikK8IHz0NQcc62c eaAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HRJpe6wxnQEYxILG/RX6TjDL0g08JlqaXxlyhYoN4jk=; b=RvDXBMe4X1nr23Xal4dXJXVjQkCD/zfn3w2uHZPQUyYUfcSSHFLju8u5Ufg8Kmkeki MyT/X9lSBJ82rMaveNYVJtfNqGqJ11jDp9BicgmRrSw+liBR8/Tkm6y6ikKUNnXjpPxp Sh5A5BG/xaSMXuTc2Rp9VK+SRqAyj9ynIDKMo6BYGjUEisMHT4O82n6moK2z83Z4xmjT hoqXTyNy2e3PNPsMY07AuoGzTG4WP5gToMEX7kOoc/VO0216omMVXc2D3/bdIEpVkI3z UCd3QNvtJx1Y+ykXezFQFVqSrbBiqHpSUKc1DbK+Ac/L6GRAjX/I+D1k8kI+jbQHxHgT WxRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nGMJKHC2; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j3si7738485pgh.445.2019.06.09.11.11.51; Sun, 09 Jun 2019 11:12: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=@kernel.org header.s=default header.b=nGMJKHC2; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732556AbfFIQ4T (ORCPT + 99 others); Sun, 9 Jun 2019 12:56:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:58674 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732932AbfFIQ4P (ORCPT ); Sun, 9 Jun 2019 12:56:15 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E6390207E0; Sun, 9 Jun 2019 16:56:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1560099374; bh=t8+aEphbJ7n7GugeXeIFEvlLemjYYNP6cZfeKKQBKm0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nGMJKHC2TDxcq27HO3HM9Q7O9HlwIYcymxM4/P6Se8+XauzhCLTb28cPj78E2PTxH /vLwjZhhwfWmtuC0Bdx+/wt808gjCQyIFi0GJYC0HOIHByOQzQQMsgAtiMitAPqqKX uxwFH9DpoyZWvvqK+AMRJYHThSH3Ui9CXVH2r8aE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Coly Li , Hannes Reinecke , Jens Axboe Subject: [PATCH 4.4 020/241] bcache: never set KEY_PTRS of journal key to 0 in journal_reclaim() Date: Sun, 9 Jun 2019 18:39:22 +0200 Message-Id: <20190609164148.365791808@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190609164147.729157653@linuxfoundation.org> References: <20190609164147.729157653@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Coly Li commit 1bee2addc0c8470c8aaa65ef0599eeae96dd88bc upstream. In journal_reclaim() ja->cur_idx of each cache will be update to reclaim available journal buckets. Variable 'int n' is used to count how many cache is successfully reclaimed, then n is set to c->journal.key by SET_KEY_PTRS(). Later in journal_write_unlocked(), a for_each_cache() loop will write the jset data onto each cache. The problem is, if all jouranl buckets on each cache is full, the following code in journal_reclaim(), 529 for_each_cache(ca, c, iter) { 530 struct journal_device *ja = &ca->journal; 531 unsigned int next = (ja->cur_idx + 1) % ca->sb.njournal_buckets; 532 533 /* No space available on this device */ 534 if (next == ja->discard_idx) 535 continue; 536 537 ja->cur_idx = next; 538 k->ptr[n++] = MAKE_PTR(0, 539 bucket_to_sector(c, ca->sb.d[ja->cur_idx]), 540 ca->sb.nr_this_dev); 541 } 542 543 bkey_init(k); 544 SET_KEY_PTRS(k, n); If there is no available bucket to reclaim, the if() condition at line 534 will always true, and n remains 0. Then at line 544, SET_KEY_PTRS() will set KEY_PTRS field of c->journal.key to 0. Setting KEY_PTRS field of c->journal.key to 0 is wrong. Because in journal_write_unlocked() the journal data is written in following loop, 649 for (i = 0; i < KEY_PTRS(k); i++) { 650-671 submit journal data to cache device 672 } If KEY_PTRS field is set to 0 in jouranl_reclaim(), the journal data won't be written to cache device here. If system crahed or rebooted before bkeys of the lost journal entries written into btree nodes, data corruption will be reported during bcache reload after rebooting the system. Indeed there is only one cache in a cache set, there is no need to set KEY_PTRS field in journal_reclaim() at all. But in order to keep the for_each_cache() logic consistent for now, this patch fixes the above problem by not setting 0 KEY_PTRS of journal key, if there is no bucket available to reclaim. Signed-off-by: Coly Li Reviewed-by: Hannes Reinecke Cc: stable@vger.kernel.org Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- drivers/md/bcache/journal.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) --- a/drivers/md/bcache/journal.c +++ b/drivers/md/bcache/journal.c @@ -513,11 +513,11 @@ static void journal_reclaim(struct cache ca->sb.nr_this_dev); } - bkey_init(k); - SET_KEY_PTRS(k, n); - - if (n) + if (n) { + bkey_init(k); + SET_KEY_PTRS(k, n); c->journal.blocks_free = c->sb.bucket_size >> c->block_bits; + } out: if (!journal_full(&c->journal)) __closure_wake_up(&c->journal.wait); @@ -641,6 +641,9 @@ static void journal_write_unlocked(struc ca->journal.seq[ca->journal.cur_idx] = w->data->seq; } + /* If KEY_PTRS(k) == 0, this jset gets lost in air */ + BUG_ON(i == 0); + atomic_dec_bug(&fifo_back(&c->journal.pin)); bch_journal_next(&c->journal); journal_reclaim(c);