Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / fs / hostfs / hostfs_user.c
CommitLineData
1da177e4 1/*
f1adc05e 2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
1da177e4
LT
3 * Licensed under the GPL
4 */
5
1da177e4 6#include <stdio.h>
84b3db04
JD
7#include <stddef.h>
8#include <unistd.h>
1da177e4
LT
9#include <dirent.h>
10#include <errno.h>
84b3db04 11#include <fcntl.h>
1da177e4
LT
12#include <string.h>
13#include <sys/stat.h>
14#include <sys/time.h>
84b3db04 15#include <sys/types.h>
1da177e4 16#include <sys/vfs.h>
9a423bb6 17#include <sys/syscall.h>
1da177e4 18#include "hostfs.h"
84b3db04 19#include <utime.h>
1da177e4 20
39b743c6
AV
21static 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)
1da177e4
LT
42{
43 struct stat64 buf;
44
84b3db04 45 if (fd >= 0) {
5822b7fa 46 if (fstat64(fd, &buf) < 0)
f1adc05e 47 return -errno;
84b3db04 48 } else if (lstat64(path, &buf) < 0) {
f1adc05e 49 return -errno;
5822b7fa 50 }
39b743c6 51 stat64_to_hostfs(&buf, p);
f1adc05e 52 return 0;
1da177e4
LT
53}
54
1da177e4
LT
55int access_file(char *path, int r, int w, int x)
56{
57 int mode = 0;
58
84b3db04
JD
59 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)
f1adc05e
JD
66 return -errno;
67 else return 0;
1da177e4
LT
68}
69
70int open_file(char *path, int r, int w, int append)
71{
72 int mode = 0, fd;
73
84b3db04 74 if (r && !w)
1da177e4 75 mode = O_RDONLY;
84b3db04 76 else if (!r && w)
1da177e4 77 mode = O_WRONLY;
84b3db04 78 else if (r && w)
1da177e4
LT
79 mode = O_RDWR;
80 else panic("Impossible mode in open_file");
81
84b3db04 82 if (append)
1da177e4
LT
83 mode |= O_APPEND;
84 fd = open64(path, mode);
84b3db04 85 if (fd < 0)
f1adc05e
JD
86 return -errno;
87 else return fd;
1da177e4
LT
88}
89
90void *open_dir(char *path, int *err_out)
91{
92 DIR *dir;
93
94 dir = opendir(path);
95 *err_out = errno;
c5c6dd4e 96
f1adc05e 97 return dir;
1da177e4
LT
98}
99
0c9bd636
RW
100void 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,
3ee6bd8e
GU
108 unsigned long long *ino_out, int *len_out,
109 unsigned int *type_out)
1da177e4
LT
110{
111 DIR *dir = stream;
112 struct dirent *ent;
113
1da177e4 114 ent = readdir(dir);
84b3db04 115 if (ent == NULL)
f1adc05e 116 return NULL;
1da177e4
LT
117 *len_out = strlen(ent->d_name);
118 *ino_out = ent->d_ino;
3ee6bd8e 119 *type_out = ent->d_type;
0c9bd636 120 *pos_out = ent->d_off;
f1adc05e 121 return ent->d_name;
1da177e4
LT
122}
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);
84b3db04 129 if (n < 0)
f1adc05e 130 return -errno;
1da177e4 131 *offset += n;
f1adc05e 132 return n;
1da177e4
LT
133}
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);
84b3db04 140 if (n < 0)
f1adc05e 141 return -errno;
1da177e4 142 *offset += n;
f1adc05e 143 return n;
1da177e4
LT
144}
145
146int lseek_file(int fd, long long offset, int whence)
147{
148 int ret;
149
150 ret = lseek64(fd, offset, whence);
84b3db04 151 if (ret < 0)
f1adc05e
JD
152 return -errno;
153 return 0;
1da177e4
LT
154}
155
a2d76bd8
PBG
156int 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
f8ad850f
AV
169int replace_file(int oldfd, int fd)
170{
171 return dup2(oldfd, fd);
172}
173
1da177e4
LT
174void close_file(void *stream)
175{
176 close(*((int *) stream));
177}
178
179void close_dir(void *stream)
180{
181 closedir(stream);
182}
183
b98b9102 184int file_create(char *name, int mode)
1da177e4 185{
b98b9102
RW
186 int fd;
187
1da177e4 188 fd = open64(name, O_CREAT | O_RDWR, mode);
84b3db04 189 if (fd < 0)
f1adc05e
JD
190 return -errno;
191 return fd;
1da177e4
LT
192}
193
5822b7fa 194int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
1da177e4 195{
39b743c6 196 struct hostfs_stat st;
5822b7fa 197 struct timeval times[2];
1da177e4
LT
198 int err, ma;
199
5822b7fa
AB
200 if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
201 if (fd >= 0) {
202 if (fchmod(fd, attrs->ia_mode) != 0)
c5c6dd4e 203 return -errno;
5822b7fa 204 } else if (chmod(file, attrs->ia_mode) != 0) {
f1adc05e 205 return -errno;
5822b7fa 206 }
1da177e4 207 }
5822b7fa
AB
208 if (attrs->ia_valid & HOSTFS_ATTR_UID) {
209 if (fd >= 0) {
210 if (fchown(fd, attrs->ia_uid, -1))
f1adc05e 211 return -errno;
84b3db04 212 } else if (chown(file, attrs->ia_uid, -1)) {
f1adc05e 213 return -errno;
5822b7fa 214 }
1da177e4 215 }
5822b7fa
AB
216 if (attrs->ia_valid & HOSTFS_ATTR_GID) {
217 if (fd >= 0) {
218 if (fchown(fd, -1, attrs->ia_gid))
f1adc05e 219 return -errno;
5822b7fa 220 } else if (chown(file, -1, attrs->ia_gid)) {
f1adc05e 221 return -errno;
5822b7fa 222 }
1da177e4 223 }
5822b7fa
AB
224 if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
225 if (fd >= 0) {
226 if (ftruncate(fd, attrs->ia_size))
f1adc05e 227 return -errno;
5822b7fa 228 } else if (truncate(file, attrs->ia_size)) {
f1adc05e 229 return -errno;
5822b7fa 230 }
1da177e4 231 }
5822b7fa 232
84b3db04
JD
233 /*
234 * Update accessed and/or modified time, in two parts: first set
5822b7fa 235 * times according to the changes to perform, and then call futimes()
84b3db04
JD
236 * or utimes() to apply them.
237 */
5822b7fa
AB
238 ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
239 if (attrs->ia_valid & ma) {
39b743c6 240 err = stat_file(file, &st, fd);
5822b7fa
AB
241 if (err != 0)
242 return err;
243
39b743c6
AV
244 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;
5822b7fa
AB
248
249 if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
250 times[0].tv_sec = attrs->ia_atime.tv_sec;
d7b88513 251 times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
1da177e4 252 }
5822b7fa
AB
253 if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
254 times[1].tv_sec = attrs->ia_mtime.tv_sec;
d7b88513 255 times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
5822b7fa
AB
256 }
257
258 if (fd >= 0) {
259 if (futimes(fd, times) != 0)
f1adc05e 260 return -errno;
5822b7fa 261 } else if (utimes(file, times) != 0) {
f1adc05e 262 return -errno;
1da177e4
LT
263 }
264 }
5822b7fa 265
baabd156 266 /* Note: ctime is not handled */
84b3db04 267 if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
39b743c6
AV
268 err = stat_file(file, &st, fd);
269 attrs->ia_atime = st.atime;
270 attrs->ia_mtime = st.mtime;
84b3db04 271 if (err != 0)
f1adc05e 272 return err;
1da177e4 273 }
f1adc05e 274 return 0;
1da177e4
LT
275}
276
277int make_symlink(const char *from, const char *to)
278{
279 int err;
280
281 err = symlink(to, from);
84b3db04 282 if (err)
f1adc05e
JD
283 return -errno;
284 return 0;
1da177e4
LT
285}
286
287int unlink_file(const char *file)
288{
289 int err;
290
291 err = unlink(file);
84b3db04 292 if (err)
f1adc05e
JD
293 return -errno;
294 return 0;
1da177e4
LT
295}
296
297int do_mkdir(const char *file, int mode)
298{
299 int err;
300
301 err = mkdir(file, mode);
84b3db04 302 if (err)
f1adc05e
JD
303 return -errno;
304 return 0;
1da177e4
LT
305}
306
307int do_rmdir(const char *file)
308{
309 int err;
310
311 err = rmdir(file);
84b3db04 312 if (err)
f1adc05e
JD
313 return -errno;
314 return 0;
1da177e4
LT
315}
316
88f6cd0c 317int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
1da177e4
LT
318{
319 int err;
320
005a59ec 321 err = mknod(file, mode, os_makedev(major, minor));
84b3db04 322 if (err)
f1adc05e
JD
323 return -errno;
324 return 0;
1da177e4
LT
325}
326
327int link_file(const char *to, const char *from)
328{
329 int err;
330
331 err = link(to, from);
84b3db04 332 if (err)
f1adc05e
JD
333 return -errno;
334 return 0;
1da177e4
LT
335}
336
ea7e743e 337int hostfs_do_readlink(char *file, char *buf, int size)
1da177e4
LT
338{
339 int n;
340
341 n = readlink(file, buf, size);
84b3db04 342 if (n < 0)
f1adc05e 343 return -errno;
84b3db04 344 if (n < size)
1da177e4 345 buf[n] = '\0';
f1adc05e 346 return n;
1da177e4
LT
347}
348
349int rename_file(char *from, char *to)
350{
351 int err;
352
353 err = rename(from, to);
84b3db04 354 if (err < 0)
f1adc05e
JD
355 return -errno;
356 return 0;
1da177e4
LT
357}
358
9a423bb6
MS
359int 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
1da177e4
LT
386int 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,
1b627d57 389 void *fsid_out, int fsid_size, long *namelen_out)
1da177e4
LT
390{
391 struct statfs64 buf;
392 int err;
393
394 err = statfs64(root, &buf);
84b3db04 395 if (err < 0)
f1adc05e
JD
396 return -errno;
397
1da177e4
LT
398 *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;
1b627d57 408
f1adc05e 409 return 0;
1da177e4 410}