-
Notifications
You must be signed in to change notification settings - Fork 11
/
nfstest.c
1382 lines (1178 loc) · 32.4 KB
/
nfstest.c
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Compile:
gcc nfstest.c -o nfstest -g -Wall -W
On machine 1 use:
./nfstest <port> <path to a test file>
On machine 2 use:
./nfstest <machine 1 hostname> <machine 1 port> <path to the same test file>
Machine 2 must be NFS client. It probably doesn't matter if machine 1 is
NFS client or the server, but run it in another NFS client just to be safe.
The test file must not be in the current directory, or the test will fail
in rmdir(".").
*/
#if !defined(__sun) && !defined(_AIX)
# define HAVE_FLOCK
#endif
#if defined(__linux__) || defined(__sun)
# define ST_NSECS(st) (st).st_mtim.tv_nsec
# define HAVE_ST_NSECS
#elif defined (__FreeBSD__) || defined(__APPLE__)
# define ST_NSECS(st) (st).st_mtimespec.tv_nsec
# define HAVE_ST_NSECS
#else
# define ST_NSECS(st) 0
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#define __USE_GNU
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#ifdef HAVE_FLOCK
# include <sys/file.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
enum nfs_cache_flush_method {
NFS_CACHE_FLUSH_METHOD_NONE,
NFS_CACHE_FLUSH_METHOD_OPEN_CLOSE,
NFS_CACHE_FLUSH_METHOD_CLOSE_OPEN,
NFS_CACHE_FLUSH_METHOD_FCHOWN_1_1,
NFS_CACHE_FLUSH_METHOD_FCHOWN_UID,
NFS_CACHE_FLUSH_METHOD_FCHMOD,
NFS_CACHE_FLUSH_METHOD_CHOWN_1_1,
NFS_CACHE_FLUSH_METHOD_CHOWN_UID,
NFS_CACHE_FLUSH_METHOD_CHMOD,
NFS_CACHE_FLUSH_METHOD_RMDIR,
NFS_CACHE_FLUSH_METHOD_RMDIR_PARENT,
NFS_CACHE_FLUSH_METHOD_DUP_CLOSE,
NFS_CACHE_FLUSH_METHOD_FCNTL_SHARED,
NFS_CACHE_FLUSH_METHOD_FCNTL_EXCL,
#ifdef HAVE_FLOCK
NFS_CACHE_FLUSH_METHOD_FLOCK_SHARED,
NFS_CACHE_FLUSH_METHOD_FLOCK_EXCL,
#endif
NFS_CACHE_FLUSH_METHOD_FSYNC,
NFS_CACHE_FLUSH_METHOD_O_SYNC,
#ifdef O_DIRECT
NFS_CACHE_FLUSH_METHOD_O_DIRECT,
#endif
NFS_CACHE_FLUSH_METHOD_COUNT
};
static const char *
nfs_cache_flush_method_names[NFS_CACHE_FLUSH_METHOD_COUNT] = {
"no caching",
"open+close",
"close+open",
"fchown(-1, -1)",
"fchown(uid, -1)",
"fchmod(mode)",
"chown(-1, -1)",
"chown(uid, -1)",
"chmod(mode)",
"rmdir()",
"rmdir(parent dir)",
"dup+close",
"fcntl(shared)",
"fcntl(exclusive)",
#ifdef HAVE_FLOCK
"flock(shared)",
"flock(exclusive)",
#endif
"fsync()",
"fcntl(O_SYNC)"
#ifdef O_DIRECT
,"O_DIRECT"
#endif
};
static int reverse = 0;
static void i_errorv(const char *fmt, va_list args)
{
char fmt2[1024];
unsigned int len = strlen(fmt);
int orig_errno = errno;
if (len > 2 && strcmp(fmt + len-2, "%m") == 0) {
snprintf(fmt2, len-1, "%s", fmt);
fmt = fmt2;
}
vprintf(fmt, args);
if (fmt == fmt2)
printf("%s", strerror(orig_errno));
printf("\n");
}
static void i_error(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
i_errorv(fmt, args);
va_end(args);
}
static void i_fatal(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
i_errorv(fmt, args);
va_end(args);
exit(1);
}
static void fcntl_lock(int fd, int lock_type)
{
struct flock fl;
fl.l_type = lock_type;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
if (fcntl(fd, F_SETLK, &fl) < 0) {
i_error("fcntl(setlk, %s) failed: %m",
lock_type == F_UNLCK ? "unlock" :
(lock_type == F_RDLCK ? "read" : "write"));
}
}
static void nfs_cache_flush_before(const char *path, int *fd_p,
enum nfs_cache_flush_method method)
{
char *p, dir[1024];
int fd = fd_p == NULL ? -1 : *fd_p;
struct stat st;
int fd2, old_flags;
off_t old_offset;
switch (method) {
case NFS_CACHE_FLUSH_METHOD_NONE:
break;
case NFS_CACHE_FLUSH_METHOD_OPEN_CLOSE:
fd2 = open(path, O_RDWR);
if (fd2 != -1)
close(fd2);
break;
case NFS_CACHE_FLUSH_METHOD_CLOSE_OPEN:
if (fd == -1)
break;
old_offset = lseek(fd, 0, SEEK_CUR);
old_flags = fcntl(fd, F_GETFL, 0);
close(fd);
*fd_p = fd = open(path, old_flags);
if (fd == -1)
i_fatal("flush reopen: open(%s) failed: %m");
lseek(fd, old_offset, SEEK_CUR);
break;
#ifdef O_DIRECT
case NFS_CACHE_FLUSH_METHOD_O_DIRECT:
if (fd == -1)
break;
old_flags = fcntl(fd, F_GETFL, 0);
if (fcntl(fd, F_SETFL, old_flags | O_DIRECT) < 0)
i_error("fcntl(%s, O_DIRECT) failed: %m", path);
break;
#endif
case NFS_CACHE_FLUSH_METHOD_O_SYNC:
if (fd == -1)
break;
old_flags = fcntl(fd, F_GETFL, 0);
if (fcntl(fd, F_SETFL, old_flags | O_SYNC) < 0)
i_error("fcntl(%s, O_SYNC) failed: %m", path);
break;
case NFS_CACHE_FLUSH_METHOD_FCHOWN_1_1:
if (fd == -1)
break;
if (fchown(fd, (uid_t)-1, (gid_t)-1) < 0)
i_fatal("fchown(-1, -1) failed: %m");
break;
case NFS_CACHE_FLUSH_METHOD_FCHOWN_UID:
if (fd == -1)
break;
if (fstat(fd, &st) < 0)
i_fatal("fstat() failed: %m");
if (fchown(fd, st.st_uid, (gid_t)-1) < 0 && errno != EPERM)
i_fatal("fchown() failed: %m");
break;
case NFS_CACHE_FLUSH_METHOD_FCHMOD:
if (fd == -1)
break;
if (fstat(fd, &st) < 0)
i_fatal("fstat() failed: %m");
if (fchmod(fd, st.st_mode) < 0)
i_fatal("fchmod() failed: %m");
break;
case NFS_CACHE_FLUSH_METHOD_CHOWN_1_1:
if (chown(path, (uid_t)-1, (gid_t)-1) < 0)
i_fatal("chown(-1, -1) failed: %m");
break;
case NFS_CACHE_FLUSH_METHOD_CHOWN_UID:
if (stat(path, &st) < 0)
i_fatal("stat(%s) failed: %m", path);
if (chown(path, st.st_uid, (gid_t)-1) < 0)
i_fatal("chown() failed: %m");
break;
case NFS_CACHE_FLUSH_METHOD_CHMOD:
if (stat(path, &st) < 0)
i_fatal("stat(%s) failed: %m", path);
if (chmod(path, st.st_mode) < 0)
i_fatal("chmod() failed: %m");
break;
case NFS_CACHE_FLUSH_METHOD_RMDIR_PARENT:
p = strrchr(path, '/');
if (p == NULL)
strcpy(dir, ".");
else
snprintf(dir, p-path+1, "%s", path);
path = dir;
/* fall through */
case NFS_CACHE_FLUSH_METHOD_RMDIR:
if (rmdir(path) == 0)
i_fatal("Oops, rmdir(%s) actually worked", path);
else if (errno != ENOTEMPTY && errno != ENOTDIR &&
errno != EBUSY && errno != EEXIST)
i_error("rmdir(%s) failed: %m", path);
break;
case NFS_CACHE_FLUSH_METHOD_DUP_CLOSE:
if (fd == -1)
break;
fd2 = dup(fd);
if (fd2 < 0)
i_fatal("dup() failed: %m");
if (close(fd2) < 0)
i_fatal("close(duped) failed: %m");
break;
case NFS_CACHE_FLUSH_METHOD_FCNTL_SHARED:
if (fd == -1)
break;
fcntl_lock(fd, F_RDLCK);
break;
case NFS_CACHE_FLUSH_METHOD_FCNTL_EXCL:
if (fd == -1)
break;
fcntl_lock(fd, F_WRLCK);
break;
#ifdef HAVE_FLOCK
case NFS_CACHE_FLUSH_METHOD_FLOCK_SHARED:
if (fd == -1)
break;
if (flock(fd, LOCK_SH | LOCK_NB) < 0)
i_error("flock() failed: %m");
break;
case NFS_CACHE_FLUSH_METHOD_FLOCK_EXCL:
if (fd == -1)
break;
if (flock(fd, LOCK_EX | LOCK_NB) < 0)
i_error("flock() failed: %m");
break;
#endif
case NFS_CACHE_FLUSH_METHOD_FSYNC:
if (fd == -1)
break;
if (fsync(fd) < 0)
i_fatal("fsync() failed: %m");
break;
case NFS_CACHE_FLUSH_METHOD_COUNT:
abort();
}
}
static void nfs_cache_flush_after(const char *path, int *fd_p,
enum nfs_cache_flush_method method)
{
int old_flags, fd = fd_p == NULL ? -1 : *fd_p;
switch (method) {
case NFS_CACHE_FLUSH_METHOD_FCNTL_SHARED:
case NFS_CACHE_FLUSH_METHOD_FCNTL_EXCL:
if (fd == -1)
break;
fcntl_lock(fd, F_UNLCK);
break;
#ifdef HAVE_FLOCK
case NFS_CACHE_FLUSH_METHOD_FLOCK_SHARED:
case NFS_CACHE_FLUSH_METHOD_FLOCK_EXCL:
if (fd == -1)
break;
flock(fd, LOCK_UN);
break;
#endif
case NFS_CACHE_FLUSH_METHOD_O_SYNC:
#ifdef O_DIRECT
case NFS_CACHE_FLUSH_METHOD_O_DIRECT:
#endif
if (fd == -1)
break;
old_flags = fcntl(fd, F_GETFL, 0);
old_flags &= ~O_SYNC;
#ifdef O_DIRECT
old_flags &= ~O_DIRECT;
#endif
if (fcntl(fd, F_SETFL, old_flags) < 0)
i_error("fcntl(%s, restore flags) failed: %m", path);
break;
default:
/* when flushing writes we called _before() before doing the
write. to get these methods working we have to do it
afterwards.. */
nfs_cache_flush_before(path, fd_p, method);
break;
}
}
static void send_cmd(int fd, char cmd)
{
if (write(fd, &cmd, 1) != 1)
i_fatal("write(cmd) failed: %m");
}
static char read_cmd(int fd)
{
int ret;
char cmd;
ret = read(fd, &cmd, 1);
if (ret <= 0) {
if (ret == 0)
i_fatal("Connection lost");
i_fatal("read(cmd) failed: %m");
}
return cmd;
}
static void wait_cmd(int fd, char wanted_cmd)
{
char cmd;
cmd = read_cmd(fd);
if (wanted_cmd != cmd)
i_fatal("Unexpected command: %c != %c", cmd, wanted_cmd);
}
static int nfs_safe_open(const char *path, int flags)
{
int fd, i;
for (i = 0; i < 10; i++) {
fd = open(path, flags);
if (fd != -1 || errno != ESTALE)
break;
}
return fd;
}
static int nfs_safe_create(const char *path, int flags, int mode)
{
int fd, i;
for (i = 0; i < 10; i++) {
fd = open(path, flags, mode);
if (fd != -1 || errno != ESTALE)
break;
}
return fd;
}
static void nfs_test_estale_server(int socket_fd, const char *path)
{
int fd;
fd = nfs_safe_create(path, O_RDWR | O_CREAT, 0600);
if (fd < 0)
i_fatal("creat(%s) failed: %m", path);
write(fd, "hello", 5);
close(fd);
send_cmd(socket_fd, '1');
wait_cmd(socket_fd, '2');
if (unlink(path) < 0)
i_error("unlink(%s) failed: %m", path);
send_cmd(socket_fd, '3');
}
static void nfs_test_estale_client(int socket_fd, const char *path)
{
char buf[100];
int fd, ret;
send_cmd(socket_fd, 'S');
wait_cmd(socket_fd, '1');
fd = open(path, O_RDWR | O_CREAT, 0600);
if (fd < 0)
i_fatal("creat(%s) failed: %m", path);
send_cmd(socket_fd, '2');
wait_cmd(socket_fd, '3');
if ((ret = read(fd, buf, sizeof(buf))) < 0) {
if (errno == ESTALE)
printf("ESTALE errors happen on read()\n");
else if (errno == EIO) {
printf("EIO errors happen on read()\n");
if (fchown(fd, 0, (gid_t)-1) == 0)
printf(" - fchown() succeeded..\n");
else if (errno == ESTALE)
printf(" - fchown() returned ESTALE\n");
else
i_error(" - fchown() failed: %m");
} else
i_fatal("read(%s) failed: %m", path);
} else if (ret != 5) {
i_error("read(%s) returned %d bytes instead of 5", path, ret);
} else {
printf("ESTALE errors don't happen\n");
}
close(fd);
wait_cmd(socket_fd, '!');
}
static void nfs_test_oexcl_server(int socket_fd, const char *path)
{
int fd;
wait_cmd(socket_fd, '1');
fd = nfs_safe_create(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
i_fatal("open(%s, O_CREAT) failed: %m", path);
close(fd);
send_cmd(socket_fd, '2');
}
static void nfs_test_oexcl_client(int socket_fd, const char *path)
{
struct stat st;
int fd;
send_cmd(socket_fd, 'E');
/* make sure it doesn't exist */
if (unlink(path) < 0 && errno != ENOENT)
i_fatal("unlink(%s) failed: %m", path);
/* make sure its nonexistence is cached */
if (stat(path, &st) < 0 && errno != ENOENT)
i_fatal("stat(%s) failed: %m", path);
send_cmd(socket_fd, '1');
wait_cmd(socket_fd, '2');
fd = nfs_safe_create(path, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd < 0) {
if (errno == EEXIST) {
printf("O_EXCL appears to be working, "
"but this could be just faked by NFS client\n");
} else
i_error("open(%s) failed: %m", path);
} else {
printf("O_EXCL doesn't work\n");
(void)close(fd);
}
wait_cmd(socket_fd, '!');
}
static void nfs_test_nsecs_server(int socket_fd, const char *path)
{
struct timeval tv[2];
int fd;
char cmd;
fd = nfs_safe_create(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
i_fatal("open(%s, O_CREAT) failed: %m", path);
/* first test microseconds */
tv[0].tv_sec = 123456789;
tv[0].tv_usec = 123456;
tv[1] = tv[0];
if (utimes(path, tv) < 0)
i_fatal("utimes(%s) failed: %m", path);
send_cmd(socket_fd, '1');
while ((cmd = read_cmd(socket_fd)) != '2') {
if (write(fd, "1", 1) < 0)
i_fatal("write(%s) failed: %m", path);
if (fsync(fd) < 0)
i_fatal("fsync(%s) failed: %m", path);
send_cmd(socket_fd, '3');
}
close(fd);
}
static void nfs_test_nsecs_client(int socket_fd, const char *path)
{
struct stat st;
int i;
send_cmd(socket_fd, 'N');
wait_cmd(socket_fd, '1');
nfs_cache_flush_before(path, NULL, NFS_CACHE_FLUSH_METHOD_OPEN_CLOSE);
if (stat(path, &st) < 0)
i_fatal("stat(%s) failed: %m", path);
if (st.st_mtime != 123456789) {
i_error("mtime test failed, timestamp %u != 123456789",
st.st_mtime);
} else if (ST_NSECS(st)/1000 == 123456) {
/* we have microsecond resolution at least,
see if we have nanoseconds */
for (i = 0; i < 10 && ST_NSECS(st) % 1000 == 0; i++) {
send_cmd(socket_fd, 'x');
wait_cmd(socket_fd, '3');
nfs_cache_flush_before(path, NULL, NFS_CACHE_FLUSH_METHOD_OPEN_CLOSE);
if (stat(path, &st) < 0)
i_fatal("stat(%s) failed: %m", path);
}
if (ST_NSECS(st) % 1000 == 0)
printf("timestamps resolution: microseconds\n");
else
printf("timestamps resolution: nanoseconds\n");
} else if (ST_NSECS(st) != 0) {
printf("timestamps resolution: other (%u)\n",
(unsigned int)ST_NSECS(st));
} else {
#ifdef HAVE_ST_NSECS
printf("timestamps resolution: seconds\n");
#else
printf("timestamps resolution: unknown, "
"don't know how to get nanoseconds from stat()\n");
#endif
}
send_cmd(socket_fd, '2');
wait_cmd(socket_fd, '!');
}
static void nfs_test_fattrcache_server(int socket_fd, const char *path)
{
struct timeval tv[2];
int fd;
char cmd;
if (unlink(path) < 0 && errno != ENOENT)
i_fatal("unlink(%s) failed: %m", path);
fd = nfs_safe_create(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
i_fatal("creat(%s) failed: %m", path);
tv[0].tv_sec = time(NULL);
tv[0].tv_usec = 0;
tv[1] = tv[0];
if (utimes(path, tv) < 0)
i_fatal("utimes(%s) failed: %m", path);
send_cmd(socket_fd, '1');
wait_cmd(socket_fd, '2');
do {
if (write(fd, "hello", 5) != 5)
i_fatal("write(%s) failed: %m", path);
nfs_cache_flush_after(path, &fd, NFS_CACHE_FLUSH_METHOD_FSYNC);
/* make sure the mtime changes */
tv[0].tv_sec++;
tv[1].tv_sec++;
if (utimes(path, tv) < 0)
i_fatal("utimes(%s) failed: %m", path);
send_cmd(socket_fd, '3');
cmd = read_cmd(socket_fd);
} while (cmd == '2');
if (cmd != '4')
i_fatal("expected '4'");
close(fd);
}
static void nfs_test_fattrcache_client(int socket_fd, const char *path)
{
struct stat st1, st2;
enum nfs_cache_flush_method method;
int fd, fails = 0;
printf("\nTesting file attribute cache..\n");
send_cmd(socket_fd, 'F');
wait_cmd(socket_fd, '1');
fd = nfs_safe_open(path, O_RDWR);
if (fd < 0)
i_fatal("open(%s) failed: %m", path);
for (method = 1; method < NFS_CACHE_FLUSH_METHOD_COUNT; ) {
if (fstat(fd, &st1) < 0)
i_fatal("fstat(%s) failed: %m", path);
send_cmd(socket_fd, '2');
wait_cmd(socket_fd, '3');
if (fstat(fd, &st2) < 0)
i_fatal("fstat(%s) failed: %m", path);
if (st1.st_mtime == st2.st_mtime) {
nfs_cache_flush_before(path, &fd, method);
if (fstat(fd, &st2) < 0)
i_fatal("fstat(%s) failed: %m", path);
nfs_cache_flush_after(path, &fd, method);
printf("Attr cache flush %s: %s\n",
nfs_cache_flush_method_names[method],
st1.st_mtime == st2.st_mtime ? "failed" : "OK");
method++;
fails = 0;
} else {
/* didn't even require flushing. try again a couple
of times. */
if (++fails == 3) {
printf("NFS attribute cache seems to be disabled\n");
break;
}
}
}
close(fd);
send_cmd(socket_fd, '4');
wait_cmd(socket_fd, '!');
}
static void nfs_test_fhandlecache_server(int socket_fd, const char *path)
{
char dir[1024], temp_path1[1024], temp_path2[1024], *p;
struct timeval tv[2], dir_tv[2];
int fd;
char cmd;
if (unlink(path) < 0 && errno != ENOENT)
i_fatal("unlink(%s) failed: %m", path);
p = strrchr(path, '/');
if (p == NULL)
strcpy(dir, ".");
else
snprintf(dir, p - path + 1, "%s", path);
snprintf(temp_path1, sizeof(temp_path1), "%s.1", path);
snprintf(temp_path2, sizeof(temp_path2), "%s.2", path);
fd = nfs_safe_create(temp_path1, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
i_fatal("creat(%s) failed: %m", temp_path1);
close(fd);
fd = nfs_safe_create(temp_path2, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
i_fatal("creat(%s) failed: %m", temp_path2);
close(fd);
tv[0].tv_sec = 0;
tv[0].tv_usec = 0;
tv[1] = tv[0];
dir_tv[0].tv_sec = time(NULL);
dir_tv[0].tv_usec = 0;
dir_tv[1] = dir_tv[0];
if (link(temp_path1, path) < 0)
i_fatal("link(%s, %s) failed: %m", temp_path1, path);
if (utimes(path, tv) < 0)
i_fatal("utimes(%s) failed: %m", path);
if (utimes(dir, dir_tv) < 0)
i_fatal("utimes(%s) failed: %m", dir);
send_cmd(socket_fd, '1');
while ((cmd = read_cmd(socket_fd)) != '4') {
unlink(path);
if (cmd == 'b') {
if (link(temp_path1, path) < 0)
i_fatal("link(%s, %s) failed: %m", temp_path1, path);
} else {
if (link(temp_path2, path) < 0)
i_fatal("link(%s, %s) failed: %m", temp_path1, path);
}
tv[1].tv_sec++;
if (utimes(path, tv) < 0)
i_fatal("utimes(%s) failed: %m", path);
/* keep directory's mtime the same so the flushing doesn't
happen because of it. */
if (utimes(dir, dir_tv) < 0)
i_fatal("utimes(%s) failed: %m", dir);
send_cmd(socket_fd, '3');
}
if (cmd != '4')
i_fatal("expected '4'");
unlink(temp_path1);
unlink(temp_path2);
}
static void nfs_test_fhandlecache_client(int socket_fd, const char *path)
{
struct stat st1, st2;
enum nfs_cache_flush_method method;
char dir[1024], temp_path1[1024], temp_path2[1024], *p;
const char *flush_path;
time_t expected_mtime;
ino_t ino1, ino2;
int fd, file_fd, success = 0;
printf("\nTesting file handle cache..\n");
snprintf(temp_path1, sizeof(temp_path1), "%s.1", path);
snprintf(temp_path2, sizeof(temp_path2), "%s.2", path);
p = strrchr(path, '/');
if (p == NULL)
strcpy(dir, ".");
else
snprintf(dir, p - path + 1, "%s", path);
send_cmd(socket_fd, 'H');
wait_cmd(socket_fd, '1');
fd = open(dir, O_RDONLY);
if (fd == -1)
i_fatal("open(%s) failed: %m", dir);
if (stat(temp_path1, &st1) < 0)
i_error("stat(%s) failed: %m", temp_path1);
if (stat(temp_path2, &st2) < 0)
i_error("stat(%s) failed: %m", temp_path2);
if (st1.st_ino == st2.st_ino)
i_error("Temp files' inodes are the same..");
ino1 = st1.st_ino;
ino2 = st2.st_ino;
/* fill the file's attribute cache */
file_fd = nfs_safe_create(path, O_RDWR | O_CREAT, 0600);
if (file_fd == -1)
i_fatal("open(%s) failed: %m", path);
close(file_fd);
expected_mtime = 1;
for (method = 0; method < NFS_CACHE_FLUSH_METHOD_COUNT; ) {
if (stat(path, &st1) < 0 ||
stat(path, &st1) < 0)
i_fatal("stat(%s) failed: %m", path);
if (st1.st_ino == ino1)
send_cmd(socket_fd, 'a');
else if (st1.st_ino == ino2)
send_cmd(socket_fd, 'b');
else
i_fatal("%s has unexpected inode", path);
wait_cmd(socket_fd, '3');
flush_path = method == NFS_CACHE_FLUSH_METHOD_RMDIR ||
method == NFS_CACHE_FLUSH_METHOD_RMDIR_PARENT ?
path : dir;
nfs_cache_flush_before(flush_path, &fd, method);
if (stat(path, &st2) < 0)
i_fatal("stat(%s) failed: %m", path);
nfs_cache_flush_after(flush_path, &fd, method);
if (st1.st_ino != st2.st_ino)
success = 1;
printf("File handle cache flush %s: %s\n",
nfs_cache_flush_method_names[method],
st1.st_ino == st2.st_ino ? "failed" : "OK");
if (st1.st_ino == st2.st_ino &&
st2.st_mtime == expected_mtime)
printf(" - inode didn't change, but mtime did\n");
else if (st1.st_ino != st2.st_ino &&
st2.st_mtime != expected_mtime)
printf(" - inode changed, but mtime is wrong\n");
if (st1.st_ino != ino1 && st1.st_ino != ino2)
printf(" - inode is neither temp1 nor temp2 file's\n");
expected_mtime++;
method++;
}
send_cmd(socket_fd, '4');
if (!success)
printf("Looks like there's no way to flush directory's attribute cache\n");
close(fd);
wait_cmd(socket_fd, '!');
}
static void nfs_test_neg_fhandlecache_server(int socket_fd, const char *path)
{
struct timeval tv[2];
int fd;
char cmd;
if (unlink(path) < 0 && errno != ENOENT)
i_fatal("unlink(%s) failed: %m", path);
tv[0].tv_sec = 0;
tv[0].tv_usec = 0;
tv[1] = tv[0];
send_cmd(socket_fd, '1');
while ((cmd = read_cmd(socket_fd)) == '2') {
fd = open(path, O_CREAT | O_TRUNC, 0600);
if (fd == -1)
i_fatal("creat(%s) failed: %m", path);
close(fd);
if (utimes(path, tv) < 0)
i_fatal("utimes(%s) failed: %m", path);
tv[1].tv_sec++;
send_cmd(socket_fd, '3');
wait_cmd(socket_fd, '4');
if (unlink(path) < 0 && errno != ENOENT)
i_fatal("unlink(%s) failed: %m", path);
send_cmd(socket_fd, '5');
}
}
static void nfs_test_neg_fhandlecache_client(int socket_fd, const char *path)
{
struct stat st;
enum nfs_cache_flush_method method;
const char *flush_path;
char dir[1024], *p;
time_t expected_mtime;
int fd, success = 0;
printf("\nTesting negative file handle cache..\n");
p = strrchr(path, '/');
if (p == NULL)
strcpy(dir, ".");
else
snprintf(dir, p - path + 1, "%s", path);
send_cmd(socket_fd, 'G');
wait_cmd(socket_fd, '1');
fd = open(dir, O_RDONLY);
if (fd == -1)
i_fatal("open(%s) failed: %m", dir);
if (unlink(path) < 0 && errno != ENOENT)
i_fatal("unlink(%s) failed: %m", path);
expected_mtime = 0;
for (method = 0; method < NFS_CACHE_FLUSH_METHOD_COUNT; ) {
/* stat() a couple of to make sure it gets cached */
if (stat(path, &st) == 0 ||
stat(path, &st) == 0 ||
stat(path, &st) == 0) {
i_error("stat() succeeded, can't continue this test");
break;
}
send_cmd(socket_fd, '2');
wait_cmd(socket_fd, '3');
flush_path = method == NFS_CACHE_FLUSH_METHOD_RMDIR ||
method == NFS_CACHE_FLUSH_METHOD_RMDIR_PARENT ?
path : dir;
nfs_cache_flush_before(flush_path, &fd, method);
success = stat(path, &st) == 0;
if (!success && errno != ENOENT)
i_fatal("stat(%s) failed: %m", path);
nfs_cache_flush_after(flush_path, &fd, method);
printf("Negative file handle cache flush %s: %s\n",
nfs_cache_flush_method_names[method],
!success ? "failed" : "OK");
if (success && st.st_mtime != expected_mtime)
printf(" - mtime is wrong though\n");
expected_mtime++;
method++;
if (unlink(path) < 0 && errno != ENOENT)
i_fatal("unlink(%s) failed: %m", path);
send_cmd(socket_fd, '4');
wait_cmd(socket_fd, '5');
}
send_cmd(socket_fd, '6');
close(fd);
wait_cmd(socket_fd, '!');
}
static void nfs_test_data_cache_server(int socket_fd, const char *path)
{
struct timeval tv[2];
char buf[1024], cmd;
int fd;
memset(buf, 'a', sizeof(buf));
tv[0].tv_sec = time(NULL);
tv[0].tv_usec = 12345;
tv[1] = tv[0];
fd = nfs_safe_create(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
i_fatal("creat(%s) failed: %m", path);
if (write(fd, buf, 1024) != 1024)
i_fatal("write(%s) failed: %m", path);
nfs_cache_flush_after(path, &fd, NFS_CACHE_FLUSH_METHOD_FSYNC);
send_cmd(socket_fd, '1');
wait_cmd(socket_fd, '2');
cmd = 'b';
do {
/* keep mtime same all the time, so it doesn't affect checking.
use utimes() instead of utime() to make sure microseconds are
also reset (hopefully also resets nanoseconds) */
tv[1].tv_sec++;
if (utimes(path, tv) < 0)
i_fatal("utimes(%s) failed: %m", path);
send_cmd(socket_fd, '3');
wait_cmd(socket_fd, '4');
if (lseek(fd, 512, SEEK_SET) < 0)
i_fatal("lseek() failed: %m");
if (write(fd, &cmd, 1) != 1)
i_fatal("write(%s) failed: %m", path);
nfs_cache_flush_after(path, &fd, NFS_CACHE_FLUSH_METHOD_FSYNC);
if (utimes(path, tv) < 0)
i_fatal("utimes(%s) failed: %m", path);
send_cmd(socket_fd, cmd);
cmd = read_cmd(socket_fd);
} while (cmd != '5');
close(fd);
}
static void nfs_test_data_cache_client(int socket_fd, const char *path)
{
struct stat st;
char buf[1024], chr;
time_t mtime;
long mtime_nsecs;
int fd, ret, i, method;
printf("\nTesting data cache..\n");
send_cmd(socket_fd, 'D');
wait_cmd(socket_fd, '1');
fd = nfs_safe_open(path, O_RDWR);
if (fd < 0)
i_fatal("open(%s) failed: %m", path);
nfs_cache_flush_before(path, &fd, NFS_CACHE_FLUSH_METHOD_CLOSE_OPEN);
/* initial read, should work */
ret = read(fd, buf, sizeof(buf));
if (ret < 0)
i_fatal("read(%s) failed: %m", path);
if (ret == 0) {
i_error("data cache: Initial read failed to return everything");
return;
}
for (i = 0; i < ret; i++) {
if (buf[i] != 'a') {