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: releng/10.3/usr.sbin/makefs/makefs.h 290589 2015-11-09 08:59:55Z ngie $
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 */
133290589Sngie	off_t	roundup;	/* round image size up to this value */
134185222Ssam
135214921Scognet	void	*fs_specific;	/* File system specific additions. */
136185222Ssam} fsinfo_t;
137185222Ssam
138185222Ssam
139185222Ssam/*
140185222Ssam * option_t - contains option name, description, pointer to location to store
141185222Ssam * result, and range checks for the result. Used to simplify fs specific
142185222Ssam * option setting
143185222Ssam */
144185222Ssamtypedef struct {
145185222Ssam	const char	*name;		/* option name */
146185222Ssam	int		*value;		/* where to stuff the value */
147185222Ssam	int		minimum;	/* minimum for value */
148185222Ssam	int		maximum;	/* maximum for value */
149185222Ssam	const char	*desc;		/* option description */
150185222Ssam} option_t;
151185222Ssam
152185222Ssam
153214921Scognetvoid		apply_specfile(const char *, const char *, fsnode *, int);
154230795Sjkimvoid		dump_fsnodes(fsnode *);
155185222Ssamconst char *	inode_type(mode_t);
156223306Smarcelfsnode *	read_mtree(const char *, fsnode *);
157185222Ssamint		set_option(option_t *, const char *, const char *);
158230795Sjkimfsnode *	walk_dir(const char *, const char *, fsnode *, fsnode *);
159214921Scognetvoid		free_fsnodes(fsnode *);
160185222Ssam
161214921Scognetvoid		ffs_prep_opts(fsinfo_t *);
162185222Ssamint		ffs_parse_opts(const char *, fsinfo_t *);
163214921Scognetvoid		ffs_cleanup_opts(fsinfo_t *);
164185222Ssamvoid		ffs_makefs(const char *, const char *, fsnode *, fsinfo_t *);
165185222Ssam
166214921Scognetvoid		cd9660_prep_opts(fsinfo_t *);
167214921Scognetint		cd9660_parse_opts(const char *, fsinfo_t *);
168214921Scognetvoid		cd9660_cleanup_opts(fsinfo_t *);
169214921Scognetvoid		cd9660_makefs(const char *, const char *, fsnode *, fsinfo_t *);
170185222Ssam
171185222Ssam
172185222Ssamextern	u_int		debug;
173247041Sbrooksextern	int		dupsok;
174185222Ssamextern	struct timespec	start_time;
175185222Ssam
176185222Ssam/*
177185222Ssam * If -x is specified, we want to exclude nodes which do not appear
178185222Ssam * in the spec file.
179185222Ssam */
180185222Ssam#define	FSNODE_EXCLUDE_P(opts, fsnode)	\
181185222Ssam	((opts)->onlyspec != 0 && ((fsnode)->flags & FSNODE_F_HASSPEC) == 0)
182185222Ssam
183185222Ssam#define	DEBUG_TIME			0x00000001
184185222Ssam		/* debug bits 1..3 unused at this time */
185185222Ssam#define	DEBUG_WALK_DIR			0x00000010
186185222Ssam#define	DEBUG_WALK_DIR_NODE		0x00000020
187185222Ssam#define	DEBUG_WALK_DIR_LINKCHECK	0x00000040
188185222Ssam#define	DEBUG_DUMP_FSNODES		0x00000080
189185222Ssam#define	DEBUG_DUMP_FSNODES_VERBOSE	0x00000100
190185222Ssam#define	DEBUG_FS_PARSE_OPTS		0x00000200
191185222Ssam#define	DEBUG_FS_MAKEFS			0x00000400
192185222Ssam#define	DEBUG_FS_VALIDATE		0x00000800
193185222Ssam#define	DEBUG_FS_CREATE_IMAGE		0x00001000
194185222Ssam#define	DEBUG_FS_SIZE_DIR		0x00002000
195185222Ssam#define	DEBUG_FS_SIZE_DIR_NODE		0x00004000
196185222Ssam#define	DEBUG_FS_SIZE_DIR_ADD_DIRENT	0x00008000
197185222Ssam#define	DEBUG_FS_POPULATE		0x00010000
198185222Ssam#define	DEBUG_FS_POPULATE_DIRBUF	0x00020000
199185222Ssam#define	DEBUG_FS_POPULATE_NODE		0x00040000
200185222Ssam#define	DEBUG_FS_WRITE_FILE		0x00080000
201185222Ssam#define	DEBUG_FS_WRITE_FILE_BLOCK	0x00100000
202185222Ssam#define	DEBUG_FS_MAKE_DIRBUF		0x00200000
203185222Ssam#define	DEBUG_FS_WRITE_INODE		0x00400000
204185222Ssam#define	DEBUG_BUF_BREAD			0x00800000
205185222Ssam#define	DEBUG_BUF_BWRITE		0x01000000
206185222Ssam#define	DEBUG_BUF_GETBLK		0x02000000
207185222Ssam#define	DEBUG_APPLY_SPECFILE		0x04000000
208185222Ssam#define	DEBUG_APPLY_SPECENTRY		0x08000000
209214921Scognet#define	DEBUG_APPLY_SPECONLY		0x10000000
210185222Ssam
211185222Ssam
212185222Ssam#define	TIMER_START(x)				\
213185222Ssam	if (debug & DEBUG_TIME)			\
214185222Ssam		gettimeofday(&(x), NULL)
215185222Ssam
216185222Ssam#define	TIMER_RESULTS(x,d)				\
217185222Ssam	if (debug & DEBUG_TIME) {			\
218185222Ssam		struct timeval end, td;			\
219185222Ssam		gettimeofday(&end, NULL);		\
220185222Ssam		timersub(&end, &(x), &td);		\
221214921Scognet		printf("%s took %lld.%06ld seconds\n",	\
222214921Scognet		    (d), (long long)td.tv_sec,		\
223214921Scognet		    (long)td.tv_usec);			\
224185222Ssam	}
225185222Ssam
226185222Ssam
227185222Ssam#ifndef	DEFAULT_FSTYPE
228185222Ssam#define	DEFAULT_FSTYPE	"ffs"
229185222Ssam#endif
230185222Ssam
231185222Ssam
232185222Ssam/*
233185222Ssam *	ffs specific settings
234185222Ssam *	---------------------
235185222Ssam */
236185222Ssam
237185222Ssam#define	FFS_EI		/* for opposite endian support in ffs headers */
238185222Ssam
239186261Ssam/*
240186261Ssam * Write-arounds/compat shims for endian-agnostic support.
241186261Ssam * These belong in the kernel if/when it's possible to mount
242186261Ssam * filesystems w/ either byte order.
243186261Ssam */
244185222Ssam
245186261Ssam/*
246186261Ssam * File system internal flags, also in fs_flags.
247186261Ssam * (Pick highest number to avoid conflicts with others)
248186261Ssam */
249214921Scognet#define        FS_SWAPPED      0x80000000      /* file system is endian swapped */
250214921Scognet#define        FS_INTERNAL     0x80000000      /* mask for internal flags */
251186261Ssam
252214921Scognet#define        FS_ISCLEAN      1
253186261Ssam
254214921Scognet#define        DINODE1_SIZE    (sizeof(struct ufs1_dinode))
255214921Scognet#define        DINODE2_SIZE    (sizeof(struct ufs2_dinode))
256186261Ssam
257214921Scognet#define MAXSYMLINKLEN_UFS1     ((NDADDR + NIADDR) * sizeof(ufs1_daddr_t))
258214921Scognet#define MAXSYMLINKLEN_UFS2     ((NDADDR + NIADDR) * sizeof(ufs2_daddr_t))
259186261Ssam
260186261Ssam#if (BYTE_ORDER == LITTLE_ENDIAN)
261214921Scognet#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
262214921Scognet    (((oldfmt) && !(needswap)) ?       \
263186261Ssam    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
264186261Ssam#else
265214921Scognet#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
266214921Scognet    (((oldfmt) && (needswap)) ?                \
267186261Ssam    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
268186261Ssam#endif
269186261Ssam
270214921Scognet#define        cg_chkmagic_swap(cgp, ns) \
271186264Ssam    (ufs_rw32((cgp)->cg_magic, (ns)) == CG_MAGIC)
272214921Scognet#define        cg_inosused_swap(cgp, ns) \
273186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_iusedoff, (ns))))
274214921Scognet#define        cg_blksfree_swap(cgp, ns) \
275186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_freeoff, (ns))))
276214921Scognet#define        cg_clustersfree_swap(cgp, ns) \
277186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_clusteroff, (ns))))
278214921Scognet#define        cg_clustersum_swap(cgp, ns) \
279186261Ssam    ((int32_t *)((uintptr_t)(cgp) + ufs_rw32((cgp)->cg_clustersumoff, ns)))
280186261Ssam
281186261Ssamstruct fs;
282214921Scognetvoid   ffs_fragacct_swap(struct fs *, int, int32_t [], int, int);
283186261Ssam
284247052Sbrooksfsinode *link_check(fsinode *);
285247052Sbrooks
286185222Ssam#endif	/* _MAKEFS_H */
287