summaryrefslogtreecommitdiff
path: root/open-vm-tools/modules/freebsd/vmhgfs/request.c
blob: b3a0cac693fb0eb64ced5f92dd4a11c7b3feaee6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
/*********************************************************
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation version 2 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 *********************************************************/


/*
 * request.c --
 *
 *      Implementation of routines used to initialize, allocate, and move
 *      requests between lists.
 */

/*
 * Includes
 */

#include "hgfs_kernel.h"
#include "requestInt.h"
#include "channel.h"

/*
 * Macros
 */

/*
 * Since the DblLnkLst_Links in the requests container is just an anchor, we want to
 * skip it (e.g., get the container for the next element)
 */
#define HGFS_SIP_LIST_HEAD(sip)         \
            (DblLnkLst_Container((sip)->reqs->list.next, HgfsKReqObject, fsNode))
#define HGFS_SIP_LIST_HEAD_NODE(sip)    (sip->reqs->list.next)


/*
 * Local data
 */

/*
 * See requestInt.h for details.
 */

DblLnkLst_Links hgfsKReqWorkItemList;
OS_MUTEX_T *hgfsKReqWorkItemLock;
OS_ZONE_T *hgfsKReqZone;

OS_CV_T hgfsKReqWorkItemCv;

/*
 * Local functions (prototypes)
 */

static int   HgfsKReqZCtor(void *mem, int size, void *arg, int flags);
static void  HgfsKReqZDtor(void *mem, int size, void *arg);
static int   HgfsKReqZInit(void *mem, int size, int flags);
static void  HgfsKReqZFini(void *mem, int size);

/*
 * Global functions (definitions)
 */


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsKReq_SysInit --
 *
 *      This function simply initializes the hgfsKReqZone.  This is done
 *      separately from the VFS initialization routine, our caller, in order
 *      to abstract away the request allocation & support code.
 *
 * Results:
 *      Zero on success, HGFS_ERR on error.
 *
 * Side effects:
 *      hgfsKReqZone is initialized.  This routine may sleep.
 *
 *-----------------------------------------------------------------------------
 */

int
HgfsKReq_SysInit(void)
{
   int ret = 0;

   hgfsKReqZone = os_zone_create(HGFS_FS_NAME "_zone",
				 sizeof (struct HgfsKReqObject),
				 HgfsKReqZCtor, HgfsKReqZDtor, HgfsKReqZInit,
				 HgfsKReqZFini, 0, 0);

   if (!hgfsKReqZone) {
      return HGFS_ERR;
   }

   hgfsKReqWorkItemLock = os_mutex_alloc_init(HGFS_FS_NAME "_workmtx");
   if (!hgfsKReqWorkItemLock) {
      os_zone_destroy(hgfsKReqZone);
      return HGFS_ERR;
   }

   /*
    * This is a nop on Mac OS because we don't actually have a condition variable
    * to initialize.
    */
   os_cv_init(&hgfsKReqWorkItemCv, HGFS_FS_NAME "_workcv");
   DblLnkLst_Init(&hgfsKReqWorkItemList);

   /* Spawn the worker thread. */
   ret = os_thread_create(HgfsKReqWorker, &hgfsKReqWorkerState,
			  "HgfsKReqWorker", &hgfsKReqWorkerThread);

   if (ret != 0) {
      os_cv_destroy(&hgfsKReqWorkItemCv);
      os_zone_destroy(hgfsKReqZone);
      os_mutex_free(hgfsKReqWorkItemLock);
      return HGFS_ERR;
   }

   return 0;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsKReq_SysFini --
 *
 *      Hgfs request subsystem cleanup routine.  This should be called when the
 *      Hgfs client module is unloaded from the kernel.
 *
 * Results:
 *      Zero on success or errno on failure.
 *
 * Side effects:
 *      This routine may (will?) sleep.  hgfsKReqZone is destroyed.
 *
 *-----------------------------------------------------------------------------
 */

int
HgfsKReq_SysFini(void)
{
   /* Signal the worker thread to exit. */
   os_mutex_lock(hgfsKReqWorkItemLock);
   hgfsKReqWorkerState.exit = TRUE;
   os_cv_signal(&hgfsKReqWorkItemCv);

   /*
    * Sleep until the worker thread exits. hgfsKReqWorkItemLock is release by
    * by os_thread_join.
    */
   os_thread_join(hgfsKReqWorkerThread, hgfsKReqWorkItemLock);

   /*
    * Destroy resources allocated during _SysInit().
    */
   os_thread_release(hgfsKReqWorkerThread);
   os_zone_destroy(hgfsKReqZone);
   os_cv_destroy(&hgfsKReqWorkItemCv);
   os_mutex_free(hgfsKReqWorkItemLock);

   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsKReq_AllocateContainer --
 *
 *      Allocate a request container for a single file system mount.
 *
 * Results:
 *      Pointer to a new allocation container or NULL on failure.
 *
 * Side effects:
 *      This routine may sleep.
 *
 *----------------------------------------------------------------------------
 */

HgfsKReqContainerHandle
HgfsKReq_AllocateContainer(void)
{
   HgfsKReqContainer *container;

   container = os_malloc(sizeof (struct HgfsKReqContainer), M_WAITOK | M_ZERO);
   if (!container) {
      return NULL;
   }

   container->listLock = os_mutex_alloc_init("hgfs_reql_mtx");
   if (!container->listLock) {
      os_free(container, sizeof *container);
      return NULL;
   }

   DblLnkLst_Init(&container->list);

   return container;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsKReq_FreeContainer --
 *
 *      Free a request container.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

void
HgfsKReq_FreeContainer(HgfsKReqContainerHandle container) // IN: file system's
                                                          // container handle
{
   ASSERT(container);
   ASSERT(DblLnkLst_IsLinked(&container->list) == FALSE);

   os_mutex_free(container->listLock);
   os_free(container, sizeof(*container));
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsKReq_CancelRequests --
 *
 *      Cancels all allocated requests by updating their status (set to
 *      HGFS_REQ_ERROR) and waking up any waiting clients.  Also, if linked,
 *      removes any items from the work item list.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      This file system's entries are removed from the work item list are
 *      removed.
 *
 *----------------------------------------------------------------------------
 */

void
HgfsKReq_CancelRequests(HgfsKReqContainerHandle container) // IN: request container
{
   DblLnkLst_Links *currNode;
   DblLnkLst_Links *nextNode;

   DEBUG(VM_DEBUG_REQUEST, "HgfsCancelAllRequests().\n");

   ASSERT(container);

   /*
    * 1. Lock this file system's request list.
    * 2. Lock the global pending request list.
    * 3. For each request in the file system's request list:
    *    a.  Remove from the global pending request list.
    *    b.  Lock the request.
    *    c.  Set the request's state to HGFS_REQ_ERROR.
    *    d.  Signal any waiters.
    *    e.  Drop our reference, destroying the object if ours was the last.
    * 4. Unlock the global pending request list.
    * 5. Unlock the file system's request list.
    */

   os_mutex_lock(container->listLock);
   os_mutex_lock(hgfsKReqWorkItemLock);

   DEBUG(VM_DEBUG_REQUEST, "HgfsCancelAllRequests(): traversing pending request list.\n");

   DblLnkLst_ForEachSafe(currNode, nextNode, &container->list) {
      HgfsKReqObject *req;
      Bool deref = FALSE;

      /* Get a pointer to the request represented by currNode. */
      req = DblLnkLst_Container(currNode, HgfsKReqObject, fsNode);

      /*
       * If linked in the pending request list, remove it.  Note that we're
       * transferring that list's reference to ourself.  (I.e., we'll be
       * responsible for decrementing the reference count and freeing if it
       * reaches zero.)
       */
      if (DblLnkLst_IsLinked(&req->pendingNode)) {
         deref = TRUE;
         DblLnkLst_Unlink1(&req->pendingNode);
      }

      /* Force this over to the error state & wake up any waiters. */
      os_mutex_lock(req->stateLock);
      req->state = HGFS_REQ_ERROR;
      os_cv_signal(&req->stateCv);
      os_mutex_unlock(req->stateLock);

      if (deref) {
         if (os_add_atomic(&req->refcnt, -1) == 1) {
	    os_zone_free(hgfsKReqZone, req);
         }
      }
   }

   os_mutex_unlock(hgfsKReqWorkItemLock);
   os_mutex_unlock(container->listLock);

   DEBUG(VM_DEBUG_REQUEST, "HgfsCancelAllRequests() done.\n");
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsKReq_ContainerIsEmpty --
 *
 *      Indicates whether a file system, represented by its superinfo, has any
 *      outstanding HgfsKReqObjectuests.
 *
 * Results:
 *    Returns zero if list is not empty, a positive integer if it is empty.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

Bool
HgfsKReq_ContainerIsEmpty(HgfsKReqContainerHandle container)       // IN:
{
   Bool result;

   ASSERT(container);

   os_mutex_lock(container->listLock);
   result = DblLnkLst_IsLinked(&container->list) ? FALSE : TRUE;
   os_mutex_unlock(container->listLock);

   DEBUG(VM_DEBUG_REQUEST, "Container empty value: %d\n", result);

   return result;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsKReq_AllocateRequest --
 *
 *      Allocates and initializes a new request structure from the request pool.
 *      This function blocks until a request is available or it has been
 *      interrupted by a signal.
 *
 * Results:
 *      Pointer to fresh HgfsKReqHandle or NULL on failure.
 *
 * Side effects:
 *      Request inserted into caller's requests container.  This routine may
 *      sleep.
 *
 *----------------------------------------------------------------------------
 */

HgfsKReqHandle
HgfsKReq_AllocateRequest(HgfsKReqContainerHandle container,  // IN:
                         int *errorRet)                      // OUT:
{
   HgfsKReqObject *req;

   ASSERT(errorRet);
   ASSERT(container);

   *errorRet = 0;

   if (!gHgfsChannel) {
      *errorRet = EIO;
      return NULL;
   }

   /*
    * In case we don't have any channel currently, set up a new channel.
    * Note that we remember the channel from which request was allocated
    * and sent, thereby making sure that we free it via correct channel.
    */
   if (gHgfsChannel->status != HGFS_CHANNEL_CONNECTED) {
      if (!HgfsSetupNewChannel()) {
         *errorRet = EIO;
         return NULL;
      }
   }

   req = os_zone_alloc(hgfsKReqZone, M_WAITOK);
   if (!req) {
      *errorRet = ENOMEM;
      return NULL;
   }

   os_mutex_lock(container->listLock);
   DblLnkLst_LinkLast(&container->list, &req->fsNode);
   os_mutex_unlock(container->listLock);

   return req;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsKReq_ReleaseReq --
 *
 *      Routine for file systems to return a request to the pool.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      oldReq->refcnt will be decremented, and oldReq may be freed.
 *
 *----------------------------------------------------------------------------
 */

void
HgfsKReq_ReleaseRequest(HgfsKReqContainerHandle container,      // IN:
                        HgfsKReqHandle oldRequest)              // IN:
{
   DEBUG(VM_DEBUG_ENTRY, "%s\n", __func__);

   ASSERT(container);
   ASSERT(oldRequest);

   /* Dissociate request from this file system. */
   os_mutex_lock(container->listLock);
   DblLnkLst_Unlink1(&oldRequest->fsNode);
   os_mutex_unlock(container->listLock);

   /* State machine update */
   os_mutex_lock(oldRequest->stateLock);

   switch (oldRequest->state) {
   case HGFS_REQ_ALLOCATED:
   case HGFS_REQ_SUBMITTED:
      oldRequest->state = HGFS_REQ_ABANDONED;
      break;
   case HGFS_REQ_ABANDONED:
      panic("%s: Request (%p) already abandoned!\n", __func__, oldRequest);
      break;
   case HGFS_REQ_ERROR:
   case HGFS_REQ_COMPLETED:
      break;
   default:
      NOT_REACHED();
   }

   os_mutex_unlock(oldRequest->stateLock);

   /* Dereference file system from request.  If refcnt goes to zero, free. */
   if (os_add_atomic(&oldRequest->refcnt, -1) == 1) {
      os_zone_free(hgfsKReqZone, oldRequest);
   }

   DEBUG(VM_DEBUG_REQUEST, "%s done.\n", __func__);
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsKReq_SubmitRequest --
 *
 *      Queues caller's request for Guest <-> Host processing and waits for
 *      it to be processed.
 *
 * Results:
 *      Zero on success, errno if interrupted.
 *
 * Side effects:
 *      Request's state may change.
 *
 * Synchronization notes:
 *      Assumes caller holds newReq->stateLock.  (Implicit from _GetNewReq.)
 *
 *----------------------------------------------------------------------------
 */

int
HgfsKReq_SubmitRequest(HgfsKReqObject *newreq)     // IN: Request to enqueue
{
   int ret = 0;

   ASSERT(newreq);
   DEBUG(VM_DEBUG_REQUEST, "HgfsEnqueueRequest().\n");

   /*
    * Insert request on pending request list, then alert of its arrival the
    * request processor.  Since the list will also reference the request, be
    * sure to bump its count before unlocking the list!
    */

   os_mutex_lock(hgfsKReqWorkItemLock);

   /*
    * With the work item list locked, lock our object and operate on its state.
    * Typically we expect it to be in the ALLOCATED state, but if the file
    * system asynchronously cancelled all requests, it may be in ERROR instead.
    */

   os_mutex_lock(newreq->stateLock);

   switch (newreq->state) {
   case HGFS_REQ_ALLOCATED:
      /*
       * Update request's state, bump refcnt, and signal worker thread.
       */

      newreq->state = HGFS_REQ_SUBMITTED;
      os_add_atomic(&newreq->refcnt, 1);
      DblLnkLst_LinkLast(&hgfsKReqWorkItemList, &newreq->pendingNode);
      os_cv_signal(&hgfsKReqWorkItemCv);
      os_mutex_unlock(hgfsKReqWorkItemLock);
      /*
       * NB: We're still holding this request's state lock for use with
       * cv_wait_sig.
       */
      break;

   case HGFS_REQ_ERROR:
      /*
       * Bail ASAP.
       */
      os_mutex_unlock(newreq->stateLock);
      os_mutex_unlock(hgfsKReqWorkItemLock);
      return EIO;
      break;

   case HGFS_REQ_UNUSED:
   case HGFS_REQ_SUBMITTED:
   case HGFS_REQ_ABANDONED:
   case HGFS_REQ_COMPLETED:
      panic("Cannot submit object (%p) in its current state: %u",
            newreq, newreq->state);
      break;
   default:
      panic("Request object (%p) in unknown state: %u", newreq, newreq->state);
   }

   /* Sleep until request is processed or we're interrupted. */
   while (newreq->state == HGFS_REQ_SUBMITTED && ret == 0) {
      ret = os_cv_wait(&newreq->stateCv, newreq->stateLock);
   }

   /* Okay, we're finished with the state lock for now. */
   os_mutex_unlock(newreq->stateLock);

   return ret;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsKReq_GetId --
 *
 *      Return this object's unique request ID.
 *
 * Results:
 *      Object's unique request ID.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

uint32_t
HgfsKReq_GetId(HgfsKReqHandle request) // IN: Request to get the ID for
{
   ASSERT(request);

   return request->id;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsKReq_GetPayload --
 *
 *      Return a pointer to the payload area of a request.  Callers may write
 *      Hgfs packet data directly to this area.  It's guaranteed to hold at
 *      most HGFS_PACKET_MAX (6144) bytes. For Hgfs version 3, the caller should
 *      explicitly write request header (HgfsRequest) into this area.
 *
 * Results:
 *      Pointer to the payload area.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

char *
HgfsKReq_GetPayload(HgfsKReqHandle request)  // IN: Request to get pointer to payload 
{
   ASSERT(request);

   return request->payload;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsKReq_GetPayloadSize --
 *
 *      Returns the amount of data current stored in the payload.  (Typically
 *      used when the file system receives an Hgfs reply.)
 *
 * Results:
 *      Size of current payload in bytes.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

size_t
HgfsKReq_GetPayloadSize(HgfsKReqHandle request) // IN: Request to get the size of
{
   ASSERT(request);

   return request->payloadSize;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsKReq_SetPayloadSize --
 *
 *      Record the amount of data currently stored in the payload.  (Typically
 *      used when the file system finishes composing its request.)
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      Request object's payload size is modified.
 *
 *-----------------------------------------------------------------------------
 */

void
HgfsKReq_SetPayloadSize(HgfsKReqHandle request, // IN: Request object
                        size_t payloadSize)     // IN: New payload size
{
   ASSERT(request);
   ASSERT(payloadSize <= HGFS_PACKET_MAX);
   request->payloadSize = payloadSize;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsKReq_GetState --
 *
 *      Retrieves state of provided request.
 *
 * Results:
 *      Returns state of request.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------------
 */

HgfsKReqState
HgfsKReq_GetState(HgfsKReqObject *req)      // IN: Request to retrieve state of
{
   HgfsKReqState state;

   ASSERT(req);

   os_mutex_lock(req->stateLock);
   state = req->state;
   os_mutex_unlock(req->stateLock);

   return state;
}


/*
 * Local functions (definitions)
 */

/*
 *-----------------------------------------------------------------------------
 *
 * HgfsKReqZInit --
 *
 *      "The initializer is called when the memory is cached in the uma zone.
 *      this should be the same state that the destructor leaves the object in."
 *        - sys/vm/uma.h
 *
 * Results:
 *      Zero on success, errno on failure.
 *
 * Side effects:
 *      A request's mutex and condvar are initialized, ID recorded, and status
 *      set to HGFS_REQ_UNUSED.
 *
 *-----------------------------------------------------------------------------
 */

static int
HgfsKReqZInit(void *mem,     // IN: Pointer to the allocated object
              int size,      // IN: Size of item being initialized [ignored]
              int flags)     // IN: malloc(9) style flags
{
   static unsigned int id = 0;
   HgfsKReqObject *req = (HgfsKReqObject *)mem;
   ASSERT(size == sizeof *req);

   os_add_atomic(&id, 1);
   req->id = id;
   req->state = HGFS_REQ_UNUSED;
   req->stateLock = os_mutex_alloc_init("hgfs_req_mtx");
   if (!req->stateLock) {
      return ENOMEM;
   }

   os_cv_init(&req->stateCv, "hgfs_req_cv");

   /* Reset list pointers. */
   DblLnkLst_Init(&req->fsNode);
   DblLnkLst_Init(&req->pendingNode);
   DblLnkLst_Init(&req->sentNode);

   /* Clear packet of request before allocating to clients. */
   bzero(&req->__rpc_packet, sizeof req->__rpc_packet);

   return 0;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsKReqZFini --
 *
 *      "This routine is called when memory leaves a zone and is returned
 *       to the system for other uses.  It is the counter part to the
 *       init function." - sys/vm/uma.h
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      A request's mutex and condvar are destroyed.
 *
 *-----------------------------------------------------------------------------
 */

static void
HgfsKReqZFini(void *mem,     // IN: Pointer to object leaving the UMA cache
              int size)      // IN: Size of object [Ignored]
{
   HgfsKReqObject *req = (HgfsKReqObject *)mem;
   ASSERT(size == sizeof *req);
   ASSERT(req->state == HGFS_REQ_UNUSED);
   os_mutex_free(req->stateLock);
   os_cv_destroy(&req->stateCv);
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsKReqZCtor
 *
 *      "The constructor is called just before the memory is returned
 *       to the user. It may block if necessary." - sys/vm/uma.h
 *
 * Results:
 *      Zero on success, errno on failure.
 *
 * Side effects:
 *      Request's state is set to HGFS_REQ_ALLOCATED, its listNode is
 *      initialized, and its packet is zeroed out.
 *
 *-----------------------------------------------------------------------------
 */

static int
HgfsKReqZCtor(void *mem,     // IN: Pointer to memory allocated to user
              int size,      // IN: Size of allocated object [ignored]
              void *arg,     // IN: Optional argument from uma_zalloc_arg [ignored]
              int flags)     // IN: malloc(9) flags
{
   HgfsKReqObject *req = (HgfsKReqObject *)mem;

   ASSERT(size == sizeof *req);
   ASSERT(req->state == HGFS_REQ_UNUSED);
   ASSERT(DblLnkLst_IsLinked(&req->fsNode) == FALSE);
   ASSERT(DblLnkLst_IsLinked(&req->pendingNode) == FALSE);

   /* Initialize state & reference count. */
   req->state = HGFS_REQ_ALLOCATED;
   req->refcnt = 1;
   return 0;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsKReqZDtor
 *
 *      "The destructor may perform operations that differ from those performed
 *       by the initializer, but it must leave the object in the same state.
 *       This IS type stable storage.  This is called after EVERY zfree call."
 *        - sys/vm/uma.h
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      Object's state is set to HGFS_REQ_UNUSED.
 *
 *-----------------------------------------------------------------------------
 */

static void
HgfsKReqZDtor(void *mem,     // IN: Pointer to allocated object
              int size,      // IN: Size of allocated object [ignored]
              void *arg)     // IN: Argument for uma_zfree_arg [ignored]
{
   HgfsKReqObject *req = (HgfsKReqObject *)mem;

   ASSERT(req->refcnt == 0);
   ASSERT(DblLnkLst_IsLinked(&req->fsNode) == FALSE);
   ASSERT(DblLnkLst_IsLinked(&req->pendingNode) == FALSE);

   req->state = HGFS_REQ_UNUSED;
}