Merge branch 'upstream' into tizen
[platform/upstream/cryptsetup.git] / lib / utils_device.c
1 /*
2  * device backend utilities
3  *
4  * Copyright (C) 2004 Jana Saout <jana@saout.de>
5  * Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2023 Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2023 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 <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/ioctl.h>
30 #include <linux/fs.h>
31 #include <unistd.h>
32 #ifdef HAVE_SYS_SYSMACROS_H
33 # include <sys/sysmacros.h>     /* for major, minor */
34 #endif
35 #ifdef HAVE_SYS_STATVFS_H
36 # include <sys/statvfs.h>
37 #endif
38 #include "internal.h"
39 #include "utils_device_locking.h"
40
41 struct device {
42         char *path;
43
44         char *file_path;
45         int loop_fd;
46
47         int ro_dev_fd;
48         int dev_fd;
49         int dev_fd_excl;
50
51         struct crypt_lock_handle *lh;
52
53         unsigned int o_direct:1;
54         unsigned int init_done:1; /* path is bdev or loop already initialized */
55
56         /* cached values */
57         size_t alignment;
58         size_t block_size;
59         size_t loop_block_size;
60 };
61
62 static size_t device_fs_block_size_fd(int fd)
63 {
64         size_t page_size = crypt_getpagesize();
65
66 #ifdef HAVE_SYS_STATVFS_H
67         struct statvfs buf;
68
69         /*
70          * NOTE: some filesystems (NFS) returns bogus blocksize (1MB).
71          * Page-size io should always work and avoids increasing IO beyond aligned LUKS header.
72          */
73         if (!fstatvfs(fd, &buf) && buf.f_bsize && buf.f_bsize <= page_size)
74                 return (size_t)buf.f_bsize;
75 #endif
76         return page_size;
77 }
78
79 static size_t device_block_size_fd(int fd, size_t *min_size)
80 {
81         struct stat st;
82         size_t bsize;
83         int arg;
84
85         if (fstat(fd, &st) < 0)
86                 return 0;
87
88         if (S_ISREG(st.st_mode))
89                 bsize = device_fs_block_size_fd(fd);
90         else {
91                 if (ioctl(fd, BLKSSZGET, &arg) < 0)
92                         bsize = crypt_getpagesize();
93                 else
94                         bsize = (size_t)arg;
95         }
96
97         if (!min_size)
98                 return bsize;
99
100         if (S_ISREG(st.st_mode)) {
101                 /* file can be empty as well */
102                 if (st.st_size > (ssize_t)bsize)
103                         *min_size = bsize;
104                 else
105                         *min_size = st.st_size;
106         } else {
107                 /* block device must have at least one block */
108                 *min_size = bsize;
109         }
110
111         return bsize;
112 }
113
114 static size_t device_block_phys_size_fd(int fd)
115 {
116         struct stat st;
117         int arg;
118         size_t bsize = SECTOR_SIZE;
119
120         if (fstat(fd, &st) < 0)
121                 return bsize;
122
123         if (S_ISREG(st.st_mode))
124                 bsize = MAX_SECTOR_SIZE;
125         else if (ioctl(fd, BLKPBSZGET, &arg) >= 0)
126                 bsize = (size_t)arg;
127
128         return bsize;
129 }
130
131 static size_t device_alignment_fd(int devfd)
132 {
133         long alignment = DEFAULT_MEM_ALIGNMENT;
134
135 #ifdef _PC_REC_XFER_ALIGN
136         alignment = fpathconf(devfd, _PC_REC_XFER_ALIGN);
137         if (alignment < 0)
138                 alignment = DEFAULT_MEM_ALIGNMENT;
139 #endif
140         return (size_t)alignment;
141 }
142
143 static int device_read_test(int devfd)
144 {
145         char buffer[512];
146         int r = -EIO;
147         size_t minsize = 0, blocksize, alignment;
148
149         blocksize = device_block_size_fd(devfd, &minsize);
150         alignment = device_alignment_fd(devfd);
151
152         if (!blocksize || !alignment)
153                 return -EINVAL;
154
155         if (minsize == 0)
156                 return 0;
157
158         if (minsize > sizeof(buffer))
159                 minsize = sizeof(buffer);
160
161         if (read_blockwise(devfd, blocksize, alignment, buffer, minsize) == (ssize_t)minsize)
162                 r = 0;
163
164         crypt_safe_memzero(buffer, sizeof(buffer));
165         return r;
166 }
167
168 /*
169  * The direct-io is always preferred. The header is usually mapped to the same
170  * device and can be accessed when the rest of device is mapped to data device.
171  * Using direct-io ensures that we do not mess with data in cache.
172  * (But proper alignment should prevent this in the first place.)
173  * The read test is needed to detect broken configurations (seen with remote
174  * block devices) that allow open with direct-io but then fails on read.
175  */
176 static int device_ready(struct crypt_device *cd, struct device *device)
177 {
178         int devfd = -1, r = 0;
179         struct stat st;
180         size_t tmp_size;
181
182         if (!device)
183                 return -EINVAL;
184
185         if (device->o_direct) {
186                 log_dbg(cd, "Trying to open and read device %s with direct-io.",
187                         device_path(device));
188                 device->o_direct = 0;
189                 devfd = open(device_path(device), O_RDONLY | O_DIRECT);
190                 if (devfd >= 0) {
191                         if (device_read_test(devfd) == 0) {
192                                 device->o_direct = 1;
193                         } else {
194                                 close(devfd);
195                                 devfd = -1;
196                         }
197                 }
198         }
199
200         if (devfd < 0) {
201                 log_dbg(cd, "Trying to open device %s without direct-io.",
202                         device_path(device));
203                 devfd = open(device_path(device), O_RDONLY);
204         }
205
206         if (devfd < 0) {
207                 log_err(cd, _("Device %s does not exist or access denied."),
208                         device_path(device));
209                 return -EINVAL;
210         }
211
212         if (fstat(devfd, &st) < 0)
213                 r = -EINVAL;
214         else if (!S_ISBLK(st.st_mode))
215                 r = S_ISREG(st.st_mode) ? -ENOTBLK : -EINVAL;
216         if (r == -EINVAL) {
217                 log_err(cd, _("Device %s is not compatible."),
218                         device_path(device));
219                 close(devfd);
220                 return r;
221         }
222
223         /* Allow only increase (loop device) */
224         tmp_size = device_alignment_fd(devfd);
225         if (tmp_size > device->alignment)
226                 device->alignment = tmp_size;
227
228         tmp_size = device_block_size_fd(devfd, NULL);
229         if (tmp_size > device->block_size)
230                 device->block_size = tmp_size;
231
232         close(devfd);
233         return r;
234 }
235
236 static int _open_locked(struct crypt_device *cd, struct device *device, int flags)
237 {
238         int fd;
239
240         if (!device)
241                 return -EINVAL;
242
243         log_dbg(cd, "Opening locked device %s", device_path(device));
244
245         if ((flags & O_ACCMODE) != O_RDONLY && device_locked_readonly(device->lh)) {
246                 log_dbg(cd, "Cannot open locked device %s in write mode. Read lock held.", device_path(device));
247                 return -EAGAIN;
248         }
249
250         fd = open(device_path(device), flags);
251         if (fd < 0)
252                 return -errno;
253
254         if (device_locked_verify(cd, fd, device->lh)) {
255                 /* fd doesn't correspond to a locked resource */
256                 close(fd);
257                 log_dbg(cd, "Failed to verify lock resource for device %s.", device_path(device));
258                 return -EINVAL;
259         }
260
261         return fd;
262 }
263
264 /*
265  * Common wrapper for device sync.
266  */
267 void device_sync(struct crypt_device *cd, struct device *device)
268 {
269         if (!device || device->dev_fd < 0)
270                 return;
271
272         if (fsync(device->dev_fd) == -1)
273                 log_dbg(cd, "Cannot sync device %s.", device_path(device));
274 }
275
276 /*
277  * in non-locked mode returns always fd or -1
278  *
279  * in locked mode:
280  *      opened fd or one of:
281  *      -EAGAIN : requested write mode while device being locked in via shared lock
282  *      -EINVAL : invalid lock fd state
283  *      -1      : all other errors
284  */
285 static int device_open_internal(struct crypt_device *cd, struct device *device, int flags)
286 {
287         int access, devfd;
288
289         if (device->o_direct)
290                 flags |= O_DIRECT;
291
292         access = flags & O_ACCMODE;
293         if (access == O_WRONLY)
294                 access = O_RDWR;
295
296         if (access == O_RDONLY && device->ro_dev_fd >= 0) {
297                 log_dbg(cd, "Reusing open r%c fd on device %s", 'o', device_path(device));
298                 return device->ro_dev_fd;
299         } else if (access == O_RDWR && device->dev_fd >= 0) {
300                 log_dbg(cd, "Reusing open r%c fd on device %s", 'w', device_path(device));
301                 return device->dev_fd;
302         }
303
304         if (device_locked(device->lh))
305                 devfd = _open_locked(cd, device, flags);
306         else
307                 devfd = open(device_path(device), flags);
308
309         if (devfd < 0) {
310                 log_dbg(cd, "Cannot open device %s%s.",
311                         device_path(device),
312                         access != O_RDONLY ? " for write" : "");
313                 return devfd;
314         }
315
316         if (access == O_RDONLY)
317                 device->ro_dev_fd = devfd;
318         else
319                 device->dev_fd = devfd;
320
321         return devfd;
322 }
323
324 int device_open(struct crypt_device *cd, struct device *device, int flags)
325 {
326         if (!device)
327                 return -EINVAL;
328
329         assert(!device_locked(device->lh));
330         return device_open_internal(cd, device, flags);
331 }
332
333 int device_open_excl(struct crypt_device *cd, struct device *device, int flags)
334 {
335         const char *path;
336         struct stat st;
337
338         if (!device)
339                 return -EINVAL;
340
341         assert(!device_locked(device->lh));
342
343         if (device->dev_fd_excl < 0) {
344                 path = device_path(device);
345                 if (stat(path, &st))
346                         return -EINVAL;
347                 if (!S_ISBLK(st.st_mode))
348                         log_dbg(cd, "%s is not a block device. Can't open in exclusive mode.",
349                                 path);
350                 else {
351                         /* open(2) with O_EXCL (w/o O_CREAT) on regular file is undefined behaviour according to man page */
352                         /* coverity[toctou] */
353                         device->dev_fd_excl = open(path, O_RDONLY | O_EXCL); /* lgtm[cpp/toctou-race-condition] */
354                         if (device->dev_fd_excl < 0)
355                                 return errno == EBUSY ? -EBUSY : device->dev_fd_excl;
356                         if (fstat(device->dev_fd_excl, &st) || !S_ISBLK(st.st_mode)) {
357                                 log_dbg(cd, "%s is not a block device. Can't open in exclusive mode.",
358                                         path);
359                                 close(device->dev_fd_excl);
360                                 device->dev_fd_excl = -1;
361                         } else
362                                 log_dbg(cd, "Device %s is blocked for exclusive open.", path);
363                 }
364         }
365
366         return device_open_internal(cd, device, flags);
367 }
368
369 void device_release_excl(struct crypt_device *cd, struct device *device)
370 {
371         if (device && device->dev_fd_excl >= 0) {
372                 if (close(device->dev_fd_excl))
373                         log_dbg(cd, "Failed to release exclusive handle on device %s.",
374                                 device_path(device));
375                 else
376                         log_dbg(cd, "Closed exclusive fd for %s.", device_path(device));
377                 device->dev_fd_excl = -1;
378         }
379 }
380
381 int device_open_locked(struct crypt_device *cd, struct device *device, int flags)
382 {
383         if (!device)
384                 return -EINVAL;
385
386         assert(!crypt_metadata_locking_enabled() || device_locked(device->lh));
387         return device_open_internal(cd, device, flags);
388 }
389
390 /* Avoid any read from device, expects direct-io to work. */
391 int device_alloc_no_check(struct device **device, const char *path)
392 {
393         struct device *dev;
394
395         if (!path) {
396                 *device = NULL;
397                 return 0;
398         }
399
400         dev = malloc(sizeof(struct device));
401         if (!dev)
402                 return -ENOMEM;
403
404         memset(dev, 0, sizeof(struct device));
405         dev->path = strdup(path);
406         if (!dev->path) {
407                 free(dev);
408                 return -ENOMEM;
409         }
410         dev->loop_fd = -1;
411         dev->ro_dev_fd = -1;
412         dev->dev_fd = -1;
413         dev->dev_fd_excl = -1;
414         dev->o_direct = 1;
415
416         *device = dev;
417         return 0;
418 }
419
420 int device_alloc(struct crypt_device *cd, struct device **device, const char *path)
421 {
422         struct device *dev;
423         int r;
424
425         r = device_alloc_no_check(&dev, path);
426         if (r < 0)
427                 return r;
428
429         if (dev) {
430                 r = device_ready(cd, dev);
431                 if (!r) {
432                         dev->init_done = 1;
433                 } else if (r == -ENOTBLK) {
434                         /* alloc loop later */
435                 } else if (r < 0) {
436                         free(dev->path);
437                         free(dev);
438                         return -ENOTBLK;
439                 }
440         }
441
442         *device = dev;
443         return 0;
444 }
445
446 void device_free(struct crypt_device *cd, struct device *device)
447 {
448         if (!device)
449                 return;
450
451         device_close(cd, device);
452
453         if (device->dev_fd_excl != -1) {
454                 log_dbg(cd, "Closed exclusive fd for %s.", device_path(device));
455                 close(device->dev_fd_excl);
456         }
457
458         if (device->loop_fd != -1) {
459                 log_dbg(cd, "Closed loop %s (%s).", device->path, device->file_path);
460                 close(device->loop_fd);
461         }
462
463         assert(!device_locked(device->lh));
464
465         free(device->file_path);
466         free(device->path);
467         free(device);
468 }
469
470 /* Get block device path */
471 const char *device_block_path(const struct device *device)
472 {
473         if (!device || !device->init_done)
474                 return NULL;
475
476         return device->path;
477 }
478
479 /* Get device-mapper name of device (if possible) */
480 const char *device_dm_name(const struct device *device)
481 {
482         const char *dmdir = dm_get_dir();
483         size_t dmdir_len = strlen(dmdir);
484
485         if (!device || !device->init_done)
486                 return NULL;
487
488         if (strncmp(device->path, dmdir, dmdir_len))
489                 return NULL;
490
491         return &device->path[dmdir_len+1];
492 }
493
494 /* Get path to device / file */
495 const char *device_path(const struct device *device)
496 {
497         if (!device)
498                 return NULL;
499
500         if (device->file_path)
501                 return device->file_path;
502
503         return device->path;
504 }
505
506 /* block device topology ioctls, introduced in 2.6.32 */
507 #ifndef BLKIOMIN
508 #define BLKIOMIN    _IO(0x12,120)
509 #define BLKIOOPT    _IO(0x12,121)
510 #define BLKALIGNOFF _IO(0x12,122)
511 #endif
512
513 void device_topology_alignment(struct crypt_device *cd,
514                                struct device *device,
515                                unsigned long *required_alignment, /* bytes */
516                                unsigned long *alignment_offset,   /* bytes */
517                                unsigned long default_alignment)
518 {
519         int dev_alignment_offset = 0;
520         unsigned int min_io_size = 0, opt_io_size = 0;
521         unsigned long temp_alignment = 0;
522         int fd;
523
524         *required_alignment = default_alignment;
525         *alignment_offset = 0;
526
527         if (!device || !device->path) //FIXME
528                 return;
529
530         fd = open(device->path, O_RDONLY);
531         if (fd == -1)
532                 return;
533
534         /* minimum io size */
535         if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
536                 log_dbg(cd, "Topology info for %s not supported, using default offset %lu bytes.",
537                         device->path, default_alignment);
538                 goto out;
539         }
540
541         /* optimal io size */
542         if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
543                 opt_io_size = min_io_size;
544
545         /* alignment offset, bogus -1 means misaligned/unknown */
546         if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
547                 dev_alignment_offset = 0;
548         *alignment_offset = (unsigned long)dev_alignment_offset;
549
550         temp_alignment = (unsigned long)min_io_size;
551
552         /*
553          * Ignore bogus opt-io that could break alignment.
554          * Also real opt_io_size should be aligned to minimal page size (4k).
555          * Some bogus USB enclosures reports wrong data here.
556          */
557         if ((temp_alignment < (unsigned long)opt_io_size) &&
558             !((unsigned long)opt_io_size % temp_alignment) && !MISALIGNED_4K(opt_io_size))
559                 temp_alignment = (unsigned long)opt_io_size;
560         else if (opt_io_size && (opt_io_size != min_io_size))
561                 log_err(cd, _("Ignoring bogus optimal-io size for data device (%u bytes)."), opt_io_size);
562
563         /* If calculated alignment is multiple of default, keep default */
564         if (temp_alignment && (default_alignment % temp_alignment))
565                 *required_alignment = temp_alignment;
566
567         log_dbg(cd, "Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
568                 min_io_size, opt_io_size, *alignment_offset, *required_alignment);
569 out:
570         (void)close(fd);
571 }
572
573 size_t device_block_size(struct crypt_device *cd, struct device *device)
574 {
575         int fd;
576
577         if (!device)
578                 return 0;
579
580         if (device->block_size)
581                 return device->block_size;
582
583         fd = open(device->file_path ?: device->path, O_RDONLY);
584         if (fd >= 0) {
585                 device->block_size = device_block_size_fd(fd, NULL);
586                 close(fd);
587         }
588
589         if (!device->block_size)
590                 log_dbg(cd, "Cannot get block size for device %s.", device_path(device));
591
592         return device->block_size;
593 }
594
595 size_t device_optimal_encryption_sector_size(struct crypt_device *cd, struct device *device)
596 {
597         int fd;
598         size_t phys_block_size;
599
600         if (!device)
601                 return SECTOR_SIZE;
602
603         fd = open(device->file_path ?: device->path, O_RDONLY);
604         if (fd < 0) {
605                 log_dbg(cd, "Cannot get optimal encryption sector size for device %s.", device_path(device));
606                 return SECTOR_SIZE;
607         }
608
609         /* cache device block size */
610         device->block_size = device_block_size_fd(fd, NULL);
611         if (!device->block_size) {
612                 close(fd);
613                 log_dbg(cd, "Cannot get block size for device %s.", device_path(device));
614                 return SECTOR_SIZE;
615         }
616
617         if (device->block_size >= MAX_SECTOR_SIZE) {
618                 close(fd);
619                 return MISALIGNED(device->block_size, MAX_SECTOR_SIZE) ? SECTOR_SIZE : MAX_SECTOR_SIZE;
620         }
621
622         phys_block_size = device_block_phys_size_fd(fd);
623         close(fd);
624
625         if (device->block_size >= phys_block_size ||
626             phys_block_size <= SECTOR_SIZE ||
627             phys_block_size > MAX_SECTOR_SIZE ||
628             MISALIGNED(phys_block_size, device->block_size))
629                 return device->block_size;
630
631         return phys_block_size;
632 }
633
634 int device_read_ahead(struct device *device, uint32_t *read_ahead)
635 {
636         int fd, r = 0;
637         long read_ahead_long;
638
639         if (!device)
640                 return 0;
641
642         if ((fd = open(device->path, O_RDONLY)) < 0)
643                 return 0;
644
645         r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
646         close(fd);
647
648         if (r)
649                 *read_ahead = (uint32_t) read_ahead_long;
650
651         return r;
652 }
653
654 /* Get data size in bytes */
655 int device_size(struct device *device, uint64_t *size)
656 {
657         struct stat st;
658         int devfd, r = -EINVAL;
659
660         if (!device)
661                 return -EINVAL;
662
663         devfd = open(device->path, O_RDONLY);
664         if (devfd == -1)
665                 return -EINVAL;
666
667         if (fstat(devfd, &st) < 0)
668                 goto out;
669
670         if (S_ISREG(st.st_mode)) {
671                 *size = (uint64_t)st.st_size;
672                 r = 0;
673         } else if (ioctl(devfd, BLKGETSIZE64, size) >= 0)
674                 r = 0;
675 out:
676         close(devfd);
677         return r;
678 }
679
680 /* For a file, allocate the required space */
681 int device_fallocate(struct device *device, uint64_t size)
682 {
683         struct stat st;
684         int devfd, r = -EINVAL;
685
686         if (!device)
687                 return -EINVAL;
688
689         devfd = open(device_path(device), O_RDWR);
690         if (devfd == -1)
691                 return -EINVAL;
692
693         if (!fstat(devfd, &st) && S_ISREG(st.st_mode) &&
694             ((uint64_t)st.st_size >= size || !posix_fallocate(devfd, 0, size))) {
695                 r = 0;
696                 if (device->file_path && crypt_loop_resize(device->path))
697                         r = -EINVAL;
698         }
699
700         close(devfd);
701         return r;
702 }
703
704 int device_check_size(struct crypt_device *cd,
705                       struct device *device,
706                       uint64_t req_offset, int falloc)
707 {
708         uint64_t dev_size;
709
710         if (device_size(device, &dev_size)) {
711                 log_dbg(cd, "Cannot get device size for device %s.", device_path(device));
712                 return -EIO;
713         }
714
715         log_dbg(cd, "Device size %" PRIu64 ", offset %" PRIu64 ".", dev_size, req_offset);
716
717         if (req_offset > dev_size) {
718                 /* If it is header file, increase its size */
719                 if (falloc && !device_fallocate(device, req_offset))
720                         return 0;
721
722                 log_err(cd, _("Device %s is too small. Need at least %" PRIu64 " bytes."),
723                         device_path(device), req_offset);
724                 return -EINVAL;
725         }
726
727         return 0;
728 }
729
730 static int device_info(struct crypt_device *cd,
731                        struct device *device,
732                        enum devcheck device_check,
733                        int *readonly, uint64_t *size)
734 {
735         struct stat st;
736         int fd = -1, r, flags = 0, real_readonly;
737         uint64_t real_size;
738
739         if (!device)
740                 return -ENOTBLK;
741
742         real_readonly = 0;
743         real_size = 0;
744
745         if (stat(device->path, &st) < 0) {
746                 r = -EINVAL;
747                 goto out;
748         }
749
750         /* never wipe header on mounted device */
751         if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
752                 flags |= O_EXCL;
753
754         /* Try to open read-write to check whether it is a read-only device */
755         /* coverity[toctou] */
756         fd = open(device->path, O_RDWR | flags);
757         if (fd == -1 && errno == EROFS) {
758                 real_readonly = 1;
759                 fd = open(device->path, O_RDONLY | flags);
760         }
761
762         if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY) {
763                 r = -EBUSY;
764                 goto out;
765         }
766
767         if (fd == -1) {
768                 r = errno ? -errno : -EINVAL;
769                 goto out;
770         }
771
772         r = 0;
773         if (S_ISREG(st.st_mode)) {
774                 //FIXME: add readonly check
775                 real_size = (uint64_t)st.st_size;
776                 real_size >>= SECTOR_SHIFT;
777         } else {
778                 /* If the device can be opened read-write, i.e. readonly is still 0, then
779                  * check whether BKROGET says that it is read-only. E.g. read-only loop
780                  * devices may be opened read-write but are read-only according to BLKROGET
781                  */
782                 if (real_readonly == 0 && (r = ioctl(fd, BLKROGET, &real_readonly)) < 0)
783                         goto out;
784
785                 r = ioctl(fd, BLKGETSIZE64, &real_size);
786                 if (r >= 0) {
787                         real_size >>= SECTOR_SHIFT;
788                         goto out;
789                 }
790         }
791 out:
792         if (fd != -1)
793                 close(fd);
794
795         switch (r) {
796         case 0:
797                 if (readonly)
798                         *readonly = real_readonly;
799                 if (size)
800                         *size = real_size;
801                 break;
802         case -EBUSY:
803                 log_err(cd, _("Cannot use device %s which is in use "
804                               "(already mapped or mounted)."), device_path(device));
805                 break;
806         case -EACCES:
807                 log_err(cd, _("Cannot use device %s, permission denied."), device_path(device));
808                 break;
809         default:
810                 log_err(cd, _("Cannot get info about device %s."), device_path(device));
811                 r = -EINVAL;
812         }
813
814         return r;
815 }
816
817 int device_check_access(struct crypt_device *cd,
818                         struct device *device,
819                         enum devcheck device_check)
820 {
821         return device_info(cd, device, device_check, NULL, NULL);
822 }
823
824 static int device_internal_prepare(struct crypt_device *cd, struct device *device)
825 {
826         char *loop_device = NULL, *file_path = NULL;
827         int r, loop_fd, readonly = 0;
828
829         if (device->init_done)
830                 return 0;
831
832         if (getuid() || geteuid()) {
833                 log_err(cd, _("Cannot use a loopback device, "
834                               "running as non-root user."));
835                 return -ENOTSUP;
836         }
837
838         log_dbg(cd, "Allocating a free loop device (block size: %zu).",
839                 device->loop_block_size ?: SECTOR_SIZE);
840
841         /* Keep the loop open, detached on last close. */
842         loop_fd = crypt_loop_attach(&loop_device, device->path, 0, 1, &readonly, device->loop_block_size);
843         if (loop_fd == -1) {
844                 log_err(cd, _("Attaching loopback device failed "
845                         "(loop device with autoclear flag is required)."));
846                 free(loop_device);
847                 return -EINVAL;
848         }
849
850         file_path = device->path;
851         device->path = loop_device;
852
853         r = device_ready(cd, device);
854         if (r < 0) {
855                 device->path = file_path;
856                 crypt_loop_detach(loop_device);
857                 free(loop_device);
858                 return r;
859         }
860
861         log_dbg(cd, "Attached loop device block size is %zu bytes.", device_block_size_fd(loop_fd, NULL));
862
863         device->loop_fd = loop_fd;
864         device->file_path = file_path;
865         device->init_done = 1;
866
867         return 0;
868 }
869
870 int device_block_adjust(struct crypt_device *cd,
871                         struct device *device,
872                         enum devcheck device_check,
873                         uint64_t device_offset,
874                         uint64_t *size,
875                         uint32_t *flags)
876 {
877         int r, real_readonly;
878         uint64_t real_size;
879
880         if (!device)
881                 return -ENOTBLK;
882
883         r = device_internal_prepare(cd, device);
884         if (r)
885                 return r;
886
887         r = device_info(cd, device, device_check, &real_readonly, &real_size);
888         if (r)
889                 return r;
890
891         if (device_offset >= real_size) {
892                 log_err(cd, _("Requested offset is beyond real size of device %s."),
893                         device_path(device));
894                 return -EINVAL;
895         }
896
897         if (size && !*size) {
898                 *size = real_size;
899                 if (!*size) {
900                         log_err(cd, _("Device %s has zero size."), device_path(device));
901                         return -ENOTBLK;
902                 }
903                 *size -= device_offset;
904         }
905
906         /* in case of size is set by parameter */
907         if (size && ((real_size - device_offset) < *size)) {
908                 log_dbg(cd, "Device %s: offset = %" PRIu64 " requested size = %" PRIu64
909                         ", backing device size = %" PRIu64,
910                         device->path, device_offset, *size, real_size);
911                 log_err(cd, _("Device %s is too small."), device_path(device));
912                 return -EINVAL;
913         }
914
915         if (flags && real_readonly)
916                 *flags |= CRYPT_ACTIVATE_READONLY;
917
918         if (size)
919                 log_dbg(cd, "Calculated device size is %" PRIu64" sectors (%s), offset %" PRIu64 ".",
920                 *size, real_readonly ? "RO" : "RW", device_offset);
921         return 0;
922 }
923
924 size_t size_round_up(size_t size, size_t block)
925 {
926         size_t s = (size + (block - 1)) / block;
927         return s * block;
928 }
929
930 void device_disable_direct_io(struct device *device)
931 {
932         if (device)
933                 device->o_direct = 0;
934 }
935
936 int device_direct_io(const struct device *device)
937 {
938         return device ? device->o_direct : 0;
939 }
940
941 static int device_compare_path(const char *path1, const char *path2)
942 {
943         struct stat st_path1, st_path2;
944
945         if (stat(path1, &st_path1 ) < 0 || stat(path2, &st_path2 ) < 0)
946                 return -EINVAL;
947
948         if (S_ISBLK(st_path1.st_mode) && S_ISBLK(st_path2.st_mode))
949                 return (st_path1.st_rdev == st_path2.st_rdev) ? 1 : 0;
950
951         if (S_ISREG(st_path1.st_mode) && S_ISREG(st_path2.st_mode))
952                 return (st_path1.st_ino == st_path2.st_ino &&
953                         st_path1.st_dev == st_path2.st_dev) ? 1 : 0;
954
955         return 0;
956 }
957
958 int device_is_identical(struct device *device1, struct device *device2)
959 {
960         if (!device1 || !device2)
961                 return 0;
962
963         if (device1 == device2)
964                 return 1;
965
966         if (!strcmp(device_path(device1), device_path(device2)))
967                 return 1;
968
969         return device_compare_path(device_path(device1), device_path(device2));
970 }
971
972 int device_is_rotational(struct device *device)
973 {
974         struct stat st;
975
976         if (!device)
977                 return -EINVAL;
978
979         if (stat(device_path(device), &st) < 0)
980                 return -EINVAL;
981
982         if (!S_ISBLK(st.st_mode))
983                 return 0;
984
985         return crypt_dev_is_rotational(major(st.st_rdev), minor(st.st_rdev));
986 }
987
988 size_t device_alignment(struct device *device)
989 {
990         int devfd;
991
992         if (!device)
993                 return -EINVAL;
994
995         if (!device->alignment) {
996                 devfd = open(device_path(device), O_RDONLY);
997                 if (devfd != -1) {
998                         device->alignment = device_alignment_fd(devfd);
999                         close(devfd);
1000                 }
1001         }
1002
1003         return device->alignment;
1004 }
1005
1006 void device_set_lock_handle(struct device *device, struct crypt_lock_handle *h)
1007 {
1008         if (device)
1009                 device->lh = h;
1010 }
1011
1012 struct crypt_lock_handle *device_get_lock_handle(struct device *device)
1013 {
1014         return device ? device->lh : NULL;
1015 }
1016
1017 int device_read_lock(struct crypt_device *cd, struct device *device)
1018 {
1019         if (!device || !crypt_metadata_locking_enabled())
1020                 return 0;
1021
1022         if (device_read_lock_internal(cd, device))
1023                 return -EBUSY;
1024
1025         return 0;
1026 }
1027
1028 int device_write_lock(struct crypt_device *cd, struct device *device)
1029 {
1030         if (!device || !crypt_metadata_locking_enabled())
1031                 return 0;
1032
1033         assert(!device_locked(device->lh) || !device_locked_readonly(device->lh));
1034
1035         return device_write_lock_internal(cd, device);
1036 }
1037
1038 void device_read_unlock(struct crypt_device *cd, struct device *device)
1039 {
1040         if (!device || !crypt_metadata_locking_enabled())
1041                 return;
1042
1043         assert(device_locked(device->lh));
1044
1045         device_unlock_internal(cd, device);
1046 }
1047
1048 void device_write_unlock(struct crypt_device *cd, struct device *device)
1049 {
1050         if (!device || !crypt_metadata_locking_enabled())
1051                 return;
1052
1053         assert(device_locked(device->lh) && !device_locked_readonly(device->lh));
1054
1055         device_unlock_internal(cd, device);
1056 }
1057
1058 bool device_is_locked(struct device *device)
1059 {
1060         return device ? device_locked(device->lh) : 0;
1061 }
1062
1063 void device_close(struct crypt_device *cd, struct device *device)
1064 {
1065         if (!device)
1066                 return;
1067
1068         if (device->ro_dev_fd != -1) {
1069                 log_dbg(cd, "Closing read only fd for %s.", device_path(device));
1070                 if (close(device->ro_dev_fd))
1071                         log_dbg(cd, "Failed to close read only fd for %s.", device_path(device));
1072                 device->ro_dev_fd = -1;
1073         }
1074
1075         if (device->dev_fd != -1) {
1076                 log_dbg(cd, "Closing read write fd for %s.", device_path(device));
1077                 if (close(device->dev_fd))
1078                         log_dbg(cd, "Failed to close read write fd for %s.", device_path(device));
1079                 device->dev_fd = -1;
1080         }
1081 }
1082
1083 void device_set_block_size(struct device *device, size_t size)
1084 {
1085         if (!device)
1086                 return;
1087
1088         device->loop_block_size = size;
1089 }