Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Licensed under the GPL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <sys/vfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "hostfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <utime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	p->ino = buf->st_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	p->mode = buf->st_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	p->nlink = buf->st_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	p->uid = buf->st_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	p->gid = buf->st_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	p->size = buf->st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	p->atime.tv_sec = buf->st_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	p->atime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	p->ctime.tv_sec = buf->st_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	p->ctime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	p->mtime.tv_sec = buf->st_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	p->mtime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	p->blksize = buf->st_blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	p->blocks = buf->st_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	p->maj = os_major(buf->st_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	p->min = os_minor(buf->st_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) int stat_file(const char *path, struct hostfs_stat *p, int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct stat64 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (fd >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		if (fstat64(fd, &buf) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 			return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	} else if (lstat64(path, &buf) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	stat64_to_hostfs(&buf, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) int access_file(char *path, int r, int w, int x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	int mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		mode = R_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		mode |= W_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		mode |= X_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (access(path, mode) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	else return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) int open_file(char *path, int r, int w, int append)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	int mode = 0, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (r && !w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		mode = O_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	else if (!r && w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		mode = O_WRONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	else if (r && w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		mode = O_RDWR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	else panic("Impossible mode in open_file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (append)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		mode |= O_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	fd = open64(path, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	else return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) void *open_dir(char *path, int *err_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	DIR *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	dir = opendir(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	*err_out = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	return dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) void seek_dir(void *stream, unsigned long long pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	DIR *dir = stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	seekdir(dir, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) char *read_dir(void *stream, unsigned long long *pos_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	       unsigned long long *ino_out, int *len_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	       unsigned int *type_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	DIR *dir = stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct dirent *ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	ent = readdir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (ent == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	*len_out = strlen(ent->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	*ino_out = ent->d_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	*type_out = ent->d_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	*pos_out = ent->d_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return ent->d_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int read_file(int fd, unsigned long long *offset, char *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	n = pread64(fd, buf, len, *offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (n < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	*offset += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int write_file(int fd, unsigned long long *offset, const char *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	n = pwrite64(fd, buf, len, *offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (n < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	*offset += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int lseek_file(int fd, long long offset, int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	ret = lseek64(fd, offset, whence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int fsync_file(int fd, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		ret = fdatasync(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		ret = fsync(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int replace_file(int oldfd, int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return dup2(oldfd, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) void close_file(void *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	close(*((int *) stream));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) void close_dir(void *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	closedir(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int file_create(char *name, int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	fd = open64(name, O_CREAT | O_RDWR, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct hostfs_stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct timeval times[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	int err, ma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (fd >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			if (fchmod(fd, attrs->ia_mode) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		} else if (chmod(file, attrs->ia_mode) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (attrs->ia_valid & HOSTFS_ATTR_UID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		if (fd >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			if (fchown(fd, attrs->ia_uid, -1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 				return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		} else if (chown(file, attrs->ia_uid, -1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (attrs->ia_valid & HOSTFS_ATTR_GID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		if (fd >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			if (fchown(fd, -1, attrs->ia_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 				return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		} else if (chown(file, -1, attrs->ia_gid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		if (fd >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			if (ftruncate(fd, attrs->ia_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		} else if (truncate(file, attrs->ia_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 * Update accessed and/or modified time, in two parts: first set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	 * times according to the changes to perform, and then call futimes()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	 * or utimes() to apply them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (attrs->ia_valid & ma) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		err = stat_file(file, &st, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		times[0].tv_sec = st.atime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		times[0].tv_usec = st.atime.tv_nsec / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		times[1].tv_sec = st.mtime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		times[1].tv_usec = st.mtime.tv_nsec / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			times[0].tv_sec = attrs->ia_atime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			times[1].tv_sec = attrs->ia_mtime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		if (fd >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			if (futimes(fd, times) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 				return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		} else if (utimes(file, times) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	/* Note: ctime is not handled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		err = stat_file(file, &st, fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		attrs->ia_atime = st.atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		attrs->ia_mtime = st.mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int make_symlink(const char *from, const char *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	err = symlink(to, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) int unlink_file(const char *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	err = unlink(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int do_mkdir(const char *file, int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	err = mkdir(file, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int hostfs_do_rmdir(const char *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	err = rmdir(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	err = mknod(file, mode, os_makedev(major, minor));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int link_file(const char *to, const char *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	err = link(to, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int hostfs_do_readlink(char *file, char *buf, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	n = readlink(file, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (n < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (n < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		buf[n] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) int rename_file(char *from, char *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	err = rename(from, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) int rename2_file(char *from, char *to, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) #ifndef SYS_renameat2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) #  ifdef __x86_64__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) #    define SYS_renameat2 316
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) #  endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) #  ifdef __i386__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) #    define SYS_renameat2 353
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) #  endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) #ifdef SYS_renameat2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	err = syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		if (errno != ENOSYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) int do_statfs(char *root, long *bsize_out, long long *blocks_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	      long long *bfree_out, long long *bavail_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	      long long *files_out, long long *ffree_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	      void *fsid_out, int fsid_size, long *namelen_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	struct statfs64 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	err = statfs64(root, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	*bsize_out = buf.f_bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	*blocks_out = buf.f_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	*bfree_out = buf.f_bfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	*bavail_out = buf.f_bavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	*files_out = buf.f_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	*ffree_out = buf.f_ffree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	memcpy(fsid_out, &buf.f_fsid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	       sizeof(buf.f_fsid) > fsid_size ? fsid_size :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	       sizeof(buf.f_fsid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	*namelen_out = buf.f_namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }