Whamcloud - gitweb
Land b_smallfix onto HEAD (20040428_2142)
[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 void null_handler(int unused) { }
49
50 static const char *
51 pop_arg(int argc, char *argv[])
52 {
53         static int cur_arg = 3;
54
55         if (cur_arg >= argc)
56                 return NULL;
57
58         return argv[cur_arg++];
59 }
60 #define POP_ARG() (pop_arg(argc, argv))
61
62 int main(int argc, char **argv)
63 {
64         char *fname, *commands;
65         const char *newfile;
66         struct stat st;
67         size_t mmap_len = 0, i;
68         unsigned char *mmap_ptr = NULL, junk = 0;
69         int fd = -1;
70
71         if (argc < 3) {
72                 fprintf(stderr, usage, argv[0]);
73                 exit(1);
74         }
75
76         signal(SIGUSR1, null_handler);
77
78         fname = argv[1];
79
80         for (commands = argv[2]; *commands; commands++) {
81                 switch (*commands) {
82                 case '_':
83                         pause();
84                         break;
85                 case 'c':
86                         if (close(fd) == -1) {
87                                 perror("close");
88                                 exit(1);
89                         }
90                         fd = -1;
91                         break;
92                 case 'd':
93                         if (mkdir(fname, 0755) == -1) {
94                                 perror("mkdir(0755)");
95                                 exit(1);
96                         }
97                         break;
98                 case 'D':
99                         if (open(fname, O_DIRECTORY) == -1) {
100                                 perror("open(O_DIRECTORY)");
101                                 exit(1);
102                         }
103                         break;
104                 case 'l':
105                         newfile = POP_ARG();
106                         if (!newfile)
107                                 newfile = fname;
108                         if (symlink(fname, newfile)) {
109                                 perror("symlink()");
110                                 exit(1);
111                         }
112                         break;
113                 case 'L':
114                         newfile = POP_ARG();
115                         if (!newfile)
116                                 newfile = fname;
117                         if (link(fname, newfile)) {
118                                 perror("symlink()");
119                                 exit(1);
120                         }
121                         break;
122                 case 'm':
123                         if (mknod(fname, S_IFREG | 0644, 0) == -1) {
124                                 perror("mknod(S_IFREG|0644, 0)");
125                                 exit(1);
126                         }
127                         break;
128                 case 'M':
129                         mmap_len = st.st_size;
130                         mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
131                                         MAP_SHARED, fd, 0);
132                         if (mmap_ptr == MAP_FAILED) {
133                                 perror("mmap");
134                                 exit(1);
135                         }
136                         break;
137                 case 'N':
138                         newfile = POP_ARG();
139                         if (!newfile)
140                                 newfile = fname;
141                         if (rename (fname, newfile)) {
142                                 perror("rename()");
143                                 exit(1);
144                         }
145                         break;
146                 case 'O':
147                         fd = open(fname, O_CREAT|O_RDWR, 0644);
148                         if (fd == -1) {
149                                 perror("open(O_RDWR|O_CREAT)");
150                                 exit(1);
151                         }
152                         break;
153                 case 'o':
154                         fd = open(fname, O_RDONLY);
155                         if (fd == -1) {
156                                 perror("open(O_RDONLY)");
157                                 exit(1);
158                         }
159                         break;
160                 case 'r': {
161                         char buf;
162                         if (read(fd, &buf, 1) == -1) {
163                                 perror("read");
164                                 exit(1);
165                         }
166                         }
167                 case 'S':
168                         if (fstat(fd, &st) == -1) {
169                                 perror("fstat");
170                                 exit(1);
171                         }
172                         break;
173                 case 'R':
174                         for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
175                                 junk += mmap_ptr[i];
176                         break;
177                 case 's':
178                         if (stat(fname, &st) == -1) {
179                                 perror("stat");
180                                 exit(1);
181                         }
182                         break;
183                 case 't':
184                         if (fchmod(fd, 0) == -1) {
185                                 perror("fchmod");
186                                 exit(1);
187                         }
188                         break;
189                 case 'T':
190                         if (ftruncate(fd, 0) == -1) {
191                                 perror("ftruncate");
192                                 exit(1);
193                         }
194                         break;
195                 case 'u':
196                         if (unlink(fname) == -1) {
197                                 perror("unlink");
198                                 exit(1);
199                         }
200                         break;
201                 case 'U':
202                         if (munmap(mmap_ptr, mmap_len)) {
203                                 perror("munmap");
204                                 exit(1);
205                         }
206                         break;
207                 case 'w': {
208                         int rc;
209                         if ((rc = write(fd, "w", 1)) == -1) {
210                                 perror("write");
211                                 exit(1);
212                         }
213                         break;
214                 }
215                 case 'W':
216                         for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
217                                 mmap_ptr[i] += junk++;
218                         break;
219                 case 'y':
220                         if (fsync(fd) == -1) {
221                                 perror("fsync");
222                                 exit(1);
223                         }
224                         break;
225                 case 'Y':
226                         if (fdatasync(fd) == -1) {
227                                 perror("fdatasync");
228                                 exit(1);
229                         }
230                 case 'z':
231                         if (lseek(fd, 0, SEEK_SET) == -1) {
232                                 perror("lseek");
233                                 exit(1);
234                         }
235                         break;
236                 default:
237                         fprintf(stderr, "unknown command \"%c\"\n", *commands);
238                         fprintf(stderr, usage, argv[0]);
239                         exit(1);
240                 }
241         }
242
243         return 0;
244 }