xref: /qemu/hw/9pfs/9p-util.h (revision a5804fcf7b22fc7d1f9ec794dd284c7d504bd16b)
16482a961SGreg Kurz /*
26482a961SGreg Kurz  * 9p utilities
36482a961SGreg Kurz  *
46482a961SGreg Kurz  * Copyright IBM, Corp. 2017
56482a961SGreg Kurz  *
66482a961SGreg Kurz  * Authors:
76482a961SGreg Kurz  *  Greg Kurz <groug@kaod.org>
86482a961SGreg Kurz  *
96482a961SGreg Kurz  * This work is licensed under the terms of the GNU GPL, version 2 or later.
106482a961SGreg Kurz  * See the COPYING file in the top-level directory.
116482a961SGreg Kurz  */
126482a961SGreg Kurz 
136482a961SGreg Kurz #ifndef QEMU_9P_UTIL_H
146482a961SGreg Kurz #define QEMU_9P_UTIL_H
156482a961SGreg Kurz 
164751fd53SGreg Kurz #ifdef O_PATH
174751fd53SGreg Kurz #define O_PATH_9P_UTIL O_PATH
184751fd53SGreg Kurz #else
194751fd53SGreg Kurz #define O_PATH_9P_UTIL 0
204751fd53SGreg Kurz #endif
214751fd53SGreg Kurz 
226482a961SGreg Kurz static inline void close_preserve_errno(int fd)
236482a961SGreg Kurz {
246482a961SGreg Kurz     int serrno = errno;
256482a961SGreg Kurz     close(fd);
266482a961SGreg Kurz     errno = serrno;
276482a961SGreg Kurz }
286482a961SGreg Kurz 
296482a961SGreg Kurz static inline int openat_dir(int dirfd, const char *name)
306482a961SGreg Kurz {
31b003fc0dSGreg Kurz     return openat(dirfd, name,
324751fd53SGreg Kurz                   O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_PATH_9P_UTIL);
336482a961SGreg Kurz }
346482a961SGreg Kurz 
356482a961SGreg Kurz static inline int openat_file(int dirfd, const char *name, int flags,
366482a961SGreg Kurz                               mode_t mode)
376482a961SGreg Kurz {
386482a961SGreg Kurz     int fd, serrno, ret;
396482a961SGreg Kurz 
40*a5804fcfSOmar Sandoval again:
416482a961SGreg Kurz     fd = openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK,
426482a961SGreg Kurz                 mode);
436482a961SGreg Kurz     if (fd == -1) {
44*a5804fcfSOmar Sandoval         if (errno == EPERM && (flags & O_NOATIME)) {
45*a5804fcfSOmar Sandoval             /*
46*a5804fcfSOmar Sandoval              * The client passed O_NOATIME but we lack permissions to honor it.
47*a5804fcfSOmar Sandoval              * Rather than failing the open, fall back without O_NOATIME. This
48*a5804fcfSOmar Sandoval              * doesn't break the semantics on the client side, as the Linux
49*a5804fcfSOmar Sandoval              * open(2) man page notes that O_NOATIME "may not be effective on
50*a5804fcfSOmar Sandoval              * all filesystems". In particular, NFS and other network
51*a5804fcfSOmar Sandoval              * filesystems ignore it entirely.
52*a5804fcfSOmar Sandoval              */
53*a5804fcfSOmar Sandoval             flags &= ~O_NOATIME;
54*a5804fcfSOmar Sandoval             goto again;
55*a5804fcfSOmar Sandoval         }
566482a961SGreg Kurz         return -1;
576482a961SGreg Kurz     }
586482a961SGreg Kurz 
596482a961SGreg Kurz     serrno = errno;
604751fd53SGreg Kurz     /* O_NONBLOCK was only needed to open the file. Let's drop it. We don't
614751fd53SGreg Kurz      * do that with O_PATH since fcntl(F_SETFL) isn't supported, and openat()
624751fd53SGreg Kurz      * ignored it anyway.
634751fd53SGreg Kurz      */
644751fd53SGreg Kurz     if (!(flags & O_PATH_9P_UTIL)) {
656482a961SGreg Kurz         ret = fcntl(fd, F_SETFL, flags);
666482a961SGreg Kurz         assert(!ret);
674751fd53SGreg Kurz     }
686482a961SGreg Kurz     errno = serrno;
696482a961SGreg Kurz     return fd;
706482a961SGreg Kurz }
716482a961SGreg Kurz 
7256ad3e54SGreg Kurz ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
7356ad3e54SGreg Kurz                              void *value, size_t size);
743e36aba7SGreg Kurz int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
753e36aba7SGreg Kurz                          void *value, size_t size, int flags);
76ec70b956SKeno Fischer ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
77ec70b956SKeno Fischer                               char *list, size_t size);
78ec70b956SKeno Fischer ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
79ec70b956SKeno Fischer                                 const char *name);
806482a961SGreg Kurz 
816482a961SGreg Kurz #endif
82