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>
55 #include <semaphore.h>
59 #include <lustre/lustre_idl.h>
60 #include <lustre/lustreapi.h>
62 #define T1 "write data before unlink\n"
63 #define T2 "write data after unlink\n"
64 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";
65 char *buf, *buf_align;
68 #define ALIGN_LEN 65535
69 #define XATTR "user.multiop"
72 "Usage: %s filename command-sequence [path...]\n"
73 " command-sequence items:\n"
74 " A fsetxattr(\"user.multiop\")\n"
75 " a fgetxattr(\"user.multiop\")\n"
77 " B[num] call setstripe ioctl to create stripes\n"
78 " C[num] create with optional stripes\n"
80 " D open(O_DIRECTORY)\n"
81 " e[R|W|U] apply lease. R: Read; W: Write; U: Unlock\n"
82 " E[+|-] get lease. +/-: expect lease to (not) exist\n"
85 " H[num] create HSM released file with num stripes\n"
86 " G gid get grouplock\n"
87 " g gid put grouplock\n"
88 " K link path to filename\n"
90 " l symlink filename to path\n"
92 " M rw mmap to EOF (must open and stat prior)\n"
93 " n rename path to filename\n"
94 " N rename filename to path\n"
96 " O open(O_CREAT|O_RDWR)\n"
97 " r[num] read [optional length]\n"
98 " R reference entire mmap-ed region\n"
102 " T[num] ftruncate [optional position, default 0]\n"
106 " V open a volatile file\n"
107 " w[num] write optional length\n"
108 " x get file data version\n"
109 " W write entire mmap-ed region\n"
112 " z[num] seek [optional position, default 0]\n"
113 " _ wait for signal\n";
115 void usr1_handler(int unused)
117 int saved_errno = errno;
120 * signal(7): POSIX.1-2004 ...requires an implementation to guarantee
121 * that the following functions can be safely called inside a signal
131 pop_arg(int argc, char *argv[])
133 static int cur_arg = 3;
138 return argv[cur_arg++];
141 struct flag_mapping {
145 {"O_RDONLY", O_RDONLY},
146 {"O_WRONLY", O_WRONLY},
148 {"O_CREAT", O_CREAT},
150 {"O_NOCTTY", O_NOCTTY},
151 {"O_TRUNC", O_TRUNC},
152 {"O_APPEND", O_APPEND},
153 {"O_NONBLOCK", O_NONBLOCK},
154 {"O_NDELAY", O_NDELAY},
157 {"O_DIRECT", O_DIRECT},
160 {"O_NOATIME", O_NOATIME},
162 {"O_LARGEFILE", O_LARGEFILE},
163 {"O_DIRECTORY", O_DIRECTORY},
164 {"O_NOFOLLOW", O_NOFOLLOW},
165 {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
169 int get_flags(char *data, int *rflags)
177 cloned_flags = strdup(data);
178 if (cloned_flags == NULL) {
179 fprintf(stderr, "Insufficient memory.\n");
183 for (tmp = strtok(cloned_flags, ":"); tmp;
184 tmp = strtok(NULL, ":")) {
187 size = tmp - cloned_flags;
188 for (i = 0; flag_table[i].flag != -1; i++) {
189 if (!strcmp(tmp, flag_table[i].string)){
190 flags |= flag_table[i].flag;
191 size += strlen(flag_table[i].string);
208 #define POP_ARG() (pop_arg(argc, argv))
210 int main(int argc, char **argv)
212 char *fname, *commands;
217 size_t mmap_len = 0, i;
218 unsigned char *mmap_ptr = NULL, junk = 0;
219 int rc, len, fd = -1;
226 struct lov_user_md_v3 lum;
230 fprintf(stderr, usage, argv[0]);
234 memset(&st, 0, sizeof(st));
235 sem_init(&sem, 0, 0);
236 /* use sigaction instead of signal to avoid SA_ONESHOT semantics */
237 sigaction(SIGUSR1, &(const struct sigaction){.sa_handler = &usr1_handler},
242 for (commands = argv[2]; *commands; commands++) {
249 len = atoi(commands+1);
251 len = 3600; /* 1 hour */
252 ts.tv_sec = time(NULL) + len;
254 while (sem_timedwait(&sem, &ts) < 0 && errno == EINTR);
257 if (fsetxattr(fd, XATTR, "multiop", 8, 0)) {
264 if (fgetxattr(fd, XATTR, NULL, 0) == -1) {
271 if (close(fd) == -1) {
279 lum = (struct lov_user_md_v3) {
280 .lmm_magic = LOV_USER_MAGIC_V3,
281 .lmm_stripe_count = atoi(commands + 1),
284 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum) < 0) {
286 perror("LL_IOC_LOV_SETSTRIPE");
291 len = atoi(commands+1);
292 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
296 perror("create stripe file");
301 if (mkdir(fname, 0755) == -1) {
303 perror("mkdir(0755)");
308 fd = open(fname, O_DIRECTORY);
311 perror("open(O_DIRECTORY)");
319 flags = LL_LEASE_UNLCK;
322 flags = LL_LEASE_RDLCK;
325 flags = LL_LEASE_WRLCK;
328 errx(-1, "unknown mode: %c", *commands);
331 rc = ioctl(fd, LL_IOC_SET_LEASE, flags);
333 err(errno, "apply lease error");
335 if (flags != LL_LEASE_UNLCK)
338 /* F_UNLCK, interpret return code */
340 const char *str = "unknown";
341 if (rc == LL_LEASE_RDLCK)
343 else if (rc == LL_LEASE_WRLCK)
345 fprintf(stdout, "%s lease(%d) released.\n",
347 } else if (rc == 0) {
348 fprintf(stdout, "lease already broken.\n");
353 if (*commands != '-' && *commands != '+')
354 errx(-1, "unknown mode: %c\n", *commands);
356 rc = ioctl(fd, LL_IOC_GET_LEASE);
358 const char *str = "unknown";
360 if (rc == LL_LEASE_RDLCK)
362 else if (rc == LL_LEASE_WRLCK)
364 fprintf(stdout, "%s lease(%d) has applied.\n",
366 if (*commands == '-')
367 errx(-1, "expect lease to not exist");
368 } else if (rc == 0) {
369 fprintf(stdout, "no lease applied.\n");
370 if (*commands == '+')
371 errx(-1, "expect lease exists");
373 err(errno, "free lease error");
377 if (statfs(fname, &stfs) == -1)
378 errx(-1, "statfs()");
382 rc = llapi_path2fid(fname, &fid);
384 rc = llapi_fd2fid(fd, &fid);
387 "llapi_path/fd2fid() on %d, rc=%d\n",
390 printf(DFID"\n", PFID(&fid));
393 gid = atoi(commands+1);
394 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
396 perror("ioctl(GROUP_LOCK)");
401 gid = atoi(commands+1);
402 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
404 perror("ioctl(GROUP_UNLOCK)");
409 len = atoi(commands+1);
410 fd = llapi_file_open(fname, O_CREAT | O_WRONLY,
412 LOV_PATTERN_RAID0 | LOV_PATTERN_F_RELEASED);
415 perror("create stripe file");
420 if (flock(fd, LOCK_EX) == -1)
428 if (link(oldpath, fname)) {
438 if (symlink(fname, newfile)) {
449 if (link(fname, newfile)) {
456 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
458 perror("mknod(S_IFREG|0644, 0)");
463 if (st.st_size == 0) {
464 fprintf(stderr, "mmap without preceeding stat, or on"
465 " zero length file.\n");
468 mmap_len = st.st_size;
469 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
471 if (mmap_ptr == MAP_FAILED) {
482 if (rename(oldpath, fname) < 0) {
492 if (rename (fname, newfile)) {
499 fd = open(fname, O_CREAT|O_RDWR, 0644);
502 perror("open(O_RDWR|O_CREAT)");
507 len = get_flags(commands+1, &flags);
510 fd = open(fname, flags, 0666);
512 fd = open(fname, flags);
520 len = atoi(commands+1);
525 tmp = realloc(buf, len + ALIGN_LEN);
529 perror("allocating buf for read\n");
534 buf_align = (char *)((long)(buf + ALIGN_LEN) &
538 rc = read(fd, buf_align, len);
545 fprintf(stderr, "short read: %u/%u\n",
552 printf("%.*s\n", rc, buf_align);
556 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
560 if (stat(fname, &st) == -1) {
567 if (fstat(fd, &st) == -1) {
574 if (fchmod(fd, 0) == -1) {
581 len = atoi(commands+1);
582 if (ftruncate(fd, len) == -1) {
584 printf("ftruncate (%d,%d)\n", fd, len);
590 if (unlink(fname) == -1) {
597 if (munmap(mmap_ptr, mmap_len)) {
607 len = get_flags(commands + 1, &flags);
609 fd = llapi_create_volatile(fname, flags);
611 perror("llapi_create_volatile");
616 len = atoi(commands+1);
621 tmp = realloc(buf, len + ALIGN_LEN);
625 perror("allocating buf for write\n");
630 buf_align = (char *)((long)(buf + ALIGN_LEN) &
632 strncpy(buf_align, msg, bufsize);
635 rc = write(fd, buf_align, len);
642 fprintf(stderr, "short write: %u/%u\n",
648 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
649 mmap_ptr[i] += junk++;
652 rc = llapi_get_data_version(fd, &dv, 0);
654 fprintf(stderr, "cannot get file data version"
658 printf("dataversion is %ju\n", (uintmax_t)dv);
661 if (fsync(fd) == -1) {
668 if (fdatasync(fd) == -1) {
675 len = atoi(commands+1);
676 if (lseek(fd, len, SEEK_SET) == -1) {
695 fprintf(stderr, "unknown command \"%c\"\n", *commands);
696 fprintf(stderr, usage, argv[0]);