loop.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 the GPL v2 or later, see the file LICENSE in this tarball.
  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. /* linux/loop.h relies on __u64. Make sure we have that as a proper type
  15. * until userspace is widely fixed. */
  16. # if (defined __INTEL_COMPILER && !defined __GNUC__) \
  17. || (defined __GNUC__ && defined __STRICT_ANSI__)
  18. __extension__ typedef long long __s64;
  19. __extension__ typedef unsigned long long __u64;
  20. # endif
  21. # include <linux/loop.h>
  22. typedef struct loop_info64 bb_loop_info;
  23. # define BB_LOOP_SET_STATUS LOOP_SET_STATUS64
  24. # define BB_LOOP_GET_STATUS LOOP_GET_STATUS64
  25. #else
  26. /* For 2.4 and earlier, use the 32 bit API (and don't trust the headers) */
  27. /* Stuff stolen from linux/loop.h for 2.4 and earlier kernels */
  28. # include <linux/posix_types.h>
  29. # define LO_NAME_SIZE 64
  30. # define LO_KEY_SIZE 32
  31. # define LOOP_SET_FD 0x4C00
  32. # define LOOP_CLR_FD 0x4C01
  33. # define BB_LOOP_SET_STATUS 0x4C02
  34. # define BB_LOOP_GET_STATUS 0x4C03
  35. typedef struct {
  36. int lo_number;
  37. __kernel_dev_t lo_device;
  38. unsigned long lo_inode;
  39. __kernel_dev_t lo_rdevice;
  40. int lo_offset;
  41. int lo_encrypt_type;
  42. int lo_encrypt_key_size;
  43. int lo_flags;
  44. char lo_file_name[LO_NAME_SIZE];
  45. unsigned char lo_encrypt_key[LO_KEY_SIZE];
  46. unsigned long lo_init[2];
  47. char reserved[4];
  48. } bb_loop_info;
  49. #endif
  50. char* FAST_FUNC query_loop(const char *device)
  51. {
  52. int fd;
  53. bb_loop_info loopinfo;
  54. char *dev = 0;
  55. fd = open(device, O_RDONLY);
  56. if (fd < 0) return 0;
  57. if (!ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo))
  58. dev = xasprintf("%ld %s", (long) loopinfo.lo_offset,
  59. (char *)loopinfo.lo_file_name);
  60. close(fd);
  61. return dev;
  62. }
  63. int FAST_FUNC del_loop(const char *device)
  64. {
  65. int fd, rc;
  66. fd = open(device, O_RDONLY);
  67. if (fd < 0) return 1;
  68. rc = ioctl(fd, LOOP_CLR_FD, 0);
  69. close(fd);
  70. return rc;
  71. }
  72. /* Returns 0 if mounted RW, 1 if mounted read-only, <0 for error.
  73. *device is loop device to use, or if *device==NULL finds a loop device to
  74. mount it on and sets *device to a strdup of that loop device name. This
  75. search will re-use an existing loop device already bound to that
  76. file/offset if it finds one.
  77. */
  78. int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset)
  79. {
  80. char dev[LOOP_NAMESIZE];
  81. char *try;
  82. bb_loop_info loopinfo;
  83. struct stat statbuf;
  84. int i, dfd, ffd, mode, rc = -1;
  85. /* Open the file. Barf if this doesn't work. */
  86. mode = O_RDWR;
  87. ffd = open(file, mode);
  88. if (ffd < 0) {
  89. mode = O_RDONLY;
  90. ffd = open(file, mode);
  91. if (ffd < 0)
  92. return -errno;
  93. }
  94. /* Find a loop device. */
  95. try = *device ? *device : dev;
  96. for (i = 0; rc; i++) {
  97. sprintf(dev, LOOP_FORMAT, i);
  98. /* Ran out of block devices, return failure. */
  99. if (stat(try, &statbuf) || !S_ISBLK(statbuf.st_mode)) {
  100. rc = -ENOENT;
  101. break;
  102. }
  103. /* Open the sucker and check its loopiness. */
  104. dfd = open(try, mode);
  105. if (dfd < 0 && errno == EROFS) {
  106. mode = O_RDONLY;
  107. dfd = open(try, mode);
  108. }
  109. if (dfd < 0)
  110. goto try_again;
  111. rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
  112. /* If device is free, claim it. */
  113. if (rc && errno == ENXIO) {
  114. memset(&loopinfo, 0, sizeof(loopinfo));
  115. safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
  116. loopinfo.lo_offset = offset;
  117. /* Associate free loop device with file. */
  118. if (!ioctl(dfd, LOOP_SET_FD, ffd)) {
  119. if (!ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo))
  120. rc = 0;
  121. else
  122. ioctl(dfd, LOOP_CLR_FD, 0);
  123. }
  124. /* If this block device already set up right, re-use it.
  125. (Yes this is racy, but associating two loop devices with the same
  126. file isn't pretty either. In general, mounting the same file twice
  127. without using losetup manually is problematic.)
  128. */
  129. } else if (strcmp(file, (char *)loopinfo.lo_file_name) != 0
  130. || offset != loopinfo.lo_offset) {
  131. rc = -1;
  132. }
  133. close(dfd);
  134. try_again:
  135. if (*device) break;
  136. }
  137. close(ffd);
  138. if (!rc) {
  139. if (!*device)
  140. *device = xstrdup(dev);
  141. return (mode == O_RDONLY); /* 1:ro, 0:rw */
  142. }
  143. return rc;
  144. }