Whamcloud - gitweb
- landed b_hd_cray_merge3
[fs/lustre-release.git] / lustre / tests / multiop.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #define _GNU_SOURCE /* pull in O_DIRECTORY in bits/fcntl.h */
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/mman.h>
12 #include <signal.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15
16 #define T1 "write data before unlink\n"
17 #define T2 "write data after unlink\n"
18 char buf[128];
19
20 char usage[] = 
21 "Usage: %s filename command-sequence\n"
22 "    command-sequence items:\n"
23 "        d  mkdir\n"
24 "        D  open(O_DIRECTORY)\n"
25 "        o  open(O_RDONLY)\n"
26 "        O  open(O_CREAT|O_RDWR)\n"
27 "        L  link\n"
28 "        l  symlink\n"
29 "        u  unlink\n"
30 "        U  munmap\n"
31 "        m  mknod\n"
32 "        M  rw mmap to EOF (must open and stat prior)\n"
33 "        N  rename\n"
34 "        c  close\n"
35 "        _  wait for signal\n"
36 "        R  reference entire mmap-ed region\n"
37 "        r  read\n"
38 "        s  stat\n"
39 "        S  fstat\n"
40 "        t  fchmod\n"
41 "        T  ftruncate to zero\n"
42 "        w  write\n"
43 "        W  write entire mmap-ed region\n"
44 "        y  fsync\n"
45 "        Y  fdatasync\n"
46 "        z  seek to zero\n";
47
48 static int usr1_received;
49 void usr1_handler(int unused) 
50
51         usr1_received = 1;
52 }
53
54 static const char *
55 pop_arg(int argc, char *argv[])
56 {
57         static int cur_arg = 3;
58
59         if (cur_arg >= argc)
60                 return NULL;
61
62         return argv[cur_arg++];
63 }
64 #define POP_ARG() (pop_arg(argc, argv))
65
66 int main(int argc, char **argv)
67 {
68         char *fname, *commands;
69         const char *newfile;
70         struct stat st;
71         size_t mmap_len = 0, i;
72         unsigned char *mmap_ptr = NULL, junk = 0;
73         int fd = -1;
74
75         if (argc < 3) {
76                 fprintf(stderr, usage, argv[0]);
77                 exit(1);
78         }
79
80         signal(SIGUSR1, usr1_handler);
81
82         fname = argv[1];
83
84         for (commands = argv[2]; *commands; commands++) {
85                 switch (*commands) {
86                 case '_':
87                         if (usr1_received == 0)
88                                 pause();
89                         usr1_received = 0;
90                         signal(SIGUSR1, usr1_handler);
91                         break;
92                 case 'c':
93                         if (close(fd) == -1) {
94                                 perror("close");
95                                 exit(1);
96                         }
97                         fd = -1;
98                         break;
99                 case 'd':
100                         if (mkdir(fname, 0755) == -1) {
101                                 perror("mkdir(0755)");
102                                 exit(1);
103                         }
104                         break;
105                 case 'D':
106                         fd = open(fname, O_DIRECTORY);
107                         if (fd == -1) {
108                                 perror("open(O_DIRECTORY)");
109                                 exit(1);
110                         }
111                         break;
112                 case 'l':
113                         newfile = POP_ARG();
114                         if (!newfile)
115                                 newfile = fname;
116                         if (symlink(fname, newfile)) {
117                                 perror("symlink()");
118                                 exit(1);
119                         }
120                         break;
121                 case 'L':
122                         newfile = POP_ARG();
123                         if (!newfile)
124                                 newfile = fname;
125                         if (link(fname, newfile)) {
126                                 perror("symlink()");
127                                 exit(1);
128                         }
129                         break;
130                 case 'm':
131                         if (mknod(fname, S_IFREG | 0644, 0) == -1) {
132                                 perror("mknod(S_IFREG|0644, 0)");
133                                 exit(1);
134                         }
135                         break;
136                 case 'M':
137                         mmap_len = st.st_size;
138                         mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
139                                         MAP_SHARED, fd, 0);
140                         if (mmap_ptr == MAP_FAILED) {
141                                 perror("mmap");
142                                 exit(1);
143                         }
144                         break;
145                 case 'N':
146                         newfile = POP_ARG();
147                         if (!newfile)
148                                 newfile = fname;
149                         if (rename (fname, newfile)) {
150                                 perror("rename()");
151                                 exit(1);
152                         }
153                         break;
154                 case 'O':
155                         fd = open(fname, O_CREAT|O_RDWR, 0644);
156                         if (fd == -1) {
157                                 perror("open(O_RDWR|O_CREAT)");
158                                 exit(1);
159                         }
160                         break;
161                 case 'o':
162                         fd = open(fname, O_RDONLY);
163                         if (fd == -1) {
164                                 perror("open(O_RDONLY)");
165                                 exit(1);
166                         }
167                         break;
168                 case 'r': {
169                         char buf;
170                         if (read(fd, &buf, 1) == -1) {
171                                 perror("read");
172                                 exit(1);
173                         }
174                         }
175                 case 'S':
176                         if (fstat(fd, &st) == -1) {
177                                 perror("fstat");
178                                 exit(1);
179                         }
180                         break;
181                 case 'R':
182                         for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
183                                 junk += mmap_ptr[i];
184                         break;
185                 case 's':
186                         if (stat(fname, &st) == -1) {
187                                 perror("stat");
188                                 exit(1);
189                         }
190                         break;
191                 case 't':
192                         if (fchmod(fd, 0) == -1) {
193                                 perror("fchmod");
194                                 exit(1);
195                         }
196                         break;
197                 case 'T':
198                         if (ftruncate(fd, 0) == -1) {
199                                 perror("ftruncate");
200                                 exit(1);
201                         }
202                         break;
203                 case 'u':
204                         if (unlink(fname) == -1) {
205                                 perror("unlink");
206                                 exit(1);
207                         }
208                         break;
209                 case 'U':
210                         if (munmap(mmap_ptr, mmap_len)) {
211                                 perror("munmap");
212                                 exit(1);
213                         }
214                         break;
215                 case 'w': {
216                         int rc;
217                         if ((rc = write(fd, "w", 1)) == -1) {
218                                 perror("write");
219                                 exit(1);
220                         }
221                         break;
222                 }
223                 case 'W':
224                         for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
225                                 mmap_ptr[i] += junk++;
226                         break;
227                 case 'y':
228                         if (fsync(fd) == -1) {
229                                 perror("fsync");
230                                 exit(1);
231                         }
232                         break;
233                 case 'Y':
234                         if (fdatasync(fd) == -1) {
235                                 perror("fdatasync");
236                                 exit(1);
237                         }
238                 case 'z':
239                         if (lseek(fd, 0, SEEK_SET) == -1) {
240                                 perror("lseek");
241                                 exit(1);
242                         }
243                         break;
244                 default:
245                         fprintf(stderr, "unknown command \"%c\"\n", *commands);
246                         fprintf(stderr, usage, argv[0]);
247                         exit(1);
248                 }
249         }
250
251         return 0;
252 }