1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
// SPDX-License-Identifier: GPL-2.0
/*
* Freezing a block device that several superblocks share.
*
* erofs claims its blob devices with the filesystem type as the holder, so
* several erofs superblocks can share a blob. Merge two images from a common
* layer and a layer of their own, put the common layer on a device-mapper
* device and mount both. A suspend of that device has to freeze each of the
* two superblocks exactly once and the resume has to thaw both, also while
* mounts of one of them come and go. erofs is read-only, so the freezes and
* thaws don't write to the suspended device.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <linux/dm-ioctl.h>
#include <linux/fs.h>
#include <sched.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/wait.h>
#include <unistd.h>
#include "../../kselftest_harness.h"
#include "loop_util.h"
#define NR_CHURN 100
enum { M12, M13, L1, L2, L3, NR_IMGS };
static const char *const img_names[NR_IMGS] = { "m12", "m13", "l1", "l2", "l3" };
FIXTURE(shared_bdev) {
char dir[64];
char a[96];
char b[96];
char dm[96];
char name[32];
char src_a[32];
char src_b[32];
char opts_a[256];
char opts_b[256];
int loop[NR_IMGS];
int fd_a;
int fd_b;
dev_t dmdev;
bool dm_created;
bool suspended;
bool mounted_a;
bool mounted_b;
};
static int dm_ioctl(unsigned long cmd, const char *name, uint32_t flags, dev_t *dev)
{
struct dm_ioctl dmi = {
.version = { DM_VERSION_MAJOR, 0, 0 },
.data_size = sizeof(dmi),
.data_start = sizeof(dmi),
.flags = flags,
};
int fd, ret = 0;
strncpy(dmi.name, name, sizeof(dmi.name) - 1);
fd = open("/dev/mapper/control", O_RDWR | O_CLOEXEC);
if (fd < 0)
return -errno;
if (ioctl(fd, cmd, &dmi))
ret = -errno;
else if (dev)
*dev = makedev((dmi.dev >> 8) & 0xfff,
(dmi.dev & 0xff) | ((dmi.dev >> 12) & 0xfff00));
close(fd);
return ret;
}
/* Create @name as a linear mapping of all of @backing and make it live. */
static int dm_create_linear(const char *name, dev_t backing, uint64_t sectors,
dev_t *dev)
{
struct {
struct dm_ioctl dmi;
struct dm_target_spec spec;
char params[64];
} __attribute__((aligned(8))) t = {};
int fd, ret;
ret = dm_ioctl(DM_DEV_CREATE, name, 0, NULL);
if (ret)
return ret;
t.dmi.version[0] = DM_VERSION_MAJOR;
t.dmi.data_size = sizeof(t);
t.dmi.data_start = sizeof(t.dmi);
t.dmi.target_count = 1;
strncpy(t.dmi.name, name, sizeof(t.dmi.name) - 1);
t.spec.length = sectors;
strcpy(t.spec.target_type, "linear");
snprintf(t.params, sizeof(t.params), "%u:%u 0", major(backing), minor(backing));
fd = open("/dev/mapper/control", O_RDWR | O_CLOEXEC);
if (fd >= 0) {
ret = ioctl(fd, DM_TABLE_LOAD, &t) ? -errno : 0;
close(fd);
} else {
ret = -errno;
}
/* Resuming makes the loaded table live. */
if (!ret)
ret = dm_ioctl(DM_DEV_SUSPEND, name, 0, dev);
if (ret)
dm_ioctl(DM_DEV_REMOVE, name, 0, NULL);
return ret;
}
static int suspend(FIXTURE_DATA(shared_bdev) *self)
{
int ret = dm_ioctl(DM_DEV_SUSPEND, self->name, DM_SUSPEND_FLAG, NULL);
if (!ret)
self->suspended = true;
return ret;
}
static int resume(FIXTURE_DATA(shared_bdev) *self)
{
int ret = dm_ioctl(DM_DEV_SUSPEND, self->name, 0, NULL);
if (!ret)
self->suspended = false;
return ret;
}
/* Drop one freeze with FITHAW; EINVAL if the superblock isn't frozen. */
static bool fithaw(int fd)
{
return !ioctl(fd, FITHAW, 0);
}
static int write_file(const char *path, const char *val)
{
ssize_t len = strlen(val);
int fd, ret = 0;
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd < 0)
return -1;
if (write(fd, val, len) != len)
ret = -1;
close(fd);
return ret;
}
static int check_file(const char *path, const char *val)
{
char buf[32] = {};
int fd;
fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return -1;
if (read(fd, buf, sizeof(buf) - 1) < 0)
buf[0] = '\0';
close(fd);
return strcmp(buf, val) ? -1 : 0;
}
static int rm_one(const char *path, const struct stat *st, int type, struct FTW *ftw)
{
return remove(path);
}
static int mount_b(FIXTURE_DATA(shared_bdev) *self)
{
int ret = mount(self->src_b, self->b, "erofs", MS_RDONLY, self->opts_b);
if (!ret)
self->mounted_b = true;
return ret;
}
static void shared_bdev_cleanup(FIXTURE_DATA(shared_bdev) *self)
{
if (self->suspended)
resume(self);
if (self->fd_a >= 0)
close(self->fd_a);
if (self->fd_b >= 0)
close(self->fd_b);
if (self->mounted_a)
umount2(self->a, MNT_DETACH);
umount2(self->b, MNT_DETACH);
if (self->dm_created)
dm_ioctl(DM_DEV_REMOVE, self->name, 0, NULL);
for (int i = 0; i < NR_IMGS; i++) {
if (self->loop[i] >= 0)
loop_clear(self->loop[i]);
}
nftw(self->dir, rm_one, 16, FTW_DEPTH | FTW_PHYS);
}
/* Two layers of their own, a common one, and two images merged from them. */
static int make_images(const char *dir)
{
static const char *const files[][2] = {
{ "src-l1/d1/f1", "one\n" },
{ "src-l2/f2", "two\n" },
{ "src-l3/f3", "three\n" },
};
char path[160], cmd[512];
for (int i = 0; i < 3; i++) {
snprintf(path, sizeof(path), "%s/src-l%d", dir, i + 1);
if (mkdir(path, 0755))
return -1;
}
snprintf(path, sizeof(path), "%s/src-l1/d1", dir);
if (mkdir(path, 0755))
return -1;
for (int i = 0; i < 3; i++) {
snprintf(path, sizeof(path), "%s/%s", dir, files[i][0]);
if (write_file(path, files[i][1]))
return -1;
}
/* Chunk-based layers can be referenced by the merged images. */
snprintf(cmd, sizeof(cmd),
"cd %s && (for l in l1 l2 l3; do "
"mkfs.erofs --chunksize=4096 -Enoinline_data $l src-$l || exit 1; done && "
"mkfs.erofs m12 l1 l2 && mkfs.erofs m13 l1 l3) >/dev/null 2>&1", dir);
return system(cmd) ? -1 : 0;
}
FIXTURE_SETUP(shared_bdev)
{
struct stat img, lst, st;
char path[160], dev[32];
int fd;
for (int i = 0; i < NR_IMGS; i++)
self->loop[i] = -1;
self->fd_a = -1;
self->fd_b = -1;
self->dm_created = false;
self->suspended = false;
self->mounted_a = false;
self->mounted_b = false;
if (geteuid() != 0)
SKIP(return, "test requires CAP_SYS_ADMIN");
if (access("/dev/loop-control", R_OK | W_OK))
SKIP(return, "test requires loop devices");
if (access("/dev/mapper/control", R_OK | W_OK))
SKIP(return, "test requires device-mapper");
ASSERT_EQ(unshare(CLONE_NEWNS), 0);
ASSERT_EQ(mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL), 0);
snprintf(self->dir, sizeof(self->dir), "/tmp/shared_bdev.XXXXXX");
ASSERT_NE(mkdtemp(self->dir), NULL);
snprintf(self->a, sizeof(self->a), "%s/a", self->dir);
snprintf(self->b, sizeof(self->b), "%s/b", self->dir);
snprintf(self->name, sizeof(self->name), "shared_bdev_%d", getpid());
/* The harness doesn't run the teardown when the setup fails. */
ASSERT_EQ(mkdir(self->a, 0700), 0)
shared_bdev_cleanup(self);
ASSERT_EQ(mkdir(self->b, 0700), 0)
shared_bdev_cleanup(self);
if (make_images(self->dir)) {
shared_bdev_cleanup(self);
SKIP(return, "mkfs.erofs failed, is missing or can't merge images");
}
for (int i = 0; i < NR_IMGS; i++) {
snprintf(path, sizeof(path), "%s/%s", self->dir, img_names[i]);
fd = open(path, O_RDWR | O_CLOEXEC);
ASSERT_GE(fd, 0)
shared_bdev_cleanup(self);
self->loop[i] = loop_bind(fd);
if (i == L1) {
ASSERT_EQ(fstat(fd, &img), 0)
shared_bdev_cleanup(self);
}
close(fd);
ASSERT_GE(self->loop[i], 0)
shared_bdev_cleanup(self);
}
/* The common layer goes on a device-mapper device. */
snprintf(dev, sizeof(dev), "/dev/loop%d", self->loop[L1]);
ASSERT_EQ(stat(dev, &lst), 0)
shared_bdev_cleanup(self);
ASSERT_EQ(dm_create_linear(self->name, lst.st_rdev, img.st_size >> 9, &self->dmdev), 0)
shared_bdev_cleanup(self);
self->dm_created = true;
snprintf(self->dm, sizeof(self->dm), "/dev/dm-%u", minor(self->dmdev));
if (stat(self->dm, &st) || st.st_rdev != self->dmdev) {
snprintf(self->dm, sizeof(self->dm), "%s/dm", self->dir);
ASSERT_EQ(mknod(self->dm, S_IFBLK | 0600, self->dmdev), 0)
shared_bdev_cleanup(self);
}
snprintf(self->src_a, sizeof(self->src_a), "/dev/loop%d", self->loop[M12]);
snprintf(self->src_b, sizeof(self->src_b), "/dev/loop%d", self->loop[M13]);
snprintf(self->opts_a, sizeof(self->opts_a), "device=%s,device=/dev/loop%d",
self->dm, self->loop[L2]);
snprintf(self->opts_b, sizeof(self->opts_b), "device=%s,device=/dev/loop%d",
self->dm, self->loop[L3]);
if (mount(self->src_a, self->a, "erofs", MS_RDONLY, self->opts_a)) {
if (errno == ENODEV) {
shared_bdev_cleanup(self);
SKIP(return, "kernel lacks erofs");
}
ASSERT_EQ(errno, 0)
shared_bdev_cleanup(self);
}
self->mounted_a = true;
ASSERT_EQ(mount_b(self), 0)
shared_bdev_cleanup(self);
/* Both read through the shared blob. */
snprintf(path, sizeof(path), "%s/d1/f1", self->a);
ASSERT_EQ(check_file(path, "one\n"), 0)
shared_bdev_cleanup(self);
snprintf(path, sizeof(path), "%s/f2", self->a);
ASSERT_EQ(check_file(path, "two\n"), 0)
shared_bdev_cleanup(self);
snprintf(path, sizeof(path), "%s/d1/f1", self->b);
ASSERT_EQ(check_file(path, "one\n"), 0)
shared_bdev_cleanup(self);
snprintf(path, sizeof(path), "%s/f3", self->b);
ASSERT_EQ(check_file(path, "three\n"), 0)
shared_bdev_cleanup(self);
self->fd_a = open(self->a, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
ASSERT_GE(self->fd_a, 0)
shared_bdev_cleanup(self);
self->fd_b = open(self->b, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
ASSERT_GE(self->fd_b, 0)
shared_bdev_cleanup(self);
}
FIXTURE_TEARDOWN(shared_bdev)
{
shared_bdev_cleanup(self);
}
TEST_F(shared_bdev, suspend_freezes_each_once)
{
ASSERT_EQ(suspend(self), 0);
/* One FITHAW drops each freeze, a second one finds nothing frozen. */
ASSERT_TRUE(fithaw(self->fd_a));
ASSERT_FALSE(fithaw(self->fd_a));
ASSERT_EQ(errno, EINVAL);
ASSERT_TRUE(fithaw(self->fd_b));
ASSERT_FALSE(fithaw(self->fd_b));
ASSERT_EQ(errno, EINVAL);
/* The resume finds nothing left to thaw and still thaws the device. */
ASSERT_EQ(resume(self), 0);
ASSERT_EQ(suspend(self), 0);
ASSERT_EQ(resume(self), 0);
}
TEST_F(shared_bdev, resume_thaws_all)
{
ASSERT_EQ(suspend(self), 0);
ASSERT_EQ(resume(self), 0);
ASSERT_FALSE(fithaw(self->fd_a));
ASSERT_EQ(errno, EINVAL);
ASSERT_FALSE(fithaw(self->fd_b));
ASSERT_EQ(errno, EINVAL);
}
TEST_F(shared_bdev, unmounted_sharer)
{
close(self->fd_b);
self->fd_b = -1;
ASSERT_EQ(umount2(self->b, 0), 0);
self->mounted_b = false;
/* Only the remaining superblock is frozen, once. */
ASSERT_EQ(suspend(self), 0);
ASSERT_TRUE(fithaw(self->fd_a));
ASSERT_FALSE(fithaw(self->fd_a));
ASSERT_EQ(errno, EINVAL);
ASSERT_EQ(resume(self), 0);
/* The blob can be claimed again. */
ASSERT_EQ(mount_b(self), 0);
}
TEST_F(shared_bdev, flushbuf)
{
int fd = open(self->dm, O_RDONLY | O_CLOEXEC);
ASSERT_GE(fd, 0);
/* BLKFLSBUF syncs every superblock that uses the device. */
ASSERT_EQ(ioctl(fd, BLKFLSBUF, 0), 0);
close(fd);
}
#ifdef __NR_ustat
/* struct ustat is not exported through UAPI, mirror include/linux/types.h. */
struct ustat_buf {
int f_tfree;
unsigned long f_tinode;
char f_fname[6];
char f_fpack[6];
char pad[64];
};
TEST_F(shared_bdev, ustat)
{
struct ustat_buf buf;
/* ustat() stops at the first superblock it can lock... */
ASSERT_EQ(syscall(__NR_ustat, (unsigned int)self->dmdev, &buf), 0);
/* ...and the walks after it still reach both. */
ASSERT_EQ(suspend(self), 0);
ASSERT_TRUE(fithaw(self->fd_a));
ASSERT_TRUE(fithaw(self->fd_b));
ASSERT_EQ(resume(self), 0);
}
#endif
TEST_F(shared_bdev, churn)
{
int status;
pid_t pid;
close(self->fd_b);
self->fd_b = -1;
ASSERT_EQ(umount2(self->b, 0), 0);
self->mounted_b = false;
pid = fork();
ASSERT_GE(pid, 0);
if (pid == 0) {
/* A mount while the device is frozen is refused with EBUSY. */
for (int i = 0; i < NR_CHURN; i++) {
if (!mount(self->src_b, self->b, "erofs", MS_RDONLY, self->opts_b)) {
if (umount2(self->b, 0))
_exit(1);
} else if (errno != EBUSY) {
_exit(2);
}
}
_exit(0);
}
for (int i = 0; i < NR_CHURN; i++) {
ASSERT_EQ(suspend(self), 0);
ASSERT_EQ(resume(self), 0);
}
ASSERT_EQ(waitpid(pid, &status, 0), pid);
ASSERT_TRUE(WIFEXITED(status));
ASSERT_EQ(WEXITSTATUS(status), 0);
/* Nothing is left frozen and the blob can be claimed again. */
ASSERT_FALSE(fithaw(self->fd_a));
ASSERT_EQ(errno, EINVAL);
ASSERT_EQ(mount_b(self), 0);
}
TEST_HARNESS_MAIN
|