summaryrefslogtreecommitdiff
path: root/open-vm-tools/modules/solaris/vmhgfs/filesystem.c
blob: 7f4d9994bbd5ac9cfb3923f02f265921f397cc55 (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
/*********************************************************
 * Copyright (C) 2004 VMware, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common
 * Development and Distribution License (the "License") version 1.0
 * and no later version.  You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 *         http://www.opensource.org/licenses/cddl1.php
 *
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 *********************************************************/


/*
 * filesystem.c --
 *
 * Implementation of the filesystem level routines.  These include functions
 * that intialize, mount, unmount, and provide other various filesystem
 * information.
 */

#include <sys/param.h>          /* MAXNAMELEN */
#include <sys/file.h>           /* FKIOCTL */

#include "hgfsSolaris.h"
#include "hgfsState.h"
#include "filesystem.h"
#include "vnode.h"
#include "request.h"
#include "debug.h"

/*
 * Prototypes
 *
 * These are only needed because Solaris 10 requires that we create the vfsops
 * structure through their provided interface (vfs_setfsops()).
 */
#if HGFS_VFS_VERSION > 2
int HgfsMount(struct vfs *vfsp, struct vnode *vnodep,
              struct mounta *mntp, struct cred *credp);
int HgfsUnmount(struct vfs *vfsp, int mflag, struct cred *credp);
int HgfsRoot(struct vfs *vfsp, struct vnode **vnodepp);
int HgfsStatvfs(struct vfs *vfsp, struct statvfs64 *stats);
int HgfsSync(struct vfs *vfsp, short flags, struct cred *credp);
int HgfsVget(struct vfs *vfsp, struct vnode **vnodepp, struct fid *fidp);
int HgfsMountroot(struct vfs *vfsp, enum whymountroot reason);
int HgfsVnstate(vfs_t *vfsp, vnode_t *vp, vntrans_t trans);
#if HGFS_VFS_VERSION == 3
int HgfsFreevfs(struct vfs *vfsp);
#else
void HgfsFreevfs(struct vfs *vfsp);
#endif
#endif

#if HGFS_VFS_VERSION > 2
static vfsops_t *hgfsVfsOpsP;
#endif

/*
 * Fileystem type number given to us upon initialization.
 */
static int hgfsType;


#if HGFS_VFS_VERSION == 2
/*
 *----------------------------------------------------------------------------
 *
 * HgfsInit --
 *
 *    This is the filesystem initialization routine which is run when the
 *    filesystem is placed in the VFS switch table.
 *
 * Results:
 *    Returns zero on success.
 *
 * Side effects:
 *    The filesystem type (index number) is set to the provided value.
 *
 *----------------------------------------------------------------------------
 */

int
HgfsInit(struct vfssw *vfsswp,  // IN: VFS Switch table struct
         int fstype)            // IN: Index into the vfssw table for this filesystem
{
   if (!vfsswp) {
      cmn_err(HGFS_ERROR, "HgfsInit: received NULL input from Kernel.\n");
      return EINVAL;
   }

   DEBUG(VM_DEBUG_ENTRY, "HgfsInit().\n");

   /*
    * Hook VFS operations into switch table and save
    * filesystem type number and pointer to vfsswp.
    */
   vfsswp->vsw_vfsops = &HgfsVfsOps;
   hgfsType = fstype;

   mutex_init(&vfsswp->vsw_lock, NULL, MUTEX_DRIVER, NULL);

   DEBUG(VM_DEBUG_LOAD, "fstype: %d\n", hgfsType);
   HgfsDebugPrintVfssw("HgfsInit()", vfsswp);

   DEBUG(VM_DEBUG_DONE, "HgfsInit() done.\n");

   return 0;
}

#else   /* Implies Solaris > 9 */

/*
 *----------------------------------------------------------------------------
 *
 * HgfsInit --
 *
 *    This is the filesystem initialization routine for Solaris 10.  It creates
 *    an array of fs_operation_def_t for all the vfs operations, then calls
 *    vfs_setfsops() to assign them to the filesystem properly.
 *
 * Results:
 *    Returns 0 on success and a non-zero error code on error.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

int
HgfsInit(int fstype,    // IN: Filesystem type
         char *name)    // IN: Name of the filesystem
{
   int ret;

   /* Construct the VFS operations array to give to vfs_setfsops() */
   static fs_operation_def_t vfsOpsArr[] = {
      HGFS_VOP(VFSNAME_MOUNT, vfs_mount, HgfsMount),
      HGFS_VOP(VFSNAME_UNMOUNT, vfs_unmount, HgfsUnmount),
      HGFS_VOP(VFSNAME_ROOT, vfs_root, HgfsRoot),
      HGFS_VOP(VFSNAME_STATVFS, vfs_statvfs, HgfsStatvfs),
      HGFS_VOP(VFSNAME_VGET, vfs_vget, HgfsVget),
      HGFS_VOP(VFSNAME_MOUNTROOT, vfs_mountroot, HgfsMountroot),
      HGFS_VOP(VFSNAME_FREEVFS, vfs_freevfs, HgfsFreevfs),
      HGFS_VOP(VFSNAME_VNSTATE, vfs_vnstate, HgfsVnstate),
#if HGFS_VFS_VERSION <= 3
      HGFS_VOP(VFSNAME_SYNC,  vfs_vnstate, (fs_generic_func_p)HgfsSync),
      { NULL,               NULL                        }
#else
      HGFS_VOP(VFSNAME_SYNC, vfs_sync, HgfsSync),
      { NULL,               { NULL }},
#endif
   };

   if (!name) {
      cmn_err(HGFS_ERROR, "HgfsInit: received NULL input from Kernel.\n");
      return EINVAL;
   }

   DEBUG(VM_DEBUG_ENTRY, "HgfsInit: fstype=%d, name=\"%s\"\n", fstype, name);

   /* Assign VFS operations to our filesystem. */
   ret = vfs_setfsops(fstype, vfsOpsArr, &hgfsVfsOpsP);
   if (ret) {
      DEBUG(VM_DEBUG_FAIL, "HgfsInit: vfs_setfsops returned %d\n", ret);
      return ret;
   }

   ret = HgfsMakeVnodeOps();
   if (ret) {
      DEBUG(VM_DEBUG_FAIL, "HgfsInit: could not register HGFS Vnode Ops.\n");
      vfs_freevfsops_by_type(fstype);
      ret = EIO;
      return ret;
   }

   /* Set our filesystem type. */
   hgfsType = fstype;
   DEBUG(VM_DEBUG_DONE, "HgfsInit: done. (fstype=%d)\n", hgfsType);

   return 0;
}

#endif

/*
 *----------------------------------------------------------------------------
 *
 * HgfsFreeVfsOps --
 *
 *    Free VFS Ops created when we initialized the filesystem.
 *
 * Results:
 *    None.
 *
 * Side effects:
 *    Resets hgfsVfsOpsP back to NULL.
 *
 *----------------------------------------------------------------------------
 */


void
HgfsFreeVfsOps(void)
{
#if HGFS_VFS_VERSION > 2
   if (hgfsVfsOpsP) {
      vfs_freevfsops_by_type(hgfsType);
   }
#endif
}


/*
 * VFS Entry Points
 */

/*
 *----------------------------------------------------------------------------
 *
 * HgfsMount --
 *
 *   This function is invoked when mount(2) is called on our filesystem.
 *   The filesystem is mounted on the supplied vnode.
 *
 * Results:
 *   Returns zero on success and an appropriate error code on error.
 *
 * Side effects:
 *   The filesystem is mounted on top of vnodep.
 *
 *----------------------------------------------------------------------------
 */

int
HgfsMount(struct vfs *vfsp,     // IN: Filesystem to mount
          struct vnode *vnodep, // IN: Vnode that we are mounting on
          struct mounta *mntp,  // IN: Arguments to mount(2) from user
          struct cred *credp)   // IN: Credentials of caller
{
   HgfsSuperInfo *sip;
   HgfsMountData *mountData;
   int ret;
   dev_t dev;

   if (!vfsp || !vnodep || !mntp || !credp) {
      cmn_err(HGFS_ERROR, "HgfsMount: NULL input from Kernel.\n");
      return EINVAL;
   }

   DEBUG(VM_DEBUG_ENTRY, "HgfsMount().\n");

   //HgfsDebugPrintVfs("HgfsMount", vfsp);
   //HgfsDebugPrintVnode(VM_DEBUG_STRUCT, "HgfsMount", vnodep, FALSE);
   //HgfsDebugPrintMounta("HgfsMount", mntp);
   //HgfsDebugPrintCred("HgfsMount", credp);

   if (!HgfsSuser(credp)) {
      return EPERM;
   }

   if (mntp->datalen != sizeof *mountData) {
      DEBUG(VM_DEBUG_FAIL, "HgfsMount: bad data size (%lu vs %lu).\n",
            (unsigned long) mntp->datalen,
            (unsigned long) sizeof *mountData);
      return EINVAL;
   }

   mountData = kmem_zalloc(sizeof *mountData, HGFS_ALLOC_FLAG);
   if (!mountData) {
      return ENOMEM;
   }

   if (ddi_copyin(mntp->dataptr, mountData, sizeof *mountData,
                  mntp->flags & MS_SYSSPACE ? FKIOCTL : 0) == -1) {
      DEBUG(VM_DEBUG_FAIL, "HgfsMount: couldn't copy mount data.\n");
      ret = EFAULT;
      goto out;
   }

   /*
    * Make sure mount data matches what mount program will send us.
    */
   if (mountData->magic != HGFS_MAGIC) {
      DEBUG(VM_DEBUG_FAIL, "HgfsMount: received invalid magic value: %x\n",
            mountData->magic);
      ret = EINVAL;
      goto out;
   }

   /* We support only one instance of hgfs, at least for now */
   if (HgfsGetSuperInfo()) {
      DEBUG(VM_DEBUG_FAIL, "HgfsMount: HGFS is already mounted somewhere\n");
      ret = EBUSY;
      goto out;
   }

   /*
    * We need to find a unique device number for this VFS that will be used to
    * construct the filesystem id.
    */
   if ((dev = getudev()) == -1) {
      DEBUG(VM_DEBUG_FAIL, "HgfsMount(): getudev() failed.\n");
      ret = ENXIO;
      goto out;
   }

   DEBUG(VM_DEBUG_LOAD, "HgfsMount: dev=%lu\n", dev);

   if (vfs_devismounted(dev)) {
      DEBUG(VM_DEBUG_FAIL,
            "HgfsMount(): dev is not unique. We should loop on this.\n");
      ret = ENXIO;
      goto out;
   }

   /*
    * Fill in values of the VFS structure for the kernel.
    *
    * There are several important values that must be set.  In particular, we
    * need to create a chain of pointers so the Kernel can easily move between
    * the various filesystems mounted on the system.
    *
    *  o Each filesystem must set its vfs_vnodecovered to the vnode of the
    *    directory it is mounted upon.
    *  o Each directory that is a mount point must set v_vfsmountedhere to
    *    point to the vfs struct of the filesystem mounted there.
    *  o The root vnode of each filesystem must have the VROOT flag set in its
    *    vnode's v_flag so that the Kernel knows to consult the two previously
    *    mentioned pointers.
    */
   vfsp->vfs_vnodecovered = vnodep;
   vfsp->vfs_flag &= ~VFS_UNMOUNTED;
   vfsp->vfs_flag |= HGFS_VFS_FLAGS;
   vfsp->vfs_bsize = HGFS_VFS_BSIZE;
   vfsp->vfs_fstype = hgfsType;
   vfsp->vfs_bcount = 0;
   /* If we had mount options, we'd call vfs_setmntopt with vfsp->vfs_mntopts */

   vfsp->vfs_dev = dev;
   vfs_make_fsid(&vfsp->vfs_fsid, vfsp->vfs_dev, hgfsType);

   /*
    * Fill in value(s) of the vnode structure we are mounted on top of.  We
    * aren't allowed to modify this ourselves in Solaris 10.
    */
#  if HGFS_VFS_VERSION == 2
   vnodep->v_vfsmountedhere = vfsp;
#  endif

   HgfsInitSuperInfo(vfsp);

   sip = HgfsGetSuperInfo();
   vfsp->vfs_data = (caddr_t)sip;

   if (!sip->transportInit()) {
      DEBUG(VM_DEBUG_FAIL, "HgfsMount: failed to start transport.\n");
      HgfsClearSuperInfo();
      ret = EIO;
      goto out;
   }

   /*
    * Now create the root vnode of the filesystem.
    *
    * Note: do not change the name from "/" here without first checking that
    * HgfsMakeFullName() in vnode.c will still do the right thing.  (See the
    * comment for the ".." special case.)
    */
   ret = HgfsVnodeGet(&sip->rootVnode,                  // vnode to fill in
                      sip,                              // Superinfo
                      vfsp,                             // This filesystem
                      "/",                              // File name
                      HGFS_FILE_TYPE_DIRECTORY,         // File type
                      &sip->fileHashTable);             // File hash table
   if (ret) {
      DEBUG(VM_DEBUG_FAIL, "HgfsMount: couldn't get root vnode.\n");
      sip->transportCleanup();
      HgfsClearSuperInfo();
      ret = EIO;
      goto out;
   }

   /* We must signify that this is the root of our filesystem. */
   mutex_enter(&sip->rootVnode->v_lock);
   sip->rootVnode->v_flag |= VROOT;
   mutex_exit(&sip->rootVnode->v_lock);

   /* XXX do this? */
   VN_HOLD(vnodep);

   DEBUG(VM_DEBUG_DONE, "HgfsMount() done.\n");
   ret = 0;     /* Return success */

out:
   kmem_free(mountData, sizeof *mountData);
   return ret;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsUnmount --
 *
 *    This function is invoked when umount(2) is called on our filesystem.
 *
 * Results:
 *    Returns 0 on success and an error code on error.
 *
 * Side effects:
 *    The root vnode will be freed.
 *
 *----------------------------------------------------------------------------
 */

int
HgfsUnmount(struct vfs *vfsp,   // IN: This filesystem
            int mflag,          // IN: Unmount flags
            struct cred *credp) // IN: Credentials of caller
{
   HgfsSuperInfo *sip;
   int ret;

   DEBUG(VM_DEBUG_ENTRY, "HgfsUnmount().\n");

   if (!vfsp || !credp) {
      cmn_err(HGFS_ERROR, "HgfsUnmount: NULL input from Kernel.\n");
      return EINVAL;
   }

   /*
    * Initial check to ensure caller is root.
    */
   if (!HgfsSuser(credp)) {
      return EPERM;
   }

   sip = HgfsGetSuperInfo();
   if (!sip) {
      return EINVAL;
   }

   if (vfsp != sip->vfsp) {
      DEBUG(VM_DEBUG_ALWAYS, "HgfsUnmount: vfsp != sip->vfsp.\n");
   }

   HgfsDebugPrintVnode(VM_DEBUG_STRUCT, "HgfsUnmount",
                       vfsp->vfs_vnodecovered, FALSE);

   /* Take the request lock to prevent submitting new requests */
   mutex_enter(&sip->reqMutex);

   /*
    * Make sure there are no active files (besides the root vnode which we
    * release at the end of the function).
    */
   HgfsDebugPrintFileHashTable(&sip->fileHashTable, VM_DEBUG_STATE);

   if (!HgfsFileHashTableIsEmpty(sip, &sip->fileHashTable) &&
       !(mflag & MS_FORCE)) {
      DEBUG(VM_DEBUG_FAIL, "HgfsUnmount: there are still active files.\n");
      ret = EBUSY;
      goto out;
   }

   HgfsCancelAllRequests(sip);

   /*
    * Set unmounted flag in vfs structure.
    */
   vfsp->vfs_flag |= VFS_UNMOUNTED;

   /*
    * Close transport channel, we should not be gettign any more requests.
    */
   sip->transportCleanup();

   /*
    * Clean up fields in vnode structure of mount point and release hold on
    * vnodes for mount.
    */
#  if HGFS_VFS_VERSION == 2
   vfsp->vfs_vnodecovered->v_vfsmountedhere = NULL;
#  endif
   VN_RELE(vfsp->vfs_vnodecovered);
   VN_RELE(HGFS_ROOT_VNODE(sip));

   /*
    * Signify to the device half that the filesystem has been unmounted.
    */
   HGFS_ROOT_VNODE(sip) = NULL;
   HgfsClearSuperInfo();

   ret = 0;

out:
   mutex_exit(&sip->reqMutex);

   DEBUG(VM_DEBUG_DONE, "HgfsUnmount() done.\n");
   return ret;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsRoot --
 *
 *    This supplies the root vnode for the filesystem.
 *
 * Results:
 *    Returns zero on success and an error code on error.  On success vnodepp
 *    is set to the pointer of the root vnode.
 *
 * Side effects:
 *    The root vnode's reference count is incremented by one.
 *
 *----------------------------------------------------------------------------
 */

int
HgfsRoot(struct vfs *vfsp,              // IN: Filesystem to find root vnode of
         struct vnode **vnodepp)        // OUT: Set to pointer to root vnode of this fs
{
   HgfsSuperInfo *sip;

   if (!vfsp || !vnodepp) {
      cmn_err(HGFS_ERROR, "HgfsRoot: NULL input from Kernel.\n");
      return EINVAL;
   }

   DEBUG(VM_DEBUG_ENTRY, "HgfsRoot().\n");

   /*
    * Get the root vnode from the superinfo structure.
    */
   sip = HgfsGetSuperInfo();
   if (!sip) {
      DEBUG(VM_DEBUG_FAIL, "HgfsRoot() failed to find superinfo.\n");
      return EIO;
   }

   if (vfsp != sip->vfsp) {
      DEBUG(VM_DEBUG_ALWAYS, "HgfsRoot: vfsp != sip->vfsp.\n");
   }

   VN_HOLD( HGFS_ROOT_VNODE(sip) );
   *vnodepp = HGFS_ROOT_VNODE(sip);

   DEBUG(VM_DEBUG_LOAD, " rootvnode=%p", HGFS_ROOT_VNODE(sip));

   DEBUG(VM_DEBUG_DONE, "HgfsRoot() done.\n");
   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsStatvfs --
 *
 *    Provides statistics for the provided filesystem.  The values provided
 *    by this function are fake.
 *
 * Results:
 *    Returns 0 on success and a non-zero error code on exit.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

int
HgfsStatvfs(struct vfs *vfsp,           // IN: Filesystem to get statistics for
            struct statvfs64 *stats)    // OUT: Statistics are placed into this struct
{
   dev32_t dev32;

   DEBUG(VM_DEBUG_ENTRY, "HgfsStatvfs().\n");

   if (!stats) {
      cmn_err(HGFS_ERROR, "HgfsStatvfs: NULL input from Kernel.\n");
      return EINVAL;
   }

   /* Clear stats struct, then fill it in with our values. */
   memset(stats, 0, sizeof *stats);

   /*
    * Macros in case we need these elsewhere later.
    *
    * These were selected pretty randomly: the numbers should be large enough
    * so a user can attempt to create any reasonably sized file, but small
    * enough to be so the Kernel doesn't give callers who are using statvfs32
    * an EOVERFLOW.
    */
   #define HGFS_BLOCKS_TOTAL    0x00ffffff
   #define HGFS_BLOCKS_FREE     0x00ffefff
   #define HGFS_BLOCKS_AVAIL    0x00ffef00
   #define HGFS_FILES_TOTAL     0x00ffffff
   #define HGFS_FILES_FREE      0x00ffefff
   #define HGFS_FILES_AVAIL     0x00ffef00

   /* Compress the device number to 32-bits for consistency on 64-bit systems. */
   cmpldev(&dev32, vfsp->vfs_dev);

   stats->f_bsize   = HGFS_BLOCKSIZE;           /* Preferred fs block size */
   stats->f_frsize  = HGFS_BLOCKSIZE;           /* Fundamental fs block size */
   /* Next six are u_longlong_t */
   stats->f_blocks  = HGFS_BLOCKS_TOTAL;        /* Total blocks on fs */
   stats->f_bfree   = HGFS_BLOCKS_FREE;         /* Total free blocks */
   stats->f_bavail  = HGFS_BLOCKS_AVAIL;        /* Total blocks avail to non-root */
   stats->f_files   = HGFS_FILES_TOTAL;         /* Total files (inodes) */
   stats->f_ffree   = HGFS_FILES_FREE;          /* Total files free */
   stats->f_favail  = HGFS_FILES_AVAIL;         /* Total files avail to non-root */
   stats->f_fsid    = dev32;                    /* Filesystem id */
   stats->f_flag   &= ST_NOSUID;                /* Flags: we don't support setuid. */
   stats->f_namemax = MAXNAMELEN;               /* Max filename; use Solaris default. */

   /* Memset above and -1 of array size as n below ensure NUL termination. */
   strncpy(stats->f_basetype, HGFS_FS_NAME, sizeof stats->f_basetype - 1);
   strncpy(stats->f_fstr, HGFS_FS_NAME, sizeof stats->f_fstr - 1);

   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsSync --
 *
 *    Flushes the filesystem cache.
 *
 * Results:
 *    Returns zero on success and an error code on failure.  Currently this
 *    always succeeds.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

int
HgfsSync(struct vfs *vfsp,      // IN: Filesystem to flush
         short flags,           // XXX: ?
         struct cred *credp)    // IN: Credentials of caller
{
   //DEBUG(VM_DEBUG_ENTRY, "HgfsSync().\n");

   /*
    * We just return success and hope the host OS calls its filesystem sync
    * operation periodically as well.
    */
   return 0;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsVget --
 *
 *    Finds the vnode that matches the unique file identifier.
 *
 *    XXX: Come back to this when figure out how/if to store fidp to vnode
 *    mappings.
 *
 * Results:
 *
 * Side effects:
 *
 *
 *----------------------------------------------------------------------------
 */

int
HgfsVget(struct vfs *vfsp,              // IN: Filesystem to operate on
         struct vnode **vnodepp,        // OUT: Set to pointer of found vnode
         struct fid *fidp)              // IN: Unique file identifier for vnode
{
   DEBUG(VM_DEBUG_NOTSUP, "HgfsVget() NOTSUP.\n");

   return ENOTSUP;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsMountroot --
 *
 *    Mounts the file system on the root directory.
 *
 *    XXX: Still need to figure out when this is invoked.
 *
 * Results:
 *
 * Side effects:
 *
 *
 *----------------------------------------------------------------------------
 */

int
HgfsMountroot(struct vfs *vfsp,         // IN: Filesystem to mount
              enum whymountroot reason) // IN: Reason why mounting on root
{
   DEBUG(VM_DEBUG_NOTSUP, "HgfsMountroot() NOTSUP.\n");

   return ENOTSUP;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsReserved --
 *
 *    XXX: Is this a placeholder function in the struct?
 *
 * Results:
 *
 * Side effects:
 *
 *
 *----------------------------------------------------------------------------
 */

int
HgfsReserved(struct vfs *vfsp,
             struct vnode **vnodepp,
             char *charp)
{
   DEBUG(VM_DEBUG_NOTSUP, "HgfsReserved() NOTSUP.\n");

   return ENOTSUP;
}


/*
 *----------------------------------------------------------------------------
 *
 * HgfsFreevfs --
 *
 *    Called when a filesystem is unmounted to free the resources held by the
 *    filesystem.
 *
 *
 * Results:
 *    None.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

#if HGFS_VFS_VERSION == 3
int HgfsFreevfs(vfs_t *vfsp)
#else
void HgfsFreevfs(vfs_t *vfsp)
#endif
{
   DEBUG(VM_DEBUG_ENTRY, "HgfsFreevfs().\n");

   /*
    * The only allocation to undo here is call mutex_destroy() on the vsw_lock
    * for our filesystem's struct vfssw.  Doing this causes a system crash,
    * from a call to a mutex free function within the Kernel (that is, not from
    * our code), so we are assured that the Kernel cleans this up for us.
    *
    * In Solaris 10 it seemed that we needed to free the vnode and vfs
    * operations we had made earlier (vn_freevnodeops() and
    * vfs_freevfsops_by_type()), but this is not so.  Freeing these prevents
    * 1) multiple mounts without first reloading the module, and 2) unloading
    * the module from the Kernel.  The combination of these two meant that
    * the guest would have to be rebooted to remount the filesystem.  Because
    * of all this, the assumption is made that the Kernel takes care of
    * removing these structures for us.
    */

#if HGFS_VFS_VERSION == 3
   return 0;
#endif
}


#if HGFS_VFS_VERSION > 2
/*
 *----------------------------------------------------------------------------
 *
 * HgfsVnstate --
 *
 *    Performs the necessary operations on the provided vnode given the state
 *    transfer that has occurred.
 *
 *    The possible transfers are VNTRANS_EXISTS, VNTRANS_IDLED,
 *    VNTRANS_RECLAIMED, and VNTRANS_DESTROYED (see <sys/vfs.h>).
 *
 * Results:
 *    Returns 0 on success and a non-zero error code on error.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

int
HgfsVnstate(vfs_t *vfsp,        // IN: Pointer to our filesystem
            vnode_t *vp,        // IN: Vnode to change state of
            vntrans_t trans)    // IN: Type of state transfer
{
   DEBUG(VM_DEBUG_NOTSUP, "HgfsVnstate: entry.\n");

   return ENOTSUP;
}
#endif


/*
 *----------------------------------------------------------------------------
 *
 * HgfsSuser --
 *
 *    Correctly implements the superuser check depending on the version of
 *    Solaris.
 *
 * Results:
 *    Returns zero if this user is not superuser, returns non-zero otherwise.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------------
 */

int
HgfsSuser(struct cred *cr)      // IN: Credentials of the caller
{
   ASSERT(cr);
#if HGFS_VFS_VERSION == 2
   return suser(cr);
#else
   /*
    * I am assuming the crgetuid() is the effective uid, since the other two
    * related functions are crgetruid() and crgetsuid().
    */
   return (crgetuid(cr) == 0);
#endif
}