]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/loop-util.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / shared / loop-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2016 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <linux/loop.h>
11 #include <sys/ioctl.h>
12 #include <sys/stat.h>
13
14 #include "alloc-util.h"
15 #include "fd-util.h"
16 #include "loop-util.h"
17 #include "stat-util.h"
18
19 int loop_device_make(int fd, int open_flags, LoopDevice **ret) {
20 const struct loop_info64 info = {
21 .lo_flags = LO_FLAGS_AUTOCLEAR|LO_FLAGS_PARTSCAN|(open_flags == O_RDONLY ? LO_FLAGS_READ_ONLY : 0),
22 };
23
24 _cleanup_close_ int control = -1, loop = -1;
25 _cleanup_free_ char *loopdev = NULL;
26 struct stat st;
27 LoopDevice *d;
28 int nr, r;
29
30 assert(fd >= 0);
31 assert(ret);
32 assert(IN_SET(open_flags, O_RDWR, O_RDONLY));
33
34 if (fstat(fd, &st) < 0)
35 return -errno;
36
37 if (S_ISBLK(st.st_mode)) {
38 int copy;
39
40 /* If this is already a block device, store a copy of the fd as it is */
41
42 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
43 if (copy < 0)
44 return -errno;
45
46 d = new0(LoopDevice, 1);
47 if (!d)
48 return -ENOMEM;
49
50 *d = (LoopDevice) {
51 .fd = copy,
52 .nr = -1,
53 };
54
55 *ret = d;
56
57 return 0;
58 }
59
60 r = stat_verify_regular(&st);
61 if (r < 0)
62 return r;
63
64 control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
65 if (control < 0)
66 return -errno;
67
68 nr = ioctl(control, LOOP_CTL_GET_FREE);
69 if (nr < 0)
70 return -errno;
71
72 if (asprintf(&loopdev, "/dev/loop%i", nr) < 0)
73 return -ENOMEM;
74
75 loop = open(loopdev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags);
76 if (loop < 0)
77 return -errno;
78
79 if (ioctl(loop, LOOP_SET_FD, fd) < 0)
80 return -errno;
81
82 if (ioctl(loop, LOOP_SET_STATUS64, &info) < 0)
83 return -errno;
84
85 d = new(LoopDevice, 1);
86 if (!d)
87 return -ENOMEM;
88
89 *d = (LoopDevice) {
90 .fd = TAKE_FD(loop),
91 .node = TAKE_PTR(loopdev),
92 .nr = nr,
93 };
94
95 *ret = d;
96
97 return (*ret)->fd;
98 }
99
100 int loop_device_make_by_path(const char *path, int open_flags, LoopDevice **ret) {
101 _cleanup_close_ int fd = -1;
102
103 assert(path);
104 assert(ret);
105 assert(IN_SET(open_flags, O_RDWR, O_RDONLY));
106
107 fd = open(path, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags);
108 if (fd < 0)
109 return -errno;
110
111 return loop_device_make(fd, open_flags, ret);
112 }
113
114 LoopDevice* loop_device_unref(LoopDevice *d) {
115 if (!d)
116 return NULL;
117
118 if (d->fd >= 0) {
119
120 if (d->nr >= 0 && !d->relinquished) {
121 if (ioctl(d->fd, LOOP_CLR_FD) < 0)
122 log_debug_errno(errno, "Failed to clear loop device: %m");
123
124 }
125
126 safe_close(d->fd);
127 }
128
129 if (d->nr >= 0 && !d->relinquished) {
130 _cleanup_close_ int control = -1;
131
132 control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
133 if (control < 0)
134 log_debug_errno(errno, "Failed to open loop control device: %m");
135 else {
136 if (ioctl(control, LOOP_CTL_REMOVE, d->nr) < 0)
137 log_debug_errno(errno, "Failed to remove loop device: %m");
138 }
139 }
140
141 free(d->node);
142 return mfree(d);
143 }
144
145 void loop_device_relinquish(LoopDevice *d) {
146 assert(d);
147
148 /* Don't attempt to clean up the loop device anymore from this point on. Leave the clean-ing up to the kernel
149 * itself, using the loop device "auto-clear" logic we already turned on when creating the device. */
150
151 d->relinquished = true;
152 }