]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/loop-util.c
Add SPDX license identifiers to source files under the LGPL
[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 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <fcntl.h>
22 #include <linux/loop.h>
23 #include <sys/ioctl.h>
24 #include <sys/stat.h>
25
26 #include "alloc-util.h"
27 #include "fd-util.h"
28 #include "loop-util.h"
29
30 int loop_device_make(int fd, int open_flags, LoopDevice **ret) {
31 const struct loop_info64 info = {
32 .lo_flags = LO_FLAGS_AUTOCLEAR|LO_FLAGS_PARTSCAN|(open_flags == O_RDONLY ? LO_FLAGS_READ_ONLY : 0),
33 };
34
35 _cleanup_close_ int control = -1, loop = -1;
36 _cleanup_free_ char *loopdev = NULL;
37 struct stat st;
38 LoopDevice *d;
39 int nr;
40
41 assert(fd >= 0);
42 assert(ret);
43 assert(IN_SET(open_flags, O_RDWR, O_RDONLY));
44
45 if (fstat(fd, &st) < 0)
46 return -errno;
47
48 if (S_ISBLK(st.st_mode)) {
49 int copy;
50
51 /* If this is already a block device, store a copy of the fd as it is */
52
53 copy = fcntl(fd, F_DUPFD_CLOEXEC, 3);
54 if (copy < 0)
55 return -errno;
56
57 d = new0(LoopDevice, 1);
58 if (!d)
59 return -ENOMEM;
60
61 *d = (LoopDevice) {
62 .fd = copy,
63 .nr = -1,
64 };
65
66 *ret = d;
67
68 return 0;
69 }
70
71 if (!S_ISREG(st.st_mode))
72 return -EINVAL;
73
74 control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
75 if (control < 0)
76 return -errno;
77
78 nr = ioctl(control, LOOP_CTL_GET_FREE);
79 if (nr < 0)
80 return -errno;
81
82 if (asprintf(&loopdev, "/dev/loop%i", nr) < 0)
83 return -ENOMEM;
84
85 loop = open(loopdev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags);
86 if (loop < 0)
87 return -errno;
88
89 if (ioctl(loop, LOOP_SET_FD, fd) < 0)
90 return -errno;
91
92 if (ioctl(loop, LOOP_SET_STATUS64, &info) < 0)
93 return -errno;
94
95 d = new(LoopDevice, 1);
96 if (!d)
97 return -ENOMEM;
98
99 *d = (LoopDevice) {
100 .fd = loop,
101 .node = loopdev,
102 .nr = nr,
103 };
104
105 loop = -1;
106 loopdev = NULL;
107
108 *ret = d;
109
110 return (*ret)->fd;
111 }
112
113 int loop_device_make_by_path(const char *path, int open_flags, LoopDevice **ret) {
114 _cleanup_close_ int fd = -1;
115
116 assert(path);
117 assert(ret);
118 assert(IN_SET(open_flags, O_RDWR, O_RDONLY));
119
120 fd = open(path, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags);
121 if (fd < 0)
122 return -errno;
123
124 return loop_device_make(fd, open_flags, ret);
125 }
126
127 LoopDevice* loop_device_unref(LoopDevice *d) {
128 if (!d)
129 return NULL;
130
131 if (d->fd >= 0) {
132
133 if (d->nr >= 0 && !d->relinquished) {
134 if (ioctl(d->fd, LOOP_CLR_FD) < 0)
135 log_debug_errno(errno, "Failed to clear loop device: %m");
136
137 }
138
139 safe_close(d->fd);
140 }
141
142 if (d->nr >= 0 && !d->relinquished) {
143 _cleanup_close_ int control = -1;
144
145 control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
146 if (control < 0)
147 log_debug_errno(errno, "Failed to open loop control device: %m");
148 else {
149 if (ioctl(control, LOOP_CTL_REMOVE, d->nr) < 0)
150 log_debug_errno(errno, "Failed to remove loop device: %m");
151 }
152 }
153
154 free(d->node);
155 free(d);
156
157 return NULL;
158 }
159
160 void loop_device_relinquish(LoopDevice *d) {
161 assert(d);
162
163 /* Don't attempt to clean up the loop device anymore from this point on. Leave the clean-ing up to the kernel
164 * itself, using the loop device "auto-clear" logic we already turned on when creating the device. */
165
166 d->relinquished = true;
167 }