Change License from GPLv2 only to GPLv2+ ("or any later").
[platform/upstream/cryptsetup.git] / lib / utils_device.c
1 /*
2  * device backend utilities
3  *
4  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2012, Milan Broz
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <linux/fs.h>
32 #include <unistd.h>
33 #include "internal.h"
34
35 struct device {
36         char *path;
37
38         char *file_path;
39         int loop_fd;
40
41         int init_done:1;
42 };
43
44 static int device_ready(const char *device)
45 {
46         int devfd, r = 0;
47         struct stat st;
48
49         log_dbg("Trying to open and read device %s.", device);
50         devfd = open(device, O_RDONLY | O_DIRECT | O_SYNC);
51         if (devfd < 0) {
52                 log_err(NULL, _("Device %s doesn't exist or access denied.\n"), device);
53                 return -EINVAL;
54         }
55         if (fstat(devfd, &st) < 0)
56                 r = -EINVAL;
57         else if (!S_ISBLK(st.st_mode))
58                 r = S_ISREG(st.st_mode) ? -ENOTBLK : -EINVAL;
59
60         close(devfd);
61         return r;
62 }
63
64 int device_alloc(struct device **device, const char *path)
65 {
66         struct device *dev;
67         int r;
68
69         if (!path) {
70                 *device = NULL;
71                 return 0;
72         }
73
74         dev = malloc(sizeof(struct device));
75         if (!dev)
76                 return -ENOMEM;
77
78         memset(dev, 0, sizeof(struct device));
79         dev->loop_fd = -1;
80
81         r = device_ready(path);
82         if (!r) {
83                 dev->init_done = 1;
84         } else if (r == -ENOTBLK) {
85                 /* alloc loop later */
86         } else if (r < 0) {
87                 free(dev);
88                 return -ENOTBLK;
89         }
90
91         dev->path = strdup(path);
92         if (!dev->path) {
93                 free(dev);
94                 return -ENOMEM;
95         }
96
97         *device = dev;
98         return 0;
99 }
100
101 void device_free(struct device *device)
102 {
103         if (!device)
104                 return;
105
106         if (device->loop_fd != -1) {
107                 log_dbg("Closed loop %s (%s).", device->path, device->file_path);
108                 close(device->loop_fd);
109         }
110
111         free(device->file_path);
112         free(device->path);
113         free(device);
114 }
115
116 /* Get block device path */
117 const char *device_block_path(const struct device *device)
118 {
119         if (!device || !device->init_done)
120                 return NULL;
121
122         return device->path;
123 }
124
125 /* Get path to device / file */
126 const char *device_path(const struct device *device)
127 {
128         if (!device)
129                 return NULL;
130
131         if (device->file_path)
132                 return device->file_path;
133
134         return device->path;
135 }
136
137 /* block device topology ioctls, introduced in 2.6.32 */
138 #ifndef BLKIOMIN
139 #define BLKIOMIN    _IO(0x12,120)
140 #define BLKIOOPT    _IO(0x12,121)
141 #define BLKALIGNOFF _IO(0x12,122)
142 #endif
143
144 void device_topology_alignment(struct device *device,
145                             unsigned long *required_alignment, /* bytes */
146                             unsigned long *alignment_offset,   /* bytes */
147                             unsigned long default_alignment)
148 {
149         int dev_alignment_offset = 0;
150         unsigned int min_io_size = 0, opt_io_size = 0;
151         unsigned long temp_alignment = 0;
152         int fd;
153
154         *required_alignment = default_alignment;
155         *alignment_offset = 0;
156
157         if (!device || !device->path) //FIXME
158                 return;
159
160         fd = open(device->path, O_RDONLY);
161         if (fd == -1)
162                 return;
163
164         /* minimum io size */
165         if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
166                 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
167                         device->path, default_alignment);
168                 goto out;
169         }
170
171         /* optimal io size */
172         if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
173                 opt_io_size = min_io_size;
174
175         /* alignment offset, bogus -1 means misaligned/unknown */
176         if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
177                 dev_alignment_offset = 0;
178         *alignment_offset = (unsigned long)dev_alignment_offset;
179
180         temp_alignment = (unsigned long)min_io_size;
181
182         if (temp_alignment < (unsigned long)opt_io_size)
183                 temp_alignment = (unsigned long)opt_io_size;
184
185         /* If calculated alignment is multiple of default, keep default */
186         if (temp_alignment && (default_alignment % temp_alignment))
187                 *required_alignment = temp_alignment;
188
189         log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
190                 min_io_size, opt_io_size, *alignment_offset, *required_alignment);
191 out:
192         (void)close(fd);
193 }
194
195 int device_block_size(struct device *device)
196 {
197         struct stat st;
198         int fd, bsize = 0, r = -EINVAL;
199
200         if (!device)
201                 return 0;
202
203         fd = open(device->path, O_RDONLY);
204         if(fd < 0)
205                 return -EINVAL;
206
207         if (fstat(fd, &st) < 0)
208                 goto out;
209
210         if (S_ISREG(st.st_mode)) {
211                 r = (int)crypt_getpagesize();
212                 goto out;
213         }
214
215         if (ioctl(fd, BLKSSZGET, &bsize) >= 0)
216                 r = bsize;
217 out:
218         close(fd);
219         return r;
220 }
221
222 int device_read_ahead(struct device *device, uint32_t *read_ahead)
223 {
224         int fd, r = 0;
225         long read_ahead_long;
226
227         if (!device)
228                 return 0;
229
230         if ((fd = open(device->path, O_RDONLY)) < 0)
231                 return 0;
232
233         r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
234         close(fd);
235
236         if (r)
237                 *read_ahead = (uint32_t) read_ahead_long;
238
239         return r;
240 }
241
242 /* Get data size in bytes */
243 int device_size(struct device *device, uint64_t *size)
244 {
245         struct stat st;
246         int devfd, r = -EINVAL;
247
248         devfd = open(device->path, O_RDONLY);
249         if(devfd == -1)
250                 return -EINVAL;
251
252         if (fstat(devfd, &st) < 0)
253                 goto out;
254
255         if (S_ISREG(st.st_mode)) {
256                 *size = (uint64_t)st.st_size;
257                 r = 0;
258         } else if (ioctl(devfd, BLKGETSIZE64, size) >= 0)
259                 r = 0;
260 out:
261         close(devfd);
262         return r;
263 }
264
265 static int device_info(struct device *device,
266                         enum devcheck device_check,
267                         int *readonly, uint64_t *size)
268 {
269         struct stat st;
270         int fd, r = -EINVAL, flags = 0;
271
272         *readonly = 0;
273         *size = 0;
274
275         if (stat(device->path, &st) < 0)
276                 return -EINVAL;
277
278         /* never wipe header on mounted device */
279         if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
280                 flags |= O_EXCL;
281
282         /* Try to open read-write to check whether it is a read-only device */
283         /* coverity[toctou] */
284         fd = open(device->path, O_RDWR | flags);
285         if (fd == -1 && errno == EROFS) {
286                 *readonly = 1;
287                 fd = open(device->path, O_RDONLY | flags);
288         }
289
290         if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY)
291                 return -EBUSY;
292
293         if (fd == -1)
294                 return -EINVAL;
295
296         if (S_ISREG(st.st_mode)) {
297                 //FIXME: add readonly check
298                 *size = (uint64_t)st.st_size;
299                 *size >>= SECTOR_SHIFT;
300         } else {
301                 /* If the device can be opened read-write, i.e. readonly is still 0, then
302                  * check whether BKROGET says that it is read-only. E.g. read-only loop
303                  * devices may be openend read-write but are read-only according to BLKROGET
304                  */
305                 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
306                         goto out;
307
308                 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
309                         *size >>= SECTOR_SHIFT;
310                         r = 0;
311                         goto out;
312                 }
313         }
314         r = -EINVAL;
315 out:
316         close(fd);
317         return r;
318 }
319
320 static int device_internal_prepare(struct crypt_device *cd, struct device *device)
321 {
322         char *loop_device;
323         int r, loop_fd, readonly = 0;
324
325         if (device->init_done)
326                 return 0;
327
328         log_dbg("Allocating a free loop device.");
329         loop_device = crypt_loop_get_device();
330         if (!loop_device) {
331                 if (getuid() || geteuid())
332                         log_err(cd, _("Cannot use a loopback device, "
333                                       "running as non-root user.\n"));
334                 else
335                         log_err(cd, _("Cannot find a free loopback device.\n"));
336                 return -ENOTSUP;
337         }
338
339         /* Keep the loop open, dettached on last close. */
340         loop_fd = crypt_loop_attach(loop_device, device->path, 0, 1, &readonly);
341         if (loop_fd == -1) {
342                 log_err(cd, _("Attaching loopback device failed "
343                         "(loop device with autoclear flag is required).\n"));
344                 free(loop_device);
345                 return -EINVAL;
346         }
347
348         r = device_ready(loop_device);
349         if (r < 0) {
350                 free(loop_device);
351                 return r;
352         }
353
354         device->loop_fd = loop_fd;
355         device->file_path = device->path;
356         device->path = loop_device;
357         device->init_done = 1;
358
359         return 0;
360 }
361
362 int device_block_adjust(struct crypt_device *cd,
363                         struct device *device,
364                         enum devcheck device_check,
365                         uint64_t device_offset,
366                         uint64_t *size,
367                         uint32_t *flags)
368 {
369         int r, real_readonly;
370         uint64_t real_size;
371
372         if (!device)
373                 return -ENOTBLK;
374
375         r = device_internal_prepare(cd, device);
376         if (r)
377                 return r;
378
379         r = device_info(device, device_check, &real_readonly, &real_size);
380         if (r < 0) {
381                 if (r == -EBUSY)
382                         log_err(cd, _("Cannot use device %s which is in use "
383                                       "(already mapped or mounted).\n"),
384                                       device->path);
385                 else
386                         log_err(cd, _("Cannot get info about device %s.\n"),
387                                 device->path);
388                 return r;
389         }
390
391         if (device_offset >= real_size) {
392                 log_err(cd, _("Requested offset is beyond real size of device %s.\n"),
393                         device->path);
394                 return -EINVAL;
395         }
396
397         if (size && !*size) {
398                 *size = real_size;
399                 if (!*size) {
400                         log_err(cd, _("Device %s has zero size.\n"), device->path);
401                         return -ENOTBLK;
402                 }
403                 *size -= device_offset;
404         }
405
406         /* in case of size is set by parameter */
407         if (size && ((real_size - device_offset) < *size)) {
408                 log_dbg("Device %s: offset = %" PRIu64 " requested size = %" PRIu64
409                         ", backing device size = %" PRIu64,
410                         device->path, device_offset, *size, real_size);
411                 log_err(cd, _("Device %s is too small.\n"), device->path);
412                 return -EINVAL;
413         }
414
415         if (flags && real_readonly)
416                 *flags |= CRYPT_ACTIVATE_READONLY;
417
418         if (size)
419                 log_dbg("Calculated device size is %" PRIu64" sectors (%s), offset %" PRIu64 ".",
420                 *size, real_readonly ? "RO" : "RW", device_offset);
421         return 0;
422 }
423
424 size_t size_round_up(size_t size, unsigned int block)
425 {
426         size_t s = (size + (block - 1)) / block;
427         return s * block;
428 }