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>
56 #include <lustre/lustre_idl.h>
57 #include <lustre/lustreapi.h>
59 #define T1 "write data before unlink\n"
60 #define T2 "write data after unlink\n"
61 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";
62 char *buf, *buf_align;
65 #define ALIGN_LEN 65535
68 "Usage: %s filename command-sequence [path...]\n"
69 " command-sequence items:\n"
71 " B[num] call setstripe ioctl to create stripes\n"
72 " C[num] create with optional stripes\n"
74 " D open(O_DIRECTORY)\n"
75 " e[R|W|U] apply lease. R: Read; W: Write; U: Unlock\n"
76 " E[+|-] get lease. +/-: expect lease to (not) exist\n"
79 " H[num] create HSM released file with num stripes\n"
80 " G gid get grouplock\n"
81 " g gid put grouplock\n"
82 " K link path to filename\n"
84 " l symlink filename to path\n"
86 " M rw mmap to EOF (must open and stat prior)\n"
87 " n rename path to filename\n"
88 " N rename filename to path\n"
90 " O open(O_CREAT|O_RDWR)\n"
91 " r[num] read [optional length]\n"
92 " R reference entire mmap-ed region\n"
96 " T[num] ftruncate [optional position, default 0]\n"
100 " V open a volatile file\n"
101 " w[num] write optional length\n"
102 " x get file data version\n"
103 " W write entire mmap-ed region\n"
106 " z[num] seek [optional position, default 0]\n"
107 " _ wait for signal\n";
109 void usr1_handler(int unused)
111 int saved_errno = errno;
114 * signal(7): POSIX.1-2004 ...requires an implementation to guarantee
115 * that the following functions can be safely called inside a signal
125 pop_arg(int argc, char *argv[])
127 static int cur_arg = 3;
132 return argv[cur_arg++];
135 struct flag_mapping {
139 {"O_RDONLY", O_RDONLY},
140 {"O_WRONLY", O_WRONLY},
142 {"O_CREAT", O_CREAT},
144 {"O_NOCTTY", O_NOCTTY},
145 {"O_TRUNC", O_TRUNC},
146 {"O_APPEND", O_APPEND},
147 {"O_NONBLOCK", O_NONBLOCK},
148 {"O_NDELAY", O_NDELAY},
151 {"O_DIRECT", O_DIRECT},
154 {"O_NOATIME", O_NOATIME},
156 {"O_LARGEFILE", O_LARGEFILE},
157 {"O_DIRECTORY", O_DIRECTORY},
158 {"O_NOFOLLOW", O_NOFOLLOW},
159 {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
163 int get_flags(char *data, int *rflags)
171 cloned_flags = strdup(data);
172 if (cloned_flags == NULL) {
173 fprintf(stderr, "Insufficient memory.\n");
177 for (tmp = strtok(cloned_flags, ":"); tmp;
178 tmp = strtok(NULL, ":")) {
181 size = tmp - cloned_flags;
182 for (i = 0; flag_table[i].flag != -1; i++) {
183 if (!strcmp(tmp, flag_table[i].string)){
184 flags |= flag_table[i].flag;
185 size += strlen(flag_table[i].string);
202 #define POP_ARG() (pop_arg(argc, argv))
204 int main(int argc, char **argv)
206 char *fname, *commands;
211 size_t mmap_len = 0, i;
212 unsigned char *mmap_ptr = NULL, junk = 0;
213 int rc, len, fd = -1;
220 struct lov_user_md_v3 lum;
224 fprintf(stderr, usage, argv[0]);
228 memset(&st, 0, sizeof(st));
229 sem_init(&sem, 0, 0);
230 /* use sigaction instead of signal to avoid SA_ONESHOT semantics */
231 sigaction(SIGUSR1, &(const struct sigaction){.sa_handler = &usr1_handler},
236 for (commands = argv[2]; *commands; commands++) {
243 len = atoi(commands+1);
245 len = 3600; /* 1 hour */
246 ts.tv_sec = time(NULL) + len;
248 while (sem_timedwait(&sem, &ts) < 0 && errno == EINTR);
251 if (close(fd) == -1) {
259 lum = (struct lov_user_md_v3) {
260 .lmm_magic = LOV_USER_MAGIC_V3,
261 .lmm_stripe_count = atoi(commands + 1),
264 if (ioctl(fd, LL_IOC_LOV_SETSTRIPE, &lum) < 0) {
266 perror("LL_IOC_LOV_SETSTRIPE");
271 len = atoi(commands+1);
272 fd = llapi_file_open(fname, O_CREAT | O_WRONLY, 0644,
276 perror("create stripe file");
281 if (mkdir(fname, 0755) == -1) {
283 perror("mkdir(0755)");
288 fd = open(fname, O_DIRECTORY);
291 perror("open(O_DIRECTORY)");
308 errx(-1, "unknown mode: %c", *commands);
311 rc = ioctl(fd, LL_IOC_SET_LEASE, flags);
313 err(errno, "apply lease error");
315 if (flags != F_UNLCK)
318 /* F_UNLCK, interpret return code */
320 const char *str = "Unknown";
321 if (rc == FMODE_READ)
323 else if (rc == FMODE_WRITE)
325 fprintf(stdout, "%s lease(%d) released.\n",
327 } else if (rc == 0) {
328 fprintf(stdout, "lease already broken.\n");
333 if (*commands != '-' && *commands != '+')
334 errx(-1, "unknown mode: %c\n", *commands);
336 rc = ioctl(fd, LL_IOC_GET_LEASE);
338 const char *str = "Unknown";
340 if (rc == FMODE_READ)
342 else if (rc == FMODE_WRITE)
344 fprintf(stdout, "%s lease(%d) has applied.\n",
346 if (*commands == '-')
347 errx(-1, "expect lease to not exist");
348 } else if (rc == 0) {
349 fprintf(stdout, "no lease applied.\n");
350 if (*commands == '+')
351 errx(-1, "expect lease exists");
353 err(errno, "free lease error");
357 if (statfs(fname, &stfs) == -1)
358 errx(-1, "statfs()");
362 rc = llapi_path2fid(fname, &fid);
364 rc = llapi_fd2fid(fd, &fid);
367 "llapi_path/fd2fid() on %d, rc=%d\n",
370 printf(DFID"\n", PFID(&fid));
373 gid = atoi(commands+1);
374 if (ioctl(fd, LL_IOC_GROUP_LOCK, gid) == -1) {
376 perror("ioctl(GROUP_LOCK)");
381 gid = atoi(commands+1);
382 if (ioctl(fd, LL_IOC_GROUP_UNLOCK, gid) == -1) {
384 perror("ioctl(GROUP_UNLOCK)");
389 len = atoi(commands+1);
390 fd = llapi_file_open(fname, O_CREAT | O_WRONLY,
392 LOV_PATTERN_RAID0 | LOV_PATTERN_F_RELEASED);
395 perror("create stripe file");
404 if (link(oldpath, fname)) {
414 if (symlink(fname, newfile)) {
425 if (link(fname, newfile)) {
432 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
434 perror("mknod(S_IFREG|0644, 0)");
439 if (st.st_size == 0) {
440 fprintf(stderr, "mmap without preceeding stat, or on"
441 " zero length file.\n");
444 mmap_len = st.st_size;
445 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
447 if (mmap_ptr == MAP_FAILED) {
458 if (rename(oldpath, fname) < 0) {
468 if (rename (fname, newfile)) {
475 fd = open(fname, O_CREAT|O_RDWR, 0644);
478 perror("open(O_RDWR|O_CREAT)");
483 len = get_flags(commands+1, &flags);
486 fd = open(fname, flags, 0666);
488 fd = open(fname, flags);
496 len = atoi(commands+1);
500 buf = realloc(buf, len + ALIGN_LEN);
503 perror("allocating buf for read\n");
507 buf_align = (char *)((long)(buf + ALIGN_LEN) &
511 rc = read(fd, buf_align, len);
518 fprintf(stderr, "short read: %u/%u\n",
525 printf("%.*s\n", rc, buf_align);
529 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
533 if (stat(fname, &st) == -1) {
540 if (fstat(fd, &st) == -1) {
547 if (fchmod(fd, 0) == -1) {
554 len = atoi(commands+1);
555 if (ftruncate(fd, len) == -1) {
557 printf("ftruncate (%d,%d)\n", fd, len);
563 if (unlink(fname) == -1) {
570 if (munmap(mmap_ptr, mmap_len)) {
580 len = get_flags(commands + 1, &flags);
582 fd = llapi_create_volatile(fname, flags);
584 perror("llapi_create_volatile");
589 len = atoi(commands+1);
593 buf = realloc(buf, len + ALIGN_LEN);
596 perror("allocating buf for write\n");
600 buf_align = (char *)((long)(buf + ALIGN_LEN) &
602 strncpy(buf_align, msg, bufsize);
605 rc = write(fd, buf_align, len);
612 fprintf(stderr, "short write: %u/%u\n",
618 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
619 mmap_ptr[i] += junk++;
622 rc = llapi_get_data_version(fd, &dv, 0);
624 fprintf(stderr, "cannot get file data version"
628 printf("dataversion is "LPU64"\n", dv);
631 if (fsync(fd) == -1) {
638 if (fdatasync(fd) == -1) {
645 len = atoi(commands+1);
646 if (lseek(fd, len, SEEK_SET) == -1) {
665 fprintf(stderr, "unknown command \"%c\"\n", *commands);
666 fprintf(stderr, usage, argv[0]);