]> Git Repo - qemu.git/blob - hw/9pfs/virtio-9p-local.c
Merge remote-tracking branch 'qemu-kvm/memory/core' into staging
[qemu.git] / hw / 9pfs / virtio-9p-local.c
1 /*
2  * Virtio 9p Posix callback
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  *  Anthony Liguori   <[email protected]>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13
14 #include "hw/virtio.h"
15 #include "virtio-9p.h"
16 #include "virtio-9p-xattr.h"
17 #include <arpa/inet.h>
18 #include <pwd.h>
19 #include <grp.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <attr/xattr.h>
23
24
25 static int local_lstat(FsContext *fs_ctx, const char *path, struct stat *stbuf)
26 {
27     int err;
28     char buffer[PATH_MAX];
29     err =  lstat(rpath(fs_ctx, path, buffer), stbuf);
30     if (err) {
31         return err;
32     }
33     if (fs_ctx->fs_sm == SM_MAPPED) {
34         /* Actual credentials are part of extended attrs */
35         uid_t tmp_uid;
36         gid_t tmp_gid;
37         mode_t tmp_mode;
38         dev_t tmp_dev;
39         if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.uid", &tmp_uid,
40                     sizeof(uid_t)) > 0) {
41             stbuf->st_uid = tmp_uid;
42         }
43         if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.gid", &tmp_gid,
44                     sizeof(gid_t)) > 0) {
45             stbuf->st_gid = tmp_gid;
46         }
47         if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.mode",
48                     &tmp_mode, sizeof(mode_t)) > 0) {
49             stbuf->st_mode = tmp_mode;
50         }
51         if (getxattr(rpath(fs_ctx, path, buffer), "user.virtfs.rdev", &tmp_dev,
52                         sizeof(dev_t)) > 0) {
53                 stbuf->st_rdev = tmp_dev;
54         }
55     }
56     return err;
57 }
58
59 static int local_set_xattr(const char *path, FsCred *credp)
60 {
61     int err;
62     if (credp->fc_uid != -1) {
63         err = setxattr(path, "user.virtfs.uid", &credp->fc_uid, sizeof(uid_t),
64                 0);
65         if (err) {
66             return err;
67         }
68     }
69     if (credp->fc_gid != -1) {
70         err = setxattr(path, "user.virtfs.gid", &credp->fc_gid, sizeof(gid_t),
71                 0);
72         if (err) {
73             return err;
74         }
75     }
76     if (credp->fc_mode != -1) {
77         err = setxattr(path, "user.virtfs.mode", &credp->fc_mode,
78                 sizeof(mode_t), 0);
79         if (err) {
80             return err;
81         }
82     }
83     if (credp->fc_rdev != -1) {
84         err = setxattr(path, "user.virtfs.rdev", &credp->fc_rdev,
85                 sizeof(dev_t), 0);
86         if (err) {
87             return err;
88         }
89     }
90     return 0;
91 }
92
93 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path,
94         FsCred *credp)
95 {
96     char buffer[PATH_MAX];
97     if (chmod(rpath(fs_ctx, path, buffer), credp->fc_mode & 07777) < 0) {
98         return -1;
99     }
100     if (lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
101                 credp->fc_gid) < 0) {
102         /*
103          * If we fail to change ownership and if we are
104          * using security model none. Ignore the error
105          */
106         if (fs_ctx->fs_sm != SM_NONE) {
107             return -1;
108         }
109     }
110     return 0;
111 }
112
113 static ssize_t local_readlink(FsContext *fs_ctx, const char *path,
114         char *buf, size_t bufsz)
115 {
116     ssize_t tsize = -1;
117     char buffer[PATH_MAX];
118     if (fs_ctx->fs_sm == SM_MAPPED) {
119         int fd;
120         fd = open(rpath(fs_ctx, path, buffer), O_RDONLY);
121         if (fd == -1) {
122             return -1;
123         }
124         do {
125             tsize = read(fd, (void *)buf, bufsz);
126         } while (tsize == -1 && errno == EINTR);
127         close(fd);
128         return tsize;
129     } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
130                (fs_ctx->fs_sm == SM_NONE)) {
131         tsize = readlink(rpath(fs_ctx, path, buffer), buf, bufsz);
132     }
133     return tsize;
134 }
135
136 static int local_close(FsContext *ctx, int fd)
137 {
138     return close(fd);
139 }
140
141 static int local_closedir(FsContext *ctx, DIR *dir)
142 {
143     return closedir(dir);
144 }
145
146 static int local_open(FsContext *ctx, const char *path, int flags)
147 {
148     char buffer[PATH_MAX];
149     return open(rpath(ctx, path, buffer), flags);
150 }
151
152 static DIR *local_opendir(FsContext *ctx, const char *path)
153 {
154     char buffer[PATH_MAX];
155     return opendir(rpath(ctx, path, buffer));
156 }
157
158 static void local_rewinddir(FsContext *ctx, DIR *dir)
159 {
160     return rewinddir(dir);
161 }
162
163 static off_t local_telldir(FsContext *ctx, DIR *dir)
164 {
165     return telldir(dir);
166 }
167
168 static int local_readdir_r(FsContext *ctx, DIR *dir, struct dirent *entry,
169                          struct dirent **result)
170 {
171     return readdir_r(dir, entry, result);
172 }
173
174 static void local_seekdir(FsContext *ctx, DIR *dir, off_t off)
175 {
176     return seekdir(dir, off);
177 }
178
179 static ssize_t local_preadv(FsContext *ctx, int fd, const struct iovec *iov,
180                             int iovcnt, off_t offset)
181 {
182 #ifdef CONFIG_PREADV
183     return preadv(fd, iov, iovcnt, offset);
184 #else
185     int err = lseek(fd, offset, SEEK_SET);
186     if (err == -1) {
187         return err;
188     } else {
189         return readv(fd, iov, iovcnt);
190     }
191 #endif
192 }
193
194 static ssize_t local_pwritev(FsContext *ctx, int fd, const struct iovec *iov,
195                             int iovcnt, off_t offset)
196 {
197 #ifdef CONFIG_PREADV
198     return pwritev(fd, iov, iovcnt, offset);
199 #else
200     int err = lseek(fd, offset, SEEK_SET);
201     if (err == -1) {
202         return err;
203     } else {
204         return writev(fd, iov, iovcnt);
205     }
206 #endif
207 }
208
209 static int local_chmod(FsContext *fs_ctx, const char *path, FsCred *credp)
210 {
211     char buffer[PATH_MAX];
212     if (fs_ctx->fs_sm == SM_MAPPED) {
213         return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
214     } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
215                (fs_ctx->fs_sm == SM_NONE)) {
216         return chmod(rpath(fs_ctx, path, buffer), credp->fc_mode);
217     }
218     return -1;
219 }
220
221 static int local_mknod(FsContext *fs_ctx, const char *path, FsCred *credp)
222 {
223     int err = -1;
224     int serrno = 0;
225     char buffer[PATH_MAX];
226
227     /* Determine the security model */
228     if (fs_ctx->fs_sm == SM_MAPPED) {
229         err = mknod(rpath(fs_ctx, path, buffer),
230                 SM_LOCAL_MODE_BITS|S_IFREG, 0);
231         if (err == -1) {
232             return err;
233         }
234         local_set_xattr(rpath(fs_ctx, path, buffer), credp);
235         if (err == -1) {
236             serrno = errno;
237             goto err_end;
238         }
239     } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
240                (fs_ctx->fs_sm == SM_NONE)) {
241         err = mknod(rpath(fs_ctx, path, buffer), credp->fc_mode,
242                 credp->fc_rdev);
243         if (err == -1) {
244             return err;
245         }
246         err = local_post_create_passthrough(fs_ctx, path, credp);
247         if (err == -1) {
248             serrno = errno;
249             goto err_end;
250         }
251     }
252     return err;
253
254 err_end:
255     remove(rpath(fs_ctx, path, buffer));
256     errno = serrno;
257     return err;
258 }
259
260 static int local_mkdir(FsContext *fs_ctx, const char *path, FsCred *credp)
261 {
262     int err = -1;
263     int serrno = 0;
264     char buffer[PATH_MAX];
265
266     /* Determine the security model */
267     if (fs_ctx->fs_sm == SM_MAPPED) {
268         err = mkdir(rpath(fs_ctx, path, buffer), SM_LOCAL_DIR_MODE_BITS);
269         if (err == -1) {
270             return err;
271         }
272         credp->fc_mode = credp->fc_mode|S_IFDIR;
273         err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
274         if (err == -1) {
275             serrno = errno;
276             goto err_end;
277         }
278     } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
279                (fs_ctx->fs_sm == SM_NONE)) {
280         err = mkdir(rpath(fs_ctx, path, buffer), credp->fc_mode);
281         if (err == -1) {
282             return err;
283         }
284         err = local_post_create_passthrough(fs_ctx, path, credp);
285         if (err == -1) {
286             serrno = errno;
287             goto err_end;
288         }
289     }
290     return err;
291
292 err_end:
293     remove(rpath(fs_ctx, path, buffer));
294     errno = serrno;
295     return err;
296 }
297
298 static int local_fstat(FsContext *fs_ctx, int fd, struct stat *stbuf)
299 {
300     int err;
301     err = fstat(fd, stbuf);
302     if (err) {
303         return err;
304     }
305     if (fs_ctx->fs_sm == SM_MAPPED) {
306         /* Actual credentials are part of extended attrs */
307         uid_t tmp_uid;
308         gid_t tmp_gid;
309         mode_t tmp_mode;
310         dev_t tmp_dev;
311
312         if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
313             stbuf->st_uid = tmp_uid;
314         }
315         if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
316             stbuf->st_gid = tmp_gid;
317         }
318         if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
319             stbuf->st_mode = tmp_mode;
320         }
321         if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
322                 stbuf->st_rdev = tmp_dev;
323         }
324     }
325     return err;
326 }
327
328 static int local_open2(FsContext *fs_ctx, const char *path, int flags,
329         FsCred *credp)
330 {
331     int fd = -1;
332     int err = -1;
333     int serrno = 0;
334     char buffer[PATH_MAX];
335
336     /* Determine the security model */
337     if (fs_ctx->fs_sm == SM_MAPPED) {
338         fd = open(rpath(fs_ctx, path, buffer), flags, SM_LOCAL_MODE_BITS);
339         if (fd == -1) {
340             return fd;
341         }
342         credp->fc_mode = credp->fc_mode|S_IFREG;
343         /* Set cleint credentials in xattr */
344         err = local_set_xattr(rpath(fs_ctx, path, buffer), credp);
345         if (err == -1) {
346             serrno = errno;
347             goto err_end;
348         }
349     } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
350                (fs_ctx->fs_sm == SM_NONE)) {
351         fd = open(rpath(fs_ctx, path, buffer), flags, credp->fc_mode);
352         if (fd == -1) {
353             return fd;
354         }
355         err = local_post_create_passthrough(fs_ctx, path, credp);
356         if (err == -1) {
357             serrno = errno;
358             goto err_end;
359         }
360     }
361     return fd;
362
363 err_end:
364     close(fd);
365     remove(rpath(fs_ctx, path, buffer));
366     errno = serrno;
367     return err;
368 }
369
370
371 static int local_symlink(FsContext *fs_ctx, const char *oldpath,
372         const char *newpath, FsCred *credp)
373 {
374     int err = -1;
375     int serrno = 0;
376     char buffer[PATH_MAX];
377
378     /* Determine the security model */
379     if (fs_ctx->fs_sm == SM_MAPPED) {
380         int fd;
381         ssize_t oldpath_size, write_size;
382         fd = open(rpath(fs_ctx, newpath, buffer), O_CREAT|O_EXCL|O_RDWR,
383                 SM_LOCAL_MODE_BITS);
384         if (fd == -1) {
385             return fd;
386         }
387         /* Write the oldpath (target) to the file. */
388         oldpath_size = strlen(oldpath);
389         do {
390             write_size = write(fd, (void *)oldpath, oldpath_size);
391         } while (write_size == -1 && errno == EINTR);
392
393         if (write_size != oldpath_size) {
394             serrno = errno;
395             close(fd);
396             err = -1;
397             goto err_end;
398         }
399         close(fd);
400         /* Set cleint credentials in symlink's xattr */
401         credp->fc_mode = credp->fc_mode|S_IFLNK;
402         err = local_set_xattr(rpath(fs_ctx, newpath, buffer), credp);
403         if (err == -1) {
404             serrno = errno;
405             goto err_end;
406         }
407     } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
408                (fs_ctx->fs_sm == SM_NONE)) {
409         err = symlink(oldpath, rpath(fs_ctx, newpath, buffer));
410         if (err) {
411             return err;
412         }
413         err = lchown(rpath(fs_ctx, newpath, buffer), credp->fc_uid,
414                 credp->fc_gid);
415         if (err == -1) {
416             /*
417              * If we fail to change ownership and if we are
418              * using security model none. Ignore the error
419              */
420             if (fs_ctx->fs_sm != SM_NONE) {
421                 serrno = errno;
422                 goto err_end;
423             } else
424                 err = 0;
425         }
426     }
427     return err;
428
429 err_end:
430     remove(rpath(fs_ctx, newpath, buffer));
431     errno = serrno;
432     return err;
433 }
434
435 static int local_link(FsContext *ctx, const char *oldpath, const char *newpath)
436 {
437     char buffer[PATH_MAX], buffer1[PATH_MAX];
438
439     return link(rpath(ctx, oldpath, buffer), rpath(ctx, newpath, buffer1));
440 }
441
442 static int local_truncate(FsContext *ctx, const char *path, off_t size)
443 {
444     char buffer[PATH_MAX];
445     return truncate(rpath(ctx, path, buffer), size);
446 }
447
448 static int local_rename(FsContext *ctx, const char *oldpath,
449                         const char *newpath)
450 {
451     char buffer[PATH_MAX], buffer1[PATH_MAX];
452
453     return rename(rpath(ctx, oldpath, buffer), rpath(ctx, newpath, buffer1));
454 }
455
456 static int local_chown(FsContext *fs_ctx, const char *path, FsCred *credp)
457 {
458     char buffer[PATH_MAX];
459     if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
460             (fs_ctx->fs_sm == SM_PASSTHROUGH)) {
461         return lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
462                 credp->fc_gid);
463     } else if (fs_ctx->fs_sm == SM_MAPPED) {
464         return local_set_xattr(rpath(fs_ctx, path, buffer), credp);
465     } else if ((fs_ctx->fs_sm == SM_PASSTHROUGH) ||
466                (fs_ctx->fs_sm == SM_NONE)) {
467         return lchown(rpath(fs_ctx, path, buffer), credp->fc_uid,
468                 credp->fc_gid);
469     }
470     return -1;
471 }
472
473 static int local_utimensat(FsContext *s, const char *path,
474                            const struct timespec *buf)
475 {
476     char buffer[PATH_MAX];
477     return qemu_utimensat(AT_FDCWD, rpath(s, path, buffer), buf,
478             AT_SYMLINK_NOFOLLOW);
479 }
480
481 static int local_remove(FsContext *ctx, const char *path)
482 {
483     char buffer[PATH_MAX];
484     return remove(rpath(ctx, path, buffer));
485 }
486
487 static int local_fsync(FsContext *ctx, int fd, int datasync)
488 {
489     if (datasync) {
490         return qemu_fdatasync(fd);
491     } else {
492         return fsync(fd);
493     }
494 }
495
496 static int local_statfs(FsContext *s, const char *path, struct statfs *stbuf)
497 {
498     char buffer[PATH_MAX];
499    return statfs(rpath(s, path, buffer), stbuf);
500 }
501
502 static ssize_t local_lgetxattr(FsContext *ctx, const char *path,
503                                const char *name, void *value, size_t size)
504 {
505     return v9fs_get_xattr(ctx, path, name, value, size);
506 }
507
508 static ssize_t local_llistxattr(FsContext *ctx, const char *path,
509                                 void *value, size_t size)
510 {
511     return v9fs_list_xattr(ctx, path, value, size);
512 }
513
514 static int local_lsetxattr(FsContext *ctx, const char *path, const char *name,
515                            void *value, size_t size, int flags)
516 {
517     return v9fs_set_xattr(ctx, path, name, value, size, flags);
518 }
519
520 static int local_lremovexattr(FsContext *ctx,
521                               const char *path, const char *name)
522 {
523     return v9fs_remove_xattr(ctx, path, name);
524 }
525
526
527 FileOperations local_ops = {
528     .lstat = local_lstat,
529     .readlink = local_readlink,
530     .close = local_close,
531     .closedir = local_closedir,
532     .open = local_open,
533     .opendir = local_opendir,
534     .rewinddir = local_rewinddir,
535     .telldir = local_telldir,
536     .readdir_r = local_readdir_r,
537     .seekdir = local_seekdir,
538     .preadv = local_preadv,
539     .pwritev = local_pwritev,
540     .chmod = local_chmod,
541     .mknod = local_mknod,
542     .mkdir = local_mkdir,
543     .fstat = local_fstat,
544     .open2 = local_open2,
545     .symlink = local_symlink,
546     .link = local_link,
547     .truncate = local_truncate,
548     .rename = local_rename,
549     .chown = local_chown,
550     .utimensat = local_utimensat,
551     .remove = local_remove,
552     .fsync = local_fsync,
553     .statfs = local_statfs,
554     .lgetxattr = local_lgetxattr,
555     .llistxattr = local_llistxattr,
556     .lsetxattr = local_lsetxattr,
557     .lremovexattr = local_lremovexattr,
558 };
This page took 0.054335 seconds and 4 git commands to generate.