loop.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. #include "libbb.h"
  11. #include <linux/version.h>
  12. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
  13. /* For 2.6, use the cleaned up header to get the 64 bit API. */
  14. // Commented out per Rob's request
  15. //# include "fix_u32.h" /* some old toolchains need __u64 for linux/loop.h */
  16. # include <linux/loop.h>
  17. typedef struct loop_info64 bb_loop_info;
  18. # define BB_LOOP_SET_STATUS LOOP_SET_STATUS64
  19. # define BB_LOOP_GET_STATUS LOOP_GET_STATUS64
  20. #else
  21. /* For 2.4 and earlier, use the 32 bit API (and don't trust the headers) */
  22. /* Stuff stolen from linux/loop.h for 2.4 and earlier kernels */
  23. # include <linux/posix_types.h>
  24. # define LO_NAME_SIZE 64
  25. # define LO_KEY_SIZE 32
  26. # define LOOP_SET_FD 0x4C00
  27. # define LOOP_CLR_FD 0x4C01
  28. # define BB_LOOP_SET_STATUS 0x4C02
  29. # define BB_LOOP_GET_STATUS 0x4C03
  30. typedef struct {
  31. int lo_number;
  32. __kernel_dev_t lo_device;
  33. unsigned long lo_inode;
  34. __kernel_dev_t lo_rdevice;
  35. int lo_offset;
  36. int lo_encrypt_type;
  37. int lo_encrypt_key_size;
  38. int lo_flags;
  39. char lo_file_name[LO_NAME_SIZE];
  40. unsigned char lo_encrypt_key[LO_KEY_SIZE];
  41. unsigned long lo_init[2];
  42. char reserved[4];
  43. } bb_loop_info;
  44. #endif
  45. char* FAST_FUNC query_loop(const char *device)
  46. {
  47. int fd;
  48. bb_loop_info loopinfo;
  49. char *dev = NULL;
  50. fd = open(device, O_RDONLY);
  51. if (fd >= 0) {
  52. if (ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo) == 0) {
  53. dev = xasprintf("%"OFF_FMT"u %s", (off_t) loopinfo.lo_offset,
  54. (char *)loopinfo.lo_file_name);
  55. }
  56. close(fd);
  57. }
  58. return dev;
  59. }
  60. int FAST_FUNC del_loop(const char *device)
  61. {
  62. int fd, rc;
  63. fd = open(device, O_RDONLY);
  64. if (fd < 0)
  65. return 1;
  66. rc = ioctl(fd, LOOP_CLR_FD, 0);
  67. close(fd);
  68. return rc;
  69. }
  70. /* Obtain an unused loop device number */
  71. int FAST_FUNC get_free_loop(void)
  72. {
  73. int fd;
  74. int loopdevno;
  75. fd = open("/dev/loop-control", O_RDWR | O_CLOEXEC);
  76. if (fd == -1)
  77. return fd - 1; /* -2: "no /dev/loop-control" */
  78. #ifndef LOOP_CTL_GET_FREE
  79. # define LOOP_CTL_GET_FREE 0x4C82
  80. #endif
  81. loopdevno = ioctl(fd, LOOP_CTL_GET_FREE);
  82. close(fd);
  83. return loopdevno; /* can be -1 if error */
  84. }
  85. /* Returns opened fd to the loop device, <0 on error.
  86. * *device is loop device to use, or if *device==NULL finds a loop device to
  87. * mount it on and sets *device to a strdup of that loop device name. This
  88. * search will re-use an existing loop device already bound to that
  89. * file/offset if it finds one.
  90. */
  91. int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, unsigned flags)
  92. {
  93. char dev[LOOP_NAMESIZE];
  94. char *try;
  95. bb_loop_info loopinfo;
  96. struct stat statbuf;
  97. int i, dfd, ffd, mode, rc;
  98. rc = dfd = -1;
  99. /* Open the file. Barf if this doesn't work. */
  100. mode = (flags & BB_LO_FLAGS_READ_ONLY) ? O_RDONLY : O_RDWR;
  101. open_ffd:
  102. ffd = open(file, mode);
  103. if (ffd < 0) {
  104. if (mode != O_RDONLY) {
  105. mode = O_RDONLY;
  106. goto open_ffd;
  107. }
  108. return -errno;
  109. }
  110. try = *device;
  111. if (!try) {
  112. i = get_free_loop();
  113. if (i == -2) { /* no /dev/loop-control */
  114. i = 0;
  115. try = dev;
  116. goto old_style;
  117. }
  118. if (i == -1) {
  119. close(ffd);
  120. return -1; /* no free loop devices */
  121. }
  122. try = *device = xasprintf(LOOP_FORMAT, i);
  123. goto try_to_open;
  124. }
  125. old_style:
  126. /* Find a loop device. */
  127. /* 1048575 (0xfffff) is a max possible minor number in Linux circa 2010 */
  128. for (i = 0; rc && i < 1048576; i++) {
  129. sprintf(dev, LOOP_FORMAT, i);
  130. IF_FEATURE_MOUNT_LOOP_CREATE(errno = 0;)
  131. if (stat(try, &statbuf) != 0 || !S_ISBLK(statbuf.st_mode)) {
  132. if (ENABLE_FEATURE_MOUNT_LOOP_CREATE
  133. && errno == ENOENT
  134. && try == dev
  135. ) {
  136. /* Node doesn't exist, try to create it. */
  137. if (mknod(dev, S_IFBLK|0644, makedev(7, i)) == 0)
  138. goto try_to_open;
  139. }
  140. /* Ran out of block devices, return failure. */
  141. rc = -1;
  142. break;
  143. }
  144. try_to_open:
  145. /* Open the sucker and check its loopiness. */
  146. dfd = open(try, mode);
  147. if (dfd < 0 && errno == EROFS) {
  148. mode = O_RDONLY;
  149. dfd = open(try, mode);
  150. }
  151. if (dfd < 0) {
  152. if (errno == ENXIO) {
  153. /* Happens if loop module is not loaded */
  154. rc = -1;
  155. break;
  156. }
  157. goto try_again;
  158. }
  159. rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
  160. /* If device is free, claim it. */
  161. if (rc && errno == ENXIO) {
  162. /* Associate free loop device with file. */
  163. if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) {
  164. memset(&loopinfo, 0, sizeof(loopinfo));
  165. safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
  166. loopinfo.lo_offset = offset;
  167. /*
  168. * Used by mount to set LO_FLAGS_AUTOCLEAR.
  169. * LO_FLAGS_READ_ONLY is not set because RO is controlled by open type of the file.
  170. * Note that closing LO_FLAGS_AUTOCLEARed dfd before mount
  171. * is wrong (would free the loop device!)
  172. */
  173. loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
  174. rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
  175. if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
  176. /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
  177. /* (this code path is not tested) */
  178. loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
  179. rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
  180. }
  181. if (rc != 0) {
  182. ioctl(dfd, LOOP_CLR_FD, 0); // actually, 0 param is unnecessary
  183. }
  184. }
  185. } else {
  186. rc = -1;
  187. }
  188. if (rc != 0) {
  189. close(dfd);
  190. }
  191. try_again:
  192. if (*device) break;
  193. }
  194. close(ffd);
  195. if (rc == 0) {
  196. if (!*device)
  197. *device = xstrdup(dev);
  198. return dfd;
  199. }
  200. return rc;
  201. }