blob: 9c1e0f01988039356874c7445459179f80730511 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dikef1adc052007-05-08 00:23:18 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <stdio.h>
Jeff Dike84b3db02007-10-16 01:27:13 -07007#include <stddef.h>
8#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <dirent.h>
10#include <errno.h>
Jeff Dike84b3db02007-10-16 01:27:13 -070011#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <string.h>
13#include <sys/stat.h>
14#include <sys/time.h>
Jeff Dike84b3db02007-10-16 01:27:13 -070015#include <sys/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <sys/vfs.h>
Miklos Szeredi9a423bb2014-07-23 15:15:35 +020017#include <sys/syscall.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "hostfs.h"
Jeff Dike84b3db02007-10-16 01:27:13 -070019#include <utime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Al Viro39b743c2010-06-06 20:08:56 -040021static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p)
22{
23 p->ino = buf->st_ino;
24 p->mode = buf->st_mode;
25 p->nlink = buf->st_nlink;
26 p->uid = buf->st_uid;
27 p->gid = buf->st_gid;
28 p->size = buf->st_size;
29 p->atime.tv_sec = buf->st_atime;
30 p->atime.tv_nsec = 0;
31 p->ctime.tv_sec = buf->st_ctime;
32 p->ctime.tv_nsec = 0;
33 p->mtime.tv_sec = buf->st_mtime;
34 p->mtime.tv_nsec = 0;
35 p->blksize = buf->st_blksize;
36 p->blocks = buf->st_blocks;
37 p->maj = os_major(buf->st_rdev);
38 p->min = os_minor(buf->st_rdev);
39}
40
41int stat_file(const char *path, struct hostfs_stat *p, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 struct stat64 buf;
44
Jeff Dike84b3db02007-10-16 01:27:13 -070045 if (fd >= 0) {
Alberto Bertogli5822b7f2007-05-08 00:23:16 -070046 if (fstat64(fd, &buf) < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -070047 return -errno;
Jeff Dike84b3db02007-10-16 01:27:13 -070048 } else if (lstat64(path, &buf) < 0) {
Jeff Dikef1adc052007-05-08 00:23:18 -070049 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -070050 }
Al Viro39b743c2010-06-06 20:08:56 -040051 stat64_to_hostfs(&buf, p);
Jeff Dikef1adc052007-05-08 00:23:18 -070052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055int access_file(char *path, int r, int w, int x)
56{
57 int mode = 0;
58
Jeff Dike84b3db02007-10-16 01:27:13 -070059 if (r)
60 mode = R_OK;
61 if (w)
62 mode |= W_OK;
63 if (x)
64 mode |= X_OK;
65 if (access(path, mode) != 0)
Jeff Dikef1adc052007-05-08 00:23:18 -070066 return -errno;
67 else return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70int open_file(char *path, int r, int w, int append)
71{
72 int mode = 0, fd;
73
Jeff Dike84b3db02007-10-16 01:27:13 -070074 if (r && !w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 mode = O_RDONLY;
Jeff Dike84b3db02007-10-16 01:27:13 -070076 else if (!r && w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 mode = O_WRONLY;
Jeff Dike84b3db02007-10-16 01:27:13 -070078 else if (r && w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 mode = O_RDWR;
80 else panic("Impossible mode in open_file");
81
Jeff Dike84b3db02007-10-16 01:27:13 -070082 if (append)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 mode |= O_APPEND;
84 fd = open64(path, mode);
Jeff Dike84b3db02007-10-16 01:27:13 -070085 if (fd < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -070086 return -errno;
87 else return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90void *open_dir(char *path, int *err_out)
91{
92 DIR *dir;
93
94 dir = opendir(path);
95 *err_out = errno;
Richard Weinbergerc5c6dd42010-10-26 14:22:20 -070096
Jeff Dikef1adc052007-05-08 00:23:18 -070097 return dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Richard Weinberger0c9bd632015-03-24 15:47:38 +0100100void seek_dir(void *stream, unsigned long long pos)
101{
102 DIR *dir = stream;
103
104 seekdir(dir, pos);
105}
106
107char *read_dir(void *stream, unsigned long long *pos_out,
Geert Uytterhoeven3ee6bd82012-01-27 19:14:58 +0100108 unsigned long long *ino_out, int *len_out,
109 unsigned int *type_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 DIR *dir = stream;
112 struct dirent *ent;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 ent = readdir(dir);
Jeff Dike84b3db02007-10-16 01:27:13 -0700115 if (ent == NULL)
Jeff Dikef1adc052007-05-08 00:23:18 -0700116 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 *len_out = strlen(ent->d_name);
118 *ino_out = ent->d_ino;
Geert Uytterhoeven3ee6bd82012-01-27 19:14:58 +0100119 *type_out = ent->d_type;
Richard Weinberger0c9bd632015-03-24 15:47:38 +0100120 *pos_out = ent->d_off;
Jeff Dikef1adc052007-05-08 00:23:18 -0700121 return ent->d_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124int read_file(int fd, unsigned long long *offset, char *buf, int len)
125{
126 int n;
127
128 n = pread64(fd, buf, len, *offset);
Jeff Dike84b3db02007-10-16 01:27:13 -0700129 if (n < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700130 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *offset += n;
Jeff Dikef1adc052007-05-08 00:23:18 -0700132 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135int write_file(int fd, unsigned long long *offset, const char *buf, int len)
136{
137 int n;
138
139 n = pwrite64(fd, buf, len, *offset);
Jeff Dike84b3db02007-10-16 01:27:13 -0700140 if (n < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700141 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *offset += n;
Jeff Dikef1adc052007-05-08 00:23:18 -0700143 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146int lseek_file(int fd, long long offset, int whence)
147{
148 int ret;
149
150 ret = lseek64(fd, offset, whence);
Jeff Dike84b3db02007-10-16 01:27:13 -0700151 if (ret < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700152 return -errno;
153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Paolo 'Blaisorblade' Giarrussoa2d76bd2005-07-28 21:16:15 -0700156int fsync_file(int fd, int datasync)
157{
158 int ret;
159 if (datasync)
160 ret = fdatasync(fd);
161 else
162 ret = fsync(fd);
163
164 if (ret < 0)
165 return -errno;
166 return 0;
167}
168
Al Virof8ad8502010-06-06 23:49:18 -0400169int replace_file(int oldfd, int fd)
170{
171 return dup2(oldfd, fd);
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174void close_file(void *stream)
175{
176 close(*((int *) stream));
177}
178
179void close_dir(void *stream)
180{
181 closedir(stream);
182}
183
Richard Weinbergerb98b9102015-03-04 23:44:54 +0100184int file_create(char *name, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Richard Weinbergerb98b9102015-03-04 23:44:54 +0100186 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 fd = open64(name, O_CREAT | O_RDWR, mode);
Jeff Dike84b3db02007-10-16 01:27:13 -0700189 if (fd < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700190 return -errno;
191 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700194int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Al Viro39b743c2010-06-06 20:08:56 -0400196 struct hostfs_stat st;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700197 struct timeval times[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int err, ma;
199
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700200 if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
201 if (fd >= 0) {
202 if (fchmod(fd, attrs->ia_mode) != 0)
Richard Weinbergerc5c6dd42010-10-26 14:22:20 -0700203 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700204 } else if (chmod(file, attrs->ia_mode) != 0) {
Jeff Dikef1adc052007-05-08 00:23:18 -0700205 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700208 if (attrs->ia_valid & HOSTFS_ATTR_UID) {
209 if (fd >= 0) {
210 if (fchown(fd, attrs->ia_uid, -1))
Jeff Dikef1adc052007-05-08 00:23:18 -0700211 return -errno;
Jeff Dike84b3db02007-10-16 01:27:13 -0700212 } else if (chown(file, attrs->ia_uid, -1)) {
Jeff Dikef1adc052007-05-08 00:23:18 -0700213 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700216 if (attrs->ia_valid & HOSTFS_ATTR_GID) {
217 if (fd >= 0) {
218 if (fchown(fd, -1, attrs->ia_gid))
Jeff Dikef1adc052007-05-08 00:23:18 -0700219 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700220 } else if (chown(file, -1, attrs->ia_gid)) {
Jeff Dikef1adc052007-05-08 00:23:18 -0700221 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700224 if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
225 if (fd >= 0) {
226 if (ftruncate(fd, attrs->ia_size))
Jeff Dikef1adc052007-05-08 00:23:18 -0700227 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700228 } else if (truncate(file, attrs->ia_size)) {
Jeff Dikef1adc052007-05-08 00:23:18 -0700229 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jeff Dike84b3db02007-10-16 01:27:13 -0700233 /*
234 * Update accessed and/or modified time, in two parts: first set
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700235 * times according to the changes to perform, and then call futimes()
Jeff Dike84b3db02007-10-16 01:27:13 -0700236 * or utimes() to apply them.
237 */
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700238 ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
239 if (attrs->ia_valid & ma) {
Al Viro39b743c2010-06-06 20:08:56 -0400240 err = stat_file(file, &st, fd);
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700241 if (err != 0)
242 return err;
243
Al Viro39b743c2010-06-06 20:08:56 -0400244 times[0].tv_sec = st.atime.tv_sec;
245 times[0].tv_usec = st.atime.tv_nsec / 1000;
246 times[1].tv_sec = st.mtime.tv_sec;
247 times[1].tv_usec = st.mtime.tv_nsec / 1000;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700248
249 if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
250 times[0].tv_sec = attrs->ia_atime.tv_sec;
Dominique Quatravauxd7b88512008-02-04 22:31:15 -0800251 times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700253 if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
254 times[1].tv_sec = attrs->ia_mtime.tv_sec;
Dominique Quatravauxd7b88512008-02-04 22:31:15 -0800255 times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700256 }
257
258 if (fd >= 0) {
259 if (futimes(fd, times) != 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700260 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700261 } else if (utimes(file, times) != 0) {
Jeff Dikef1adc052007-05-08 00:23:18 -0700262 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 }
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700265
Jeff Dikebaabd152007-10-16 01:27:13 -0700266 /* Note: ctime is not handled */
Jeff Dike84b3db02007-10-16 01:27:13 -0700267 if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
Al Viro39b743c2010-06-06 20:08:56 -0400268 err = stat_file(file, &st, fd);
269 attrs->ia_atime = st.atime;
270 attrs->ia_mtime = st.mtime;
Jeff Dike84b3db02007-10-16 01:27:13 -0700271 if (err != 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700272 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Jeff Dikef1adc052007-05-08 00:23:18 -0700274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277int make_symlink(const char *from, const char *to)
278{
279 int err;
280
281 err = symlink(to, from);
Jeff Dike84b3db02007-10-16 01:27:13 -0700282 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700283 return -errno;
284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287int unlink_file(const char *file)
288{
289 int err;
290
291 err = unlink(file);
Jeff Dike84b3db02007-10-16 01:27:13 -0700292 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700293 return -errno;
294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297int do_mkdir(const char *file, int mode)
298{
299 int err;
300
301 err = mkdir(file, mode);
Jeff Dike84b3db02007-10-16 01:27:13 -0700302 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700303 return -errno;
304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307int do_rmdir(const char *file)
308{
309 int err;
310
311 err = rmdir(file);
Jeff Dike84b3db02007-10-16 01:27:13 -0700312 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700313 return -errno;
314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Johannes Stezenbach88f6cd02007-01-29 13:19:44 -0800317int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 int err;
320
Al Viro005a59e2009-04-21 01:27:08 -0400321 err = mknod(file, mode, os_makedev(major, minor));
Jeff Dike84b3db02007-10-16 01:27:13 -0700322 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700323 return -errno;
324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327int link_file(const char *to, const char *from)
328{
329 int err;
330
331 err = link(to, from);
Jeff Dike84b3db02007-10-16 01:27:13 -0700332 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700333 return -errno;
334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
WANG Congea7e7432008-11-19 15:36:46 -0800337int hostfs_do_readlink(char *file, char *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 int n;
340
341 n = readlink(file, buf, size);
Jeff Dike84b3db02007-10-16 01:27:13 -0700342 if (n < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700343 return -errno;
Jeff Dike84b3db02007-10-16 01:27:13 -0700344 if (n < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 buf[n] = '\0';
Jeff Dikef1adc052007-05-08 00:23:18 -0700346 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349int rename_file(char *from, char *to)
350{
351 int err;
352
353 err = rename(from, to);
Jeff Dike84b3db02007-10-16 01:27:13 -0700354 if (err < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700355 return -errno;
356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Miklos Szeredi9a423bb2014-07-23 15:15:35 +0200359int rename2_file(char *from, char *to, unsigned int flags)
360{
361 int err;
362
363#ifndef SYS_renameat2
364# ifdef __x86_64__
365# define SYS_renameat2 316
366# endif
367# ifdef __i386__
368# define SYS_renameat2 353
369# endif
370#endif
371
372#ifdef SYS_renameat2
373 err = syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, flags);
374 if (err < 0) {
375 if (errno != ENOSYS)
376 return -errno;
377 else
378 return -EINVAL;
379 }
380 return 0;
381#else
382 return -EINVAL;
383#endif
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386int do_statfs(char *root, long *bsize_out, long long *blocks_out,
387 long long *bfree_out, long long *bavail_out,
388 long long *files_out, long long *ffree_out,
Richard Weinberger1b627d52010-10-26 14:21:18 -0700389 void *fsid_out, int fsid_size, long *namelen_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct statfs64 buf;
392 int err;
393
394 err = statfs64(root, &buf);
Jeff Dike84b3db02007-10-16 01:27:13 -0700395 if (err < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700396 return -errno;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 *bsize_out = buf.f_bsize;
399 *blocks_out = buf.f_blocks;
400 *bfree_out = buf.f_bfree;
401 *bavail_out = buf.f_bavail;
402 *files_out = buf.f_files;
403 *ffree_out = buf.f_ffree;
404 memcpy(fsid_out, &buf.f_fsid,
405 sizeof(buf.f_fsid) > fsid_size ? fsid_size :
406 sizeof(buf.f_fsid));
407 *namelen_out = buf.f_namelen;
Richard Weinberger1b627d52010-10-26 14:21:18 -0700408
Jeff Dikef1adc052007-05-08 00:23:18 -0700409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}