From patchwork Wed Mar 28 06:39:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 10312225 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E91E860325 for ; Wed, 28 Mar 2018 06:39:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D8BE7298FE for ; Wed, 28 Mar 2018 06:39:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CAFBA29929; Wed, 28 Mar 2018 06:39:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2055C298FE for ; Wed, 28 Mar 2018 06:39:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752072AbeC1Gj1 (ORCPT ); Wed, 28 Mar 2018 02:39:27 -0400 Received: from prv3-mh.provo.novell.com ([137.65.250.26]:52039 "EHLO prv3-mh.provo.novell.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750703AbeC1Gj0 (ORCPT ); Wed, 28 Mar 2018 02:39:26 -0400 Received: from adam-pc.lan (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (NOT encrypted); Wed, 28 Mar 2018 00:39:13 -0600 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH] btrfs-progs: mkfs/rootdir: Don't follow symbolic link when calcuating size Date: Wed, 28 Mar 2018 14:39:09 +0800 Message-Id: <20180328063909.937-1-wqu@suse.com> X-Mailer: git-send-email 2.16.2 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP [BUG] If we have a symbolic link in rootdir pointing to non-existing location, mkfs.btrfs --rootdir will just fail: ------ $ mkfs.btrfs -f --rootdir /tmp/rootdir/ /dev/data/btrfs btrfs-progs v4.15.1 See http://btrfs.wiki.kernel.org for more information. ERROR: ftw subdir walk of /tmp/rootdir/ failed: No such file or directory ------ [CAUSE] Commit 599a0abed564 ("btrfs-progs: mkfs/rootdir: Use over-reserve method to make size estimate easier") add extra ftw walk to estimate the filesystem size. Such default ftw walk will follow symbolic link and gives ENOENT error. [FIX] Use nftw() to specify FTW_PHYS so we won't follow symbolic link for size calculation. Reported-by: Alexander Kanavin Fixes: 599a0abed564 ("btrfs-progs: mkfs/rootdir: Use over-reserve method to make size estimate easier") Signed-off-by: Qu Wenruo --- mkfs/rootdir.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mkfs/rootdir.c b/mkfs/rootdir.c index a1d223a2408a..33c3ff1e18cf 100644 --- a/mkfs/rootdir.c +++ b/mkfs/rootdir.c @@ -696,7 +696,7 @@ out: } static int ftw_add_entry_size(const char *fpath, const struct stat *st, - int type) + int type, struct FTW *ftwbuf) { /* * Failed to read the directory, mostly due to EPERM. Abort ASAP, so @@ -731,7 +731,12 @@ u64 btrfs_mkfs_size_dir(const char *dir_name, u32 sectorsize, u64 min_dev_size, fs_block_size = sectorsize; ftw_data_size = 0; ftw_meta_nr_inode = 0; - ret = ftw(dir_name, ftw_add_entry_size, 10); + + /* + * Symbolic link is not followed when creating files, so no need to + * follow them here. + */ + ret = nftw(dir_name, ftw_add_entry_size, 10, FTW_PHYS); if (ret < 0) { error("ftw subdir walk of %s failed: %s", dir_name, strerror(errno));