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
25 #include <sys/ioctl.h>
28 #include <sysmacros.h>
29 #include <asm/posix_types.h>
30 #include <linux/loop.h>
35 #ifndef LOOP_CTL_GET_FREE
36 #define LOOP_CTL_GET_FREE 0x4C82
39 #if !defined (__alpha__) && !defined (__ia64__) && !defined (__x86_64__) \
40 && !defined (__s390x__)
41 #define int2ptr(x) ((void *) ((int) x))
43 #define int2ptr(x) ((void *) ((long) x))
47 xstrdup (const char *s)
57 fprintf(stderr, "not enough memory");
65 is_loop_device (const char *device)
77 if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
79 while (fgets (line, sizeof(line), procdev)) {
81 if ((cp = strstr (line, " loop\n")) != NULL) {
91 return (loopmajor && stat(device, &statbuf) == 0 &&
92 S_ISBLK(statbuf.st_mode) &&
93 major(statbuf.st_rdev) == loopmajor);
96 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
99 find_loop_by_file (const char * filename)
102 char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
105 struct loop_info loopinfo;
107 for (j = 0; j < SIZE(loop_formats); j++) {
109 for (i = 0; i < 256; i++) {
110 sprintf (dev, loop_formats[j], i);
112 if (stat (dev, &statbuf) != 0 ||
113 !S_ISBLK(statbuf.st_mode))
116 fd = open (dev, O_RDONLY);
121 if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) != 0) {
126 if (0 == strcmp(filename, loopinfo.lo_name)) {
128 return xstrdup(dev); /*found */
139 find_unused_loop_device (void)
141 /* Just creating a device, say in /tmp, is probably a bad idea -
142 people might have problems with backup or so.
143 So, we just try /dev/loop[0-7]. */
146 char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
147 int i, j, fd, first = 0, somedev = 0, someloop = 0, loop_known = 0;
149 struct loop_info loopinfo;
152 if (stat("/dev/loop-control", &statbuf) == 0 &&
153 S_ISCHR(statbuf.st_mode)) {
154 fd = open("/dev/loop-control", O_RDWR);
156 first = ioctl(fd, LOOP_CTL_GET_FREE);
161 for (j = 0; j < SIZE(loop_formats); j++) {
163 for(i = first; i < 256; i++) {
164 sprintf(dev, loop_formats[j], i);
166 if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
168 fd = open (dev, O_RDONLY);
172 if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
173 someloop++; /* in use */
175 else if (errno == ENXIO) {
177 return xstrdup(dev);/* probably free */
183 /* continue trying as long as devices exist */
190 /* Nothing found. Why not? */
191 if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
194 while (fgets (line, sizeof(line), procdev))
196 if (strstr (line, " loop\n")) {
208 fprintf(stderr, "mount: could not find any device /dev/loop#");
210 else if (!someloop) {
214 "mount: Could not find any loop device.\n"
215 " Maybe /dev/loop# has a wrong major number?");
217 else if (loop_known == -1)
219 "mount: Could not find any loop device, and, according to %s,\n"
220 " this kernel does not know about the loop device.\n"
221 " (If so, then recompile or `modprobe loop'.)",
226 "mount: Could not find any loop device. Maybe this kernel does not know\n"
227 " about the loop device (then recompile or `modprobe loop'), or\n"
228 " maybe /dev/loop# has the wrong major number?");
231 fprintf(stderr, "mount: could not find any free loop device");
237 set_loop (const char *device, const char *file, int offset, int *loopro)
239 struct loop_info loopinfo;
242 mode = (*loopro ? O_RDONLY : O_RDWR);
244 if ((ffd = open (file, mode)) < 0) {
246 if (!*loopro && (errno == EROFS || errno == EACCES))
247 ffd = open (file, mode = O_RDONLY);
255 if ((fd = open (device, mode)) < 0) {
260 *loopro = (mode == O_RDONLY);
261 memset (&loopinfo, 0, sizeof (loopinfo));
263 xstrncpy (loopinfo.lo_name, file, LO_NAME_SIZE);
264 loopinfo.lo_offset = offset;
265 loopinfo.lo_encrypt_type = LO_CRYPT_NONE;
266 loopinfo.lo_encrypt_key_size = 0;
268 if (ioctl (fd, LOOP_SET_FD, int2ptr(ffd)) < 0) {
269 perror ("ioctl: LOOP_SET_FD");
275 if (ioctl (fd, LOOP_SET_STATUS, &loopinfo) < 0) {
276 (void) ioctl (fd, LOOP_CLR_FD, 0);
277 perror ("ioctl: LOOP_SET_STATUS");
289 del_loop (const char *device)
294 if ((fd = open (device, O_RDONLY)) < 0) {
296 fprintf(stderr, "loop: can't delete device %s: %s\n",
297 device, strerror (errsv));
301 while (ioctl (fd, LOOP_CLR_FD, 0) < 0) {
302 if (errno != EBUSY || retries-- <= 0) {
303 perror ("ioctl: LOOP_CLR_FD");
308 "loop: device %s still in use, retrying delete\n",