From patchwork Fri Jul 4 07:29:17 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 4478331 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id D764EBEEAA for ; Fri, 4 Jul 2014 07:28:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F0247203F1 for ; Fri, 4 Jul 2014 07:28:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EFC7220416 for ; Fri, 4 Jul 2014 07:28:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751726AbaGDH2R (ORCPT ); Fri, 4 Jul 2014 03:28:17 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:53978 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751082AbaGDH2Q (ORCPT ); Fri, 4 Jul 2014 03:28:16 -0400 X-IronPort-AV: E=Sophos;i="5.00,830,1396972800"; d="scan'208";a="32835581" Received: from localhost (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 04 Jul 2014 15:25:31 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id s647SCjp028781; Fri, 4 Jul 2014 15:28:12 +0800 Received: from adam-work.lan (10.167.226.24) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Fri, 4 Jul 2014 15:28:14 +0800 From: Qu Wenruo To: , Subject: [PATCH v4] btrfs-progs: Add minimum device size check Date: Fri, 4 Jul 2014 15:29:17 +0800 Message-ID: <1404458957-23621-1-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.0.1 MIME-Version: 1.0 X-Originating-IP: [10.167.226.24] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Btrfs has global block reservation, so even mkfs.btrfs can execute without problem, there is still a possibility that the filesystem can't be mounted. For example when mkfs.btrfs on a 8M file on x86_64 platform, kernel will refuse to mount due to ENOSPC, since system block group takes 4M and mixed block group takes 4M, and global block reservation will takes all the 4M from mixed block group, which makes btrfs unable to create uuid tree. This patch will add minimum device size check before actually mkfs. The minimum size calculation uses a simplified one: minimum_size_for_each_dev = 2 * (system block group + global block rsv) and global block rsv = leafsize << 10 Signed-off-by: Qu Wenruo [minor wording tweaks in messages] Signed-off-by: David Sterba --- changelog: v2: Use leafsize to get consistent with kernel global_block_rsv calculation. v3: Change words in error string. Use separate inline function to calculate global block rsv size. Rename the minimum device size calculation function. v4: Fix a variant reuse bug which causes mkfs.btrfs use wrong first device. --- mkfs.c | 29 +++++++++++++++++++++++++++++ utils.c | 20 ++++++++++++++++++++ utils.h | 22 ++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/mkfs.c b/mkfs.c index 7695c01..e2cc9d2 100644 --- a/mkfs.c +++ b/mkfs.c @@ -1451,6 +1451,35 @@ int main(int ac, char **av) } } + /* Check device/block_count after the leafsize is determined */ + if (block_count && block_count < btrfs_min_dev_size(leafsize)) { + fprintf(stderr, + "Size '%llu' is too small to make a usable filesystem\n", + block_count); + fprintf(stderr, + "Minimum size for btrfs filesystem is %llu\n", + btrfs_min_dev_size(leafsize)); + exit(1); + } + for (i = saved_optind; i < saved_optind + dev_cnt; i++) { + char *path; + path = av[i]; + ret = test_minimum_size(path, leafsize); + if (ret < 0) { + fprintf(stderr, "Failed to check size for '%s': %s\n", + path, strerror(-ret)); + exit (1); + } + if (ret > 0) { + fprintf(stderr, + "'%s' is too small to make a usable filesystem\n", + path); + fprintf(stderr, + "Minimum size for each btrfs device is %llu.\n", + btrfs_min_dev_size(leafsize)); + exit(1); + } + } ret = test_num_disk_vs_raid(metadata_profile, data_profile, dev_cnt, mixed, estr); if (ret) { diff --git a/utils.c b/utils.c index 46c3a43..c139eb2 100644 --- a/utils.c +++ b/utils.c @@ -2344,3 +2344,23 @@ int find_mount_root(const char *path, char **mount_root) free(longest_match); return ret; } + +int test_minimum_size(const char *file, u32 leafsize) +{ + int fd; + struct stat statbuf; + + fd = open(file, O_RDONLY); + if (fd < 0) + return -errno; + if (stat(file, &statbuf) < 0) { + close(fd); + return -errno; + } + if (btrfs_device_size(fd, &statbuf) < btrfs_min_dev_size(leafsize)) { + close(fd); + return 1; + } + close(fd); + return 0; +} diff --git a/utils.h b/utils.h index 37fe1ba..6599ee4 100644 --- a/utils.h +++ b/utils.h @@ -105,4 +105,26 @@ int get_device_info(int fd, u64 devid, struct btrfs_ioctl_dev_info_args *di_args); int test_uuid_unique(char *fs_uuid); +int test_minimum_size(const char *file, u32 leafsize); + +/* + * Btrfs minimum size calculation is complicated, it should include at least: + * 1. system group size + * 2. minimum global block reserve + * 3. metadata used at mkfs + * 4. space reservation to create uuid for first mount. + * Also, raid factor should also be taken into consideration. + * To avoid the overkill calculation, (system group + global block rsv) * 2 + * for *EACH* device should be good enough. + */ +static inline u64 btrfs_min_global_blk_rsv_size(u32 leafsize) +{ + return leafsize << 10; +} +static inline u64 btrfs_min_dev_size(u32 leafsize) +{ + return 2 * (BTRFS_MKFS_SYSTEM_GROUP_SIZE + + btrfs_min_global_blk_rsv_size(leafsize)); +} + #endif