Require loop autoclear support.
[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
87         /* Verify that autoclear is really set */
88         memset(&lo64, 0, sizeof(lo64));
89         if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
90             !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
91                 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
92                 goto out;
93         }
94
95         r = 0;
96 out:
97         if (r && loop_fd >= 0)
98                 close(loop_fd);
99         if (file_fd >= 0)
100                 close(file_fd);
101         return r ? -1 : loop_fd;
102 }
103
104 char *crypt_loop_backing_file(const char *loop)
105 {
106         struct loop_info64 lo64 = {0};
107         int loop_fd;
108
109         loop_fd = open(loop, O_RDONLY);
110         if (loop_fd < 0)
111                 return NULL;
112
113         if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
114                 close(loop_fd);
115                 return NULL;
116         }
117
118         lo64.lo_file_name[LO_NAME_SIZE-2] = '*';
119         lo64.lo_file_name[LO_NAME_SIZE-1] = 0;
120
121         close(loop_fd);
122
123         return strdup((char*)lo64.lo_file_name);
124 }
125
126 int crypt_loop_device(const char *loop)
127 {
128         struct stat st;
129
130         if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
131             major(st.st_rdev) != LOOP_DEV_MAJOR)
132                 return 0;
133
134         return 1;
135 }