3
0

loop.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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)
  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 = O_RDWR;
  85. ffd = open(file, mode);
  86. if (ffd < 0) {
  87. mode = O_RDONLY;
  88. ffd = open(file, mode);
  89. if (ffd < 0)
  90. return -errno;
  91. }
  92. /* Find a loop device. */
  93. try = *device ? *device : dev;
  94. /* 1048575 is a max possible minor number in Linux circa 2010 */
  95. for (i = 0; rc && i < 1048576; i++) {
  96. sprintf(dev, LOOP_FORMAT, i);
  97. IF_FEATURE_MOUNT_LOOP_CREATE(errno = 0;)
  98. if (stat(try, &statbuf) != 0 || !S_ISBLK(statbuf.st_mode)) {
  99. if (ENABLE_FEATURE_MOUNT_LOOP_CREATE
  100. && errno == ENOENT
  101. && try == dev
  102. ) {
  103. /* Node doesn't exist, try to create it. */
  104. if (mknod(dev, S_IFBLK|0644, makedev(7, i)) == 0)
  105. goto try_to_open;
  106. }
  107. /* Ran out of block devices, return failure. */
  108. rc = -ENOENT;
  109. break;
  110. }
  111. try_to_open:
  112. /* Open the sucker and check its loopiness. */
  113. dfd = open(try, mode);
  114. if (dfd < 0 && errno == EROFS) {
  115. mode = O_RDONLY;
  116. dfd = open(try, mode);
  117. }
  118. if (dfd < 0)
  119. goto try_again;
  120. rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
  121. /* If device is free, claim it. */
  122. if (rc && errno == ENXIO) {
  123. memset(&loopinfo, 0, sizeof(loopinfo));
  124. safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
  125. loopinfo.lo_offset = offset;
  126. /* Associate free loop device with file. */
  127. if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) {
  128. if (ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo) == 0)
  129. rc = 0;
  130. else
  131. ioctl(dfd, LOOP_CLR_FD, 0);
  132. }
  133. /* If this block device already set up right, re-use it.
  134. (Yes this is racy, but associating two loop devices with the same
  135. file isn't pretty either. In general, mounting the same file twice
  136. without using losetup manually is problematic.)
  137. */
  138. } else
  139. if (strcmp(file, (char *)loopinfo.lo_file_name) != 0
  140. || offset != loopinfo.lo_offset
  141. ) {
  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. }