1 | /* $Id: vnode_cache.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared folders - Haiku Guest Additions, vnode cache header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /*
|
---|
29 | * This code is based on:
|
---|
30 | *
|
---|
31 | * VirtualBox Guest Additions for Haiku.
|
---|
32 | * Copyright (c) 2011 Mike Smith <[email protected]>
|
---|
33 | *
|
---|
34 | * Permission is hereby granted, free of charge, to any person
|
---|
35 | * obtaining a copy of this software and associated documentation
|
---|
36 | * files (the "Software"), to deal in the Software without
|
---|
37 | * restriction, including without limitation the rights to use,
|
---|
38 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
39 | * copies of the Software, and to permit persons to whom the
|
---|
40 | * Software is furnished to do so, subject to the following
|
---|
41 | * conditions:
|
---|
42 | *
|
---|
43 | * The above copyright notice and this permission notice shall be
|
---|
44 | * included in all copies or substantial portions of the Software.
|
---|
45 | *
|
---|
46 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
47 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
48 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
49 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
50 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
51 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
52 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
53 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
54 | */
|
---|
55 |
|
---|
56 | #include "vboxsf.h"
|
---|
57 | #include "OpenHashTable.h"
|
---|
58 |
|
---|
59 | struct HashTableDefinition
|
---|
60 | {
|
---|
61 | typedef uint32 KeyType;
|
---|
62 | typedef vboxsf_vnode ValueType;
|
---|
63 |
|
---|
64 | size_t HashKey(uint32 key) const
|
---|
65 | {
|
---|
66 | return key;
|
---|
67 | }
|
---|
68 |
|
---|
69 | size_t Hash(vboxsf_vnode* value) const
|
---|
70 | {
|
---|
71 | return HashKey(value->vnode);
|
---|
72 | }
|
---|
73 |
|
---|
74 | bool Compare(uint32 key, vboxsf_vnode* value) const
|
---|
75 | {
|
---|
76 | return value->vnode == key;
|
---|
77 | }
|
---|
78 |
|
---|
79 | vboxsf_vnode*& GetLink(vboxsf_vnode* value) const
|
---|
80 | {
|
---|
81 | return value->next;
|
---|
82 | }
|
---|
83 | };
|
---|
84 |
|
---|
85 | static BOpenHashTable<HashTableDefinition> g_cache;
|
---|
86 | static ino_t g_nextVnid = 1;
|
---|
87 | mutex g_vnodeCacheLock;
|
---|
88 |
|
---|
89 |
|
---|
90 | extern "C" status_t vboxsf_new_vnode(PVBSFMAP map, PSHFLSTRING path, PSHFLSTRING name, vboxsf_vnode** p)
|
---|
91 | {
|
---|
92 | vboxsf_vnode* vn = (vboxsf_vnode*)malloc(sizeof(vboxsf_vnode));
|
---|
93 | if (vn == NULL)
|
---|
94 | return B_NO_MEMORY;
|
---|
95 |
|
---|
96 | dprintf("creating new vnode at %p with path=%p (%s)\n", vn, path->String.utf8, path->String.utf8);
|
---|
97 | vn->map = map;
|
---|
98 | vn->path = path;
|
---|
99 | if (name)
|
---|
100 | vn->name = name;
|
---|
101 | else
|
---|
102 | {
|
---|
103 | const char* cname = strrchr((char*)path->String.utf8, '/');
|
---|
104 | if (!cname)
|
---|
105 | vn->name = path; // no slash, assume this *is* the filename
|
---|
106 | else
|
---|
107 | vn->name = make_shflstring(cname);
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (mutex_lock(&g_vnodeCacheLock) < B_OK)
|
---|
111 | {
|
---|
112 | free(vn);
|
---|
113 | return B_ERROR;
|
---|
114 | }
|
---|
115 |
|
---|
116 | vn->vnode = g_nextVnid++;
|
---|
117 | *p = vn;
|
---|
118 | dprintf("vboxsf: allocated %p (path=%p name=%p)\n", vn, vn->path, vn->name);
|
---|
119 | status_t rv = g_cache.Insert(vn);
|
---|
120 |
|
---|
121 | mutex_unlock(&g_vnodeCacheLock);
|
---|
122 |
|
---|
123 | return rv;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | extern "C" status_t vboxsf_get_vnode(fs_volume* volume, ino_t id, fs_vnode* vnode,
|
---|
128 | int* _type, uint32* _flags, bool reenter)
|
---|
129 | {
|
---|
130 | vboxsf_vnode* vn = g_cache.Lookup(id);
|
---|
131 | if (vn)
|
---|
132 | {
|
---|
133 | vnode->private_node = vn;
|
---|
134 | return B_OK;
|
---|
135 | }
|
---|
136 | return B_ERROR;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | extern "C" status_t vboxsf_put_vnode(fs_volume* volume, fs_vnode* vnode, bool reenter)
|
---|
141 | {
|
---|
142 | g_cache.Remove((vboxsf_vnode*)vnode->private_node);
|
---|
143 | }
|
---|
144 |
|
---|