1 | /* $Id: VSCSILun.cpp 67959 2017-07-14 09:01:23Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual SCSI driver: LUN handling
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 | #define LOG_GROUP LOG_GROUP_VSCSI
|
---|
18 | #include <VBox/log.h>
|
---|
19 | #include <VBox/err.h>
|
---|
20 | #include <VBox/types.h>
|
---|
21 | #include <VBox/vscsi.h>
|
---|
22 | #include <iprt/assert.h>
|
---|
23 | #include <iprt/mem.h>
|
---|
24 |
|
---|
25 | #include "VSCSIInternal.h"
|
---|
26 |
|
---|
27 | /** SBC descriptor */
|
---|
28 | extern VSCSILUNDESC g_VScsiLunTypeSbc;
|
---|
29 | /** MMC descriptor */
|
---|
30 | extern VSCSILUNDESC g_VScsiLunTypeMmc;
|
---|
31 | /** SSC descriptor */
|
---|
32 | extern VSCSILUNDESC g_VScsiLunTypeSsc;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Array of supported SCSI LUN types.
|
---|
36 | */
|
---|
37 | static PVSCSILUNDESC g_aVScsiLunTypesSupported[] =
|
---|
38 | {
|
---|
39 | &g_VScsiLunTypeSbc,
|
---|
40 | &g_VScsiLunTypeMmc,
|
---|
41 | &g_VScsiLunTypeSsc,
|
---|
42 | };
|
---|
43 |
|
---|
44 | VBOXDDU_DECL(int) VSCSILunCreate(PVSCSILUN phVScsiLun, VSCSILUNTYPE enmLunType,
|
---|
45 | PVSCSILUNIOCALLBACKS pVScsiLunIoCallbacks,
|
---|
46 | void *pvVScsiLunUser)
|
---|
47 | {
|
---|
48 | PVSCSILUNINT pVScsiLun = NULL;
|
---|
49 | PVSCSILUNDESC pVScsiLunDesc = NULL;
|
---|
50 |
|
---|
51 | AssertPtrReturn(phVScsiLun, VERR_INVALID_POINTER);
|
---|
52 | AssertReturn( enmLunType > VSCSILUNTYPE_INVALID
|
---|
53 | && enmLunType < VSCSILUNTYPE_LAST, VERR_INVALID_PARAMETER);
|
---|
54 | AssertPtrReturn(pVScsiLunIoCallbacks, VERR_INVALID_PARAMETER);
|
---|
55 |
|
---|
56 | for (unsigned idxLunType = 0; idxLunType < RT_ELEMENTS(g_aVScsiLunTypesSupported); idxLunType++)
|
---|
57 | {
|
---|
58 | if (g_aVScsiLunTypesSupported[idxLunType]->enmLunType == enmLunType)
|
---|
59 | {
|
---|
60 | pVScsiLunDesc = g_aVScsiLunTypesSupported[idxLunType];
|
---|
61 | break;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (!pVScsiLunDesc)
|
---|
66 | return VERR_VSCSI_LUN_TYPE_NOT_SUPPORTED;
|
---|
67 |
|
---|
68 | pVScsiLun = (PVSCSILUNINT)RTMemAllocZ(pVScsiLunDesc->cbLun);
|
---|
69 | if (!pVScsiLun)
|
---|
70 | return VERR_NO_MEMORY;
|
---|
71 |
|
---|
72 | pVScsiLun->pVScsiDevice = NULL;
|
---|
73 | pVScsiLun->pvVScsiLunUser = pvVScsiLunUser;
|
---|
74 | pVScsiLun->pVScsiLunIoCallbacks = pVScsiLunIoCallbacks;
|
---|
75 | pVScsiLun->pVScsiLunDesc = pVScsiLunDesc;
|
---|
76 |
|
---|
77 | int rc = vscsiIoReqInit(pVScsiLun);
|
---|
78 | if (RT_SUCCESS(rc))
|
---|
79 | {
|
---|
80 | rc = vscsiLunGetFeatureFlags(pVScsiLun, &pVScsiLun->fFeatures);
|
---|
81 | if (RT_SUCCESS(rc))
|
---|
82 | {
|
---|
83 | rc = pVScsiLunDesc->pfnVScsiLunInit(pVScsiLun);
|
---|
84 | if (RT_SUCCESS(rc))
|
---|
85 | {
|
---|
86 | *phVScsiLun = pVScsiLun;
|
---|
87 | return VINF_SUCCESS;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | RTMemFree(pVScsiLun);
|
---|
93 |
|
---|
94 | return rc;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Destroy virtual SCSI LUN.
|
---|
99 | *
|
---|
100 | * @returns VBox status code.
|
---|
101 | * @param hVScsiLun The virtual SCSI LUN handle to destroy.
|
---|
102 | */
|
---|
103 | VBOXDDU_DECL(int) VSCSILunDestroy(VSCSILUN hVScsiLun)
|
---|
104 | {
|
---|
105 | PVSCSILUNINT pVScsiLun = (PVSCSILUNINT)hVScsiLun;
|
---|
106 |
|
---|
107 | AssertPtrReturn(pVScsiLun, VERR_INVALID_HANDLE);
|
---|
108 | AssertReturn(!pVScsiLun->pVScsiDevice, VERR_VSCSI_LUN_ATTACHED_TO_DEVICE);
|
---|
109 | AssertReturn(vscsiIoReqOutstandingCountGet(pVScsiLun) == 0, VERR_VSCSI_LUN_BUSY);
|
---|
110 |
|
---|
111 | int rc = pVScsiLun->pVScsiLunDesc->pfnVScsiLunDestroy(pVScsiLun);
|
---|
112 | if (RT_FAILURE(rc))
|
---|
113 | return rc;
|
---|
114 |
|
---|
115 | /* Make LUN invalid */
|
---|
116 | pVScsiLun->pvVScsiLunUser = NULL;
|
---|
117 | pVScsiLun->pVScsiLunIoCallbacks = NULL;
|
---|
118 | pVScsiLun->pVScsiLunDesc = NULL;
|
---|
119 |
|
---|
120 | RTMemFree(pVScsiLun);
|
---|
121 |
|
---|
122 | return VINF_SUCCESS;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Notify virtual SCSI LUN of media being mounted.
|
---|
127 | *
|
---|
128 | * @returns VBox status code.
|
---|
129 | * @param hVScsiLun The virtual SCSI LUN
|
---|
130 | * mounting the medium.
|
---|
131 | */
|
---|
132 | VBOXDDU_DECL(int) VSCSILunMountNotify(VSCSILUN hVScsiLun)
|
---|
133 | {
|
---|
134 | int rc = VINF_SUCCESS;
|
---|
135 | PVSCSILUNINT pVScsiLun = (PVSCSILUNINT)hVScsiLun;
|
---|
136 |
|
---|
137 | LogFlowFunc(("hVScsiLun=%p\n", hVScsiLun));
|
---|
138 | AssertPtrReturn(pVScsiLun, VERR_INVALID_HANDLE);
|
---|
139 | AssertReturn(vscsiIoReqOutstandingCountGet(pVScsiLun) == 0, VERR_VSCSI_LUN_BUSY);
|
---|
140 |
|
---|
141 | /* Mark the LUN as not ready so that LUN specific code can do its job. */
|
---|
142 | pVScsiLun->fReady = false;
|
---|
143 | pVScsiLun->fMediaPresent = true;
|
---|
144 | if (pVScsiLun->pVScsiLunDesc->pfnVScsiLunMediumInserted)
|
---|
145 | rc = pVScsiLun->pVScsiLunDesc->pfnVScsiLunMediumInserted(pVScsiLun);
|
---|
146 |
|
---|
147 | return rc;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Notify virtual SCSI LUN of media being unmounted.
|
---|
152 | *
|
---|
153 | * @returns VBox status code.
|
---|
154 | * @param hVScsiLun The virtual SCSI LUN
|
---|
155 | * mounting the medium.
|
---|
156 | */
|
---|
157 | VBOXDDU_DECL(int) VSCSILunUnmountNotify(VSCSILUN hVScsiLun)
|
---|
158 | {
|
---|
159 | int rc = VINF_SUCCESS;
|
---|
160 | PVSCSILUNINT pVScsiLun = (PVSCSILUNINT)hVScsiLun;
|
---|
161 |
|
---|
162 | LogFlowFunc(("hVScsiLun=%p\n", hVScsiLun));
|
---|
163 | AssertPtrReturn(pVScsiLun, VERR_INVALID_HANDLE);
|
---|
164 | AssertReturn(vscsiIoReqOutstandingCountGet(pVScsiLun) == 0, VERR_VSCSI_LUN_BUSY);
|
---|
165 |
|
---|
166 | pVScsiLun->fReady = false;
|
---|
167 | pVScsiLun->fMediaPresent = false;
|
---|
168 | if (pVScsiLun->pVScsiLunDesc->pfnVScsiLunMediumRemoved)
|
---|
169 | rc = pVScsiLun->pVScsiLunDesc->pfnVScsiLunMediumRemoved(pVScsiLun);
|
---|
170 |
|
---|
171 | return rc;
|
---|
172 | }
|
---|