Whamcloud - gitweb
- landing of b_hd_cleanup_merge to HEAD.
[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                         fd = open(fname, O_DIRECTORY);
100                         if (fd == -1) {
101                                 perror("open(O_DIRECTORY)");
102                                 exit(1);
103                         }
104                         break;
105                 case 'l':
106                         newfile = POP_ARG();
107                         if (!newfile)
108                                 newfile = fname;
109                         if (symlink(fname, newfile)) {
110                                 perror("symlink()");
111                                 exit(1);
112                         }
113                         break;
114                 case 'L':
115                         newfile = POP_ARG();
116                         if (!newfile)
117                                 newfile = fname;
118                         if (link(fname, newfile)) {
119                                 perror("symlink()");
120                                 exit(1);
121                         }
122                         break;
123                 case 'm':
124                         if (mknod(fname, S_IFREG | 0644, 0) == -1) {
125                                 perror("mknod(S_IFREG|0644, 0)");
126                                 exit(1);
127                         }
128                         break;
129                 case 'M':
130                         mmap_len = st.st_size;
131                         mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
132                                         MAP_SHARED, fd, 0);
133                         if (mmap_ptr == MAP_FAILED) {
134                                 perror("mmap");
135                                 exit(1);
136                         }
137                         break;
138                 case 'N':
139                         newfile = POP_ARG();
140                         if (!newfile)
141                                 newfile = fname;
142                         if (rename (fname, newfile)) {
143                                 perror("rename()");
144                                 exit(1);
145                         }
146                         break;
147                 case 'O':
148                         fd = open(fname, O_CREAT|O_RDWR, 0644);
149                         if (fd == -1) {
150                                 perror("open(O_RDWR|O_CREAT)");
151                                 exit(1);
152                         }
153                         break;
154                 case 'o':
155                         fd = open(fname, O_RDONLY);
156                         if (fd == -1) {
157                                 perror("open(O_RDONLY)");
158                                 exit(1);
159                         }
160                         break;
161                 case 'r': {
162                         char buf;
163                         if (read(fd, &buf, 1) == -1) {
164                                 perror("read");
165                                 exit(1);
166                         }
167                         }
168                 case 'S':
169                         if (fstat(fd, &st) == -1) {
170                                 perror("fstat");
171                                 exit(1);
172                         }
173                         break;
174                 case 'R':
175                         for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
176                                 junk += mmap_ptr[i];
177                         break;
178                 case 's':
179                         if (stat(fname, &st) == -1) {
180                                 perror("stat");
181                                 exit(1);
182                         }
183                         break;
184                 case 't':
185                         if (fchmod(fd, 0) == -1) {
186                                 perror("fchmod");
187                                 exit(1);
188                         }
189                         break;
190                 case 'T':
191                         if (ftruncate(fd, 0) == -1) {
192                                 perror("ftruncate");
193                                 exit(1);
194                         }
195                         break;
196                 case 'u':
197                         if (unlink(fname) == -1) {
198                                 perror("unlink");
199                                 exit(1);
200                         }
201                         break;
202                 case 'U':
203                         if (munmap(mmap_ptr, mmap_len)) {
204                                 perror("munmap");
205                                 exit(1);
206                         }
207                         break;
208                 case 'w': {
209                         int rc;
210                         if ((rc = write(fd, "w", 1)) == -1) {
211                                 perror("write");
212                                 exit(1);
213                         }
214                         break;
215                 }
216                 case 'W':
217                         for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
218                                 mmap_ptr[i] += junk++;
219                         break;
220                 case 'y':
221                         if (fsync(fd) == -1) {
222                                 perror("fsync");
223                                 exit(1);
224                         }
225                         break;
226                 case 'Y':
227                         if (fdatasync(fd) == -1) {
228                                 perror("fdatasync");
229                                 exit(1);
230                         }
231                 case 'z':
232                         if (lseek(fd, 0, SEEK_SET) == -1) {
233                                 perror("lseek");
234                                 exit(1);
235                         }
236                         break;
237                 default:
238                         fprintf(stderr, "unknown command \"%c\"\n", *commands);
239                         fprintf(stderr, usage, argv[0]);
240                         exit(1);
241                 }
242         }
243
244         return 0;
245 }