Path: blob/master/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
26302 views
// SPDX-License-Identifier: GPL-2.01#define _GNU_SOURCE2#define __SANE_USERSPACE_TYPES__ // Use ll6434#include <inttypes.h>5#include <unistd.h>6#include <stdio.h>78#include <linux/unistd.h>9#include <linux/types.h>10#include <linux/mount.h>11#include <sys/syscall.h>12#include <sys/stat.h>13#include <sys/mman.h>14#include <sched.h>15#include <fcntl.h>1617#include "../../kselftest.h"18#include "log.h"19#include "../wrappers.h"2021static long get_file_dev_and_inode(void *addr, struct statx *stx)22{23char buf[4096];24FILE *mapf;2526mapf = fopen("/proc/self/maps", "r");27if (mapf == NULL)28return pr_perror("fopen(/proc/self/maps)");2930while (fgets(buf, sizeof(buf), mapf)) {31unsigned long start, end;32uint32_t maj, min;33__u64 ino;3435if (sscanf(buf, "%lx-%lx %*s %*s %x:%x %llu",36&start, &end, &maj, &min, &ino) != 5)37return pr_perror("unable to parse: %s", buf);38if (start == (unsigned long)addr) {39stx->stx_dev_major = maj;40stx->stx_dev_minor = min;41stx->stx_ino = ino;42return 0;43}44}4546return pr_err("unable to find the mapping");47}4849static int ovl_mount(void)50{51int tmpfs, fsfd, ovl;5253fsfd = sys_fsopen("tmpfs", 0);54if (fsfd == -1)55return pr_perror("fsopen(tmpfs)");5657if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1)58return pr_perror("FSCONFIG_CMD_CREATE");5960tmpfs = sys_fsmount(fsfd, 0, 0);61if (tmpfs == -1)62return pr_perror("fsmount");6364close(fsfd);6566/* overlayfs can't be constructed on top of a detached mount. */67if (sys_move_mount(tmpfs, "", AT_FDCWD, "/tmp", MOVE_MOUNT_F_EMPTY_PATH))68return pr_perror("move_mount");69close(tmpfs);7071if (mkdir("/tmp/w", 0755) == -1 ||72mkdir("/tmp/u", 0755) == -1 ||73mkdir("/tmp/l", 0755) == -1)74return pr_perror("mkdir");7576fsfd = sys_fsopen("overlay", 0);77if (fsfd == -1)78return pr_perror("fsopen(overlay)");79if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "test", 0) == -1 ||80sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "lowerdir", "/tmp/l", 0) == -1 ||81sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "upperdir", "/tmp/u", 0) == -1 ||82sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "workdir", "/tmp/w", 0) == -1)83return pr_perror("fsconfig");84if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1)85return pr_perror("fsconfig");86ovl = sys_fsmount(fsfd, 0, 0);87if (ovl == -1)88return pr_perror("fsmount");8990return ovl;91}9293/*94* Check that the file device and inode shown in /proc/pid/maps match values95* returned by stat(2).96*/97static int test(void)98{99struct statx stx, mstx;100int ovl, fd;101void *addr;102103ovl = ovl_mount();104if (ovl == -1)105return -1;106107fd = openat(ovl, "test", O_RDWR | O_CREAT, 0644);108if (fd == -1)109return pr_perror("openat");110111addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);112if (addr == MAP_FAILED)113return pr_perror("mmap");114115if (get_file_dev_and_inode(addr, &mstx))116return -1;117if (statx(fd, "", AT_EMPTY_PATH | AT_STATX_SYNC_AS_STAT, STATX_INO, &stx))118return pr_perror("statx");119120if (stx.stx_dev_major != mstx.stx_dev_major ||121stx.stx_dev_minor != mstx.stx_dev_minor ||122stx.stx_ino != mstx.stx_ino)123return pr_fail("unmatched dev:ino %x:%x:%llx (expected %x:%x:%llx)\n",124mstx.stx_dev_major, mstx.stx_dev_minor, mstx.stx_ino,125stx.stx_dev_major, stx.stx_dev_minor, stx.stx_ino);126127ksft_test_result_pass("devices are matched\n");128return 0;129}130131int main(int argc, char **argv)132{133int fsfd;134135fsfd = sys_fsopen("overlay", 0);136if (fsfd == -1) {137ksft_test_result_skip("unable to create overlay mount\n");138return 1;139}140close(fsfd);141142/* Create a new mount namespace to not care about cleaning test mounts. */143if (unshare(CLONE_NEWNS) == -1) {144ksft_test_result_skip("unable to create a new mount namespace\n");145return 1;146}147if (sys_mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) == -1) {148pr_perror("mount");149return 1;150}151152ksft_set_plan(1);153154if (test())155return 1;156157ksft_exit_pass();158return 0;159}160161162