tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/filesystems/overlayfs/dev_in_maps.c
Extension
.c
Size
3926 bytes
Lines
161
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (start == (unsigned long)addr) {
			stx->stx_dev_major = maj;
			stx->stx_dev_minor = min;
			stx->stx_ino = ino;
			return 0;
		}
	}

	return pr_err("unable to find the mapping");
}

static int ovl_mount(void)
{
	int tmpfs, fsfd, ovl;

	fsfd = sys_fsopen("tmpfs", 0);
	if (fsfd == -1)
		return pr_perror("fsopen(tmpfs)");

	if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1)
		return pr_perror("FSCONFIG_CMD_CREATE");

	tmpfs = sys_fsmount(fsfd, 0, 0);
	if (tmpfs == -1)
		return pr_perror("fsmount");

	close(fsfd);

	/* overlayfs can't be constructed on top of a detached mount. */
	if (sys_move_mount(tmpfs, "", AT_FDCWD, "/tmp", MOVE_MOUNT_F_EMPTY_PATH))
		return pr_perror("move_mount");
	close(tmpfs);

	if (mkdir("/tmp/w", 0755) == -1 ||
	    mkdir("/tmp/u", 0755) == -1 ||
	    mkdir("/tmp/l", 0755) == -1)
		return pr_perror("mkdir");

	fsfd = sys_fsopen("overlay", 0);
	if (fsfd == -1)
		return pr_perror("fsopen(overlay)");
	if (sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "source", "test", 0) == -1 ||
	    sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "lowerdir", "/tmp/l", 0) == -1 ||
	    sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "upperdir", "/tmp/u", 0) == -1 ||
	    sys_fsconfig(fsfd, FSCONFIG_SET_STRING, "workdir", "/tmp/w", 0) == -1)
		return pr_perror("fsconfig");
	if (sys_fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) == -1)
		return pr_perror("fsconfig");
	ovl = sys_fsmount(fsfd, 0, 0);
	if (ovl == -1)
		return pr_perror("fsmount");

	return ovl;
}

/*
 * Check that the file device and inode shown in /proc/pid/maps match values
 * returned by stat(2).
 */
static int test(void)
{
	struct statx stx, mstx;
	int ovl, fd;
	void *addr;

	ovl = ovl_mount();
	if (ovl == -1)
		return -1;

	fd = openat(ovl, "test", O_RDWR | O_CREAT, 0644);
	if (fd == -1)
		return pr_perror("openat");

	addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
	if (addr == MAP_FAILED)
		return pr_perror("mmap");

	if (get_file_dev_and_inode(addr, &mstx))
		return -1;
	if (statx(fd, "", AT_EMPTY_PATH | AT_STATX_SYNC_AS_STAT, STATX_INO, &stx))
		return pr_perror("statx");

	if (stx.stx_dev_major != mstx.stx_dev_major ||
	    stx.stx_dev_minor != mstx.stx_dev_minor ||
	    stx.stx_ino != mstx.stx_ino)
		return pr_fail("unmatched dev:ino %x:%x:%llx (expected %x:%x:%llx)\n",
			mstx.stx_dev_major, mstx.stx_dev_minor, mstx.stx_ino,
			stx.stx_dev_major, stx.stx_dev_minor, stx.stx_ino);

	ksft_test_result_pass("devices are matched\n");

Annotation

Implementation Notes