]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8c1be37e | 2 | |
dccca82b | 3 | #include <errno.h> |
8c1be37e LP |
4 | #include <fcntl.h> |
5 | #include <linux/loop.h> | |
6 | #include <sys/ioctl.h> | |
7 | #include <sys/stat.h> | |
8 | ||
9 | #include "alloc-util.h" | |
10 | #include "fd-util.h" | |
11 | #include "loop-util.h" | |
3cc44114 | 12 | #include "stat-util.h" |
8c1be37e LP |
13 | |
14 | int loop_device_make(int fd, int open_flags, LoopDevice **ret) { | |
15 | const struct loop_info64 info = { | |
16 | .lo_flags = LO_FLAGS_AUTOCLEAR|LO_FLAGS_PARTSCAN|(open_flags == O_RDONLY ? LO_FLAGS_READ_ONLY : 0), | |
17 | }; | |
18 | ||
19 | _cleanup_close_ int control = -1, loop = -1; | |
20 | _cleanup_free_ char *loopdev = NULL; | |
21 | struct stat st; | |
22 | LoopDevice *d; | |
3cc44114 | 23 | int nr, r; |
8c1be37e LP |
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, | |
16420be1 | 48 | .relinquished = true, /* It's not allocated by us, don't destroy it when this object is freed */ |
8c1be37e LP |
49 | }; |
50 | ||
51 | *ret = d; | |
16420be1 | 52 | return d->fd; |
8c1be37e LP |
53 | } |
54 | ||
3cc44114 LP |
55 | r = stat_verify_regular(&st); |
56 | if (r < 0) | |
57 | return r; | |
8c1be37e LP |
58 | |
59 | control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
60 | if (control < 0) | |
61 | return -errno; | |
62 | ||
63 | nr = ioctl(control, LOOP_CTL_GET_FREE); | |
64 | if (nr < 0) | |
65 | return -errno; | |
66 | ||
67 | if (asprintf(&loopdev, "/dev/loop%i", nr) < 0) | |
68 | return -ENOMEM; | |
69 | ||
70 | loop = open(loopdev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags); | |
71 | if (loop < 0) | |
72 | return -errno; | |
73 | ||
74 | if (ioctl(loop, LOOP_SET_FD, fd) < 0) | |
75 | return -errno; | |
76 | ||
77 | if (ioctl(loop, LOOP_SET_STATUS64, &info) < 0) | |
78 | return -errno; | |
79 | ||
80 | d = new(LoopDevice, 1); | |
81 | if (!d) | |
82 | return -ENOMEM; | |
83 | ||
84 | *d = (LoopDevice) { | |
1cc6c93a YW |
85 | .fd = TAKE_FD(loop), |
86 | .node = TAKE_PTR(loopdev), | |
8c1be37e LP |
87 | .nr = nr, |
88 | }; | |
89 | ||
8c1be37e | 90 | *ret = d; |
26c1be0f | 91 | return d->fd; |
8c1be37e LP |
92 | } |
93 | ||
94 | int loop_device_make_by_path(const char *path, int open_flags, LoopDevice **ret) { | |
95 | _cleanup_close_ int fd = -1; | |
96 | ||
97 | assert(path); | |
98 | assert(ret); | |
99 | assert(IN_SET(open_flags, O_RDWR, O_RDONLY)); | |
100 | ||
101 | fd = open(path, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags); | |
102 | if (fd < 0) | |
103 | return -errno; | |
104 | ||
105 | return loop_device_make(fd, open_flags, ret); | |
106 | } | |
107 | ||
108 | LoopDevice* loop_device_unref(LoopDevice *d) { | |
109 | if (!d) | |
110 | return NULL; | |
111 | ||
112 | if (d->fd >= 0) { | |
113 | ||
a2ea3b2f | 114 | if (d->nr >= 0 && !d->relinquished) { |
8c1be37e LP |
115 | if (ioctl(d->fd, LOOP_CLR_FD) < 0) |
116 | log_debug_errno(errno, "Failed to clear loop device: %m"); | |
117 | ||
118 | } | |
119 | ||
120 | safe_close(d->fd); | |
121 | } | |
122 | ||
a2ea3b2f | 123 | if (d->nr >= 0 && !d->relinquished) { |
8c1be37e LP |
124 | _cleanup_close_ int control = -1; |
125 | ||
126 | control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); | |
127 | if (control < 0) | |
128 | log_debug_errno(errno, "Failed to open loop control device: %m"); | |
129 | else { | |
130 | if (ioctl(control, LOOP_CTL_REMOVE, d->nr) < 0) | |
131 | log_debug_errno(errno, "Failed to remove loop device: %m"); | |
132 | } | |
133 | } | |
134 | ||
135 | free(d->node); | |
5fecf46d | 136 | return mfree(d); |
8c1be37e | 137 | } |
a2ea3b2f LP |
138 | |
139 | void loop_device_relinquish(LoopDevice *d) { | |
140 | assert(d); | |
141 | ||
142 | /* Don't attempt to clean up the loop device anymore from this point on. Leave the clean-ing up to the kernel | |
143 | * itself, using the loop device "auto-clear" logic we already turned on when creating the device. */ | |
144 | ||
145 | d->relinquished = true; | |
146 | } |