loop.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.
  88. */
  89. int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset,
  90. unsigned long long sizelimit, unsigned flags)
  91. {
  92. char dev[LOOP_NAMESIZE];
  93. char *try;
  94. bb_loop_info loopinfo;
  95. struct stat statbuf;
  96. int i, lfd, ffd, mode, rc;
  97. /* Open the file. Barf if this doesn't work. */
  98. mode = (flags & BB_LO_FLAGS_READ_ONLY) ? O_RDONLY : O_RDWR;
  99. open_ffd:
  100. ffd = open(file, mode);
  101. if (ffd < 0) {
  102. if (mode != O_RDONLY) {
  103. mode = O_RDONLY;
  104. goto open_ffd;
  105. }
  106. return -errno;
  107. }
  108. try = *device;
  109. if (!try) {
  110. get_free_loopN:
  111. i = get_free_loop();
  112. if (i == -1) {
  113. close(ffd);
  114. return -1; /* no free loop devices */
  115. }
  116. if (i >= 0) {
  117. try = xasprintf(LOOP_FORMAT, i);
  118. goto open_lfd;
  119. }
  120. /* i == -2: no /dev/loop-control. Do an old-style search for a free device */
  121. try = dev;
  122. }
  123. /* Find a loop device */
  124. /* 0xfffff is a max possible minor number in Linux circa 2010 */
  125. for (i = 0; i <= 0xfffff; i++) {
  126. sprintf(dev, LOOP_FORMAT, i);
  127. IF_FEATURE_MOUNT_LOOP_CREATE(errno = 0;)
  128. if (stat(try, &statbuf) != 0 || !S_ISBLK(statbuf.st_mode)) {
  129. if (ENABLE_FEATURE_MOUNT_LOOP_CREATE
  130. && errno == ENOENT
  131. && try == dev
  132. ) {
  133. /* Node doesn't exist, try to create it */
  134. if (mknod(dev, S_IFBLK|0644, makedev(7, i)) == 0)
  135. goto open_lfd;
  136. }
  137. /* Ran out of block devices, return failure */
  138. rc = -1;
  139. break;
  140. }
  141. open_lfd:
  142. /* Open the sucker and check its loopiness */
  143. lfd = rc = open(try, mode);
  144. if (lfd < 0 && errno == EROFS) {
  145. mode = O_RDONLY;
  146. lfd = rc = open(try, mode);
  147. }
  148. if (lfd < 0) {
  149. if (errno == ENXIO) {
  150. /* Happens if loop module is not loaded */
  151. /* rc is -1; */
  152. break;
  153. }
  154. goto try_next_loopN;
  155. }
  156. rc = ioctl(lfd, BB_LOOP_GET_STATUS, &loopinfo);
  157. /* If device is free, try to claim it */
  158. if (rc && errno == ENXIO) {
  159. /* Associate free loop device with file */
  160. if (ioctl(lfd, LOOP_SET_FD, ffd)) {
  161. /* Ouch. Are we racing with other mount? */
  162. if (!*device /* yes */
  163. && try != dev /* tried a _kernel-offered_ loopN? */
  164. ) {
  165. free(try);
  166. close(lfd);
  167. //TODO: add "if (--failcount != 0) ..."?
  168. goto get_free_loopN;
  169. }
  170. goto close_and_try_next_loopN;
  171. }
  172. memset(&loopinfo, 0, sizeof(loopinfo));
  173. safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
  174. loopinfo.lo_offset = offset;
  175. loopinfo.lo_sizelimit = sizelimit;
  176. /*
  177. * Used by mount to set LO_FLAGS_AUTOCLEAR.
  178. * LO_FLAGS_READ_ONLY is not set because RO is controlled by open type of the file.
  179. * Note that closing LO_FLAGS_AUTOCLEARed lfd before mount
  180. * is wrong (would free the loop device!)
  181. */
  182. loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
  183. rc = ioctl(lfd, BB_LOOP_SET_STATUS, &loopinfo);
  184. if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
  185. /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
  186. /* (this code path is not tested) */
  187. loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
  188. rc = ioctl(lfd, BB_LOOP_SET_STATUS, &loopinfo);
  189. }
  190. if (rc == 0) {
  191. /* SUCCESS! */
  192. if (try != dev) /* tried a kernel-offered free loopN? */
  193. *device = try; /* malloced */
  194. if (!*device) /* was looping in search of free "/dev/loopN"? */
  195. *device = xstrdup(dev);
  196. rc = lfd; /* return this */
  197. break;
  198. }
  199. /* failure, undo LOOP_SET_FD */
  200. ioctl(lfd, LOOP_CLR_FD, 0); // actually, 0 param is unnecessary
  201. }
  202. /* else: device is not free (rc == 0) or error other than ENXIO */
  203. close_and_try_next_loopN:
  204. close(lfd);
  205. try_next_loopN:
  206. rc = -1;
  207. if (*device) /* was looking for a particular "/dev/loopN"? */
  208. break; /* yes, do not try other names */
  209. } /* for() */
  210. close(ffd);
  211. return rc;
  212. }