xref: /haiku/src/add-ons/kernel/file_systems/fat/bsd/kern/vfs_vnops.c (revision eea5774f46bba925156498abf9cb1a1165647bf7)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
13  * Copyright (c) 2013, 2014 The FreeBSD Foundation
14  *
15  * Portions of this software were developed by Konstantin Belousov
16  * under sponsorship from the FreeBSD Foundation.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  */
42 
43 
44 // Modified to support the Haiku FAT driver.
45 
46 #include "sys/param.h"
47 #include "sys/systm.h"
48 #include "sys/buf.h"
49 #include "sys/mount.h"
50 #include "sys/namei.h"
51 #include "sys/vnode.h"
52 
53 
54 struct deget_dotdot {
55 	u_long cluster;
56 	int blkoff;
57 };
58 
59 
60 /*! Return in rvp the private node that is specified by alloc_arg, using the function alloc.
61 	The FAT driver uses this to call deget when looking up ".." directory entries.
62 */
63 int
64 vn_vget_ino_gen(struct vnode* vp, vn_get_ino_t alloc, void* alloc_arg, int lkflags,
65 	struct vnode** rvp)
66 {
67 	struct mount* bsdVolume = vp->v_mount;
68 	struct msdosfsmount* fatVolume = (struct msdosfsmount*)bsdVolume->mnt_data;
69 	struct deget_dotdot* dotdotLocation = alloc_arg;
70 	uint64 inode
71 		= (uint64)fatVolume->pm_bpcluster * dotdotLocation->cluster + dotdotLocation->blkoff;
72 
73 	status_t status = assign_inode(bsdVolume, (ino_t*)&inode);
74 	ASSERT_ALWAYS(status == B_OK && node_exists(bsdVolume, inode));
75 		// It's important that this node has already been constructed.
76 		// If it hasn't, then deget will construct a private node without publishing it to the VFS.
77 
78 	return alloc(bsdVolume, alloc_arg, lkflags, rvp);
79 }
80 
81 
82 /*! The driver uses this to free up memory if a low resource condition occurs after extending a
83 	regular file. In FreeBSD, it does this by flushing dirty struct bufs associated with vp.
84 	In the Haiku port, we remove all pages from this file's file cache.
85 */
86 int
87 vn_fsync_buf(struct vnode* vp, int waitfor)
88 {
89 	status_t status = B_OK;
90 
91 	if (waitfor == MNT_WAIT) {
92 		vp->v_sync = true;
93 		status = file_cache_disable(vp->v_cache);
94 		vp->v_sync = false;
95 	} else {
96 		status = file_cache_disable(vp->v_cache);
97 	}
98 
99 	if (status == B_OK)
100 		file_cache_enable(vp->v_cache);
101 
102 	return B_TO_POSIX_ERROR(status);
103 }
104