xref: /openbmc/qemu/hw/9pfs/9p-util.h (revision 824cac68)
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 /* Translates errno from host -> Linux if needed */
62 static inline int errno_to_dotl(int err) {
63 #if defined(CONFIG_LINUX)
64     /* nothing to translate (Linux -> Linux) */
65 #elif defined(CONFIG_DARWIN)
66     /*
67      * translation mandatory for macOS hosts
68      *
69      * FIXME: Only most important errnos translated here yet, this should be
70      * extended to as many errnos being translated as possible in future.
71      */
72     if (err == ENAMETOOLONG) {
73         err = 36; /* ==ENAMETOOLONG on Linux */
74     } else if (err == ENOTEMPTY) {
75         err = 39; /* ==ENOTEMPTY on Linux */
76     } else if (err == ELOOP) {
77         err = 40; /* ==ELOOP on Linux */
78     } else if (err == ENOATTR) {
79         err = 61; /* ==ENODATA on Linux */
80     } else if (err == ENOTSUP) {
81         err = 95; /* ==EOPNOTSUPP on Linux */
82     } else if (err == EOPNOTSUPP) {
83         err = 95; /* ==EOPNOTSUPP on Linux */
84     }
85 #else
86 #error Missing errno translation to Linux for this host system
87 #endif
88     return err;
89 }
90 
91 #ifdef CONFIG_DARWIN
92 #define qemu_fgetxattr(...) fgetxattr(__VA_ARGS__, 0, 0)
93 #else
94 #define qemu_fgetxattr fgetxattr
95 #endif
96 
97 #define qemu_openat     openat
98 #define qemu_fstatat    fstatat
99 #define qemu_mkdirat    mkdirat
100 #define qemu_renameat   renameat
101 #define qemu_utimensat  utimensat
102 #define qemu_unlinkat   unlinkat
103 
104 static inline void close_preserve_errno(int fd)
105 {
106     int serrno = errno;
107     close(fd);
108     errno = serrno;
109 }
110 
111 static inline int openat_dir(int dirfd, const char *name)
112 {
113     return qemu_openat(dirfd, name,
114                        O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_PATH_9P_UTIL);
115 }
116 
117 static inline int openat_file(int dirfd, const char *name, int flags,
118                               mode_t mode)
119 {
120     int fd, serrno, ret;
121 
122 #ifndef CONFIG_DARWIN
123 again:
124 #endif
125     fd = qemu_openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK,
126                      mode);
127     if (fd == -1) {
128 #ifndef CONFIG_DARWIN
129         if (errno == EPERM && (flags & O_NOATIME)) {
130             /*
131              * The client passed O_NOATIME but we lack permissions to honor it.
132              * Rather than failing the open, fall back without O_NOATIME. This
133              * doesn't break the semantics on the client side, as the Linux
134              * open(2) man page notes that O_NOATIME "may not be effective on
135              * all filesystems". In particular, NFS and other network
136              * filesystems ignore it entirely.
137              */
138             flags &= ~O_NOATIME;
139             goto again;
140         }
141 #endif
142         return -1;
143     }
144 
145     serrno = errno;
146     /* O_NONBLOCK was only needed to open the file. Let's drop it. We don't
147      * do that with O_PATH since fcntl(F_SETFL) isn't supported, and openat()
148      * ignored it anyway.
149      */
150     if (!(flags & O_PATH_9P_UTIL)) {
151         ret = fcntl(fd, F_SETFL, flags);
152         assert(!ret);
153     }
154     errno = serrno;
155     return fd;
156 }
157 
158 ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
159                              void *value, size_t size);
160 int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
161                          void *value, size_t size, int flags);
162 ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
163                               char *list, size_t size);
164 ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
165                                 const char *name);
166 
167 /*
168  * Darwin has d_seekoff, which appears to function similarly to d_off.
169  * However, it does not appear to be supported on all file systems,
170  * so ensure it is manually injected earlier and call here when
171  * needed.
172  */
173 static inline off_t qemu_dirent_off(struct dirent *dent)
174 {
175 #ifdef CONFIG_DARWIN
176     return dent->d_seekoff;
177 #else
178     return dent->d_off;
179 #endif
180 }
181 
182 /**
183  * qemu_dirent_dup() - Duplicate directory entry @dent.
184  *
185  * @dent: original directory entry to be duplicated
186  * Return: duplicated directory entry which should be freed with g_free()
187  *
188  * It is highly recommended to use this function instead of open coding
189  * duplication of dirent objects, because the actual struct dirent
190  * size may be bigger or shorter than sizeof(struct dirent) and correct
191  * handling is platform specific (see gitlab issue #841).
192  */
193 static inline struct dirent *qemu_dirent_dup(struct dirent *dent)
194 {
195     size_t sz = 0;
196 #if defined _DIRENT_HAVE_D_RECLEN
197     /* Avoid use of strlen() if platform supports d_reclen. */
198     sz = dent->d_reclen;
199 #endif
200     /*
201      * Test sz for zero even if d_reclen is available
202      * because some drivers may set d_reclen to zero.
203      */
204     if (sz == 0) {
205         /* Fallback to the most portable way. */
206         sz = offsetof(struct dirent, d_name) +
207                       strlen(dent->d_name) + 1;
208     }
209     return g_memdup(dent, sz);
210 }
211 
212 /*
213  * As long as mknodat is not available on macOS, this workaround
214  * using pthread_fchdir_np is needed. qemu_mknodat is defined in
215  * os-posix.c. pthread_fchdir_np is weakly linked here as a guard
216  * in case it disappears in future macOS versions, because it is
217  * is a private API.
218  */
219 #if defined CONFIG_DARWIN && defined CONFIG_PTHREAD_FCHDIR_NP
220 int pthread_fchdir_np(int fd) __attribute__((weak_import));
221 #endif
222 int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev);
223 
224 #endif
225