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, 2014, 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 */
44 #include <sys/types.h>
48 #include <sys/ioctl.h>
49 #include <sys/xattr.h>
53 #include <semaphore.h>
57 #include <lustre/lustre_idl.h>
58 #include <lustre/lustreapi.h>
60 #define T1 "write data before unlink\n"
61 #define T2 "write data after unlink\n"
62 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";
63 char *buf, *buf_align;
66 #define ALIGN_LEN 65535
67 #define XATTR "user.multiop"
70 "Usage: %s filename command-sequence [path...]\n"
71 " command-sequence items:\n"
72 " A fsetxattr(\"user.multiop\")\n"
73 " a fgetxattr(\"user.multiop\")\n"
75 " B[num] call setstripe ioctl to create stripes\n"
76 " C[num] create with optional stripes\n"
78 " D open(O_DIRECTORY)\n"
79 " e[R|W|U] apply lease. R: Read; W: Write; U: Unlock\n"
80 " E[+|-] get lease. +/-: expect lease to (not) exist\n"
83 " H[num] create HSM released file with num stripes\n"
84 " G gid get grouplock\n"
85 " g gid put grouplock\n"
86 " K link path to filename\n"
88 " l symlink filename to path\n"
90 " M rw mmap to EOF (must open and stat prior)\n"
91 " n rename path to filename\n"
92 " N rename filename to path\n"
94 " O open(O_CREAT|O_RDWR)\n"
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] seek [optional position, 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 = 0;
217 int rc, len, fd = -1;
224 struct lov_user_md_v3 lum;
228 fprintf(stderr, usage, argv[0]);
232 memset(&st, 0, sizeof(st));
233 sem_init(&sem, 0, 0);
234 /* use sigaction instead of signal to avoid SA_ONESHOT semantics */
235 sigaction(SIGUSR1, &(const struct sigaction){.sa_handler = &usr1_handler},
240 for (commands = argv[2]; *commands; commands++) {
247 len = atoi(commands+1);
249 len = 3600; /* 1 hour */
250 ts.tv_sec = time(NULL) + len;
252 while (sem_timedwait(&sem, &ts) < 0 && errno == EINTR);
255 if (fsetxattr(fd, XATTR, "multiop", 8, 0)) {
262 if (fgetxattr(fd, XATTR, NULL, 0) == -1) {
269 if (close(fd) == -1) {
277 lum = (struct lov_user_md_v3) {
278 .lmm_magic = LOV_USER_MAGIC_V3,
279 .lmm_stripe_count = atoi(commands + 1),
282 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum) < 0) {
284 perror("LL_IOC_LOV_SETSTRIPE");
289 len = atoi(commands+1);
290 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
294 perror("create stripe file");
299 if (mkdir(fname, 0755) == -1) {
301 perror("mkdir(0755)");
306 fd = open(fname, O_DIRECTORY);
309 perror("open(O_DIRECTORY)");
317 flags = LL_LEASE_UNLCK;
320 flags = LL_LEASE_RDLCK;
323 flags = LL_LEASE_WRLCK;
326 errx(-1, "unknown mode: %c", *commands);
329 rc = ioctl(fd, LL_IOC_SET_LEASE, flags);
331 err(errno, "apply lease error");
333 if (flags != LL_LEASE_UNLCK)
336 /* F_UNLCK, interpret return code */
338 const char *str = "unknown";
339 if (rc == LL_LEASE_RDLCK)
341 else if (rc == LL_LEASE_WRLCK)
343 fprintf(stdout, "%s lease(%d) released.\n",
345 } else if (rc == 0) {
346 fprintf(stdout, "lease already broken.\n");
351 if (*commands != '-' && *commands != '+')
352 errx(-1, "unknown mode: %c\n", *commands);
354 rc = ioctl(fd, LL_IOC_GET_LEASE);
356 const char *str = "unknown";
358 if (rc == LL_LEASE_RDLCK)
360 else if (rc == LL_LEASE_WRLCK)
362 fprintf(stdout, "%s lease(%d) has applied.\n",
364 if (*commands == '-')
365 errx(-1, "expect lease to not exist");
366 } else if (rc == 0) {
367 fprintf(stdout, "no lease applied.\n");
368 if (*commands == '+')
369 errx(-1, "expect lease exists");
371 err(errno, "free lease error");
375 if (statfs(fname, &stfs) == -1)
376 errx(-1, "statfs()");
380 rc = llapi_path2fid(fname, &fid);
382 rc = llapi_fd2fid(fd, &fid);
385 "llapi_path/fd2fid() on %d, rc=%d\n",
388 printf(DFID"\n", PFID(&fid));
391 gid = atoi(commands+1);
392 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
394 perror("ioctl(GROUP_LOCK)");
399 gid = atoi(commands+1);
400 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
402 perror("ioctl(GROUP_UNLOCK)");
407 len = atoi(commands+1);
408 fd = llapi_file_open(fname, O_CREAT | O_WRONLY,
410 LOV_PATTERN_RAID0 | LOV_PATTERN_F_RELEASED);
413 perror("create stripe file");
422 if (link(oldpath, fname)) {
432 if (symlink(fname, newfile)) {
443 if (link(fname, newfile)) {
450 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
452 perror("mknod(S_IFREG|0644, 0)");
457 if (st.st_size == 0) {
458 fprintf(stderr, "mmap without preceeding stat, or on"
459 " zero length file.\n");
462 mmap_len = st.st_size;
463 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
465 if (mmap_ptr == MAP_FAILED) {
476 if (rename(oldpath, fname) < 0) {
486 if (rename (fname, newfile)) {
493 fd = open(fname, O_CREAT|O_RDWR, 0644);
496 perror("open(O_RDWR|O_CREAT)");
501 len = get_flags(commands+1, &flags);
504 fd = open(fname, flags, 0666);
506 fd = open(fname, flags);
514 len = atoi(commands+1);
518 buf = realloc(buf, len + ALIGN_LEN);
521 perror("allocating buf for read\n");
525 buf_align = (char *)((long)(buf + ALIGN_LEN) &
529 rc = read(fd, buf_align, len);
536 fprintf(stderr, "short read: %u/%u\n",
543 printf("%.*s\n", rc, buf_align);
547 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
551 if (stat(fname, &st) == -1) {
558 if (fstat(fd, &st) == -1) {
565 if (fchmod(fd, 0) == -1) {
572 len = atoi(commands+1);
573 if (ftruncate(fd, len) == -1) {
575 printf("ftruncate (%d,%d)\n", fd, len);
581 if (unlink(fname) == -1) {
588 if (munmap(mmap_ptr, mmap_len)) {
598 len = get_flags(commands + 1, &flags);
600 fd = llapi_create_volatile(fname, flags);
602 perror("llapi_create_volatile");
607 len = atoi(commands+1);
611 buf = realloc(buf, len + ALIGN_LEN);
614 perror("allocating buf for write\n");
618 buf_align = (char *)((long)(buf + ALIGN_LEN) &
620 strncpy(buf_align, msg, bufsize);
623 rc = write(fd, buf_align, len);
630 fprintf(stderr, "short write: %u/%u\n",
636 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
637 mmap_ptr[i] += junk++;
640 rc = llapi_get_data_version(fd, &dv, 0);
642 fprintf(stderr, "cannot get file data version"
646 printf("dataversion is "LPU64"\n", dv);
649 if (fsync(fd) == -1) {
656 if (fdatasync(fd) == -1) {
663 len = atoi(commands+1);
664 if (lseek(fd, len, SEEK_SET) == -1) {
683 fprintf(stderr, "unknown command \"%c\"\n", *commands);
684 fprintf(stderr, usage, argv[0]);