tools/testing/selftests/filesystems/file_stressor.c

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/filesystems/file_stressor.c
Extension
.c
Size
4984 bytes
Lines
195
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

// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#define __SANE_USERSPACE_TYPES__

#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <unistd.h>

#include "kselftest_harness.h"

#include <linux/types.h>
#include <linux/mount.h>
#include <sys/syscall.h>

static inline int sys_fsopen(const char *fsname, unsigned int flags)
{
	return syscall(__NR_fsopen, fsname, flags);
}

static inline int sys_fsconfig(int fd, unsigned int cmd, const char *key,
			       const char *value, int aux)
{
	return syscall(__NR_fsconfig, fd, cmd, key, value, aux);
}

static inline int sys_fsmount(int fd, unsigned int flags,
			      unsigned int attr_flags)
{
	return syscall(__NR_fsmount, fd, flags, attr_flags);
}

#ifndef MOVE_MOUNT_F_EMPTY_PATH
#define MOVE_MOUNT_F_EMPTY_PATH 0x00000004 /* Empty from path permitted */
#endif

static inline int sys_move_mount(int from_dfd, const char *from_pathname,
				 int to_dfd, const char *to_pathname,
				 unsigned int flags)
{
	return syscall(__NR_move_mount, from_dfd, from_pathname, to_dfd,
		       to_pathname, flags);
}

FIXTURE(file_stressor) {
	int fd_tmpfs;
	int nr_procs;
	int max_fds;
	pid_t *pids_openers;
	pid_t *pids_getdents;
	int *fd_proc_pid;
};

FIXTURE_SETUP(file_stressor)
{
	int fd_context;

	ASSERT_EQ(unshare(CLONE_NEWNS), 0);
	ASSERT_EQ(mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL), 0);
	ASSERT_EQ(mkdir("/slab_typesafe_by_rcu", 0755), 0);

	fd_context = sys_fsopen("tmpfs", 0);
	ASSERT_GE(fd_context, 0);

	ASSERT_EQ(sys_fsconfig(fd_context, FSCONFIG_CMD_CREATE, NULL, NULL, 0), 0);
	self->fd_tmpfs = sys_fsmount(fd_context, 0, 0);
	ASSERT_GE(self->fd_tmpfs, 0);
	ASSERT_EQ(close(fd_context), 0);

	ASSERT_EQ(sys_move_mount(self->fd_tmpfs, "", -EBADF, "/slab_typesafe_by_rcu", MOVE_MOUNT_F_EMPTY_PATH), 0);

	self->nr_procs = sysconf(_SC_NPROCESSORS_ONLN);
	self->pids_openers = malloc(sizeof(pid_t) * self->nr_procs);
	ASSERT_NE(self->pids_openers, NULL);
	self->pids_getdents = malloc(sizeof(pid_t) * self->nr_procs);
	ASSERT_NE(self->pids_getdents, NULL);
	self->fd_proc_pid = malloc(sizeof(int) * self->nr_procs);
	ASSERT_NE(self->fd_proc_pid, NULL);
	self->max_fds = 500;
}

FIXTURE_TEARDOWN(file_stressor)
{
	for (int i = 0; i < self->nr_procs; i++) {
		int wstatus;

Annotation

Implementation Notes