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