Lines Matching refs:be_lun

145 typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
147 typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
239 static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
241 static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
243 static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
245 static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
247 static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
249 static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
251 static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
253 static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
255 static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
257 static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
259 static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
265 static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
267 static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
269 static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
270 static int ctl_be_block_open(struct ctl_be_block_lun *be_lun,
417 struct ctl_be_block_lun *be_lun;
458 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
460 be_lun->dispatch(be_lun, beio);
462 mtx_lock(&be_lun->queue_lock);
463 STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
464 mtx_unlock(&be_lun->queue_lock);
465 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
474 struct ctl_be_block_lun *be_lun;
479 be_lun = beio->lun;
485 mtx_lock(&be_lun->io_lock);
507 mtx_unlock(&be_lun->io_lock);
518 mtx_unlock(&be_lun->io_lock);
566 ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
576 mtx_lock(&be_lun->io_lock);
578 mtx_unlock(&be_lun->io_lock);
580 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
583 ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
587 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
588 error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
590 VOP_UNLOCK(be_lun->vn, 0);
594 mtx_lock(&be_lun->io_lock);
598 mtx_unlock(&be_lun->io_lock);
618 ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
630 file_data = &be_lun->backend.file;
659 mtx_lock(&be_lun->io_lock);
661 mtx_unlock(&be_lun->io_lock);
664 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
683 error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
685 VOP_UNLOCK(be_lun->vn, 0);
707 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
710 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
714 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
731 error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
732 VOP_UNLOCK(be_lun->vn, 0);
738 mtx_lock(&be_lun->io_lock);
742 mtx_unlock(&be_lun->io_lock);
780 ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
791 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
792 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
793 error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
798 error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
804 off = be_lun->size_bytes;
807 VOP_UNLOCK(be_lun->vn, 0);
811 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
819 ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
827 if (be_lun->vn == NULL)
829 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
831 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
833 val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
836 (be_lun->vn->v_iflag & VI_DOOMED) == 0) {
837 error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
840 be_lun->cbe_lun.blocksize;
842 VOP_UNLOCK(be_lun->vn, 0);
847 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
887 mtx_lock(&be_lun->io_lock);
889 mtx_unlock(&be_lun->io_lock);
891 csw = devvn_refthread(be_lun->vn, &dev, &ref);
906 mtx_lock(&be_lun->io_lock);
910 mtx_unlock(&be_lun->io_lock);
948 ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
961 csw = devvn_refthread(be_lun->vn, &dev, &ref);
964 off = be_lun->size_bytes;
967 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
979 off = be_lun->size_bytes;
987 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
995 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1024 mtx_lock(&be_lun->io_lock);
1025 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1026 mtx_unlock(&be_lun->io_lock);
1028 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1040 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1050 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1051 maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1061 bio->bio_pblkno = off / be_lun->cbe_lun.blocksize;
1066 mtx_lock(&be_lun->io_lock);
1070 mtx_unlock(&be_lun->io_lock);
1084 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1097 mtx_lock(&be_lun->io_lock);
1098 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1099 mtx_unlock(&be_lun->io_lock);
1108 be_lun->cbe_lun.blocksize;
1110 ctl_be_block_unmap_dev_range(be_lun, beio,
1111 scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
1115 ctl_be_block_unmap_dev_range(be_lun, beio,
1120 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1131 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1165 bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1176 mtx_lock(&be_lun->io_lock);
1177 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1179 mtx_unlock(&be_lun->io_lock);
1198 ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1205 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1222 ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
1225 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1239 be_lun->lun_flush(be_lun, beio);
1260 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1263 struct ctl_be_block_softc *softc = be_lun->softc;
1264 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1278 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1296 be_lun->unmap(be_lun, beio);
1306 pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
1307 if (be_lun->cbe_lun.pblockoff > 0)
1308 pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1360 be_lun->dispatch(be_lun, beio);
1364 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1375 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1392 be_lun->unmap(be_lun, beio);
1406 ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1414 softc = be_lun->softc;
1417 beio->lun = be_lun;
1427 if (be_lun->get_lba_status)
1428 be_lun->get_lba_status(be_lun, beio);
1449 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1457 softc = be_lun->softc;
1460 beio->lun = be_lun;
1481 ctl_be_block_cw_dispatch_sync(be_lun, io);
1485 ctl_be_block_cw_dispatch_ws(be_lun, io);
1488 ctl_be_block_cw_dispatch_unmap(be_lun, io);
1504 struct ctl_be_block_lun *be_lun;
1508 be_lun = beio->lun;
1520 mtx_lock(&be_lun->queue_lock);
1521 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1522 mtx_unlock(&be_lun->queue_lock);
1523 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1527 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1530 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1538 softc = be_lun->softc;
1551 beio->lun = be_lun;
1634 be_lun->dispatch(be_lun, beio);
1644 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1645 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1656 mtx_lock(&be_lun->queue_lock);
1657 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1660 STAILQ_REMOVE_HEAD(&be_lun->datamove_queue, links);
1661 mtx_unlock(&be_lun->queue_lock);
1668 be_lun->dispatch(be_lun, beio);
1671 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1674 STAILQ_REMOVE_HEAD(&be_lun->config_write_queue, links);
1675 mtx_unlock(&be_lun->queue_lock);
1681 ctl_be_block_cw_dispatch(be_lun, io);
1684 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1687 STAILQ_REMOVE_HEAD(&be_lun->config_read_queue, links);
1688 mtx_unlock(&be_lun->queue_lock);
1694 ctl_be_block_cr_dispatch(be_lun, io);
1697 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1700 STAILQ_REMOVE_HEAD(&be_lun->input_queue, links);
1701 mtx_unlock(&be_lun->queue_lock);
1707 ctl_be_block_dispatch(be_lun, io);
1715 mtx_unlock(&be_lun->queue_lock);
1728 struct ctl_be_block_lun *be_lun;
1732 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
1739 mtx_lock(&be_lun->queue_lock);
1740 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1741 mtx_unlock(&be_lun->queue_lock);
1742 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1789 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1799 cbe_lun = &be_lun->cbe_lun;
1800 file_data = &be_lun->backend.file;
1801 params = &be_lun->params;
1803 be_lun->dev_type = CTL_BE_BLOCK_FILE;
1804 be_lun->dispatch = ctl_be_block_dispatch_file;
1805 be_lun->lun_flush = ctl_be_block_flush_file;
1806 be_lun->get_lba_status = ctl_be_block_gls_file;
1807 be_lun->getattr = ctl_be_block_getattr_file;
1808 be_lun->unmap = NULL;
1811 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1815 be_lun->dev_path);
1821 be_lun->size_bytes = params->lun_size_bytes;
1823 be_lun->size_bytes = vattr.va_size;
1837 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
1838 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
1839 0 : (be_lun->size_blocks - 1);
1876 if (be_lun->size_bytes < cbe_lun->blocksize) {
1879 "file %s size %ju < block size %u", be_lun->dev_path,
1880 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1888 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1890 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1898 params = &be_lun->params;
1900 be_lun->dev_type = CTL_BE_BLOCK_DEV;
1901 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1905 be_lun->dispatch = ctl_be_block_dispatch_zvol;
1906 be_lun->get_lba_status = ctl_be_block_gls_zvol;
1909 be_lun->dispatch = ctl_be_block_dispatch_dev;
1910 be_lun->get_lba_status = NULL;
1918 be_lun->lun_flush = ctl_be_block_flush_dev;
1919 be_lun->getattr = ctl_be_block_getattr_dev;
1920 be_lun->unmap = ctl_be_block_unmap_dev;
1925 "no d_ioctl for device %s!", be_lun->dev_path);
1935 "on %s!", error, be_lun->dev_path);
1975 be_lun->dev_path);
1990 be_lun->size_bytes = params->lun_size_bytes;
1992 be_lun->size_bytes = otmp;
1993 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
1994 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
1995 0 : (be_lun->size_blocks - 1);
2041 if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2065 ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2067 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2070 if (be_lun->vn) {
2074 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2075 be_lun->vn = NULL;
2077 switch (be_lun->dev_type) {
2081 if (be_lun->backend.file.cred != NULL) {
2082 crfree(be_lun->backend.file.cred);
2083 be_lun->backend.file.cred = NULL;
2089 panic("Unexpected backend type %d", be_lun->dev_type);
2092 be_lun->dev_type = CTL_BE_BLOCK_NONE;
2098 ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2100 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2119 free(be_lun->dev_path, M_CTLBLK);
2120 be_lun->dev_path = strdup(value, M_CTLBLK);
2131 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2144 if (be_lun->dev_path[0] != '/') {
2148 be_lun->dev_path);
2149 free(be_lun->dev_path, M_CTLBLK);
2150 be_lun->dev_path = dev_name;
2154 "error opening %s: %d", be_lun->dev_path, error);
2163 be_lun->vn = nd.ni_vp;
2166 if (vn_isdisk(be_lun->vn, &error)) {
2167 error = ctl_be_block_open_dev(be_lun, req);
2168 } else if (be_lun->vn->v_type == VREG) {
2169 error = ctl_be_block_open_file(be_lun, req);
2173 "%s is not a disk or plain file", be_lun->dev_path);
2175 VOP_UNLOCK(be_lun->vn, 0);
2178 ctl_be_block_close(be_lun);
2180 if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2196 struct ctl_be_block_lun *be_lun;
2208 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
2209 cbe_lun = &be_lun->cbe_lun;
2210 be_lun->params = req->reqdata.create;
2211 be_lun->softc = softc;
2212 STAILQ_INIT(&be_lun->input_queue);
2213 STAILQ_INIT(&be_lun->config_read_queue);
2214 STAILQ_INIT(&be_lun->config_write_queue);
2215 STAILQ_INIT(&be_lun->datamove_queue);
2216 mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF);
2217 mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF);
2224 be_lun->flags = 0;
2235 be_lun->size_bytes = params->lun_size_bytes;
2242 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2243 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2244 0 : (be_lun->size_blocks - 1);
2248 retval = ctl_be_block_open(be_lun, req);
2277 if (be_lun->vn == NULL)
2280 params->lun_size_bytes = be_lun->size_bytes;
2319 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2321 be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK,
2322 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2324 if (be_lun->io_taskqueue == NULL) {
2344 retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
2353 be_lun->num_threads = num_threads;
2355 retval = ctl_add_lun(&be_lun->cbe_lun);
2364 be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id,
2373 SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
2383 if (be_lun->io_taskqueue != NULL)
2384 taskqueue_free(be_lun->io_taskqueue);
2385 ctl_be_block_close(be_lun);
2386 if (be_lun->dev_path != NULL)
2387 free(be_lun->dev_path, M_CTLBLK);
2389 mtx_destroy(&be_lun->queue_lock);
2390 mtx_destroy(&be_lun->io_lock);
2391 free(be_lun, M_CTLBLK);
2400 struct ctl_be_block_lun *be_lun;
2408 SLIST_FOREACH(be_lun, &softc->lun_list, links) {
2409 if (be_lun->cbe_lun.lun_id == params->lun_id) {
2410 SLIST_REMOVE(&softc->lun_list, be_lun,
2418 if (be_lun == NULL) {
2424 cbe_lun = &be_lun->cbe_lun;
2426 if (be_lun->vn != NULL) {
2429 taskqueue_drain_all(be_lun->io_taskqueue);
2430 ctl_be_block_close(be_lun);
2434 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2443 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2449 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2450 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0);
2454 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2455 if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2457 free(be_lun, M_CTLBLK);
2475 struct ctl_be_block_lun *be_lun;
2485 SLIST_FOREACH(be_lun, &softc->lun_list, links) {
2486 if (be_lun->cbe_lun.lun_id == params->lun_id)
2490 if (be_lun == NULL) {
2496 cbe_lun = &be_lun->cbe_lun;
2499 be_lun->params.lun_size_bytes = params->lun_size_bytes;
2524 oldsize = be_lun->size_blocks;
2527 if (be_lun->vn == NULL)
2528 error = ctl_be_block_open(be_lun, req);
2529 else if (vn_isdisk(be_lun->vn, &error))
2530 error = ctl_be_block_open_dev(be_lun, req);
2531 else if (be_lun->vn->v_type == VREG) {
2532 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2533 error = ctl_be_block_open_file(be_lun, req);
2534 VOP_UNLOCK(be_lun->vn, 0);
2538 be_lun->vn != NULL) {
2542 be_lun->vn == NULL) {
2548 if (be_lun->vn != NULL) {
2551 taskqueue_drain_all(be_lun->io_taskqueue);
2552 error = ctl_be_block_close(be_lun);
2556 if (be_lun->size_blocks != oldsize)
2560 params->lun_size_bytes = be_lun->size_bytes;
2575 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun;
2576 struct ctl_be_block_softc *softc = be_lun->softc;
2578 taskqueue_drain_all(be_lun->io_taskqueue);
2579 taskqueue_free(be_lun->io_taskqueue);
2580 if (be_lun->disk_stats != NULL)
2581 devstat_remove_entry(be_lun->disk_stats);
2582 nvlist_destroy(be_lun->cbe_lun.options);
2583 free(be_lun->dev_path, M_CTLBLK);
2584 mtx_destroy(&be_lun->queue_lock);
2585 mtx_destroy(&be_lun->io_lock);
2588 be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2589 if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2590 wakeup(be_lun);
2592 free(be_lun, M_CTLBLK);
2599 struct ctl_be_block_lun *be_lun;
2606 be_lun = (struct ctl_be_block_lun *)cbe_lun;
2623 mtx_lock(&be_lun->queue_lock);
2624 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2626 mtx_unlock(&be_lun->queue_lock);
2627 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2640 if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2641 retval = ctl_be_block_open(be_lun, &req);
2658 if (be_lun->vn != NULL)
2659 ctl_be_block_close(be_lun);
2684 struct ctl_be_block_lun *be_lun;
2689 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
2694 mtx_lock(&be_lun->queue_lock);
2695 STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2697 mtx_unlock(&be_lun->queue_lock);
2698 taskqueue_enqueue(be_lun->io_taskqueue,
2699 &be_lun->io_task);