VirtualBox

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

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

Additions/solaris/SharedFolders: fix directory seeking bug with incorrect dirent offset. du on a shared folder works now instead of causing a kernel panic.

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

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