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>
27 #include <sys/ioctl.h>
44 #include "include/intarith.h"
45 #include "include/krbd.h"
46 #include "include/rados/librados.h"
47 #include "include/rados/librados.hpp"
48 #include "include/rbd/librbd.h"
49 #include "include/rbd/librbd.hpp"
50 #include "common/Cond.h"
51 #include "common/SubProcess.h"
52 #include "common/safe_io.h"
53 #include "journal/Journaler.h"
54 #include "journal/ReplayEntry.h"
55 #include "journal/ReplayHandler.h"
56 #include "journal/Settings.h"
58 #include <boost/scope_exit.hpp>
60 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
63 * A log entry is an operation and a bunch of arguments.
73 struct log_entry oplog
[LOGSIZE
]; /* the log */
74 int logptr
= 0; /* current position in log */
75 int logcount
= 0; /* total ops */
78 * The operation matrix is complex due to conditional execution of different
79 * features. Hence when we come to deciding what operation to run, we need to
80 * be careful in how we select the different operations. The active operations
81 * are mapped to numbers as follows:
93 * When mapped read/writes are disabled, they are simply converted to normal
94 * reads and writes. When fallocate/fpunch calls are disabled, they are
95 * converted to OP_SKIPPED. Hence OP_SKIPPED needs to have a number higher than
96 * the operation selction matrix, as does the OP_CLOSEOPEN which is an
97 * operation modifier rather than an operation in itself.
99 * Because of the "lite" version, we also need to have different "maximum
100 * operation" defines to allow the ops to be selected correctly based on the
104 /* common operations */
108 #define OP_MAPWRITE 3
109 #define OP_MAX_LITE 4
111 /* !lite operations */
112 #define OP_TRUNCATE 4
113 #define OP_FALLOCATE 5
114 #define OP_PUNCH_HOLE 6
115 #define OP_WRITESAME 7
116 /* rbd-specific operations */
119 #define OP_MAX_FULL 10
121 /* operation modifiers */
122 #define OP_CLOSEOPEN 100
123 #define OP_SKIPPED 101
126 #define PAGE_SIZE getpagesize()
128 #define PAGE_MASK (PAGE_SIZE - 1)
131 char *original_buf
; /* a pointer to the original data */
132 char *good_buf
; /* a pointer to the correct data */
133 char *temp_buf
; /* a pointer to the current data */
139 unsigned long testcalls
= 0; /* calls to function "test" */
141 unsigned long simulatedopcount
= 0; /* -b flag */
142 int closeprob
= 0; /* -c flag */
143 int debug
= 0; /* -d flag */
144 unsigned long debugstart
= 0; /* -D flag */
145 int flush_enabled
= 0; /* -f flag */
146 int holebdy
= 1; /* -h flag */
147 bool journal_replay
= false; /* -j flah */
148 int keep_on_success
= 0; /* -k flag */
149 int do_fsync
= 0; /* -y flag */
150 unsigned long maxfilelen
= 256 * 1024; /* -l flag */
151 int sizechecks
= 1; /* -n flag disables them */
152 int maxoplen
= 64 * 1024; /* -o flag */
153 int quiet
= 0; /* -q flag */
154 unsigned long progressinterval
= 0; /* -p flag */
155 int readbdy
= 1; /* -r flag */
156 int style
= 0; /* -s flag */
157 int prealloc
= 0; /* -x flag */
158 int truncbdy
= 1; /* -t flag */
159 int writebdy
= 1; /* -w flag */
160 long monitorstart
= -1; /* -m flag */
161 long monitorend
= -1; /* -m flag */
162 int lite
= 0; /* -L flag */
163 long numops
= -1; /* -N flag */
164 int randomoplen
= 1; /* -O flag disables it */
165 int seed
= 1; /* -S flag */
166 int mapped_writes
= 0; /* -W flag disables */
167 int fallocate_calls
= 0; /* -F flag disables */
168 int punch_hole_calls
= 1; /* -H flag disables */
169 int clone_calls
= 1; /* -C flag disables */
170 int randomize_striping
= 1; /* -U flag disables */
171 int randomize_parent_overlap
= 1;
172 int mapped_reads
= 0; /* -R flag disables it */
174 int o_direct
= 0; /* -Z flag */
182 FILE * fsxlogf
= NULL
;
187 vwarnc(int code
, const char *fmt
, va_list ap
) {
188 fprintf(stderr
, "fsx: ");
190 vfprintf(stderr
, fmt
, ap
);
191 fprintf(stderr
, ": ");
193 fprintf(stderr
, "%s\n", strerror(code
));
197 warn(const char * fmt
, ...) {
200 vwarnc(errno
, fmt
, ap
);
204 #define BUF_SIZE 1024
207 prt(const char *fmt
, ...)
210 char buffer
[BUF_SIZE
];
213 vsnprintf(buffer
, BUF_SIZE
, fmt
, args
);
215 fprintf(stdout
, "%s", buffer
);
217 fprintf(fsxlogf
, "%s", buffer
);
221 prterr(const char *prefix
)
223 prt("%s%s%s\n", prefix
, prefix
? ": " : "", strerror(errno
));
227 prterrcode(const char *prefix
, int code
)
229 prt("%s%s%s\n", prefix
, prefix
? ": " : "", strerror(-code
));
233 simple_err(const char *msg
, int err
)
235 fprintf(stderr
, "%s: %s\n", msg
, strerror(-err
));
241 std::mt19937 random_generator
;
246 return random_generator();
249 void replay_imagename(char *buf
, size_t len
, int clones
);
253 static const std::string
JOURNAL_CLIENT_ID("fsx");
255 struct ReplayHandler
: public journal::ReplayHandler
{
256 journal::Journaler
*journaler
;
257 journal::Journaler
*replay_journaler
;
260 ReplayHandler(journal::Journaler
*journaler
,
261 journal::Journaler
*replay_journaler
, Context
*on_finish
)
262 : journaler(journaler
), replay_journaler(replay_journaler
),
263 on_finish(on_finish
) {
266 void get() override
{
268 void put() override
{
271 void handle_entries_available() override
{
273 journal::ReplayEntry replay_entry
;
274 if (!journaler
->try_pop_front(&replay_entry
)) {
278 replay_journaler
->append(0, replay_entry
.get_data());
282 void handle_complete(int r
) override
{
283 on_finish
->complete(r
);
287 int get_image_id(librados::IoCtx
&io_ctx
, const char *image_name
,
288 std::string
*image_id
) {
291 int r
= rbd
.open(io_ctx
, image
, image_name
);
293 simple_err("failed to open image", r
);
297 rbd_image_info_t info
;
298 r
= image
.stat(info
, sizeof(info
));
300 simple_err("failed to stat image", r
);
304 *image_id
= std::string(&info
.block_name_prefix
[strlen(RBD_DATA_PREFIX
)]);
308 int register_journal(rados_ioctx_t ioctx
, const char *image_name
) {
309 librados::IoCtx io_ctx
;
310 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
312 std::string image_id
;
313 int r
= get_image_id(io_ctx
, image_name
, &image_id
);
318 journal::Journaler
journaler(io_ctx
, image_id
, JOURNAL_CLIENT_ID
, {});
319 r
= journaler
.register_client(bufferlist());
321 simple_err("failed to register journal client", r
);
327 int unregister_journal(rados_ioctx_t ioctx
, const char *image_name
) {
328 librados::IoCtx io_ctx
;
329 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
331 std::string image_id
;
332 int r
= get_image_id(io_ctx
, image_name
, &image_id
);
337 journal::Journaler
journaler(io_ctx
, image_id
, JOURNAL_CLIENT_ID
, {});
338 r
= journaler
.unregister_client();
340 simple_err("failed to unregister journal client", r
);
346 int create_replay_image(rados_ioctx_t ioctx
, int order
,
347 uint64_t stripe_unit
, int stripe_count
,
348 const char *replay_image_name
,
349 const char *last_replay_image_name
) {
350 librados::IoCtx io_ctx
;
351 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
355 if (last_replay_image_name
== nullptr) {
356 r
= rbd
.create2(io_ctx
, replay_image_name
, 0,
357 RBD_FEATURES_ALL
, &order
);
359 r
= rbd
.clone2(io_ctx
, last_replay_image_name
, "snap",
360 io_ctx
, replay_image_name
, RBD_FEATURES_ALL
,
361 &order
, stripe_unit
, stripe_count
);
365 simple_err("failed to create replay image", r
);
372 int replay_journal(rados_ioctx_t ioctx
, const char *image_name
,
373 const char *replay_image_name
) {
374 librados::IoCtx io_ctx
;
375 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
377 std::string image_id
;
378 int r
= get_image_id(io_ctx
, image_name
, &image_id
);
383 std::string replay_image_id
;
384 r
= get_image_id(io_ctx
, replay_image_name
, &replay_image_id
);
389 journal::Journaler
journaler(io_ctx
, image_id
, JOURNAL_CLIENT_ID
, {});
390 C_SaferCond init_ctx
;
391 journaler
.init(&init_ctx
);
392 BOOST_SCOPE_EXIT_ALL( (&journaler
) ) {
393 journaler
.shut_down();
398 simple_err("failed to initialize journal", r
);
402 journal::Journaler
replay_journaler(io_ctx
, replay_image_id
, "", {});
404 C_SaferCond replay_init_ctx
;
405 replay_journaler
.init(&replay_init_ctx
);
406 BOOST_SCOPE_EXIT_ALL( (&replay_journaler
) ) {
407 replay_journaler
.shut_down();
410 r
= replay_init_ctx
.wait();
412 simple_err("failed to initialize replay journal", r
);
416 replay_journaler
.start_append(0, 0, 0);
418 C_SaferCond replay_ctx
;
419 ReplayHandler
replay_handler(&journaler
, &replay_journaler
,
422 // copy journal events from source image to replay image
423 journaler
.start_replay(&replay_handler
);
424 r
= replay_ctx
.wait();
426 journaler
.stop_replay();
428 C_SaferCond stop_ctx
;
429 replay_journaler
.stop_append(&stop_ctx
);
430 int stop_r
= stop_ctx
.wait();
431 if (r
== 0 && stop_r
< 0) {
436 simple_err("failed to replay journal", r
);
442 r
= rbd
.open(io_ctx
, image
, replay_image_name
);
444 simple_err("failed to open replay image", r
);
448 // perform an IO op to initiate the journal replay
450 r
= static_cast<ssize_t
>(image
.write(0, 0, bl
));
452 simple_err("failed to write to replay image", r
);
458 int finalize_journal(rados_ioctx_t ioctx
, const char *imagename
, int clones
,
459 int order
, uint64_t stripe_unit
, int stripe_count
) {
460 char replayimagename
[1024];
461 replay_imagename(replayimagename
, sizeof(replayimagename
), clones
);
463 char lastreplayimagename
[1024];
465 replay_imagename(lastreplayimagename
,
466 sizeof(lastreplayimagename
), clones
- 1);
469 int ret
= create_replay_image(ioctx
, order
, stripe_unit
,
470 stripe_count
, replayimagename
,
471 clones
> 0 ? lastreplayimagename
:
477 ret
= replay_journal(ioctx
, imagename
, replayimagename
);
484 } // anonymous namespace
491 const char *name
; /* image name */
492 rbd_image_t image
; /* image handle */
493 const char *krbd_name
; /* image /dev/rbd<id> name */ /* reused for nbd test */
494 int krbd_fd
; /* image /dev/rbd<id> fd */ /* reused for nbd test */
497 #define RBD_CTX_INIT (struct rbd_ctx) { NULL, NULL, NULL, -1}
499 struct rbd_operations
{
500 int (*open
)(const char *name
, struct rbd_ctx
*ctx
);
501 int (*close
)(struct rbd_ctx
*ctx
);
502 ssize_t (*read
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, char *buf
);
503 ssize_t (*write
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, const char *buf
);
504 int (*flush
)(struct rbd_ctx
*ctx
);
505 int (*discard
)(struct rbd_ctx
*ctx
, uint64_t off
, uint64_t len
);
506 int (*get_size
)(struct rbd_ctx
*ctx
, uint64_t *size
);
507 int (*resize
)(struct rbd_ctx
*ctx
, uint64_t size
);
508 int (*clone
)(struct rbd_ctx
*ctx
, const char *src_snapname
,
509 const char *dst_imagename
, int *order
, int stripe_unit
,
511 int (*flatten
)(struct rbd_ctx
*ctx
);
512 ssize_t (*writesame
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
,
513 const char *buf
, size_t data_len
);
516 char *pool
; /* name of the pool our test image is in */
517 char *iname
; /* name of our test image */
518 rados_t cluster
; /* handle for our test cluster */
519 rados_ioctx_t ioctx
; /* handle for our test pool */
520 struct krbd_ctx
*krbd
; /* handle for libkrbd */
521 bool skip_partial_discard
; /* rbd_skip_partial_discard config value*/
524 * librbd/krbd rbd_operations handlers. Given the rest of fsx.c, no
525 * attempt to do error handling is made in these handlers.
529 __librbd_open(const char *name
, struct rbd_ctx
*ctx
)
534 assert(!ctx
->name
&& !ctx
->image
&&
535 !ctx
->krbd_name
&& ctx
->krbd_fd
< 0);
537 ret
= rbd_open(ioctx
, name
, &image
, NULL
);
539 prt("rbd_open(%s) failed\n", name
);
543 ctx
->name
= strdup(name
);
545 ctx
->krbd_name
= NULL
;
552 librbd_open(const char *name
, struct rbd_ctx
*ctx
)
554 return __librbd_open(name
, ctx
);
558 __librbd_close(struct rbd_ctx
*ctx
)
562 assert(ctx
->name
&& ctx
->image
);
564 ret
= rbd_close(ctx
->image
);
566 prt("rbd_close(%s) failed\n", ctx
->name
);
570 free((void *)ctx
->name
);
579 librbd_close(struct rbd_ctx
*ctx
)
581 return __librbd_close(ctx
);
585 librbd_verify_object_map(struct rbd_ctx
*ctx
)
589 n
= rbd_get_flags(ctx
->image
, &flags
);
591 prt("rbd_get_flags() failed\n");
595 if ((flags
& RBD_FLAG_OBJECT_MAP_INVALID
) != 0) {
596 prt("rbd_get_flags() indicates object map is invalid\n");
603 librbd_read(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, char *buf
)
607 n
= rbd_read(ctx
->image
, off
, len
, buf
);
609 prt("rbd_read(%llu, %zu) failed\n", off
, len
);
615 librbd_write(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, const char *buf
)
620 n
= rbd_write(ctx
->image
, off
, len
, buf
);
622 prt("rbd_write(%llu, %zu) failed\n", off
, len
);
626 ret
= librbd_verify_object_map(ctx
);
634 librbd_flush(struct rbd_ctx
*ctx
)
638 ret
= rbd_flush(ctx
->image
);
640 prt("rbd_flush failed\n");
644 return librbd_verify_object_map(ctx
);
648 librbd_discard(struct rbd_ctx
*ctx
, uint64_t off
, uint64_t len
)
652 ret
= rbd_discard(ctx
->image
, off
, len
);
654 prt("rbd_discard(%llu, %llu) failed\n", off
, len
);
658 return librbd_verify_object_map(ctx
);
662 librbd_writesame(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
,
663 const char *buf
, size_t data_len
)
668 n
= rbd_writesame(ctx
->image
, off
, len
, buf
, data_len
, 0);
670 prt("rbd_writesame(%llu, %zu) failed\n", off
, len
);
674 ret
= librbd_verify_object_map(ctx
);
682 librbd_get_size(struct rbd_ctx
*ctx
, uint64_t *size
)
684 rbd_image_info_t info
;
687 ret
= rbd_stat(ctx
->image
, &info
, sizeof(info
));
689 prt("rbd_stat failed\n");
699 __librbd_resize(struct rbd_ctx
*ctx
, uint64_t size
)
703 ret
= rbd_resize(ctx
->image
, size
);
705 prt("rbd_resize(%llu) failed\n", size
);
709 return librbd_verify_object_map(ctx
);
713 librbd_resize(struct rbd_ctx
*ctx
, uint64_t size
)
715 return __librbd_resize(ctx
, size
);
719 __librbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
720 const char *dst_imagename
, int *order
, int stripe_unit
,
721 int stripe_count
, bool krbd
)
725 ret
= rbd_snap_create(ctx
->image
, src_snapname
);
727 prt("rbd_snap_create(%s@%s) failed\n", ctx
->name
,
732 ret
= rbd_snap_protect(ctx
->image
, src_snapname
);
734 prt("rbd_snap_protect(%s@%s) failed\n", ctx
->name
,
739 uint64_t features
= RBD_FEATURES_ALL
;
741 features
&= ~(RBD_FEATURE_OBJECT_MAP
|
742 RBD_FEATURE_FAST_DIFF
|
743 RBD_FEATURE_DEEP_FLATTEN
|
744 RBD_FEATURE_JOURNALING
);
746 ret
= rbd_clone2(ioctx
, ctx
->name
, src_snapname
, ioctx
,
747 dst_imagename
, features
, order
,
748 stripe_unit
, stripe_count
);
750 prt("rbd_clone2(%s@%s -> %s) failed\n", ctx
->name
,
751 src_snapname
, dst_imagename
);
759 librbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
760 const char *dst_imagename
, int *order
, int stripe_unit
,
763 return __librbd_clone(ctx
, src_snapname
, dst_imagename
, order
,
764 stripe_unit
, stripe_count
, false);
768 __librbd_flatten(struct rbd_ctx
*ctx
)
772 ret
= rbd_flatten(ctx
->image
);
774 prt("rbd_flatten failed\n");
778 return librbd_verify_object_map(ctx
);
782 librbd_flatten(struct rbd_ctx
*ctx
)
784 return __librbd_flatten(ctx
);
787 const struct rbd_operations librbd_operations
= {
802 krbd_open(const char *name
, struct rbd_ctx
*ctx
)
808 ret
= __librbd_open(name
, ctx
);
812 ret
= krbd_map(krbd
, pool
, name
, "", "", &devnode
);
814 prt("krbd_map(%s) failed\n", name
);
818 fd
= open(devnode
, O_RDWR
| o_direct
);
821 prt("open(%s) failed\n", devnode
);
825 ctx
->krbd_name
= devnode
;
832 krbd_close(struct rbd_ctx
*ctx
)
836 assert(ctx
->krbd_name
&& ctx
->krbd_fd
>= 0);
838 if (close(ctx
->krbd_fd
) < 0) {
840 prt("close(%s) failed\n", ctx
->krbd_name
);
844 ret
= krbd_unmap(krbd
, ctx
->krbd_name
, "");
846 prt("krbd_unmap(%s) failed\n", ctx
->krbd_name
);
850 free((void *)ctx
->krbd_name
);
852 ctx
->krbd_name
= NULL
;
855 return __librbd_close(ctx
);
859 krbd_read(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, char *buf
)
863 n
= pread(ctx
->krbd_fd
, buf
, len
, off
);
866 prt("pread(%llu, %zu) failed\n", off
, len
);
874 krbd_write(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, const char *buf
)
878 n
= pwrite(ctx
->krbd_fd
, buf
, len
, off
);
881 prt("pwrite(%llu, %zu) failed\n", off
, len
);
889 __krbd_flush(struct rbd_ctx
*ctx
, bool invalidate
)
897 * BLKFLSBUF will sync the filesystem on top of the device (we
898 * don't care about that here, since we write directly to it),
899 * write out any dirty buffers and invalidate the buffer cache.
900 * It won't do a hardware cache flush.
902 * fsync() will write out any dirty buffers and do a hardware
903 * cache flush (which we don't care about either, because for
904 * krbd it's a noop). It won't try to empty the buffer cache
905 * nor poke the filesystem before writing out.
907 * Given that, for our purposes, fsync is a flush, while
908 * BLKFLSBUF is a flush+invalidate.
911 ret
= ioctl(ctx
->krbd_fd
, BLKFLSBUF
, NULL
);
913 ret
= fsync(ctx
->krbd_fd
);
916 prt("%s failed\n", invalidate
? "BLKFLSBUF" : "fsync");
924 krbd_flush(struct rbd_ctx
*ctx
)
926 return __krbd_flush(ctx
, false);
930 krbd_discard(struct rbd_ctx
*ctx
, uint64_t off
, uint64_t len
)
932 uint64_t range
[2] = { off
, len
};
936 * BLKDISCARD goes straight to disk and doesn't do anything
937 * about dirty buffers. This means we need to flush so that
942 * results in "data 0000 data" rather than "data data data" on
943 * disk and invalidate so that
948 * returns "data 0000 data" rather than "data data data" in
949 * case 1..2M was cached.
951 ret
= __krbd_flush(ctx
, true);
956 * off and len must be 512-byte aligned, otherwise BLKDISCARD
957 * will fail with -EINVAL. This means that -K (enable krbd
958 * mode) requires -h 512 or similar.
960 if (ioctl(ctx
->krbd_fd
, BLKDISCARD
, &range
) < 0) {
962 prt("BLKDISCARD(%llu, %llu) failed\n", off
, len
);
970 krbd_get_size(struct rbd_ctx
*ctx
, uint64_t *size
)
974 if (ioctl(ctx
->krbd_fd
, BLKGETSIZE64
, &bytes
) < 0) {
976 prt("BLKGETSIZE64 failed\n");
986 krbd_resize(struct rbd_ctx
*ctx
, uint64_t size
)
990 assert(size
% truncbdy
== 0);
993 * When krbd detects a size change, it calls revalidate_disk(),
994 * which ends up calling invalidate_bdev(), which invalidates
995 * clean pages and does nothing about dirty pages beyond the
996 * new size. The preceding cache flush makes sure those pages
997 * are invalidated, which is what we need on shrink so that
1004 * returns "0000 0000" rather than "data 0000".
1006 ret
= __krbd_flush(ctx
, false);
1010 return __librbd_resize(ctx
, size
);
1014 krbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
1015 const char *dst_imagename
, int *order
, int stripe_unit
,
1020 ret
= __krbd_flush(ctx
, false);
1024 return __librbd_clone(ctx
, src_snapname
, dst_imagename
, order
,
1025 stripe_unit
, stripe_count
, true);
1029 krbd_flatten(struct rbd_ctx
*ctx
)
1033 ret
= __krbd_flush(ctx
, false);
1037 return __librbd_flatten(ctx
);
1040 const struct rbd_operations krbd_operations
= {
1055 nbd_open(const char *name
, struct rbd_ctx
*ctx
)
1062 SubProcess
process("rbd-nbd", SubProcess::KEEP
, SubProcess::PIPE
,
1064 process
.add_cmd_arg("map");
1069 process
.add_cmd_arg(img
.c_str());
1071 r
= __librbd_open(name
, ctx
);
1075 r
= process
.spawn();
1077 prt("nbd_open failed to run rbd-nbd error: %s\n", process
.err().c_str());
1080 r
= safe_read(process
.get_stdout(), dev
, sizeof(dev
));
1082 prt("nbd_open failed to get nbd device path\n");
1085 for (int i
= 0; i
< r
; ++i
)
1086 if (dev
[i
] == 10 || dev
[i
] == 13)
1091 prt("rbd-nbd failed with error: %s", process
.err().c_str());
1095 devnode
= strdup(dev
);
1099 fd
= open(devnode
, O_RDWR
| o_direct
);
1102 prt("open(%s) failed\n", devnode
);
1106 ctx
->krbd_name
= devnode
;
1113 nbd_close(struct rbd_ctx
*ctx
)
1117 assert(ctx
->krbd_name
&& ctx
->krbd_fd
>= 0);
1119 if (close(ctx
->krbd_fd
) < 0) {
1121 prt("close(%s) failed\n", ctx
->krbd_name
);
1125 SubProcess
process("rbd-nbd");
1126 process
.add_cmd_arg("unmap");
1127 process
.add_cmd_arg(ctx
->krbd_name
);
1129 r
= process
.spawn();
1131 prt("nbd_close failed to run rbd-nbd error: %s\n", process
.err().c_str());
1136 prt("rbd-nbd failed with error: %d", process
.err().c_str());
1140 free((void *)ctx
->krbd_name
);
1142 ctx
->krbd_name
= NULL
;
1145 return __librbd_close(ctx
);
1149 nbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
1150 const char *dst_imagename
, int *order
, int stripe_unit
,
1155 ret
= __krbd_flush(ctx
, false);
1159 return __librbd_clone(ctx
, src_snapname
, dst_imagename
, order
,
1160 stripe_unit
, stripe_count
, false);
1163 const struct rbd_operations nbd_operations
= {
1177 struct rbd_ctx ctx
= RBD_CTX_INIT
;
1178 const struct rbd_operations
*ops
= &librbd_operations
;
1180 static bool rbd_image_has_parent(struct rbd_ctx
*ctx
)
1184 ret
= rbd_get_parent_info(ctx
->image
, NULL
, 0, NULL
, 0, NULL
, 0);
1185 if (ret
< 0 && ret
!= -ENOENT
) {
1186 prterrcode("rbd_get_parent_info", ret
);
1198 log4(int operation
, int arg0
, int arg1
, int arg2
)
1200 struct log_entry
*le
;
1202 le
= &oplog
[logptr
];
1203 le
->operation
= operation
;
1205 le
->operation
= ~ le
->operation
;
1211 if (logptr
>= LOGSIZE
)
1219 struct log_entry
*lp
;
1220 const char *falloc_type
[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
1222 prt("LOG DUMP (%d total operations):\n", logcount
);
1223 if (logcount
< LOGSIZE
) {
1230 for ( ; count
> 0; count
--) {
1233 opnum
= i
+1 + (logcount
/LOGSIZE
)*LOGSIZE
;
1234 prt("%d(%3d mod 256): ", opnum
, opnum
%256);
1236 if ((closeopen
= lp
->operation
< 0))
1237 lp
->operation
= ~ lp
->operation
;
1239 switch (lp
->operation
) {
1241 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
1242 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1244 if (badoff
>= lp
->args
[0] && badoff
<
1245 lp
->args
[0] + lp
->args
[1])
1246 prt("\t***RRRR***");
1249 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
1250 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1252 if (badoff
>= lp
->args
[0] && badoff
<
1253 lp
->args
[0] + lp
->args
[1])
1254 prt("\t******WWWW");
1257 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
1258 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1260 if (badoff
>= lp
->args
[0] &&
1261 badoff
< lp
->args
[0] + lp
->args
[1])
1262 prt("\t***RRRR***");
1265 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
1266 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1268 if (lp
->args
[0] > lp
->args
[2])
1270 else if (lp
->args
[0] + lp
->args
[1] > lp
->args
[2])
1272 if ((badoff
>= lp
->args
[0] || badoff
>=lp
->args
[2]) &&
1273 badoff
< lp
->args
[0] + lp
->args
[1])
1277 down
= lp
->args
[0] < lp
->args
[1];
1278 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
1279 down
? "DOWN" : "UP", lp
->args
[1], lp
->args
[0]);
1280 if (badoff
>= lp
->args
[!down
] &&
1281 badoff
< lp
->args
[!!down
])
1282 prt("\t******WWWW");
1285 /* 0: offset 1: length 2: where alloced */
1286 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) %s",
1287 lp
->args
[0], lp
->args
[0] + lp
->args
[1],
1288 lp
->args
[1], falloc_type
[lp
->args
[2]]);
1289 if (badoff
>= lp
->args
[0] &&
1290 badoff
< lp
->args
[0] + lp
->args
[1])
1291 prt("\t******FFFF");
1294 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
1295 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1297 if (badoff
>= lp
->args
[0] && badoff
<
1298 lp
->args
[0] + lp
->args
[1])
1299 prt("\t******PPPP");
1302 prt("WRITESAME 0x%x thru 0x%x\t(0x%x bytes) data_size 0x%x",
1303 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1304 lp
->args
[1], lp
->args
[2]);
1305 if (badoff
>= lp
->args
[0] &&
1306 badoff
< lp
->args
[0] + lp
->args
[1])
1307 prt("\t***WSWSWSWS");
1316 prt("SKIPPED (no operation)");
1319 prt("BOGUS LOG ENTRY (operation code = %d)!",
1323 prt("\n\t\tCLOSE/OPEN");
1332 save_buffer(char *buffer
, off_t bufferlength
, int fd
)
1335 ssize_t byteswritten
;
1337 if (fd
<= 0 || bufferlength
== 0)
1340 if (bufferlength
> SSIZE_MAX
) {
1341 prt("fsx flaw: overflow in save_buffer\n");
1345 ret
= lseek(fd
, (off_t
)0, SEEK_SET
);
1346 if (ret
== (off_t
)-1)
1347 prterr("save_buffer: lseek 0");
1349 byteswritten
= write(fd
, buffer
, (size_t)bufferlength
);
1350 if (byteswritten
!= bufferlength
) {
1351 if (byteswritten
== -1)
1352 prterr("save_buffer write");
1354 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
1355 (unsigned)byteswritten
,
1356 (unsigned long long)bufferlength
);
1362 report_failure(int status
)
1368 save_buffer(good_buf
, file_size
, fsxgoodfd
);
1369 prt("Correct content saved for comparison\n");
1370 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
1375 sleep(3); // so the log can flush to disk. KLUDGEY!
1379 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
1380 *(((unsigned char *)(cp)) + 1)))
1383 fsxcmp(char *good_buf
, char *temp_buf
, unsigned size
)
1385 if (!skip_partial_discard
) {
1386 return memcmp(good_buf
, temp_buf
, size
);
1389 for (unsigned i
= 0; i
< size
; i
++) {
1390 if (good_buf
[i
] != temp_buf
[i
] && good_buf
[i
] != 0) {
1391 return good_buf
[i
] - temp_buf
[i
];
1398 check_buffers(char *good_buf
, char *temp_buf
, unsigned offset
, unsigned size
)
1400 if (fsxcmp(good_buf
+ offset
, temp_buf
, size
) != 0) {
1404 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
1405 offset
, size
, iname
);
1406 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
1408 unsigned char c
= good_buf
[offset
];
1409 unsigned char t
= temp_buf
[i
];
1412 unsigned bad
= short_at(&temp_buf
[i
]);
1413 prt("0x%5x\t0x%04x\t0x%04x", offset
,
1414 short_at(&good_buf
[offset
]), bad
);
1415 unsigned op
= temp_buf
[(offset
& 1) ? i
+1 : i
];
1416 prt("\t0x%5x\n", n
);
1418 prt("operation# (mod 256) for "
1419 "the bad data may be %u\n",
1420 ((unsigned)op
& 0xff));
1422 prt("operation# (mod 256) for "
1423 "the bad data unknown, check"
1424 " HOLE and EXTEND ops\n");
1433 report_failure(110);
1444 ret
= ops
->get_size(&ctx
, &size
);
1446 prterrcode("check_size: ops->get_size", ret
);
1448 if ((uint64_t)file_size
!= size
) {
1449 prt("Size error: expected 0x%llx stat 0x%llx\n",
1450 (unsigned long long)file_size
,
1451 (unsigned long long)size
);
1452 report_failure(120);
1456 #define TRUNC_HACK_SIZE (200ULL << 9) /* 512-byte aligned for krbd */
1459 check_trunc_hack(void)
1464 ret
= ops
->resize(&ctx
, 0ULL);
1466 prterrcode("check_trunc_hack: ops->resize pre", ret
);
1468 ret
= ops
->resize(&ctx
, TRUNC_HACK_SIZE
);
1470 prterrcode("check_trunc_hack: ops->resize actual", ret
);
1472 ret
= ops
->get_size(&ctx
, &size
);
1474 prterrcode("check_trunc_hack: ops->get_size", ret
);
1476 if (size
!= TRUNC_HACK_SIZE
) {
1477 prt("no extend on truncate! not posix!\n");
1481 ret
= ops
->resize(&ctx
, 0ULL);
1483 prterrcode("check_trunc_hack: ops->resize post", ret
);
1493 r
= rados_create(&cluster
, NULL
);
1495 simple_err("Could not create cluster handle", r
);
1498 rados_conf_parse_env(cluster
, NULL
);
1499 r
= rados_conf_read_file(cluster
, NULL
);
1501 simple_err("Error reading ceph config file", r
);
1502 goto failed_shutdown
;
1504 r
= rados_connect(cluster
);
1506 simple_err("Error connecting to cluster", r
);
1507 goto failed_shutdown
;
1509 r
= krbd_create_from_context(rados_cct(cluster
), &krbd
);
1511 simple_err("Could not create libkrbd handle", r
);
1512 goto failed_shutdown
;
1515 r
= rados_pool_create(cluster
, pool
);
1516 if (r
< 0 && r
!= -EEXIST
) {
1517 simple_err("Error creating pool", r
);
1520 r
= rados_ioctx_create(cluster
, pool
, &ioctx
);
1522 simple_err("Error creating ioctx", r
);
1525 if (clone_calls
|| journal_replay
) {
1526 uint64_t features
= 0;
1528 features
|= RBD_FEATURE_LAYERING
;
1530 if (journal_replay
) {
1531 features
|= (RBD_FEATURE_EXCLUSIVE_LOCK
|
1532 RBD_FEATURE_JOURNALING
);
1534 r
= rbd_create2(ioctx
, iname
, 0, features
, &order
);
1536 r
= rbd_create(ioctx
, iname
, 0, &order
);
1539 simple_err("Error creating image", r
);
1543 if (journal_replay
) {
1544 r
= register_journal(ioctx
, iname
);
1550 r
= rados_conf_get(cluster
, "rbd_skip_partial_discard", buf
,
1553 simple_err("Could not get rbd_skip_partial_discard value", r
);
1556 skip_partial_discard
= (strcmp(buf
, "true") == 0);
1561 rados_ioctx_destroy(ioctx
);
1565 rados_shutdown(cluster
);
1570 doflush(unsigned offset
, unsigned size
)
1577 ret
= ops
->flush(&ctx
);
1579 prterrcode("doflush: ops->flush", ret
);
1583 doread(unsigned offset
, unsigned size
)
1587 offset
-= offset
% readbdy
;
1589 size
-= size
% readbdy
;
1591 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
1592 prt("skipping zero size read\n");
1593 log4(OP_SKIPPED
, OP_READ
, offset
, size
);
1596 if (size
+ offset
> file_size
) {
1597 if (!quiet
&& testcalls
> simulatedopcount
)
1598 prt("skipping seek/read past end of file\n");
1599 log4(OP_SKIPPED
, OP_READ
, offset
, size
);
1603 log4(OP_READ
, offset
, size
, 0);
1605 if (testcalls
<= simulatedopcount
)
1609 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1611 (monitorstart
== -1 ||
1612 (static_cast<long>(offset
+ size
) > monitorstart
&&
1613 (monitorend
== -1 ||
1614 static_cast<long>(offset
) <= monitorend
))))))
1615 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
1616 offset
, offset
+ size
- 1, size
);
1618 ret
= ops
->read(&ctx
, offset
, size
, temp_buf
);
1619 if (ret
!= (int)size
) {
1621 prterrcode("doread: ops->read", ret
);
1623 prt("short read: 0x%x bytes instead of 0x%x\n",
1625 report_failure(141);
1628 check_buffers(good_buf
, temp_buf
, offset
, size
);
1633 check_eofpage(char *s
, unsigned offset
, char *p
, int size
)
1635 unsigned long last_page
, should_be_zero
;
1637 if (offset
+ size
<= (file_size
& ~page_mask
))
1640 * we landed in the last page of the file
1641 * test to make sure the VM system provided 0's
1642 * beyond the true end of the file mapping
1643 * (as required by mmap def in 1996 posix 1003.1)
1645 last_page
= ((unsigned long)p
+ (offset
& page_mask
) + size
) & ~page_mask
;
1647 for (should_be_zero
= last_page
+ (file_size
& page_mask
);
1648 should_be_zero
< last_page
+ page_size
;
1650 if (*(char *)should_be_zero
) {
1651 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
1652 s
, file_size
- 1, should_be_zero
& page_mask
,
1653 short_at(should_be_zero
));
1654 report_failure(205);
1660 gendata(char *original_buf
, char *good_buf
, unsigned offset
, unsigned size
)
1663 good_buf
[offset
] = testcalls
% 256;
1665 good_buf
[offset
] += original_buf
[offset
];
1672 dowrite(unsigned offset
, unsigned size
)
1677 offset
-= offset
% writebdy
;
1679 size
-= size
% writebdy
;
1681 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
1682 prt("skipping zero size write\n");
1683 log4(OP_SKIPPED
, OP_WRITE
, offset
, size
);
1687 log4(OP_WRITE
, offset
, size
, file_size
);
1689 gendata(original_buf
, good_buf
, offset
, size
);
1690 if (file_size
< offset
+ size
) {
1691 newsize
= ceil(((double)offset
+ size
) / truncbdy
) * truncbdy
;
1692 if (file_size
< newsize
)
1693 memset(good_buf
+ file_size
, '\0', newsize
- file_size
);
1694 file_size
= newsize
;
1696 warn("Lite file size bug in fsx!");
1697 report_failure(149);
1699 ret
= ops
->resize(&ctx
, newsize
);
1701 prterrcode("dowrite: ops->resize", ret
);
1702 report_failure(150);
1706 if (testcalls
<= simulatedopcount
)
1710 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1712 (monitorstart
== -1 ||
1713 (static_cast<long>(offset
+ size
) > monitorstart
&&
1714 (monitorend
== -1 ||
1715 static_cast<long>(offset
) <= monitorend
))))))
1716 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
1717 offset
, offset
+ size
- 1, size
);
1719 ret
= ops
->write(&ctx
, offset
, size
, good_buf
+ offset
);
1720 if (ret
!= (ssize_t
)size
) {
1722 prterrcode("dowrite: ops->write", ret
);
1724 prt("short write: 0x%x bytes instead of 0x%x\n",
1726 report_failure(151);
1730 doflush(offset
, size
);
1735 dotruncate(unsigned size
)
1737 int oldsize
= file_size
;
1740 size
-= size
% truncbdy
;
1741 if (size
> biggest
) {
1743 if (!quiet
&& testcalls
> simulatedopcount
)
1744 prt("truncating to largest ever: 0x%x\n", size
);
1747 log4(OP_TRUNCATE
, size
, (unsigned)file_size
, 0);
1749 if (size
> file_size
)
1750 memset(good_buf
+ file_size
, '\0', size
- file_size
);
1751 else if (size
< file_size
)
1752 memset(good_buf
+ size
, '\0', file_size
- size
);
1755 if (testcalls
<= simulatedopcount
)
1758 if ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1759 (debug
&& (monitorstart
== -1 || monitorend
== -1 ||
1760 (long)size
<= monitorend
)))
1761 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls
, oldsize
, size
);
1763 ret
= ops
->resize(&ctx
, size
);
1765 prterrcode("dotruncate: ops->resize", ret
);
1766 report_failure(160);
1771 do_punch_hole(unsigned offset
, unsigned length
)
1773 unsigned end_offset
;
1778 offset
-= offset
% holebdy
;
1779 length
-= length
% holebdy
;
1781 if (!quiet
&& testcalls
> simulatedopcount
)
1782 prt("skipping zero length punch hole\n");
1783 log4(OP_SKIPPED
, OP_PUNCH_HOLE
, offset
, length
);
1787 if (file_size
<= (loff_t
)offset
) {
1788 if (!quiet
&& testcalls
> simulatedopcount
)
1789 prt("skipping hole punch off the end of the file\n");
1790 log4(OP_SKIPPED
, OP_PUNCH_HOLE
, offset
, length
);
1794 end_offset
= offset
+ length
;
1796 log4(OP_PUNCH_HOLE
, offset
, length
, 0);
1798 if (testcalls
<= simulatedopcount
)
1801 if ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1802 (debug
&& (monitorstart
== -1 || monitorend
== -1 ||
1803 (long)end_offset
<= monitorend
))) {
1804 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls
,
1805 offset
, offset
+length
, length
);
1808 ret
= ops
->discard(&ctx
, (unsigned long long)offset
,
1809 (unsigned long long)length
);
1811 prterrcode("do_punch_hole: ops->discard", ret
);
1812 report_failure(161);
1815 max_offset
= offset
< file_size
? offset
: file_size
;
1816 max_len
= max_offset
+ length
<= file_size
? length
:
1817 file_size
- max_offset
;
1818 memset(good_buf
+ max_offset
, '\0', max_len
);
1821 unsigned get_data_size(unsigned size
)
1825 unsigned max
= sqrt((double)size
) + 1;
1827 unsigned curr
= good
;
1829 hint
= get_random() % max
;
1831 for (i
= 1; i
< max
&& curr
< hint
; i
++) {
1832 if (size
% i
== 0) {
1845 dowritesame(unsigned offset
, unsigned size
)
1853 offset
-= offset
% writebdy
;
1855 size
-= size
% writebdy
;
1857 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
1858 prt("skipping zero size writesame\n");
1859 log4(OP_SKIPPED
, OP_WRITESAME
, offset
, size
);
1863 data_size
= get_data_size(size
);
1865 log4(OP_WRITESAME
, offset
, size
, data_size
);
1867 gendata(original_buf
, good_buf
, offset
, data_size
);
1868 if (file_size
< offset
+ size
) {
1869 newsize
= ceil(((double)offset
+ size
) / truncbdy
) * truncbdy
;
1870 if (file_size
< newsize
)
1871 memset(good_buf
+ file_size
, '\0', newsize
- file_size
);
1872 file_size
= newsize
;
1874 warn("Lite file size bug in fsx!");
1875 report_failure(162);
1877 ret
= ops
->resize(&ctx
, newsize
);
1879 prterrcode("dowritesame: ops->resize", ret
);
1880 report_failure(163);
1884 for (n
= size
/ data_size
, buf_off
= data_size
; n
> 1; n
--) {
1885 memcpy(good_buf
+ offset
+ buf_off
, good_buf
+ offset
, data_size
);
1886 buf_off
+= data_size
;
1889 if (testcalls
<= simulatedopcount
)
1893 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1895 (monitorstart
== -1 ||
1896 (static_cast<long>(offset
+ size
) > monitorstart
&&
1897 (monitorend
== -1 ||
1898 static_cast<long>(offset
) <= monitorend
))))))
1899 prt("%lu writesame\t0x%x thru\t0x%x\tdata_size\t0x%x(0x%x bytes)\n", testcalls
,
1900 offset
, offset
+ size
- 1, data_size
, size
);
1902 ret
= ops
->writesame(&ctx
, offset
, size
, good_buf
+ offset
, data_size
);
1903 if (ret
!= (ssize_t
)size
) {
1905 prterrcode("dowritesame: ops->writesame", ret
);
1907 prt("short writesame: 0x%x bytes instead of 0x%x\n",
1909 report_failure(164);
1913 doflush(offset
, size
);
1916 void clone_filename(char *buf
, size_t len
, int clones
)
1918 snprintf(buf
, len
, "%s/fsx-%s-parent%d",
1919 dirpath
, iname
, clones
);
1922 void clone_imagename(char *buf
, size_t len
, int clones
)
1925 snprintf(buf
, len
, "%s-clone%d", iname
, clones
);
1927 strncpy(buf
, iname
, len
);
1928 buf
[len
- 1] = '\0';
1931 void replay_imagename(char *buf
, size_t len
, int clones
)
1933 clone_imagename(buf
, len
, clones
);
1934 strncat(buf
, "-replay", len
- strlen(buf
));
1935 buf
[len
- 1] = '\0';
1938 void check_clone(int clonenum
, bool replay_image
);
1943 char filename
[1024];
1944 char imagename
[1024];
1945 char lastimagename
[1024];
1947 int order
= 0, stripe_unit
= 0, stripe_count
= 0;
1948 uint64_t newsize
= file_size
;
1950 log4(OP_CLONE
, 0, 0, 0);
1953 if (randomize_striping
) {
1954 order
= 18 + get_random() % 8;
1955 stripe_unit
= 1ull << (order
- 1 - (get_random() % 8));
1956 stripe_count
= 2 + get_random() % 14;
1959 prt("%lu clone\t%d order %d su %d sc %d\n", testcalls
, num_clones
,
1960 order
, stripe_unit
, stripe_count
);
1962 clone_imagename(imagename
, sizeof(imagename
), num_clones
);
1963 clone_imagename(lastimagename
, sizeof(lastimagename
),
1965 assert(strcmp(lastimagename
, ctx
.name
) == 0);
1967 ret
= ops
->clone(&ctx
, "snap", imagename
, &order
, stripe_unit
,
1970 prterrcode("do_clone: ops->clone", ret
);
1974 if (randomize_parent_overlap
&& rbd_image_has_parent(&ctx
)) {
1975 int rand
= get_random() % 16 + 1; // [1..16]
1980 ret
= rbd_get_overlap(ctx
.image
, &overlap
);
1982 prterrcode("do_clone: rbd_get_overlap", ret
);
1986 if (rand
< 10) { // 9/16
1987 newsize
= overlap
* ((double)rand
/ 10);
1988 newsize
-= newsize
% truncbdy
;
1993 assert(newsize
!= (uint64_t)file_size
);
1994 prt("truncating image %s from 0x%llx (overlap 0x%llx) to 0x%llx\n",
1995 ctx
.name
, file_size
, overlap
, newsize
);
1997 ret
= ops
->resize(&ctx
, newsize
);
1999 prterrcode("do_clone: ops->resize", ret
);
2002 } else if (rand
< 15) { // 2/16
2003 prt("flattening image %s\n", ctx
.name
);
2005 ret
= ops
->flatten(&ctx
);
2007 prterrcode("do_clone: ops->flatten", ret
);
2011 prt("leaving image %s intact\n", ctx
.name
);
2015 clone_filename(filename
, sizeof(filename
), num_clones
);
2016 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666)) < 0) {
2017 simple_err("do_clone: open", -errno
);
2020 save_buffer(good_buf
, newsize
, fd
);
2021 if ((ret
= close(fd
)) < 0) {
2022 simple_err("do_clone: close", -errno
);
2029 if ((ret
= ops
->close(&ctx
)) < 0) {
2030 prterrcode("do_clone: ops->close", ret
);
2034 if (journal_replay
) {
2035 ret
= finalize_journal(ioctx
, lastimagename
, num_clones
- 1,
2036 order
, stripe_unit
, stripe_count
);
2041 ret
= register_journal(ioctx
, imagename
);
2048 * Open freshly made clone.
2050 if ((ret
= ops
->open(imagename
, &ctx
)) < 0) {
2051 prterrcode("do_clone: ops->open", ret
);
2055 if (num_clones
> 1) {
2056 if (journal_replay
) {
2057 check_clone(num_clones
- 2, true);
2059 check_clone(num_clones
- 2, false);
2064 check_clone(int clonenum
, bool replay_image
)
2067 char imagename
[128];
2069 struct rbd_ctx cur_ctx
= RBD_CTX_INIT
;
2070 struct stat file_info
;
2071 char *good_buf
, *temp_buf
;
2074 replay_imagename(imagename
, sizeof(imagename
), clonenum
);
2076 clone_imagename(imagename
, sizeof(imagename
), clonenum
);
2079 if ((ret
= ops
->open(imagename
, &cur_ctx
)) < 0) {
2080 prterrcode("check_clone: ops->open", ret
);
2084 clone_filename(filename
, sizeof(filename
), clonenum
+ 1);
2085 if ((fd
= open(filename
, O_RDONLY
)) < 0) {
2086 simple_err("check_clone: open", -errno
);
2090 prt("checking clone #%d, image %s against file %s\n",
2091 clonenum
, imagename
, filename
);
2092 if ((ret
= fstat(fd
, &file_info
)) < 0) {
2093 simple_err("check_clone: fstat", -errno
);
2098 ret
= posix_memalign((void **)&good_buf
,
2099 MAX(writebdy
, (int)sizeof(void *)),
2102 prterrcode("check_clone: posix_memalign(good_buf)", -ret
);
2107 ret
= posix_memalign((void **)&temp_buf
,
2108 MAX(readbdy
, (int)sizeof(void *)),
2111 prterrcode("check_clone: posix_memalign(temp_buf)", -ret
);
2115 if ((ret
= pread(fd
, good_buf
, file_info
.st_size
, 0)) < 0) {
2116 simple_err("check_clone: pread", -errno
);
2119 if ((ret
= ops
->read(&cur_ctx
, 0, file_info
.st_size
, temp_buf
)) < 0) {
2120 prterrcode("check_clone: ops->read", ret
);
2124 if ((ret
= ops
->close(&cur_ctx
)) < 0) {
2125 prterrcode("check_clone: ops->close", ret
);
2128 check_buffers(good_buf
, temp_buf
, 0, file_info
.st_size
);
2130 if (!replay_image
) {
2143 ret
= ops
->write(&ctx
, 0, file_size
, good_buf
);
2144 if (ret
!= file_size
) {
2146 prterrcode("writefileimage: ops->write", ret
);
2148 prt("short write: 0x%x bytes instead of 0x%llx\n",
2149 ret
, (unsigned long long)file_size
);
2150 report_failure(172);
2154 ret
= ops
->resize(&ctx
, file_size
);
2156 prterrcode("writefileimage: ops->resize", ret
);
2157 report_failure(173);
2167 if (!rbd_image_has_parent(&ctx
)) {
2168 log4(OP_SKIPPED
, OP_FLATTEN
, 0, 0);
2171 log4(OP_FLATTEN
, 0, 0, 0);
2172 prt("%lu flatten\n", testcalls
);
2174 ret
= ops
->flatten(&ctx
);
2176 prterrcode("writefileimage: ops->flatten", ret
);
2187 if (testcalls
<= simulatedopcount
)
2190 name
= strdup(ctx
.name
);
2193 prt("%lu close/open\n", testcalls
);
2195 ret
= ops
->close(&ctx
);
2197 prterrcode("docloseopen: ops->close", ret
);
2198 report_failure(180);
2201 ret
= ops
->open(name
, &ctx
);
2203 prterrcode("docloseopen: ops->open", ret
);
2204 report_failure(181);
2210 #define TRIM_OFF_LEN(off, len, size) \
2216 if ((unsigned)(off) + (unsigned)(len) > (unsigned)(size)) \
2217 (len) = (size) - (off); \
2223 unsigned long offset
;
2224 unsigned long size
= maxoplen
;
2225 unsigned long rv
= get_random();
2228 if (simulatedopcount
> 0 && testcalls
== simulatedopcount
)
2234 closeopen
= (rv
>> 3) < (1u << 28) / (unsigned)closeprob
;
2236 if (debugstart
> 0 && testcalls
>= debugstart
)
2239 if (!quiet
&& testcalls
< simulatedopcount
&& testcalls
% 100000 == 0)
2240 prt("%lu...\n", testcalls
);
2242 offset
= get_random();
2244 size
= get_random() % (maxoplen
+ 1);
2246 /* calculate appropriate op to run */
2248 op
= rv
% OP_MAX_LITE
;
2250 op
= rv
% OP_MAX_FULL
;
2262 if (!fallocate_calls
) {
2263 log4(OP_SKIPPED
, OP_FALLOCATE
, offset
, size
);
2268 if (!punch_hole_calls
) {
2269 log4(OP_SKIPPED
, OP_PUNCH_HOLE
, offset
, size
);
2274 /* clone, 8% chance */
2275 if (!clone_calls
|| file_size
== 0 || get_random() % 100 >= 8) {
2276 log4(OP_SKIPPED
, OP_CLONE
, 0, 0);
2281 /* flatten four times as rarely as clone, 2% chance */
2282 if (get_random() % 100 >= 2) {
2283 log4(OP_SKIPPED
, OP_FLATTEN
, 0, 0);
2288 /* writesame not implemented */
2289 if (!ops
->writesame
) {
2290 log4(OP_SKIPPED
, OP_WRITESAME
, offset
, size
);
2297 TRIM_OFF_LEN(offset
, size
, file_size
);
2298 doread(offset
, size
);
2302 TRIM_OFF_LEN(offset
, size
, maxfilelen
);
2303 dowrite(offset
, size
);
2307 TRIM_OFF_LEN(offset
, size
, file_size
);
2312 TRIM_OFF_LEN(offset
, size
, maxfilelen
);
2318 size
= get_random() % maxfilelen
;
2323 TRIM_OFF_LEN(offset
, size
, file_size
);
2324 do_punch_hole(offset
, size
);
2328 TRIM_OFF_LEN(offset
, size
, maxfilelen
);
2329 dowritesame(offset
, size
);
2341 prterr("test: unknown operation");
2347 if (sizechecks
&& testcalls
> simulatedopcount
)
2358 prt("signal %d\n", sig
);
2359 prt("testcalls = %lu\n", testcalls
);
2367 fprintf(stdout
, "usage: %s",
2368 "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\
2369 -b opnum: beginning operation number (default 1)\n\
2370 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
2371 -d: debug output for all operations\n\
2372 -f: flush and invalidate cache after I/O\n\
2373 -h holebdy: 4096 would make discards page aligned (default 1)\n\
2374 -j: journal replay stress test\n\
2375 -k: keep data on success (default 0)\n\
2376 -l flen: the upper bound on file size (default 262144)\n\
2377 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
2378 -n: no verifications of file size\n\
2379 -o oplen: the upper bound on operation size (default 65536)\n\
2380 -p progressinterval: debug output at specified operation interval\n\
2381 -q: quieter operation\n\
2382 -r readbdy: 4096 would make reads page aligned (default 1)\n\
2383 -s style: 1 gives smaller truncates (default 0)\n\
2384 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
2385 -w writebdy: 4096 would make writes page aligned (default 1)\n\
2386 -x: preallocate file space before starting, XFS only (default 0)\n\
2387 -y: synchronize changes to a file\n"
2389 " -C: do not use clone calls\n\
2390 -D startingop: debug output starting at specified operation\n"
2392 " -F: Do not use fallocate (preallocation) calls\n"
2394 " -H: do not use punch hole calls\n\
2395 -K: enable krbd mode (use -t and -h too)\n\
2396 -M: enable rbd-nbd mode (use -t and -h too)\n\
2397 -L: fsxLite - no file creations & no file size changes\n\
2398 -N numops: total # operations to do (default infinity)\n\
2399 -O: use oplen (see -o flag) for every op (default random)\n\
2400 -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
2401 -R: read() system calls only (mapped reads disabled)\n\
2402 -S seed: for random # generator (default 1) 0 gets timestamp\n\
2403 -U: disable randomized striping\n\
2404 -W: mapped write operations DISabled\n\
2405 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
2406 poolname: this is REQUIRED (no default)\n\
2407 imagename: this is REQUIRED (no default)\n");
2413 getnum(char *s
, char **e
)
2418 ret
= strtol(s
, e
, 0);
2449 if (!lite
&& fallocate_calls
) {
2450 if (fallocate(fd
, 0, 0, 1) && errno
== EOPNOTSUPP
) {
2452 warn("main: filesystem does not support fallocate, disabling\n");
2453 fallocate_calls
= 0;
2458 #else /* ! FALLOCATE */
2459 fallocate_calls
= 0;
2464 void remove_image(rados_ioctx_t ioctx
, char *imagename
, bool remove_snap
,
2470 if ((ret
= rbd_open(ioctx
, imagename
, &image
, NULL
)) < 0) {
2471 sprintf(errmsg
, "rbd_open %s", imagename
);
2472 prterrcode(errmsg
, ret
);
2473 report_failure(101);
2476 if ((ret
= rbd_snap_unprotect(image
, "snap")) < 0) {
2477 sprintf(errmsg
, "rbd_snap_unprotect %s@snap",
2479 prterrcode(errmsg
, ret
);
2480 report_failure(102);
2482 if ((ret
= rbd_snap_remove(image
, "snap")) < 0) {
2483 sprintf(errmsg
, "rbd_snap_remove %s@snap",
2485 prterrcode(errmsg
, ret
);
2486 report_failure(103);
2489 if ((ret
= rbd_close(image
)) < 0) {
2490 sprintf(errmsg
, "rbd_close %s", imagename
);
2491 prterrcode(errmsg
, ret
);
2492 report_failure(104);
2496 (ret
= unregister_journal(ioctx
, imagename
)) < 0) {
2497 report_failure(105);
2500 if ((ret
= rbd_remove(ioctx
, imagename
)) < 0) {
2501 sprintf(errmsg
, "rbd_remove %s", imagename
);
2502 prterrcode(errmsg
, ret
);
2503 report_failure(106);
2508 main(int argc
, char **argv
)
2510 int i
, style
, ch
, ret
;
2512 char goodfile
[1024];
2518 page_size
= getpagesize();
2519 page_mask
= page_size
- 1;
2520 mmap_mask
= page_mask
;
2522 setvbuf(stdout
, (char *)0, _IOLBF
, 0); /* line buffered stdout */
2524 while ((ch
= getopt(argc
, argv
, "b:c:dfh:jkl:m:no:p:qr:s:t:w:xyCD:FHKMLN:OP:RS:UWZ"))
2528 simulatedopcount
= getnum(optarg
, &endp
);
2530 fprintf(stdout
, "Will begin at operation %lu\n",
2532 if (simulatedopcount
== 0)
2534 simulatedopcount
-= 1;
2537 closeprob
= getnum(optarg
, &endp
);
2540 "Chance of close/open is 1 in %d\n",
2552 holebdy
= getnum(optarg
, &endp
);
2557 journal_replay
= true;
2560 keep_on_success
= 1;
2564 int _num
= getnum(optarg
, &endp
);
2571 monitorstart
= getnum(optarg
, &endp
);
2572 if (monitorstart
< 0)
2574 if (!endp
|| *endp
++ != ':')
2576 monitorend
= getnum(endp
, &endp
);
2579 if (monitorend
== 0)
2580 monitorend
= -1; /* aka infinity */
2587 maxoplen
= getnum(optarg
, &endp
);
2592 progressinterval
= getnum(optarg
, &endp
);
2593 if (progressinterval
== 0)
2600 readbdy
= getnum(optarg
, &endp
);
2605 style
= getnum(optarg
, &endp
);
2606 if (style
< 0 || style
> 1)
2610 truncbdy
= getnum(optarg
, &endp
);
2615 writebdy
= getnum(optarg
, &endp
);
2629 debugstart
= getnum(optarg
, &endp
);
2634 fallocate_calls
= 0;
2637 punch_hole_calls
= 0;
2640 prt("krbd mode enabled\n");
2641 ops
= &krbd_operations
;
2644 prt("rbd-nbd mode enabled\n");
2645 ops
= &nbd_operations
;
2648 prt("lite mode not supported for rbd\n");
2652 numops
= getnum(optarg
, &endp
);
2660 strncpy(dirpath
, optarg
, sizeof(dirpath
)-1);
2661 dirpath
[sizeof(dirpath
)-1] = '\0';
2662 strncpy(goodfile
, dirpath
, sizeof(goodfile
)-1);
2663 goodfile
[sizeof(goodfile
)-1] = '\0';
2664 if (strlen(goodfile
) < sizeof(goodfile
)-2) {
2665 strcat(goodfile
, "/");
2667 prt("file name to long\n");
2670 strncpy(logfile
, dirpath
, sizeof(logfile
)-1);
2671 logfile
[sizeof(logfile
)-1] = '\0';
2672 if (strlen(logfile
) < sizeof(logfile
)-2) {
2673 strcat(logfile
, "/");
2675 prt("file path to long\n");
2682 fprintf(stdout
, "mapped reads DISABLED\n");
2685 seed
= getnum(optarg
, &endp
);
2687 seed
= time(0) % 10000;
2689 fprintf(stdout
, "Seed set to %d\n", seed
);
2694 randomize_striping
= 0;
2699 fprintf(stdout
, "mapped writes DISABLED\n");
2702 o_direct
= O_DIRECT
;
2715 signal(SIGHUP
, cleanup
);
2716 signal(SIGINT
, cleanup
);
2717 signal(SIGPIPE
, cleanup
);
2718 signal(SIGALRM
, cleanup
);
2719 signal(SIGTERM
, cleanup
);
2720 signal(SIGXCPU
, cleanup
);
2721 signal(SIGXFSZ
, cleanup
);
2722 signal(SIGVTALRM
, cleanup
);
2723 signal(SIGUSR1
, cleanup
);
2724 signal(SIGUSR2
, cleanup
);
2726 random_generator
.seed(seed
);
2728 ret
= create_image();
2730 prterrcode(iname
, ret
);
2733 ret
= ops
->open(iname
, &ctx
);
2735 simple_err("Error opening image", ret
);
2739 strcat(dirpath
, ".");
2740 strncat(goodfile
, iname
, 256);
2741 strcat (goodfile
, ".fsxgood");
2742 fsxgoodfd
= open(goodfile
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
2743 if (fsxgoodfd
< 0) {
2747 strncat(logfile
, iname
, 256);
2748 strcat (logfile
, ".fsxlog");
2749 fsxlogf
= fopen(logfile
, "w");
2750 if (fsxlogf
== NULL
) {
2755 original_buf
= (char *) malloc(maxfilelen
);
2756 for (i
= 0; i
< (int)maxfilelen
; i
++)
2757 original_buf
[i
] = get_random() % 256;
2759 ret
= posix_memalign((void **)&good_buf
,
2760 MAX(writebdy
, (int)sizeof(void *)), maxfilelen
);
2763 prt("writebdy is not a suitable power of two\n");
2765 prterrcode("main: posix_memalign(good_buf)", -ret
);
2768 memset(good_buf
, '\0', maxfilelen
);
2770 ret
= posix_memalign((void **)&temp_buf
,
2771 MAX(readbdy
, (int)sizeof(void *)), maxfilelen
);
2774 prt("readbdy is not a suitable power of two\n");
2776 prterrcode("main: posix_memalign(temp_buf)", -ret
);
2779 memset(temp_buf
, '\0', maxfilelen
);
2781 if (lite
) { /* zero entire existing file */
2784 written
= ops
->write(&ctx
, 0, (size_t)maxfilelen
, good_buf
);
2785 if (written
!= (ssize_t
)maxfilelen
) {
2787 prterrcode(iname
, written
);
2788 warn("main: error on write");
2790 warn("main: short write, 0x%x bytes instead "
2801 while (numops
== -1 || numops
--)
2804 ret
= ops
->close(&ctx
);
2806 prterrcode("ops->close", ret
);
2810 if (journal_replay
) {
2811 char imagename
[1024];
2812 clone_imagename(imagename
, sizeof(imagename
), num_clones
);
2813 ret
= finalize_journal(ioctx
, imagename
, num_clones
, 0, 0, 0);
2815 report_failure(100);
2819 if (num_clones
> 0) {
2820 if (journal_replay
) {
2821 check_clone(num_clones
- 1, true);
2823 check_clone(num_clones
- 1, false);
2826 if (!keep_on_success
) {
2827 while (num_clones
>= 0) {
2828 static bool remove_snap
= false;
2830 if (journal_replay
) {
2831 char replayimagename
[1024];
2832 replay_imagename(replayimagename
,
2833 sizeof(replayimagename
),
2835 remove_image(ioctx
, replayimagename
,
2840 char clonename
[128];
2841 clone_imagename(clonename
, 128, num_clones
);
2842 remove_image(ioctx
, clonename
, remove_snap
,
2850 prt("All operations completed A-OK!\n");
2853 rados_ioctx_destroy(ioctx
);
2855 rados_shutdown(cluster
);