Start 1.33.0 development cycle
[oweals/busybox.git] / libbb / loop.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  * Copyright (C) 2005 by Rob Landley <rob@landley.net>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10 #include "libbb.h"
11 #include <linux/version.h>
12
13 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
14
15 /* For 2.6, use the cleaned up header to get the 64 bit API. */
16 // Commented out per Rob's request
17 //# include "fix_u32.h" /* some old toolchains need __u64 for linux/loop.h */
18 # include <linux/loop.h>
19 typedef struct loop_info64 bb_loop_info;
20 # define BB_LOOP_SET_STATUS LOOP_SET_STATUS64
21 # define BB_LOOP_GET_STATUS LOOP_GET_STATUS64
22
23 #else
24
25 /* For 2.4 and earlier, use the 32 bit API (and don't trust the headers) */
26 /* Stuff stolen from linux/loop.h for 2.4 and earlier kernels */
27 # include <linux/posix_types.h>
28 # define LO_NAME_SIZE        64
29 # define LO_KEY_SIZE         32
30 # define LOOP_SET_FD         0x4C00
31 # define LOOP_CLR_FD         0x4C01
32 # define BB_LOOP_SET_STATUS  0x4C02
33 # define BB_LOOP_GET_STATUS  0x4C03
34 typedef struct {
35         int                lo_number;
36         __kernel_dev_t     lo_device;
37         unsigned long      lo_inode;
38         __kernel_dev_t     lo_rdevice;
39         int                lo_offset;
40         int                lo_encrypt_type;
41         int                lo_encrypt_key_size;
42         int                lo_flags;
43         char               lo_file_name[LO_NAME_SIZE];
44         unsigned char      lo_encrypt_key[LO_KEY_SIZE];
45         unsigned long      lo_init[2];
46         char               reserved[4];
47 } bb_loop_info;
48 #endif
49
50 char* FAST_FUNC query_loop(const char *device)
51 {
52         int fd;
53         bb_loop_info loopinfo;
54         char *dev = NULL;
55
56         fd = open(device, O_RDONLY);
57         if (fd >= 0) {
58                 if (ioctl(fd, BB_LOOP_GET_STATUS, &loopinfo) == 0) {
59                         dev = xasprintf("%"OFF_FMT"u %s", (off_t) loopinfo.lo_offset,
60                                         (char *)loopinfo.lo_file_name);
61                 }
62                 close(fd);
63         }
64
65         return dev;
66 }
67
68 int FAST_FUNC del_loop(const char *device)
69 {
70         int fd, rc;
71
72         fd = open(device, O_RDONLY);
73         if (fd < 0)
74                 return 1;
75         rc = ioctl(fd, LOOP_CLR_FD, 0);
76         close(fd);
77
78         return rc;
79 }
80
81 /* Obtain an unused loop device number */
82 int FAST_FUNC get_free_loop(void)
83 {
84         int fd;
85         int loopdevno;
86
87         fd = open("/dev/loop-control", O_RDWR | O_CLOEXEC);
88         if (fd == -1)
89                 return fd - 1; /* -2: "no /dev/loop-control" */
90
91 #ifndef LOOP_CTL_GET_FREE
92 # define LOOP_CTL_GET_FREE 0x4C82
93 #endif
94         loopdevno = ioctl(fd, LOOP_CTL_GET_FREE);
95         close(fd);
96         return loopdevno; /* can be -1 if error */
97 }
98
99 /* Returns opened fd to the loop device, <0 on error.
100  * *device is loop device to use, or if *device==NULL finds a loop device to
101  * mount it on and sets *device to a strdup of that loop device name.  This
102  * search will re-use an existing loop device already bound to that
103  * file/offset if it finds one.
104  */
105 int FAST_FUNC set_loop(char **device, const char *file, unsigned long long offset, unsigned flags)
106 {
107         char dev[LOOP_NAMESIZE];
108         char *try;
109         bb_loop_info loopinfo;
110         struct stat statbuf;
111         int i, dfd, ffd, mode, rc;
112
113         rc = dfd = -1;
114
115         /* Open the file.  Barf if this doesn't work.  */
116         mode = (flags & BB_LO_FLAGS_READ_ONLY) ? O_RDONLY : O_RDWR;
117  open_ffd:
118         ffd = open(file, mode);
119         if (ffd < 0) {
120                 if (mode != O_RDONLY) {
121                         mode = O_RDONLY;
122                         goto open_ffd;
123                 }
124                 return -errno;
125         }
126
127         try = *device;
128         if (!try) {
129                 i = get_free_loop();
130                 if (i == -2) { /* no /dev/loop-control */
131                         i = 0;
132                         try = dev;
133                         goto old_style;
134                 }
135                 if (i == -1) {
136                         close(ffd);
137                         return -1; /* no free loop devices */
138                 }
139                 try = *device = xasprintf(LOOP_FORMAT, i);
140                 goto try_to_open;
141         }
142
143  old_style:
144         /* Find a loop device.  */
145         /* 1048575 (0xfffff) is a max possible minor number in Linux circa 2010 */
146         for (i = 0; rc && i < 1048576; i++) {
147                 sprintf(dev, LOOP_FORMAT, i);
148
149                 IF_FEATURE_MOUNT_LOOP_CREATE(errno = 0;)
150                 if (stat(try, &statbuf) != 0 || !S_ISBLK(statbuf.st_mode)) {
151                         if (ENABLE_FEATURE_MOUNT_LOOP_CREATE
152                          && errno == ENOENT
153                          && try == dev
154                         ) {
155                                 /* Node doesn't exist, try to create it.  */
156                                 if (mknod(dev, S_IFBLK|0644, makedev(7, i)) == 0)
157                                         goto try_to_open;
158                         }
159                         /* Ran out of block devices, return failure.  */
160                         rc = -1;
161                         break;
162                 }
163  try_to_open:
164                 /* Open the sucker and check its loopiness.  */
165                 dfd = open(try, mode);
166                 if (dfd < 0 && errno == EROFS) {
167                         mode = O_RDONLY;
168                         dfd = open(try, mode);
169                 }
170                 if (dfd < 0) {
171                         if (errno == ENXIO) {
172                                 /* Happens if loop module is not loaded */
173                                 rc = -1;
174                                 break;
175                         }
176                         goto try_again;
177                 }
178
179                 rc = ioctl(dfd, BB_LOOP_GET_STATUS, &loopinfo);
180
181                 /* If device is free, claim it.  */
182                 if (rc && errno == ENXIO) {
183                         /* Associate free loop device with file.  */
184                         if (ioctl(dfd, LOOP_SET_FD, ffd) == 0) {
185                                 memset(&loopinfo, 0, sizeof(loopinfo));
186                                 safe_strncpy((char *)loopinfo.lo_file_name, file, LO_NAME_SIZE);
187                                 loopinfo.lo_offset = offset;
188                                 /*
189                                  * Used by mount to set LO_FLAGS_AUTOCLEAR.
190                                  * LO_FLAGS_READ_ONLY is not set because RO is controlled by open type of the file.
191                                  * Note that closing LO_FLAGS_AUTOCLEARed dfd before mount
192                                  * is wrong (would free the loop device!)
193                                  */
194                                 loopinfo.lo_flags = (flags & ~BB_LO_FLAGS_READ_ONLY);
195                                 rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
196                                 if (rc != 0 && (loopinfo.lo_flags & BB_LO_FLAGS_AUTOCLEAR)) {
197                                         /* Old kernel, does not support LO_FLAGS_AUTOCLEAR? */
198                                         /* (this code path is not tested) */
199                                         loopinfo.lo_flags -= BB_LO_FLAGS_AUTOCLEAR;
200                                         rc = ioctl(dfd, BB_LOOP_SET_STATUS, &loopinfo);
201                                 }
202                                 if (rc != 0) {
203                                         ioctl(dfd, LOOP_CLR_FD, 0); // actually, 0 param is unnecessary
204                                 }
205                         }
206                 } else {
207                         rc = -1;
208                 }
209                 if (rc != 0) {
210                         close(dfd);
211                 }
212  try_again:
213                 if (*device) break;
214         }
215         close(ffd);
216         if (rc == 0) {
217                 if (!*device)
218                         *device = xstrdup(dev);
219                 return dfd;
220         }
221         return rc;
222 }