1 | /* $Id: HardDisk2Impl.cpp 15594 2008-12-16 15:05:18Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VirtualBox COM class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "HardDisk2Impl.h"
|
---|
25 |
|
---|
26 | #include "ProgressImpl.h"
|
---|
27 | #include "SystemPropertiesImpl.h"
|
---|
28 |
|
---|
29 | #include "Logging.h"
|
---|
30 |
|
---|
31 | #include <VBox/com/array.h>
|
---|
32 | #include <VBox/com/SupportErrorInfo.h>
|
---|
33 |
|
---|
34 | #include <VBox/err.h>
|
---|
35 |
|
---|
36 | #include <iprt/param.h>
|
---|
37 | #include <iprt/path.h>
|
---|
38 | #include <iprt/file.h>
|
---|
39 | #include <iprt/tcp.h>
|
---|
40 |
|
---|
41 | #include <list>
|
---|
42 | #include <memory>
|
---|
43 |
|
---|
44 | ////////////////////////////////////////////////////////////////////////////////
|
---|
45 | // Globals
|
---|
46 | ////////////////////////////////////////////////////////////////////////////////
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Asynchronous task thread parameter bucket.
|
---|
50 | *
|
---|
51 | * Note that instances of this class must be created using new() because the
|
---|
52 | * task thread function will delete them when the task is complete!
|
---|
53 | *
|
---|
54 | * @note The constructor of this class adds a caller on the managed HardDisk2
|
---|
55 | * object which is automatically released upon destruction.
|
---|
56 | */
|
---|
57 | struct HardDisk2::Task : public com::SupportErrorInfoBase
|
---|
58 | {
|
---|
59 | enum Operation { CreateDynamic, CreateFixed, CreateDiff,
|
---|
60 | Merge, Clone, Delete };
|
---|
61 |
|
---|
62 | HardDisk2 *that;
|
---|
63 | VirtualBoxBaseProto::AutoCaller autoCaller;
|
---|
64 |
|
---|
65 | ComObjPtr <Progress> progress;
|
---|
66 | Operation operation;
|
---|
67 |
|
---|
68 | /** Where to save the result when executed using #runNow(). */
|
---|
69 | HRESULT rc;
|
---|
70 |
|
---|
71 | Task (HardDisk2 *aThat, Progress *aProgress, Operation aOperation)
|
---|
72 | : that (aThat), autoCaller (aThat)
|
---|
73 | , progress (aProgress)
|
---|
74 | , operation (aOperation)
|
---|
75 | , rc (S_OK) {}
|
---|
76 |
|
---|
77 | ~Task();
|
---|
78 |
|
---|
79 | void setData (HardDisk2 *aTarget)
|
---|
80 | {
|
---|
81 | d.target = aTarget;
|
---|
82 | HRESULT rc = d.target->addCaller();
|
---|
83 | AssertComRC (rc);
|
---|
84 | }
|
---|
85 |
|
---|
86 | void setData (MergeChain *aChain)
|
---|
87 | {
|
---|
88 | AssertReturnVoid (aChain != NULL);
|
---|
89 | d.chain.reset (aChain);
|
---|
90 | }
|
---|
91 |
|
---|
92 | HRESULT startThread();
|
---|
93 | HRESULT runNow();
|
---|
94 |
|
---|
95 | struct Data
|
---|
96 | {
|
---|
97 | Data() : size (0) {}
|
---|
98 |
|
---|
99 | /* CreateDynamic, CreateStatic */
|
---|
100 |
|
---|
101 | uint64_t size;
|
---|
102 |
|
---|
103 | /* CreateDiff */
|
---|
104 |
|
---|
105 | ComObjPtr <HardDisk2> target;
|
---|
106 |
|
---|
107 | /* Merge */
|
---|
108 |
|
---|
109 | /** Hard disks to merge, in {parent,child} order */
|
---|
110 | std::auto_ptr <MergeChain> chain;
|
---|
111 | }
|
---|
112 | d;
|
---|
113 |
|
---|
114 | protected:
|
---|
115 |
|
---|
116 | // SupportErrorInfoBase interface
|
---|
117 | const GUID &mainInterfaceID() const { return COM_IIDOF (IHardDisk2); }
|
---|
118 | const char *componentName() const { return HardDisk2::ComponentName(); }
|
---|
119 | };
|
---|
120 |
|
---|
121 | HardDisk2::Task::~Task()
|
---|
122 | {
|
---|
123 | /* remove callers added by setData() */
|
---|
124 | if (!d.target.isNull())
|
---|
125 | d.target->releaseCaller();
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Starts a new thread driven by the HardDisk2::taskThread() function and passes
|
---|
130 | * this Task instance as an argument.
|
---|
131 | *
|
---|
132 | * Note that if this method returns success, this Task object becomes an ownee
|
---|
133 | * of the started thread and will be automatically deleted when the thread
|
---|
134 | * terminates.
|
---|
135 | *
|
---|
136 | * @note When the task is executed by this method, IProgress::notifyComplete()
|
---|
137 | * is automatically called for the progress object associated with this
|
---|
138 | * task when the task is finished to signal the operation completion for
|
---|
139 | * other threads asynchronously waiting for it.
|
---|
140 | */
|
---|
141 | HRESULT HardDisk2::Task::startThread()
|
---|
142 | {
|
---|
143 | int vrc = RTThreadCreate (NULL, HardDisk2::taskThread, this,
|
---|
144 | 0, RTTHREADTYPE_MAIN_HEAVY_WORKER, 0,
|
---|
145 | "HardDisk::Task");
|
---|
146 | ComAssertMsgRCRet (vrc,
|
---|
147 | ("Could not create HardDisk::Task thread (%Rrc)\n", vrc), E_FAIL);
|
---|
148 |
|
---|
149 | return S_OK;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Runs HardDisk2::taskThread() by passing it this Task instance as an argument
|
---|
154 | * on the current thread instead of creating a new one.
|
---|
155 | *
|
---|
156 | * This call implies that it is made on another temporary thread created for
|
---|
157 | * some asynchronous task. Avoid calling it from a normal thread since the task
|
---|
158 | * operatinos are potentially lengthy and will block the calling thread in this
|
---|
159 | * case.
|
---|
160 | *
|
---|
161 | * Note that this Task object will be deleted by taskThread() when this method
|
---|
162 | * returns!
|
---|
163 | *
|
---|
164 | * @note When the task is executed by this method, IProgress::notifyComplete()
|
---|
165 | * is not called for the progress object associated with this task when
|
---|
166 | * the task is finished. Instead, the result of the operation is returned
|
---|
167 | * by this method directly and it's the caller's responsibility to
|
---|
168 | * complete the progress object in this case.
|
---|
169 | */
|
---|
170 | HRESULT HardDisk2::Task::runNow()
|
---|
171 | {
|
---|
172 | HardDisk2::taskThread (NIL_RTTHREAD, this);
|
---|
173 |
|
---|
174 | return rc;
|
---|
175 | }
|
---|
176 |
|
---|
177 | ////////////////////////////////////////////////////////////////////////////////
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Helper class for merge operations.
|
---|
181 | *
|
---|
182 | * @note It is assumed that when modifying methods of this class are called,
|
---|
183 | * HardDisk2::treeLock() is held in read mode.
|
---|
184 | */
|
---|
185 | class HardDisk2::MergeChain : public HardDisk2::List,
|
---|
186 | public com::SupportErrorInfoBase
|
---|
187 | {
|
---|
188 | public:
|
---|
189 |
|
---|
190 | MergeChain (bool aForward, bool aIgnoreAttachments)
|
---|
191 | : mForward (aForward)
|
---|
192 | , mIgnoreAttachments (aIgnoreAttachments) {}
|
---|
193 |
|
---|
194 | ~MergeChain()
|
---|
195 | {
|
---|
196 | for (iterator it = mChildren.begin(); it != mChildren.end(); ++ it)
|
---|
197 | {
|
---|
198 | HRESULT rc = (*it)->UnlockWrite (NULL);
|
---|
199 | AssertComRC (rc);
|
---|
200 |
|
---|
201 | (*it)->releaseCaller();
|
---|
202 | }
|
---|
203 |
|
---|
204 | for (iterator it = begin(); it != end(); ++ it)
|
---|
205 | {
|
---|
206 | AutoWriteLock alock (*it);
|
---|
207 | Assert ((*it)->m.state == MediaState_LockedWrite ||
|
---|
208 | (*it)->m.state == MediaState_Deleting);
|
---|
209 | if ((*it)->m.state == MediaState_LockedWrite)
|
---|
210 | (*it)->UnlockWrite (NULL);
|
---|
211 | else
|
---|
212 | (*it)->m.state = MediaState_Created;
|
---|
213 |
|
---|
214 | (*it)->releaseCaller();
|
---|
215 | }
|
---|
216 |
|
---|
217 | if (!mParent.isNull())
|
---|
218 | mParent->releaseCaller();
|
---|
219 | }
|
---|
220 |
|
---|
221 | HRESULT addSource (HardDisk2 *aHardDisk)
|
---|
222 | {
|
---|
223 | HRESULT rc = aHardDisk->addCaller();
|
---|
224 | CheckComRCReturnRC (rc);
|
---|
225 |
|
---|
226 | AutoWriteLock alock (aHardDisk);
|
---|
227 |
|
---|
228 | if (mForward)
|
---|
229 | {
|
---|
230 | rc = checkChildrenAndAttachmentsAndImmutable (aHardDisk);
|
---|
231 | if (FAILED (rc))
|
---|
232 | {
|
---|
233 | aHardDisk->releaseCaller();
|
---|
234 | return rc;
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /* go to Deleting */
|
---|
239 | switch (aHardDisk->m.state)
|
---|
240 | {
|
---|
241 | case MediaState_Created:
|
---|
242 | aHardDisk->m.state = MediaState_Deleting;
|
---|
243 | break;
|
---|
244 | default:
|
---|
245 | aHardDisk->releaseCaller();
|
---|
246 | return aHardDisk->setStateError();
|
---|
247 | }
|
---|
248 |
|
---|
249 | push_front (aHardDisk);
|
---|
250 |
|
---|
251 | if (mForward)
|
---|
252 | {
|
---|
253 | /* we will need parent to reparent target */
|
---|
254 | if (!aHardDisk->mParent.isNull())
|
---|
255 | {
|
---|
256 | rc = aHardDisk->mParent->addCaller();
|
---|
257 | CheckComRCReturnRC (rc);
|
---|
258 |
|
---|
259 | mParent = aHardDisk->mParent;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | else
|
---|
263 | {
|
---|
264 | /* we will need to reparent children */
|
---|
265 | for (List::const_iterator it = aHardDisk->children().begin();
|
---|
266 | it != aHardDisk->children().end(); ++ it)
|
---|
267 | {
|
---|
268 | rc = (*it)->addCaller();
|
---|
269 | CheckComRCReturnRC (rc);
|
---|
270 |
|
---|
271 | rc = (*it)->LockWrite (NULL);
|
---|
272 | if (FAILED (rc))
|
---|
273 | {
|
---|
274 | (*it)->releaseCaller();
|
---|
275 | return rc;
|
---|
276 | }
|
---|
277 |
|
---|
278 | mChildren.push_back (*it);
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | return S_OK;
|
---|
283 | }
|
---|
284 |
|
---|
285 | HRESULT addTarget (HardDisk2 *aHardDisk)
|
---|
286 | {
|
---|
287 | HRESULT rc = aHardDisk->addCaller();
|
---|
288 | CheckComRCReturnRC (rc);
|
---|
289 |
|
---|
290 | AutoWriteLock alock (aHardDisk);
|
---|
291 |
|
---|
292 | if (!mForward)
|
---|
293 | {
|
---|
294 | rc = checkChildrenAndImmutable (aHardDisk);
|
---|
295 | if (FAILED (rc))
|
---|
296 | {
|
---|
297 | aHardDisk->releaseCaller();
|
---|
298 | return rc;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | /* go to LockedWrite */
|
---|
303 | rc = aHardDisk->LockWrite (NULL);
|
---|
304 | if (FAILED (rc))
|
---|
305 | {
|
---|
306 | aHardDisk->releaseCaller();
|
---|
307 | return rc;
|
---|
308 | }
|
---|
309 |
|
---|
310 | push_front (aHardDisk);
|
---|
311 |
|
---|
312 | return S_OK;
|
---|
313 | }
|
---|
314 |
|
---|
315 | HRESULT addIntermediate (HardDisk2 *aHardDisk)
|
---|
316 | {
|
---|
317 | HRESULT rc = aHardDisk->addCaller();
|
---|
318 | CheckComRCReturnRC (rc);
|
---|
319 |
|
---|
320 | AutoWriteLock alock (aHardDisk);
|
---|
321 |
|
---|
322 | rc = checkChildrenAndAttachments (aHardDisk);
|
---|
323 | if (FAILED (rc))
|
---|
324 | {
|
---|
325 | aHardDisk->releaseCaller();
|
---|
326 | return rc;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /* go to Deleting */
|
---|
330 | switch (aHardDisk->m.state)
|
---|
331 | {
|
---|
332 | case MediaState_Created:
|
---|
333 | aHardDisk->m.state = MediaState_Deleting;
|
---|
334 | break;
|
---|
335 | default:
|
---|
336 | aHardDisk->releaseCaller();
|
---|
337 | return aHardDisk->setStateError();
|
---|
338 | }
|
---|
339 |
|
---|
340 | push_front (aHardDisk);
|
---|
341 |
|
---|
342 | return S_OK;
|
---|
343 | }
|
---|
344 |
|
---|
345 | bool isForward() const { return mForward; }
|
---|
346 | HardDisk2 *parent() const { return mParent; }
|
---|
347 | const List &children() const { return mChildren; }
|
---|
348 |
|
---|
349 | HardDisk2 *source() const
|
---|
350 | { AssertReturn (size() > 0, NULL); return mForward ? front() : back(); }
|
---|
351 |
|
---|
352 | HardDisk2 *target() const
|
---|
353 | { AssertReturn (size() > 0, NULL); return mForward ? back() : front(); }
|
---|
354 |
|
---|
355 | protected:
|
---|
356 |
|
---|
357 | // SupportErrorInfoBase interface
|
---|
358 | const GUID &mainInterfaceID() const { return COM_IIDOF (IHardDisk2); }
|
---|
359 | const char *componentName() const { return HardDisk2::ComponentName(); }
|
---|
360 |
|
---|
361 | private:
|
---|
362 |
|
---|
363 | HRESULT check (HardDisk2 *aHardDisk, bool aChildren, bool aAttachments,
|
---|
364 | bool aImmutable)
|
---|
365 | {
|
---|
366 | if (aChildren)
|
---|
367 | {
|
---|
368 | /* not going to multi-merge as it's too expensive */
|
---|
369 | if (aHardDisk->children().size() > 1)
|
---|
370 | {
|
---|
371 | return setError (E_FAIL,
|
---|
372 | tr ("Hard disk '%ls' involved in the merge operation "
|
---|
373 | "has more than one child hard disk (%d)"),
|
---|
374 | aHardDisk->m.locationFull.raw(),
|
---|
375 | aHardDisk->children().size());
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | if (aAttachments && !mIgnoreAttachments)
|
---|
380 | {
|
---|
381 | if (aHardDisk->m.backRefs.size() != 0)
|
---|
382 | return setError (E_FAIL,
|
---|
383 | tr ("Hard disk '%ls' is attached to %d virtual machines"),
|
---|
384 | aHardDisk->m.locationFull.raw(),
|
---|
385 | aHardDisk->m.backRefs.size());
|
---|
386 | }
|
---|
387 |
|
---|
388 | if (aImmutable)
|
---|
389 | {
|
---|
390 | if (aHardDisk->mm.type == HardDiskType_Immutable)
|
---|
391 | return setError (E_FAIL,
|
---|
392 | tr ("Hard disk '%ls' is immutable"),
|
---|
393 | aHardDisk->m.locationFull.raw());
|
---|
394 | }
|
---|
395 |
|
---|
396 | return S_OK;
|
---|
397 | }
|
---|
398 |
|
---|
399 | HRESULT checkChildren (HardDisk2 *aHardDisk)
|
---|
400 | { return check (aHardDisk, true, false, false); }
|
---|
401 |
|
---|
402 | HRESULT checkChildrenAndImmutable (HardDisk2 *aHardDisk)
|
---|
403 | { return check (aHardDisk, true, false, true); }
|
---|
404 |
|
---|
405 | HRESULT checkChildrenAndAttachments (HardDisk2 *aHardDisk)
|
---|
406 | { return check (aHardDisk, true, true, false); }
|
---|
407 |
|
---|
408 | HRESULT checkChildrenAndAttachmentsAndImmutable (HardDisk2 *aHardDisk)
|
---|
409 | { return check (aHardDisk, true, true, true); }
|
---|
410 |
|
---|
411 | /** true if forward merge, false if backward */
|
---|
412 | bool mForward : 1;
|
---|
413 | /** true to not perform attachment checks */
|
---|
414 | bool mIgnoreAttachments : 1;
|
---|
415 |
|
---|
416 | /** Parent of the source when forward merge (if any) */
|
---|
417 | ComObjPtr <HardDisk2> mParent;
|
---|
418 | /** Children of the source when backward merge (if any) */
|
---|
419 | List mChildren;
|
---|
420 | };
|
---|
421 |
|
---|
422 | ////////////////////////////////////////////////////////////////////////////////
|
---|
423 | // HardDisk2 class
|
---|
424 | ////////////////////////////////////////////////////////////////////////////////
|
---|
425 |
|
---|
426 | // constructor / destructor
|
---|
427 | ////////////////////////////////////////////////////////////////////////////////
|
---|
428 |
|
---|
429 | DEFINE_EMPTY_CTOR_DTOR (HardDisk2)
|
---|
430 |
|
---|
431 | HRESULT HardDisk2::FinalConstruct()
|
---|
432 | {
|
---|
433 | /* Initialize the callbacks of the VD error interface */
|
---|
434 | mm.vdIfCallsError.cbSize = sizeof (VDINTERFACEERROR);
|
---|
435 | mm.vdIfCallsError.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
436 | mm.vdIfCallsError.pfnError = vdErrorCall;
|
---|
437 |
|
---|
438 | /* Initialize the callbacks of the VD progress interface */
|
---|
439 | mm.vdIfCallsProgress.cbSize = sizeof (VDINTERFACEPROGRESS);
|
---|
440 | mm.vdIfCallsProgress.enmInterface = VDINTERFACETYPE_PROGRESS;
|
---|
441 | mm.vdIfCallsProgress.pfnProgress = vdProgressCall;
|
---|
442 |
|
---|
443 | /* Initialize the callbacks of the VD config interface */
|
---|
444 | mm.vdIfCallsConfig.cbSize = sizeof (VDINTERFACECONFIG);
|
---|
445 | mm.vdIfCallsConfig.enmInterface = VDINTERFACETYPE_CONFIG;
|
---|
446 | mm.vdIfCallsConfig.pfnAreKeysValid = vdConfigAreKeysValid;
|
---|
447 | mm.vdIfCallsConfig.pfnQuerySize = vdConfigQuerySize;
|
---|
448 | mm.vdIfCallsConfig.pfnQuery = vdConfigQuery;
|
---|
449 |
|
---|
450 | /* Initialize the callbacks of the VD TCP interface (we always use the host
|
---|
451 | * IP stack for now) */
|
---|
452 | mm.vdIfCallsTcpNet.cbSize = sizeof (VDINTERFACETCPNET);
|
---|
453 | mm.vdIfCallsTcpNet.enmInterface = VDINTERFACETYPE_TCPNET;
|
---|
454 | mm.vdIfCallsTcpNet.pfnClientConnect = RTTcpClientConnect;
|
---|
455 | mm.vdIfCallsTcpNet.pfnClientClose = RTTcpClientClose;
|
---|
456 | mm.vdIfCallsTcpNet.pfnSelectOne = RTTcpSelectOne;
|
---|
457 | mm.vdIfCallsTcpNet.pfnRead = RTTcpRead;
|
---|
458 | mm.vdIfCallsTcpNet.pfnWrite = RTTcpWrite;
|
---|
459 | mm.vdIfCallsTcpNet.pfnFlush = RTTcpFlush;
|
---|
460 |
|
---|
461 | /* Initialize the per-disk interface chain */
|
---|
462 | int vrc;
|
---|
463 | vrc = VDInterfaceAdd (&mm.vdIfError,
|
---|
464 | "HardDisk2::vdInterfaceError",
|
---|
465 | VDINTERFACETYPE_ERROR,
|
---|
466 | &mm.vdIfCallsError, this, &mm.vdDiskIfaces);
|
---|
467 | AssertRCReturn (vrc, E_FAIL);
|
---|
468 |
|
---|
469 | vrc = VDInterfaceAdd (&mm.vdIfProgress,
|
---|
470 | "HardDisk2::vdInterfaceProgress",
|
---|
471 | VDINTERFACETYPE_PROGRESS,
|
---|
472 | &mm.vdIfCallsProgress, this, &mm.vdDiskIfaces);
|
---|
473 | AssertRCReturn (vrc, E_FAIL);
|
---|
474 |
|
---|
475 | vrc = VDInterfaceAdd (&mm.vdIfConfig,
|
---|
476 | "HardDisk2::vdInterfaceConfig",
|
---|
477 | VDINTERFACETYPE_CONFIG,
|
---|
478 | &mm.vdIfCallsConfig, this, &mm.vdDiskIfaces);
|
---|
479 | AssertRCReturn (vrc, E_FAIL);
|
---|
480 |
|
---|
481 | vrc = VDInterfaceAdd (&mm.vdIfTcpNet,
|
---|
482 | "HardDisk2::vdInterfaceTcpNet",
|
---|
483 | VDINTERFACETYPE_TCPNET,
|
---|
484 | &mm.vdIfCallsTcpNet, this, &mm.vdDiskIfaces);
|
---|
485 | AssertRCReturn (vrc, E_FAIL);
|
---|
486 |
|
---|
487 | return S_OK;
|
---|
488 | }
|
---|
489 |
|
---|
490 | void HardDisk2::FinalRelease()
|
---|
491 | {
|
---|
492 | uninit();
|
---|
493 | }
|
---|
494 |
|
---|
495 | // public initializer/uninitializer for internal purposes only
|
---|
496 | ////////////////////////////////////////////////////////////////////////////////
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Initializes the hard disk object without creating or opening an associated
|
---|
500 | * storage unit.
|
---|
501 | *
|
---|
502 | * For hard disks that don't have the VD_CAP_CREATE_FIXED or
|
---|
503 | * VD_CAP_CREATE_DYNAMIC capability (and therefore cannot be created or deleted
|
---|
504 | * with the means of VirtualBox) the associated storage unit is assumed to be
|
---|
505 | * ready for use so the state of the hard disk object will be set to Created.
|
---|
506 | *
|
---|
507 | * @param aVirtualBox VirtualBox object.
|
---|
508 | * @param aLocaiton Storage unit location.
|
---|
509 | */
|
---|
510 | HRESULT HardDisk2::init (VirtualBox *aVirtualBox, CBSTR aFormat,
|
---|
511 | CBSTR aLocation)
|
---|
512 | {
|
---|
513 | AssertReturn (aVirtualBox != NULL, E_FAIL);
|
---|
514 | AssertReturn (aFormat != NULL && *aFormat != '\0', E_FAIL);
|
---|
515 |
|
---|
516 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
517 | AutoInitSpan autoInitSpan (this);
|
---|
518 | AssertReturn (autoInitSpan.isOk(), E_FAIL);
|
---|
519 |
|
---|
520 | HRESULT rc = S_OK;
|
---|
521 |
|
---|
522 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
523 | unconst (mVirtualBox) = aVirtualBox;
|
---|
524 |
|
---|
525 | /* register with VirtualBox early, since uninit() will
|
---|
526 | * unconditionally unregister on failure */
|
---|
527 | aVirtualBox->addDependentChild (this);
|
---|
528 |
|
---|
529 | /* no storage yet */
|
---|
530 | m.state = MediaState_NotCreated;
|
---|
531 |
|
---|
532 | /* No storage unit is created yet, no need to queryInfo() */
|
---|
533 |
|
---|
534 | rc = setFormat (aFormat);
|
---|
535 | CheckComRCReturnRC (rc);
|
---|
536 |
|
---|
537 | if (mm.formatObj->capabilities() & HardDiskFormatCapabilities_File)
|
---|
538 | {
|
---|
539 | rc = setLocation (aLocation);
|
---|
540 | CheckComRCReturnRC (rc);
|
---|
541 | }
|
---|
542 | else
|
---|
543 | {
|
---|
544 | rc = setLocation (aLocation);
|
---|
545 | CheckComRCReturnRC (rc);
|
---|
546 |
|
---|
547 | /// @todo later we may want to use a pfnComposeLocation backend info
|
---|
548 | /// callback to generate a well-formed location value (based on the hard
|
---|
549 | /// disk properties we have) rather than allowing each caller to invent
|
---|
550 | /// its own (pseudo-)location.
|
---|
551 | }
|
---|
552 |
|
---|
553 | if (!(mm.formatObj->capabilities() &
|
---|
554 | (HardDiskFormatCapabilities_CreateFixed |
|
---|
555 | HardDiskFormatCapabilities_CreateDynamic)))
|
---|
556 | {
|
---|
557 | /* storage for hard disks of this format can neither be explicitly
|
---|
558 | * created by VirtualBox nor deleted, so we place the hard disk to
|
---|
559 | * Created state here and also add it to the registry */
|
---|
560 | m.state = MediaState_Created;
|
---|
561 | unconst (m.id).create();
|
---|
562 | rc = mVirtualBox->registerHardDisk2 (this);
|
---|
563 |
|
---|
564 | /// @todo later we may want to use a pfnIsConfigSufficient backend info
|
---|
565 | /// callback that would tell us when we have enough properties to work
|
---|
566 | /// with the hard disk and this information could be used to actually
|
---|
567 | /// move such hard disks from NotCreated to Created state. Instead of
|
---|
568 | /// pfnIsConfigSufficient we can use HardDiskFormat property
|
---|
569 | /// descriptions to see which properties are mandatory
|
---|
570 | }
|
---|
571 |
|
---|
572 | /* Confirm a successful initialization when it's the case */
|
---|
573 | if (SUCCEEDED (rc))
|
---|
574 | autoInitSpan.setSucceeded();
|
---|
575 |
|
---|
576 | return rc;
|
---|
577 | }
|
---|
578 |
|
---|
579 | /**
|
---|
580 | * Initializes the hard disk object by opening the storage unit at the specified
|
---|
581 | * location.
|
---|
582 | *
|
---|
583 | * Note that the UUID, format and the parent of this hard disk will be
|
---|
584 | * determined when reading the hard disk storage unit. If the detected parent is
|
---|
585 | * not known to VirtualBox, then this method will fail.
|
---|
586 | *
|
---|
587 | * @param aVirtualBox VirtualBox object.
|
---|
588 | * @param aLocaiton Storage unit location.
|
---|
589 | */
|
---|
590 | HRESULT HardDisk2::init (VirtualBox *aVirtualBox, CBSTR aLocation)
|
---|
591 | {
|
---|
592 | AssertReturn (aVirtualBox, E_INVALIDARG);
|
---|
593 | AssertReturn (aLocation, E_INVALIDARG);
|
---|
594 |
|
---|
595 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
596 | AutoInitSpan autoInitSpan (this);
|
---|
597 | AssertReturn (autoInitSpan.isOk(), E_FAIL);
|
---|
598 |
|
---|
599 | HRESULT rc = S_OK;
|
---|
600 |
|
---|
601 | /* share VirtualBox weakly (parent remains NULL so far) */
|
---|
602 | unconst (mVirtualBox) = aVirtualBox;
|
---|
603 |
|
---|
604 | /* register with VirtualBox early, since uninit() will
|
---|
605 | * unconditionally unregister on failure */
|
---|
606 | aVirtualBox->addDependentChild (this);
|
---|
607 |
|
---|
608 | /* there must be a storage unit */
|
---|
609 | m.state = MediaState_Created;
|
---|
610 |
|
---|
611 | rc = setLocation (aLocation);
|
---|
612 | CheckComRCReturnRC (rc);
|
---|
613 |
|
---|
614 | /* get all the information about the medium from the storage unit */
|
---|
615 | rc = queryInfo();
|
---|
616 | if (SUCCEEDED (rc))
|
---|
617 | {
|
---|
618 | /* if the storage unit is not accessible, it's not acceptable for the
|
---|
619 | * newly opened media so convert this into an error */
|
---|
620 | if (m.state == MediaState_Inaccessible)
|
---|
621 | {
|
---|
622 | Assert (!m.lastAccessError.isEmpty());
|
---|
623 | rc = setError (E_FAIL, Utf8Str (m.lastAccessError));
|
---|
624 | }
|
---|
625 |
|
---|
626 | /* storage format must be detected by queryInfo() if the medium is
|
---|
627 | * accessible */
|
---|
628 | AssertReturn (!m.id.isEmpty() && !mm.format.isNull(), E_FAIL);
|
---|
629 | }
|
---|
630 |
|
---|
631 | /* Confirm a successful initialization when it's the case */
|
---|
632 | if (SUCCEEDED (rc))
|
---|
633 | autoInitSpan.setSucceeded();
|
---|
634 |
|
---|
635 | return rc;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /**
|
---|
639 | * Initializes the hard disk object by loading its data from the given settings
|
---|
640 | * node.
|
---|
641 | *
|
---|
642 | * @param aVirtualBox VirtualBox object.
|
---|
643 | * @param aParent Parent hard disk or NULL for a root hard disk.
|
---|
644 | * @param aNode <HardDisk> settings node.
|
---|
645 | *
|
---|
646 | * @note Locks VirtualBox lock for writing, treeLock() for writing.
|
---|
647 | */
|
---|
648 | HRESULT HardDisk2::init (VirtualBox *aVirtualBox, HardDisk2 *aParent,
|
---|
649 | const settings::Key &aNode)
|
---|
650 | {
|
---|
651 | using namespace settings;
|
---|
652 |
|
---|
653 | AssertReturn (aVirtualBox, E_INVALIDARG);
|
---|
654 |
|
---|
655 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
656 | AutoInitSpan autoInitSpan (this);
|
---|
657 | AssertReturn (autoInitSpan.isOk(), E_FAIL);
|
---|
658 |
|
---|
659 | HRESULT rc = S_OK;
|
---|
660 |
|
---|
661 | /* share VirtualBox and parent weakly */
|
---|
662 | unconst (mVirtualBox) = aVirtualBox;
|
---|
663 |
|
---|
664 | /* register with VirtualBox/parent early, since uninit() will
|
---|
665 | * unconditionally unregister on failure */
|
---|
666 | if (aParent == NULL)
|
---|
667 | aVirtualBox->addDependentChild (this);
|
---|
668 | else
|
---|
669 | {
|
---|
670 | /* we set mParent */
|
---|
671 | AutoWriteLock treeLock (this->treeLock());
|
---|
672 |
|
---|
673 | mParent = aParent;
|
---|
674 | aParent->addDependentChild (this);
|
---|
675 | }
|
---|
676 |
|
---|
677 | /* see below why we don't call queryInfo() (and therefore treat the medium
|
---|
678 | * as inaccessible for now */
|
---|
679 | m.state = MediaState_Inaccessible;
|
---|
680 | m.lastAccessError = tr ("Accessibility check was not yet performed");
|
---|
681 |
|
---|
682 | /* required */
|
---|
683 | unconst (m.id) = aNode.value <Guid> ("uuid");
|
---|
684 |
|
---|
685 | /* optional */
|
---|
686 | {
|
---|
687 | settings::Key descNode = aNode.findKey ("Description");
|
---|
688 | if (!descNode.isNull())
|
---|
689 | m.description = descNode.keyStringValue();
|
---|
690 | }
|
---|
691 |
|
---|
692 | /* required */
|
---|
693 | Bstr format = aNode.stringValue ("format");
|
---|
694 | AssertReturn (!format.isNull(), E_FAIL);
|
---|
695 | rc = setFormat (format);
|
---|
696 | CheckComRCReturnRC (rc);
|
---|
697 |
|
---|
698 | /* properties (after setting the format as it populates the map). Note that
|
---|
699 | * if some properties are not supported but preseint in the settings file,
|
---|
700 | * they will still be read and accessible (for possible backward
|
---|
701 | * compatibility; we can also clean them up from the XML upon next
|
---|
702 | * XML format versino change if we wish) */
|
---|
703 | Key::List properties = aNode.keys ("Property");
|
---|
704 | for (Key::List::const_iterator it = properties.begin();
|
---|
705 | it != properties.end(); ++ it)
|
---|
706 | {
|
---|
707 | mm.properties [Bstr (it->stringValue ("name"))] =
|
---|
708 | Bstr (it->stringValue ("value"));
|
---|
709 | }
|
---|
710 |
|
---|
711 | /* required */
|
---|
712 | Bstr location = aNode.stringValue ("location");
|
---|
713 | rc = setLocation (location);
|
---|
714 | CheckComRCReturnRC (rc);
|
---|
715 |
|
---|
716 | /* type is only for base hard disks */
|
---|
717 | if (mParent.isNull())
|
---|
718 | {
|
---|
719 | const char *type = aNode.stringValue ("type");
|
---|
720 | if (strcmp (type, "Normal") == 0)
|
---|
721 | mm.type = HardDiskType_Normal;
|
---|
722 | else if (strcmp (type, "Immutable") == 0)
|
---|
723 | mm.type = HardDiskType_Immutable;
|
---|
724 | else if (strcmp (type, "Writethrough") == 0)
|
---|
725 | mm.type = HardDiskType_Writethrough;
|
---|
726 | else
|
---|
727 | AssertFailed();
|
---|
728 | }
|
---|
729 |
|
---|
730 | LogFlowThisFunc (("m.locationFull='%ls', mm.format=%ls, m.id={%RTuuid}\n",
|
---|
731 | m.locationFull.raw(), mm.format.raw(), m.id.raw()));
|
---|
732 |
|
---|
733 | /* Don't call queryInfo() for registered media to prevent the calling
|
---|
734 | * thread (i.e. the VirtualBox server startup thread) from an unexpected
|
---|
735 | * freeze but mark it as initially inaccessible instead. The vital UUID,
|
---|
736 | * location and format properties are read from the registry file above; to
|
---|
737 | * get the actual state and the rest of the data, the user will have to call
|
---|
738 | * COMGETTER(State). */
|
---|
739 |
|
---|
740 | /* load all children */
|
---|
741 | Key::List hardDisks = aNode.keys ("HardDisk");
|
---|
742 | for (Key::List::const_iterator it = hardDisks.begin();
|
---|
743 | it != hardDisks.end(); ++ it)
|
---|
744 | {
|
---|
745 | ComObjPtr <HardDisk2> hardDisk;
|
---|
746 | hardDisk.createObject();
|
---|
747 | rc = hardDisk->init (aVirtualBox, this, *it);
|
---|
748 | CheckComRCBreakRC (rc);
|
---|
749 |
|
---|
750 | rc = mVirtualBox->registerHardDisk2 (hardDisk, false /* aSaveRegistry */);
|
---|
751 | CheckComRCBreakRC (rc);
|
---|
752 | }
|
---|
753 |
|
---|
754 | /* Confirm a successful initialization when it's the case */
|
---|
755 | if (SUCCEEDED (rc))
|
---|
756 | autoInitSpan.setSucceeded();
|
---|
757 |
|
---|
758 | return rc;
|
---|
759 | }
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * Uninitializes the instance.
|
---|
763 | *
|
---|
764 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
765 | *
|
---|
766 | * @note All children of this hard disk get uninitialized by calling their
|
---|
767 | * uninit() methods.
|
---|
768 | *
|
---|
769 | * @note Locks treeLock() for writing, VirtualBox for writing.
|
---|
770 | */
|
---|
771 | void HardDisk2::uninit()
|
---|
772 | {
|
---|
773 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
774 | AutoUninitSpan autoUninitSpan (this);
|
---|
775 | if (autoUninitSpan.uninitDone())
|
---|
776 | return;
|
---|
777 |
|
---|
778 | if (!mm.formatObj.isNull())
|
---|
779 | {
|
---|
780 | /* remove the caller reference we added in setFormat() */
|
---|
781 | mm.formatObj->releaseCaller();
|
---|
782 | mm.formatObj.setNull();
|
---|
783 | }
|
---|
784 |
|
---|
785 | if (m.state == MediaState_Deleting)
|
---|
786 | {
|
---|
787 | /* we are being uninitialized after've been deleted by merge.
|
---|
788 | * Reparenting has already been done so don't touch it here (we are
|
---|
789 | * now orphans and remoeDependentChild() will assert) */
|
---|
790 |
|
---|
791 | Assert (mParent.isNull());
|
---|
792 | }
|
---|
793 | else
|
---|
794 | {
|
---|
795 | /* we uninit children and reset mParent
|
---|
796 | * and VirtualBox::removeDependentChild() needs a write lock */
|
---|
797 | AutoMultiWriteLock2 alock (mVirtualBox->lockHandle(), this->treeLock());
|
---|
798 |
|
---|
799 | uninitDependentChildren();
|
---|
800 |
|
---|
801 | if (!mParent.isNull())
|
---|
802 | {
|
---|
803 | mParent->removeDependentChild (this);
|
---|
804 | mParent.setNull();
|
---|
805 | }
|
---|
806 | else
|
---|
807 | mVirtualBox->removeDependentChild (this);
|
---|
808 | }
|
---|
809 |
|
---|
810 | unconst (mVirtualBox).setNull();
|
---|
811 | }
|
---|
812 |
|
---|
813 | // IHardDisk2 properties
|
---|
814 | ////////////////////////////////////////////////////////////////////////////////
|
---|
815 |
|
---|
816 | STDMETHODIMP HardDisk2::COMGETTER(Format) (BSTR *aFormat)
|
---|
817 | {
|
---|
818 | if (aFormat == NULL)
|
---|
819 | return E_POINTER;
|
---|
820 |
|
---|
821 | AutoCaller autoCaller (this);
|
---|
822 | CheckComRCReturnRC (autoCaller.rc());
|
---|
823 |
|
---|
824 | /* no need to lock, mm.format is const */
|
---|
825 | mm.format.cloneTo (aFormat);
|
---|
826 |
|
---|
827 | return S_OK;
|
---|
828 | }
|
---|
829 |
|
---|
830 | STDMETHODIMP HardDisk2::COMGETTER(Type) (HardDiskType_T *aType)
|
---|
831 | {
|
---|
832 | if (aType == NULL)
|
---|
833 | return E_POINTER;
|
---|
834 |
|
---|
835 | AutoCaller autoCaller (this);
|
---|
836 | CheckComRCReturnRC (autoCaller.rc());
|
---|
837 |
|
---|
838 | AutoReadLock alock (this);
|
---|
839 |
|
---|
840 | *aType = mm.type;
|
---|
841 |
|
---|
842 | return S_OK;
|
---|
843 | }
|
---|
844 |
|
---|
845 | STDMETHODIMP HardDisk2::COMSETTER(Type) (HardDiskType_T aType)
|
---|
846 | {
|
---|
847 | AutoCaller autoCaller (this);
|
---|
848 | CheckComRCReturnRC (autoCaller.rc());
|
---|
849 |
|
---|
850 | /* VirtualBox::saveSettings() needs a write lock */
|
---|
851 | AutoMultiWriteLock2 alock (mVirtualBox, this);
|
---|
852 |
|
---|
853 | switch (m.state)
|
---|
854 | {
|
---|
855 | case MediaState_Created:
|
---|
856 | case MediaState_Inaccessible:
|
---|
857 | break;
|
---|
858 | default:
|
---|
859 | return setStateError();
|
---|
860 | }
|
---|
861 |
|
---|
862 | if (mm.type == aType)
|
---|
863 | {
|
---|
864 | /* Nothing to do */
|
---|
865 | return S_OK;
|
---|
866 | }
|
---|
867 |
|
---|
868 | /* we access mParent & children() */
|
---|
869 | AutoReadLock treeLock (this->treeLock());
|
---|
870 |
|
---|
871 | /* cannot change the type of a differencing hard disk */
|
---|
872 | if (!mParent.isNull())
|
---|
873 | return setError (E_FAIL,
|
---|
874 | tr ("Hard disk '%ls' is a differencing hard disk"),
|
---|
875 | m.locationFull.raw());
|
---|
876 |
|
---|
877 | /* cannot change the type of a hard disk being in use */
|
---|
878 | if (m.backRefs.size() != 0)
|
---|
879 | return setError (E_FAIL,
|
---|
880 | tr ("Hard disk '%ls' is attached to %d virtual machines"),
|
---|
881 | m.locationFull.raw(), m.backRefs.size());
|
---|
882 |
|
---|
883 | switch (aType)
|
---|
884 | {
|
---|
885 | case HardDiskType_Normal:
|
---|
886 | case HardDiskType_Immutable:
|
---|
887 | {
|
---|
888 | /* normal can be easily converted to imutable and vice versa even
|
---|
889 | * if they have children as long as they are not attached to any
|
---|
890 | * machine themselves */
|
---|
891 | break;
|
---|
892 | }
|
---|
893 | case HardDiskType_Writethrough:
|
---|
894 | {
|
---|
895 | /* cannot change to writethrough if there are children */
|
---|
896 | if (children().size() != 0)
|
---|
897 | return setError (E_FAIL,
|
---|
898 | tr ("Hard disk '%ls' has %d child hard disks"),
|
---|
899 | children().size());
|
---|
900 | break;
|
---|
901 | }
|
---|
902 | default:
|
---|
903 | AssertFailedReturn (E_FAIL);
|
---|
904 | }
|
---|
905 |
|
---|
906 | mm.type = aType;
|
---|
907 |
|
---|
908 | HRESULT rc = mVirtualBox->saveSettings();
|
---|
909 |
|
---|
910 | return rc;
|
---|
911 | }
|
---|
912 |
|
---|
913 | STDMETHODIMP HardDisk2::COMGETTER(Parent) (IHardDisk2 **aParent)
|
---|
914 | {
|
---|
915 | if (aParent == NULL)
|
---|
916 | return E_POINTER;
|
---|
917 |
|
---|
918 | AutoCaller autoCaller (this);
|
---|
919 | CheckComRCReturnRC (autoCaller.rc());
|
---|
920 |
|
---|
921 | /* we access mParent */
|
---|
922 | AutoReadLock treeLock (this->treeLock());
|
---|
923 |
|
---|
924 | mParent.queryInterfaceTo (aParent);
|
---|
925 |
|
---|
926 | return S_OK;
|
---|
927 | }
|
---|
928 |
|
---|
929 | STDMETHODIMP HardDisk2::COMGETTER(Children) (ComSafeArrayOut (IHardDisk2 *, aChildren))
|
---|
930 | {
|
---|
931 | if (ComSafeArrayOutIsNull (aChildren))
|
---|
932 | return E_POINTER;
|
---|
933 |
|
---|
934 | AutoCaller autoCaller (this);
|
---|
935 | CheckComRCReturnRC (autoCaller.rc());
|
---|
936 |
|
---|
937 | /* we access children */
|
---|
938 | AutoReadLock treeLock (this->treeLock());
|
---|
939 |
|
---|
940 | SafeIfaceArray <IHardDisk2> children (this->children());
|
---|
941 | children.detachTo (ComSafeArrayOutArg (aChildren));
|
---|
942 |
|
---|
943 | return S_OK;
|
---|
944 | }
|
---|
945 |
|
---|
946 | STDMETHODIMP HardDisk2::COMGETTER(Root) (IHardDisk2 **aRoot)
|
---|
947 | {
|
---|
948 | if (aRoot == NULL)
|
---|
949 | return E_POINTER;
|
---|
950 |
|
---|
951 | /* root() will do callers/locking */
|
---|
952 |
|
---|
953 | root().queryInterfaceTo (aRoot);
|
---|
954 |
|
---|
955 | return S_OK;
|
---|
956 | }
|
---|
957 |
|
---|
958 | STDMETHODIMP HardDisk2::COMGETTER(ReadOnly) (BOOL *aReadOnly)
|
---|
959 | {
|
---|
960 | if (aReadOnly == NULL)
|
---|
961 | return E_POINTER;
|
---|
962 |
|
---|
963 | AutoCaller autoCaller (this);
|
---|
964 | CheckComRCReturnRC (autoCaller.rc());
|
---|
965 |
|
---|
966 | /* isRadOnly() will do locking */
|
---|
967 |
|
---|
968 | *aReadOnly = isReadOnly();
|
---|
969 |
|
---|
970 | return S_OK;
|
---|
971 | }
|
---|
972 |
|
---|
973 | STDMETHODIMP HardDisk2::COMGETTER(LogicalSize) (ULONG64 *aLogicalSize)
|
---|
974 | {
|
---|
975 | if (aLogicalSize == NULL)
|
---|
976 | return E_POINTER;
|
---|
977 |
|
---|
978 | {
|
---|
979 | AutoCaller autoCaller (this);
|
---|
980 | CheckComRCReturnRC (autoCaller.rc());
|
---|
981 |
|
---|
982 | AutoReadLock alock (this);
|
---|
983 |
|
---|
984 | /* we access mParent */
|
---|
985 | AutoReadLock treeLock (this->treeLock());
|
---|
986 |
|
---|
987 | if (mParent.isNull())
|
---|
988 | {
|
---|
989 | *aLogicalSize = mm.logicalSize;
|
---|
990 |
|
---|
991 | return S_OK;
|
---|
992 | }
|
---|
993 | }
|
---|
994 |
|
---|
995 | /* We assume that some backend may decide to return a meaningless value in
|
---|
996 | * response to VDGetSize() for differencing hard disks and therefore
|
---|
997 | * always ask the base hard disk ourselves. */
|
---|
998 |
|
---|
999 | /* root() will do callers/locking */
|
---|
1000 |
|
---|
1001 | return root()->COMGETTER (LogicalSize) (aLogicalSize);
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | // IHardDisk2 methods
|
---|
1005 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1006 |
|
---|
1007 | STDMETHODIMP HardDisk2::GetProperty (IN_BSTR aName, BSTR *aValue)
|
---|
1008 | {
|
---|
1009 | CheckComArgStrNotEmptyOrNull (aName);
|
---|
1010 | CheckComArgOutPointerValid (aValue);
|
---|
1011 |
|
---|
1012 | AutoCaller autoCaller (this);
|
---|
1013 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1014 |
|
---|
1015 | AutoReadLock alock (this);
|
---|
1016 |
|
---|
1017 | Data::PropertyMap::const_iterator it = mm.properties.find (Bstr (aName));
|
---|
1018 | if (it == mm.properties.end())
|
---|
1019 | return setError (VBOX_E_OBJECT_NOT_FOUND,
|
---|
1020 | tr ("Property '%ls' does not exist"), aName);
|
---|
1021 |
|
---|
1022 | it->second.cloneTo (aValue);
|
---|
1023 |
|
---|
1024 | return S_OK;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | STDMETHODIMP HardDisk2::SetProperty (IN_BSTR aName, IN_BSTR aValue)
|
---|
1028 | {
|
---|
1029 | CheckComArgStrNotEmptyOrNull (aName);
|
---|
1030 |
|
---|
1031 | AutoCaller autoCaller (this);
|
---|
1032 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1033 |
|
---|
1034 | /* VirtualBox::saveSettings() needs a write lock */
|
---|
1035 | AutoMultiWriteLock2 alock (mVirtualBox, this);
|
---|
1036 |
|
---|
1037 | switch (m.state)
|
---|
1038 | {
|
---|
1039 | case MediaState_Created:
|
---|
1040 | case MediaState_Inaccessible:
|
---|
1041 | break;
|
---|
1042 | default:
|
---|
1043 | return setStateError();
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | Data::PropertyMap::iterator it = mm.properties.find (Bstr (aName));
|
---|
1047 | if (it == mm.properties.end())
|
---|
1048 | return setError (VBOX_E_OBJECT_NOT_FOUND,
|
---|
1049 | tr ("Property '%ls' does not exist"), aName);
|
---|
1050 |
|
---|
1051 | it->second = aValue;
|
---|
1052 |
|
---|
1053 | HRESULT rc = mVirtualBox->saveSettings();
|
---|
1054 |
|
---|
1055 | return rc;
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | STDMETHODIMP HardDisk2::GetProperties (IN_BSTR aNames,
|
---|
1059 | ComSafeArrayOut (BSTR, aReturnNames),
|
---|
1060 | ComSafeArrayOut (BSTR, aReturnValues))
|
---|
1061 | {
|
---|
1062 | CheckComArgOutSafeArrayPointerValid (aReturnNames);
|
---|
1063 | CheckComArgOutSafeArrayPointerValid (aReturnValues);
|
---|
1064 |
|
---|
1065 | AutoCaller autoCaller (this);
|
---|
1066 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1067 |
|
---|
1068 | AutoReadLock alock (this);
|
---|
1069 |
|
---|
1070 | /// @todo make use of aNames according to the documentation
|
---|
1071 | NOREF (aNames);
|
---|
1072 |
|
---|
1073 | com::SafeArray <BSTR> names (mm.properties.size());
|
---|
1074 | com::SafeArray <BSTR> values (mm.properties.size());
|
---|
1075 | size_t i = 0;
|
---|
1076 |
|
---|
1077 | for (Data::PropertyMap::const_iterator it = mm.properties.begin();
|
---|
1078 | it != mm.properties.end(); ++ it)
|
---|
1079 | {
|
---|
1080 | it->first.cloneTo (&names [i]);
|
---|
1081 | it->second.cloneTo (&values [i]);
|
---|
1082 | ++ i;
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | names.detachTo (ComSafeArrayOutArg (aReturnNames));
|
---|
1086 | values.detachTo (ComSafeArrayOutArg (aReturnValues));
|
---|
1087 |
|
---|
1088 | return S_OK;
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | STDMETHODIMP HardDisk2::SetProperties (ComSafeArrayIn (IN_BSTR, aNames),
|
---|
1092 | ComSafeArrayIn (IN_BSTR, aValues))
|
---|
1093 | {
|
---|
1094 | CheckComArgSafeArrayNotNull (aNames);
|
---|
1095 | CheckComArgSafeArrayNotNull (aValues);
|
---|
1096 |
|
---|
1097 | AutoCaller autoCaller (this);
|
---|
1098 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1099 |
|
---|
1100 | /* VirtualBox::saveSettings() needs a write lock */
|
---|
1101 | AutoMultiWriteLock2 alock (mVirtualBox, this);
|
---|
1102 |
|
---|
1103 | com::SafeArray <IN_BSTR> names (ComSafeArrayInArg (aNames));
|
---|
1104 | com::SafeArray <IN_BSTR> values (ComSafeArrayInArg (aValues));
|
---|
1105 |
|
---|
1106 | /* first pass: validate names */
|
---|
1107 | for (size_t i = 0; i < names.size(); ++ i)
|
---|
1108 | {
|
---|
1109 | if (mm.properties.find (Bstr (names [i])) == mm.properties.end())
|
---|
1110 | return setError (VBOX_E_OBJECT_NOT_FOUND,
|
---|
1111 | tr ("Property '%ls' does not exist"), names [i]);
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | /* second pass: assign */
|
---|
1115 | for (size_t i = 0; i < names.size(); ++ i)
|
---|
1116 | {
|
---|
1117 | Data::PropertyMap::iterator it = mm.properties.find (Bstr (names [i]));
|
---|
1118 | AssertReturn (it != mm.properties.end(), E_FAIL);
|
---|
1119 |
|
---|
1120 | it->second = values [i];
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | HRESULT rc = mVirtualBox->saveSettings();
|
---|
1124 |
|
---|
1125 | return rc;
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | STDMETHODIMP HardDisk2::CreateDynamicStorage (ULONG64 aLogicalSize,
|
---|
1129 | IProgress **aProgress)
|
---|
1130 | {
|
---|
1131 | CheckComArgOutPointerValid (aProgress);
|
---|
1132 |
|
---|
1133 | AutoCaller autoCaller (this);
|
---|
1134 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1135 |
|
---|
1136 | AutoWriteLock alock (this);
|
---|
1137 |
|
---|
1138 | if (!(mm.formatObj->capabilities() &
|
---|
1139 | HardDiskFormatCapabilities_CreateDynamic))
|
---|
1140 | return setError (VBOX_E_NOT_SUPPORTED,
|
---|
1141 | tr ("Hard disk format '%ls' does not support dynamic storage "
|
---|
1142 | "creation"), mm.format.raw());
|
---|
1143 |
|
---|
1144 | switch (m.state)
|
---|
1145 | {
|
---|
1146 | case MediaState_NotCreated:
|
---|
1147 | break;
|
---|
1148 | default:
|
---|
1149 | return setStateError();
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | ComObjPtr <Progress> progress;
|
---|
1153 | progress.createObject();
|
---|
1154 | HRESULT rc = progress->init (mVirtualBox, static_cast <IHardDisk2 *> (this),
|
---|
1155 | BstrFmt (tr ("Creating dynamic hard disk storage unit '%ls'"),
|
---|
1156 | m.location.raw()),
|
---|
1157 | FALSE /* aCancelable */);
|
---|
1158 | CheckComRCReturnRC (rc);
|
---|
1159 |
|
---|
1160 | /* setup task object and thread to carry out the operation
|
---|
1161 | * asynchronously */
|
---|
1162 |
|
---|
1163 | std::auto_ptr <Task> task (new Task (this, progress, Task::CreateDynamic));
|
---|
1164 | AssertComRCReturnRC (task->autoCaller.rc());
|
---|
1165 |
|
---|
1166 | task->d.size = aLogicalSize;
|
---|
1167 |
|
---|
1168 | rc = task->startThread();
|
---|
1169 | CheckComRCReturnRC (rc);
|
---|
1170 |
|
---|
1171 | /* go to Creating state on success */
|
---|
1172 | m.state = MediaState_Creating;
|
---|
1173 |
|
---|
1174 | /* task is now owned by taskThread() so release it */
|
---|
1175 | task.release();
|
---|
1176 |
|
---|
1177 | /* return progress to the caller */
|
---|
1178 | progress.queryInterfaceTo (aProgress);
|
---|
1179 |
|
---|
1180 | return S_OK;
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | STDMETHODIMP HardDisk2::CreateFixedStorage (ULONG64 aLogicalSize,
|
---|
1184 | IProgress **aProgress)
|
---|
1185 | {
|
---|
1186 | CheckComArgOutPointerValid (aProgress);
|
---|
1187 |
|
---|
1188 | AutoCaller autoCaller (this);
|
---|
1189 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1190 |
|
---|
1191 | AutoWriteLock alock (this);
|
---|
1192 |
|
---|
1193 | if (!(mm.formatObj->capabilities() &
|
---|
1194 | HardDiskFormatCapabilities_CreateFixed))
|
---|
1195 | return setError (VBOX_E_NOT_SUPPORTED,
|
---|
1196 | tr ("Hard disk format '%ls' does not support fixed storage "
|
---|
1197 | "creation"), mm.format.raw());
|
---|
1198 |
|
---|
1199 | switch (m.state)
|
---|
1200 | {
|
---|
1201 | case MediaState_NotCreated:
|
---|
1202 | break;
|
---|
1203 | default:
|
---|
1204 | return setStateError();
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | ComObjPtr <Progress> progress;
|
---|
1208 | progress.createObject();
|
---|
1209 | HRESULT rc = progress->init (mVirtualBox, static_cast <IHardDisk2 *> (this),
|
---|
1210 | BstrFmt (tr ("Creating fixed hard disk storage unit '%ls'"),
|
---|
1211 | m.location.raw()),
|
---|
1212 | FALSE /* aCancelable */);
|
---|
1213 | CheckComRCReturnRC (rc);
|
---|
1214 |
|
---|
1215 | /* setup task object and thread to carry out the operation
|
---|
1216 | * asynchronously */
|
---|
1217 |
|
---|
1218 | std::auto_ptr <Task> task (new Task (this, progress, Task::CreateFixed));
|
---|
1219 | AssertComRCReturnRC (task->autoCaller.rc());
|
---|
1220 |
|
---|
1221 | task->d.size = aLogicalSize;
|
---|
1222 |
|
---|
1223 | rc = task->startThread();
|
---|
1224 | CheckComRCReturnRC (rc);
|
---|
1225 |
|
---|
1226 | /* go to Creating state on success */
|
---|
1227 | m.state = MediaState_Creating;
|
---|
1228 |
|
---|
1229 | /* task is now owned by taskThread() so release it */
|
---|
1230 | task.release();
|
---|
1231 |
|
---|
1232 | /* return progress to the caller */
|
---|
1233 | progress.queryInterfaceTo (aProgress);
|
---|
1234 |
|
---|
1235 | return S_OK;
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | STDMETHODIMP HardDisk2::DeleteStorage (IProgress **aProgress)
|
---|
1239 | {
|
---|
1240 | CheckComArgOutPointerValid (aProgress);
|
---|
1241 |
|
---|
1242 | AutoCaller autoCaller (this);
|
---|
1243 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1244 |
|
---|
1245 | ComObjPtr <Progress> progress;
|
---|
1246 |
|
---|
1247 | HRESULT rc = deleteStorageNoWait (progress);
|
---|
1248 | if (SUCCEEDED (rc))
|
---|
1249 | {
|
---|
1250 | /* return progress to the caller */
|
---|
1251 | progress.queryInterfaceTo (aProgress);
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | return rc;
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | STDMETHODIMP HardDisk2::CreateDiffStorage (IHardDisk2 *aTarget, IProgress **aProgress)
|
---|
1258 | {
|
---|
1259 | CheckComArgNotNull (aTarget);
|
---|
1260 | CheckComArgOutPointerValid (aProgress);
|
---|
1261 |
|
---|
1262 | AutoCaller autoCaller (this);
|
---|
1263 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1264 |
|
---|
1265 | ComObjPtr <HardDisk2> diff;
|
---|
1266 | HRESULT rc = mVirtualBox->cast (aTarget, diff);
|
---|
1267 | CheckComRCReturnRC (rc);
|
---|
1268 |
|
---|
1269 | AutoWriteLock alock (this);
|
---|
1270 |
|
---|
1271 | if (mm.type == HardDiskType_Writethrough)
|
---|
1272 | return setError (E_FAIL, tr ("Hard disk '%ls' is Writethrough"));
|
---|
1273 |
|
---|
1274 | /* We want to be locked for reading as long as our diff child is being
|
---|
1275 | * created */
|
---|
1276 | rc = LockRead (NULL);
|
---|
1277 | CheckComRCReturnRC (rc);
|
---|
1278 |
|
---|
1279 | ComObjPtr <Progress> progress;
|
---|
1280 |
|
---|
1281 | rc = createDiffStorageNoWait (diff, progress);
|
---|
1282 | if (FAILED (rc))
|
---|
1283 | {
|
---|
1284 | HRESULT rc2 = UnlockRead (NULL);
|
---|
1285 | AssertComRC (rc2);
|
---|
1286 | /* Note: on success, taskThread() will unlock this */
|
---|
1287 | }
|
---|
1288 | else
|
---|
1289 | {
|
---|
1290 | /* return progress to the caller */
|
---|
1291 | progress.queryInterfaceTo (aProgress);
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | return rc;
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | STDMETHODIMP HardDisk2::MergeTo (IN_GUID aTargetId, IProgress **aProgress)
|
---|
1298 | {
|
---|
1299 | AutoCaller autoCaller (this);
|
---|
1300 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1301 |
|
---|
1302 | ReturnComNotImplemented();
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | STDMETHODIMP HardDisk2::CloneTo (IHardDisk2 *aTarget, IProgress **aProgress)
|
---|
1306 | {
|
---|
1307 | CheckComArgNotNull (aTarget);
|
---|
1308 | CheckComArgOutPointerValid (aProgress);
|
---|
1309 |
|
---|
1310 | AutoCaller autoCaller (this);
|
---|
1311 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1312 |
|
---|
1313 | ComObjPtr <HardDisk2> target;
|
---|
1314 | HRESULT rc = mVirtualBox->cast (aTarget, target);
|
---|
1315 | CheckComRCReturnRC (rc);
|
---|
1316 |
|
---|
1317 | AutoMultiWriteLock2 alock (this, target);
|
---|
1318 |
|
---|
1319 | /* We want to be locked for reading as long as the clone hard disk is being
|
---|
1320 | * created*/
|
---|
1321 | rc = LockRead (NULL);
|
---|
1322 | CheckComRCReturnRC (rc);
|
---|
1323 |
|
---|
1324 | ComObjPtr <Progress> progress;
|
---|
1325 |
|
---|
1326 | try
|
---|
1327 | {
|
---|
1328 | if (target->m.state != MediaState_NotCreated)
|
---|
1329 | throw target->setStateError();
|
---|
1330 |
|
---|
1331 | progress.createObject();
|
---|
1332 | rc = progress->init (mVirtualBox, static_cast <IHardDisk2 *> (this),
|
---|
1333 | BstrFmt (tr ("Creating a clone hard disk '%s'"),
|
---|
1334 | target->name().raw()),
|
---|
1335 | FALSE /* aCancelable */);
|
---|
1336 | CheckComRCThrowRC (rc);
|
---|
1337 |
|
---|
1338 | /* setup task object and thread to carry out the operation
|
---|
1339 | * asynchronously */
|
---|
1340 |
|
---|
1341 | std::auto_ptr <Task> task (new Task (this, progress, Task::Clone));
|
---|
1342 | AssertComRCThrowRC (task->autoCaller.rc());
|
---|
1343 |
|
---|
1344 | task->setData (target);
|
---|
1345 |
|
---|
1346 | rc = task->startThread();
|
---|
1347 | CheckComRCThrowRC (rc);
|
---|
1348 |
|
---|
1349 | /* go to Creating state before leaving the lock */
|
---|
1350 | target->m.state = MediaState_Creating;
|
---|
1351 |
|
---|
1352 | /* task is now owned (or already deleted) by taskThread() so release it */
|
---|
1353 | task.release();
|
---|
1354 | }
|
---|
1355 | catch (HRESULT aRC)
|
---|
1356 | {
|
---|
1357 | rc = aRC;
|
---|
1358 | }
|
---|
1359 |
|
---|
1360 | if (FAILED (rc))
|
---|
1361 | {
|
---|
1362 | HRESULT rc2 = UnlockRead (NULL);
|
---|
1363 | AssertComRC (rc2);
|
---|
1364 | /* Note: on success, taskThread() will unlock this */
|
---|
1365 | }
|
---|
1366 | else
|
---|
1367 | {
|
---|
1368 | /* return progress to the caller */
|
---|
1369 | progress.queryInterfaceTo (aProgress);
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | return rc;
|
---|
1373 | }
|
---|
1374 |
|
---|
1375 | STDMETHODIMP HardDisk2::FlattenTo (IHardDisk2 *aTarget, IProgress **aProgress)
|
---|
1376 | {
|
---|
1377 | AutoCaller autoCaller (this);
|
---|
1378 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1379 |
|
---|
1380 | ReturnComNotImplemented();
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | STDMETHODIMP HardDisk2::Compact (IProgress **aProgress)
|
---|
1384 | {
|
---|
1385 | AutoCaller autoCaller (this);
|
---|
1386 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1387 |
|
---|
1388 | ReturnComNotImplemented();
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | // public methods for internal purposes only
|
---|
1392 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1393 |
|
---|
1394 | /**
|
---|
1395 | * Checks if the given change of \a aOldPath to \a aNewPath affects the location
|
---|
1396 | * of this hard disk or any its child and updates the paths if necessary to
|
---|
1397 | * reflect the new location.
|
---|
1398 | *
|
---|
1399 | * @param aOldPath Old path (full).
|
---|
1400 | * @param aNewPath New path (full).
|
---|
1401 | *
|
---|
1402 | * @note Locks treeLock() for reading, this object and all children for writing.
|
---|
1403 | */
|
---|
1404 | void HardDisk2::updatePaths (const char *aOldPath, const char *aNewPath)
|
---|
1405 | {
|
---|
1406 | AssertReturnVoid (aOldPath);
|
---|
1407 | AssertReturnVoid (aNewPath);
|
---|
1408 |
|
---|
1409 | AutoCaller autoCaller (this);
|
---|
1410 | AssertComRCReturnVoid (autoCaller.rc());
|
---|
1411 |
|
---|
1412 | AutoWriteLock alock (this);
|
---|
1413 |
|
---|
1414 | /* we access children() */
|
---|
1415 | AutoReadLock treeLock (this->treeLock());
|
---|
1416 |
|
---|
1417 | updatePath (aOldPath, aNewPath);
|
---|
1418 |
|
---|
1419 | /* update paths of all children */
|
---|
1420 | for (List::const_iterator it = children().begin();
|
---|
1421 | it != children().end();
|
---|
1422 | ++ it)
|
---|
1423 | {
|
---|
1424 | (*it)->updatePaths (aOldPath, aNewPath);
|
---|
1425 | }
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | /**
|
---|
1429 | * Returns the base hard disk of the hard disk chain this hard disk is part of.
|
---|
1430 | *
|
---|
1431 | * The root hard disk is found by walking up the parent-child relationship axis.
|
---|
1432 | * If the hard disk doesn't have a parent (i.e. it's a base hard disk), it
|
---|
1433 | * returns itself in response to this method.
|
---|
1434 | *
|
---|
1435 | * @param aLevel Where to store the number of ancestors of this hard disk
|
---|
1436 | * (zero for the root), may be @c NULL.
|
---|
1437 | *
|
---|
1438 | * @note Locks treeLock() for reading.
|
---|
1439 | */
|
---|
1440 | ComObjPtr <HardDisk2> HardDisk2::root (uint32_t *aLevel /*= NULL*/)
|
---|
1441 | {
|
---|
1442 | ComObjPtr <HardDisk2> root;
|
---|
1443 | uint32_t level;
|
---|
1444 |
|
---|
1445 | AutoCaller autoCaller (this);
|
---|
1446 | AssertReturn (autoCaller.isOk(), root);
|
---|
1447 |
|
---|
1448 | /* we access mParent */
|
---|
1449 | AutoReadLock treeLock (this->treeLock());
|
---|
1450 |
|
---|
1451 | root = this;
|
---|
1452 | level = 0;
|
---|
1453 |
|
---|
1454 | if (!mParent.isNull())
|
---|
1455 | {
|
---|
1456 | for (;;)
|
---|
1457 | {
|
---|
1458 | AutoCaller rootCaller (root);
|
---|
1459 | AssertReturn (rootCaller.isOk(), root);
|
---|
1460 |
|
---|
1461 | if (root->mParent.isNull())
|
---|
1462 | break;
|
---|
1463 |
|
---|
1464 | root = root->mParent;
|
---|
1465 | ++ level;
|
---|
1466 | }
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | if (aLevel != NULL)
|
---|
1470 | *aLevel = level;
|
---|
1471 |
|
---|
1472 | return root;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | /**
|
---|
1476 | * Returns @c true if this hard disk cannot be modified because it has
|
---|
1477 | * dependants (children) or is part of the snapshot. Related to the hard disk
|
---|
1478 | * type and posterity, not to the current media state.
|
---|
1479 | *
|
---|
1480 | * @note Locks this object and treeLock() for reading.
|
---|
1481 | */
|
---|
1482 | bool HardDisk2::isReadOnly()
|
---|
1483 | {
|
---|
1484 | AutoCaller autoCaller (this);
|
---|
1485 | AssertComRCReturn (autoCaller.rc(), false);
|
---|
1486 |
|
---|
1487 | AutoReadLock alock (this);
|
---|
1488 |
|
---|
1489 | /* we access children */
|
---|
1490 | AutoReadLock treeLock (this->treeLock());
|
---|
1491 |
|
---|
1492 | switch (mm.type)
|
---|
1493 | {
|
---|
1494 | case HardDiskType_Normal:
|
---|
1495 | {
|
---|
1496 | if (children().size() != 0)
|
---|
1497 | return true;
|
---|
1498 |
|
---|
1499 | for (BackRefList::const_iterator it = m.backRefs.begin();
|
---|
1500 | it != m.backRefs.end(); ++ it)
|
---|
1501 | if (it->snapshotIds.size() != 0)
|
---|
1502 | return true;
|
---|
1503 |
|
---|
1504 | return false;
|
---|
1505 | }
|
---|
1506 | case HardDiskType_Immutable:
|
---|
1507 | {
|
---|
1508 | return true;
|
---|
1509 | }
|
---|
1510 | case HardDiskType_Writethrough:
|
---|
1511 | {
|
---|
1512 | return false;
|
---|
1513 | }
|
---|
1514 | default:
|
---|
1515 | break;
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | AssertFailedReturn (false);
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | /**
|
---|
1522 | * Saves hard disk data by appending a new <HardDisk> child node to the given
|
---|
1523 | * parent node which can be either <HardDisks> or <HardDisk>.
|
---|
1524 | *
|
---|
1525 | * @param aaParentNode Parent <HardDisks> or <HardDisk> node.
|
---|
1526 | *
|
---|
1527 | * @note Locks this object, treeLock() and children for reading.
|
---|
1528 | */
|
---|
1529 | HRESULT HardDisk2::saveSettings (settings::Key &aParentNode)
|
---|
1530 | {
|
---|
1531 | using namespace settings;
|
---|
1532 |
|
---|
1533 | AssertReturn (!aParentNode.isNull(), E_FAIL);
|
---|
1534 |
|
---|
1535 | AutoCaller autoCaller (this);
|
---|
1536 | CheckComRCReturnRC (autoCaller.rc());
|
---|
1537 |
|
---|
1538 | AutoReadLock alock (this);
|
---|
1539 |
|
---|
1540 | /* we access mParent */
|
---|
1541 | AutoReadLock treeLock (this->treeLock());
|
---|
1542 |
|
---|
1543 | Key diskNode = aParentNode.appendKey ("HardDisk");
|
---|
1544 | /* required */
|
---|
1545 | diskNode.setValue <Guid> ("uuid", m.id);
|
---|
1546 | /* required (note: the original locaiton, not full) */
|
---|
1547 | diskNode.setValue <Bstr> ("location", m.location);
|
---|
1548 | /* required */
|
---|
1549 | diskNode.setValue <Bstr> ("format", mm.format);
|
---|
1550 | /* optional */
|
---|
1551 | if (!m.description.isNull())
|
---|
1552 | {
|
---|
1553 | Key descNode = diskNode.createKey ("Description");
|
---|
1554 | descNode.setKeyValue <Bstr> (m.description);
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | /* optional properties */
|
---|
1558 | for (Data::PropertyMap::const_iterator it = mm.properties.begin();
|
---|
1559 | it != mm.properties.end(); ++ it)
|
---|
1560 | {
|
---|
1561 | /* only save properties that have non-default values */
|
---|
1562 | if (!it->second.isNull())
|
---|
1563 | {
|
---|
1564 | Key propNode = diskNode.appendKey ("Property");
|
---|
1565 | propNode.setValue <Bstr> ("name", it->first);
|
---|
1566 | propNode.setValue <Bstr> ("value", it->second);
|
---|
1567 | }
|
---|
1568 | }
|
---|
1569 |
|
---|
1570 | /* only for base hard disks */
|
---|
1571 | if (mParent.isNull())
|
---|
1572 | {
|
---|
1573 | const char *type =
|
---|
1574 | mm.type == HardDiskType_Normal ? "Normal" :
|
---|
1575 | mm.type == HardDiskType_Immutable ? "Immutable" :
|
---|
1576 | mm.type == HardDiskType_Writethrough ? "Writethrough" : NULL;
|
---|
1577 | Assert (type != NULL);
|
---|
1578 | diskNode.setStringValue ("type", type);
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | /* save all children */
|
---|
1582 | for (List::const_iterator it = children().begin();
|
---|
1583 | it != children().end();
|
---|
1584 | ++ it)
|
---|
1585 | {
|
---|
1586 | HRESULT rc = (*it)->saveSettings (diskNode);
|
---|
1587 | AssertComRCReturnRC (rc);
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | return S_OK;
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 | /**
|
---|
1594 | * Compares the location of this hard disk to the given location.
|
---|
1595 | *
|
---|
1596 | * The comparison takes the location details into account. For example, if the
|
---|
1597 | * location is a file in the host's filesystem, a case insensitive comparison
|
---|
1598 | * will be performed for case insensitive filesystems.
|
---|
1599 | *
|
---|
1600 | * @param aLocation Location to compare to (as is).
|
---|
1601 | * @param aResult Where to store the result of comparison: 0 if locations
|
---|
1602 | * are equal, 1 if this object's location is greater than
|
---|
1603 | * the specified location, and -1 otherwise.
|
---|
1604 | */
|
---|
1605 | HRESULT HardDisk2::compareLocationTo (const char *aLocation, int &aResult)
|
---|
1606 | {
|
---|
1607 | AutoCaller autoCaller (this);
|
---|
1608 | AssertComRCReturnRC (autoCaller.rc());
|
---|
1609 |
|
---|
1610 | AutoReadLock alock (this);
|
---|
1611 |
|
---|
1612 | Utf8Str locationFull (m.locationFull);
|
---|
1613 |
|
---|
1614 | /// @todo NEWMEDIA delegate the comparison to the backend?
|
---|
1615 |
|
---|
1616 | if (mm.formatObj->capabilities() & HardDiskFormatCapabilities_File)
|
---|
1617 | {
|
---|
1618 | Utf8Str location (aLocation);
|
---|
1619 |
|
---|
1620 | /* For locations represented by files, append the default path if
|
---|
1621 | * only the name is given, and then get the full path. */
|
---|
1622 | if (!RTPathHavePath (aLocation))
|
---|
1623 | {
|
---|
1624 | AutoReadLock propsLock (mVirtualBox->systemProperties());
|
---|
1625 | location = Utf8StrFmt ("%ls%c%s",
|
---|
1626 | mVirtualBox->systemProperties()->defaultHardDiskFolder().raw(),
|
---|
1627 | RTPATH_DELIMITER, aLocation);
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 | int vrc = mVirtualBox->calculateFullPath (location, location);
|
---|
1631 | if (RT_FAILURE (vrc))
|
---|
1632 | return setError (E_FAIL,
|
---|
1633 | tr ("Invalid hard disk storage file location '%s' (%Rrc)"),
|
---|
1634 | location.raw(), vrc);
|
---|
1635 |
|
---|
1636 | aResult = RTPathCompare (locationFull, location);
|
---|
1637 | }
|
---|
1638 | else
|
---|
1639 | aResult = locationFull.compare (aLocation);
|
---|
1640 |
|
---|
1641 | return S_OK;
|
---|
1642 | }
|
---|
1643 |
|
---|
1644 | /**
|
---|
1645 | * Returns a short version of the location attribute.
|
---|
1646 | *
|
---|
1647 | * Reimplements MediumBase::name() to specially treat non-FS-path locations.
|
---|
1648 | *
|
---|
1649 | * @note Must be called from under this object's read or write lock.
|
---|
1650 | */
|
---|
1651 | Utf8Str HardDisk2::name()
|
---|
1652 | {
|
---|
1653 | /// @todo NEWMEDIA treat non-FS-paths specially! (may require to requiest
|
---|
1654 | /// this information from the VD backend)
|
---|
1655 |
|
---|
1656 | Utf8Str location (m.locationFull);
|
---|
1657 |
|
---|
1658 | Utf8Str name = RTPathFilename (location);
|
---|
1659 | return name;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | /**
|
---|
1663 | * Checks that this hard disk may be discarded and performs necessary state
|
---|
1664 | * changes.
|
---|
1665 | *
|
---|
1666 | * This method is to be called prior to calling the #discrad() to perform
|
---|
1667 | * necessary consistency checks and place involved hard disks to appropriate
|
---|
1668 | * states. If #discard() is not called or fails, the state modifications
|
---|
1669 | * performed by this method must be undone by #cancelDiscard().
|
---|
1670 | *
|
---|
1671 | * See #discard() for more info about discarding hard disks.
|
---|
1672 | *
|
---|
1673 | * @param aChain Where to store the created merge chain (may return NULL
|
---|
1674 | * if no real merge is necessary).
|
---|
1675 | *
|
---|
1676 | * @note Locks treeLock() for reading. Locks this object, aTarget and all
|
---|
1677 | * intermediate hard disks for writing.
|
---|
1678 | */
|
---|
1679 | HRESULT HardDisk2::prepareDiscard (MergeChain * &aChain)
|
---|
1680 | {
|
---|
1681 | AutoCaller autoCaller (this);
|
---|
1682 | AssertComRCReturnRC (autoCaller.rc());
|
---|
1683 |
|
---|
1684 | aChain = NULL;
|
---|
1685 |
|
---|
1686 | AutoWriteLock alock (this);
|
---|
1687 |
|
---|
1688 | /* we access mParent & children() */
|
---|
1689 | AutoReadLock treeLock (this->treeLock());
|
---|
1690 |
|
---|
1691 | AssertReturn (mm.type == HardDiskType_Normal, E_FAIL);
|
---|
1692 |
|
---|
1693 | if (children().size() == 0)
|
---|
1694 | {
|
---|
1695 | /* special treatment of the last hard disk in the chain: */
|
---|
1696 |
|
---|
1697 | if (mParent.isNull())
|
---|
1698 | {
|
---|
1699 | /* lock only, to prevent any usage; discard() will unlock */
|
---|
1700 | return LockWrite (NULL);
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 | /* the differencing hard disk w/o children will be deleted, protect it
|
---|
1704 | * from attaching to other VMs (this is why Deleting) */
|
---|
1705 |
|
---|
1706 | switch (m.state)
|
---|
1707 | {
|
---|
1708 | case MediaState_Created:
|
---|
1709 | m.state = MediaState_Deleting;
|
---|
1710 | break;
|
---|
1711 | default:
|
---|
1712 | return setStateError();
|
---|
1713 | }
|
---|
1714 |
|
---|
1715 | /* aChain is intentionally NULL here */
|
---|
1716 |
|
---|
1717 | return S_OK;
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 | /* not going multi-merge as it's too expensive */
|
---|
1721 | if (children().size() > 1)
|
---|
1722 | return setError (E_FAIL,
|
---|
1723 | tr ("Hard disk '%ls' has more than one child hard disk (%d)"),
|
---|
1724 | m.locationFull.raw(), children().size());
|
---|
1725 |
|
---|
1726 | /* this is a read-only hard disk with children; it must be associated with
|
---|
1727 | * exactly one snapshot (when the snapshot is being taken, none of the
|
---|
1728 | * current VM's hard disks may be attached to other VMs). Note that by the
|
---|
1729 | * time when discard() is called, there must be no any attachments at all
|
---|
1730 | * (the code calling prepareDiscard() should detach). */
|
---|
1731 | AssertReturn (m.backRefs.size() == 1 &&
|
---|
1732 | !m.backRefs.front().inCurState &&
|
---|
1733 | m.backRefs.front().snapshotIds.size() == 1, E_FAIL);
|
---|
1734 |
|
---|
1735 | ComObjPtr <HardDisk2> child = children().front();
|
---|
1736 |
|
---|
1737 | /* we keep this locked, so lock the affected child to make sure the lock
|
---|
1738 | * order is correct when calling prepareMergeTo() */
|
---|
1739 | AutoWriteLock childLock (child);
|
---|
1740 |
|
---|
1741 | /* delegate the rest to the profi */
|
---|
1742 | if (mParent.isNull())
|
---|
1743 | {
|
---|
1744 | /* base hard disk, backward merge */
|
---|
1745 |
|
---|
1746 | Assert (child->m.backRefs.size() == 1);
|
---|
1747 | if (child->m.backRefs.front().machineId != m.backRefs.front().machineId)
|
---|
1748 | {
|
---|
1749 | /* backward merge is too tricky, we'll just detach on discard, so
|
---|
1750 | * lock only, to prevent any usage; discard() will only unlock
|
---|
1751 | * (since we return NULL in aChain) */
|
---|
1752 | return LockWrite (NULL);
|
---|
1753 | }
|
---|
1754 |
|
---|
1755 | return child->prepareMergeTo (this, aChain,
|
---|
1756 | true /* aIgnoreAttachments */);
|
---|
1757 | }
|
---|
1758 | else
|
---|
1759 | {
|
---|
1760 | /* forward merge */
|
---|
1761 | return prepareMergeTo (child, aChain,
|
---|
1762 | true /* aIgnoreAttachments */);
|
---|
1763 | }
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | /**
|
---|
1767 | * Discards this hard disk.
|
---|
1768 | *
|
---|
1769 | * Discarding the hard disk is merging its contents to its differencing child
|
---|
1770 | * hard disk (forward merge) or contents of its child hard disk to itself
|
---|
1771 | * (backward merge) if this hard disk is a base hard disk. If this hard disk is
|
---|
1772 | * a differencing hard disk w/o children, then it will be simply deleted.
|
---|
1773 | * Calling this method on a base hard disk w/o children will do nothing and
|
---|
1774 | * silently succeed. If this hard disk has more than one child, the method will
|
---|
1775 | * currently return an error (since merging in this case would be too expensive
|
---|
1776 | * and result in data duplication).
|
---|
1777 | *
|
---|
1778 | * When the backward merge takes place (i.e. this hard disk is a target) then,
|
---|
1779 | * on success, this hard disk will automatically replace the differencing child
|
---|
1780 | * hard disk used as a source (which will then be deleted) in the attachment
|
---|
1781 | * this child hard disk is associated with. This will happen only if both hard
|
---|
1782 | * disks belong to the same machine because otherwise such a replace would be
|
---|
1783 | * too tricky and could be not expected by the other machine. Same relates to a
|
---|
1784 | * case when the child hard disk is not associated with any machine at all. When
|
---|
1785 | * the backward merge is not applied, the method behaves as if the base hard
|
---|
1786 | * disk were not attached at all -- i.e. simply detaches it from the machine but
|
---|
1787 | * leaves the hard disk chain intact.
|
---|
1788 | *
|
---|
1789 | * This method is basically a wrapper around #mergeTo() that selects the correct
|
---|
1790 | * merge direction and performs additional actions as described above and.
|
---|
1791 | *
|
---|
1792 | * Note that this method will not return until the merge operation is complete
|
---|
1793 | * (which may be quite time consuming depending on the size of the merged hard
|
---|
1794 | * disks).
|
---|
1795 | *
|
---|
1796 | * Note that #prepareDiscard() must be called before calling this method. If
|
---|
1797 | * this method returns a failure, the caller must call #cancelDiscard(). On
|
---|
1798 | * success, #cancelDiscard() must not be called (this method will perform all
|
---|
1799 | * necessary steps such as resetting states of all involved hard disks and
|
---|
1800 | * deleting @a aChain).
|
---|
1801 | *
|
---|
1802 | * @param aChain Merge chain created by #prepareDiscard() (may be NULL if
|
---|
1803 | * no real merge takes place).
|
---|
1804 | *
|
---|
1805 | * @note Locks the hard disks from the chain for writing. Locks the machine
|
---|
1806 | * object when the backward merge takes place. Locks treeLock() lock for
|
---|
1807 | * reading or writing.
|
---|
1808 | */
|
---|
1809 | HRESULT HardDisk2::discard (ComObjPtr <Progress> &aProgress, MergeChain *aChain)
|
---|
1810 | {
|
---|
1811 | AssertReturn (!aProgress.isNull(), E_FAIL);
|
---|
1812 |
|
---|
1813 | ComObjPtr <HardDisk2> hdFrom;
|
---|
1814 |
|
---|
1815 | HRESULT rc = S_OK;
|
---|
1816 |
|
---|
1817 | {
|
---|
1818 | AutoCaller autoCaller (this);
|
---|
1819 | AssertComRCReturnRC (autoCaller.rc());
|
---|
1820 |
|
---|
1821 | aProgress->advanceOperation (BstrFmt (
|
---|
1822 | tr ("Discarding hard disk '%s'"), name().raw()));
|
---|
1823 |
|
---|
1824 | if (aChain == NULL)
|
---|
1825 | {
|
---|
1826 | AutoWriteLock alock (this);
|
---|
1827 |
|
---|
1828 | /* we access mParent & children() */
|
---|
1829 | AutoReadLock treeLock (this->treeLock());
|
---|
1830 |
|
---|
1831 | Assert (children().size() == 0);
|
---|
1832 |
|
---|
1833 | /* special treatment of the last hard disk in the chain: */
|
---|
1834 |
|
---|
1835 | if (mParent.isNull())
|
---|
1836 | {
|
---|
1837 | rc = UnlockWrite (NULL);
|
---|
1838 | AssertComRC (rc);
|
---|
1839 | return rc;
|
---|
1840 | }
|
---|
1841 |
|
---|
1842 | /* delete the differencing hard disk w/o children */
|
---|
1843 |
|
---|
1844 | Assert (m.state == MediaState_Deleting);
|
---|
1845 |
|
---|
1846 | /* go back to Created since deleteStorage() expects this state */
|
---|
1847 | m.state = MediaState_Created;
|
---|
1848 |
|
---|
1849 | hdFrom = this;
|
---|
1850 |
|
---|
1851 | rc = deleteStorageAndWait (&aProgress);
|
---|
1852 | }
|
---|
1853 | else
|
---|
1854 | {
|
---|
1855 | hdFrom = aChain->source();
|
---|
1856 |
|
---|
1857 | rc = hdFrom->mergeToAndWait (aChain, &aProgress);
|
---|
1858 | }
|
---|
1859 | }
|
---|
1860 |
|
---|
1861 | if (SUCCEEDED (rc))
|
---|
1862 | {
|
---|
1863 | /* mergeToAndWait() cannot uninitialize the initiator because of
|
---|
1864 | * possible AutoCallers on the current thread, deleteStorageAndWait()
|
---|
1865 | * doesn't do it either; do it ourselves */
|
---|
1866 | hdFrom->uninit();
|
---|
1867 | }
|
---|
1868 |
|
---|
1869 | return rc;
|
---|
1870 | }
|
---|
1871 |
|
---|
1872 | /**
|
---|
1873 | * Undoes what #prepareDiscard() did. Must be called if #discard() is not called
|
---|
1874 | * or fails. Frees memory occupied by @a aChain.
|
---|
1875 | *
|
---|
1876 | * @param aChain Merge chain created by #prepareDiscard() (may be NULL if
|
---|
1877 | * no real merge takes place).
|
---|
1878 | *
|
---|
1879 | * @note Locks the hard disks from the chain for writing. Locks treeLock() for
|
---|
1880 | * reading.
|
---|
1881 | */
|
---|
1882 | void HardDisk2::cancelDiscard (MergeChain *aChain)
|
---|
1883 | {
|
---|
1884 | AutoCaller autoCaller (this);
|
---|
1885 | AssertComRCReturnVoid (autoCaller.rc());
|
---|
1886 |
|
---|
1887 | if (aChain == NULL)
|
---|
1888 | {
|
---|
1889 | AutoWriteLock alock (this);
|
---|
1890 |
|
---|
1891 | /* we access mParent & children() */
|
---|
1892 | AutoReadLock treeLock (this->treeLock());
|
---|
1893 |
|
---|
1894 | Assert (children().size() == 0);
|
---|
1895 |
|
---|
1896 | /* special treatment of the last hard disk in the chain: */
|
---|
1897 |
|
---|
1898 | if (mParent.isNull())
|
---|
1899 | {
|
---|
1900 | HRESULT rc = UnlockWrite (NULL);
|
---|
1901 | AssertComRC (rc);
|
---|
1902 | return;
|
---|
1903 | }
|
---|
1904 |
|
---|
1905 | /* the differencing hard disk w/o children will be deleted, protect it
|
---|
1906 | * from attaching to other VMs (this is why Deleting) */
|
---|
1907 |
|
---|
1908 | Assert (m.state == MediaState_Deleting);
|
---|
1909 | m.state = MediaState_Created;
|
---|
1910 |
|
---|
1911 | return;
|
---|
1912 | }
|
---|
1913 |
|
---|
1914 | /* delegate the rest to the profi */
|
---|
1915 | cancelMergeTo (aChain);
|
---|
1916 | }
|
---|
1917 |
|
---|
1918 | /**
|
---|
1919 | * Returns a preferred format for differencing hard disks.
|
---|
1920 | */
|
---|
1921 | Bstr HardDisk2::preferredDiffFormat()
|
---|
1922 | {
|
---|
1923 | Bstr format;
|
---|
1924 |
|
---|
1925 | AutoCaller autoCaller (this);
|
---|
1926 | AssertComRCReturn (autoCaller.rc(), format);
|
---|
1927 |
|
---|
1928 | /* mm.format is const, no need to lock */
|
---|
1929 | format = mm.format;
|
---|
1930 |
|
---|
1931 | /* check that our own format supports diffs */
|
---|
1932 | if (!(mm.formatObj->capabilities() & HardDiskFormatCapabilities_Differencing))
|
---|
1933 | {
|
---|
1934 | /* use the default format if not */
|
---|
1935 | AutoReadLock propsLock (mVirtualBox->systemProperties());
|
---|
1936 | format = mVirtualBox->systemProperties()->defaultHardDiskFormat();
|
---|
1937 | }
|
---|
1938 |
|
---|
1939 | return format;
|
---|
1940 | }
|
---|
1941 |
|
---|
1942 | // protected methods
|
---|
1943 | ////////////////////////////////////////////////////////////////////////////////
|
---|
1944 |
|
---|
1945 | /**
|
---|
1946 | * Deletes the hard disk storage unit.
|
---|
1947 | *
|
---|
1948 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
1949 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
1950 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
1951 | * progress object is created/used at all.
|
---|
1952 | *
|
---|
1953 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
1954 | * delete operation asynchronously and will return immediately. Otherwise, it
|
---|
1955 | * will perform the operation on the calling thread and will not return to the
|
---|
1956 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
1957 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
1958 | *
|
---|
1959 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
1960 | * completion.
|
---|
1961 | * @param aWait @c true if this method should block instead of creating
|
---|
1962 | * an asynchronous thread.
|
---|
1963 | *
|
---|
1964 | * @note Locks mVirtualBox and this object for writing. Locks treeLock() for
|
---|
1965 | * writing.
|
---|
1966 | */
|
---|
1967 | HRESULT HardDisk2::deleteStorage (ComObjPtr <Progress> *aProgress, bool aWait)
|
---|
1968 | {
|
---|
1969 | AssertReturn (aProgress != NULL || aWait == true, E_FAIL);
|
---|
1970 |
|
---|
1971 | /* unregisterWithVirtualBox() needs a write lock. We want to unregister
|
---|
1972 | * ourselves atomically after detecting that deletion is possible to make
|
---|
1973 | * sure that we don't do that after another thread has done
|
---|
1974 | * VirtualBox::findHardDisk2() but before it starts using us (provided that
|
---|
1975 | * it holds a mVirtualBox lock too of course). */
|
---|
1976 |
|
---|
1977 | AutoWriteLock vboxLock (mVirtualBox);
|
---|
1978 |
|
---|
1979 | AutoWriteLock alock (this);
|
---|
1980 |
|
---|
1981 | if (!(mm.formatObj->capabilities() &
|
---|
1982 | (HardDiskFormatCapabilities_CreateDynamic |
|
---|
1983 | HardDiskFormatCapabilities_CreateFixed)))
|
---|
1984 | return setError (VBOX_E_NOT_SUPPORTED,
|
---|
1985 | tr ("Hard disk format '%ls' does not support storage deletion"),
|
---|
1986 | mm.format.raw());
|
---|
1987 |
|
---|
1988 | /* Note that we are fine with Inaccessible state too: a) for symmetry with
|
---|
1989 | * create calls and b) because it doesn't really harm to try, if it is
|
---|
1990 | * really inaccessibke, the delete operation will fail anyway. Accepting
|
---|
1991 | * Inaccessible state is especially important because all registered hard
|
---|
1992 | * disks are initially Inaccessible upon VBoxSVC startup until
|
---|
1993 | * COMGETTER(State) is called. */
|
---|
1994 |
|
---|
1995 | switch (m.state)
|
---|
1996 | {
|
---|
1997 | case MediaState_Created:
|
---|
1998 | case MediaState_Inaccessible:
|
---|
1999 | break;
|
---|
2000 | default:
|
---|
2001 | return setStateError();
|
---|
2002 | }
|
---|
2003 |
|
---|
2004 | if (m.backRefs.size() != 0)
|
---|
2005 | return setError (VBOX_E_OBJECT_IN_USE,
|
---|
2006 | tr ("Hard disk '%ls' is attached to %d virtual machines"),
|
---|
2007 | m.locationFull.raw(), m.backRefs.size());
|
---|
2008 |
|
---|
2009 | HRESULT rc = canClose();
|
---|
2010 | CheckComRCReturnRC (rc);
|
---|
2011 |
|
---|
2012 | /* go to Deleting state before leaving the lock */
|
---|
2013 | m.state = MediaState_Deleting;
|
---|
2014 |
|
---|
2015 | /* we need to leave this object's write lock now because of
|
---|
2016 | * unregisterWithVirtualBox() that locks treeLock() for writing */
|
---|
2017 | alock.leave();
|
---|
2018 |
|
---|
2019 | /* try to remove from the list of known hard disks before performing actual
|
---|
2020 | * deletion (we favor the consistency of the media registry in the first
|
---|
2021 | * place which would have been broken if unregisterWithVirtualBox() failed
|
---|
2022 | * after we successfully deleted the storage) */
|
---|
2023 |
|
---|
2024 | rc = unregisterWithVirtualBox();
|
---|
2025 |
|
---|
2026 | alock.enter();
|
---|
2027 |
|
---|
2028 | /* restore the state because we may fail below; we will set it later again*/
|
---|
2029 | m.state = MediaState_Created;
|
---|
2030 |
|
---|
2031 | CheckComRCReturnRC (rc);
|
---|
2032 |
|
---|
2033 | ComObjPtr <Progress> progress;
|
---|
2034 |
|
---|
2035 | if (aProgress != NULL)
|
---|
2036 | {
|
---|
2037 | /* use the existing progress object... */
|
---|
2038 | progress = *aProgress;
|
---|
2039 |
|
---|
2040 | /* ...but create a new one if it is null */
|
---|
2041 | if (progress.isNull())
|
---|
2042 | {
|
---|
2043 | progress.createObject();
|
---|
2044 | rc = progress->init (mVirtualBox, static_cast <IHardDisk2 *> (this),
|
---|
2045 | BstrFmt (tr ("Deleting hard disk storage unit '%ls'"),
|
---|
2046 | name().raw()),
|
---|
2047 | FALSE /* aCancelable */);
|
---|
2048 | CheckComRCReturnRC (rc);
|
---|
2049 | }
|
---|
2050 | }
|
---|
2051 |
|
---|
2052 | std::auto_ptr <Task> task (new Task (this, progress, Task::Delete));
|
---|
2053 | AssertComRCReturnRC (task->autoCaller.rc());
|
---|
2054 |
|
---|
2055 | if (aWait)
|
---|
2056 | {
|
---|
2057 | /* go to Deleting state before starting the task */
|
---|
2058 | m.state = MediaState_Deleting;
|
---|
2059 |
|
---|
2060 | rc = task->runNow();
|
---|
2061 | }
|
---|
2062 | else
|
---|
2063 | {
|
---|
2064 | rc = task->startThread();
|
---|
2065 | CheckComRCReturnRC (rc);
|
---|
2066 |
|
---|
2067 | /* go to Deleting state before leaving the lock */
|
---|
2068 | m.state = MediaState_Deleting;
|
---|
2069 | }
|
---|
2070 |
|
---|
2071 | /* task is now owned (or already deleted) by taskThread() so release it */
|
---|
2072 | task.release();
|
---|
2073 |
|
---|
2074 | if (aProgress != NULL)
|
---|
2075 | {
|
---|
2076 | /* return progress to the caller */
|
---|
2077 | *aProgress = progress;
|
---|
2078 | }
|
---|
2079 |
|
---|
2080 | return rc;
|
---|
2081 | }
|
---|
2082 |
|
---|
2083 | /**
|
---|
2084 | * Creates a new differencing storage unit using the given target hard disk's
|
---|
2085 | * format and the location. Note that @c aTarget must be NotCreated.
|
---|
2086 | *
|
---|
2087 | * As opposed to the CreateDiffStorage() method, this method doesn't try to lock
|
---|
2088 | * this hard disk for reading assuming that the caller has already done so. This
|
---|
2089 | * is used when taking an online snaopshot (where all original hard disks are
|
---|
2090 | * locked for writing and must remain such). Note however that if @a aWait is
|
---|
2091 | * @c false and this method returns a success then the thread started by
|
---|
2092 | * this method will unlock the hard disk (unless it is in
|
---|
2093 | * MediaState_LockedWrite state) so make sure the hard disk is either in
|
---|
2094 | * MediaState_LockedWrite or call #LockRead() before calling this method! If @a
|
---|
2095 | * aWait is @c true then this method neither locks nor unlocks the hard disk, so
|
---|
2096 | * make sure you do it yourself as needed.
|
---|
2097 | *
|
---|
2098 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
2099 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
2100 | * otherwise the existing progress object is used. If @a aProgress is NULL, then no
|
---|
2101 | * progress object is created/used at all.
|
---|
2102 | *
|
---|
2103 | * When @a aWait is @c false, this method will create a thread to perform the
|
---|
2104 | * create operation asynchronously and will return immediately. Otherwise, it
|
---|
2105 | * will perform the operation on the calling thread and will not return to the
|
---|
2106 | * caller until the operation is completed. Note that @a aProgress cannot be
|
---|
2107 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
2108 | *
|
---|
2109 | * @param aTarget Target hard disk.
|
---|
2110 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
2111 | * completion.
|
---|
2112 | * @param aWait @c true if this method should block instead of creating
|
---|
2113 | * an asynchronous thread.
|
---|
2114 | *
|
---|
2115 | * @note Locks this object and @a aTarget for writing.
|
---|
2116 | */
|
---|
2117 | HRESULT HardDisk2::createDiffStorage (ComObjPtr <HardDisk2> &aTarget,
|
---|
2118 | ComObjPtr <Progress> *aProgress,
|
---|
2119 | bool aWait)
|
---|
2120 | {
|
---|
2121 | AssertReturn (!aTarget.isNull(), E_FAIL);
|
---|
2122 | AssertReturn (aProgress != NULL || aWait == true, E_FAIL);
|
---|
2123 |
|
---|
2124 | AutoCaller autoCaller (this);
|
---|
2125 | CheckComRCReturnRC (autoCaller.rc());
|
---|
2126 |
|
---|
2127 | AutoCaller targetCaller (aTarget);
|
---|
2128 | CheckComRCReturnRC (targetCaller.rc());
|
---|
2129 |
|
---|
2130 | AutoMultiWriteLock2 alock (this, aTarget);
|
---|
2131 |
|
---|
2132 | AssertReturn (mm.type != HardDiskType_Writethrough, E_FAIL);
|
---|
2133 |
|
---|
2134 | /* Note: MediaState_LockedWrite is ok when taking an online snapshot */
|
---|
2135 | AssertReturn (m.state == MediaState_LockedRead ||
|
---|
2136 | m.state == MediaState_LockedWrite, E_FAIL);
|
---|
2137 |
|
---|
2138 | if (aTarget->m.state != MediaState_NotCreated)
|
---|
2139 | return aTarget->setStateError();
|
---|
2140 |
|
---|
2141 | HRESULT rc = S_OK;
|
---|
2142 |
|
---|
2143 | /* check that the hard disk is not attached to any VM in the current state*/
|
---|
2144 | for (BackRefList::const_iterator it = m.backRefs.begin();
|
---|
2145 | it != m.backRefs.end(); ++ it)
|
---|
2146 | {
|
---|
2147 | if (it->inCurState)
|
---|
2148 | {
|
---|
2149 | /* Note: when a VM snapshot is being taken, all normal hard disks
|
---|
2150 | * attached to the VM in the current state will be, as an exception,
|
---|
2151 | * also associated with the snapshot which is about to create (see
|
---|
2152 | * SnapshotMachine::init()) before deassociating them from the
|
---|
2153 | * current state (which takes place only on success in
|
---|
2154 | * Machine::fixupHardDisks2()), so that the size of snapshotIds
|
---|
2155 | * will be 1 in this case. The given condition is used to filter out
|
---|
2156 | * this legal situatinon and do not report an error. */
|
---|
2157 |
|
---|
2158 | if (it->snapshotIds.size() == 0)
|
---|
2159 | {
|
---|
2160 | return setError (VBOX_E_INVALID_OBJECT_STATE,
|
---|
2161 | tr ("Hard disk '%ls' is attached to a virtual machine "
|
---|
2162 | "with UUID {%RTuuid}. No differencing hard disks "
|
---|
2163 | "based on it may be created until it is detached"),
|
---|
2164 | m.location.raw(), it->machineId.raw());
|
---|
2165 | }
|
---|
2166 |
|
---|
2167 | Assert (it->snapshotIds.size() == 1);
|
---|
2168 | }
|
---|
2169 | }
|
---|
2170 |
|
---|
2171 | ComObjPtr <Progress> progress;
|
---|
2172 |
|
---|
2173 | if (aProgress != NULL)
|
---|
2174 | {
|
---|
2175 | /* use the existing progress object... */
|
---|
2176 | progress = *aProgress;
|
---|
2177 |
|
---|
2178 | /* ...but create a new one if it is null */
|
---|
2179 | if (progress.isNull())
|
---|
2180 | {
|
---|
2181 | progress.createObject();
|
---|
2182 | rc = progress->init (mVirtualBox, static_cast <IHardDisk2 *> (this),
|
---|
2183 | BstrFmt (tr ("Creating differencing hard disk storage unit '%ls'"),
|
---|
2184 | aTarget->name().raw()),
|
---|
2185 | FALSE /* aCancelable */);
|
---|
2186 | CheckComRCReturnRC (rc);
|
---|
2187 | }
|
---|
2188 | }
|
---|
2189 |
|
---|
2190 | /* setup task object and thread to carry out the operation
|
---|
2191 | * asynchronously */
|
---|
2192 |
|
---|
2193 | std::auto_ptr <Task> task (new Task (this, progress, Task::CreateDiff));
|
---|
2194 | AssertComRCReturnRC (task->autoCaller.rc());
|
---|
2195 |
|
---|
2196 | task->setData (aTarget);
|
---|
2197 |
|
---|
2198 | /* register a task (it will deregister itself when done) */
|
---|
2199 | ++ mm.numCreateDiffTasks;
|
---|
2200 | Assert (mm.numCreateDiffTasks != 0); /* overflow? */
|
---|
2201 |
|
---|
2202 | if (aWait)
|
---|
2203 | {
|
---|
2204 | /* go to Creating state before starting the task */
|
---|
2205 | aTarget->m.state = MediaState_Creating;
|
---|
2206 |
|
---|
2207 | rc = task->runNow();
|
---|
2208 | }
|
---|
2209 | else
|
---|
2210 | {
|
---|
2211 | rc = task->startThread();
|
---|
2212 | CheckComRCReturnRC (rc);
|
---|
2213 |
|
---|
2214 | /* go to Creating state before leaving the lock */
|
---|
2215 | aTarget->m.state = MediaState_Creating;
|
---|
2216 | }
|
---|
2217 |
|
---|
2218 | /* task is now owned (or already deleted) by taskThread() so release it */
|
---|
2219 | task.release();
|
---|
2220 |
|
---|
2221 | if (aProgress != NULL)
|
---|
2222 | {
|
---|
2223 | /* return progress to the caller */
|
---|
2224 | *aProgress = progress;
|
---|
2225 | }
|
---|
2226 |
|
---|
2227 | return rc;
|
---|
2228 | }
|
---|
2229 |
|
---|
2230 | /**
|
---|
2231 | * Prepares this (source) hard disk, target hard disk and all intermediate hard
|
---|
2232 | * disks for the merge operation.
|
---|
2233 | *
|
---|
2234 | * This method is to be called prior to calling the #mergeTo() to perform
|
---|
2235 | * necessary consistency checks and place involved hard disks to appropriate
|
---|
2236 | * states. If #mergeTo() is not called or fails, the state modifications
|
---|
2237 | * performed by this method must be undone by #cancelMergeTo().
|
---|
2238 | *
|
---|
2239 | * Note that when @a aIgnoreAttachments is @c true then it's the caller's
|
---|
2240 | * responsibility to detach the source and all intermediate hard disks before
|
---|
2241 | * calling #mergeTo() (which will fail otherwise).
|
---|
2242 | *
|
---|
2243 | * See #mergeTo() for more information about merging.
|
---|
2244 | *
|
---|
2245 | * @param aTarget Target hard disk.
|
---|
2246 | * @param aChain Where to store the created merge chain.
|
---|
2247 | * @param aIgnoreAttachments Don't check if the source or any intermediate
|
---|
2248 | * hard disk is attached to any VM.
|
---|
2249 | *
|
---|
2250 | * @note Locks treeLock() for reading. Locks this object, aTarget and all
|
---|
2251 | * intermediate hard disks for writing.
|
---|
2252 | */
|
---|
2253 | HRESULT HardDisk2::prepareMergeTo (HardDisk2 *aTarget,
|
---|
2254 | MergeChain * &aChain,
|
---|
2255 | bool aIgnoreAttachments /*= false*/)
|
---|
2256 | {
|
---|
2257 | AssertReturn (aTarget != NULL, E_FAIL);
|
---|
2258 |
|
---|
2259 | AutoCaller autoCaller (this);
|
---|
2260 | AssertComRCReturnRC (autoCaller.rc());
|
---|
2261 |
|
---|
2262 | AutoCaller targetCaller (aTarget);
|
---|
2263 | AssertComRCReturnRC (targetCaller.rc());
|
---|
2264 |
|
---|
2265 | aChain = NULL;
|
---|
2266 |
|
---|
2267 | /* we walk the tree */
|
---|
2268 | AutoReadLock treeLock (this->treeLock());
|
---|
2269 |
|
---|
2270 | HRESULT rc = S_OK;
|
---|
2271 |
|
---|
2272 | /* detect the merge direction */
|
---|
2273 | bool forward;
|
---|
2274 | {
|
---|
2275 | HardDisk2 *parent = mParent;
|
---|
2276 | while (parent != NULL && parent != aTarget)
|
---|
2277 | parent = parent->mParent;
|
---|
2278 | if (parent == aTarget)
|
---|
2279 | forward = false;
|
---|
2280 | else
|
---|
2281 | {
|
---|
2282 | parent = aTarget->mParent;
|
---|
2283 | while (parent != NULL && parent != this)
|
---|
2284 | parent = parent->mParent;
|
---|
2285 | if (parent == this)
|
---|
2286 | forward = true;
|
---|
2287 | else
|
---|
2288 | {
|
---|
2289 | Bstr tgtLoc;
|
---|
2290 | {
|
---|
2291 | AutoReadLock alock (this);
|
---|
2292 | tgtLoc = aTarget->locationFull();
|
---|
2293 | }
|
---|
2294 |
|
---|
2295 | AutoReadLock alock (this);
|
---|
2296 | return setError (E_FAIL,
|
---|
2297 | tr ("Hard disks '%ls' and '%ls' are unrelated"),
|
---|
2298 | m.locationFull.raw(), tgtLoc.raw());
|
---|
2299 | }
|
---|
2300 | }
|
---|
2301 | }
|
---|
2302 |
|
---|
2303 | /* build the chain (will do necessary checks and state changes) */
|
---|
2304 | std::auto_ptr <MergeChain> chain (new MergeChain (forward,
|
---|
2305 | aIgnoreAttachments));
|
---|
2306 | {
|
---|
2307 | HardDisk2 *last = forward ? aTarget : this;
|
---|
2308 | HardDisk2 *first = forward ? this : aTarget;
|
---|
2309 |
|
---|
2310 | for (;;)
|
---|
2311 | {
|
---|
2312 | if (last == aTarget)
|
---|
2313 | rc = chain->addTarget (last);
|
---|
2314 | else if (last == this)
|
---|
2315 | rc = chain->addSource (last);
|
---|
2316 | else
|
---|
2317 | rc = chain->addIntermediate (last);
|
---|
2318 | CheckComRCReturnRC (rc);
|
---|
2319 |
|
---|
2320 | if (last == first)
|
---|
2321 | break;
|
---|
2322 |
|
---|
2323 | last = last->mParent;
|
---|
2324 | }
|
---|
2325 | }
|
---|
2326 |
|
---|
2327 | aChain = chain.release();
|
---|
2328 |
|
---|
2329 | return S_OK;
|
---|
2330 | }
|
---|
2331 |
|
---|
2332 | /**
|
---|
2333 | * Merges this hard disk to the specified hard disk which must be either its
|
---|
2334 | * direct ancestor or descendant.
|
---|
2335 | *
|
---|
2336 | * Given this hard disk is SOURCE and the specified hard disk is TARGET, we will
|
---|
2337 | * get two varians of the merge operation:
|
---|
2338 | *
|
---|
2339 | * forward merge
|
---|
2340 | * ------------------------->
|
---|
2341 | * [Extra] <- SOURCE <- Intermediate <- TARGET
|
---|
2342 | * Any Del Del LockWr
|
---|
2343 | *
|
---|
2344 | *
|
---|
2345 | * backward merge
|
---|
2346 | * <-------------------------
|
---|
2347 | * TARGET <- Intermediate <- SOURCE <- [Extra]
|
---|
2348 | * LockWr Del Del LockWr
|
---|
2349 | *
|
---|
2350 | * Each scheme shows the involved hard disks on the hard disk chain where
|
---|
2351 | * SOURCE and TARGET belong. Under each hard disk there is a state value which
|
---|
2352 | * the hard disk must have at a time of the mergeTo() call.
|
---|
2353 | *
|
---|
2354 | * The hard disks in the square braces may be absent (e.g. when the forward
|
---|
2355 | * operation takes place and SOURCE is the base hard disk, or when the backward
|
---|
2356 | * merge operation takes place and TARGET is the last child in the chain) but if
|
---|
2357 | * they present they are involved too as shown.
|
---|
2358 | *
|
---|
2359 | * Nor the source hard disk neither intermediate hard disks may be attached to
|
---|
2360 | * any VM directly or in the snapshot, otherwise this method will assert.
|
---|
2361 | *
|
---|
2362 | * The #prepareMergeTo() method must be called prior to this method to place all
|
---|
2363 | * involved to necessary states and perform other consistency checks.
|
---|
2364 | *
|
---|
2365 | * If @a aWait is @c true then this method will perform the operation on the
|
---|
2366 | * calling thread and will not return to the caller until the operation is
|
---|
2367 | * completed. When this method succeeds, all intermediate hard disk objects in
|
---|
2368 | * the chain will be uninitialized, the state of the target hard disk (and all
|
---|
2369 | * involved extra hard disks) will be restored and @a aChain will be deleted.
|
---|
2370 | * Note that this (source) hard disk is not uninitialized because of possible
|
---|
2371 | * AutoCaller instances held by the caller of this method on the current thread.
|
---|
2372 | * It's therefore the responsibility of the caller to call HardDisk2::uninit()
|
---|
2373 | * after releasing all callers in this case!
|
---|
2374 | *
|
---|
2375 | * If @a aWait is @c false then this method will crea,te a thread to perform the
|
---|
2376 | * create operation asynchronously and will return immediately. If the operation
|
---|
2377 | * succeeds, the thread will uninitialize the source hard disk object and all
|
---|
2378 | * intermediate hard disk objects in the chain, reset the state of the target
|
---|
2379 | * hard disk (and all involved extra hard disks) and delete @a aChain. If the
|
---|
2380 | * operation fails, the thread will only reset the states of all involved hard
|
---|
2381 | * disks and delete @a aChain.
|
---|
2382 | *
|
---|
2383 | * When this method fails (regardless of the @a aWait mode), it is a caller's
|
---|
2384 | * responsiblity to undo state changes and delete @a aChain using
|
---|
2385 | * #cancelMergeTo().
|
---|
2386 | *
|
---|
2387 | * If @a aProgress is not NULL but the object it points to is @c null then a new
|
---|
2388 | * progress object will be created and assigned to @a *aProgress on success,
|
---|
2389 | * otherwise the existing progress object is used. If Progress is NULL, then no
|
---|
2390 | * progress object is created/used at all. Note that @a aProgress cannot be
|
---|
2391 | * NULL when @a aWait is @c false (this method will assert in this case).
|
---|
2392 | *
|
---|
2393 | * @param aChain Merge chain created by #prepareMergeTo().
|
---|
2394 | * @param aProgress Where to find/store a Progress object to track operation
|
---|
2395 | * completion.
|
---|
2396 | * @param aWait @c true if this method should block instead of creating
|
---|
2397 | * an asynchronous thread.
|
---|
2398 | *
|
---|
2399 | * @note Locks the branch lock for writing. Locks the hard disks from the chain
|
---|
2400 | * for writing.
|
---|
2401 | */
|
---|
2402 | HRESULT HardDisk2::mergeTo (MergeChain *aChain,
|
---|
2403 | ComObjPtr <Progress> *aProgress,
|
---|
2404 | bool aWait)
|
---|
2405 | {
|
---|
2406 | AssertReturn (aChain != NULL, E_FAIL);
|
---|
2407 | AssertReturn (aProgress != NULL || aWait == true, E_FAIL);
|
---|
2408 |
|
---|
2409 | AutoCaller autoCaller (this);
|
---|
2410 | CheckComRCReturnRC (autoCaller.rc());
|
---|
2411 |
|
---|
2412 | HRESULT rc = S_OK;
|
---|
2413 |
|
---|
2414 | ComObjPtr <Progress> progress;
|
---|
2415 |
|
---|
2416 | if (aProgress != NULL)
|
---|
2417 | {
|
---|
2418 | /* use the existing progress object... */
|
---|
2419 | progress = *aProgress;
|
---|
2420 |
|
---|
2421 | /* ...but create a new one if it is null */
|
---|
2422 | if (progress.isNull())
|
---|
2423 | {
|
---|
2424 | AutoReadLock alock (this);
|
---|
2425 |
|
---|
2426 | progress.createObject();
|
---|
2427 | rc = progress->init (mVirtualBox, static_cast <IHardDisk2 *> (this),
|
---|
2428 | BstrFmt (tr ("Merging hard disk '%ls' to '%ls'"),
|
---|
2429 | name().raw(), aChain->target()->name().raw()),
|
---|
2430 | FALSE /* aCancelable */);
|
---|
2431 | CheckComRCReturnRC (rc);
|
---|
2432 | }
|
---|
2433 | }
|
---|
2434 |
|
---|
2435 | /* setup task object and thread to carry out the operation
|
---|
2436 | * asynchronously */
|
---|
2437 |
|
---|
2438 | std::auto_ptr <Task> task (new Task (this, progress, Task::Merge));
|
---|
2439 | AssertComRCReturnRC (task->autoCaller.rc());
|
---|
2440 |
|
---|
2441 | task->setData (aChain);
|
---|
2442 |
|
---|
2443 | /* Note: task owns aChain (will delete it when not needed) in all cases
|
---|
2444 | * except when @a aWait is @c true and runNow() fails -- in this case
|
---|
2445 | * aChain will be left away because cancelMergeTo() will be applied by the
|
---|
2446 | * caller on it as it is required in the documentation above */
|
---|
2447 |
|
---|
2448 | if (aWait)
|
---|
2449 | {
|
---|
2450 | rc = task->runNow();
|
---|
2451 | }
|
---|
2452 | else
|
---|
2453 | {
|
---|
2454 | rc = task->startThread();
|
---|
2455 | CheckComRCReturnRC (rc);
|
---|
2456 | }
|
---|
2457 |
|
---|
2458 | /* task is now owned (or already deleted) by taskThread() so release it */
|
---|
2459 | task.release();
|
---|
2460 |
|
---|
2461 | if (aProgress != NULL)
|
---|
2462 | {
|
---|
2463 | /* return progress to the caller */
|
---|
2464 | *aProgress = progress;
|
---|
2465 | }
|
---|
2466 |
|
---|
2467 | return rc;
|
---|
2468 | }
|
---|
2469 |
|
---|
2470 | /**
|
---|
2471 | * Undoes what #prepareMergeTo() did. Must be called if #mergeTo() is not called
|
---|
2472 | * or fails. Frees memory occupied by @a aChain.
|
---|
2473 | *
|
---|
2474 | * @param aChain Merge chain created by #prepareMergeTo().
|
---|
2475 | *
|
---|
2476 | * @note Locks the hard disks from the chain for writing.
|
---|
2477 | */
|
---|
2478 | void HardDisk2::cancelMergeTo (MergeChain *aChain)
|
---|
2479 | {
|
---|
2480 | AutoCaller autoCaller (this);
|
---|
2481 | AssertComRCReturnVoid (autoCaller.rc());
|
---|
2482 |
|
---|
2483 | AssertReturnVoid (aChain != NULL);
|
---|
2484 |
|
---|
2485 | /* the destructor will do the thing */
|
---|
2486 | delete aChain;
|
---|
2487 | }
|
---|
2488 |
|
---|
2489 | // private methods
|
---|
2490 | ////////////////////////////////////////////////////////////////////////////////
|
---|
2491 |
|
---|
2492 | /**
|
---|
2493 | * Sets the value of m.location and calculates the value of m.locationFull.
|
---|
2494 | *
|
---|
2495 | * Reimplements MediumBase::setLocation() to specially treat non-FS-path
|
---|
2496 | * locations and to prepend the default hard disk folder if the given location
|
---|
2497 | * string does not contain any path information at all.
|
---|
2498 | *
|
---|
2499 | * Also, if the specified location is a file path that ends with '/' then the
|
---|
2500 | * file name part will be generated by this method automatically in the format
|
---|
2501 | * '{<uuid>}.<ext>' where <uuid> is a fresh UUID that this method will generate
|
---|
2502 | * and assign to this medium, and <ext> is the default extension for this
|
---|
2503 | * medium's storage format. Note that this procedure requires the media state to
|
---|
2504 | * be NotCreated and will return a faiulre otherwise.
|
---|
2505 | *
|
---|
2506 | * @param aLocation Location of the storage unit. If the locaiton is a FS-path,
|
---|
2507 | * then it can be relative to the VirtualBox home directory.
|
---|
2508 | *
|
---|
2509 | * @note Must be called from under this object's write lock.
|
---|
2510 | */
|
---|
2511 | HRESULT HardDisk2::setLocation (CBSTR aLocation)
|
---|
2512 | {
|
---|
2513 | /// @todo so far, we assert but later it makes sense to support null
|
---|
2514 | /// locations for hard disks that are not yet created fail to create a
|
---|
2515 | /// storage unit instead
|
---|
2516 | CheckComArgStrNotEmptyOrNull (aLocation);
|
---|
2517 |
|
---|
2518 | AutoCaller autoCaller (this);
|
---|
2519 | AssertComRCReturnRC (autoCaller.rc());
|
---|
2520 |
|
---|
2521 | /* formatObj may be null only when initializing from an existing path and
|
---|
2522 | * no format is known yet */
|
---|
2523 | AssertReturn ((!mm.format.isNull() && !mm.formatObj.isNull()) ||
|
---|
2524 | (autoCaller.state() == InInit &&
|
---|
2525 | m.state != MediaState_NotCreated && m.id.isEmpty() &&
|
---|
2526 | mm.format.isNull() && mm.formatObj.isNull()),
|
---|
2527 | E_FAIL);
|
---|
2528 |
|
---|
2529 | /* are we dealing with a new hard disk constructed using the existing
|
---|
2530 | * location? */
|
---|
2531 | bool isImport = mm.format.isNull();
|
---|
2532 |
|
---|
2533 | if (isImport ||
|
---|
2534 | (mm.formatObj->capabilities() & HardDiskFormatCapabilities_File))
|
---|
2535 | {
|
---|
2536 | Guid id;
|
---|
2537 |
|
---|
2538 | Utf8Str location (aLocation);
|
---|
2539 |
|
---|
2540 | if (m.state == MediaState_NotCreated)
|
---|
2541 | {
|
---|
2542 | /* must be a file (formatObj must be already known) */
|
---|
2543 | Assert (mm.formatObj->capabilities() & HardDiskFormatCapabilities_File);
|
---|
2544 |
|
---|
2545 | if (RTPathFilename (location) == NULL)
|
---|
2546 | {
|
---|
2547 | /* no file name is given (either an empty string or ends with a
|
---|
2548 | * slash), generate a new UUID + file name if the state allows
|
---|
2549 | * this */
|
---|
2550 |
|
---|
2551 | ComAssertMsgRet (!mm.formatObj->fileExtensions().empty(),
|
---|
2552 | ("Must be at least one extension if it is "
|
---|
2553 | "HardDiskFormatCapabilities_File\n"),
|
---|
2554 | E_FAIL);
|
---|
2555 |
|
---|
2556 | Bstr ext = mm.formatObj->fileExtensions().front();
|
---|
2557 | ComAssertMsgRet (!ext.isEmpty(),
|
---|
2558 | ("Default extension must not be empty\n"),
|
---|
2559 | E_FAIL);
|
---|
2560 |
|
---|
2561 | id.create();
|
---|
2562 |
|
---|
2563 | location = Utf8StrFmt ("%s{%RTuuid}.%ls",
|
---|
2564 | location.raw(), id.raw(), ext.raw());
|
---|
2565 | }
|
---|
2566 | }
|
---|
2567 |
|
---|
2568 | /* append the default folder if no path is given */
|
---|
2569 | if (!RTPathHavePath (location))
|
---|
2570 | {
|
---|
2571 | AutoReadLock propsLock (mVirtualBox->systemProperties());
|
---|
2572 | location = Utf8StrFmt ("%ls%c%s",
|
---|
2573 | mVirtualBox->systemProperties()->defaultHardDiskFolder().raw(),
|
---|
2574 | RTPATH_DELIMITER,
|
---|
2575 | location.raw());
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 | /* get the full file name */
|
---|
2579 | Utf8Str locationFull;
|
---|
2580 | int vrc = mVirtualBox->calculateFullPath (location, locationFull);
|
---|
2581 | if (RT_FAILURE (vrc))
|
---|
2582 | return setError (VBOX_E_FILE_ERROR,
|
---|
2583 | tr ("Invalid hard disk storage file location '%s' (%Rrc)"),
|
---|
2584 | location.raw(), vrc);
|
---|
2585 |
|
---|
2586 | /* detect the backend from the storage unit if importing */
|
---|
2587 | if (isImport)
|
---|
2588 | {
|
---|
2589 | char *backendName = NULL;
|
---|
2590 |
|
---|
2591 | /* is it a file? */
|
---|
2592 | {
|
---|
2593 | RTFILE file;
|
---|
2594 | vrc = RTFileOpen (&file, locationFull, RTFILE_O_READ);
|
---|
2595 | if (RT_SUCCESS (vrc))
|
---|
2596 | RTFileClose (file);
|
---|
2597 | }
|
---|
2598 | if (RT_SUCCESS (vrc))
|
---|
2599 | {
|
---|
2600 | vrc = VDGetFormat (locationFull, &backendName);
|
---|
2601 | }
|
---|
2602 | else if (vrc != VERR_FILE_NOT_FOUND && vrc != VERR_PATH_NOT_FOUND)
|
---|
2603 | {
|
---|
2604 | /* assume it's not a file, restore the original location */
|
---|
2605 | location = locationFull = aLocation;
|
---|
2606 | vrc = VDGetFormat (locationFull, &backendName);
|
---|
2607 | }
|
---|
2608 |
|
---|
2609 | if (RT_FAILURE (vrc))
|
---|
2610 | return setError (VBOX_E_IPRT_ERROR,
|
---|
2611 | tr ("Could not get the storage format of the hard disk "
|
---|
2612 | "'%s' (%Rrc)"), locationFull.raw(), vrc);
|
---|
2613 |
|
---|
2614 | ComAssertRet (backendName != NULL && *backendName != '\0', E_FAIL);
|
---|
2615 |
|
---|
2616 | HRESULT rc = setFormat (Bstr (backendName));
|
---|
2617 | RTStrFree (backendName);
|
---|
2618 |
|
---|
2619 | /* setFormat() must not fail since we've just used the backend so
|
---|
2620 | * the format object must be there */
|
---|
2621 | AssertComRCReturnRC (rc);
|
---|
2622 | }
|
---|
2623 |
|
---|
2624 | /* is it still a file? */
|
---|
2625 | if (mm.formatObj->capabilities() & HardDiskFormatCapabilities_File)
|
---|
2626 | {
|
---|
2627 | m.location = location;
|
---|
2628 | m.locationFull = locationFull;
|
---|
2629 |
|
---|
2630 | if (m.state == MediaState_NotCreated)
|
---|
2631 | {
|
---|
2632 | /* assign a new UUID (this UUID will be used when calling
|
---|
2633 | * VDCreateBase/VDCreateDiff as a wanted UUID). Note that we
|
---|
2634 | * also do that if we didn't generate it to make sure it is
|
---|
2635 | * either generated by us or reset to null */
|
---|
2636 | unconst (m.id) = id;
|
---|
2637 | }
|
---|
2638 | }
|
---|
2639 | else
|
---|
2640 | {
|
---|
2641 | m.location = locationFull;
|
---|
2642 | m.locationFull = locationFull;
|
---|
2643 | }
|
---|
2644 | }
|
---|
2645 | else
|
---|
2646 | {
|
---|
2647 | m.location = aLocation;
|
---|
2648 | m.locationFull = aLocation;
|
---|
2649 | }
|
---|
2650 |
|
---|
2651 | return S_OK;
|
---|
2652 | }
|
---|
2653 |
|
---|
2654 | /**
|
---|
2655 | * Checks that the format ID is valid and sets it on success.
|
---|
2656 | *
|
---|
2657 | * Note that this method will caller-reference the format object on success!
|
---|
2658 | * This reference must be released somewhere to let the HardDiskFormat object be
|
---|
2659 | * uninitialized.
|
---|
2660 | *
|
---|
2661 | * @note Must be called from under this object's write lock.
|
---|
2662 | */
|
---|
2663 | HRESULT HardDisk2::setFormat (CBSTR aFormat)
|
---|
2664 | {
|
---|
2665 | /* get the format object first */
|
---|
2666 | {
|
---|
2667 | AutoReadLock propsLock (mVirtualBox->systemProperties());
|
---|
2668 |
|
---|
2669 | unconst (mm.formatObj)
|
---|
2670 | = mVirtualBox->systemProperties()->hardDiskFormat (aFormat);
|
---|
2671 | if (mm.formatObj.isNull())
|
---|
2672 | return setError (E_INVALIDARG,
|
---|
2673 | tr ("Invalid hard disk storage format '%ls'"), aFormat);
|
---|
2674 |
|
---|
2675 | /* reference the format permanently to prevent its unexpected
|
---|
2676 | * uninitialization */
|
---|
2677 | HRESULT rc = mm.formatObj->addCaller();
|
---|
2678 | AssertComRCReturnRC (rc);
|
---|
2679 |
|
---|
2680 | /* get properties (preinsert them as keys in the map). Note that the
|
---|
2681 | * map doesn't grow over the object life time since the set of
|
---|
2682 | * properties is meant to be constant. */
|
---|
2683 |
|
---|
2684 | Assert (mm.properties.empty());
|
---|
2685 |
|
---|
2686 | for (HardDiskFormat::PropertyList::const_iterator it =
|
---|
2687 | mm.formatObj->properties().begin();
|
---|
2688 | it != mm.formatObj->properties().end();
|
---|
2689 | ++ it)
|
---|
2690 | {
|
---|
2691 | mm.properties.insert (std::make_pair (it->name, Bstr::Null));
|
---|
2692 | }
|
---|
2693 | }
|
---|
2694 |
|
---|
2695 | unconst (mm.format) = aFormat;
|
---|
2696 |
|
---|
2697 | return S_OK;
|
---|
2698 | }
|
---|
2699 |
|
---|
2700 | /**
|
---|
2701 | * Queries information from the image file.
|
---|
2702 | *
|
---|
2703 | * As a result of this call, the accessibility state and data members such as
|
---|
2704 | * size and description will be updated with the current information.
|
---|
2705 | *
|
---|
2706 | * Reimplements MediumBase::queryInfo() to query hard disk information using the
|
---|
2707 | * VD backend interface.
|
---|
2708 | *
|
---|
2709 | * @note This method may block during a system I/O call that checks storage
|
---|
2710 | * accessibility.
|
---|
2711 | *
|
---|
2712 | * @note Locks treeLock() for reading and writing (for new diff media checked
|
---|
2713 | * for the first time). Locks mParent for reading. Locks this object for
|
---|
2714 | * writing.
|
---|
2715 | */
|
---|
2716 | HRESULT HardDisk2::queryInfo()
|
---|
2717 | {
|
---|
2718 | AutoWriteLock alock (this);
|
---|
2719 |
|
---|
2720 | AssertReturn (m.state == MediaState_Created ||
|
---|
2721 | m.state == MediaState_Inaccessible ||
|
---|
2722 | m.state == MediaState_LockedRead ||
|
---|
2723 | m.state == MediaState_LockedWrite,
|
---|
2724 | E_FAIL);
|
---|
2725 |
|
---|
2726 | HRESULT rc = S_OK;
|
---|
2727 |
|
---|
2728 | int vrc = VINF_SUCCESS;
|
---|
2729 |
|
---|
2730 | /* check if a blocking queryInfo() call is in progress on some other thread,
|
---|
2731 | * and wait for it to finish if so instead of querying data ourselves */
|
---|
2732 | if (m.queryInfoSem != NIL_RTSEMEVENTMULTI)
|
---|
2733 | {
|
---|
2734 | Assert (m.state == MediaState_LockedRead);
|
---|
2735 |
|
---|
2736 | ++ m.queryInfoCallers;
|
---|
2737 | alock.leave();
|
---|
2738 |
|
---|
2739 | vrc = RTSemEventMultiWait (m.queryInfoSem, RT_INDEFINITE_WAIT);
|
---|
2740 |
|
---|
2741 | alock.enter();
|
---|
2742 | -- m.queryInfoCallers;
|
---|
2743 |
|
---|
2744 | if (m.queryInfoCallers == 0)
|
---|
2745 | {
|
---|
2746 | /* last waiting caller deletes the semaphore */
|
---|
2747 | RTSemEventMultiDestroy (m.queryInfoSem);
|
---|
2748 | m.queryInfoSem = NIL_RTSEMEVENTMULTI;
|
---|
2749 | }
|
---|
2750 |
|
---|
2751 | AssertRC (vrc);
|
---|
2752 |
|
---|
2753 | return S_OK;
|
---|
2754 | }
|
---|
2755 |
|
---|
2756 | /* lazily create a semaphore for possible callers */
|
---|
2757 | vrc = RTSemEventMultiCreate (&m.queryInfoSem);
|
---|
2758 | ComAssertRCRet (vrc, E_FAIL);
|
---|
2759 |
|
---|
2760 | bool tempStateSet = false;
|
---|
2761 | if (m.state != MediaState_LockedRead &&
|
---|
2762 | m.state != MediaState_LockedWrite)
|
---|
2763 | {
|
---|
2764 | /* Cause other methods to prevent any modifications before leaving the
|
---|
2765 | * lock. Note that clients will never see this temporary state change
|
---|
2766 | * since any COMGETTER(State) is (or will be) blocked until we finish
|
---|
2767 | * and restore the actual state. */
|
---|
2768 | m.state = MediaState_LockedRead;
|
---|
2769 | tempStateSet = true;
|
---|
2770 | }
|
---|
2771 |
|
---|
2772 | /* leave the lock before a blocking operation */
|
---|
2773 | alock.leave();
|
---|
2774 |
|
---|
2775 | bool success = false;
|
---|
2776 | Utf8Str lastAccessError;
|
---|
2777 |
|
---|
2778 | try
|
---|
2779 | {
|
---|
2780 | Utf8Str location (m.locationFull);
|
---|
2781 |
|
---|
2782 | /* are we dealing with a new hard disk constructed using the existing
|
---|
2783 | * location? */
|
---|
2784 | bool isImport = m.id.isEmpty();
|
---|
2785 |
|
---|
2786 | PVBOXHDD hdd;
|
---|
2787 | vrc = VDCreate (mm.vdDiskIfaces, &hdd);
|
---|
2788 | ComAssertRCThrow (vrc, E_FAIL);
|
---|
2789 |
|
---|
2790 | try
|
---|
2791 | {
|
---|
2792 | unsigned flags = VD_OPEN_FLAGS_INFO;
|
---|
2793 |
|
---|
2794 | /* Note that we don't use VD_OPEN_FLAGS_READONLY when opening new
|
---|
2795 | * hard disks because that would prevent necessary modifications
|
---|
2796 | * when opening hard disks of some third-party formats for the first
|
---|
2797 | * time in VirtualBox (such as VMDK for which VDOpen() needs to
|
---|
2798 | * generate an UUID if it is missing) */
|
---|
2799 | if (!isImport)
|
---|
2800 | flags |= VD_OPEN_FLAGS_READONLY;
|
---|
2801 |
|
---|
2802 | vrc = VDOpen (hdd, Utf8Str (mm.format), location, flags,
|
---|
2803 | mm.vdDiskIfaces);
|
---|
2804 | if (RT_FAILURE (vrc))
|
---|
2805 | {
|
---|
2806 | lastAccessError = Utf8StrFmt (
|
---|
2807 | tr ("Could not open the hard disk '%ls'%s"),
|
---|
2808 | m.locationFull.raw(), vdError (vrc).raw());
|
---|
2809 | throw S_OK;
|
---|
2810 | }
|
---|
2811 |
|
---|
2812 | if (mm.formatObj->capabilities() & HardDiskFormatCapabilities_Uuid)
|
---|
2813 | {
|
---|
2814 | /* check the UUID */
|
---|
2815 | RTUUID uuid;
|
---|
2816 | vrc = VDGetUuid (hdd, 0, &uuid);
|
---|
2817 | ComAssertRCThrow (vrc, E_FAIL);
|
---|
2818 |
|
---|
2819 | if (isImport)
|
---|
2820 | {
|
---|
2821 | unconst (m.id) = uuid;
|
---|
2822 | }
|
---|
2823 | else
|
---|
2824 | {
|
---|
2825 | Assert (!m.id.isEmpty());
|
---|
2826 |
|
---|
2827 | if (m.id != uuid)
|
---|
2828 | {
|
---|
2829 | lastAccessError = Utf8StrFmt (
|
---|
2830 | tr ("UUID {%RTuuid} of the hard disk '%ls' does "
|
---|
2831 | "not match the value {%RTuuid} stored in the "
|
---|
2832 | "media registry ('%ls')"),
|
---|
2833 | &uuid, m.locationFull.raw(), m.id.raw(),
|
---|
2834 | mVirtualBox->settingsFileName().raw());
|
---|
2835 | throw S_OK;
|
---|
2836 | }
|
---|
2837 | }
|
---|
2838 | }
|
---|
2839 | else
|
---|
2840 | {
|
---|
2841 | /* the backend does not support storing UUIDs within the
|
---|
2842 | * underlying storage so use what we store in XML */
|
---|
2843 |
|
---|
2844 | /* generate an UUID for an imported UUID-less hard disk */
|
---|
2845 | if (isImport)
|
---|
2846 | unconst (m.id).create();
|
---|
2847 | }
|
---|
2848 |
|
---|
2849 | /* check the type */
|
---|
2850 | VDIMAGETYPE type;
|
---|
2851 | vrc = VDGetImageType (hdd, 0, &type);
|
---|
2852 | ComAssertRCThrow (vrc, E_FAIL);
|
---|
2853 |
|
---|
2854 | if (type == VD_IMAGE_TYPE_DIFF)
|
---|
2855 | {
|
---|
2856 | RTUUID parentId;
|
---|
2857 | vrc = VDGetParentUuid (hdd, 0, &parentId);
|
---|
2858 | ComAssertRCThrow (vrc, E_FAIL);
|
---|
2859 |
|
---|
2860 | if (isImport)
|
---|
2861 | {
|
---|
2862 | /* the parent must be known to us. Note that we freely
|
---|
2863 | * call locking methods of mVirtualBox and parent from the
|
---|
2864 | * write lock (breaking the {parent,child} lock order)
|
---|
2865 | * because there may be no concurrent access to the just
|
---|
2866 | * opened hard disk on ther threads yet (and init() will
|
---|
2867 | * fail if this method reporst MediaState_Inaccessible) */
|
---|
2868 |
|
---|
2869 | Guid id = parentId;
|
---|
2870 | ComObjPtr <HardDisk2> parent;
|
---|
2871 | rc = mVirtualBox->findHardDisk2 (&id, NULL,
|
---|
2872 | false /* aSetError */,
|
---|
2873 | &parent);
|
---|
2874 | if (FAILED (rc))
|
---|
2875 | {
|
---|
2876 | lastAccessError = Utf8StrFmt (
|
---|
2877 | tr ("Parent hard disk with UUID {%RTuuid} of the "
|
---|
2878 | "hard disk '%ls' is not found in the media "
|
---|
2879 | "registry ('%ls')"),
|
---|
2880 | &parentId, m.locationFull.raw(),
|
---|
2881 | mVirtualBox->settingsFileName().raw());
|
---|
2882 | throw S_OK;
|
---|
2883 | }
|
---|
2884 |
|
---|
2885 | /* deassociate from VirtualBox, associate with parent */
|
---|
2886 |
|
---|
2887 | mVirtualBox->removeDependentChild (this);
|
---|
2888 |
|
---|
2889 | /* we set mParent & children() */
|
---|
2890 | AutoWriteLock treeLock (this->treeLock());
|
---|
2891 |
|
---|
2892 | Assert (mParent.isNull());
|
---|
2893 | mParent = parent;
|
---|
2894 | mParent->addDependentChild (this);
|
---|
2895 | }
|
---|
2896 | else
|
---|
2897 | {
|
---|
2898 | /* we access mParent */
|
---|
2899 | AutoReadLock treeLock (this->treeLock());
|
---|
2900 |
|
---|
2901 | /* check that parent UUIDs match. Note that there's no need
|
---|
2902 | * for the parent's AutoCaller (our lifetime is bound to
|
---|
2903 | * it) */
|
---|
2904 |
|
---|
2905 | if (mParent.isNull())
|
---|
2906 | {
|
---|
2907 | lastAccessError = Utf8StrFmt (
|
---|
2908 | tr ("Hard disk '%ls' is differencing but it is not "
|
---|
2909 | "associated with any parent hard disk in the "
|
---|
2910 | "media registry ('%ls')"),
|
---|
2911 | m.locationFull.raw(),
|
---|
2912 | mVirtualBox->settingsFileName().raw());
|
---|
2913 | throw S_OK;
|
---|
2914 | }
|
---|
2915 |
|
---|
2916 | AutoReadLock parentLock (mParent);
|
---|
2917 | if (mParent->state() != MediaState_Inaccessible &&
|
---|
2918 | mParent->id() != parentId)
|
---|
2919 | {
|
---|
2920 | lastAccessError = Utf8StrFmt (
|
---|
2921 | tr ("Parent UUID {%RTuuid} of the hard disk '%ls' "
|
---|
2922 | "does not match UUID {%RTuuid} of its parent "
|
---|
2923 | "hard disk stored in the media registry ('%ls')"),
|
---|
2924 | &parentId, m.locationFull.raw(),
|
---|
2925 | mParent->id().raw(),
|
---|
2926 | mVirtualBox->settingsFileName().raw());
|
---|
2927 | throw S_OK;
|
---|
2928 | }
|
---|
2929 |
|
---|
2930 | /// @todo NEWMEDIA what to do if the parent is not
|
---|
2931 | /// accessible while the diff is? Probably, nothing. The
|
---|
2932 | /// real code will detect the mismatch anyway.
|
---|
2933 | }
|
---|
2934 | }
|
---|
2935 |
|
---|
2936 | m.size = VDGetFileSize (hdd, 0);
|
---|
2937 | mm.logicalSize = VDGetSize (hdd, 0) / _1M;
|
---|
2938 |
|
---|
2939 | success = true;
|
---|
2940 | }
|
---|
2941 | catch (HRESULT aRC)
|
---|
2942 | {
|
---|
2943 | rc = aRC;
|
---|
2944 | }
|
---|
2945 |
|
---|
2946 | VDDestroy (hdd);
|
---|
2947 |
|
---|
2948 | }
|
---|
2949 | catch (HRESULT aRC)
|
---|
2950 | {
|
---|
2951 | rc = aRC;
|
---|
2952 | }
|
---|
2953 |
|
---|
2954 | alock.enter();
|
---|
2955 |
|
---|
2956 | if (success)
|
---|
2957 | m.lastAccessError.setNull();
|
---|
2958 | else
|
---|
2959 | {
|
---|
2960 | m.lastAccessError = lastAccessError;
|
---|
2961 | LogWarningFunc (("'%ls' is not accessible (error='%ls', "
|
---|
2962 | "rc=%Rhrc, vrc=%Rrc)\n",
|
---|
2963 | m.locationFull.raw(), m.lastAccessError.raw(),
|
---|
2964 | rc, vrc));
|
---|
2965 | }
|
---|
2966 |
|
---|
2967 | /* inform other callers if there are any */
|
---|
2968 | if (m.queryInfoCallers > 0)
|
---|
2969 | {
|
---|
2970 | RTSemEventMultiSignal (m.queryInfoSem);
|
---|
2971 | }
|
---|
2972 | else
|
---|
2973 | {
|
---|
2974 | /* delete the semaphore ourselves */
|
---|
2975 | RTSemEventMultiDestroy (m.queryInfoSem);
|
---|
2976 | m.queryInfoSem = NIL_RTSEMEVENTMULTI;
|
---|
2977 | }
|
---|
2978 |
|
---|
2979 | if (tempStateSet)
|
---|
2980 | {
|
---|
2981 | /* Set the proper state according to the result of the check */
|
---|
2982 | if (success)
|
---|
2983 | m.state = MediaState_Created;
|
---|
2984 | else
|
---|
2985 | m.state = MediaState_Inaccessible;
|
---|
2986 | }
|
---|
2987 | else
|
---|
2988 | {
|
---|
2989 | /* we're locked, use a special field to store the result */
|
---|
2990 | m.accessibleInLock = success;
|
---|
2991 | }
|
---|
2992 |
|
---|
2993 | return rc;
|
---|
2994 | }
|
---|
2995 |
|
---|
2996 | /**
|
---|
2997 | * @note Called from this object's AutoMayUninitSpan and from under mVirtualBox
|
---|
2998 | * write lock.
|
---|
2999 | *
|
---|
3000 | * @note Locks treeLock() for reading.
|
---|
3001 | */
|
---|
3002 | HRESULT HardDisk2::canClose()
|
---|
3003 | {
|
---|
3004 | /* we access children */
|
---|
3005 | AutoReadLock treeLock (this->treeLock());
|
---|
3006 |
|
---|
3007 | if (children().size() != 0)
|
---|
3008 | return setError (E_FAIL,
|
---|
3009 | tr ("Hard disk '%ls' has %d child hard disks"),
|
---|
3010 | children().size());
|
---|
3011 |
|
---|
3012 | return S_OK;
|
---|
3013 | }
|
---|
3014 |
|
---|
3015 | /**
|
---|
3016 | * @note Called from within this object's AutoWriteLock.
|
---|
3017 | */
|
---|
3018 | HRESULT HardDisk2::canAttach (const Guid &aMachineId,
|
---|
3019 | const Guid &aSnapshotId)
|
---|
3020 | {
|
---|
3021 | if (mm.numCreateDiffTasks > 0)
|
---|
3022 | return setError (E_FAIL,
|
---|
3023 | tr ("One or more differencing child hard disks are "
|
---|
3024 | "being created for the hard disk '%ls' (%u)"),
|
---|
3025 | m.locationFull.raw(), mm.numCreateDiffTasks);
|
---|
3026 |
|
---|
3027 | return S_OK;
|
---|
3028 | }
|
---|
3029 |
|
---|
3030 | /**
|
---|
3031 | * @note Called from within this object's AutoMayUninitSpan (or AutoCaller) and
|
---|
3032 | * from under mVirtualBox write lock.
|
---|
3033 | *
|
---|
3034 | * @note Locks treeLock() for writing.
|
---|
3035 | */
|
---|
3036 | HRESULT HardDisk2::unregisterWithVirtualBox()
|
---|
3037 | {
|
---|
3038 | /* Note that we need to de-associate ourselves from the parent to let
|
---|
3039 | * unregisterHardDisk2() properly save the registry */
|
---|
3040 |
|
---|
3041 | /* we modify mParent and access children */
|
---|
3042 | AutoWriteLock treeLock (this->treeLock());
|
---|
3043 |
|
---|
3044 | const ComObjPtr <HardDisk2, ComWeakRef> parent = mParent;
|
---|
3045 |
|
---|
3046 | AssertReturn (children().size() == 0, E_FAIL);
|
---|
3047 |
|
---|
3048 | if (!mParent.isNull())
|
---|
3049 | {
|
---|
3050 | /* deassociate from the parent, associate with VirtualBox */
|
---|
3051 | mVirtualBox->addDependentChild (this);
|
---|
3052 | mParent->removeDependentChild (this);
|
---|
3053 | mParent.setNull();
|
---|
3054 | }
|
---|
3055 |
|
---|
3056 | HRESULT rc = mVirtualBox->unregisterHardDisk2 (this);
|
---|
3057 |
|
---|
3058 | if (FAILED (rc))
|
---|
3059 | {
|
---|
3060 | if (!parent.isNull())
|
---|
3061 | {
|
---|
3062 | /* re-associate with the parent as we are still relatives in the
|
---|
3063 | * registry */
|
---|
3064 | mParent = parent;
|
---|
3065 | mParent->addDependentChild (this);
|
---|
3066 | mVirtualBox->removeDependentChild (this);
|
---|
3067 | }
|
---|
3068 | }
|
---|
3069 |
|
---|
3070 | return rc;
|
---|
3071 | }
|
---|
3072 |
|
---|
3073 | /**
|
---|
3074 | * Returns the last error message collected by the vdErrorCall callback and
|
---|
3075 | * resets it.
|
---|
3076 | *
|
---|
3077 | * The error message is returned prepended with a dot and a space, like this:
|
---|
3078 | * <code>
|
---|
3079 | * ". <error_text> (%Rrc)"
|
---|
3080 | * </code>
|
---|
3081 | * to make it easily appendable to a more general error message. The @c %Rrc
|
---|
3082 | * format string is given @a aVRC as an argument.
|
---|
3083 | *
|
---|
3084 | * If there is no last error message collected by vdErrorCall or if it is a
|
---|
3085 | * null or empty string, then this function returns the following text:
|
---|
3086 | * <code>
|
---|
3087 | * " (%Rrc)"
|
---|
3088 | * </code>
|
---|
3089 | *
|
---|
3090 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
3091 | * the callback isn't called by more than one thread at a time.
|
---|
3092 | *
|
---|
3093 | * @param aVRC VBox error code to use when no error message is provided.
|
---|
3094 | */
|
---|
3095 | Utf8Str HardDisk2::vdError (int aVRC)
|
---|
3096 | {
|
---|
3097 | Utf8Str error;
|
---|
3098 |
|
---|
3099 | if (mm.vdError.isEmpty())
|
---|
3100 | error = Utf8StrFmt (" (%Rrc)", aVRC);
|
---|
3101 | else
|
---|
3102 | error = Utf8StrFmt (".\n%s", mm.vdError.raw());
|
---|
3103 |
|
---|
3104 | mm.vdError.setNull();
|
---|
3105 |
|
---|
3106 | return error;
|
---|
3107 | }
|
---|
3108 |
|
---|
3109 | /**
|
---|
3110 | * Error message callback.
|
---|
3111 | *
|
---|
3112 | * Puts the reported error message to the mm.vdError field.
|
---|
3113 | *
|
---|
3114 | * @note Doesn't do any object locking; it is assumed that the caller makes sure
|
---|
3115 | * the callback isn't called by more than one thread at a time.
|
---|
3116 | *
|
---|
3117 | * @param pvUser The opaque data passed on container creation.
|
---|
3118 | * @param rc The VBox error code.
|
---|
3119 | * @param RT_SRC_POS_DECL Use RT_SRC_POS.
|
---|
3120 | * @param pszFormat Error message format string.
|
---|
3121 | * @param va Error message arguments.
|
---|
3122 | */
|
---|
3123 | /*static*/
|
---|
3124 | DECLCALLBACK(void) HardDisk2::vdErrorCall (void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
3125 | const char *pszFormat, va_list va)
|
---|
3126 | {
|
---|
3127 | HardDisk2 *that = static_cast <HardDisk2 *> (pvUser);
|
---|
3128 | AssertReturnVoid (that != NULL);
|
---|
3129 |
|
---|
3130 | if (that->mm.vdError.isEmpty())
|
---|
3131 | that->mm.vdError =
|
---|
3132 | Utf8StrFmt ("%s (%Rrc)", Utf8StrFmtVA (pszFormat, va).raw(), rc);
|
---|
3133 | else
|
---|
3134 | that->mm.vdError =
|
---|
3135 | Utf8StrFmt ("%s.\n%s (%Rrc)", that->mm.vdError.raw(),
|
---|
3136 | Utf8StrFmtVA (pszFormat, va).raw(), rc);
|
---|
3137 | }
|
---|
3138 |
|
---|
3139 | /**
|
---|
3140 | * PFNVMPROGRESS callback handler for Task operations.
|
---|
3141 | *
|
---|
3142 | * @param uPercent Completetion precentage (0-100).
|
---|
3143 | * @param pvUser Pointer to the Progress instance.
|
---|
3144 | */
|
---|
3145 | /*static*/
|
---|
3146 | DECLCALLBACK(int) HardDisk2::vdProgressCall (PVM /* pVM */, unsigned uPercent,
|
---|
3147 | void *pvUser)
|
---|
3148 | {
|
---|
3149 | HardDisk2 *that = static_cast <HardDisk2 *> (pvUser);
|
---|
3150 | AssertReturn (that != NULL, VERR_GENERAL_FAILURE);
|
---|
3151 |
|
---|
3152 | if (that->mm.vdProgress != NULL)
|
---|
3153 | {
|
---|
3154 | /* update the progress object, capping it at 99% as the final percent
|
---|
3155 | * is used for additional operations like setting the UUIDs and similar. */
|
---|
3156 | that->mm.vdProgress->notifyProgress (RT_MIN (uPercent, 99));
|
---|
3157 | }
|
---|
3158 |
|
---|
3159 | return VINF_SUCCESS;
|
---|
3160 | }
|
---|
3161 |
|
---|
3162 | /* static */
|
---|
3163 | DECLCALLBACK(bool) HardDisk2::vdConfigAreKeysValid (void *pvUser,
|
---|
3164 | const char *pszzValid)
|
---|
3165 | {
|
---|
3166 | HardDisk2 *that = static_cast <HardDisk2 *> (pvUser);
|
---|
3167 | AssertReturn (that != NULL, false);
|
---|
3168 |
|
---|
3169 | /* we always return true since the only keys we have are those found in
|
---|
3170 | * VDBACKENDINFO */
|
---|
3171 | return true;
|
---|
3172 | }
|
---|
3173 |
|
---|
3174 | /* static */
|
---|
3175 | DECLCALLBACK(int) HardDisk2::vdConfigQuerySize (void *pvUser, const char *pszName,
|
---|
3176 | size_t *pcbValue)
|
---|
3177 | {
|
---|
3178 | AssertReturn (VALID_PTR (pcbValue), VERR_INVALID_POINTER);
|
---|
3179 |
|
---|
3180 | HardDisk2 *that = static_cast <HardDisk2 *> (pvUser);
|
---|
3181 | AssertReturn (that != NULL, VERR_GENERAL_FAILURE);
|
---|
3182 |
|
---|
3183 | Data::PropertyMap::const_iterator it =
|
---|
3184 | that->mm.properties.find (Bstr (pszName));
|
---|
3185 | if (it == that->mm.properties.end())
|
---|
3186 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
3187 |
|
---|
3188 | /* we interpret null values as "no value" in HardDisk2 */
|
---|
3189 | if (it->second.isNull())
|
---|
3190 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
3191 |
|
---|
3192 | *pcbValue = it->second.length() + 1 /* include terminator */;
|
---|
3193 |
|
---|
3194 | return VINF_SUCCESS;
|
---|
3195 | }
|
---|
3196 |
|
---|
3197 | /* static */
|
---|
3198 | DECLCALLBACK(int) HardDisk2::vdConfigQuery (void *pvUser, const char *pszName,
|
---|
3199 | char *pszValue, size_t cchValue)
|
---|
3200 | {
|
---|
3201 | AssertReturn (VALID_PTR (pszValue), VERR_INVALID_POINTER);
|
---|
3202 |
|
---|
3203 | HardDisk2 *that = static_cast <HardDisk2 *> (pvUser);
|
---|
3204 | AssertReturn (that != NULL, VERR_GENERAL_FAILURE);
|
---|
3205 |
|
---|
3206 | Data::PropertyMap::const_iterator it =
|
---|
3207 | that->mm.properties.find (Bstr (pszName));
|
---|
3208 | if (it == that->mm.properties.end())
|
---|
3209 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
3210 |
|
---|
3211 | Utf8Str value = it->second;
|
---|
3212 | if (value.length() >= cchValue)
|
---|
3213 | return VERR_CFGM_NOT_ENOUGH_SPACE;
|
---|
3214 |
|
---|
3215 | /* we interpret null values as "no value" in HardDisk2 */
|
---|
3216 | if (it->second.isNull())
|
---|
3217 | return VERR_CFGM_VALUE_NOT_FOUND;
|
---|
3218 |
|
---|
3219 | memcpy (pszValue, value, value.length() + 1);
|
---|
3220 |
|
---|
3221 | return VINF_SUCCESS;
|
---|
3222 | }
|
---|
3223 |
|
---|
3224 | /**
|
---|
3225 | * Thread function for time-consuming tasks.
|
---|
3226 | *
|
---|
3227 | * The Task structure passed to @a pvUser must be allocated using new and will
|
---|
3228 | * be freed by this method before it returns.
|
---|
3229 | *
|
---|
3230 | * @param pvUser Pointer to the Task instance.
|
---|
3231 | */
|
---|
3232 | /* static */
|
---|
3233 | DECLCALLBACK(int) HardDisk2::taskThread (RTTHREAD thread, void *pvUser)
|
---|
3234 | {
|
---|
3235 | std::auto_ptr <Task> task (static_cast <Task *> (pvUser));
|
---|
3236 | AssertReturn (task.get(), VERR_GENERAL_FAILURE);
|
---|
3237 |
|
---|
3238 | bool isAsync = thread != NIL_RTTHREAD;
|
---|
3239 |
|
---|
3240 | HardDisk2 *that = task->that;
|
---|
3241 |
|
---|
3242 | /// @todo ugly hack, fix ComAssert... later
|
---|
3243 | #define setError that->setError
|
---|
3244 |
|
---|
3245 | /* Note: no need in AutoCaller because Task does that */
|
---|
3246 |
|
---|
3247 | LogFlowFuncEnter();
|
---|
3248 | LogFlowFunc (("{%p}: operation=%d\n", that, task->operation));
|
---|
3249 |
|
---|
3250 | HRESULT rc = S_OK;
|
---|
3251 |
|
---|
3252 | switch (task->operation)
|
---|
3253 | {
|
---|
3254 | ////////////////////////////////////////////////////////////////////////
|
---|
3255 |
|
---|
3256 | case Task::CreateDynamic:
|
---|
3257 | case Task::CreateFixed:
|
---|
3258 | {
|
---|
3259 | /* The lock is also used as a signal from the task initiator (which
|
---|
3260 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
3261 | AutoWriteLock thatLock (that);
|
---|
3262 |
|
---|
3263 | /* these parameters we need after creation */
|
---|
3264 | uint64_t size = 0, logicalSize = 0;
|
---|
3265 |
|
---|
3266 | /* The object may request a specific UUID (through a special form of
|
---|
3267 | * the setLocation() argumet). Otherwise we have to generate it */
|
---|
3268 | Guid id = that->m.id;
|
---|
3269 | bool generateUuid = id.isEmpty();
|
---|
3270 | if (generateUuid)
|
---|
3271 | {
|
---|
3272 | id.create();
|
---|
3273 | /* VirtualBox::registerHardDisk2() will need UUID */
|
---|
3274 | unconst (that->m.id) = id;
|
---|
3275 | }
|
---|
3276 |
|
---|
3277 | try
|
---|
3278 | {
|
---|
3279 | PVBOXHDD hdd;
|
---|
3280 | int vrc = VDCreate (that->mm.vdDiskIfaces, &hdd);
|
---|
3281 | ComAssertRCThrow (vrc, E_FAIL);
|
---|
3282 |
|
---|
3283 | Utf8Str format (that->mm.format);
|
---|
3284 | Utf8Str location (that->m.locationFull);
|
---|
3285 | uint64_t capabilities = that->mm.formatObj->capabilities();
|
---|
3286 |
|
---|
3287 | /* unlock before the potentially lengthy operation */
|
---|
3288 | Assert (that->m.state == MediaState_Creating);
|
---|
3289 | thatLock.leave();
|
---|
3290 |
|
---|
3291 | try
|
---|
3292 | {
|
---|
3293 | /* ensure the directory exists */
|
---|
3294 | rc = VirtualBox::ensureFilePathExists (location);
|
---|
3295 | CheckComRCThrowRC (rc);
|
---|
3296 |
|
---|
3297 | PDMMEDIAGEOMETRY geo = { 0 }; /* auto-detect */
|
---|
3298 |
|
---|
3299 | /* needed for vdProgressCallback */
|
---|
3300 | that->mm.vdProgress = task->progress;
|
---|
3301 |
|
---|
3302 | vrc = VDCreateBase (hdd, format, location,
|
---|
3303 | task->operation == Task::CreateDynamic ?
|
---|
3304 | VD_IMAGE_TYPE_NORMAL :
|
---|
3305 | VD_IMAGE_TYPE_FIXED,
|
---|
3306 | task->d.size * _1M,
|
---|
3307 | VD_IMAGE_FLAGS_NONE,
|
---|
3308 | NULL, &geo, &geo, id.raw(),
|
---|
3309 | VD_OPEN_FLAGS_NORMAL,
|
---|
3310 | NULL, that->mm.vdDiskIfaces);
|
---|
3311 |
|
---|
3312 | if (RT_FAILURE (vrc))
|
---|
3313 | {
|
---|
3314 | throw setError (E_FAIL,
|
---|
3315 | tr ("Could not create the hard disk storage "
|
---|
3316 | "unit '%s'%s"),
|
---|
3317 | location.raw(), that->vdError (vrc).raw());
|
---|
3318 | }
|
---|
3319 |
|
---|
3320 | size = VDGetFileSize (hdd, 0);
|
---|
3321 | logicalSize = VDGetSize (hdd, 0) / _1M;
|
---|
3322 | }
|
---|
3323 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3324 |
|
---|
3325 | VDDestroy (hdd);
|
---|
3326 | }
|
---|
3327 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3328 |
|
---|
3329 | if (SUCCEEDED (rc))
|
---|
3330 | {
|
---|
3331 | /* register with mVirtualBox as the last step and move to
|
---|
3332 | * Created state only on success (leaving an orphan file is
|
---|
3333 | * better than breaking media registry consistency) */
|
---|
3334 | rc = that->mVirtualBox->registerHardDisk2 (that);
|
---|
3335 | }
|
---|
3336 |
|
---|
3337 | thatLock.maybeEnter();
|
---|
3338 |
|
---|
3339 | if (SUCCEEDED (rc))
|
---|
3340 | {
|
---|
3341 | that->m.state = MediaState_Created;
|
---|
3342 |
|
---|
3343 | that->m.size = size;
|
---|
3344 | that->mm.logicalSize = logicalSize;
|
---|
3345 | }
|
---|
3346 | else
|
---|
3347 | {
|
---|
3348 | /* back to NotCreated on failiure */
|
---|
3349 | that->m.state = MediaState_NotCreated;
|
---|
3350 |
|
---|
3351 | /* reset UUID to prevent it from being reused next time */
|
---|
3352 | if (generateUuid)
|
---|
3353 | unconst (that->m.id).clear();
|
---|
3354 | }
|
---|
3355 |
|
---|
3356 | break;
|
---|
3357 | }
|
---|
3358 |
|
---|
3359 | ////////////////////////////////////////////////////////////////////////
|
---|
3360 |
|
---|
3361 | case Task::CreateDiff:
|
---|
3362 | {
|
---|
3363 | ComObjPtr <HardDisk2> &target = task->d.target;
|
---|
3364 |
|
---|
3365 | /* Lock both in {parent,child} order. The lock is also used as a
|
---|
3366 | * signal from the task initiator (which releases it only after
|
---|
3367 | * RTThreadCreate()) that we can start the job*/
|
---|
3368 | AutoMultiWriteLock2 thatLock (that, target);
|
---|
3369 |
|
---|
3370 | uint64_t size = 0, logicalSize = 0;
|
---|
3371 |
|
---|
3372 | /* The object may request a specific UUID (through a special form of
|
---|
3373 | * the setLocation() argument). Otherwise we have to generate it */
|
---|
3374 | Guid targetId = target->m.id;
|
---|
3375 | bool generateUuid = targetId.isEmpty();
|
---|
3376 | if (generateUuid)
|
---|
3377 | {
|
---|
3378 | targetId.create();
|
---|
3379 | /* VirtualBox::registerHardDisk2() will need UUID */
|
---|
3380 | unconst (target->m.id) = targetId;
|
---|
3381 | }
|
---|
3382 |
|
---|
3383 | try
|
---|
3384 | {
|
---|
3385 | PVBOXHDD hdd;
|
---|
3386 | int vrc = VDCreate (that->mm.vdDiskIfaces, &hdd);
|
---|
3387 | ComAssertRCThrow (vrc, E_FAIL);
|
---|
3388 |
|
---|
3389 | Utf8Str format (that->mm.format);
|
---|
3390 | Utf8Str location (that->m.locationFull);
|
---|
3391 |
|
---|
3392 | Utf8Str targetFormat (target->mm.format);
|
---|
3393 | Utf8Str targetLocation (target->m.locationFull);
|
---|
3394 |
|
---|
3395 | Guid thatId = that->m.id;
|
---|
3396 |
|
---|
3397 | Assert (target->m.state == MediaState_Creating);
|
---|
3398 |
|
---|
3399 | /* Note: MediaState_LockedWrite is ok when taking an online
|
---|
3400 | * snapshot */
|
---|
3401 | Assert (that->m.state == MediaState_LockedRead ||
|
---|
3402 | that->m.state == MediaState_LockedWrite);
|
---|
3403 |
|
---|
3404 | /* unlock before the potentially lengthy operation */
|
---|
3405 | thatLock.leave();
|
---|
3406 |
|
---|
3407 | try
|
---|
3408 | {
|
---|
3409 | vrc = VDOpen (hdd, format, location,
|
---|
3410 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO,
|
---|
3411 | that->mm.vdDiskIfaces);
|
---|
3412 | if (RT_FAILURE (vrc))
|
---|
3413 | {
|
---|
3414 | throw setError (E_FAIL,
|
---|
3415 | tr ("Could not open the hard disk storage "
|
---|
3416 | "unit '%s'%s"),
|
---|
3417 | location.raw(), that->vdError (vrc).raw());
|
---|
3418 | }
|
---|
3419 |
|
---|
3420 | /* ensure the target directory exists */
|
---|
3421 | rc = VirtualBox::ensureFilePathExists (targetLocation);
|
---|
3422 | CheckComRCThrowRC (rc);
|
---|
3423 |
|
---|
3424 | /* needed for vdProgressCallback */
|
---|
3425 | that->mm.vdProgress = task->progress;
|
---|
3426 |
|
---|
3427 | vrc = VDCreateDiff (hdd, targetFormat, targetLocation,
|
---|
3428 | VD_IMAGE_FLAGS_NONE,
|
---|
3429 | NULL, targetId.raw(),
|
---|
3430 | thatId.raw(),
|
---|
3431 | VD_OPEN_FLAGS_NORMAL,
|
---|
3432 | target->mm.vdDiskIfaces,
|
---|
3433 | that->mm.vdDiskIfaces);
|
---|
3434 |
|
---|
3435 | that->mm.vdProgress = NULL;
|
---|
3436 |
|
---|
3437 | if (RT_FAILURE (vrc))
|
---|
3438 | {
|
---|
3439 | throw setError (E_FAIL,
|
---|
3440 | tr ("Could not create the differencing hard disk "
|
---|
3441 | "storage unit '%s'%s"),
|
---|
3442 | targetLocation.raw(), that->vdError (vrc).raw());
|
---|
3443 | }
|
---|
3444 |
|
---|
3445 | size = VDGetFileSize (hdd, 0);
|
---|
3446 | logicalSize = VDGetSize (hdd, 0) / _1M;
|
---|
3447 | }
|
---|
3448 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3449 |
|
---|
3450 | VDDestroy (hdd);
|
---|
3451 | }
|
---|
3452 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3453 |
|
---|
3454 | if (SUCCEEDED (rc))
|
---|
3455 | {
|
---|
3456 | /* we set mParent & children() (note that thatLock is released
|
---|
3457 | * here), but lock VirtualBox first to follow the rule */
|
---|
3458 | AutoMultiWriteLock2 alock (that->mVirtualBox->lockHandle(),
|
---|
3459 | that->treeLock());
|
---|
3460 |
|
---|
3461 | Assert (target->mParent.isNull());
|
---|
3462 |
|
---|
3463 | /* associate the child with the parent and deassociate from
|
---|
3464 | * VirtualBox */
|
---|
3465 | target->mParent = that;
|
---|
3466 | that->addDependentChild (target);
|
---|
3467 | target->mVirtualBox->removeDependentChild (target);
|
---|
3468 |
|
---|
3469 | /* register with mVirtualBox as the last step and move to
|
---|
3470 | * Created state only on success (leaving an orphan file is
|
---|
3471 | * better than breaking media registry consistency) */
|
---|
3472 | rc = that->mVirtualBox->registerHardDisk2 (target);
|
---|
3473 |
|
---|
3474 | if (FAILED (rc))
|
---|
3475 | {
|
---|
3476 | /* break the parent association on failure to register */
|
---|
3477 | target->mVirtualBox->addDependentChild (target);
|
---|
3478 | that->removeDependentChild (target);
|
---|
3479 | target->mParent.setNull();
|
---|
3480 | }
|
---|
3481 | }
|
---|
3482 |
|
---|
3483 | thatLock.maybeEnter();
|
---|
3484 |
|
---|
3485 | if (SUCCEEDED (rc))
|
---|
3486 | {
|
---|
3487 | target->m.state = MediaState_Created;
|
---|
3488 |
|
---|
3489 | target->m.size = size;
|
---|
3490 | target->mm.logicalSize = logicalSize;
|
---|
3491 | }
|
---|
3492 | else
|
---|
3493 | {
|
---|
3494 | /* back to NotCreated on failiure */
|
---|
3495 | target->m.state = MediaState_NotCreated;
|
---|
3496 |
|
---|
3497 | /* reset UUID to prevent it from being reused next time */
|
---|
3498 | if (generateUuid)
|
---|
3499 | unconst (target->m.id).clear();
|
---|
3500 | }
|
---|
3501 |
|
---|
3502 | if (isAsync)
|
---|
3503 | {
|
---|
3504 | /* unlock ourselves when done (unless in MediaState_LockedWrite
|
---|
3505 | * state because of taking the online snapshot*/
|
---|
3506 | if (that->m.state != MediaState_LockedWrite)
|
---|
3507 | {
|
---|
3508 | HRESULT rc2 = that->UnlockRead (NULL);
|
---|
3509 | AssertComRC (rc2);
|
---|
3510 | }
|
---|
3511 | }
|
---|
3512 |
|
---|
3513 | /* deregister the task registered in createDiffStorage() */
|
---|
3514 | Assert (that->mm.numCreateDiffTasks != 0);
|
---|
3515 | -- that->mm.numCreateDiffTasks;
|
---|
3516 |
|
---|
3517 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
3518 | * unlock the hard disk */
|
---|
3519 |
|
---|
3520 | break;
|
---|
3521 | }
|
---|
3522 |
|
---|
3523 | ////////////////////////////////////////////////////////////////////////
|
---|
3524 |
|
---|
3525 | case Task::Merge:
|
---|
3526 | {
|
---|
3527 | /* The lock is also used as a signal from the task initiator (which
|
---|
3528 | * releases it only after RTThreadCreate()) that we can start the
|
---|
3529 | * job. We don't actually need the lock for anything else since the
|
---|
3530 | * object is protected by MediaState_Deleting and we don't modify
|
---|
3531 | * its sensitive fields below */
|
---|
3532 | {
|
---|
3533 | AutoWriteLock thatLock (that);
|
---|
3534 | }
|
---|
3535 |
|
---|
3536 | MergeChain *chain = task->d.chain.get();
|
---|
3537 |
|
---|
3538 | #if 1
|
---|
3539 | LogFlow (("*** MERGE forward = %RTbool\n", chain->isForward()));
|
---|
3540 | #endif
|
---|
3541 |
|
---|
3542 | try
|
---|
3543 | {
|
---|
3544 | PVBOXHDD hdd;
|
---|
3545 | int vrc = VDCreate (that->mm.vdDiskIfaces, &hdd);
|
---|
3546 | ComAssertRCThrow (vrc, E_FAIL);
|
---|
3547 |
|
---|
3548 | try
|
---|
3549 | {
|
---|
3550 | /* open all hard disks in the chain (they are in the
|
---|
3551 | * {parent,child} order in there. Note that we don't lock
|
---|
3552 | * objects in this chain since they must be in states
|
---|
3553 | * (Deleting and LockedWrite) that prevent from chaning
|
---|
3554 | * their format and location fields from outside. */
|
---|
3555 |
|
---|
3556 | for (MergeChain::const_iterator it = chain->begin();
|
---|
3557 | it != chain->end(); ++ it)
|
---|
3558 | {
|
---|
3559 | /* complex sanity (sane complexity) */
|
---|
3560 | Assert ((chain->isForward() &&
|
---|
3561 | ((*it != chain->back() &&
|
---|
3562 | (*it)->m.state == MediaState_Deleting) ||
|
---|
3563 | (*it == chain->back() &&
|
---|
3564 | (*it)->m.state == MediaState_LockedWrite))) ||
|
---|
3565 | (!chain->isForward() &&
|
---|
3566 | ((*it != chain->front() &&
|
---|
3567 | (*it)->m.state == MediaState_Deleting) ||
|
---|
3568 | (*it == chain->front() &&
|
---|
3569 | (*it)->m.state == MediaState_LockedWrite))));
|
---|
3570 |
|
---|
3571 | Assert (*it == chain->target() ||
|
---|
3572 | (*it)->m.backRefs.size() == 0);
|
---|
3573 |
|
---|
3574 | /* open the first image with VDOPEN_FLAGS_INFO because
|
---|
3575 | * it's not necessarily the base one */
|
---|
3576 | vrc = VDOpen (hdd, Utf8Str ((*it)->mm.format),
|
---|
3577 | Utf8Str ((*it)->m.locationFull),
|
---|
3578 | it == chain->begin() ?
|
---|
3579 | VD_OPEN_FLAGS_INFO : 0,
|
---|
3580 | (*it)->mm.vdDiskIfaces);
|
---|
3581 | if (RT_FAILURE (vrc))
|
---|
3582 | throw vrc;
|
---|
3583 | #if 1
|
---|
3584 | LogFlow (("*** MERGE disk = %ls\n",
|
---|
3585 | (*it)->m.locationFull.raw()));
|
---|
3586 | #endif
|
---|
3587 | }
|
---|
3588 |
|
---|
3589 | /* needed for vdProgressCallback */
|
---|
3590 | that->mm.vdProgress = task->progress;
|
---|
3591 |
|
---|
3592 | unsigned start = chain->isForward() ?
|
---|
3593 | 0 : chain->size() - 1;
|
---|
3594 | unsigned end = chain->isForward() ?
|
---|
3595 | chain->size() - 1 : 0;
|
---|
3596 | #if 1
|
---|
3597 | LogFlow (("*** MERGE from %d to %d\n", start, end));
|
---|
3598 | #endif
|
---|
3599 | vrc = VDMerge (hdd, start, end, that->mm.vdDiskIfaces);
|
---|
3600 |
|
---|
3601 | that->mm.vdProgress = NULL;
|
---|
3602 |
|
---|
3603 | if (RT_FAILURE (vrc))
|
---|
3604 | throw vrc;
|
---|
3605 |
|
---|
3606 | /* update parent UUIDs */
|
---|
3607 | /// @todo VDMerge should be taught to do so, including the
|
---|
3608 | /// multiple children case
|
---|
3609 | if (chain->isForward())
|
---|
3610 | {
|
---|
3611 | /* target's UUID needs to be updated (note that target
|
---|
3612 | * is the only image in the container on success) */
|
---|
3613 | vrc = VDSetParentUuid (hdd, 0, chain->parent()->m.id);
|
---|
3614 | if (RT_FAILURE (vrc))
|
---|
3615 | throw vrc;
|
---|
3616 | }
|
---|
3617 | else
|
---|
3618 | {
|
---|
3619 | /* we need to update UUIDs of all source's children
|
---|
3620 | * which cannot be part of the container at once so
|
---|
3621 | * add each one in there individually */
|
---|
3622 | if (chain->children().size() > 0)
|
---|
3623 | {
|
---|
3624 | for (List::const_iterator it = chain->children().begin();
|
---|
3625 | it != chain->children().end(); ++ it)
|
---|
3626 | {
|
---|
3627 | /* VD_OPEN_FLAGS_INFO since UUID is wrong yet */
|
---|
3628 | vrc = VDOpen (hdd, Utf8Str ((*it)->mm.format),
|
---|
3629 | Utf8Str ((*it)->m.locationFull),
|
---|
3630 | VD_OPEN_FLAGS_INFO,
|
---|
3631 | (*it)->mm.vdDiskIfaces);
|
---|
3632 | if (RT_FAILURE (vrc))
|
---|
3633 | throw vrc;
|
---|
3634 |
|
---|
3635 | vrc = VDSetParentUuid (hdd, 1,
|
---|
3636 | chain->target()->m.id);
|
---|
3637 | if (RT_FAILURE (vrc))
|
---|
3638 | throw vrc;
|
---|
3639 |
|
---|
3640 | vrc = VDClose (hdd, false /* fDelete */);
|
---|
3641 | if (RT_FAILURE (vrc))
|
---|
3642 | throw vrc;
|
---|
3643 | }
|
---|
3644 | }
|
---|
3645 | }
|
---|
3646 | }
|
---|
3647 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3648 | catch (int aVRC)
|
---|
3649 | {
|
---|
3650 | throw setError (E_FAIL,
|
---|
3651 | tr ("Could not merge the hard disk '%ls' to '%ls'%s"),
|
---|
3652 | chain->source()->m.locationFull.raw(),
|
---|
3653 | chain->target()->m.locationFull.raw(),
|
---|
3654 | that->vdError (aVRC).raw());
|
---|
3655 | }
|
---|
3656 |
|
---|
3657 | VDDestroy (hdd);
|
---|
3658 | }
|
---|
3659 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3660 |
|
---|
3661 | HRESULT rc2;
|
---|
3662 |
|
---|
3663 | bool saveSettingsFailed = false;
|
---|
3664 |
|
---|
3665 | if (SUCCEEDED (rc))
|
---|
3666 | {
|
---|
3667 | /* all hard disks but the target were successfully deleted by
|
---|
3668 | * VDMerge; reparent the last one and uninitialize deleted */
|
---|
3669 |
|
---|
3670 | /* we set mParent & children() (note that thatLock is released
|
---|
3671 | * here), but lock VirtualBox first to follow the rule */
|
---|
3672 | AutoMultiWriteLock2 alock (that->mVirtualBox->lockHandle(),
|
---|
3673 | that->treeLock());
|
---|
3674 |
|
---|
3675 | HardDisk2 *source = chain->source();
|
---|
3676 | HardDisk2 *target = chain->target();
|
---|
3677 |
|
---|
3678 | if (chain->isForward())
|
---|
3679 | {
|
---|
3680 | /* first, unregister the target since it may become a base
|
---|
3681 | * hard disk which needs re-registration */
|
---|
3682 | rc2 = target->mVirtualBox->
|
---|
3683 | unregisterHardDisk2 (target, false /* aSaveSettings */);
|
---|
3684 | AssertComRC (rc2);
|
---|
3685 |
|
---|
3686 | /* then, reparent it and disconnect the deleted branch at
|
---|
3687 | * both ends (chain->parent() is source's parent) */
|
---|
3688 | target->mParent->removeDependentChild (target);
|
---|
3689 | target->mParent = chain->parent();
|
---|
3690 | if (!target->mParent.isNull())
|
---|
3691 | {
|
---|
3692 | target->mParent->addDependentChild (target);
|
---|
3693 | target->mParent->removeDependentChild (source);
|
---|
3694 | source->mParent.setNull();
|
---|
3695 | }
|
---|
3696 | else
|
---|
3697 | {
|
---|
3698 | target->mVirtualBox->addDependentChild (target);
|
---|
3699 | target->mVirtualBox->removeDependentChild (source);
|
---|
3700 | }
|
---|
3701 |
|
---|
3702 | /* then, register again */
|
---|
3703 | rc2 = target->mVirtualBox->
|
---|
3704 | registerHardDisk2 (target, false /* aSaveSettings */);
|
---|
3705 | AssertComRC (rc2);
|
---|
3706 | }
|
---|
3707 | else
|
---|
3708 | {
|
---|
3709 | Assert (target->children().size() == 1);
|
---|
3710 | HardDisk2 *targetChild = target->children().front();
|
---|
3711 |
|
---|
3712 | /* disconnect the deleted branch at the elder end */
|
---|
3713 | target->removeDependentChild (targetChild);
|
---|
3714 | targetChild->mParent.setNull();
|
---|
3715 |
|
---|
3716 | const List &children = chain->children();
|
---|
3717 |
|
---|
3718 | /* reparent source's chidren and disconnect the deleted
|
---|
3719 | * branch at the younger end m*/
|
---|
3720 | if (children.size() > 0)
|
---|
3721 | {
|
---|
3722 | /* obey {parent,child} lock order */
|
---|
3723 | AutoWriteLock sourceLock (source);
|
---|
3724 |
|
---|
3725 | for (List::const_iterator it = children.begin();
|
---|
3726 | it != children.end(); ++ it)
|
---|
3727 | {
|
---|
3728 | AutoWriteLock childLock (*it);
|
---|
3729 |
|
---|
3730 | (*it)->mParent = target;
|
---|
3731 | (*it)->mParent->addDependentChild (*it);
|
---|
3732 | source->removeDependentChild (*it);
|
---|
3733 | }
|
---|
3734 | }
|
---|
3735 | }
|
---|
3736 |
|
---|
3737 | /* try to save the hard disk registry */
|
---|
3738 | rc = that->mVirtualBox->saveSettings();
|
---|
3739 |
|
---|
3740 | if (SUCCEEDED (rc))
|
---|
3741 | {
|
---|
3742 | /* unregister and uninitialize all hard disks in the chain
|
---|
3743 | * but the target */
|
---|
3744 |
|
---|
3745 | for (MergeChain::iterator it = chain->begin();
|
---|
3746 | it != chain->end();)
|
---|
3747 | {
|
---|
3748 | if (*it == chain->target())
|
---|
3749 | {
|
---|
3750 | ++ it;
|
---|
3751 | continue;
|
---|
3752 | }
|
---|
3753 |
|
---|
3754 | rc2 = (*it)->mVirtualBox->
|
---|
3755 | unregisterHardDisk2 (*it, false /* aSaveSettings */);
|
---|
3756 | AssertComRC (rc2);
|
---|
3757 |
|
---|
3758 | /* now, uninitialize the deleted hard disk (note that
|
---|
3759 | * due to the Deleting state, uninit() will not touch
|
---|
3760 | * the parent-child relationship so we need to
|
---|
3761 | * uninitialize each disk individually) */
|
---|
3762 |
|
---|
3763 | /* note that the operation initiator hard disk (which is
|
---|
3764 | * normally also the source hard disk) is a special case
|
---|
3765 | * -- there is one more caller added by Task to it which
|
---|
3766 | * we must release. Also, if we are in sync mode, the
|
---|
3767 | * caller may still hold an AutoCaller instance for it
|
---|
3768 | * and therefore we cannot uninit() it (it's therefore
|
---|
3769 | * the caller's responsibility) */
|
---|
3770 | if (*it == that)
|
---|
3771 | task->autoCaller.release();
|
---|
3772 |
|
---|
3773 | /* release the caller added by MergeChain before
|
---|
3774 | * uninit() */
|
---|
3775 | (*it)->releaseCaller();
|
---|
3776 |
|
---|
3777 | if (isAsync || *it != that)
|
---|
3778 | (*it)->uninit();
|
---|
3779 |
|
---|
3780 | /* delete (to prevent uninitialization in MergeChain
|
---|
3781 | * dtor) and advance to the next item */
|
---|
3782 | it = chain->erase (it);
|
---|
3783 | }
|
---|
3784 |
|
---|
3785 | /* Note that states of all other hard disks (target, parent,
|
---|
3786 | * children) will be restored by the MergeChain dtor */
|
---|
3787 | }
|
---|
3788 | else
|
---|
3789 | {
|
---|
3790 | /* too bad if we fail, but we'll need to rollback everything
|
---|
3791 | * we did above to at least keep the HD tree in sync with
|
---|
3792 | * the current registry on disk */
|
---|
3793 |
|
---|
3794 | saveSettingsFailed = true;
|
---|
3795 |
|
---|
3796 | /// @todo NEWMEDIA implement a proper undo
|
---|
3797 |
|
---|
3798 | AssertFailed();
|
---|
3799 | }
|
---|
3800 | }
|
---|
3801 |
|
---|
3802 | if (FAILED (rc))
|
---|
3803 | {
|
---|
3804 | /* Here we come if either VDMerge() failed (in which case we
|
---|
3805 | * assume that it tried to do everything to make a further
|
---|
3806 | * retry possible -- e.g. not deleted intermediate hard disks
|
---|
3807 | * and so on) or VirtualBox::saveSettings() failed (where we
|
---|
3808 | * should have the original tree but with intermediate storage
|
---|
3809 | * units deleted by VDMerge()). We have to only restore states
|
---|
3810 | * (through the MergeChain dtor) unless we are run synchronously
|
---|
3811 | * in which case it's the responsibility of the caller as stated
|
---|
3812 | * in the mergeTo() docs. The latter also implies that we
|
---|
3813 | * don't own the merge chain, so release it in this case. */
|
---|
3814 |
|
---|
3815 | if (!isAsync)
|
---|
3816 | task->d.chain.release();
|
---|
3817 |
|
---|
3818 | NOREF (saveSettingsFailed);
|
---|
3819 | }
|
---|
3820 |
|
---|
3821 | break;
|
---|
3822 | }
|
---|
3823 |
|
---|
3824 | ////////////////////////////////////////////////////////////////////////
|
---|
3825 |
|
---|
3826 | case Task::Clone:
|
---|
3827 | {
|
---|
3828 | ComObjPtr <HardDisk2> &target = task->d.target;
|
---|
3829 |
|
---|
3830 | /* Lock both in {parent,child} order. The lock is also used as a
|
---|
3831 | * signal from the task initiator (which releases it only after
|
---|
3832 | * RTThreadCreate()) that we can start the job*/
|
---|
3833 | AutoMultiWriteLock2 thatLock (that, target);
|
---|
3834 |
|
---|
3835 | uint64_t size = 0, logicalSize = 0;
|
---|
3836 |
|
---|
3837 | /* The object may request a specific UUID (through a special form of
|
---|
3838 | * the setLocation() argumet). Otherwise we have to generate it */
|
---|
3839 | Guid targetId = target->m.id;
|
---|
3840 | bool generateUuid = targetId.isEmpty();
|
---|
3841 | if (generateUuid)
|
---|
3842 | {
|
---|
3843 | targetId.create();
|
---|
3844 | /* VirtualBox::registerHardDisk2() will need UUID */
|
---|
3845 | unconst (target->m.id) = targetId;
|
---|
3846 | }
|
---|
3847 |
|
---|
3848 | try
|
---|
3849 | {
|
---|
3850 | PVBOXHDD hdd;
|
---|
3851 | int vrc = VDCreate (that->mm.vdDiskIfaces, &hdd);
|
---|
3852 | ComAssertRCThrow (vrc, E_FAIL);
|
---|
3853 |
|
---|
3854 | Utf8Str format (that->mm.format);
|
---|
3855 | Utf8Str location (that->m.locationFull);
|
---|
3856 |
|
---|
3857 | Utf8Str targetFormat (target->mm.format);
|
---|
3858 | Utf8Str targetLocation (target->m.locationFull);
|
---|
3859 |
|
---|
3860 | Assert (target->m.state == MediaState_Creating);
|
---|
3861 |
|
---|
3862 | Assert (that->m.state == MediaState_LockedRead);
|
---|
3863 |
|
---|
3864 | /* unlock before the potentially lengthy operation */
|
---|
3865 | thatLock.leave();
|
---|
3866 |
|
---|
3867 | try
|
---|
3868 | {
|
---|
3869 | vrc = VDOpen (hdd, format, location,
|
---|
3870 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO,
|
---|
3871 | that->mm.vdDiskIfaces);
|
---|
3872 | if (RT_FAILURE (vrc))
|
---|
3873 | {
|
---|
3874 | throw setError (E_FAIL,
|
---|
3875 | tr ("Could not open the hard disk storage "
|
---|
3876 | "unit '%s'%s"),
|
---|
3877 | location.raw(), that->vdError (vrc).raw());
|
---|
3878 | }
|
---|
3879 |
|
---|
3880 | /* ensure the target directory exists */
|
---|
3881 | rc = VirtualBox::ensureFilePathExists (targetLocation);
|
---|
3882 | CheckComRCThrowRC (rc);
|
---|
3883 |
|
---|
3884 | /* needed for vdProgressCallback */
|
---|
3885 | that->mm.vdProgress = task->progress;
|
---|
3886 |
|
---|
3887 | PVBOXHDD targetHdd;
|
---|
3888 | int vrc = VDCreate (that->mm.vdDiskIfaces, &targetHdd);
|
---|
3889 | ComAssertRCThrow (vrc, E_FAIL);
|
---|
3890 |
|
---|
3891 | vrc = VDCopy (hdd, 0, targetHdd, targetFormat,
|
---|
3892 | targetLocation, false, 0, targetId.raw(),
|
---|
3893 | NULL, target->mm.vdDiskIfaces,
|
---|
3894 | that->mm.vdDiskIfaces);
|
---|
3895 |
|
---|
3896 | that->mm.vdProgress = NULL;
|
---|
3897 |
|
---|
3898 | if (RT_FAILURE (vrc))
|
---|
3899 | {
|
---|
3900 | VDDestroy (targetHdd);
|
---|
3901 |
|
---|
3902 | throw setError (E_FAIL,
|
---|
3903 | tr ("Could not create the clone hard disk "
|
---|
3904 | "'%s'%s"),
|
---|
3905 | targetLocation.raw(), that->vdError (vrc).raw());
|
---|
3906 | }
|
---|
3907 |
|
---|
3908 | size = VDGetFileSize (hdd, 0);
|
---|
3909 | logicalSize = VDGetSize (hdd, 0) / _1M;
|
---|
3910 |
|
---|
3911 | VDDestroy (targetHdd);
|
---|
3912 | }
|
---|
3913 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3914 |
|
---|
3915 | VDDestroy (hdd);
|
---|
3916 | }
|
---|
3917 | catch (HRESULT aRC) { rc = aRC; }
|
---|
3918 |
|
---|
3919 | if (SUCCEEDED (rc))
|
---|
3920 | {
|
---|
3921 | /* we set mParent & children() (note that thatLock is released
|
---|
3922 | * here), but lock VirtualBox first to follow the rule */
|
---|
3923 | AutoMultiWriteLock2 alock (that->mVirtualBox->lockHandle(),
|
---|
3924 | that->treeLock());
|
---|
3925 |
|
---|
3926 | Assert (target->mParent.isNull());
|
---|
3927 |
|
---|
3928 | if (!that->mParent.isNull())
|
---|
3929 | {
|
---|
3930 | /* associate the clone with the original's parent and
|
---|
3931 | * deassociate from VirtualBox */
|
---|
3932 | target->mParent = that->mParent;
|
---|
3933 | that->mParent->addDependentChild (target);
|
---|
3934 | target->mVirtualBox->removeDependentChild (target);
|
---|
3935 |
|
---|
3936 | /* register with mVirtualBox as the last step and move to
|
---|
3937 | * Created state only on success (leaving an orphan file is
|
---|
3938 | * better than breaking media registry consistency) */
|
---|
3939 | rc = that->mVirtualBox->registerHardDisk2 (target);
|
---|
3940 |
|
---|
3941 | if (FAILED (rc))
|
---|
3942 | {
|
---|
3943 | /* break the parent association on failure to register */
|
---|
3944 | target->mVirtualBox->addDependentChild (target);
|
---|
3945 | that->mParent->removeDependentChild (target);
|
---|
3946 | target->mParent.setNull();
|
---|
3947 | }
|
---|
3948 | }
|
---|
3949 | else
|
---|
3950 | {
|
---|
3951 | /* just register */
|
---|
3952 | rc = that->mVirtualBox->registerHardDisk2 (target);
|
---|
3953 | }
|
---|
3954 | }
|
---|
3955 |
|
---|
3956 | thatLock.maybeEnter();
|
---|
3957 |
|
---|
3958 | if (SUCCEEDED (rc))
|
---|
3959 | {
|
---|
3960 | target->m.state = MediaState_Created;
|
---|
3961 |
|
---|
3962 | target->m.size = size;
|
---|
3963 | target->mm.logicalSize = logicalSize;
|
---|
3964 | }
|
---|
3965 | else
|
---|
3966 | {
|
---|
3967 | /* back to NotCreated on failiure */
|
---|
3968 | target->m.state = MediaState_NotCreated;
|
---|
3969 |
|
---|
3970 | /* reset UUID to prevent it from being reused next time */
|
---|
3971 | if (generateUuid)
|
---|
3972 | unconst (target->m.id).clear();
|
---|
3973 | }
|
---|
3974 |
|
---|
3975 | if (isAsync)
|
---|
3976 | {
|
---|
3977 | /* unlock ourselves when done (unless in MediaState_LockedWrite
|
---|
3978 | * state because of taking the online snapshot*/
|
---|
3979 | if (that->m.state != MediaState_LockedWrite)
|
---|
3980 | {
|
---|
3981 | HRESULT rc2 = that->UnlockRead (NULL);
|
---|
3982 | AssertComRC (rc2);
|
---|
3983 | }
|
---|
3984 | }
|
---|
3985 |
|
---|
3986 | /* Note that in sync mode, it's the caller's responsibility to
|
---|
3987 | * unlock the hard disk */
|
---|
3988 |
|
---|
3989 | break;
|
---|
3990 | }
|
---|
3991 |
|
---|
3992 | ////////////////////////////////////////////////////////////////////////
|
---|
3993 |
|
---|
3994 | case Task::Delete:
|
---|
3995 | {
|
---|
3996 | /* The lock is also used as a signal from the task initiator (which
|
---|
3997 | * releases it only after RTThreadCreate()) that we can start the job */
|
---|
3998 | AutoWriteLock thatLock (that);
|
---|
3999 |
|
---|
4000 | try
|
---|
4001 | {
|
---|
4002 | PVBOXHDD hdd;
|
---|
4003 | int vrc = VDCreate (that->mm.vdDiskIfaces, &hdd);
|
---|
4004 | ComAssertRCThrow (vrc, E_FAIL);
|
---|
4005 |
|
---|
4006 | Utf8Str format (that->mm.format);
|
---|
4007 | Utf8Str location (that->m.locationFull);
|
---|
4008 |
|
---|
4009 | /* unlock before the potentially lengthy operation */
|
---|
4010 | Assert (that->m.state == MediaState_Deleting);
|
---|
4011 | thatLock.leave();
|
---|
4012 |
|
---|
4013 | try
|
---|
4014 | {
|
---|
4015 | vrc = VDOpen (hdd, format, location,
|
---|
4016 | VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO,
|
---|
4017 | that->mm.vdDiskIfaces);
|
---|
4018 | if (RT_SUCCESS (vrc))
|
---|
4019 | vrc = VDClose (hdd, true /* fDelete */);
|
---|
4020 |
|
---|
4021 | if (RT_FAILURE (vrc))
|
---|
4022 | {
|
---|
4023 | throw setError (E_FAIL,
|
---|
4024 | tr ("Could not delete the hard disk storage "
|
---|
4025 | "unit '%s'%s"),
|
---|
4026 | location.raw(), that->vdError (vrc).raw());
|
---|
4027 | }
|
---|
4028 |
|
---|
4029 | }
|
---|
4030 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4031 |
|
---|
4032 | VDDestroy (hdd);
|
---|
4033 | }
|
---|
4034 | catch (HRESULT aRC) { rc = aRC; }
|
---|
4035 |
|
---|
4036 | thatLock.maybeEnter();
|
---|
4037 |
|
---|
4038 | /* go to the NotCreated state even on failure since the storage
|
---|
4039 | * may have been already partially deleted and cannot be used any
|
---|
4040 | * more. One will be able to manually re-open the storage if really
|
---|
4041 | * needed to re-register it. */
|
---|
4042 | that->m.state = MediaState_NotCreated;
|
---|
4043 |
|
---|
4044 | /* Reset UUID to prevent Create* from reusing it again */
|
---|
4045 | unconst (that->m.id).clear();
|
---|
4046 |
|
---|
4047 | break;
|
---|
4048 | }
|
---|
4049 |
|
---|
4050 | default:
|
---|
4051 | AssertFailedReturn (VERR_GENERAL_FAILURE);
|
---|
4052 | }
|
---|
4053 |
|
---|
4054 | /* complete the progress if run asynchronously */
|
---|
4055 | if (isAsync)
|
---|
4056 | {
|
---|
4057 | if (!task->progress.isNull())
|
---|
4058 | task->progress->notifyComplete (rc);
|
---|
4059 | }
|
---|
4060 | else
|
---|
4061 | {
|
---|
4062 | task->rc = rc;
|
---|
4063 | }
|
---|
4064 |
|
---|
4065 | LogFlowFunc (("rc=%Rhrc\n", rc));
|
---|
4066 | LogFlowFuncLeave();
|
---|
4067 |
|
---|
4068 | return VINF_SUCCESS;
|
---|
4069 |
|
---|
4070 | /// @todo ugly hack, fix ComAssert... later
|
---|
4071 | #undef setError
|
---|
4072 | }
|
---|
4073 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|