VirtualBox

source: vbox/trunk/src/VBox/Additions/solaris/SharedFolders/vboxfs_vnode.c@ 51709

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

Additions/solaris/SharedFolder: Fix node permissions to apply mask and mode.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 54.5 KB
Line 
1/** @file
2 * VirtualBox File System for Solaris Guests, vnode implementation.
3 * Portions contributed by: Ronald.
4 */
5
6/*
7 * Copyright (C) 2009-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*
28 * Shared Folder File System is used from Solaris when run as a guest operating
29 * system on VirtualBox, though is meant to be usable with any hypervisor that
30 * can provide similar functionality. The sffs code handles all the Solaris
31 * specific semantics and relies on a provider module to actually access
32 * directories, files, etc. The provider interfaces are described in
33 * "vboxfs_prov.h" and the module implementing them is shipped as part of the
34 * VirtualBox Guest Additions for Solaris.
35 *
36 * The shared folder file system is similar to a networked file system,
37 * but with some caveats. The sffs code caches minimal information and proxies
38 * out to the provider whenever possible. Here are some things that are
39 * handled in this code and not by the proxy:
40 *
41 * - a way to open ".." from any already open directory
42 * - st_ino numbers
43 * - detecting directory changes that happened on the host.
44 *
45 * The implementation builds a cache of information for every file/directory
46 * ever accessed in all mounted sffs filesystems using sf_node structures.
47 *
48 * This information for both open or closed files can become invalid if
49 * asynchronous changes are made on the host. Solaris should not panic() in
50 * this event, but some file system operations may return unexpected errors.
51 * Information for such directories or files while they have active vnodes
52 * is removed from the regular cache and stored in a "stale" bucket until
53 * the vnode becomes completely inactive.
54 *
55 * We suppport only read-only mmap (VBOXVFS_WITH_MMAP) i.e. MAP_SHARED,
56 * MAP_PRIVATE in PROT_READ, this data caching would not be coherent with
57 * normal simultaneous read()/write() operations, nor will it be coherent
58 * with data access on the host. Writable mmap(MAP_SHARED) access is not
59 * implemented, as guaranteeing any kind of coherency with concurrent
60 * activity on the host would be near impossible with the existing
61 * interfaces.
62 *
63 * A note about locking. sffs is not a high performance file system.
64 * No fine grained locking is done. The one sffs_lock protects just about
65 * everything.
66 */
67
68#include <VBox/log.h>
69#include <iprt/asm.h>
70
71#include <unistd.h>
72#include <sys/types.h>
73#include <sys/stat.h>
74#include <sys/mntent.h>
75#include <sys/param.h>
76#include <sys/modctl.h>
77#include <sys/mount.h>
78#include <sys/policy.h>
79#include <sys/atomic.h>
80#include <sys/sysmacros.h>
81#include <sys/ddi.h>
82#include <sys/sunddi.h>
83#include <sys/vfs.h>
84#include <sys/vmsystm.h>
85#include <vm/seg_kpm.h>
86#include <vm/pvn.h>
87#if !defined(VBOX_VFS_SOLARIS_10U6)
88#include <sys/vfs_opreg.h>
89#endif
90#include <sys/pathname.h>
91#include <sys/dirent.h>
92#include <sys/fs_subr.h>
93#include <sys/time.h>
94#include <sys/cmn_err.h>
95#include "vboxfs_prov.h"
96#include "vboxfs_vnode.h"
97#include "vboxfs_vfs.h"
98
99/*
100 * Solaris 11u1b10 Extended Policy putback CR 7121445 removes secpolicy_vnode_access from sys/policy.h
101 */
102#ifdef VBOX_VFS_EXTENDED_POLICY
103int secpolicy_vnode_access(const cred_t *, vnode_t *, uid_t, mode_t);
104#endif
105
106#define VBOXVFS_WITH_MMAP
107
108static struct vnodeops *sffs_ops = NULL;
109
110kmutex_t sffs_lock;
111static avl_tree_t sfnodes;
112static avl_tree_t stale_sfnodes;
113
114/*
115 * For now we'll use an I/O buffer that doesn't page fault for VirtualBox
116 * to transfer data into.
117 */
118char *sffs_buffer;
119
120/*
121 * sfnode_compare() is needed for AVL tree functionality.
122 * The nodes are sorted by mounted filesystem, then path. If the
123 * nodes are stale, the node pointer itself is used to force uniqueness.
124 */
125static int
126sfnode_compare(const void *a, const void *b)
127{
128 sfnode_t *x = (sfnode_t *)a;
129 sfnode_t *y = (sfnode_t *)b;
130 int diff;
131
132 if (x->sf_is_stale) {
133 ASSERT(y->sf_is_stale);
134 diff = strcmp(x->sf_path, y->sf_path);
135 if (diff == 0)
136 diff = (uintptr_t)y - (uintptr_t)x;
137 } else {
138 ASSERT(!y->sf_is_stale);
139 diff = (uintptr_t)y->sf_sffs - (uintptr_t)x->sf_sffs;
140 if (diff == 0)
141 diff = strcmp(x->sf_path, y->sf_path);
142 }
143 if (diff < 0)
144 return (-1);
145 if (diff > 0)
146 return (1);
147 return (0);
148}
149
150/*
151 * Construct a new pathname given an sfnode plus an optional tail component.
152 * This handles ".." and "."
153 */
154static char *
155sfnode_construct_path(sfnode_t *node, char *tail)
156{
157 char *p;
158
159 if (strcmp(tail, ".") == 0 || strcmp(tail, "..") == 0)
160 panic("construct path for %s", tail);
161 p = kmem_alloc(strlen(node->sf_path) + 1 + strlen(tail) + 1, KM_SLEEP);
162 strcpy(p, node->sf_path);
163 strcat(p, "/");
164 strcat(p, tail);
165 return (p);
166}
167
168/*
169 * Clears the (cached) directory listing for the node.
170 */
171static void
172sfnode_clear_dir_list(sfnode_t *node)
173{
174 ASSERT(MUTEX_HELD(&sffs_lock));
175
176 while (node->sf_dir_list != NULL) {
177 sffs_dirents_t *next = node->sf_dir_list->sf_next;
178 kmem_free(node->sf_dir_list, SFFS_DIRENTS_SIZE);
179 node->sf_dir_list = next;
180 }
181}
182
183/*
184 * Open the provider file associated with a vnode. Holding the file open is
185 * the only way we have of trying to have a vnode continue to refer to the
186 * same host file in the host in light of the possibility of host side renames.
187 */
188static void
189sfnode_open(sfnode_t *node, int flag)
190{
191 int error;
192 sfp_file_t *fp;
193
194 if (node->sf_file != NULL)
195 return;
196 error = sfprov_open(node->sf_sffs->sf_handle, node->sf_path, &fp, flag);
197 if (error == 0)
198 {
199 node->sf_file = fp;
200 node->sf_flag = flag;
201 }
202 else
203 node->sf_flag = ~0;
204}
205
206/*
207 * get a new vnode reference for an sfnode
208 */
209vnode_t *
210sfnode_get_vnode(sfnode_t *node)
211{
212 vnode_t *vp;
213
214 if (node->sf_vnode != NULL) {
215 VN_HOLD(node->sf_vnode);
216 } else {
217 vp = vn_alloc(KM_SLEEP);
218 LogFlowFunc((" %s gets vnode 0x%p\n", node->sf_path, vp));
219 vp->v_type = node->sf_type;
220 vp->v_vfsp = node->sf_sffs->sf_vfsp;
221 vn_setops(vp, sffs_ops);
222 vp->v_flag = VNOSWAP;
223#ifndef VBOXVFS_WITH_MMAP
224 vp->v_flag |= VNOMAP;
225#endif
226 vn_exists(vp);
227 vp->v_data = node;
228 node->sf_vnode = vp;
229 }
230 return (node->sf_vnode);
231}
232
233/*
234 * Allocate and initialize a new sfnode and assign it a vnode
235 */
236sfnode_t *
237sfnode_make(
238 sffs_data_t *sffs,
239 char *path,
240 vtype_t type,
241 sfp_file_t *fp,
242 sfnode_t *parent, /* can be NULL for root */
243 sffs_stat_t *stat,
244 uint64_t stat_time)
245{
246 sfnode_t *node;
247 avl_index_t where;
248
249 ASSERT(MUTEX_HELD(&sffs_lock));
250 ASSERT(path != NULL);
251
252 /*
253 * build the sfnode
254 */
255 LogFlowFunc(("sffs_make(%s)\n", path));
256 node = kmem_alloc(sizeof (*node), KM_SLEEP);
257 node->sf_sffs = sffs;
258 VFS_HOLD(node->sf_sffs->sf_vfsp);
259 node->sf_path = path;
260 node->sf_ino = sffs->sf_ino++;
261 node->sf_type = type;
262 node->sf_is_stale = 0; /* never stale at creation */
263 node->sf_file = fp;
264 node->sf_flag = ~0;
265 node->sf_vnode = NULL; /* do this before any sfnode_get_vnode() */
266 node->sf_children = 0;
267 node->sf_parent = parent;
268 if (parent)
269 ++parent->sf_children;
270 node->sf_dir_list = NULL;
271 if (stat != NULL) {
272 node->sf_stat = *stat;
273 node->sf_stat_time = stat_time;
274 } else {
275 node->sf_stat_time = 0;
276 }
277
278 /*
279 * add the new node to our cache
280 */
281 if (avl_find(&sfnodes, node, &where) != NULL)
282 panic("sffs_create_sfnode(%s): duplicate sfnode_t", path);
283 avl_insert(&sfnodes, node, where);
284 return (node);
285}
286
287/*
288 * destroy an sfnode
289 */
290static void
291sfnode_destroy(sfnode_t *node)
292{
293 avl_index_t where;
294 avl_tree_t *tree;
295 sfnode_t *parent;
296top:
297 parent = node->sf_parent;
298 ASSERT(MUTEX_HELD(&sffs_lock));
299 ASSERT(node->sf_path != NULL);
300 LogFlowFunc(("sffs_destroy(%s)%s\n", node->sf_path, node->sf_is_stale ? " stale": ""));
301 if (node->sf_children != 0)
302 panic("sfnode_destroy(%s) has %d children", node->sf_path, node->sf_children);
303 if (node->sf_vnode != NULL)
304 panic("sfnode_destroy(%s) has active vnode", node->sf_path);
305
306 if (node->sf_is_stale)
307 tree = &stale_sfnodes;
308 else
309 tree = &sfnodes;
310 if (avl_find(tree, node, &where) == NULL)
311 panic("sfnode_destroy(%s) not found", node->sf_path);
312 avl_remove(tree, node);
313
314 VFS_RELE(node->sf_sffs->sf_vfsp);
315 sfnode_clear_dir_list(node);
316 kmem_free(node->sf_path, strlen(node->sf_path) + 1);
317 kmem_free(node, sizeof (*node));
318 if (parent != NULL) {
319 sfnode_clear_dir_list(parent);
320 if (parent->sf_children == 0)
321 panic("sfnode_destroy parent (%s) has no child", parent->sf_path);
322 --parent->sf_children;
323 if (parent->sf_children == 0 &&
324 parent->sf_is_stale &&
325 parent->sf_vnode == NULL) {
326 node = parent;
327 goto top;
328 }
329 }
330}
331
332/*
333 * Some sort of host operation on an sfnode has failed or it has been
334 * deleted. Mark this node and any children as stale, deleting knowledge
335 * about any which do not have active vnodes or children
336 * This also handle deleting an inactive node that was already stale.
337 */
338static void
339sfnode_make_stale(sfnode_t *node)
340{
341 sfnode_t *n;
342 int len;
343 ASSERT(MUTEX_HELD(&sffs_lock));
344 avl_index_t where;
345
346 /*
347 * First deal with any children of a directory node.
348 * If a directory becomes stale, anything below it becomes stale too.
349 */
350 if (!node->sf_is_stale && node->sf_type == VDIR) {
351 len = strlen(node->sf_path);
352
353 n = node;
354 while ((n = AVL_NEXT(&sfnodes, node)) != NULL) {
355 ASSERT(!n->sf_is_stale);
356
357 /*
358 * quit when no longer seeing children of node
359 */
360 if (n->sf_sffs != node->sf_sffs ||
361 strncmp(node->sf_path, n->sf_path, len) != 0 ||
362 n->sf_path[len] != '/')
363 break;
364
365 /*
366 * Either mark the child as stale or destroy it
367 */
368 if (n->sf_vnode == NULL && n->sf_children == 0) {
369 sfnode_destroy(n);
370 } else {
371 LogFlowFunc(("sffs_make_stale(%s) sub\n", n->sf_path));
372 sfnode_clear_dir_list(n);
373 if (avl_find(&sfnodes, n, &where) == NULL)
374 panic("sfnode_make_stale(%s)"
375 " not in sfnodes", n->sf_path);
376 avl_remove(&sfnodes, n);
377 n->sf_is_stale = 1;
378 if (avl_find(&stale_sfnodes, n, &where) != NULL)
379 panic("sffs_make_stale(%s) duplicates",
380 n->sf_path);
381 avl_insert(&stale_sfnodes, n, where);
382 }
383 }
384 }
385
386 /*
387 * Now deal with the given node.
388 */
389 if (node->sf_vnode == NULL && node->sf_children == 0) {
390 sfnode_destroy(node);
391 } else if (!node->sf_is_stale) {
392 LogFlowFunc(("sffs_make_stale(%s)\n", node->sf_path));
393 sfnode_clear_dir_list(node);
394 if (node->sf_parent)
395 sfnode_clear_dir_list(node->sf_parent);
396 if (avl_find(&sfnodes, node, &where) == NULL)
397 panic("sfnode_make_stale(%s) not in sfnodes",
398 node->sf_path);
399 avl_remove(&sfnodes, node);
400 node->sf_is_stale = 1;
401 if (avl_find(&stale_sfnodes, node, &where) != NULL)
402 panic("sffs_make_stale(%s) duplicates", node->sf_path);
403 avl_insert(&stale_sfnodes, node, where);
404 }
405}
406
407static uint64_t
408sfnode_cur_time_usec(void)
409{
410 clock_t now = drv_hztousec(ddi_get_lbolt());
411 return now;
412}
413
414static int
415sfnode_stat_cached(sfnode_t *node)
416{
417 return (sfnode_cur_time_usec() - node->sf_stat_time) <
418 node->sf_sffs->sf_stat_ttl * 1000L;
419}
420
421static void
422sfnode_invalidate_stat_cache(sfnode_t *node)
423{
424 node->sf_stat_time = 0;
425}
426
427static int
428sfnode_update_stat_cache(sfnode_t *node)
429{
430 int error;
431
432 error = sfprov_get_attr(node->sf_sffs->sf_handle, node->sf_path,
433 &node->sf_stat);
434 if (error == ENOENT)
435 sfnode_make_stale(node);
436 if (error == 0)
437 node->sf_stat_time = sfnode_cur_time_usec();
438
439 return (error);
440}
441
442/*
443 * Rename a file or a directory
444 */
445static void
446sfnode_rename(sfnode_t *node, sfnode_t *newparent, char *path)
447{
448 sfnode_t *n;
449 sfnode_t template;
450 avl_index_t where;
451 int len = strlen(path);
452 int old_len;
453 char *new_path;
454 char *tail;
455 ASSERT(MUTEX_HELD(&sffs_lock));
456
457 ASSERT(!node->sf_is_stale);
458
459 /*
460 * Have to remove anything existing that had the new name.
461 */
462 template.sf_sffs = node->sf_sffs;
463 template.sf_path = path;
464 template.sf_is_stale = 0;
465 n = avl_find(&sfnodes, &template, &where);
466 if (n != NULL)
467 sfnode_make_stale(n);
468
469 /*
470 * Do the renaming, deal with any children of this node first.
471 */
472 if (node->sf_type == VDIR) {
473 old_len = strlen(node->sf_path);
474 while ((n = AVL_NEXT(&sfnodes, node)) != NULL) {
475
476 /*
477 * quit when no longer seeing children of node
478 */
479 if (n->sf_sffs != node->sf_sffs ||
480 strncmp(node->sf_path, n->sf_path, old_len) != 0 ||
481 n->sf_path[old_len] != '/')
482 break;
483
484 /*
485 * Rename the child:
486 * - build the new path name
487 * - unlink the AVL node
488 * - assign the new name
489 * - re-insert the AVL name
490 */
491 ASSERT(strlen(n->sf_path) > old_len);
492 tail = n->sf_path + old_len; /* includes initial "/" */
493 new_path = kmem_alloc(len + strlen(tail) + 1,
494 KM_SLEEP);
495 strcpy(new_path, path);
496 strcat(new_path, tail);
497 if (avl_find(&sfnodes, n, &where) == NULL)
498 panic("sfnode_rename(%s) not in sfnodes",
499 n->sf_path);
500 avl_remove(&sfnodes, n);
501 LogFlowFunc(("sfnode_rname(%s to %s) sub\n", n->sf_path, new_path));
502 kmem_free(n->sf_path, strlen(n->sf_path) + 1);
503 n->sf_path = new_path;
504 if (avl_find(&sfnodes, n, &where) != NULL)
505 panic("sfnode_rename(%s) duplicates",
506 n->sf_path);
507 avl_insert(&sfnodes, n, where);
508 }
509 }
510
511 /*
512 * Deal with the given node.
513 */
514 if (avl_find(&sfnodes, node, &where) == NULL)
515 panic("sfnode_rename(%s) not in sfnodes", node->sf_path);
516 avl_remove(&sfnodes, node);
517 LogFlowFunc(("sfnode_rname(%s to %s)\n", node->sf_path, path));
518 kmem_free(node->sf_path, strlen(node->sf_path) + 1);
519 node->sf_path = path;
520 if (avl_find(&sfnodes, node, &where) != NULL)
521 panic("sfnode_rename(%s) duplicates", node->sf_path);
522 avl_insert(&sfnodes, node, where);
523
524 /*
525 * change the parent
526 */
527 if (node->sf_parent == NULL)
528 panic("sfnode_rename(%s) no parent", node->sf_path);
529 if (node->sf_parent->sf_children == 0)
530 panic("sfnode_rename(%s) parent has no child", node->sf_path);
531 sfnode_clear_dir_list(node->sf_parent);
532 sfnode_clear_dir_list(newparent);
533 --node->sf_parent->sf_children;
534 node->sf_parent = newparent;
535 ++newparent->sf_children;
536}
537
538/*
539 * Look for a cached node, if not found either handle ".." or try looking
540 * via the provider. Create an entry in sfnodes if found but not cached yet.
541 * If the create flag is set, a file or directory is created. If the file
542 * already existed, an error is returned.
543 * Nodes returned from this routine always have a vnode with its ref count
544 * bumped by 1.
545 */
546static sfnode_t *
547sfnode_lookup(
548 sfnode_t *dir,
549 char *name,
550 vtype_t create,
551 mode_t c_mode,
552 sffs_stat_t *stat,
553 uint64_t stat_time,
554 int *err)
555{
556 avl_index_t where;
557 sfnode_t template;
558 sfnode_t *node;
559 int error = 0;
560 int type;
561 char *fullpath;
562 sfp_file_t *fp;
563 sffs_stat_t tmp_stat;
564
565 ASSERT(MUTEX_HELD(&sffs_lock));
566
567 if (err)
568 *err = error;
569
570 /*
571 * handle referencing myself
572 */
573 if (strcmp(name, "") == 0 || strcmp(name, ".") == 0)
574 return (dir);
575
576 /*
577 * deal with parent
578 */
579 if (strcmp(name, "..") == 0)
580 return (dir->sf_parent);
581
582 /*
583 * Look for an existing node.
584 */
585 fullpath = sfnode_construct_path(dir, name);
586 template.sf_sffs = dir->sf_sffs;
587 template.sf_path = fullpath;
588 template.sf_is_stale = 0;
589 node = avl_find(&sfnodes, &template, &where);
590 if (node != NULL) {
591 kmem_free(fullpath, strlen(fullpath) + 1);
592 if (create != VNON)
593 return (NULL);
594 return (node);
595 }
596
597 /*
598 * No entry for this path currently.
599 * Check if the file exists with the provider and get the type from
600 * there.
601 */
602 if (create == VREG) {
603 type = VREG;
604 stat = &tmp_stat;
605 error = sfprov_create(dir->sf_sffs->sf_handle, fullpath, c_mode,
606 &fp, stat);
607 stat_time = sfnode_cur_time_usec();
608 } else if (create == VDIR) {
609 type = VDIR;
610 stat = &tmp_stat;
611 error = sfprov_mkdir(dir->sf_sffs->sf_handle, fullpath, c_mode,
612 &fp, stat);
613 stat_time = sfnode_cur_time_usec();
614 } else {
615 mode_t m;
616 fp = NULL;
617 type = VNON;
618 if (stat == NULL) {
619 stat = &tmp_stat;
620 error = sfprov_get_attr(dir->sf_sffs->sf_handle,
621 fullpath, stat);
622 stat_time = sfnode_cur_time_usec();
623 } else {
624 error = 0;
625 }
626 m = stat->sf_mode;
627 if (error != 0)
628 error = ENOENT;
629 else if (S_ISDIR(m))
630 type = VDIR;
631 else if (S_ISREG(m))
632 type = VREG;
633 else if (S_ISLNK(m))
634 type = VLNK;
635 }
636
637 if (err)
638 *err = error;
639
640 /*
641 * If no errors, make a new node and return it.
642 */
643 if (error) {
644 kmem_free(fullpath, strlen(fullpath) + 1);
645 return (NULL);
646 }
647 node = sfnode_make(dir->sf_sffs, fullpath, type, fp, dir, stat,
648 stat_time);
649 return (node);
650}
651
652
653/*
654 * uid and gid in sffs determine owner and group for all files.
655 */
656static int
657sfnode_access(sfnode_t *node, mode_t mode, cred_t *cr)
658{
659 sffs_data_t *sffs = node->sf_sffs;
660 mode_t m;
661 int shift = 0;
662 int error;
663 vnode_t *vp;
664
665 ASSERT(MUTEX_HELD(&sffs_lock));
666
667 /*
668 * get the mode from the cache or provider
669 */
670 if (sfnode_stat_cached(node))
671 error = 0;
672 else
673 error = sfnode_update_stat_cache(node);
674 m = (error == 0) ? (node->sf_stat.sf_mode & MODEMASK) : 0;
675
676 /*
677 * mask off the permissions based on uid/gid
678 */
679 if (crgetuid(cr) != sffs->sf_handle->sf_uid) {
680 shift += 3;
681 if (groupmember(sffs->sf_handle->sf_gid, cr) == 0)
682 shift += 3;
683 }
684 mode &= ~(m << shift);
685
686 if (mode == 0) {
687 error = 0;
688 } else {
689 /** @todo r=ramshankar: This can probably be optimized by holding static vnode
690 * templates for dir/file, as it only checks the type rather than
691 * fetching/allocating the real vnode. */
692 vp = sfnode_get_vnode(node);
693 error = secpolicy_vnode_access(cr, vp, sffs->sf_handle->sf_uid, mode);
694 VN_RELE(vp);
695 }
696 return (error);
697}
698
699
700/*
701 *
702 * Everything below this point are the vnode operations used by Solaris VFS
703 */
704static int
705sffs_readdir(
706 vnode_t *vp,
707 uio_t *uiop,
708 cred_t *cred,
709 int *eofp,
710 caller_context_t *ct,
711 int flag)
712{
713 sfnode_t *dir = VN2SFN(vp);
714 sfnode_t *node;
715 struct sffs_dirent *dirent = NULL;
716 sffs_dirents_t *cur_buf;
717 offset_t offset = 0;
718 offset_t orig_off = uiop->uio_loffset;
719 int dummy_eof;
720 int error = 0;
721
722 if (uiop->uio_iovcnt != 1)
723 return (EINVAL);
724
725 if (vp->v_type != VDIR)
726 return (ENOTDIR);
727
728 if (eofp == NULL)
729 eofp = &dummy_eof;
730 *eofp = 0;
731
732 if (uiop->uio_loffset >= MAXOFFSET_T) {
733 *eofp = 1;
734 return (0);
735 }
736
737 /*
738 * Get the directory entry names from the host. This gets all
739 * entries. These are stored in a linked list of sffs_dirents_t
740 * buffers, each of which contains a list of dirent64_t's.
741 */
742 mutex_enter(&sffs_lock);
743
744 if (dir->sf_dir_list == NULL) {
745 error = sfprov_readdir(dir->sf_sffs->sf_handle, dir->sf_path,
746 &dir->sf_dir_list, flag);
747 if (error != 0)
748 goto done;
749 }
750
751 /*
752 * Validate and skip to the desired offset.
753 */
754 cur_buf = dir->sf_dir_list;
755 offset = 0;
756
757 while (cur_buf != NULL &&
758 offset + cur_buf->sf_len <= uiop->uio_loffset) {
759 offset += cur_buf->sf_len;
760 cur_buf = cur_buf->sf_next;
761 }
762
763 if (cur_buf == NULL && offset != uiop->uio_loffset) {
764 error = EINVAL;
765 goto done;
766 }
767 if (cur_buf != NULL && offset != uiop->uio_loffset) {
768 offset_t off = offset;
769 int step;
770 dirent = &cur_buf->sf_entries[0];
771
772 while (off < uiop->uio_loffset) {
773 if (dirent->sf_entry.d_off == uiop->uio_loffset)
774 break;
775 step = sizeof(sffs_stat_t) + dirent->sf_entry.d_reclen;
776 dirent = (struct sffs_dirent *) (((char *) dirent) + step);
777 off += step;
778 }
779
780 if (off >= uiop->uio_loffset) {
781 error = EINVAL;
782 goto done;
783 }
784 }
785
786 offset = uiop->uio_loffset - offset;
787
788 /*
789 * Lookup each of the names, so that we have ino's, and copy to
790 * result buffer.
791 */
792 while (cur_buf != NULL) {
793 if (offset >= cur_buf->sf_len) {
794 cur_buf = cur_buf->sf_next;
795 offset = 0;
796 continue;
797 }
798
799 dirent = (struct sffs_dirent *)
800 (((char *) &cur_buf->sf_entries[0]) + offset);
801 if (dirent->sf_entry.d_reclen > uiop->uio_resid)
802 break;
803
804 if (strcmp(dirent->sf_entry.d_name, ".") == 0) {
805 node = dir;
806 } else if (strcmp(dirent->sf_entry.d_name, "..") == 0) {
807 node = dir->sf_parent;
808 if (node == NULL)
809 node = dir;
810 } else {
811 node = sfnode_lookup(dir, dirent->sf_entry.d_name, VNON,
812 0, &dirent->sf_stat, sfnode_cur_time_usec(), NULL);
813 if (node == NULL)
814 panic("sffs_readdir() lookup failed");
815 }
816 dirent->sf_entry.d_ino = node->sf_ino;
817
818 error = uiomove(&dirent->sf_entry, dirent->sf_entry.d_reclen, UIO_READ, uiop);
819 if (error != 0)
820 break;
821
822 uiop->uio_loffset= dirent->sf_entry.d_off;
823 offset += sizeof(sffs_stat_t) + dirent->sf_entry.d_reclen;
824 }
825 if (error == 0 && cur_buf == NULL)
826 *eofp = 1;
827done:
828 mutex_exit(&sffs_lock);
829 if (error != 0)
830 uiop->uio_loffset = orig_off;
831 return (error);
832}
833
834
835#if defined(VBOX_VFS_SOLARIS_10U6)
836/*
837 * HERE JOE.. this may need more logic, need to look at other file systems
838 */
839static int
840sffs_pathconf(
841 vnode_t *vp,
842 int cmd,
843 ulong_t *valp,
844 cred_t *cr)
845{
846 return (fs_pathconf(vp, cmd, valp, cr));
847}
848#else
849/*
850 * HERE JOE.. this may need more logic, need to look at other file systems
851 */
852static int
853sffs_pathconf(
854 vnode_t *vp,
855 int cmd,
856 ulong_t *valp,
857 cred_t *cr,
858 caller_context_t *ct)
859{
860 return (fs_pathconf(vp, cmd, valp, cr, ct));
861}
862#endif
863
864static int
865sffs_getattr(
866 vnode_t *vp,
867 vattr_t *vap,
868 int flags,
869 cred_t *cred,
870 caller_context_t *ct)
871{
872 sfnode_t *node = VN2SFN(vp);
873 sffs_data_t *sffs = node->sf_sffs;
874 mode_t mode;
875 int error = 0;
876
877 mutex_enter(&sffs_lock);
878 vap->va_type = vp->v_type;
879 vap->va_uid = sffs->sf_handle->sf_uid;
880 vap->va_gid = sffs->sf_handle->sf_gid;
881 vap->va_fsid = sffs->sf_vfsp->vfs_dev;
882 vap->va_nodeid = node->sf_ino;
883 vap->va_nlink = 1;
884 vap->va_rdev = sffs->sf_vfsp->vfs_dev;
885 vap->va_seq = 0;
886
887 if (!sfnode_stat_cached(node)) {
888 error = sfnode_update_stat_cache(node);
889 if (error != 0)
890 goto done;
891 }
892
893 vap->va_atime = node->sf_stat.sf_atime;
894 vap->va_mtime = node->sf_stat.sf_mtime;
895 vap->va_ctime = node->sf_stat.sf_ctime;
896
897 mode = node->sf_stat.sf_mode;
898 vap->va_mode = mode & MODEMASK;
899
900 vap->va_size = node->sf_stat.sf_size;
901 vap->va_blksize = 512;
902 vap->va_nblocks = (node->sf_stat.sf_alloc + 511) / 512;
903
904done:
905 mutex_exit(&sffs_lock);
906 return (error);
907}
908
909static int
910sffs_setattr(
911 vnode_t *vp,
912 vattr_t *vap,
913 int flags,
914 cred_t *cred,
915 caller_context_t *ct)
916{
917 sfnode_t *node = VN2SFN(vp);
918 int error;
919 mode_t mode;
920
921 mode = vap->va_mode;
922 if (vp->v_type == VREG)
923 mode |= S_IFREG;
924 else if (vp->v_type == VDIR)
925 mode |= S_IFDIR;
926 else if (vp->v_type == VBLK)
927 mode |= S_IFBLK;
928 else if (vp->v_type == VCHR)
929 mode |= S_IFCHR;
930 else if (vp->v_type == VLNK)
931 mode |= S_IFLNK;
932 else if (vp->v_type == VFIFO)
933 mode |= S_IFIFO;
934 else if (vp->v_type == VSOCK)
935 mode |= S_IFSOCK;
936
937 mutex_enter(&sffs_lock);
938
939 sfnode_invalidate_stat_cache(node);
940 error = sfprov_set_attr(node->sf_sffs->sf_handle, node->sf_path,
941 vap->va_mask, mode, vap->va_atime, vap->va_mtime, vap->va_ctime);
942 if (error == ENOENT)
943 sfnode_make_stale(node);
944
945 mutex_exit(&sffs_lock);
946 return (error);
947}
948
949static int
950sffs_space(
951 vnode_t *vp,
952 int cmd,
953 struct flock64 *bfp,
954 int flags,
955 offset_t off,
956 cred_t *cred,
957 caller_context_t *ct)
958{
959 sfnode_t *node = VN2SFN(vp);
960 int error;
961
962 /* we only support changing the length of the file */
963 if (bfp->l_whence != SEEK_SET || bfp->l_len != 0)
964 return ENOSYS;
965
966 mutex_enter(&sffs_lock);
967
968 sfnode_invalidate_stat_cache(node);
969
970 error = sfprov_set_size(node->sf_sffs->sf_handle, node->sf_path,
971 bfp->l_start);
972 if (error == ENOENT)
973 sfnode_make_stale(node);
974
975 mutex_exit(&sffs_lock);
976 return (error);
977}
978
979/*ARGSUSED*/
980static int
981sffs_read(
982 vnode_t *vp,
983 struct uio *uio,
984 int ioflag,
985 cred_t *cred,
986 caller_context_t *ct)
987{
988 sfnode_t *node = VN2SFN(vp);
989 int error = 0;
990 uint32_t bytes;
991 uint32_t done;
992 ulong_t offset;
993 ssize_t total;
994
995 if (vp->v_type == VDIR)
996 return (EISDIR);
997 if (vp->v_type != VREG)
998 return (EINVAL);
999 if (uio->uio_loffset >= MAXOFFSET_T)
1000 {
1001 proc_t *p = ttoproc(curthread);
1002 mutex_enter(&p->p_lock);
1003 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
1004 p, RCA_UNSAFE_SIGINFO);
1005 mutex_exit(&p->p_lock);
1006 return (EFBIG);
1007 }
1008 if (uio->uio_loffset < 0)
1009 return (EINVAL);
1010 total = uio->uio_resid;
1011 if (total == 0)
1012 return (0);
1013
1014 mutex_enter(&sffs_lock);
1015 if (node->sf_file == NULL) {
1016 ASSERT(node->sf_flag != ~0);
1017 sfnode_open(node, node->sf_flag);
1018 if (node->sf_file == NULL)
1019 return (EBADF);
1020 }
1021
1022 do {
1023 offset = uio->uio_offset;
1024 done = bytes = MIN(PAGESIZE, uio->uio_resid);
1025 error = sfprov_read(node->sf_file, sffs_buffer, offset, &done);
1026 if (error == 0 && done > 0)
1027 error = uiomove(sffs_buffer, done, UIO_READ, uio);
1028 } while (error == 0 && uio->uio_resid > 0 && done > 0);
1029
1030 mutex_exit(&sffs_lock);
1031
1032 /*
1033 * a partial read is never an error
1034 */
1035 if (total != uio->uio_resid)
1036 error = 0;
1037 return (error);
1038}
1039
1040/*ARGSUSED*/
1041static int
1042sffs_write(
1043 vnode_t *vp,
1044 struct uio *uiop,
1045 int ioflag,
1046 cred_t *cred,
1047 caller_context_t *ct)
1048{
1049 sfnode_t *node = VN2SFN(vp);
1050 int error = 0;
1051 uint32_t bytes;
1052 uint32_t done;
1053 ulong_t offset;
1054 ssize_t total;
1055 rlim64_t limit = uiop->uio_llimit;
1056
1057 if (vp->v_type == VDIR)
1058 return (EISDIR);
1059 if (vp->v_type != VREG)
1060 return (EINVAL);
1061
1062 /*
1063 * We have to hold this lock for a long time to keep
1064 * multiple FAPPEND writes from intermixing
1065 */
1066 mutex_enter(&sffs_lock);
1067 if (node->sf_file == NULL) {
1068 ASSERT(node->sf_flag != ~0);
1069 sfnode_open(node, node->sf_flag);
1070 if (node->sf_file == NULL)
1071 return (EBADF);
1072 }
1073
1074 sfnode_invalidate_stat_cache(node);
1075
1076 if (ioflag & FAPPEND) {
1077 uint64_t endoffile;
1078
1079 error = sfprov_get_size(node->sf_sffs->sf_handle,
1080 node->sf_path, &endoffile);
1081 if (error == ENOENT)
1082 sfnode_make_stale(node);
1083 if (error != 0) {
1084 mutex_exit(&sffs_lock);
1085 return (error);
1086 }
1087 uiop->uio_loffset = endoffile;
1088 }
1089
1090 if (vp->v_type != VREG || uiop->uio_loffset < 0) {
1091 mutex_exit(&sffs_lock);
1092 return (EINVAL);
1093 }
1094 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
1095 limit = MAXOFFSET_T;
1096
1097 if (uiop->uio_loffset >= limit) {
1098 proc_t *p = ttoproc(curthread);
1099 mutex_enter(&p->p_lock);
1100 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
1101 p, RCA_UNSAFE_SIGINFO);
1102 mutex_exit(&p->p_lock);
1103 mutex_exit(&sffs_lock);
1104 return (EFBIG);
1105 }
1106
1107 if (uiop->uio_loffset >= MAXOFFSET_T) {
1108 mutex_exit(&sffs_lock);
1109 return (EFBIG);
1110 }
1111
1112 total = uiop->uio_resid;
1113 if (total == 0) {
1114 mutex_exit(&sffs_lock);
1115 return (0);
1116 }
1117
1118 do {
1119 offset = uiop->uio_offset;
1120 bytes = MIN(PAGESIZE, uiop->uio_resid);
1121 if (offset + bytes >= limit) {
1122 if (offset >= limit) {
1123 error = EFBIG;
1124 break;
1125 }
1126 bytes = limit - offset;
1127 }
1128 error = uiomove(sffs_buffer, bytes, UIO_WRITE, uiop);
1129 if (error != 0)
1130 break;
1131 done = bytes;
1132 if (error == 0)
1133 error = sfprov_write(node->sf_file, sffs_buffer,
1134 offset, &done);
1135 total -= done;
1136 if (done != bytes) {
1137 uiop->uio_resid += bytes - done;
1138 break;
1139 }
1140 } while (error == 0 && uiop->uio_resid > 0 && done > 0);
1141
1142 mutex_exit(&sffs_lock);
1143
1144 /*
1145 * A short write is never really an error.
1146 */
1147 if (total != uiop->uio_resid)
1148 error = 0;
1149 return (error);
1150}
1151
1152/*ARGSUSED*/
1153static int
1154sffs_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
1155{
1156 sfnode_t *node = VN2SFN(vp);
1157 int error;
1158
1159 mutex_enter(&sffs_lock);
1160 error = sfnode_access(node, mode, cr);
1161 mutex_exit(&sffs_lock);
1162 return (error);
1163}
1164
1165/*
1166 * Lookup an entry in a directory and create a new vnode if found.
1167 */
1168/* ARGSUSED3 */
1169static int
1170sffs_lookup(
1171 vnode_t *dvp, /* the directory vnode */
1172 char *name, /* the name of the file or directory */
1173 vnode_t **vpp, /* the vnode we found or NULL */
1174 struct pathname *pnp,
1175 int flags,
1176 vnode_t *rdir,
1177 cred_t *cred,
1178 caller_context_t *ct,
1179 int *direntflags,
1180 struct pathname *realpnp)
1181{
1182 int error;
1183 sfnode_t *node;
1184
1185 /*
1186 * dvp must be a directory
1187 */
1188 if (dvp->v_type != VDIR)
1189 return (ENOTDIR);
1190
1191 /*
1192 * An empty component name or just "." means the directory itself.
1193 * Don't do any further lookup or checking.
1194 */
1195 if (strcmp(name, "") == 0 || strcmp(name, ".") == 0) {
1196 VN_HOLD(dvp);
1197 *vpp = dvp;
1198 return (0);
1199 }
1200
1201 /*
1202 * Check permission to look at this directory. We always allow "..".
1203 */
1204 mutex_enter(&sffs_lock);
1205 if (strcmp(name, "..") != 0) {
1206 error = sfnode_access(VN2SFN(dvp), VEXEC, cred);
1207 if (error) {
1208 mutex_exit(&sffs_lock);
1209 return (error);
1210 }
1211 }
1212
1213 /*
1214 * Lookup the node.
1215 */
1216 node = sfnode_lookup(VN2SFN(dvp), name, VNON, 0, NULL, 0, NULL);
1217 if (node != NULL)
1218 *vpp = sfnode_get_vnode(node);
1219 mutex_exit(&sffs_lock);
1220 return ((node == NULL) ? ENOENT : 0);
1221}
1222
1223/*ARGSUSED*/
1224static int
1225sffs_create(
1226 vnode_t *dvp,
1227 char *name,
1228 struct vattr *vap,
1229 vcexcl_t exclusive,
1230 int mode,
1231 vnode_t **vpp,
1232 cred_t *cr,
1233 int flag,
1234 caller_context_t *ct,
1235 vsecattr_t *vsecp)
1236{
1237 vnode_t *vp;
1238 sfnode_t *node;
1239 int error;
1240
1241 ASSERT(name != NULL);
1242
1243 /*
1244 * this is used for regular files, not mkdir
1245 */
1246 if (vap->va_type == VDIR)
1247 return (EISDIR);
1248 if (vap->va_type != VREG)
1249 return (EINVAL);
1250
1251 /*
1252 * is this a pre-existing file?
1253 */
1254 error = sffs_lookup(dvp, name, &vp,
1255 NULL, 0, NULL, cr, ct, NULL, NULL);
1256 if (error == ENOENT)
1257 vp = NULL;
1258 else if (error != 0)
1259 return (error);
1260
1261 /*
1262 * Operation on a pre-existing file.
1263 */
1264 if (vp != NULL) {
1265 if (exclusive == EXCL) {
1266 VN_RELE(vp);
1267 return (EEXIST);
1268 }
1269 if (vp->v_type == VDIR && (mode & VWRITE) == VWRITE) {
1270 VN_RELE(vp);
1271 return (EISDIR);
1272 }
1273
1274 mutex_enter(&sffs_lock);
1275 node = VN2SFN(vp);
1276 error = sfnode_access(node, mode, cr);
1277 if (error != 0) {
1278 mutex_exit(&sffs_lock);
1279 VN_RELE(vp);
1280 return (error);
1281 }
1282
1283 sfnode_invalidate_stat_cache(VN2SFN(dvp));
1284
1285 /*
1286 * handle truncating an existing file
1287 */
1288 if (vp->v_type == VREG && (vap->va_mask & AT_SIZE) &&
1289 vap->va_size == 0) {
1290 sfnode_open(node, flag | FTRUNC);
1291 if (node->sf_path == NULL) {
1292 mutex_exit(&sffs_lock);
1293 VN_RELE(vp);
1294 return (ENOENT);
1295 }
1296 }
1297 mutex_exit(&sffs_lock);
1298 *vpp = vp;
1299 return (0);
1300 }
1301
1302 /*
1303 * Create a new node. First check for a race creating it.
1304 */
1305 mutex_enter(&sffs_lock);
1306 node = sfnode_lookup(VN2SFN(dvp), name, VNON, 0, NULL, 0, NULL);
1307 if (node != NULL) {
1308 mutex_exit(&sffs_lock);
1309 return (EEXIST);
1310 }
1311
1312 /*
1313 * Doesn't exist yet and we have the lock, so create it.
1314 */
1315 sfnode_invalidate_stat_cache(VN2SFN(dvp));
1316 int lookuperr;
1317 node = sfnode_lookup(VN2SFN(dvp), name, VREG,
1318 (vap->va_mask & AT_MODE) ? vap->va_mode : 0, NULL, 0, &lookuperr);
1319
1320 if (node && node->sf_parent)
1321 sfnode_clear_dir_list(node->sf_parent);
1322
1323 mutex_exit(&sffs_lock);
1324 if (node == NULL)
1325 return (lookuperr);
1326 *vpp = sfnode_get_vnode(node);
1327 return (0);
1328}
1329
1330/*ARGSUSED*/
1331static int
1332sffs_mkdir(
1333 vnode_t *dvp,
1334 char *nm,
1335 vattr_t *va,
1336 vnode_t **vpp,
1337 cred_t *cred,
1338 caller_context_t *ct,
1339 int flags,
1340 vsecattr_t *vsecp)
1341{
1342 sfnode_t *node;
1343 vnode_t *vp;
1344 int error;
1345
1346 /*
1347 * These should never happen
1348 */
1349 ASSERT(nm != NULL);
1350 ASSERT(strcmp(nm, "") != 0);
1351 ASSERT(strcmp(nm, ".") != 0);
1352 ASSERT(strcmp(nm, "..") != 0);
1353
1354 /*
1355 * Do an unlocked look up first
1356 */
1357 error = sffs_lookup(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
1358 if (error == 0) {
1359 VN_RELE(vp);
1360 return (EEXIST);
1361 }
1362 if (error != ENOENT)
1363 return (error);
1364
1365 /*
1366 * Must be able to write in current directory
1367 */
1368 mutex_enter(&sffs_lock);
1369 error = sfnode_access(VN2SFN(dvp), VWRITE, cred);
1370 if (error) {
1371 mutex_exit(&sffs_lock);
1372 return (error);
1373 }
1374
1375 sfnode_invalidate_stat_cache(VN2SFN(dvp));
1376 int lookuperr = EACCES;
1377 node = sfnode_lookup(VN2SFN(dvp), nm, VDIR,
1378 (va->va_mode & AT_MODE) ? va->va_mode : 0, NULL, 0, &lookuperr);
1379
1380 if (node && node->sf_parent)
1381 sfnode_clear_dir_list(node->sf_parent);
1382
1383 mutex_exit(&sffs_lock);
1384 if (node == NULL)
1385 return (lookuperr);
1386 *vpp = sfnode_get_vnode(node);
1387 return (0);
1388}
1389
1390/*ARGSUSED*/
1391static int
1392sffs_rmdir(
1393 struct vnode *dvp,
1394 char *nm,
1395 vnode_t *cdir,
1396 cred_t *cred,
1397 caller_context_t *ct,
1398 int flags)
1399{
1400 sfnode_t *node;
1401 vnode_t *vp;
1402 int error;
1403
1404 /*
1405 * Return error when removing . and ..
1406 */
1407 if (strcmp(nm, ".") == 0 || strcmp(nm, "") == 0)
1408 return (EINVAL);
1409 if (strcmp(nm, "..") == 0)
1410 return (EEXIST);
1411
1412 error = sffs_lookup(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
1413 if (error)
1414 return (error);
1415 if (vp->v_type != VDIR) {
1416 VN_RELE(vp);
1417 return (ENOTDIR);
1418 }
1419
1420#ifdef VBOXVFS_WITH_MMAP
1421 if (vn_vfswlock(vp)) {
1422 VN_RELE(vp);
1423 return (EBUSY);
1424 }
1425#endif
1426
1427 if (vn_mountedvfs(vp)) {
1428 VN_RELE(vp);
1429 return (EBUSY);
1430 }
1431
1432 node = VN2SFN(vp);
1433
1434 mutex_enter(&sffs_lock);
1435 error = sfnode_access(VN2SFN(dvp), VEXEC | VWRITE, cred);
1436 if (error)
1437 goto done;
1438
1439 /*
1440 * If anything else is using this vnode, then fail the remove.
1441 * Why? Windows hosts can't remove something that is open,
1442 * so we have to sfprov_close() it first.
1443 * There is no errno for this - since it's not a problem on UNIX,
1444 * but EINVAL is the closest.
1445 */
1446 if (node->sf_file != NULL) {
1447 if (vp->v_count > 1) {
1448 error = EINVAL;
1449 goto done;
1450 }
1451 (void)sfprov_close(node->sf_file);
1452 node->sf_file = NULL;
1453 }
1454
1455 /*
1456 * Remove the directory on the host and mark the node as stale.
1457 */
1458 sfnode_invalidate_stat_cache(VN2SFN(dvp));
1459 error = sfprov_rmdir(node->sf_sffs->sf_handle, node->sf_path);
1460 if (error == ENOENT || error == 0)
1461 sfnode_make_stale(node);
1462
1463 if (node->sf_parent)
1464 sfnode_clear_dir_list(node->sf_parent);
1465done:
1466 mutex_exit(&sffs_lock);
1467#ifdef VBOXVFS_WITH_MMAP
1468 vn_vfsunlock(vp);
1469#endif
1470 VN_RELE(vp);
1471 return (error);
1472}
1473
1474
1475#ifdef VBOXVFS_WITH_MMAP
1476static caddr_t
1477sffs_page_map(
1478 page_t *ppage,
1479 enum seg_rw segaccess)
1480{
1481 /* Use seg_kpm driver if possible (64-bit) */
1482 if (kpm_enable)
1483 return (hat_kpm_mapin(ppage, NULL));
1484 ASSERT(segaccess == S_READ || segaccess == S_WRITE);
1485 return (ppmapin(ppage, PROT_READ | ((segaccess == S_WRITE) ? PROT_WRITE : 0), (caddr_t)-1));
1486}
1487
1488
1489static void
1490sffs_page_unmap(
1491 page_t *ppage,
1492 caddr_t addr)
1493{
1494 if (kpm_enable)
1495 hat_kpm_mapout(ppage, NULL, addr);
1496 else
1497 ppmapout(addr);
1498}
1499
1500
1501/*
1502 * Called when there's no page in the cache. This will create new page(s) and read
1503 * the file data into it.
1504 */
1505static int
1506sffs_readpages(
1507 vnode_t *dvp,
1508 offset_t off,
1509 page_t *pagelist[],
1510 size_t pagelistsize,
1511 struct seg *segp,
1512 caddr_t addr,
1513 enum seg_rw segaccess)
1514{
1515 ASSERT(MUTEX_HELD(&sffs_lock));
1516
1517 int error = 0;
1518 u_offset_t io_off, total;
1519 size_t io_len;
1520 page_t *ppages;
1521 page_t *pcur;
1522
1523 sfnode_t *node = VN2SFN(dvp);
1524 ASSERT(node);
1525 ASSERT(node->sf_file);
1526
1527 if (pagelistsize == PAGESIZE)
1528 {
1529 io_off = off;
1530 io_len = PAGESIZE;
1531 ppages = page_create_va(dvp, io_off, io_len, PG_WAIT | PG_EXCL, segp, addr);
1532 }
1533 else
1534 ppages = pvn_read_kluster(dvp, off, segp, addr, &io_off, &io_len, off, pagelistsize, 0);
1535
1536 /* If page already exists return success */
1537 if (!ppages)
1538 {
1539 *pagelist = NULL;
1540 return (0);
1541 }
1542
1543 /*
1544 * Map & read page-by-page.
1545 */
1546 total = io_off + io_len;
1547 pcur = ppages;
1548 while (io_off < total)
1549 {
1550 ASSERT3U(io_off, ==, pcur->p_offset);
1551
1552 caddr_t virtaddr = sffs_page_map(pcur, segaccess);
1553 uint32_t bytes = PAGESIZE;
1554 error = sfprov_read(node->sf_file, virtaddr, io_off, &bytes);
1555 /*
1556 * If we reuse pages without zero'ing them, one process can mmap() and read-past the length
1557 * to read previously mmap'd contents (from possibly other processes).
1558 */
1559 if (error == 0 && bytes < PAGESIZE)
1560 memset(virtaddr + bytes, 0, PAGESIZE - bytes);
1561 sffs_page_unmap(pcur, virtaddr);
1562 if (error != 0)
1563 {
1564 cmn_err(CE_WARN, "sffs_readpages: sfprov_read() failed. error=%d bytes=%u\n", error, bytes);
1565 /* Get rid of all kluster pages read & bail. */
1566 pvn_read_done(ppages, B_ERROR);
1567 return (error);
1568 }
1569 pcur = pcur->p_next;
1570 io_off += PAGESIZE;
1571 }
1572
1573 /*
1574 * Fill in the pagelist from kluster at the requested offset.
1575 */
1576 pvn_plist_init(ppages, pagelist, pagelistsize, off, io_len, segaccess);
1577 ASSERT(pagelist == NULL || (*pagelist)->p_offset == off);
1578 return (0);
1579}
1580
1581
1582/*ARGSUSED*/
1583static int
1584sffs_getpage(
1585 vnode_t *dvp,
1586 offset_t off,
1587 size_t len,
1588 uint_t *protp,
1589 page_t *pagelist[],
1590 size_t pagelistsize,
1591 struct seg *segp,
1592 caddr_t addr,
1593 enum seg_rw segaccess,
1594 cred_t *credp
1595#if !defined(VBOX_VFS_SOLARIS_10U6)
1596 , caller_context_t *ct
1597#endif
1598 )
1599{
1600 int error = 0;
1601 int is_recursive = 0;
1602 page_t **pageliststart = pagelist;
1603 sfnode_t *node = VN2SFN(dvp);
1604 ASSERT(node);
1605 ASSERT(node->sf_file);
1606
1607 if (segaccess == S_WRITE)
1608 return (ENOSYS); /* Will this ever happen? */
1609
1610 /* Don't bother about faultahead for now. */
1611 if (pagelist == NULL)
1612 return (0);
1613
1614 if (len > pagelistsize)
1615 len = pagelistsize;
1616 else
1617 len = P2ROUNDUP(len, PAGESIZE);
1618 ASSERT(pagelistsize >= len);
1619
1620 if (protp)
1621 *protp = PROT_ALL;
1622
1623 /*
1624 * The buffer passed to sffs_write may be mmap'd so we may get a
1625 * pagefault there, in which case we'll end up here with this thread
1626 * already owning the mutex. Mutexes aren't recursive.
1627 */
1628 if (mutex_owner(&sffs_lock) == curthread)
1629 is_recursive = 1;
1630 else
1631 mutex_enter(&sffs_lock);
1632
1633 /* Don't map pages past end of the file. */
1634 if (off + len > node->sf_stat.sf_size + PAGEOFFSET)
1635 {
1636 if (!is_recursive)
1637 mutex_exit(&sffs_lock);
1638 return (EFAULT);
1639 }
1640
1641 while (len > 0)
1642 {
1643 /*
1644 * Look for pages in the requested offset range, or create them if we can't find any.
1645 */
1646 if ((*pagelist = page_lookup(dvp, off, SE_SHARED)) != NULL)
1647 *(pagelist + 1) = NULL;
1648 else if ((error = sffs_readpages(dvp, off, pagelist, pagelistsize, segp, addr, segaccess)) != 0)
1649 {
1650 while (pagelist > pageliststart)
1651 page_unlock(*--pagelist);
1652
1653 *pagelist = NULL;
1654 if (!is_recursive)
1655 mutex_exit(&sffs_lock);
1656 return (error);
1657 }
1658
1659 while (*pagelist)
1660 {
1661 ASSERT3U((*pagelist)->p_offset, ==, off);
1662 off += PAGESIZE;
1663 addr += PAGESIZE;
1664 if (len > 0)
1665 {
1666 ASSERT3U(len, >=, PAGESIZE);
1667 len -= PAGESIZE;
1668 }
1669
1670 ASSERT3U(pagelistsize, >=, PAGESIZE);
1671 pagelistsize -= PAGESIZE;
1672 pagelist++;
1673 }
1674 }
1675
1676 /*
1677 * Fill the page list array with any pages left in the cache.
1678 */
1679 while ( pagelistsize > 0
1680 && (*pagelist++ = page_lookup_nowait(dvp, off, SE_SHARED)))
1681 {
1682 off += PAGESIZE;
1683 pagelistsize -= PAGESIZE;
1684 }
1685
1686 *pagelist = NULL;
1687 if (!is_recursive)
1688 mutex_exit(&sffs_lock);
1689 return (error);
1690}
1691
1692
1693/*ARGSUSED*/
1694static int
1695sffs_putpage(
1696 vnode_t *dvp,
1697 offset_t off,
1698 size_t len,
1699 int flags,
1700 cred_t *credp
1701#if !defined(VBOX_VFS_SOLARIS_10U6)
1702 , caller_context_t *ct
1703#endif
1704 )
1705{
1706 /*
1707 * We don't support PROT_WRITE mmaps.
1708 */
1709 return (ENOSYS);
1710}
1711
1712
1713/*ARGSUSED*/
1714static int
1715sffs_discardpage(
1716 vnode_t *dvp,
1717 page_t *ppage,
1718 u_offset_t *poff,
1719 size_t *plen,
1720 int flags,
1721 cred_t *pcred)
1722{
1723 /*
1724 * This would not get invoked i.e. via pvn_vplist_dirty() since we don't support
1725 * PROT_WRITE mmaps and therefore will not have dirty pages.
1726 */
1727 pvn_write_done(ppage, B_INVAL | B_ERROR | B_FORCE);
1728 return (0);
1729}
1730
1731
1732/*ARGSUSED*/
1733static int
1734sffs_map(
1735 vnode_t *dvp,
1736 offset_t off,
1737 struct as *asp,
1738 caddr_t *addrp,
1739 size_t len,
1740 uchar_t prot,
1741 uchar_t maxprot,
1742 uint_t flags,
1743 cred_t *credp
1744#if !defined(VBOX_VFS_SOLARIS_10U6)
1745 , caller_context_t *ct
1746#endif
1747 )
1748{
1749 /*
1750 * Invocation: mmap()->smmap_common()->VOP_MAP()->sffs_map(). Once the
1751 * segment driver creates the new segment via segvn_create(), it'll
1752 * invoke down the line VOP_ADDMAP()->sffs_addmap()
1753 */
1754 int error = 0;
1755 sfnode_t *node = VN2SFN(dvp);
1756 ASSERT(node);
1757 if ((flags & MAP_SHARED) && (prot & PROT_WRITE))
1758 return (ENOTSUP);
1759
1760 if (off < 0 || len > MAXOFFSET_T - off)
1761 return (ENXIO);
1762
1763 if (dvp->v_type != VREG)
1764 return (ENODEV);
1765
1766 if (dvp->v_flag & VNOMAP)
1767 return (ENOSYS);
1768
1769 if (vn_has_mandatory_locks(dvp, node->sf_stat.sf_mode))
1770 return (EAGAIN);
1771
1772 mutex_enter(&sffs_lock);
1773 as_rangelock(asp);
1774
1775#if defined(VBOX_VFS_SOLARIS_10U6)
1776 if ((flags & MAP_FIXED) == 0)
1777 {
1778 map_addr(addrp, len, off, 1, flags);
1779 if (*addrp == NULL)
1780 error = ENOMEM;
1781 }
1782 else
1783 as_unmap(asp, *addrp, len); /* User specified address, remove any previous mappings */
1784#else
1785 error = choose_addr(asp, addrp, len, off, ADDR_VACALIGN, flags);
1786#endif
1787
1788 if (error)
1789 {
1790 as_rangeunlock(asp);
1791 mutex_exit(&sffs_lock);
1792 return (error);
1793 }
1794
1795 segvn_crargs_t vnodeargs;
1796 memset(&vnodeargs, 0, sizeof(vnodeargs));
1797 vnodeargs.vp = dvp;
1798 vnodeargs.cred = credp;
1799 vnodeargs.offset = off;
1800 vnodeargs.type = flags & MAP_TYPE;
1801 vnodeargs.prot = prot;
1802 vnodeargs.maxprot = maxprot;
1803 vnodeargs.flags = flags & ~MAP_TYPE;
1804 vnodeargs.amp = NULL; /* anon. mapping */
1805 vnodeargs.szc = 0; /* preferred page size code */
1806 vnodeargs.lgrp_mem_policy_flags = 0;
1807
1808 error = as_map(asp, *addrp, len, segvn_create, &vnodeargs);
1809
1810 as_rangeunlock(asp);
1811 mutex_exit(&sffs_lock);
1812 return (error);
1813}
1814
1815
1816/*ARGSUSED*/
1817static int
1818sffs_addmap(
1819 vnode_t *dvp,
1820 offset_t off,
1821 struct as *asp,
1822 caddr_t addr,
1823 size_t len,
1824 uchar_t prot,
1825 uchar_t maxprot,
1826 uint_t flags,
1827 cred_t *credp
1828#if !defined(VBOX_VFS_SOLARIS_10U6)
1829 , caller_context_t *ct
1830#endif
1831 )
1832{
1833 if (dvp->v_flag & VNOMAP)
1834 return (ENOSYS);
1835 return (0);
1836}
1837
1838
1839/*ARGSUSED*/
1840static int
1841sffs_delmap(
1842 vnode_t *dvp,
1843 offset_t off,
1844 struct as *asp,
1845 caddr_t addr,
1846 size_t len,
1847 uint_t prot,
1848 uint_t maxprot,
1849 uint_t flags,
1850 cred_t *credp
1851#if !defined(VBOX_VFS_SOLARIS_10U6)
1852 , caller_context_t *ct
1853#endif
1854 )
1855{
1856 if (dvp->v_flag & VNOMAP)
1857 return (ENOSYS);
1858
1859 return (0);
1860}
1861#endif /* VBOXVFS_WITH_MMAP */
1862
1863
1864/*ARGSUSED*/
1865static int
1866sffs_readlink(
1867 vnode_t *vp,
1868 uio_t *uiop,
1869 cred_t *cred
1870#if !defined(VBOX_VFS_SOLARIS_10U6)
1871 ,
1872 caller_context_t *ct
1873#endif
1874 )
1875{
1876 sfnode_t *node;
1877 int error = 0;
1878 char *target = NULL;
1879
1880 if (uiop->uio_iovcnt != 1)
1881 return (EINVAL);
1882
1883 if (vp->v_type != VLNK)
1884 return (EINVAL);
1885
1886 mutex_enter(&sffs_lock);
1887 node = VN2SFN(vp);
1888
1889 target = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1890
1891 error = sfprov_readlink(node->sf_sffs->sf_handle, node->sf_path, target,
1892 MAXPATHLEN);
1893 if (error)
1894 goto done;
1895
1896 error = uiomove(target, strlen(target), UIO_READ, uiop);
1897
1898done:
1899 mutex_exit(&sffs_lock);
1900 if (target)
1901 kmem_free(target, MAXPATHLEN);
1902 return (error);
1903}
1904
1905
1906/*ARGSUSED*/
1907static int
1908sffs_symlink(
1909 vnode_t *dvp,
1910 char *linkname,
1911 vattr_t *vap,
1912 char *target,
1913 cred_t *cred
1914#if !defined(VBOX_VFS_SOLARIS_10U6)
1915 ,
1916 caller_context_t *ct,
1917 int flags
1918#endif
1919 )
1920{
1921 sfnode_t *dir;
1922 sfnode_t *node;
1923 sffs_stat_t stat;
1924 int error = 0;
1925 char *fullpath;
1926
1927 /*
1928 * These should never happen
1929 */
1930 ASSERT(linkname != NULL);
1931 ASSERT(strcmp(linkname, "") != 0);
1932 ASSERT(strcmp(linkname, ".") != 0);
1933 ASSERT(strcmp(linkname, "..") != 0);
1934
1935 /*
1936 * Basic checks.
1937 */
1938 if (vap->va_type != VLNK)
1939 return (EINVAL);
1940
1941 mutex_enter(&sffs_lock);
1942
1943 if (sfnode_lookup(VN2SFN(dvp), linkname, VNON, 0, NULL, 0, NULL) !=
1944 NULL) {
1945 error = EEXIST;
1946 goto done;
1947 }
1948
1949 dir = VN2SFN(dvp);
1950 error = sfnode_access(dir, VWRITE, cred);
1951 if (error)
1952 goto done;
1953
1954 /*
1955 * Create symlink. Note that we ignore vap->va_mode because generally
1956 * we can't change the attributes of the symlink itself.
1957 */
1958 fullpath = sfnode_construct_path(dir, linkname);
1959 error = sfprov_symlink(dir->sf_sffs->sf_handle, fullpath, target,
1960 &stat);
1961 kmem_free(fullpath, strlen(fullpath) + 1);
1962 if (error)
1963 goto done;
1964
1965 node = sfnode_lookup(dir, linkname, VLNK, 0, &stat,
1966 sfnode_cur_time_usec(), NULL);
1967
1968 sfnode_invalidate_stat_cache(dir);
1969 sfnode_clear_dir_list(dir);
1970
1971done:
1972 mutex_exit(&sffs_lock);
1973 return (error);
1974}
1975
1976
1977/*ARGSUSED*/
1978static int
1979sffs_remove(
1980 vnode_t *dvp,
1981 char *name,
1982 cred_t *cred,
1983 caller_context_t *ct,
1984 int flags)
1985{
1986 vnode_t *vp;
1987 sfnode_t *node;
1988 int error;
1989
1990 /*
1991 * These should never happen
1992 */
1993 ASSERT(name != NULL);
1994 ASSERT(strcmp(name, "..") != 0);
1995
1996 error = sffs_lookup(dvp, name, &vp,
1997 NULL, 0, NULL, cred, ct, NULL, NULL);
1998 if (error)
1999 return (error);
2000 node = VN2SFN(vp);
2001
2002 mutex_enter(&sffs_lock);
2003 error = sfnode_access(VN2SFN(dvp), VEXEC | VWRITE, cred);
2004 if (error)
2005 goto done;
2006
2007 /*
2008 * If anything else is using this vnode, then fail the remove.
2009 * Why? Windows hosts can't sfprov_remove() a file that is open,
2010 * so we have to sfprov_close() it first.
2011 * There is no errno for this - since it's not a problem on UNIX,
2012 * but ETXTBSY is the closest.
2013 */
2014 if (node->sf_file != NULL) {
2015 if (vp->v_count > 1) {
2016 error = ETXTBSY;
2017 goto done;
2018 }
2019 (void)sfprov_close(node->sf_file);
2020 node->sf_file = NULL;
2021 }
2022
2023 /*
2024 * Remove the file on the host and mark the node as stale.
2025 */
2026 sfnode_invalidate_stat_cache(VN2SFN(dvp));
2027
2028 error = sfprov_remove(node->sf_sffs->sf_handle, node->sf_path,
2029 node->sf_type == VLNK);
2030 if (error == ENOENT || error == 0)
2031 sfnode_make_stale(node);
2032
2033 if (node->sf_parent)
2034 sfnode_clear_dir_list(node->sf_parent);
2035done:
2036 mutex_exit(&sffs_lock);
2037 VN_RELE(vp);
2038 return (error);
2039}
2040
2041/*ARGSUSED*/
2042static int
2043sffs_rename(
2044 vnode_t *old_dir,
2045 char *old_nm,
2046 vnode_t *new_dir,
2047 char *new_nm,
2048 cred_t *cred,
2049 caller_context_t *ct,
2050 int flags)
2051{
2052 char *newpath;
2053 int error;
2054 sfnode_t *node;
2055
2056 if (strcmp(new_nm, "") == 0 ||
2057 strcmp(new_nm, ".") == 0 ||
2058 strcmp(new_nm, "..") == 0 ||
2059 strcmp(old_nm, "") == 0 ||
2060 strcmp(old_nm, ".") == 0 ||
2061 strcmp(old_nm, "..") == 0)
2062 return (EINVAL);
2063
2064 /*
2065 * make sure we have permission to do the rename
2066 */
2067 mutex_enter(&sffs_lock);
2068 error = sfnode_access(VN2SFN(old_dir), VEXEC | VWRITE, cred);
2069 if (error == 0 && new_dir != old_dir)
2070 error = sfnode_access(VN2SFN(new_dir), VEXEC | VWRITE, cred);
2071 if (error)
2072 goto done;
2073
2074 node = sfnode_lookup(VN2SFN(old_dir), old_nm, VNON, 0, NULL, 0, NULL);
2075 if (node == NULL) {
2076 error = ENOENT;
2077 goto done;
2078 }
2079
2080 /*
2081 * Rename the file on the host and in our caches.
2082 */
2083 sfnode_invalidate_stat_cache(node);
2084 sfnode_invalidate_stat_cache(VN2SFN(old_dir));
2085 sfnode_invalidate_stat_cache(VN2SFN(new_dir));
2086
2087 newpath = sfnode_construct_path(VN2SFN(new_dir), new_nm);
2088 error = sfprov_rename(node->sf_sffs->sf_handle, node->sf_path, newpath,
2089 node->sf_type == VDIR);
2090 if (error == 0)
2091 sfnode_rename(node, VN2SFN(new_dir), newpath);
2092 else {
2093 kmem_free(newpath, strlen(newpath) + 1);
2094 if (error == ENOENT)
2095 sfnode_make_stale(node);
2096 }
2097done:
2098 mutex_exit(&sffs_lock);
2099 return (error);
2100}
2101
2102
2103/*ARGSUSED*/
2104static int
2105sffs_fsync(vnode_t *vp, int flag, cred_t *cr, caller_context_t *ct)
2106{
2107 sfnode_t *node;
2108 int error;
2109
2110 /*
2111 * Ask the host to sync any data it may have cached for open files.
2112 */
2113 mutex_enter(&sffs_lock);
2114 node = VN2SFN(vp);
2115 if (node->sf_file == NULL)
2116 error = EBADF;
2117 else if (node->sf_sffs->sf_fsync)
2118 error = sfprov_fsync(node->sf_file);
2119 else
2120 error = 0;
2121 mutex_exit(&sffs_lock);
2122 return (error);
2123}
2124
2125/*
2126 * This may be the last reference, possibly time to close the file and
2127 * destroy the vnode. If the sfnode is stale, we'll destroy that too.
2128 */
2129/*ARGSUSED*/
2130static void
2131#if defined(VBOX_VFS_SOLARIS_10U6)
2132sffs_inactive(vnode_t *vp, cred_t *cr)
2133#else
2134sffs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
2135#endif
2136{
2137 sfnode_t *node;
2138
2139 /*
2140 * nothing to do if this isn't the last use
2141 */
2142 mutex_enter(&sffs_lock);
2143 node = VN2SFN(vp);
2144 mutex_enter(&vp->v_lock);
2145 if (vp->v_count > 1) {
2146 --vp->v_count;
2147 mutex_exit(&vp->v_lock);
2148 mutex_exit(&sffs_lock);
2149 return;
2150 }
2151
2152 if (vn_has_cached_data(vp)) {
2153#ifdef VBOXVFS_WITH_MMAP
2154 /* We're fine with releasing the vnode lock here as we should be covered by the sffs_lock */
2155 mutex_exit(&vp->v_lock);
2156 /* We won't have any dirty pages, this will just invalidate (destroy) the pages and move it to the cachelist. */
2157 pvn_vplist_dirty(vp, 0 /* offset */, sffs_discardpage, B_INVAL, cr);
2158 mutex_enter(&vp->v_lock);
2159#else
2160 panic("sffs_inactive() found cached data");
2161#endif
2162 }
2163
2164 /*
2165 * destroy the vnode
2166 */
2167 node->sf_vnode = NULL;
2168 mutex_exit(&vp->v_lock);
2169 vn_invalid(vp);
2170 vn_free(vp);
2171 LogFlowFunc((" %s vnode cleared\n", node->sf_path));
2172
2173 /*
2174 * Close the sf_file for the node.
2175 */
2176 if (node->sf_file != NULL) {
2177 (void)sfprov_close(node->sf_file);
2178 node->sf_file = NULL;
2179 }
2180
2181 /*
2182 * Free the directory entries for the node. This should normally
2183 * have been taken care of in sffs_close(), but better safe than
2184 * sorry.
2185 */
2186 sfnode_clear_dir_list(node);
2187
2188 /*
2189 * If the node is stale, we can also destroy it.
2190 */
2191 if (node->sf_is_stale && node->sf_children == 0)
2192 sfnode_destroy(node);
2193
2194 mutex_exit(&sffs_lock);
2195 return;
2196}
2197
2198/*
2199 * All the work for this is really done in sffs_lookup().
2200 */
2201/*ARGSUSED*/
2202static int
2203sffs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
2204{
2205 sfnode_t *node;
2206 int error = 0;
2207
2208 mutex_enter(&sffs_lock);
2209
2210 node = VN2SFN(*vpp);
2211 sfnode_open(node, flag);
2212 if (node->sf_file == NULL)
2213 error = EINVAL;
2214 mutex_exit(&sffs_lock);
2215
2216 return (error);
2217}
2218
2219/*
2220 * All the work for this is really done in inactive.
2221 */
2222/*ARGSUSED*/
2223static int
2224sffs_close(
2225 vnode_t *vp,
2226 int flag,
2227 int count,
2228 offset_t offset,
2229 cred_t *cr,
2230 caller_context_t *ct)
2231{
2232 sfnode_t *node;
2233
2234 mutex_enter(&sffs_lock);
2235 node = VN2SFN(vp);
2236
2237 /*
2238 * Free the directory entries for the node. We do this on this call
2239 * here because the directory node may not become inactive for a long
2240 * time after the readdir is over. Case in point, if somebody cd's into
2241 * the directory then it won't become inactive until they cd away again.
2242 * In such a case we would end up with the directory listing not getting
2243 * updated (i.e. the result of 'ls' always being the same) until they
2244 * change the working directory.
2245 */
2246 sfnode_clear_dir_list(node);
2247
2248 sfnode_invalidate_stat_cache(node);
2249
2250 if (node->sf_file != NULL && vp->v_count <= 1)
2251 {
2252 (void)sfprov_close(node->sf_file);
2253 node->sf_file = NULL;
2254 }
2255
2256 mutex_exit(&sffs_lock);
2257 return (0);
2258}
2259
2260/* ARGSUSED */
2261static int
2262sffs_seek(vnode_t *v, offset_t o, offset_t *no, caller_context_t *ct)
2263{
2264 if (*no < 0 || *no > MAXOFFSET_T)
2265 return (EINVAL);
2266
2267 if (v->v_type == VDIR)
2268 {
2269 sffs_dirents_t *cur_buf = VN2SFN(v)->sf_dir_list;
2270 off_t offset = 0;
2271
2272 if (cur_buf == NULL)
2273 return (0);
2274
2275 while (cur_buf != NULL) {
2276 if (*no >= offset && *no <= offset + cur_buf->sf_len)
2277 return (0);
2278 offset += cur_buf->sf_len;
2279 cur_buf = cur_buf->sf_next;
2280 }
2281 return (EINVAL);
2282 }
2283 return (0);
2284}
2285
2286
2287
2288/*
2289 * By returning an error for this, we prevent anything in sffs from
2290 * being re-exported by NFS
2291 */
2292/* ARGSUSED */
2293static int
2294sffs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
2295{
2296 return (ENOTSUP);
2297}
2298
2299/*
2300 * vnode operations for regular files
2301 */
2302const fs_operation_def_t sffs_ops_template[] = {
2303#if defined(VBOX_VFS_SOLARIS_10U6)
2304 VOPNAME_ACCESS, sffs_access,
2305 VOPNAME_CLOSE, sffs_close,
2306 VOPNAME_CREATE, sffs_create,
2307 VOPNAME_FID, sffs_fid,
2308 VOPNAME_FSYNC, sffs_fsync,
2309 VOPNAME_GETATTR, sffs_getattr,
2310 VOPNAME_INACTIVE, sffs_inactive,
2311 VOPNAME_LOOKUP, sffs_lookup,
2312 VOPNAME_MKDIR, sffs_mkdir,
2313 VOPNAME_OPEN, sffs_open,
2314 VOPNAME_PATHCONF, sffs_pathconf,
2315 VOPNAME_READ, sffs_read,
2316 VOPNAME_READDIR, sffs_readdir,
2317 VOPNAME_READLINK, sffs_readlink,
2318 VOPNAME_REMOVE, sffs_remove,
2319 VOPNAME_RENAME, sffs_rename,
2320 VOPNAME_RMDIR, sffs_rmdir,
2321 VOPNAME_SEEK, sffs_seek,
2322 VOPNAME_SETATTR, sffs_setattr,
2323 VOPNAME_SPACE, sffs_space,
2324 VOPNAME_SYMLINK, sffs_symlink,
2325 VOPNAME_WRITE, sffs_write,
2326
2327# ifdef VBOXVFS_WITH_MMAP
2328 VOPNAME_MAP, sffs_map,
2329 VOPNAME_ADDMAP, sffs_addmap,
2330 VOPNAME_DELMAP, sffs_delmap,
2331 VOPNAME_GETPAGE, sffs_getpage,
2332 VOPNAME_PUTPAGE, sffs_putpage,
2333# endif
2334
2335 NULL, NULL
2336#else
2337 VOPNAME_ACCESS, { .vop_access = sffs_access },
2338 VOPNAME_CLOSE, { .vop_close = sffs_close },
2339 VOPNAME_CREATE, { .vop_create = sffs_create },
2340 VOPNAME_FID, { .vop_fid = sffs_fid },
2341 VOPNAME_FSYNC, { .vop_fsync = sffs_fsync },
2342 VOPNAME_GETATTR, { .vop_getattr = sffs_getattr },
2343 VOPNAME_INACTIVE, { .vop_inactive = sffs_inactive },
2344 VOPNAME_LOOKUP, { .vop_lookup = sffs_lookup },
2345 VOPNAME_MKDIR, { .vop_mkdir = sffs_mkdir },
2346 VOPNAME_OPEN, { .vop_open = sffs_open },
2347 VOPNAME_PATHCONF, { .vop_pathconf = sffs_pathconf },
2348 VOPNAME_READ, { .vop_read = sffs_read },
2349 VOPNAME_READDIR, { .vop_readdir = sffs_readdir },
2350 VOPNAME_READLINK, { .vop_readlink = sffs_readlink },
2351 VOPNAME_REMOVE, { .vop_remove = sffs_remove },
2352 VOPNAME_RENAME, { .vop_rename = sffs_rename },
2353 VOPNAME_RMDIR, { .vop_rmdir = sffs_rmdir },
2354 VOPNAME_SEEK, { .vop_seek = sffs_seek },
2355 VOPNAME_SETATTR, { .vop_setattr = sffs_setattr },
2356 VOPNAME_SPACE, { .vop_space = sffs_space },
2357 VOPNAME_SYMLINK, { .vop_symlink = sffs_symlink },
2358 VOPNAME_WRITE, { .vop_write = sffs_write },
2359
2360# ifdef VBOXVFS_WITH_MMAP
2361 VOPNAME_MAP, { .vop_map = sffs_map },
2362 VOPNAME_ADDMAP, { .vop_addmap = sffs_addmap },
2363 VOPNAME_DELMAP, { .vop_delmap = sffs_delmap },
2364 VOPNAME_GETPAGE, { .vop_getpage = sffs_getpage },
2365 VOPNAME_PUTPAGE, { .vop_putpage = sffs_putpage },
2366# endif
2367
2368 NULL, NULL
2369#endif
2370};
2371
2372/*
2373 * Also, init and fini functions...
2374 */
2375int
2376sffs_vnode_init(void)
2377{
2378 int err;
2379
2380 err = vn_make_ops("sffs", sffs_ops_template, &sffs_ops);
2381 if (err)
2382 return (err);
2383
2384 avl_create(&sfnodes, sfnode_compare, sizeof (sfnode_t),
2385 offsetof(sfnode_t, sf_linkage));
2386 avl_create(&stale_sfnodes, sfnode_compare, sizeof (sfnode_t),
2387 offsetof(sfnode_t, sf_linkage));
2388
2389 sffs_buffer = kmem_alloc(PAGESIZE, KM_SLEEP);
2390
2391 return (0);
2392}
2393
2394void
2395sffs_vnode_fini(void)
2396{
2397 if (sffs_ops)
2398 vn_freevnodeops(sffs_ops);
2399 ASSERT(avl_first(&sfnodes) == NULL);
2400 avl_destroy(&sfnodes);
2401 if (sffs_buffer != NULL) {
2402 kmem_free(sffs_buffer, PAGESIZE);
2403 sffs_buffer = NULL;
2404 }
2405}
2406
2407/*
2408 * Utility at unmount to get all nodes in that mounted filesystem removed.
2409 */
2410int
2411sffs_purge(struct sffs_data *sffs)
2412{
2413 sfnode_t *node;
2414 sfnode_t *prev;
2415
2416 /*
2417 * Check that no vnodes are active.
2418 */
2419 if (sffs->sf_rootnode->v_count > 1)
2420 return (-1);
2421 for (node = avl_first(&sfnodes); node;
2422 node = AVL_NEXT(&sfnodes, node)) {
2423 if (node->sf_sffs == sffs && node->sf_vnode &&
2424 node->sf_vnode != sffs->sf_rootnode)
2425 return (-1);
2426 }
2427 for (node = avl_first(&stale_sfnodes); node;
2428 node = AVL_NEXT(&stale_sfnodes, node)) {
2429 if (node->sf_sffs == sffs && node->sf_vnode &&
2430 node->sf_vnode != sffs->sf_rootnode)
2431 return (-1);
2432 }
2433
2434 /*
2435 * All clear to destroy all node information. Since there are no
2436 * vnodes, the make stale will cause deletion.
2437 */
2438 VN_RELE(sffs->sf_rootnode);
2439 mutex_enter(&sffs_lock);
2440 for (prev = NULL;;) {
2441 if (prev == NULL)
2442 node = avl_first(&sfnodes);
2443 else
2444 node = AVL_NEXT(&sfnodes, prev);
2445
2446 if (node == NULL)
2447 break;
2448
2449 if (node->sf_sffs == sffs) {
2450 if (node->sf_vnode != NULL)
2451 panic("vboxfs: purge hit active vnode");
2452 sfnode_make_stale(node);
2453 } else {
2454 prev = node;
2455 }
2456 }
2457 mutex_exit(&sffs_lock);
2458 return (0);
2459}
2460
2461#if 0
2462/* Debug helper functions */
2463static void
2464sfnode_print(sfnode_t *node)
2465{
2466 Log(("0x%p", node));
2467 Log((" type=%s (%d)",
2468 node->sf_type == VDIR ? "VDIR" :
2469 node->sf_type == VNON ? "VNON" :
2470 node->sf_type == VLNK ? "VLNK" :
2471 node->sf_type == VREG ? "VREG" : "other", node->sf_type));
2472 Log((" ino=%d", (uint_t)node->sf_ino));
2473 Log((" path=%s", node->sf_path));
2474 Log((" parent=0x%p", node->sf_parent));
2475 if (node->sf_children)
2476 Log((" children=%d", node->sf_children));
2477 if (node->sf_vnode)
2478 Log((" vnode=0x%p", node->sf_vnode));
2479 Log(("%s\n", node->sf_is_stale ? " STALE" : ""));
2480}
2481
2482static void
2483sfnode_list(void)
2484{
2485 sfnode_t *n;
2486 for (n = avl_first(&sfnodes); n != NULL; n = AVL_NEXT(&sfnodes, n))
2487 sfnode_print(n);
2488 for (n = avl_first(&stale_sfnodes); n != NULL;
2489 n = AVL_NEXT(&stale_sfnodes, n))
2490 sfnode_print(n);
2491}
2492#endif
2493
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