From patchwork Thu Apr 9 21:34:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 6191021 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.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 1DBA4BF4A6 for ; Thu, 9 Apr 2015 21:37:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3831F2037A for ; Thu, 9 Apr 2015 21:37:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2AAFE2037E for ; Thu, 9 Apr 2015 21:37:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754362AbbDIVg7 (ORCPT ); Thu, 9 Apr 2015 17:36:59 -0400 Received: from mail-pd0-f176.google.com ([209.85.192.176]:35391 "EHLO mail-pd0-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753619AbbDIVfZ (ORCPT ); Thu, 9 Apr 2015 17:35:25 -0400 Received: by pddn5 with SMTP id n5so320256pdd.2 for ; Thu, 09 Apr 2015 14:35:24 -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=NS6rEfSne/gsjqn7pDvQwVy7POYrVQ/ZiITg90F5MUM=; b=aX0wnAWf6nu8KCkh4o+uNPD4NhrI+HxaTJ8IvCU6VjgpQIBo3p+5RdA4LIAWOj7hu/ rMU4jwYmpaQU/JY3jh99ke8o0pt7A8NX7BxQ9qTGWK/rWlOfstQJb2uhobpe5edrzA/r PTD6Ph63SasARy83urarRBocZkvWitay0a/j+dTB5PA039Lr5TfkBkgZ6Uvd5ciUSJsH 6U4uQvo62xBbSzfHupC48nthUn1rdhcI/AoXKAsh2sUKugmgBwlrjCo1NP2WZWPCphNZ WgW1k6K8WvzPqwO08MbOVLA+QDAQPvdMr9q6W/7GIGaMp+YVUfaz7CwpAhWZFdQLCY1g rO1A== X-Gm-Message-State: ALoCoQnMTQtgDviRtKoKBYswEBxYMsh4ynxbBO9ibWXa8PIlVEqbfzZnRexD2V8K8meaVTqiqwOa X-Received: by 10.66.221.193 with SMTP id qg1mr58083130pac.134.1428615324614; Thu, 09 Apr 2015 14:35:24 -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 ot6sm15540730pdb.28.2015.04.09.14.35.23 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 09 Apr 2015 14:35:23 -0700 (PDT) From: Omar Sandoval To: Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Qu Wenruo , Omar Sandoval Subject: [PATCH v2 2/6] Btrfs: remove all subvol options before mounting top-level Date: Thu, 9 Apr 2015 14:34:52 -0700 Message-Id: <8ead0eb0e24aac86fae1d748853ed64e3a4161d9.1428614837.git.osandov@osandov.com> X-Mailer: git-send-email 2.3.5 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=unavailable 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. Signed-off-by: Omar Sandoval Reviewed-by: David Sterba --- 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 d38be09..bac3c9a 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -1129,52 +1129,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; }