xref: /qemu/hw/9pfs/9p-util.h (revision e5c88e2264ebd0bfe35b347b8cdca1e3af784d84)
1 /*
2  * 9p utilities
3  *
4  * Copyright IBM, Corp. 2017
5  *
6  * Authors:
7  *  Greg Kurz <groug@kaod.org>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #ifndef QEMU_9P_UTIL_H
14 #define QEMU_9P_UTIL_H
15 
16 #ifdef O_PATH
17 #define O_PATH_9P_UTIL O_PATH
18 #else
19 #define O_PATH_9P_UTIL 0
20 #endif
21 
22 #if !defined(CONFIG_LINUX)
23 
24 /*
25  * Generates a Linux device number (a.k.a. dev_t) for given device major
26  * and minor numbers.
27  *
28  * To be more precise: it generates a device number in glibc's format
29  * (MMMM_Mmmm_mmmM_MMmm, 64 bits) actually, which is compatible with
30  * Linux's format (mmmM_MMmm, 32 bits), as described in <bits/sysmacros.h>.
31  */
32 static inline uint64_t makedev_dotl(uint32_t dev_major, uint32_t dev_minor)
33 {
34     uint64_t dev;
35 
36     // from glibc sysmacros.h:
37     dev  = (((uint64_t) (dev_major & 0x00000fffu)) <<  8);
38     dev |= (((uint64_t) (dev_major & 0xfffff000u)) << 32);
39     dev |= (((uint64_t) (dev_minor & 0x000000ffu)) <<  0);
40     dev |= (((uint64_t) (dev_minor & 0xffffff00u)) << 12);
41     return dev;
42 }
43 
44 #endif
45 
46 /*
47  * Converts given device number from host's device number format to Linux
48  * device number format. As both the size of type dev_t and encoding of
49  * dev_t is system dependant, we have to convert them for Linux guests if
50  * host is not running Linux.
51  */
52 static inline uint64_t host_dev_to_dotl_dev(dev_t dev)
53 {
54 #ifdef CONFIG_LINUX
55     return dev;
56 #else
57     return makedev_dotl(major(dev), minor(dev));
58 #endif
59 }
60 
61 #ifdef CONFIG_DARWIN
62 #define qemu_fgetxattr(...) fgetxattr(__VA_ARGS__, 0, 0)
63 #define qemu_lgetxattr(...) getxattr(__VA_ARGS__, 0, XATTR_NOFOLLOW)
64 #define qemu_llistxattr(...) listxattr(__VA_ARGS__, XATTR_NOFOLLOW)
65 #define qemu_lremovexattr(...) removexattr(__VA_ARGS__, XATTR_NOFOLLOW)
66 static inline int qemu_lsetxattr(const char *path, const char *name,
67                                  const void *value, size_t size, int flags) {
68     return setxattr(path, name, value, size, 0, flags | XATTR_NOFOLLOW);
69 }
70 #else
71 #define qemu_fgetxattr fgetxattr
72 #define qemu_lgetxattr lgetxattr
73 #define qemu_llistxattr llistxattr
74 #define qemu_lremovexattr lremovexattr
75 #define qemu_lsetxattr lsetxattr
76 #endif
77 
78 static inline void close_preserve_errno(int fd)
79 {
80     int serrno = errno;
81     close(fd);
82     errno = serrno;
83 }
84 
85 static inline int openat_dir(int dirfd, const char *name)
86 {
87     return openat(dirfd, name,
88                   O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_PATH_9P_UTIL);
89 }
90 
91 static inline int openat_file(int dirfd, const char *name, int flags,
92                               mode_t mode)
93 {
94     int fd, serrno, ret;
95 
96 #ifndef CONFIG_DARWIN
97 again:
98 #endif
99     fd = openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK,
100                 mode);
101     if (fd == -1) {
102 #ifndef CONFIG_DARWIN
103         if (errno == EPERM && (flags & O_NOATIME)) {
104             /*
105              * The client passed O_NOATIME but we lack permissions to honor it.
106              * Rather than failing the open, fall back without O_NOATIME. This
107              * doesn't break the semantics on the client side, as the Linux
108              * open(2) man page notes that O_NOATIME "may not be effective on
109              * all filesystems". In particular, NFS and other network
110              * filesystems ignore it entirely.
111              */
112             flags &= ~O_NOATIME;
113             goto again;
114         }
115 #endif
116         return -1;
117     }
118 
119     serrno = errno;
120     /* O_NONBLOCK was only needed to open the file. Let's drop it. We don't
121      * do that with O_PATH since fcntl(F_SETFL) isn't supported, and openat()
122      * ignored it anyway.
123      */
124     if (!(flags & O_PATH_9P_UTIL)) {
125         ret = fcntl(fd, F_SETFL, flags);
126         assert(!ret);
127     }
128     errno = serrno;
129     return fd;
130 }
131 
132 ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
133                              void *value, size_t size);
134 int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
135                          void *value, size_t size, int flags);
136 ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
137                               char *list, size_t size);
138 ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
139                                 const char *name);
140 
141 /*
142  * Darwin has d_seekoff, which appears to function similarly to d_off.
143  * However, it does not appear to be supported on all file systems,
144  * so ensure it is manually injected earlier and call here when
145  * needed.
146  */
147 static inline off_t qemu_dirent_off(struct dirent *dent)
148 {
149 #ifdef CONFIG_DARWIN
150     return dent->d_seekoff;
151 #else
152     return dent->d_off;
153 #endif
154 }
155 
156 /**
157  * qemu_dirent_dup() - Duplicate directory entry @dent.
158  *
159  * @dent: original directory entry to be duplicated
160  * Return: duplicated directory entry which should be freed with g_free()
161  *
162  * It is highly recommended to use this function instead of open coding
163  * duplication of dirent objects, because the actual struct dirent
164  * size may be bigger or shorter than sizeof(struct dirent) and correct
165  * handling is platform specific (see gitlab issue #841).
166  */
167 static inline struct dirent *qemu_dirent_dup(struct dirent *dent)
168 {
169     size_t sz = 0;
170 #if defined _DIRENT_HAVE_D_RECLEN
171     /* Avoid use of strlen() if platform supports d_reclen. */
172     sz = dent->d_reclen;
173 #endif
174     /*
175      * Test sz for zero even if d_reclen is available
176      * because some drivers may set d_reclen to zero.
177      */
178     if (sz == 0) {
179         /* Fallback to the most portable way. */
180         sz = offsetof(struct dirent, d_name) +
181                       strlen(dent->d_name) + 1;
182     }
183     return g_memdup(dent, sz);
184 }
185 
186 /*
187  * As long as mknodat is not available on macOS, this workaround
188  * using pthread_fchdir_np is needed. qemu_mknodat is defined in
189  * os-posix.c. pthread_fchdir_np is weakly linked here as a guard
190  * in case it disappears in future macOS versions, because it is
191  * is a private API.
192  */
193 #if defined CONFIG_DARWIN && defined CONFIG_PTHREAD_FCHDIR_NP
194 int pthread_fchdir_np(int fd) __attribute__((weak_import));
195 #endif
196 int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev);
197 
198 #endif
199