bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * No copyright is claimed. This code is in the public domain; do with |
| 3 | * it what you wish. |
| 4 | * |
| 5 | * Written by Karel Zak <kzak@redhat.com> |
| 6 | * |
| 7 | * -- based on mount/losetup.c |
| 8 | * |
| 9 | * Simple library for work with loop devices. |
| 10 | * |
| 11 | * - requires kernel 2.6.x |
| 12 | * - reads info from /sys/block/loop<N>/loop/<attr> (new kernels) |
| 13 | * - reads info by ioctl |
| 14 | * - supports *unlimited* number of loop devices |
| 15 | * - supports /dev/loop<N> as well as /dev/loop/<N> |
| 16 | * - minimize overhead (fd, loopinfo, ... are shared for all operations) |
| 17 | * - setup (associate device and backing file) |
| 18 | * - delete (dis-associate file) |
| 19 | * - old LOOP_{SET,GET}_STATUS (32bit) ioctls are unsupported |
| 20 | * - extendible |
| 21 | */ |
| 22 | #include <stdio.h> |
| 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include <ctype.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <unistd.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/mman.h> |
| 32 | #include <sys/sysmacros.h> |
| 33 | #include <inttypes.h> |
| 34 | #include <dirent.h> |
| 35 | #include <linux/posix_types.h> |
| 36 | |
| 37 | #include "linux_version.h" |
| 38 | #include "c.h" |
| 39 | #include "sysfs.h" |
| 40 | #include "pathnames.h" |
| 41 | #include "loopdev.h" |
| 42 | #include "canonicalize.h" |
| 43 | #include "at.h" |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 44 | #include "blkdev.h" |
| 45 | #include "debug.h" |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 46 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 47 | /* |
| 48 | * Debug stuff (based on include/debug.h) |
| 49 | */ |
| 50 | UL_DEBUG_DEFINE_MASK(loopdev); |
| 51 | UL_DEBUG_DEFINE_MASKNAMES(loopdev) = UL_DEBUG_EMPTY_MASKNAMES; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 52 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 53 | #define LOOPDEV_DEBUG_INIT (1 << 1) |
| 54 | #define LOOPDEV_DEBUG_CXT (1 << 2) |
| 55 | #define LOOPDEV_DEBUG_ITER (1 << 3) |
| 56 | #define LOOPDEV_DEBUG_SETUP (1 << 4) |
| 57 | #define SFDISKPROG_DEBUG_ALL 0xFFFF |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 58 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 59 | #define DBG(m, x) __UL_DBG(loopdev, LOOPDEV_DEBUG_, m, x) |
| 60 | #define ON_DBG(m, x) __UL_DBG_CALL(loopdev, LOOPDEV_DEBUG_, m, x) |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 61 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 62 | static void loopdev_init_debug(void) |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 63 | { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 64 | if (loopdev_debug_mask) |
| 65 | return; |
| 66 | __UL_INIT_DEBUG(loopdev, LOOPDEV_DEBUG_, 0, LOOPDEV_DEBUG); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 67 | } |
| 68 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 69 | /* |
| 70 | * see loopcxt_init() |
| 71 | */ |
| 72 | #define loopcxt_ioctl_enabled(_lc) (!((_lc)->flags & LOOPDEV_FL_NOIOCTL)) |
| 73 | #define loopcxt_sysfs_available(_lc) (!((_lc)->flags & LOOPDEV_FL_NOSYSFS)) \ |
| 74 | && !loopcxt_ioctl_enabled(_lc) |
| 75 | |
| 76 | /* |
| 77 | * @lc: context |
| 78 | * @device: device name, absolute device path or NULL to reset the current setting |
| 79 | * |
| 80 | * Sets device, absolute paths (e.g. "/dev/loop<N>") are unchanged, device |
| 81 | * names ("loop<N>") are converted to the path (/dev/loop<N> or to |
| 82 | * /dev/loop/<N>) |
| 83 | * |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 84 | * This sets the device name, but does not check if the device exists! |
| 85 | * |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 86 | * Returns: <0 on error, 0 on success |
| 87 | */ |
| 88 | int loopcxt_set_device(struct loopdev_cxt *lc, const char *device) |
| 89 | { |
| 90 | if (!lc) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | if (lc->fd >= 0) { |
| 94 | close(lc->fd); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 95 | DBG(CXT, ul_debugobj(lc, "closing old open fd")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 96 | } |
| 97 | lc->fd = -1; |
| 98 | lc->mode = 0; |
| 99 | lc->has_info = 0; |
| 100 | lc->info_failed = 0; |
| 101 | *lc->device = '\0'; |
| 102 | memset(&lc->info, 0, sizeof(lc->info)); |
| 103 | |
| 104 | /* set new */ |
| 105 | if (device) { |
| 106 | if (*device != '/') { |
| 107 | const char *dir = _PATH_DEV; |
| 108 | |
| 109 | /* compose device name for /dev/loop<n> or /dev/loop/<n> */ |
| 110 | if (lc->flags & LOOPDEV_FL_DEVSUBDIR) { |
| 111 | if (strlen(device) < 5) |
| 112 | return -1; |
| 113 | device += 4; |
| 114 | dir = _PATH_DEV_LOOP "/"; /* _PATH_DEV uses tailing slash */ |
| 115 | } |
| 116 | snprintf(lc->device, sizeof(lc->device), "%s%s", |
| 117 | dir, device); |
| 118 | } else { |
| 119 | strncpy(lc->device, device, sizeof(lc->device)); |
| 120 | lc->device[sizeof(lc->device) - 1] = '\0'; |
| 121 | } |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 122 | DBG(CXT, ul_debugobj(lc, "%s name assigned", device)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | sysfs_deinit(&lc->sysfs); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | int loopcxt_has_device(struct loopdev_cxt *lc) |
| 130 | { |
| 131 | return lc && *lc->device; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * @lc: context |
| 136 | * @flags: LOOPDEV_FL_* flags |
| 137 | * |
| 138 | * Initilize loop handler. |
| 139 | * |
| 140 | * We have two sets of the flags: |
| 141 | * |
| 142 | * * LOOPDEV_FL_* flags control loopcxt_* API behavior |
| 143 | * |
| 144 | * * LO_FLAGS_* are kernel flags used for LOOP_{SET,GET}_STAT64 ioctls |
| 145 | * |
| 146 | * Note about LOOPDEV_FL_{RDONLY,RDWR} flags. These flags are used for open(2) |
| 147 | * syscall to open loop device. By default is the device open read-only. |
| 148 | * |
| 149 | * The expection is loopcxt_setup_device(), where the device is open read-write |
| 150 | * if LO_FLAGS_READ_ONLY flags is not set (see loopcxt_set_flags()). |
| 151 | * |
| 152 | * Returns: <0 on error, 0 on success. |
| 153 | */ |
| 154 | int loopcxt_init(struct loopdev_cxt *lc, int flags) |
| 155 | { |
| 156 | int rc; |
| 157 | struct stat st; |
| 158 | struct loopdev_cxt dummy = UL_LOOPDEVCXT_EMPTY; |
| 159 | |
| 160 | if (!lc) |
| 161 | return -EINVAL; |
| 162 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 163 | loopdev_init_debug(); |
| 164 | DBG(CXT, ul_debugobj(lc, "initialize context")); |
| 165 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 166 | memcpy(lc, &dummy, sizeof(dummy)); |
| 167 | lc->flags = flags; |
| 168 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 169 | rc = loopcxt_set_device(lc, NULL); |
| 170 | if (rc) |
| 171 | return rc; |
| 172 | |
| 173 | if (stat(_PATH_SYS_BLOCK, &st) || !S_ISDIR(st.st_mode)) { |
| 174 | lc->flags |= LOOPDEV_FL_NOSYSFS; |
| 175 | lc->flags &= ~LOOPDEV_FL_NOIOCTL; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 176 | DBG(CXT, ul_debugobj(lc, "init: disable /sys usage")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if (!(lc->flags & LOOPDEV_FL_NOSYSFS) && |
| 180 | get_linux_version() >= KERNEL_VERSION(2,6,37)) { |
| 181 | /* |
| 182 | * Use only sysfs for basic information about loop devices |
| 183 | */ |
| 184 | lc->flags |= LOOPDEV_FL_NOIOCTL; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 185 | DBG(CXT, ul_debugobj(lc, "init: ignore ioctls")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | if (!(lc->flags & LOOPDEV_FL_CONTROL) && !stat(_PATH_DEV_LOOPCTL, &st)) { |
| 189 | lc->flags |= LOOPDEV_FL_CONTROL; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 190 | DBG(CXT, ul_debugobj(lc, "init: loop-control detected ")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * @lc: context |
| 198 | * |
| 199 | * Deinitialize loop context |
| 200 | */ |
| 201 | void loopcxt_deinit(struct loopdev_cxt *lc) |
| 202 | { |
| 203 | int errsv = errno; |
| 204 | |
| 205 | if (!lc) |
| 206 | return; |
| 207 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 208 | DBG(CXT, ul_debugobj(lc, "de-initialize")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 209 | |
| 210 | free(lc->filename); |
| 211 | lc->filename = NULL; |
| 212 | |
| 213 | ignore_result( loopcxt_set_device(lc, NULL) ); |
| 214 | loopcxt_deinit_iterator(lc); |
| 215 | |
| 216 | errno = errsv; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * @lc: context |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 221 | * |
| 222 | * Returns newly allocated device path. |
| 223 | */ |
| 224 | char *loopcxt_strdup_device(struct loopdev_cxt *lc) |
| 225 | { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 226 | if (!lc || !*lc->device) |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 227 | return NULL; |
| 228 | return strdup(lc->device); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * @lc: context |
| 233 | * |
| 234 | * Returns pointer device name in the @lc struct. |
| 235 | */ |
| 236 | const char *loopcxt_get_device(struct loopdev_cxt *lc) |
| 237 | { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 238 | return lc && *lc->device ? lc->device : NULL; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* |
| 242 | * @lc: context |
| 243 | * |
| 244 | * Returns pointer to the sysfs context (see lib/sysfs.c) |
| 245 | */ |
| 246 | struct sysfs_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) |
| 247 | { |
| 248 | if (!lc || !*lc->device || (lc->flags & LOOPDEV_FL_NOSYSFS)) |
| 249 | return NULL; |
| 250 | |
| 251 | if (!lc->sysfs.devno) { |
| 252 | dev_t devno = sysfs_devname_to_devno(lc->device, NULL); |
| 253 | if (!devno) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 254 | DBG(CXT, ul_debugobj(lc, "sysfs: failed devname to devno")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 255 | return NULL; |
| 256 | } |
| 257 | if (sysfs_init(&lc->sysfs, devno, NULL)) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 258 | DBG(CXT, ul_debugobj(lc, "sysfs: init failed")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 259 | return NULL; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | return &lc->sysfs; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * @lc: context |
| 268 | * |
| 269 | * Returns: file descriptor to the open loop device or <0 on error. The mode |
| 270 | * depends on LOOPDEV_FL_{RDWR,RDONLY} context flags. Default is |
| 271 | * read-only. |
| 272 | */ |
| 273 | int loopcxt_get_fd(struct loopdev_cxt *lc) |
| 274 | { |
| 275 | if (!lc || !*lc->device) |
| 276 | return -EINVAL; |
| 277 | |
| 278 | if (lc->fd < 0) { |
| 279 | lc->mode = lc->flags & LOOPDEV_FL_RDWR ? O_RDWR : O_RDONLY; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 280 | lc->fd = open(lc->device, lc->mode | O_CLOEXEC); |
| 281 | DBG(CXT, ul_debugobj(lc, "open %s [%s]: %m", lc->device, |
| 282 | lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 283 | } |
| 284 | return lc->fd; |
| 285 | } |
| 286 | |
| 287 | int loopcxt_set_fd(struct loopdev_cxt *lc, int fd, int mode) |
| 288 | { |
| 289 | if (!lc) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | lc->fd = fd; |
| 293 | lc->mode = mode; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * @lc: context |
| 299 | * @flags: LOOPITER_FL_* flags |
| 300 | * |
| 301 | * Iterator allows to scan list of the free or used loop devices. |
| 302 | * |
| 303 | * Returns: <0 on error, 0 on success |
| 304 | */ |
| 305 | int loopcxt_init_iterator(struct loopdev_cxt *lc, int flags) |
| 306 | { |
| 307 | struct loopdev_iter *iter; |
| 308 | struct stat st; |
| 309 | |
| 310 | if (!lc) |
| 311 | return -EINVAL; |
| 312 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 313 | |
| 314 | iter = &lc->iter; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 315 | DBG(ITER, ul_debugobj(iter, "initialize")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 316 | |
| 317 | /* always zeroize |
| 318 | */ |
| 319 | memset(iter, 0, sizeof(*iter)); |
| 320 | iter->ncur = -1; |
| 321 | iter->flags = flags; |
| 322 | iter->default_check = 1; |
| 323 | |
| 324 | if (!lc->extra_check) { |
| 325 | /* |
| 326 | * Check for /dev/loop/<N> subdirectory |
| 327 | */ |
| 328 | if (!(lc->flags & LOOPDEV_FL_DEVSUBDIR) && |
| 329 | stat(_PATH_DEV_LOOP, &st) == 0 && S_ISDIR(st.st_mode)) |
| 330 | lc->flags |= LOOPDEV_FL_DEVSUBDIR; |
| 331 | |
| 332 | lc->extra_check = 1; |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * @lc: context |
| 339 | * |
| 340 | * Returns: <0 on error, 0 on success |
| 341 | */ |
| 342 | int loopcxt_deinit_iterator(struct loopdev_cxt *lc) |
| 343 | { |
| 344 | struct loopdev_iter *iter; |
| 345 | |
| 346 | if (!lc) |
| 347 | return -EINVAL; |
| 348 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 349 | iter = &lc->iter; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 350 | DBG(ITER, ul_debugobj(iter, "de-initialize")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 351 | |
| 352 | free(iter->minors); |
| 353 | if (iter->proc) |
| 354 | fclose(iter->proc); |
| 355 | if (iter->sysblock) |
| 356 | closedir(iter->sysblock); |
| 357 | iter->minors = NULL; |
| 358 | iter->proc = NULL; |
| 359 | iter->sysblock = NULL; |
| 360 | iter->done = 1; |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Same as loopcxt_set_device, but also checks if the device is |
| 366 | * associeted with any file. |
| 367 | * |
| 368 | * Returns: <0 on error, 0 on success, 1 device does not match with |
| 369 | * LOOPITER_FL_{USED,FREE} flags. |
| 370 | */ |
| 371 | static int loopiter_set_device(struct loopdev_cxt *lc, const char *device) |
| 372 | { |
| 373 | int rc = loopcxt_set_device(lc, device); |
| 374 | int used; |
| 375 | |
| 376 | if (rc) |
| 377 | return rc; |
| 378 | |
| 379 | if (!(lc->iter.flags & LOOPITER_FL_USED) && |
| 380 | !(lc->iter.flags & LOOPITER_FL_FREE)) |
| 381 | return 0; /* caller does not care about device status */ |
| 382 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 383 | if (!is_loopdev(lc->device)) { |
| 384 | DBG(ITER, ul_debugobj(&lc->iter, "%s does not exist", lc->device)); |
| 385 | return -errno; |
| 386 | } |
| 387 | |
| 388 | DBG(ITER, ul_debugobj(&lc->iter, "%s exist", lc->device)); |
| 389 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 390 | used = loopcxt_get_offset(lc, NULL) == 0; |
| 391 | |
| 392 | if ((lc->iter.flags & LOOPITER_FL_USED) && used) |
| 393 | return 0; |
| 394 | |
| 395 | if ((lc->iter.flags & LOOPITER_FL_FREE) && !used) |
| 396 | return 0; |
| 397 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 398 | DBG(ITER, ul_debugobj(&lc->iter, "failed to use %s device", lc->device)); |
| 399 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 400 | ignore_result( loopcxt_set_device(lc, NULL) ); |
| 401 | return 1; |
| 402 | } |
| 403 | |
| 404 | static int cmpnum(const void *p1, const void *p2) |
| 405 | { |
| 406 | return (((* (int *) p1) > (* (int *) p2)) - |
| 407 | ((* (int *) p1) < (* (int *) p2))); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * The classic scandir() is more expensive and less portable. |
| 412 | * We needn't full loop device names -- loop numbers (loop<N>) |
| 413 | * are enough. |
| 414 | */ |
| 415 | static int loop_scandir(const char *dirname, int **ary, int hasprefix) |
| 416 | { |
| 417 | DIR *dir; |
| 418 | struct dirent *d; |
| 419 | unsigned int n, count = 0, arylen = 0; |
| 420 | |
| 421 | if (!dirname || !ary) |
| 422 | return 0; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 423 | |
| 424 | DBG(ITER, ul_debug("scan dir: %s", dirname)); |
| 425 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 426 | dir = opendir(dirname); |
| 427 | if (!dir) |
| 428 | return 0; |
| 429 | free(*ary); |
| 430 | *ary = NULL; |
| 431 | |
| 432 | while((d = readdir(dir))) { |
| 433 | #ifdef _DIRENT_HAVE_D_TYPE |
| 434 | if (d->d_type != DT_BLK && d->d_type != DT_UNKNOWN && |
| 435 | d->d_type != DT_LNK) |
| 436 | continue; |
| 437 | #endif |
| 438 | if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) |
| 439 | continue; |
| 440 | |
| 441 | if (hasprefix) { |
| 442 | /* /dev/loop<N> */ |
| 443 | if (sscanf(d->d_name, "loop%u", &n) != 1) |
| 444 | continue; |
| 445 | } else { |
| 446 | /* /dev/loop/<N> */ |
| 447 | char *end = NULL; |
| 448 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 449 | errno = 0; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 450 | n = strtol(d->d_name, &end, 10); |
| 451 | if (d->d_name == end || (end && *end) || errno) |
| 452 | continue; |
| 453 | } |
| 454 | if (n < LOOPDEV_DEFAULT_NNODES) |
| 455 | continue; /* ignore loop<0..7> */ |
| 456 | |
| 457 | if (count + 1 > arylen) { |
| 458 | int *tmp; |
| 459 | |
| 460 | arylen += 1; |
| 461 | |
| 462 | tmp = realloc(*ary, arylen * sizeof(int)); |
| 463 | if (!tmp) { |
| 464 | free(*ary); |
| 465 | closedir(dir); |
| 466 | return -1; |
| 467 | } |
| 468 | *ary = tmp; |
| 469 | } |
| 470 | if (*ary) |
| 471 | (*ary)[count++] = n; |
| 472 | } |
| 473 | if (count && *ary) |
| 474 | qsort(*ary, count, sizeof(int), cmpnum); |
| 475 | |
| 476 | closedir(dir); |
| 477 | return count; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Set the next *used* loop device according to /proc/partitions. |
| 482 | * |
| 483 | * Loop devices smaller than 512 bytes are invisible for this function. |
| 484 | */ |
| 485 | static int loopcxt_next_from_proc(struct loopdev_cxt *lc) |
| 486 | { |
| 487 | struct loopdev_iter *iter = &lc->iter; |
| 488 | char buf[BUFSIZ]; |
| 489 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 490 | DBG(ITER, ul_debugobj(iter, "scan /proc/partitions")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 491 | |
| 492 | if (!iter->proc) |
| 493 | iter->proc = fopen(_PATH_PROC_PARTITIONS, "r"); |
| 494 | if (!iter->proc) |
| 495 | return 1; |
| 496 | |
| 497 | while (fgets(buf, sizeof(buf), iter->proc)) { |
| 498 | unsigned int m; |
| 499 | char name[128 + 1]; |
| 500 | |
| 501 | |
| 502 | if (sscanf(buf, " %u %*s %*s %128[^\n ]", |
| 503 | &m, name) != 2 || m != LOOPDEV_MAJOR) |
| 504 | continue; |
| 505 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 506 | DBG(ITER, ul_debugobj(iter, "checking %s", name)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 507 | |
| 508 | if (loopiter_set_device(lc, name) == 0) |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | return 1; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * Set the next *used* loop device according to |
| 517 | * /sys/block/loopN/loop/backing_file (kernel >= 2.6.37 is required). |
| 518 | * |
| 519 | * This is preferred method. |
| 520 | */ |
| 521 | static int loopcxt_next_from_sysfs(struct loopdev_cxt *lc) |
| 522 | { |
| 523 | struct loopdev_iter *iter = &lc->iter; |
| 524 | struct dirent *d; |
| 525 | int fd; |
| 526 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 527 | DBG(ITER, ul_debugobj(iter, "scanning /sys/block")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 528 | |
| 529 | if (!iter->sysblock) |
| 530 | iter->sysblock = opendir(_PATH_SYS_BLOCK); |
| 531 | |
| 532 | if (!iter->sysblock) |
| 533 | return 1; |
| 534 | |
| 535 | fd = dirfd(iter->sysblock); |
| 536 | |
| 537 | while ((d = readdir(iter->sysblock))) { |
| 538 | char name[256]; |
| 539 | struct stat st; |
| 540 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 541 | DBG(ITER, ul_debugobj(iter, "check %s", d->d_name)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 542 | |
| 543 | if (strcmp(d->d_name, ".") == 0 |
| 544 | || strcmp(d->d_name, "..") == 0 |
| 545 | || strncmp(d->d_name, "loop", 4) != 0) |
| 546 | continue; |
| 547 | |
| 548 | snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name); |
| 549 | if (fstat_at(fd, _PATH_SYS_BLOCK, name, &st, 0) != 0) |
| 550 | continue; |
| 551 | |
| 552 | if (loopiter_set_device(lc, d->d_name) == 0) |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | return 1; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * @lc: context, has to initialized by loopcxt_init_iterator() |
| 561 | * |
| 562 | * Returns: 0 on success, -1 on error, 1 at the end of scanning. The details |
| 563 | * about the current loop device are available by |
| 564 | * loopcxt_get_{fd,backing_file,device,offset, ...} functions. |
| 565 | */ |
| 566 | int loopcxt_next(struct loopdev_cxt *lc) |
| 567 | { |
| 568 | struct loopdev_iter *iter; |
| 569 | |
| 570 | if (!lc) |
| 571 | return -EINVAL; |
| 572 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 573 | |
| 574 | iter = &lc->iter; |
| 575 | if (iter->done) |
| 576 | return 1; |
| 577 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 578 | DBG(ITER, ul_debugobj(iter, "next")); |
| 579 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 580 | /* A) Look for used loop devices in /proc/partitions ("losetup -a" only) |
| 581 | */ |
| 582 | if (iter->flags & LOOPITER_FL_USED) { |
| 583 | int rc; |
| 584 | |
| 585 | if (loopcxt_sysfs_available(lc)) |
| 586 | rc = loopcxt_next_from_sysfs(lc); |
| 587 | else |
| 588 | rc = loopcxt_next_from_proc(lc); |
| 589 | if (rc == 0) |
| 590 | return 0; |
| 591 | goto done; |
| 592 | } |
| 593 | |
| 594 | /* B) Classic way, try first eight loop devices (default number |
| 595 | * of loop devices). This is enough for 99% of all cases. |
| 596 | */ |
| 597 | if (iter->default_check) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 598 | DBG(ITER, ul_debugobj(iter, "next: default check")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 599 | for (++iter->ncur; iter->ncur < LOOPDEV_DEFAULT_NNODES; |
| 600 | iter->ncur++) { |
| 601 | char name[16]; |
| 602 | snprintf(name, sizeof(name), "loop%d", iter->ncur); |
| 603 | |
| 604 | if (loopiter_set_device(lc, name) == 0) |
| 605 | return 0; |
| 606 | } |
| 607 | iter->default_check = 0; |
| 608 | } |
| 609 | |
| 610 | /* C) the worst possibility, scan whole /dev or /dev/loop/<N> |
| 611 | */ |
| 612 | if (!iter->minors) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 613 | DBG(ITER, ul_debugobj(iter, "next: scanning /dev")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 614 | iter->nminors = (lc->flags & LOOPDEV_FL_DEVSUBDIR) ? |
| 615 | loop_scandir(_PATH_DEV_LOOP, &iter->minors, 0) : |
| 616 | loop_scandir(_PATH_DEV, &iter->minors, 1); |
| 617 | iter->ncur = -1; |
| 618 | } |
| 619 | for (++iter->ncur; iter->ncur < iter->nminors; iter->ncur++) { |
| 620 | char name[16]; |
| 621 | snprintf(name, sizeof(name), "loop%d", iter->minors[iter->ncur]); |
| 622 | |
| 623 | if (loopiter_set_device(lc, name) == 0) |
| 624 | return 0; |
| 625 | } |
| 626 | done: |
| 627 | loopcxt_deinit_iterator(lc); |
| 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * @device: path to device |
| 633 | */ |
| 634 | int is_loopdev(const char *device) |
| 635 | { |
| 636 | struct stat st; |
| 637 | |
| 638 | if (!device) |
| 639 | return 0; |
| 640 | |
| 641 | return (stat(device, &st) == 0 && |
| 642 | S_ISBLK(st.st_mode) && |
| 643 | major(st.st_rdev) == LOOPDEV_MAJOR); |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * @lc: context |
| 648 | * |
| 649 | * Returns result from LOOP_GET_STAT64 ioctl or NULL on error. |
| 650 | */ |
| 651 | struct loop_info64 *loopcxt_get_info(struct loopdev_cxt *lc) |
| 652 | { |
| 653 | int fd; |
| 654 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 655 | if (!lc || lc->info_failed) { |
| 656 | errno = EINVAL; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 657 | return NULL; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 658 | } |
| 659 | errno = 0; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 660 | if (lc->has_info) |
| 661 | return &lc->info; |
| 662 | |
| 663 | fd = loopcxt_get_fd(lc); |
| 664 | if (fd < 0) |
| 665 | return NULL; |
| 666 | |
| 667 | if (ioctl(fd, LOOP_GET_STATUS64, &lc->info) == 0) { |
| 668 | lc->has_info = 1; |
| 669 | lc->info_failed = 0; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 670 | DBG(CXT, ul_debugobj(lc, "reading loop_info64 OK")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 671 | return &lc->info; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 672 | } |
| 673 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 674 | lc->info_failed = 1; |
| 675 | DBG(CXT, ul_debugobj(lc, "reading loop_info64 FAILED")); |
| 676 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 677 | return NULL; |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * @lc: context |
| 682 | * |
| 683 | * Returns (allocated) string with path to the file assicieted |
| 684 | * with the current loop device. |
| 685 | */ |
| 686 | char *loopcxt_get_backing_file(struct loopdev_cxt *lc) |
| 687 | { |
| 688 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); |
| 689 | char *res = NULL; |
| 690 | |
| 691 | if (sysfs) |
| 692 | /* |
| 693 | * This is always preffered, the loop_info64 |
| 694 | * has too small buffer for the filename. |
| 695 | */ |
| 696 | res = sysfs_strdup(sysfs, "loop/backing_file"); |
| 697 | |
| 698 | if (!res && loopcxt_ioctl_enabled(lc)) { |
| 699 | struct loop_info64 *lo = loopcxt_get_info(lc); |
| 700 | |
| 701 | if (lo) { |
| 702 | lo->lo_file_name[LO_NAME_SIZE - 2] = '*'; |
| 703 | lo->lo_file_name[LO_NAME_SIZE - 1] = '\0'; |
| 704 | res = strdup((char *) lo->lo_file_name); |
| 705 | } |
| 706 | } |
| 707 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 708 | DBG(CXT, ul_debugobj(lc, "get_backing_file [%s]", res)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 709 | return res; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * @lc: context |
| 714 | * @offset: returns offset number for the given device |
| 715 | * |
| 716 | * Returns: <0 on error, 0 on success |
| 717 | */ |
| 718 | int loopcxt_get_offset(struct loopdev_cxt *lc, uint64_t *offset) |
| 719 | { |
| 720 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); |
| 721 | int rc = -EINVAL; |
| 722 | |
| 723 | if (sysfs) |
| 724 | rc = sysfs_read_u64(sysfs, "loop/offset", offset); |
| 725 | |
| 726 | if (rc && loopcxt_ioctl_enabled(lc)) { |
| 727 | struct loop_info64 *lo = loopcxt_get_info(lc); |
| 728 | if (lo) { |
| 729 | if (offset) |
| 730 | *offset = lo->lo_offset; |
| 731 | rc = 0; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 732 | } else |
| 733 | rc = -errno; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 734 | } |
| 735 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 736 | DBG(CXT, ul_debugobj(lc, "get_offset [rc=%d]", rc)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 737 | return rc; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * @lc: context |
| 742 | * @sizelimit: returns size limit for the given device |
| 743 | * |
| 744 | * Returns: <0 on error, 0 on success |
| 745 | */ |
| 746 | int loopcxt_get_sizelimit(struct loopdev_cxt *lc, uint64_t *size) |
| 747 | { |
| 748 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); |
| 749 | int rc = -EINVAL; |
| 750 | |
| 751 | if (sysfs) |
| 752 | rc = sysfs_read_u64(sysfs, "loop/sizelimit", size); |
| 753 | |
| 754 | if (rc && loopcxt_ioctl_enabled(lc)) { |
| 755 | struct loop_info64 *lo = loopcxt_get_info(lc); |
| 756 | if (lo) { |
| 757 | if (size) |
| 758 | *size = lo->lo_sizelimit; |
| 759 | rc = 0; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 760 | } else |
| 761 | rc = -errno; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 762 | } |
| 763 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 764 | DBG(CXT, ul_debugobj(lc, "get_sizelimit [rc=%d]", rc)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 765 | return rc; |
| 766 | } |
| 767 | |
| 768 | /* |
| 769 | * @lc: context |
| 770 | * @devno: returns encryption type |
| 771 | * |
| 772 | * Cryptoloop is DEPRECATED! |
| 773 | * |
| 774 | * Returns: <0 on error, 0 on success |
| 775 | */ |
| 776 | int loopcxt_get_encrypt_type(struct loopdev_cxt *lc, uint32_t *type) |
| 777 | { |
| 778 | struct loop_info64 *lo = loopcxt_get_info(lc); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 779 | int rc; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 780 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 781 | /* not provided by sysfs */ |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 782 | if (lo) { |
| 783 | if (type) |
| 784 | *type = lo->lo_encrypt_type; |
| 785 | rc = 0; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 786 | } else |
| 787 | rc = -errno; |
| 788 | |
| 789 | DBG(CXT, ul_debugobj(lc, "get_encrypt_type [rc=%d]", rc)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 790 | return rc; |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | * @lc: context |
| 795 | * @devno: returns crypt name |
| 796 | * |
| 797 | * Cryptoloop is DEPRECATED! |
| 798 | * |
| 799 | * Returns: <0 on error, 0 on success |
| 800 | */ |
| 801 | const char *loopcxt_get_crypt_name(struct loopdev_cxt *lc) |
| 802 | { |
| 803 | struct loop_info64 *lo = loopcxt_get_info(lc); |
| 804 | |
| 805 | if (lo) |
| 806 | return (char *) lo->lo_crypt_name; |
| 807 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 808 | DBG(CXT, ul_debugobj(lc, "get_crypt_name failed")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 809 | return NULL; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * @lc: context |
| 814 | * @devno: returns backing file devno |
| 815 | * |
| 816 | * Returns: <0 on error, 0 on success |
| 817 | */ |
| 818 | int loopcxt_get_backing_devno(struct loopdev_cxt *lc, dev_t *devno) |
| 819 | { |
| 820 | struct loop_info64 *lo = loopcxt_get_info(lc); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 821 | int rc; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 822 | |
| 823 | if (lo) { |
| 824 | if (devno) |
| 825 | *devno = lo->lo_device; |
| 826 | rc = 0; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 827 | } else |
| 828 | rc = -errno; |
| 829 | |
| 830 | DBG(CXT, ul_debugobj(lc, "get_backing_devno [rc=%d]", rc)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 831 | return rc; |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | * @lc: context |
| 836 | * @ino: returns backing file inode |
| 837 | * |
| 838 | * Returns: <0 on error, 0 on success |
| 839 | */ |
| 840 | int loopcxt_get_backing_inode(struct loopdev_cxt *lc, ino_t *ino) |
| 841 | { |
| 842 | struct loop_info64 *lo = loopcxt_get_info(lc); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 843 | int rc; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 844 | |
| 845 | if (lo) { |
| 846 | if (ino) |
| 847 | *ino = lo->lo_inode; |
| 848 | rc = 0; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 849 | } else |
| 850 | rc = -errno; |
| 851 | |
| 852 | DBG(CXT, ul_debugobj(lc, "get_backing_inode [rc=%d]", rc)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 853 | return rc; |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * Check if the kernel supports partitioned loop devices. |
| 858 | * |
| 859 | * Notes: |
| 860 | * - kernels < 3.2 support partitioned loop devices and PT scanning |
| 861 | * only if max_part= module paremeter is non-zero |
| 862 | * |
| 863 | * - kernels >= 3.2 always support partitioned loop devices |
| 864 | * |
| 865 | * - kernels >= 3.2 always support BLKPG_{ADD,DEL}_PARTITION ioctls |
| 866 | * |
| 867 | * - kernels >= 3.2 enable PT scanner only if max_part= is non-zero or if the |
| 868 | * LO_FLAGS_PARTSCAN flag is set for the device. The PT scanner is disabled |
| 869 | * by default. |
| 870 | * |
| 871 | * See kernel commit e03c8dd14915fabc101aa495828d58598dc5af98. |
| 872 | */ |
| 873 | int loopmod_supports_partscan(void) |
| 874 | { |
| 875 | int rc, ret = 0; |
| 876 | FILE *f; |
| 877 | |
| 878 | if (get_linux_version() >= KERNEL_VERSION(3,2,0)) |
| 879 | return 1; |
| 880 | |
| 881 | f = fopen("/sys/module/loop/parameters/max_part", "r"); |
| 882 | if (!f) |
| 883 | return 0; |
| 884 | rc = fscanf(f, "%d", &ret); |
| 885 | fclose(f); |
| 886 | return rc == 1 ? ret : 0; |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | * @lc: context |
| 891 | * |
| 892 | * Returns: 1 if the partscan flags is set *or* (for old kernels) partitions |
| 893 | * scannig is enabled for all loop devices. |
| 894 | */ |
| 895 | int loopcxt_is_partscan(struct loopdev_cxt *lc) |
| 896 | { |
| 897 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); |
| 898 | |
| 899 | if (sysfs) { |
| 900 | /* kernel >= 3.2 */ |
| 901 | int fl; |
| 902 | if (sysfs_read_int(sysfs, "loop/partscan", &fl) == 0) |
| 903 | return fl; |
| 904 | } |
| 905 | |
| 906 | /* old kernels (including kernels without loopN/loop/<flags> directory */ |
| 907 | return loopmod_supports_partscan(); |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * @lc: context |
| 912 | * |
| 913 | * Returns: 1 if the autoclear flags is set. |
| 914 | */ |
| 915 | int loopcxt_is_autoclear(struct loopdev_cxt *lc) |
| 916 | { |
| 917 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); |
| 918 | |
| 919 | if (sysfs) { |
| 920 | int fl; |
| 921 | if (sysfs_read_int(sysfs, "loop/autoclear", &fl) == 0) |
| 922 | return fl; |
| 923 | } |
| 924 | |
| 925 | if (loopcxt_ioctl_enabled(lc)) { |
| 926 | struct loop_info64 *lo = loopcxt_get_info(lc); |
| 927 | if (lo) |
| 928 | return lo->lo_flags & LO_FLAGS_AUTOCLEAR; |
| 929 | } |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | /* |
| 934 | * @lc: context |
| 935 | * |
| 936 | * Returns: 1 if the readonly flags is set. |
| 937 | */ |
| 938 | int loopcxt_is_readonly(struct loopdev_cxt *lc) |
| 939 | { |
| 940 | struct sysfs_cxt *sysfs = loopcxt_get_sysfs(lc); |
| 941 | |
| 942 | if (sysfs) { |
| 943 | int fl; |
| 944 | if (sysfs_read_int(sysfs, "ro", &fl) == 0) |
| 945 | return fl; |
| 946 | } |
| 947 | |
| 948 | if (loopcxt_ioctl_enabled(lc)) { |
| 949 | struct loop_info64 *lo = loopcxt_get_info(lc); |
| 950 | if (lo) |
| 951 | return lo->lo_flags & LO_FLAGS_READ_ONLY; |
| 952 | } |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | /* |
| 957 | * @lc: context |
| 958 | * @st: backing file stat or NULL |
| 959 | * @backing_file: filename |
| 960 | * @offset: offset |
| 961 | * @flags: LOOPDEV_FL_OFFSET if @offset should not be ignored |
| 962 | * |
| 963 | * Returns 1 if the current @lc loopdev is associated with the given backing |
| 964 | * file. Note that the preferred way is to use devno and inode number rather |
| 965 | * than filename. The @backing_file filename is poor solution usable in case |
| 966 | * that you don't have rights to call stat(). |
| 967 | * |
| 968 | * Don't forget that old kernels provide very restricted (in size) backing |
| 969 | * filename by LOOP_GET_STAT64 ioctl only. |
| 970 | */ |
| 971 | int loopcxt_is_used(struct loopdev_cxt *lc, |
| 972 | struct stat *st, |
| 973 | const char *backing_file, |
| 974 | uint64_t offset, |
| 975 | int flags) |
| 976 | { |
| 977 | ino_t ino; |
| 978 | dev_t dev; |
| 979 | |
| 980 | if (!lc) |
| 981 | return 0; |
| 982 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 983 | DBG(CXT, ul_debugobj(lc, "checking %s vs. %s", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 984 | loopcxt_get_device(lc), |
| 985 | backing_file)); |
| 986 | |
| 987 | if (st && loopcxt_get_backing_inode(lc, &ino) == 0 && |
| 988 | loopcxt_get_backing_devno(lc, &dev) == 0) { |
| 989 | |
| 990 | if (ino == st->st_ino && dev == st->st_dev) |
| 991 | goto found; |
| 992 | |
| 993 | /* don't use filename if we have devno and inode */ |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | /* poor man's solution */ |
| 998 | if (backing_file) { |
| 999 | char *name = loopcxt_get_backing_file(lc); |
| 1000 | int rc = name && strcmp(name, backing_file) == 0; |
| 1001 | |
| 1002 | free(name); |
| 1003 | if (rc) |
| 1004 | goto found; |
| 1005 | } |
| 1006 | |
| 1007 | return 0; |
| 1008 | found: |
| 1009 | if (flags & LOOPDEV_FL_OFFSET) { |
| 1010 | uint64_t off; |
| 1011 | |
| 1012 | return loopcxt_get_offset(lc, &off) == 0 && off == offset; |
| 1013 | } |
| 1014 | return 1; |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * The setting is removed by loopcxt_set_device() loopcxt_next()! |
| 1019 | */ |
| 1020 | int loopcxt_set_offset(struct loopdev_cxt *lc, uint64_t offset) |
| 1021 | { |
| 1022 | if (!lc) |
| 1023 | return -EINVAL; |
| 1024 | lc->info.lo_offset = offset; |
| 1025 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1026 | DBG(CXT, ul_debugobj(lc, "set offset=%jd", offset)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | /* |
| 1031 | * The setting is removed by loopcxt_set_device() loopcxt_next()! |
| 1032 | */ |
| 1033 | int loopcxt_set_sizelimit(struct loopdev_cxt *lc, uint64_t sizelimit) |
| 1034 | { |
| 1035 | if (!lc) |
| 1036 | return -EINVAL; |
| 1037 | lc->info.lo_sizelimit = sizelimit; |
| 1038 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1039 | DBG(CXT, ul_debugobj(lc, "set sizelimit=%jd", sizelimit)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | * @lc: context |
| 1045 | * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags |
| 1046 | * |
| 1047 | * The setting is removed by loopcxt_set_device() loopcxt_next()! |
| 1048 | * |
| 1049 | * Returns: 0 on success, <0 on error. |
| 1050 | */ |
| 1051 | int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags) |
| 1052 | { |
| 1053 | if (!lc) |
| 1054 | return -EINVAL; |
| 1055 | lc->info.lo_flags = flags; |
| 1056 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1057 | DBG(CXT, ul_debugobj(lc, "set flags=%u", (unsigned) flags)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * @lc: context |
| 1063 | * @filename: backing file path (the path will be canonicalized) |
| 1064 | * |
| 1065 | * The setting is removed by loopcxt_set_device() loopcxt_next()! |
| 1066 | * |
| 1067 | * Returns: 0 on success, <0 on error. |
| 1068 | */ |
| 1069 | int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename) |
| 1070 | { |
| 1071 | if (!lc) |
| 1072 | return -EINVAL; |
| 1073 | |
| 1074 | lc->filename = canonicalize_path(filename); |
| 1075 | if (!lc->filename) |
| 1076 | return -errno; |
| 1077 | |
| 1078 | strncpy((char *)lc->info.lo_file_name, lc->filename, LO_NAME_SIZE); |
| 1079 | lc->info.lo_file_name[LO_NAME_SIZE- 1] = '\0'; |
| 1080 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1081 | DBG(CXT, ul_debugobj(lc, "set backing file=%s", lc->info.lo_file_name)); |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
| 1086 | * In kernels prior to v3.9, if the offset or sizelimit options |
| 1087 | * are used, the block device's size won't be synced automatically. |
| 1088 | * blockdev --getsize64 and filesystems will use the backing |
| 1089 | * file size until the block device has been re-opened or the |
| 1090 | * LOOP_SET_CAPACITY ioctl is called to sync the sizes. |
| 1091 | * |
| 1092 | * Since mount -oloop uses the LO_FLAGS_AUTOCLEAR option and passes |
| 1093 | * the open file descriptor to the mount system call, we need to use |
| 1094 | * the ioctl. Calling losetup directly doesn't have this problem since |
| 1095 | * it closes the device when it exits and whatever consumes the device |
| 1096 | * next will re-open it, causing the resync. |
| 1097 | */ |
| 1098 | static int loopcxt_check_size(struct loopdev_cxt *lc, int file_fd) |
| 1099 | { |
| 1100 | uint64_t size, expected_size; |
| 1101 | int dev_fd; |
| 1102 | struct stat st; |
| 1103 | |
| 1104 | if (!lc->info.lo_offset && !lc->info.lo_sizelimit) |
| 1105 | return 0; |
| 1106 | |
| 1107 | if (fstat(file_fd, &st)) { |
| 1108 | DBG(CXT, ul_debugobj(lc, "failed to fstat backing file")); |
| 1109 | return -errno; |
| 1110 | } |
| 1111 | if (S_ISBLK(st.st_mode)) { |
| 1112 | if (blkdev_get_size(file_fd, |
| 1113 | (unsigned long long *) &expected_size)) { |
| 1114 | DBG(CXT, ul_debugobj(lc, "failed to determine device size")); |
| 1115 | return -errno; |
| 1116 | } |
| 1117 | } else |
| 1118 | expected_size = st.st_size; |
| 1119 | |
| 1120 | if (expected_size == 0 || expected_size <= lc->info.lo_offset) { |
| 1121 | DBG(CXT, ul_debugobj(lc, "failed to determine expected size")); |
| 1122 | return 0; /* ignore this error */ |
| 1123 | } |
| 1124 | |
| 1125 | if (lc->info.lo_offset > 0) |
| 1126 | expected_size -= lc->info.lo_offset; |
| 1127 | |
| 1128 | if (lc->info.lo_sizelimit > 0 && lc->info.lo_sizelimit < expected_size) |
| 1129 | expected_size = lc->info.lo_sizelimit; |
| 1130 | |
| 1131 | dev_fd = loopcxt_get_fd(lc); |
| 1132 | if (dev_fd < 0) { |
| 1133 | DBG(CXT, ul_debugobj(lc, "failed to get loop FD")); |
| 1134 | return -errno; |
| 1135 | } |
| 1136 | |
| 1137 | if (blkdev_get_size(dev_fd, (unsigned long long *) &size)) { |
| 1138 | DBG(CXT, ul_debugobj(lc, "failed to determine loopdev size")); |
| 1139 | return -errno; |
| 1140 | } |
| 1141 | |
| 1142 | /* It's block device, so, align to 512-byte sectors */ |
| 1143 | if (expected_size % 512) { |
| 1144 | DBG(CXT, ul_debugobj(lc, "expected size misaligned to 512-byte sectors")); |
| 1145 | expected_size = (expected_size >> 9) << 9; |
| 1146 | } |
| 1147 | |
| 1148 | if (expected_size != size) { |
| 1149 | DBG(CXT, ul_debugobj(lc, "warning: loopdev and expected " |
| 1150 | "size dismatch (%ju/%ju)", |
| 1151 | size, expected_size)); |
| 1152 | |
| 1153 | if (loopcxt_set_capacity(lc)) { |
| 1154 | /* ioctl not available */ |
| 1155 | if (errno == ENOTTY || errno == EINVAL) |
| 1156 | errno = ERANGE; |
| 1157 | return -errno; |
| 1158 | } |
| 1159 | |
| 1160 | if (blkdev_get_size(dev_fd, (unsigned long long *) &size)) |
| 1161 | return -errno; |
| 1162 | |
| 1163 | if (expected_size != size) { |
| 1164 | errno = ERANGE; |
| 1165 | DBG(CXT, ul_debugobj(lc, "failed to set loopdev size, " |
| 1166 | "size: %ju, expected: %ju", |
| 1167 | size, expected_size)); |
| 1168 | return -errno; |
| 1169 | } |
| 1170 | } |
| 1171 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * @cl: context |
| 1177 | * |
| 1178 | * Associate the current device (see loopcxt_{set,get}_device()) with |
| 1179 | * a file (see loopcxt_set_backing_file()). |
| 1180 | * |
| 1181 | * The device is initialized read-write by default. If you want read-only |
| 1182 | * device then set LO_FLAGS_READ_ONLY by loopcxt_set_flags(). The LOOPDEV_FL_* |
| 1183 | * flags are ignored and modified according to LO_FLAGS_*. |
| 1184 | * |
| 1185 | * If the device is already open by loopcxt_get_fd() then this setup device |
| 1186 | * function will re-open the device to fix read/write mode. |
| 1187 | * |
| 1188 | * The device is also initialized read-only if the backing file is not |
| 1189 | * possible to open read-write (e.g. read-only FS). |
| 1190 | * |
| 1191 | * Returns: <0 on error, 0 on success. |
| 1192 | */ |
| 1193 | int loopcxt_setup_device(struct loopdev_cxt *lc) |
| 1194 | { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1195 | int file_fd, dev_fd, mode = O_RDWR, rc = -1, cnt = 0; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1196 | |
| 1197 | if (!lc || !*lc->device || !lc->filename) |
| 1198 | return -EINVAL; |
| 1199 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1200 | DBG(SETUP, ul_debugobj(lc, "device setup requested")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1201 | |
| 1202 | /* |
| 1203 | * Open backing file and device |
| 1204 | */ |
| 1205 | if (lc->info.lo_flags & LO_FLAGS_READ_ONLY) |
| 1206 | mode = O_RDONLY; |
| 1207 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1208 | if ((file_fd = open(lc->filename, mode | O_CLOEXEC)) < 0) { |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1209 | if (mode != O_RDONLY && (errno == EROFS || errno == EACCES)) |
| 1210 | file_fd = open(lc->filename, mode = O_RDONLY); |
| 1211 | |
| 1212 | if (file_fd < 0) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1213 | DBG(SETUP, ul_debugobj(lc, "open backing file failed: %m")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1214 | return -errno; |
| 1215 | } |
| 1216 | } |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1217 | DBG(SETUP, ul_debugobj(lc, "backing file open: OK")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1218 | |
| 1219 | if (lc->fd != -1 && lc->mode != mode) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1220 | DBG(SETUP, ul_debugobj(lc, "closing already open device (mode mismatch)")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1221 | close(lc->fd); |
| 1222 | lc->fd = -1; |
| 1223 | lc->mode = 0; |
| 1224 | } |
| 1225 | |
| 1226 | if (mode == O_RDONLY) { |
| 1227 | lc->flags |= LOOPDEV_FL_RDONLY; /* open() mode */ |
| 1228 | lc->info.lo_flags |= LO_FLAGS_READ_ONLY; /* kernel loopdev mode */ |
| 1229 | } else { |
| 1230 | lc->flags |= LOOPDEV_FL_RDWR; /* open() mode */ |
| 1231 | lc->info.lo_flags &= ~LO_FLAGS_READ_ONLY; |
| 1232 | lc->flags &= ~LOOPDEV_FL_RDONLY; |
| 1233 | } |
| 1234 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1235 | do { |
| 1236 | errno = 0; |
| 1237 | dev_fd = loopcxt_get_fd(lc); |
| 1238 | if (dev_fd >= 0 || lc->control_ok == 0) |
| 1239 | break; |
| 1240 | if (errno != EACCES && errno != ENOENT) |
| 1241 | break; |
| 1242 | /* We have permissions to open /dev/loop-control, but open |
| 1243 | * /dev/loopN failed with EACCES, it's probably because udevd |
| 1244 | * does not applied chown yet. Let's wait a moment. */ |
| 1245 | usleep(25000); |
| 1246 | } while (cnt++ < 16); |
| 1247 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1248 | if (dev_fd < 0) { |
| 1249 | rc = -errno; |
| 1250 | goto err; |
| 1251 | } |
| 1252 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1253 | DBG(SETUP, ul_debugobj(lc, "device open: OK")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1254 | |
| 1255 | /* |
| 1256 | * Set FD |
| 1257 | */ |
| 1258 | if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) { |
| 1259 | rc = -errno; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1260 | DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD failed: %m")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1261 | goto err; |
| 1262 | } |
| 1263 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1264 | DBG(SETUP, ul_debugobj(lc, "LOOP_SET_FD: OK")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1265 | |
| 1266 | if (ioctl(dev_fd, LOOP_SET_STATUS64, &lc->info)) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1267 | DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64 failed: %m")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1268 | goto err; |
| 1269 | } |
| 1270 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1271 | DBG(SETUP, ul_debugobj(lc, "LOOP_SET_STATUS64: OK")); |
| 1272 | |
| 1273 | if ((rc = loopcxt_check_size(lc, file_fd))) |
| 1274 | goto err; |
| 1275 | |
| 1276 | close(file_fd); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1277 | |
| 1278 | memset(&lc->info, 0, sizeof(lc->info)); |
| 1279 | lc->has_info = 0; |
| 1280 | lc->info_failed = 0; |
| 1281 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1282 | DBG(SETUP, ul_debugobj(lc, "success [rc=0]")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1283 | return 0; |
| 1284 | err: |
| 1285 | if (file_fd >= 0) |
| 1286 | close(file_fd); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1287 | if (dev_fd >= 0 && rc != -EBUSY) |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1288 | ioctl(dev_fd, LOOP_CLR_FD, 0); |
| 1289 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1290 | DBG(SETUP, ul_debugobj(lc, "failed [rc=%d]", rc)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1291 | return rc; |
| 1292 | } |
| 1293 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1294 | int loopcxt_set_capacity(struct loopdev_cxt *lc) |
| 1295 | { |
| 1296 | int fd = loopcxt_get_fd(lc); |
| 1297 | |
| 1298 | if (fd < 0) |
| 1299 | return -EINVAL; |
| 1300 | |
| 1301 | /* Kernels prior to v2.6.30 don't support this ioctl */ |
| 1302 | if (ioctl(fd, LOOP_SET_CAPACITY, 0) < 0) { |
| 1303 | int rc = -errno; |
| 1304 | DBG(CXT, ul_debugobj(lc, "LOOP_SET_CAPACITY failed: %m")); |
| 1305 | return rc; |
| 1306 | } |
| 1307 | |
| 1308 | DBG(CXT, ul_debugobj(lc, "capacity set")); |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1312 | int loopcxt_delete_device(struct loopdev_cxt *lc) |
| 1313 | { |
| 1314 | int fd = loopcxt_get_fd(lc); |
| 1315 | |
| 1316 | if (fd < 0) |
| 1317 | return -EINVAL; |
| 1318 | |
| 1319 | if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1320 | DBG(CXT, ul_debugobj(lc, "LOOP_CLR_FD failed: %m")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1321 | return -errno; |
| 1322 | } |
| 1323 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1324 | DBG(CXT, ul_debugobj(lc, "device removed")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1325 | return 0; |
| 1326 | } |
| 1327 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1328 | int loopcxt_add_device(struct loopdev_cxt *lc) |
| 1329 | { |
| 1330 | int rc = -EINVAL; |
| 1331 | int ctl, nr = -1; |
| 1332 | const char *p, *dev = loopcxt_get_device(lc); |
| 1333 | |
| 1334 | if (!dev) |
| 1335 | goto done; |
| 1336 | |
| 1337 | if (!(lc->flags & LOOPDEV_FL_CONTROL)) { |
| 1338 | rc = -ENOSYS; |
| 1339 | goto done; |
| 1340 | } |
| 1341 | |
| 1342 | p = strrchr(dev, '/'); |
| 1343 | if (!p || (sscanf(p, "/loop%d", &nr) != 1 && sscanf(p, "/%d", &nr) != 1) |
| 1344 | || nr < 0) |
| 1345 | goto done; |
| 1346 | |
| 1347 | ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC); |
| 1348 | if (ctl >= 0) { |
| 1349 | DBG(CXT, ul_debugobj(lc, "add_device %d", nr)); |
| 1350 | rc = ioctl(ctl, LOOP_CTL_ADD, nr); |
| 1351 | close(ctl); |
| 1352 | } |
| 1353 | lc->control_ok = rc >= 0 ? 1 : 0; |
| 1354 | done: |
| 1355 | DBG(CXT, ul_debugobj(lc, "add_device done [rc=%d]", rc)); |
| 1356 | return rc; |
| 1357 | } |
| 1358 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1359 | /* |
| 1360 | * Note that LOOP_CTL_GET_FREE ioctl is supported since kernel 3.1. In older |
| 1361 | * kernels we have to check all loop devices to found unused one. |
| 1362 | * |
| 1363 | * See kernel commit 770fe30a46a12b6fb6b63fbe1737654d28e8484. |
| 1364 | */ |
| 1365 | int loopcxt_find_unused(struct loopdev_cxt *lc) |
| 1366 | { |
| 1367 | int rc = -1; |
| 1368 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1369 | DBG(CXT, ul_debugobj(lc, "find_unused requested")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1370 | |
| 1371 | if (lc->flags & LOOPDEV_FL_CONTROL) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1372 | int ctl = open(_PATH_DEV_LOOPCTL, O_RDWR|O_CLOEXEC); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1373 | |
| 1374 | if (ctl >= 0) |
| 1375 | rc = ioctl(ctl, LOOP_CTL_GET_FREE); |
| 1376 | if (rc >= 0) { |
| 1377 | char name[16]; |
| 1378 | snprintf(name, sizeof(name), "loop%d", rc); |
| 1379 | |
| 1380 | rc = loopiter_set_device(lc, name); |
| 1381 | } |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1382 | lc->control_ok = ctl >= 0 && rc == 0 ? 1 : 0; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1383 | if (ctl >= 0) |
| 1384 | close(ctl); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1385 | DBG(CXT, ul_debugobj(lc, "find_unused by loop-control [rc=%d]", rc)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | if (rc < 0) { |
| 1389 | rc = loopcxt_init_iterator(lc, LOOPITER_FL_FREE); |
| 1390 | if (rc) |
| 1391 | return rc; |
| 1392 | |
| 1393 | rc = loopcxt_next(lc); |
| 1394 | loopcxt_deinit_iterator(lc); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 1395 | DBG(CXT, ul_debugobj(lc, "find_unused by scan [rc=%d]", rc)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1396 | } |
| 1397 | return rc; |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | |
| 1402 | /* |
| 1403 | * Return: TRUE/FALSE |
| 1404 | */ |
| 1405 | int loopdev_is_autoclear(const char *device) |
| 1406 | { |
| 1407 | struct loopdev_cxt lc; |
| 1408 | int rc; |
| 1409 | |
| 1410 | if (!device) |
| 1411 | return 0; |
| 1412 | |
| 1413 | rc = loopcxt_init(&lc, 0); |
| 1414 | if (!rc) |
| 1415 | rc = loopcxt_set_device(&lc, device); |
| 1416 | if (!rc) |
| 1417 | rc = loopcxt_is_autoclear(&lc); |
| 1418 | |
| 1419 | loopcxt_deinit(&lc); |
| 1420 | return rc; |
| 1421 | } |
| 1422 | |
| 1423 | char *loopdev_get_backing_file(const char *device) |
| 1424 | { |
| 1425 | struct loopdev_cxt lc; |
| 1426 | char *res = NULL; |
| 1427 | |
| 1428 | if (!device) |
| 1429 | return NULL; |
| 1430 | if (loopcxt_init(&lc, 0)) |
| 1431 | return NULL; |
| 1432 | if (loopcxt_set_device(&lc, device) == 0) |
| 1433 | res = loopcxt_get_backing_file(&lc); |
| 1434 | |
| 1435 | loopcxt_deinit(&lc); |
| 1436 | return res; |
| 1437 | } |
| 1438 | |
| 1439 | /* |
| 1440 | * Returns: TRUE/FALSE |
| 1441 | */ |
| 1442 | int loopdev_is_used(const char *device, const char *filename, |
| 1443 | uint64_t offset, int flags) |
| 1444 | { |
| 1445 | struct loopdev_cxt lc; |
| 1446 | struct stat st; |
| 1447 | int rc = 0; |
| 1448 | |
| 1449 | if (!device || !filename) |
| 1450 | return 0; |
| 1451 | |
| 1452 | rc = loopcxt_init(&lc, 0); |
| 1453 | if (!rc) |
| 1454 | rc = loopcxt_set_device(&lc, device); |
| 1455 | if (rc) |
| 1456 | return rc; |
| 1457 | |
| 1458 | rc = !stat(filename, &st); |
| 1459 | rc = loopcxt_is_used(&lc, rc ? &st : NULL, filename, offset, flags); |
| 1460 | |
| 1461 | loopcxt_deinit(&lc); |
| 1462 | return rc; |
| 1463 | } |
| 1464 | |
| 1465 | int loopdev_delete(const char *device) |
| 1466 | { |
| 1467 | struct loopdev_cxt lc; |
| 1468 | int rc; |
| 1469 | |
| 1470 | if (!device) |
| 1471 | return -EINVAL; |
| 1472 | |
| 1473 | rc = loopcxt_init(&lc, 0); |
| 1474 | if (!rc) |
| 1475 | rc = loopcxt_set_device(&lc, device); |
| 1476 | if (!rc) |
| 1477 | rc = loopcxt_delete_device(&lc); |
| 1478 | loopcxt_deinit(&lc); |
| 1479 | return rc; |
| 1480 | } |
| 1481 | |
| 1482 | /* |
| 1483 | * Returns: 0 = success, < 0 error, 1 not found |
| 1484 | */ |
| 1485 | int loopcxt_find_by_backing_file(struct loopdev_cxt *lc, const char *filename, |
| 1486 | uint64_t offset, int flags) |
| 1487 | { |
| 1488 | int rc, hasst; |
| 1489 | struct stat st; |
| 1490 | |
| 1491 | if (!filename) |
| 1492 | return -EINVAL; |
| 1493 | |
| 1494 | hasst = !stat(filename, &st); |
| 1495 | |
| 1496 | rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED); |
| 1497 | if (rc) |
| 1498 | return rc; |
| 1499 | |
| 1500 | while ((rc = loopcxt_next(lc)) == 0) { |
| 1501 | |
| 1502 | if (loopcxt_is_used(lc, hasst ? &st : NULL, |
| 1503 | filename, offset, flags)) |
| 1504 | break; |
| 1505 | } |
| 1506 | |
| 1507 | loopcxt_deinit_iterator(lc); |
| 1508 | return rc; |
| 1509 | } |
| 1510 | |
| 1511 | /* |
| 1512 | * Returns allocated string with device name |
| 1513 | */ |
| 1514 | char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, int flags) |
| 1515 | { |
| 1516 | struct loopdev_cxt lc; |
| 1517 | char *res = NULL; |
| 1518 | |
| 1519 | if (!filename) |
| 1520 | return NULL; |
| 1521 | |
| 1522 | if (loopcxt_init(&lc, 0)) |
| 1523 | return NULL; |
| 1524 | if (loopcxt_find_by_backing_file(&lc, filename, offset, flags) == 0) |
| 1525 | res = loopcxt_strdup_device(&lc); |
| 1526 | loopcxt_deinit(&lc); |
| 1527 | |
| 1528 | return res; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * Returns number of loop devices associated with @file, if only one loop |
| 1533 | * device is associeted with the given @filename and @loopdev is not NULL then |
| 1534 | * @loopdev returns name of the device. |
| 1535 | */ |
| 1536 | int loopdev_count_by_backing_file(const char *filename, char **loopdev) |
| 1537 | { |
| 1538 | struct loopdev_cxt lc; |
| 1539 | int count = 0, rc; |
| 1540 | |
| 1541 | if (!filename) |
| 1542 | return -1; |
| 1543 | |
| 1544 | rc = loopcxt_init(&lc, 0); |
| 1545 | if (rc) |
| 1546 | return rc; |
| 1547 | if (loopcxt_init_iterator(&lc, LOOPITER_FL_USED)) |
| 1548 | return -1; |
| 1549 | |
| 1550 | while(loopcxt_next(&lc) == 0) { |
| 1551 | char *backing = loopcxt_get_backing_file(&lc); |
| 1552 | |
| 1553 | if (!backing || strcmp(backing, filename)) { |
| 1554 | free(backing); |
| 1555 | continue; |
| 1556 | } |
| 1557 | |
| 1558 | free(backing); |
| 1559 | if (loopdev && count == 0) |
| 1560 | *loopdev = loopcxt_strdup_device(&lc); |
| 1561 | count++; |
| 1562 | } |
| 1563 | |
| 1564 | loopcxt_deinit(&lc); |
| 1565 | |
| 1566 | if (loopdev && count > 1) { |
| 1567 | free(*loopdev); |
| 1568 | *loopdev = NULL; |
| 1569 | } |
| 1570 | return count; |
| 1571 | } |
| 1572 | |