3
0

loop.c 3.9 KB

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