Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / lustre / tests / createmany.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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <getopt.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/ioctl.h>
43 #include <sys/xattr.h>
44 #include <time.h>
45 #include <unistd.h>
46 #include <lustre/lustreapi.h>
47
48 #include <linux/lustre/lustre_user.h>
49 #include <lustre/lustreapi.h>
50
51 static void usage(const char *prog)
52 {
53         printf(
54                "usage: %s {-o [-k] [-x <size>]|-m|-d|-l<tgt>} [-u[<unlinkfmt>]] [-i mdt_index] [-t seconds] filenamefmt [[start] count]\n",
55                prog);
56         printf("\t-i\tMDT to create the directories on\n"
57                "\t-l\tlink files to existing <tgt> file\n"
58                "\t-m\tmknod regular files (don't create OST objects)\n"
59                "\t-o\topen+create files with path and printf format\n"
60                "\t-k\t    keep files open until all files are opened\n"
61                "\t-x\t    set an xattr with <size> length on the files\n"
62                "\t-u\tunlink file/dir (with optional <unlinkfmt>)\n");
63         printf("\t-d\tuse directories instead of regular files\n"
64                "\t-t\tstop creating files after <seconds> have elapsed\n");
65         printf("\t-S\tthe file size\n"
66                "\t-U\tthe start User ID of the file\n"
67                "\t-G\tthe start Group ID of the file\n"
68                "\t-P\tthe start Project ID of the file\n");
69
70         exit(EXIT_FAILURE);
71 }
72
73 static char *get_file_name(const char *fmt, long n, int has_fmt_spec)
74 {
75         static char filename[4096];
76         int bytes;
77
78         bytes = has_fmt_spec ? snprintf(filename, 4095, fmt, n) :
79                 snprintf(filename, 4095, "%s%ld", fmt, n);
80         if (bytes >= 4095) {
81                 printf("file name too long\n");
82                 exit(EXIT_FAILURE);
83         }
84         return filename;
85 }
86
87 static double now(void)
88 {
89         struct timeval tv;
90
91         gettimeofday(&tv, NULL);
92         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
93 }
94
95 int main(int argc, char **argv)
96 {
97         bool do_open = false, do_keep = false, do_link = false;
98         bool do_unlink = false, do_mknod = false, do_mkdir = false;
99         bool do_setsize = false, do_chuid = false, do_chgid = false;
100         bool do_chprj = false;
101         bool do_rmdir = false;
102         bool do_xattr = false;
103         int stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
104         int stripe_offset = -1, stripe_count = 1;
105         size_t xattr_size = 0;
106         char *xattr_buf = NULL;
107         char *filename, *progname;
108         char *fmt = NULL, *fmt_unlink = NULL, *tgt = NULL;
109         char *endp = NULL;
110         double start, last_t, end;
111         long begin = 0, count = ~0UL >> 1;
112         int has_fmt_spec = 0, unlink_has_fmt_spec = 0;
113         long i, total, last_i = 0;
114         int c, last_fd = -1, stderr_fd;
115         unsigned int uid = 0, gid = 0, pid = 0;
116         int size = 0;
117         int rc = 0;
118
119         /* Handle the deprecated positional last argument "-seconds" */
120         if (argc > 1 && argv[argc - 1][0] == '-' &&
121             (end = strtol(argv[argc - 1] + 1, &endp, 0)) && *endp == '\0') {
122                 fprintf(stderr,
123                         "warning: '-runtime' deprecated, use '-t runtime' instead\n");
124                 argv[--argc] = NULL;
125         } else {
126                 /* Not '-number', let regular argument parsing handle it. */
127                 end = ~0U >> 1;
128         }
129
130         if ((endp = strrchr(argv[0], '/')) != NULL)
131                 progname = endp + 1;
132         else
133                 progname = argv[0];
134
135         while ((c = getopt(argc, argv, "i:dG:l:kmor::S:t:u::U:x:")) != -1) {
136                 switch (c) {
137                 case 'd':
138                         do_mkdir = true;
139                         break;
140                 case 'G':
141                         do_chgid = true;
142                         gid = strtoul(optarg, NULL, 0);
143                         break;
144                 case 'i':
145                         stripe_offset = strtoul(optarg, &endp, 0);
146                         if (*endp != '\0') {
147                                 fprintf(stderr, "invalid MDT index '%s'\n",
148                                         optarg);
149                                 return 1;
150                         }
151                         break;
152                 case 'k':
153                         do_keep = true;
154                         break;
155                 case 'l':
156                         do_link = true;
157                         tgt = optarg;
158                         break;
159                 case 'm':
160                         do_mknod = true;
161                         break;
162                 case 'o':
163                         do_open = true;
164                         break;
165                 case 'P':
166                         do_chprj = true;
167                         pid = strtoul(optarg, NULL, 0);
168                         break;
169                 case 'S':
170                         do_setsize = true;
171                         size = atoi(optarg);
172                         break;
173                 case 't':
174                         end = strtol(optarg, &endp, 0);
175                         if (end <= 0.0 || *endp != '\0')
176                                 usage(progname);
177                         break;
178                 case 'r':
179                 case 'u':
180                         do_unlink = true;
181                         fmt_unlink = optarg;
182                         break;
183                 case 'U':
184                         do_chuid = true;
185                         uid = strtoul(optarg, NULL, 0);
186                         break;
187                 case 'x':
188                         do_xattr = true;
189                         xattr_size = strtoul(optarg, &endp, 0);
190                         if (*endp != '\0') {
191                                 fprintf(stderr, "invalid xattr size '%s'\n",
192                                         optarg);
193                                 return 1;
194                         }
195                         break;
196                 case '?':
197                         fprintf(stderr, "Unknown option '%c'\n", optopt);
198                         usage(progname);
199                 }
200         }
201
202         if (!do_open && (do_setsize || do_chuid || do_chgid || do_chprj)) {
203                 fprintf(stderr, "error: -S, -U, -G, -P works only with -o\n");
204                 usage(progname);
205         }
206
207         if (do_open + do_mkdir + do_link + do_mknod > 1 ||
208             do_open + do_mkdir + do_link + do_mknod + do_unlink == 0) {
209                 fprintf(stderr, "error: only one of -o, -m, -l, -d\n");
210                 usage(progname);
211         }
212         if (do_mkdir && do_unlink)
213                 do_rmdir = true;
214
215         if (!do_open && (do_keep || do_xattr)) {
216                 fprintf(stderr, "error: can only use -k|-x with -o\n");
217                 usage(progname);
218         }
219
220         switch (argc - optind) {
221         case 3:
222                 begin = strtol(argv[argc - 2], NULL, 0);
223         case 2:
224                 count = strtol(argv[argc - 1], NULL, 0);
225         case 1:
226                 fmt = argv[optind];
227                 break;
228         default:
229                 usage(progname);
230         }
231
232         has_fmt_spec = strchr(fmt, '%') != NULL;
233         if (fmt_unlink != NULL)
234                 unlink_has_fmt_spec = strchr(fmt_unlink, '%') != NULL;
235
236         if (do_xattr) {
237                 xattr_buf = malloc(xattr_size);
238                 if (!xattr_buf) {
239                         printf("malloc xattr buf error: %s\n", strerror(errno));
240                         return errno;
241                 }
242         }
243
244         for (i = 0, start = last_t = now(), end += start;
245              i < count && now() < end; i++, begin++) {
246                 double tmp;
247
248                 filename = get_file_name(fmt, begin, has_fmt_spec);
249                 if (do_open) {
250                         int fd;
251
252                         fd = open(filename, O_CREAT|O_RDWR, 0644);
253                         if (fd < 0) {
254                                 printf("open(%s) error: %s\n", filename,
255                                        strerror(errno));
256                                 rc = errno;
257                                 break;
258                         }
259                         if (do_setsize) {
260                                 rc = lseek(fd, (size - 6) < 0 ? 0 : size - 6,
261                                            SEEK_SET);
262                                 if (rc < 0) {
263                                         printf("lseek(%s, %d) error: %s\n",
264                                                filename, size, strerror(errno));
265                                         break;
266                                 }
267                                 rc = write(fd, "Lustre", 6);
268                                 if (rc < 0) {
269                                         printf("write(%s, %d) error: %s\n",
270                                                filename, 6, strerror(errno));
271                                         break;
272                                 }
273                         }
274
275                         if (do_chuid || do_chgid) {
276                                 rc = fchown(fd, do_chuid ? uid + i : -1,
277                                             do_chgid ? gid + i : -1);
278                                 if (rc < 0) {
279                                         printf("fchown(%s, %u, %u) error: %s\n",
280                                                filename, do_chuid ? uid : -1,
281                                                do_chgid ? gid : -1,
282                                                strerror(errno));
283                                         break;
284                                 }
285                         }
286
287                         if (do_chprj) {
288                                 struct fsxattr fsx;
289
290                                 rc = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
291                                 if (rc < 0) {
292                                         printf("ioctl(%s) error: %s\n",
293                                                "FS_IOC_GETXATTR",
294                                                strerror(errno));
295                                         break;
296                                 }
297
298                                 fsx.fsx_projid = pid + i;
299                                 rc = ioctl(fd, FS_IOC_FSSETXATTR, &fsx);
300                                 if (rc < 0) {
301                                         printf("ioctl(%s, %d) error: %s\n",
302                                                "FS_IOC_SETXATTR", pid,
303                                                strerror(errno));
304                                         break;
305                                 }
306                         }
307
308                         if (do_xattr) {
309                                 strncpy(xattr_buf, filename, xattr_size);
310                                 rc = fsetxattr(fd, "user.createmany", xattr_buf,
311                                                xattr_size, 0);
312                                 if (rc < 0) {
313                                         printf("fsetxattr(%s) error: %s\n",
314                                                filename, strerror(errno));
315                                         rc = errno;
316                                         break;
317                                 }
318                         }
319
320                         if (!do_keep)
321                                 close(fd);
322                         else if (fd > last_fd)
323                                 last_fd = fd;
324                 } else if (do_link) {
325                         rc = link(tgt, filename);
326                         if (rc) {
327                                 printf("link(%s, %s) error: %s\n",
328                                        tgt, filename, strerror(errno));
329                                 rc = errno;
330                                 break;
331                         }
332                 } else if (do_mkdir) {
333                         if (stripe_offset != -1) {
334                                 rc = llapi_dir_create_pool(filename, 0755,
335                                                            stripe_offset,
336                                                            stripe_count,
337                                                            stripe_pattern,
338                                                            NULL);
339                                 if (rc) {
340                                         printf("llapi_dir_create_pool(%s) error: %s\n",
341                                                filename, strerror(-rc));
342                                         rc = errno;
343                                         break;
344                                 }
345                         } else {
346                                 rc = mkdir(filename, 0755);
347                                 if (rc) {
348                                         printf("mkdir(%s) error: %s\n",
349                                                filename, strerror(errno));
350                                         rc = errno;
351                                         break;
352                                 }
353                         }
354                 } else if (do_mknod) {
355                         rc = mknod(filename, S_IFREG | 0444, 0);
356                         if (rc) {
357                                 printf("mknod(%s) error: %s\n",
358                                        filename, strerror(errno));
359                                 rc = errno;
360                                 break;
361                         }
362                 }
363                 if (do_unlink) {
364                         if (fmt_unlink != NULL)
365                                 filename = get_file_name(fmt_unlink, begin,
366                                                          unlink_has_fmt_spec);
367
368                         rc = do_rmdir ? rmdir(filename) : unlink(filename);
369                         /* use rmdir if this is a directory */
370                         if (!do_rmdir && rc && errno == EISDIR) {
371                                 do_rmdir = true;
372                                 rc = rmdir(filename);
373                         }
374                         if (rc) {
375                                 printf("unlink(%s) error: %s\n",
376                                        filename, strerror(errno));
377                                 rc = errno;
378                                 break;
379                         }
380                 }
381
382                 tmp = now();
383                 if (tmp - last_t >= 10.0 ||
384                     (tmp - last_t > 2.0 && (i % 10000) == 0)) {
385                         printf(" - %s%s %ld (time %.2f total %.2f last %.2f)\n",
386                                do_open ? do_keep ? "open/keep" : "open/close" :
387                                         do_mkdir ? "mkdir" : do_link ? "link" :
388                                         do_mknod ? "create" : "",
389                                do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
390                                i, tmp, tmp - start,
391                                (i - last_i) / (tmp - last_t));
392                         last_t = tmp;
393                         last_i = i;
394                 }
395         }
396         last_t = now();
397         total = i;
398         printf("total: %ld %s%s in %.2f seconds: %.2f ops/second\n", total,
399                do_open ? do_keep ? "open/keep" : "open/close" :
400                         do_mkdir ? "mkdir" : do_link ? "link" :
401                                              do_mknod ? "create" : "",
402                do_unlink ? do_mkdir ? "/rmdir" : "/unlink" : "",
403                last_t - start, ((double)total / (last_t - start)));
404
405         if (xattr_buf)
406                 free(xattr_buf);
407
408         if (!do_keep)
409                 return rc;
410
411         stderr_fd = fileno(stderr);
412         start = last_t;
413         /* Assume fd is allocated in order, doing extra closes is not harmful */
414         for (i = 0; i < total && last_fd > stderr_fd; i++, --last_fd) {
415                 close(last_fd);
416
417                 if ((i != 0 && (i % 10000) == 0) || now() - last_t >= 10.0) {
418                         double tmp = now();
419
420                         printf(" - closed %ld (time %.2f total %.2f last %.2f)\n",
421                                i, tmp, tmp - start,
422                                (i - last_i) / (tmp - last_t));
423                         last_t = tmp;
424                         last_i = i;
425                 }
426         }
427         last_t = now();
428
429         printf("total: %ld close in %.2f seconds: %.2f close/second\n",
430                total, last_t - start, ((double)total / (last_t - start)));
431         return rc;
432 }