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