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, 2015, 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 */
41 #include <sys/types.h>
45 #include <sys/ioctl.h>
46 #include <sys/xattr.h>
51 #include <semaphore.h>
55 #include <lustre/lustreapi.h>
57 #define T1 "write data before unlink\n"
58 #define T2 "write data after unlink\n"
59 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";
60 char *buf, *buf_align;
63 #define ALIGN_LEN 65535
64 #define XATTR "user.multiop"
67 "Usage: %s filename command-sequence [path...]\n"
68 " command-sequence items:\n"
69 " A fsetxattr(\"user.multiop\")\n"
70 " a fgetxattr(\"user.multiop\")\n"
72 " B[num] call setstripe ioctl to create stripes\n"
73 " C[num] create with optional stripes\n"
75 " D open(O_DIRECTORY)\n"
76 " e[R|W|U] apply lease. R: Read; W: Write; U: Unlock\n"
77 " E[+|-] get lease. +/-: expect lease to (not) exist\n"
80 " H[num] create HSM released file with num stripes\n"
81 " G gid get grouplock\n"
82 " g gid put grouplock\n"
83 " K link path to filename\n"
85 " l symlink filename to path\n"
87 " M rw mmap to EOF (must open and stat prior)\n"
88 " n rename path to filename\n"
89 " N rename filename to path\n"
91 " O open(O_CREAT|O_RDWR)\n"
92 " p print return value of last command\n"
93 " r[num] read [optional length]\n"
94 " R reference entire mmap-ed region\n"
98 " T[num] ftruncate [optional position, default 0]\n"
102 " V open a volatile file\n"
103 " w[num] write optional length\n"
104 " x get file data version\n"
105 " W write entire mmap-ed region\n"
108 " z[num] lseek(SEEK_SET) [optional offset, default 0]\n"
109 " Z[num] lseek(SEEK_CUR) [optional offset, default 0]\n"
110 " _ wait for signal\n";
112 void usr1_handler(int unused)
114 int saved_errno = errno;
117 * signal(7): POSIX.1-2004 ...requires an implementation to guarantee
118 * that the following functions can be safely called inside a signal
128 pop_arg(int argc, char *argv[])
130 static int cur_arg = 3;
135 return argv[cur_arg++];
138 struct flag_mapping {
142 {"O_RDONLY", O_RDONLY},
143 {"O_WRONLY", O_WRONLY},
145 {"O_CREAT", O_CREAT},
147 {"O_NOCTTY", O_NOCTTY},
148 {"O_TRUNC", O_TRUNC},
149 {"O_APPEND", O_APPEND},
150 {"O_NONBLOCK", O_NONBLOCK},
151 {"O_NDELAY", O_NDELAY},
154 {"O_DIRECT", O_DIRECT},
157 {"O_NOATIME", O_NOATIME},
159 {"O_LARGEFILE", O_LARGEFILE},
160 {"O_DIRECTORY", O_DIRECTORY},
161 {"O_NOFOLLOW", O_NOFOLLOW},
162 {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
166 int get_flags(char *data, int *rflags)
174 cloned_flags = strdup(data);
175 if (cloned_flags == NULL) {
176 fprintf(stderr, "Insufficient memory.\n");
180 for (tmp = strtok(cloned_flags, ":"); tmp;
181 tmp = strtok(NULL, ":")) {
184 size = tmp - cloned_flags;
185 for (i = 0; flag_table[i].flag != -1; i++) {
186 if (!strcmp(tmp, flag_table[i].string)){
187 flags |= flag_table[i].flag;
188 size += strlen(flag_table[i].string);
205 #define POP_ARG() (pop_arg(argc, argv))
207 int main(int argc, char **argv)
209 char *fname, *commands;
214 size_t mmap_len = 0, i;
215 unsigned char *mmap_ptr = NULL, junk = 0;
223 struct lov_user_md_v3 lum;
229 fprintf(stderr, usage, argv[0]);
233 memset(&st, 0, sizeof(st));
234 sem_init(&sem, 0, 0);
235 /* use sigaction instead of signal to avoid SA_ONESHOT semantics */
236 sigaction(SIGUSR1, &(const struct sigaction){.sa_handler = &usr1_handler},
241 for (commands = argv[2]; *commands; commands++) {
242 /* XXX Most commands return 0 or we exit so we only
243 * update rc where really needed. */
253 len = atoi(commands+1);
255 len = 3600; /* 1 hour */
256 ts.tv_sec = time(NULL) + len;
258 while (sem_timedwait(&sem, &ts) < 0 && errno == EINTR);
261 if (fsetxattr(fd, XATTR, "multiop", 8, 0)) {
268 rc = fgetxattr(fd, XATTR, NULL, 0);
276 if (close(fd) == -1) {
284 lum = (struct lov_user_md_v3) {
285 .lmm_magic = LOV_USER_MAGIC_V3,
286 .lmm_stripe_count = atoi(commands + 1),
289 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum) < 0) {
291 perror("LL_IOC_LOV_SETSTRIPE");
296 len = atoi(commands+1);
297 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
301 perror("create stripe file");
307 if (mkdir(fname, 0755) == -1) {
309 perror("mkdir(0755)");
314 fd = open(fname, O_DIRECTORY);
317 perror("open(O_DIRECTORY)");
326 flags = LL_LEASE_UNLCK;
329 flags = LL_LEASE_RDLCK;
332 flags = LL_LEASE_WRLCK;
335 errx(-1, "unknown mode: %c", *commands);
338 rc = ioctl(fd, LL_IOC_SET_LEASE, flags);
340 err(errno, "apply lease error");
342 if (flags != LL_LEASE_UNLCK)
345 /* F_UNLCK, interpret return code */
347 const char *str = "unknown";
348 if (rc == LL_LEASE_RDLCK)
350 else if (rc == LL_LEASE_WRLCK)
352 fprintf(stdout, "%s lease(%lld) released.\n",
354 } else if (rc == 0) {
355 fprintf(stdout, "lease already broken.\n");
360 if (*commands != '-' && *commands != '+')
361 errx(-1, "unknown mode: %c\n", *commands);
363 rc = ioctl(fd, LL_IOC_GET_LEASE);
365 const char *str = "unknown";
367 if (rc == LL_LEASE_RDLCK)
369 else if (rc == LL_LEASE_WRLCK)
371 fprintf(stdout, "%s lease(%lld) has applied.\n",
373 if (*commands == '-')
374 errx(-1, "expect lease to not exist");
375 } else if (rc == 0) {
376 fprintf(stdout, "no lease applied.\n");
377 if (*commands == '+')
378 errx(-1, "expect lease exists");
380 err(errno, "free lease error");
384 if (statfs(fname, &stfs) == -1)
385 errx(-1, "statfs()");
389 rc = llapi_path2fid(fname, &fid);
391 rc = llapi_fd2fid(fd, &fid);
394 "llapi_path/fd2fid() on %d, rc=%lld\n",
397 printf(DFID"\n", PFID(&fid));
400 gid = atoi(commands+1);
401 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
403 perror("ioctl(GROUP_LOCK)");
408 gid = atoi(commands+1);
409 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
411 perror("ioctl(GROUP_UNLOCK)");
416 len = atoi(commands+1);
417 fd = llapi_file_open(fname, O_CREAT | O_WRONLY,
419 LOV_PATTERN_RAID0 | LOV_PATTERN_F_RELEASED);
422 perror("create stripe file");
428 if (flock(fd, LOCK_EX) == -1)
436 if (link(oldpath, fname)) {
446 if (symlink(fname, newfile)) {
457 if (link(fname, newfile)) {
464 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
466 perror("mknod(S_IFREG|0644, 0)");
471 if (st.st_size == 0) {
472 fprintf(stderr, "mmap without preceeding stat, or on"
473 " zero length file.\n");
476 mmap_len = st.st_size;
477 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
479 if (mmap_ptr == MAP_FAILED) {
490 if (rename(oldpath, fname) < 0) {
500 if (rename (fname, newfile)) {
507 fd = open(fname, O_CREAT|O_RDWR, 0644);
510 perror("open(O_RDWR|O_CREAT)");
516 len = get_flags(commands+1, &flags);
519 fd = open(fname, flags, 0666);
521 fd = open(fname, flags);
530 printf("%lld\n", last_rc);
533 len = atoi(commands+1);
538 tmp = realloc(buf, len + ALIGN_LEN);
542 perror("allocating buf for read\n");
547 buf_align = (char *)((long)(buf + ALIGN_LEN) &
551 rc = read(fd, buf_align, len);
558 fprintf(stderr, "short read: %lld/%u\n",
565 printf("%.*s\n", (int)rc, buf_align);
569 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
573 if (stat(fname, &st) == -1) {
580 if (fstat(fd, &st) == -1) {
587 if (fchmod(fd, 0) == -1) {
594 len = atoi(commands+1);
595 if (ftruncate(fd, len) == -1) {
597 printf("ftruncate (%d,%d)\n", fd, len);
603 if (unlink(fname) == -1) {
610 if (munmap(mmap_ptr, mmap_len)) {
620 len = get_flags(commands + 1, &flags);
622 fd = llapi_create_volatile(fname, flags);
624 perror("llapi_create_volatile");
630 len = atoi(commands+1);
635 tmp = realloc(buf, len + ALIGN_LEN);
639 perror("allocating buf for write\n");
644 buf_align = (char *)((long)(buf + ALIGN_LEN) &
646 strncpy(buf_align, msg, bufsize);
649 rc = write(fd, buf_align, len);
656 fprintf(stderr, "short write: %lld/%u\n",
662 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
663 mmap_ptr[i] += junk++;
666 rc = llapi_get_data_version(fd, &dv, 0);
668 fprintf(stderr, "cannot get file data version"
672 printf("dataversion is %ju\n", (uintmax_t)dv);
675 if (fsync(fd) == -1) {
682 if (fdatasync(fd) == -1) {
691 len = atoi(commands + 1);
692 off = lseek(fd, len, SEEK_SET);
693 if (off == (off_t)-1) {
705 len = atoi(commands + 1);
706 off = lseek(fd, len, SEEK_CUR);
707 if (off == (off_t)-1) {
729 fprintf(stderr, "unknown command \"%c\"\n", *commands);
730 fprintf(stderr, usage, argv[0]);