xref: /haiku/src/add-ons/kernel/file_systems/fat/bsd/sys/buf.h (revision 342a1b221b5bb385410f758df2c625b70cafdd03)
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  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 #ifndef FAT_BUF_H
37 #define FAT_BUF_H
38 
39 
40 // Modified to support the Haiku FAT driver.
41 
42 #include <sys/queue.h>
43 
44 #ifdef FS_SHELL
45 #include "fssh_fs_cache.h"
46 #else
47 #include <fs_cache.h>
48 #endif // FS_SHELL
49 
50 #include "fs/msdosfs/bpb.h"
51 #include "fs/msdosfs/msdosfsmount.h"
52 #include "sys/mount.h"
53 #include "sys/pcpu.h"
54 #include "sys/ucred.h"
55 
56 #include "dosfs.h"
57 
58 
59 /*
60  * The buffer header describes an I/O operation in the kernel.
61  *
62  * NOTES:
63  *	b_bufsize, b_bcount. b_bufsize is the allocation size of the
64  *	buffer, either DEV_BSIZE or PAGE_SIZE aligned. b_bcount is the
65  *	originally requested buffer size and can serve as a bounds check
66  *	against EOF. For most, but not all uses, b_bcount == b_bufsize.
67  *
68  *	b_resid. Number of bytes remaining in I/O. After an I/O operation
69  *	completes, b_resid is usually 0 indicating 100% success.
70  */
71 struct buf {
72 	long b_bcount;
73 	caddr_t b_data;
74 	long b_resid;
75 	daddr_t b_blkno; /* Underlying physical block number. */
76 	uint32_t b_flags; /* B_* flags. */
77 	long b_bufsize; /* Allocated buffer size. */
78 		// In the Haiku port, if b_data is a pointer to a block cache block,
79 		// this is still populated even though b_data is not owned by the driver.
80 	daddr_t b_lblkno; /* Logical block number. */
81 	struct vnode* b_vp; /* Device vnode. */
82 
83 	// Members added for Haiku port
84 	bool b_owned;
85 		// false if b_data is a pointer to a block cache block
86 	struct vnode* b_vreg;
87 		// the node that was passed to bread, if it was of type VREG; otherwise, NULL
88 	void* b_bcpointers[128];
89 		// If b_data is assembled from multiple block cache blocks, this holds pointers to
90 		// each source block. The FAT specification allows up to 128 sectors per cluster.
91 	SLIST_ENTRY(buf) link;
92 		// for the list members of struct bufobj
93 };
94 
95 // In the Haiku port, these FreeBSD flags are referenced but do not have any effect
96 #define B_DELWRI 0x00000080 /* Delay I/O until buffer reused. */
97 #define B_CLUSTEROK 0x00020000 /* Pagein op, so swap() can count it. */
98 #define B_INVALONERR 0x00040000 /* Invalidate on write error. */
99 
100 // In FreeBSD, struct vn_clusterw stores the state of an optimized routine for writing multiple
101 // bufs. In the Haiku port, it is not used for anything meaningful.
102 struct vn_clusterw {
103 	daddr_t v_cstart; /* v start block of cluster */
104 	daddr_t v_lasta; /* v last allocation  */
105 	daddr_t v_lastw; /* v last write  */
106 	int v_clen; /* v length of cur. cluster */
107 };
108 
109 // Zero out the data member of a struct buf.
110 // Port version is tolerant of a buf that holds less than the client-requested size in b_data.
111 #define clrbuf(bp) \
112 	{ \
113 		bzero((bp)->b_data, (u_int)(bp)->b_bufsize); \
114 		(bp)->b_resid = 0; \
115 	}
116 
117 
118 int bwrite(struct buf* bp);
119 int buf_dirty_count_severe(void);
120 int bread(struct vnode* vp, daddr_t blkno, int size, struct ucred* cred, struct buf** bpp);
121 void bdwrite(struct buf* bp);
122 void bawrite(struct buf* bp);
123 void brelse(struct buf* bp);
124 struct buf* getblk(struct vnode* vp, daddr_t blkno, int size, int slpflag, int slptimeo, int flags);
125 
126 
127 static inline void
cluster_init_vn(struct vn_clusterw * vnc)128 cluster_init_vn(struct vn_clusterw* vnc)
129 {
130 	return;
131 }
132 
133 
134 void vfs_bio_bzero_buf(struct buf* bp, int base, int size);
135 void vfs_bio_clrbuf(struct buf* bp);
136 
137 
138 #endif // FAT_BUF_H
139