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>
30 #if defined(__hppa__) || defined(__powerpc64__) || defined (__alpha__) \
31 || defined (__x86_64__)
32 typedef unsigned long __kernel_old_dev_t;
33 #elif defined(__powerpc__) || defined(__ia64__)
34 typedef unsigned int __kernel_old_dev_t;
36 typedef unsigned short __kernel_old_dev_t;
39 #define dev_t __kernel_old_dev_t
41 #include <linux/loop.h>
46 #if !defined (__alpha__) && !defined (__ia64__) && !defined (__x86_64__) \
47 && !defined (__s390x__)
48 #define int2ptr(x) ((void *) ((int) x))
50 #define int2ptr(x) ((void *) ((long) x))
54 xstrdup (const char *s)
64 fprintf(stderr, "not enough memory");
72 is_loop_device (const char *device)
84 if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
86 while (fgets (line, sizeof(line), procdev)) {
88 if ((cp = strstr (line, " loop\n")) != NULL) {
98 return (loopmajor && stat(device, &statbuf) == 0 &&
99 S_ISBLK(statbuf.st_mode) &&
100 major(statbuf.st_rdev) == loopmajor);
103 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
106 find_loop_by_file (const char * filename)
109 char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
112 struct loop_info loopinfo;
114 for (j = 0; j < SIZE(loop_formats); j++) {
116 for (i = 0; i < 256; i++) {
117 sprintf (dev, loop_formats[j], i);
119 if (stat (dev, &statbuf) != 0 ||
120 !S_ISBLK(statbuf.st_mode))
123 fd = open (dev, O_RDONLY);
128 if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) != 0) {
133 if (0 == strcmp(filename, loopinfo.lo_name)) {
135 return xstrdup(dev); /*found */
146 find_unused_loop_device (void)
148 /* Just creating a device, say in /tmp, is probably a bad idea -
149 people might have problems with backup or so.
150 So, we just try /dev/loop[0-7]. */
153 char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
154 int i, j, fd, somedev = 0, someloop = 0, loop_known = 0;
156 struct loop_info loopinfo;
159 for (j = 0; j < SIZE(loop_formats); j++) {
161 for(i = 0; i < 256; i++) {
162 sprintf(dev, loop_formats[j], i);
164 if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
166 fd = open (dev, O_RDONLY);
170 if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
171 someloop++; /* in use */
173 else if (errno == ENXIO) {
175 return xstrdup(dev);/* probably free */
181 /* continue trying as long as devices exist */
188 /* Nothing found. Why not? */
189 if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
192 while (fgets (line, sizeof(line), procdev))
194 if (strstr (line, " loop\n")) {
206 fprintf(stderr, "mount: could not find any device /dev/loop#");
208 else if (!someloop) {
212 "mount: Could not find any loop device.\n"
213 " Maybe /dev/loop# has a wrong major number?");
215 else if (loop_known == -1)
217 "mount: Could not find any loop device, and, according to %s,\n"
218 " this kernel does not know about the loop device.\n"
219 " (If so, then recompile or `insmod loop.o'.)",
224 "mount: Could not find any loop device. Maybe this kernel does not know\n"
225 " about the loop device (then recompile or `insmod loop.o'), or\n"
226 " maybe /dev/loop# has the wrong major number?");
229 fprintf(stderr, "mount: could not find any free loop device");
235 set_loop (const char *device, const char *file, int offset, int *loopro)
237 struct loop_info loopinfo;
240 mode = (*loopro ? O_RDONLY : O_RDWR);
242 if ((ffd = open (file, mode)) < 0) {
244 if (!*loopro && errno == EROFS)
245 ffd = open (file, mode = O_RDONLY);
253 if ((fd = open (device, mode)) < 0) {
258 *loopro = (mode == O_RDONLY);
259 memset (&loopinfo, 0, sizeof (loopinfo));
261 xstrncpy (loopinfo.lo_name, file, LO_NAME_SIZE);
262 loopinfo.lo_offset = offset;
263 loopinfo.lo_encrypt_type = LO_CRYPT_NONE;
264 loopinfo.lo_encrypt_key_size = 0;
266 if (ioctl (fd, LOOP_SET_FD, int2ptr(ffd)) < 0) {
267 perror ("ioctl: LOOP_SET_FD");
273 if (ioctl (fd, LOOP_SET_STATUS, &loopinfo) < 0) {
274 (void) ioctl (fd, LOOP_CLR_FD, 0);
275 perror ("ioctl: LOOP_SET_STATUS");
287 del_loop (const char *device)
291 if ((fd = open (device, O_RDONLY)) < 0) {
293 fprintf(stderr, "loop: can't delete device %s: %s\n",
294 device, strerror (errsv));
298 if (ioctl (fd, LOOP_CLR_FD, 0) < 0) {
299 perror ("ioctl: LOOP_CLR_FD");