summaryrefslogtreecommitdiff
path: root/open-vm-tools/modules/freebsd/vmhgfs/vfsops.c
blob: a0fa8f1df0e3f67fdf70f16ed9c84ac81835a3e0 (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
/*********************************************************
 * 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
 *
 *********************************************************/

/*
 * vfsops.c --
 *
 *	VFS operations for the FreeBSD Hgfs client.
 */


#include <sys/param.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#if __FreeBSD_version >= 700000
#include <sys/priv.h>
#endif

#include "hgfs_kernel.h"
#include "request.h"
#include "debug.h"
#include "hgfsDevLinux.h"
#include "os.h"
#include "compat_freebsd.h"
#include "vfsopscommon.h"

/*
 * Local functions (prototypes)
 */

static vfs_mount_t      HgfsVfsMount;
static vfs_unmount_t    HgfsVfsUnmount;
static vfs_root_t       HgfsVfsRoot;
static vfs_statfs_t     HgfsVfsStatfs;
static vfs_init_t       HgfsVfsInit;
static vfs_uninit_t     HgfsVfsUninit;


/*
 * Global data
 */

/*
 * Hgfs VFS operations vector
 */
static struct vfsops HgfsVfsOps = {
   .vfs_mount           = HgfsVfsMount,
   .vfs_unmount         = HgfsVfsUnmount,
   .vfs_root            = HgfsVfsRoot,
   .vfs_quotactl        = vfs_stdquotactl,
   .vfs_statfs          = HgfsVfsStatfs,
   .vfs_sync            = vfs_stdsync,
   .vfs_vget            = vfs_stdvget,
   .vfs_fhtovp          = vfs_stdfhtovp,
   .vfs_checkexp        = vfs_stdcheckexp,
#if __FreeBSD_version < 700000
   .vfs_vptofh          = vfs_stdvptofh,
#endif
   .vfs_init            = HgfsVfsInit,
   .vfs_uninit          = HgfsVfsUninit,
   .vfs_extattrctl      = vfs_stdextattrctl,
   .vfs_sysctl          = vfs_stdsysctl,
};

/*
 * Kernel module glue to execute init/uninit at load/unload.
 */
VFS_SET(HgfsVfsOps, vmhgfs, 0);


/*
 * Local functions (definitions)
 */


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsVfsMount
 *
 *      "The VFS_MOUNT() macro mounts a file system into the system's
 *      namespace or updates the attributes of an already mounted file
 *      system."  (VFS_MOUNT(9).)
 *
 * Results:
 *      Zero on success, an appropriate system error on failure.
 *
 * Side effects:
 *      Done.
 *
 *-----------------------------------------------------------------------------
 */

static int
HgfsVfsMount(struct mount *mp,  // IN: structure representing the file system
             struct thread *td) // IN: thread which is mounting the file system
{
   HgfsSuperInfo *sip;
   struct vnode *vp;
   int ret = 0;
   char *target;
   int error;
   int size;
   Bool *uidSet = NULL;
   int *uid = NULL;
   Bool *gidSet = NULL;
   int *gid = NULL;

   /*
    * - Examine/validate mount flags from userland.
    * - Grab mount options from userland, validate.  (Paths, etc.)
    * - Allocate HgfsSuperInfo, root vnode; bind the two.
    * - Update mnt_flag/mnt_kern_flags (ex: MPSAFE?)
    * - vfs_getnewfsid
    * - vfs_mountedfrom
    */

   /*
    * We do not support any of the user's mount options, so fail any mount
    * attempt with a non-zero mnt_flag.  (It'd be quite a shock to find out
    * that a share successfully mounted read-only is really writeable!)
    */
   if (mp->mnt_flag != 0) {
      return EOPNOTSUPP;
   }

   /*
    * Since Hgfs requires the caller to be root, only allow mount attempts made
    * by the superuser.
    */
   if ((ret = suser(td)) != 0) {
      return ret;
   }

   /*
    * Allocate a new HgfsSuperInfo structure.  This is the super structure
    * maintained for each file system.  (With M_WAITOK, this call cannot fail.)
    */
   sip = os_malloc(sizeof *sip, M_WAITOK | M_ZERO);
   mp->mnt_data = sip;

   error = HgfsInitFileHashTable(&sip->fileHashTable);
   if (error) {
      goto out;
   }

   /*
    * Allocate the root vnode, then record it and the file system information
    * in our superinfo.
    */
   error = HgfsVnodeGetRoot(&vp, sip, mp, "/",
			    HGFS_FILE_TYPE_DIRECTORY, &sip->fileHashTable);
   if (error) {
      HgfsDestroyFileHashTable(&sip->fileHashTable);
      goto out;
   }

   sip->vfsp = mp;
   sip->rootVnode = vp;

   /* We're finished with the root vnode, so unlock it. */
   COMPAT_VOP_UNLOCK(vp, 0, td);

   /*
    * Initialize this file system's Hgfs requests container.
    */
   sip->reqs = HgfsKReq_AllocateContainer();

   /*
    * Since this implementation supports fine-grained locking, inform the kernel
    * that we're MPSAFE.  (This is in the context of protecting its own data
    * structures, not oplocks/leases with the VM's host.)
    */
   MNT_ILOCK(mp);
   mp->mnt_kern_flag |= MNTK_MPSAFE;
   MNT_IUNLOCK(mp);

   /* Get a new unique filesystem ID */
   vfs_getnewfsid(mp);

   error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &size);
   if (error || target[size - 1] != '\0') {
      target = "host:hgfs";
   }

   /* Get uidSet */
   error = vfs_getopt(mp->mnt_optnew, "uidSet", (void**)&uidSet, &size);

   if (!error && size == sizeof(Bool) && uidSet) {
      sip->uidSet = *uidSet;
   } else {
      sip->uidSet = FALSE;
   }

   /* Get uid */
   error = vfs_getopt(mp->mnt_optnew, "uid", (void**)&uid, &size);

   if (!error && size == sizeof(int) && uid) {
      sip->uid = *uid;
   } else {
      sip->uidSet = FALSE;
   }

   /* Get gidSet */
   error = vfs_getopt(mp->mnt_optnew, "gidSet", (void**)&gidSet, &size);

   if (!error && size == sizeof(Bool) && gidSet) {
      sip->gidSet = *gidSet;
   } else {
      sip->gidSet = FALSE;
   }

   /* Get gid */
   error = vfs_getopt(mp->mnt_optnew, "gid", (void**)&gid, &size);

   if (!error && size == sizeof(int) && gid) {
      sip->gid = *gid;
   } else {
      sip->gidSet = FALSE;
   }

   vfs_mountedfrom(mp, target);

   /*
    * Fill in the statfs structure. Note that even if HgfsStatfsInt
    * fails, we shall just log the error and move on, since it is
    * not a critical operation.
    */
   error = HgfsStatfsInt(vp, &mp->mnt_stat);
   if (error) {
      DEBUG(VM_DEBUG_FAIL, "HgfsStatfsInt failed with ret = %d\n", ret);
      error = 0;
   }

   DEBUG(VM_DEBUG_LOAD, "Exit\n");

out:
   if (error) {
      os_free(sip, sizeof *sip);
   }

   return error;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsVfsUnmount --
 *
 *      "VFS_UNMOUNT -- unmount a file system", VFS_UNMOUNT(9).
 *
 * Results:
 *      Zero if filesystem unmounted, otherwise errno.
 *
 * Side effects:
 *      This call may fail if the filesystem is busy & MNT_FORCE not set.
 *
 *-----------------------------------------------------------------------------
 */

static int
HgfsVfsUnmount(struct mount *mp, int mntflags, struct thread *td)
{
   HgfsSuperInfo *sip;
   int ret = 0;
   int flags = 0;

   sip = (HgfsSuperInfo *)mp->mnt_data;

   ASSERT(sip);

   /*
    * If there are pending requests & we're not being forced out, tell the user
    * that we're still busy.
    */
   if (((mntflags & MNT_FORCE) == 0) &&
       ((HgfsKReq_ContainerIsEmpty(sip->reqs) == FALSE) ||
        (HgfsFileHashTableIsEmpty(sip, &sip->fileHashTable) == FALSE))) {
      return EBUSY;
   }

   /*
    * If the user wants us out, cancel all pending Hgfs requests and fail all
    * existing vnode operations.
    */
   if (mntflags & MNT_FORCE) {
      HgfsKReq_CancelRequests(sip->reqs);
      flags |= FORCECLOSE;
   }

   /*
    * Vflush will wait until all pending vnode operations are complete.
    */
   ret = vflush(mp, 1, flags, td);
   if (ret != 0) {
      return ret;
   }

   HgfsDestroyFileHashTable(&sip->fileHashTable);

   /*
    * Now we can throw away our superinfo.  Let's reclaim everything allocated
    * during HgfsVfsMount.
    */
   HgfsKReq_FreeContainer(sip->reqs);

   mp->mnt_data = NULL;
   os_free(sip, sizeof *sip);

   DEBUG(VM_DEBUG_LOAD, "Exit\n");

   return 0;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsVfsStatvs --
 *
 *      "VFS_STATFS(9) - return file system status."
 *
 * Results:
 *      Zero on success and non-zero error code on failure.
 *
 * Side effects:
 *      Caller's statfs structure is populated.
 *
 *-----------------------------------------------------------------------------
 */

static int
HgfsVfsStatfs(struct mount *mp, struct statfs *sbp, struct thread *td)
{
   int ret = 0;
   struct vnode *vp;

   /* We always want HGFS_BLOCKSIZE to be a power of two */
   ASSERT_ON_COMPILE(HGFS_IS_POWER_OF_TWO(HGFS_BLOCKSIZE));

   /* 
    * This fills in file system ID and the type number that
    * we got from a call to vfs_getnewfsid() in HgfsVfsMount()
    */
   bcopy(&mp->mnt_stat, sbp, sizeof mp->mnt_stat);

   ret = HgfsVfsRoot(mp, LK_SHARED, &vp, td);
   if (ret) {
      DEBUG(VM_DEBUG_FAIL, "HgfsVfsRoot failed\n");
      return ret;
   }

   ret = HgfsStatfsInt(vp, sbp);
   if (ret) {
      DEBUG(VM_DEBUG_FAIL, "HgfsStatfsInt failed with ret = %d\n", ret);
      goto out;
   }

out:
   /* Drop the reference and shared lock that we acquired in HgfsVfsRoot */
   vput(vp);
   return ret;
}

/*
 *-----------------------------------------------------------------------------
 *
 * HgfsVfsRoot --
 *
 *      Retrieves the root Vnode of the specified filesystem.
 *
 * Results:
 *      Zero if successful, non-zero otherwise.
 *
 * Side effects:
 *      Sets *vpp.
 *
 *-----------------------------------------------------------------------------
 */

static int
HgfsVfsRoot(struct mount *mp,   // IN: Filesystem structure
            int flags,          // IN: Flags to vget
            struct vnode **vpp, // OUT: Address of root vnode
            struct thread *td)  // IN: Thread structure
{
   HgfsSuperInfo *sip = (HgfsSuperInfo *)mp->mnt_data;
   int ret = 0;

   *vpp = NULL;

   ret = vget(sip->rootVnode, flags, td);
   if (ret == 0) {
      *vpp = sip->rootVnode;
   }

   return ret;
}

/*
 *-----------------------------------------------------------------------------
 *
 * HgfsVfsInit --
 *
 *      Initializes the Hgfs filesystem implementation
 *
 * Results:
 *      Zero if successful, an errno-type value otherwise.
 *
 * Side effects:
 *      Initializes the hgfs request processing system.
 *
 *-----------------------------------------------------------------------------
 */

static int
HgfsVfsInit(struct vfsconf *vfsconf)
{
   int ret = 0;

   /* Initialize the os memory allocation and thread synchronization subsystem. */
   if ((ret = os_init()) != 0) {
      return ret;
   }

   ret = HgfsKReq_SysInit();

   DEBUG(VM_DEBUG_LOAD, "Hgfs filesystem loaded");

   return ret;
}

/*
 *-----------------------------------------------------------------------------
 *
 * HgfsVfsUninit --
 *
 *      Tears down the Hgfs filesystem module state
 *
 * Results:
 *      Zero if successful, an errno-type value otherwise.
 *
 * Side effects:
 *      Can no longer use any hgfs file systems.
 *
 *-----------------------------------------------------------------------------
 */

static int
HgfsVfsUninit(struct vfsconf *vfsconf)
{
   int ret = 0;

   ret = HgfsKReq_SysFini();
   os_cleanup();

   DEBUG(VM_DEBUG_LOAD, "Hgfs filesystem unloaded");
   return ret;
}