VirtualBox

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

Last change on this file since 31691 was 31691, checked in by vboxsync, 14 years ago

Additions/Solaris/SharedFolders: OSE compatibility.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.1 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 int stat_ttl = DEF_STAT_TTL_MS;
242 int fsync = 0;
243 char *optval;
244 long val;
245 char *path;
246 sfp_mount_t *handle;
247 sfnode_t *sfnode;
248
249 /*
250 * check we have permission to do the mount
251 */
252 LogFlowFunc(("sffs_mount() started\n"));
253 error = secpolicy_fs_mount(cr, mvp, vfsp);
254 if (error != 0)
255 return (error);
256
257 /*
258 * Mount point must be a directory
259 */
260 if (mvp->v_type != VDIR)
261 return (ENOTDIR);
262
263 /*
264 * no support for remount (what is it?)
265 */
266 if (uap->flags & MS_REMOUNT)
267 return (ENOTSUP);
268
269 /*
270 * Ensure that nothing else is actively in/under the mount point
271 */
272 mutex_enter(&mvp->v_lock);
273 if ((uap->flags & MS_OVERLAY) == 0 &&
274 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
275 mutex_exit(&mvp->v_lock);
276 return (EBUSY);
277 }
278 mutex_exit(&mvp->v_lock);
279
280 /*
281 * check for read only has to be done early
282 */
283 if (uap->flags & MS_RDONLY) {
284 vfsp->vfs_flag |= VFS_RDONLY;
285 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
286 }
287
288 /*
289 * UID to use for all files
290 */
291 if (vfs_optionisset(vfsp, "uid", &optval) &&
292 ddi_strtol(optval, NULL, 10, &val) == 0 &&
293 (uid_t)val == val)
294 uid = val;
295
296 /*
297 * GID to use for all files
298 */
299 if (vfs_optionisset(vfsp, "gid", &optval) &&
300 ddi_strtol(optval, NULL, 10, &val) == 0 &&
301 (gid_t)val == val)
302 gid = val;
303
304 /*
305 * ttl to use for stat caches
306 */
307 if (vfs_optionisset(vfsp, "stat_ttl", &optval) &&
308 ddi_strtol(optval, NULL, 10, &val) == 0 &&
309 (int)val == val)
310 stat_ttl = val;
311
312 /*
313 * whether to honor fsync
314 */
315 if (vfs_optionisset(vfsp, "fsync", &optval))
316 fsync = 1;
317
318 /*
319 * Any unknown options are an error
320 */
321 if ((uap->flags & MS_DATA) && uap->datalen > 0) {
322 cmn_err(CE_WARN, "sffs: unknown mount options specified");
323 return (EINVAL);
324 }
325
326 /*
327 * get the mount point pathname
328 */
329 error = sf_pn_get(uap->dir, uap, &mount_point);
330 if (error)
331 return (error);
332
333 /*
334 * find what we are mounting
335 */
336 error = sf_pn_get(uap->spec, uap, &share_name);
337 if (error) {
338 kmem_free(mount_point, strlen(mount_point) + 1);
339 return (error);
340 }
341
342 /*
343 * Invoke Hypervisor mount interface before proceeding
344 */
345 error = sfprov_mount(sfprov, share_name, &handle);
346 if (error) {
347 kmem_free(share_name, strlen(share_name) + 1);
348 kmem_free(mount_point, strlen(mount_point) + 1);
349 return (error);
350 }
351
352 /*
353 * find an available minor device number for this mount
354 */
355 mutex_enter(&sffs_minor_lock);
356 do {
357 sffs_minor = (sffs_minor + 1) & L_MAXMIN32;
358 dev = makedevice(sffs_major, sffs_minor);
359 } while (vfs_devismounted(dev));
360 mutex_exit(&sffs_minor_lock);
361
362 /*
363 * allocate and fill in the sffs structure
364 */
365 sffs = kmem_alloc(sizeof (*sffs), KM_SLEEP);
366 sffs->sf_vfsp = vfsp;
367 sffs->sf_uid = uid;
368 sffs->sf_gid = gid;
369 sffs->sf_stat_ttl = stat_ttl;
370 sffs->sf_fsync = fsync;
371 sffs->sf_share_name = share_name;
372 sffs->sf_mntpath = mount_point;
373 sffs->sf_handle = handle;
374 sffs->sf_ino = 3; /* root mount point is always '3' */
375
376 /*
377 * fill in the vfs structure
378 */
379 vfsp->vfs_data = (caddr_t)sffs;
380 vfsp->vfs_fstype = sffs_fstype;
381 vfsp->vfs_dev = dev;
382 vfsp->vfs_bsize = PAGESIZE; /* HERE JOE ??? */
383 vfsp->vfs_flag |= VFS_NOTRUNC; /* HERE JOE ???? */
384 vfs_make_fsid(&vfsp->vfs_fsid, dev, sffs_fstype);
385
386 /*
387 * create the root vnode.
388 * XXX JOE What should the path be here? is "/" really right?
389 * other options?
390 */
391 path = kmem_alloc(2, KM_SLEEP);
392 strcpy(path, ".");
393 mutex_enter(&sffs_lock);
394 sfnode = sfnode_make(sffs, path, VDIR, NULL, NULL, NULL, 0);
395 sffs->sf_rootnode = sfnode_get_vnode(sfnode);
396 sffs->sf_rootnode->v_flag |= VROOT;
397 sffs->sf_rootnode->v_vfsp = vfsp;
398 mutex_exit(&sffs_lock);
399
400 LogFlowFunc(("sffs_mount() success sffs=0x%p\n", sffs));
401 return (error);
402}
403
404static int
405sffs_unmount(vfs_t *vfsp, int flag, cred_t *cr)
406{
407 sffs_data_t *sffs = (sffs_data_t *)vfsp->vfs_data;
408 int error;
409
410 /*
411 * generic securty check
412 */
413 LogFlowFunc(("sffs_unmount() of sffs=0x%p\n", sffs));
414 if ((error = secpolicy_fs_unmount(cr, vfsp)) != 0)
415 return (error);
416
417 /*
418 * forced unmount is not supported by this file system
419 * and thus, ENOTSUP, is being returned.
420 */
421 if (flag & MS_FORCE)
422 return (ENOTSUP);
423
424 /*
425 * Make sure nothing is still in use.
426 */
427 if (sffs_purge(sffs) != 0)
428 return (EBUSY);
429
430 /*
431 * Invoke Hypervisor unmount interface before proceeding
432 */
433 error = sfprov_unmount(sffs->sf_handle);
434 if (error != 0) {
435 /* TBD anything here? */
436 }
437
438 kmem_free(sffs->sf_share_name, strlen(sffs->sf_share_name) + 1);
439 kmem_free(sffs->sf_mntpath, strlen(sffs->sf_mntpath) + 1);
440 kmem_free(sffs, sizeof(*sffs));
441 LogFlowFunc(("sffs_unmount() done\n"));
442 return (0);
443}
444
445/*
446 * return the vnode for the root of the mounted file system
447 */
448static int
449sffs_root(vfs_t *vfsp, vnode_t **vpp)
450{
451 sffs_data_t *sffs = (sffs_data_t *)vfsp->vfs_data;
452 vnode_t *vp = sffs->sf_rootnode;
453
454 VN_HOLD(vp);
455 *vpp = vp;
456 return (0);
457}
458
459/*
460 * get some stats.. fake up the rest
461 */
462static int
463sffs_statvfs(vfs_t *vfsp, statvfs64_t *sbp)
464{
465 sffs_data_t *sffs = (sffs_data_t *)vfsp->vfs_data;
466 uint64_t x;
467 uint32_t u;
468 dev32_t d32;
469 int error;
470
471 bzero(sbp, sizeof(*sbp));
472 error = sfprov_get_blksize(sffs->sf_handle, &x);
473 if (error != 0)
474 return (error);
475 sbp->f_bsize = x;
476 sbp->f_frsize = x;
477
478 error = sfprov_get_blksavail(sffs->sf_handle, &x);
479 if (error != 0)
480 return (error);
481 sbp->f_bfree = x;
482 sbp->f_bavail = x;
483 sbp->f_files = x / 4; /* some kind of reasonable value */
484 sbp->f_ffree = x / 4;
485 sbp->f_favail = x / 4;
486
487 error = sfprov_get_blksused(sffs->sf_handle, &x);
488 if (error != 0)
489 return (error);
490 sbp->f_blocks = x + sbp->f_bavail;
491
492 (void) cmpldev(&d32, vfsp->vfs_dev);
493 sbp->f_fsid = d32;
494 strcpy(&sbp->f_basetype[0], "sffs");
495 sbp->f_flag |= ST_NOSUID;
496
497 error = sfprov_get_readonly(sffs->sf_handle, &u);
498 if (error != 0)
499 return (error);
500 if (u)
501 sbp->f_flag |= ST_RDONLY;
502
503 error = sfprov_get_maxnamesize(sffs->sf_handle, &u);
504 if (error != 0)
505 return (error);
506 sbp->f_namemax = u;
507 return (0);
508}
509
510static void sffs_print(sffs_data_t *sffs)
511{
512 Log(("sffs_data_t at 0x%p\n", sffs));
513 Log((" vfs_t *sf_vfsp = 0x%p\n", sffs->sf_vfsp));
514 Log((" vnode_t *sf_rootnode = 0x%p\n", sffs->sf_rootnode));
515 Log((" uid_t sf_uid = 0x%l\n", (ulong_t)sffs->sf_uid));
516 Log((" gid_t sf_gid = 0x%l\n", (ulong_t)sffs->sf_gid));
517 Log((" char *sf_share_name = %s\n", sffs->sf_share_name));
518 Log((" char *sf_mntpath = %s\n", sffs->sf_mntpath));
519 Log((" sfp_mount_t *sf_handle = 0x%p\n", sffs->sf_handle));
520}
521
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