1 // -*- mode:C++; tab-width:8; c-basic-offset:8; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=8 smarttab
4 * Copyright (C) 1991, NeXT Computer, Inc. All Rights Reserverd.
7 * Author: Avadis Tevanian, Jr.
9 * File system exerciser.
11 * Rewritten 8/98 by Conrad Minshall.
13 * Small changes to work under Linux -- davej.
15 * Checks for mmap last-page zero fill.
18 #include <sys/types.h>
23 #if defined(__FreeBSD__)
29 #if defined(__linux__)
32 #include <sys/ioctl.h>
48 #include "include/compat.h"
49 #include "include/intarith.h"
50 #if defined(WITH_KRBD)
51 #include "include/krbd.h"
53 #include "include/rados/librados.h"
54 #include "include/rados/librados.hpp"
55 #include "include/rbd/librbd.h"
56 #include "include/rbd/librbd.hpp"
57 #include "common/Cond.h"
58 #include "common/SubProcess.h"
59 #include "common/safe_io.h"
60 #include "journal/Journaler.h"
61 #include "journal/ReplayEntry.h"
62 #include "journal/ReplayHandler.h"
63 #include "journal/Settings.h"
65 #include <boost/scope_exit.hpp>
67 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
70 * A log entry is an operation and a bunch of arguments.
80 struct log_entry oplog
[LOGSIZE
]; /* the log */
81 int logptr
= 0; /* current position in log */
82 int logcount
= 0; /* total ops */
85 * The operation matrix is complex due to conditional execution of different
86 * features. Hence when we come to deciding what operation to run, we need to
87 * be careful in how we select the different operations. The active operations
88 * are mapped to numbers as follows:
99 * COMPAREANDWRITE: - 8
101 * When mapped read/writes are disabled, they are simply converted to normal
102 * reads and writes. When fallocate/fpunch calls are disabled, they are
103 * converted to OP_SKIPPED. Hence OP_SKIPPED needs to have a number higher than
104 * the operation selction matrix, as does the OP_CLOSEOPEN which is an
105 * operation modifier rather than an operation in itself.
107 * Because of the "lite" version, we also need to have different "maximum
108 * operation" defines to allow the ops to be selected correctly based on the
112 /* common operations */
116 #define OP_MAPWRITE 3
117 #define OP_MAX_LITE 4
119 /* !lite operations */
120 #define OP_TRUNCATE 4
121 #define OP_FALLOCATE 5
122 #define OP_PUNCH_HOLE 6
123 #define OP_WRITESAME 7
124 #define OP_COMPARE_AND_WRITE 8
125 /* rbd-specific operations */
127 #define OP_FLATTEN 10
128 #define OP_MAX_FULL 11
130 /* operation modifiers */
131 #define OP_CLOSEOPEN 100
132 #define OP_SKIPPED 101
135 #define PAGE_SIZE getpagesize()
137 #define PAGE_MASK (PAGE_SIZE - 1)
140 char *original_buf
; /* a pointer to the original data */
141 char *good_buf
; /* a pointer to the correct data */
142 char *temp_buf
; /* a pointer to the current data */
148 unsigned long testcalls
= 0; /* calls to function "test" */
150 const char* cluster_name
= "ceph"; /* --cluster optional */
151 const char* client_id
= "admin"; /* --id optional */
153 unsigned long simulatedopcount
= 0; /* -b flag */
154 int closeprob
= 0; /* -c flag */
155 int debug
= 0; /* -d flag */
156 unsigned long debugstart
= 0; /* -D flag */
157 int flush_enabled
= 0; /* -f flag */
158 int deep_copy
= 0; /* -g flag */
159 int holebdy
= 1; /* -h flag */
160 bool journal_replay
= false; /* -j flah */
161 int keep_on_success
= 0; /* -k flag */
162 int do_fsync
= 0; /* -y flag */
163 unsigned long maxfilelen
= 256 * 1024; /* -l flag */
164 int sizechecks
= 1; /* -n flag disables them */
165 int maxoplen
= 64 * 1024; /* -o flag */
166 int quiet
= 0; /* -q flag */
167 unsigned long progressinterval
= 0; /* -p flag */
168 int readbdy
= 1; /* -r flag */
169 int style
= 0; /* -s flag */
170 int prealloc
= 0; /* -x flag */
171 int truncbdy
= 1; /* -t flag */
172 int writebdy
= 1; /* -w flag */
173 long monitorstart
= -1; /* -m flag */
174 long monitorend
= -1; /* -m flag */
175 int lite
= 0; /* -L flag */
176 long numops
= -1; /* -N flag */
177 int randomoplen
= 1; /* -O flag disables it */
178 int seed
= 1; /* -S flag */
179 int mapped_writes
= 0; /* -W flag disables */
180 int fallocate_calls
= 0; /* -F flag disables */
181 int punch_hole_calls
= 1; /* -H flag disables */
182 int clone_calls
= 1; /* -C flag disables */
183 int randomize_striping
= 1; /* -U flag disables */
184 int randomize_parent_overlap
= 1;
185 int mapped_reads
= 0; /* -R flag disables it */
187 int o_direct
= 0; /* -Z flag */
195 FILE * fsxlogf
= NULL
;
200 vwarnc(int code
, const char *fmt
, va_list ap
) {
201 fprintf(stderr
, "fsx: ");
203 vfprintf(stderr
, fmt
, ap
);
204 fprintf(stderr
, ": ");
206 fprintf(stderr
, "%s\n", strerror(code
));
210 warn(const char * fmt
, ...) {
213 vwarnc(errno
, fmt
, ap
);
217 #define BUF_SIZE 1024
220 prt(const char *fmt
, ...)
223 char buffer
[BUF_SIZE
];
226 vsnprintf(buffer
, BUF_SIZE
, fmt
, args
);
228 fprintf(stdout
, "%s", buffer
);
230 fprintf(fsxlogf
, "%s", buffer
);
234 prterr(const char *prefix
)
236 prt("%s%s%s\n", prefix
, prefix
? ": " : "", strerror(errno
));
240 prterrcode(const char *prefix
, int code
)
242 prt("%s%s%s\n", prefix
, prefix
? ": " : "", strerror(-code
));
246 simple_err(const char *msg
, int err
)
248 fprintf(stderr
, "%s: %s\n", msg
, strerror(-err
));
254 std::mt19937 random_generator
;
259 return random_generator();
262 int get_features(uint64_t* features
);
263 void replay_imagename(char *buf
, size_t len
, int clones
);
267 static const std::string
JOURNAL_CLIENT_ID("fsx");
269 struct ReplayHandler
: public journal::ReplayHandler
{
270 journal::Journaler
*journaler
;
271 journal::Journaler
*replay_journaler
;
274 ReplayHandler(journal::Journaler
*journaler
,
275 journal::Journaler
*replay_journaler
, Context
*on_finish
)
276 : journaler(journaler
), replay_journaler(replay_journaler
),
277 on_finish(on_finish
) {
280 void handle_entries_available() override
{
282 journal::ReplayEntry replay_entry
;
283 if (!journaler
->try_pop_front(&replay_entry
)) {
287 replay_journaler
->append(0, replay_entry
.get_data());
291 void handle_complete(int r
) override
{
292 on_finish
->complete(r
);
296 int get_image_id(librados::IoCtx
&io_ctx
, const char *image_name
,
297 std::string
*image_id
) {
300 int r
= rbd
.open(io_ctx
, image
, image_name
);
302 simple_err("failed to open image", r
);
306 rbd_image_info_t info
;
307 r
= image
.stat(info
, sizeof(info
));
309 simple_err("failed to stat image", r
);
313 *image_id
= std::string(&info
.block_name_prefix
[strlen(RBD_DATA_PREFIX
)]);
317 int register_journal(rados_ioctx_t ioctx
, const char *image_name
) {
318 librados::IoCtx io_ctx
;
319 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
321 std::string image_id
;
322 int r
= get_image_id(io_ctx
, image_name
, &image_id
);
327 journal::Journaler
journaler(io_ctx
, image_id
, JOURNAL_CLIENT_ID
, {},
329 r
= journaler
.register_client(bufferlist());
331 simple_err("failed to register journal client", r
);
337 int unregister_journal(rados_ioctx_t ioctx
, const char *image_name
) {
338 librados::IoCtx io_ctx
;
339 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
341 std::string image_id
;
342 int r
= get_image_id(io_ctx
, image_name
, &image_id
);
347 journal::Journaler
journaler(io_ctx
, image_id
, JOURNAL_CLIENT_ID
, {},
349 r
= journaler
.unregister_client();
351 simple_err("failed to unregister journal client", r
);
357 int create_replay_image(rados_ioctx_t ioctx
, int order
,
358 uint64_t stripe_unit
, int stripe_count
,
359 const char *replay_image_name
,
360 const char *last_replay_image_name
) {
361 librados::IoCtx io_ctx
;
362 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
365 int r
= get_features(&features
);
371 if (last_replay_image_name
== nullptr) {
372 r
= rbd
.create2(io_ctx
, replay_image_name
, 0, features
, &order
);
374 r
= rbd
.clone2(io_ctx
, last_replay_image_name
, "snap",
375 io_ctx
, replay_image_name
, features
, &order
,
376 stripe_unit
, stripe_count
);
380 simple_err("failed to create replay image", r
);
387 int replay_journal(rados_ioctx_t ioctx
, const char *image_name
,
388 const char *replay_image_name
) {
389 librados::IoCtx io_ctx
;
390 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
392 std::string image_id
;
393 int r
= get_image_id(io_ctx
, image_name
, &image_id
);
398 std::string replay_image_id
;
399 r
= get_image_id(io_ctx
, replay_image_name
, &replay_image_id
);
404 journal::Journaler
journaler(io_ctx
, image_id
, JOURNAL_CLIENT_ID
, {},
406 C_SaferCond init_ctx
;
407 journaler
.init(&init_ctx
);
408 BOOST_SCOPE_EXIT_ALL( (&journaler
) ) {
409 journaler
.shut_down();
414 simple_err("failed to initialize journal", r
);
418 journal::Journaler
replay_journaler(io_ctx
, replay_image_id
, "", {},
421 C_SaferCond replay_init_ctx
;
422 replay_journaler
.init(&replay_init_ctx
);
423 BOOST_SCOPE_EXIT_ALL( (&replay_journaler
) ) {
424 replay_journaler
.shut_down();
427 r
= replay_init_ctx
.wait();
429 simple_err("failed to initialize replay journal", r
);
433 replay_journaler
.start_append(0);
435 C_SaferCond replay_ctx
;
436 ReplayHandler
replay_handler(&journaler
, &replay_journaler
,
439 // copy journal events from source image to replay image
440 journaler
.start_replay(&replay_handler
);
441 r
= replay_ctx
.wait();
443 journaler
.stop_replay();
445 C_SaferCond stop_ctx
;
446 replay_journaler
.stop_append(&stop_ctx
);
447 int stop_r
= stop_ctx
.wait();
448 if (r
== 0 && stop_r
< 0) {
453 simple_err("failed to replay journal", r
);
459 r
= rbd
.open(io_ctx
, image
, replay_image_name
);
461 simple_err("failed to open replay image", r
);
465 // perform an IO op to initiate the journal replay
467 r
= static_cast<ssize_t
>(image
.write(0, 0, bl
));
469 simple_err("failed to write to replay image", r
);
475 int finalize_journal(rados_ioctx_t ioctx
, const char *imagename
, int clones
,
476 int order
, uint64_t stripe_unit
, int stripe_count
) {
477 char replayimagename
[1024];
478 replay_imagename(replayimagename
, sizeof(replayimagename
), clones
);
480 char lastreplayimagename
[1024];
482 replay_imagename(lastreplayimagename
,
483 sizeof(lastreplayimagename
), clones
- 1);
486 int ret
= create_replay_image(ioctx
, order
, stripe_unit
,
487 stripe_count
, replayimagename
,
488 clones
> 0 ? lastreplayimagename
:
494 ret
= replay_journal(ioctx
, imagename
, replayimagename
);
501 } // anonymous namespace
508 const char *name
; /* image name */
509 rbd_image_t image
; /* image handle */
510 const char *krbd_name
; /* image /dev/rbd<id> name */ /* reused for nbd test */
511 int krbd_fd
; /* image /dev/rbd<id> fd */ /* reused for nbd test */
514 #define RBD_CTX_INIT (struct rbd_ctx) { NULL, NULL, NULL, -1}
516 struct rbd_operations
{
517 int (*open
)(const char *name
, struct rbd_ctx
*ctx
);
518 int (*close
)(struct rbd_ctx
*ctx
);
519 ssize_t (*read
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, char *buf
);
520 ssize_t (*write
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, const char *buf
);
521 int (*flush
)(struct rbd_ctx
*ctx
);
522 int (*discard
)(struct rbd_ctx
*ctx
, uint64_t off
, uint64_t len
);
523 int (*get_size
)(struct rbd_ctx
*ctx
, uint64_t *size
);
524 int (*resize
)(struct rbd_ctx
*ctx
, uint64_t size
);
525 int (*clone
)(struct rbd_ctx
*ctx
, const char *src_snapname
,
526 const char *dst_imagename
, int *order
, int stripe_unit
,
528 int (*flatten
)(struct rbd_ctx
*ctx
);
529 ssize_t (*writesame
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
,
530 const char *buf
, size_t data_len
);
531 ssize_t (*compare_and_write
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
,
532 const char *cmp_buf
, const char *buf
);
535 char *pool
; /* name of the pool our test image is in */
536 char *iname
; /* name of our test image */
537 rados_t cluster
; /* handle for our test cluster */
538 rados_ioctx_t ioctx
; /* handle for our test pool */
539 #if defined(WITH_KRBD)
540 struct krbd_ctx
*krbd
; /* handle for libkrbd */
542 bool skip_partial_discard
; /* rbd_skip_partial_discard config value*/
544 int get_features(uint64_t* features
) {
546 int r
= rados_conf_get(cluster
, "rbd_default_features", buf
,
549 simple_err("Could not get rbd_default_features value", r
);
553 *features
= strtol(buf
, NULL
, 0);
556 *features
|= RBD_FEATURE_LAYERING
;
558 if (journal_replay
) {
559 *features
|= (RBD_FEATURE_EXCLUSIVE_LOCK
|
560 RBD_FEATURE_JOURNALING
);
566 * librbd/krbd rbd_operations handlers. Given the rest of fsx.c, no
567 * attempt to do error handling is made in these handlers.
571 __librbd_open(const char *name
, struct rbd_ctx
*ctx
)
576 ceph_assert(!ctx
->name
&& !ctx
->image
&&
577 !ctx
->krbd_name
&& ctx
->krbd_fd
< 0);
579 ret
= rbd_open(ioctx
, name
, &image
, NULL
);
581 prt("rbd_open(%s) failed\n", name
);
585 ctx
->name
= strdup(name
);
587 ctx
->krbd_name
= NULL
;
594 librbd_open(const char *name
, struct rbd_ctx
*ctx
)
596 return __librbd_open(name
, ctx
);
600 __librbd_close(struct rbd_ctx
*ctx
)
604 ceph_assert(ctx
->name
&& ctx
->image
);
606 ret
= rbd_close(ctx
->image
);
608 prt("rbd_close(%s) failed\n", ctx
->name
);
612 free((void *)ctx
->name
);
621 librbd_close(struct rbd_ctx
*ctx
)
623 return __librbd_close(ctx
);
627 librbd_verify_object_map(struct rbd_ctx
*ctx
)
631 n
= rbd_get_flags(ctx
->image
, &flags
);
633 prt("rbd_get_flags() failed\n");
637 if ((flags
& RBD_FLAG_OBJECT_MAP_INVALID
) != 0) {
638 prt("rbd_get_flags() indicates object map is invalid\n");
645 librbd_read(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, char *buf
)
649 n
= rbd_read(ctx
->image
, off
, len
, buf
);
651 prt("rbd_read(%llu, %zu) failed\n", off
, len
);
657 librbd_write(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, const char *buf
)
662 n
= rbd_write(ctx
->image
, off
, len
, buf
);
664 prt("rbd_write(%llu, %zu) failed\n", off
, len
);
668 ret
= librbd_verify_object_map(ctx
);
676 librbd_flush(struct rbd_ctx
*ctx
)
680 ret
= rbd_flush(ctx
->image
);
682 prt("rbd_flush failed\n");
686 return librbd_verify_object_map(ctx
);
690 librbd_discard(struct rbd_ctx
*ctx
, uint64_t off
, uint64_t len
)
694 ret
= rbd_discard(ctx
->image
, off
, len
);
696 prt("rbd_discard(%llu, %llu) failed\n", off
, len
);
700 return librbd_verify_object_map(ctx
);
704 librbd_writesame(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
,
705 const char *buf
, size_t data_len
)
710 n
= rbd_writesame(ctx
->image
, off
, len
, buf
, data_len
, 0);
712 prt("rbd_writesame(%llu, %zu) failed\n", off
, len
);
716 ret
= librbd_verify_object_map(ctx
);
724 librbd_compare_and_write(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
,
725 const char *cmp_buf
, const char *buf
)
729 uint64_t mismatch_off
= 0;
731 n
= rbd_compare_and_write(ctx
->image
, off
, len
, cmp_buf
, buf
, &mismatch_off
, 0);
735 prt("rbd_compare_and_write mismatch(%llu, %zu, %llu) failed\n",
736 off
, len
, mismatch_off
);
740 ret
= librbd_verify_object_map(ctx
);
749 librbd_get_size(struct rbd_ctx
*ctx
, uint64_t *size
)
753 ret
= rbd_get_size(ctx
->image
, size
);
755 prt("rbd_get_size failed\n");
763 __librbd_resize(struct rbd_ctx
*ctx
, uint64_t size
)
767 ret
= rbd_resize(ctx
->image
, size
);
769 prt("rbd_resize(%llu) failed\n", size
);
773 return librbd_verify_object_map(ctx
);
777 librbd_resize(struct rbd_ctx
*ctx
, uint64_t size
)
779 return __librbd_resize(ctx
, size
);
783 __librbd_deep_copy(struct rbd_ctx
*ctx
, const char *src_snapname
,
784 const char *dst_imagename
, uint64_t features
, int *order
,
785 int stripe_unit
, int stripe_count
) {
788 rbd_image_options_t opts
;
789 rbd_image_options_create(&opts
);
790 BOOST_SCOPE_EXIT_ALL( (&opts
) ) {
791 rbd_image_options_destroy(opts
);
793 ret
= rbd_image_options_set_uint64(opts
, RBD_IMAGE_OPTION_FEATURES
,
795 ceph_assert(ret
== 0);
796 ret
= rbd_image_options_set_uint64(opts
, RBD_IMAGE_OPTION_ORDER
,
798 ceph_assert(ret
== 0);
799 ret
= rbd_image_options_set_uint64(opts
, RBD_IMAGE_OPTION_STRIPE_UNIT
,
801 ceph_assert(ret
== 0);
802 ret
= rbd_image_options_set_uint64(opts
, RBD_IMAGE_OPTION_STRIPE_COUNT
,
804 ceph_assert(ret
== 0);
806 ret
= rbd_snap_set(ctx
->image
, src_snapname
);
808 prt("rbd_snap_set(%s@%s) failed\n", ctx
->name
, src_snapname
);
812 ret
= rbd_deep_copy(ctx
->image
, ioctx
, dst_imagename
, opts
);
814 prt("rbd_deep_copy(%s@%s -> %s) failed\n",
815 ctx
->name
, src_snapname
, dst_imagename
);
819 ret
= rbd_snap_set(ctx
->image
, "");
821 prt("rbd_snap_set(%s@) failed\n", ctx
->name
);
826 ret
= rbd_open(ioctx
, dst_imagename
, &image
, nullptr);
828 prt("rbd_open(%s) failed\n", dst_imagename
);
832 ret
= rbd_snap_unprotect(image
, src_snapname
);
834 prt("rbd_snap_unprotect(%s@%s) failed\n", dst_imagename
,
839 ret
= rbd_snap_remove(image
, src_snapname
);
841 prt("rbd_snap_remove(%s@%s) failed\n", dst_imagename
,
846 ret
= rbd_close(image
);
848 prt("rbd_close(%s) failed\n", dst_imagename
);
856 __librbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
857 const char *dst_imagename
, int *order
, int stripe_unit
,
862 ret
= rbd_snap_create(ctx
->image
, src_snapname
);
864 prt("rbd_snap_create(%s@%s) failed\n", ctx
->name
,
869 ret
= rbd_snap_protect(ctx
->image
, src_snapname
);
871 prt("rbd_snap_protect(%s@%s) failed\n", ctx
->name
,
877 ret
= get_features(&features
);
883 ret
= __librbd_deep_copy(ctx
, src_snapname
, dst_imagename
, features
,
884 order
, stripe_unit
, stripe_count
);
886 prt("deep_copy(%s@%s -> %s) failed\n", ctx
->name
,
887 src_snapname
, dst_imagename
);
891 ret
= rbd_clone2(ioctx
, ctx
->name
, src_snapname
, ioctx
,
892 dst_imagename
, features
, order
,
893 stripe_unit
, stripe_count
);
895 prt("rbd_clone2(%s@%s -> %s) failed\n", ctx
->name
,
896 src_snapname
, dst_imagename
);
905 librbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
906 const char *dst_imagename
, int *order
, int stripe_unit
,
909 return __librbd_clone(ctx
, src_snapname
, dst_imagename
, order
,
910 stripe_unit
, stripe_count
);
914 __librbd_flatten(struct rbd_ctx
*ctx
)
918 ret
= rbd_flatten(ctx
->image
);
920 prt("rbd_flatten failed\n");
924 return librbd_verify_object_map(ctx
);
928 librbd_flatten(struct rbd_ctx
*ctx
)
930 return __librbd_flatten(ctx
);
933 const struct rbd_operations librbd_operations
= {
945 librbd_compare_and_write
,
948 #if defined(WITH_KRBD)
950 krbd_open(const char *name
, struct rbd_ctx
*ctx
)
956 ret
= __librbd_open(name
, ctx
);
960 ret
= krbd_map(krbd
, pool
, "", name
, "", "", &devnode
);
962 prt("krbd_map(%s) failed\n", name
);
966 fd
= open(devnode
, O_RDWR
| o_direct
);
969 prt("open(%s) failed\n", devnode
);
973 ctx
->krbd_name
= devnode
;
980 krbd_close(struct rbd_ctx
*ctx
)
984 ceph_assert(ctx
->krbd_name
&& ctx
->krbd_fd
>= 0);
986 if (close(ctx
->krbd_fd
) < 0) {
988 prt("close(%s) failed\n", ctx
->krbd_name
);
992 ret
= krbd_unmap(krbd
, ctx
->krbd_name
, "");
994 prt("krbd_unmap(%s) failed\n", ctx
->krbd_name
);
998 free((void *)ctx
->krbd_name
);
1000 ctx
->krbd_name
= NULL
;
1003 return __librbd_close(ctx
);
1007 #if defined(__linux__)
1009 krbd_read(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, char *buf
)
1013 n
= pread(ctx
->krbd_fd
, buf
, len
, off
);
1016 prt("pread(%llu, %zu) failed\n", off
, len
);
1024 krbd_write(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, const char *buf
)
1028 n
= pwrite(ctx
->krbd_fd
, buf
, len
, off
);
1031 prt("pwrite(%llu, %zu) failed\n", off
, len
);
1039 __krbd_flush(struct rbd_ctx
*ctx
, bool invalidate
)
1047 * BLKFLSBUF will sync the filesystem on top of the device (we
1048 * don't care about that here, since we write directly to it),
1049 * write out any dirty buffers and invalidate the buffer cache.
1050 * It won't do a hardware cache flush.
1052 * fsync() will write out any dirty buffers and do a hardware
1053 * cache flush (which we don't care about either, because for
1054 * krbd it's a noop). It won't try to empty the buffer cache
1055 * nor poke the filesystem before writing out.
1057 * Given that, for our purposes, fsync is a flush, while
1058 * BLKFLSBUF is a flush+invalidate.
1061 ret
= ioctl(ctx
->krbd_fd
, BLKFLSBUF
, NULL
);
1063 ret
= fsync(ctx
->krbd_fd
);
1066 prt("%s failed\n", invalidate
? "BLKFLSBUF" : "fsync");
1074 krbd_flush(struct rbd_ctx
*ctx
)
1076 return __krbd_flush(ctx
, false);
1080 krbd_discard(struct rbd_ctx
*ctx
, uint64_t off
, uint64_t len
)
1082 uint64_t range
[2] = { off
, len
};
1086 * BLKZEROOUT goes straight to disk and doesn't do anything
1087 * about dirty buffers. This means we need to flush so that
1092 * results in "data 0000 data" rather than "data data data" on
1093 * disk and invalidate so that
1098 * returns "data 0000 data" rather than "data data data" in
1099 * case 1..2M was cached.
1101 * Note: These cache coherency issues are supposed to be fixed
1102 * in recent kernels.
1104 ret
= __krbd_flush(ctx
, true);
1109 * off and len must be 512-byte aligned, otherwise BLKZEROOUT
1110 * will fail with -EINVAL. This means that -K (enable krbd
1111 * mode) requires -h 512 or similar.
1113 if (ioctl(ctx
->krbd_fd
, BLKZEROOUT
, &range
) < 0) {
1115 prt("BLKZEROOUT(%llu, %llu) failed\n", off
, len
);
1123 krbd_get_size(struct rbd_ctx
*ctx
, uint64_t *size
)
1127 if (ioctl(ctx
->krbd_fd
, BLKGETSIZE64
, &bytes
) < 0) {
1129 prt("BLKGETSIZE64 failed\n");
1139 krbd_resize(struct rbd_ctx
*ctx
, uint64_t size
)
1143 ceph_assert(size
% truncbdy
== 0);
1146 * When krbd detects a size change, it calls revalidate_disk(),
1147 * which ends up calling invalidate_bdev(), which invalidates
1148 * clean pages and does nothing about dirty pages beyond the
1149 * new size. The preceding cache flush makes sure those pages
1150 * are invalidated, which is what we need on shrink so that
1157 * returns "0000 0000" rather than "data 0000".
1159 ret
= __krbd_flush(ctx
, false);
1163 return __librbd_resize(ctx
, size
);
1167 krbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
1168 const char *dst_imagename
, int *order
, int stripe_unit
,
1173 ret
= __krbd_flush(ctx
, false);
1177 return __librbd_clone(ctx
, src_snapname
, dst_imagename
, order
,
1178 stripe_unit
, stripe_count
);
1182 krbd_flatten(struct rbd_ctx
*ctx
)
1186 ret
= __krbd_flush(ctx
, false);
1190 return __librbd_flatten(ctx
);
1194 #if defined(WITH_KRBD)
1195 const struct rbd_operations krbd_operations
= {
1210 #if defined(__linux__)
1212 nbd_open(const char *name
, struct rbd_ctx
*ctx
)
1219 SubProcess
process("rbd-nbd", SubProcess::KEEP
, SubProcess::PIPE
,
1221 process
.add_cmd_arg("map");
1222 process
.add_cmd_arg("--timeout=600");
1227 process
.add_cmd_arg(img
.c_str());
1229 r
= __librbd_open(name
, ctx
);
1233 r
= process
.spawn();
1235 prt("nbd_open failed to run rbd-nbd error: %s\n", process
.err().c_str());
1238 r
= safe_read(process
.get_stdout(), dev
, sizeof(dev
));
1240 prt("nbd_open failed to get nbd device path\n");
1243 for (int i
= 0; i
< r
; ++i
)
1244 if (dev
[i
] == 10 || dev
[i
] == 13)
1249 prt("rbd-nbd failed with error: %s", process
.err().c_str());
1253 devnode
= strdup(dev
);
1257 fd
= open(devnode
, O_RDWR
| o_direct
);
1260 prt("open(%s) failed\n", devnode
);
1264 ctx
->krbd_name
= devnode
;
1271 nbd_close(struct rbd_ctx
*ctx
)
1275 ceph_assert(ctx
->krbd_name
&& ctx
->krbd_fd
>= 0);
1277 if (close(ctx
->krbd_fd
) < 0) {
1279 prt("close(%s) failed\n", ctx
->krbd_name
);
1283 SubProcess
process("rbd-nbd");
1284 process
.add_cmd_arg("unmap");
1285 process
.add_cmd_arg(ctx
->krbd_name
);
1287 r
= process
.spawn();
1289 prt("nbd_close failed to run rbd-nbd error: %s\n", process
.err().c_str());
1294 prt("rbd-nbd failed with error: %d", process
.err().c_str());
1298 free((void *)ctx
->krbd_name
);
1300 ctx
->krbd_name
= NULL
;
1303 return __librbd_close(ctx
);
1307 nbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
1308 const char *dst_imagename
, int *order
, int stripe_unit
,
1313 ret
= __krbd_flush(ctx
, false);
1317 return __librbd_clone(ctx
, src_snapname
, dst_imagename
, order
,
1318 stripe_unit
, stripe_count
);
1321 const struct rbd_operations nbd_operations
= {
1336 #if defined(__FreeBSD__)
1338 ggate_open(const char *name
, struct rbd_ctx
*ctx
)
1345 SubProcess
process("rbd-ggate", SubProcess::KEEP
, SubProcess::PIPE
,
1347 process
.add_cmd_arg("map");
1352 process
.add_cmd_arg(img
.c_str());
1354 r
= __librbd_open(name
, ctx
);
1359 r
= process
.spawn();
1361 prt("ggate_open failed to run rbd-ggate: %s\n",
1362 process
.err().c_str());
1365 r
= safe_read(process
.get_stdout(), dev
, sizeof(dev
));
1367 prt("ggate_open failed to get ggate device path\n");
1370 for (int i
= 0; i
< r
; ++i
) {
1371 if (dev
[i
] == '\r' || dev
[i
] == '\n') {
1378 prt("rbd-ggate failed with error: %s", process
.err().c_str());
1382 devnode
= strdup(dev
);
1387 for (int i
= 0; i
< 100; i
++) {
1388 fd
= open(devnode
, O_RDWR
| o_direct
);
1389 if (fd
>= 0 || errno
!= ENOENT
) {
1396 prt("open(%s) failed\n", devnode
);
1400 ctx
->krbd_name
= devnode
;
1407 ggate_close(struct rbd_ctx
*ctx
)
1411 ceph_assert(ctx
->krbd_name
&& ctx
->krbd_fd
>= 0);
1413 if (close(ctx
->krbd_fd
) < 0) {
1415 prt("close(%s) failed\n", ctx
->krbd_name
);
1419 SubProcess
process("rbd-ggate");
1420 process
.add_cmd_arg("unmap");
1421 process
.add_cmd_arg(ctx
->krbd_name
);
1423 r
= process
.spawn();
1425 prt("ggate_close failed to run rbd-nbd: %s\n",
1426 process
.err().c_str());
1431 prt("rbd-ggate failed with error: %d", process
.err().c_str());
1435 free((void *)ctx
->krbd_name
);
1437 ctx
->krbd_name
= NULL
;
1440 return __librbd_close(ctx
);
1444 ggate_read(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, char *buf
)
1448 n
= pread(ctx
->krbd_fd
, buf
, len
, off
);
1451 prt("pread(%llu, %zu) failed\n", off
, len
);
1459 ggate_write(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, const char *buf
)
1463 n
= pwrite(ctx
->krbd_fd
, buf
, len
, off
);
1466 prt("pwrite(%llu, %zu) failed\n", off
, len
);
1474 __ggate_flush(struct rbd_ctx
*ctx
, bool invalidate
)
1483 ret
= ioctl(ctx
->krbd_fd
, DIOCGFLUSH
, NULL
);
1485 ret
= fsync(ctx
->krbd_fd
);
1489 prt("%s failed\n", invalidate
? "DIOCGFLUSH" : "fsync");
1497 ggate_flush(struct rbd_ctx
*ctx
)
1499 return __ggate_flush(ctx
, false);
1503 ggate_discard(struct rbd_ctx
*ctx
, uint64_t off
, uint64_t len
)
1505 off_t range
[2] = {static_cast<off_t
>(off
), static_cast<off_t
>(len
)};
1508 ret
= __ggate_flush(ctx
, true);
1513 if (ioctl(ctx
->krbd_fd
, DIOCGDELETE
, &range
) < 0) {
1515 prt("DIOCGDELETE(%llu, %llu) failed\n", off
, len
);
1523 ggate_get_size(struct rbd_ctx
*ctx
, uint64_t *size
)
1527 if (ioctl(ctx
->krbd_fd
, DIOCGMEDIASIZE
, &bytes
) < 0) {
1529 prt("DIOCGMEDIASIZE failed\n");
1539 ggate_resize(struct rbd_ctx
*ctx
, uint64_t size
)
1543 ceph_assert(size
% truncbdy
== 0);
1545 ret
= __ggate_flush(ctx
, false);
1550 return __librbd_resize(ctx
, size
);
1554 ggate_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
1555 const char *dst_imagename
, int *order
, int stripe_unit
,
1560 ret
= __ggate_flush(ctx
, false);
1565 return __librbd_clone(ctx
, src_snapname
, dst_imagename
, order
,
1566 stripe_unit
, stripe_count
);
1570 ggate_flatten(struct rbd_ctx
*ctx
)
1574 ret
= __ggate_flush(ctx
, false);
1579 return __librbd_flatten(ctx
);
1582 const struct rbd_operations ggate_operations
= {
1595 #endif // __FreeBSD__
1597 struct rbd_ctx ctx
= RBD_CTX_INIT
;
1598 const struct rbd_operations
*ops
= &librbd_operations
;
1600 static bool rbd_image_has_parent(struct rbd_ctx
*ctx
)
1603 rbd_linked_image_spec_t parent_image
;
1604 rbd_snap_spec_t parent_snap
;
1606 ret
= rbd_get_parent(ctx
->image
, &parent_image
, &parent_snap
);
1607 if (ret
< 0 && ret
!= -ENOENT
) {
1608 prterrcode("rbd_get_parent_info", ret
);
1611 rbd_linked_image_spec_cleanup(&parent_image
);
1612 rbd_snap_spec_cleanup(&parent_snap
);
1622 log4(int operation
, int arg0
, int arg1
, int arg2
)
1624 struct log_entry
*le
;
1626 le
= &oplog
[logptr
];
1627 le
->operation
= operation
;
1629 le
->operation
= ~ le
->operation
;
1635 if (logptr
>= LOGSIZE
)
1643 struct log_entry
*lp
;
1644 const char *falloc_type
[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
1646 prt("LOG DUMP (%d total operations):\n", logcount
);
1647 if (logcount
< LOGSIZE
) {
1654 for ( ; count
> 0; count
--) {
1657 opnum
= i
+1 + (logcount
/LOGSIZE
)*LOGSIZE
;
1658 prt("%d(%3d mod 256): ", opnum
, opnum
%256);
1660 if ((closeopen
= lp
->operation
< 0))
1661 lp
->operation
= ~ lp
->operation
;
1663 switch (lp
->operation
) {
1665 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
1666 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1668 if (badoff
>= lp
->args
[0] && badoff
<
1669 lp
->args
[0] + lp
->args
[1])
1670 prt("\t***RRRR***");
1673 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
1674 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1676 if (badoff
>= lp
->args
[0] && badoff
<
1677 lp
->args
[0] + lp
->args
[1])
1678 prt("\t******WWWW");
1681 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
1682 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1684 if (badoff
>= lp
->args
[0] &&
1685 badoff
< lp
->args
[0] + lp
->args
[1])
1686 prt("\t***RRRR***");
1689 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
1690 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1692 if (lp
->args
[0] > lp
->args
[2])
1694 else if (lp
->args
[0] + lp
->args
[1] > lp
->args
[2])
1696 if ((badoff
>= lp
->args
[0] || badoff
>=lp
->args
[2]) &&
1697 badoff
< lp
->args
[0] + lp
->args
[1])
1701 down
= lp
->args
[0] < lp
->args
[1];
1702 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
1703 down
? "DOWN" : "UP", lp
->args
[1], lp
->args
[0]);
1704 if (badoff
>= lp
->args
[!down
] &&
1705 badoff
< lp
->args
[!!down
])
1706 prt("\t******WWWW");
1709 /* 0: offset 1: length 2: where alloced */
1710 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) %s",
1711 lp
->args
[0], lp
->args
[0] + lp
->args
[1],
1712 lp
->args
[1], falloc_type
[lp
->args
[2]]);
1713 if (badoff
>= lp
->args
[0] &&
1714 badoff
< lp
->args
[0] + lp
->args
[1])
1715 prt("\t******FFFF");
1718 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
1719 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1721 if (badoff
>= lp
->args
[0] && badoff
<
1722 lp
->args
[0] + lp
->args
[1])
1723 prt("\t******PPPP");
1726 prt("WRITESAME 0x%x thru 0x%x\t(0x%x bytes) data_size 0x%x",
1727 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1728 lp
->args
[1], lp
->args
[2]);
1729 if (badoff
>= lp
->args
[0] &&
1730 badoff
< lp
->args
[0] + lp
->args
[1])
1731 prt("\t***WSWSWSWS");
1733 case OP_COMPARE_AND_WRITE
:
1734 prt("COMPARE_AND_WRITE 0x%x thru 0x%x\t(0x%x bytes)",
1735 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1737 if (lp
->args
[0] > lp
->args
[2])
1739 else if (lp
->args
[0] + lp
->args
[1] > lp
->args
[2])
1741 if ((badoff
>= lp
->args
[0] || badoff
>=lp
->args
[2]) &&
1742 badoff
< lp
->args
[0] + lp
->args
[1])
1752 prt("SKIPPED (no operation)");
1755 prt("BOGUS LOG ENTRY (operation code = %d)!",
1759 prt("\n\t\tCLOSE/OPEN");
1768 save_buffer(char *buffer
, off_t bufferlength
, int fd
)
1771 ssize_t byteswritten
;
1773 if (fd
<= 0 || bufferlength
== 0)
1776 if (bufferlength
> SSIZE_MAX
) {
1777 prt("fsx flaw: overflow in save_buffer\n");
1781 ret
= lseek(fd
, (off_t
)0, SEEK_SET
);
1782 if (ret
== (off_t
)-1)
1783 prterr("save_buffer: lseek 0");
1785 byteswritten
= write(fd
, buffer
, (size_t)bufferlength
);
1786 if (byteswritten
!= bufferlength
) {
1787 if (byteswritten
== -1)
1788 prterr("save_buffer write");
1790 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
1791 (unsigned)byteswritten
,
1792 (unsigned long long)bufferlength
);
1798 report_failure(int status
)
1804 save_buffer(good_buf
, file_size
, fsxgoodfd
);
1805 prt("Correct content saved for comparison\n");
1806 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
1811 sleep(3); // so the log can flush to disk. KLUDGEY!
1815 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
1816 *(((unsigned char *)(cp)) + 1)))
1819 fsxcmp(char *good_buf
, char *temp_buf
, unsigned size
)
1821 if (!skip_partial_discard
) {
1822 return memcmp(good_buf
, temp_buf
, size
);
1825 for (unsigned i
= 0; i
< size
; i
++) {
1826 if (good_buf
[i
] != temp_buf
[i
] && good_buf
[i
] != 0) {
1827 return good_buf
[i
] - temp_buf
[i
];
1834 check_buffers(char *good_buf
, char *temp_buf
, unsigned offset
, unsigned size
)
1836 if (fsxcmp(good_buf
+ offset
, temp_buf
, size
) != 0) {
1840 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
1841 offset
, size
, iname
);
1842 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
1844 unsigned char c
= good_buf
[offset
];
1845 unsigned char t
= temp_buf
[i
];
1848 unsigned bad
= short_at(&temp_buf
[i
]);
1849 prt("0x%5x\t0x%04x\t0x%04x", offset
,
1850 short_at(&good_buf
[offset
]), bad
);
1851 unsigned op
= temp_buf
[(offset
& 1) ? i
+1 : i
];
1852 prt("\t0x%5x\n", n
);
1854 prt("operation# (mod 256) for "
1855 "the bad data may be %u\n",
1856 ((unsigned)op
& 0xff));
1858 prt("operation# (mod 256) for "
1859 "the bad data unknown, check"
1860 " HOLE and EXTEND ops\n");
1869 report_failure(110);
1880 ret
= ops
->get_size(&ctx
, &size
);
1882 prterrcode("check_size: ops->get_size", ret
);
1884 if ((uint64_t)file_size
!= size
) {
1885 prt("Size error: expected 0x%llx stat 0x%llx\n",
1886 (unsigned long long)file_size
,
1887 (unsigned long long)size
);
1888 report_failure(120);
1892 #define TRUNC_HACK_SIZE (200ULL << 9) /* 512-byte aligned for krbd */
1895 check_trunc_hack(void)
1900 ret
= ops
->resize(&ctx
, 0ULL);
1902 prterrcode("check_trunc_hack: ops->resize pre", ret
);
1904 ret
= ops
->resize(&ctx
, TRUNC_HACK_SIZE
);
1906 prterrcode("check_trunc_hack: ops->resize actual", ret
);
1908 ret
= ops
->get_size(&ctx
, &size
);
1910 prterrcode("check_trunc_hack: ops->get_size", ret
);
1912 if (size
!= TRUNC_HACK_SIZE
) {
1913 prt("no extend on truncate! not posix!\n");
1917 ret
= ops
->resize(&ctx
, 0ULL);
1919 prterrcode("check_trunc_hack: ops->resize post", ret
);
1928 char client_name
[256];
1930 sprintf(client_name
, "client.%s", client_id
);
1932 r
= rados_create2(&cluster
, cluster_name
, client_name
, 0);
1934 simple_err("Could not create cluster handle", r
);
1937 rados_conf_parse_env(cluster
, NULL
);
1938 r
= rados_conf_read_file(cluster
, NULL
);
1940 simple_err("Error reading ceph config file", r
);
1941 goto failed_shutdown
;
1943 r
= rados_connect(cluster
);
1945 simple_err("Error connecting to cluster", r
);
1946 goto failed_shutdown
;
1948 #if defined(WITH_KRBD)
1949 r
= krbd_create_from_context(rados_cct(cluster
), &krbd
);
1951 simple_err("Could not create libkrbd handle", r
);
1952 goto failed_shutdown
;
1956 r
= rados_pool_create(cluster
, pool
);
1957 if (r
< 0 && r
!= -EEXIST
) {
1958 simple_err("Error creating pool", r
);
1961 r
= rados_ioctx_create(cluster
, pool
, &ioctx
);
1963 simple_err("Error creating ioctx", r
);
1966 rados_application_enable(ioctx
, "rbd", 1);
1968 if (clone_calls
|| journal_replay
) {
1970 r
= get_features(&features
);
1975 r
= rbd_create2(ioctx
, iname
, file_size
, features
, &order
);
1977 r
= rbd_create(ioctx
, iname
, file_size
, &order
);
1980 simple_err("Error creating image", r
);
1984 if (journal_replay
) {
1985 r
= register_journal(ioctx
, iname
);
1991 r
= rados_conf_get(cluster
, "rbd_skip_partial_discard", buf
,
1994 simple_err("Could not get rbd_skip_partial_discard value", r
);
1997 skip_partial_discard
= (strcmp(buf
, "true") == 0);
2002 rados_ioctx_destroy(ioctx
);
2004 #if defined(WITH_KRBD)
2008 rados_shutdown(cluster
);
2013 doflush(unsigned offset
, unsigned size
)
2020 ret
= ops
->flush(&ctx
);
2022 prterrcode("doflush: ops->flush", ret
);
2026 doread(unsigned offset
, unsigned size
)
2030 offset
-= offset
% readbdy
;
2032 size
-= size
% readbdy
;
2034 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
2035 prt("skipping zero size read\n");
2036 log4(OP_SKIPPED
, OP_READ
, offset
, size
);
2039 if (size
+ offset
> file_size
) {
2040 if (!quiet
&& testcalls
> simulatedopcount
)
2041 prt("skipping seek/read past end of file\n");
2042 log4(OP_SKIPPED
, OP_READ
, offset
, size
);
2046 log4(OP_READ
, offset
, size
, 0);
2048 if (testcalls
<= simulatedopcount
)
2052 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
2054 (monitorstart
== -1 ||
2055 (static_cast<long>(offset
+ size
) > monitorstart
&&
2056 (monitorend
== -1 ||
2057 static_cast<long>(offset
) <= monitorend
))))))
2058 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
2059 offset
, offset
+ size
- 1, size
);
2061 ret
= ops
->read(&ctx
, offset
, size
, temp_buf
);
2062 if (ret
!= (int)size
) {
2064 prterrcode("doread: ops->read", ret
);
2066 prt("short read: 0x%x bytes instead of 0x%x\n",
2068 report_failure(141);
2071 check_buffers(good_buf
, temp_buf
, offset
, size
);
2076 check_eofpage(char *s
, unsigned offset
, char *p
, int size
)
2078 unsigned long last_page
, should_be_zero
;
2080 if (offset
+ size
<= (file_size
& ~page_mask
))
2083 * we landed in the last page of the file
2084 * test to make sure the VM system provided 0's
2085 * beyond the true end of the file mapping
2086 * (as required by mmap def in 1996 posix 1003.1)
2088 last_page
= ((unsigned long)p
+ (offset
& page_mask
) + size
) & ~page_mask
;
2090 for (should_be_zero
= last_page
+ (file_size
& page_mask
);
2091 should_be_zero
< last_page
+ page_size
;
2093 if (*(char *)should_be_zero
) {
2094 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
2095 s
, file_size
- 1, should_be_zero
& page_mask
,
2096 short_at(should_be_zero
));
2097 report_failure(205);
2103 gendata(char *original_buf
, char *good_buf
, unsigned offset
, unsigned size
)
2106 good_buf
[offset
] = testcalls
% 256;
2108 good_buf
[offset
] += original_buf
[offset
];
2115 dowrite(unsigned offset
, unsigned size
)
2120 offset
-= offset
% writebdy
;
2122 size
-= size
% writebdy
;
2124 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
2125 prt("skipping zero size write\n");
2126 log4(OP_SKIPPED
, OP_WRITE
, offset
, size
);
2130 log4(OP_WRITE
, offset
, size
, file_size
);
2132 gendata(original_buf
, good_buf
, offset
, size
);
2133 if (file_size
< offset
+ size
) {
2134 newsize
= ceil(((double)offset
+ size
) / truncbdy
) * truncbdy
;
2135 if (file_size
< newsize
)
2136 memset(good_buf
+ file_size
, '\0', newsize
- file_size
);
2137 file_size
= newsize
;
2139 warn("Lite file size bug in fsx!");
2140 report_failure(149);
2142 ret
= ops
->resize(&ctx
, newsize
);
2144 prterrcode("dowrite: ops->resize", ret
);
2145 report_failure(150);
2149 if (testcalls
<= simulatedopcount
)
2153 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
2155 (monitorstart
== -1 ||
2156 (static_cast<long>(offset
+ size
) > monitorstart
&&
2157 (monitorend
== -1 ||
2158 static_cast<long>(offset
) <= monitorend
))))))
2159 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
2160 offset
, offset
+ size
- 1, size
);
2162 ret
= ops
->write(&ctx
, offset
, size
, good_buf
+ offset
);
2163 if (ret
!= (ssize_t
)size
) {
2165 prterrcode("dowrite: ops->write", ret
);
2167 prt("short write: 0x%x bytes instead of 0x%x\n",
2169 report_failure(151);
2173 doflush(offset
, size
);
2178 dotruncate(unsigned size
)
2180 int oldsize
= file_size
;
2183 size
-= size
% truncbdy
;
2184 if (size
> biggest
) {
2186 if (!quiet
&& testcalls
> simulatedopcount
)
2187 prt("truncating to largest ever: 0x%x\n", size
);
2190 log4(OP_TRUNCATE
, size
, (unsigned)file_size
, 0);
2192 if (size
> file_size
)
2193 memset(good_buf
+ file_size
, '\0', size
- file_size
);
2194 else if (size
< file_size
)
2195 memset(good_buf
+ size
, '\0', file_size
- size
);
2198 if (testcalls
<= simulatedopcount
)
2201 if ((progressinterval
&& testcalls
% progressinterval
== 0) ||
2202 (debug
&& (monitorstart
== -1 || monitorend
== -1 ||
2203 (long)size
<= monitorend
)))
2204 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls
, oldsize
, size
);
2206 ret
= ops
->resize(&ctx
, size
);
2208 prterrcode("dotruncate: ops->resize", ret
);
2209 report_failure(160);
2214 do_punch_hole(unsigned offset
, unsigned length
)
2216 unsigned end_offset
;
2221 offset
-= offset
% holebdy
;
2222 length
-= length
% holebdy
;
2224 if (!quiet
&& testcalls
> simulatedopcount
)
2225 prt("skipping zero length punch hole\n");
2226 log4(OP_SKIPPED
, OP_PUNCH_HOLE
, offset
, length
);
2230 if (file_size
<= (loff_t
)offset
) {
2231 if (!quiet
&& testcalls
> simulatedopcount
)
2232 prt("skipping hole punch off the end of the file\n");
2233 log4(OP_SKIPPED
, OP_PUNCH_HOLE
, offset
, length
);
2237 end_offset
= offset
+ length
;
2239 log4(OP_PUNCH_HOLE
, offset
, length
, 0);
2241 if (testcalls
<= simulatedopcount
)
2244 if ((progressinterval
&& testcalls
% progressinterval
== 0) ||
2245 (debug
&& (monitorstart
== -1 || monitorend
== -1 ||
2246 (long)end_offset
<= monitorend
))) {
2247 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls
,
2248 offset
, offset
+length
, length
);
2251 ret
= ops
->discard(&ctx
, (unsigned long long)offset
,
2252 (unsigned long long)length
);
2254 prterrcode("do_punch_hole: ops->discard", ret
);
2255 report_failure(161);
2258 max_offset
= offset
< file_size
? offset
: file_size
;
2259 max_len
= max_offset
+ length
<= file_size
? length
:
2260 file_size
- max_offset
;
2261 memset(good_buf
+ max_offset
, '\0', max_len
);
2264 unsigned get_data_size(unsigned size
)
2268 unsigned max
= sqrt((double)size
) + 1;
2270 unsigned curr
= good
;
2272 hint
= get_random() % max
;
2274 for (i
= 1; i
< max
&& curr
< hint
; i
++) {
2275 if (size
% i
== 0) {
2288 dowritesame(unsigned offset
, unsigned size
)
2296 offset
-= offset
% writebdy
;
2298 size
-= size
% writebdy
;
2300 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
2301 prt("skipping zero size writesame\n");
2302 log4(OP_SKIPPED
, OP_WRITESAME
, offset
, size
);
2306 data_size
= get_data_size(size
);
2308 log4(OP_WRITESAME
, offset
, size
, data_size
);
2310 gendata(original_buf
, good_buf
, offset
, data_size
);
2311 if (file_size
< offset
+ size
) {
2312 newsize
= ceil(((double)offset
+ size
) / truncbdy
) * truncbdy
;
2313 if (file_size
< newsize
)
2314 memset(good_buf
+ file_size
, '\0', newsize
- file_size
);
2315 file_size
= newsize
;
2317 warn("Lite file size bug in fsx!");
2318 report_failure(162);
2320 ret
= ops
->resize(&ctx
, newsize
);
2322 prterrcode("dowritesame: ops->resize", ret
);
2323 report_failure(163);
2327 for (n
= size
/ data_size
, buf_off
= data_size
; n
> 1; n
--) {
2328 memcpy(good_buf
+ offset
+ buf_off
, good_buf
+ offset
, data_size
);
2329 buf_off
+= data_size
;
2332 if (testcalls
<= simulatedopcount
)
2336 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
2338 (monitorstart
== -1 ||
2339 (static_cast<long>(offset
+ size
) > monitorstart
&&
2340 (monitorend
== -1 ||
2341 static_cast<long>(offset
) <= monitorend
))))))
2342 prt("%lu writesame\t0x%x thru\t0x%x\tdata_size\t0x%x(0x%x bytes)\n", testcalls
,
2343 offset
, offset
+ size
- 1, data_size
, size
);
2345 ret
= ops
->writesame(&ctx
, offset
, size
, good_buf
+ offset
, data_size
);
2346 if (ret
!= (ssize_t
)size
) {
2348 prterrcode("dowritesame: ops->writesame", ret
);
2350 prt("short writesame: 0x%x bytes instead of 0x%x\n",
2352 report_failure(164);
2356 doflush(offset
, size
);
2360 docompareandwrite(unsigned offset
, unsigned size
)
2364 if (skip_partial_discard
) {
2365 if (!quiet
&& testcalls
> simulatedopcount
)
2366 prt("compare and write disabled\n");
2367 log4(OP_SKIPPED
, OP_COMPARE_AND_WRITE
, offset
, size
);
2371 offset
-= offset
% writebdy
;
2373 size
-= size
% writebdy
;
2376 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
2377 prt("skipping zero size read\n");
2378 log4(OP_SKIPPED
, OP_READ
, offset
, size
);
2382 if (size
+ offset
> file_size
) {
2383 if (!quiet
&& testcalls
> simulatedopcount
)
2384 prt("skipping seek/compare past end of file\n");
2385 log4(OP_SKIPPED
, OP_COMPARE_AND_WRITE
, offset
, size
);
2389 memcpy(temp_buf
+ offset
, good_buf
+ offset
, size
);
2390 gendata(original_buf
, good_buf
, offset
, size
);
2391 log4(OP_COMPARE_AND_WRITE
, offset
, size
, 0);
2393 if (testcalls
<= simulatedopcount
)
2397 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
2399 (monitorstart
== -1 ||
2400 (static_cast<long>(offset
+ size
) > monitorstart
&&
2401 (monitorend
== -1 ||
2402 static_cast<long>(offset
) <= monitorend
))))))
2403 prt("%lu compareandwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
2404 offset
, offset
+ size
- 1, size
);
2406 ret
= ops
->compare_and_write(&ctx
, offset
, size
, temp_buf
+ offset
,
2408 if (ret
!= (ssize_t
)size
) {
2409 if (ret
== -EINVAL
) {
2410 memcpy(good_buf
+ offset
, temp_buf
+ offset
, size
);
2414 prterrcode("docompareandwrite: ops->compare_and_write", ret
);
2416 prt("short write: 0x%x bytes instead of 0x%x\n", ret
, size
);
2417 report_failure(151);
2422 doflush(offset
, size
);
2425 void clone_filename(char *buf
, size_t len
, int clones
)
2427 #if __GNUC__ && __GNUC__ >= 8
2428 #pragma GCC diagnostic push
2429 #pragma GCC diagnostic ignored "-Wformat-truncation"
2431 snprintf(buf
, len
, "%s/fsx-%s-parent%d",
2432 dirpath
, iname
, clones
);
2433 #if __GNUC__ && __GNUC__ >= 8
2434 #pragma GCC diagnostic pop
2438 void clone_imagename(char *buf
, size_t len
, int clones
)
2441 snprintf(buf
, len
, "%s-clone%d", iname
, clones
);
2443 strncpy(buf
, iname
, len
);
2444 buf
[len
- 1] = '\0';
2447 void replay_imagename(char *buf
, size_t len
, int clones
)
2449 clone_imagename(buf
, len
, clones
);
2450 strncat(buf
, "-replay", len
- strlen(buf
));
2451 buf
[len
- 1] = '\0';
2454 void check_clone(int clonenum
, bool replay_image
);
2459 char filename
[1024];
2460 char imagename
[1024];
2461 char lastimagename
[1024];
2463 int order
= 0, stripe_unit
= 0, stripe_count
= 0;
2464 uint64_t newsize
= file_size
;
2466 log4(OP_CLONE
, 0, 0, 0);
2469 if (randomize_striping
) {
2470 order
= 18 + get_random() % 8;
2471 stripe_unit
= 1ull << (order
- 1 - (get_random() % 8));
2472 stripe_count
= 2 + get_random() % 14;
2475 prt("%lu clone\t%d order %d su %d sc %d\n", testcalls
, num_clones
,
2476 order
, stripe_unit
, stripe_count
);
2478 clone_imagename(imagename
, sizeof(imagename
), num_clones
);
2479 clone_imagename(lastimagename
, sizeof(lastimagename
),
2481 ceph_assert(strcmp(lastimagename
, ctx
.name
) == 0);
2483 ret
= ops
->clone(&ctx
, "snap", imagename
, &order
, stripe_unit
,
2486 prterrcode("do_clone: ops->clone", ret
);
2490 if (randomize_parent_overlap
&& rbd_image_has_parent(&ctx
)) {
2491 int rand
= get_random() % 16 + 1; // [1..16]
2496 ret
= rbd_get_overlap(ctx
.image
, &overlap
);
2498 prterrcode("do_clone: rbd_get_overlap", ret
);
2502 if (rand
< 10) { // 9/16
2503 newsize
= overlap
* ((double)rand
/ 10);
2504 newsize
-= newsize
% truncbdy
;
2509 ceph_assert(newsize
!= (uint64_t)file_size
);
2510 prt("truncating image %s from 0x%llx (overlap 0x%llx) to 0x%llx\n",
2511 ctx
.name
, file_size
, overlap
, newsize
);
2513 ret
= ops
->resize(&ctx
, newsize
);
2515 prterrcode("do_clone: ops->resize", ret
);
2518 } else if (rand
< 15) { // 2/16
2519 prt("flattening image %s\n", ctx
.name
);
2521 ret
= ops
->flatten(&ctx
);
2523 prterrcode("do_clone: ops->flatten", ret
);
2527 prt("leaving image %s intact\n", ctx
.name
);
2531 clone_filename(filename
, sizeof(filename
), num_clones
);
2532 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666)) < 0) {
2533 simple_err("do_clone: open", -errno
);
2536 save_buffer(good_buf
, newsize
, fd
);
2537 if ((ret
= close(fd
)) < 0) {
2538 simple_err("do_clone: close", -errno
);
2545 if ((ret
= ops
->close(&ctx
)) < 0) {
2546 prterrcode("do_clone: ops->close", ret
);
2550 if (journal_replay
) {
2551 ret
= finalize_journal(ioctx
, lastimagename
, num_clones
- 1,
2552 order
, stripe_unit
, stripe_count
);
2557 ret
= register_journal(ioctx
, imagename
);
2564 * Open freshly made clone.
2566 if ((ret
= ops
->open(imagename
, &ctx
)) < 0) {
2567 prterrcode("do_clone: ops->open", ret
);
2571 if (num_clones
> 1) {
2572 if (journal_replay
) {
2573 check_clone(num_clones
- 2, true);
2575 check_clone(num_clones
- 2, false);
2580 check_clone(int clonenum
, bool replay_image
)
2583 char imagename
[128];
2585 struct rbd_ctx cur_ctx
= RBD_CTX_INIT
;
2586 struct stat file_info
;
2587 char *good_buf
, *temp_buf
;
2590 replay_imagename(imagename
, sizeof(imagename
), clonenum
);
2592 clone_imagename(imagename
, sizeof(imagename
), clonenum
);
2595 if ((ret
= ops
->open(imagename
, &cur_ctx
)) < 0) {
2596 prterrcode("check_clone: ops->open", ret
);
2600 clone_filename(filename
, sizeof(filename
), clonenum
+ 1);
2601 if ((fd
= open(filename
, O_RDONLY
)) < 0) {
2602 simple_err("check_clone: open", -errno
);
2606 prt("checking clone #%d, image %s against file %s\n",
2607 clonenum
, imagename
, filename
);
2608 if ((ret
= fstat(fd
, &file_info
)) < 0) {
2609 simple_err("check_clone: fstat", -errno
);
2614 ret
= posix_memalign((void **)&good_buf
,
2615 std::max(writebdy
, (int)sizeof(void *)),
2618 prterrcode("check_clone: posix_memalign(good_buf)", -ret
);
2623 ret
= posix_memalign((void **)&temp_buf
,
2624 std::max(readbdy
, (int)sizeof(void *)),
2627 prterrcode("check_clone: posix_memalign(temp_buf)", -ret
);
2631 if ((ret
= pread(fd
, good_buf
, file_info
.st_size
, 0)) < 0) {
2632 simple_err("check_clone: pread", -errno
);
2635 if ((ret
= ops
->read(&cur_ctx
, 0, file_info
.st_size
, temp_buf
)) < 0) {
2636 prterrcode("check_clone: ops->read", ret
);
2640 if ((ret
= ops
->close(&cur_ctx
)) < 0) {
2641 prterrcode("check_clone: ops->close", ret
);
2644 check_buffers(good_buf
, temp_buf
, 0, file_info
.st_size
);
2646 if (!replay_image
) {
2659 ret
= ops
->write(&ctx
, 0, file_size
, good_buf
);
2660 if (ret
!= file_size
) {
2662 prterrcode("writefileimage: ops->write", ret
);
2664 prt("short write: 0x%x bytes instead of 0x%llx\n",
2665 ret
, (unsigned long long)file_size
);
2666 report_failure(172);
2670 ret
= ops
->resize(&ctx
, file_size
);
2672 prterrcode("writefileimage: ops->resize", ret
);
2673 report_failure(173);
2683 if (!rbd_image_has_parent(&ctx
)) {
2684 log4(OP_SKIPPED
, OP_FLATTEN
, 0, 0);
2687 log4(OP_FLATTEN
, 0, 0, 0);
2688 prt("%lu flatten\n", testcalls
);
2690 ret
= ops
->flatten(&ctx
);
2692 prterrcode("writefileimage: ops->flatten", ret
);
2703 if (testcalls
<= simulatedopcount
)
2706 name
= strdup(ctx
.name
);
2709 prt("%lu close/open\n", testcalls
);
2711 ret
= ops
->close(&ctx
);
2713 prterrcode("docloseopen: ops->close", ret
);
2714 report_failure(180);
2717 ret
= ops
->open(name
, &ctx
);
2719 prterrcode("docloseopen: ops->open", ret
);
2720 report_failure(181);
2726 #define TRIM_OFF_LEN(off, len, size) \
2732 if ((unsigned)(off) + (unsigned)(len) > (unsigned)(size)) \
2733 (len) = (size) - (off); \
2739 unsigned long offset
;
2740 unsigned long size
= maxoplen
;
2741 unsigned long rv
= get_random();
2744 if (simulatedopcount
> 0 && testcalls
== simulatedopcount
)
2750 closeopen
= (rv
>> 3) < (1u << 28) / (unsigned)closeprob
;
2752 if (debugstart
> 0 && testcalls
>= debugstart
)
2755 if (!quiet
&& testcalls
< simulatedopcount
&& testcalls
% 100000 == 0)
2756 prt("%lu...\n", testcalls
);
2758 offset
= get_random();
2760 size
= get_random() % (maxoplen
+ 1);
2762 /* calculate appropriate op to run */
2764 op
= rv
% OP_MAX_LITE
;
2766 op
= rv
% OP_MAX_FULL
;
2778 if (!fallocate_calls
) {
2779 log4(OP_SKIPPED
, OP_FALLOCATE
, offset
, size
);
2784 if (!punch_hole_calls
) {
2785 log4(OP_SKIPPED
, OP_PUNCH_HOLE
, offset
, size
);
2790 /* clone, 8% chance */
2791 if (!clone_calls
|| file_size
== 0 || get_random() % 100 >= 8) {
2792 log4(OP_SKIPPED
, OP_CLONE
, 0, 0);
2797 /* flatten four times as rarely as clone, 2% chance */
2798 if (get_random() % 100 >= 2) {
2799 log4(OP_SKIPPED
, OP_FLATTEN
, 0, 0);
2804 /* writesame not implemented */
2805 if (!ops
->writesame
) {
2806 log4(OP_SKIPPED
, OP_WRITESAME
, offset
, size
);
2810 case OP_COMPARE_AND_WRITE
:
2811 /* compare_and_write not implemented */
2812 if (!ops
->compare_and_write
) {
2813 log4(OP_SKIPPED
, OP_COMPARE_AND_WRITE
, offset
, size
);
2821 TRIM_OFF_LEN(offset
, size
, file_size
);
2822 doread(offset
, size
);
2826 TRIM_OFF_LEN(offset
, size
, maxfilelen
);
2827 dowrite(offset
, size
);
2831 TRIM_OFF_LEN(offset
, size
, file_size
);
2836 TRIM_OFF_LEN(offset
, size
, maxfilelen
);
2842 size
= get_random() % maxfilelen
;
2847 TRIM_OFF_LEN(offset
, size
, file_size
);
2848 do_punch_hole(offset
, size
);
2852 TRIM_OFF_LEN(offset
, size
, maxfilelen
);
2853 dowritesame(offset
, size
);
2855 case OP_COMPARE_AND_WRITE
:
2856 TRIM_OFF_LEN(offset
, size
, file_size
);
2857 docompareandwrite(offset
, size
);
2869 prterr("test: unknown operation");
2875 if (sizechecks
&& testcalls
> simulatedopcount
)
2886 prt("signal %d\n", sig
);
2887 prt("testcalls = %lu\n", testcalls
);
2895 fprintf(stdout
, "usage: %s",
2896 "fsx [-dfjknqxyACFHKLORUWZ] [-b opnum] [-c Prob] [-h holebdy] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] pname iname\n\
2897 -b opnum: beginning operation number (default 1)\n\
2898 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
2899 -d: debug output for all operations\n\
2900 -f: flush and invalidate cache after I/O\n\
2901 -g: deep copy instead of clone\n\
2902 -h holebdy: 4096 would make discards page aligned (default 1)\n\
2903 -j: journal replay stress test\n\
2904 -k: keep data on success (default 0)\n\
2905 -l flen: the upper bound on file size (default 262144)\n\
2906 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
2907 -n: no verifications of file size\n\
2908 -o oplen: the upper bound on operation size (default 65536)\n\
2909 -p progressinterval: debug output at specified operation interval\n\
2910 -q: quieter operation\n\
2911 -r readbdy: 4096 would make reads page aligned (default 1)\n\
2912 -s style: 1 gives smaller truncates (default 0)\n\
2913 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
2914 -w writebdy: 4096 would make writes page aligned (default 1)\n\
2915 -x: preallocate file space before starting, XFS only (default 0)\n\
2916 -y: synchronize changes to a file\n"
2918 " -C: do not use clone calls\n\
2919 -D startingop: debug output starting at specified operation\n"
2921 " -F: Do not use fallocate (preallocation) calls\n"
2923 #if defined(__FreeBSD__)
2924 " -G: enable rbd-ggate mode (use -L, -r and -w too)\n"
2926 " -H: do not use punch hole calls\n"
2927 #if defined(WITH_KRBD)
2928 " -K: enable krbd mode (use -t and -h too)\n"
2930 #if defined(__linux__)
2931 " -M: enable rbd-nbd mode (use -t and -h too)\n"
2933 " -L: fsxLite - no file creations & no file size changes\n\
2934 -N numops: total # operations to do (default infinity)\n\
2935 -O: use oplen (see -o flag) for every op (default random)\n\
2936 -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
2937 -R: read() system calls only (mapped reads disabled)\n\
2938 -S seed: for random # generator (default 1) 0 gets timestamp\n\
2939 -U: disable randomized striping\n\
2940 -W: mapped write operations DISabled\n\
2941 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
2942 poolname: this is REQUIRED (no default)\n\
2943 imagename: this is REQUIRED (no default)\n");
2949 getnum(char *s
, char **e
)
2954 ret
= strtol(s
, e
, 0);
2985 if (!lite
&& fallocate_calls
) {
2986 if (fallocate(fd
, 0, 0, 1) && errno
== EOPNOTSUPP
) {
2988 warn("main: filesystem does not support fallocate, disabling\n");
2989 fallocate_calls
= 0;
2994 #else /* ! FALLOCATE */
2995 fallocate_calls
= 0;
3000 void remove_image(rados_ioctx_t ioctx
, char *imagename
, bool remove_snap
,
3006 if ((ret
= rbd_open(ioctx
, imagename
, &image
, NULL
)) < 0) {
3007 sprintf(errmsg
, "rbd_open %s", imagename
);
3008 prterrcode(errmsg
, ret
);
3009 report_failure(101);
3012 if ((ret
= rbd_snap_unprotect(image
, "snap")) < 0) {
3013 sprintf(errmsg
, "rbd_snap_unprotect %s@snap",
3015 prterrcode(errmsg
, ret
);
3016 report_failure(102);
3018 if ((ret
= rbd_snap_remove(image
, "snap")) < 0) {
3019 sprintf(errmsg
, "rbd_snap_remove %s@snap",
3021 prterrcode(errmsg
, ret
);
3022 report_failure(103);
3025 if ((ret
= rbd_close(image
)) < 0) {
3026 sprintf(errmsg
, "rbd_close %s", imagename
);
3027 prterrcode(errmsg
, ret
);
3028 report_failure(104);
3032 (ret
= unregister_journal(ioctx
, imagename
)) < 0) {
3033 report_failure(105);
3036 if ((ret
= rbd_remove(ioctx
, imagename
)) < 0) {
3037 sprintf(errmsg
, "rbd_remove %s", imagename
);
3038 prterrcode(errmsg
, ret
);
3039 report_failure(106);
3044 main(int argc
, char **argv
)
3047 LONG_OPT_CLUSTER
= 1000,
3051 int i
, style
, ch
, ret
;
3053 char goodfile
[1024];
3056 const char* optstring
= "b:c:dfgh:jkl:m:no:p:qr:s:t:w:xyCD:FGHKMLN:OP:RS:UWZ";
3057 const struct option longopts
[] = {
3058 {"cluster", 1, NULL
, LONG_OPT_CLUSTER
},
3059 {"id", 1, NULL
, LONG_OPT_ID
}};
3064 page_size
= getpagesize();
3065 page_mask
= page_size
- 1;
3066 mmap_mask
= page_mask
;
3068 setvbuf(stdout
, (char *)0, _IOLBF
, 0); /* line buffered stdout */
3070 while ((ch
= getopt_long(argc
, argv
, optstring
, longopts
, NULL
)) != EOF
) {
3072 case LONG_OPT_CLUSTER
:
3073 cluster_name
= optarg
;
3079 simulatedopcount
= getnum(optarg
, &endp
);
3081 fprintf(stdout
, "Will begin at operation %lu\n",
3083 if (simulatedopcount
== 0)
3085 simulatedopcount
-= 1;
3088 closeprob
= getnum(optarg
, &endp
);
3091 "Chance of close/open is 1 in %d\n",
3106 holebdy
= getnum(optarg
, &endp
);
3111 journal_replay
= true;
3114 keep_on_success
= 1;
3118 int _num
= getnum(optarg
, &endp
);
3125 monitorstart
= getnum(optarg
, &endp
);
3126 if (monitorstart
< 0)
3128 if (!endp
|| *endp
++ != ':')
3130 monitorend
= getnum(endp
, &endp
);
3133 if (monitorend
== 0)
3134 monitorend
= -1; /* aka infinity */
3141 maxoplen
= getnum(optarg
, &endp
);
3146 progressinterval
= getnum(optarg
, &endp
);
3147 if (progressinterval
== 0)
3154 readbdy
= getnum(optarg
, &endp
);
3159 style
= getnum(optarg
, &endp
);
3160 if (style
< 0 || style
> 1)
3164 truncbdy
= getnum(optarg
, &endp
);
3169 writebdy
= getnum(optarg
, &endp
);
3183 debugstart
= getnum(optarg
, &endp
);
3188 fallocate_calls
= 0;
3190 #if defined(__FreeBSD__)
3192 prt("rbd-ggate mode enabled\n");
3193 ops
= &ggate_operations
;
3197 punch_hole_calls
= 0;
3199 #if defined(WITH_KRBD)
3201 prt("krbd mode enabled\n");
3202 ops
= &krbd_operations
;
3205 #if defined(__linux__)
3207 prt("rbd-nbd mode enabled\n");
3208 ops
= &nbd_operations
;
3215 numops
= getnum(optarg
, &endp
);
3223 strncpy(dirpath
, optarg
, sizeof(dirpath
)-1);
3224 dirpath
[sizeof(dirpath
)-1] = '\0';
3225 strncpy(goodfile
, dirpath
, sizeof(goodfile
)-1);
3226 goodfile
[sizeof(goodfile
)-1] = '\0';
3227 if (strlen(goodfile
) < sizeof(goodfile
)-2) {
3228 strcat(goodfile
, "/");
3230 prt("file name to long\n");
3233 strncpy(logfile
, dirpath
, sizeof(logfile
)-1);
3234 logfile
[sizeof(logfile
)-1] = '\0';
3235 if (strlen(logfile
) < sizeof(logfile
)-2) {
3236 strcat(logfile
, "/");
3238 prt("file path to long\n");
3245 fprintf(stdout
, "mapped reads DISABLED\n");
3248 seed
= getnum(optarg
, &endp
);
3250 seed
= std::random_device()() % 10000;
3252 fprintf(stdout
, "Seed set to %d\n", seed
);
3257 randomize_striping
= 0;
3262 fprintf(stdout
, "mapped writes DISABLED\n");
3265 o_direct
= O_DIRECT
;
3279 signal(SIGHUP
, cleanup
);
3280 signal(SIGINT
, cleanup
);
3281 signal(SIGPIPE
, cleanup
);
3282 signal(SIGALRM
, cleanup
);
3283 signal(SIGTERM
, cleanup
);
3284 signal(SIGXCPU
, cleanup
);
3285 signal(SIGXFSZ
, cleanup
);
3286 signal(SIGVTALRM
, cleanup
);
3287 signal(SIGUSR1
, cleanup
);
3288 signal(SIGUSR2
, cleanup
);
3290 random_generator
.seed(seed
);
3293 file_size
= maxfilelen
;
3296 ret
= create_image();
3298 prterrcode(iname
, ret
);
3301 ret
= ops
->open(iname
, &ctx
);
3303 simple_err("Error opening image", ret
);
3307 strcat(dirpath
, ".");
3308 strncat(goodfile
, iname
, 256);
3309 strcat (goodfile
, ".fsxgood");
3310 fsxgoodfd
= open(goodfile
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
3311 if (fsxgoodfd
< 0) {
3315 strncat(logfile
, iname
, 256);
3316 strcat (logfile
, ".fsxlog");
3317 fsxlogf
= fopen(logfile
, "w");
3318 if (fsxlogf
== NULL
) {
3323 original_buf
= (char *) malloc(maxfilelen
);
3324 for (i
= 0; i
< (int)maxfilelen
; i
++)
3325 original_buf
[i
] = get_random() % 256;
3327 ret
= posix_memalign((void **)&good_buf
,
3328 std::max(writebdy
, (int)sizeof(void *)), maxfilelen
);
3331 prt("writebdy is not a suitable power of two\n");
3333 prterrcode("main: posix_memalign(good_buf)", -ret
);
3336 memset(good_buf
, '\0', maxfilelen
);
3338 ret
= posix_memalign((void **)&temp_buf
,
3339 std::max(readbdy
, (int)sizeof(void *)), maxfilelen
);
3342 prt("readbdy is not a suitable power of two\n");
3344 prterrcode("main: posix_memalign(temp_buf)", -ret
);
3347 memset(temp_buf
, '\0', maxfilelen
);
3349 if (lite
) { /* zero entire existing file */
3352 written
= ops
->write(&ctx
, 0, (size_t)maxfilelen
, good_buf
);
3353 if (written
!= (ssize_t
)maxfilelen
) {
3355 prterrcode(iname
, written
);
3356 warn("main: error on write");
3358 warn("main: short write, 0x%x bytes instead "
3369 while (numops
== -1 || numops
--)
3372 ret
= ops
->close(&ctx
);
3374 prterrcode("ops->close", ret
);
3378 if (journal_replay
) {
3379 char imagename
[1024];
3380 clone_imagename(imagename
, sizeof(imagename
), num_clones
);
3381 ret
= finalize_journal(ioctx
, imagename
, num_clones
, 0, 0, 0);
3383 report_failure(100);
3387 if (num_clones
> 0) {
3388 if (journal_replay
) {
3389 check_clone(num_clones
- 1, true);
3391 check_clone(num_clones
- 1, false);
3394 if (!keep_on_success
) {
3395 while (num_clones
>= 0) {
3396 static bool remove_snap
= false;
3398 if (journal_replay
) {
3399 char replayimagename
[1024];
3400 replay_imagename(replayimagename
,
3401 sizeof(replayimagename
),
3403 remove_image(ioctx
, replayimagename
,
3408 char clonename
[128];
3409 clone_imagename(clonename
, 128, num_clones
);
3410 remove_image(ioctx
, clonename
, remove_snap
,
3418 prt("All operations completed A-OK!\n");
3421 rados_ioctx_destroy(ioctx
);
3422 #if defined(WITH_KRBD)
3425 rados_shutdown(cluster
);