Commit Graph

74 Commits

Author SHA1 Message Date
Karel Zak 9ada40d3cc fstrim: clean return code on --quiet-unsupported
This feature is already supported for -a and -A. Let's support it also
when FS specified on command line.

Addresses: https://github.com/systemd/mkosi/pull/721
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-07-20 11:54:03 +02:00
Karel Zak 3e8403522c fstrim: fix paths comparison
Fix: https://github.com/karelzak/util-linux/issues/1312
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-05-20 09:29:34 +02:00
Karel Zak a887d587bf fstrim: fix memory leak [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-03-25 11:53:02 +01:00
Karel Zak 9995da01e0 fstrim: add --listed-in <file[:file ..]>
This new option works like --all but it allows to specify multiple
files with filesystems to make fstrim configuration more portable
between distributions. For example:

 fstrim --listed-in /etc/fstab:/proc/self/mountinfo

forces fstrim to try fstab and if unsuccessful than try mountinfo.

Addresses: https://github.com/karelzak/util-linux/issues/1019
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-22 14:48:31 +02:00
Karel Zak ef89802e83 fstrim: rename --quite to --quite-unsupported
We use --verbose together with --quite in service files. It seems
confusing, let's make the option more descriptive.

Addresses: https://github.com/karelzak/util-linux/issues/1001
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-03-31 12:26:54 +02:00
Karel Zak f1970cc557 docs: improve size arguments description in --help output
Let's add "Arguments:" section to the --help output and describe
{K,M,G...}iB suffixes there.

Addresses: https://github.com/karelzak/util-linux/pull/917
Co-Author: ed <ed@s5h.net>
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-05 11:01:36 +01:00
Karel Zak f227757c77 fstrim: ignore non-directory mountpoints
It seems better to silently ignore mount binds on file (= mountpoint
is not a directory).

This patch also fixes use-after-free bug from commit 402006fa6e.

Addresses: https://github.com/karelzak/util-linux/issues/857
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-09-05 16:32:10 +02:00
Karel Zak 6466959d9a fstrim: update man page, reuse libmnt_iter
* add info about read-only to the man page
* don't be systemd specific, people aso use crond
* reuse libmnt_iter

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-06-11 11:05:12 +02:00
Stanislav Brabec 402006fa6e fstrim: properly de-duplicate fstrim -A
fstab can contain tag based mounts. De-duplication by source has to be
done after resolving the full source path.

Perform the table iteration twice. First time, prepare for
de-duplication, second time perform the TRIM itself.

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2019-06-11 10:26:55 +02:00
Stanislav Brabec 2d22ac64e4 fstrim -a/-A: Skip read-only volumes
Calling TRIM on some read-only volumes can fail with:
fstrim: /win: FITRIM ioctl failed: Bad file descriptor

Skipping all read-only mounts seems to be safe and logical strategy.

Fixes opensuse#1106214.

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2019-06-11 10:23:45 +02:00
Stanislav Brabec 4080be3d7d fstrim: Fix fstrim_all() comment
"convert LABEL=" does not happens in mnt_fs_get_source(), but later in
mnt_resolve_spec(). To make this more clean, move the comment before this
chunk of code.

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2019-06-11 10:23:45 +02:00
Karel Zak 5fcdf204b0 fstrim: affect only warnings by --quiet
We need the same return code from fstrim_filesystem() independently on
--quiet command line option.

Addresses: https://github.com/karelzak/util-linux/pull/791
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-09 13:59:54 +02:00
Sami Kerola 374baa6f64
fstrim: add --quiet option to suppress error messages
When fstrim interacts with NTFS it result can be error reporting bad file
descriptor.  That seems to be a bug in NTFS.  While waiting driver to get on
top of the issue and be commonly available lets add to fstrim option to make
it be more silent about errno 9 aka EBADF, Bad file descriptor.

Reported-by: https://github.com/moviuro
Proposed-by: Dave Reisner <dreisner@archlinux.org>
Reference: https://bugs.archlinux.org/task/62288
Addresses: https://github.com/karelzak/util-linux/issues/789
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-05-07 20:18:08 +01:00
Karel Zak 2c308875a7 misc: consolidate version printing and close_stdout()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-16 15:14:13 +02:00
Wang Shilong 8302b2009a fstrim: get realpath before trim ioctl
Original motivation is we want to run fstrim command
on Lustre[1] osd server mount point directly, however
our server mount point doesn't export osd directory
to users, and it will cause following command fail:

 $fstrim -v /mnt/mds/

But following succeed:

 $fstrim -v /mnt/mds

We could improve this a bit by getting realpath
before trapping kernel, this also give benifits
to normal use cases.

Cc: Andreas Dilger <adilger@dilger.ca>
Cc: Shuichi Ihara <sihara@ddn.com>
[1] http://wiki.lustre.org/Main_Page
Signed-off-by: Wang Shilong <wshilong@ddn.com>
2019-04-15 11:12:55 +02:00
Marcos Paulo de Souza e108643981 fstrim.c: Remove commnet about vfat not supporting fstrim
Commit f663b5b38f ("fat: add FITRIM ioctl for FAT file system") in
linux kernel added support for using fstrim with vfat filesystem.

Signed-off-by: Marcos Paulo de Souza <marcos.souza.org@gmail.com>
2019-04-01 18:38:31 -03:00
Karel Zak fe7d63105d fstrim: check for read-only devices on -a/-A
to avoid "FITRIM ioctl failed: Remote I/O error"

Addresses: https://github.com/karelzak/util-linux/issues/738
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-01-11 10:44:14 +01:00
Karel Zak f78f2983d3 fstrim: fix usage()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-10 12:03:18 +01:00
Karel Zak 410d12e5be fstrim: trim also root FS on --fstab
The root fs ("/") is optional in many case in /etc/fstab. This patch
forces fstrim to read kernel command line for the root= option to get
root FS if the entry is missing in your fstab.

Addresses: https://github.com/karelzak/util-linux/issues/719
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-12-10 11:45:03 +01:00
Karel Zak 26b6525a51 fstrim: use -n shortcut for --dry-run
It seems 'n' is more common than 'd'. The patch also cleanup
getopt_long() options string.

Addresses: fda0e2cf04 (commitcomment-30097920)
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-08-16 14:00:45 +02:00
Karel Zak c5b8909f13 fstrim: add -A|--fstab to trim according to /etc/fstab
Addresses: https://github.com/karelzak/util-linux/issues/673
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-31 16:38:59 +02:00
Karel Zak 72213916c3 fstrim: add control struct
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-31 15:58:49 +02:00
Karel Zak fda0e2cf04 fstrim: add --dry-run
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:19:07 +02:00
Karel Zak c69bd2cbff fstrim: use new ul_path_* API, improve sysfs context usage
* improve has_discard() function to initialize sysfs context only once
* use new ul_path_* API

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-06-21 13:07:46 +02:00
Karel Zak 7afdbb6f07 fstrim: Return EXIT_FAILURE when FTRIM ioctl fails
commit 36c370cbf1 adds fstrim_filesystem()
that return -1 or 1 depending on the FTRIM ioctl failures.

The fstrim_filesystem() return codes should not be used as exit codes.

Reported-by: Gwendal Grignou <gwendal@chromium.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-04-06 12:53:18 +02:00
Karel Zak 061d1a5109 libmount: include sys/mount.h only if necessary
Addresses: https://github.com/systemd/systemd/issues/8507
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-03-22 14:05:17 +01:00
Karel Zak 43abda6686 fstrim: cleanup includes
* HAVE_SYS_FS_H is incorrect (should be HAVE_LINUX_FS_H)

* linux/fs.h cannot be included together with sys/mount.h as the both
  files define MS_* constants. The libmount.h includes sys/mount.h now.

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-03-22 13:17:10 +01:00
Karel Zak a0357292b5 fstrim: fix fs.h and mount.h collision
Unfortunately, old version of the file linux/fs.h defines MS_*
macros, so the file cannot be included together with sys/mount.h.

We include sys/mount.h from libmount.h now.

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-03-20 11:42:34 +01:00
Karel Zak 3bf6c44462 fstrim: don't include exitcodes.h
The code uses generic and libmount return codes only.

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-02-01 14:49:49 +01:00
Ruediger Meier f45f3ec34a misc: consolidate macro style USAGE_HELP_OPTIONS
changed in include/c.h and applied via sed:

  sed -i 's/fprintf.*\(USAGE_MAN_TAIL.*\)/printf(\1/' $(git ls-files -- "*.c")
  sed -i 's/print_usage_help_options\(.*\);/printf(USAGE_HELP_OPTIONS\1);/' $(git ls-files -- "*.c")

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-29 16:54:33 +02:00
Ruediger Meier b1a294c448 misc: introduce print_usage_help_options()
Consolidate --help and --version descriptions. We are
now able to align them to the other options.

We changed include/c.h. The rest of this patch was
generated by sed, plus manually setting the right
alignment numbers. We do not change anything but
white spaces in the --help output.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-27 12:26:19 +02:00
Ruediger Meier 6e1eda6f22 misc: never use usage(stderr)
Here we fix all cases where we have usage(FILE*)
functions.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-26 14:38:24 +02:00
Karel Zak c0b0748544 fstrim: remove obsolete comment
We do not de-duplicate by FS root (since afa382f2e0).

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-06-15 11:49:09 +02:00
Alex Ivanov 155d48f590 fstrim: prefer earlier mounted filesystems
fstrim --all is broken in a way that if there is a bind mount for some filesystem,
that filesystem will not be trimmed. This is especially critical for e.g.
NixOS distribution, which needs bind mount within root fs:
https://github.com/NixOS/nixos/blob/master/modules/system/boot/stage-2-init.sh#L55

Currently for a given filesystem during "de-duplication by source and root" phase,
an early mounted fs path is filtered (e.g. "/"), while later mounted fs path is kept
(e.g. "/nix/store") though anyway discarded later (since it's an overlaying mount).
This leads to skipped trimming.

So flip this behaviour. Should also help for other types of overlaying mounts.

Reference:
https://github.com/karelzak/util-linux/blob/stable/v2.29/libmount/src/tab.c#L715

Signed-off-by: Alex Ivanov <yourbestfriend@openmailbox.org>
2017-06-15 11:45:35 +02:00
Karel Zak 8e9039afe4 fstrim: use MNT_EX_* from libmount
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-04-27 14:26:16 +02:00
Sami Kerola 8791804065 misc: do not use plain 0 as NULL [smatch scan]
text-utils/tailf.c:69:21: warning: Using plain integer as NULL pointer

Since many 'struct option' has used zero as NULL make them more readable in
same go by reindenting, and using named argument requirements.

Reference: https://lwn.net/Articles/93577/
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-02-20 12:58:49 +01:00
Stanislav Brabec afa382f2e0 fstrim: de-duplicate btrfs sub-volumes
There was a comparison of roots in uniq_fs_source_cmp(). As we care only on
device names, comparing roots makes only a little sense, and it breaks on btrfs.
As a result, FITRIM ioctl() is called for each subvolume. But by design, only
one TRIM is needed for the whole btrfs volume.

Remove it to be able to de-duplicate btrfs subvolumes.

Fixes opensuse #1020077.

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
2017-01-31 12:26:01 +01:00
Karel Zak 677ec86cef Use --help suggestion on invalid option
The current default is to print all usage() output. This is overkill
in many case.

Addresses: https://github.com/karelzak/util-linux/issues/338
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-12-19 13:13:34 +01:00
Sebastian Rasmussen 9e93004171 misc: Fix various typos
Fix various typos in error messages, warnings, debug strings,
comments and names of static functions.

Signed-off-by: Sebastian Rasmussen <sebras@gmail.com>
2016-05-31 23:40:21 +02:00
Sami Kerola ee24ab6f1e misc: fix redundant assignment and reassignments before use [cppcheck]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-08-08 07:46:39 +01:00
Karel Zak 289b153340 fstrim: close dir before exit [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-06-29 16:14:35 +02:00
Karel Zak 8dd51c1833 fstrim: de-duplicate by mount source too
Now fstrim de-duplicates by target (mountpoint). This patch adds
de-duplication according to mount source (device) to avoid bind mounts
or devices mounted more than once. Note that the patch also check FS
root, the different FS roots of the same multi-root FS (e.g. btrfs)
maybe mounted on different places.

  # mount --bind /home/wine /mnt/test

old version:

  # fstrim -av
  /mnt/test: 0 B (0 bytes) trimmed            <---
  /home/wine: 0 B (0 bytes) trimmed           <---
  /boot: 0 B (0 bytes) trimmed
  /home: 0 B (0 bytes) trimmed
  /: 0 B (0 bytes) trimmed

new version:

  # fstrim -av
  /mnt/test: 0 B (0 bytes) trimmed            <---
  /boot: 0 B (0 bytes) trimmed
  /home: 171.8 MiB (180113408 bytes) trimmed
  /: 0 B (0 bytes) trimmed

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1162213
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-06-10 14:19:45 +02:00
Benno Schulenberg fc14ceba5e textual: grammarize and harmonize the stat error message
The message "stat failed %s" seems to say that stat() failed to
do something, or failed to pass a test, but of course it means
that the statting of something failed.  So say so.  Also make
two very similar messages equal to this one.

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-02-02 11:27:10 +01:00
Benno Schulenberg 451dbcfae1 textual: add a docstring to most of the utilities
This adds a concise description of a tool to its usage text.

A first form of this patch was proposed by Steven Honeyman
(see http://www.spinics.net/lists/util-linux-ng/msg09994.html).

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2015-01-06 11:27:38 +01:00
Sami Kerola b35c37274c fstrim: initialize path variable
Reading the code this initialization is not necessary, so the change is
mostly about silencing a compiler warning and ensuring the program keeps
on working if there is a future bug that could make uninitialized 'path'
reachable to use.

sys-utils/fstrim.c:247: warning: 'path' may be used uninitialized in this
function

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-10-31 10:38:58 +01:00
Benno Schulenberg 1d23119072 textual: remove some inconsistent periods from error messages
While doing so, also improve translatability and some wordings.

Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2014-08-11 14:51:54 +02:00
Karel Zak df1a1ca0c9 fstrim: fix memory leak [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-07-17 15:07:49 +02:00
Sami Kerola e612ead96a fstrim: avoid TOCTOU race
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-07-17 14:01:02 +02:00
Sami Kerola 0e65dcde88 textual: fix some typos
Found with misspell-check version 2.0d.

Reference: https://github.com/lyda/misspell-check
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2014-07-13 17:21:02 +01:00
Benno Schulenberg d6bbe8044b textual: small inprovements to usage and man page of fstrim
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
2013-12-12 11:10:53 +01:00