loop.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /* Returns opened fd to the loop device, <0 on error.
  71. * *device is loop device to use, or if *device==NULL finds a loop device to
  72. * mount it on and sets *device to a strdup of that loop device name. This
  73. * search will re-use an existing loop device already bound to that
  74. * file/offset if it finds one.
  75. */
  76. int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, unsigned flags)
  77. {
  78. char dev[LOOP_NAMESIZE];
  79. char *try;
  80. bb_loop_info loopinfo;
  81. struct stat statbuf;
  82. int i, dfd, ffd, mode, rc;
  83. rc = dfd = -1;
  84. /* Open the file. Barf if this doesn't work. */
  85. mode = (flags & BB_LO_FLAGS_READ_ONLY) ? O_RDONLY : O_RDWR;
  86. open_ffd:
  87. ffd = open(file, mode);
  88. if (ffd < 0) {
  89. if (mode != O_RDONLY) {
  90. mode = O_RDONLY;
  91. goto open_ffd;
  92. }
  93. return -errno;
  94. }
  95. //TODO: use LOOP_CTL_GET_FREE instead of trying every loopN in sequence? a-la:
  96. // fd = open("/dev/loop-control", O_RDWR);
  97. // loopN = ioctl(fd, LOOP_CTL_GET_FREE);
  98. //
  99. /* Find a loop device. */
  100. try = *device ? *device : dev;
  101. /* 1048575 (0xfffff) is a max possible minor number in Linux circa 2010 */
  102. for (i = 0; rc && i < 1048576; i++) {
  103. sprintf(dev, LOOP_FORMAT, i);
  104. IF_FEATURE_MOUNT_LOOP_CREATE(errno = 0;)
  105. if (stat(try, &statbuf) != 0 || !S_ISBLK(statbuf.st_mode)) {
  106. if (ENABLE_FEATURE_MOUNT_LOOP_CREATE
  107. && errno == ENOENT
  108. && try == dev
  109. ) {
  110. /* Node doesn't exist, try to create it. */
  111. if (mknod(dev, S_IFBLK|0644, makedev(7, i)) == 0)
  112. goto try_to_open;
  113. }
  114. /* Ran out of block devices, return failure. */
  115. rc = -1;
  116. break;
  117. }
  118. try_to_open:
  119. /* Open the sucker and check its loopiness. */
  120. dfd = open(try, mode);
  121. if (dfd < 0 && errno == EROFS) {
  122. mode = O_RDONLY;
  123. dfd = open(try, mode);
  124. }
  125. if (dfd < 0) {
  126. if (errno == ENXIO) {
  127. /* Happens if loop module is not loaded */
  128. rc = -1;
  129. break;
  130. }
  131. goto try_again;
  132. }
  133. rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
  134. /* If device is free, claim it. */
  135. if (rc && errno == ENXIO) {
  136. /* Associate free loop device with file. */
  137. if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) {
  138. memset(&loopinfo, 0, sizeof(loopinfo));
  139. safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
  140. loopinfo.lo_offset = offset;
  141. /*
  142. * Used by mount to set LO_FLAGS_AUTOCLEAR.
  143. * LO_FLAGS_READ_ONLY is not set because RO is controlled by open type of the file.
  144. * Note that closing LO_FLAGS_AUTOCLEARed dfd before mount
  145. * is wrong (would free the loop device!)
  146. */
  147. loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
  148. rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
  149. if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
  150. /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
  151. /* (this code path is not tested) */
  152. loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
  153. rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
  154. }
  155. if (rc != 0) {
  156. ioctl(dfd, LOOP_CLR_FD, 0);
  157. }
  158. }
  159. } else {
  160. rc = -1;
  161. }
  162. if (rc != 0) {
  163. close(dfd);
  164. }
  165. try_again:
  166. if (*device) break;
  167. }
  168. close(ffd);
  169. if (rc == 0) {
  170. if (!*device)
  171. *device = xstrdup(dev);
  172. return dfd;
  173. }
  174. return rc;
  175. }