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