diff options
Diffstat (limited to 'fs/hostfs/hostfs_user.c')
-rw-r--r-- | fs/hostfs/hostfs_user.c | 139 |
1 files changed, 81 insertions, 58 deletions
diff --git a/fs/hostfs/hostfs_user.c b/fs/hostfs/hostfs_user.c index d8d54af1b497..35c1a9f33f47 100644 --- a/fs/hostfs/hostfs_user.c +++ b/fs/hostfs/hostfs_user.c | |||
@@ -3,19 +3,21 @@ | |||
3 | * Licensed under the GPL | 3 | * Licensed under the GPL |
4 | */ | 4 | */ |
5 | 5 | ||
6 | #include <unistd.h> | ||
7 | #include <stdio.h> | 6 | #include <stdio.h> |
8 | #include <fcntl.h> | 7 | #include <stddef.h> |
8 | #include <unistd.h> | ||
9 | #include <dirent.h> | 9 | #include <dirent.h> |
10 | #include <errno.h> | 10 | #include <errno.h> |
11 | #include <utime.h> | 11 | #include <fcntl.h> |
12 | #include <string.h> | 12 | #include <string.h> |
13 | #include <sys/stat.h> | 13 | #include <sys/stat.h> |
14 | #include <sys/time.h> | 14 | #include <sys/time.h> |
15 | #include <sys/types.h> | ||
15 | #include <sys/vfs.h> | 16 | #include <sys/vfs.h> |
16 | #include "hostfs.h" | 17 | #include "hostfs.h" |
17 | #include "kern_util.h" | 18 | #include "os.h" |
18 | #include "user.h" | 19 | #include "user.h" |
20 | #include <utime.h> | ||
19 | 21 | ||
20 | int stat_file(const char *path, unsigned long long *inode_out, int *mode_out, | 22 | int stat_file(const char *path, unsigned long long *inode_out, int *mode_out, |
21 | int *nlink_out, int *uid_out, int *gid_out, | 23 | int *nlink_out, int *uid_out, int *gid_out, |
@@ -25,33 +27,41 @@ int stat_file(const char *path, unsigned long long *inode_out, int *mode_out, | |||
25 | { | 27 | { |
26 | struct stat64 buf; | 28 | struct stat64 buf; |
27 | 29 | ||
28 | if(fd >= 0) { | 30 | if (fd >= 0) { |
29 | if (fstat64(fd, &buf) < 0) | 31 | if (fstat64(fd, &buf) < 0) |
30 | return -errno; | 32 | return -errno; |
31 | } else if(lstat64(path, &buf) < 0) { | 33 | } else if (lstat64(path, &buf) < 0) { |
32 | return -errno; | 34 | return -errno; |
33 | } | 35 | } |
34 | 36 | ||
35 | if(inode_out != NULL) *inode_out = buf.st_ino; | 37 | if (inode_out != NULL) |
36 | if(mode_out != NULL) *mode_out = buf.st_mode; | 38 | *inode_out = buf.st_ino; |
37 | if(nlink_out != NULL) *nlink_out = buf.st_nlink; | 39 | if (mode_out != NULL) |
38 | if(uid_out != NULL) *uid_out = buf.st_uid; | 40 | *mode_out = buf.st_mode; |
39 | if(gid_out != NULL) *gid_out = buf.st_gid; | 41 | if (nlink_out != NULL) |
40 | if(size_out != NULL) *size_out = buf.st_size; | 42 | *nlink_out = buf.st_nlink; |
41 | if(atime_out != NULL) { | 43 | if (uid_out != NULL) |
44 | *uid_out = buf.st_uid; | ||
45 | if (gid_out != NULL) | ||
46 | *gid_out = buf.st_gid; | ||
47 | if (size_out != NULL) | ||
48 | *size_out = buf.st_size; | ||
49 | if (atime_out != NULL) { | ||
42 | atime_out->tv_sec = buf.st_atime; | 50 | atime_out->tv_sec = buf.st_atime; |
43 | atime_out->tv_nsec = 0; | 51 | atime_out->tv_nsec = 0; |
44 | } | 52 | } |
45 | if(mtime_out != NULL) { | 53 | if (mtime_out != NULL) { |
46 | mtime_out->tv_sec = buf.st_mtime; | 54 | mtime_out->tv_sec = buf.st_mtime; |
47 | mtime_out->tv_nsec = 0; | 55 | mtime_out->tv_nsec = 0; |
48 | } | 56 | } |
49 | if(ctime_out != NULL) { | 57 | if (ctime_out != NULL) { |
50 | ctime_out->tv_sec = buf.st_ctime; | 58 | ctime_out->tv_sec = buf.st_ctime; |
51 | ctime_out->tv_nsec = 0; | 59 | ctime_out->tv_nsec = 0; |
52 | } | 60 | } |
53 | if(blksize_out != NULL) *blksize_out = buf.st_blksize; | 61 | if (blksize_out != NULL) |
54 | if(blocks_out != NULL) *blocks_out = buf.st_blocks; | 62 | *blksize_out = buf.st_blksize; |
63 | if (blocks_out != NULL) | ||
64 | *blocks_out = buf.st_blocks; | ||
55 | return 0; | 65 | return 0; |
56 | } | 66 | } |
57 | 67 | ||
@@ -59,21 +69,29 @@ int file_type(const char *path, int *maj, int *min) | |||
59 | { | 69 | { |
60 | struct stat64 buf; | 70 | struct stat64 buf; |
61 | 71 | ||
62 | if(lstat64(path, &buf) < 0) | 72 | if (lstat64(path, &buf) < 0) |
63 | return -errno; | 73 | return -errno; |
64 | /*We cannot pass rdev as is because glibc and the kernel disagree | 74 | /* |
65 | *about its definition.*/ | 75 | * We cannot pass rdev as is because glibc and the kernel disagree |
66 | if(maj != NULL) | 76 | * about its definition. |
77 | */ | ||
78 | if (maj != NULL) | ||
67 | *maj = major(buf.st_rdev); | 79 | *maj = major(buf.st_rdev); |
68 | if(min != NULL) | 80 | if (min != NULL) |
69 | *min = minor(buf.st_rdev); | 81 | *min = minor(buf.st_rdev); |
70 | 82 | ||
71 | if(S_ISDIR(buf.st_mode)) return OS_TYPE_DIR; | 83 | if (S_ISDIR(buf.st_mode)) |
72 | else if(S_ISLNK(buf.st_mode)) return OS_TYPE_SYMLINK; | 84 | return OS_TYPE_DIR; |
73 | else if(S_ISCHR(buf.st_mode)) return OS_TYPE_CHARDEV; | 85 | else if (S_ISLNK(buf.st_mode)) |
74 | else if(S_ISBLK(buf.st_mode)) return OS_TYPE_BLOCKDEV; | 86 | return OS_TYPE_SYMLINK; |
75 | else if(S_ISFIFO(buf.st_mode))return OS_TYPE_FIFO; | 87 | else if (S_ISCHR(buf.st_mode)) |
76 | else if(S_ISSOCK(buf.st_mode))return OS_TYPE_SOCK; | 88 | return OS_TYPE_CHARDEV; |
89 | else if (S_ISBLK(buf.st_mode)) | ||
90 | return OS_TYPE_BLOCKDEV; | ||
91 | else if (S_ISFIFO(buf.st_mode)) | ||
92 | return OS_TYPE_FIFO; | ||
93 | else if (S_ISSOCK(buf.st_mode)) | ||
94 | return OS_TYPE_SOCK; | ||
77 | else return OS_TYPE_FILE; | 95 | else return OS_TYPE_FILE; |
78 | } | 96 | } |
79 | 97 | ||
@@ -81,10 +99,13 @@ int access_file(char *path, int r, int w, int x) | |||
81 | { | 99 | { |
82 | int mode = 0; | 100 | int mode = 0; |
83 | 101 | ||
84 | if(r) mode = R_OK; | 102 | if (r) |
85 | if(w) mode |= W_OK; | 103 | mode = R_OK; |
86 | if(x) mode |= X_OK; | 104 | if (w) |
87 | if(access(path, mode) != 0) | 105 | mode |= W_OK; |
106 | if (x) | ||
107 | mode |= X_OK; | ||
108 | if (access(path, mode) != 0) | ||
88 | return -errno; | 109 | return -errno; |
89 | else return 0; | 110 | else return 0; |
90 | } | 111 | } |
@@ -93,18 +114,18 @@ int open_file(char *path, int r, int w, int append) | |||
93 | { | 114 | { |
94 | int mode = 0, fd; | 115 | int mode = 0, fd; |
95 | 116 | ||
96 | if(r && !w) | 117 | if (r && !w) |
97 | mode = O_RDONLY; | 118 | mode = O_RDONLY; |
98 | else if(!r && w) | 119 | else if (!r && w) |
99 | mode = O_WRONLY; | 120 | mode = O_WRONLY; |
100 | else if(r && w) | 121 | else if (r && w) |
101 | mode = O_RDWR; | 122 | mode = O_RDWR; |
102 | else panic("Impossible mode in open_file"); | 123 | else panic("Impossible mode in open_file"); |
103 | 124 | ||
104 | if(append) | 125 | if (append) |
105 | mode |= O_APPEND; | 126 | mode |= O_APPEND; |
106 | fd = open64(path, mode); | 127 | fd = open64(path, mode); |
107 | if(fd < 0) | 128 | if (fd < 0) |
108 | return -errno; | 129 | return -errno; |
109 | else return fd; | 130 | else return fd; |
110 | } | 131 | } |
@@ -115,7 +136,7 @@ void *open_dir(char *path, int *err_out) | |||
115 | 136 | ||
116 | dir = opendir(path); | 137 | dir = opendir(path); |
117 | *err_out = errno; | 138 | *err_out = errno; |
118 | if(dir == NULL) | 139 | if (dir == NULL) |
119 | return NULL; | 140 | return NULL; |
120 | return dir; | 141 | return dir; |
121 | } | 142 | } |
@@ -128,7 +149,7 @@ char *read_dir(void *stream, unsigned long long *pos, | |||
128 | 149 | ||
129 | seekdir(dir, *pos); | 150 | seekdir(dir, *pos); |
130 | ent = readdir(dir); | 151 | ent = readdir(dir); |
131 | if(ent == NULL) | 152 | if (ent == NULL) |
132 | return NULL; | 153 | return NULL; |
133 | *len_out = strlen(ent->d_name); | 154 | *len_out = strlen(ent->d_name); |
134 | *ino_out = ent->d_ino; | 155 | *ino_out = ent->d_ino; |
@@ -141,7 +162,7 @@ int read_file(int fd, unsigned long long *offset, char *buf, int len) | |||
141 | int n; | 162 | int n; |
142 | 163 | ||
143 | n = pread64(fd, buf, len, *offset); | 164 | n = pread64(fd, buf, len, *offset); |
144 | if(n < 0) | 165 | if (n < 0) |
145 | return -errno; | 166 | return -errno; |
146 | *offset += n; | 167 | *offset += n; |
147 | return n; | 168 | return n; |
@@ -152,7 +173,7 @@ int write_file(int fd, unsigned long long *offset, const char *buf, int len) | |||
152 | int n; | 173 | int n; |
153 | 174 | ||
154 | n = pwrite64(fd, buf, len, *offset); | 175 | n = pwrite64(fd, buf, len, *offset); |
155 | if(n < 0) | 176 | if (n < 0) |
156 | return -errno; | 177 | return -errno; |
157 | *offset += n; | 178 | *offset += n; |
158 | return n; | 179 | return n; |
@@ -163,7 +184,7 @@ int lseek_file(int fd, long long offset, int whence) | |||
163 | int ret; | 184 | int ret; |
164 | 185 | ||
165 | ret = lseek64(fd, offset, whence); | 186 | ret = lseek64(fd, offset, whence); |
166 | if(ret < 0) | 187 | if (ret < 0) |
167 | return -errno; | 188 | return -errno; |
168 | return 0; | 189 | return 0; |
169 | } | 190 | } |
@@ -207,7 +228,7 @@ int file_create(char *name, int ur, int uw, int ux, int gr, | |||
207 | mode |= ow ? S_IWOTH : 0; | 228 | mode |= ow ? S_IWOTH : 0; |
208 | mode |= ox ? S_IXOTH : 0; | 229 | mode |= ox ? S_IXOTH : 0; |
209 | fd = open64(name, O_CREAT | O_RDWR, mode); | 230 | fd = open64(name, O_CREAT | O_RDWR, mode); |
210 | if(fd < 0) | 231 | if (fd < 0) |
211 | return -errno; | 232 | return -errno; |
212 | return fd; | 233 | return fd; |
213 | } | 234 | } |
@@ -230,7 +251,7 @@ int set_attr(const char *file, struct hostfs_iattr *attrs, int fd) | |||
230 | if (fd >= 0) { | 251 | if (fd >= 0) { |
231 | if (fchown(fd, attrs->ia_uid, -1)) | 252 | if (fchown(fd, attrs->ia_uid, -1)) |
232 | return -errno; | 253 | return -errno; |
233 | } else if(chown(file, attrs->ia_uid, -1)) { | 254 | } else if (chown(file, attrs->ia_uid, -1)) { |
234 | return -errno; | 255 | return -errno; |
235 | } | 256 | } |
236 | } | 257 | } |
@@ -251,9 +272,11 @@ int set_attr(const char *file, struct hostfs_iattr *attrs, int fd) | |||
251 | } | 272 | } |
252 | } | 273 | } |
253 | 274 | ||
254 | /* Update accessed and/or modified time, in two parts: first set | 275 | /* |
276 | * Update accessed and/or modified time, in two parts: first set | ||
255 | * times according to the changes to perform, and then call futimes() | 277 | * times according to the changes to perform, and then call futimes() |
256 | * or utimes() to apply them. */ | 278 | * or utimes() to apply them. |
279 | */ | ||
257 | ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET); | 280 | ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET); |
258 | if (attrs->ia_valid & ma) { | 281 | if (attrs->ia_valid & ma) { |
259 | err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL, | 282 | err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL, |
@@ -284,11 +307,11 @@ int set_attr(const char *file, struct hostfs_iattr *attrs, int fd) | |||
284 | } | 307 | } |
285 | 308 | ||
286 | /* Note: ctime is not handled */ | 309 | /* Note: ctime is not handled */ |
287 | if(attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)){ | 310 | if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) { |
288 | err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL, | 311 | err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL, |
289 | &attrs->ia_atime, &attrs->ia_mtime, NULL, | 312 | &attrs->ia_atime, &attrs->ia_mtime, NULL, |
290 | NULL, NULL, fd); | 313 | NULL, NULL, fd); |
291 | if(err != 0) | 314 | if (err != 0) |
292 | return err; | 315 | return err; |
293 | } | 316 | } |
294 | return 0; | 317 | return 0; |
@@ -299,7 +322,7 @@ int make_symlink(const char *from, const char *to) | |||
299 | int err; | 322 | int err; |
300 | 323 | ||
301 | err = symlink(to, from); | 324 | err = symlink(to, from); |
302 | if(err) | 325 | if (err) |
303 | return -errno; | 326 | return -errno; |
304 | return 0; | 327 | return 0; |
305 | } | 328 | } |
@@ -309,7 +332,7 @@ int unlink_file(const char *file) | |||
309 | int err; | 332 | int err; |
310 | 333 | ||
311 | err = unlink(file); | 334 | err = unlink(file); |
312 | if(err) | 335 | if (err) |
313 | return -errno; | 336 | return -errno; |
314 | return 0; | 337 | return 0; |
315 | } | 338 | } |
@@ -319,7 +342,7 @@ int do_mkdir(const char *file, int mode) | |||
319 | int err; | 342 | int err; |
320 | 343 | ||
321 | err = mkdir(file, mode); | 344 | err = mkdir(file, mode); |
322 | if(err) | 345 | if (err) |
323 | return -errno; | 346 | return -errno; |
324 | return 0; | 347 | return 0; |
325 | } | 348 | } |
@@ -329,7 +352,7 @@ int do_rmdir(const char *file) | |||
329 | int err; | 352 | int err; |
330 | 353 | ||
331 | err = rmdir(file); | 354 | err = rmdir(file); |
332 | if(err) | 355 | if (err) |
333 | return -errno; | 356 | return -errno; |
334 | return 0; | 357 | return 0; |
335 | } | 358 | } |
@@ -339,7 +362,7 @@ int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor) | |||
339 | int err; | 362 | int err; |
340 | 363 | ||
341 | err = mknod(file, mode, makedev(major, minor)); | 364 | err = mknod(file, mode, makedev(major, minor)); |
342 | if(err) | 365 | if (err) |
343 | return -errno; | 366 | return -errno; |
344 | return 0; | 367 | return 0; |
345 | } | 368 | } |
@@ -349,7 +372,7 @@ int link_file(const char *to, const char *from) | |||
349 | int err; | 372 | int err; |
350 | 373 | ||
351 | err = link(to, from); | 374 | err = link(to, from); |
352 | if(err) | 375 | if (err) |
353 | return -errno; | 376 | return -errno; |
354 | return 0; | 377 | return 0; |
355 | } | 378 | } |
@@ -359,9 +382,9 @@ int do_readlink(char *file, char *buf, int size) | |||
359 | int n; | 382 | int n; |
360 | 383 | ||
361 | n = readlink(file, buf, size); | 384 | n = readlink(file, buf, size); |
362 | if(n < 0) | 385 | if (n < 0) |
363 | return -errno; | 386 | return -errno; |
364 | if(n < size) | 387 | if (n < size) |
365 | buf[n] = '\0'; | 388 | buf[n] = '\0'; |
366 | return n; | 389 | return n; |
367 | } | 390 | } |
@@ -371,7 +394,7 @@ int rename_file(char *from, char *to) | |||
371 | int err; | 394 | int err; |
372 | 395 | ||
373 | err = rename(from, to); | 396 | err = rename(from, to); |
374 | if(err < 0) | 397 | if (err < 0) |
375 | return -errno; | 398 | return -errno; |
376 | return 0; | 399 | return 0; |
377 | } | 400 | } |
@@ -386,7 +409,7 @@ int do_statfs(char *root, long *bsize_out, long long *blocks_out, | |||
386 | int err; | 409 | int err; |
387 | 410 | ||
388 | err = statfs64(root, &buf); | 411 | err = statfs64(root, &buf); |
389 | if(err < 0) | 412 | if (err < 0) |
390 | return -errno; | 413 | return -errno; |
391 | 414 | ||
392 | *bsize_out = buf.f_bsize; | 415 | *bsize_out = buf.f_bsize; |