1 | /* $Id: vnode_cache.cpp 43363 2012-09-20 09:56:07Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared folders - Haiku Guest Additions, vnode cache header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 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 |
|
---|
18 | /*
|
---|
19 | * This code is based on:
|
---|
20 | *
|
---|
21 | * VirtualBox Guest Additions for Haiku.
|
---|
22 | * Copyright (c) 2011 Mike Smith <[email protected]>
|
---|
23 | *
|
---|
24 | * Permission is hereby granted, free of charge, to any person
|
---|
25 | * obtaining a copy of this software and associated documentation
|
---|
26 | * files (the "Software"), to deal in the Software without
|
---|
27 | * restriction, including without limitation the rights to use,
|
---|
28 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
29 | * copies of the Software, and to permit persons to whom the
|
---|
30 | * Software is furnished to do so, subject to the following
|
---|
31 | * conditions:
|
---|
32 | *
|
---|
33 | * The above copyright notice and this permission notice shall be
|
---|
34 | * included in all copies or substantial portions of the Software.
|
---|
35 | *
|
---|
36 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
37 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
38 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
39 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
40 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
41 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
42 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
43 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
44 | */
|
---|
45 |
|
---|
46 | #include "vboxsf.h"
|
---|
47 | #include "OpenHashTable.h"
|
---|
48 |
|
---|
49 | struct HashTableDefinition {
|
---|
50 | typedef uint32 KeyType;
|
---|
51 | typedef vboxsf_vnode ValueType;
|
---|
52 |
|
---|
53 | size_t HashKey(uint32 key) const
|
---|
54 | {
|
---|
55 | return key;
|
---|
56 | }
|
---|
57 |
|
---|
58 | size_t Hash(vboxsf_vnode* value) const
|
---|
59 | {
|
---|
60 | return HashKey(value->vnode);
|
---|
61 | }
|
---|
62 |
|
---|
63 | bool Compare(uint32 key, vboxsf_vnode* value) const
|
---|
64 | {
|
---|
65 | return value->vnode == key;
|
---|
66 | }
|
---|
67 |
|
---|
68 | vboxsf_vnode*& GetLink(vboxsf_vnode* value) const
|
---|
69 | {
|
---|
70 | return value->next;
|
---|
71 | }
|
---|
72 | };
|
---|
73 |
|
---|
74 | static BOpenHashTable<HashTableDefinition> g_cache;
|
---|
75 | static ino_t g_nextVnid = 1;
|
---|
76 | mutex g_vnodeCacheLock;
|
---|
77 |
|
---|
78 | extern "C" status_t vboxsf_new_vnode(PVBSFMAP map, PSHFLSTRING path, PSHFLSTRING name, vboxsf_vnode** p) {
|
---|
79 | vboxsf_vnode* vn = (vboxsf_vnode*)malloc(sizeof(vboxsf_vnode));
|
---|
80 | if (vn == NULL) {
|
---|
81 | return B_NO_MEMORY;
|
---|
82 | }
|
---|
83 | dprintf("creating new vnode at %p with path=%p (%s)\n", vn, path->String.utf8, path->String.utf8);
|
---|
84 | vn->map = map;
|
---|
85 | vn->path = path;
|
---|
86 | if (name) {
|
---|
87 | vn->name = name;
|
---|
88 | }
|
---|
89 | else {
|
---|
90 | const char* cname = strrchr((char*)path->String.utf8, '/');
|
---|
91 | if (!cname)
|
---|
92 | vn->name = path; // no slash, assume this *is* the filename
|
---|
93 | else
|
---|
94 | vn->name = make_shflstring(cname);
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (mutex_lock(&g_vnodeCacheLock) < B_OK) {
|
---|
98 | free(vn);
|
---|
99 | return B_ERROR;
|
---|
100 | }
|
---|
101 |
|
---|
102 | vn->vnode = g_nextVnid++;
|
---|
103 | *p = vn;
|
---|
104 | dprintf("vboxsf: allocated %p (path=%p name=%p)\n", vn, vn->path, vn->name);
|
---|
105 | status_t rv = g_cache.Insert(vn);
|
---|
106 |
|
---|
107 | mutex_unlock(&g_vnodeCacheLock);
|
---|
108 |
|
---|
109 | return rv;
|
---|
110 | }
|
---|
111 |
|
---|
112 | extern "C" status_t vboxsf_get_vnode(fs_volume* volume, ino_t id, fs_vnode* vnode,
|
---|
113 | int* _type, uint32* _flags, bool reenter) {
|
---|
114 | vboxsf_vnode* vn = g_cache.Lookup(id);
|
---|
115 | if (vn) {
|
---|
116 | vnode->private_node = vn;
|
---|
117 | return B_OK;
|
---|
118 | }
|
---|
119 | else {
|
---|
120 | return B_ERROR;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | extern "C" status_t vboxsf_put_vnode(fs_volume* volume, fs_vnode* vnode, bool reenter) {
|
---|
125 | g_cache.Remove((vboxsf_vnode*)vnode->private_node);
|
---|
126 | }
|
---|