Separate loop handling from internal code.
[platform/upstream/cryptsetup.git] / lib / utils_loop.c
1 /*
2  * loopback block device utilities
3  *
4  * Copyright (C) 2011, Red Hat, Inc. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/stat.h>
27 #include <linux/loop.h>
28
29 #include "utils_loop.h"
30
31 char *crypt_loop_get_device(void)
32 {
33         char dev[20];
34         int i, loop_fd;
35         struct stat st;
36         struct loop_info64 lo64 = {0};
37
38         for(i = 0; i < 256; i++) {
39                 sprintf(dev, "/dev/loop%d", i);
40                 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
41                         return NULL;
42
43                 loop_fd = open(dev, O_RDONLY);
44                 if (loop_fd < 0)
45                         return NULL;
46
47                 if(ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) && errno == ENXIO) {
48                         close(loop_fd);
49                         return strdup(dev);
50                 }
51                 close(loop_fd);
52         }
53
54         return NULL;
55 }
56
57 int crypt_loop_attach(const char *loop, const char *file,
58                       int offset, int *readonly)
59 {
60         struct loop_info64 lo64 = {0};
61         int loop_fd = -1, file_fd = -1, r = 1;
62
63         file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
64         if (file_fd < 0 && errno == EROFS && !*readonly) {
65                 *readonly = 1;
66                 file_fd = open(file, O_RDONLY | O_EXCL);
67         }
68         if (file_fd < 0)
69                 goto out;
70
71         loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR);
72         if (loop_fd < 0)
73                 goto out;
74
75         strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE);
76         lo64.lo_offset = offset;
77         lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
78
79         if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0)
80                 goto out;
81
82         if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
83                 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
84                 goto out;
85         }
86         r = 0;
87 out:
88         if (r && loop_fd >= 0)
89                 close(loop_fd);
90         if (file_fd >= 0)
91                 close(file_fd);
92         return r ? -1 : loop_fd;
93 }
94
95 char *crypt_loop_backing_file(const char *loop)
96 {
97         struct loop_info64 lo64 = {0};
98         int loop_fd;
99
100         loop_fd = open(loop, O_RDONLY);
101         if (loop_fd < 0)
102                 return NULL;
103
104         if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
105                 close(loop_fd);
106                 return NULL;
107         }
108
109         lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
110         lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
111
112         close(loop_fd);
113
114         return strdup((char*)lo64.lo_file_name);
115 }
116
117 int crypt_loop_device(const char *loop)
118 {
119         struct stat st;
120
121         if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
122             major(st.st_rdev) != LOOP_DEV_MAJOR)
123                 return 0;
124
125         return 1;
126 }