2 years agoblock: simplify check_extroot() a bit
Rafał Miłecki [Tue, 5 May 2020 06:49:17 +0000 (08:49 +0200)]
block: simplify check_extroot() a bit

Avoid too big code indent.

Signed-off-by: Rafał Miłecki <>
2 years agoblock: add some basic extroot documentation
Rafał Miłecki [Sat, 2 May 2020 11:29:08 +0000 (13:29 +0200)]
block: add some basic extroot documentation

Signed-off-by: Rafał Miłecki <>
2 years agoRevert "block: support hierarchical mount/umount"
Rafał Miłecki [Thu, 2 Apr 2020 12:12:13 +0000 (14:12 +0200)]
Revert "block: support hierarchical mount/umount"

This reverts commit fb0700f0fdfc4bec220203d31df87be2b4ccc467.

Switching to AVL tree allows sorting devices by selected parameter
(mount path) but does not implement a real hierarchical layout. That
would require a tree with parent nodes being parent devices / mount

Cc: Yousong Zhou <>
Signed-off-by: Rafał Miłecki <>
2 years agoRevert "block: mount_action: handle mount/umount deps"
Rafał Miłecki [Thu, 2 Apr 2020 12:12:12 +0000 (14:12 +0200)]
Revert "block: mount_action: handle mount/umount deps"

This reverts commit 32c3126b2f0464106d74317336b6aef1d7d5f82f.

Internally stored list of devices is implemented using struct vlist_tree
and keeps devices sorted by their mount target paths. This DOESN'T mean
that all preceding entires of a given device are its parent devices.
Making such an assumption and mounting all preceding devices results in
unwanted mounts in most cases.

For example having devices like:
/dev/sda1 (mount target: /mnt/foo1)
/dev/sda2 (mount target: /mnt/foo2)
/dev/sdb1 (mount target: /mnt/bar1)
/dev/sdb2 (mount target: /mnt/bar2)
will result in devices vlist_tree having entries sorted like:
/dev/sdb1 (bar1), /dev/sdb2 (bar2), /dev/sda1 (foo1), /dev/sda2 (foo2)

Using autofs and accessing /mnt/foo2 would result in mounting all 4
partitions and spinning unneeded /dev/sdb.

Cc: Yousong Zhou <>
Signed-off-by: Rafał Miłecki <>
2 years agolibfstools/mtd: attempt to read from OOB data if empty space is found
Felix Fietkau [Thu, 9 Apr 2020 11:19:08 +0000 (13:19 +0200)]
libfstools/mtd: attempt to read from OOB data if empty space is found

When using jffs2 on NAND flash, it stores its magic in the OOB data on
newly erased blocks. This change fixes identifying the filesystem type.

Signed-off-by: Felix Fietkau <>
2 years agoblockd: remove symlink linkpath file if it's a dir or link
Rafał Miłecki [Sat, 28 Dec 2019 21:07:22 +0000 (22:07 +0100)]
blockd: remove symlink linkpath file if it's a dir or link

Files like that can remain from using non-autofs mounting and can cause
mounting errors after switching to autofs:
blockd: failed to symlink /mnt/sda1->/tmp/run/blockd/sda1

Signed-off-by: Rafał Miłecki <>
2 years agoblock: remove mount target file if it's a link
Rafał Miłecki [Sat, 28 Dec 2019 21:07:21 +0000 (22:07 +0100)]
block: remove mount target file if it's a link

Links like that can remain from using autofs and can cause mounting
errors after switching to non-autofs:

block: mounting /dev/sda1 (vfat) as /mnt/sda1 failed (2) - No such file or directory

Signed-off-by: Rafał Miłecki <>
2 years agoblockd: report "target" path as "mount" for autofs available mounts
Rafał Miłecki [Fri, 27 Dec 2019 12:09:11 +0000 (13:09 +0100)]
blockd: report "target" path as "mount" for autofs available mounts

Devices handled with autofs should be seen as available even if
currently unmounted. Mounting is handled on demand and transparently for

Signed-off-by: Rafał Miłecki <>
2 years agoblockd: print symlink error code and string message
Rafał Miłecki [Sat, 28 Dec 2019 21:29:03 +0000 (22:29 +0100)]
blockd: print symlink error code and string message

It may help understanding failure reason, e.g.:
blockd: failed to symlink /mnt/sda1->/tmp/run/blockd/sda1 (17) - File exists

Signed-off-by: Rafał Miłecki <>
2 years agoRevert "fstools: Add support to read-only MTD partitions (eg. recovery images)"
Petr Štetiar [Tue, 21 Jan 2020 16:11:07 +0000 (17:11 +0100)]
Revert "fstools: Add support to read-only MTD partitions (eg. recovery images)"

This reverts commit f5c7c1813f52e6d7b59ecfb2f9f95e69b05b1980 which needs
more work and testing as it broke at least jffs2 overlays at least on
ath79 platform, marking them as read-only, thus unusable:

 jffs2_build_filesystem(): erasing all blocks after the end marker...
 jffs2: Erase at 0x009e0000 failed immediately: -EROFS. Is the sector locked?

Reported-by: Steve Brown <>
Signed-off-by: Petr Štetiar <>
2 years agofstools: Add support to read-only MTD partitions (eg. recovery images)
Bruno Pena [Sat, 4 Jan 2020 11:52:08 +0000 (12:52 +0100)]
fstools: Add support to read-only MTD partitions (eg. recovery images)

This patch enables fstools to open read-only MTD partitions, which in
turn also enables OpenWrt to boot from read-only partitions.

The use of read-only partitions is of special importance for WiFi-only
devices, where a protected read-only recovery image can be used in case
something goes wrong with the main firmware (eg. user gets locked out
due to bad settings, flash of an unbootable dev firmware, etc).

Signed-off-by: Bruno Pena <>
2 years agolibblkid-tiny: fix f2fs labels by increasing label buffer
Petr Štetiar [Tue, 14 Jan 2020 18:49:41 +0000 (19:49 +0100)]
libblkid-tiny: fix f2fs labels by increasing label buffer

f2fs max label length is 1024B (512 * uint16_t) which is way above
current 256B used in `struct blkid_struct_probe` which renders labels on
f2fs system unusable:

 $ mkfs.f2fs -l drive /dev/sda1
 $ block info /dev/sda1
 label buffer too small 1024 > 255

So increase the label buffer again in order to make enough space for
f2fs labels.

Fixes: FS#2735
Signed-off-by: Petr Štetiar <>
2 years agoblock: re-discover mtd devices on extroot mount retry
Jo-Philipp Wich [Sun, 5 Jan 2020 16:21:25 +0000 (17:21 +0100)]
block: re-discover mtd devices on extroot mount retry

When retrying extroot mounting after waiting for the underlying device to
appear, the internal device cache was rebuilt without mtd devices, leading
to the following error:

    block: extroot: unable to lookup root device /dev/...

The extroot mount procedure needs access to the actual mtd root device to
temporarily mount it for uuid file verification.

Fix this error by rebuilding the cache with mtd devices to allow the
`check_extroot()` procedure to find the internal root device.

Fixes: FS#2701
Fixes: fb0700f ("block: support hierarchical mount/umount")
Signed-off-by: Jo-Philipp Wich <>
2 years agolibblkid-tiny: fix symbol collision with full libblkid
Jo-Philipp Wich [Sun, 22 Dec 2019 18:46:05 +0000 (19:46 +0100)]
libblkid-tiny: fix symbol collision with full libblkid

The recent introduction of blkid_new_probe() and blkid_free_probe() in the
dynamically linked libblkid-tiny caused the dlopen'd to call into
the wrong version of blkid_new_probe() within blkid_new_probe_from_filename(),
leading to memory corruption and eventual segmentation faults.

Fixes: b82c5c1 ("libblkid-tiny: add functions for allocating & freeing probe struct")
Signed-off-by: Jo-Philipp Wich <>
2 years agolibblkid-tiny: vfat: Change parsing label in special cases
Pali Rohár [Tue, 17 Dec 2019 07:28:36 +0000 (08:28 +0100)]
libblkid-tiny: vfat: Change parsing label in special cases

commit f0ca7e80d7a171701d0d04a3eae22d97f15d0683 upstream.

* Use only label from the root directory and do not fallback to the label
  stored in boot sector. This is how MS-DOS 6.22, MS-DOS 7.10, Windows 98,
  Windows XP and also Windows 10 behave. Moreover Windows XP and Windows 10
  do not touch label in boot sector anymore, so removing FAT label on those
  Windowses leads to having old label still stored in boot sector (which
  MS-DOS and Windows fully ignore).

* Label entry "NO NAME" in root directory is treated as label "NO NAME"
  instead of empty label. In root directory it has no special meaning.
  String "NO NAME" has a special meaning (empty label) only for label
  stored in boot sector.

* Label from the boot sector is now stored into LABEL_FATBOOT field. So if
  there are applications which depends or needs to read this label, they
  have ability.

After this change LABEL always correspondent to the label from the root
directory and LABEL_FATBOOT to the label stored in the boot sector. If some
of those labels is missing or is not present (e.g. "NO LABEL" in boot
sector) then particular field is not set.

Signed-off-by: Pali Rohár <>
[rmilecki: drop unneeded now downstream hacks for handling spaces]
Signed-off-by: Rafał Miłecki <>
2 years agolibblkid-tiny: vfat: Fix reading labels which starts with byte 0x05
Pali Rohár [Tue, 17 Dec 2019 07:28:35 +0000 (08:28 +0100)]
libblkid-tiny: vfat: Fix reading labels which starts with byte 0x05

commit e526f503918cc29d8b1ccf36a5c3a34645d2be6e upstream.

When FAT directory entry has leading byte 0x05 it is interpreted as byte
0xE5. This is how FAT stores file name which starts with byte 0xE5 as
leading byte in 0xE5 in FAT directory entry means that file slot is empty.

Fixes: #533
2 years agolibblkid-tiny: add blkid_probe_set_id_label() stub
Rafał Miłecki [Tue, 17 Dec 2019 07:28:34 +0000 (08:28 +0100)]
libblkid-tiny: add blkid_probe_set_id_label() stub

This stub will allow porting more upstream code without commenting out
calls and them unused variables. This will simplify maintenance.

Signed-off-by: Rafał Miłecki <>
2 years agolibblkid-tiny: use separated buffer for each block device read
Rafał Miłecki [Sat, 14 Dec 2019 21:55:01 +0000 (22:55 +0100)]
libblkid-tiny: use separated buffer for each block device read

This allows reading multiple chunks of block device data and operating
on them simultaneously. Previous implementation was using a single
buffer (except for reading more data than allocated size) and subsequent
reads were corrupting memory of previously returned buffers.

This fixes e.g. problem with reading NTFS UUID and validating VFAT

Implementation is based on original libblkid code which handles it
similarly. Buffers are put on probe internal list and freed when
releasing a probe struct.

Signed-off-by: Rafał Miłecki <>
2 years agolibblkid-tiny: add functions for allocating & freeing probe struct
Rafał Miłecki [Sat, 14 Dec 2019 21:55:00 +0000 (22:55 +0100)]
libblkid-tiny: add functions for allocating & freeing probe struct

This adds blkid_new_probe() and blkid_free_probe() which have to be used
in place of simple struct memory allocation. They will allow extending
probe struct by any extra initialization code and resources release.

Newly introduced probe.c file is based on original libblkid's code.

Signed-off-by: Rafał Miłecki <>
2 years agoblockd: don't flush devices list on "hotplug" call
Rafał Miłecki [Fri, 13 Dec 2019 10:05:12 +0000 (11:05 +0100)]
blockd: don't flush devices list on "hotplug" call

The point of "hotplug" call is to add or remove a single entry to/from
devices list. Using vlist_update() and vlist_flush() was clearing whole
list (and leaving the last entry in case of adding a device).

Signed-off-by: Rafał Miłecki <>
2 years agoblockd: fix vlist memory corruption
Rafał Miłecki [Fri, 13 Dec 2019 10:05:11 +0000 (11:05 +0100)]
blockd: fix vlist memory corruption

vlist_add() expects key to point a persistent memory as it doesn't make
its copy. Passing blob_attr of current message was resulting in
undefined/random behavior including list corruption and possible

Signed-off-by: Rafał Miłecki <>
2 years agoblock: detect: use current mp as value of target option
Yousong Zhou [Sun, 3 Nov 2019 03:06:32 +0000 (03:06 +0000)]
block: detect: use current mp as value of target option

Move down the function so that no declaration of find_mount_point() is
not needed

Signed-off-by: Yousong Zhou <>
2 years agoblock: mount_device: err log only when mp deviates from spec
Yousong Zhou [Tue, 29 Oct 2019 12:39:50 +0000 (12:39 +0000)]
block: mount_device: err log only when mp deviates from spec

Fixes possible memleak of mp as well

Resolves FS#1523

Signed-off-by: Yousong Zhou <>
2 years agoblock: mount_device: skip extroot earlier
Yousong Zhou [Tue, 29 Oct 2019 12:39:49 +0000 (12:39 +0000)]
block: mount_device: skip extroot earlier

Signed-off-by: Yousong Zhou <>
2 years agoblock: mount_action: handle mount/umount deps
Yousong Zhou [Tue, 29 Oct 2019 12:39:48 +0000 (12:39 +0000)]
block: mount_action: handle mount/umount deps

This is required at least in system startup when "block hotplug" will be
triggered by udevtrigger.  E.g. /dev/vdb needs to be mounted at /mnt/s
and /dev/vdc /mnt.  It does not work if /dev/vdb was triggered then
mounted first

Signed-off-by: Yousong Zhou <>
2 years agoblock: support hierarchical mount/umount
Yousong Zhou [Tue, 29 Oct 2019 12:39:47 +0000 (12:39 +0000)]
block: support hierarchical mount/umount

This is needed when e.g. sda1 needs to be mounted to /mnt and sda2
/mnt/srv.  We need to make sure sda1 was mounted first.

"devices" is now an ordered list with elements pointing to both "struct
probe_info" and "struct mount".  The order is firstly on presence of
mount info, then strlen(, then strcmp(

Resolves FS#2214

Signed-off-by: Yousong Zhou <>
2 years agoblock: umount: skip / unless -a is given
Yousong Zhou [Tue, 29 Oct 2019 12:39:46 +0000 (12:39 +0000)]
block: umount: skip / unless -a is given

Signed-off-by: Yousong Zhou <>
2 years agoblock: use fsck.fat instead of dosfsck
Yousong Zhou [Fri, 25 Oct 2019 10:51:49 +0000 (10:51 +0000)]
block: use fsck.fat instead of dosfsck

Dosfsck is only available when --enable-compat-symlinks was given when
configuring dosfstools.  These symlinks are not enabled in OpenWrt
dosfstools package

Suggested by Reiner Otto in FS#2408

Signed-off-by: Yousong Zhou <>
2 years agolibblkid-tiny: ntfs: fix use-after-free
Yousong Zhou [Fri, 25 Oct 2019 10:48:47 +0000 (10:48 +0000)]
libblkid-tiny: ntfs: fix use-after-free

The memory pointed to by ns can be reallocated when checking mft records

Fixes FS#2129

Signed-off-by: Yousong Zhou <>
2 years agomkdev: Avoid out of bounds read
Hauke Mehrtens [Mon, 2 Sep 2019 19:26:18 +0000 (21:26 +0200)]
mkdev: Avoid out of bounds read

readlink() truncates and does not null terminate the string when more
bytes would be written than available. Just increase the char array by
one and assume that there is a problem when all bytes are needed.

Coverity: #1330087, #1329991
Signed-off-by: Hauke Mehrtens <>
2 years agolibblkid-tiny: use blkid_probe_set_utf8label for label set
Ansuel Smith [Sun, 16 Jun 2019 17:00:50 +0000 (19:00 +0200)]
libblkid-tiny: use blkid_probe_set_utf8label for label set

Signed-off-by: Ansuel Smith <>
2 years agolibblkid-tiny: adds blkid_probe_set_utf8label support
Ansuel Smith [Sun, 16 Jun 2019 17:00:49 +0000 (19:00 +0200)]
libblkid-tiny: adds blkid_probe_set_utf8label support

Currently set_utf8label support is missing. Adds a stripped down version of encode.c file from original libblkid and adds the function to libblkid-tiny.c.

Signed-off-by: Ansuel Smith <>
2 years agoprobe: fallback to when does not exist
Jo-Philipp Wich [Wed, 28 Aug 2019 10:19:29 +0000 (12:19 +0200)]
probe: fallback to when does not exist

OpenWrt commit 0e70f69a35 ("treewide: revise library packaging") removed
the unversioned library symlink from the libblkid package, breaking
dynamic loading of the library at runtime.

Add a fallback to "" to allow using the dynamic library
for filesystem probing again.

Fixes: FS#2274
Signed-off-by: Jo-Philipp Wich <>
2 years agolibblkid-tiny: increment label size to 256
Ansuel Smith [Sun, 16 Jun 2019 17:00:48 +0000 (19:00 +0200)]
libblkid-tiny: increment label size to 256

Btrfs max label lenght is 255. Increment the blkid_struct_probe struct to respect this new max value.

Signed-off-by: Ansuel Smith <>
2 years agolibblkid-tiny: fix wrong btrfs label length
Ansuel Smith [Sun, 16 Jun 2019 17:00:47 +0000 (19:00 +0200)]
libblkid-tiny: fix wrong btrfs label length

Btrfs label length is 256 included the termination char ( '\0' ) as the real max length is 255 (less than 256 from the wiki).

Signed-off-by: Ansuel Smith <>
3 years agoblock: prevent mount point confusion
Jo-Philipp Wich [Wed, 27 Mar 2019 07:59:02 +0000 (08:59 +0100)]
block: prevent mount point confusion

Do not perform substring matches when identifying mount points to
avoid returning wrong entries, e.g. "/dev/mmcblk0p10" when
"/dev/mmcblk0p1" was requested.

Fixes: FS#2196
Signed-off-by: Jo-Philipp Wich <>
3 years agolibfstools: avoid false positives when matching devices and volumes
Jo-Philipp Wich [Wed, 27 Mar 2019 07:55:07 +0000 (08:55 +0100)]
libfstools: avoid false positives when matching devices and volumes

Revise matching code using strncmp() in order to avoid returning wrong
items, e.g. /dev/sda1 when /dev/sda was requested.

Signed-off-by: Jo-Philipp Wich <>
3 years agolibfstools: Fix overflow of F2FS_MINSIZE constant
Petr Štetiar [Tue, 26 Mar 2019 22:50:47 +0000 (23:50 +0100)]
libfstools: Fix overflow of F2FS_MINSIZE constant

I wasn't able to to use f2fs on armvirt/32 platform and I've found out
that it was due to F2FS_MINSIZE constant overflow leading to value of 13
exabytes instead of 100 megabytes.

Acked-by: Jo-Philipp Wich <>
Signed-off-by: Petr Štetiar <>
3 years agolibfstools: Print error in case of loop blkdev failure
Petr Štetiar [Tue, 26 Mar 2019 15:34:29 +0000 (16:34 +0100)]
libfstools: Print error in case of loop blkdev failure

It took me quite some time today(while fixing squashfs+overlay on
armvirt) to find out, that I was missing support for loop block device
in kernel, so I'm adding error message which might be helpful for
someone else in the future as well.

Acked-by: Jo-Philipp Wich <>
Signed-off-by: Petr Štetiar <>
3 years agoblock(d): improve hotplug.d "mount" events for the autofs
Rafał Miłecki [Wed, 12 Dec 2018 14:24:35 +0000 (15:24 +0100)]
block(d): improve hotplug.d "mount" events for the autofs

So far - when using autofs - "add" and "remove" action events were
triggered on every (un)mount. It wasn't very helpful when using autofs
due to its nature.

The point of blockd & autofs is to have block devices mounted on an
access request. Its users should not care / know if it's currently
mounted or not. Mounting should be handled transparently.

To make that work it requires informing listeners whenever device:
1) Becomes *ready* for mounting (by triggering "add" action)
2) Becomes *unavailable* (by triggering "remove" action)

The current mounting state is something that autofs & blockd should
handle internally and should not notify listeners about.

This is implemented by:
1) block generating events for non-autofs cases only (when (un)mounting)
2) blockd informing block when autofs resource becomes (un)available

Signed-off-by: Rafał Miłecki <>
3 years agoblockd: unmount device explicitly when it disappears
Rafał Miłecki [Wed, 12 Dec 2018 14:24:34 +0000 (15:24 +0100)]
blockd: unmount device explicitly when it disappears

To keep autofs behavior consistent blockd should request both: mounting
and unmounting when needed. It's important as autofs-related actions may
require slightly different handling.

Without this patch:
1) autofs mounts were handled using TYPE_AUTOFS
2) autofs unmounts were handled using TYPE_HOTPLUG

Signed-off-by: Rafał Miłecki <>
3 years agoblock: remove target directory after unmounting
Rafał Miłecki [Mon, 10 Dec 2018 06:48:50 +0000 (07:48 +0100)]
block: remove target directory after unmounting

This removes dangling directory fixing two issues:

1) Non autofs case
   Leaving directory in /mnt/ and switching to autofs would result in
   blockd failing to symlink().
2) autofs case
   Leaving directory in /var/run/blockd/ could result in apps trying to
   access it causing errors like:
   blockd: kernel is requesting a mount -> sda1
   blockd: failed to run block. add/sda1

Signed-off-by: Rafał Miłecki <>
3 years agoblock: cleanup handling "start" action of the "autofs" command
Rafał Miłecki [Sun, 9 Dec 2018 16:52:10 +0000 (17:52 +0100)]
block: cleanup handling "start" action of the "autofs" command

When blockd starts it uses "start" action for getting current state of
block devices. The way main_autofs() was implemented was a bit hacky

1) It was calling mount_device() with TYPE_HOTPLUG
   First of all that made code harder to understand. Faking TYPE_HOTPLUG
   for a TYPE_AUTOFS made following mount_device() logic more complex.
   Secondly the only reason for that seems to be to trigger "swap" and
   extroot checks in the mount_device(). Other than that it was only
   about calling blockd_notify() anyway.

2) It wasn't consistent
   Consider a "swap" or extroot without an "autofs" set (a pretty
   expected case).
   a) During original TYPE_HOTPLUG event mount_device() would detect
      both cases early and return without calling blockd_notify().
   b) With previous main_autofs() implementation blockd_notify() was
      called for both cases.

With this change main_autofs() doesn't fake TYPE_HOTPLUG and it follows
mount_device() logic which should result in an expected & consistent
state of devices in blockd.

Signed-off-by: Rafał Miłecki <>
3 years agoblock: make blockd_notify() return an int instead of void
Rafał Miłecki [Sun, 9 Dec 2018 15:11:59 +0000 (16:11 +0100)]
block: make blockd_notify() return an int instead of void

It allows improving block by handling blockd_notify() errors.

Signed-off-by: Rafał Miłecki <>
3 years agoblock: generate hotplug.d mount events
Rafał Miłecki [Sun, 9 Dec 2018 14:52:32 +0000 (15:52 +0100)]
block: generate hotplug.d mount events

With this change block generates 2 "mount" hotplug.d subsystem events:
1) "add" when block device gets mounted
2) "remove" when block device gets unmounted

This allows e.g. controlling USB storage dependant software using
hotplug.d listeners.

A very similar solution was implemented in mountd which was replaced by

Right now this is implemented using a call to the /sbin/hotplug-call.
A possible improvement is to rewrite above shell script into a C lib
function. For now let's just assume that script exists.

Signed-off-by: Rafał Miłecki <>
3 years agoblock: validate amount of arguments for the "autofs" command
Rafał Miłecki [Sun, 9 Dec 2018 13:22:17 +0000 (14:22 +0100)]
block: validate amount of arguments for the "autofs" command

Using argv[3] without checking argc value could result in undefined
behavior. It could result in a crash or accessing a NULL that separates
argv from envp on UNIX.

Signed-off-by: Rafał Miłecki <>
Acked-by: John Crispin <>
3 years agoblockd: don't reparse blob msg in the vlist callbacks
Rafał Miłecki [Fri, 7 Dec 2018 13:13:10 +0000 (14:13 +0100)]
blockd: don't reparse blob msg in the vlist callbacks

ubus message is parsed in the block_hotplug() which fills all the struct
device fields. Once that is done there is no need to parse original
message again - it's enough to get required data from the struct.

This also fixes handling messages with "autofs" set to 0. They were
incorrectly interpreted due to the missing blobmsg_get_u32().

Signed-off-by: Rafał Miłecki <>
Acked-by: John Crispin <>
3 years agoblockd: don't unmount device when removing it from the list
Rafał Miłecki [Thu, 6 Dec 2018 08:13:39 +0000 (09:13 +0100)]
blockd: don't unmount device when removing it from the list

Device gets removed from the list (vlist_delete()) when block calls
"hotplug" method of blockd using ubus. Right after that block unmounts
that device on its own.

blockd shouldn't care about unmounting on its own for following reasons:
1) To avoid code/behavior duplication with block
2) To keep behavior consistent with mounting (blockd doesn't mount)
3) To allow implementing more features in block (e.g. hotplug.d events)

To make unmounting the most reliable the plan is to have:
1) block receiving hotplug.d "block" subsystem events "remove"
2) blockd stopping reporting device (so we avoid new users & let
   existing ones realize mount can't be used anymore)
3) block notifying (through hotplug.d "mount" subsystem) all users about
   device being unmounted - that should stop all apps accessing it
4) block unmount device

That should allow storage users stop accessing mount point & let block
unmount device cleanly.

Signed-off-by: Rafał Miłecki <>
Acked-by: John Crispin <>
3 years agoblock: don't duplicate unmounting code in the mount_action()
Rafał Miłecki [Fri, 30 Nov 2018 17:26:12 +0000 (18:26 +0100)]
block: don't duplicate unmounting code in the mount_action()

Use umount_device() helper instead.

Signed-off-by: Rafał Miłecki <>
3 years agoblock: make umount_device() function more generic
Rafał Miłecki [Fri, 30 Nov 2018 17:26:11 +0000 (18:26 +0100)]
block: make umount_device() function more generic

Make it accept device path instead of struct probe_info. This way it can
be reused by code fired when device is already gone.

To keep existing functionality two checks have been moved to the

Signed-off-by: Rafał Miłecki <>
3 years agoblock: don't duplicate mounting code in the mount_device()
Rafał Miłecki [Fri, 30 Nov 2018 13:09:23 +0000 (14:09 +0100)]
block: don't duplicate mounting code in the mount_device()

Once target directory gets specified mounting code is identical for
devices having and not having UCI config entry. Share it.

Signed-off-by: Rafał Miłecki <>
Reviewed-by: Michael Heimpold <>
3 years agoblock: simplify code picking mount target directory
Rafał Miłecki [Fri, 30 Nov 2018 13:09:22 +0000 (14:09 +0100)]
block: simplify code picking mount target directory

Using a set of 3 conditional block each setting "target" variable makes
code easier to follow.

Signed-off-by: Rafał Miłecki <>
Reviewed-by: Michael Heimpold <>
3 years agoblock: move blockd_notify() call out of the conditional blocks
Rafał Miłecki [Fri, 30 Nov 2018 13:09:21 +0000 (14:09 +0100)]
block: move blockd_notify() call out of the conditional blocks

That function is called no matter if "m" variable is null or not. This
saves a one duplicated line & allows merging two conditional blocks
starting with if (m). It doesn't change code execution order.

Signed-off-by: Rafał Miłecki <>
Reviewed-by: Michael Heimpold <>
3 years agoblock: fix formatting & indent in the mount_device()
Rafał Miłecki [Fri, 30 Nov 2018 13:09:20 +0000 (14:09 +0100)]
block: fix formatting & indent in the mount_device()

There was some misformatted code that seemed like switch block having an
else block. This improves code readability.

Signed-off-by: Rafał Miłecki <>
Reviewed-by: Michael Heimpold <>
3 years agofstools: use EXIT_FAILURE when indicating error on exit
Michael Heimpold [Sat, 24 Nov 2018 09:22:44 +0000 (10:22 +0100)]
fstools: use EXIT_FAILURE when indicating error on exit

According to man page, using the EXIT_* macros is more portable than using
plain integer values.

Signed-off-by: Michael Heimpold <>
3 years agofstools: guard usage of WEXITSTATUS
Michael Heimpold [Sat, 24 Nov 2018 09:22:53 +0000 (10:22 +0100)]
fstools: guard usage of WEXITSTATUS

According to man page, using WEXITSTATUS should be guarded by a check
of WIFEXITED, so add this check.

While at, also print an error message in case fsck was terminated
by a signal.

Signed-off-by: Michael Heimpold <>
3 years agofstools: add ntfs support
Ansuel Smith [Fri, 28 Sep 2018 23:01:02 +0000 (01:01 +0200)]
fstools: add ntfs support

This adds ntfs support to block-mount

Signed-off-by: Ansuel Smith <>
3 years agofstools: allow the mounting with full access time accounting
Pierre Lebleu [Wed, 23 May 2018 08:55:58 +0000 (10:55 +0200)]
fstools: allow the mounting with full access time accounting

In order to allow the mounting of the filesystem with full access time
accounting, a new CMake option (eg: CMAKE_OVL_MOUNT_FULL_ACCESS_TIME)
has been added.

Signed-off-by: Pierre Lebleu <>
3 years agofstools: allow to compress the filesystem
Pierre Lebleu [Wed, 23 May 2018 08:55:57 +0000 (10:55 +0200)]
fstools: allow to compress the filesystem

In order to allow the mounting of the filesystem with the zlib compression,
a new CMake option (eg: CMAKE_OVL_MOUNT_COMPRESS_ZLIB) has been added.

Signed-off-by: Pierre Lebleu <>
4 years agolibfstools: move mount points when switching to JFFS2
Rafał Miłecki [Mon, 16 Apr 2018 14:11:47 +0000 (16:11 +0200)]
libfstools: move mount points when switching to JFFS2

Switching from "tmpfs" to "jffs2" happens after JFFS2 formatting is
done. During that time user can use filesystem (thanks to RAM) and the
role of switch2jffs() is to copy all changes to the JFFS2 overlay

What wasn't handled so far was moving mount points. User can create
custom mounts, cp command won't copy them and umounting "tmpfs" will
cause these mounts to go away. To preserve them switch2jffs() has to
find all custom mount points and move them to the new filesystem.

Signed-off-by: Rafał Miłecki <>
Acked-by: John Crispin <>
4 years agolibfstools: add "const" to char pointer arguments in mount_move()
Rafał Miłecki [Mon, 16 Apr 2018 12:30:12 +0000 (14:30 +0200)]
libfstools: add "const" to char pointer arguments in mount_move()

That function never modifies these strings so it can/should use consts.
It makes it a bit more flexible as now callers can also pass consts.

Signed-off-by: Rafał Miłecki <>
Acked-by: John Crispin <>
4 years agolibfstools: fix foreachdir() to pass dir with a trailing slash
Rafał Miłecki [Fri, 13 Apr 2018 11:26:14 +0000 (13:26 +0200)]
libfstools: fix foreachdir() to pass dir with a trailing slash

Commit cc63723d886fd ("overlay: use lstat rather than stat and make sure
there are no trailing spaces") changed behavior of foreachdir() breaking
some callbacks. Before that modification all callbacks were getting
directory with a trailing slash. Above commit started removing them.

This broke handle_whiteout() which doesn't work at all since then. It
constructs file paths incorrectly: slash is missing between directory
and a file name. It seems noone noticed it for years because this issue
got hidden by switch2jffs() which also handles whiteouts with its system
command "cp -a" call.

Fix that regression by setting trailing slash back - right after calling
lstat(). Also to keep code simple just skip all entries that aren't
directories. This keeps conditions for removing/setting trailing slash
trivial. A side effect is not calling callbacks for files which is a
free bonus optimization.

Fixes: cc63723d886fd ("overlay: use lstat rather than stat and make sure there are no trailing spaces")
Signed-off-by: Rafał Miłecki <>
Acked-by: John Crispin <>
4 years agostrip trailing spaces from vfat labels
John Crispin [Sun, 11 Feb 2018 13:24:59 +0000 (14:24 +0100)]
strip trailing spaces from vfat labels

Signed-off-by: John Crispin <>
4 years agofix vfat volume label
John Crispin [Mon, 22 Jan 2018 06:22:40 +0000 (07:22 +0100)]
fix vfat volume label

the libblkid copies len+1 bytes causing a buffer overrun.

Signed-off-by: John Crispin <>
4 years agoblock: support /dev/nvme* nodes
Daniel Golle [Fri, 2 Feb 2018 00:32:21 +0000 (01:32 +0100)]
block: support /dev/nvme* nodes

Add glob for NVM Express SSD device nodes to support systems with
such PCIe SSDs.

Signed-off-by: Daniel Golle <>
4 years agooverlay: fix compilation with glibc
Roman Yeryomin [Sun, 7 Jan 2018 18:04:02 +0000 (20:04 +0200)]
overlay: fix compilation with glibc

glibc fails with:
warn_unused_result [-Wunused-result]
   system("cp -a /tmp/root/upper/* / 2>/dev/null");

Regression intoroduced with 11efbf3b90f031ce634fc38cad07a4aef2985777
Reported here:

Signed-off-by: Roman Yeryomin <>
Acked-by: Florian Fainelli <>
4 years agolibfstools: optimize building directory string for glob
Rafał Miłecki [Fri, 5 Jan 2018 14:24:30 +0000 (15:24 +0100)]
libfstools: optimize building directory string for glob

glob should handle paths like /foo/bar//* so we probably don't need this
extra check & code.

Signed-off-by: Rafał Miłecki <>
4 years agolibfstools: support file paths longer than 255 chars
Rafał Miłecki [Thu, 4 Jan 2018 13:58:18 +0000 (14:58 +0100)]
libfstools: support file paths longer than 255 chars

Alloc globdir buffer dynamically and simply use realloc when needed.
This fixes e.g. segmentation fault in jffs2reset due to an infinite
recurrency when dealing with longs paths.

Signed-off-by: Rafał Miłecki <>
4 years agooverlay: fix race condition when switching to jffs2
Roman Yeryomin [Fri, 15 Dec 2017 14:10:30 +0000 (16:10 +0200)]
overlay: fix race condition when switching to jffs2

There is a race between `cp -a /tmp/root/* /rom/overlay` from
libfstools/overlay.c and a process creating new file(s) before
pivot(/rom, /mnt) occured.
That is a process can create a file and it will not be copied.

To workaround this, do additional copy after jffs2 is ready.
This doesn't completely solve the problem but since there
was no other fix provided since original RFC [1], it is better
than nothiing.


Signed-off-by: Roman Yeryomin <>
4 years agolibblkid-tiny: add support for NTFS superblock
Rafał Miłecki [Mon, 25 Dec 2017 19:52:11 +0000 (20:52 +0100)]
libblkid-tiny: add support for NTFS superblock

Its copied from the util-linux project and its libblkid. Call to the
blkid_probe_set_utf8label was commented out due to libblkid-tiny not
supporting it yet.

Signed-off-by: Rafał Miłecki <>
Acked-by: John Crispin <>
4 years agofstools: Replace strerror(errno) with %m format.
Rosen Penev [Sun, 10 Dec 2017 04:54:41 +0000 (20:54 -0800)]
fstools: Replace strerror(errno) with %m format.

Saves 1472 bytes under glibc. No other difference.

Signed-off-by: Rosen Penev <>
4 years agolibblkid-tiny: add support for UBI superblock
Rafał Miłecki [Mon, 7 Aug 2017 09:20:09 +0000 (11:20 +0200)]
libblkid-tiny: add support for UBI superblock

Detecting UBI superblock may be useful for tools wanting to simplify or
automate attaching UBI. Please note it's not directly related to the
ubifs support which is just a filesystem working on top of UBI volume.

This patch adds simple code reading UBI version and unique number and
setting it in the blkid_probe.

Signed-off-by: Rafał Miłecki <>
4 years agofstools: add btrfs support
Robert Young [Mon, 31 Jul 2017 16:49:59 +0000 (18:49 +0200)]
fstools: add btrfs support

Signed-off-by: Robert Young <>
4 years agoblock: support /dev/xvd* nodes
W. Michael Petullo [Wed, 12 Jul 2017 11:02:18 +0000 (07:02 -0400)]
block: support /dev/xvd* nodes

Xen provides paravirtualized block devices which most often appear as
/dev/xvd*. This patch adds this pattern to those known to the block
utilitiy. These devices require a kernel compiled with the xen-blkfront

Signed-off-by: W. Michael Petullo <>
4 years agolibfstools: fix matching device name
Daniel Golle [Fri, 30 Jun 2017 15:20:22 +0000 (17:20 +0200)]
libfstools: fix matching device name

compare strlen()+1 characters to make sure we match the trailing \0 as
well. Otherwise things get fishy when using lvm2, see this example:

/dev/mapper/data: UUID="xxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxxxx" MOUNT="/mnt" TYPE="LVM2_member"
/dev/mapper/data-fs: UUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" LABEL="xxxxxxxx" VERSION="1.0" MOUNT="/mnt" TYPE="ext4"

Signed-off-by: Daniel Golle <>
5 years agofstools: use -Wno-format-truncation instead of -Wno-error=format-truncation
Felix Fietkau [Tue, 9 May 2017 10:37:42 +0000 (12:37 +0200)]
fstools: use -Wno-format-truncation instead of -Wno-error=format-truncation

Fixes build error with older gcc

Signed-off-by: Felix Fietkau <>
5 years agolibfstools: fix multiple volume_identify usages with the same volume
Pieter Smith [Wed, 29 Mar 2017 16:21:56 +0000 (18:21 +0200)]
libfstools: fix multiple volume_identify usages with the same volume

This fixes e.g. factory-flashed startup issue with jffs2 on ubi overlay

Commit ba019965 ("libfstools: accept volume as argument in most calls")
broke startup for factory-flashed jffs2 on ubi systems, causing substantial
slowdown in factory environments.

When starting up with a factory-flashed jffs2 on ubi system, the "rootfs_data"
volume contains a deadcode marker. In the start phase, mount_root then mounts a
tmpfs overlay, and postpones remounting of the jffs2 overlay until the done
phase of the startup.

The refactoring in ba019965 eliminated an unneeded call to volume_find() when
done() called jffs2_switch(). Unfortunately the refactoring did not take into
account that volume_identify() does not function correctly when called twice in
a row on the same struct volume when using an mtd driver.

mtd_volume_identify() uses mtd_volume_load() to open an fd to the mtd device
and reads a potential deadcode marker from the fd. The first time this works,
and FS_DEADCODE is returned.

When volume_identify() is called a second time however, mtd_volume_load()
notices that we already have an open fd, does nothing further and returns 0
without resetting the file offset to 0. mtd_volume_identify() now reads past
the deadcode marker and now returns FS_JFFS2 if the mtd device is a UBIVOLUME.

jffs2_switch() then handles the wrong case, either pulling the root out from
under user-space in Chaos Calmer, or indefinitely sticking to a tmpfs overlay
in later OpenWRT builds.

Signed-off-by: Pieter Smith <>
Signed-off-by: Rafał Miłecki <>
5 years agobuild: disable the format-truncation warning error to fix gcc 7 build errors
Felix Fietkau [Thu, 4 May 2017 14:13:34 +0000 (16:13 +0200)]
build: disable the format-truncation warning error to fix gcc 7 build errors

Signed-off-by: Felix Fietkau <>
5 years agolibfstools: silence mkfs.{ext4,f2fs}
Daniel Golle [Thu, 20 Apr 2017 17:50:41 +0000 (19:50 +0200)]
libfstools: silence mkfs.{ext4,f2fs}

Reduce noise during firstboot when creating overlay fs on block
rootdisk devices.

Signed-off-by: Daniel Golle <>
5 years agocmake: Make blockd link against libjson-c
Florian Fainelli [Tue, 28 Mar 2017 00:10:57 +0000 (17:10 -0700)]
cmake: Make blockd link against libjson-c

Similar to commit 35aa20c51995 ("cmake: Link against libjson-c"), blockd
uses libblob_msg which needs libjson-c.

Fixes: 98bbb5a068d6 ("blockd: add automounting support")
Signed-off-by: Florian Fainelli <>
5 years agolibfstools: add basic documentation of mount functions
Rafał Miłecki [Mon, 27 Mar 2017 11:17:40 +0000 (13:17 +0200)]
libfstools: add basic documentation of mount functions

Signed-off-by: Rafał Miłecki <>
Acked-by: John Crispin <>
5 years agoblockd: add automounting support
John Crispin [Tue, 21 Feb 2017 11:52:15 +0000 (12:52 +0100)]
blockd: add automounting support

Signed-off-by: John Crispin <>
5 years agoadd missing includes
Felix Fietkau [Sat, 11 Feb 2017 15:01:50 +0000 (16:01 +0100)]
add missing includes

On glibc 2.25, sys/sysmacros.h needs to be included for makedev, major
and minor.

Signed-off-by: Felix Fietkau <>
5 years agolibfstools: Check return values for fread and system
Florian Fainelli [Sun, 4 Dec 2016 23:13:49 +0000 (15:13 -0800)]
libfstools: Check return values for fread and system

libfstools/rootdisk.c: In function 'rootdisk_volume_identify':
libfstools/rootdisk.c:172:7: error: ignoring return value of 'fread', declared with attribute warn_unused_result [-Werror=unused-result]
  fread(&magic, sizeof(magic), 1, f);
libfstools/rootdisk.c:179:7: error: ignoring return value of 'fread', declared with attribute warn_unused_result [-Werror=unused-result]
  fread(&magic, sizeof(magic), 1, f);
libfstools/rootdisk.c: In function 'rootdisk_volume_init':
libfstools/rootdisk.c:268:9: error: ignoring return value of 'system', declared with attribute warn_unused_result [-Werror=unused-result]
cc1: all warnings being treated as errors

Signed-off-by: Florian Fainelli <>
5 years agofstools: added f2fs to block
Alberto Bursi [Thu, 27 Oct 2016 20:59:27 +0000 (20:59 +0000)]
fstools: added f2fs to block

added code to block so it can recognize and operate the filesystem checker of f2fs
added f2fs to the filesystem whitelist of block so it can mount it on
/overlay at boot.

Signed-off-by: Alberto Bursi <>
5 years agoblock: fall back to external mount helper
Jo-Philipp Wich [Fri, 21 Oct 2016 14:53:57 +0000 (16:53 +0200)]
block: fall back to external mount helper

If the mount(2) syscall fails with ENOENT, attempt to mount the filesystem
using an external "/sbin/mount.$fstype" command.

This allows filesystems which do not have direct kernel support, like fuse-
or network filesystems.

Signed-off-by: Jo-Philipp Wich <>
5 years agoblock: fix error reporting
Jo-Philipp Wich [Fri, 21 Oct 2016 13:46:25 +0000 (15:46 +0200)]
block: fix error reporting

The current block code wrongly reported the return value of the mount() and
umount2() syscalls, which is always -1 in case the call failed.

Use errno and strerror(errno) instead to propagate the correct error code to
the user.

Signed-off-by: Jo-Philipp Wich <>
5 years agolibblkid-tiny: remove unused name member
Jo-Philipp Wich [Mon, 17 Oct 2016 11:12:31 +0000 (13:12 +0200)]
libblkid-tiny: remove unused name member

The "name" member of struct blkid_struct_probe is not used anywhere anymore
so remove it from the definition to save some stack and heap space.

Signed-off-by: Jo-Philipp Wich <>
5 years agoprobe: stop handling name property
Jo-Philipp Wich [Mon, 17 Oct 2016 11:10:07 +0000 (13:10 +0200)]
probe: stop handling name property

In the full liblkid there is no tag called "NAME" and the name value reported
by libblkid-tiny was just the kind of uuid passed to blkid_probe_set_uuid_as().

Strip any handling of this property to allow removing it from libblkid-tiny.

Signed-off-by: Jo-Philipp Wich <>
5 years agoblock: remove handling of name property
Jo-Philipp Wich [Mon, 17 Oct 2016 11:06:57 +0000 (13:06 +0200)]
block: remove handling of name property

There is no concept of a "NAME" property within libblkid, the value previously
used was the name parameter of blkid_probe_set_uuid_as() which does not denote
the file system name, but the kind of UUID to store.

Since the value never makes sense, stop using it when reporting block

Signed-off-by: Jo-Philipp Wich <>
5 years agolibblkid-tiny: fix SquashFS version detection on different endian systems
Jo-Philipp Wich [Mon, 17 Oct 2016 09:40:14 +0000 (11:40 +0200)]
libblkid-tiny: fix SquashFS version detection on different endian systems

Signed-off-by: Jo-Philipp Wich <>
5 years agolibblkid-tiny: avoid setting phantom UUIDs
Jo-Philipp Wich [Mon, 17 Oct 2016 09:28:31 +0000 (11:28 +0200)]
libblkid-tiny: avoid setting phantom UUIDs

When blkid_probe_set_uuid_as() is invoked with a non-NULL name parameter
then the name parameter denotes the kind of UUID (like "EXT_JOURNAL") not
the name of the file system.

Only copy the UUID value to the probe uuid member if the given name is
either NULL or "UUID".

Signed-off-by: Jo-Philipp Wich <>
5 years agoprobe: add full libblkid support
Jo-Philipp Wich [Sun, 16 Oct 2016 22:05:15 +0000 (00:05 +0200)]
probe: add full libblkid support

Attempt to dlopen() at runtime and use it for proping
filesystems if available.

Signed-off-by: Jo-Philipp Wich <>
5 years agoblock: add probe abstraction layer
Jo-Philipp Wich [Sun, 16 Oct 2016 20:45:59 +0000 (22:45 +0200)]
block: add probe abstraction layer

Add an abstraction layer which separates block.c from libblkid-tiny implementation
details in order to prepare support for optionally using the full libblkid.

Signed-off-by: Jo-Philipp Wich <>
5 years agolibfstools: properly label ext4 overlay
Daniel Golle [Fri, 23 Sep 2016 03:10:42 +0000 (05:10 +0200)]
libfstools: properly label ext4 overlay

Signed-off-by: Daniel Golle <>
5 years agoblock: also probe loop devices
Daniel Golle [Fri, 23 Sep 2016 03:10:05 +0000 (05:10 +0200)]
block: also probe loop devices

Signed-off-by: Daniel Golle <>
5 years agoblock: include mountpoint in info output
Daniel Golle [Sun, 24 Jul 2016 18:44:38 +0000 (20:44 +0200)]
block: include mountpoint in info output

Signed-off-by: Daniel Golle <>
5 years agolibfstools: gather mountpoints from /proc/self/mountinfo
Daniel Golle [Sun, 11 Sep 2016 14:52:52 +0000 (16:52 +0200)]
libfstools: gather mountpoints from /proc/self/mountinfo

This allows identifying /dev/root by its major:minor number which
are part of /proc/self/mountinfo but aren't contained in /proc/mounts.
Also fix jffs2reset when using an ext4 overlay by adding it to the
list of filesystems allowed if the root_only parameter of
find_mount_point is set.

Signed-off-by: Daniel Golle <>
5 years agolibfstools: add rootdisk overlay volume support
Felix Fietkau [Tue, 6 Sep 2016 14:37:13 +0000 (16:37 +0200)]
libfstools: add rootdisk overlay volume support

Uses either ext4 or f2fs, depending on the overlay volume size

Signed-off-by: Felix Fietkau <>
5 years agolibfstools: replace hardcoded mentions of jffs2 in a few places
Felix Fietkau [Thu, 8 Sep 2016 11:03:26 +0000 (13:03 +0200)]
libfstools: replace hardcoded mentions of jffs2 in a few places

Signed-off-by: Felix Fietkau <>