Deleted Added
full compact
ctl_backend_block.c (240993) ctl_backend_block.c (241896)
1/*-
2 * Copyright (c) 2003 Silicon Graphics International Corp.
3 * Copyright (c) 2009-2011 Spectra Logic Corporation
4 * Copyright (c) 2012 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.

--- 26 unchanged lines hidden (view full) ---

35 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
36 */
37/*
38 * CAM Target Layer driver backend for block devices.
39 *
40 * Author: Ken Merry <ken@FreeBSD.org>
41 */
42#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2003 Silicon Graphics International Corp.
3 * Copyright (c) 2009-2011 Spectra Logic Corporation
4 * Copyright (c) 2012 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.

--- 26 unchanged lines hidden (view full) ---

35 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
36 */
37/*
38 * CAM Target Layer driver backend for block devices.
39 *
40 * Author: Ken Merry <ken@FreeBSD.org>
41 */
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 240993 2012-09-27 10:51:38Z trasz $");
43__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 241896 2012-10-22 17:50:54Z kib $");
44
45#include <opt_kdtrace.h>
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/types.h>
51#include <sys/kthread.h>

--- 537 unchanged lines hidden (view full) ---

589}
590
591static void
592ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
593 struct ctl_be_block_io *beio)
594{
595 union ctl_io *io;
596 struct mount *mountpoint;
44
45#include <opt_kdtrace.h>
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/types.h>
51#include <sys/kthread.h>

--- 537 unchanged lines hidden (view full) ---

589}
590
591static void
592ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
593 struct ctl_be_block_io *beio)
594{
595 union ctl_io *io;
596 struct mount *mountpoint;
597 int vfs_is_locked, error, lock_flags;
597 int error, lock_flags;
598
599 DPRINTF("entered\n");
600
601 io = beio->io;
602
598
599 DPRINTF("entered\n");
600
601 io = beio->io;
602
603 vfs_is_locked = VFS_LOCK_GIANT(be_lun->vn->v_mount);
604
605 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
606
607 if (MNT_SHARED_WRITES(mountpoint)
608 || ((mountpoint == NULL)
609 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
610 lock_flags = LK_SHARED;
611 else
612 lock_flags = LK_EXCLUSIVE;
613
614 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
615
616 binuptime(&beio->ds_t0);
617 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
618
619 error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
620 VOP_UNLOCK(be_lun->vn, 0);
621
622 vn_finished_write(mountpoint);
623
603 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
604
605 if (MNT_SHARED_WRITES(mountpoint)
606 || ((mountpoint == NULL)
607 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
608 lock_flags = LK_SHARED;
609 else
610 lock_flags = LK_EXCLUSIVE;
611
612 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
613
614 binuptime(&beio->ds_t0);
615 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
616
617 error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
618 VOP_UNLOCK(be_lun->vn, 0);
619
620 vn_finished_write(mountpoint);
621
624 VFS_UNLOCK_GIANT(vfs_is_locked);
625
626 if (error == 0)
627 ctl_set_success(&io->scsiio);
628 else {
629 /* XXX KDM is there is a better error here? */
630 ctl_set_internal_failure(&io->scsiio,
631 /*sks_valid*/ 1,
632 /*retry_count*/ 0xbad1);
633 }

--- 9 unchanged lines hidden (view full) ---

643static void
644ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
645 struct ctl_be_block_io *beio)
646{
647 struct ctl_be_block_filedata *file_data;
648 union ctl_io *io;
649 struct uio xuio;
650 struct iovec *xiovec;
622 if (error == 0)
623 ctl_set_success(&io->scsiio);
624 else {
625 /* XXX KDM is there is a better error here? */
626 ctl_set_internal_failure(&io->scsiio,
627 /*sks_valid*/ 1,
628 /*retry_count*/ 0xbad1);
629 }

--- 9 unchanged lines hidden (view full) ---

639static void
640ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
641 struct ctl_be_block_io *beio)
642{
643 struct ctl_be_block_filedata *file_data;
644 union ctl_io *io;
645 struct uio xuio;
646 struct iovec *xiovec;
651 int vfs_is_locked, flags;
647 int flags;
652 int error, i;
653
654 DPRINTF("entered\n");
655
656 file_data = &be_lun->backend.file;
657 io = beio->io;
658 flags = beio->bio_flags;
659

--- 16 unchanged lines hidden (view full) ---

676 xuio.uio_iovcnt = beio->num_segs;
677 xuio.uio_td = curthread;
678
679 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
680 xiovec->iov_base = beio->sg_segs[i].addr;
681 xiovec->iov_len = beio->sg_segs[i].len;
682 }
683
648 int error, i;
649
650 DPRINTF("entered\n");
651
652 file_data = &be_lun->backend.file;
653 io = beio->io;
654 flags = beio->bio_flags;
655

--- 16 unchanged lines hidden (view full) ---

672 xuio.uio_iovcnt = beio->num_segs;
673 xuio.uio_td = curthread;
674
675 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
676 xiovec->iov_base = beio->sg_segs[i].addr;
677 xiovec->iov_len = beio->sg_segs[i].len;
678 }
679
684 vfs_is_locked = VFS_LOCK_GIANT(be_lun->vn->v_mount);
685 if (beio->bio_cmd == BIO_READ) {
686 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
687
688 binuptime(&beio->ds_t0);
689 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
690
691 /*
692 * UFS pays attention to IO_DIRECT for reads. If the

--- 56 unchanged lines hidden (view full) ---

749 * IO_SYNC in either the UFS or ZFS case.
750 */
751 error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
752 IO_SYNC : 0, file_data->cred);
753 VOP_UNLOCK(be_lun->vn, 0);
754
755 vn_finished_write(mountpoint);
756 }
680 if (beio->bio_cmd == BIO_READ) {
681 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
682
683 binuptime(&beio->ds_t0);
684 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
685
686 /*
687 * UFS pays attention to IO_DIRECT for reads. If the

--- 56 unchanged lines hidden (view full) ---

744 * IO_SYNC in either the UFS or ZFS case.
745 */
746 error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
747 IO_SYNC : 0, file_data->cred);
748 VOP_UNLOCK(be_lun->vn, 0);
749
750 vn_finished_write(mountpoint);
751 }
757 VFS_UNLOCK_GIANT(vfs_is_locked);
758
759 /*
760 * If we got an error, set the sense data to "MEDIUM ERROR" and
761 * return the I/O to the user.
762 */
763 if (error != 0) {
764 char path_str[32];
765

--- 707 unchanged lines hidden (view full) ---

1473}
1474
1475static int
1476ctl_be_block_close(struct ctl_be_block_lun *be_lun)
1477{
1478 DROP_GIANT();
1479 if (be_lun->vn) {
1480 int flags = FREAD | FWRITE;
752
753 /*
754 * If we got an error, set the sense data to "MEDIUM ERROR" and
755 * return the I/O to the user.
756 */
757 if (error != 0) {
758 char path_str[32];
759

--- 707 unchanged lines hidden (view full) ---

1467}
1468
1469static int
1470ctl_be_block_close(struct ctl_be_block_lun *be_lun)
1471{
1472 DROP_GIANT();
1473 if (be_lun->vn) {
1474 int flags = FREAD | FWRITE;
1481 int vfs_is_locked = 0;
1482
1483 switch (be_lun->dev_type) {
1484 case CTL_BE_BLOCK_DEV:
1485 if (be_lun->backend.dev.csw) {
1486 dev_relthread(be_lun->backend.dev.cdev,
1487 be_lun->backend.dev.dev_ref);
1488 be_lun->backend.dev.csw = NULL;
1489 be_lun->backend.dev.cdev = NULL;
1490 }
1491 break;
1492 case CTL_BE_BLOCK_FILE:
1475
1476 switch (be_lun->dev_type) {
1477 case CTL_BE_BLOCK_DEV:
1478 if (be_lun->backend.dev.csw) {
1479 dev_relthread(be_lun->backend.dev.cdev,
1480 be_lun->backend.dev.dev_ref);
1481 be_lun->backend.dev.csw = NULL;
1482 be_lun->backend.dev.cdev = NULL;
1483 }
1484 break;
1485 case CTL_BE_BLOCK_FILE:
1493 vfs_is_locked = VFS_LOCK_GIANT(be_lun->vn->v_mount);
1494 break;
1495 case CTL_BE_BLOCK_NONE:
1496 default:
1497 panic("Unexpected backend type.");
1498 break;
1499 }
1500
1501 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1502 be_lun->vn = NULL;
1503
1504 switch (be_lun->dev_type) {
1505 case CTL_BE_BLOCK_DEV:
1506 break;
1507 case CTL_BE_BLOCK_FILE:
1486 break;
1487 case CTL_BE_BLOCK_NONE:
1488 default:
1489 panic("Unexpected backend type.");
1490 break;
1491 }
1492
1493 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1494 be_lun->vn = NULL;
1495
1496 switch (be_lun->dev_type) {
1497 case CTL_BE_BLOCK_DEV:
1498 break;
1499 case CTL_BE_BLOCK_FILE:
1508 VFS_UNLOCK_GIANT(vfs_is_locked);
1509 if (be_lun->backend.file.cred != NULL) {
1510 crfree(be_lun->backend.file.cred);
1511 be_lun->backend.file.cred = NULL;
1512 }
1513 break;
1514 case CTL_BE_BLOCK_NONE:
1515 default:
1516 panic("Unexpected backend type.");

--- 7 unchanged lines hidden (view full) ---

1524
1525static int
1526ctl_be_block_open(struct ctl_be_block_softc *softc,
1527 struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1528{
1529 struct nameidata nd;
1530 int flags;
1531 int error;
1500 if (be_lun->backend.file.cred != NULL) {
1501 crfree(be_lun->backend.file.cred);
1502 be_lun->backend.file.cred = NULL;
1503 }
1504 break;
1505 case CTL_BE_BLOCK_NONE:
1506 default:
1507 panic("Unexpected backend type.");

--- 7 unchanged lines hidden (view full) ---

1515
1516static int
1517ctl_be_block_open(struct ctl_be_block_softc *softc,
1518 struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1519{
1520 struct nameidata nd;
1521 int flags;
1522 int error;
1532 int vfs_is_locked;
1533
1534 /*
1535 * XXX KDM allow a read-only option?
1536 */
1537 flags = FREAD | FWRITE;
1538 error = 0;
1539
1540 if (rootvnode == NULL) {

--- 41 unchanged lines hidden (view full) ---

1582 goto again;
1583 }
1584 }
1585 snprintf(req->error_str, sizeof(req->error_str),
1586 "%s: error opening %s", __func__, be_lun->dev_path);
1587 return (error);
1588 }
1589
1523
1524 /*
1525 * XXX KDM allow a read-only option?
1526 */
1527 flags = FREAD | FWRITE;
1528 error = 0;
1529
1530 if (rootvnode == NULL) {

--- 41 unchanged lines hidden (view full) ---

1572 goto again;
1573 }
1574 }
1575 snprintf(req->error_str, sizeof(req->error_str),
1576 "%s: error opening %s", __func__, be_lun->dev_path);
1577 return (error);
1578 }
1579
1590 vfs_is_locked = NDHASGIANT(&nd);
1591
1592 NDFREE(&nd, NDF_ONLY_PNBUF);
1593
1594 be_lun->vn = nd.ni_vp;
1595
1596 /* We only support disks and files. */
1597 if (vn_isdisk(be_lun->vn, &error)) {
1598 error = ctl_be_block_open_dev(be_lun, req);
1599 } else if (be_lun->vn->v_type == VREG) {
1600 error = ctl_be_block_open_file(be_lun, req);
1601 } else {
1602 error = EINVAL;
1603 snprintf(req->error_str, sizeof(req->error_str),
1604 "%s is not a disk or file", be_lun->dev_path);
1605 }
1606 VOP_UNLOCK(be_lun->vn, 0);
1580 NDFREE(&nd, NDF_ONLY_PNBUF);
1581
1582 be_lun->vn = nd.ni_vp;
1583
1584 /* We only support disks and files. */
1585 if (vn_isdisk(be_lun->vn, &error)) {
1586 error = ctl_be_block_open_dev(be_lun, req);
1587 } else if (be_lun->vn->v_type == VREG) {
1588 error = ctl_be_block_open_file(be_lun, req);
1589 } else {
1590 error = EINVAL;
1591 snprintf(req->error_str, sizeof(req->error_str),
1592 "%s is not a disk or file", be_lun->dev_path);
1593 }
1594 VOP_UNLOCK(be_lun->vn, 0);
1607 VFS_UNLOCK_GIANT(vfs_is_locked);
1608
1609 if (error != 0) {
1610 ctl_be_block_close(be_lun);
1611 return (error);
1612 }
1613
1614 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1615 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;

--- 469 unchanged lines hidden (view full) ---

2085 return (0);
2086}
2087
2088static int
2089ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2090{
2091 struct ctl_lun_modify_params *params;
2092 struct ctl_be_block_lun *be_lun;
1595
1596 if (error != 0) {
1597 ctl_be_block_close(be_lun);
1598 return (error);
1599 }
1600
1601 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1602 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;

--- 469 unchanged lines hidden (view full) ---

2072 return (0);
2073}
2074
2075static int
2076ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2077{
2078 struct ctl_lun_modify_params *params;
2079 struct ctl_be_block_lun *be_lun;
2093 int vfs_is_locked, error;
2080 int error;
2094
2095 params = &req->reqdata.modify;
2096
2097 mtx_lock(&softc->lock);
2098
2099 be_lun = NULL;
2100
2101 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {

--- 13 unchanged lines hidden (view full) ---

2115 if (params->lun_size_bytes < be_lun->blocksize) {
2116 snprintf(req->error_str, sizeof(req->error_str),
2117 "%s: LUN size %ju < blocksize %u", __func__,
2118 params->lun_size_bytes, be_lun->blocksize);
2119 goto bailout_error;
2120 }
2121 }
2122
2081
2082 params = &req->reqdata.modify;
2083
2084 mtx_lock(&softc->lock);
2085
2086 be_lun = NULL;
2087
2088 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {

--- 13 unchanged lines hidden (view full) ---

2102 if (params->lun_size_bytes < be_lun->blocksize) {
2103 snprintf(req->error_str, sizeof(req->error_str),
2104 "%s: LUN size %ju < blocksize %u", __func__,
2105 params->lun_size_bytes, be_lun->blocksize);
2106 goto bailout_error;
2107 }
2108 }
2109
2123 vfs_is_locked = VFS_LOCK_GIANT(be_lun->vn->v_mount);
2124 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2125
2126 if (be_lun->vn->v_type == VREG)
2127 error = ctl_be_block_modify_file(be_lun, req);
2128 else
2129 error = ctl_be_block_modify_dev(be_lun, req);
2130
2131 VOP_UNLOCK(be_lun->vn, 0);
2110 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2111
2112 if (be_lun->vn->v_type == VREG)
2113 error = ctl_be_block_modify_file(be_lun, req);
2114 else
2115 error = ctl_be_block_modify_dev(be_lun, req);
2116
2117 VOP_UNLOCK(be_lun->vn, 0);
2132 VFS_UNLOCK_GIANT(vfs_is_locked);
2133
2134 if (error != 0)
2135 goto bailout_error;
2136
2137 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
2138
2139 /*
2140 * The maximum LBA is the size - 1.

--- 224 unchanged lines hidden ---
2118
2119 if (error != 0)
2120 goto bailout_error;
2121
2122 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
2123
2124 /*
2125 * The maximum LBA is the size - 1.

--- 224 unchanged lines hidden ---