Imported Upstream version 0.7.6
[platform/upstream/multipath-tools.git] / kpartx / lopart.c
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 */
4
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
9  */
10
11 #define PROC_DEVICES    "/proc/devices"
12
13 /*
14  * losetup.c - setup and control loop devices
15  */
16
17 #include "kpartx.h"
18 #include <stdio.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <unistd.h>
26 #include <sys/ioctl.h>
27 #include <sys/stat.h>
28 #include <sys/mman.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31 #include <sys/sysmacros.h>
32 #include <linux/loop.h>
33 #include <limits.h>
34
35 #include "lopart.h"
36 #include "xstrncpy.h"
37
38 #ifndef LOOP_CTL_GET_FREE
39 #define LOOP_CTL_GET_FREE       0x4C82
40 #endif
41
42 static char *
43 xstrdup (const char *s)
44 {
45         char *t;
46
47         if (s == NULL)
48                 return NULL;
49
50         t = strdup (s);
51
52         if (t == NULL) {
53                 fprintf(stderr, "not enough memory");
54                 exit(1);
55         }
56
57         return t;
58 }
59
60 #define SIZE(a) (sizeof(a)/sizeof(a[0]))
61
62 char *find_loop_by_file(const char *filename)
63 {
64         DIR *dir;
65         struct dirent *dent;
66         char dev[64], *found = NULL, *p;
67         int fd, bytes_read;
68         struct stat statbuf;
69         struct loop_info loopinfo;
70         const char VIRT_BLOCK[] = "/sys/devices/virtual/block";
71         char path[PATH_MAX];
72
73         dir = opendir(VIRT_BLOCK);
74         if (!dir)
75                 return NULL;
76
77         while ((dent = readdir(dir)) != NULL) {
78                 if (strncmp(dent->d_name,"loop",4))
79                         continue;
80
81                 if (snprintf(path, PATH_MAX, "%s/%s/dev", VIRT_BLOCK,
82                              dent->d_name) >= PATH_MAX)
83                         continue;
84
85                 fd = open(path, O_RDONLY);
86                 if (fd < 0)
87                         continue;
88
89                 bytes_read = read(fd, dev, sizeof(dev) - 1);
90                 if (bytes_read <= 0) {
91                         close(fd);
92                         continue;
93                 }
94
95                 close(fd);
96
97                 dev[bytes_read] = '\0';
98                 p = strchr(dev, '\n');
99                 if (p != NULL)
100                         *p = '\0';
101                 if (snprintf(path, PATH_MAX, "/dev/block/%s", dev) >= PATH_MAX)
102                         continue;
103
104                 fd = open (path, O_RDONLY);
105                 if (fd < 0)
106                         continue;
107
108                 if (fstat (fd, &statbuf) != 0 ||
109                     !S_ISBLK(statbuf.st_mode)) {
110                         close (fd);
111                         continue;
112                 }
113
114                 if (ioctl (fd, LOOP_GET_STATUS, &loopinfo) != 0) {
115                         close (fd);
116                         continue;
117                 }
118
119                 close (fd);
120
121                 if (0 == strcmp(filename, loopinfo.lo_name)) {
122                         found = realpath(path, NULL);
123                         break;
124                 }
125         }
126         closedir(dir);
127         return found;
128 }
129
130 char *find_unused_loop_device(void)
131 {
132         char dev[20], *next_loop_dev = NULL;
133         int fd, next_loop = 0, somedev = 0, someloop = 0, loop_known = 0;
134         struct stat statbuf;
135         struct loop_info loopinfo;
136         FILE *procdev;
137
138         while (next_loop_dev == NULL) {
139                 if (stat("/dev/loop-control", &statbuf) == 0 &&
140                     S_ISCHR(statbuf.st_mode)) {
141                         int next_loop_fd;
142
143                         next_loop_fd = open("/dev/loop-control", O_RDWR);
144                         if (next_loop_fd < 0)
145                                 return NULL;
146                         next_loop = ioctl(next_loop_fd, LOOP_CTL_GET_FREE);
147                         close(next_loop_fd);
148                         if (next_loop < 0)
149                                 return NULL;
150                 }
151
152                 sprintf(dev, "/dev/loop%d", next_loop);
153
154                 fd = open (dev, O_RDONLY);
155                 if (fd >= 0) {
156                         if (fstat (fd, &statbuf) == 0 &&
157                             S_ISBLK(statbuf.st_mode)) {
158                                 somedev++;
159                                 if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
160                                         someloop++;             /* in use */
161                                 else if (errno == ENXIO)
162                                         next_loop_dev = xstrdup(dev);
163
164                         }
165                         close (fd);
166
167                         /* continue trying as long as devices exist */
168                         continue;
169                 }
170                 break;
171         }
172         if (next_loop_dev)
173                 return next_loop_dev;
174
175         /* Nothing found. Why not? */
176         if ((procdev = fopen(PROC_DEVICES, "r")) != NULL) {
177                 char line[100];
178
179                 while (fgets (line, sizeof(line), procdev))
180
181                         if (strstr (line, " loop\n")) {
182                                 loop_known = 1;
183                                 break;
184                         }
185
186                 fclose(procdev);
187
188                 if (!loop_known)
189                         loop_known = -1;
190         }
191
192         if (!somedev)
193                 fprintf(stderr, "mount: could not find any device /dev/loop#");
194
195         else if (!someloop) {
196                 if (loop_known == 1)
197                         fprintf(stderr,
198                                 "mount: Could not find any loop device.\n"
199                                 "       Maybe /dev/loop# has a wrong major number?");
200                 else if (loop_known == -1)
201                         fprintf(stderr,
202                                 "mount: Could not find any loop device, and, according to %s,\n"
203                                 "       this kernel does not know about the loop device.\n"
204                                 "       (If so, then recompile or `modprobe loop'.)",
205                                 PROC_DEVICES);
206                 else
207                         fprintf(stderr,
208                                 "mount: Could not find any loop device. Maybe this kernel does not know\n"
209                                 "       about the loop device (then recompile or `modprobe loop'), or\n"
210                                 "       maybe /dev/loop# has the wrong major number?");
211         } else
212                 fprintf(stderr, "mount: could not find any free loop device");
213         return NULL;
214 }
215
216 int set_loop(const char *device, const char *file, int offset, int *loopro)
217 {
218         struct loop_info loopinfo;
219         int fd, ffd, mode;
220
221         mode = (*loopro ? O_RDONLY : O_RDWR);
222
223         if ((ffd = open (file, mode)) < 0) {
224
225                 if (!*loopro && (errno == EROFS || errno == EACCES))
226                         ffd = open (file, mode = O_RDONLY);
227
228                 if (ffd < 0) {
229                         perror (file);
230                         return 1;
231                 }
232         }
233
234         if ((fd = open (device, mode)) < 0) {
235                 close(ffd);
236                 perror (device);
237                 return 1;
238         }
239
240         *loopro = (mode == O_RDONLY);
241         memset (&loopinfo, 0, sizeof (loopinfo));
242
243         xstrncpy (loopinfo.lo_name, file, LO_NAME_SIZE);
244         loopinfo.lo_offset = offset;
245         loopinfo.lo_encrypt_type = LO_CRYPT_NONE;
246         loopinfo.lo_encrypt_key_size = 0;
247
248         if (ioctl(fd, LOOP_SET_FD, (void*)(uintptr_t)(ffd)) < 0) {
249                 perror ("ioctl: LOOP_SET_FD");
250                 close (fd);
251                 close (ffd);
252                 return 1;
253         }
254
255         if (ioctl (fd, LOOP_SET_STATUS, &loopinfo) < 0) {
256                 (void) ioctl (fd, LOOP_CLR_FD, 0);
257                 perror ("ioctl: LOOP_SET_STATUS");
258                 close (fd);
259                 close (ffd);
260                 return 1;
261         }
262
263         close (fd);
264         close (ffd);
265         return 0;
266 }
267
268 int del_loop(const char *device)
269 {
270         int retries = 5;
271         int fd;
272
273         if ((fd = open (device, O_RDONLY)) < 0) {
274                 int errsv = errno;
275                 fprintf(stderr, "loop: can't delete device %s: %s\n",
276                         device, strerror (errsv));
277                 return 1;
278         }
279
280         while (ioctl (fd, LOOP_CLR_FD, 0) < 0) {
281                 if (errno != EBUSY || retries-- <= 0) {
282                         perror ("ioctl: LOOP_CLR_FD");
283                         close (fd);
284                         return 1;
285                 }
286                 fprintf(stderr,
287                         "loop: device %s still in use, retrying delete\n",
288                         device);
289                 sleep(1);
290                 continue;
291         }
292
293         close (fd);
294         return 0;
295 }