Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1260967pxa; Thu, 20 Aug 2020 07:01:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzrTDl5Y8Ei0UQhqlyB52Dk8YeZCwM5IRApt5KWMWzOZK0iFb4WJX5O4YtZAB90QSRMb2Q3 X-Received: by 2002:a05:6402:1e2:: with SMTP id i2mr2973702edy.70.1597932109026; Thu, 20 Aug 2020 07:01:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597932109; cv=none; d=google.com; s=arc-20160816; b=uD6qSZ6/zrJr/TLj1eHAqX9VR7Iuf6WKmjkcW8O3f/jdfTCgbOGSXd5xSENaf4GFNK ZuWAJtd7wM7VGBYNU8RWk1yXj/xy8+PLsUTBE4L3gvIEMhx6nCp8joJBpL6sF8f+5fo7 XTOgHk87xF17aGt1/As5rHVe6B2fWpte2BExI42kKrA24I2UEk+V2nYr2Y4i4pfZSJV+ EOCWdtqFxGlU97HctT5KMxArtDGYWSo2zA0VImwFEY4LMumq0EuMMBGzVf21q1pWdU6g gepzaexP9JzwtQYncKBj7HQ1oyQ43v07/qA99pJnK0HlBHJxlF5/ifBYYA6xK9NcyA0g q5Vg== 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=CmcOtavUiTuWCe+W0UdkiV0YMRVYki2I4rOxF2ZrQAU=; b=xbEsR1ZmpOI2TZOOKKn/hwDXeDOaAS9H97w2YTdOfgRby1yKowCIg8DdavfweR7835 AQZYiVJeAvu620UQzIRRb7JnrN96Kp4hdcFF5OQCJ1H9sYuRep0remVg1yfDq/PQPSp+ w9sKH8HhxxT2SVl5kplvd2XRozVCBADLh83T9CZBrTqmuiL7uIMB7kVj+DZa0IUATQOv mmkEZtD4YjrKOe4Zjiky2l976fMKCrBuAfygkh5jKS35U1yDQJfhLdNX648EYGhRNXQ8 uv/kNUkCNkWlCF0WRVIGJqfIIV8xDXwj7cAeEFVLC7jNAYb+Cy7EGvMMIb05Vn8/AvPh Zw1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=k39D4Fys; 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=fail (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 n1si1276413eju.121.2020.08.20.07.01.25; Thu, 20 Aug 2020 07:01:49 -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=@kernel.org header.s=default header.b=k39D4Fys; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728602AbgHTN7h (ORCPT + 99 others); Thu, 20 Aug 2020 09:59:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:60006 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726851AbgHTJYD (ORCPT ); Thu, 20 Aug 2020 05:24:03 -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 9811522CA1; Thu, 20 Aug 2020 09:24:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597915442; bh=hzqSWuJzDmtvb2qez9IAyU9QTeXi2mHqD9sN/gJRJcg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k39D4FyswGLj957dda8GIEDvXmDubusGtaZnJb2GlyQcUjBMybOpqur7y0Fp8sjmP FkDB9hboAu2cNOUiiHm2nETIcdm8bSeA0+MGd/plIQubvpOjPG/rbQTNKZhj1QYZ8E fVDtFd+c5W+xR913la+eulVhgatQDln8HOwle3JQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Greed Rong , Josef Bacik , Qu Wenruo , David Sterba Subject: [PATCH 5.8 015/232] btrfs: dont allocate anonymous block device for user invisible roots Date: Thu, 20 Aug 2020 11:17:46 +0200 Message-Id: <20200820091613.468585852@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200820091612.692383444@linuxfoundation.org> References: <20200820091612.692383444@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: Qu Wenruo commit 851fd730a743e072badaf67caf39883e32439431 upstream. [BUG] When a lot of subvolumes are created, there is a user report about transaction aborted: BTRFS: Transaction aborted (error -24) WARNING: CPU: 17 PID: 17041 at fs/btrfs/transaction.c:1576 create_pending_snapshot+0xbc4/0xd10 [btrfs] RIP: 0010:create_pending_snapshot+0xbc4/0xd10 [btrfs] Call Trace: create_pending_snapshots+0x82/0xa0 [btrfs] btrfs_commit_transaction+0x275/0x8c0 [btrfs] btrfs_mksubvol+0x4b9/0x500 [btrfs] btrfs_ioctl_snap_create_transid+0x174/0x180 [btrfs] btrfs_ioctl_snap_create_v2+0x11c/0x180 [btrfs] btrfs_ioctl+0x11a4/0x2da0 [btrfs] do_vfs_ioctl+0xa9/0x640 ksys_ioctl+0x67/0x90 __x64_sys_ioctl+0x1a/0x20 do_syscall_64+0x5a/0x110 entry_SYSCALL_64_after_hwframe+0x44/0xa9 ---[ end trace 33f2f83f3d5250e9 ]--- BTRFS: error (device sda1) in create_pending_snapshot:1576: errno=-24 unknown BTRFS info (device sda1): forced readonly BTRFS warning (device sda1): Skipping commit of aborted transaction. BTRFS: error (device sda1) in cleanup_transaction:1831: errno=-24 unknown [CAUSE] The error is EMFILE (Too many files open) and comes from the anonymous block device allocation. The ids are in a shared pool of size 1<<20. The ids are assigned to live subvolumes, ie. the root structure exists in memory (eg. after creation or after the root appears in some path). The pool could be exhausted if the numbers are not reclaimed fast enough, after subvolume deletion or if other system component uses the anon block devices. [WORKAROUND] Since it's not possible to completely solve the problem, we can only minimize the time the id is allocated to a subvolume root. Firstly, we can reduce the use of anon_dev by trees that are not subvolume roots, like data reloc tree. This patch will do extra check on root objectid, to skip roots that don't need anon_dev. Currently it's only data reloc tree and orphan roots. Reported-by: Greed Rong Link: https://lore.kernel.org/linux-btrfs/CA+UqX+NTrZ6boGnWHhSeZmEY5J76CTqmYjO2S+=tHJX7nb9DPw@mail.gmail.com/ CC: stable@vger.kernel.org # 4.4+ Reviewed-by: Josef Bacik Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/disk-io.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -1428,9 +1428,16 @@ static int btrfs_init_fs_root(struct btr spin_lock_init(&root->ino_cache_lock); init_waitqueue_head(&root->ino_cache_wait); - ret = get_anon_bdev(&root->anon_dev); - if (ret) - goto fail; + /* + * Don't assign anonymous block device to roots that are not exposed to + * userspace, the id pool is limited to 1M + */ + if (is_fstree(root->root_key.objectid) && + btrfs_root_refs(&root->root_item) > 0) { + ret = get_anon_bdev(&root->anon_dev); + if (ret) + goto fail; + } mutex_lock(&root->objectid_mutex); ret = btrfs_find_highest_objectid(root,