2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
15 #include <sys/types.h>
17 #include <sys/syscall.h>
21 static void statx_to_hostfs(const struct statx *buf, struct hostfs_stat *p)
23 p->ino = buf->stx_ino;
24 p->mode = buf->stx_mode;
25 p->nlink = buf->stx_nlink;
26 p->uid = buf->stx_uid;
27 p->gid = buf->stx_gid;
28 p->size = buf->stx_size;
29 p->atime.tv_sec = buf->stx_atime.tv_sec;
30 p->atime.tv_nsec = buf->stx_atime.tv_nsec;
31 p->ctime.tv_sec = buf->stx_ctime.tv_sec;
32 p->ctime.tv_nsec = buf->stx_ctime.tv_nsec;
33 p->mtime.tv_sec = buf->stx_mtime.tv_sec;
34 p->mtime.tv_nsec = buf->stx_mtime.tv_nsec;
35 if (buf->stx_mask & STATX_BTIME) {
36 p->btime.tv_sec = buf->stx_btime.tv_sec;
37 p->btime.tv_nsec = buf->stx_btime.tv_nsec;
39 memset(&p->btime, 0, sizeof(p->btime));
41 p->blksize = buf->stx_blksize;
42 p->blocks = buf->stx_blocks;
43 p->rdev.maj = buf->stx_rdev_major;
44 p->rdev.min = buf->stx_rdev_minor;
45 p->dev.maj = buf->stx_dev_major;
46 p->dev.min = buf->stx_dev_minor;
49 int stat_file(const char *path, struct hostfs_stat *p, int fd)
52 int flags = AT_SYMLINK_NOFOLLOW;
55 flags |= AT_EMPTY_PATH;
59 if ((statx(fd, path, flags, STATX_BASIC_STATS | STATX_BTIME, &buf)) < 0)
62 statx_to_hostfs(&buf, p);
66 int access_file(char *path, int r, int w, int x)
76 if (access(path, mode) != 0)
81 int open_file(char *path, int r, int w, int append)
91 else panic("Impossible mode in open_file");
95 fd = open64(path, mode);
101 void *open_dir(char *path, int *err_out)
111 void seek_dir(void *stream, unsigned long long pos)
118 char *read_dir(void *stream, unsigned long long *pos_out,
119 unsigned long long *ino_out, int *len_out,
120 unsigned int *type_out)
128 *len_out = strlen(ent->d_name);
129 *ino_out = ent->d_ino;
130 *type_out = ent->d_type;
131 *pos_out = ent->d_off;
135 int read_file(int fd, unsigned long long *offset, char *buf, int len)
139 n = pread64(fd, buf, len, *offset);
146 int write_file(int fd, unsigned long long *offset, const char *buf, int len)
150 n = pwrite64(fd, buf, len, *offset);
157 int lseek_file(int fd, long long offset, int whence)
161 ret = lseek64(fd, offset, whence);
167 int fsync_file(int fd, int datasync)
180 int replace_file(int oldfd, int fd)
182 return dup2(oldfd, fd);
185 void close_file(void *stream)
187 close(*((int *) stream));
190 void close_dir(void *stream)
195 int file_create(char *name, int mode)
199 fd = open64(name, O_CREAT | O_RDWR, mode);
205 int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
207 struct hostfs_stat st;
208 struct timeval times[2];
211 if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
213 if (fchmod(fd, attrs->ia_mode) != 0)
215 } else if (chmod(file, attrs->ia_mode) != 0) {
219 if (attrs->ia_valid & HOSTFS_ATTR_UID) {
221 if (fchown(fd, attrs->ia_uid, -1))
223 } else if (chown(file, attrs->ia_uid, -1)) {
227 if (attrs->ia_valid & HOSTFS_ATTR_GID) {
229 if (fchown(fd, -1, attrs->ia_gid))
231 } else if (chown(file, -1, attrs->ia_gid)) {
235 if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
237 if (ftruncate(fd, attrs->ia_size))
239 } else if (truncate(file, attrs->ia_size)) {
245 * Update accessed and/or modified time, in two parts: first set
246 * times according to the changes to perform, and then call futimes()
247 * or utimes() to apply them.
249 ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
250 if (attrs->ia_valid & ma) {
251 err = stat_file(file, &st, fd);
255 times[0].tv_sec = st.atime.tv_sec;
256 times[0].tv_usec = st.atime.tv_nsec / 1000;
257 times[1].tv_sec = st.mtime.tv_sec;
258 times[1].tv_usec = st.mtime.tv_nsec / 1000;
260 if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
261 times[0].tv_sec = attrs->ia_atime.tv_sec;
262 times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
264 if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
265 times[1].tv_sec = attrs->ia_mtime.tv_sec;
266 times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
270 if (futimes(fd, times) != 0)
272 } else if (utimes(file, times) != 0) {
277 /* Note: ctime is not handled */
278 if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
279 err = stat_file(file, &st, fd);
280 attrs->ia_atime = st.atime;
281 attrs->ia_mtime = st.mtime;
288 int make_symlink(const char *from, const char *to)
292 err = symlink(to, from);
298 int unlink_file(const char *file)
308 int do_mkdir(const char *file, int mode)
312 err = mkdir(file, mode);
318 int hostfs_do_rmdir(const char *file)
328 int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
332 err = mknod(file, mode, os_makedev(major, minor));
338 int link_file(const char *to, const char *from)
342 err = link(to, from);
348 int hostfs_do_readlink(char *file, char *buf, int size)
352 n = readlink(file, buf, size);
360 int rename_file(char *from, char *to)
364 err = rename(from, to);
370 int rename2_file(char *from, char *to, unsigned int flags)
374 #ifndef SYS_renameat2
376 # define SYS_renameat2 316
379 # define SYS_renameat2 353
384 err = syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, flags);
397 int do_statfs(char *root, long *bsize_out, long long *blocks_out,
398 long long *bfree_out, long long *bavail_out,
399 long long *files_out, long long *ffree_out,
400 void *fsid_out, int fsid_size, long *namelen_out)
405 err = statfs64(root, &buf);
409 *bsize_out = buf.f_bsize;
410 *blocks_out = buf.f_blocks;
411 *bfree_out = buf.f_bfree;
412 *bavail_out = buf.f_bavail;
413 *files_out = buf.f_files;
414 *ffree_out = buf.f_ffree;
415 memcpy(fsid_out, &buf.f_fsid,
416 sizeof(buf.f_fsid) > fsid_size ? fsid_size :
418 *namelen_out = buf.f_namelen;