Whamcloud - gitweb
LU-14786 lod: create missing debugfs file
[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  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <sys/mman.h>
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #include <sys/stat.h>
42 #include <assert.h>
43
44 int save_errno;
45
46 char usage[] =
47 "Usage: %s <file>\n"
48 "       mknod, truncate to larger size, open, and mmap file";
49
50 int main(int argc, char **argv)
51 {
52         void *mmappedData;
53         int rc;
54
55         if (argc != 2) {
56                 fprintf(stderr, usage, argv[0]);
57                 exit(1);
58         }
59
60         /* Create file without striping */
61         rc = mknod(argv[1], S_IFREG | 0666, 0);
62         if (rc) {
63                 save_errno = errno;
64                 perror("mknod");
65                 exit(save_errno);
66         }
67
68         /* use truncate to extend file size */
69         rc = truncate(argv[1], 4096);
70         if (rc) {
71                 save_errno = errno;
72                 perror("mknod");
73                 exit(save_errno);
74         }
75
76         rc = open(argv[1], O_RDONLY);
77         if (rc < 0) {
78                 save_errno = errno;
79                 perror("mknod");
80                 exit(save_errno);
81         }
82
83         /* mmap of file without striping should work */
84         mmappedData = mmap(NULL, 4096, PROT_READ,
85                            MAP_SHARED, rc, 0);
86         if (mmappedData == MAP_FAILED) {
87                 save_errno = errno;
88                 perror("mknod");
89                 exit(save_errno);
90         }
91
92         return 0;
93 }
94