loop.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 fd; /* -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. static int get_next_free_loop(char *dev, int id)
  86. {
  87. int loopdevno;
  88. loopdevno = get_free_loop();
  89. if (loopdevno != -1) {
  90. /* loopdevno is -2 (use id) or >= 0 (use id = loopdevno): */
  91. if (loopdevno >= 0)
  92. id = loopdevno;
  93. sprintf(dev, LOOP_FORMAT, id);
  94. }
  95. return loopdevno;
  96. }
  97. #if ENABLE_TRY_LOOP_CONFIGURE || ENABLE_LOOP_CONFIGURE
  98. # define LOOP_CONFIGURE 0x4C0A
  99. struct bb_loop_config {
  100. uint32_t fd;
  101. uint32_t block_size;
  102. struct loop_info64 info;
  103. uint64_t __reserved[8];
  104. };
  105. #endif
  106. static int set_loopdev_params(int lfd,
  107. int ffd, const char *file,
  108. unsigned long long offset,
  109. unsigned long long sizelimit,
  110. unsigned flags)
  111. {
  112. int rc;
  113. #if ENABLE_TRY_LOOP_CONFIGURE || ENABLE_LOOP_CONFIGURE
  114. struct bb_loop_config lconfig;
  115. # define loopinfo lconfig.info
  116. #else
  117. bb_loop_info loopinfo;
  118. #endif
  119. rc = ioctl(lfd, BB_LOOP_GET_STATUS, &loopinfo);
  120. /* If device is free, try to claim it */
  121. if (rc && errno == ENXIO) {
  122. #if ENABLE_TRY_LOOP_CONFIGURE || ENABLE_LOOP_CONFIGURE
  123. memset(&lconfig, 0, sizeof(lconfig));
  124. #else
  125. memset(&loopinfo, 0, sizeof(loopinfo));
  126. #endif
  127. safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
  128. loopinfo.lo_offset = offset;
  129. loopinfo.lo_sizelimit = sizelimit;
  130. /*
  131. * LO_FLAGS_READ_ONLY is not set because RO is controlled
  132. * by open type of the lfd.
  133. */
  134. loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
  135. #if ENABLE_TRY_LOOP_CONFIGURE || ENABLE_LOOP_CONFIGURE
  136. lconfig.fd = ffd;
  137. rc = ioctl(lfd, LOOP_CONFIGURE, &lconfig);
  138. if (rc == 0)
  139. return rc; /* SUCCESS! */
  140. # if ENABLE_TRY_LOOP_CONFIGURE
  141. if (errno != EINVAL)
  142. return rc; /* error other than old kernel */
  143. /* Old kernel, fall through into old way to do it: */
  144. # endif
  145. #endif
  146. #if ENABLE_TRY_LOOP_CONFIGURE || ENABLE_NO_LOOP_CONFIGURE
  147. /* Associate free loop device with file */
  148. rc = ioctl(lfd, LOOP_SET_FD, ffd);
  149. if (rc != 0) {
  150. /* Ouch... race: the device already has a fd */
  151. return rc;
  152. }
  153. rc = ioctl(lfd, BB_LOOP_SET_STATUS, &loopinfo);
  154. if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
  155. /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
  156. /* (this code path is not tested) */
  157. loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
  158. rc = ioctl(lfd, BB_LOOP_SET_STATUS, &loopinfo);
  159. }
  160. if (rc == 0)
  161. return rc; /* SUCCESS! */
  162. /* failure, undo LOOP_SET_FD */
  163. ioctl(lfd, LOOP_CLR_FD, 0); // actually, 0 param is unnecessary
  164. #endif
  165. }
  166. return -1;
  167. #undef loopinfo
  168. }
  169. /* Returns opened fd to the loop device, <0 on error.
  170. * *device is loop device to use, or if *device==NULL finds a loop device to
  171. * mount it on and sets *device to a strdup of that loop device name.
  172. */
  173. int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset,
  174. unsigned long long sizelimit, unsigned flags)
  175. {
  176. char dev[LOOP_NAMESIZE];
  177. char *try;
  178. struct stat statbuf;
  179. int i, lfd, ffd, mode, rc;
  180. /* Open the file. Barf if this doesn't work. */
  181. mode = (flags & BB_LO_FLAGS_READ_ONLY) ? O_RDONLY : O_RDWR;
  182. open_ffd:
  183. ffd = open(file, mode);
  184. if (ffd < 0) {
  185. if (mode != O_RDONLY) {
  186. mode = O_RDONLY;
  187. goto open_ffd;
  188. }
  189. return -errno;
  190. }
  191. try = *device;
  192. if (!try) {
  193. try = dev;
  194. }
  195. /* Find a loop device */
  196. /* 0xfffff is a max possible minor number in Linux circa 2010 */
  197. for (i = 0; i <= 0xfffff; i++) {
  198. if (!*device) {
  199. rc = get_next_free_loop(dev, i);
  200. if (rc == -1)
  201. break; /* no free loop devices (or other error in LOOP_CTL_GET_FREE) */
  202. if (rc >= 0)
  203. /* /dev/loop-control gave us the next free /dev/loopN */
  204. goto open_lfd;
  205. /* else: sequential /dev/loopN, needs to be tested/maybe_created */
  206. }
  207. IF_FEATURE_MOUNT_LOOP_CREATE(errno = 0;)
  208. if (stat(try, &statbuf) != 0 || !S_ISBLK(statbuf.st_mode)) {
  209. if (ENABLE_FEATURE_MOUNT_LOOP_CREATE
  210. && errno == ENOENT
  211. && (!*device)
  212. ) {
  213. /* Node doesn't exist, try to create it */
  214. if (mknod(dev, S_IFBLK|0644, makedev(7, i)) == 0)
  215. goto open_lfd;
  216. }
  217. /* Ran out of block devices, return failure */
  218. rc = -1;
  219. break;
  220. }
  221. open_lfd:
  222. /* Open the sucker and check its loopiness */
  223. lfd = rc = open(try, mode);
  224. if (lfd < 0 && errno == EROFS) {
  225. mode = O_RDONLY;
  226. lfd = rc = open(try, mode);
  227. }
  228. if (lfd < 0) {
  229. if (errno == ENXIO) {
  230. /* Happens if loop module is not loaded */
  231. /* rc is -1; */
  232. break;
  233. }
  234. goto try_next_loopN;
  235. }
  236. rc = set_loopdev_params(lfd, ffd, file, offset, sizelimit, flags);
  237. if (rc == 0) {
  238. /* SUCCESS! */
  239. if (!*device)
  240. *device = xstrdup(dev);
  241. /* Note: mount asks for LO_FLAGS_AUTOCLEAR loopdev.
  242. * Closing LO_FLAGS_AUTOCLEARed lfd before mount
  243. * is wrong (would free the loop device!),
  244. * this is why we return without closing it.
  245. */
  246. rc = lfd; /* return this */
  247. break;
  248. }
  249. close(lfd);
  250. try_next_loopN:
  251. rc = -1;
  252. if (*device) /* was looking for a particular "/dev/loopN"? */
  253. break; /* yes, do not try other names */
  254. } /* for() */
  255. close(ffd);
  256. return rc;
  257. }