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:
92 * COMPAREANDWRITE: - 8
94 * When mapped read/writes are disabled, they are simply converted to normal
95 * reads and writes. When fallocate/fpunch calls are disabled, they are
96 * converted to OP_SKIPPED. Hence OP_SKIPPED needs to have a number higher than
97 * the operation selction matrix, as does the OP_CLOSEOPEN which is an
98 * operation modifier rather than an operation in itself.
100 * Because of the "lite" version, we also need to have different "maximum
101 * operation" defines to allow the ops to be selected correctly based on the
105 /* common operations */
109 #define OP_MAPWRITE 3
110 #define OP_MAX_LITE 4
112 /* !lite operations */
113 #define OP_TRUNCATE 4
114 #define OP_FALLOCATE 5
115 #define OP_PUNCH_HOLE 6
116 #define OP_WRITESAME 7
117 #define OP_COMPARE_AND_WRITE 8
118 /* rbd-specific operations */
120 #define OP_FLATTEN 10
121 #define OP_MAX_FULL 11
123 /* operation modifiers */
124 #define OP_CLOSEOPEN 100
125 #define OP_SKIPPED 101
128 #define PAGE_SIZE getpagesize()
130 #define PAGE_MASK (PAGE_SIZE - 1)
133 char *original_buf
; /* a pointer to the original data */
134 char *good_buf
; /* a pointer to the correct data */
135 char *temp_buf
; /* a pointer to the current data */
141 unsigned long testcalls
= 0; /* calls to function "test" */
143 unsigned long simulatedopcount
= 0; /* -b flag */
144 int closeprob
= 0; /* -c flag */
145 int debug
= 0; /* -d flag */
146 unsigned long debugstart
= 0; /* -D flag */
147 int flush_enabled
= 0; /* -f flag */
148 int holebdy
= 1; /* -h flag */
149 bool journal_replay
= false; /* -j flah */
150 int keep_on_success
= 0; /* -k flag */
151 int do_fsync
= 0; /* -y flag */
152 unsigned long maxfilelen
= 256 * 1024; /* -l flag */
153 int sizechecks
= 1; /* -n flag disables them */
154 int maxoplen
= 64 * 1024; /* -o flag */
155 int quiet
= 0; /* -q flag */
156 unsigned long progressinterval
= 0; /* -p flag */
157 int readbdy
= 1; /* -r flag */
158 int style
= 0; /* -s flag */
159 int prealloc
= 0; /* -x flag */
160 int truncbdy
= 1; /* -t flag */
161 int writebdy
= 1; /* -w flag */
162 long monitorstart
= -1; /* -m flag */
163 long monitorend
= -1; /* -m flag */
164 int lite
= 0; /* -L flag */
165 long numops
= -1; /* -N flag */
166 int randomoplen
= 1; /* -O flag disables it */
167 int seed
= 1; /* -S flag */
168 int mapped_writes
= 0; /* -W flag disables */
169 int fallocate_calls
= 0; /* -F flag disables */
170 int punch_hole_calls
= 1; /* -H flag disables */
171 int clone_calls
= 1; /* -C flag disables */
172 int randomize_striping
= 1; /* -U flag disables */
173 int randomize_parent_overlap
= 1;
174 int mapped_reads
= 0; /* -R flag disables it */
176 int o_direct
= 0; /* -Z flag */
184 FILE * fsxlogf
= NULL
;
189 vwarnc(int code
, const char *fmt
, va_list ap
) {
190 fprintf(stderr
, "fsx: ");
192 vfprintf(stderr
, fmt
, ap
);
193 fprintf(stderr
, ": ");
195 fprintf(stderr
, "%s\n", strerror(code
));
199 warn(const char * fmt
, ...) {
202 vwarnc(errno
, fmt
, ap
);
206 #define BUF_SIZE 1024
209 prt(const char *fmt
, ...)
212 char buffer
[BUF_SIZE
];
215 vsnprintf(buffer
, BUF_SIZE
, fmt
, args
);
217 fprintf(stdout
, "%s", buffer
);
219 fprintf(fsxlogf
, "%s", buffer
);
223 prterr(const char *prefix
)
225 prt("%s%s%s\n", prefix
, prefix
? ": " : "", strerror(errno
));
229 prterrcode(const char *prefix
, int code
)
231 prt("%s%s%s\n", prefix
, prefix
? ": " : "", strerror(-code
));
235 simple_err(const char *msg
, int err
)
237 fprintf(stderr
, "%s: %s\n", msg
, strerror(-err
));
243 std::mt19937 random_generator
;
248 return random_generator();
251 void replay_imagename(char *buf
, size_t len
, int clones
);
255 static const std::string
JOURNAL_CLIENT_ID("fsx");
257 struct ReplayHandler
: public journal::ReplayHandler
{
258 journal::Journaler
*journaler
;
259 journal::Journaler
*replay_journaler
;
262 ReplayHandler(journal::Journaler
*journaler
,
263 journal::Journaler
*replay_journaler
, Context
*on_finish
)
264 : journaler(journaler
), replay_journaler(replay_journaler
),
265 on_finish(on_finish
) {
268 void get() override
{
270 void put() override
{
273 void handle_entries_available() override
{
275 journal::ReplayEntry replay_entry
;
276 if (!journaler
->try_pop_front(&replay_entry
)) {
280 replay_journaler
->append(0, replay_entry
.get_data());
284 void handle_complete(int r
) override
{
285 on_finish
->complete(r
);
289 int get_image_id(librados::IoCtx
&io_ctx
, const char *image_name
,
290 std::string
*image_id
) {
293 int r
= rbd
.open(io_ctx
, image
, image_name
);
295 simple_err("failed to open image", r
);
299 rbd_image_info_t info
;
300 r
= image
.stat(info
, sizeof(info
));
302 simple_err("failed to stat image", r
);
306 *image_id
= std::string(&info
.block_name_prefix
[strlen(RBD_DATA_PREFIX
)]);
310 int register_journal(rados_ioctx_t ioctx
, const char *image_name
) {
311 librados::IoCtx io_ctx
;
312 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
314 std::string image_id
;
315 int r
= get_image_id(io_ctx
, image_name
, &image_id
);
320 journal::Journaler
journaler(io_ctx
, image_id
, JOURNAL_CLIENT_ID
, {});
321 r
= journaler
.register_client(bufferlist());
323 simple_err("failed to register journal client", r
);
329 int unregister_journal(rados_ioctx_t ioctx
, const char *image_name
) {
330 librados::IoCtx io_ctx
;
331 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
333 std::string image_id
;
334 int r
= get_image_id(io_ctx
, image_name
, &image_id
);
339 journal::Journaler
journaler(io_ctx
, image_id
, JOURNAL_CLIENT_ID
, {});
340 r
= journaler
.unregister_client();
342 simple_err("failed to unregister journal client", r
);
348 int create_replay_image(rados_ioctx_t ioctx
, int order
,
349 uint64_t stripe_unit
, int stripe_count
,
350 const char *replay_image_name
,
351 const char *last_replay_image_name
) {
352 librados::IoCtx io_ctx
;
353 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
357 if (last_replay_image_name
== nullptr) {
358 r
= rbd
.create2(io_ctx
, replay_image_name
, 0,
359 RBD_FEATURES_ALL
, &order
);
361 r
= rbd
.clone2(io_ctx
, last_replay_image_name
, "snap",
362 io_ctx
, replay_image_name
, RBD_FEATURES_ALL
,
363 &order
, stripe_unit
, stripe_count
);
367 simple_err("failed to create replay image", r
);
374 int replay_journal(rados_ioctx_t ioctx
, const char *image_name
,
375 const char *replay_image_name
) {
376 librados::IoCtx io_ctx
;
377 librados::IoCtx::from_rados_ioctx_t(ioctx
, io_ctx
);
379 std::string image_id
;
380 int r
= get_image_id(io_ctx
, image_name
, &image_id
);
385 std::string replay_image_id
;
386 r
= get_image_id(io_ctx
, replay_image_name
, &replay_image_id
);
391 journal::Journaler
journaler(io_ctx
, image_id
, JOURNAL_CLIENT_ID
, {});
392 C_SaferCond init_ctx
;
393 journaler
.init(&init_ctx
);
394 BOOST_SCOPE_EXIT_ALL( (&journaler
) ) {
395 journaler
.shut_down();
400 simple_err("failed to initialize journal", r
);
404 journal::Journaler
replay_journaler(io_ctx
, replay_image_id
, "", {});
406 C_SaferCond replay_init_ctx
;
407 replay_journaler
.init(&replay_init_ctx
);
408 BOOST_SCOPE_EXIT_ALL( (&replay_journaler
) ) {
409 replay_journaler
.shut_down();
412 r
= replay_init_ctx
.wait();
414 simple_err("failed to initialize replay journal", r
);
418 replay_journaler
.start_append(0, 0, 0);
420 C_SaferCond replay_ctx
;
421 ReplayHandler
replay_handler(&journaler
, &replay_journaler
,
424 // copy journal events from source image to replay image
425 journaler
.start_replay(&replay_handler
);
426 r
= replay_ctx
.wait();
428 journaler
.stop_replay();
430 C_SaferCond stop_ctx
;
431 replay_journaler
.stop_append(&stop_ctx
);
432 int stop_r
= stop_ctx
.wait();
433 if (r
== 0 && stop_r
< 0) {
438 simple_err("failed to replay journal", r
);
444 r
= rbd
.open(io_ctx
, image
, replay_image_name
);
446 simple_err("failed to open replay image", r
);
450 // perform an IO op to initiate the journal replay
452 r
= static_cast<ssize_t
>(image
.write(0, 0, bl
));
454 simple_err("failed to write to replay image", r
);
460 int finalize_journal(rados_ioctx_t ioctx
, const char *imagename
, int clones
,
461 int order
, uint64_t stripe_unit
, int stripe_count
) {
462 char replayimagename
[1024];
463 replay_imagename(replayimagename
, sizeof(replayimagename
), clones
);
465 char lastreplayimagename
[1024];
467 replay_imagename(lastreplayimagename
,
468 sizeof(lastreplayimagename
), clones
- 1);
471 int ret
= create_replay_image(ioctx
, order
, stripe_unit
,
472 stripe_count
, replayimagename
,
473 clones
> 0 ? lastreplayimagename
:
479 ret
= replay_journal(ioctx
, imagename
, replayimagename
);
486 } // anonymous namespace
493 const char *name
; /* image name */
494 rbd_image_t image
; /* image handle */
495 const char *krbd_name
; /* image /dev/rbd<id> name */ /* reused for nbd test */
496 int krbd_fd
; /* image /dev/rbd<id> fd */ /* reused for nbd test */
499 #define RBD_CTX_INIT (struct rbd_ctx) { NULL, NULL, NULL, -1}
501 struct rbd_operations
{
502 int (*open
)(const char *name
, struct rbd_ctx
*ctx
);
503 int (*close
)(struct rbd_ctx
*ctx
);
504 ssize_t (*read
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, char *buf
);
505 ssize_t (*write
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, const char *buf
);
506 int (*flush
)(struct rbd_ctx
*ctx
);
507 int (*discard
)(struct rbd_ctx
*ctx
, uint64_t off
, uint64_t len
);
508 int (*get_size
)(struct rbd_ctx
*ctx
, uint64_t *size
);
509 int (*resize
)(struct rbd_ctx
*ctx
, uint64_t size
);
510 int (*clone
)(struct rbd_ctx
*ctx
, const char *src_snapname
,
511 const char *dst_imagename
, int *order
, int stripe_unit
,
513 int (*flatten
)(struct rbd_ctx
*ctx
);
514 ssize_t (*writesame
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
,
515 const char *buf
, size_t data_len
);
516 ssize_t (*compare_and_write
)(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
,
517 const char *cmp_buf
, const char *buf
);
520 char *pool
; /* name of the pool our test image is in */
521 char *iname
; /* name of our test image */
522 rados_t cluster
; /* handle for our test cluster */
523 rados_ioctx_t ioctx
; /* handle for our test pool */
524 struct krbd_ctx
*krbd
; /* handle for libkrbd */
525 bool skip_partial_discard
; /* rbd_skip_partial_discard config value*/
528 * librbd/krbd rbd_operations handlers. Given the rest of fsx.c, no
529 * attempt to do error handling is made in these handlers.
533 __librbd_open(const char *name
, struct rbd_ctx
*ctx
)
538 assert(!ctx
->name
&& !ctx
->image
&&
539 !ctx
->krbd_name
&& ctx
->krbd_fd
< 0);
541 ret
= rbd_open(ioctx
, name
, &image
, NULL
);
543 prt("rbd_open(%s) failed\n", name
);
547 ctx
->name
= strdup(name
);
549 ctx
->krbd_name
= NULL
;
556 librbd_open(const char *name
, struct rbd_ctx
*ctx
)
558 return __librbd_open(name
, ctx
);
562 __librbd_close(struct rbd_ctx
*ctx
)
566 assert(ctx
->name
&& ctx
->image
);
568 ret
= rbd_close(ctx
->image
);
570 prt("rbd_close(%s) failed\n", ctx
->name
);
574 free((void *)ctx
->name
);
583 librbd_close(struct rbd_ctx
*ctx
)
585 return __librbd_close(ctx
);
589 librbd_verify_object_map(struct rbd_ctx
*ctx
)
593 n
= rbd_get_flags(ctx
->image
, &flags
);
595 prt("rbd_get_flags() failed\n");
599 if ((flags
& RBD_FLAG_OBJECT_MAP_INVALID
) != 0) {
600 prt("rbd_get_flags() indicates object map is invalid\n");
607 librbd_read(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, char *buf
)
611 n
= rbd_read(ctx
->image
, off
, len
, buf
);
613 prt("rbd_read(%llu, %zu) failed\n", off
, len
);
619 librbd_write(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, const char *buf
)
624 n
= rbd_write(ctx
->image
, off
, len
, buf
);
626 prt("rbd_write(%llu, %zu) failed\n", off
, len
);
630 ret
= librbd_verify_object_map(ctx
);
638 librbd_flush(struct rbd_ctx
*ctx
)
642 ret
= rbd_flush(ctx
->image
);
644 prt("rbd_flush failed\n");
648 return librbd_verify_object_map(ctx
);
652 librbd_discard(struct rbd_ctx
*ctx
, uint64_t off
, uint64_t len
)
656 ret
= rbd_discard(ctx
->image
, off
, len
);
658 prt("rbd_discard(%llu, %llu) failed\n", off
, len
);
662 return librbd_verify_object_map(ctx
);
666 librbd_writesame(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
,
667 const char *buf
, size_t data_len
)
672 n
= rbd_writesame(ctx
->image
, off
, len
, buf
, data_len
, 0);
674 prt("rbd_writesame(%llu, %zu) failed\n", off
, len
);
678 ret
= librbd_verify_object_map(ctx
);
686 librbd_compare_and_write(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
,
687 const char *cmp_buf
, const char *buf
)
691 uint64_t mismatch_off
= 0;
693 n
= rbd_compare_and_write(ctx
->image
, off
, len
, cmp_buf
, buf
, &mismatch_off
, 0);
697 prt("rbd_compare_and_write mismatch(%llu, %zu, %llu) failed\n",
698 off
, len
, mismatch_off
);
702 ret
= librbd_verify_object_map(ctx
);
711 librbd_get_size(struct rbd_ctx
*ctx
, uint64_t *size
)
713 rbd_image_info_t info
;
716 ret
= rbd_stat(ctx
->image
, &info
, sizeof(info
));
718 prt("rbd_stat failed\n");
728 __librbd_resize(struct rbd_ctx
*ctx
, uint64_t size
)
732 ret
= rbd_resize(ctx
->image
, size
);
734 prt("rbd_resize(%llu) failed\n", size
);
738 return librbd_verify_object_map(ctx
);
742 librbd_resize(struct rbd_ctx
*ctx
, uint64_t size
)
744 return __librbd_resize(ctx
, size
);
748 __librbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
749 const char *dst_imagename
, int *order
, int stripe_unit
,
750 int stripe_count
, bool krbd
)
754 ret
= rbd_snap_create(ctx
->image
, src_snapname
);
756 prt("rbd_snap_create(%s@%s) failed\n", ctx
->name
,
761 ret
= rbd_snap_protect(ctx
->image
, src_snapname
);
763 prt("rbd_snap_protect(%s@%s) failed\n", ctx
->name
,
768 uint64_t features
= RBD_FEATURES_ALL
;
770 features
&= ~(RBD_FEATURE_OBJECT_MAP
|
771 RBD_FEATURE_FAST_DIFF
|
772 RBD_FEATURE_DEEP_FLATTEN
|
773 RBD_FEATURE_JOURNALING
);
775 ret
= rbd_clone2(ioctx
, ctx
->name
, src_snapname
, ioctx
,
776 dst_imagename
, features
, order
,
777 stripe_unit
, stripe_count
);
779 prt("rbd_clone2(%s@%s -> %s) failed\n", ctx
->name
,
780 src_snapname
, dst_imagename
);
788 librbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
789 const char *dst_imagename
, int *order
, int stripe_unit
,
792 return __librbd_clone(ctx
, src_snapname
, dst_imagename
, order
,
793 stripe_unit
, stripe_count
, false);
797 __librbd_flatten(struct rbd_ctx
*ctx
)
801 ret
= rbd_flatten(ctx
->image
);
803 prt("rbd_flatten failed\n");
807 return librbd_verify_object_map(ctx
);
811 librbd_flatten(struct rbd_ctx
*ctx
)
813 return __librbd_flatten(ctx
);
816 const struct rbd_operations librbd_operations
= {
828 librbd_compare_and_write
,
832 krbd_open(const char *name
, struct rbd_ctx
*ctx
)
838 ret
= __librbd_open(name
, ctx
);
842 ret
= krbd_map(krbd
, pool
, name
, "", "", &devnode
);
844 prt("krbd_map(%s) failed\n", name
);
848 fd
= open(devnode
, O_RDWR
| o_direct
);
851 prt("open(%s) failed\n", devnode
);
855 ctx
->krbd_name
= devnode
;
862 krbd_close(struct rbd_ctx
*ctx
)
866 assert(ctx
->krbd_name
&& ctx
->krbd_fd
>= 0);
868 if (close(ctx
->krbd_fd
) < 0) {
870 prt("close(%s) failed\n", ctx
->krbd_name
);
874 ret
= krbd_unmap(krbd
, ctx
->krbd_name
, "");
876 prt("krbd_unmap(%s) failed\n", ctx
->krbd_name
);
880 free((void *)ctx
->krbd_name
);
882 ctx
->krbd_name
= NULL
;
885 return __librbd_close(ctx
);
889 krbd_read(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, char *buf
)
893 n
= pread(ctx
->krbd_fd
, buf
, len
, off
);
896 prt("pread(%llu, %zu) failed\n", off
, len
);
904 krbd_write(struct rbd_ctx
*ctx
, uint64_t off
, size_t len
, const char *buf
)
908 n
= pwrite(ctx
->krbd_fd
, buf
, len
, off
);
911 prt("pwrite(%llu, %zu) failed\n", off
, len
);
919 __krbd_flush(struct rbd_ctx
*ctx
, bool invalidate
)
927 * BLKFLSBUF will sync the filesystem on top of the device (we
928 * don't care about that here, since we write directly to it),
929 * write out any dirty buffers and invalidate the buffer cache.
930 * It won't do a hardware cache flush.
932 * fsync() will write out any dirty buffers and do a hardware
933 * cache flush (which we don't care about either, because for
934 * krbd it's a noop). It won't try to empty the buffer cache
935 * nor poke the filesystem before writing out.
937 * Given that, for our purposes, fsync is a flush, while
938 * BLKFLSBUF is a flush+invalidate.
941 ret
= ioctl(ctx
->krbd_fd
, BLKFLSBUF
, NULL
);
943 ret
= fsync(ctx
->krbd_fd
);
946 prt("%s failed\n", invalidate
? "BLKFLSBUF" : "fsync");
954 krbd_flush(struct rbd_ctx
*ctx
)
956 return __krbd_flush(ctx
, false);
960 krbd_discard(struct rbd_ctx
*ctx
, uint64_t off
, uint64_t len
)
962 uint64_t range
[2] = { off
, len
};
966 * BLKDISCARD goes straight to disk and doesn't do anything
967 * about dirty buffers. This means we need to flush so that
972 * results in "data 0000 data" rather than "data data data" on
973 * disk and invalidate so that
978 * returns "data 0000 data" rather than "data data data" in
979 * case 1..2M was cached.
981 ret
= __krbd_flush(ctx
, true);
986 * off and len must be 512-byte aligned, otherwise BLKDISCARD
987 * will fail with -EINVAL. This means that -K (enable krbd
988 * mode) requires -h 512 or similar.
990 if (ioctl(ctx
->krbd_fd
, BLKDISCARD
, &range
) < 0) {
992 prt("BLKDISCARD(%llu, %llu) failed\n", off
, len
);
1000 krbd_get_size(struct rbd_ctx
*ctx
, uint64_t *size
)
1004 if (ioctl(ctx
->krbd_fd
, BLKGETSIZE64
, &bytes
) < 0) {
1006 prt("BLKGETSIZE64 failed\n");
1016 krbd_resize(struct rbd_ctx
*ctx
, uint64_t size
)
1020 assert(size
% truncbdy
== 0);
1023 * When krbd detects a size change, it calls revalidate_disk(),
1024 * which ends up calling invalidate_bdev(), which invalidates
1025 * clean pages and does nothing about dirty pages beyond the
1026 * new size. The preceding cache flush makes sure those pages
1027 * are invalidated, which is what we need on shrink so that
1034 * returns "0000 0000" rather than "data 0000".
1036 ret
= __krbd_flush(ctx
, false);
1040 return __librbd_resize(ctx
, size
);
1044 krbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
1045 const char *dst_imagename
, int *order
, int stripe_unit
,
1050 ret
= __krbd_flush(ctx
, false);
1054 return __librbd_clone(ctx
, src_snapname
, dst_imagename
, order
,
1055 stripe_unit
, stripe_count
, true);
1059 krbd_flatten(struct rbd_ctx
*ctx
)
1063 ret
= __krbd_flush(ctx
, false);
1067 return __librbd_flatten(ctx
);
1070 const struct rbd_operations krbd_operations
= {
1085 nbd_open(const char *name
, struct rbd_ctx
*ctx
)
1092 SubProcess
process("rbd-nbd", SubProcess::KEEP
, SubProcess::PIPE
,
1094 process
.add_cmd_arg("map");
1099 process
.add_cmd_arg(img
.c_str());
1101 r
= __librbd_open(name
, ctx
);
1105 r
= process
.spawn();
1107 prt("nbd_open failed to run rbd-nbd error: %s\n", process
.err().c_str());
1110 r
= safe_read(process
.get_stdout(), dev
, sizeof(dev
));
1112 prt("nbd_open failed to get nbd device path\n");
1115 for (int i
= 0; i
< r
; ++i
)
1116 if (dev
[i
] == 10 || dev
[i
] == 13)
1121 prt("rbd-nbd failed with error: %s", process
.err().c_str());
1125 devnode
= strdup(dev
);
1129 fd
= open(devnode
, O_RDWR
| o_direct
);
1132 prt("open(%s) failed\n", devnode
);
1136 ctx
->krbd_name
= devnode
;
1143 nbd_close(struct rbd_ctx
*ctx
)
1147 assert(ctx
->krbd_name
&& ctx
->krbd_fd
>= 0);
1149 if (close(ctx
->krbd_fd
) < 0) {
1151 prt("close(%s) failed\n", ctx
->krbd_name
);
1155 SubProcess
process("rbd-nbd");
1156 process
.add_cmd_arg("unmap");
1157 process
.add_cmd_arg(ctx
->krbd_name
);
1159 r
= process
.spawn();
1161 prt("nbd_close failed to run rbd-nbd error: %s\n", process
.err().c_str());
1166 prt("rbd-nbd failed with error: %d", process
.err().c_str());
1170 free((void *)ctx
->krbd_name
);
1172 ctx
->krbd_name
= NULL
;
1175 return __librbd_close(ctx
);
1179 nbd_clone(struct rbd_ctx
*ctx
, const char *src_snapname
,
1180 const char *dst_imagename
, int *order
, int stripe_unit
,
1185 ret
= __krbd_flush(ctx
, false);
1189 return __librbd_clone(ctx
, src_snapname
, dst_imagename
, order
,
1190 stripe_unit
, stripe_count
, false);
1193 const struct rbd_operations nbd_operations
= {
1207 struct rbd_ctx ctx
= RBD_CTX_INIT
;
1208 const struct rbd_operations
*ops
= &librbd_operations
;
1210 static bool rbd_image_has_parent(struct rbd_ctx
*ctx
)
1214 ret
= rbd_get_parent_info(ctx
->image
, NULL
, 0, NULL
, 0, NULL
, 0);
1215 if (ret
< 0 && ret
!= -ENOENT
) {
1216 prterrcode("rbd_get_parent_info", ret
);
1228 log4(int operation
, int arg0
, int arg1
, int arg2
)
1230 struct log_entry
*le
;
1232 le
= &oplog
[logptr
];
1233 le
->operation
= operation
;
1235 le
->operation
= ~ le
->operation
;
1241 if (logptr
>= LOGSIZE
)
1249 struct log_entry
*lp
;
1250 const char *falloc_type
[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
1252 prt("LOG DUMP (%d total operations):\n", logcount
);
1253 if (logcount
< LOGSIZE
) {
1260 for ( ; count
> 0; count
--) {
1263 opnum
= i
+1 + (logcount
/LOGSIZE
)*LOGSIZE
;
1264 prt("%d(%3d mod 256): ", opnum
, opnum
%256);
1266 if ((closeopen
= lp
->operation
< 0))
1267 lp
->operation
= ~ lp
->operation
;
1269 switch (lp
->operation
) {
1271 prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
1272 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1274 if (badoff
>= lp
->args
[0] && badoff
<
1275 lp
->args
[0] + lp
->args
[1])
1276 prt("\t***RRRR***");
1279 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
1280 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1282 if (badoff
>= lp
->args
[0] && badoff
<
1283 lp
->args
[0] + lp
->args
[1])
1284 prt("\t******WWWW");
1287 prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
1288 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1290 if (badoff
>= lp
->args
[0] &&
1291 badoff
< lp
->args
[0] + lp
->args
[1])
1292 prt("\t***RRRR***");
1295 prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
1296 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1298 if (lp
->args
[0] > lp
->args
[2])
1300 else if (lp
->args
[0] + lp
->args
[1] > lp
->args
[2])
1302 if ((badoff
>= lp
->args
[0] || badoff
>=lp
->args
[2]) &&
1303 badoff
< lp
->args
[0] + lp
->args
[1])
1307 down
= lp
->args
[0] < lp
->args
[1];
1308 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
1309 down
? "DOWN" : "UP", lp
->args
[1], lp
->args
[0]);
1310 if (badoff
>= lp
->args
[!down
] &&
1311 badoff
< lp
->args
[!!down
])
1312 prt("\t******WWWW");
1315 /* 0: offset 1: length 2: where alloced */
1316 prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) %s",
1317 lp
->args
[0], lp
->args
[0] + lp
->args
[1],
1318 lp
->args
[1], falloc_type
[lp
->args
[2]]);
1319 if (badoff
>= lp
->args
[0] &&
1320 badoff
< lp
->args
[0] + lp
->args
[1])
1321 prt("\t******FFFF");
1324 prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
1325 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1327 if (badoff
>= lp
->args
[0] && badoff
<
1328 lp
->args
[0] + lp
->args
[1])
1329 prt("\t******PPPP");
1332 prt("WRITESAME 0x%x thru 0x%x\t(0x%x bytes) data_size 0x%x",
1333 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1334 lp
->args
[1], lp
->args
[2]);
1335 if (badoff
>= lp
->args
[0] &&
1336 badoff
< lp
->args
[0] + lp
->args
[1])
1337 prt("\t***WSWSWSWS");
1339 case OP_COMPARE_AND_WRITE
:
1340 prt("COMPARE_AND_WRITE 0x%x thru 0x%x\t(0x%x bytes)",
1341 lp
->args
[0], lp
->args
[0] + lp
->args
[1] - 1,
1343 if (lp
->args
[0] > lp
->args
[2])
1345 else if (lp
->args
[0] + lp
->args
[1] > lp
->args
[2])
1347 if ((badoff
>= lp
->args
[0] || badoff
>=lp
->args
[2]) &&
1348 badoff
< lp
->args
[0] + lp
->args
[1])
1358 prt("SKIPPED (no operation)");
1361 prt("BOGUS LOG ENTRY (operation code = %d)!",
1365 prt("\n\t\tCLOSE/OPEN");
1374 save_buffer(char *buffer
, off_t bufferlength
, int fd
)
1377 ssize_t byteswritten
;
1379 if (fd
<= 0 || bufferlength
== 0)
1382 if (bufferlength
> SSIZE_MAX
) {
1383 prt("fsx flaw: overflow in save_buffer\n");
1387 ret
= lseek(fd
, (off_t
)0, SEEK_SET
);
1388 if (ret
== (off_t
)-1)
1389 prterr("save_buffer: lseek 0");
1391 byteswritten
= write(fd
, buffer
, (size_t)bufferlength
);
1392 if (byteswritten
!= bufferlength
) {
1393 if (byteswritten
== -1)
1394 prterr("save_buffer write");
1396 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
1397 (unsigned)byteswritten
,
1398 (unsigned long long)bufferlength
);
1404 report_failure(int status
)
1410 save_buffer(good_buf
, file_size
, fsxgoodfd
);
1411 prt("Correct content saved for comparison\n");
1412 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
1417 sleep(3); // so the log can flush to disk. KLUDGEY!
1421 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
1422 *(((unsigned char *)(cp)) + 1)))
1425 fsxcmp(char *good_buf
, char *temp_buf
, unsigned size
)
1427 if (!skip_partial_discard
) {
1428 return memcmp(good_buf
, temp_buf
, size
);
1431 for (unsigned i
= 0; i
< size
; i
++) {
1432 if (good_buf
[i
] != temp_buf
[i
] && good_buf
[i
] != 0) {
1433 return good_buf
[i
] - temp_buf
[i
];
1440 check_buffers(char *good_buf
, char *temp_buf
, unsigned offset
, unsigned size
)
1442 if (fsxcmp(good_buf
+ offset
, temp_buf
, size
) != 0) {
1446 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
1447 offset
, size
, iname
);
1448 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
1450 unsigned char c
= good_buf
[offset
];
1451 unsigned char t
= temp_buf
[i
];
1454 unsigned bad
= short_at(&temp_buf
[i
]);
1455 prt("0x%5x\t0x%04x\t0x%04x", offset
,
1456 short_at(&good_buf
[offset
]), bad
);
1457 unsigned op
= temp_buf
[(offset
& 1) ? i
+1 : i
];
1458 prt("\t0x%5x\n", n
);
1460 prt("operation# (mod 256) for "
1461 "the bad data may be %u\n",
1462 ((unsigned)op
& 0xff));
1464 prt("operation# (mod 256) for "
1465 "the bad data unknown, check"
1466 " HOLE and EXTEND ops\n");
1475 report_failure(110);
1486 ret
= ops
->get_size(&ctx
, &size
);
1488 prterrcode("check_size: ops->get_size", ret
);
1490 if ((uint64_t)file_size
!= size
) {
1491 prt("Size error: expected 0x%llx stat 0x%llx\n",
1492 (unsigned long long)file_size
,
1493 (unsigned long long)size
);
1494 report_failure(120);
1498 #define TRUNC_HACK_SIZE (200ULL << 9) /* 512-byte aligned for krbd */
1501 check_trunc_hack(void)
1506 ret
= ops
->resize(&ctx
, 0ULL);
1508 prterrcode("check_trunc_hack: ops->resize pre", ret
);
1510 ret
= ops
->resize(&ctx
, TRUNC_HACK_SIZE
);
1512 prterrcode("check_trunc_hack: ops->resize actual", ret
);
1514 ret
= ops
->get_size(&ctx
, &size
);
1516 prterrcode("check_trunc_hack: ops->get_size", ret
);
1518 if (size
!= TRUNC_HACK_SIZE
) {
1519 prt("no extend on truncate! not posix!\n");
1523 ret
= ops
->resize(&ctx
, 0ULL);
1525 prterrcode("check_trunc_hack: ops->resize post", ret
);
1535 r
= rados_create(&cluster
, NULL
);
1537 simple_err("Could not create cluster handle", r
);
1540 rados_conf_parse_env(cluster
, NULL
);
1541 r
= rados_conf_read_file(cluster
, NULL
);
1543 simple_err("Error reading ceph config file", r
);
1544 goto failed_shutdown
;
1546 r
= rados_connect(cluster
);
1548 simple_err("Error connecting to cluster", r
);
1549 goto failed_shutdown
;
1551 r
= krbd_create_from_context(rados_cct(cluster
), &krbd
);
1553 simple_err("Could not create libkrbd handle", r
);
1554 goto failed_shutdown
;
1557 r
= rados_pool_create(cluster
, pool
);
1558 if (r
< 0 && r
!= -EEXIST
) {
1559 simple_err("Error creating pool", r
);
1562 r
= rados_ioctx_create(cluster
, pool
, &ioctx
);
1564 simple_err("Error creating ioctx", r
);
1567 rados_application_enable(ioctx
, "rbd", 1);
1569 if (clone_calls
|| journal_replay
) {
1570 uint64_t features
= 0;
1572 features
|= RBD_FEATURE_LAYERING
;
1574 if (journal_replay
) {
1575 features
|= (RBD_FEATURE_EXCLUSIVE_LOCK
|
1576 RBD_FEATURE_JOURNALING
);
1578 r
= rbd_create2(ioctx
, iname
, 0, features
, &order
);
1580 r
= rbd_create(ioctx
, iname
, 0, &order
);
1583 simple_err("Error creating image", r
);
1587 if (journal_replay
) {
1588 r
= register_journal(ioctx
, iname
);
1594 r
= rados_conf_get(cluster
, "rbd_skip_partial_discard", buf
,
1597 simple_err("Could not get rbd_skip_partial_discard value", r
);
1600 skip_partial_discard
= (strcmp(buf
, "true") == 0);
1605 rados_ioctx_destroy(ioctx
);
1609 rados_shutdown(cluster
);
1614 doflush(unsigned offset
, unsigned size
)
1621 ret
= ops
->flush(&ctx
);
1623 prterrcode("doflush: ops->flush", ret
);
1627 doread(unsigned offset
, unsigned size
)
1631 offset
-= offset
% readbdy
;
1633 size
-= size
% readbdy
;
1635 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
1636 prt("skipping zero size read\n");
1637 log4(OP_SKIPPED
, OP_READ
, offset
, size
);
1640 if (size
+ offset
> file_size
) {
1641 if (!quiet
&& testcalls
> simulatedopcount
)
1642 prt("skipping seek/read past end of file\n");
1643 log4(OP_SKIPPED
, OP_READ
, offset
, size
);
1647 log4(OP_READ
, offset
, size
, 0);
1649 if (testcalls
<= simulatedopcount
)
1653 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1655 (monitorstart
== -1 ||
1656 (static_cast<long>(offset
+ size
) > monitorstart
&&
1657 (monitorend
== -1 ||
1658 static_cast<long>(offset
) <= monitorend
))))))
1659 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
1660 offset
, offset
+ size
- 1, size
);
1662 ret
= ops
->read(&ctx
, offset
, size
, temp_buf
);
1663 if (ret
!= (int)size
) {
1665 prterrcode("doread: ops->read", ret
);
1667 prt("short read: 0x%x bytes instead of 0x%x\n",
1669 report_failure(141);
1672 check_buffers(good_buf
, temp_buf
, offset
, size
);
1677 check_eofpage(char *s
, unsigned offset
, char *p
, int size
)
1679 unsigned long last_page
, should_be_zero
;
1681 if (offset
+ size
<= (file_size
& ~page_mask
))
1684 * we landed in the last page of the file
1685 * test to make sure the VM system provided 0's
1686 * beyond the true end of the file mapping
1687 * (as required by mmap def in 1996 posix 1003.1)
1689 last_page
= ((unsigned long)p
+ (offset
& page_mask
) + size
) & ~page_mask
;
1691 for (should_be_zero
= last_page
+ (file_size
& page_mask
);
1692 should_be_zero
< last_page
+ page_size
;
1694 if (*(char *)should_be_zero
) {
1695 prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
1696 s
, file_size
- 1, should_be_zero
& page_mask
,
1697 short_at(should_be_zero
));
1698 report_failure(205);
1704 gendata(char *original_buf
, char *good_buf
, unsigned offset
, unsigned size
)
1707 good_buf
[offset
] = testcalls
% 256;
1709 good_buf
[offset
] += original_buf
[offset
];
1716 dowrite(unsigned offset
, unsigned size
)
1721 offset
-= offset
% writebdy
;
1723 size
-= size
% writebdy
;
1725 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
1726 prt("skipping zero size write\n");
1727 log4(OP_SKIPPED
, OP_WRITE
, offset
, size
);
1731 log4(OP_WRITE
, offset
, size
, file_size
);
1733 gendata(original_buf
, good_buf
, offset
, size
);
1734 if (file_size
< offset
+ size
) {
1735 newsize
= ceil(((double)offset
+ size
) / truncbdy
) * truncbdy
;
1736 if (file_size
< newsize
)
1737 memset(good_buf
+ file_size
, '\0', newsize
- file_size
);
1738 file_size
= newsize
;
1740 warn("Lite file size bug in fsx!");
1741 report_failure(149);
1743 ret
= ops
->resize(&ctx
, newsize
);
1745 prterrcode("dowrite: ops->resize", ret
);
1746 report_failure(150);
1750 if (testcalls
<= simulatedopcount
)
1754 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1756 (monitorstart
== -1 ||
1757 (static_cast<long>(offset
+ size
) > monitorstart
&&
1758 (monitorend
== -1 ||
1759 static_cast<long>(offset
) <= monitorend
))))))
1760 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
1761 offset
, offset
+ size
- 1, size
);
1763 ret
= ops
->write(&ctx
, offset
, size
, good_buf
+ offset
);
1764 if (ret
!= (ssize_t
)size
) {
1766 prterrcode("dowrite: ops->write", ret
);
1768 prt("short write: 0x%x bytes instead of 0x%x\n",
1770 report_failure(151);
1774 doflush(offset
, size
);
1779 dotruncate(unsigned size
)
1781 int oldsize
= file_size
;
1784 size
-= size
% truncbdy
;
1785 if (size
> biggest
) {
1787 if (!quiet
&& testcalls
> simulatedopcount
)
1788 prt("truncating to largest ever: 0x%x\n", size
);
1791 log4(OP_TRUNCATE
, size
, (unsigned)file_size
, 0);
1793 if (size
> file_size
)
1794 memset(good_buf
+ file_size
, '\0', size
- file_size
);
1795 else if (size
< file_size
)
1796 memset(good_buf
+ size
, '\0', file_size
- size
);
1799 if (testcalls
<= simulatedopcount
)
1802 if ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1803 (debug
&& (monitorstart
== -1 || monitorend
== -1 ||
1804 (long)size
<= monitorend
)))
1805 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls
, oldsize
, size
);
1807 ret
= ops
->resize(&ctx
, size
);
1809 prterrcode("dotruncate: ops->resize", ret
);
1810 report_failure(160);
1815 do_punch_hole(unsigned offset
, unsigned length
)
1817 unsigned end_offset
;
1822 offset
-= offset
% holebdy
;
1823 length
-= length
% holebdy
;
1825 if (!quiet
&& testcalls
> simulatedopcount
)
1826 prt("skipping zero length punch hole\n");
1827 log4(OP_SKIPPED
, OP_PUNCH_HOLE
, offset
, length
);
1831 if (file_size
<= (loff_t
)offset
) {
1832 if (!quiet
&& testcalls
> simulatedopcount
)
1833 prt("skipping hole punch off the end of the file\n");
1834 log4(OP_SKIPPED
, OP_PUNCH_HOLE
, offset
, length
);
1838 end_offset
= offset
+ length
;
1840 log4(OP_PUNCH_HOLE
, offset
, length
, 0);
1842 if (testcalls
<= simulatedopcount
)
1845 if ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1846 (debug
&& (monitorstart
== -1 || monitorend
== -1 ||
1847 (long)end_offset
<= monitorend
))) {
1848 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls
,
1849 offset
, offset
+length
, length
);
1852 ret
= ops
->discard(&ctx
, (unsigned long long)offset
,
1853 (unsigned long long)length
);
1855 prterrcode("do_punch_hole: ops->discard", ret
);
1856 report_failure(161);
1859 max_offset
= offset
< file_size
? offset
: file_size
;
1860 max_len
= max_offset
+ length
<= file_size
? length
:
1861 file_size
- max_offset
;
1862 memset(good_buf
+ max_offset
, '\0', max_len
);
1865 unsigned get_data_size(unsigned size
)
1869 unsigned max
= sqrt((double)size
) + 1;
1871 unsigned curr
= good
;
1873 hint
= get_random() % max
;
1875 for (i
= 1; i
< max
&& curr
< hint
; i
++) {
1876 if (size
% i
== 0) {
1889 dowritesame(unsigned offset
, unsigned size
)
1897 offset
-= offset
% writebdy
;
1899 size
-= size
% writebdy
;
1901 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
1902 prt("skipping zero size writesame\n");
1903 log4(OP_SKIPPED
, OP_WRITESAME
, offset
, size
);
1907 data_size
= get_data_size(size
);
1909 log4(OP_WRITESAME
, offset
, size
, data_size
);
1911 gendata(original_buf
, good_buf
, offset
, data_size
);
1912 if (file_size
< offset
+ size
) {
1913 newsize
= ceil(((double)offset
+ size
) / truncbdy
) * truncbdy
;
1914 if (file_size
< newsize
)
1915 memset(good_buf
+ file_size
, '\0', newsize
- file_size
);
1916 file_size
= newsize
;
1918 warn("Lite file size bug in fsx!");
1919 report_failure(162);
1921 ret
= ops
->resize(&ctx
, newsize
);
1923 prterrcode("dowritesame: ops->resize", ret
);
1924 report_failure(163);
1928 for (n
= size
/ data_size
, buf_off
= data_size
; n
> 1; n
--) {
1929 memcpy(good_buf
+ offset
+ buf_off
, good_buf
+ offset
, data_size
);
1930 buf_off
+= data_size
;
1933 if (testcalls
<= simulatedopcount
)
1937 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1939 (monitorstart
== -1 ||
1940 (static_cast<long>(offset
+ size
) > monitorstart
&&
1941 (monitorend
== -1 ||
1942 static_cast<long>(offset
) <= monitorend
))))))
1943 prt("%lu writesame\t0x%x thru\t0x%x\tdata_size\t0x%x(0x%x bytes)\n", testcalls
,
1944 offset
, offset
+ size
- 1, data_size
, size
);
1946 ret
= ops
->writesame(&ctx
, offset
, size
, good_buf
+ offset
, data_size
);
1947 if (ret
!= (ssize_t
)size
) {
1949 prterrcode("dowritesame: ops->writesame", ret
);
1951 prt("short writesame: 0x%x bytes instead of 0x%x\n",
1953 report_failure(164);
1957 doflush(offset
, size
);
1961 docompareandwrite(unsigned offset
, unsigned size
)
1965 offset
-= offset
% writebdy
;
1967 size
-= size
% writebdy
;
1970 if (!quiet
&& testcalls
> simulatedopcount
&& !o_direct
)
1971 prt("skipping zero size read\n");
1972 log4(OP_SKIPPED
, OP_READ
, offset
, size
);
1976 if (size
+ offset
> file_size
) {
1977 if (!quiet
&& testcalls
> simulatedopcount
)
1978 prt("skipping seek/compare past end of file\n");
1979 log4(OP_SKIPPED
, OP_COMPARE_AND_WRITE
, offset
, size
);
1983 memcpy(temp_buf
+ offset
, good_buf
+ offset
, size
);
1984 gendata(original_buf
, good_buf
, offset
, size
);
1985 log4(OP_COMPARE_AND_WRITE
, offset
, size
, 0);
1987 if (testcalls
<= simulatedopcount
)
1991 ((progressinterval
&& testcalls
% progressinterval
== 0) ||
1993 (monitorstart
== -1 ||
1994 (static_cast<long>(offset
+ size
) > monitorstart
&&
1995 (monitorend
== -1 ||
1996 static_cast<long>(offset
) <= monitorend
))))))
1997 prt("%lu compareandwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls
,
1998 offset
, offset
+ size
- 1, size
);
2000 ret
= ops
->compare_and_write(&ctx
, offset
, size
, temp_buf
+ offset
,
2002 if (ret
!= (ssize_t
)size
) {
2003 if (ret
== -EINVAL
) {
2004 memcpy(good_buf
+ offset
, temp_buf
+ offset
, size
);
2008 prterrcode("docompareandwrite: ops->compare_and_write", ret
);
2010 prt("short write: 0x%x bytes instead of 0x%x\n", ret
, size
);
2011 report_failure(151);
2016 doflush(offset
, size
);
2019 void clone_filename(char *buf
, size_t len
, int clones
)
2021 snprintf(buf
, len
, "%s/fsx-%s-parent%d",
2022 dirpath
, iname
, clones
);
2025 void clone_imagename(char *buf
, size_t len
, int clones
)
2028 snprintf(buf
, len
, "%s-clone%d", iname
, clones
);
2030 strncpy(buf
, iname
, len
);
2031 buf
[len
- 1] = '\0';
2034 void replay_imagename(char *buf
, size_t len
, int clones
)
2036 clone_imagename(buf
, len
, clones
);
2037 strncat(buf
, "-replay", len
- strlen(buf
));
2038 buf
[len
- 1] = '\0';
2041 void check_clone(int clonenum
, bool replay_image
);
2046 char filename
[1024];
2047 char imagename
[1024];
2048 char lastimagename
[1024];
2050 int order
= 0, stripe_unit
= 0, stripe_count
= 0;
2051 uint64_t newsize
= file_size
;
2053 log4(OP_CLONE
, 0, 0, 0);
2056 if (randomize_striping
) {
2057 order
= 18 + get_random() % 8;
2058 stripe_unit
= 1ull << (order
- 1 - (get_random() % 8));
2059 stripe_count
= 2 + get_random() % 14;
2062 prt("%lu clone\t%d order %d su %d sc %d\n", testcalls
, num_clones
,
2063 order
, stripe_unit
, stripe_count
);
2065 clone_imagename(imagename
, sizeof(imagename
), num_clones
);
2066 clone_imagename(lastimagename
, sizeof(lastimagename
),
2068 assert(strcmp(lastimagename
, ctx
.name
) == 0);
2070 ret
= ops
->clone(&ctx
, "snap", imagename
, &order
, stripe_unit
,
2073 prterrcode("do_clone: ops->clone", ret
);
2077 if (randomize_parent_overlap
&& rbd_image_has_parent(&ctx
)) {
2078 int rand
= get_random() % 16 + 1; // [1..16]
2083 ret
= rbd_get_overlap(ctx
.image
, &overlap
);
2085 prterrcode("do_clone: rbd_get_overlap", ret
);
2089 if (rand
< 10) { // 9/16
2090 newsize
= overlap
* ((double)rand
/ 10);
2091 newsize
-= newsize
% truncbdy
;
2096 assert(newsize
!= (uint64_t)file_size
);
2097 prt("truncating image %s from 0x%llx (overlap 0x%llx) to 0x%llx\n",
2098 ctx
.name
, file_size
, overlap
, newsize
);
2100 ret
= ops
->resize(&ctx
, newsize
);
2102 prterrcode("do_clone: ops->resize", ret
);
2105 } else if (rand
< 15) { // 2/16
2106 prt("flattening image %s\n", ctx
.name
);
2108 ret
= ops
->flatten(&ctx
);
2110 prterrcode("do_clone: ops->flatten", ret
);
2114 prt("leaving image %s intact\n", ctx
.name
);
2118 clone_filename(filename
, sizeof(filename
), num_clones
);
2119 if ((fd
= open(filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666)) < 0) {
2120 simple_err("do_clone: open", -errno
);
2123 save_buffer(good_buf
, newsize
, fd
);
2124 if ((ret
= close(fd
)) < 0) {
2125 simple_err("do_clone: close", -errno
);
2132 if ((ret
= ops
->close(&ctx
)) < 0) {
2133 prterrcode("do_clone: ops->close", ret
);
2137 if (journal_replay
) {
2138 ret
= finalize_journal(ioctx
, lastimagename
, num_clones
- 1,
2139 order
, stripe_unit
, stripe_count
);
2144 ret
= register_journal(ioctx
, imagename
);
2151 * Open freshly made clone.
2153 if ((ret
= ops
->open(imagename
, &ctx
)) < 0) {
2154 prterrcode("do_clone: ops->open", ret
);
2158 if (num_clones
> 1) {
2159 if (journal_replay
) {
2160 check_clone(num_clones
- 2, true);
2162 check_clone(num_clones
- 2, false);
2167 check_clone(int clonenum
, bool replay_image
)
2170 char imagename
[128];
2172 struct rbd_ctx cur_ctx
= RBD_CTX_INIT
;
2173 struct stat file_info
;
2174 char *good_buf
, *temp_buf
;
2177 replay_imagename(imagename
, sizeof(imagename
), clonenum
);
2179 clone_imagename(imagename
, sizeof(imagename
), clonenum
);
2182 if ((ret
= ops
->open(imagename
, &cur_ctx
)) < 0) {
2183 prterrcode("check_clone: ops->open", ret
);
2187 clone_filename(filename
, sizeof(filename
), clonenum
+ 1);
2188 if ((fd
= open(filename
, O_RDONLY
)) < 0) {
2189 simple_err("check_clone: open", -errno
);
2193 prt("checking clone #%d, image %s against file %s\n",
2194 clonenum
, imagename
, filename
);
2195 if ((ret
= fstat(fd
, &file_info
)) < 0) {
2196 simple_err("check_clone: fstat", -errno
);
2201 ret
= posix_memalign((void **)&good_buf
,
2202 MAX(writebdy
, (int)sizeof(void *)),
2205 prterrcode("check_clone: posix_memalign(good_buf)", -ret
);
2210 ret
= posix_memalign((void **)&temp_buf
,
2211 MAX(readbdy
, (int)sizeof(void *)),
2214 prterrcode("check_clone: posix_memalign(temp_buf)", -ret
);
2218 if ((ret
= pread(fd
, good_buf
, file_info
.st_size
, 0)) < 0) {
2219 simple_err("check_clone: pread", -errno
);
2222 if ((ret
= ops
->read(&cur_ctx
, 0, file_info
.st_size
, temp_buf
)) < 0) {
2223 prterrcode("check_clone: ops->read", ret
);
2227 if ((ret
= ops
->close(&cur_ctx
)) < 0) {
2228 prterrcode("check_clone: ops->close", ret
);
2231 check_buffers(good_buf
, temp_buf
, 0, file_info
.st_size
);
2233 if (!replay_image
) {
2246 ret
= ops
->write(&ctx
, 0, file_size
, good_buf
);
2247 if (ret
!= file_size
) {
2249 prterrcode("writefileimage: ops->write", ret
);
2251 prt("short write: 0x%x bytes instead of 0x%llx\n",
2252 ret
, (unsigned long long)file_size
);
2253 report_failure(172);
2257 ret
= ops
->resize(&ctx
, file_size
);
2259 prterrcode("writefileimage: ops->resize", ret
);
2260 report_failure(173);
2270 if (!rbd_image_has_parent(&ctx
)) {
2271 log4(OP_SKIPPED
, OP_FLATTEN
, 0, 0);
2274 log4(OP_FLATTEN
, 0, 0, 0);
2275 prt("%lu flatten\n", testcalls
);
2277 ret
= ops
->flatten(&ctx
);
2279 prterrcode("writefileimage: ops->flatten", ret
);
2290 if (testcalls
<= simulatedopcount
)
2293 name
= strdup(ctx
.name
);
2296 prt("%lu close/open\n", testcalls
);
2298 ret
= ops
->close(&ctx
);
2300 prterrcode("docloseopen: ops->close", ret
);
2301 report_failure(180);
2304 ret
= ops
->open(name
, &ctx
);
2306 prterrcode("docloseopen: ops->open", ret
);
2307 report_failure(181);
2313 #define TRIM_OFF_LEN(off, len, size) \
2319 if ((unsigned)(off) + (unsigned)(len) > (unsigned)(size)) \
2320 (len) = (size) - (off); \
2326 unsigned long offset
;
2327 unsigned long size
= maxoplen
;
2328 unsigned long rv
= get_random();
2331 if (simulatedopcount
> 0 && testcalls
== simulatedopcount
)
2337 closeopen
= (rv
>> 3) < (1u << 28) / (unsigned)closeprob
;
2339 if (debugstart
> 0 && testcalls
>= debugstart
)
2342 if (!quiet
&& testcalls
< simulatedopcount
&& testcalls
% 100000 == 0)
2343 prt("%lu...\n", testcalls
);
2345 offset
= get_random();
2347 size
= get_random() % (maxoplen
+ 1);
2349 /* calculate appropriate op to run */
2351 op
= rv
% OP_MAX_LITE
;
2353 op
= rv
% OP_MAX_FULL
;
2365 if (!fallocate_calls
) {
2366 log4(OP_SKIPPED
, OP_FALLOCATE
, offset
, size
);
2371 if (!punch_hole_calls
) {
2372 log4(OP_SKIPPED
, OP_PUNCH_HOLE
, offset
, size
);
2377 /* clone, 8% chance */
2378 if (!clone_calls
|| file_size
== 0 || get_random() % 100 >= 8) {
2379 log4(OP_SKIPPED
, OP_CLONE
, 0, 0);
2384 /* flatten four times as rarely as clone, 2% chance */
2385 if (get_random() % 100 >= 2) {
2386 log4(OP_SKIPPED
, OP_FLATTEN
, 0, 0);
2391 /* writesame not implemented */
2392 if (!ops
->writesame
) {
2393 log4(OP_SKIPPED
, OP_WRITESAME
, offset
, size
);
2396 case OP_COMPARE_AND_WRITE
:
2397 /* compare_and_write not implemented */
2398 if (!ops
->compare_and_write
) {
2399 log4(OP_SKIPPED
, OP_COMPARE_AND_WRITE
, offset
, size
);
2406 TRIM_OFF_LEN(offset
, size
, file_size
);
2407 doread(offset
, size
);
2411 TRIM_OFF_LEN(offset
, size
, maxfilelen
);
2412 dowrite(offset
, size
);
2416 TRIM_OFF_LEN(offset
, size
, file_size
);
2421 TRIM_OFF_LEN(offset
, size
, maxfilelen
);
2427 size
= get_random() % maxfilelen
;
2432 TRIM_OFF_LEN(offset
, size
, file_size
);
2433 do_punch_hole(offset
, size
);
2437 TRIM_OFF_LEN(offset
, size
, maxfilelen
);
2438 dowritesame(offset
, size
);
2440 case OP_COMPARE_AND_WRITE
:
2441 TRIM_OFF_LEN(offset
, size
, file_size
);
2442 docompareandwrite(offset
, size
);
2454 prterr("test: unknown operation");
2460 if (sizechecks
&& testcalls
> simulatedopcount
)
2471 prt("signal %d\n", sig
);
2472 prt("testcalls = %lu\n", testcalls
);
2480 fprintf(stdout
, "usage: %s",
2481 "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\
2482 -b opnum: beginning operation number (default 1)\n\
2483 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
2484 -d: debug output for all operations\n\
2485 -f: flush and invalidate cache after I/O\n\
2486 -h holebdy: 4096 would make discards page aligned (default 1)\n\
2487 -j: journal replay stress test\n\
2488 -k: keep data on success (default 0)\n\
2489 -l flen: the upper bound on file size (default 262144)\n\
2490 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
2491 -n: no verifications of file size\n\
2492 -o oplen: the upper bound on operation size (default 65536)\n\
2493 -p progressinterval: debug output at specified operation interval\n\
2494 -q: quieter operation\n\
2495 -r readbdy: 4096 would make reads page aligned (default 1)\n\
2496 -s style: 1 gives smaller truncates (default 0)\n\
2497 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
2498 -w writebdy: 4096 would make writes page aligned (default 1)\n\
2499 -x: preallocate file space before starting, XFS only (default 0)\n\
2500 -y: synchronize changes to a file\n"
2502 " -C: do not use clone calls\n\
2503 -D startingop: debug output starting at specified operation\n"
2505 " -F: Do not use fallocate (preallocation) calls\n"
2507 " -H: do not use punch hole calls\n\
2508 -K: enable krbd mode (use -t and -h too)\n\
2509 -M: enable rbd-nbd mode (use -t and -h too)\n\
2510 -L: fsxLite - no file creations & no file size changes\n\
2511 -N numops: total # operations to do (default infinity)\n\
2512 -O: use oplen (see -o flag) for every op (default random)\n\
2513 -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
2514 -R: read() system calls only (mapped reads disabled)\n\
2515 -S seed: for random # generator (default 1) 0 gets timestamp\n\
2516 -U: disable randomized striping\n\
2517 -W: mapped write operations DISabled\n\
2518 -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
2519 poolname: this is REQUIRED (no default)\n\
2520 imagename: this is REQUIRED (no default)\n");
2526 getnum(char *s
, char **e
)
2531 ret
= strtol(s
, e
, 0);
2562 if (!lite
&& fallocate_calls
) {
2563 if (fallocate(fd
, 0, 0, 1) && errno
== EOPNOTSUPP
) {
2565 warn("main: filesystem does not support fallocate, disabling\n");
2566 fallocate_calls
= 0;
2571 #else /* ! FALLOCATE */
2572 fallocate_calls
= 0;
2577 void remove_image(rados_ioctx_t ioctx
, char *imagename
, bool remove_snap
,
2583 if ((ret
= rbd_open(ioctx
, imagename
, &image
, NULL
)) < 0) {
2584 sprintf(errmsg
, "rbd_open %s", imagename
);
2585 prterrcode(errmsg
, ret
);
2586 report_failure(101);
2589 if ((ret
= rbd_snap_unprotect(image
, "snap")) < 0) {
2590 sprintf(errmsg
, "rbd_snap_unprotect %s@snap",
2592 prterrcode(errmsg
, ret
);
2593 report_failure(102);
2595 if ((ret
= rbd_snap_remove(image
, "snap")) < 0) {
2596 sprintf(errmsg
, "rbd_snap_remove %s@snap",
2598 prterrcode(errmsg
, ret
);
2599 report_failure(103);
2602 if ((ret
= rbd_close(image
)) < 0) {
2603 sprintf(errmsg
, "rbd_close %s", imagename
);
2604 prterrcode(errmsg
, ret
);
2605 report_failure(104);
2609 (ret
= unregister_journal(ioctx
, imagename
)) < 0) {
2610 report_failure(105);
2613 if ((ret
= rbd_remove(ioctx
, imagename
)) < 0) {
2614 sprintf(errmsg
, "rbd_remove %s", imagename
);
2615 prterrcode(errmsg
, ret
);
2616 report_failure(106);
2621 main(int argc
, char **argv
)
2623 int i
, style
, ch
, ret
;
2625 char goodfile
[1024];
2631 page_size
= getpagesize();
2632 page_mask
= page_size
- 1;
2633 mmap_mask
= page_mask
;
2635 setvbuf(stdout
, (char *)0, _IOLBF
, 0); /* line buffered stdout */
2637 while ((ch
= getopt(argc
, argv
, "b:c:dfh:jkl:m:no:p:qr:s:t:w:xyCD:FHKMLN:OP:RS:UWZ"))
2641 simulatedopcount
= getnum(optarg
, &endp
);
2643 fprintf(stdout
, "Will begin at operation %lu\n",
2645 if (simulatedopcount
== 0)
2647 simulatedopcount
-= 1;
2650 closeprob
= getnum(optarg
, &endp
);
2653 "Chance of close/open is 1 in %d\n",
2665 holebdy
= getnum(optarg
, &endp
);
2670 journal_replay
= true;
2673 keep_on_success
= 1;
2677 int _num
= getnum(optarg
, &endp
);
2684 monitorstart
= getnum(optarg
, &endp
);
2685 if (monitorstart
< 0)
2687 if (!endp
|| *endp
++ != ':')
2689 monitorend
= getnum(endp
, &endp
);
2692 if (monitorend
== 0)
2693 monitorend
= -1; /* aka infinity */
2700 maxoplen
= getnum(optarg
, &endp
);
2705 progressinterval
= getnum(optarg
, &endp
);
2706 if (progressinterval
== 0)
2713 readbdy
= getnum(optarg
, &endp
);
2718 style
= getnum(optarg
, &endp
);
2719 if (style
< 0 || style
> 1)
2723 truncbdy
= getnum(optarg
, &endp
);
2728 writebdy
= getnum(optarg
, &endp
);
2742 debugstart
= getnum(optarg
, &endp
);
2747 fallocate_calls
= 0;
2750 punch_hole_calls
= 0;
2753 prt("krbd mode enabled\n");
2754 ops
= &krbd_operations
;
2757 prt("rbd-nbd mode enabled\n");
2758 ops
= &nbd_operations
;
2761 prt("lite mode not supported for rbd\n");
2765 numops
= getnum(optarg
, &endp
);
2773 strncpy(dirpath
, optarg
, sizeof(dirpath
)-1);
2774 dirpath
[sizeof(dirpath
)-1] = '\0';
2775 strncpy(goodfile
, dirpath
, sizeof(goodfile
)-1);
2776 goodfile
[sizeof(goodfile
)-1] = '\0';
2777 if (strlen(goodfile
) < sizeof(goodfile
)-2) {
2778 strcat(goodfile
, "/");
2780 prt("file name to long\n");
2783 strncpy(logfile
, dirpath
, sizeof(logfile
)-1);
2784 logfile
[sizeof(logfile
)-1] = '\0';
2785 if (strlen(logfile
) < sizeof(logfile
)-2) {
2786 strcat(logfile
, "/");
2788 prt("file path to long\n");
2795 fprintf(stdout
, "mapped reads DISABLED\n");
2798 seed
= getnum(optarg
, &endp
);
2800 seed
= time(0) % 10000;
2802 fprintf(stdout
, "Seed set to %d\n", seed
);
2807 randomize_striping
= 0;
2812 fprintf(stdout
, "mapped writes DISABLED\n");
2815 o_direct
= O_DIRECT
;
2828 signal(SIGHUP
, cleanup
);
2829 signal(SIGINT
, cleanup
);
2830 signal(SIGPIPE
, cleanup
);
2831 signal(SIGALRM
, cleanup
);
2832 signal(SIGTERM
, cleanup
);
2833 signal(SIGXCPU
, cleanup
);
2834 signal(SIGXFSZ
, cleanup
);
2835 signal(SIGVTALRM
, cleanup
);
2836 signal(SIGUSR1
, cleanup
);
2837 signal(SIGUSR2
, cleanup
);
2839 random_generator
.seed(seed
);
2841 ret
= create_image();
2843 prterrcode(iname
, ret
);
2846 ret
= ops
->open(iname
, &ctx
);
2848 simple_err("Error opening image", ret
);
2852 strcat(dirpath
, ".");
2853 strncat(goodfile
, iname
, 256);
2854 strcat (goodfile
, ".fsxgood");
2855 fsxgoodfd
= open(goodfile
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
2856 if (fsxgoodfd
< 0) {
2860 strncat(logfile
, iname
, 256);
2861 strcat (logfile
, ".fsxlog");
2862 fsxlogf
= fopen(logfile
, "w");
2863 if (fsxlogf
== NULL
) {
2868 original_buf
= (char *) malloc(maxfilelen
);
2869 for (i
= 0; i
< (int)maxfilelen
; i
++)
2870 original_buf
[i
] = get_random() % 256;
2872 ret
= posix_memalign((void **)&good_buf
,
2873 MAX(writebdy
, (int)sizeof(void *)), maxfilelen
);
2876 prt("writebdy is not a suitable power of two\n");
2878 prterrcode("main: posix_memalign(good_buf)", -ret
);
2881 memset(good_buf
, '\0', maxfilelen
);
2883 ret
= posix_memalign((void **)&temp_buf
,
2884 MAX(readbdy
, (int)sizeof(void *)), maxfilelen
);
2887 prt("readbdy is not a suitable power of two\n");
2889 prterrcode("main: posix_memalign(temp_buf)", -ret
);
2892 memset(temp_buf
, '\0', maxfilelen
);
2894 if (lite
) { /* zero entire existing file */
2897 written
= ops
->write(&ctx
, 0, (size_t)maxfilelen
, good_buf
);
2898 if (written
!= (ssize_t
)maxfilelen
) {
2900 prterrcode(iname
, written
);
2901 warn("main: error on write");
2903 warn("main: short write, 0x%x bytes instead "
2914 while (numops
== -1 || numops
--)
2917 ret
= ops
->close(&ctx
);
2919 prterrcode("ops->close", ret
);
2923 if (journal_replay
) {
2924 char imagename
[1024];
2925 clone_imagename(imagename
, sizeof(imagename
), num_clones
);
2926 ret
= finalize_journal(ioctx
, imagename
, num_clones
, 0, 0, 0);
2928 report_failure(100);
2932 if (num_clones
> 0) {
2933 if (journal_replay
) {
2934 check_clone(num_clones
- 1, true);
2936 check_clone(num_clones
- 1, false);
2939 if (!keep_on_success
) {
2940 while (num_clones
>= 0) {
2941 static bool remove_snap
= false;
2943 if (journal_replay
) {
2944 char replayimagename
[1024];
2945 replay_imagename(replayimagename
,
2946 sizeof(replayimagename
),
2948 remove_image(ioctx
, replayimagename
,
2953 char clonename
[128];
2954 clone_imagename(clonename
, 128, num_clones
);
2955 remove_image(ioctx
, clonename
, remove_snap
,
2963 prt("All operations completed A-OK!\n");
2966 rados_ioctx_destroy(ioctx
);
2968 rados_shutdown(cluster
);