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, 2013, 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>
52 #include <semaphore.h>
55 #include <lustre/lustre_idl.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
67 "Usage: %s filename command-sequence [path...]\n"
68 " command-sequence items:\n"
70 " B[num] call setstripe ioctl to create stripes\n"
71 " C[num] create with optional stripes\n"
73 " D open(O_DIRECTORY)\n"
76 " H[num] create HSM released file with num stripes\n"
77 " G gid get grouplock\n"
78 " g gid put grouplock\n"
79 " K link path to filename\n"
81 " l symlink filename to path\n"
83 " M rw mmap to EOF (must open and stat prior)\n"
84 " n rename path to filename\n"
85 " N rename filename to path\n"
87 " O open(O_CREAT|O_RDWR)\n"
88 " r[num] read [optional length]\n"
89 " R reference entire mmap-ed region\n"
93 " T[num] ftruncate [optional position, default 0]\n"
97 " V open a volatile file\n"
98 " w[num] write optional length\n"
99 " x get file data version\n"
100 " W write entire mmap-ed region\n"
103 " z[num] seek [optional position, default 0]\n"
104 " _ wait for signal\n";
106 void usr1_handler(int unused)
108 int saved_errno = errno;
111 * signal(7): POSIX.1-2004 ...requires an implementation to guarantee
112 * that the following functions can be safely called inside a signal
122 pop_arg(int argc, char *argv[])
124 static int cur_arg = 3;
129 return argv[cur_arg++];
132 struct flag_mapping {
136 {"O_RDONLY", O_RDONLY},
137 {"O_WRONLY", O_WRONLY},
139 {"O_CREAT", O_CREAT},
141 {"O_NOCTTY", O_NOCTTY},
142 {"O_TRUNC", O_TRUNC},
143 {"O_APPEND", O_APPEND},
144 {"O_NONBLOCK", O_NONBLOCK},
145 {"O_NDELAY", O_NDELAY},
148 {"O_DIRECT", O_DIRECT},
150 {"O_LARGEFILE", O_LARGEFILE},
151 {"O_DIRECTORY", O_DIRECTORY},
152 {"O_NOFOLLOW", O_NOFOLLOW},
153 {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
157 int get_flags(char *data, int *rflags)
165 cloned_flags = strdup(data);
166 if (cloned_flags == NULL) {
167 fprintf(stderr, "Insufficient memory.\n");
171 for (tmp = strtok(cloned_flags, ":"); tmp;
172 tmp = strtok(NULL, ":")) {
175 size = tmp - cloned_flags;
176 for (i = 0; flag_table[i].flag != -1; i++) {
177 if (!strcmp(tmp, flag_table[i].string)){
178 flags |= flag_table[i].flag;
179 size += strlen(flag_table[i].string);
196 #define POP_ARG() (pop_arg(argc, argv))
198 int main(int argc, char **argv)
200 char *fname, *commands;
205 size_t mmap_len = 0, i;
206 unsigned char *mmap_ptr = NULL, junk = 0;
207 int rc, len, fd = -1;
214 struct lov_user_md_v3 lum;
218 fprintf(stderr, usage, argv[0]);
222 memset(&st, 0, sizeof(st));
223 sem_init(&sem, 0, 0);
224 /* use sigaction instead of signal to avoid SA_ONESHOT semantics */
225 sigaction(SIGUSR1, &(const struct sigaction){.sa_handler = &usr1_handler},
230 for (commands = argv[2]; *commands; commands++) {
237 len = atoi(commands+1);
239 len = 3600; /* 1 hour */
240 ts.tv_sec = time(NULL) + len;
242 while (sem_timedwait(&sem, &ts) < 0 && errno == EINTR);
245 if (close(fd) == -1) {
253 lum = (struct lov_user_md_v3) {
254 .lmm_magic = LOV_USER_MAGIC_V3,
255 .lmm_stripe_count = atoi(commands + 1),
258 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum) < 0) {
260 perror("LL_IOC_LOV_SETSTRIPE");
265 len = atoi(commands+1);
266 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
270 perror("create stripe file");
275 if (mkdir(fname, 0755) == -1) {
277 perror("mkdir(0755)");
282 fd = open(fname, O_DIRECTORY);
285 perror("open(O_DIRECTORY)");
290 if (statfs(fname, &stfs) == -1) {
298 rc = llapi_path2fid(fname, &fid);
300 rc = llapi_fd2fid(fd, &fid);
303 "llapi_path/fd2fid() on %d, rc=%d\n",
306 printf(DFID"\n", PFID(&fid));
309 gid = atoi(commands+1);
310 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
312 perror("ioctl(GROUP_LOCK)");
317 gid = atoi(commands+1);
318 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
320 perror("ioctl(GROUP_UNLOCK)");
325 len = atoi(commands+1);
326 fd = llapi_file_open(fname, O_CREAT | O_WRONLY,
328 LOV_PATTERN_RAID0 | LOV_PATTERN_F_RELEASED);
331 perror("create stripe file");
340 if (link(oldpath, fname)) {
350 if (symlink(fname, newfile)) {
361 if (link(fname, newfile)) {
368 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
370 perror("mknod(S_IFREG|0644, 0)");
375 if (st.st_size == 0) {
376 fprintf(stderr, "mmap without preceeding stat, or on"
377 " zero length file.\n");
380 mmap_len = st.st_size;
381 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
383 if (mmap_ptr == MAP_FAILED) {
394 if (rename(oldpath, fname) < 0) {
404 if (rename (fname, newfile)) {
411 fd = open(fname, O_CREAT|O_RDWR, 0644);
414 perror("open(O_RDWR|O_CREAT)");
419 len = get_flags(commands+1, &flags);
422 fd = open(fname, flags, 0666);
424 fd = open(fname, flags);
432 len = atoi(commands+1);
436 buf = realloc(buf, len + ALIGN_LEN);
439 perror("allocating buf for read\n");
443 buf_align = (char *)((long)(buf + ALIGN_LEN) &
447 rc = read(fd, buf_align, len);
454 fprintf(stderr, "short read: %u/%u\n",
461 printf("%.*s\n", rc, buf_align);
465 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
469 if (stat(fname, &st) == -1) {
476 if (fstat(fd, &st) == -1) {
483 if (fchmod(fd, 0) == -1) {
490 len = atoi(commands+1);
491 if (ftruncate(fd, len) == -1) {
493 printf("ftruncate (%d,%d)\n", fd, len);
499 if (unlink(fname) == -1) {
506 if (munmap(mmap_ptr, mmap_len)) {
516 len = get_flags(commands + 1, &flags);
518 fd = llapi_create_volatile(fname, flags);
520 perror("llapi_create_volatile");
525 len = atoi(commands+1);
529 buf = realloc(buf, len + ALIGN_LEN);
532 perror("allocating buf for write\n");
536 buf_align = (char *)((long)(buf + ALIGN_LEN) &
538 strncpy(buf_align, msg, bufsize);
541 rc = write(fd, buf_align, len);
548 fprintf(stderr, "short write: %u/%u\n",
554 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
555 mmap_ptr[i] += junk++;
558 rc = llapi_get_data_version(fd, &dv, 0);
560 fprintf(stderr, "cannot get file data version"
564 printf("dataversion is "LPU64"\n", dv);
567 if (fsync(fd) == -1) {
574 if (fdatasync(fd) == -1) {
580 len = atoi(commands+1);
581 if (lseek(fd, len, SEEK_SET) == -1) {
600 fprintf(stderr, "unknown command \"%c\"\n", *commands);
601 fprintf(stderr, usage, argv[0]);