2015-01-09 03:44:16

by Dexuan Cui

[permalink] [raw]
Subject: Can one file system be R/W mounted multiple times in Linux?

The 'mount' utility allows me to mount 1 file systems multiple times at different
mount points, like "mount /dev/sdb1 /a; mount /dev/sdb1 /b".

I tried to write from /a and /b at the same time and it seems everything is OK
and no data corruption happens.
I tried only ext4 only.

Can somebody please tell me if this usage is safe?

I'm not familiar with the internals of fs.
googling can't find an affirmative answer, I think.

Thanks,
-- Dexuan



2015-01-09 03:51:23

by Al Viro

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

On Fri, Jan 09, 2015 at 03:44:16AM +0000, Dexuan Cui wrote:
> The 'mount' utility allows me to mount 1 file systems multiple times at different
> mount points, like "mount /dev/sdb1 /a; mount /dev/sdb1 /b".
>
> I tried to write from /a and /b at the same time and it seems everything is OK
> and no data corruption happens.
> I tried only ext4 only.
>
> Can somebody please tell me if this usage is safe?

Yes.

2015-01-09 04:58:47

by Dexuan Cui

[permalink] [raw]
Subject: RE: Can one file system be R/W mounted multiple times in Linux?

> -----Original Message-----
> From: Al Viro [mailto:[email protected]] On Behalf Of Al Viro
> Sent: Friday, January 9, 2015 11:51 AM
> To: Dexuan Cui
> Cc: [email protected]; 'linux-ext4'; [email protected]
> Subject: Re: Can one file system be R/W mounted multiple times in Linux?
>
> On Fri, Jan 09, 2015 at 03:44:16AM +0000, Dexuan Cui wrote:
> > The 'mount' utility allows me to mount 1 file systems multiple times at
> different
> > mount points, like "mount /dev/sdb1 /a; mount /dev/sdb1 /b".
> >
> > I tried to write from /a and /b at the same time and it seems everything is OK
> > and no data corruption happens.
> > I tried only ext4 only.
> >
> > Can somebody please tell me if this usage is safe?
>
> Yes.

Thanks for the confirmation!

-- Dexuan

2015-01-09 06:04:36

by Eric Sandeen

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

On Jan 8, 2015, at 10:59 PM, Dexuan Cui <[email protected]> wrote:

>> -----Original Message-----
>> From: Al Viro [mailto:[email protected]] On Behalf Of Al Viro
>> Sent: Friday, January 9, 2015 11:51 AM
>> To: Dexuan Cui
>> Cc: [email protected]; 'linux-ext4'; [email protected]
>> Subject: Re: Can one file system be R/W mounted multiple times in Linux?
>>
>>> On Fri, Jan 09, 2015 at 03:44:16AM +0000, Dexuan Cui wrote:
>>> The 'mount' utility allows me to mount 1 file systems multiple times at
>> different
>>> mount points, like "mount /dev/sdb1 /a; mount /dev/sdb1 /b".
>>>
>>> I tried to write from /a and /b at the same time and it seems everything is OK
>>> and no data corruption happens.
>>> I tried only ext4 only.
>>>
>>> Can somebody please tell me if this usage is safe?
>>
>> Yes.
>
> Thanks for the confirmation!
>
> -- Dexuan

But to be clear, just in case - this is completely different from, say, mounting the same ext4 fs on two different nodes on a SAN, which is quite certainly not safe...

-Eric

2015-01-09 06:48:54

by Dexuan Cui

[permalink] [raw]
Subject: RE: Can one file system be R/W mounted multiple times in Linux?

> -----Original Message-----
> From: Eric Sandeen [mailto:[email protected]]
> Sent: Friday, January 9, 2015 14:05 PM
> To: Dexuan Cui
> Cc: Al Viro; [email protected]; linux-ext4; [email protected]
> Subject: Re: Can one file system be R/W mounted multiple times in Linux?
>
> On Jan 8, 2015, at 10:59 PM, Dexuan Cui <[email protected]> wrote:
>
> >> -----Original Message-----
> >> From: Al Viro [mailto:[email protected]] On Behalf Of Al Viro
> >> Sent: Friday, January 9, 2015 11:51 AM
> >> To: Dexuan Cui
> >> Cc: [email protected]; 'linux-ext4'; [email protected]
> >> Subject: Re: Can one file system be R/W mounted multiple times in Linux?
> >>
> >>> On Fri, Jan 09, 2015 at 03:44:16AM +0000, Dexuan Cui wrote:
> >>> The 'mount' utility allows me to mount 1 file systems multiple times at
> >> different
> >>> mount points, like "mount /dev/sdb1 /a; mount /dev/sdb1 /b".
> >>>
> >>> I tried to write from /a and /b at the same time and it seems everything is
> OK
> >>> and no data corruption happens.
> >>> I tried only ext4 only.
> >>>
> >>> Can somebody please tell me if this usage is safe?
> >>
> >> Yes.
> >
> > Thanks for the confirmation!
> >
> > -- Dexuan
>
> But to be clear, just in case - this is completely different from, say, mounting the
> same ext4 fs on two different nodes on a SAN, which is quite certainly not safe...
>
> -Eric

Thank Eric for pointing this out!
Can you please explain this? I didn't use SAN...

Thanks,
-- Dexuan

2015-01-09 12:01:59

by Stefan Hajnoczi

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

On Fri, Jan 09, 2015 at 03:51:23AM +0000, Al Viro wrote:
> On Fri, Jan 09, 2015 at 03:44:16AM +0000, Dexuan Cui wrote:
> > The 'mount' utility allows me to mount 1 file systems multiple times at different
> > mount points, like "mount /dev/sdb1 /a; mount /dev/sdb1 /b".
> >
> > I tried to write from /a and /b at the same time and it seems everything is OK
> > and no data corruption happens.
> > I tried only ext4 only.
> >
> > Can somebody please tell me if this usage is safe?
>
> Yes.

Can you explain under what conditions mounting the same file system
twice will work?

I guess the kernel is looking up the block device and then sharing the
superblock if the file system is already mounted on this block device?

If I use two loop devices for the same underlying storage it does not
work:

# mount -o loop /var/tmp/ext4.img /tmp/a
# mount -o loop /var/tmp/ext4.img /tmp/b
# touch /tmp/a/a
# ls /tmp/b
lost+found
# umount /tmp/a
# umount /tmp/b
# mount -o loop /var/tmp/ext4.img /tmp/a
[1078357.297245] EXT4-fs error (device loop0): ext4_lookup:1441: inode #2: comm ls: deleted inode referenced: 12

Stefan


Attachments:
(No filename) (1.11 kB)
(No filename) (473.00 B)
Download all attachments

2015-01-09 13:25:15

by Bob Peterson

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

> > >>> Can somebody please tell me if this usage is safe?
> > >>
> > >> Yes.
> > But to be clear, just in case - this is completely different from, say,
> > mounting the
> > same ext4 fs on two different nodes on a SAN, which is quite certainly not
> > safe...
> >
> > -Eric
>
> Thank Eric for pointing this out!
> Can you please explain this? I didn't use SAN...
>
> Thanks,
> -- Dexuan

Hi Dexuan,

I think Eric means mounting the same physical device from two different
machines (whether they be VMs or nodes in a cluster, etc.) where shared
storage is involved (like SAN or iSCSI). If one system has no knowledge
of what the other is changing, the file system will likely be corrupted.
It sounds like you're probably not doing that.

However, shared file systems like gfs2 and ocfs2 are designed to allow
simultaneous R/W access from multiple nodes, and the file system metadata
is kept in sync and coherent across the cluster.
I don't know if gfs2 allows multiple mounts from the same machine to
different mount points, due to how its inter-node locking and journal
recovery are coordinated. (I haven't tried it in recent years).

Regards,

Bob Peterson
Red Hat File Systems

2015-01-09 14:55:49

by Christoph Hellwig

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

On Fri, Jan 09, 2015 at 12:01:59PM +0000, Stefan Hajnoczi wrote:
> Can you explain under what conditions mounting the same file system
> twice will work?
>
> I guess the kernel is looking up the block device and then sharing the
> superblock if the file system is already mounted on this block device?

Exactly.

>
> If I use two loop devices for the same underlying storage it does not
> work:

Because that's very much the same situation as two different systems
trying to mount it.

2015-01-09 15:52:24

by Lukas Czerner

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

On Fri, 9 Jan 2015, Stefan Hajnoczi wrote:

> Date: Fri, 9 Jan 2015 12:01:59 +0000
> From: Stefan Hajnoczi <[email protected]>
> To: Al Viro <[email protected]>
> Cc: Dexuan Cui <[email protected]>,
> "[email protected]" <[email protected]>,
> 'linux-ext4' <[email protected]>
> Subject: Re: Can one file system be R/W mounted multiple times in Linux?
>
> On Fri, Jan 09, 2015 at 03:51:23AM +0000, Al Viro wrote:
> > On Fri, Jan 09, 2015 at 03:44:16AM +0000, Dexuan Cui wrote:
> > > The 'mount' utility allows me to mount 1 file systems multiple times at different
> > > mount points, like "mount /dev/sdb1 /a; mount /dev/sdb1 /b".
> > >
> > > I tried to write from /a and /b at the same time and it seems everything is OK
> > > and no data corruption happens.
> > > I tried only ext4 only.
> > >
> > > Can somebody please tell me if this usage is safe?
> >
> > Yes.
>
> Can you explain under what conditions mounting the same file system
> twice will work?
>
> I guess the kernel is looking up the block device and then sharing the
> superblock if the file system is already mounted on this block device?
>
> If I use two loop devices for the same underlying storage it does not
> work:
>
> # mount -o loop /var/tmp/ext4.img /tmp/a
> # mount -o loop /var/tmp/ext4.img /tmp/b

Because now you have two block devices backed by the same file.
Which as Christoph pointed out is equivalent of accessing it from
two different systems.

What you probably want to do is this:

# losetup --show -f /var/tmp/ext4.img
/dev/loop0

mount /dev/loop0 /tmp/a
mount /dev/loop0 /tmp/b

Now the question is, whether 'mount' can be a bit smarter than that
and just mount the already existing block device instead of creating
new one ? Karel ?

-Lukas


> # touch /tmp/a/a
> # ls /tmp/b
> lost+found
> # umount /tmp/a
> # umount /tmp/b
> # mount -o loop /var/tmp/ext4.img /tmp/a
> [1078357.297245] EXT4-fs error (device loop0): ext4_lookup:1441: inode #2: comm ls: deleted inode referenced: 12
>
> Stefan
>

2015-01-09 19:31:19

by Pranay Srivastava

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

Hi

On Fri, Jan 9, 2015 at 9:22 PM, Lukáš Czerner <[email protected]> wrote:
> On Fri, 9 Jan 2015, Stefan Hajnoczi wrote:
>
>> Date: Fri, 9 Jan 2015 12:01:59 +0000
>> From: Stefan Hajnoczi <[email protected]>
>> To: Al Viro <[email protected]>
>> Cc: Dexuan Cui <[email protected]>,
>> "[email protected]" <[email protected]>,
>> 'linux-ext4' <[email protected]>
>> Subject: Re: Can one file system be R/W mounted multiple times in Linux?
>>
>> On Fri, Jan 09, 2015 at 03:51:23AM +0000, Al Viro wrote:
>> > On Fri, Jan 09, 2015 at 03:44:16AM +0000, Dexuan Cui wrote:
>> > > The 'mount' utility allows me to mount 1 file systems multiple times at different
>> > > mount points, like "mount /dev/sdb1 /a; mount /dev/sdb1 /b".
>> > >
>> > > I tried to write from /a and /b at the same time and it seems everything is OK
>> > > and no data corruption happens.
>> > > I tried only ext4 only.
>> > >
>> > > Can somebody please tell me if this usage is safe?
>> >
>> > Yes.
>>
>> Can you explain under what conditions mounting the same file system
>> twice will work?
>>
>> I guess the kernel is looking up the block device and then sharing the
>> superblock if the file system is already mounted on this block device?
>>
>> If I use two loop devices for the same underlying storage it does not
>> work:
>>
>> # mount -o loop /var/tmp/ext4.img /tmp/a
>> # mount -o loop /var/tmp/ext4.img /tmp/b
>
> Because now you have two block devices backed by the same file.
> Which as Christoph pointed out is equivalent of accessing it from
> two different systems.
>
> What you probably want to do is this:
>
> # losetup --show -f /var/tmp/ext4.img
> /dev/loop0
>
> mount /dev/loop0 /tmp/a
> mount /dev/loop0 /tmp/b
>
> Now the question is, whether 'mount' can be a bit smarter than that
> and just mount the already existing block device instead of creating
> new one ? Karel ?

It's not the mount. If you see mount_bdev it checks for this and uses
the existing super block from the first mount
for all subsequent mounts of that block device.

When block devices differ, as in with 2 loop devices they have
different bdev and that's why it doesn't work with 2
loop devices.

>
> -Lukas
>
>
>> # touch /tmp/a/a
>> # ls /tmp/b
>> lost+found
>> # umount /tmp/a
>> # umount /tmp/b
>> # mount -o loop /var/tmp/ext4.img /tmp/a
>> [1078357.297245] EXT4-fs error (device loop0): ext4_lookup:1441: inode #2: comm ls: deleted inode referenced: 12
>>
>> Stefan
>>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html



--
---P.K.S

2015-01-09 20:37:11

by Al Viro

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

On Sat, Jan 10, 2015 at 01:01:19AM +0530, Pranay Srivastava wrote:

> It's not the mount. If you see mount_bdev it checks for this and uses
> the existing super block from the first mount
> for all subsequent mounts of that block device.
>
> When block devices differ, as in with 2 loop devices they have
> different bdev and that's why it doesn't work with 2
> loop devices.

What Lukas is suggesting is for mount(8) to spot an existing loop device with
the desired backing store and reuse it, rather than try and set the new one
up. That's where -o loop is handled - it's gone before mount(2) might see
it. The problem is in races...

2015-01-10 04:07:18

by Dexuan Cui

[permalink] [raw]
Subject: RE: Can one file system be R/W mounted multiple times in Linux?

> -----Original Message-----
> From: Al Viro [mailto:[email protected]] On Behalf Of Al Viro
> Sent: Saturday, January 10, 2015 4:37 AM
> To: Pranay Srivastava
> Cc: Luk?? Czerner; Stefan Hajnoczi; [email protected]; Dexuan Cui; linux-
> [email protected]; linux-ext4
> Subject: Re: Can one file system be R/W mounted multiple times in Linux?
>
> On Sat, Jan 10, 2015 at 01:01:19AM +0530, Pranay Srivastava wrote:
>
> > It's not the mount. If you see mount_bdev it checks for this and uses
> > the existing super block from the first mount
> > for all subsequent mounts of that block device.
> >
> > When block devices differ, as in with 2 loop devices they have
> > different bdev and that's why it doesn't work with 2
> > loop devices.
>
> What Lukas is suggesting is for mount(8) to spot an existing loop device with
> the desired backing store and reuse it, rather than try and set the new one
> up. That's where -o loop is handled - it's gone before mount(2) might see
> it. The problem is in races...

Thank you all for the pretty informative discussion!

I think now I know when it's safe and when it's unsafe... :-)

Thanks,
-- Dexuan

2015-01-12 13:00:58

by Karel Zak

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

On Fri, Jan 09, 2015 at 04:52:11PM +0100, Lukáš Czerner wrote:
> Now the question is, whether 'mount' can be a bit smarter than that
> and just mount the already existing block device instead of creating
> new one ? Karel ?

Well, the question is how smart is too smart :-) And Al is right, problem
is in races.

Anyway, I'd like to avoid complex FS specific code in mount(8). IMHO
it's FS driver responsibility to avoid (or support) FS sharing between
more block devices.

BTW, for ext4 you can enable ETX4_FEATURE_INCOMPAT_MMP to protect
against multi-mount.

Karel

--
Karel Zak <[email protected]>
http://karelzak.blogspot.com

2015-01-12 14:20:19

by Lukas Czerner

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

On Mon, 12 Jan 2015, Karel Zak wrote:

> Date: Mon, 12 Jan 2015 14:00:58 +0100
> From: Karel Zak <[email protected]>
> To: Lukáš Czerner <[email protected]>
> Cc: Stefan Hajnoczi <[email protected]>, Al Viro <[email protected]>,
> Dexuan Cui <[email protected]>,
> "[email protected]" <[email protected]>,
> 'linux-ext4' <[email protected]>
> Subject: Re: Can one file system be R/W mounted multiple times in Linux?
>
> On Fri, Jan 09, 2015 at 04:52:11PM +0100, Lukáš Czerner wrote:
> > Now the question is, whether 'mount' can be a bit smarter than that
> > and just mount the already existing block device instead of creating
> > new one ? Karel ?
>
> Well, the question is how smart is too smart :-) And Al is right, problem
> is in races.
>
> Anyway, I'd like to avoid complex FS specific code in mount(8). IMHO
> it's FS driver responsibility to avoid (or support) FS sharing between
> more block devices.
>
> BTW, for ext4 you can enable ETX4_FEATURE_INCOMPAT_MMP to protect
> against multi-mount.
>
> Karel

Hi Karel,

that's not what I meant at all. Yes, things like MMP should help.
But what I was suggesting was not FS specific.

mount(8) could in case of '-o loop' look whether there is already a
block device associated with the file and if so use it instead of
creating new one.

However this might race with device removal among other things and I
am not sure whether there is any easy solution.

Thanks!
-Lukas

2015-01-12 16:47:05

by Karel Zak

[permalink] [raw]
Subject: Re: Can one file system be R/W mounted multiple times in Linux?

On Mon, Jan 12, 2015 at 03:20:19PM +0100, Lukáš Czerner wrote:
> that's not what I meant at all. Yes, things like MMP should help.
> But what I was suggesting was not FS specific.

And what do you want to do when somebody will implement any
cluster-like or multi-root filesystem where share the same FS image
between two loop devices is expected and supported feature?

I don't think there is any such generic VFS restriction, it's about FS
driver(s).

> mount(8) could in case of '-o loop' look whether there is already a
> block device associated with the file and if so use it instead of
> creating new one.

mount(8) already checks if the same backing file is not mounted on the
same mountpoint

# mount -o loop fs.img /mnt/A
# mount -o loop fs.img /mnt/A
mount: fs.img is already mounted

but I don't want to make decision about anything else in userspace.

Karel

--
Karel Zak <[email protected]>
http://karelzak.blogspot.com