xref: /haiku/src/add-ons/kernel/file_systems/fat/bsd/kern/vfs_hash.c (revision 342a1b221b5bb385410f758df2c625b70cafdd03)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005 Poul-Henning Kamp
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 
30 // Modified to support the Haiku FAT driver.
31 
32 #ifdef FS_SHELL
33 #include "fssh_api_wrapper.h"
34 #else
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #endif
38 
39 #include "sys/param.h"
40 #include "sys/systm.h"
41 #include "sys/buf.h"
42 #include "sys/mount.h"
43 #include "sys/namei.h"
44 #include "sys/vnode.h"
45 
46 #include "fs/msdosfs/denode.h"
47 #include "fs/msdosfs/direntry.h"
48 
49 #include "dosfs.h"
50 
51 
52 #ifdef USER
53 #define dprintf printf
54 #endif
55 
56 
57 /*! Determine the inode number for a direntry location and return the corresponding node if it is
58 	constructed.
59 	@param hash A tentative location-based inode.
60 	@param arg Pointer to a pre-allocated uint64.
61 	@param td Ignored in the port.
62 	@param fn Ignored in the port. In FreeBSD, this is a function pointer that would be passed to
63 	the VFS and applied to the VFS's list of vnodes to find a match.
64 	@param arg Pointer to a pre-allocated uint64 that will be set to the final inode number.
65 	@post *arg is set to the inode number corresponding to hash (which may or may not equal hash).
66 	*vpp points to the corresponding BSD-style vnode, if that vnode already exists in memory. If
67 	the node doesn't exist in memory, *vpp is set to NULL.
68 	The function signature is modified to make hash a uint64 instead of uint32. This is consistent
69 	with the type passed by deget, and protects against overflow.
70 	This function never calls get_vnode when the node in question is not already registered with
71 	the VFS, so it is suitable for use within dosfs_read_vnode.
72 */
73 int
vfs_hash_get(struct mount * mp,uint64 hash,int flags,thread_id td,struct vnode ** vpp,vfs_hash_cmp_t * fn,void * arg)74 vfs_hash_get(struct mount* mp, uint64 hash, int flags, thread_id td, struct vnode** vpp,
75 	vfs_hash_cmp_t* fn, void* arg)
76 {
77 	uint64* finalInode = arg;
78 	status_t status = B_OK;
79 	bool exists = false;
80 		// Has a node with the same inode number already been constructed?
81 
82 	status = assign_inode(mp, (ino_t*)&hash);
83 	if (status != B_OK)
84 		return B_TO_POSIX_ERROR(status);
85 
86 	exists = node_exists(mp, hash);
87 	if (exists == true) {
88 		// get_vnode will just return a ref (without calling dosfs_read_vnode)
89 		status = get_vnode(mp->mnt_fsvolume, hash, (void**)vpp);
90 		if (status != B_OK) {
91 			dprintf("FAT vfs_hash_get:  get_vnode failure\n");
92 			return B_TO_POSIX_ERROR(status);
93 		}
94 		rw_lock_write_lock(&(*vpp)->v_vnlock->haikuRW);
95 			// the driver expects the node to be returned locked
96 	} else {
97 		// The node needs to be initialized. Returning with a NULL vpp will
98 		// signal deget() to go through with setting it up.
99 		*vpp = NULL;
100 	}
101 
102 	*finalInode = hash;
103 
104 	return 0;
105 }
106 
107 
108 /*! In FreeBSD, this adds vp to a VFS-level list of nodes. In Haiku, the same thing is
109 	taken care of automatically by get_vnode.
110 */
111 int
vfs_hash_insert(struct vnode * vp,uint64 hash,int flags,thread_id td,struct vnode ** vpp,vfs_hash_cmp_t * fn,void * arg)112 vfs_hash_insert(struct vnode* vp, uint64 hash, int flags, thread_id td, struct vnode** vpp,
113 	vfs_hash_cmp_t* fn, void* arg)
114 {
115 	ASSERT(node_exists(vp->v_mount, hash) == false);
116 
117 	// This tells the client that there is no existing vnode with the same
118 	// inode number. This should always be the case under the conditions in which
119 	// the FAT driver calls this function:  if there was an existing node,
120 	// deget would have returned early.
121 	*vpp = NULL;
122 
123 	return 0;
124 }
125 
126 
127 /*! In FreeBSD, this provides the VFS with an updated node hash value based on the new directory
128 	entry offset when a file is renamed. Not applicable in Haiku.
129 */
130 void
vfs_hash_rehash(struct vnode * vp,uint64 hash)131 vfs_hash_rehash(struct vnode* vp, uint64 hash)
132 {
133 	return;
134 }
135