1/*	$NetBSD$	*/
2
3/*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/* V7FS implementation. */
33#ifndef _V7FS_IMPL_H_
34#define	_V7FS_IMPL_H_
35
36#ifndef _KERNEL
37#include <stdbool.h>
38#include <assert.h>
39#define	KDASSERT(x)	assert(x)
40#endif
41
42struct block_io_ops {
43	void *cookie;
44	bool (*drive)(void *, uint8_t);
45	bool (*read)(void *, uint8_t *, daddr_t);
46	bool (*read_n)(void *, uint8_t *, daddr_t, int);
47	bool (*write)(void *, uint8_t *, daddr_t);
48	bool (*write_n)(void *, uint8_t *, daddr_t, int);
49};
50
51#ifdef V7FS_EI
52struct endian_conversion_ops {
53	uint32_t (*conv32)(uint32_t);
54	uint16_t (*conv16)(uint16_t);
55	/* For daddr packing */
56	v7fs_daddr_t (*conv24read)(uint8_t *);
57	void (*conv24write)(v7fs_daddr_t, uint8_t *);
58};
59#endif
60#ifdef _KERNEL
61#define	V7FS_LOCK
62#endif
63#ifdef V7FS_LOCK
64struct lock_ops {
65	void *cookie;
66	void (*lock)(void*);
67	void (*unlock)(void *);
68};
69#define	SUPERB_LOCK(x)		((x)->sb_lock.lock((x)->sb_lock.cookie))
70#define	SUPERB_UNLOCK(x)	((x)->sb_lock.unlock((x)->sb_lock.cookie))
71#define	ILIST_LOCK(x)		((x)->ilist_lock.lock((x)->ilist_lock.cookie))
72#define	ILIST_UNLOCK(x)		((x)->ilist_lock.unlock((x)->ilist_lock.cookie))
73#define	MEM_LOCK(x)		((x)->mem_lock.lock((x)->mem_lock.cookie))
74#define	MEM_UNLOCK(x)		((x)->mem_lock.unlock((x)->mem_lock.cookie))
75#else /*V7FS_LOCK */
76#define	SUPERB_LOCK(x)		((void)0)
77#define	SUPERB_UNLOCK(x)	((void)0)
78#define	ILIST_LOCK(x)		((void)0)
79#define	ILIST_UNLOCK(x)		((void)0)
80#define	MEM_LOCK(x)		((void)0)
81#define	MEM_UNLOCK(x)		((void)0)
82#endif /*V7FS_LOCK */
83
84struct v7fs_stat {
85	int32_t total_blocks;
86	int32_t free_blocks;
87	int32_t total_inode;
88	int32_t free_inode;
89	int32_t total_files;
90};
91
92struct v7fs_fileattr {
93	int16_t uid;
94	int16_t gid;
95	v7fs_mode_t mode;
96	v7fs_dev_t device;
97	v7fs_time_t ctime;
98	v7fs_time_t mtime;
99	v7fs_time_t atime;
100};
101
102struct v7fs_self {
103#define	V7FS_SELF_NSCRATCH	3
104	uint8_t scratch[V7FS_SELF_NSCRATCH][V7FS_BSIZE];
105	int scratch_free;	/* free block bitmap. */
106	int scratch_remain;	/* for statistic */
107	struct block_io_ops io;
108#ifdef V7FS_EI
109	struct endian_conversion_ops val;
110#endif
111#ifdef V7FS_LOCK
112	/* in-core superblock access. (freeblock/freeinode) split? -uch */
113	struct lock_ops sb_lock;
114	struct lock_ops ilist_lock;	/* disk ilist access. */
115	struct lock_ops mem_lock;	/* work memory allocation lock. */
116#endif
117	struct v7fs_superblock superblock;
118	struct v7fs_stat stat;
119	int endian;
120};
121
122struct v7fs_mount_device {
123	union {
124		void *vnode;	/* NetBSD kernel */
125		int fd;		/* NetBSD newfs,fsck */
126		const char *filename;/* misc test */
127	} device;
128	daddr_t sectors;	/*total size in sector. */
129	int endian;
130};
131
132#define	V7FS_ITERATOR_BREAK	(-1)
133#define	V7FS_ITERATOR_END	(-2)
134#define	V7FS_ITERATOR_ERROR	(-3)
135__BEGIN_DECLS
136int v7fs_io_init(struct v7fs_self **, const struct v7fs_mount_device *, size_t);
137void v7fs_io_fini(struct v7fs_self *);
138void *scratch_read(struct v7fs_self *, daddr_t);
139void scratch_free(struct v7fs_self *, void *);
140int scratch_remain(const struct v7fs_self *);
141__END_DECLS
142
143#if 0
144#define	V7FS_IO_DEBUG
145#define	V7FS_SUPERBLOCK_DEBUG
146#define	V7FS_DATABLOCK_DEBUG
147#define	V7FS_INODE_DEBUG
148#define	V7FS_DIRENT_DEBUG
149#define	V7FS_FILE_DEBUG
150#define	V7FS_VFSOPS_DEBUG
151#define	V7FS_VNOPS_DEBUG
152#endif
153
154#endif /*!_V7FS_IMPL_H_ */
155