2 * Copyright (c) 1997-2007 Kenneth D. Merry
3 * Copyright (c) 2013, 2014, 2015 Spectra Logic Corporation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 * substantially similar to the "NO WARRANTY" disclaimer below
14 * ("Disclaimer") and any redistribution must be conditioned upon
15 * including a substantially similar Disclaimer requirement for further
16 * binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGES.
31 * Authors: Ken Merry (Spectra Logic Corporation)
35 * This is eventually intended to be:
36 * - A basic data transfer/copy utility
37 * - A simple benchmark utility
38 * - An example of how to use the asynchronous pass(4) driver interface.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/ioctl.h>
44 #include <sys/stdint.h>
45 #include <sys/types.h>
46 #include <sys/endian.h>
47 #include <sys/param.h>
50 #include <sys/event.h>
54 #include <machine/bus.h>
56 #include <sys/bus_dma.h>
63 #include <semaphore.h>
77 #include <cam/cam_debug.h>
78 #include <cam/cam_ccb.h>
79 #include <cam/scsi/scsi_all.h>
80 #include <cam/scsi/scsi_da.h>
81 #include <cam/scsi/scsi_pass.h>
82 #include <cam/scsi/scsi_message.h>
83 #include <cam/scsi/smp_all.h>
89 CAMDD_CMD_NONE = 0x00000000,
90 CAMDD_CMD_HELP = 0x00000001,
91 CAMDD_CMD_WRITE = 0x00000002,
92 CAMDD_CMD_READ = 0x00000003
96 CAMDD_ARG_NONE = 0x00000000,
97 CAMDD_ARG_VERBOSE = 0x00000001,
98 CAMDD_ARG_DEVICE = 0x00000002,
99 CAMDD_ARG_BUS = 0x00000004,
100 CAMDD_ARG_TARGET = 0x00000008,
101 CAMDD_ARG_LUN = 0x00000010,
102 CAMDD_ARG_UNIT = 0x00000020,
103 CAMDD_ARG_TIMEOUT = 0x00000040,
104 CAMDD_ARG_ERR_RECOVER = 0x00000080,
105 CAMDD_ARG_RETRIES = 0x00000100
109 CAMDD_DEV_NONE = 0x00,
110 CAMDD_DEV_PASS = 0x01,
111 CAMDD_DEV_FILE = 0x02
114 struct camdd_io_opts {
115 camdd_dev_type dev_type;
118 uint64_t queue_depth;
131 struct camdd_buf_indirect {
133 * Pointer to the source buffer.
135 struct camdd_buf *src_buf;
138 * Offset into the source buffer, in bytes.
142 * Pointer to the starting point in the source buffer.
147 * Length of this chunk in bytes.
152 struct camdd_buf_data {
154 * Buffer allocated when we allocate this camdd_buf. This should
155 * be the size of the blocksize for this device.
160 * The amount of backing store allocated in buf. Generally this
161 * will be the blocksize of the device.
166 * The amount of data that was put into the buffer (on reads) or
167 * the amount of data we have put onto the src_list so far (on
173 * The amount of data that was not transferred.
178 * Starting byte offset on the reader.
180 uint64_t src_start_offset;
183 * CCB used for pass(4) device targets.
188 * Number of scatter/gather segments.
193 * Set if we had to tack on an extra buffer to round the transfer
194 * up to a sector size.
199 * Scatter/gather list used generally when we're the writer for a
202 bus_dma_segment_t *segs;
205 * Scatter/gather list used generally when we're the writer for a
206 * file or block device;
211 union camdd_buf_types {
212 struct camdd_buf_indirect indirect;
213 struct camdd_buf_data data;
219 CAMDD_STATUS_SHORT_IO,
225 camdd_buf_type buf_type;
226 union camdd_buf_types buf_type_spec;
228 camdd_buf_status status;
234 * A reference count of how many indirect buffers point to this
240 * A link back to our parent device.
242 struct camdd_dev *dev;
243 STAILQ_ENTRY(camdd_buf) links;
244 STAILQ_ENTRY(camdd_buf) work_links;
247 * A count of the buffers on the src_list.
252 * List of buffers from our partner thread that are the components
253 * of this buffer for the I/O. Uses src_links.
255 STAILQ_HEAD(,camdd_buf) src_list;
256 STAILQ_ENTRY(camdd_buf) src_links;
259 #define NUM_DEV_TYPES 2
261 struct camdd_dev_pass {
263 struct cam_device *dev;
281 CAMDD_FF_NONE = 0x00,
282 CAMDD_FF_CAN_SEEK = 0x01
285 struct camdd_dev_file {
288 char filename[MAXPATHLEN + 1];
289 camdd_file_type file_type;
290 camdd_file_flags file_flags;
294 struct camdd_dev_block {
300 union camdd_dev_spec {
301 struct camdd_dev_pass pass;
302 struct camdd_dev_file file;
303 struct camdd_dev_block block;
307 CAMDD_DEV_FLAG_NONE = 0x00,
308 CAMDD_DEV_FLAG_EOF = 0x01,
309 CAMDD_DEV_FLAG_PEER_EOF = 0x02,
310 CAMDD_DEV_FLAG_ACTIVE = 0x04,
311 CAMDD_DEV_FLAG_EOF_SENT = 0x08,
312 CAMDD_DEV_FLAG_EOF_QUEUED = 0x10
316 camdd_dev_type dev_type;
317 union camdd_dev_spec dev_spec;
318 camdd_dev_flags flags;
319 char device_name[MAXPATHLEN+1];
321 uint32_t sector_size;
323 uint64_t sector_io_limit;
329 uint64_t start_offset_bytes;
330 uint64_t next_io_pos_bytes;
331 uint64_t next_peer_pos_bytes;
332 uint64_t next_completion_pos_bytes;
333 uint64_t peer_bytes_queued;
334 uint64_t bytes_transferred;
335 uint32_t target_queue_depth;
336 uint32_t cur_active_io;
338 uint32_t extra_buf_len;
339 struct camdd_dev *peer_dev;
340 pthread_mutex_t mutex;
344 int (*run)(struct camdd_dev *dev);
345 int (*fetch)(struct camdd_dev *dev);
348 * Buffers that are available for I/O. Uses links.
350 STAILQ_HEAD(,camdd_buf) free_queue;
353 * Free indirect buffers. These are used for breaking a large
354 * buffer into multiple pieces.
356 STAILQ_HEAD(,camdd_buf) free_indirect_queue;
359 * Buffers that have been queued to the kernel. Uses links.
361 STAILQ_HEAD(,camdd_buf) active_queue;
364 * Will generally contain one of our buffers that is waiting for enough
365 * I/O from our partner thread to be able to execute. This will
366 * generally happen when our per-I/O-size is larger than the
367 * partner thread's per-I/O-size. Uses links.
369 STAILQ_HEAD(,camdd_buf) pending_queue;
372 * Number of buffers on the pending queue
374 int num_pending_queue;
377 * Buffers that are filled and ready to execute. This is used when
378 * our partner (reader) thread sends us blocks that are larger than
379 * our blocksize, and so we have to split them into multiple pieces.
381 STAILQ_HEAD(,camdd_buf) run_queue;
384 * Number of buffers on the run queue.
388 STAILQ_HEAD(,camdd_buf) reorder_queue;
390 int num_reorder_queue;
393 * Buffers that have been queued to us by our partner thread
394 * (generally the reader thread) to be written out. Uses
397 STAILQ_HEAD(,camdd_buf) work_queue;
400 * Buffers that have been completed by our partner thread. Uses
403 STAILQ_HEAD(,camdd_buf) peer_done_queue;
406 * Number of buffers on the peer done queue.
408 uint32_t num_peer_done_queue;
411 * A list of buffers that we have queued to our peer thread. Uses
414 STAILQ_HEAD(,camdd_buf) peer_work_queue;
417 * Number of buffers on the peer work queue.
419 uint32_t num_peer_work_queue;
422 static sem_t camdd_sem;
423 static sig_atomic_t need_exit = 0;
424 static sig_atomic_t error_exit = 0;
425 static sig_atomic_t need_status = 0;
428 #define min(a, b) (a < b) ? a : b
432 * XXX KDM private copy of timespecsub(). This is normally defined in
433 * sys/time.h, but is only enabled in the kernel. If that definition is
434 * enabled in userland, it breaks the build of libnetbsd.
437 #define timespecsub(vvp, uvp) \
439 (vvp)->tv_sec -= (uvp)->tv_sec; \
440 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
441 if ((vvp)->tv_nsec < 0) { \
443 (vvp)->tv_nsec += 1000000000; \
449 /* Generically usefull offsets into the peripheral private area */
450 #define ppriv_ptr0 periph_priv.entries[0].ptr
451 #define ppriv_ptr1 periph_priv.entries[1].ptr
452 #define ppriv_field0 periph_priv.entries[0].field
453 #define ppriv_field1 periph_priv.entries[1].field
455 #define ccb_buf ppriv_ptr0
457 #define CAMDD_FILE_DEFAULT_BLOCK 524288
458 #define CAMDD_FILE_DEFAULT_DEPTH 1
459 #define CAMDD_PASS_MAX_BLOCK 1048576
460 #define CAMDD_PASS_DEFAULT_DEPTH 6
461 #define CAMDD_PASS_RW_TIMEOUT 60 * 1000
463 static int parse_btl(char *tstr, int *bus, int *target, int *lun,
464 camdd_argmask *arglst);
465 void camdd_free_dev(struct camdd_dev *dev);
466 struct camdd_dev *camdd_alloc_dev(camdd_dev_type dev_type,
467 struct kevent *new_ke, int num_ke,
468 int retry_count, int timeout);
469 static struct camdd_buf *camdd_alloc_buf(struct camdd_dev *dev,
470 camdd_buf_type buf_type);
471 void camdd_release_buf(struct camdd_buf *buf);
472 struct camdd_buf *camdd_get_buf(struct camdd_dev *dev, camdd_buf_type buf_type);
473 int camdd_buf_sg_create(struct camdd_buf *buf, int iovec,
474 uint32_t sector_size, uint32_t *num_sectors_used,
475 int *double_buf_needed);
476 uint32_t camdd_buf_get_len(struct camdd_buf *buf);
477 void camdd_buf_add_child(struct camdd_buf *buf, struct camdd_buf *child_buf);
478 int camdd_probe_tape(int fd, char *filename, uint64_t *max_iosize,
479 uint64_t *max_blk, uint64_t *min_blk, uint64_t *blk_gran);
480 struct camdd_dev *camdd_probe_file(int fd, struct camdd_io_opts *io_opts,
481 int retry_count, int timeout);
482 struct camdd_dev *camdd_probe_pass(struct cam_device *cam_dev,
483 struct camdd_io_opts *io_opts,
484 camdd_argmask arglist, int probe_retry_count,
485 int probe_timeout, int io_retry_count,
487 void *camdd_file_worker(void *arg);
488 camdd_buf_status camdd_ccb_status(union ccb *ccb);
489 int camdd_queue_peer_buf(struct camdd_dev *dev, struct camdd_buf *buf);
490 int camdd_complete_peer_buf(struct camdd_dev *dev, struct camdd_buf *peer_buf);
491 void camdd_peer_done(struct camdd_buf *buf);
492 void camdd_complete_buf(struct camdd_dev *dev, struct camdd_buf *buf,
494 int camdd_pass_fetch(struct camdd_dev *dev);
495 int camdd_file_run(struct camdd_dev *dev);
496 int camdd_pass_run(struct camdd_dev *dev);
497 int camdd_get_next_lba_len(struct camdd_dev *dev, uint64_t *lba, ssize_t *len);
498 int camdd_queue(struct camdd_dev *dev, struct camdd_buf *read_buf);
499 void camdd_get_depth(struct camdd_dev *dev, uint32_t *our_depth,
500 uint32_t *peer_depth, uint32_t *our_bytes,
501 uint32_t *peer_bytes);
502 void *camdd_worker(void *arg);
503 void camdd_sig_handler(int sig);
504 void camdd_print_status(struct camdd_dev *camdd_dev,
505 struct camdd_dev *other_dev,
506 struct timespec *start_time);
507 int camdd_rw(struct camdd_io_opts *io_opts, int num_io_opts,
508 uint64_t max_io, int retry_count, int timeout);
509 int camdd_parse_io_opts(char *args, int is_write,
510 struct camdd_io_opts *io_opts);
514 * Parse out a bus, or a bus, target and lun in the following
520 * Returns the number of parsed components, or 0.
523 parse_btl(char *tstr, int *bus, int *target, int *lun, camdd_argmask *arglst)
528 while (isspace(*tstr) && (*tstr != '\0'))
531 tmpstr = (char *)strtok(tstr, ":");
532 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
533 *bus = strtol(tmpstr, NULL, 0);
534 *arglst |= CAMDD_ARG_BUS;
536 tmpstr = (char *)strtok(NULL, ":");
537 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
538 *target = strtol(tmpstr, NULL, 0);
539 *arglst |= CAMDD_ARG_TARGET;
541 tmpstr = (char *)strtok(NULL, ":");
542 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
543 *lun = strtol(tmpstr, NULL, 0);
544 *arglst |= CAMDD_ARG_LUN;
554 * XXX KDM clean up and free all of the buffers on the queue!
557 camdd_free_dev(struct camdd_dev *dev)
562 switch (dev->dev_type) {
563 case CAMDD_DEV_FILE: {
564 struct camdd_dev_file *file_dev = &dev->dev_spec.file;
566 if (file_dev->fd != -1)
568 free(file_dev->tmp_buf);
571 case CAMDD_DEV_PASS: {
572 struct camdd_dev_pass *pass_dev = &dev->dev_spec.pass;
574 if (pass_dev->dev != NULL)
575 cam_close_device(pass_dev->dev);
586 camdd_alloc_dev(camdd_dev_type dev_type, struct kevent *new_ke, int num_ke,
587 int retry_count, int timeout)
589 struct camdd_dev *dev = NULL;
594 dev = malloc(sizeof(*dev));
596 warn("%s: unable to malloc %zu bytes", __func__, sizeof(*dev));
600 bzero(dev, sizeof(*dev));
602 dev->dev_type = dev_type;
603 dev->io_timeout = timeout;
604 dev->retry_count = retry_count;
605 STAILQ_INIT(&dev->free_queue);
606 STAILQ_INIT(&dev->free_indirect_queue);
607 STAILQ_INIT(&dev->active_queue);
608 STAILQ_INIT(&dev->pending_queue);
609 STAILQ_INIT(&dev->run_queue);
610 STAILQ_INIT(&dev->reorder_queue);
611 STAILQ_INIT(&dev->work_queue);
612 STAILQ_INIT(&dev->peer_done_queue);
613 STAILQ_INIT(&dev->peer_work_queue);
614 retval = pthread_mutex_init(&dev->mutex, NULL);
616 warnc(retval, "%s: failed to initialize mutex", __func__);
620 retval = pthread_cond_init(&dev->cond, NULL);
622 warnc(retval, "%s: failed to initialize condition variable",
629 warn("%s: Unable to create kqueue", __func__);
633 ke_size = sizeof(struct kevent) * (num_ke + 4);
634 ke = malloc(ke_size);
636 warn("%s: unable to malloc %zu bytes", __func__, ke_size);
641 bcopy(new_ke, ke, num_ke * sizeof(struct kevent));
643 EV_SET(&ke[num_ke++], (uintptr_t)&dev->work_queue, EVFILT_USER,
644 EV_ADD|EV_ENABLE|EV_CLEAR, 0,0, 0);
645 EV_SET(&ke[num_ke++], (uintptr_t)&dev->peer_done_queue, EVFILT_USER,
646 EV_ADD|EV_ENABLE|EV_CLEAR, 0,0, 0);
647 EV_SET(&ke[num_ke++], SIGINFO, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0,0,0);
648 EV_SET(&ke[num_ke++], SIGINT, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0,0,0);
650 retval = kevent(dev->kq, ke, num_ke, NULL, 0, NULL);
652 warn("%s: Unable to register kevents", __func__);
665 static struct camdd_buf *
666 camdd_alloc_buf(struct camdd_dev *dev, camdd_buf_type buf_type)
668 struct camdd_buf *buf = NULL;
669 uint8_t *data_ptr = NULL;
672 * We only need to allocate data space for data buffers.
676 data_ptr = malloc(dev->blocksize);
677 if (data_ptr == NULL) {
678 warn("unable to allocate %u bytes", dev->blocksize);
686 buf = malloc(sizeof(*buf));
688 warn("unable to allocate %zu bytes", sizeof(*buf));
692 bzero(buf, sizeof(*buf));
693 buf->buf_type = buf_type;
696 case CAMDD_BUF_DATA: {
697 struct camdd_buf_data *data;
699 data = &buf->buf_type_spec.data;
701 data->alloc_len = dev->blocksize;
702 data->buf = data_ptr;
705 case CAMDD_BUF_INDIRECT:
710 STAILQ_INIT(&buf->src_list);
721 camdd_release_buf(struct camdd_buf *buf)
723 struct camdd_dev *dev;
727 switch (buf->buf_type) {
728 case CAMDD_BUF_DATA: {
729 struct camdd_buf_data *data;
731 data = &buf->buf_type_spec.data;
733 if (data->segs != NULL) {
734 if (data->extra_buf != 0) {
738 data->segs[data->sg_count - 1].ds_addr;
745 } else if (data->iovec != NULL) {
746 if (data->extra_buf != 0) {
747 free(data->iovec[data->sg_count - 1].iov_base);
754 STAILQ_INSERT_TAIL(&dev->free_queue, buf, links);
757 case CAMDD_BUF_INDIRECT:
758 STAILQ_INSERT_TAIL(&dev->free_indirect_queue, buf, links);
761 err(1, "%s: Invalid buffer type %d for released buffer",
762 __func__, buf->buf_type);
768 camdd_get_buf(struct camdd_dev *dev, camdd_buf_type buf_type)
770 struct camdd_buf *buf = NULL;
774 buf = STAILQ_FIRST(&dev->free_queue);
776 struct camdd_buf_data *data;
780 STAILQ_REMOVE_HEAD(&dev->free_queue, links);
781 data = &buf->buf_type_spec.data;
782 data_ptr = data->buf;
783 alloc_len = data->alloc_len;
784 bzero(buf, sizeof(*buf));
785 data->buf = data_ptr;
786 data->alloc_len = alloc_len;
789 case CAMDD_BUF_INDIRECT:
790 buf = STAILQ_FIRST(&dev->free_indirect_queue);
792 STAILQ_REMOVE_HEAD(&dev->free_indirect_queue, links);
794 bzero(buf, sizeof(*buf));
798 warnx("Unknown buffer type %d requested", buf_type);
804 return (camdd_alloc_buf(dev, buf_type));
806 STAILQ_INIT(&buf->src_list);
808 buf->buf_type = buf_type;
815 camdd_buf_sg_create(struct camdd_buf *buf, int iovec, uint32_t sector_size,
816 uint32_t *num_sectors_used, int *double_buf_needed)
818 struct camdd_buf *tmp_buf;
819 struct camdd_buf_data *data;
820 uint8_t *extra_buf = NULL;
821 size_t extra_buf_len = 0;
824 data = &buf->buf_type_spec.data;
826 data->sg_count = buf->src_count;
828 * Compose a scatter/gather list from all of the buffers in the list.
829 * If the length of the buffer isn't a multiple of the sector size,
830 * we'll have to add an extra buffer. This should only happen
831 * at the end of a transfer.
833 if ((data->fill_len % sector_size) != 0) {
834 extra_buf_len = sector_size - (data->fill_len % sector_size);
835 extra_buf = calloc(extra_buf_len, 1);
836 if (extra_buf == NULL) {
837 warn("%s: unable to allocate %zu bytes for extra "
838 "buffer space", __func__, extra_buf_len);
846 data->segs = calloc(data->sg_count, sizeof(bus_dma_segment_t));
847 if (data->segs == NULL) {
848 warn("%s: unable to allocate %zu bytes for S/G list",
849 __func__, sizeof(bus_dma_segment_t) *
856 data->iovec = calloc(data->sg_count, sizeof(struct iovec));
857 if (data->iovec == NULL) {
858 warn("%s: unable to allocate %zu bytes for S/G list",
859 __func__, sizeof(struct iovec) * data->sg_count);
865 for (i = 0, tmp_buf = STAILQ_FIRST(&buf->src_list);
866 i < buf->src_count && tmp_buf != NULL; i++,
867 tmp_buf = STAILQ_NEXT(tmp_buf, src_links)) {
869 if (tmp_buf->buf_type == CAMDD_BUF_DATA) {
870 struct camdd_buf_data *tmp_data;
872 tmp_data = &tmp_buf->buf_type_spec.data;
874 data->segs[i].ds_addr =
875 (bus_addr_t) tmp_data->buf;
876 data->segs[i].ds_len = tmp_data->fill_len -
879 data->iovec[i].iov_base = tmp_data->buf;
880 data->iovec[i].iov_len = tmp_data->fill_len -
883 if (((tmp_data->fill_len - tmp_data->resid) %
885 *double_buf_needed = 1;
887 struct camdd_buf_indirect *tmp_ind;
889 tmp_ind = &tmp_buf->buf_type_spec.indirect;
891 data->segs[i].ds_addr =
892 (bus_addr_t)tmp_ind->start_ptr;
893 data->segs[i].ds_len = tmp_ind->len;
895 data->iovec[i].iov_base = tmp_ind->start_ptr;
896 data->iovec[i].iov_len = tmp_ind->len;
898 if ((tmp_ind->len % sector_size) != 0)
899 *double_buf_needed = 1;
903 if (extra_buf != NULL) {
905 data->segs[i].ds_addr = (bus_addr_t)extra_buf;
906 data->segs[i].ds_len = extra_buf_len;
908 data->iovec[i].iov_base = extra_buf;
909 data->iovec[i].iov_len = extra_buf_len;
913 if ((tmp_buf != NULL) || (i != data->sg_count)) {
914 warnx("buffer source count does not match "
915 "number of buffers in list!");
922 *num_sectors_used = (data->fill_len + extra_buf_len) /
929 camdd_buf_get_len(struct camdd_buf *buf)
933 if (buf->buf_type != CAMDD_BUF_DATA) {
934 struct camdd_buf_indirect *indirect;
936 indirect = &buf->buf_type_spec.indirect;
939 struct camdd_buf_data *data;
941 data = &buf->buf_type_spec.data;
942 len = data->fill_len;
949 camdd_buf_add_child(struct camdd_buf *buf, struct camdd_buf *child_buf)
951 struct camdd_buf_data *data;
953 assert(buf->buf_type == CAMDD_BUF_DATA);
955 data = &buf->buf_type_spec.data;
957 STAILQ_INSERT_TAIL(&buf->src_list, child_buf, src_links);
960 data->fill_len += camdd_buf_get_len(child_buf);
968 } camdd_status_item_index;
970 static struct camdd_status_items {
972 struct mt_status_entry *entry;
973 } req_status_items[] = {
976 { "blk_gran", NULL },
977 { "max_effective_iosize", NULL }
981 camdd_probe_tape(int fd, char *filename, uint64_t *max_iosize,
982 uint64_t *max_blk, uint64_t *min_blk, uint64_t *blk_gran)
984 struct mt_status_data status_data;
985 char *xml_str = NULL;
989 retval = mt_get_xml_str(fd, MTIOCEXTGET, &xml_str);
991 err(1, "Couldn't get XML string from %s", filename);
993 retval = mt_get_status(xml_str, &status_data);
994 if (retval != XML_STATUS_OK) {
995 warn("couldn't get status for %s", filename);
1001 if (status_data.error != 0) {
1002 warnx("%s", status_data.error_str);
1007 for (i = 0; i < sizeof(req_status_items) /
1008 sizeof(req_status_items[0]); i++) {
1011 name = __DECONST(char *, req_status_items[i].name);
1012 req_status_items[i].entry = mt_status_entry_find(&status_data,
1014 if (req_status_items[i].entry == NULL) {
1015 errx(1, "Cannot find status entry %s",
1016 req_status_items[i].name);
1020 *max_iosize = req_status_items[CAMDD_TS_EFF_IOSIZE].entry->value_unsigned;
1021 *max_blk= req_status_items[CAMDD_TS_MAX_BLK].entry->value_unsigned;
1022 *min_blk= req_status_items[CAMDD_TS_MIN_BLK].entry->value_unsigned;
1023 *blk_gran = req_status_items[CAMDD_TS_BLK_GRAN].entry->value_unsigned;
1027 mt_status_free(&status_data);
1033 camdd_probe_file(int fd, struct camdd_io_opts *io_opts, int retry_count,
1036 struct camdd_dev *dev = NULL;
1037 struct camdd_dev_file *file_dev;
1038 uint64_t blocksize = io_opts->blocksize;
1040 dev = camdd_alloc_dev(CAMDD_DEV_FILE, NULL, 0, retry_count, timeout);
1044 file_dev = &dev->dev_spec.file;
1046 strlcpy(file_dev->filename, io_opts->dev_name,
1047 sizeof(file_dev->filename));
1048 strlcpy(dev->device_name, io_opts->dev_name, sizeof(dev->device_name));
1050 dev->blocksize = CAMDD_FILE_DEFAULT_BLOCK;
1052 dev->blocksize = blocksize;
1054 if ((io_opts->queue_depth != 0)
1055 && (io_opts->queue_depth != 1)) {
1056 warnx("Queue depth %ju for %s ignored, only 1 outstanding "
1057 "command supported", (uintmax_t)io_opts->queue_depth,
1060 dev->target_queue_depth = CAMDD_FILE_DEFAULT_DEPTH;
1061 dev->run = camdd_file_run;
1065 * We can effectively access files on byte boundaries. We'll reset
1066 * this for devices like disks that can be accessed on sector
1069 dev->sector_size = 1;
1071 if ((fd != STDIN_FILENO)
1072 && (fd != STDOUT_FILENO)) {
1075 retval = fstat(fd, &file_dev->sb);
1077 warn("Cannot stat %s", dev->device_name);
1080 if (S_ISREG(file_dev->sb.st_mode)) {
1081 file_dev->file_type = CAMDD_FILE_REG;
1082 } else if (S_ISCHR(file_dev->sb.st_mode)) {
1085 if (ioctl(fd, FIODTYPE, &type) == -1)
1086 err(1, "FIODTYPE ioctl failed on %s",
1090 file_dev->file_type = CAMDD_FILE_TAPE;
1091 else if (type & D_DISK)
1092 file_dev->file_type = CAMDD_FILE_DISK;
1093 else if (type & D_MEM)
1094 file_dev->file_type = CAMDD_FILE_MEM;
1095 else if (type & D_TTY)
1096 file_dev->file_type = CAMDD_FILE_TTY;
1098 } else if (S_ISDIR(file_dev->sb.st_mode)) {
1099 errx(1, "cannot operate on directory %s",
1101 } else if (S_ISFIFO(file_dev->sb.st_mode)) {
1102 file_dev->file_type = CAMDD_FILE_PIPE;
1104 errx(1, "Cannot determine file type for %s",
1107 switch (file_dev->file_type) {
1108 case CAMDD_FILE_REG:
1109 if (file_dev->sb.st_size != 0)
1110 dev->max_sector = file_dev->sb.st_size - 1;
1112 dev->max_sector = 0;
1113 file_dev->file_flags |= CAMDD_FF_CAN_SEEK;
1115 case CAMDD_FILE_TAPE: {
1116 uint64_t max_iosize, max_blk, min_blk, blk_gran;
1118 * Check block limits and maximum effective iosize.
1119 * Make sure the blocksize is within the block
1120 * limits (and a multiple of the minimum blocksize)
1121 * and that the blocksize is <= maximum effective
1124 retval = camdd_probe_tape(fd, dev->device_name,
1125 &max_iosize, &max_blk, &min_blk, &blk_gran);
1127 errx(1, "Unable to probe tape %s",
1131 * The blocksize needs to be <= the maximum
1132 * effective I/O size of the tape device. Note
1133 * that this also takes into account the maximum
1134 * blocksize reported by READ BLOCK LIMITS.
1136 if (dev->blocksize > max_iosize) {
1137 warnx("Blocksize %u too big for %s, limiting "
1138 "to %ju", dev->blocksize, dev->device_name,
1140 dev->blocksize = max_iosize;
1144 * The blocksize needs to be at least min_blk;
1146 if (dev->blocksize < min_blk) {
1147 warnx("Blocksize %u too small for %s, "
1148 "increasing to %ju", dev->blocksize,
1149 dev->device_name, min_blk);
1150 dev->blocksize = min_blk;
1154 * And the blocksize needs to be a multiple of
1155 * the block granularity.
1158 && (dev->blocksize % (1 << blk_gran))) {
1159 warnx("Blocksize %u for %s not a multiple of "
1160 "%d, adjusting to %d", dev->blocksize,
1161 dev->device_name, (1 << blk_gran),
1162 dev->blocksize & ~((1 << blk_gran) - 1));
1163 dev->blocksize &= ~((1 << blk_gran) - 1);
1166 if (dev->blocksize == 0) {
1167 errx(1, "Unable to derive valid blocksize for "
1168 "%s", dev->device_name);
1172 * For tape drives, set the sector size to the
1173 * blocksize so that we make sure not to write
1174 * less than the blocksize out to the drive.
1176 dev->sector_size = dev->blocksize;
1179 case CAMDD_FILE_DISK: {
1181 unsigned int sector_size;
1183 file_dev->file_flags |= CAMDD_FF_CAN_SEEK;
1185 if (ioctl(fd, DIOCGSECTORSIZE, §or_size) == -1) {
1186 err(1, "DIOCGSECTORSIZE ioctl failed on %s",
1190 if (sector_size == 0) {
1191 errx(1, "DIOCGSECTORSIZE ioctl returned "
1192 "invalid sector size %u for %s",
1193 sector_size, dev->device_name);
1196 if (ioctl(fd, DIOCGMEDIASIZE, &media_size) == -1) {
1197 err(1, "DIOCGMEDIASIZE ioctl failed on %s",
1201 if (media_size == 0) {
1202 errx(1, "DIOCGMEDIASIZE ioctl returned "
1203 "invalid media size %ju for %s",
1204 (uintmax_t)media_size, dev->device_name);
1207 if (dev->blocksize % sector_size) {
1208 errx(1, "%s blocksize %u not a multiple of "
1209 "sector size %u", dev->device_name,
1210 dev->blocksize, sector_size);
1213 dev->sector_size = sector_size;
1214 dev->max_sector = (media_size / sector_size) - 1;
1217 case CAMDD_FILE_MEM:
1218 file_dev->file_flags |= CAMDD_FF_CAN_SEEK;
1225 if ((io_opts->offset != 0)
1226 && ((file_dev->file_flags & CAMDD_FF_CAN_SEEK) == 0)) {
1227 warnx("Offset %ju specified for %s, but we cannot seek on %s",
1228 io_opts->offset, io_opts->dev_name, io_opts->dev_name);
1232 else if ((io_opts->offset != 0)
1233 && ((io_opts->offset % dev->sector_size) != 0)) {
1234 warnx("Offset %ju for %s is not a multiple of the "
1235 "sector size %u", io_opts->offset,
1236 io_opts->dev_name, dev->sector_size);
1239 dev->start_offset_bytes = io_opts->offset;
1247 camdd_free_dev(dev);
1252 * Need to implement this. Do a basic probe:
1253 * - Check the inquiry data, make sure we're talking to a device that we
1254 * can reasonably expect to talk to -- direct, RBC, CD, WORM.
1255 * - Send a test unit ready, make sure the device is available.
1256 * - Get the capacity and block size.
1259 camdd_probe_pass(struct cam_device *cam_dev, struct camdd_io_opts *io_opts,
1260 camdd_argmask arglist, int probe_retry_count,
1261 int probe_timeout, int io_retry_count, int io_timeout)
1265 uint32_t cpi_maxio, max_iosize, pass_numblocks;
1267 struct scsi_read_capacity_data rcap;
1268 struct scsi_read_capacity_data_long rcaplong;
1269 struct camdd_dev *dev;
1270 struct camdd_dev_pass *pass_dev;
1276 scsi_dev_type = SID_TYPE(&cam_dev->inq_data);
1281 * For devices that support READ CAPACITY, we'll attempt to get the
1282 * capacity. Otherwise, we really don't support tape or other
1283 * devices via SCSI passthrough, so just return an error in that case.
1285 switch (scsi_dev_type) {
1293 errx(1, "Unsupported SCSI device type %d", scsi_dev_type);
1294 break; /*NOTREACHED*/
1297 ccb = cam_getccb(cam_dev);
1300 warnx("%s: error allocating ccb", __func__);
1304 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
1306 scsi_read_capacity(&ccb->csio,
1307 /*retries*/ probe_retry_count,
1309 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1312 /*timeout*/ probe_timeout ? probe_timeout : 5000);
1314 /* Disable freezing the device queue */
1315 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1317 if (arglist & CAMDD_ARG_ERR_RECOVER)
1318 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1320 if (cam_send_ccb(cam_dev, ccb) < 0) {
1321 warn("error sending READ CAPACITY command");
1323 cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
1324 CAM_EPF_ALL, stderr);
1329 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1330 cam_error_print(cam_dev, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
1334 maxsector = scsi_4btoul(rcap.addr);
1335 block_len = scsi_4btoul(rcap.length);
1338 * A last block of 2^32-1 means that the true capacity is over 2TB,
1339 * and we need to issue the long READ CAPACITY to get the real
1340 * capacity. Otherwise, we're all set.
1342 if (maxsector != 0xffffffff)
1345 scsi_read_capacity_16(&ccb->csio,
1346 /*retries*/ probe_retry_count,
1348 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1352 (uint8_t *)&rcaplong,
1354 /*sense_len*/ SSD_FULL_SIZE,
1355 /*timeout*/ probe_timeout ? probe_timeout : 5000);
1357 /* Disable freezing the device queue */
1358 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1360 if (arglist & CAMDD_ARG_ERR_RECOVER)
1361 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1363 if (cam_send_ccb(cam_dev, ccb) < 0) {
1364 warn("error sending READ CAPACITY (16) command");
1365 cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
1366 CAM_EPF_ALL, stderr);
1370 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1371 cam_error_print(cam_dev, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
1375 maxsector = scsi_8btou64(rcaplong.addr);
1376 block_len = scsi_4btoul(rcaplong.length);
1379 if (block_len == 0) {
1380 warnx("Sector size for %s%u is 0, cannot continue",
1381 cam_dev->device_name, cam_dev->dev_unit_num);
1385 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cpi);
1387 ccb->ccb_h.func_code = XPT_PATH_INQ;
1388 ccb->ccb_h.flags = CAM_DIR_NONE;
1389 ccb->ccb_h.retry_count = 1;
1391 if (cam_send_ccb(cam_dev, ccb) < 0) {
1392 warn("error sending XPT_PATH_INQ CCB");
1394 cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
1395 CAM_EPF_ALL, stderr);
1399 EV_SET(&ke, cam_dev->fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
1401 dev = camdd_alloc_dev(CAMDD_DEV_PASS, &ke, 1, io_retry_count,
1406 pass_dev = &dev->dev_spec.pass;
1407 pass_dev->scsi_dev_type = scsi_dev_type;
1408 pass_dev->dev = cam_dev;
1409 pass_dev->max_sector = maxsector;
1410 pass_dev->block_len = block_len;
1411 pass_dev->cpi_maxio = ccb->cpi.maxio;
1412 snprintf(dev->device_name, sizeof(dev->device_name), "%s%u",
1413 pass_dev->dev->device_name, pass_dev->dev->dev_unit_num);
1414 dev->sector_size = block_len;
1415 dev->max_sector = maxsector;
1419 * Determine the optimal blocksize to use for this device.
1423 * If the controller has not specified a maximum I/O size,
1424 * just go with 128K as a somewhat conservative value.
1426 if (pass_dev->cpi_maxio == 0)
1429 cpi_maxio = pass_dev->cpi_maxio;
1432 * If the controller has a large maximum I/O size, limit it
1433 * to something smaller so that the kernel doesn't have trouble
1434 * allocating buffers to copy data in and out for us.
1435 * XXX KDM this is until we have unmapped I/O support in the kernel.
1437 max_iosize = min(cpi_maxio, CAMDD_PASS_MAX_BLOCK);
1440 * If we weren't able to get a block size for some reason,
1441 * default to 512 bytes.
1443 block_len = pass_dev->block_len;
1448 * Figure out how many blocksize chunks will fit in the
1451 pass_numblocks = max_iosize / block_len;
1454 * And finally, multiple the number of blocks by the LBA
1455 * length to get our maximum block size;
1457 dev->blocksize = pass_numblocks * block_len;
1459 if (io_opts->blocksize != 0) {
1460 if ((io_opts->blocksize % dev->sector_size) != 0) {
1461 warnx("Blocksize %ju for %s is not a multiple of "
1462 "sector size %u", (uintmax_t)io_opts->blocksize,
1463 dev->device_name, dev->sector_size);
1466 dev->blocksize = io_opts->blocksize;
1468 dev->target_queue_depth = CAMDD_PASS_DEFAULT_DEPTH;
1469 if (io_opts->queue_depth != 0)
1470 dev->target_queue_depth = io_opts->queue_depth;
1472 if (io_opts->offset != 0) {
1473 if (io_opts->offset > (dev->max_sector * dev->sector_size)) {
1474 warnx("Offset %ju is past the end of device %s",
1475 io_opts->offset, dev->device_name);
1479 else if ((io_opts->offset % dev->sector_size) != 0) {
1480 warnx("Offset %ju for %s is not a multiple of the "
1481 "sector size %u", io_opts->offset,
1482 dev->device_name, dev->sector_size);
1485 dev->start_offset_bytes = io_opts->offset;
1489 dev->min_cmd_size = io_opts->min_cmd_size;
1491 dev->run = camdd_pass_run;
1492 dev->fetch = camdd_pass_fetch;
1502 camdd_free_dev(dev);
1508 camdd_worker(void *arg)
1510 struct camdd_dev *dev = arg;
1511 struct camdd_buf *buf;
1512 struct timespec ts, *kq_ts;
1517 pthread_mutex_lock(&dev->mutex);
1519 dev->flags |= CAMDD_DEV_FLAG_ACTIVE;
1526 * XXX KDM check the reorder queue depth?
1528 if (dev->write_dev == 0) {
1529 uint32_t our_depth, peer_depth, peer_bytes, our_bytes;
1530 uint32_t target_depth = dev->target_queue_depth;
1531 uint32_t peer_target_depth =
1532 dev->peer_dev->target_queue_depth;
1533 uint32_t peer_blocksize = dev->peer_dev->blocksize;
1535 camdd_get_depth(dev, &our_depth, &peer_depth,
1536 &our_bytes, &peer_bytes);
1539 while (((our_depth < target_depth)
1540 && (peer_depth < peer_target_depth))
1541 || ((peer_bytes + our_bytes) <
1542 (peer_blocksize * 2))) {
1544 while (((our_depth + peer_depth) <
1545 (target_depth + peer_target_depth))
1546 || ((peer_bytes + our_bytes) <
1547 (peer_blocksize * 3))) {
1549 retval = camdd_queue(dev, NULL);
1552 else if (retval != 0) {
1557 camdd_get_depth(dev, &our_depth, &peer_depth,
1558 &our_bytes, &peer_bytes);
1562 * See if we have any I/O that is ready to execute.
1564 buf = STAILQ_FIRST(&dev->run_queue);
1566 while (dev->target_queue_depth > dev->cur_active_io) {
1567 retval = dev->run(dev);
1569 dev->flags |= CAMDD_DEV_FLAG_EOF;
1572 } else if (retval != 0) {
1579 * We've reached EOF, or our partner has reached EOF.
1581 if ((dev->flags & CAMDD_DEV_FLAG_EOF)
1582 || (dev->flags & CAMDD_DEV_FLAG_PEER_EOF)) {
1583 if (dev->write_dev != 0) {
1584 if ((STAILQ_EMPTY(&dev->work_queue))
1585 && (dev->num_run_queue == 0)
1586 && (dev->cur_active_io == 0)) {
1591 * If we're the reader, and the writer
1592 * got EOF, he is already done. If we got
1593 * the EOF, then we need to wait until
1594 * everything is flushed out for the writer.
1596 if (dev->flags & CAMDD_DEV_FLAG_PEER_EOF) {
1598 } else if ((dev->num_peer_work_queue == 0)
1599 && (dev->num_peer_done_queue == 0)
1600 && (dev->cur_active_io == 0)
1601 && (dev->num_run_queue == 0)) {
1606 * XXX KDM need to do something about the pending
1607 * queue and cleanup resources.
1611 if ((dev->write_dev == 0)
1612 && (dev->cur_active_io == 0)
1613 && (dev->peer_bytes_queued < dev->peer_dev->blocksize))
1619 * Run kevent to see if there are events to process.
1621 pthread_mutex_unlock(&dev->mutex);
1622 retval = kevent(dev->kq, NULL, 0, &ke, 1, kq_ts);
1623 pthread_mutex_lock(&dev->mutex);
1625 warn("%s: error returned from kevent",__func__);
1627 } else if (retval != 0) {
1628 switch (ke.filter) {
1630 if (dev->fetch != NULL) {
1631 retval = dev->fetch(dev);
1640 * We register for this so we don't get
1641 * an error as a result of a SIGINFO or a
1642 * SIGINT. It will actually get handled
1643 * by the signal handler. If we get a
1644 * SIGINT, bail out without printing an
1645 * error message. Any other signals
1646 * will result in the error message above.
1648 if (ke.ident == SIGINT)
1654 * Check to see if the other thread has
1655 * queued any I/O for us to do. (In this
1656 * case we're the writer.)
1658 for (buf = STAILQ_FIRST(&dev->work_queue);
1660 buf = STAILQ_FIRST(&dev->work_queue)) {
1661 STAILQ_REMOVE_HEAD(&dev->work_queue,
1663 retval = camdd_queue(dev, buf);
1665 * We keep going unless we get an
1666 * actual error. If we get EOF, we
1667 * still want to remove the buffers
1668 * from the queue and send the back
1669 * to the reader thread.
1679 * Next check to see if the other thread has
1680 * queued any completed buffers back to us.
1681 * (In this case we're the reader.)
1683 for (buf = STAILQ_FIRST(&dev->peer_done_queue);
1685 buf = STAILQ_FIRST(&dev->peer_done_queue)){
1687 &dev->peer_done_queue, work_links);
1688 dev->num_peer_done_queue--;
1689 camdd_peer_done(buf);
1693 warnx("%s: unknown kevent filter %d",
1694 __func__, ke.filter);
1702 dev->flags &= ~CAMDD_DEV_FLAG_ACTIVE;
1704 /* XXX KDM cleanup resources here? */
1706 pthread_mutex_unlock(&dev->mutex);
1709 sem_post(&camdd_sem);
1715 * Simplistic translation of CCB status to our local status.
1718 camdd_ccb_status(union ccb *ccb)
1720 camdd_buf_status status = CAMDD_STATUS_NONE;
1721 cam_status ccb_status;
1723 ccb_status = ccb->ccb_h.status & CAM_STATUS_MASK;
1725 switch (ccb_status) {
1727 if (ccb->csio.resid == 0) {
1728 status = CAMDD_STATUS_OK;
1729 } else if (ccb->csio.dxfer_len > ccb->csio.resid) {
1730 status = CAMDD_STATUS_SHORT_IO;
1732 status = CAMDD_STATUS_EOF;
1736 case CAM_SCSI_STATUS_ERROR: {
1737 switch (ccb->csio.scsi_status) {
1738 case SCSI_STATUS_OK:
1739 case SCSI_STATUS_COND_MET:
1740 case SCSI_STATUS_INTERMED:
1741 case SCSI_STATUS_INTERMED_COND_MET:
1742 status = CAMDD_STATUS_OK;
1744 case SCSI_STATUS_CMD_TERMINATED:
1745 case SCSI_STATUS_CHECK_COND:
1746 case SCSI_STATUS_QUEUE_FULL:
1747 case SCSI_STATUS_BUSY:
1748 case SCSI_STATUS_RESERV_CONFLICT:
1750 status = CAMDD_STATUS_ERROR;
1756 status = CAMDD_STATUS_ERROR;
1764 * Queue a buffer to our peer's work thread for writing.
1766 * Returns 0 for success, -1 for failure, 1 if the other thread exited.
1769 camdd_queue_peer_buf(struct camdd_dev *dev, struct camdd_buf *buf)
1772 STAILQ_HEAD(, camdd_buf) local_queue;
1773 struct camdd_buf *buf1, *buf2;
1774 struct camdd_buf_data *data = NULL;
1775 uint64_t peer_bytes_queued = 0;
1779 STAILQ_INIT(&local_queue);
1782 * Since we're the reader, we need to queue our I/O to the writer
1783 * in sequential order in order to make sure it gets written out
1784 * in sequential order.
1786 * Check the next expected I/O starting offset. If this doesn't
1787 * match, put it on the reorder queue.
1789 if ((buf->lba * dev->sector_size) != dev->next_completion_pos_bytes) {
1792 * If there is nothing on the queue, there is no sorting
1795 if (STAILQ_EMPTY(&dev->reorder_queue)) {
1796 STAILQ_INSERT_TAIL(&dev->reorder_queue, buf, links);
1797 dev->num_reorder_queue++;
1802 * Sort in ascending order by starting LBA. There should
1803 * be no identical LBAs.
1805 for (buf1 = STAILQ_FIRST(&dev->reorder_queue); buf1 != NULL;
1807 buf2 = STAILQ_NEXT(buf1, links);
1808 if (buf->lba < buf1->lba) {
1810 * If we're less than the first one, then
1811 * we insert at the head of the list
1812 * because this has to be the first element
1815 STAILQ_INSERT_HEAD(&dev->reorder_queue,
1817 dev->num_reorder_queue++;
1819 } else if (buf->lba > buf1->lba) {
1821 STAILQ_INSERT_TAIL(&dev->reorder_queue,
1823 dev->num_reorder_queue++;
1825 } else if (buf->lba < buf2->lba) {
1826 STAILQ_INSERT_AFTER(&dev->reorder_queue,
1828 dev->num_reorder_queue++;
1832 errx(1, "Found buffers with duplicate LBA %ju!",
1840 * We're the next expected I/O completion, so put ourselves
1841 * on the local queue to be sent to the writer. We use
1842 * work_links here so that we can queue this to the
1843 * peer_work_queue before taking the buffer off of the
1846 dev->next_completion_pos_bytes += buf->len;
1847 STAILQ_INSERT_TAIL(&local_queue, buf, work_links);
1850 * Go through the reorder queue looking for more sequential
1851 * I/O and add it to the local queue.
1853 for (buf1 = STAILQ_FIRST(&dev->reorder_queue); buf1 != NULL;
1854 buf1 = STAILQ_FIRST(&dev->reorder_queue)) {
1856 * As soon as we see an I/O that is out of sequence,
1859 if ((buf1->lba * dev->sector_size) !=
1860 dev->next_completion_pos_bytes)
1863 STAILQ_REMOVE_HEAD(&dev->reorder_queue, links);
1864 dev->num_reorder_queue--;
1865 STAILQ_INSERT_TAIL(&local_queue, buf1, work_links);
1866 dev->next_completion_pos_bytes += buf1->len;
1871 * Setup the event to let the other thread know that it has work
1874 EV_SET(&ke, (uintptr_t)&dev->peer_dev->work_queue, EVFILT_USER, 0,
1875 NOTE_TRIGGER, 0, NULL);
1878 * Put this on our shadow queue so that we know what we've queued
1879 * to the other thread.
1881 STAILQ_FOREACH_SAFE(buf1, &local_queue, work_links, buf2) {
1882 if (buf1->buf_type != CAMDD_BUF_DATA) {
1883 errx(1, "%s: should have a data buffer, not an "
1884 "indirect buffer", __func__);
1886 data = &buf1->buf_type_spec.data;
1889 * We only need to send one EOF to the writer, and don't
1890 * need to continue sending EOFs after that.
1892 if (buf1->status == CAMDD_STATUS_EOF) {
1893 if (dev->flags & CAMDD_DEV_FLAG_EOF_SENT) {
1894 STAILQ_REMOVE(&local_queue, buf1, camdd_buf,
1896 camdd_release_buf(buf1);
1900 dev->flags |= CAMDD_DEV_FLAG_EOF_SENT;
1904 STAILQ_INSERT_TAIL(&dev->peer_work_queue, buf1, links);
1905 peer_bytes_queued += (data->fill_len - data->resid);
1906 dev->peer_bytes_queued += (data->fill_len - data->resid);
1907 dev->num_peer_work_queue++;
1910 if (STAILQ_FIRST(&local_queue) == NULL)
1914 * Drop our mutex and pick up the other thread's mutex. We need to
1915 * do this to avoid deadlocks.
1917 pthread_mutex_unlock(&dev->mutex);
1918 pthread_mutex_lock(&dev->peer_dev->mutex);
1920 if (dev->peer_dev->flags & CAMDD_DEV_FLAG_ACTIVE) {
1922 * Put the buffers on the other thread's incoming work queue.
1924 for (buf1 = STAILQ_FIRST(&local_queue); buf1 != NULL;
1925 buf1 = STAILQ_FIRST(&local_queue)) {
1926 STAILQ_REMOVE_HEAD(&local_queue, work_links);
1927 STAILQ_INSERT_TAIL(&dev->peer_dev->work_queue, buf1,
1931 * Send an event to the other thread's kqueue to let it know
1932 * that there is something on the work queue.
1934 retval = kevent(dev->peer_dev->kq, &ke, 1, NULL, 0, NULL);
1936 warn("%s: unable to add peer work_queue kevent",
1943 pthread_mutex_unlock(&dev->peer_dev->mutex);
1944 pthread_mutex_lock(&dev->mutex);
1947 * If the other side isn't active, run through the queue and
1948 * release all of the buffers.
1951 for (buf1 = STAILQ_FIRST(&local_queue); buf1 != NULL;
1952 buf1 = STAILQ_FIRST(&local_queue)) {
1953 STAILQ_REMOVE_HEAD(&local_queue, work_links);
1954 STAILQ_REMOVE(&dev->peer_work_queue, buf1, camdd_buf,
1956 dev->num_peer_work_queue--;
1957 camdd_release_buf(buf1);
1959 dev->peer_bytes_queued -= peer_bytes_queued;
1968 * Return a buffer to the reader thread when we have completed writing it.
1971 camdd_complete_peer_buf(struct camdd_dev *dev, struct camdd_buf *peer_buf)
1977 * Setup the event to let the other thread know that we have
1978 * completed a buffer.
1980 EV_SET(&ke, (uintptr_t)&dev->peer_dev->peer_done_queue, EVFILT_USER, 0,
1981 NOTE_TRIGGER, 0, NULL);
1984 * Drop our lock and acquire the other thread's lock before
1987 pthread_mutex_unlock(&dev->mutex);
1988 pthread_mutex_lock(&dev->peer_dev->mutex);
1991 * Put the buffer on the reader thread's peer done queue now that
1992 * we have completed it.
1994 STAILQ_INSERT_TAIL(&dev->peer_dev->peer_done_queue, peer_buf,
1996 dev->peer_dev->num_peer_done_queue++;
1999 * Send an event to the peer thread to let it know that we've added
2000 * something to its peer done queue.
2002 retval = kevent(dev->peer_dev->kq, &ke, 1, NULL, 0, NULL);
2004 warn("%s: unable to add peer_done_queue kevent", __func__);
2009 * Drop the other thread's lock and reacquire ours.
2011 pthread_mutex_unlock(&dev->peer_dev->mutex);
2012 pthread_mutex_lock(&dev->mutex);
2018 * Free a buffer that was written out by the writer thread and returned to
2019 * the reader thread.
2022 camdd_peer_done(struct camdd_buf *buf)
2024 struct camdd_dev *dev;
2025 struct camdd_buf_data *data;
2028 if (buf->buf_type != CAMDD_BUF_DATA) {
2029 errx(1, "%s: should have a data buffer, not an "
2030 "indirect buffer", __func__);
2033 data = &buf->buf_type_spec.data;
2035 STAILQ_REMOVE(&dev->peer_work_queue, buf, camdd_buf, links);
2036 dev->num_peer_work_queue--;
2037 dev->peer_bytes_queued -= (data->fill_len - data->resid);
2039 if (buf->status == CAMDD_STATUS_EOF)
2040 dev->flags |= CAMDD_DEV_FLAG_PEER_EOF;
2042 STAILQ_INSERT_TAIL(&dev->free_queue, buf, links);
2046 * Assumes caller holds the lock for this device.
2049 camdd_complete_buf(struct camdd_dev *dev, struct camdd_buf *buf,
2055 * If we're the reader, we need to send the completed I/O
2056 * to the writer. If we're the writer, we need to just
2057 * free up resources, or let the reader know if we've
2058 * encountered an error.
2060 if (dev->write_dev == 0) {
2061 retval = camdd_queue_peer_buf(dev, buf);
2065 struct camdd_buf *tmp_buf, *next_buf;
2067 STAILQ_FOREACH_SAFE(tmp_buf, &buf->src_list, src_links,
2069 struct camdd_buf *src_buf;
2070 struct camdd_buf_indirect *indirect;
2072 STAILQ_REMOVE(&buf->src_list, tmp_buf,
2073 camdd_buf, src_links);
2075 tmp_buf->status = buf->status;
2077 if (tmp_buf->buf_type == CAMDD_BUF_DATA) {
2078 camdd_complete_peer_buf(dev, tmp_buf);
2082 indirect = &tmp_buf->buf_type_spec.indirect;
2083 src_buf = indirect->src_buf;
2084 src_buf->refcount--;
2086 * XXX KDM we probably need to account for
2087 * exactly how many bytes we were able to
2088 * write. Allocate the residual to the
2089 * first N buffers? Or just track the
2090 * number of bytes written? Right now the reader
2091 * doesn't do anything with a residual.
2093 src_buf->status = buf->status;
2094 if (src_buf->refcount <= 0)
2095 camdd_complete_peer_buf(dev, src_buf);
2096 STAILQ_INSERT_TAIL(&dev->free_indirect_queue,
2100 STAILQ_INSERT_TAIL(&dev->free_queue, buf, links);
2105 * Fetch all completed commands from the pass(4) device.
2107 * Returns the number of commands received, or -1 if any of the commands
2108 * completed with an error. Returns 0 if no commands are available.
2111 camdd_pass_fetch(struct camdd_dev *dev)
2113 struct camdd_dev_pass *pass_dev = &dev->dev_spec.pass;
2115 int retval = 0, num_fetched = 0, error_count = 0;
2117 pthread_mutex_unlock(&dev->mutex);
2119 * XXX KDM we don't distinguish between EFAULT and ENOENT.
2121 while ((retval = ioctl(pass_dev->dev->fd, CAMIOGET, &ccb)) != -1) {
2122 struct camdd_buf *buf;
2123 struct camdd_buf_data *data;
2124 cam_status ccb_status;
2127 buf = ccb.ccb_h.ccb_buf;
2128 data = &buf->buf_type_spec.data;
2129 buf_ccb = &data->ccb;
2134 * Copy the CCB back out so we get status, sense data, etc.
2136 bcopy(&ccb, buf_ccb, sizeof(ccb));
2138 pthread_mutex_lock(&dev->mutex);
2141 * We're now done, so take this off the active queue.
2143 STAILQ_REMOVE(&dev->active_queue, buf, camdd_buf, links);
2144 dev->cur_active_io--;
2146 ccb_status = ccb.ccb_h.status & CAM_STATUS_MASK;
2147 if (ccb_status != CAM_REQ_CMP) {
2148 cam_error_print(pass_dev->dev, &ccb, CAM_ESF_ALL,
2149 CAM_EPF_ALL, stderr);
2152 data->resid = ccb.csio.resid;
2153 dev->bytes_transferred += (ccb.csio.dxfer_len - ccb.csio.resid);
2155 if (buf->status == CAMDD_STATUS_NONE)
2156 buf->status = camdd_ccb_status(&ccb);
2157 if (buf->status == CAMDD_STATUS_ERROR)
2159 else if (buf->status == CAMDD_STATUS_EOF) {
2161 * Once we queue this buffer to our partner thread,
2162 * he will know that we've hit EOF.
2164 dev->flags |= CAMDD_DEV_FLAG_EOF;
2167 camdd_complete_buf(dev, buf, &error_count);
2170 * Unlock in preparation for the ioctl call.
2172 pthread_mutex_unlock(&dev->mutex);
2175 pthread_mutex_lock(&dev->mutex);
2177 if (error_count > 0)
2180 return (num_fetched);
2184 * Returns -1 for error, 0 for success/continue, and 1 for resource
2185 * shortage/stop processing.
2188 camdd_file_run(struct camdd_dev *dev)
2190 struct camdd_dev_file *file_dev = &dev->dev_spec.file;
2191 struct camdd_buf_data *data;
2192 struct camdd_buf *buf;
2194 int retval = 0, write_dev = dev->write_dev;
2195 int error_count = 0, no_resources = 0, double_buf_needed = 0;
2196 uint32_t num_sectors = 0, db_len = 0;
2198 buf = STAILQ_FIRST(&dev->run_queue);
2202 } else if ((dev->write_dev == 0)
2203 && (dev->flags & (CAMDD_DEV_FLAG_EOF |
2204 CAMDD_DEV_FLAG_EOF_SENT))) {
2205 STAILQ_REMOVE(&dev->run_queue, buf, camdd_buf, links);
2206 dev->num_run_queue--;
2207 buf->status = CAMDD_STATUS_EOF;
2213 * If we're writing, we need to go through the source buffer list
2214 * and create an S/G list.
2216 if (write_dev != 0) {
2217 retval = camdd_buf_sg_create(buf, /*iovec*/ 1,
2218 dev->sector_size, &num_sectors, &double_buf_needed);
2225 STAILQ_REMOVE(&dev->run_queue, buf, camdd_buf, links);
2226 dev->num_run_queue--;
2228 data = &buf->buf_type_spec.data;
2231 * pread(2) and pwrite(2) offsets are byte offsets.
2233 io_offset = buf->lba * dev->sector_size;
2236 * Unlock the mutex while we read or write.
2238 pthread_mutex_unlock(&dev->mutex);
2241 * Note that we don't need to double buffer if we're the reader
2242 * because in that case, we have allocated a single buffer of
2243 * sufficient size to do the read. This copy is necessary on
2244 * writes because if one of the components of the S/G list is not
2245 * a sector size multiple, the kernel will reject the write. This
2246 * is unfortunate but not surprising. So this will make sure that
2247 * we're using a single buffer that is a multiple of the sector size.
2249 if ((double_buf_needed != 0)
2250 && (data->sg_count > 1)
2251 && (write_dev != 0)) {
2252 uint32_t cur_offset;
2255 if (file_dev->tmp_buf == NULL)
2256 file_dev->tmp_buf = calloc(dev->blocksize, 1);
2257 if (file_dev->tmp_buf == NULL) {
2258 buf->status = CAMDD_STATUS_ERROR;
2260 pthread_mutex_lock(&dev->mutex);
2263 for (i = 0, cur_offset = 0; i < data->sg_count; i++) {
2264 bcopy(data->iovec[i].iov_base,
2265 &file_dev->tmp_buf[cur_offset],
2266 data->iovec[i].iov_len);
2267 cur_offset += data->iovec[i].iov_len;
2269 db_len = cur_offset;
2272 if (file_dev->file_flags & CAMDD_FF_CAN_SEEK) {
2273 if (write_dev == 0) {
2275 * XXX KDM is there any way we would need a S/G
2278 retval = pread(file_dev->fd, data->buf,
2279 buf->len, io_offset);
2281 if (double_buf_needed != 0) {
2282 retval = pwrite(file_dev->fd, file_dev->tmp_buf,
2284 } else if (data->sg_count == 0) {
2285 retval = pwrite(file_dev->fd, data->buf,
2286 data->fill_len, io_offset);
2288 retval = pwritev(file_dev->fd, data->iovec,
2289 data->sg_count, io_offset);
2293 if (write_dev == 0) {
2295 * XXX KDM is there any way we would need a S/G
2298 retval = read(file_dev->fd, data->buf, buf->len);
2300 if (double_buf_needed != 0) {
2301 retval = write(file_dev->fd, file_dev->tmp_buf,
2303 } else if (data->sg_count == 0) {
2304 retval = write(file_dev->fd, data->buf,
2307 retval = writev(file_dev->fd, data->iovec,
2313 /* We're done, re-acquire the lock */
2314 pthread_mutex_lock(&dev->mutex);
2316 if (retval >= (ssize_t)data->fill_len) {
2318 * If the bytes transferred is more than the request size,
2319 * that indicates an overrun, which should only happen at
2320 * the end of a transfer if we have to round up to a sector
2323 if (buf->status == CAMDD_STATUS_NONE)
2324 buf->status = CAMDD_STATUS_OK;
2326 dev->bytes_transferred += retval;
2327 } else if (retval == -1) {
2328 warn("Error %s %s", (write_dev) ? "writing to" :
2329 "reading from", file_dev->filename);
2331 buf->status = CAMDD_STATUS_ERROR;
2332 data->resid = data->fill_len;
2335 if (dev->debug == 0)
2338 if ((double_buf_needed != 0)
2339 && (write_dev != 0)) {
2340 fprintf(stderr, "%s: fd %d, DB buf %p, len %u lba %ju "
2341 "offset %ju\n", __func__, file_dev->fd,
2342 file_dev->tmp_buf, db_len, (uintmax_t)buf->lba,
2343 (uintmax_t)io_offset);
2344 } else if (data->sg_count == 0) {
2345 fprintf(stderr, "%s: fd %d, buf %p, len %u, lba %ju "
2346 "offset %ju\n", __func__, file_dev->fd, data->buf,
2347 data->fill_len, (uintmax_t)buf->lba,
2348 (uintmax_t)io_offset);
2352 fprintf(stderr, "%s: fd %d, len %u, lba %ju "
2353 "offset %ju\n", __func__, file_dev->fd,
2354 data->fill_len, (uintmax_t)buf->lba,
2355 (uintmax_t)io_offset);
2357 for (i = 0; i < data->sg_count; i++) {
2358 fprintf(stderr, "index %d ptr %p len %zu\n",
2359 i, data->iovec[i].iov_base,
2360 data->iovec[i].iov_len);
2363 } else if (retval == 0) {
2364 buf->status = CAMDD_STATUS_EOF;
2365 if (dev->debug != 0)
2366 printf("%s: got EOF from %s!\n", __func__,
2367 file_dev->filename);
2368 data->resid = data->fill_len;
2370 } else if (retval < (ssize_t)data->fill_len) {
2371 if (buf->status == CAMDD_STATUS_NONE)
2372 buf->status = CAMDD_STATUS_SHORT_IO;
2373 data->resid = data->fill_len - retval;
2374 dev->bytes_transferred += retval;
2379 if (buf->status == CAMDD_STATUS_EOF) {
2380 struct camdd_buf *buf2;
2381 dev->flags |= CAMDD_DEV_FLAG_EOF;
2382 STAILQ_FOREACH(buf2, &dev->run_queue, links)
2383 buf2->status = CAMDD_STATUS_EOF;
2386 camdd_complete_buf(dev, buf, &error_count);
2389 if (error_count != 0)
2391 else if (no_resources != 0)
2398 * Execute one command from the run queue. Returns 0 for success, 1 for
2399 * stop processing, and -1 for error.
2402 camdd_pass_run(struct camdd_dev *dev)
2404 struct camdd_buf *buf = NULL;
2405 struct camdd_dev_pass *pass_dev = &dev->dev_spec.pass;
2406 struct camdd_buf_data *data;
2407 uint32_t num_blocks, sectors_used = 0;
2409 int retval = 0, is_write = dev->write_dev;
2410 int double_buf_needed = 0;
2412 buf = STAILQ_FIRST(&dev->run_queue);
2419 * If we're writing, we need to go through the source buffer list
2420 * and create an S/G list.
2422 if (is_write != 0) {
2423 retval = camdd_buf_sg_create(buf, /*iovec*/ 0,dev->sector_size,
2424 §ors_used, &double_buf_needed);
2431 STAILQ_REMOVE(&dev->run_queue, buf, camdd_buf, links);
2432 dev->num_run_queue--;
2434 data = &buf->buf_type_spec.data;
2437 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
2440 * In almost every case the number of blocks should be the device
2441 * block size. The exception may be at the end of an I/O stream
2442 * for a partial block or at the end of a device.
2445 num_blocks = sectors_used;
2447 num_blocks = data->fill_len / pass_dev->block_len;
2449 scsi_read_write(&ccb->csio,
2450 /*retries*/ dev->retry_count,
2452 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2453 /*readop*/ (dev->write_dev == 0) ? SCSI_RW_READ :
2456 /*minimum_cmd_size*/ dev->min_cmd_size,
2458 /*block_count*/ num_blocks,
2459 /*data_ptr*/ (data->sg_count != 0) ?
2460 (uint8_t *)data->segs : data->buf,
2461 /*dxfer_len*/ (num_blocks * pass_dev->block_len),
2462 /*sense_len*/ SSD_FULL_SIZE,
2463 /*timeout*/ dev->io_timeout);
2465 /* Disable freezing the device queue */
2466 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
2468 if (dev->retry_count != 0)
2469 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
2471 if (data->sg_count != 0) {
2472 ccb->csio.sglist_cnt = data->sg_count;
2473 ccb->ccb_h.flags |= CAM_DATA_SG;
2477 * Store a pointer to the buffer in the CCB. The kernel will
2478 * restore this when we get it back, and we'll use it to identify
2479 * the buffer this CCB came from.
2481 ccb->ccb_h.ccb_buf = buf;
2484 * Unlock our mutex in preparation for issuing the ioctl.
2486 pthread_mutex_unlock(&dev->mutex);
2488 * Queue the CCB to the pass(4) driver.
2490 if (ioctl(pass_dev->dev->fd, CAMIOQUEUE, ccb) == -1) {
2491 pthread_mutex_lock(&dev->mutex);
2493 warn("%s: error sending CAMIOQUEUE ioctl to %s%u", __func__,
2494 pass_dev->dev->device_name, pass_dev->dev->dev_unit_num);
2495 warn("%s: CCB address is %p", __func__, ccb);
2498 STAILQ_INSERT_TAIL(&dev->free_queue, buf, links);
2500 pthread_mutex_lock(&dev->mutex);
2502 dev->cur_active_io++;
2503 STAILQ_INSERT_TAIL(&dev->active_queue, buf, links);
2511 camdd_get_next_lba_len(struct camdd_dev *dev, uint64_t *lba, ssize_t *len)
2513 struct camdd_dev_pass *pass_dev;
2514 uint32_t num_blocks;
2517 pass_dev = &dev->dev_spec.pass;
2519 *lba = dev->next_io_pos_bytes / dev->sector_size;
2520 *len = dev->blocksize;
2521 num_blocks = *len / dev->sector_size;
2524 * If max_sector is 0, then we have no set limit. This can happen
2525 * if we're writing to a file in a filesystem, or reading from
2526 * something like /dev/zero.
2528 if ((dev->max_sector != 0)
2529 || (dev->sector_io_limit != 0)) {
2530 uint64_t max_sector;
2532 if ((dev->max_sector != 0)
2533 && (dev->sector_io_limit != 0))
2534 max_sector = min(dev->sector_io_limit, dev->max_sector);
2535 else if (dev->max_sector != 0)
2536 max_sector = dev->max_sector;
2538 max_sector = dev->sector_io_limit;
2542 * Check to see whether we're starting off past the end of
2543 * the device. If so, we need to just send an EOF
2544 * notification to the writer.
2546 if (*lba > max_sector) {
2549 } else if (((*lba + num_blocks) > max_sector + 1)
2550 || ((*lba + num_blocks) < *lba)) {
2552 * If we get here (but pass the first check), we
2553 * can trim the request length down to go to the
2554 * end of the device.
2556 num_blocks = (max_sector + 1) - *lba;
2557 *len = num_blocks * dev->sector_size;
2562 dev->next_io_pos_bytes += *len;
2568 * Returns 0 for success, 1 for EOF detected, and -1 for failure.
2571 camdd_queue(struct camdd_dev *dev, struct camdd_buf *read_buf)
2573 struct camdd_buf *buf = NULL;
2574 struct camdd_buf_data *data;
2575 struct camdd_dev_pass *pass_dev;
2577 struct camdd_buf_data *rb_data;
2578 int is_write = dev->write_dev;
2579 int eof_flush_needed = 0;
2583 pass_dev = &dev->dev_spec.pass;
2586 * If we've gotten EOF or our partner has, we should not continue
2587 * queueing I/O. If we're a writer, though, we should continue
2588 * to write any buffers that don't have EOF status.
2590 if ((dev->flags & CAMDD_DEV_FLAG_EOF)
2591 || ((dev->flags & CAMDD_DEV_FLAG_PEER_EOF)
2592 && (is_write == 0))) {
2594 * Tell the worker thread that we have seen EOF.
2599 * If we're the writer, send the buffer back with EOF status.
2602 read_buf->status = CAMDD_STATUS_EOF;
2604 error = camdd_complete_peer_buf(dev, read_buf);
2609 if (is_write == 0) {
2610 buf = camdd_get_buf(dev, CAMDD_BUF_DATA);
2615 data = &buf->buf_type_spec.data;
2617 retval = camdd_get_next_lba_len(dev, &buf->lba, &buf->len);
2619 buf->status = CAMDD_STATUS_EOF;
2622 && ((dev->flags & (CAMDD_DEV_FLAG_EOF_SENT |
2623 CAMDD_DEV_FLAG_EOF_QUEUED)) != 0)) {
2624 camdd_release_buf(buf);
2627 dev->flags |= CAMDD_DEV_FLAG_EOF_QUEUED;
2630 data->fill_len = buf->len;
2631 data->src_start_offset = buf->lba * dev->sector_size;
2634 * Put this on the run queue.
2636 STAILQ_INSERT_TAIL(&dev->run_queue, buf, links);
2637 dev->num_run_queue++;
2644 * Check for new EOF status from the reader.
2646 if ((read_buf->status == CAMDD_STATUS_EOF)
2647 || (read_buf->status == CAMDD_STATUS_ERROR)) {
2648 dev->flags |= CAMDD_DEV_FLAG_PEER_EOF;
2649 if ((STAILQ_FIRST(&dev->pending_queue) == NULL)
2650 && (read_buf->len == 0)) {
2651 camdd_complete_peer_buf(dev, read_buf);
2655 eof_flush_needed = 1;
2659 * See if we have a buffer we're composing with pieces from our
2662 buf = STAILQ_FIRST(&dev->pending_queue);
2667 retval = camdd_get_next_lba_len(dev, &lba, &len);
2669 read_buf->status = CAMDD_STATUS_EOF;
2672 dev->flags |= CAMDD_DEV_FLAG_EOF;
2673 error = camdd_complete_peer_buf(dev, read_buf);
2679 * If we don't have a pending buffer, we need to grab a new
2680 * one from the free list or allocate another one.
2682 buf = camdd_get_buf(dev, CAMDD_BUF_DATA);
2691 STAILQ_INSERT_TAIL(&dev->pending_queue, buf, links);
2692 dev->num_pending_queue++;
2695 data = &buf->buf_type_spec.data;
2697 rb_data = &read_buf->buf_type_spec.data;
2699 if ((rb_data->src_start_offset != dev->next_peer_pos_bytes)
2700 && (dev->debug != 0)) {
2701 printf("%s: WARNING: reader offset %#jx != expected offset "
2702 "%#jx\n", __func__, (uintmax_t)rb_data->src_start_offset,
2703 (uintmax_t)dev->next_peer_pos_bytes);
2705 dev->next_peer_pos_bytes = rb_data->src_start_offset +
2706 (rb_data->fill_len - rb_data->resid);
2708 new_len = (rb_data->fill_len - rb_data->resid) + data->fill_len;
2709 if (new_len < buf->len) {
2711 * There are three cases here:
2712 * 1. We need more data to fill up a block, so we put
2713 * this I/O on the queue and wait for more I/O.
2714 * 2. We have a pending buffer in the queue that is
2715 * smaller than our blocksize, but we got an EOF. So we
2716 * need to go ahead and flush the write out.
2717 * 3. We got an error.
2721 * Increment our fill length.
2723 data->fill_len += (rb_data->fill_len - rb_data->resid);
2726 * Add the new read buffer to the list for writing.
2728 STAILQ_INSERT_TAIL(&buf->src_list, read_buf, src_links);
2730 /* Increment the count */
2733 if (eof_flush_needed == 0) {
2735 * We need to exit, because we don't have enough
2741 * Take the buffer off of the pending queue.
2743 STAILQ_REMOVE(&dev->pending_queue, buf, camdd_buf,
2745 dev->num_pending_queue--;
2748 * If we need an EOF flush, but there is no data
2749 * to flush, go ahead and return this buffer.
2751 if (data->fill_len == 0) {
2752 camdd_complete_buf(dev, buf, /*error_count*/0);
2758 * Put this on the next queue for execution.
2760 STAILQ_INSERT_TAIL(&dev->run_queue, buf, links);
2761 dev->num_run_queue++;
2763 } else if (new_len == buf->len) {
2765 * We have enough data to completey fill one block,
2766 * so we're ready to issue the I/O.
2770 * Take the buffer off of the pending queue.
2772 STAILQ_REMOVE(&dev->pending_queue, buf, camdd_buf, links);
2773 dev->num_pending_queue--;
2776 * Add the new read buffer to the list for writing.
2778 STAILQ_INSERT_TAIL(&buf->src_list, read_buf, src_links);
2780 /* Increment the count */
2784 * Increment our fill length.
2786 data->fill_len += (rb_data->fill_len - rb_data->resid);
2789 * Put this on the next queue for execution.
2791 STAILQ_INSERT_TAIL(&dev->run_queue, buf, links);
2792 dev->num_run_queue++;
2794 struct camdd_buf *idb;
2795 struct camdd_buf_indirect *indirect;
2796 uint32_t len_to_go, cur_offset;
2799 idb = camdd_get_buf(dev, CAMDD_BUF_INDIRECT);
2804 indirect = &idb->buf_type_spec.indirect;
2805 indirect->src_buf = read_buf;
2806 read_buf->refcount++;
2807 indirect->offset = 0;
2808 indirect->start_ptr = rb_data->buf;
2810 * We've already established that there is more
2811 * data in read_buf than we have room for in our
2812 * current write request. So this particular chunk
2813 * of the request should just be the remainder
2814 * needed to fill up a block.
2816 indirect->len = buf->len - (data->fill_len - data->resid);
2818 camdd_buf_add_child(buf, idb);
2821 * This buffer is ready to execute, so we can take
2822 * it off the pending queue and put it on the run
2825 STAILQ_REMOVE(&dev->pending_queue, buf, camdd_buf,
2827 dev->num_pending_queue--;
2828 STAILQ_INSERT_TAIL(&dev->run_queue, buf, links);
2829 dev->num_run_queue++;
2831 cur_offset = indirect->offset + indirect->len;
2834 * The resulting I/O would be too large to fit in
2835 * one block. We need to split this I/O into
2836 * multiple pieces. Allocate as many buffers as needed.
2838 for (len_to_go = rb_data->fill_len - rb_data->resid -
2839 indirect->len; len_to_go > 0;) {
2840 struct camdd_buf *new_buf;
2841 struct camdd_buf_data *new_data;
2845 retval = camdd_get_next_lba_len(dev, &lba, &len);
2849 * The device has already been marked
2850 * as EOF, and there is no space left.
2855 new_buf = camdd_get_buf(dev, CAMDD_BUF_DATA);
2856 if (new_buf == NULL) {
2864 idb = camdd_get_buf(dev, CAMDD_BUF_INDIRECT);
2870 indirect = &idb->buf_type_spec.indirect;
2872 indirect->src_buf = read_buf;
2873 read_buf->refcount++;
2874 indirect->offset = cur_offset;
2875 indirect->start_ptr = rb_data->buf + cur_offset;
2876 indirect->len = min(len_to_go, new_buf->len);
2878 if (((indirect->len % dev->sector_size) != 0)
2879 || ((indirect->offset % dev->sector_size) != 0)) {
2880 warnx("offset %ju len %ju not aligned with "
2881 "sector size %u", indirect->offset,
2882 (uintmax_t)indirect->len, dev->sector_size);
2885 cur_offset += indirect->len;
2886 len_to_go -= indirect->len;
2888 camdd_buf_add_child(new_buf, idb);
2890 new_data = &new_buf->buf_type_spec.data;
2892 if ((new_data->fill_len == new_buf->len)
2893 || (eof_flush_needed != 0)) {
2894 STAILQ_INSERT_TAIL(&dev->run_queue,
2896 dev->num_run_queue++;
2897 } else if (new_data->fill_len < buf->len) {
2898 STAILQ_INSERT_TAIL(&dev->pending_queue,
2900 dev->num_pending_queue++;
2902 warnx("%s: too much data in new "
2903 "buffer!", __func__);
2915 camdd_get_depth(struct camdd_dev *dev, uint32_t *our_depth,
2916 uint32_t *peer_depth, uint32_t *our_bytes, uint32_t *peer_bytes)
2918 *our_depth = dev->cur_active_io + dev->num_run_queue;
2919 if (dev->num_peer_work_queue >
2920 dev->num_peer_done_queue)
2921 *peer_depth = dev->num_peer_work_queue -
2922 dev->num_peer_done_queue;
2925 *our_bytes = *our_depth * dev->blocksize;
2926 *peer_bytes = dev->peer_bytes_queued;
2930 camdd_sig_handler(int sig)
2939 sem_post(&camdd_sem);
2943 camdd_print_status(struct camdd_dev *camdd_dev, struct camdd_dev *other_dev,
2944 struct timespec *start_time)
2946 struct timespec done_time;
2948 long double mb_sec, total_sec;
2951 error = clock_gettime(CLOCK_MONOTONIC_PRECISE, &done_time);
2953 warn("Unable to get done time");
2957 timespecsub(&done_time, start_time);
2959 total_ns = done_time.tv_nsec + (done_time.tv_sec * 1000000000);
2960 total_sec = total_ns;
2961 total_sec /= 1000000000;
2963 fprintf(stderr, "%ju bytes %s %s\n%ju bytes %s %s\n"
2964 "%.4Lf seconds elapsed\n",
2965 (uintmax_t)camdd_dev->bytes_transferred,
2966 (camdd_dev->write_dev == 0) ? "read from" : "written to",
2967 camdd_dev->device_name,
2968 (uintmax_t)other_dev->bytes_transferred,
2969 (other_dev->write_dev == 0) ? "read from" : "written to",
2970 other_dev->device_name, total_sec);
2972 mb_sec = min(other_dev->bytes_transferred,camdd_dev->bytes_transferred);
2973 mb_sec /= 1024 * 1024;
2974 mb_sec *= 1000000000;
2976 fprintf(stderr, "%.2Lf MB/sec\n", mb_sec);
2980 camdd_rw(struct camdd_io_opts *io_opts, int num_io_opts, uint64_t max_io,
2981 int retry_count, int timeout)
2983 struct cam_device *new_cam_dev = NULL;
2984 struct camdd_dev *devs[2];
2985 struct timespec start_time;
2986 pthread_t threads[2];
2991 if (num_io_opts != 2) {
2992 warnx("Must have one input and one output path");
2997 bzero(devs, sizeof(devs));
2999 for (i = 0; i < num_io_opts; i++) {
3000 switch (io_opts[i].dev_type) {
3001 case CAMDD_DEV_PASS: {
3002 if (isdigit(io_opts[i].dev_name[0])) {
3003 camdd_argmask new_arglist = CAMDD_ARG_NONE;
3004 int bus = 0, target = 0, lun = 0;
3007 /* device specified as bus:target[:lun] */
3008 rv = parse_btl(io_opts[i].dev_name, &bus,
3009 &target, &lun, &new_arglist);
3011 warnx("numeric device specification "
3012 "must be either bus:target, or "
3017 /* default to 0 if lun was not specified */
3018 if ((new_arglist & CAMDD_ARG_LUN) == 0) {
3020 new_arglist |= CAMDD_ARG_LUN;
3022 new_cam_dev = cam_open_btl(bus, target, lun,
3027 if (cam_get_device(io_opts[i].dev_name, name,
3028 sizeof name, &unit) == -1) {
3029 warnx("%s", cam_errbuf);
3033 new_cam_dev = cam_open_spec_device(name, unit,
3037 if (new_cam_dev == NULL) {
3038 warnx("%s", cam_errbuf);
3043 devs[i] = camdd_probe_pass(new_cam_dev,
3044 /*io_opts*/ &io_opts[i],
3045 CAMDD_ARG_ERR_RECOVER,
3046 /*probe_retry_count*/ 3,
3047 /*probe_timeout*/ 5000,
3048 /*io_retry_count*/ retry_count,
3049 /*io_timeout*/ timeout);
3050 if (devs[i] == NULL) {
3051 warn("Unable to probe device %s%u",
3052 new_cam_dev->device_name,
3053 new_cam_dev->dev_unit_num);
3059 case CAMDD_DEV_FILE: {
3062 if (io_opts[i].dev_name[0] == '-') {
3063 if (io_opts[i].write_dev != 0)
3068 if (io_opts[i].write_dev != 0) {
3069 fd = open(io_opts[i].dev_name,
3070 O_RDWR | O_CREAT, S_IWUSR |S_IRUSR);
3072 fd = open(io_opts[i].dev_name,
3077 warn("error opening file %s",
3078 io_opts[i].dev_name);
3083 devs[i] = camdd_probe_file(fd, &io_opts[i],
3084 retry_count, timeout);
3085 if (devs[i] == NULL) {
3093 warnx("Unknown device type %d (%s)",
3094 io_opts[i].dev_type, io_opts[i].dev_name);
3097 break; /*NOTREACHED */
3100 devs[i]->write_dev = io_opts[i].write_dev;
3102 devs[i]->start_offset_bytes = io_opts[i].offset;
3105 devs[i]->sector_io_limit =
3106 (devs[i]->start_offset_bytes /
3107 devs[i]->sector_size) +
3108 (max_io / devs[i]->sector_size) - 1;
3109 devs[i]->sector_io_limit =
3110 (devs[i]->start_offset_bytes /
3111 devs[i]->sector_size) +
3112 (max_io / devs[i]->sector_size) - 1;
3115 devs[i]->next_io_pos_bytes = devs[i]->start_offset_bytes;
3116 devs[i]->next_completion_pos_bytes =devs[i]->start_offset_bytes;
3119 devs[0]->peer_dev = devs[1];
3120 devs[1]->peer_dev = devs[0];
3121 devs[0]->next_peer_pos_bytes = devs[0]->peer_dev->next_io_pos_bytes;
3122 devs[1]->next_peer_pos_bytes = devs[1]->peer_dev->next_io_pos_bytes;
3124 sem_init(&camdd_sem, /*pshared*/ 0, 0);
3126 signal(SIGINFO, camdd_sig_handler);
3127 signal(SIGINT, camdd_sig_handler);
3129 error = clock_gettime(CLOCK_MONOTONIC_PRECISE, &start_time);
3131 warn("Unable to get start time");
3135 for (i = 0; i < num_io_opts; i++) {
3136 error = pthread_create(&threads[i], NULL, camdd_worker,
3139 warnc(error, "pthread_create() failed");
3145 if ((sem_wait(&camdd_sem) == -1)
3146 || (need_exit != 0)) {
3149 for (i = 0; i < num_io_opts; i++) {
3150 EV_SET(&ke, (uintptr_t)&devs[i]->work_queue,
3151 EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
3153 devs[i]->flags |= CAMDD_DEV_FLAG_EOF;
3155 error = kevent(devs[i]->kq, &ke, 1, NULL, 0,
3158 warn("%s: unable to wake up thread",
3163 } else if (need_status != 0) {
3164 camdd_print_status(devs[0], devs[1], &start_time);
3168 for (i = 0; i < num_io_opts; i++) {
3169 pthread_join(threads[i], NULL);
3172 camdd_print_status(devs[0], devs[1], &start_time);
3176 for (i = 0; i < num_io_opts; i++)
3177 camdd_free_dev(devs[i]);
3179 return (error + error_exit);
3186 "usage: camdd <-i|-o pass=pass0,bs=1M,offset=1M,depth=4>\n"
3187 " <-i|-o file=/tmp/file,bs=512K,offset=1M>\n"
3188 " <-i|-o file=/dev/da0,bs=512K,offset=1M>\n"
3189 " <-i|-o file=/dev/nsa0,bs=512K>\n"
3190 " [-C retry_count][-E][-m max_io_amt][-t timeout_secs][-v][-h]\n"
3191 "Option description\n"
3192 "-i <arg=val> Specify input device/file and parameters\n"
3193 "-o <arg=val> Specify output device/file and parameters\n"
3194 "Input and Output parameters\n"
3195 "pass=name Specify a pass(4) device like pass0 or /dev/pass0\n"
3196 "file=name Specify a file or device, /tmp/foo, /dev/da0, /dev/null\n"
3197 " or - for stdin/stdout\n"
3198 "bs=blocksize Specify blocksize in bytes, or using K, M, G, etc. suffix\n"
3199 "offset=len Specify starting offset in bytes or using K, M, G suffix\n"
3200 " NOTE: offset cannot be specified on tapes, pipes, stdin/out\n"
3201 "depth=N Specify a numeric queue depth. This only applies to pass(4)\n"
3202 "mcs=N Specify a minimum cmd size for pass(4) read/write commands\n"
3203 "Optional arguments\n"
3204 "-C retry_cnt Specify a retry count for pass(4) devices\n"
3205 "-E Enable CAM error recovery for pass(4) devices\n"
3206 "-m max_io Specify the maximum amount to be transferred in bytes or\n"
3207 " using K, G, M, etc. suffixes\n"
3208 "-t timeout Specify the I/O timeout to use with pass(4) devices\n"
3209 "-v Enable verbose error recovery\n"
3210 "-h Print this message\n");
3215 camdd_parse_io_opts(char *args, int is_write, struct camdd_io_opts *io_opts)
3217 char *tmpstr, *tmpstr2;
3218 char *orig_tmpstr = NULL;
3221 io_opts->write_dev = is_write;
3223 tmpstr = strdup(args);
3224 if (tmpstr == NULL) {
3225 warn("strdup failed");
3229 orig_tmpstr = tmpstr;
3230 while ((tmpstr2 = strsep(&tmpstr, ",")) != NULL) {
3234 * If the user creates an empty parameter by putting in two
3235 * commas, skip over it and look for the next field.
3237 if (*tmpstr2 == '\0')
3240 name = strsep(&tmpstr2, "=");
3241 if (*name == '\0') {
3242 warnx("Got empty I/O parameter name");
3246 value = strsep(&tmpstr2, "=");
3248 || (*value == '\0')) {
3249 warnx("Empty I/O parameter value for %s", name);
3253 if (strncasecmp(name, "file", 4) == 0) {
3254 io_opts->dev_type = CAMDD_DEV_FILE;
3255 io_opts->dev_name = strdup(value);
3256 if (io_opts->dev_name == NULL) {
3257 warn("Error allocating memory");
3261 } else if (strncasecmp(name, "pass", 4) == 0) {
3262 io_opts->dev_type = CAMDD_DEV_PASS;
3263 io_opts->dev_name = strdup(value);
3264 if (io_opts->dev_name == NULL) {
3265 warn("Error allocating memory");
3269 } else if ((strncasecmp(name, "bs", 2) == 0)
3270 || (strncasecmp(name, "blocksize", 9) == 0)) {
3271 retval = expand_number(value, &io_opts->blocksize);
3273 warn("expand_number(3) failed on %s=%s", name,
3278 } else if (strncasecmp(name, "depth", 5) == 0) {
3281 io_opts->queue_depth = strtoull(value, &endptr, 0);
3282 if (*endptr != '\0') {
3283 warnx("invalid queue depth %s", value);
3287 } else if (strncasecmp(name, "mcs", 3) == 0) {
3290 io_opts->min_cmd_size = strtol(value, &endptr, 0);
3291 if ((*endptr != '\0')
3292 || ((io_opts->min_cmd_size > 16)
3293 || (io_opts->min_cmd_size < 0))) {
3294 warnx("invalid minimum cmd size %s", value);
3298 } else if (strncasecmp(name, "offset", 6) == 0) {
3299 retval = expand_number(value, &io_opts->offset);
3301 warn("expand_number(3) failed on %s=%s", name,
3306 } else if (strncasecmp(name, "debug", 5) == 0) {
3309 io_opts->debug = strtoull(value, &endptr, 0);
3310 if (*endptr != '\0') {
3311 warnx("invalid debug level %s", value);
3316 warnx("Unrecognized parameter %s=%s", name, value);
3326 main(int argc, char **argv)
3329 camdd_argmask arglist = CAMDD_ARG_NONE;
3330 int timeout = 0, retry_count = 1;
3332 uint64_t max_io = 0;
3333 struct camdd_io_opts *opt_list = NULL;
3340 opt_list = calloc(2, sizeof(struct camdd_io_opts));
3341 if (opt_list == NULL) {
3342 warn("Unable to allocate option list");
3347 while ((c = getopt(argc, argv, "C:Ehi:m:o:t:v")) != -1){
3350 retry_count = strtol(optarg, NULL, 0);
3351 if (retry_count < 0)
3352 errx(1, "retry count %d is < 0",
3354 arglist |= CAMDD_ARG_RETRIES;
3357 arglist |= CAMDD_ARG_ERR_RECOVER;
3362 && (opt_list[0].dev_type != CAMDD_DEV_NONE))
3364 && (opt_list[1].dev_type != CAMDD_DEV_NONE))) {
3365 errx(1, "Only one input and output path "
3368 error = camdd_parse_io_opts(optarg, (c == 'o') ? 1 : 0,
3369 (c == 'o') ? &opt_list[1] : &opt_list[0]);
3374 error = expand_number(optarg, &max_io);
3376 warn("invalid maximum I/O amount %s", optarg);
3382 timeout = strtol(optarg, NULL, 0);
3384 errx(1, "invalid timeout %d", timeout);
3385 /* Convert the timeout from seconds to ms */
3387 arglist |= CAMDD_ARG_TIMEOUT;
3390 arglist |= CAMDD_ARG_VERBOSE;
3396 break; /*NOTREACHED*/
3400 if ((opt_list[0].dev_type == CAMDD_DEV_NONE)
3401 || (opt_list[1].dev_type == CAMDD_DEV_NONE))
3402 errx(1, "Must specify both -i and -o");
3405 * Set the timeout if the user hasn't specified one.
3408 timeout = CAMDD_PASS_RW_TIMEOUT;
3410 error = camdd_rw(opt_list, 2, max_io, retry_count, timeout);