From patchwork Mon May 18 09:16:27 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 6427081 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 29D309F1C1 for ; Mon, 18 May 2015 09:17:06 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 36EFD2060C for ; Mon, 18 May 2015 09:17:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 46BB52060B for ; Mon, 18 May 2015 09:17:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753123AbbERJQ7 (ORCPT ); Mon, 18 May 2015 05:16:59 -0400 Received: from mail-pa0-f50.google.com ([209.85.220.50]:32836 "EHLO mail-pa0-f50.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752762AbbERJQo (ORCPT ); Mon, 18 May 2015 05:16:44 -0400 Received: by padbw4 with SMTP id bw4so144023113pad.0 for ; Mon, 18 May 2015 02:16:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=LHM3FijIROIMVxegA/zhSMwJ7fAat0oT1E8aUiFf8uQ=; b=Ln38CDuh4Vy3tJdgTZJCzyB/2yqtEm85wSRkrlXWyAg4FRVmij4xORTdUD2S8mxtm4 PmxAHvNmeRbp/Tl6qLHOwhWPAK18xS2IurbUROh8AoIt7UCos7rGgZ2rt0xPXN5t5hRt WU0qyKxitK9PHA/sDc+KvvKzKQynDdDN5gQYdR9/YsEB8TxXe2n13zZrPJLwzPwQuIZk TykJBOWvPt7Uz/XS5Yyw4efvOSXwqFZbTMJvAkdBzbediH34IQmVmbxzZAG+pEZbjbd7 RpEAW5KTRWHgj6JidgLoSCpzn3fYPGKwaeWu+AHRZgax0+AL3yicBV3b/E3FCRliBMSX SfxQ== X-Gm-Message-State: ALoCoQku1OoMqz0YKu8RJcp6Ynyll74sj/ULCVm5xndZYAsR6NlkW49GQAuPKuvpYtNqtEN34wwD X-Received: by 10.66.159.169 with SMTP id xd9mr43291453pab.138.1431940604164; Mon, 18 May 2015 02:16:44 -0700 (PDT) Received: from mew.localdomain (c-76-104-211-44.hsd1.wa.comcast.net. [76.104.211.44]) by mx.google.com with ESMTPSA id v2sm9377370pdn.90.2015.05.18.02.16.43 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 18 May 2015 02:16:43 -0700 (PDT) From: Omar Sandoval To: Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org Cc: Qu Wenruo , Omar Sandoval Subject: [PATCH v3 2/6] Btrfs: remove all subvol options before mounting top-level Date: Mon, 18 May 2015 02:16:27 -0700 Message-Id: <890a770a617e3b7bb2c857fc0e87ac9b4cecfd0c.1431939732.git.osandov@osandov.com> X-Mailer: git-send-email 2.4.1 In-Reply-To: References: In-Reply-To: References: 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 Currently, setup_root_args() substitutes 's/subvol=[^,]*/subvolid=0/'. But, this means that if the user passes both a subvol and subvolid for some reason, we won't actually mount the top-level when we recursively mount. For example, consider: mkfs.btrfs -f /dev/sdb mount /dev/sdb /mnt btrfs subvol create /mnt/subvol1 # subvolid=257 btrfs subvol create /mnt/subvol2 # subvolid=258 umount /mnt mount -osubvol=/subvol1,subvolid=258 /dev/sdb /mnt In the final mount, subvol=/subvol1,subvolid=258 becomes subvolid=0,subvolid=258, and the last option takes precedence, so we mount subvol2 and try to look up subvol1 inside of it, which fails. So, instead, do a thorough scan through the argument list and remove any subvol= and subvolid= options, then append subvolid=0 to the end. This implicitly makes subvol= take precedence over subvolid=, but we're about to add a stricter check for that. This also makes setup_root_args() more generic, which we'll need soon. Reviewed-by: David Sterba Signed-off-by: Omar Sandoval --- fs/btrfs/super.c | 56 ++++++++++++++++++++------------------------------------ 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 0576750..b5d04e4 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -1138,52 +1138,36 @@ static inline int is_subvolume_inode(struct inode *inode) } /* - * This will strip out the subvol=%s argument for an argument string and add - * subvolid=0 to make sure we get the actual tree root for path walking to the - * subvol we want. + * This will add subvolid=0 to the argument string while removing any subvol= + * and subvolid= arguments to make sure we get the top-level root for path + * walking to the subvol we want. */ static char *setup_root_args(char *args) { - unsigned len = strlen(args) + 2 + 1; - char *src, *dst, *buf; + char *buf, *dst, *sep; - /* - * We need the same args as before, but with this substitution: - * s!subvol=[^,]+!subvolid=0! - * - * Since the replacement string is up to 2 bytes longer than the - * original, allocate strlen(args) + 2 + 1 bytes. - */ - - src = strstr(args, "subvol="); - /* This shouldn't happen, but just in case.. */ - if (!src) - return NULL; + if (!args) + return kstrdup("subvolid=0", GFP_NOFS); - buf = dst = kmalloc(len, GFP_NOFS); + /* The worst case is that we add ",subvolid=0" to the end. */ + buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, GFP_NOFS); if (!buf) return NULL; - /* - * If the subvol= arg is not at the start of the string, - * copy whatever precedes it into buf. - */ - if (src != args) { - *src++ = '\0'; - strcpy(buf, args); - dst += strlen(args); + while (1) { + sep = strchrnul(args, ','); + if (!strstarts(args, "subvol=") && + !strstarts(args, "subvolid=")) { + memcpy(dst, args, sep - args); + dst += sep - args; + *dst++ = ','; + } + if (*sep) + args = sep + 1; + else + break; } - strcpy(dst, "subvolid=0"); - dst += strlen("subvolid=0"); - - /* - * If there is a "," after the original subvol=... string, - * copy that suffix into our buffer. Otherwise, we're done. - */ - src = strchr(src, ','); - if (src) - strcpy(dst, src); return buf; }