loop.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /* Find a loop device. */
  96. try = *device ? *device : dev;
  97. /* 1048575 (0xfffff) is a max possible minor number in Linux circa 2010 */
  98. for (i = 0; rc && i < 1048576; i++) {
  99. sprintf(dev, LOOP_FORMAT, i);
  100. IF_FEATURE_MOUNT_LOOP_CREATE(errno = 0;)
  101. if (stat(try, &statbuf) != 0 || !S_ISBLK(statbuf.st_mode)) {
  102. if (ENABLE_FEATURE_MOUNT_LOOP_CREATE
  103. && errno == ENOENT
  104. && try == dev
  105. ) {
  106. /* Node doesn't exist, try to create it. */
  107. if (mknod(dev, S_IFBLK|0644, makedev(7, i)) == 0)
  108. goto try_to_open;
  109. }
  110. /* Ran out of block devices, return failure. */
  111. rc = -1;
  112. break;
  113. }
  114. try_to_open:
  115. /* Open the sucker and check its loopiness. */
  116. dfd = open(try, mode);
  117. if (dfd < 0 && errno == EROFS) {
  118. mode = O_RDONLY;
  119. dfd = open(try, mode);
  120. }
  121. if (dfd < 0) {
  122. if (errno == ENXIO) {
  123. /* Happens if loop module is not loaded */
  124. rc = -1;
  125. break;
  126. }
  127. goto try_again;
  128. }
  129. rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
  130. /* If device is free, claim it. */
  131. if (rc && errno == ENXIO) {
  132. /* Associate free loop device with file. */
  133. if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) {
  134. memset(&loopinfo, 0, sizeof(loopinfo));
  135. safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
  136. loopinfo.lo_offset = offset;
  137. /*
  138. * Used by mount to set LO_FLAGS_AUTOCLEAR.
  139. * LO_FLAGS_READ_ONLY is not set because RO is controlled by open type of the file.
  140. * Note that closing LO_FLAGS_AUTOCLEARed dfd before mount
  141. * is wrong (would free the loop device!)
  142. */
  143. loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
  144. rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
  145. if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
  146. /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
  147. /* (this code path is not tested) */
  148. loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
  149. rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
  150. }
  151. if (rc != 0) {
  152. ioctl(dfd, LOOP_CLR_FD, 0);
  153. }
  154. }
  155. } else {
  156. rc = -1;
  157. }
  158. if (rc != 0) {
  159. close(dfd);
  160. }
  161. try_again:
  162. if (*device) break;
  163. }
  164. close(ffd);
  165. if (rc == 0) {
  166. if (!*device)
  167. *device = xstrdup(dev);
  168. return dfd;
  169. }
  170. return rc;
  171. }