3
0

loop.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. * Copyright (C) 2005 by Rob Landley <rob@landley.net>
  7. *
  8. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  9. */
  10. #include "libbb.h"
  11. /* For 2.6, use the cleaned up header to get the 64 bit API. */
  12. #include <linux/version.h>
  13. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
  14. #include <linux/loop.h>
  15. typedef struct loop_info64 bb_loop_info;
  16. #define BB_LOOP_SET_STATUS LOOP_SET_STATUS64
  17. #define BB_LOOP_GET_STATUS LOOP_GET_STATUS64
  18. /* For 2.4 and earlier, use the 32 bit API (and don't trust the headers) */
  19. #else
  20. /* Stuff stolen from linux/loop.h for 2.4 and earlier kernels*/
  21. #include <linux/posix_types.h>
  22. #define LO_NAME_SIZE 64
  23. #define LO_KEY_SIZE 32
  24. #define LOOP_SET_FD 0x4C00
  25. #define LOOP_CLR_FD 0x4C01
  26. #define BB_LOOP_SET_STATUS 0x4C02
  27. #define BB_LOOP_GET_STATUS 0x4C03
  28. typedef struct {
  29. int lo_number;
  30. __kernel_dev_t lo_device;
  31. unsigned long lo_inode;
  32. __kernel_dev_t lo_rdevice;
  33. int lo_offset;
  34. int lo_encrypt_type;
  35. int lo_encrypt_key_size;
  36. int lo_flags;
  37. char lo_file_name[LO_NAME_SIZE];
  38. unsigned char lo_encrypt_key[LO_KEY_SIZE];
  39. unsigned long lo_init[2];
  40. char reserved[4];
  41. } bb_loop_info;
  42. #endif
  43. char *query_loop(const char *device)
  44. {
  45. int fd;
  46. bb_loop_info loopinfo;
  47. char *dev = 0;
  48. fd = open(device, O_RDONLY);
  49. if (fd < 0) return 0;
  50. if (!ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo))
  51. dev = xasprintf("%ld %s", (long) loopinfo.lo_offset,
  52. (char *)loopinfo.lo_file_name);
  53. close(fd);
  54. return dev;
  55. }
  56. int del_loop(const char *device)
  57. {
  58. int fd, rc;
  59. fd = open(device, O_RDONLY);
  60. if (fd < 0) return 1;
  61. rc = ioctl(fd, LOOP_CLR_FD, 0);
  62. close(fd);
  63. return rc;
  64. }
  65. /* Returns 0 if mounted RW, 1 if mounted read-only, <0 for error.
  66. *device is loop device to use, or if *device==NULL finds a loop device to
  67. mount it on and sets *device to a strdup of that loop device name. This
  68. search will re-use an existing loop device already bound to that
  69. file/offset if it finds one.
  70. */
  71. int set_loop(char **device, const char *file, unsigned long long offset)
  72. {
  73. char dev[LOOP_NAMESIZE];
  74. char *try;
  75. bb_loop_info loopinfo;
  76. struct stat statbuf;
  77. int i, dfd, ffd, mode, rc = -1;
  78. /* Open the file. Barf if this doesn't work. */
  79. mode = O_RDWR;
  80. ffd = open(file, mode);
  81. if (ffd < 0) {
  82. mode = O_RDONLY;
  83. ffd = open(file, mode);
  84. if (ffd < 0)
  85. return -errno;
  86. }
  87. /* Find a loop device. */
  88. try = *device ? : dev;
  89. for (i = 0; rc; i++) {
  90. sprintf(dev, LOOP_FORMAT, i);
  91. /* Ran out of block devices, return failure. */
  92. if (stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) {
  93. rc = -ENOENT;
  94. break;
  95. }
  96. /* Open the sucker and check its loopiness. */
  97. dfd = open(try, mode);
  98. if (dfd < 0 && errno == EROFS) {
  99. mode = O_RDONLY;
  100. dfd = open(try, mode);
  101. }
  102. if (dfd < 0)
  103. goto try_again;
  104. rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
  105. /* If device is free, claim it. */
  106. if (rc && errno == ENXIO) {
  107. memset(&loopinfo, 0, sizeof(loopinfo));
  108. safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
  109. loopinfo.lo_offset = offset;
  110. /* Associate free loop device with file. */
  111. if (!ioctl(dfd, LOOP_SET_FD, ffd)) {
  112. if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo))
  113. rc = 0;
  114. else
  115. ioctl(dfd, LOOP_CLR_FD, 0);
  116. }
  117. /* If this block device already set up right, re-use it.
  118. (Yes this is racy, but associating two loop devices with the same
  119. file isn't pretty either. In general, mounting the same file twice
  120. without using losetup manually is problematic.)
  121. */
  122. } else if (strcmp(file, (char *)loopinfo.lo_file_name) != 0
  123. || offset != loopinfo.lo_offset) {
  124. rc = -1;
  125. }
  126. close(dfd);
  127. try_again:
  128. if (*device) break;
  129. }
  130. close(ffd);
  131. if (!rc) {
  132. if (!*device)
  133. *device = xstrdup(dev);
  134. return (mode == O_RDONLY); /* 1:ro, 0:rw */
  135. }
  136. return rc;
  137. }