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