FreeBSD Bugzilla – Attachment 248653 Details for
Bug 276002
nfscl: data corruption using both copy_file_range and mmap'd I/O
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Problem reproducer
mmap44.c (text/plain), 3.30 KB, created by
Peter Holm
on 2024-02-21 08:41:28 UTC
(
hide
)
Description:
Problem reproducer
Filename:
MIME Type:
Creator:
Peter Holm
Created:
2024-02-21 08:41:28 UTC
Size:
3.30 KB
patch
obsolete
>#include <sys/mman.h> >#include <sys/stat.h> > >#include <err.h> >#include <fcntl.h> >#include <pthread.h> >#include <stdio.h> >#include <stdlib.h> >#include <string.h> >#include <unistd.h> > >static off_t siz; >static pthread_mutex_t write_mutex; >static int fd, go; >static char *cp; > >static void * >memread(void *arg __unused) >{ > int i; > char c; > > while (go == 1) { > i = arc4random() % siz; > c = cp[i]; > if (c != 0x77) /* No unused vars here */ > usleep(arc4random() % 400); > } > return (0); >} > >static void * >memwrite(void *arg __unused) >{ > int i; > char c; > > while (go == 1) { > i = arc4random() % siz; > pthread_mutex_lock(&write_mutex); > c = cp[i]; > cp[i] = 0xee; /* This value seems to linger with NFS */ > cp[i] = c; > pthread_mutex_unlock(&write_mutex); > usleep(arc4random() % 400); > } > return (0); >} > >static void * >wr(void *arg __unused) >{ > off_t pos; > int r, s; > char buf[1024]; > > while (go == 1) { > s = arc4random() % sizeof(buf) + 1; > pos = arc4random() % (siz - s); > pthread_mutex_lock(&write_mutex); > if (lseek(fd, pos, SEEK_SET) == -1) > err(1, "lseek(%d)", (int)pos); > if ((r = read(fd, buf, s)) != s) { > fprintf(stderr, "r = %d, s = %d, pos = %d\n", r, s, (int)pos); > err(1, "read():2"); > } > if (lseek(fd, pos, SEEK_SET) == -1) > err(1, "lseek(%d)", (int)pos); > if (write(fd, buf, s) != s) > err(1, "write()"); > pthread_mutex_unlock(&write_mutex); > usleep(arc4random() % 400); > } > return (0); >} > >static void * >s1(void *arg __unused) >{ > > while (go == 1) { > if (fdatasync(fd) == -1) > err(1, "fdatasync()"); > usleep(arc4random() % 1000); > } > return (0); >} > >static void * >s2(void *arg __unused) >{ > > while (go == 1) { > if (fsync(fd) == -1) > err(1, "fdatasync()"); > usleep(arc4random() % 1000); > } > return (0); >} > >static void * >tr(void *arg __unused) >{ > int i, s; > char buf[1024]; > > memset(buf, 0x5a, sizeof(buf)); > while (go == 1) { > pthread_mutex_lock(&write_mutex); > if (lseek(fd, arc4random() % siz, SEEK_END) == -1) > err(1, "lseek() END"); > s = sizeof(buf); > for (i = 0; i < 50; i++) { > if (write(fd, buf, s) != s) > warn("write()"); > } > if (ftruncate(fd, siz) == -1) > err(1, "truncate()"); > pthread_mutex_unlock(&write_mutex); > usleep(arc4random() % 400); > } > return (0); >} > >int >main(int argc, char *argv[]) >{ > struct stat st; > pthread_t tp[6]; > int e, i; > > if (argc != 2) { > fprintf(stderr, "Usage: %s <file>\n", argv[0]); > exit(1); > } > if ((fd = open(argv[1], O_RDWR)) == -1) > err(1, "open(%s)", argv[1]); > if (fstat(fd, &st) == -1) > err(1, "stat(%s)", argv[1]); > siz = st.st_size; > cp = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); > if (cp == MAP_FAILED) > err(1, "mmap()"); > > go = 1; > pthread_mutex_init(&write_mutex, NULL); > if ((e = pthread_create(&tp[0], NULL, memwrite, NULL)) != 0) > errc(1, e, "pthread_create"); > if ((e = pthread_create(&tp[1], NULL, memread, NULL)) != 0) > errc(1, e, "pthread_create"); > if ((e = pthread_create(&tp[2], NULL, wr, NULL)) != 0) > errc(1, e, "pthread_create"); > if ((e = pthread_create(&tp[3], NULL, s1, NULL)) != 0) > errc(1, e, "pthread_create"); > if ((e = pthread_create(&tp[4], NULL, s2, NULL)) != 0) > errc(1, e, "pthread_create"); > if ((e = pthread_create(&tp[5], NULL, tr, NULL)) != 0) > errc(1, e, "pthread_create"); > > sleep(60); > go = 0; > for (i = 0; i < 5; i++) > pthread_join(tp[i], NULL); > if (munmap(cp, siz) == -1) > err(1, "munmap()"); > close(fd); >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 276002
:
247343
|
247346
|
247360
|
247424
| 248653 |
248770
|
248807
|
249298
|
250535