1 /*        $NetBSD: bfs.h,v 1.8 2014/01/09 13:23:57 hannken Exp $      */
2 
3 /*-
4  * Copyright (c) 2004 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 #ifndef _FS_SYSVBFS_BFS_H_
33 #define   _FS_SYSVBFS_BFS_H_
34 /*
35  *   Boot File System
36  *
37  *        +----------
38  *        |bfs_super_block (512byte)
39  *        |                                       1 sector
40  *        |
41  *        +----------
42  *        |bfs_inode (64byte) * 8
43  *        |    .                                  1 sector
44  *        |bfs_inode
45  *        +----------  <--- bfs_super_block.header.data_start
46  *        |DATA BLOCK
47  *        |    .
48  *        |    .
49  *        |
50  *        +----------  <--- bfs_super_block.header.data_end
51  */
52 
53 /* BFS specification */
54 #define   BFS_SECTOR                    0         /* no offset */
55 #define   BFS_MAGIC           0x1badface
56 #define   BFS_FILENAME_MAXLEN 14
57 #define   BFS_ROOT_INODE                2
58 #define   BFS_BSIZE           512
59 #define   BFS_BSHIFT                    9
60 
61 struct bfs_super_block_header {
62           uint32_t magic;
63           uint32_t data_start_byte;
64           uint32_t data_end_byte;
65 } __packed;
66 
67 struct bfs_compaction {
68           uint32_t from;
69           uint32_t to;
70           uint32_t from_backup;
71           uint32_t to_backup;
72 } __packed;
73 
74 struct bfs_fileattr {
75           uint32_t type;
76           uint32_t mode;
77           int32_t uid;
78           int32_t gid;
79           uint32_t nlink;
80           int32_t atime;
81           int32_t mtime;
82           int32_t ctime;
83           int32_t padding[4];
84 } __packed;         /* 48byte */
85 
86 struct bfs_inode {
87           uint16_t number;              /*  0 */
88           int16_t padding;
89           uint32_t start_sector;                  /*  4 */
90           uint32_t end_sector;                    /*  8 */
91           uint32_t eof_offset_byte;     /* 12 (offset from super block start) */
92           struct bfs_fileattr attr;     /* 16 */
93 } __packed;         /* 64byte */
94 
95 struct bfs_super_block {
96           struct bfs_super_block_header header;
97           struct bfs_compaction compaction;
98           char fsname[6];
99           char volume[6];
100           int32_t padding[118];
101 } __packed;
102 
103 struct bfs_dirent {
104           uint16_t inode;
105           char name[BFS_FILENAME_MAXLEN];
106 } __packed; /* 16byte */
107 
108 #if defined _KERNEL || defined _STANDALONE
109 /* Software definition */
110 struct sector_io_ops;
111 struct bfs {
112           int start_sector;
113           /* Super block */
114           struct bfs_super_block *super_block;
115           size_t super_block_size;
116 
117           /* Data block */
118           uint32_t data_start, data_end;
119 
120           /* Inode */
121           struct bfs_inode *inode;
122           int n_inode;
123           int max_inode;
124 
125           /* root directory */
126           struct bfs_dirent *dirent;
127           size_t dirent_size;
128           int n_dirent;
129           int max_dirent;
130           struct bfs_inode *root_inode;
131 
132           /* Sector I/O operation */
133           struct sector_io_ops *io;
134 
135           bool debug;
136 };
137 
138 struct sector_io_ops {
139           bool (*read)(void *, uint8_t *, daddr_t);
140           bool (*read_n)(void *, uint8_t *, daddr_t, int);
141           bool (*write)(void *, uint8_t *, daddr_t);
142           bool (*write_n)(void *, uint8_t *, daddr_t, int);
143 };
144 
145 struct vnode;
146 
147 int bfs_init2(struct bfs **, int, struct sector_io_ops *, bool);
148 void bfs_fini(struct bfs *);
149 int bfs_file_read(const struct bfs *, const char *, void *, size_t, size_t *);
150 int bfs_file_write(struct bfs *, const char *, void *, size_t);
151 int bfs_file_create(struct bfs *, const char *, void *,  size_t,
152     const struct bfs_fileattr *);
153 int bfs_file_delete(struct bfs *, const char *, bool);
154 int bfs_file_rename(struct bfs *, const char *, const char *);
155 bool bfs_file_lookup(const struct bfs *, const char *, int *, int *,
156     size_t *);
157 size_t bfs_file_size(const struct bfs_inode *);
158 
159 bool bfs_dump(const struct bfs *);
160 
161 /* filesystem ops */
162 int sysvbfs_bfs_init(struct bfs **, struct vnode *);
163 void sysvbfs_bfs_fini(struct bfs *);
164 bool bfs_inode_lookup(const struct bfs *, ino_t, struct bfs_inode **);
165 int bfs_inode_delete(struct bfs *, ino_t);
166 bool bfs_dirent_lookup_by_name(const struct bfs *, const char *,
167     struct bfs_dirent **);
168 bool bfs_dirent_lookup_by_inode(const struct bfs *, int,
169     struct bfs_dirent **);
170 void bfs_inode_set_attr(const struct bfs *, struct bfs_inode *,
171     const struct bfs_fileattr *attr);
172 int bfs_inode_alloc(const struct bfs *, struct bfs_inode **, int *,
173     int *);
174 #endif /* _KERNEL || _STANDALONE */
175 #endif /* _FS_SYSVBFS_BFS_H_ */
176