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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
36 #define _GNU_SOURCE /* pull in O_DIRECTORY in bits/fcntl.h */
42 #include <sys/types.h>
49 #include <semaphore.h>
51 #include <lustre/lustreapi.h>
53 #define T1 "write data before unlink\n"
54 #define T2 "write data after unlink\n"
55 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";
56 char *buf, *buf_align;
62 "Usage: %s filename command-sequence\n"
63 " command-sequence items:\n"
65 " C[num] create with optional stripes\n"
67 " D open(O_DIRECTORY)\n"
69 " G gid get grouplock\n"
70 " g gid put grouplock\n"
74 " M rw mmap to EOF (must open and stat prior)\n"
77 " O open(O_CREAT|O_RDWR)\n"
78 " r[num] read [optional length]\n"
79 " R reference entire mmap-ed region\n"
83 " T[num] ftruncate [optional position, default 0]\n"
87 " w[num] write optional length\n"
88 " W write entire mmap-ed region\n"
91 " z[num] seek [optional position, default 0]\n"
92 " _ wait for signal\n";
94 void usr1_handler(int unused)
96 int saved_errno = errno;
99 * signal(7): POSIX.1-2004 ...requires an implementation to guarantee
100 * that the following functions can be safely called inside a signal
110 pop_arg(int argc, char *argv[])
112 static int cur_arg = 3;
117 return argv[cur_arg++];
120 struct flag_mapping {
124 {"O_RDONLY", O_RDONLY},
125 {"O_WRONLY", O_WRONLY},
127 {"O_CREAT", O_CREAT},
129 {"O_NOCTTY", O_NOCTTY},
130 {"O_TRUNC", O_TRUNC},
131 {"O_APPEND", O_APPEND},
132 {"O_NONBLOCK", O_NONBLOCK},
133 {"O_NDELAY", O_NDELAY},
136 {"O_DIRECT", O_DIRECT},
138 {"O_LARGEFILE", O_LARGEFILE},
139 {"O_DIRECTORY", O_DIRECTORY},
140 {"O_NOFOLLOW", O_NOFOLLOW},
144 int get_flags(char *data, int *rflags)
152 cloned_flags = strdup(data);
153 if (cloned_flags == NULL) {
154 fprintf(stderr, "Insufficient memory.\n");
158 for (tmp = strtok(cloned_flags, ":"); tmp;
159 tmp = strtok(NULL, ":")) {
162 size = tmp - cloned_flags;
163 for (i = 0; flag_table[i].flag != -1; i++) {
164 if (!strcmp(tmp, flag_table[i].string)){
165 flags |= flag_table[i].flag;
166 size += strlen(flag_table[i].string);
183 #define POP_ARG() (pop_arg(argc, argv))
185 int main(int argc, char **argv)
187 char *fname, *commands;
191 size_t mmap_len = 0, i;
192 unsigned char *mmap_ptr = NULL, junk = 0;
193 int rc, len, fd = -1;
200 fprintf(stderr, usage, argv[0]);
204 memset(&st, 0, sizeof(st));
205 sem_init(&sem, 0, 0);
206 /* use sigaction instead of signal to avoid SA_ONESHOT semantics */
207 sigaction(SIGUSR1, &(const struct sigaction){.sa_handler = &usr1_handler},
212 for (commands = argv[2]; *commands; commands++) {
219 while (sem_wait(&sem) == -1 && errno == EINTR);
222 if (close(fd) == -1) {
230 len = atoi(commands+1);
231 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
235 perror("create stripe file");
240 if (mkdir(fname, 0755) == -1) {
242 perror("mkdir(0755)");
247 fd = open(fname, O_DIRECTORY);
250 perror("open(O_DIRECTORY)");
255 if (statfs(fname, &stfs) == -1) {
262 gid = atoi(commands+1);
263 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
265 perror("ioctl(GROUP_LOCK)");
270 gid = atoi(commands+1);
271 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
273 perror("ioctl(GROUP_UNLOCK)");
281 if (symlink(fname, newfile)) {
291 if (link(fname, newfile)) {
298 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
300 perror("mknod(S_IFREG|0644, 0)");
305 if (st.st_size == 0) {
306 fprintf(stderr, "mmap without preceeding stat, or on"
307 " zero length file.\n");
310 mmap_len = st.st_size;
311 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
313 if (mmap_ptr == MAP_FAILED) {
323 if (rename (fname, newfile)) {
330 fd = open(fname, O_CREAT|O_RDWR, 0644);
333 perror("open(O_RDWR|O_CREAT)");
338 len = get_flags(commands+1, &flags);
341 fd = open(fname, flags, 0666);
343 fd = open(fname, flags);
351 len = atoi(commands+1);
355 buf = realloc(buf, len + ALIGN);
358 perror("allocating buf for read\n");
362 buf_align = (char *)((long)(buf + ALIGN) &
366 rc = read(fd, buf_align, len);
373 fprintf(stderr, "short read: %u/%u\n",
380 printf("%.*s\n", rc, buf_align);
384 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
388 if (stat(fname, &st) == -1) {
395 if (fstat(fd, &st) == -1) {
402 if (fchmod(fd, 0) == -1) {
409 len = atoi(commands+1);
410 if (ftruncate(fd, len) == -1) {
412 printf("ftruncate (%d,%d)\n", fd, len);
418 if (unlink(fname) == -1) {
425 if (munmap(mmap_ptr, mmap_len)) {
435 len = atoi(commands+1);
439 buf = realloc(buf, len + ALIGN);
442 perror("allocating buf for write\n");
446 buf_align = (char *)((long)(buf + ALIGN) &
448 strncpy(buf_align, msg, bufsize);
451 rc = write(fd, buf_align, len);
458 fprintf(stderr, "short write: %u/%u\n",
464 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
465 mmap_ptr[i] += junk++;
468 if (fsync(fd) == -1) {
475 if (fdatasync(fd) == -1) {
481 len = atoi(commands+1);
482 if (lseek(fd, len, SEEK_SET) == -1) {
500 fprintf(stderr, "unknown command \"%c\"\n", *commands);
501 fprintf(stderr, usage, argv[0]);