0acf562a5f06f3932a6234db0ac38c4c0386749d
[safe/jmp/linux-2.6] / fs / hostfs / hostfs_user.c
1 /*
2  * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <dirent.h>
10 #include <errno.h>
11 #include <utime.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/time.h>
15 #include <sys/vfs.h>
16 #include "hostfs.h"
17 #include "kern_util.h"
18 #include "user.h"
19
20 int stat_file(const char *path, unsigned long long *inode_out, int *mode_out,
21               int *nlink_out, int *uid_out, int *gid_out,
22               unsigned long long *size_out, struct timespec *atime_out,
23               struct timespec *mtime_out, struct timespec *ctime_out,
24               int *blksize_out, unsigned long long *blocks_out, int fd)
25 {
26         struct stat64 buf;
27
28         if(fd >= 0) {
29                 if (fstat64(fd, &buf) < 0)
30                         return(-errno);
31         } else if(lstat64(path, &buf) < 0) {
32                 return(-errno);
33         }
34
35         if(inode_out != NULL) *inode_out = buf.st_ino;
36         if(mode_out != NULL) *mode_out = buf.st_mode;
37         if(nlink_out != NULL) *nlink_out = buf.st_nlink;
38         if(uid_out != NULL) *uid_out = buf.st_uid;
39         if(gid_out != NULL) *gid_out = buf.st_gid;
40         if(size_out != NULL) *size_out = buf.st_size;
41         if(atime_out != NULL) {
42                 atime_out->tv_sec = buf.st_atime;
43                 atime_out->tv_nsec = 0;
44         }
45         if(mtime_out != NULL) {
46                 mtime_out->tv_sec = buf.st_mtime;
47                 mtime_out->tv_nsec = 0;
48         }
49         if(ctime_out != NULL) {
50                 ctime_out->tv_sec = buf.st_ctime;
51                 ctime_out->tv_nsec = 0;
52         }
53         if(blksize_out != NULL) *blksize_out = buf.st_blksize;
54         if(blocks_out != NULL) *blocks_out = buf.st_blocks;
55         return(0);
56 }
57
58 int file_type(const char *path, int *maj, int *min)
59 {
60         struct stat64 buf;
61
62         if(lstat64(path, &buf) < 0)
63                 return(-errno);
64         /*We cannot pass rdev as is because glibc and the kernel disagree
65          *about its definition.*/
66         if(maj != NULL)
67                 *maj = major(buf.st_rdev);
68         if(min != NULL)
69                 *min = minor(buf.st_rdev);
70
71         if(S_ISDIR(buf.st_mode)) return(OS_TYPE_DIR);
72         else if(S_ISLNK(buf.st_mode)) return(OS_TYPE_SYMLINK);
73         else if(S_ISCHR(buf.st_mode)) return(OS_TYPE_CHARDEV);
74         else if(S_ISBLK(buf.st_mode)) return(OS_TYPE_BLOCKDEV);
75         else if(S_ISFIFO(buf.st_mode))return(OS_TYPE_FIFO);
76         else if(S_ISSOCK(buf.st_mode))return(OS_TYPE_SOCK);
77         else return(OS_TYPE_FILE);
78 }
79
80 int access_file(char *path, int r, int w, int x)
81 {
82         int mode = 0;
83
84         if(r) mode = R_OK;
85         if(w) mode |= W_OK;
86         if(x) mode |= X_OK;
87         if(access(path, mode) != 0) return(-errno);
88         else return(0);
89 }
90
91 int open_file(char *path, int r, int w, int append)
92 {
93         int mode = 0, fd;
94
95         if(r && !w)
96                 mode = O_RDONLY;
97         else if(!r && w)
98                 mode = O_WRONLY;
99         else if(r && w)
100                 mode = O_RDWR;
101         else panic("Impossible mode in open_file");
102
103         if(append)
104                 mode |= O_APPEND;
105         fd = open64(path, mode);
106         if(fd < 0) return(-errno);
107         else return(fd);
108 }
109
110 void *open_dir(char *path, int *err_out)
111 {
112         DIR *dir;
113
114         dir = opendir(path);
115         *err_out = errno;
116         if(dir == NULL) return(NULL);
117         return(dir);
118 }
119
120 char *read_dir(void *stream, unsigned long long *pos,
121                unsigned long long *ino_out, int *len_out)
122 {
123         DIR *dir = stream;
124         struct dirent *ent;
125
126         seekdir(dir, *pos);
127         ent = readdir(dir);
128         if(ent == NULL) return(NULL);
129         *len_out = strlen(ent->d_name);
130         *ino_out = ent->d_ino;
131         *pos = telldir(dir);
132         return(ent->d_name);
133 }
134
135 int read_file(int fd, unsigned long long *offset, char *buf, int len)
136 {
137         int n;
138
139         n = pread64(fd, buf, len, *offset);
140         if(n < 0) return(-errno);
141         *offset += n;
142         return(n);
143 }
144
145 int write_file(int fd, unsigned long long *offset, const char *buf, int len)
146 {
147         int n;
148
149         n = pwrite64(fd, buf, len, *offset);
150         if(n < 0) return(-errno);
151         *offset += n;
152         return(n);
153 }
154
155 int lseek_file(int fd, long long offset, int whence)
156 {
157         int ret;
158
159         ret = lseek64(fd, offset, whence);
160         if(ret < 0)
161                 return(-errno);
162         return(0);
163 }
164
165 int fsync_file(int fd, int datasync)
166 {
167         int ret;
168         if (datasync)
169                 ret = fdatasync(fd);
170         else
171                 ret = fsync(fd);
172
173         if (ret < 0)
174                 return -errno;
175         return 0;
176 }
177
178 void close_file(void *stream)
179 {
180         close(*((int *) stream));
181 }
182
183 void close_dir(void *stream)
184 {
185         closedir(stream);
186 }
187
188 int file_create(char *name, int ur, int uw, int ux, int gr,
189                 int gw, int gx, int or, int ow, int ox)
190 {
191         int mode, fd;
192
193         mode = 0;
194         mode |= ur ? S_IRUSR : 0;
195         mode |= uw ? S_IWUSR : 0;
196         mode |= ux ? S_IXUSR : 0;
197         mode |= gr ? S_IRGRP : 0;
198         mode |= gw ? S_IWGRP : 0;
199         mode |= gx ? S_IXGRP : 0;
200         mode |= or ? S_IROTH : 0;
201         mode |= ow ? S_IWOTH : 0;
202         mode |= ox ? S_IXOTH : 0;
203         fd = open64(name, O_CREAT | O_RDWR, mode);
204         if(fd < 0)
205                 return(-errno);
206         return(fd);
207 }
208
209 int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
210 {
211         struct timeval times[2];
212         struct timespec atime_ts, mtime_ts;
213         int err, ma;
214
215         if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
216                 if (fd >= 0) {
217                         if (fchmod(fd, attrs->ia_mode) != 0)
218                                 return (-errno);
219                 } else if (chmod(file, attrs->ia_mode) != 0) {
220                         return (-errno);
221                 }
222         }
223         if (attrs->ia_valid & HOSTFS_ATTR_UID) {
224                 if (fd >= 0) {
225                         if (fchown(fd, attrs->ia_uid, -1))
226                                 return (-errno);
227                 } else if(chown(file, attrs->ia_uid, -1)) {
228                         return (-errno);
229                 }
230         }
231         if (attrs->ia_valid & HOSTFS_ATTR_GID) {
232                 if (fd >= 0) {
233                         if (fchown(fd, -1, attrs->ia_gid))
234                                 return (-errno);
235                 } else if (chown(file, -1, attrs->ia_gid)) {
236                         return (-errno);
237                 }
238         }
239         if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
240                 if (fd >= 0) {
241                         if (ftruncate(fd, attrs->ia_size))
242                                 return (-errno);
243                 } else if (truncate(file, attrs->ia_size)) {
244                         return (-errno);
245                 }
246         }
247
248         /* Update accessed and/or modified time, in two parts: first set
249          * times according to the changes to perform, and then call futimes()
250          * or utimes() to apply them. */
251         ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
252         if (attrs->ia_valid & ma) {
253                 err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL,
254                                 &atime_ts, &mtime_ts, NULL, NULL, NULL, fd);
255                 if (err != 0)
256                         return err;
257
258                 times[0].tv_sec = atime_ts.tv_sec;
259                 times[0].tv_usec = atime_ts.tv_nsec * 1000;
260                 times[1].tv_sec = mtime_ts.tv_sec;
261                 times[1].tv_usec = mtime_ts.tv_nsec * 1000;
262
263                 if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
264                         times[0].tv_sec = attrs->ia_atime.tv_sec;
265                         times[0].tv_usec = attrs->ia_atime.tv_nsec * 1000;
266                 }
267                 if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
268                         times[1].tv_sec = attrs->ia_mtime.tv_sec;
269                         times[1].tv_usec = attrs->ia_mtime.tv_nsec * 1000;
270                 }
271
272                 if (fd >= 0) {
273                         if (futimes(fd, times) != 0)
274                                 return (-errno);
275                 } else if (utimes(file, times) != 0) {
276                         return (-errno);
277                 }
278         }
279
280         if(attrs->ia_valid & HOSTFS_ATTR_CTIME) ;
281         if(attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)){
282                 err = stat_file(file, NULL, NULL, NULL, NULL, NULL, NULL,
283                                 &attrs->ia_atime, &attrs->ia_mtime, NULL,
284                                 NULL, NULL, fd);
285                 if(err != 0) return(err);
286         }
287         return(0);
288 }
289
290 int make_symlink(const char *from, const char *to)
291 {
292         int err;
293
294         err = symlink(to, from);
295         if(err) return(-errno);
296         return(0);
297 }
298
299 int unlink_file(const char *file)
300 {
301         int err;
302
303         err = unlink(file);
304         if(err) return(-errno);
305         return(0);
306 }
307
308 int do_mkdir(const char *file, int mode)
309 {
310         int err;
311
312         err = mkdir(file, mode);
313         if(err) return(-errno);
314         return(0);
315 }
316
317 int do_rmdir(const char *file)
318 {
319         int err;
320
321         err = rmdir(file);
322         if(err) return(-errno);
323         return(0);
324 }
325
326 int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
327 {
328         int err;
329
330         err = mknod(file, mode, makedev(major, minor));
331         if(err) return(-errno);
332         return(0);
333 }
334
335 int link_file(const char *to, const char *from)
336 {
337         int err;
338
339         err = link(to, from);
340         if(err) return(-errno);
341         return(0);
342 }
343
344 int do_readlink(char *file, char *buf, int size)
345 {
346         int n;
347
348         n = readlink(file, buf, size);
349         if(n < 0)
350                 return(-errno);
351         if(n < size)
352                 buf[n] = '\0';
353         return(n);
354 }
355
356 int rename_file(char *from, char *to)
357 {
358         int err;
359
360         err = rename(from, to);
361         if(err < 0) return(-errno);
362         return(0);
363 }
364
365 int do_statfs(char *root, long *bsize_out, long long *blocks_out,
366               long long *bfree_out, long long *bavail_out,
367               long long *files_out, long long *ffree_out,
368               void *fsid_out, int fsid_size, long *namelen_out,
369               long *spare_out)
370 {
371         struct statfs64 buf;
372         int err;
373
374         err = statfs64(root, &buf);
375         if(err < 0) return(-errno);
376         *bsize_out = buf.f_bsize;
377         *blocks_out = buf.f_blocks;
378         *bfree_out = buf.f_bfree;
379         *bavail_out = buf.f_bavail;
380         *files_out = buf.f_files;
381         *ffree_out = buf.f_ffree;
382         memcpy(fsid_out, &buf.f_fsid,
383                sizeof(buf.f_fsid) > fsid_size ? fsid_size :
384                sizeof(buf.f_fsid));
385         *namelen_out = buf.f_namelen;
386         spare_out[0] = buf.f_spare[0];
387         spare_out[1] = buf.f_spare[1];
388         spare_out[2] = buf.f_spare[2];
389         spare_out[3] = buf.f_spare[3];
390         spare_out[4] = buf.f_spare[4];
391         return(0);
392 }
393
394 /*
395  * Overrides for Emacs so that we follow Linus's tabbing style.
396  * Emacs will notice this stuff at the end of the file and automatically
397  * adjust the settings for this buffer only.  This must remain at the end
398  * of the file.
399  * ---------------------------------------------------------------------------
400  * Local variables:
401  * c-file-style: "linux"
402  * End:
403  */