Whamcloud - gitweb
LU-14095 gss: use RCU protection for sunrpc cache
[fs/lustre-release.git] / lustre / tests / mmap_mknod_test.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2015, Intel Corporation.
27  *
28  * Copyright (c) 2019, Whamcloud.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <sys/mman.h>
40 #include <sys/types.h>
41 #include <fcntl.h>
42 #include <sys/stat.h>
43 #include <assert.h>
44
45 int save_errno;
46
47 char usage[] =
48 "Usage: %s <file>\n"
49 "       mknod, truncate to larger size, open, and mmap file";
50
51 int main(int argc, char **argv)
52 {
53         void *mmappedData;
54         int rc;
55
56         if (argc != 2) {
57                 fprintf(stderr, usage, argv[0]);
58                 exit(1);
59         }
60
61         /* Create file without striping */
62         rc = mknod(argv[1], S_IFREG | 0666, 0);
63         if (rc) {
64                 save_errno = errno;
65                 perror("mknod");
66                 exit(save_errno);
67         }
68
69         /* use truncate to extend file size */
70         rc = truncate(argv[1], 4096);
71         if (rc) {
72                 save_errno = errno;
73                 perror("mknod");
74                 exit(save_errno);
75         }
76
77         rc = open(argv[1], O_RDONLY);
78         if (rc < 0) {
79                 save_errno = errno;
80                 perror("mknod");
81                 exit(save_errno);
82         }
83
84         /* mmap of file without striping should work */
85         mmappedData = mmap(NULL, 4096, PROT_READ,
86                            MAP_SHARED, rc, 0);
87         if (mmappedData == MAP_FAILED) {
88                 save_errno = errno;
89                 perror("mknod");
90                 exit(save_errno);
91         }
92
93         return 0;
94 }
95