| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
/* |
| 5 |
* QEMU's 9pfs server passes through O_NOATIME from the client. If the server |
| 6 |
* process doesn't have permission to use O_NOATIME (e.g., because it's being |
| 7 |
* run without privileges and it doesn't own the file), then the open will fail. |
| 8 |
* Overlayfs uses O_NOATIME, so overlayfs on top of 9pfs doesn't work. We work |
| 9 |
* around this with this LD_PRELOAD hack to remove O_NOATIME from open() and |
| 10 |
* fcntl() calls. |
| 11 |
* |
| 12 |
* As of QEMU 5.1.0, the 9pfs server falls back to removing O_NOATIME, so this |
| 13 |
* isn't necessary on newer versions. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <dlfcn.h> |
| 17 |
#include <fcntl.h> |
| 18 |
#include <stdarg.h> |
| 19 |
#include <stdint.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
#include <sys/types.h> |
| 24 |
|
| 25 |
#define ORIG(name) ({ \ |
| 26 |
static typeof(&name) orig; \ |
| 27 |
\ |
| 28 |
if (!orig) { \ |
| 29 |
void *tmp; \ |
| 30 |
\ |
| 31 |
tmp = dlsym(RTLD_NEXT, #name); \ |
| 32 |
if (!tmp) { \ |
| 33 |
fprintf(stderr, "%s\n", dlerror()); \ |
| 34 |
abort(); \ |
| 35 |
} \ |
| 36 |
orig = tmp; \ |
| 37 |
} \ |
| 38 |
orig; \ |
| 39 |
}) |
| 40 |
|
| 41 |
#ifndef __OPEN_NEEDS_MODE |
| 42 |
/* From glibc fnctl.h. */ |
| 43 |
#ifdef __O_TMPFILE |
| 44 |
# define __OPEN_NEEDS_MODE(oflag) \ |
| 45 |
(((oflag) & O_CREAT) != 0 || ((oflag) & __O_TMPFILE) == __O_TMPFILE) |
| 46 |
#else |
| 47 |
# define __OPEN_NEEDS_MODE(oflag) (((oflag) & O_CREAT) != 0) |
| 48 |
#endif |
| 49 |
#endif |
| 50 |
|
| 51 |
#define OPEN_MODE(flags) ({ \ |
| 52 |
mode_t mode = 0; \ |
| 53 |
\ |
| 54 |
if (__OPEN_NEEDS_MODE(flags)) { \ |
| 55 |
va_list ap; \ |
| 56 |
\ |
| 57 |
va_start(ap, flags); \ |
| 58 |
mode = va_arg(ap, mode_t); \ |
| 59 |
va_end(ap); \ |
| 60 |
} \ |
| 61 |
mode; \ |
| 62 |
}) |
| 63 |
|
| 64 |
int open(const char *pathname, int flags, ...) |
| 65 |
{ |
| 66 |
flags &= ~O_NOATIME; |
| 67 |
return ORIG(open)(pathname, flags, OPEN_MODE(flags)); |
| 68 |
} |
| 69 |
|
| 70 |
int open64(const char *pathname, int flags, ...) |
| 71 |
{ |
| 72 |
flags &= ~O_NOATIME; |
| 73 |
return ORIG(open64)(pathname, flags, OPEN_MODE(flags)); |
| 74 |
} |
| 75 |
|
| 76 |
int openat(int dirfd, const char *pathname, int flags, ...) |
| 77 |
{ |
| 78 |
flags &= ~O_NOATIME; |
| 79 |
return ORIG(openat)(dirfd, pathname, flags, OPEN_MODE(flags)); |
| 80 |
} |
| 81 |
|
| 82 |
int openat64(int dirfd, const char *pathname, int flags, ...) |
| 83 |
{ |
| 84 |
flags &= ~O_NOATIME; |
| 85 |
return ORIG(openat64)(dirfd, pathname, flags, OPEN_MODE(flags)); |
| 86 |
} |
| 87 |
|
| 88 |
#define FCNTL_ARG(cmd) ({ \ |
| 89 |
va_list ap; \ |
| 90 |
void *arg; \ |
| 91 |
\ |
| 92 |
va_start(ap, cmd); \ |
| 93 |
arg = va_arg(ap, void *); \ |
| 94 |
va_end(ap); \ |
| 95 |
if (cmd == F_SETFL) \ |
| 96 |
arg = (void *)((uintptr_t)arg & ~O_NOATIME); \ |
| 97 |
arg; \ |
| 98 |
}) |
| 99 |
|
| 100 |
int fcntl(int fd, int cmd, ...) |
| 101 |
{ |
| 102 |
return ORIG(fcntl)(fd, cmd, FCNTL_ARG(cmd)); |
| 103 |
} |
| 104 |
|
| 105 |
int fcntl64(int fd, int cmd, ...) |
| 106 |
{ |
| 107 |
return ORIG(fcntl64)(fd, cmd, FCNTL_ARG(cmd)); |
| 108 |
} |