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