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