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 <sys/types.h>
30 #include "sysmacros.h"
31 #include <linux/loop.h>
36 #ifndef LOOP_CTL_GET_FREE
37 #define LOOP_CTL_GET_FREE 0x4C82
40 #if !defined (__alpha__) && !defined (__ia64__) && !defined (__x86_64__) \
41 && !defined (__s390x__)
42 #define int2ptr(x) ((void *) ((int) x))
44 #define int2ptr(x) ((void *) ((long) x))
48 xstrdup (const char *s)
58 fprintf(stderr, "not enough memory");
65 int 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]))
98 char *find_loop_by_file(const char *filename)
102 char dev[64], *found = NULL;
105 struct loop_info loopinfo;
107 dir = opendir("/dev");
111 while ((dent = readdir(dir)) != NULL) {
112 if (strncmp(dent->d_name,"loop",4))
114 if (!strcmp(dent->d_name, "loop-control"))
116 sprintf(dev, "/dev/%s", dent->d_name);
118 fd = open (dev, O_RDONLY);
122 if (fstat (fd, &statbuf) != 0 ||
123 !S_ISBLK(statbuf.st_mode)) {
128 if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) != 0) {
133 if (0 == strcmp(filename, loopinfo.lo_name)) {
135 found = xstrdup(dev);
145 char *find_unused_loop_device(void)
147 char dev[20], *next_loop_dev = NULL;
148 int fd, next_loop = 0, somedev = 0, someloop = 0, loop_known = 0;
150 struct loop_info loopinfo;
153 while (next_loop_dev == NULL) {
154 if (stat("/dev/loop-control", &statbuf) == 0 &&
155 S_ISCHR(statbuf.st_mode)) {
158 next_loop_fd = open("/dev/loop-control", O_RDWR);
159 if (next_loop_fd < 0)
161 next_loop = ioctl(next_loop_fd, LOOP_CTL_GET_FREE);
167 sprintf(dev, "/dev/loop%d", next_loop);
169 fd = open (dev, O_RDONLY);
171 if (fstat (fd, &statbuf) == 0 &&
172 S_ISBLK(statbuf.st_mode)) {
174 if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
175 someloop++; /* in use */
176 else if (errno == ENXIO)
177 next_loop_dev = xstrdup(dev);
182 /* continue trying as long as devices exist */
188 return next_loop_dev;
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) {
213 "mount: Could not find any loop device.\n"
214 " 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 `modprobe loop'.)",
223 "mount: Could not find any loop device. Maybe this kernel does not know\n"
224 " about the loop device (then recompile or `modprobe loop'), or\n"
225 " maybe /dev/loop# has the wrong major number?");
227 fprintf(stderr, "mount: could not find any free loop device");
231 int set_loop(const char *device, const char *file, int offset, int *loopro)
233 struct loop_info loopinfo;
236 mode = (*loopro ? O_RDONLY : O_RDWR);
238 if ((ffd = open (file, mode)) < 0) {
240 if (!*loopro && (errno == EROFS || errno == EACCES))
241 ffd = open (file, mode = O_RDONLY);
249 if ((fd = open (device, mode)) < 0) {
255 *loopro = (mode == O_RDONLY);
256 memset (&loopinfo, 0, sizeof (loopinfo));
258 xstrncpy (loopinfo.lo_name, file, LO_NAME_SIZE);
259 loopinfo.lo_offset = offset;
260 loopinfo.lo_encrypt_type = LO_CRYPT_NONE;
261 loopinfo.lo_encrypt_key_size = 0;
263 if (ioctl (fd, LOOP_SET_FD, int2ptr(ffd)) < 0) {
264 perror ("ioctl: LOOP_SET_FD");
270 if (ioctl (fd, LOOP_SET_STATUS, &loopinfo) < 0) {
271 (void) ioctl (fd, LOOP_CLR_FD, 0);
272 perror ("ioctl: LOOP_SET_STATUS");
283 int del_loop(const char *device)
288 if ((fd = open (device, O_RDONLY)) < 0) {
290 fprintf(stderr, "loop: can't delete device %s: %s\n",
291 device, strerror (errsv));
295 while (ioctl (fd, LOOP_CLR_FD, 0) < 0) {
296 if (errno != EBUSY || retries-- <= 0) {
297 perror ("ioctl: LOOP_CLR_FD");
302 "loop: device %s still in use, retrying delete\n",