makefs.h revision 1.5
1/*	$OpenBSD: makefs.h,v 1.5 2016/10/16 21:59:28 tedu Exp $	*/
2/*	$NetBSD: makefs.h,v 1.36 2015/11/25 00:48:49 christos Exp $	*/
3
4/*
5 * Copyright (c) 2001 Wasabi Systems, Inc.
6 * All rights reserved.
7 *
8 * Written by Luke Mewburn for Wasabi Systems, Inc.
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 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *      This product includes software developed for the NetBSD Project by
21 *      Wasabi Systems, Inc.
22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 *    or promote products derived from this software without specific prior
24 *    written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#ifndef	_MAKEFS_H
40#define	_MAKEFS_H
41
42#define HAVE_STRUCT_STAT_ST_FLAGS 1
43#define HAVE_STRUCT_STAT_ST_GEN 1
44#define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
45#define HAVE_STRUCT_STATVFS_F_IOSIZE 0
46#define HAVE_FSTATVFS 1
47
48#include <sys/stat.h>
49#include <err.h>
50
51/*
52 * fsnode -
53 *	a component of the tree; contains a filename, a pointer to
54 *	fsinode, optional symlink name, and tree pointers
55 *
56 * fsinode -
57 *	equivalent to an inode, containing target file system inode number,
58 *	refcount (nlink), and stat buffer
59 *
60 * A tree of fsnodes looks like this:
61 *
62 *	name	"."		"bin"		"netbsd"
63 *	type	S_IFDIR		S_IFDIR		S_IFREG
64 *	next 	  >		  >		NULL
65 *	parent	NULL		NULL		NULL
66 *	child	NULL		  v
67 *
68 *	name			"."		"ls"
69 *	type			S_IFDIR		S_IFREG
70 *	next			  >		NULL
71 *	parent			  ^		^ (to "bin")
72 *	child			NULL		NULL
73 *
74 * Notes:
75 *	-   first always points to first entry, at current level, which
76 *	    must be "." when the tree has been built; during build it may
77 *	    not be if "." hasn't yet been found by readdir(2).
78 */
79
80enum fi_flags {
81	FI_SIZED =	1<<0,		/* inode sized */
82	FI_ALLOCATED =	1<<1,		/* fsinode->ino allocated */
83	FI_WRITTEN =	1<<2,		/* inode written */
84};
85
86typedef struct {
87	uint32_t	 ino;		/* inode number used on target fs */
88	uint32_t	 nlink;		/* number of links to this entry */
89	enum fi_flags	 flags;		/* flags used by fs specific code */
90	struct stat	 st;		/* stat entry */
91	void		*fsuse;		/* for storing FS dependent info */
92} fsinode;
93
94typedef struct _fsnode {
95	struct _fsnode	*parent;	/* parent (NULL if root) */
96	struct _fsnode	*child;		/* child (if type == S_IFDIR) */
97	struct _fsnode	*next;		/* next */
98	struct _fsnode	*first;		/* first node of current level (".") */
99	uint32_t	 type;		/* type of entry */
100	fsinode		*inode;		/* actual inode data */
101	char		*symlink;	/* symlink target */
102	const char	*root;		/* root path */
103	char		*path;		/* directory name */
104	char		*name;		/* file name */
105	int		flags;		/* misc flags */
106} fsnode;
107
108#define	FSNODE_F_HASSPEC	0x01	/* fsnode has a spec entry */
109
110/*
111 * option_t - contains option name, description, pointer to location to store
112 * result, and range checks for the result. Used to simplify fs specific
113 * option setting
114 */
115typedef enum {
116	OPT_STRARRAY,
117	OPT_STRPTR,
118	OPT_STRBUF,
119	OPT_BOOL,
120	OPT_INT8,
121	OPT_INT16,
122	OPT_INT32,
123	OPT_INT64
124} opttype_t;
125
126typedef struct {
127	char		letter;		/* option letter NUL for none */
128	const char	*name;		/* option name */
129	void		*value;		/* where to stuff the value */
130	opttype_t	type;		/* type of entry */
131	long long	minimum;	/* minimum for value */
132	long long	maximum;	/* maximum for value */
133	const char	*desc;		/* option description */
134} option_t;
135
136/*
137 * fsinfo_t - contains various settings and parameters pertaining to
138 * the image, including current settings, global options, and fs
139 * specific options
140 */
141typedef struct makefs_fsinfo {
142		/* current settings */
143	off_t	size;		/* total size */
144	off_t	inodes;		/* number of inodes */
145	uint32_t curinode;	/* current inode */
146
147		/* image settings */
148	int	fd;		/* file descriptor of image */
149	void	*superblock;	/* superblock */
150
151		/* global options */
152	off_t	minsize;	/* minimum size image should be */
153	off_t	maxsize;	/* maximum size image can be */
154	off_t	freefiles;	/* free file entries to leave */
155	off_t	freeblocks;	/* free blocks to leave */
156	off_t	offset;		/* offset from start of file */
157	int	freefilepc;	/* free file % */
158	int	freeblockpc;	/* free block % */
159	int	sectorsize;	/* sector size */
160
161	void	*fs_specific;	/* File system specific additions. */
162	option_t *fs_options;	/* File system specific options */
163} fsinfo_t;
164
165
166
167
168void		dump_fsnodes(fsnode *);
169const char *	inode_type(mode_t);
170int		set_option(const option_t *, const char *, char *, size_t);
171int		set_option_var(const option_t *, const char *, const char *,
172    char *, size_t);
173fsnode *	walk_dir(const char *, const char *, fsnode *, fsnode *);
174void		free_fsnodes(fsnode *);
175option_t *	copy_opts(const option_t *);
176
177#define DECLARE_FUN(fs)							\
178void		fs ## _prep_opts(fsinfo_t *);				\
179int		fs ## _parse_opts(const char *, fsinfo_t *);		\
180void		fs ## _cleanup_opts(fsinfo_t *);			\
181void		fs ## _makefs(const char *, const char *, fsnode *, fsinfo_t *)
182
183DECLARE_FUN(ffs);
184DECLARE_FUN(cd9660);
185DECLARE_FUN(msdos);
186
187extern	u_int		debug;
188extern	struct timespec	start_time;
189extern	struct stat stampst;
190
191#define	DEBUG_TIME			0x00000001
192		/* debug bits 1..3 unused at this time */
193#define	DEBUG_WALK_DIR			0x00000010
194#define	DEBUG_WALK_DIR_NODE		0x00000020
195#define	DEBUG_WALK_DIR_LINKCHECK	0x00000040
196#define	DEBUG_DUMP_FSNODES		0x00000080
197#define	DEBUG_DUMP_FSNODES_VERBOSE	0x00000100
198#define	DEBUG_FS_PARSE_OPTS		0x00000200
199#define	DEBUG_FS_MAKEFS			0x00000400
200#define	DEBUG_FS_VALIDATE		0x00000800
201#define	DEBUG_FS_CREATE_IMAGE		0x00001000
202#define	DEBUG_FS_SIZE_DIR		0x00002000
203#define	DEBUG_FS_SIZE_DIR_NODE		0x00004000
204#define	DEBUG_FS_SIZE_DIR_ADD_DIRENT	0x00008000
205#define	DEBUG_FS_POPULATE		0x00010000
206#define	DEBUG_FS_POPULATE_DIRBUF	0x00020000
207#define	DEBUG_FS_POPULATE_NODE		0x00040000
208#define	DEBUG_FS_WRITE_FILE		0x00080000
209#define	DEBUG_FS_WRITE_FILE_BLOCK	0x00100000
210#define	DEBUG_FS_MAKE_DIRBUF		0x00200000
211#define	DEBUG_FS_WRITE_INODE		0x00400000
212#define	DEBUG_BUF_BREAD			0x00800000
213#define	DEBUG_BUF_BWRITE		0x01000000
214#define	DEBUG_BUF_GETBLK		0x02000000
215#define	DEBUG_APPLY_SPECFILE		0x04000000
216#define	DEBUG_APPLY_SPECENTRY		0x08000000
217#define	DEBUG_APPLY_SPECONLY		0x10000000
218
219
220#define	TIMER_START(x)				\
221	if (debug & DEBUG_TIME)			\
222		gettimeofday(&(x), NULL)
223
224#define	TIMER_RESULTS(x,d)				\
225	if (debug & DEBUG_TIME) {			\
226		struct timeval end, td;			\
227		gettimeofday(&end, NULL);		\
228		timersub(&end, &(x), &td);		\
229		printf("%s took %lld.%06ld seconds\n",	\
230		    (d), (long long)td.tv_sec,		\
231		    (long)td.tv_usec);			\
232	}
233
234
235#ifndef	DEFAULT_FSTYPE
236#define	DEFAULT_FSTYPE	"ffs"
237#endif
238
239
240/* xmalloc.c */
241void	*emalloc(size_t);
242void	*ecalloc(size_t, size_t);
243void	*erealloc(void *, size_t);
244char	*estrdup(const char *);
245
246#endif	/* _MAKEFS_H */
247