VirtualBox

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

Last change on this file since 39072 was 39072, checked in by vboxsync, 13 years ago

Additions/solaris/SharedFolders: implement umask and interpret as octal.

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