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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 #define _GNU_SOURCE /* pull in O_DIRECTORY in bits/fcntl.h */
45 #include <sys/types.h>
49 #include <sys/ioctl.h>
50 #include <sys/xattr.h>
54 #include <semaphore.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre/lustreapi.h>
61 #define T1 "write data before unlink\n"
62 #define T2 "write data after unlink\n"
63 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";
64 char *buf, *buf_align;
67 #define ALIGN_LEN 65535
68 #define XATTR "user.multiop"
71 "Usage: %s filename command-sequence [path...]\n"
72 " command-sequence items:\n"
73 " A fsetxattr(\"user.multiop\")\n"
74 " a fgetxattr(\"user.multiop\")\n"
76 " B[num] call setstripe ioctl to create stripes\n"
77 " C[num] create with optional stripes\n"
79 " D open(O_DIRECTORY)\n"
80 " e[R|W|U] apply lease. R: Read; W: Write; U: Unlock\n"
81 " E[+|-] get lease. +/-: expect lease to (not) exist\n"
84 " H[num] create HSM released file with num stripes\n"
85 " G gid get grouplock\n"
86 " g gid put grouplock\n"
87 " K link path to filename\n"
89 " l symlink filename to path\n"
91 " M rw mmap to EOF (must open and stat prior)\n"
92 " n rename path to filename\n"
93 " N rename filename to path\n"
95 " O open(O_CREAT|O_RDWR)\n"
96 " r[num] read [optional length]\n"
97 " R reference entire mmap-ed region\n"
101 " T[num] ftruncate [optional position, default 0]\n"
105 " V open a volatile file\n"
106 " w[num] write optional length\n"
107 " x get file data version\n"
108 " W write entire mmap-ed region\n"
111 " z[num] seek [optional position, 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);
177 if (cloned_flags == NULL) {
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 #define POP_ARG() (pop_arg(argc, argv))
209 int main(int argc, char **argv)
211 char *fname, *commands;
216 size_t mmap_len = 0, i;
217 unsigned char *mmap_ptr = NULL, junk = 0;
218 int rc, len, fd = -1;
225 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++) {
248 len = atoi(commands+1);
250 len = 3600; /* 1 hour */
251 ts.tv_sec = time(NULL) + len;
253 while (sem_timedwait(&sem, &ts) < 0 && errno == EINTR);
256 if (fsetxattr(fd, XATTR, "multiop", 8, 0)) {
263 if (fgetxattr(fd, XATTR, NULL, 0) == -1) {
270 if (close(fd) == -1) {
278 lum = (struct lov_user_md_v3) {
279 .lmm_magic = LOV_USER_MAGIC_V3,
280 .lmm_stripe_count = atoi(commands + 1),
283 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum) < 0) {
285 perror("LL_IOC_LOV_SETSTRIPE");
290 len = atoi(commands+1);
291 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
295 perror("create stripe file");
300 if (mkdir(fname, 0755) == -1) {
302 perror("mkdir(0755)");
307 fd = open(fname, O_DIRECTORY);
310 perror("open(O_DIRECTORY)");
318 flags = LL_LEASE_UNLCK;
321 flags = LL_LEASE_RDLCK;
324 flags = LL_LEASE_WRLCK;
327 errx(-1, "unknown mode: %c", *commands);
330 rc = ioctl(fd, LL_IOC_SET_LEASE, flags);
332 err(errno, "apply lease error");
334 if (flags != LL_LEASE_UNLCK)
337 /* F_UNLCK, interpret return code */
339 const char *str = "unknown";
340 if (rc == LL_LEASE_RDLCK)
342 else if (rc == LL_LEASE_WRLCK)
344 fprintf(stdout, "%s lease(%d) released.\n",
346 } else if (rc == 0) {
347 fprintf(stdout, "lease already broken.\n");
352 if (*commands != '-' && *commands != '+')
353 errx(-1, "unknown mode: %c\n", *commands);
355 rc = ioctl(fd, LL_IOC_GET_LEASE);
357 const char *str = "unknown";
359 if (rc == LL_LEASE_RDLCK)
361 else if (rc == LL_LEASE_WRLCK)
363 fprintf(stdout, "%s lease(%d) has applied.\n",
365 if (*commands == '-')
366 errx(-1, "expect lease to not exist");
367 } else if (rc == 0) {
368 fprintf(stdout, "no lease applied.\n");
369 if (*commands == '+')
370 errx(-1, "expect lease exists");
372 err(errno, "free lease error");
376 if (statfs(fname, &stfs) == -1)
377 errx(-1, "statfs()");
381 rc = llapi_path2fid(fname, &fid);
383 rc = llapi_fd2fid(fd, &fid);
386 "llapi_path/fd2fid() on %d, rc=%d\n",
389 printf(DFID"\n", PFID(&fid));
392 gid = atoi(commands+1);
393 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
395 perror("ioctl(GROUP_LOCK)");
400 gid = atoi(commands+1);
401 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
403 perror("ioctl(GROUP_UNLOCK)");
408 len = atoi(commands+1);
409 fd = llapi_file_open(fname, O_CREAT | O_WRONLY,
411 LOV_PATTERN_RAID0 | LOV_PATTERN_F_RELEASED);
414 perror("create stripe file");
423 if (link(oldpath, fname)) {
433 if (symlink(fname, newfile)) {
444 if (link(fname, newfile)) {
451 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
453 perror("mknod(S_IFREG|0644, 0)");
458 if (st.st_size == 0) {
459 fprintf(stderr, "mmap without preceeding stat, or on"
460 " zero length file.\n");
463 mmap_len = st.st_size;
464 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
466 if (mmap_ptr == MAP_FAILED) {
477 if (rename(oldpath, fname) < 0) {
487 if (rename (fname, newfile)) {
494 fd = open(fname, O_CREAT|O_RDWR, 0644);
497 perror("open(O_RDWR|O_CREAT)");
502 len = get_flags(commands+1, &flags);
505 fd = open(fname, flags, 0666);
507 fd = open(fname, flags);
515 len = atoi(commands+1);
520 tmp = realloc(buf, len + ALIGN_LEN);
524 perror("allocating buf for read\n");
529 buf_align = (char *)((long)(buf + ALIGN_LEN) &
533 rc = read(fd, buf_align, len);
540 fprintf(stderr, "short read: %u/%u\n",
547 printf("%.*s\n", rc, buf_align);
551 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
555 if (stat(fname, &st) == -1) {
562 if (fstat(fd, &st) == -1) {
569 if (fchmod(fd, 0) == -1) {
576 len = atoi(commands+1);
577 if (ftruncate(fd, len) == -1) {
579 printf("ftruncate (%d,%d)\n", fd, len);
585 if (unlink(fname) == -1) {
592 if (munmap(mmap_ptr, mmap_len)) {
602 len = get_flags(commands + 1, &flags);
604 fd = llapi_create_volatile(fname, flags);
606 perror("llapi_create_volatile");
611 len = atoi(commands+1);
616 tmp = realloc(buf, len + ALIGN_LEN);
620 perror("allocating buf for write\n");
625 buf_align = (char *)((long)(buf + ALIGN_LEN) &
627 strncpy(buf_align, msg, bufsize);
630 rc = write(fd, buf_align, len);
637 fprintf(stderr, "short write: %u/%u\n",
643 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
644 mmap_ptr[i] += junk++;
647 rc = llapi_get_data_version(fd, &dv, 0);
649 fprintf(stderr, "cannot get file data version"
653 printf("dataversion is %ju\n", (uintmax_t)dv);
656 if (fsync(fd) == -1) {
663 if (fdatasync(fd) == -1) {
670 len = atoi(commands+1);
671 if (lseek(fd, len, SEEK_SET) == -1) {
690 fprintf(stderr, "unknown command \"%c\"\n", *commands);
691 fprintf(stderr, usage, argv[0]);