blob: d51a98384bc03b0410f7ba3d5c021804511d10bf [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>
17#include "hostfs.h"
Jeff Dike84b3db02007-10-16 01:27:13 -070018#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "user.h"
Jeff Dike84b3db02007-10-16 01:27:13 -070020#include <utime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Al Viro39b743c2010-06-06 20:08:56 -040022static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p)
23{
24 p->ino = buf->st_ino;
25 p->mode = buf->st_mode;
26 p->nlink = buf->st_nlink;
27 p->uid = buf->st_uid;
28 p->gid = buf->st_gid;
29 p->size = buf->st_size;
30 p->atime.tv_sec = buf->st_atime;
31 p->atime.tv_nsec = 0;
32 p->ctime.tv_sec = buf->st_ctime;
33 p->ctime.tv_nsec = 0;
34 p->mtime.tv_sec = buf->st_mtime;
35 p->mtime.tv_nsec = 0;
36 p->blksize = buf->st_blksize;
37 p->blocks = buf->st_blocks;
38 p->maj = os_major(buf->st_rdev);
39 p->min = os_minor(buf->st_rdev);
40}
41
42int stat_file(const char *path, struct hostfs_stat *p, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 struct stat64 buf;
45
Jeff Dike84b3db02007-10-16 01:27:13 -070046 if (fd >= 0) {
Alberto Bertogli5822b7f2007-05-08 00:23:16 -070047 if (fstat64(fd, &buf) < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -070048 return -errno;
Jeff Dike84b3db02007-10-16 01:27:13 -070049 } else if (lstat64(path, &buf) < 0) {
Jeff Dikef1adc052007-05-08 00:23:18 -070050 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -070051 }
Al Viro39b743c2010-06-06 20:08:56 -040052 stat64_to_hostfs(&buf, p);
Jeff Dikef1adc052007-05-08 00:23:18 -070053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056int access_file(char *path, int r, int w, int x)
57{
58 int mode = 0;
59
Jeff Dike84b3db02007-10-16 01:27:13 -070060 if (r)
61 mode = R_OK;
62 if (w)
63 mode |= W_OK;
64 if (x)
65 mode |= X_OK;
66 if (access(path, mode) != 0)
Jeff Dikef1adc052007-05-08 00:23:18 -070067 return -errno;
68 else return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71int open_file(char *path, int r, int w, int append)
72{
73 int mode = 0, fd;
74
Jeff Dike84b3db02007-10-16 01:27:13 -070075 if (r && !w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 mode = O_RDONLY;
Jeff Dike84b3db02007-10-16 01:27:13 -070077 else if (!r && w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 mode = O_WRONLY;
Jeff Dike84b3db02007-10-16 01:27:13 -070079 else if (r && w)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 mode = O_RDWR;
81 else panic("Impossible mode in open_file");
82
Jeff Dike84b3db02007-10-16 01:27:13 -070083 if (append)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 mode |= O_APPEND;
85 fd = open64(path, mode);
Jeff Dike84b3db02007-10-16 01:27:13 -070086 if (fd < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -070087 return -errno;
88 else return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91void *open_dir(char *path, int *err_out)
92{
93 DIR *dir;
94
95 dir = opendir(path);
96 *err_out = errno;
Richard Weinbergerc5c6dd42010-10-26 14:22:20 -070097
Jeff Dikef1adc052007-05-08 00:23:18 -070098 return dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101char *read_dir(void *stream, unsigned long long *pos,
102 unsigned long long *ino_out, int *len_out)
103{
104 DIR *dir = stream;
105 struct dirent *ent;
106
107 seekdir(dir, *pos);
108 ent = readdir(dir);
Jeff Dike84b3db02007-10-16 01:27:13 -0700109 if (ent == NULL)
Jeff Dikef1adc052007-05-08 00:23:18 -0700110 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *len_out = strlen(ent->d_name);
112 *ino_out = ent->d_ino;
113 *pos = telldir(dir);
Jeff Dikef1adc052007-05-08 00:23:18 -0700114 return ent->d_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117int read_file(int fd, unsigned long long *offset, char *buf, int len)
118{
119 int n;
120
121 n = pread64(fd, buf, len, *offset);
Jeff Dike84b3db02007-10-16 01:27:13 -0700122 if (n < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700123 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *offset += n;
Jeff Dikef1adc052007-05-08 00:23:18 -0700125 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128int write_file(int fd, unsigned long long *offset, const char *buf, int len)
129{
130 int n;
131
132 n = pwrite64(fd, buf, len, *offset);
Jeff Dike84b3db02007-10-16 01:27:13 -0700133 if (n < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700134 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *offset += n;
Jeff Dikef1adc052007-05-08 00:23:18 -0700136 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139int lseek_file(int fd, long long offset, int whence)
140{
141 int ret;
142
143 ret = lseek64(fd, offset, whence);
Jeff Dike84b3db02007-10-16 01:27:13 -0700144 if (ret < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700145 return -errno;
146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Paolo 'Blaisorblade' Giarrussoa2d76bd2005-07-28 21:16:15 -0700149int fsync_file(int fd, int datasync)
150{
151 int ret;
152 if (datasync)
153 ret = fdatasync(fd);
154 else
155 ret = fsync(fd);
156
157 if (ret < 0)
158 return -errno;
159 return 0;
160}
161
Al Virof8ad8502010-06-06 23:49:18 -0400162int replace_file(int oldfd, int fd)
163{
164 return dup2(oldfd, fd);
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167void close_file(void *stream)
168{
169 close(*((int *) stream));
170}
171
172void close_dir(void *stream)
173{
174 closedir(stream);
175}
176
177int file_create(char *name, int ur, int uw, int ux, int gr,
178 int gw, int gx, int or, int ow, int ox)
179{
180 int mode, fd;
181
182 mode = 0;
183 mode |= ur ? S_IRUSR : 0;
184 mode |= uw ? S_IWUSR : 0;
185 mode |= ux ? S_IXUSR : 0;
186 mode |= gr ? S_IRGRP : 0;
187 mode |= gw ? S_IWGRP : 0;
188 mode |= gx ? S_IXGRP : 0;
189 mode |= or ? S_IROTH : 0;
190 mode |= ow ? S_IWOTH : 0;
191 mode |= ox ? S_IXOTH : 0;
192 fd = open64(name, O_CREAT | O_RDWR, mode);
Jeff Dike84b3db02007-10-16 01:27:13 -0700193 if (fd < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700194 return -errno;
195 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700198int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Al Viro39b743c2010-06-06 20:08:56 -0400200 struct hostfs_stat st;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700201 struct timeval times[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int err, ma;
203
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700204 if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
205 if (fd >= 0) {
206 if (fchmod(fd, attrs->ia_mode) != 0)
Richard Weinbergerc5c6dd42010-10-26 14:22:20 -0700207 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700208 } else if (chmod(file, attrs->ia_mode) != 0) {
Jeff Dikef1adc052007-05-08 00:23:18 -0700209 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700212 if (attrs->ia_valid & HOSTFS_ATTR_UID) {
213 if (fd >= 0) {
214 if (fchown(fd, attrs->ia_uid, -1))
Jeff Dikef1adc052007-05-08 00:23:18 -0700215 return -errno;
Jeff Dike84b3db02007-10-16 01:27:13 -0700216 } else if (chown(file, attrs->ia_uid, -1)) {
Jeff Dikef1adc052007-05-08 00:23:18 -0700217 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700220 if (attrs->ia_valid & HOSTFS_ATTR_GID) {
221 if (fd >= 0) {
222 if (fchown(fd, -1, attrs->ia_gid))
Jeff Dikef1adc052007-05-08 00:23:18 -0700223 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700224 } else if (chown(file, -1, attrs->ia_gid)) {
Jeff Dikef1adc052007-05-08 00:23:18 -0700225 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700228 if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
229 if (fd >= 0) {
230 if (ftruncate(fd, attrs->ia_size))
Jeff Dikef1adc052007-05-08 00:23:18 -0700231 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700232 } else if (truncate(file, attrs->ia_size)) {
Jeff Dikef1adc052007-05-08 00:23:18 -0700233 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jeff Dike84b3db02007-10-16 01:27:13 -0700237 /*
238 * Update accessed and/or modified time, in two parts: first set
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700239 * times according to the changes to perform, and then call futimes()
Jeff Dike84b3db02007-10-16 01:27:13 -0700240 * or utimes() to apply them.
241 */
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700242 ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
243 if (attrs->ia_valid & ma) {
Al Viro39b743c2010-06-06 20:08:56 -0400244 err = stat_file(file, &st, fd);
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700245 if (err != 0)
246 return err;
247
Al Viro39b743c2010-06-06 20:08:56 -0400248 times[0].tv_sec = st.atime.tv_sec;
249 times[0].tv_usec = st.atime.tv_nsec / 1000;
250 times[1].tv_sec = st.mtime.tv_sec;
251 times[1].tv_usec = st.mtime.tv_nsec / 1000;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700252
253 if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
254 times[0].tv_sec = attrs->ia_atime.tv_sec;
Dominique Quatravauxd7b88512008-02-04 22:31:15 -0800255 times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700257 if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
258 times[1].tv_sec = attrs->ia_mtime.tv_sec;
Dominique Quatravauxd7b88512008-02-04 22:31:15 -0800259 times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700260 }
261
262 if (fd >= 0) {
263 if (futimes(fd, times) != 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700264 return -errno;
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700265 } else if (utimes(file, times) != 0) {
Jeff Dikef1adc052007-05-08 00:23:18 -0700266 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 }
Alberto Bertogli5822b7f2007-05-08 00:23:16 -0700269
Jeff Dikebaabd152007-10-16 01:27:13 -0700270 /* Note: ctime is not handled */
Jeff Dike84b3db02007-10-16 01:27:13 -0700271 if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
Al Viro39b743c2010-06-06 20:08:56 -0400272 err = stat_file(file, &st, fd);
273 attrs->ia_atime = st.atime;
274 attrs->ia_mtime = st.mtime;
Jeff Dike84b3db02007-10-16 01:27:13 -0700275 if (err != 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Jeff Dikef1adc052007-05-08 00:23:18 -0700278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281int make_symlink(const char *from, const char *to)
282{
283 int err;
284
285 err = symlink(to, from);
Jeff Dike84b3db02007-10-16 01:27:13 -0700286 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700287 return -errno;
288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291int unlink_file(const char *file)
292{
293 int err;
294
295 err = unlink(file);
Jeff Dike84b3db02007-10-16 01:27:13 -0700296 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700297 return -errno;
298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301int do_mkdir(const char *file, int mode)
302{
303 int err;
304
305 err = mkdir(file, mode);
Jeff Dike84b3db02007-10-16 01:27:13 -0700306 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700307 return -errno;
308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311int do_rmdir(const char *file)
312{
313 int err;
314
315 err = rmdir(file);
Jeff Dike84b3db02007-10-16 01:27:13 -0700316 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700317 return -errno;
318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Johannes Stezenbach88f6cd02007-01-29 13:19:44 -0800321int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int err;
324
Al Viro005a59e2009-04-21 01:27:08 -0400325 err = mknod(file, mode, os_makedev(major, minor));
Jeff Dike84b3db02007-10-16 01:27:13 -0700326 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700327 return -errno;
328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331int link_file(const char *to, const char *from)
332{
333 int err;
334
335 err = link(to, from);
Jeff Dike84b3db02007-10-16 01:27:13 -0700336 if (err)
Jeff Dikef1adc052007-05-08 00:23:18 -0700337 return -errno;
338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
WANG Congea7e7432008-11-19 15:36:46 -0800341int hostfs_do_readlink(char *file, char *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 int n;
344
345 n = readlink(file, buf, size);
Jeff Dike84b3db02007-10-16 01:27:13 -0700346 if (n < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700347 return -errno;
Jeff Dike84b3db02007-10-16 01:27:13 -0700348 if (n < size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 buf[n] = '\0';
Jeff Dikef1adc052007-05-08 00:23:18 -0700350 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353int rename_file(char *from, char *to)
354{
355 int err;
356
357 err = rename(from, to);
Jeff Dike84b3db02007-10-16 01:27:13 -0700358 if (err < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700359 return -errno;
360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363int do_statfs(char *root, long *bsize_out, long long *blocks_out,
364 long long *bfree_out, long long *bavail_out,
365 long long *files_out, long long *ffree_out,
Richard Weinberger1b627d52010-10-26 14:21:18 -0700366 void *fsid_out, int fsid_size, long *namelen_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct statfs64 buf;
369 int err;
370
371 err = statfs64(root, &buf);
Jeff Dike84b3db02007-10-16 01:27:13 -0700372 if (err < 0)
Jeff Dikef1adc052007-05-08 00:23:18 -0700373 return -errno;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *bsize_out = buf.f_bsize;
376 *blocks_out = buf.f_blocks;
377 *bfree_out = buf.f_bfree;
378 *bavail_out = buf.f_bavail;
379 *files_out = buf.f_files;
380 *ffree_out = buf.f_ffree;
381 memcpy(fsid_out, &buf.f_fsid,
382 sizeof(buf.f_fsid) > fsid_size ? fsid_size :
383 sizeof(buf.f_fsid));
384 *namelen_out = buf.f_namelen;
Richard Weinberger1b627d52010-10-26 14:21:18 -0700385
Jeff Dikef1adc052007-05-08 00:23:18 -0700386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}