util-linux/lib/loopdev.c

1867 lines
41 KiB
C
Raw Normal View History

/*
* No copyright is claimed. This code is in the public domain; do with
* it what you wish.
*
* Written by Karel Zak <kzak@redhat.com>
*
* -- based on mount/losetup.c
*
* Simple library for work with loop devices.
*
* - requires kernel 2.6.x
* - reads info from /sys/block/loop<N>/loop/<attr> (new kernels)
* - reads info by ioctl
* - supports *unlimited* number of loop devices
* - supports /dev/loop<N> as well as /dev/loop/<N>
* - minimize overhead (fd, loopinfo, ... are shared for all operations)
* - setup (associate device and backing file)
* - delete (dis-associate file)
* - old LOOP_{SET,GET}_STATUS (32bit) ioctls are unsupported
* - extendible
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <inttypes.h>
#include <dirent.h>
#include "linux_version.h"
#include "c.h"
#include "sysfs.h"
#include "pathnames.h"
#include "loopdev.h"
#include "canonicalize.h"
loopdev: sync capacity after setting it 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>
2013-04-09 14:32:50 +02:00
#include "blkdev.h"
#include "debug.h"
/*
* Debug stuff (based on include/debug.h)
*/
static UL_DEBUG_DEFINE_MASK(loopdev);
UL_DEBUG_DEFINE_MASKNAMES(loopdev) = UL_DEBUG_EMPTY_MASKNAMES;
#define LOOPDEV_DEBUG_INIT (1 << 1)
#define LOOPDEV_DEBUG_CXT (1 << 2)
#define LOOPDEV_DEBUG_ITER (1 << 3)
#define LOOPDEV_DEBUG_SETUP (1 << 4)
#define DBG(m, x) __UL_DBG(loopdev, LOOPDEV_DEBUG_, m, x)
#define ON_DBG(m, x) __UL_DBG_CALL(loopdev, LOOPDEV_DEBUG_, m, x)
#define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(loopdev)
#include "debugobj.h"
static void loopdev_init_debug(void)
{
if (loopdev_debug_mask)
return;
__UL_INIT_DEBUG_FROM_ENV(loopdev, LOOPDEV_DEBUG_, 0, LOOPDEV_DEBUG);
}
/*
* see loopcxt_init()
*/
#define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL))
#define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \
&& !loopcxt_ioctl_enabled(_lc)
/*
* @lc: context
* @device: device name, absolute device path or NULL to reset the current setting
*
* Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device
* names ("loop<N>") are converted to the path (/dev/loop<N> or to
* /dev/loop/<N>)
*
* This sets the device name, but does not check if the device exists!
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_set_device(struct loopdev_cxt *lc, const char *device)
{
if (!lc)
return -EINVAL;
if (lc->fd >= 0) {
close(lc->fd);
DBG(CXT, ul_debugobj(lc, "closing old open fd"));
}
lc->fd = -1;
lc->mode = 0;
lc->blocksize = 0;
lc->has_info = 0;
lc->info_failed = 0;
*lc->device = '\0';
memset(&lc->config, 0, sizeof(lc->config));
/* set new */
if (device) {
if (*device != '/') {
const char *dir = _PATH_DEV;
/* compose device name for /dev/loop<n> or /dev/loop/<n> */
if (lc->flags & LOOPDEV_FL_DEVSUBDIR) {
if (strlen(device) < 5)
return -1;
device += 4;
dir = _PATH_DEV_LOOP "/"; /* _PATH_DEV uses tailing slash */
}
snprintf(lc->device, sizeof(lc->device), "%s%s",
dir, device);
} else
xstrncpy(lc->device, device, sizeof(lc->device));
DBG(CXT, ul_debugobj(lc, "%s name assigned", device));
}
ul_unref_path(lc->sysfs);
lc->sysfs = NULL;
return 0;
}
int loopcxt_has_device(struct loopdev_cxt *lc)
{
return lc && *lc->device;
}
/*
* @lc: context
* @flags: LOOPDEV_FL_* flags
*
* Initialize loop handler.
*
* We have two sets of the flags:
*
* * LOOPDEV_FL_* flags control loopcxt_* API behavior
*
* * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls
*
* Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2)
* syscall to open loop device. By default is the device open read-only.
*
* The exception is loopcxt_setup_device(), where the device is open read-write
* if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()).
*
* Returns: <0 on error, 0 on success.
*/
int loopcxt_init(struct loopdev_cxt *lc, int flags)
{
int rc;
struct stat st;
struct loopdev_cxt dummy = UL_LOOPDEVCXT_EMPTY;
if (!lc)
return -EINVAL;
loopdev_init_debug();
DBG(CXT, ul_debugobj(lc, "initialize context"));
memcpy(lc, &dummy, sizeof(dummy));
lc->flags = flags;
rc = loopcxt_set_device(lc, NULL);
if (rc)
return rc;
if (stat(_PATH_SYS_BLOCK, &st) || !S_ISDIR(st.st_mode)) {
lc->flags |= LOOPDEV_FL_NOSYSFS;
lc->flags &= ~LOOPDEV_FL_NOIOCTL;
DBG(CXT, ul_debugobj(lc, "init: disable /sys usage"));
}
if (!(lc->flags & LOOPDEV_FL_NOSYSFS) &&
get_linux_version() >= KERNEL_VERSION(2,6,37)) {
/*
* Use only sysfs for basic information about loop devices
*/
lc->flags |= LOOPDEV_FL_NOIOCTL;
DBG(CXT, ul_debugobj(lc, "init: ignore ioctls"));
}
if (!(lc->flags & LOOPDEV_FL_CONTROL) && !stat(_PATH_DEV_LOOPCTL, &st)) {
lc->flags |= LOOPDEV_FL_CONTROL;
DBG(CXT, ul_debugobj(lc, "init: loop-control detected "));
}
return 0;
}
/*
* @lc: context
*
* Deinitialize loop context
*/
void loopcxt_deinit(struct loopdev_cxt *lc)
{
int errsv = errno;
if (!lc)
return;
DBG(CXT, ul_debugobj(lc, "de-initialize"));
free(lc->filename);
lc->filename = NULL;
ignore_result( loopcxt_set_device(lc, NULL) );
loopcxt_deinit_iterator(lc);
errno = errsv;
}
/*
* @lc: context
*
* Returns newly allocated device path.
*/
char *loopcxt_strdup_device(struct loopdev_cxt *lc)
{
if (!lc || !*lc->device)
return NULL;
return strdup(lc->device);
}
/*
* @lc: context
*
* Returns pointer device name in the @lc struct.
*/
const char *loopcxt_get_device(struct loopdev_cxt *lc)
{
return lc && *lc->device ? lc->device : NULL;
}
/*
* @lc: context
*
* Returns pointer to the sysfs context (see lib/sysfs.c)
*/
static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)
{
if (!lc || !*lc->device || (lc->flags & LOOPDEV_FL_NOSYSFS))
return NULL;
if (!lc->sysfs) {
dev_t devno = sysfs_devname_to_devno(lc->device);
if (!devno) {
DBG(CXT, ul_debugobj(lc, "sysfs: failed devname to devno"));
return NULL;
}
lc->sysfs = ul_new_sysfs_path(devno, NULL, NULL);
if (!lc->sysfs)
DBG(CXT, ul_debugobj(lc, "sysfs: init failed"));
}
return lc->sysfs;
}
/*
* @lc: context
*
* Returns: file descriptor to the open loop device or <0 on error. The mode
* depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is
* read-only.
*/
int loopcxt_get_fd(struct loopdev_cxt *lc)
{
if (!lc || !*lc->device)
return -EINVAL;
if (lc->fd < 0) {
lc->mode = lc->flags & LOOPDEV_FL_RDWR ? O_RDWR : O_RDONLY;
lc->fd = open(lc->device, lc->mode | O_CLOEXEC);
DBG(CXT, ul_debugobj(lc, "open %s [%s]: %m", lc->device,
lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro"));
}
return lc->fd;
}
int loopcxt_set_fd(struct loopdev_cxt *lc, int fd, int mode)
{
if (!lc)
return -EINVAL;
lc->fd = fd;
lc->mode = mode;
return 0;
}
/*
* @lc: context
* @flags: LOOPITER_FL_* flags
*
* Iterator can be used to scan list of the free or used loop devices.
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags)
{
struct loopdev_iter *iter;
struct stat st;
if (!lc)
return -EINVAL;
iter = &lc->iter;
DBG(ITER, ul_debugobj(iter, "initialize"));
/* always zeroize
*/
memset(iter, 0, sizeof(*iter));
iter->ncur = -1;
iter->flags = flags;
iter->default_check = 1;
if (!lc->extra_check) {
/*
* Check for /dev/loop/<N> subdirectory
*/
if (!(lc->flags & LOOPDEV_FL_DEVSUBDIR) &&
stat(_PATH_DEV_LOOP, &st) == 0 && S_ISDIR(st.st_mode))
lc->flags |= LOOPDEV_FL_DEVSUBDIR;
lc->extra_check = 1;
}
return 0;
}
/*
* @lc: context
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_deinit_iterator(struct loopdev_cxt *lc)
{
struct loopdev_iter *iter;
if (!lc)
return -EINVAL;
iter = &lc->iter;
DBG(ITER, ul_debugobj(iter, "de-initialize"));
free(iter->minors);
if (iter->proc)
fclose(iter->proc);
if (iter->sysblock)
closedir(iter->sysblock);
memset(iter, 0, sizeof(*iter));
return 0;
}
/*
* Same as loopcxt_set_device, but also checks if the device is
* associated with any file.
*
* Returns: <0 on error, 0 on success, 1 device does not match with
* LOOPITER_FL_{USED,FREE} flags.
*/
static int loopiter_set_device(struct loopdev_cxt *lc, const char *device)
{
int rc = loopcxt_set_device(lc, device);
int used;
if (rc)
return rc;
if (!(lc->iter.flags & LOOPITER_FL_USED) &&
!(lc->iter.flags & LOOPITER_FL_FREE))
return 0; /* caller does not care about device status */
if (!is_loopdev(lc->device)) {
DBG(ITER, ul_debugobj(&lc->iter, "%s does not exist", lc->device));
return -errno;
}
DBG(ITER, ul_debugobj(&lc->iter, "%s exist", lc->device));
used = loopcxt_get_offset(lc, NULL) == 0;
if ((lc->iter.flags & LOOPITER_FL_USED) && used)
return 0;
if ((lc->iter.flags & LOOPITER_FL_FREE) && !used)
return 0;
DBG(ITER, ul_debugobj(&lc->iter, "failed to use %s device", lc->device));
ignore_result( loopcxt_set_device(lc, NULL) );
return 1;
}
static int cmpnum(const void *p1, const void *p2)
{
return (((* (const int *) p1) > (* (const int *) p2)) -
((* (const int *) p1) < (* (const int *) p2)));
}
/*
* The classic scandir() is more expensive and less portable.
* We needn't full loop device names -- loop numbers (loop<N>)
* are enough.
*/
static int loop_scandir(const char *dirname, int **ary, int hasprefix)
{
DIR *dir;
struct dirent *d;
unsigned int n, count = 0, arylen = 0;
if (!dirname || !ary)
return 0;
DBG(ITER, ul_debug("scan dir: %s", dirname));
dir = opendir(dirname);
if (!dir)
return 0;
free(*ary);
*ary = NULL;
while((d = readdir(dir))) {
#ifdef _DIRENT_HAVE_D_TYPE
if (d->d_type != DT_BLK && d->d_type != DT_UNKNOWN &&
d->d_type != DT_LNK)
continue;
#endif
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
if (hasprefix) {
/* /dev/loop<N> */
if (sscanf(d->d_name, "loop%u", &n) != 1)
continue;
} else {
/* /dev/loop/<N> */
char *end = NULL;
errno = 0;
n = strtol(d->d_name, &end, 10);
if (d->d_name == end || (end && *end) || errno)
continue;
}
if (n < LOOPDEV_DEFAULT_NNODES)
continue; /* ignore loop<0..7> */
if (count + 1 > arylen) {
int *tmp;
arylen += 1;
tmp = realloc(*ary, arylen * sizeof(int));
if (!tmp) {
free(*ary);
*ary = NULL;
closedir(dir);
return -1;
}
*ary = tmp;
}
if (*ary)
(*ary)[count++] = n;
}
if (count && *ary)
qsort(*ary, count, sizeof(int), cmpnum);
closedir(dir);
return count;
}
/*
* Set the next *used* loop device according to /proc/partitions.
*
* Loop devices smaller than 512 bytes are invisible for this function.
*/
static int loopcxt_next_from_proc(struct loopdev_cxt *lc)
{
struct loopdev_iter *iter = &lc->iter;
char buf[BUFSIZ];
DBG(ITER, ul_debugobj(iter, "scan /proc/partitions"));
if (!iter->proc)
iter->proc = fopen(_PATH_PROC_PARTITIONS, "r" UL_CLOEXECSTR);
if (!iter->proc)
return 1;
while (fgets(buf, sizeof(buf), iter->proc)) {
unsigned int m;
char name[128 + 1];
if (sscanf(buf, " %u %*s %*s %128[^\n ]",
&m, name) != 2 || m != LOOPDEV_MAJOR)
continue;
DBG(ITER, ul_debugobj(iter, "checking %s", name));
if (loopiter_set_device(lc, name) == 0)
return 0;
}
return 1;
}
/*
* Set the next *used* loop device according to
* /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required).
*
* This is preferred method.
*/
static int loopcxt_next_from_sysfs(struct loopdev_cxt *lc)
{
struct loopdev_iter *iter = &lc->iter;
struct dirent *d;
int fd;
DBG(ITER, ul_debugobj(iter, "scanning /sys/block"));
if (!iter->sysblock)
iter->sysblock = opendir(_PATH_SYS_BLOCK);
if (!iter->sysblock)
return 1;
fd = dirfd(iter->sysblock);
while ((d = readdir(iter->sysblock))) {
misc: fix gcc-7 snprintf warnings -Wformat-truncation ../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>
2017-06-11 23:18:21 +02:00
char name[NAME_MAX + 18 + 1];
struct stat st;
DBG(ITER, ul_debugobj(iter, "check %s", d->d_name));
if (strcmp(d->d_name, ".") == 0
|| strcmp(d->d_name, "..") == 0
|| strncmp(d->d_name, "loop", 4) != 0)
continue;
snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name);
if (fstatat(fd, name, &st, 0) != 0)
continue;
if (loopiter_set_device(lc, d->d_name) == 0)
return 0;
}
return 1;
}
/*
* @lc: context, has to initialized by loopcxt_init_iterator()
*
* Returns: 0 on success, -1 on error, 1 at the end of scanning. The details
* about the current loop device are available by
* loopcxt_get_{fd,backing_file,device,offset, ...} functions.
*/
int loopcxt_next(struct loopdev_cxt *lc)
{
struct loopdev_iter *iter;
if (!lc)
return -EINVAL;
iter = &lc->iter;
if (iter->done)
return 1;
DBG(ITER, ul_debugobj(iter, "next"));
/* A) Look for used loop devices in /proc/partitions ("losetup -a" only)
*/
if (iter->flags & LOOPITER_FL_USED) {
int rc;
if (loopcxt_sysfs_available(lc))
rc = loopcxt_next_from_sysfs(lc);
else
rc = loopcxt_next_from_proc(lc);
if (rc == 0)
return 0;
goto done;
}
/* B) Classic way, try first eight loop devices (default number
* of loop devices). This is enough for 99% of all cases.
*/
if (iter->default_check) {
DBG(ITER, ul_debugobj(iter, "next: default check"));
for (++iter->ncur; iter->ncur < LOOPDEV_DEFAULT_NNODES;
iter->ncur++) {
char name[16];
snprintf(name, sizeof(name), "loop%d", iter->ncur);
if (loopiter_set_device(lc, name) == 0)
return 0;
}
iter->default_check = 0;
}
/* C) the worst possibility, scan whole /dev or /dev/loop/<N>
*/
if (!iter->minors) {
DBG(ITER, ul_debugobj(iter, "next: scanning /dev"));
iter->nminors = (lc->flags & LOOPDEV_FL_DEVSUBDIR) ?
loop_scandir(_PATH_DEV_LOOP, &iter->minors, 0) :
loop_scandir(_PATH_DEV, &iter->minors, 1);
iter->ncur = -1;
}
for (++iter->ncur; iter->ncur < iter->nminors; iter->ncur++) {
char name[16];
snprintf(name, sizeof(name), "loop%d", iter->minors[iter->ncur]);
if (loopiter_set_device(lc, name) == 0)
return 0;
}
done:
loopcxt_deinit_iterator(lc);
return 1;
}
/*
* @device: path to device
*/
int is_loopdev(const char *device)
{
struct stat st;
if (device && stat(device, &st) == 0 &&
S_ISBLK(st.st_mode) &&
major(st.st_rdev) == LOOPDEV_MAJOR)
return 1;
errno = ENODEV;
return 0;
}
/*
* @lc: context
*
* Returns result from LOOP_GET_STAT64 ioctl or NULL on error.
*/
struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc)
{
int fd;
if (!lc || lc->info_failed) {
errno = EINVAL;
return NULL;
}
errno = 0;
if (lc->has_info)
return &lc->config.info;
fd = loopcxt_get_fd(lc);
if (fd < 0)
return NULL;
if (ioctl(fd, LOOP_GET_STATUS64, &lc->config.info) == 0) {
lc->has_info = 1;
lc->info_failed = 0;
DBG(CXT, ul_debugobj(lc, "reading loop_info64 OK"));
return &lc->config.info;
}
lc->info_failed = 1;
DBG(CXT, ul_debugobj(lc, "reading loop_info64 FAILED"));
return NULL;
}
/*
* @lc: context
*
2020-07-09 19:14:32 +02:00
* Returns (allocated) string with path to the file associated
* with the current loop device.
*/
char *loopcxt_get_backing_file(struct loopdev_cxt *lc)
{
struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
char *res = NULL;
if (sysfs)
/*
* This is always preferred, the loop_info64
* has too small buffer for the filename.
*/
ul_path_read_string(sysfs, &res, "loop/backing_file");
if (!res && loopcxt_ioctl_enabled(lc)) {
struct loop_info64 *lo = loopcxt_get_info(lc);
if (lo) {
lo->lo_file_name[LO_NAME_SIZE - 2] = '*';
lo->lo_file_name[LO_NAME_SIZE - 1] = '\0';
res = strdup((char *) lo->lo_file_name);
}
}
DBG(CXT, ul_debugobj(lc, "get_backing_file [%s]", res));
return res;
}
/*
* @lc: context
* @offset: returns offset number for the given device
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset)
{
struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
int rc = -EINVAL;
if (sysfs)
rc = ul_path_read_u64(sysfs, offset, "loop/offset");
if (rc && loopcxt_ioctl_enabled(lc)) {
struct loop_info64 *lo = loopcxt_get_info(lc);
if (lo) {
if (offset)
*offset = lo->lo_offset;
rc = 0;
} else
rc = -errno;
}
DBG(CXT, ul_debugobj(lc, "get_offset [rc=%d]", rc));
return rc;
}
/*
* @lc: context
* @blocksize: returns logical blocksize for the given device
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_get_blocksize(struct loopdev_cxt *lc, uint64_t *blocksize)
{
struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
int rc = -EINVAL;
if (sysfs)
rc = ul_path_read_u64(sysfs, blocksize, "queue/logical_block_size");
/* Fallback based on BLKSSZGET ioctl */
if (rc) {
int fd = loopcxt_get_fd(lc);
int sz = 0;
if (fd < 0)
return -EINVAL;
rc = blkdev_get_sector_size(fd, &sz);
if (rc)
return rc;
*blocksize = sz;
}
DBG(CXT, ul_debugobj(lc, "get_blocksize [rc=%d]", rc));
return rc;
}
/*
* @lc: context
* @sizelimit: returns size limit for the given device
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size)
{
struct path_cxt *sysfs = loopcxt_get_sysfs(lc);
int rc = -EINVAL;
if (sysfs)
rc = ul_path_read_u64(sysfs, size, "loop/sizelimit");
if (rc && loopcxt_ioctl_enabled(lc)) {
struct loop_info64 *lo = loopcxt_get_info(lc);
if (lo) {
if (size)
*size = lo->lo_sizelimit;
rc = 0;
} else
rc = -errno;
}
DBG(CXT, ul_debugobj(lc, "get_sizelimit [rc=%d]", rc));
return rc;
}
/*
* @lc: context
* @devno: returns encryption type
*
* Cryptoloop is DEPRECATED!
*
* Returns: <0 on error, 0 on success
*/
int loopcxt_get_encrypt_type(struct loopdev_cxt *lc, uint32_t *type)
{
struct loop_info64 *lo = loopcxt_get_info(lc);
int rc;
/* not provided by sysfs */