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