linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Austin S. Hemmelgarn" <ahferroin7@gmail.com>
To: Peter Grandi <pg@btrfs.list.sabi.co.UK>,
	Linux fs Btrfs <linux-btrfs@vger.kernel.org>
Subject: Re: finding root filesystem of a subvolume?
Date: Wed, 23 Aug 2017 08:07:53 -0400	[thread overview]
Message-ID: <3026f8a6-0ea6-0ed2-41c7-7d25c51a32fe@gmail.com> (raw)
In-Reply-To: <22940.27854.159264.779233@tree.ty.sabi.co.uk>

On 2017-08-22 13:41, Peter Grandi wrote:
> [ ... ]
> 
>>> There is no fixed relationship between the root directory
>>> inode of a subvolume and the root directory inode of any
>>> other subvolume or the main volume.
> 
>> Actually, there is, because it's inherently rooted in the
>> hierarchy of the volume itself. That root inode for the
>> subvolume is anchored somewhere under the next higher
>> subvolume.
> 
> This stupid point relies on ignoring that it is not mandatory to
> mount the main volume, and that therefore "There is no fixed
> relationship between the root directory inode of a subvolume and
> the root directory inode of any other subvolume or the main
> volume", because the "root directory inode" of the "main volume"
> may not be mounted at all.
> 
> This stupid point also relies on ignoring that subvolumes can be
> mounted *also* under another directory, even if the main volume
> is mounted somewhere else. Suppose that the following applies:
> 
>    subvol=5	/local
>    subvol=383	/local/.backup/home
>    subvol=383	/mnt/home-backup
> 
> and you are given the mountpoint '/mnt/home-backup', how can you
> find the main volume mountpoint '/local' from that?Assuming /mnt/home-backup is mounted and visible in /proc/mounts (your 
explanation requires it was either mounted directly via a subvol option 
or indirectly via a bind mount), then the following generic method works 
(and should always work on a modern kernel):
1. Find it in /proc/mounts or the output of `mount` run without any 
arguments.
2. If the mount type is 'bind', the device field will point to the 
source directory.  Using any method you like, figure out what the actual 
mount point that is under is, and repeat from step one with that mount 
point.
3. If the mount type is 'btrfs', and 'subvolid=5' is present in the 
mount options (this gets added automatically on any recent kernel), you 
just found the top level subvolume.
4. If the mount type is 'btrfs', and 'subvolid=5' is not present in the 
mount options, scan /proc/mounts for other entries with the same device 
value (this is going to be consistent).
5. If another entry does exist, and has 'subvolid=5' in the mount 
options, that's the top level subvolume.
6. If other entries exist, but 'subvolid=5' not in the mount options for 
any of them, or no other entries exist, the top level volume isn't 
mounted, and you can view it by mounting the device with 'subvolid=5'.

In the event that you need to know where the next higher level subvolume 
is mounted instead of the root subvolume for the volume, you can use the 
same method, but add the following after step two to figure out what 
subvolid to look for:
2b. Run `btrfs subvol show` on the mount point (it by definition has to 
be a subvolume because it's mounted and it's not a bind mount in 
userspace).  Find the 'Top level ID' (note that this just points to the 
next higher subvolume, not the root subvolume) value in the output, and 
use that instead of 5 when looking for 'subvolid=' options.
> 
> Please explain how '/mnt/home-backup' is indeed "inherently
> rooted in the hierarchy of the volume itself", because there is
> always a "fixed relationship between the root directory inode of
> a subvolume and the root directory inode of any other subvolume
> or the main volume".
You did one of three things to mount that:
1. You used a bind mount directly.  In this case, you're not changing 
the relation at all of the subvolume (which is technically not what's 
mounted in this case because a bind mount operates at the VFS layer and 
knows nothing about subvolumes) and it's parent subvolumes.
2. You mounted using 'subvol=.backup/home'.  This in and of itself 
should pretty concisely explain that there is a hierarchical 
relationship between subvolumes.  That path indicates the path in the 
hierarchy of the volume itself used to access the subvolume.
3. You mounted using 'subvolid=383'.  This is the only case where there 
isn't a hierarchical relationship, and just uses the tree ID to 
reference the subvolume.  This is the only case where there isn't some 
direct reference to the parent subvolume.

Note also that this relation is not inherent to the subvolume, but to 
it's parent.  Just like a symbolic link, the reference is in the 
containing object, not the referenced object (although subvolumes do 
store info about what their parent subvolume is).
> 
> [ ... ]
> 
>> Again, it does, it's just not inherently exposed to userspace
>> unless you mount the top-level subvolume (subvolid=5 and/or
>> subvol=/ in mount options).
> 
> This extra stupid point is based on ignoring that to "mount the
> top-level subvolume" relies on knowing already which one is the
> "top-level subvolume", which is begging the question.
You have information in /proc/mounts as to what is mounted.  Trace your 
particular path up to that, and then mount the indicated block device 
with `-o subvolid=5`.  The subvolume with ID 5 is _ALWAYS_ the top level 
subvolume for the volume, that's inherently hard coded in the filesystem 
format, so if that's all you want it's trivial to get it.

  reply	other threads:[~2017-08-23 12:07 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-22 12:23 finding root filesystem of a subvolume? Ulli Horlacher
2017-08-22 12:40 ` Hugo Mills
2017-08-22 12:50   ` Ulli Horlacher
2017-08-22 12:58     ` Marat Khalili
2017-08-22 13:12       ` Ulli Horlacher
2017-08-22 13:41         ` Marat Khalili
2017-08-22 13:27     ` Austin S. Hemmelgarn
2017-08-22 13:30       ` Ulli Horlacher
2017-08-22 13:37         ` Austin S. Hemmelgarn
2017-08-22 13:45           ` Marat Khalili
2017-08-22 13:53           ` Ulli Horlacher
2017-08-22 14:12             ` Austin S. Hemmelgarn
2017-08-22 14:23               ` Hugo Mills
2017-08-22 14:37                 ` Austin S. Hemmelgarn
2017-08-22 14:43 ` Peter Grandi
2017-08-22 15:03   ` Austin S. Hemmelgarn
2017-08-22 15:45     ` Ulli Horlacher
2017-08-22 16:48       ` Roman Mamedov
2017-08-22 17:41     ` Peter Grandi
2017-08-23 12:07       ` Austin S. Hemmelgarn [this message]
2017-08-23 13:14         ` Axel Burri

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3026f8a6-0ea6-0ed2-41c7-7d25c51a32fe@gmail.com \
    --to=ahferroin7@gmail.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=pg@btrfs.list.sabi.co.UK \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).