1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
50 #include <semaphore.h>
51 #include <libcfs/libcfs.h>
52 #include <lustre/liblustreapi.h>
54 #define T1 "write data before unlink\n"
55 #define T2 "write data after unlink\n"
56 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";
57 char *buf, *buf_align;
63 "Usage: %s filename command-sequence\n"
64 " command-sequence items:\n"
66 " C[num] create with optional stripes\n"
68 " D open(O_DIRECTORY)\n"
72 " M rw mmap to EOF (must open and stat prior)\n"
75 " O open(O_CREAT|O_RDWR)\n"
76 " r[num] read [optional length]\n"
77 " R reference entire mmap-ed region\n"
81 " T[num] ftruncate [optional position, default 0]\n"
85 " w[num] write optional length\n"
86 " W write entire mmap-ed region\n"
89 " z[num] seek [optional position, default 0]\n"
90 " _ wait for signal\n";
92 void usr1_handler(int unused)
94 int saved_errno = errno;
97 * signal(7): POSIX.1-2004 ...requires an implementation to guarantee
98 * that the following functions can be safely called inside a signal
108 pop_arg(int argc, char *argv[])
110 static int cur_arg = 3;
115 return argv[cur_arg++];
118 struct flag_mapping {
122 {"O_RDONLY", O_RDONLY},
123 {"O_WRONLY", O_WRONLY},
125 {"O_CREAT", O_CREAT},
127 {"O_NOCTTY", O_NOCTTY},
128 {"O_TRUNC", O_TRUNC},
129 {"O_APPEND", O_APPEND},
130 {"O_NONBLOCK", O_NONBLOCK},
131 {"O_NDELAY", O_NDELAY},
134 {"O_DIRECT", O_DIRECT},
136 {"O_LARGEFILE", O_LARGEFILE},
137 {"O_DIRECTORY", O_DIRECTORY},
138 {"O_NOFOLLOW", O_NOFOLLOW},
142 int get_flags(char *data, int *rflags)
150 cloned_flags = strdup(data);
151 if (cloned_flags == NULL) {
152 fprintf(stderr, "Insufficient memory.\n");
156 for (tmp = strtok(cloned_flags, ":"); tmp;
157 tmp = strtok(NULL, ":")) {
160 size = tmp - cloned_flags;
161 for (i = 0; flag_table[i].flag != -1; i++) {
162 if (!strcmp(tmp, flag_table[i].string)){
163 flags |= flag_table[i].flag;
164 size += strlen(flag_table[i].string);
181 #define POP_ARG() (pop_arg(argc, argv))
182 #define min(a,b) ((a)>(b)?(b):(a))
184 int main(int argc, char **argv)
186 char *fname, *commands;
189 size_t mmap_len = 0, i;
190 unsigned char *mmap_ptr = NULL, junk = 0;
191 int rc, len, fd = -1;
197 fprintf(stderr, usage, argv[0]);
201 memset(&st, 0, sizeof(st));
202 sem_init(&sem, 0, 0);
203 /* use sigaction instead of signal to avoid SA_ONESHOT semantics */
204 sigaction(SIGUSR1, &(const struct sigaction){.sa_handler = &usr1_handler},
209 for (commands = argv[2]; *commands; commands++) {
216 while (sem_wait(&sem) == -1 && errno == EINTR);
219 if (close(fd) == -1) {
227 len = atoi(commands+1);
228 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
232 perror("create stripe file");
237 if (mkdir(fname, 0755) == -1) {
239 perror("mkdir(0755)");
244 fd = open(fname, O_DIRECTORY);
247 perror("open(O_DIRECTORY)");
255 if (symlink(fname, newfile)) {
265 if (link(fname, newfile)) {
272 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
274 perror("mknod(S_IFREG|0644, 0)");
279 if (st.st_size == 0) {
280 fprintf(stderr, "mmap without preceeding stat, or on"
281 " zero length file.\n");
284 mmap_len = st.st_size;
285 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
287 if (mmap_ptr == MAP_FAILED) {
297 if (rename (fname, newfile)) {
304 fd = open(fname, O_CREAT|O_RDWR, 0644);
307 perror("open(O_RDWR|O_CREAT)");
312 len = get_flags(commands+1, &flags);
314 fd = open(fname, flags);
322 len = atoi(commands+1);
326 buf = realloc(buf, len + ALIGN);
329 perror("allocating buf for read\n");
333 buf_align = (char *)((long)(buf + ALIGN) &
337 rc = read(fd, buf_align, len);
344 fprintf(stderr, "short read: %u/%u\n",
348 printf("%.*s\n", rc, buf_align);
352 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
356 if (stat(fname, &st) == -1) {
363 if (fstat(fd, &st) == -1) {
370 if (fchmod(fd, 0) == -1) {
377 len = atoi(commands+1);
378 if (ftruncate(fd, len) == -1) {
380 printf("ftruncate (%d,%d)\n", fd, len);
386 if (unlink(fname) == -1) {
393 if (munmap(mmap_ptr, mmap_len)) {
403 len = atoi(commands+1);
407 buf = realloc(buf, len + ALIGN);
410 perror("allocating buf for write\n");
414 buf_align = (char *)((long)(buf + ALIGN) &
416 strncpy(buf_align, msg, bufsize);
419 rc = write(fd, buf_align, len);
426 fprintf(stderr, "short write: %u/%u\n",
432 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
433 mmap_ptr[i] += junk++;
436 if (fsync(fd) == -1) {
443 if (fdatasync(fd) == -1) {
449 len = atoi(commands+1);
450 if (lseek(fd, len, SEEK_SET) == -1) {
468 fprintf(stderr, "unknown command \"%c\"\n", *commands);
469 fprintf(stderr, usage, argv[0]);