1214921Scognet/*	$NetBSD: makefs.h,v 1.20 2008/12/28 21:51:46 christos Exp $	*/
2185222Ssam
3185222Ssam/*
4185222Ssam * Copyright (c) 2001 Wasabi Systems, Inc.
5185222Ssam * All rights reserved.
6185222Ssam *
7185222Ssam * Written by Luke Mewburn for Wasabi Systems, Inc.
8185222Ssam *
9185222Ssam * Redistribution and use in source and binary forms, with or without
10185222Ssam * modification, are permitted provided that the following conditions
11185222Ssam * are met:
12185222Ssam * 1. Redistributions of source code must retain the above copyright
13185222Ssam *    notice, this list of conditions and the following disclaimer.
14185222Ssam * 2. Redistributions in binary form must reproduce the above copyright
15185222Ssam *    notice, this list of conditions and the following disclaimer in the
16185222Ssam *    documentation and/or other materials provided with the distribution.
17185222Ssam * 3. All advertising materials mentioning features or use of this software
18185222Ssam *    must display the following acknowledgement:
19185222Ssam *      This product includes software developed for the NetBSD Project by
20185222Ssam *      Wasabi Systems, Inc.
21185222Ssam * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22185222Ssam *    or promote products derived from this software without specific prior
23185222Ssam *    written permission.
24185222Ssam *
25185222Ssam * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26185222Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27185222Ssam * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28185222Ssam * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29185222Ssam * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30185222Ssam * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31185222Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32185222Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33185222Ssam * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34185222Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35185222Ssam * POSSIBILITY OF SUCH DAMAGE.
36186334Ssam *
37186334Ssam * $FreeBSD$
38185222Ssam */
39185222Ssam
40185222Ssam#ifndef	_MAKEFS_H
41185222Ssam#define	_MAKEFS_H
42185222Ssam
43185222Ssam#include <sys/stat.h>
44185222Ssam#include <err.h>
45185222Ssam
46185222Ssam/*
47185222Ssam * fsnode -
48185222Ssam *	a component of the tree; contains a filename, a pointer to
49185222Ssam *	fsinode, optional symlink name, and tree pointers
50185222Ssam *
51185222Ssam * fsinode -
52185222Ssam *	equivalent to an inode, containing target file system inode number,
53185222Ssam *	refcount (nlink), and stat buffer
54185222Ssam *
55185222Ssam * A tree of fsnodes looks like this:
56185222Ssam *
57185222Ssam *	name	"."		"bin"		"netbsd"
58185222Ssam *	type	S_IFDIR		S_IFDIR		S_IFREG
59185222Ssam *	next 	  >		  >		NULL
60185222Ssam *	parent	NULL		NULL		NULL
61185222Ssam *	child	NULL		  v
62185222Ssam *
63185222Ssam *	name			"."		"ls"
64185222Ssam *	type			S_IFDIR		S_IFREG
65185222Ssam *	next			  >		NULL
66185222Ssam *	parent			  ^		^ (to "bin")
67185222Ssam *	child			NULL		NULL
68185222Ssam *
69185222Ssam * Notes:
70185222Ssam *	-   first always points to first entry, at current level, which
71185222Ssam *	    must be "." when the tree has been built; during build it may
72185222Ssam *	    not be if "." hasn't yet been found by readdir(2).
73185222Ssam */
74185222Ssam
75185222Ssamenum fi_flags {
76185222Ssam	FI_SIZED =	1<<0,		/* inode sized */
77185222Ssam	FI_ALLOCATED =	1<<1,		/* fsinode->ino allocated */
78185222Ssam	FI_WRITTEN =	1<<2,		/* inode written */
79185222Ssam};
80185222Ssam
81185222Ssamtypedef struct {
82185222Ssam	uint32_t	 ino;		/* inode number used on target fs */
83185222Ssam	uint32_t	 nlink;		/* number of links to this entry */
84185222Ssam	enum fi_flags	 flags;		/* flags used by fs specific code */
85185222Ssam	struct stat	 st;		/* stat entry */
86185222Ssam} fsinode;
87185222Ssam
88185222Ssamtypedef struct _fsnode {
89185222Ssam	struct _fsnode	*parent;	/* parent (NULL if root) */
90185222Ssam	struct _fsnode	*child;		/* child (if type == S_IFDIR) */
91185222Ssam	struct _fsnode	*next;		/* next */
92185222Ssam	struct _fsnode	*first;		/* first node of current level (".") */
93185222Ssam	uint32_t	 type;		/* type of entry */
94185222Ssam	fsinode		*inode;		/* actual inode data */
95185222Ssam	char		*symlink;	/* symlink target */
96223306Smarcel	char		*contents;	/* file to provide contents */
97230795Sjkim	const char	*root;		/* root path */
98230795Sjkim	char		*path;		/* directory name */
99185222Ssam	char		*name;		/* file name */
100185222Ssam	int		flags;		/* misc flags */
101185222Ssam} fsnode;
102185222Ssam
103185222Ssam#define	FSNODE_F_HASSPEC	0x01	/* fsnode has a spec entry */
104223306Smarcel#define	FSNODE_F_OPTIONAL	0x02	/* fsnode is optional */
105185222Ssam
106185222Ssam/*
107185222Ssam * fsinfo_t - contains various settings and parameters pertaining to
108185222Ssam * the image, including current settings, global options, and fs
109185222Ssam * specific options
110185222Ssam */
111185222Ssamtypedef struct {
112185222Ssam		/* current settings */
113185222Ssam	off_t	size;		/* total size */
114185222Ssam	off_t	inodes;		/* number of inodes */
115185222Ssam	uint32_t curinode;	/* current inode */
116185222Ssam
117185222Ssam		/* image settings */
118185222Ssam	int	fd;		/* file descriptor of image */
119185222Ssam	void	*superblock;	/* superblock */
120185222Ssam	int	onlyspec;	/* only add entries in specfile */
121185222Ssam
122185222Ssam
123185222Ssam		/* global options */
124185222Ssam	off_t	minsize;	/* minimum size image should be */
125185222Ssam	off_t	maxsize;	/* maximum size image can be */
126185222Ssam	off_t	freefiles;	/* free file entries to leave */
127185222Ssam	int	freefilepc;	/* free file % */
128185222Ssam	off_t	freeblocks;	/* free blocks to leave */
129185222Ssam	int	freeblockpc;	/* free block % */
130185222Ssam	int	needswap;	/* non-zero if byte swapping needed */
131185222Ssam	int	sectorsize;	/* sector size */
132239562Shrs	int	sparse;		/* sparse image, don't fill it with zeros */
133185222Ssam
134214921Scognet	void	*fs_specific;	/* File system specific additions. */
135185222Ssam} fsinfo_t;
136185222Ssam
137185222Ssam
138185222Ssam/*
139185222Ssam * option_t - contains option name, description, pointer to location to store
140185222Ssam * result, and range checks for the result. Used to simplify fs specific
141185222Ssam * option setting
142185222Ssam */
143185222Ssamtypedef struct {
144185222Ssam	const char	*name;		/* option name */
145185222Ssam	int		*value;		/* where to stuff the value */
146185222Ssam	int		minimum;	/* minimum for value */
147185222Ssam	int		maximum;	/* maximum for value */
148185222Ssam	const char	*desc;		/* option description */
149185222Ssam} option_t;
150185222Ssam
151185222Ssam
152214921Scognetvoid		apply_specfile(const char *, const char *, fsnode *, int);
153230795Sjkimvoid		dump_fsnodes(fsnode *);
154185222Ssamconst char *	inode_type(mode_t);
155223306Smarcelfsnode *	read_mtree(const char *, fsnode *);
156185222Ssamint		set_option(option_t *, const char *, const char *);
157230795Sjkimfsnode *	walk_dir(const char *, const char *, fsnode *, fsnode *);
158214921Scognetvoid		free_fsnodes(fsnode *);
159185222Ssam
160214921Scognetvoid		ffs_prep_opts(fsinfo_t *);
161185222Ssamint		ffs_parse_opts(const char *, fsinfo_t *);
162214921Scognetvoid		ffs_cleanup_opts(fsinfo_t *);
163185222Ssamvoid		ffs_makefs(const char *, const char *, fsnode *, fsinfo_t *);
164185222Ssam
165214921Scognetvoid		cd9660_prep_opts(fsinfo_t *);
166214921Scognetint		cd9660_parse_opts(const char *, fsinfo_t *);
167214921Scognetvoid		cd9660_cleanup_opts(fsinfo_t *);
168214921Scognetvoid		cd9660_makefs(const char *, const char *, fsnode *, fsinfo_t *);
169185222Ssam
170185222Ssam
171185222Ssamextern	u_int		debug;
172247041Sbrooksextern	int		dupsok;
173185222Ssamextern	struct timespec	start_time;
174185222Ssam
175185222Ssam/*
176185222Ssam * If -x is specified, we want to exclude nodes which do not appear
177185222Ssam * in the spec file.
178185222Ssam */
179185222Ssam#define	FSNODE_EXCLUDE_P(opts, fsnode)	\
180185222Ssam	((opts)->onlyspec != 0 && ((fsnode)->flags & FSNODE_F_HASSPEC) == 0)
181185222Ssam
182185222Ssam#define	DEBUG_TIME			0x00000001
183185222Ssam		/* debug bits 1..3 unused at this time */
184185222Ssam#define	DEBUG_WALK_DIR			0x00000010
185185222Ssam#define	DEBUG_WALK_DIR_NODE		0x00000020
186185222Ssam#define	DEBUG_WALK_DIR_LINKCHECK	0x00000040
187185222Ssam#define	DEBUG_DUMP_FSNODES		0x00000080
188185222Ssam#define	DEBUG_DUMP_FSNODES_VERBOSE	0x00000100
189185222Ssam#define	DEBUG_FS_PARSE_OPTS		0x00000200
190185222Ssam#define	DEBUG_FS_MAKEFS			0x00000400
191185222Ssam#define	DEBUG_FS_VALIDATE		0x00000800
192185222Ssam#define	DEBUG_FS_CREATE_IMAGE		0x00001000
193185222Ssam#define	DEBUG_FS_SIZE_DIR		0x00002000
194185222Ssam#define	DEBUG_FS_SIZE_DIR_NODE		0x00004000
195185222Ssam#define	DEBUG_FS_SIZE_DIR_ADD_DIRENT	0x00008000
196185222Ssam#define	DEBUG_FS_POPULATE		0x00010000
197185222Ssam#define	DEBUG_FS_POPULATE_DIRBUF	0x00020000
198185222Ssam#define	DEBUG_FS_POPULATE_NODE		0x00040000
199185222Ssam#define	DEBUG_FS_WRITE_FILE		0x00080000
200185222Ssam#define	DEBUG_FS_WRITE_FILE_BLOCK	0x00100000
201185222Ssam#define	DEBUG_FS_MAKE_DIRBUF		0x00200000
202185222Ssam#define	DEBUG_FS_WRITE_INODE		0x00400000
203185222Ssam#define	DEBUG_BUF_BREAD			0x00800000
204185222Ssam#define	DEBUG_BUF_BWRITE		0x01000000
205185222Ssam#define	DEBUG_BUF_GETBLK		0x02000000
206185222Ssam#define	DEBUG_APPLY_SPECFILE		0x04000000
207185222Ssam#define	DEBUG_APPLY_SPECENTRY		0x08000000
208214921Scognet#define	DEBUG_APPLY_SPECONLY		0x10000000
209185222Ssam
210185222Ssam
211185222Ssam#define	TIMER_START(x)				\
212185222Ssam	if (debug & DEBUG_TIME)			\
213185222Ssam		gettimeofday(&(x), NULL)
214185222Ssam
215185222Ssam#define	TIMER_RESULTS(x,d)				\
216185222Ssam	if (debug & DEBUG_TIME) {			\
217185222Ssam		struct timeval end, td;			\
218185222Ssam		gettimeofday(&end, NULL);		\
219185222Ssam		timersub(&end, &(x), &td);		\
220214921Scognet		printf("%s took %lld.%06ld seconds\n",	\
221214921Scognet		    (d), (long long)td.tv_sec,		\
222214921Scognet		    (long)td.tv_usec);			\
223185222Ssam	}
224185222Ssam
225185222Ssam
226185222Ssam#ifndef	DEFAULT_FSTYPE
227185222Ssam#define	DEFAULT_FSTYPE	"ffs"
228185222Ssam#endif
229185222Ssam
230185222Ssam
231185222Ssam/*
232185222Ssam *	ffs specific settings
233185222Ssam *	---------------------
234185222Ssam */
235185222Ssam
236185222Ssam#define	FFS_EI		/* for opposite endian support in ffs headers */
237185222Ssam
238186261Ssam/*
239186261Ssam * Write-arounds/compat shims for endian-agnostic support.
240186261Ssam * These belong in the kernel if/when it's possible to mount
241186261Ssam * filesystems w/ either byte order.
242186261Ssam */
243185222Ssam
244186261Ssam/*
245186261Ssam * File system internal flags, also in fs_flags.
246186261Ssam * (Pick highest number to avoid conflicts with others)
247186261Ssam */
248214921Scognet#define        FS_SWAPPED      0x80000000      /* file system is endian swapped */
249214921Scognet#define        FS_INTERNAL     0x80000000      /* mask for internal flags */
250186261Ssam
251214921Scognet#define        FS_ISCLEAN      1
252186261Ssam
253214921Scognet#define        DINODE1_SIZE    (sizeof(struct ufs1_dinode))
254214921Scognet#define        DINODE2_SIZE    (sizeof(struct ufs2_dinode))
255186261Ssam
256214921Scognet#define MAXSYMLINKLEN_UFS1     ((NDADDR + NIADDR) * sizeof(ufs1_daddr_t))
257214921Scognet#define MAXSYMLINKLEN_UFS2     ((NDADDR + NIADDR) * sizeof(ufs2_daddr_t))
258186261Ssam
259186261Ssam#if (BYTE_ORDER == LITTLE_ENDIAN)
260214921Scognet#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
261214921Scognet    (((oldfmt) && !(needswap)) ?       \
262186261Ssam    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
263186261Ssam#else
264214921Scognet#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
265214921Scognet    (((oldfmt) && (needswap)) ?                \
266186261Ssam    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
267186261Ssam#endif
268186261Ssam
269214921Scognet#define        cg_chkmagic_swap(cgp, ns) \
270186264Ssam    (ufs_rw32((cgp)->cg_magic, (ns)) == CG_MAGIC)
271214921Scognet#define        cg_inosused_swap(cgp, ns) \
272186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_iusedoff, (ns))))
273214921Scognet#define        cg_blksfree_swap(cgp, ns) \
274186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_freeoff, (ns))))
275214921Scognet#define        cg_clustersfree_swap(cgp, ns) \
276186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_clusteroff, (ns))))
277214921Scognet#define        cg_clustersum_swap(cgp, ns) \
278186261Ssam    ((int32_t *)((uintptr_t)(cgp) + ufs_rw32((cgp)->cg_clustersumoff, ns)))
279186261Ssam
280186261Ssamstruct fs;
281214921Scognetvoid   ffs_fragacct_swap(struct fs *, int, int32_t [], int, int);
282186261Ssam
283247052Sbrooksfsinode *link_check(fsinode *);
284247052Sbrooks
285185222Ssam#endif	/* _MAKEFS_H */
286