libfuse: test/test_write_cache.c Source File

libfuse

test_write_cache.c
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2016 Nikolaus Rath <Nikolaus@rath.org>
4
5 This program can be distributed under the terms of the GNU GPLv2.
6 See the file COPYING.
7*/
8
9
10#define FUSE_USE_VERSION 30
11
12/* Not really needed - just to test build with FUSE_USE_VERSION == 30 */
13#include <fuse.h>
14
15#include <fuse_config.h>
16#include <fuse_lowlevel.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <assert.h>
23#include <stddef.h>
24#include <unistd.h>
25#include <sys/stat.h>
26#include <pthread.h>
27#include <stdatomic.h>
28
29#ifndef __linux__
30#include <limits.h>
31#else
32#include <linux/limits.h>
33#endif
34
35#define FILE_INO 2
36#define FILE_NAME "write_me"
37
38/* Command line parsing */
39struct options {
40 int writeback;
41 int data_size;
42 int delay_ms;
43} options = {
44 .writeback = 0,
45 .data_size = 2048,
46 .delay_ms = 0,
47};
48
49#define WRITE_SYSCALLS 64
50
51#define OPTION(t, p) \
52 { t, offsetof(struct options, p), 1 }
53static const struct fuse_opt option_spec[] = {
54 OPTION("writeback_cache", writeback),
55 OPTION("--data-size=%d", data_size),
56 OPTION("--delay_ms=%d", delay_ms),
57 FUSE_OPT_END
58};
59static int got_write;
60static atomic_int write_cnt;
61
62pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
63pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
64static int write_start, write_done;
65
66static void tfs_init (void *userdata, struct fuse_conn_info *conn)
67{
68 (void) userdata;
69
70 if(options.writeback) {
71 assert(fuse_get_feature_flag(conn, FUSE_CAP_WRITEBACK_CACHE));
72 fuse_set_feature_flag(conn, FUSE_CAP_WRITEBACK_CACHE);
73 }
74}
75
76static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) {
77 stbuf->st_ino = ino;
78 if (ino == FUSE_ROOT_ID) {
79 stbuf->st_mode = S_IFDIR | 0755;
80 stbuf->st_nlink = 1;
81 }
82
83 else if (ino == FILE_INO) {
84 stbuf->st_mode = S_IFREG | 0222;
85 stbuf->st_nlink = 1;
86 stbuf->st_size = 0;
87 }
88
89 else
90 return -1;
91
92 return 0;
93}
94
95static void tfs_lookup(fuse_req_t req, fuse_ino_t parent,
96 const char *name) {
97 struct fuse_entry_param e;
98 memset(&e, 0, sizeof(e));
99
100 if (parent != FUSE_ROOT_ID)
101 goto err_out;
102 else if (strcmp(name, FILE_NAME) == 0)
103 e.ino = FILE_INO;
104 else
105 goto err_out;
106
107 if (tfs_stat(e.ino, &e.attr) != 0)
108 goto err_out;
109 fuse_reply_entry(req, &e);
110 return;
111
112err_out:
113 fuse_reply_err(req, ENOENT);
114}
115
116static void tfs_getattr(fuse_req_t req, fuse_ino_t ino,
117 struct fuse_file_info *fi) {
118 struct stat stbuf;
119
120 (void) fi;
121
122 memset(&stbuf, 0, sizeof(stbuf));
123 if (tfs_stat(ino, &stbuf) != 0)
124 fuse_reply_err(req, ENOENT);
125 else
126 fuse_reply_attr(req, &stbuf, 5);
127}
128
129static void tfs_open(fuse_req_t req, fuse_ino_t ino,
130 struct fuse_file_info *fi) {
131 if (ino == FUSE_ROOT_ID)
132 fuse_reply_err(req, EISDIR);
133 else {
134 assert(ino == FILE_INO);
135 /* Test close(rofd) does not block waiting for pending writes */
136 fi->noflush = !options.writeback && options.delay_ms &&
137 (fi->flags & O_ACCMODE) == O_RDONLY;
138 fuse_reply_open(req, fi);
139 }
140}
141
142static void tfs_write(fuse_req_t req, fuse_ino_t ino, const char *buf,
143 size_t size, off_t off, struct fuse_file_info *fi) {
144 (void) fi; (void) buf; (void) off;
145 size_t expected;
146
147 assert(ino == FILE_INO);
148 expected = options.data_size;
149 if(options.writeback)
150 expected *= 2;
151
152 write_cnt++;
153
154 if(size != expected && !options.writeback)
155 fprintf(stderr, "ERROR: Expected %zd bytes, got %zd\n!",
156 expected, size);
157 else
158 got_write = 1;
159
160 /* Simulate waiting for pending writes */
161 if (options.delay_ms) {
162 pthread_mutex_lock(&lock);
163 write_start = 1;
164 pthread_cond_signal(&cond);
165 pthread_mutex_unlock(&lock);
166
167 usleep(options.delay_ms * 1000);
168
169 pthread_mutex_lock(&lock);
170 write_done = 1;
171 pthread_cond_signal(&cond);
172 pthread_mutex_unlock(&lock);
173 }
174
175 fuse_reply_write(req, size);
176}
177
178static struct fuse_lowlevel_ops tfs_oper = {
179 .init = tfs_init,
180 .lookup = tfs_lookup,
181 .getattr = tfs_getattr,
182 .open = tfs_open,
183 .write = tfs_write,
184};
185
186static void* close_rofd(void *data) {
187 int rofd = (int)(long) data;
188
189 /* Wait for first write to start */
190 pthread_mutex_lock(&lock);
191 while (!write_start && !write_done)
192 pthread_cond_wait(&cond, &lock);
193 pthread_mutex_unlock(&lock);
194
195 close(rofd);
196 printf("rofd closed. write_start: %d write_done: %d\n", write_start, write_done);
197
198 /* First write should not have been completed */
199 if (write_done)
200 fprintf(stderr, "ERROR: close(rofd) blocked on write!\n");
201
202 return NULL;
203}
204
205static void* run_fs(void *data) {
206 struct fuse_session *se = (struct fuse_session*) data;
207 assert(fuse_session_loop(se) == 0);
208 return NULL;
209}
210
211static void test_fs(char *mountpoint) {
212 char fname[PATH_MAX];
213 char *buf;
214 const size_t iosize = options.data_size;
215 const size_t dsize = options.data_size * WRITE_SYSCALLS;
216 int fd, rofd;
217 pthread_t rofd_thread;
218 off_t off = 0;
219
220 buf = malloc(dsize);
221 assert(buf != NULL);
222 assert((fd = open("/dev/urandom", O_RDONLY)) != -1);
223 assert(read(fd, buf, dsize) == dsize);
224 close(fd);
225
226 assert(snprintf(fname, PATH_MAX, "%s/" FILE_NAME,
227 mountpoint) > 0);
228 fd = open(fname, O_WRONLY);
229 if (fd == -1) {
230 perror(fname);
231 assert(0);
232 }
233
234 if (options.delay_ms) {
235 /* Verify that close(rofd) does not block waiting for pending writes */
236 rofd = open(fname, O_RDONLY);
237 assert(pthread_create(&rofd_thread, NULL, close_rofd, (void *)(long)rofd) == 0);
238 /* Give close_rofd time to start */
239 usleep(options.delay_ms * 1000);
240 }
241
242 for (int cnt = 0; cnt < WRITE_SYSCALLS; cnt++) {
243 assert(pwrite(fd, buf + off, iosize, off) == iosize);
244 off += iosize;
245 assert(off <= dsize);
246 }
247 free(buf);
248 close(fd);
249
250 if (options.delay_ms) {
251 printf("rwfd closed. write_start: %d write_done: %d\n", write_start, write_done);
252 assert(pthread_join(rofd_thread, NULL) == 0);
253 }
254}
255
256int main(int argc, char *argv[]) {
257 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
258 struct fuse_session *se;
259 struct fuse_cmdline_opts fuse_opts;
260 pthread_t fs_thread;
261
262 assert(fuse_opt_parse(&args, &options, option_spec, NULL) == 0);
263 assert(fuse_parse_cmdline(&args, &fuse_opts) == 0);
264#ifndef __FreeBSD__
265 assert(fuse_opt_add_arg(&args, "-oauto_unmount") == 0);
266#endif
267 se = fuse_session_new(&args, &tfs_oper,
268 sizeof(tfs_oper), NULL);
269 fuse_opt_free_args(&args);
270 assert (se != NULL);
271 assert(fuse_set_signal_handlers(se) == 0);
272 assert(fuse_session_mount(se, fuse_opts.mountpoint) == 0);
273
274 /* Start file-system thread */
275 assert(pthread_create(&fs_thread, NULL, run_fs, (void *)se) == 0);
276
277 /* Write test data */
278 test_fs(fuse_opts.mountpoint);
279 free(fuse_opts.mountpoint);
280
281 /* Stop file system */
282 fuse_session_exit(se);
283 fuse_session_unmount(se);
284 assert(pthread_join(fs_thread, NULL) == 0);
285
286 assert(got_write == 1);
287
288 /*
289 * when writeback cache is enabled, kernel side can merge requests, but
290 * memory pressure, system 'sync' might trigger data flushes before - flush
291 * might happen in between write syscalls - merging subpage writes into
292 * a single page and pages into large fuse requests might or might not work.
293 * Though we can expect that that at least some (but maybe all) write
294 * system calls can be merged.
295 */
296 if (options.writeback)
297 assert(write_cnt < WRITE_SYSCALLS);
298 else
299 assert(write_cnt == WRITE_SYSCALLS);
300
301 fuse_remove_signal_handlers(se);
302 fuse_session_destroy(se);
303
304 printf("Test completed successfully.\n");
305 return 0;
306}
307
308
fuse.h
fuse_set_signal_handlers
int fuse_set_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:138
FUSE_CAP_WRITEBACK_CACHE
#define FUSE_CAP_WRITEBACK_CACHE
Definition fuse_common.h:345
fuse_remove_signal_handlers
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:185
fuse_lowlevel.h
fuse_session_destroy
void fuse_session_destroy(struct fuse_session *se)
Definition fuse_lowlevel.c:2951
FUSE_ROOT_ID
#define FUSE_ROOT_ID
Definition fuse_lowlevel.h:44
fuse_reply_open
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
Definition fuse_lowlevel.c:509
fuse_session_exit
void fuse_session_exit(struct fuse_session *se)
fuse_reply_err
int fuse_reply_err(fuse_req_t req, int err)
Definition fuse_lowlevel.c:335
fuse_req_t
struct fuse_req * fuse_req_t
Definition fuse_lowlevel.h:50
fuse_session_loop
int fuse_session_loop(struct fuse_session *se)
Definition fuse_loop.c:19
fuse_reply_entry
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
Definition fuse_lowlevel.c:432
fuse_session_unmount
void fuse_session_unmount(struct fuse_session *se)
Definition fuse_lowlevel.c:3474
fuse_reply_write
int fuse_reply_write(fuse_req_t req, size_t count)
Definition fuse_lowlevel.c:518
fuse_session_mount
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
Definition fuse_lowlevel.c:3414
fuse_ino_t
uint64_t fuse_ino_t
Definition fuse_lowlevel.h:47
fuse_reply_attr
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
Definition fuse_lowlevel.c:464
fuse_opt_add_arg
int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
Definition fuse_opt.c:55
fuse_opt_free_args
void fuse_opt_free_args(struct fuse_args *args)
Definition fuse_opt.c:34
fuse_opt_parse
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
Definition fuse_opt.c:398
FUSE_ARGS_INIT
#define FUSE_ARGS_INIT(argc, argv)
Definition fuse_opt.h:123
FUSE_OPT_END
#define FUSE_OPT_END
Definition fuse_opt.h:104
fuse_args
Definition fuse_opt.h:109
fuse_args::argc
int argc
Definition fuse_opt.h:111
fuse_args::argv
char ** argv
Definition fuse_opt.h:114
fuse_cmdline_opts
Definition fuse_lowlevel.h:2003
fuse_conn_info
Definition fuse_common.h:546
fuse_entry_param
Definition fuse_lowlevel.h:60
fuse_entry_param::ino
fuse_ino_t ino
Definition fuse_lowlevel.h:68
fuse_file_info
Definition fuse_common.h:56
fuse_file_info::noflush
uint32_t noflush
Definition fuse_common.h:99
fuse_file_info::flags
int32_t flags
Definition fuse_common.h:58
fuse_lowlevel_ops
Definition fuse_lowlevel.h:206
fuse_lowlevel_ops::init
void(* init)(void *userdata, struct fuse_conn_info *conn)
Definition fuse_lowlevel.h:223
fuse_opt
Definition fuse_opt.h:77