VirtualBox

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

Last change on this file since 30689 was 30527, checked in by vboxsync, 15 years ago

Additions/Solaris/SharedFolders: optionally honor fsync requests from the guest.

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