loop.c 3.9 KB

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