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