VirtualBox

source: vbox/trunk/src/VBox/Additions/solaris/SharedFolders/vboxfs_vfs.c@ 53402

Last change on this file since 53402 was 51729, checked in by vboxsync, 10 years ago

Recently missed header updates.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 13.9 KB
Line 
1/** @file
2 * VirtualBox File System for Solaris Guests, VFS implementation.
3 */
4
5/*
6 * Copyright (C) 2009-2014 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#include <VBox/log.h>
27#include <VBox/version.h>
28
29#include <sys/types.h>
30#include <sys/mntent.h>
31#include <sys/param.h>
32#include <sys/modctl.h>
33#include <sys/mount.h>
34#include <sys/policy.h>
35#include <sys/atomic.h>
36#include <sys/sysmacros.h>
37#include <sys/ddi.h>
38#include <sys/sunddi.h>
39#include <sys/vfs.h>
40#if !defined(VBOX_VFS_SOLARIS_10U6)
41#include <sys/vfs_opreg.h>
42#endif
43#include <sys/pathname.h>
44#include <sys/cmn_err.h>
45#include "vboxfs_prov.h"
46#include "vboxfs_vnode.h"
47#include "vboxfs_vfs.h"
48#include "vboxfs.h"
49
50#ifdef u
51#undef u
52#endif
53
54#define VBOXSOLQUOTE2(x) #x
55#define VBOXSOLQUOTE(x) VBOXSOLQUOTE2(x)
56/** The module name. */
57#define DEVICE_NAME "vboxfs"
58/** The module description as seen in 'modinfo'. */
59#define DEVICE_DESC "VirtualBox ShrdFS"
60
61
62/*
63 * Shared Folders filesystem implementation of the Solaris VFS interfaces.
64 * Much of this is cookie cutter code for Solaris filesystem implementation.
65 */
66
67/* forward declarations */
68static int sffs_init(int fstype, char *name);
69static int sffs_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
70static int sffs_unmount(vfs_t *vfsp, int flag, cred_t *cr);
71static int sffs_root(vfs_t *vfsp, vnode_t **vpp);
72static int sffs_statvfs(vfs_t *vfsp, statvfs64_t *sbp);
73
74static mntopt_t sffs_options[] = {
75 /* Option Cancels Opt Arg Flags Data */
76 {"uid", NULL, NULL, MO_HASVALUE, NULL},
77 {"gid", NULL, NULL, MO_HASVALUE, NULL},
78 {"dmode", NULL, NULL, MO_HASVALUE, NULL},
79 {"fmode", NULL, NULL, MO_HASVALUE, NULL},
80 {"dmask", NULL, NULL, MO_HASVALUE, NULL},
81 {"fmask", NULL, NULL, MO_HASVALUE, NULL},
82 {"stat_ttl", NULL, NULL, MO_HASVALUE, NULL},
83 {"fsync", NULL, NULL, 0, NULL}
84};
85
86static mntopts_t sffs_options_table = {
87 sizeof (sffs_options) / sizeof (mntopt_t),
88 sffs_options
89};
90
91static vfsdef_t sffs_vfsdef = {
92 VFSDEF_VERSION,
93 DEVICE_NAME,
94 sffs_init,
95 VSW_HASPROTO,
96 &sffs_options_table
97};
98
99static int sffs_fstype;
100static int sffs_major; /* major number for device */
101
102kmutex_t sffs_minor_lock;
103int sffs_minor; /* minor number for device */
104
105/*
106 * Module linkage information
107 */
108static struct modlfs modlfs = {
109 &mod_fsops,
110 DEVICE_DESC " " VBOX_VERSION_STRING "r" VBOXSOLQUOTE(VBOX_SVN_REV),
111 &sffs_vfsdef
112};
113
114static struct modlinkage modlinkage = {
115 MODREV_1, &modlfs, NULL
116};
117
118static sfp_connection_t *sfprov = NULL;
119
120int
121_init()
122{
123 return (mod_install(&modlinkage));
124}
125
126int
127_info(struct modinfo *modinfop)
128{
129 return (mod_info(&modlinkage, modinfop));
130}
131
132
133int
134_fini()
135{
136 int error;
137
138 error = mod_remove(&modlinkage);
139 if (error)
140 return (error);
141
142 /*
143 * Tear down the operations vectors
144 */
145 sffs_vnode_fini();
146 (void) vfs_freevfsops_by_type(sffs_fstype);
147
148 /*
149 * close connection to the provider
150 */
151 sfprov_disconnect(sfprov);
152 return (0);
153}
154
155
156static int
157sffs_init(int fstype, char *name)
158{
159#if defined(VBOX_VFS_SOLARIS_10U6)
160 static const fs_operation_def_t sffs_vfsops_template[] = {
161 VFSNAME_MOUNT, sffs_mount,
162 VFSNAME_UNMOUNT, sffs_unmount,
163 VFSNAME_ROOT, sffs_root,
164 VFSNAME_STATVFS, sffs_statvfs,
165 NULL, NULL
166 };
167#else
168 static const fs_operation_def_t sffs_vfsops_template[] = {
169 VFSNAME_MOUNT, { .vfs_mount = sffs_mount },
170 VFSNAME_UNMOUNT, { .vfs_unmount = sffs_unmount },
171 VFSNAME_ROOT, { .vfs_root = sffs_root },
172 VFSNAME_STATVFS, { .vfs_statvfs = sffs_statvfs },
173 NULL, NULL
174 };
175#endif
176 int error;
177
178 ASSERT(fstype != 0);
179 sffs_fstype = fstype;
180 LogFlowFunc(("sffs_init() name=%s\n", name));
181
182 /*
183 * This may seem a silly way to do things for now. But the code
184 * is structured to easily allow it to be used on other hypervisors
185 * which would have a different implementation of the provider.
186 * Hopefully that'll never happen. :)
187 */
188 sfprov = sfprov_connect(SFPROV_VERSION);
189 if (sfprov == NULL) {
190 cmn_err(CE_WARN, "sffs_init: couldn't init sffs provider");
191 return (ENODEV);
192 }
193
194 error = sfprov_set_show_symlinks();
195 if (error != 0) {
196 cmn_err(CE_WARN, "sffs_init: host unable to show symlinks, "
197 "rc=%d\n", error);
198 }
199
200 error = vfs_setfsops(fstype, sffs_vfsops_template, NULL);
201 if (error != 0) {
202 cmn_err(CE_WARN, "sffs_init: bad vfs ops template");
203 return (error);
204 }
205
206 error = sffs_vnode_init();
207 if (error != 0) {
208 (void) vfs_freevfsops_by_type(fstype);
209 cmn_err(CE_WARN, "sffs_init: bad vnode ops template");
210 return (error);
211 }
212
213 if ((sffs_major = getudev()) == (major_t)-1) {
214 cmn_err(CE_WARN, "sffs_init: Can't get unique device number.");
215 sffs_major = 0;
216 }
217 mutex_init(&sffs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
218 return (0);
219}
220
221/*
222 * wrapper for pn_get
223 */
224static int
225sf_pn_get(char *rawpath, struct mounta *uap, char **outpath)
226{
227 pathname_t path;
228 int error;
229
230 error = pn_get(rawpath, (uap->flags & MS_SYSSPACE) ? UIO_SYSSPACE :
231 UIO_USERSPACE, &path);
232 if (error) {
233 LogFlowFunc(("pn_get(%s) failed\n", rawpath));
234 return (error);
235 }
236 *outpath = kmem_alloc(path.pn_pathlen + 1, KM_SLEEP);
237 strcpy(*outpath, path.pn_path);
238 pn_free(&path);
239 return (0);
240}
241
242static void
243sffs_print(sffs_data_t *sffs)
244{
245 cmn_err(CE_NOTE, "sffs_data_t at 0x%p\n", sffs);
246 cmn_err(CE_NOTE, " vfs_t *sf_vfsp = 0x%p\n", sffs->sf_vfsp);
247 cmn_err(CE_NOTE, " vnode_t *sf_rootnode = 0x%p\n", sffs->sf_rootnode);
248 cmn_err(CE_NOTE, " uid_t sf_uid = 0x%lu\n", (ulong_t)sffs->sf_handle->sf_uid);
249 cmn_err(CE_NOTE, " gid_t sf_gid = 0x%lu\n", (ulong_t)sffs->sf_handle->sf_gid);
250 cmn_err(CE_NOTE, " mode_t sf_dmode = 0x%lu\n", (ulong_t)sffs->sf_handle->sf_dmode);
251 cmn_err(CE_NOTE, " mode_t sf_fmode = 0x%lu\n", (ulong_t)sffs->sf_handle->sf_fmode);
252 cmn_err(CE_NOTE, " mode_t sf_dmask = 0x%lu\n", (ulong_t)sffs->sf_handle->sf_dmask);
253 cmn_err(CE_NOTE, " mode_t sf_fmask = 0x%lu\n", (ulong_t)sffs->sf_handle->sf_fmask);
254 cmn_err(CE_NOTE, " char *sf_share_name = %s\n", sffs->sf_share_name);
255 cmn_err(CE_NOTE, " char *sf_mntpath = %s\n", sffs->sf_mntpath);
256 cmn_err(CE_NOTE, " sfp_mount_t *sf_handle = 0x%p\n", sffs->sf_handle);
257}
258
259static int
260sffs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
261{
262 sffs_data_t *sffs;
263 char *mount_point = NULL;
264 char *share_name = NULL;
265 int error;
266 dev_t dev;
267 uid_t uid = 0;
268 gid_t gid = 0;
269 mode_t dmode = ~0U;
270 mode_t fmode = ~0U;
271 mode_t dmask = 0;
272 mode_t fmask = 0;
273 int stat_ttl = DEF_STAT_TTL_MS;
274 int fsync = 0;
275 char *optval;
276 long val;
277 char *path;
278 sfp_mount_t *handle;
279 sfnode_t *sfnode;
280
281 /*
282 * check we have permission to do the mount
283 */
284 LogFlowFunc(("sffs_mount() started\n"));
285 error = secpolicy_fs_mount(cr, mvp, vfsp);
286 if (error != 0)
287 return (error);
288
289 /*
290 * Mount point must be a directory
291 */
292 if (mvp->v_type != VDIR)
293 return (ENOTDIR);
294
295 /*
296 * no support for remount (what is it?)
297 */
298 if (uap->flags & MS_REMOUNT)
299 return (ENOTSUP);
300
301 /*
302 * Ensure that nothing else is actively in/under the mount point
303 */
304 mutex_enter(&mvp->v_lock);
305 if ((uap->flags & MS_OVERLAY) == 0 &&
306 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
307 mutex_exit(&mvp->v_lock);
308 return (EBUSY);
309 }
310 mutex_exit(&mvp->v_lock);
311
312 /*
313 * check for read only has to be done early
314 */
315 if (uap->flags & MS_RDONLY) {
316 vfsp->vfs_flag |= VFS_RDONLY;
317 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
318 }
319
320 /*
321 * UID to use for all files
322 */
323 if (vfs_optionisset(vfsp, "uid", &optval) &&
324 ddi_strtol(optval, NULL, 10, &val) == 0 &&
325 (uid_t)val == val)
326 uid = val;
327
328 /*
329 * GID to use for all files
330 */
331 if (vfs_optionisset(vfsp, "gid", &optval) &&
332 ddi_strtol(optval, NULL, 10, &val) == 0 &&
333 (gid_t)val == val)
334 gid = val;
335
336 /*
337 * dmode to use for all directories
338 */
339 if (vfs_optionisset(vfsp, "dmode", &optval) &&
340 ddi_strtol(optval, NULL, 8, &val) == 0 &&
341 (mode_t)val == val)
342 dmode = val;
343
344 /*
345 * fmode to use for all files
346 */
347 if (vfs_optionisset(vfsp, "fmode", &optval) &&
348 ddi_strtol(optval, NULL, 8, &val) == 0 &&
349 (mode_t)val == val)
350 fmode = val;
351
352 /*
353 * dmask to use for all directories
354 */
355 if (vfs_optionisset(vfsp, "dmask", &optval) &&
356 ddi_strtol(optval, NULL, 8, &val) == 0 &&
357 (mode_t)val == val)
358 dmask = val;
359
360 /*
361 * fmask to use for all files
362 */
363 if (vfs_optionisset(vfsp, "fmask", &optval) &&
364 ddi_strtol(optval, NULL, 8, &val) == 0 &&
365 (mode_t)val == val)
366 fmask = val;
367
368 /*
369 * umask to use for all directories & files
370 */
371 if (vfs_optionisset(vfsp, "umask", &optval) &&
372 ddi_strtol(optval, NULL, 8, &val) == 0 &&
373 (mode_t)val == val)
374 dmask = fmask = val;
375
376 /*
377 * ttl to use for stat caches
378 */
379 if (vfs_optionisset(vfsp, "stat_ttl", &optval) &&
380 ddi_strtol(optval, NULL, 10, &val) == 0 &&
381 (int)val == val)
382 {
383 stat_ttl = val;
384 }
385 else
386 vfs_setmntopt(vfsp, "stat_ttl", VBOXSOLQUOTE(DEF_STAT_TTL_MS), 0);
387
388 /*
389 * whether to honor fsync
390 */
391 if (vfs_optionisset(vfsp, "fsync", &optval))
392 fsync = 1;
393
394 /*
395 * Any unknown options are an error
396 */
397 if ((uap->flags & MS_DATA) && uap->datalen > 0) {
398 cmn_err(CE_WARN, "sffs: unknown mount options specified");
399 return (EINVAL);
400 }
401
402 /*
403 * get the mount point pathname
404 */
405 error = sf_pn_get(uap->dir, uap, &mount_point);
406 if (error)
407 return (error);
408
409 /*
410 * find what we are mounting
411 */
412 error = sf_pn_get(uap->spec, uap, &share_name);
413 if (error) {
414 kmem_free(mount_point, strlen(mount_point) + 1);
415 return (error);
416 }
417
418 /*
419 * Invoke Hypervisor mount interface before proceeding
420 */
421 error = sfprov_mount(sfprov, share_name, &handle);
422 if (error) {
423 kmem_free(share_name, strlen(share_name) + 1);
424 kmem_free(mount_point, strlen(mount_point) + 1);
425 return (error);
426 }
427
428 /*
429 * find an available minor device number for this mount
430 */
431 mutex_enter(&sffs_minor_lock);
432 do {
433 sffs_minor = (sffs_minor + 1) & L_MAXMIN32;
434 dev = makedevice(sffs_major, sffs_minor);
435 } while (vfs_devismounted(dev));
436 mutex_exit(&sffs_minor_lock);
437
438 /*
439 * allocate and fill in the sffs structure
440 */
441 sffs = kmem_alloc(sizeof (*sffs), KM_SLEEP);
442 sffs->sf_vfsp = vfsp;
443 sffs->sf_handle = handle;
444 sffs->sf_handle->sf_uid = uid;
445 sffs->sf_handle->sf_gid = gid;
446 sffs->sf_handle->sf_dmode = dmode;
447 sffs->sf_handle->sf_fmode = fmode;
448 sffs->sf_handle->sf_dmask = dmask;
449 sffs->sf_handle->sf_fmask = fmask;
450 sffs->sf_stat_ttl = stat_ttl;
451 sffs->sf_fsync = fsync;
452 sffs->sf_share_name = share_name;
453 sffs->sf_mntpath = mount_point;
454 sffs->sf_ino = 3; /* root mount point is always '3' */
455
456 /*
457 * fill in the vfs structure
458 */
459 vfsp->vfs_data = (caddr_t)sffs;
460 vfsp->vfs_fstype = sffs_fstype;
461 vfsp->vfs_dev = dev;
462 vfsp->vfs_bsize = PAGESIZE; /* HERE JOE ??? */
463 vfsp->vfs_flag |= VFS_NOTRUNC; /* HERE JOE ???? */
464 vfs_make_fsid(&vfsp->vfs_fsid, dev, sffs_fstype);
465
466 /*
467 * create the root vnode.
468 * XXX JOE What should the path be here? is "/" really right?
469 * other options?
470 */
471 path = kmem_alloc(2, KM_SLEEP);
472 strcpy(path, ".");
473 mutex_enter(&sffs_lock);
474 sfnode = sfnode_make(sffs, path, VDIR, NULL, NULL, NULL, 0);
475 sffs->sf_rootnode = sfnode_get_vnode(sfnode);
476 sffs->sf_rootnode->v_flag |= VROOT;
477 sffs->sf_rootnode->v_vfsp = vfsp;
478 mutex_exit(&sffs_lock);
479
480 LogFlowFunc(("sffs_mount() success sffs=0x%p\n", sffs));
481#ifdef DEBUG_ramshankar
482 sffs_print(sffs);
483#endif
484 return (error);
485}
486
487static int
488sffs_unmount(vfs_t *vfsp, int flag, cred_t *cr)
489{
490 sffs_data_t *sffs = (sffs_data_t *)vfsp->vfs_data;
491 int error;
492
493 /*
494 * generic security check
495 */
496 LogFlowFunc(("sffs_unmount() of sffs=0x%p\n", sffs));
497 if ((error = secpolicy_fs_unmount(cr, vfsp)) != 0)
498 return (error);
499
500 /*
501 * forced unmount is not supported by this file system
502 * and thus, ENOTSUP, is being returned.
503 */
504 if (flag & MS_FORCE)
505 return (ENOTSUP);
506
507 /*
508 * Make sure nothing is still in use.
509 */
510 if (sffs_purge(sffs) != 0)
511 return (EBUSY);
512
513 /*
514 * Invoke Hypervisor unmount interface before proceeding
515 */
516 error = sfprov_unmount(sffs->sf_handle);
517 if (error != 0) {
518 /* TBD anything here? */
519 }
520
521 kmem_free(sffs->sf_share_name, strlen(sffs->sf_share_name) + 1);
522 kmem_free(sffs->sf_mntpath, strlen(sffs->sf_mntpath) + 1);
523 kmem_free(sffs, sizeof(*sffs));
524 LogFlowFunc(("sffs_unmount() done\n"));
525 return (0);
526}
527
528/*
529 * return the vnode for the root of the mounted file system
530 */
531static int
532sffs_root(vfs_t *vfsp, vnode_t **vpp)
533{
534 sffs_data_t *sffs = (sffs_data_t *)vfsp->vfs_data;
535 vnode_t *vp = sffs->sf_rootnode;
536
537 VN_HOLD(vp);
538 *vpp = vp;
539 return (0);
540}
541
542/*
543 * get some stats.. fake up the rest
544 */
545static int
546sffs_statvfs(vfs_t *vfsp, statvfs64_t *sbp)
547{
548 sffs_data_t *sffs = (sffs_data_t *)vfsp->vfs_data;
549 sffs_fsinfo_t fsinfo;
550 dev32_t d32;
551 int error;
552
553 bzero(sbp, sizeof(*sbp));
554 error = sfprov_get_fsinfo(sffs->sf_handle, &fsinfo);
555 if (error != 0)
556 return (error);
557
558 sbp->f_bsize = fsinfo.blksize;
559 sbp->f_frsize = fsinfo.blksize;
560
561 sbp->f_bfree = fsinfo.blksavail;
562 sbp->f_bavail = fsinfo.blksavail;
563 sbp->f_files = fsinfo.blksavail / 4; /* some kind of reasonable value */
564 sbp->f_ffree = fsinfo.blksavail / 4;
565 sbp->f_favail = fsinfo.blksavail / 4;
566
567 sbp->f_blocks = fsinfo.blksused + sbp->f_bavail;
568
569 (void) cmpldev(&d32, vfsp->vfs_dev);
570 sbp->f_fsid = d32;
571 strcpy(&sbp->f_basetype[0], "sffs");
572 sbp->f_flag |= ST_NOSUID;
573
574 if (fsinfo.readonly)
575 sbp->f_flag |= ST_RDONLY;
576
577 sbp->f_namemax = fsinfo.maxnamesize;
578 return (0);
579}
580
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette