1 /* Taken from Ted's losetup.c - Mitch <m.dsouza@mrc-apu.cam.ac.uk> */
2 /* Added vfs mount options - aeb - 960223 */
3 /* Removed lomount - aeb - 960224 */
5 /* 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
6 * - added Native Language Support
7 * Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
8 * - fixed strerr(errno) in gettext calls
11 #define PROC_DEVICES "/proc/devices"
14 * losetup.c - setup and control loop devices
26 #include <sys/ioctl.h>
29 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 #include <linux/loop.h>
38 #ifndef LOOP_CTL_GET_FREE
39 #define LOOP_CTL_GET_FREE 0x4C82
43 xstrdup (const char *s)
53 fprintf(stderr, "not enough memory");
60 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
62 char *find_loop_by_file(const char *filename)
66 char dev[64], *found = NULL, *p;
69 struct loop_info loopinfo;
70 const char VIRT_BLOCK[] = "/sys/devices/virtual/block";
73 dir = opendir(VIRT_BLOCK);
77 while ((dent = readdir(dir)) != NULL) {
78 if (strncmp(dent->d_name,"loop",4))
81 if (snprintf(path, PATH_MAX, "%s/%s/dev", VIRT_BLOCK,
82 dent->d_name) >= PATH_MAX)
85 fd = open(path, O_RDONLY);
89 if (read(fd, dev, sizeof(dev)) <= 0) {
96 dev[sizeof(dev)-1] = '\0';
97 p = strchr(dev, '\n');
100 if (snprintf(path, PATH_MAX, "/dev/block/%s", dev) >= PATH_MAX)
103 fd = open (path, O_RDONLY);
107 if (fstat (fd, &statbuf) != 0 ||
108 !S_ISBLK(statbuf.st_mode)) {
113 if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) != 0) {
120 if (0 == strcmp(filename, loopinfo.lo_name)) {
121 found = realpath(path, NULL);
129 char *find_unused_loop_device(void)
131 char dev[20], *next_loop_dev = NULL;
132 int fd, next_loop = 0, somedev = 0, someloop = 0, loop_known = 0;
134 struct loop_info loopinfo;
137 while (next_loop_dev == NULL) {
138 if (stat("/dev/loop-control", &statbuf) == 0 &&
139 S_ISCHR(statbuf.st_mode)) {
142 next_loop_fd = open("/dev/loop-control", O_RDWR);
143 if (next_loop_fd < 0)
145 next_loop = ioctl(next_loop_fd, LOOP_CTL_GET_FREE);
151 sprintf(dev, "/dev/loop%d", next_loop);
153 fd = open (dev, O_RDONLY);
155 if (fstat (fd, &statbuf) == 0 &&
156 S_ISBLK(statbuf.st_mode)) {
158 if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
159 someloop++; /* in use */
160 else if (errno == ENXIO)
161 next_loop_dev = xstrdup(dev);
166 /* continue trying as long as devices exist */
172 return next_loop_dev;
174 /* Nothing found. Why not? */
175 if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
178 while (fgets (line, sizeof(line), procdev))
180 if (strstr (line, " loop\n")) {
192 fprintf(stderr, "mount: could not find any device /dev/loop#");
194 else if (!someloop) {
197 "mount: Could not find any loop device.\n"
198 " Maybe /dev/loop# has a wrong major number?");
199 else if (loop_known == -1)
201 "mount: Could not find any loop device, and, according to %s,\n"
202 " this kernel does not know about the loop device.\n"
203 " (If so, then recompile or `modprobe loop'.)",
207 "mount: Could not find any loop device. Maybe this kernel does not know\n"
208 " about the loop device (then recompile or `modprobe loop'), or\n"
209 " maybe /dev/loop# has the wrong major number?");
211 fprintf(stderr, "mount: could not find any free loop device");
215 int set_loop(const char *device, const char *file, int offset, int *loopro)
217 struct loop_info loopinfo;
220 mode = (*loopro ? O_RDONLY : O_RDWR);
222 if ((ffd = open (file, mode)) < 0) {
224 if (!*loopro && (errno == EROFS || errno == EACCES))
225 ffd = open (file, mode = O_RDONLY);
233 if ((fd = open (device, mode)) < 0) {
239 *loopro = (mode == O_RDONLY);
240 memset (&loopinfo, 0, sizeof (loopinfo));
242 xstrncpy (loopinfo.lo_name, file, LO_NAME_SIZE);
243 loopinfo.lo_offset = offset;
244 loopinfo.lo_encrypt_type = LO_CRYPT_NONE;
245 loopinfo.lo_encrypt_key_size = 0;
247 if (ioctl(fd, LOOP_SET_FD, (void*)(uintptr_t)(ffd)) < 0) {
248 perror ("ioctl: LOOP_SET_FD");
254 if (ioctl (fd, LOOP_SET_STATUS, &loopinfo) < 0) {
255 (void) ioctl (fd, LOOP_CLR_FD, 0);
256 perror ("ioctl: LOOP_SET_STATUS");
267 int del_loop(const char *device)
272 if ((fd = open (device, O_RDONLY)) < 0) {
274 fprintf(stderr, "loop: can't delete device %s: %s\n",
275 device, strerror (errsv));
279 while (ioctl (fd, LOOP_CLR_FD, 0) < 0) {
280 if (errno != EBUSY || retries-- <= 0) {
281 perror ("ioctl: LOOP_CLR_FD");
286 "loop: device %s still in use, retrying delete\n",