loop.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 0 if mounted RW, 1 if mounted read-only, <0 for 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, int ro)
  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 = -1;
  83. /* Open the file. Barf if this doesn't work. */
  84. mode = ro ? O_RDONLY : O_RDWR;
  85. open_ffd:
  86. ffd = open(file, mode);
  87. if (ffd < 0) {
  88. if (mode != O_RDONLY) {
  89. mode = O_RDONLY;
  90. goto open_ffd;
  91. }
  92. return -errno;
  93. }
  94. /* Find a loop device. */
  95. try = *device ? *device : dev;
  96. /* 1048575 (0xfffff) is a max possible minor number in Linux circa 2010 */
  97. for (i = 0; rc && i < 1048576; i++) {
  98. sprintf(dev, LOOP_FORMAT, i);
  99. IF_FEATURE_MOUNT_LOOP_CREATE(errno = 0;)
  100. if (stat(try, &statbuf) != 0 || !S_ISBLK(statbuf.st_mode)) {
  101. if (ENABLE_FEATURE_MOUNT_LOOP_CREATE
  102. && errno == ENOENT
  103. && try == dev
  104. ) {
  105. /* Node doesn't exist, try to create it. */
  106. if (mknod(dev, S_IFBLK|0644, makedev(7, i)) == 0)
  107. goto try_to_open;
  108. }
  109. /* Ran out of block devices, return failure. */
  110. rc = -1;
  111. break;
  112. }
  113. try_to_open:
  114. /* Open the sucker and check its loopiness. */
  115. dfd = open(try, mode);
  116. if (dfd < 0 && errno == EROFS) {
  117. mode = O_RDONLY;
  118. dfd = open(try, mode);
  119. }
  120. if (dfd < 0) {
  121. if (errno == ENXIO) {
  122. /* Happens if loop module is not loaded */
  123. rc = -1;
  124. break;
  125. }
  126. goto try_again;
  127. }
  128. rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
  129. /* If device is free, claim it. */
  130. if (rc && errno == ENXIO) {
  131. memset(&loopinfo, 0, sizeof(loopinfo));
  132. safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
  133. loopinfo.lo_offset = offset;
  134. /* Associate free loop device with file. */
  135. if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) {
  136. if (ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo) == 0)
  137. rc = 0;
  138. else
  139. ioctl(dfd, LOOP_CLR_FD, 0);
  140. }
  141. } else {
  142. rc = -1;
  143. }
  144. close(dfd);
  145. try_again:
  146. if (*device) break;
  147. }
  148. close(ffd);
  149. if (rc == 0) {
  150. if (!*device)
  151. *device = xstrdup(dev);
  152. return (mode == O_RDONLY); /* 1:ro, 0:rw */
  153. }
  154. return rc;
  155. }