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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 #define _GNU_SOURCE /* pull in O_DIRECTORY in bits/fcntl.h */
42 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 #include <sys/xattr.h>
52 #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 " H[num] create HSM released file with num stripes\n"
82 " G gid get grouplock\n"
83 " g gid put grouplock\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 " r[num] read [optional length]\n"
95 " R reference entire mmap-ed region\n"
99 " T[num] ftruncate [optional position, default 0]\n"
103 " V open a volatile file\n"
104 " w[num] write optional length\n"
105 " x get file data version\n"
106 " W write entire mmap-ed region\n"
109 " z[num] lseek(SEEK_SET) [optional offset, default 0]\n"
110 " Z[num] lseek(SEEK_CUR) [optional offset, default 0]\n"
111 " _ wait for signal\n";
113 void usr1_handler(int unused)
115 int saved_errno = errno;
118 * signal(7): POSIX.1-2004 ...requires an implementation to guarantee
119 * that the following functions can be safely called inside a signal
129 pop_arg(int argc, char *argv[])
131 static int cur_arg = 3;
136 return argv[cur_arg++];
139 struct flag_mapping {
143 {"O_RDONLY", O_RDONLY},
144 {"O_WRONLY", O_WRONLY},
146 {"O_CREAT", O_CREAT},
148 {"O_NOCTTY", O_NOCTTY},
149 {"O_TRUNC", O_TRUNC},
150 {"O_APPEND", O_APPEND},
151 {"O_NONBLOCK", O_NONBLOCK},
152 {"O_NDELAY", O_NDELAY},
155 {"O_DIRECT", O_DIRECT},
158 {"O_NOATIME", O_NOATIME},
160 {"O_LARGEFILE", O_LARGEFILE},
161 {"O_DIRECTORY", O_DIRECTORY},
162 {"O_NOFOLLOW", O_NOFOLLOW},
163 {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
167 int get_flags(char *data, int *rflags)
175 cloned_flags = strdup(data);
176 if (cloned_flags == NULL) {
177 fprintf(stderr, "Insufficient memory.\n");
181 for (tmp = strtok(cloned_flags, ":"); tmp;
182 tmp = strtok(NULL, ":")) {
185 size = tmp - cloned_flags;
186 for (i = 0; flag_table[i].flag != -1; i++) {
187 if (!strcmp(tmp, flag_table[i].string)){
188 flags |= flag_table[i].flag;
189 size += strlen(flag_table[i].string);
206 #define POP_ARG() (pop_arg(argc, argv))
208 int main(int argc, char **argv)
210 char *fname, *commands;
215 size_t mmap_len = 0, i;
216 unsigned char *mmap_ptr = NULL, junk = 1;
224 struct lov_user_md_v3 lum;
225 char *xattr_buf = NULL;
226 size_t xattr_buf_size = 0;
231 fprintf(stderr, usage, argv[0]);
235 memset(&st, 0, sizeof(st));
236 sem_init(&sem, 0, 0);
237 /* use sigaction instead of signal to avoid SA_ONESHOT semantics */
238 sigaction(SIGUSR1, &(const struct sigaction){.sa_handler = &usr1_handler},
243 for (commands = argv[2]; *commands; commands++) {
244 /* XXX Most commands return 0 or we exit so we only
245 * update rc where really needed. */
255 len = atoi(commands+1);
257 len = 3600; /* 1 hour */
258 ts.tv_sec = time(NULL) + len;
260 while (sem_timedwait(&sem, &ts) < 0 && errno == EINTR);
263 if (fsetxattr(fd, XATTR, "multiop", 8, 0)) {
270 len = atoi(commands + 1);
271 if (xattr_buf_size < len) {
272 xattr_buf = realloc(xattr_buf, len);
273 if (xattr_buf == NULL) {
275 perror("allocating xattr buffer\n");
279 xattr_buf_size = len;
282 rc = fgetxattr(fd, XATTR, xattr_buf, len);
290 if (close(fd) == -1) {
298 lum = (struct lov_user_md_v3) {
299 .lmm_magic = LOV_USER_MAGIC_V3,
300 .lmm_stripe_count = atoi(commands + 1),
303 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum) < 0) {
305 perror("LL_IOC_LOV_SETSTRIPE");
310 len = atoi(commands+1);
311 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
315 perror("create stripe file");
321 if (mkdir(fname, 0755) == -1) {
323 perror("mkdir(0755)");
328 fd = open(fname, O_DIRECTORY);
331 perror("open(O_DIRECTORY)");
340 rc = llapi_lease_release(fd);
343 rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
346 rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
349 errx(-1, "unknown mode: %c", *commands);
352 err(errno, "apply/unlock lease error");
354 if (flags != LL_LEASE_UNLCK)
357 /* F_UNLCK, interpret return code */
359 const char *str = "unknown";
360 if (rc == LL_LEASE_RDLCK)
362 else if (rc == LL_LEASE_WRLCK)
364 fprintf(stdout, "%s lease(%lld) released.\n",
366 } else if (rc == 0) {
367 fprintf(stdout, "lease already broken.\n");
372 if (*commands != '-' && *commands != '+')
373 errx(-1, "unknown mode: %c\n", *commands);
375 rc = llapi_lease_check(fd);
377 const char *str = "unknown";
379 if (rc == LL_LEASE_RDLCK)
381 else if (rc == LL_LEASE_WRLCK)
383 fprintf(stdout, "%s lease(%lld) has applied.\n",
385 if (*commands == '-')
386 errx(-1, "expect lease to not exist");
387 } else if (rc == 0) {
388 fprintf(stdout, "no lease applied.\n");
389 if (*commands == '+')
390 errx(-1, "expect lease exists");
392 err(errno, "free lease error");
396 if (statfs(fname, &stfs) == -1)
397 errx(-1, "statfs()");
401 rc = llapi_path2fid(fname, &fid);
403 rc = llapi_fd2fid(fd, &fid);
406 "llapi_path/fd2fid() on %d, rc=%lld\n",
409 printf(DFID"\n", PFID(&fid));
413 gid = atoi(commands+1);
414 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
416 perror("ioctl(GROUP_LOCK)");
421 gid = atoi(commands+1);
422 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
424 perror("ioctl(GROUP_UNLOCK)");
429 len = atoi(commands+1);
430 fd = llapi_file_open(fname, O_CREAT | O_WRONLY,
432 LOV_PATTERN_RAID0 | LOV_PATTERN_F_RELEASED);
435 perror("create stripe file");
441 if (flock(fd, LOCK_EX) == -1)
449 if (link(oldpath, fname)) {
459 if (symlink(fname, newfile)) {
470 if (link(fname, newfile)) {
477 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
479 perror("mknod(S_IFREG|0644, 0)");
484 if (st.st_size == 0) {
485 fprintf(stderr, "mmap without preceeding stat, or on"
486 " zero length file.\n");
489 mmap_len = st.st_size;
490 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
492 if (mmap_ptr == MAP_FAILED) {
503 if (rename(oldpath, fname) < 0) {
513 if (rename (fname, newfile)) {
520 fd = open(fname, O_CREAT|O_RDWR, 0644);
523 perror("open(O_RDWR|O_CREAT)");
529 len = get_flags(commands+1, &flags);
532 fd = open(fname, flags, 0666);
534 fd = open(fname, flags);
543 printf("%lld\n", last_rc);
546 len = atoi(commands+1);
551 tmp = realloc(buf, len + ALIGN_LEN);
555 perror("allocating buf for read\n");
560 buf_align = (char *)((long)(buf + ALIGN_LEN) &
564 rc = read(fd, buf_align, len);
571 fprintf(stderr, "short read: %lld/%u\n",
578 printf("%.*s\n", (int)rc, buf_align);
582 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
586 if (stat(fname, &st) == -1) {
593 if (fstat(fd, &st) == -1) {
600 if (fchmod(fd, 0) == -1) {
607 len = atoi(commands+1);
608 if (ftruncate(fd, len) == -1) {
610 printf("ftruncate (%d,%d)\n", fd, len);
616 if (unlink(fname) == -1) {
623 if (munmap(mmap_ptr, mmap_len)) {
633 len = get_flags(commands + 1, &flags);
635 len = -1; /* mdt index */
636 if (commands[1] >= '0' && commands[1] <= '9')
637 len = atoi(commands+1);
638 fd = llapi_create_volatile_idx(fname, len, flags);
640 perror("llapi_create_volatile");
646 len = atoi(commands+1);
651 tmp = realloc(buf, len + ALIGN_LEN);
655 perror("allocating buf for write\n");
660 buf_align = (char *)((long)(buf + ALIGN_LEN) &
662 strncpy(buf_align, msg, bufsize);
665 rc = write(fd, buf_align, len);
672 fprintf(stderr, "short write: %lld/%u\n",
678 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
679 mmap_ptr[i] += junk++;
684 rc = llapi_get_data_version(fd, &dv, 0);
686 fprintf(stderr, "cannot get file data version"
690 printf("dataversion is %ju\n", (uintmax_t)dv);
694 __u32 layout_version;
696 rc = llapi_get_ost_layout_version(fd, &layout_version);
698 fprintf(stderr, "cannot get ost layout version"
702 printf("ostlayoutversion: %u\n", layout_version);
706 if (fsync(fd) == -1) {
713 if (fdatasync(fd) == -1) {
722 len = atoi(commands + 1);
723 off = lseek(fd, len, SEEK_SET);
724 if (off == (off_t)-1) {
736 len = atoi(commands + 1);
737 off = lseek(fd, len, SEEK_CUR);
738 if (off == (off_t)-1) {
760 fprintf(stderr, "unknown command \"%c\"\n", *commands);
761 fprintf(stderr, usage, argv[0]);