summaryrefslogtreecommitdiffstats
path: root/lib/loopdev.c
Commit message (Collapse)AuthorAgeFilesLines
* Revert "lib/loopdev.c: Inline loopcxt_has_device"Karel Zak2019-06-181-1/+1
| | | | | | ... no caller in that file, this change has no effect. This reverts commit 3bb960c7b5f1428f1bff885b2667787e8af5001b.
* losetup: use xstrncpy()Karel Zak2019-05-201-6/+4Star
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* losetup: keep static analyzer happy [coverity scan]Karel Zak2019-05-141-4/+4
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev.c: Retry LOOP_SET_STATUS64 on EAGAINRomain Izard2019-05-071-4/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | A recent bugfix in the Linux kernel made it possible for the LOOP_SET_STATUS64 ioctl to fail when called with a non-zero offset, with an EAGAIN errno: 5db470e229e2 loop: drop caches if offset or block_size are changed This fix changes a silent failure (where mount could sometimes access the backing loop image through the cache without the specified offset) to an explicit failure, and it has also been backported on stable branches. On a 5.0 kernel, other changes to the loop driver make it hard to get generate the EAGAIN error, but this bugfix has also been backported to stables branches, without these changes. At least with the 4.14 stable branch, the EAGAIN error can be quickly generated with the following loop: while mount -o loop,offset=239 disk point && umount point; do :; done Retry the ioctl when it fails with EAGAIN, which means that mount or losetup will eventually succeed when encountering this case. [kzak@redhat.com: - use our local portable xusleep()] Signed-off-by: Romain Izard <romain.izard.pro@gmail.com> Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev.c: Inline loopcxt_has_deviceMarcos Paulo de Souza2019-03-031-1/+1
| | | | | | | loopcxt_has_device is a one liner, so adding inline statement does not hurt. Signed-off-by: Marcos Paulo de Souza <marcos.souza.org@gmail.com>
* docs: fix typos [codespell]Sami Kerola2019-02-181-3/+3
| | | | | Reference: https://github.com/codespell-project/codespell Signed-off-by: Sami Kerola <kerolasa@iki.fi>
* lib/loopdev: set blocksize when create a new deviceKarel Zak2019-01-221-0/+23
| | | | | | | | | | | The partition scanner in kernel depends on blocksize. We need to set the blocksize before we call LOOP_SET_STATUS64 (this ioctl triggers the scanner). This patch extends the internal API to save blocksize into loopdev context to be usable later for loopcxt_setup_device(). Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev: differentiate between setter()s and ioctl callsKarel Zak2019-01-221-5/+5
| | | | | | | | Now the internal API uses loopcxt_set_...() to set context variables as well as to call ioctls. This patch introduces loopcxt_ioctl_...() to makes things more obvious to readers. Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev: fix compiler warning [-Wcast-qual]Karel Zak2018-07-231-2/+2
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev: remove obsolete macroKarel Zak2018-06-211-1/+0Star
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev: use new ul_path_* APIKarel Zak2018-06-211-24/+25
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* misc: fix typos using codespellRuediger Meier2018-02-161-1/+1
| | | | | | Some more funny typos, please review carefully. Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
* include/debug: introduce __UL_INIT_DEBUG_FROM_STRING()Karel Zak2018-01-171-1/+1
| | | | | | | Let's make it possible to use debug.h without environment variables. Suggested-by: J William Piggott <elseifthen@gmx.com> Signed-off-by: Karel Zak <kzak@redhat.com>
* include/debug: don't print pointer address for SUID programsKarel Zak2018-01-121-0/+3
| | | | | | | | | | | | | | | | * introduce new flag __UL_DEBUG_FL_NOADDR to suppress pointer address printing * use __UL_DEBUG_FL_NOADDR when SUID * move ul_debugobj() to debugobj.h, and require UL_DEBUG_CURRENT_MASK to provide access to the current mask from ul_debugobj(). It's better than modify all ul_debugobj() calls and use the global mask as argument. * remove never used UL_DEBUG_DEFINE_FLAG Reported-by: halfdog <me@halfdog.net> Signed-off-by: Karel Zak <kzak@redhat.com>
* losetup: fix conflicting types for ‘loopcxt_set_blocksize’Karel Zak2017-10-041-2/+6
| | | | | Reported-by: Lars Wendler <polynomial-c@gentoo.org> Signed-off-by: Karel Zak <kzak@redhat.com>
* losetup: Add support for logical block sizeStanislav Brabec2017-09-271-0/+50
| | | | | | | | | | | | | | | | | | | | Kernel since 4.14 supports setting of logical block size[1]. It allows to create loop devices that report logical block size different from 512. Add support for this feature to losetup. References: [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/block/loop.c?id=89e4fdecb51cf5535867026274bc97de9480ade5 [kzak@redhat.com: - fix loopcxt_get_blocksize() - remove lo_blocksize from loop_info64] Signed-off-by: Stanislav Brabec <sbrabec@suse.cz> Cc: Ming Lei <ming.lei@redhat.com> Cc: Hannes Reinecke <hare@suse.com> Cc: Omar Sandoval <osandov@fb.com> Cc: Jens Axboe <axboe@kernel.dk> Signed-off-by: Karel Zak <kzak@redhat.com>
* misc: fix gcc-7 snprintf warnings -Wformat-truncationRuediger Meier2017-06-141-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ../lib/loopdev.c: In function 'loopcxt_next_from_sysfs': ../lib/loopdev.c:545:32: warning: '/loop/backing_file' directive output may be truncated writing 18 bytes into a region of size between 1 and 256 [-Wformat-truncation=] snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name); ^~~~~~~~~~~~~~~~~~~~~~ ../lib/loopdev.c:545:3: note: 'snprintf' output between 19 and 274 bytes into a destination of size 256 snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../lib/sysfs.c: In function 'sysfs_is_partition_dirent': ../lib/sysfs.c:343:31: warning: '/start' directive output may be truncated writing 6 bytes into a region of size between 1 and 256 [-Wformat-truncation=] snprintf(path, sizeof(path), "%s/start", d->d_name); ^~~~~~~~~~ ../lib/sysfs.c:343:2: note: 'snprintf' output between 7 and 262 bytes into a destination of size 256 snprintf(path, sizeof(path), "%s/start", d->d_name); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../lib/sysfs.c: In function 'sysfs_partno_to_devno': ../lib/sysfs.c:372:32: warning: '/partition' directive output may be truncated writing 10 bytes into a region of size between 1 and 256 [-Wformat-truncation=] snprintf(path, sizeof(path), "%s/partition", d->d_name); ^~~~~~~~~~~~~~ ../lib/sysfs.c:372:3: note: 'snprintf' output between 11 and 266 bytes into a destination of size 256 snprintf(path, sizeof(path), "%s/partition", d->d_name); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../lib/sysfs.c:377:33: warning: '/dev' directive output may be truncated writing 4 bytes into a region of size between 1 and 256 [-Wformat-truncation=] snprintf(path, sizeof(path), "%s/dev", d->d_name); ^~~~~~~~ ../lib/sysfs.c:377:4: note: 'snprintf' output between 5 and 260 bytes into a destination of size 256 snprintf(path, sizeof(path), "%s/dev", d->d_name); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
* lib/loopdev: remove unnecessary headerKarel Zak2017-05-301-1/+0Star
| | | | | Reported-by: Assaf Gordon <assafgordon@gmail.com> Signed-off-by: Karel Zak <kzak@redhat.com>
* misc: add static keyword to where needed [smatch scan]Sami Kerola2017-02-201-1/+1
| | | | | | | text-utils/rev.c:68:9: warning: symbol 'buf' was not declared. Should it be static? Signed-off-by: Sami Kerola <kerolasa@iki.fi>
* lib/loopdev: Set errno in is_loopdev on errorTobias Stoeckmann2016-08-311-5/+6
| | | | | | | | | | | | | | The function is_loopdev does not set errno if the supplied string does not reference a valid loop device. Fix this to avoid an error message like this one: losetup: /: failed to use device: Success I prefer this one: losetup: /: failed to use device: No such device Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
* loopdev: Implememt loopcxt_set_status()Stanislav Brabec2016-08-171-1/+35
| | | | | | | | | | | | Implement stand-alone loopcxt_set_status(). It allows manipulation with some loop device parameters even if it is initialized. Its function is limited by the kernel implementation, and only a small subset of changes is allowed. For more see linux/drivers/block/loop.c:loop_set_status() Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
* losetup: add --nooverlap optionsKarel Zak2016-08-171-5/+9
| | | | | | | | | | | | | | | | | | | | | This patch introduces overlap detections and loop devices re-use for losetup(8). We already support this feature for mount(8) where it's enabled by default (because we mount filesystems and it's always mistake to share the same filesystem between more loop devices). Stanislav has suggested to enable this feature also for losetup by default. I'm not sure about it, IMHO it's better to keep losetup(8) simple and stupid by default, and inform users about possible problems and solutions in the man page. The feature forces losetup to scan all loop devices always when new one is requested. This maybe disadvantage (especially when we use control-loop to avoid /sys or /dev scans) on system with huge number of loop devices. Co-Author: Stanislav Brabec <sbrabec@suse.cz> Signed-off-by: Karel Zak <kzak@redhat.com>
* libmount: one iteration to detect overlap and reuse loopdevKarel Zak2016-08-041-4/+18
| | | | | | | | | | The current code scans loopdevs to detect already used loop device and another scan to detect overlap. Let's use one scan only, for this purpose loopcxt_find_overlap() has been modified to return info (rc==2) about full size and offset match. Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev: cleanup sizelimit checkKarel Zak2016-08-041-9/+11
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* Implement loopcxt_check_conflict()Stanislav Brabec2016-08-031-0/+55
| | | | | | | Add a function that searches for a possible conflicting (i. e. overlaying loop device). Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
* Add sizelimit to internal APIStanislav Brabec2016-08-031-7/+19
| | | | | | | | | | | | | | Fully safe checks of loop device need to check sizelimit. To prevent need of two nearly equal functions, introduce sizelimit parameter to several internal functions: loopdev_is_used() loopdev_find_by_backing_file() loopcxt_is_used() loopcxt_find_by_backing_file() If sizelimit is zero, fall back to the old behavior (ignoring of sizelimit). Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
* misc: Fix various typosSebastian Rasmussen2016-05-311-3/+3
| | | | | | | Fix various typos in error messages, warnings, debug strings, comments and names of static functions. Signed-off-by: Sebastian Rasmussen <sebras@gmail.com>
* lib: avoid double free in loopdev.cSami Kerola2016-04-201-0/+1
| | | | | | | Found with scan-build. Reviewed-by: Yuriy M. Kaminskiy <yumkam@gmail.com> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
* include: move sys/sysmacros.h to c.hKarel Zak2016-03-081-1/+0Star
| | | | | | | The file is no portable (#ifdef HAVE_SYS_SYSMACROS_H is necessary), but needed on many places. It seems better to keep it in c.h. Signed-off-by: Karel Zak <kzak@redhat.com>
* lib: remove openat fallback functions (include/at.h)Ruediger Meier2016-03-071-2/+1Star
| | | | | | | | | | | | | | I have validated that we are still compatible at least back to - openSUSE 11.4 - SLE 11 - RHEL/CentOS 6 - OSX 10.10.x, (Xcode 6.3) - FreeBSD 10.2 Confirmed incompatibility: - OSX 10.9.x, (Xcode 6.2) Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
* docs: fix typos found by codespellRuediger Meier2016-02-031-1/+1
| | | | | | Using "codespell" from https://github.com/lucasdemarchi/codespell Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
* losetup: support list direct ioMing Lei2015-11-191-0/+22
| | | | | | So that user can see if DIO is set for current loop device. Signed-off-by: Ming Lei <tom.leiming@gmail.com>
* losetup: support ioctl cmd of LOOP_SET_DIRECT_IOMing Lei2015-11-191-0/+18
| | | | | | | | From v4.4, linux kernel starts to support direct I/O and AIO to backing file for loop driver, so allow losetup to enable the feature by using LOOP_SET_DIRECT_IO ioctl cmd. Signed-off-by: Ming Lei <tom.leiming@gmail.com>
* losetup: be careful with errnoKarel Zak2015-06-151-0/+6
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev: remove test programKarel Zak2014-11-041-149/+0Star
| | | | | | | All is already covered by losetup and mount. The test program has never been used in our regression tests. Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev: replace custom DBG() with include/debug.hKarel Zak2014-11-041-102/+88Star
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev: comparing an array to null is not useful [coverity scan]Karel Zak2014-07-171-2/+2
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev.c: reset errno before strtol() callYuriy Nazarov2014-06-191-0/+1
| | | | Fixed unsuccessful attempt to find unused loop devices if 0-7 devices already used and /dev/loop directory exists.
* losetup: don't clear FD at EBUSY error in loopcxt_setup_device()Stanislav Brabec2014-05-281-1/+1
| | | | | | | | | loopback lib clears the existing FD unconditionally at error in loopcxt_setup_device(). This is done even after EBUSY, thus the second call actually clears the previous setup wrongly. Author: Takashi Iwai <tiwai@suse.de> Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
* losetup: fix -f on systems without /dev/loop-controlKarel Zak2014-05-141-14/+35
| | | | | Reported-by: Ruediger Meier <sweet_f_a@gmx.de> Signed-off-by: Karel Zak <kzak@redhat.com>
* losetup: wait for udevKarel Zak2014-01-241-5/+18
| | | | | | | | | | | | | On system with /dev/lop-control the udevd creates /dev/loopN nodes. It seems better to wait a moment after unsuccessful open(/dev/loopN) and try it to open again. The problem is pretty visible on systems where udevd also modifies permission for loopN devices, then open() fails with EACCES when losetup executed by non-root user (but user who is in "disk" group). Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1045432 Signed-off-by: Karel Zak <kzak@redhat.com>
* losetup: don't require 512-byte aligned offsetsKarel Zak2013-10-011-0/+6
| | | | | | | | | | | | | | | | Kernel aligns the device size, but the offset where the device starts is not required to be aligned. # losetup --offset 32 -f file.img is just fine, the final size of the look device will be (in sectors) (backing_file_size - offset) >> 9 so we have to do the same in userspace when we check for successful set capacity ioctl. Signed-off-by: Karel Zak <kzak@redhat.com>
* losetup: use loop-control to explicitly ask for deviceKarel Zak2013-08-061-0/+30
| | | | | | | | | | | | | | | | Now we use LOOP_CTL_GET_FREE ioctl to ask for free device, for example losetup -f foo.img Unfortunately, losetup(8) allows to ask for specified device losetup /dev/loop100 foo.img and in this case we assume that the device already exists in the system. This is incorrect, we should be able to use loop-control LOOP_CTL_ADD ioctl to ask for the specified device. Signed-off-by: Karel Zak <kzak@redhat.com>
* losetup: set errno for misaligned offsetsKarel Zak2013-07-191-7/+19
| | | | | References: https://bugs.archlinux.org/task/36189 Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev: assigned value is never read [clang-analyzer]Sami Kerola2013-07-091-1/+0Star
| | | | Signed-off-by: Sami Kerola <kerolasa@iki.fi>
* lib/loopdev: fix loopcxt_check_size() to work with blkdevsKarel Zak2013-05-131-1/+15
| | | | | | | | | | | | | | The loopcxt_check_size() is workaround for kernels < v3.9, kernel has been fixed by commit 541c742a7559eb65f0e36d3e2338c2ca532a3e61. The function sets loopdev size according to backing file size. The problem is that the backing file could be a block device where stat.st_size is zero, so we have to use blkdev_get_size() for block devices. Addresses: https://bugs.archlinux.org/task/35193 Reported-by: Dave Reisner <d@falconindy.com> Signed-off-by: Karel Zak <kzak@redhat.com>
* loopdev: sync capacity after setting itJeff Mahoney2013-04-091-3/+83
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I recently tried to mount an hfsplus file system from an image file with a partition table by using the loop offset and sizelimit options to specify the location of the file system. hfsplus stores some metadata at a set offset from the end of the partition, so it's sensitive to the device size reported by the kernel. It worked with this: But failed with this: /dev/loop0: [0089]:2 (<imagefile>), offset 32768, sizelimit 102400000 /dev/loop1: [0089]:2 (<imagefile>), offset 32768, sizelimit 102400000 /proc/partitions shows the correct number of blocks to match the sizelimit. But if I set a breakpoint in mount before the mount syscall, I could see: 102400000 102432768 The kernel loop driver will set the gendisk capacity of the device at LOOP_SET_STATUS64 but won't sync it to the block device until one of two conditions are met: All open file descriptors referring to the device are closed (and it will sync when re-opened) or if the LOOP_SET_CAPACITY ioctl is called to sync it. Since mount opens the device and passes it directly to the mount syscall after LOOP_SET_STATUS64 without closing and reopening it, the sizelimit argument is effectively ignroed. The capacity needs to be synced immediately for it to work as expected. This patch adds the LOOP_SET_CAPACITY call to loopctx_setup_device since the device isn't yet released to the user, so it's safe to sync the capacity immediately. [kzak@redhat.com: - port to the current git HEAD, - use uint64_t] Signed-off-by: Jeff Mahoney <jeffm@suse.com> Signed-off-by: Karel Zak <kzak@redhat.com>
* lib: use O_CLOEXEC in libcommonKarel Zak2013-04-031-5/+5
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>
* include: cleanup copyright headersKarel Zak2013-01-081-1/+4
| | | | | | | We use the code from include/ and lib/ on many places, so use public domain if possible or LGPL for code copied from libs. Signed-off-by: Karel Zak <kzak@redhat.com>
* lib/loopdev: fix loopdev_find_by_backing_file()Karel Zak2012-12-191-1/+1
| | | | Signed-off-by: Karel Zak <kzak@redhat.com>