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 <linux/loop.h>
34 #if !defined (__alpha__) && !defined (__ia64__) && !defined (__x86_64__) \
35 && !defined (__s390x__)
36 #define int2ptr(x) ((void *) ((int) x))
38 #define int2ptr(x) ((void *) ((long) x))
42 xstrdup (const char *s)
52 fprintf(stderr, "not enough memory");
60 is_loop_device (const char *device)
72 if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
74 while (fgets (line, sizeof(line), procdev)) {
76 if ((cp = strstr (line, " loop\n")) != NULL) {
86 return (loopmajor && stat(device, &statbuf) == 0 &&
87 S_ISBLK(statbuf.st_mode) &&
88 major(statbuf.st_rdev) == loopmajor);
91 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
94 find_loop_by_file (const char * filename)
97 char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
100 struct loop_info loopinfo;
102 for (j = 0; j < SIZE(loop_formats); j++) {
104 for (i = 0; i < 256; i++) {
105 sprintf (dev, loop_formats[j], i);
107 if (stat (dev, &statbuf) != 0 ||
108 !S_ISBLK(statbuf.st_mode))
111 fd = open (dev, O_RDONLY);
116 if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) != 0) {
121 if (0 == strcmp(filename, loopinfo.lo_name)) {
123 return xstrdup(dev); /*found */
134 find_unused_loop_device (void)
136 /* Just creating a device, say in /tmp, is probably a bad idea -
137 people might have problems with backup or so.
138 So, we just try /dev/loop[0-7]. */
141 char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
142 int i, j, fd, somedev = 0, someloop = 0, loop_known = 0;
144 struct loop_info loopinfo;
147 for (j = 0; j < SIZE(loop_formats); j++) {
149 for(i = 0; i < 256; i++) {
150 sprintf(dev, loop_formats[j], i);
152 if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
154 fd = open (dev, O_RDONLY);
158 if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
159 someloop++; /* in use */
161 else if (errno == ENXIO) {
163 return xstrdup(dev);/* probably free */
169 /* continue trying as long as devices exist */
176 /* Nothing found. Why not? */
177 if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
180 while (fgets (line, sizeof(line), procdev))
182 if (strstr (line, " loop\n")) {
194 fprintf(stderr, "mount: could not find any device /dev/loop#");
196 else if (!someloop) {
200 "mount: Could not find any loop device.\n"
201 " Maybe /dev/loop# has a wrong major number?");
203 else if (loop_known == -1)
205 "mount: Could not find any loop device, and, according to %s,\n"
206 " this kernel does not know about the loop device.\n"
207 " (If so, then recompile or `insmod loop.o'.)",
212 "mount: Could not find any loop device. Maybe this kernel does not know\n"
213 " about the loop device (then recompile or `insmod loop.o'), or\n"
214 " maybe /dev/loop# has the wrong major number?");
217 fprintf(stderr, "mount: could not find any free loop device");
223 set_loop (const char *device, const char *file, int offset, int *loopro)
225 struct loop_info loopinfo;
228 mode = (*loopro ? O_RDONLY : O_RDWR);
230 if ((ffd = open (file, mode)) < 0) {
232 if (!*loopro && errno == EROFS)
233 ffd = open (file, mode = O_RDONLY);
241 if ((fd = open (device, mode)) < 0) {
246 *loopro = (mode == O_RDONLY);
247 memset (&loopinfo, 0, sizeof (loopinfo));
249 xstrncpy (loopinfo.lo_name, file, LO_NAME_SIZE);
250 loopinfo.lo_offset = offset;
251 loopinfo.lo_encrypt_type = LO_CRYPT_NONE;
252 loopinfo.lo_encrypt_key_size = 0;
254 if (ioctl (fd, LOOP_SET_FD, int2ptr(ffd)) < 0) {
255 perror ("ioctl: LOOP_SET_FD");
261 if (ioctl (fd, LOOP_SET_STATUS, &loopinfo) < 0) {
262 (void) ioctl (fd, LOOP_CLR_FD, 0);
263 perror ("ioctl: LOOP_SET_STATUS");
275 del_loop (const char *device)
279 if ((fd = open (device, O_RDONLY)) < 0) {
281 fprintf(stderr, "loop: can't delete device %s: %s\n",
282 device, strerror (errsv));
286 if (ioctl (fd, LOOP_CLR_FD, 0) < 0) {
287 perror ("ioctl: LOOP_CLR_FD");