aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/fs/fusefs/interrupt.cc
blob: 6893bea077e780cd080f7d160b2c9e04cde637db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2019 The FreeBSD Foundation
 *
 * This software was developed by BFF Storage Systems, LLC under sponsorship
 * from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

extern "C" {
#include <sys/types.h>
#include <sys/extattr.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
}

#include "mockfs.hh"
#include "utils.hh"

using namespace testing;

/* Initial size of files used by these tests */
const off_t FILESIZE = 1000;
/* Access mode used by all directories in these tests */
const mode_t MODE = 0755;
const char FULLDIRPATH0[] = "mountpoint/some_dir";
const char RELDIRPATH0[] = "some_dir";
const char FULLDIRPATH1[] = "mountpoint/other_dir";
const char RELDIRPATH1[] = "other_dir";

static sem_t *blocked_semaphore;
static sem_t *signaled_semaphore;

static bool killer_should_sleep = false;

/* Don't do anything; all we care about is that the syscall gets interrupted */
void sigusr2_handler(int __unused sig) {
	if (verbosity > 1) {
		printf("Signaled!  thread %p\n", pthread_self());
	}

}

void* killer(void* target) {
	/* Wait until the main thread is blocked in fdisp_wait_answ */
	if (killer_should_sleep)
		nap();
	else
		sem_wait(blocked_semaphore);
	if (verbosity > 1)
		printf("Signalling!  thread %p\n", target);
	pthread_kill((pthread_t)target, SIGUSR2);
	if (signaled_semaphore != NULL)
		sem_post(signaled_semaphore);

	return(NULL);
}

class Interrupt: public FuseTest {
public:
pthread_t m_child;

Interrupt(): m_child(NULL) {};

void expect_lookup(const char *relpath, uint64_t ino)
{
	FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, FILESIZE, 1);
}

/* 
 * Expect a FUSE_MKDIR but don't reply.  Instead, just record the unique value
 * to the provided pointer
 */
void expect_mkdir(uint64_t *mkdir_unique)
{
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_MKDIR);
		}, Eq(true)),
		_)
	).WillOnce(Invoke([=](auto in, auto &out __unused) {
		*mkdir_unique = in.header.unique;
		sem_post(blocked_semaphore);
	}));
}

/* 
 * Expect a FUSE_READ but don't reply.  Instead, just record the unique value
 * to the provided pointer
 */
void expect_read(uint64_t ino, uint64_t *read_unique)
{
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_READ &&
				in.header.nodeid == ino);
		}, Eq(true)),
		_)
	).WillOnce(Invoke([=](auto in, auto &out __unused) {
		*read_unique = in.header.unique;
		sem_post(blocked_semaphore);
	}));
}

/* 
 * Expect a FUSE_WRITE but don't reply.  Instead, just record the unique value
 * to the provided pointer
 */
void expect_write(uint64_t ino, uint64_t *write_unique)
{
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_WRITE &&
				in.header.nodeid == ino);
		}, Eq(true)),
		_)
	).WillOnce(Invoke([=](auto in, auto &out __unused) {
		*write_unique = in.header.unique;
		sem_post(blocked_semaphore);
	}));
}

void setup_interruptor(pthread_t target, bool sleep = false)
{
	ASSERT_NE(SIG_ERR, signal(SIGUSR2, sigusr2_handler)) << strerror(errno);
	killer_should_sleep = sleep;
	ASSERT_EQ(0, pthread_create(&m_child, NULL, killer, (void*)target))
		<< strerror(errno);
}

void SetUp() {
	const int mprot = PROT_READ | PROT_WRITE;
	const int mflags = MAP_ANON | MAP_SHARED;

	signaled_semaphore = NULL;

	blocked_semaphore = (sem_t*)mmap(NULL, sizeof(*blocked_semaphore),
		mprot, mflags, -1, 0);
	ASSERT_NE(MAP_FAILED, blocked_semaphore) << strerror(errno);
	ASSERT_EQ(0, sem_init(blocked_semaphore, 1, 0)) << strerror(errno);
	ASSERT_EQ(0, siginterrupt(SIGUSR2, 1));

	FuseTest::SetUp();
}

void TearDown() {
	struct sigaction sa;

	if (m_child != NULL) {
		pthread_join(m_child, NULL);
	}
	bzero(&sa, sizeof(sa));
	sa.sa_handler = SIG_DFL;
	sigaction(SIGUSR2, &sa, NULL);

	sem_destroy(blocked_semaphore);
	munmap(blocked_semaphore, sizeof(*blocked_semaphore));

	FuseTest::TearDown();
}
};

class Intr: public Interrupt {};

class Nointr: public Interrupt {
	void SetUp() {
		m_nointr = true;
		Interrupt::SetUp();
	}
};

static void* mkdir0(void* arg __unused) {
	ssize_t r;

	r = mkdir(FULLDIRPATH0, MODE);
	if (r >= 0)
		return 0;
	else
		return (void*)(intptr_t)errno;
}

static void* read1(void* arg) {
	const size_t bufsize = FILESIZE;
	char buf[bufsize];
	int fd = (int)(intptr_t)arg;
	ssize_t r;

	r = read(fd, buf, bufsize);
	if (r >= 0)
		return 0;
	else
		return (void*)(intptr_t)errno;
}

/* 
 * An interrupt operation that gets received after the original command is
 * complete should generate an EAGAIN response.
 */
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236530 */
TEST_F(Intr, already_complete)
{
	uint64_t ino = 42;
	pthread_t self;
	uint64_t mkdir_unique = 0;
	Sequence seq;

	self = pthread_self();

	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH0)
	.InSequence(seq)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	expect_mkdir(&mkdir_unique);
	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_INTERRUPT &&
				in.body.interrupt.unique == mkdir_unique);
		}, Eq(true)),
		_)
	).WillOnce(Invoke([&](auto in, auto &out) {
		// First complete the mkdir request
		std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
		out0->header.unique = mkdir_unique;
		SET_OUT_HEADER_LEN(*out0, entry);
		out0->body.create.entry.attr.mode = S_IFDIR | MODE;
		out0->body.create.entry.nodeid = ino;
		out.push_back(std::move(out0));

		// Then, respond EAGAIN to the interrupt request
		std::unique_ptr<mockfs_buf_out> out1(new mockfs_buf_out);
		out1->header.unique = in.header.unique;
		out1->header.error = -EAGAIN;
		out1->header.len = sizeof(out1->header);
		out.push_back(std::move(out1));
	}));
	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH0)
	.InSequence(seq)
	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
		SET_OUT_HEADER_LEN(out, entry);
		out.body.entry.attr.mode = S_IFDIR | MODE;
		out.body.entry.nodeid = ino;
		out.body.entry.attr.nlink = 2;
	})));

	setup_interruptor(self);
	EXPECT_EQ(0, mkdir(FULLDIRPATH0, MODE)) << strerror(errno);
	/* 
	 * The final syscall simply ensures that the test's main thread doesn't
	 * end before the daemon finishes responding to the FUSE_INTERRUPT.
	 */
	EXPECT_EQ(0, access(FULLDIRPATH0, F_OK)) << strerror(errno);
}

/*
 * If a FUSE file system returns ENOSYS for a FUSE_INTERRUPT operation, the
 * kernel should not attempt to interrupt any other operations on that mount
 * point.
 */
TEST_F(Intr, enosys)
{
	uint64_t ino0 = 42, ino1 = 43;;
	uint64_t mkdir_unique;
	pthread_t self, th0;
	sem_t sem0, sem1;
	void *thr0_value;
	Sequence seq;

	self = pthread_self();
	ASSERT_EQ(0, sem_init(&sem0, 0, 0)) << strerror(errno);
	ASSERT_EQ(0, sem_init(&sem1, 0, 0)) << strerror(errno);

	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH1)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH0)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	expect_mkdir(&mkdir_unique);
	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_INTERRUPT &&
				in.body.interrupt.unique == mkdir_unique);
		}, Eq(true)),
		_)
	).InSequence(seq)
	.WillOnce(Invoke([&](auto in, auto &out) {
		// reject FUSE_INTERRUPT and respond to the FUSE_MKDIR
		std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
		std::unique_ptr<mockfs_buf_out> out1(new mockfs_buf_out);

		out0->header.unique = in.header.unique;
		out0->header.error = -ENOSYS;
		out0->header.len = sizeof(out0->header);
		out.push_back(std::move(out0));

		SET_OUT_HEADER_LEN(*out1, entry);
		out1->body.create.entry.attr.mode = S_IFDIR | MODE;
		out1->body.create.entry.nodeid = ino1;
		out1->header.unique = mkdir_unique;
		out.push_back(std::move(out1));
	}));
	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_MKDIR);
		}, Eq(true)),
		_)
	).InSequence(seq)
	.WillOnce(Invoke([&](auto in, auto &out) {
		std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);

		sem_post(&sem0);
		sem_wait(&sem1);

		SET_OUT_HEADER_LEN(*out0, entry);
		out0->body.create.entry.attr.mode = S_IFDIR | MODE;
		out0->body.create.entry.nodeid = ino0;
		out0->header.unique = in.header.unique;
		out.push_back(std::move(out0));
	}));

	setup_interruptor(self);
	/* First mkdir operation should finish synchronously */
	ASSERT_EQ(0, mkdir(FULLDIRPATH1, MODE)) << strerror(errno);

	ASSERT_EQ(0, pthread_create(&th0, NULL, mkdir0, NULL))
		<< strerror(errno);

	sem_wait(&sem0);
	/*
	 * th0 should be blocked waiting for the fuse daemon thread.
	 * Signal it.  No FUSE_INTERRUPT should result
	 */
	pthread_kill(th0, SIGUSR1);
	/* Allow the daemon thread to proceed */
	sem_post(&sem1);
	pthread_join(th0, &thr0_value);
	/* Second mkdir should've finished without error */
	EXPECT_EQ(0, (intptr_t)thr0_value);
}

/*
 * A FUSE filesystem is legally allowed to ignore INTERRUPT operations, and
 * complete the original operation whenever it damn well pleases.
 */
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236530 */
TEST_F(Intr, ignore)
{
	uint64_t ino = 42;
	pthread_t self;
	uint64_t mkdir_unique;

	self = pthread_self();

	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH0)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	expect_mkdir(&mkdir_unique);
	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_INTERRUPT &&
				in.body.interrupt.unique == mkdir_unique);
		}, Eq(true)),
		_)
	).WillOnce(Invoke([&](auto in __unused, auto &out) {
		// Ignore FUSE_INTERRUPT; respond to the FUSE_MKDIR
		std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
		out0->header.unique = mkdir_unique;
		SET_OUT_HEADER_LEN(*out0, entry);
		out0->body.create.entry.attr.mode = S_IFDIR | MODE;
		out0->body.create.entry.nodeid = ino;
		out.push_back(std::move(out0));
	}));

	setup_interruptor(self);
	ASSERT_EQ(0, mkdir(FULLDIRPATH0, MODE)) << strerror(errno);
}

/*
 * A restartable operation (basically, anything except write or setextattr)
 * that hasn't yet been sent to userland can be interrupted without sending
 * FUSE_INTERRUPT, and will be automatically restarted.
 */
TEST_F(Intr, in_kernel_restartable)
{
	const char FULLPATH1[] = "mountpoint/other_file.txt";
	const char RELPATH1[] = "other_file.txt";
	uint64_t ino0 = 42, ino1 = 43;
	int fd1;
	pthread_t self, th0, th1;
	sem_t sem0, sem1;
	void *thr0_value, *thr1_value;

	ASSERT_EQ(0, sem_init(&sem0, 0, 0)) << strerror(errno);
	ASSERT_EQ(0, sem_init(&sem1, 0, 0)) << strerror(errno);
	self = pthread_self();

	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH0)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	expect_lookup(RELPATH1, ino1);
	expect_open(ino1, 0, 1);
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_MKDIR);
		}, Eq(true)),
		_)
	).WillOnce(Invoke(ReturnImmediate([&](auto in __unused, auto& out) {
		/* Let the next write proceed */
		sem_post(&sem1);
		/* Pause the daemon thread so it won't read the next op */
		sem_wait(&sem0);

		SET_OUT_HEADER_LEN(out, entry);
		out.body.create.entry.attr.mode = S_IFDIR | MODE;
		out.body.create.entry.nodeid = ino0;
	})));
	FuseTest::expect_read(ino1, 0, FILESIZE, 0, NULL);

	fd1 = open(FULLPATH1, O_RDONLY);
	ASSERT_LE(0, fd1) << strerror(errno);

	/* Use a separate thread for each operation */
	ASSERT_EQ(0, pthread_create(&th0, NULL, mkdir0, NULL))
		<< strerror(errno);

	sem_wait(&sem1);	/* Sequence the two operations */

	ASSERT_EQ(0, pthread_create(&th1, NULL, read1, (void*)(intptr_t)fd1))
		<< strerror(errno);

	setup_interruptor(self, true);

	pause();		/* Wait for signal */

	/* Unstick the daemon */
	ASSERT_EQ(0, sem_post(&sem0)) << strerror(errno);

	/* Wait awhile to make sure the signal generates no FUSE_INTERRUPT */
	nap();

	pthread_join(th1, &thr1_value);
	pthread_join(th0, &thr0_value);
	EXPECT_EQ(0, (intptr_t)thr1_value);
	EXPECT_EQ(0, (intptr_t)thr0_value);
	sem_destroy(&sem1);
	sem_destroy(&sem0);

	leak(fd1);
}

/*
 * An operation that hasn't yet been sent to userland can be interrupted
 * without sending FUSE_INTERRUPT.  If it's a non-restartable operation (write
 * or setextattr) it will return EINTR.
 */
TEST_F(Intr, in_kernel_nonrestartable)
{
	const char FULLPATH1[] = "mountpoint/other_file.txt";
	const char RELPATH1[] = "other_file.txt";
	const char value[] = "whatever";
	ssize_t value_len = strlen(value) + 1;
	uint64_t ino0 = 42, ino1 = 43;
	int ns = EXTATTR_NAMESPACE_USER;
	int fd1;
	pthread_t self, th0;
	sem_t sem0, sem1;
	void *thr0_value;
	ssize_t r;

	ASSERT_EQ(0, sem_init(&sem0, 0, 0)) << strerror(errno);
	ASSERT_EQ(0, sem_init(&sem1, 0, 0)) << strerror(errno);
	self = pthread_self();

	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH0)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	expect_lookup(RELPATH1, ino1);
	expect_open(ino1, 0, 1);
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_MKDIR);
		}, Eq(true)),
		_)
	).WillOnce(Invoke(ReturnImmediate([&](auto in __unused, auto& out) {
		/* Let the next write proceed */
		sem_post(&sem1);
		/* Pause the daemon thread so it won't read the next op */
		sem_wait(&sem0);

		SET_OUT_HEADER_LEN(out, entry);
		out.body.create.entry.attr.mode = S_IFDIR | MODE;
		out.body.create.entry.nodeid = ino0;
	})));

	fd1 = open(FULLPATH1, O_WRONLY);
	ASSERT_LE(0, fd1) << strerror(errno);

	/* Use a separate thread for the first write */
	ASSERT_EQ(0, pthread_create(&th0, NULL, mkdir0, NULL))
		<< strerror(errno);

	sem_wait(&sem1);	/* Sequence the two operations */

	setup_interruptor(self, true);

	r = extattr_set_fd(fd1, ns, "foo", (const void*)value, value_len);
	EXPECT_NE(0, r);
	EXPECT_EQ(EINTR, errno);

	/* Unstick the daemon */
	ASSERT_EQ(0, sem_post(&sem0)) << strerror(errno);

	/* Wait awhile to make sure the signal generates no FUSE_INTERRUPT */
	nap();

	pthread_join(th0, &thr0_value);
	EXPECT_EQ(0, (intptr_t)thr0_value);
	sem_destroy(&sem1);
	sem_destroy(&sem0);

	leak(fd1);
}

/* 
 * A syscall that gets interrupted while blocking on FUSE I/O should send a
 * FUSE_INTERRUPT command to the fuse filesystem, which should then send EINTR
 * in response to the _original_ operation.  The kernel should ultimately
 * return EINTR to userspace
 */
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236530 */
TEST_F(Intr, in_progress)
{
	pthread_t self;
	uint64_t mkdir_unique;

	self = pthread_self();

	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH0)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	expect_mkdir(&mkdir_unique);
	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_INTERRUPT &&
				in.body.interrupt.unique == mkdir_unique);
		}, Eq(true)),
		_)
	).WillOnce(Invoke([&](auto in __unused, auto &out) {
		std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
		out0->header.error = -EINTR;
		out0->header.unique = mkdir_unique;
		out0->header.len = sizeof(out0->header);
		out.push_back(std::move(out0));
	}));

	setup_interruptor(self);
	ASSERT_EQ(-1, mkdir(FULLDIRPATH0, MODE));
	EXPECT_EQ(EINTR, errno);
}

/* Reads should also be interruptible */
TEST_F(Intr, in_progress_read)
{
	const char FULLPATH[] = "mountpoint/some_file.txt";
	const char RELPATH[] = "some_file.txt";
	const size_t bufsize = 80;
	char buf[bufsize];
	uint64_t ino = 42;
	int fd;
	pthread_t self;
	uint64_t read_unique;

	self = pthread_self();

	expect_lookup(RELPATH, ino);
	expect_open(ino, 0, 1);
	expect_read(ino, &read_unique);
	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_INTERRUPT &&
				in.body.interrupt.unique == read_unique);
		}, Eq(true)),
		_)
	).WillOnce(Invoke([&](auto in __unused, auto &out) {
		std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
		out0->header.error = -EINTR;
		out0->header.unique = read_unique;
		out0->header.len = sizeof(out0->header);
		out.push_back(std::move(out0));
	}));

	fd = open(FULLPATH, O_RDONLY);
	ASSERT_LE(0, fd) << strerror(errno);

	setup_interruptor(self);
	ASSERT_EQ(-1, read(fd, buf, bufsize));
	EXPECT_EQ(EINTR, errno);

	leak(fd);
}

/*
 * When mounted with -o nointr, fusefs will block signals while waiting for the
 * server.
 */
TEST_F(Nointr, block)
{
	uint64_t ino = 42;
	pthread_t self;
	sem_t sem0;

	ASSERT_EQ(0, sem_init(&sem0, 0, 0)) << strerror(errno);
	signaled_semaphore = &sem0;
	self = pthread_self();

	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH0)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_MKDIR);
		}, Eq(true)),
		_)
	).WillOnce(Invoke(ReturnImmediate([&](auto in __unused, auto& out) {
		/* Let the killer proceed */
		sem_post(blocked_semaphore);

		/* Wait until after the signal has been sent */
		sem_wait(signaled_semaphore);
		/* Allow time for the mkdir thread to receive the signal */
		nap();

		/* Finally, complete the original op */
		SET_OUT_HEADER_LEN(out, entry);
		out.body.create.entry.attr.mode = S_IFDIR | MODE;
		out.body.create.entry.nodeid = ino;
	})));
	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_INTERRUPT);
		}, Eq(true)),
		_)
	).Times(0);

	setup_interruptor(self);
	ASSERT_EQ(0, mkdir(FULLDIRPATH0, MODE)) << strerror(errno);

	sem_destroy(&sem0);
}

/* FUSE_INTERRUPT operations should take priority over other pending ops */
TEST_F(Intr, priority)
{
	Sequence seq;
	uint64_t ino1 = 43;
	uint64_t mkdir_unique;
	pthread_t th0;
	sem_t sem0, sem1;

	ASSERT_EQ(0, sem_init(&sem0, 0, 0)) << strerror(errno);
	ASSERT_EQ(0, sem_init(&sem1, 0, 0)) << strerror(errno);

	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH0)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH1)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	EXPECT_CALL(*m_mock, process(
		ResultOf([=](auto in) {
			return (in.header.opcode == FUSE_MKDIR);
		}, Eq(true)),
		_)
	).InSequence(seq)
	.WillOnce(Invoke(ReturnImmediate([&](auto in, auto& out) {
		mkdir_unique = in.header.unique;

		/* Let the next mkdir proceed */
		sem_post(&sem1);

		/* Pause the daemon thread so it won't read the next op */
		sem_wait(&sem0);

		/* Finally, interrupt the original op */
		out.header.error = -EINTR;
		out.header.unique = mkdir_unique;
		out.header.len = sizeof(out.header);
	})));
	/* 
	 * FUSE_INTERRUPT should be received before the second FUSE_MKDIR,
	 * even though it was generated later
	 */
	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_INTERRUPT &&
				in.body.interrupt.unique == mkdir_unique);
		}, Eq(true)),
		_)
	).InSequence(seq)
	.WillOnce(Invoke(ReturnErrno(EAGAIN)));
	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_MKDIR);
		}, Eq(true)),
		_)
	).InSequence(seq)
	.WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
		SET_OUT_HEADER_LEN(out, entry);
		out.body.create.entry.attr.mode = S_IFDIR | MODE;
		out.body.create.entry.nodeid = ino1;
	})));

	/* Use a separate thread for the first mkdir */
	ASSERT_EQ(0, pthread_create(&th0, NULL, mkdir0, NULL))
		<< strerror(errno);

	signaled_semaphore = &sem0;

	sem_wait(&sem1);	/* Sequence the two mkdirs */
	setup_interruptor(th0, true);
	ASSERT_EQ(0, mkdir(FULLDIRPATH1, MODE)) << strerror(errno);

	pthread_join(th0, NULL);
	sem_destroy(&sem1);
	sem_destroy(&sem0);
}

/*
 * If the FUSE filesystem receives the FUSE_INTERRUPT operation before
 * processing the original, then it should wait for "some timeout" for the
 * original operation to arrive.  If not, it should send EAGAIN to the
 * INTERRUPT operation, and the kernel should requeue the INTERRUPT.
 *
 * In this test, we'll pretend that the INTERRUPT arrives too soon, gets
 * EAGAINed, then the kernel requeues it, and the second time around it
 * successfully interrupts the original
 */
/* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236530 */
TEST_F(Intr, too_soon)
{
	Sequence seq;
	pthread_t self;
	uint64_t mkdir_unique;

	self = pthread_self();

	EXPECT_LOOKUP(FUSE_ROOT_ID, RELDIRPATH0)
	.WillOnce(Invoke(ReturnErrno(ENOENT)));
	expect_mkdir(&mkdir_unique);

	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_INTERRUPT &&
				in.body.interrupt.unique == mkdir_unique);
		}, Eq(true)),
		_)
	).InSequence(seq)
	.WillOnce(Invoke(ReturnErrno(EAGAIN)));

	EXPECT_CALL(*m_mock, process(
		ResultOf([&](auto in) {
			return (in.header.opcode == FUSE_INTERRUPT &&
				in.body.interrupt.unique == mkdir_unique);
		}, Eq(true)),
		_)
	).InSequence(seq)
	.WillOnce(Invoke([&](auto in __unused, auto &out __unused) {
		std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
		out0->header.error = -EINTR;
		out0->header.unique = mkdir_unique;
		out0->header.len = sizeof(out0->header);
		out.push_back(std::move(out0));
	}));

	setup_interruptor(self);
	ASSERT_EQ(-1, mkdir(FULLDIRPATH0, MODE));
	EXPECT_EQ(EINTR, errno);
}


// TODO: add a test where write returns EWOULDBLOCK