4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 #define _GNU_SOURCE /* pull in O_DIRECTORY in bits/fcntl.h */
41 #include <sys/types.h>
45 #include <sys/ioctl.h>
46 #include <sys/xattr.h>
51 #include <semaphore.h>
56 #include <lustre/lustreapi.h>
58 #define T1 "write data before unlink\n"
59 #define T2 "write data after unlink\n"
60 char msg[] = "yabba dabba doo, I'm coming for you, I live in a shoe, I don't know what to do.\n'Bigger, bigger,and bigger yet!' cried the Creator. 'You are not yet substantial enough for my boundless intents!' And ever greater and greater the object became, until all was lost 'neath its momentus bulk.\n";
61 char *buf, *buf_align;
64 #define ALIGN_LEN 65535
65 #define XATTR "user.multiop"
68 "Usage: %s filename command-sequence [path...]\n"
69 " command-sequence items:\n"
70 " A fsetxattr(\"user.multiop\")\n"
71 " a[num] fgetxattr(\"user.multiop\") [optional buffer size, default 0]\n"
73 " B[num] call setstripe ioctl to create stripes\n"
74 " C[num] create with optional stripes\n"
76 " D open(O_DIRECTORY)\n"
77 " e[R|W|U] apply lease. R: Read; W: Write; U: Unlock\n"
78 " E[+|-] get lease. +/-: expect lease to (not) exist\n"
81 " G gid get grouplock\n"
82 " g gid put grouplock\n"
83 " H[num] create HSM released file with num stripes\n"
84 " K link path to filename\n"
86 " l symlink filename to path\n"
88 " M rw mmap to EOF (must open and stat prior)\n"
89 " n rename path to filename\n"
90 " N rename filename to path\n"
92 " O open(O_CREAT|O_RDWR)\n"
93 " p print return value of last command\n"
94 " Q open filename (should be dir), stat first entry to init statahead"
95 " r[num] read [optional length]\n"
96 " R reference entire mmap-ed region\n"
100 " T[num] ftruncate [optional position, default 0]\n"
104 " V open a volatile file\n"
105 " w[num] write optional length\n"
106 " x get file data version\n"
107 " W write entire mmap-ed region\n"
110 " z[num] lseek(SEEK_SET) [optional offset, default 0]\n"
111 " Z[num] lseek(SEEK_CUR) [optional offset, default 0]\n"
112 " _ wait for signal\n";
114 void usr1_handler(int unused)
116 int saved_errno = errno;
119 * signal(7): POSIX.1-2004 ...requires an implementation to guarantee
120 * that the following functions can be safely called inside a signal
130 pop_arg(int argc, char *argv[])
132 static int cur_arg = 3;
137 return argv[cur_arg++];
140 struct flag_mapping {
144 {"O_RDONLY", O_RDONLY},
145 {"O_WRONLY", O_WRONLY},
147 {"O_CREAT", O_CREAT},
149 {"O_NOCTTY", O_NOCTTY},
150 {"O_TRUNC", O_TRUNC},
151 {"O_APPEND", O_APPEND},
152 {"O_NONBLOCK", O_NONBLOCK},
153 {"O_NDELAY", O_NDELAY},
156 {"O_DIRECT", O_DIRECT},
159 {"O_NOATIME", O_NOATIME},
161 {"O_LARGEFILE", O_LARGEFILE},
162 {"O_DIRECTORY", O_DIRECTORY},
163 {"O_NOFOLLOW", O_NOFOLLOW},
164 {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
168 int get_flags(char *data, int *rflags)
176 cloned_flags = strdup(data);
178 fprintf(stderr, "Insufficient memory.\n");
182 for (tmp = strtok(cloned_flags, ":"); tmp;
183 tmp = strtok(NULL, ":")) {
186 size = tmp - cloned_flags;
187 for (i = 0; flag_table[i].flag != -1; i++) {
188 if (!strcmp(tmp, flag_table[i].string)) {
189 flags |= flag_table[i].flag;
190 size += strlen(flag_table[i].string);
207 static int statahead(char *dname)
224 if (asprintf(&buf, "%s/%s", dname, dent->d_name) == -1) {
236 #define POP_ARG() (pop_arg(argc, argv))
238 int main(int argc, char **argv)
240 char *fname, *commands;
245 size_t mmap_len = 0, i;
246 unsigned char *mmap_ptr = NULL, junk = 1;
254 struct lov_user_md_v3 lum;
255 char *xattr_buf = NULL;
256 size_t xattr_buf_size = 0;
261 fprintf(stderr, usage, argv[0]);
265 memset(&st, 0, sizeof(st));
266 sem_init(&sem, 0, 0);
267 /* use sigaction instead of signal to avoid SA_ONESHOT semantics */
269 &(const struct sigaction){.sa_handler = &usr1_handler}, NULL);
273 for (commands = argv[2]; *commands; commands++) {
275 * XXX Most commands return 0 or we exit so we only
276 * update rc where really needed.
287 len = atoi(commands + 1);
289 len = 3600; /* 1 hour */
290 ts.tv_sec = time(NULL) + len;
292 while (sem_timedwait(&sem, &ts) < 0 && errno == EINTR)
296 if (fsetxattr(fd, XATTR, "multiop", 8, 0)) {
303 len = atoi(commands + 1);
304 if (xattr_buf_size < len) {
305 xattr_buf = realloc(xattr_buf, len);
308 perror("allocating xattr buffer\n");
312 xattr_buf_size = len;
315 rc = fgetxattr(fd, XATTR, xattr_buf, len);
323 if (close(fd) == -1) {
331 lum = (struct lov_user_md_v3) {
332 .lmm_magic = LOV_USER_MAGIC_V3,
333 .lmm_stripe_count = atoi(commands + 1),
336 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum) < 0) {
338 perror("LL_IOC_LOV_SETSTRIPE");
343 len = atoi(commands + 1);
344 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
348 perror("create stripe file");
354 if (mkdir(fname, 0755) == -1) {
356 perror("mkdir(0755)");
361 fd = open(fname, O_DIRECTORY);
364 perror("open(O_DIRECTORY)");
373 rc = llapi_lease_release(fd);
376 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
379 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
382 errx(-1, "unknown mode: %c", *commands);
385 err(errno, "apply/unlock lease error");
387 if (flags != LL_LEASE_UNLCK)
390 /* F_UNLCK, interpret return code */
392 const char *str = "unknown";
394 if (rc == LL_LEASE_RDLCK)
396 else if (rc == LL_LEASE_WRLCK)
398 fprintf(stdout, "%s lease(%lld) released.\n",
400 } else if (rc == 0) {
401 fprintf(stdout, "lease already broken.\n");
406 if (*commands != '-' && *commands != '+')
407 errx(-1, "unknown mode: %c\n", *commands);
409 rc = llapi_lease_check(fd);
411 const char *str = "unknown";
413 if (rc == LL_LEASE_RDLCK)
415 else if (rc == LL_LEASE_WRLCK)
417 fprintf(stdout, "%s lease(%lld) has applied.\n",
419 if (*commands == '-')
420 errx(-1, "expect lease to not exist");
421 } else if (rc == 0) {
422 fprintf(stdout, "no lease applied.\n");
423 if (*commands == '+')
424 errx(-1, "expect lease exists");
426 err(errno, "free lease error");
430 if (statfs(fname, &stfs) == -1)
431 errx(-1, "statfs()");
435 rc = llapi_path2fid(fname, &fid);
437 rc = llapi_fd2fid(fd, &fid);
440 "llapi_path/fd2fid() on %d, rc=%lld\n",
443 printf(DFID"\n", PFID(&fid));
447 gid = atoi(commands + 1);
448 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
450 perror("ioctl(GROUP_LOCK)");
455 gid = atoi(commands + 1);
456 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
458 perror("ioctl(GROUP_UNLOCK)");
463 len = atoi(commands + 1);
464 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
465 0, 0, len, LOV_PATTERN_RAID0 |
466 LOV_PATTERN_F_RELEASED);
469 perror("create stripe file");
475 if (flock(fd, LOCK_EX) == -1)
483 if (link(oldpath, fname)) {
493 if (symlink(fname, newfile)) {
504 if (link(fname, newfile)) {
511 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
513 perror("mknod(S_IFREG|0644, 0)");
518 if (st.st_size == 0) {
520 "mmap without preceeding stat, or on zero length file.\n");
523 mmap_len = st.st_size;
524 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
526 if (mmap_ptr == MAP_FAILED) {
537 if (rename(oldpath, fname) < 0) {
547 if (rename(fname, newfile)) {
554 fd = open(fname, O_CREAT | O_RDWR, 0644);
557 perror("open(O_RDWR|O_CREAT)");
563 len = get_flags(commands + 1, &flags);
566 fd = open(fname, flags, 0666);
568 fd = open(fname, flags);
577 printf("%lld\n", last_rc);
580 save_errno = statahead(fname);
587 len = atoi(commands + 1);
593 tmp = realloc(buf, len + ALIGN_LEN);
597 perror("allocating buf for read\n");
602 buf_align = (char *)((long)(buf + ALIGN_LEN) &
606 rc = read(fd, buf_align, len);
613 fprintf(stderr, "short read: %lld/%u\n",
620 printf("%.*s\n", (int)rc, buf_align);
624 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
628 if (stat(fname, &st) == -1) {
635 if (fstat(fd, &st) == -1) {
642 if (fchmod(fd, 0) == -1) {
649 len = atoi(commands + 1);
650 if (ftruncate(fd, len) == -1) {
652 printf("ftruncate (%d,%d)\n", fd, len);
658 if (unlink(fname) == -1) {
665 if (munmap(mmap_ptr, mmap_len)) {
675 len = get_flags(commands + 1, &flags);
677 len = -1; /* mdt index */
678 if (commands[1] >= '0' && commands[1] <= '9')
679 len = atoi(commands + 1);
680 fd = llapi_create_volatile_idx(fname, len, flags);
682 perror("llapi_create_volatile");
688 len = atoi(commands + 1);
694 tmp = realloc(buf, len + ALIGN_LEN);
698 perror("allocating buf for write\n");
703 buf_align = (char *)((long)(buf + ALIGN_LEN) &
705 strncpy(buf_align, msg, bufsize);
708 rc = write(fd, buf_align, len);
716 "short write: %lld/%u\n",
722 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
723 mmap_ptr[i] += junk++;
728 rc = llapi_get_data_version(fd, &dv, 0);
731 "cannot get file data version %lld\n",
735 printf("dataversion is %ju\n", (uintmax_t)dv);
739 __u32 layout_version;
741 rc = llapi_get_ost_layout_version(fd, &layout_version);
744 "cannot get ost layout version %lld\n",
748 printf("ostlayoutversion: %u\n", layout_version);
752 if (fsync(fd) == -1) {
759 if (fdatasync(fd) == -1) {
768 len = atoi(commands + 1);
769 off = lseek(fd, len, SEEK_SET);
770 if (off == (off_t)-1) {
782 len = atoi(commands + 1);
783 off = lseek(fd, len, SEEK_CUR);
784 if (off == (off_t)-1) {
806 fprintf(stderr, "unknown command \"%c\"\n", *commands);
807 fprintf(stderr, usage, argv[0]);