1214921Scognet/*	$NetBSD: makefs.h,v 1.20 2008/12/28 21:51:46 christos Exp $	*/
2185222Ssam
3330449Seadler/*-
4330449Seadler * SPDX-License-Identifier: BSD-4-Clause
5330449Seadler *
6185222Ssam * Copyright (c) 2001 Wasabi Systems, Inc.
7185222Ssam * All rights reserved.
8185222Ssam *
9185222Ssam * Written by Luke Mewburn for Wasabi Systems, Inc.
10185222Ssam *
11185222Ssam * Redistribution and use in source and binary forms, with or without
12185222Ssam * modification, are permitted provided that the following conditions
13185222Ssam * are met:
14185222Ssam * 1. Redistributions of source code must retain the above copyright
15185222Ssam *    notice, this list of conditions and the following disclaimer.
16185222Ssam * 2. Redistributions in binary form must reproduce the above copyright
17185222Ssam *    notice, this list of conditions and the following disclaimer in the
18185222Ssam *    documentation and/or other materials provided with the distribution.
19185222Ssam * 3. All advertising materials mentioning features or use of this software
20185222Ssam *    must display the following acknowledgement:
21185222Ssam *      This product includes software developed for the NetBSD Project by
22185222Ssam *      Wasabi Systems, Inc.
23185222Ssam * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24185222Ssam *    or promote products derived from this software without specific prior
25185222Ssam *    written permission.
26185222Ssam *
27185222Ssam * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28185222Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29185222Ssam * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30185222Ssam * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
31185222Ssam * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32185222Ssam * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33185222Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34185222Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35185222Ssam * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36185222Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37185222Ssam * POSSIBILITY OF SUCH DAMAGE.
38186334Ssam *
39186334Ssam * $FreeBSD: stable/11/usr.sbin/makefs/makefs.h 332978 2018-04-25 01:20:25Z benno $
40185222Ssam */
41185222Ssam
42185222Ssam#ifndef	_MAKEFS_H
43185222Ssam#define	_MAKEFS_H
44185222Ssam
45185222Ssam#include <sys/stat.h>
46185222Ssam#include <err.h>
47185222Ssam
48185222Ssam/*
49185222Ssam * fsnode -
50185222Ssam *	a component of the tree; contains a filename, a pointer to
51185222Ssam *	fsinode, optional symlink name, and tree pointers
52185222Ssam *
53185222Ssam * fsinode -
54185222Ssam *	equivalent to an inode, containing target file system inode number,
55185222Ssam *	refcount (nlink), and stat buffer
56185222Ssam *
57185222Ssam * A tree of fsnodes looks like this:
58185222Ssam *
59185222Ssam *	name	"."		"bin"		"netbsd"
60185222Ssam *	type	S_IFDIR		S_IFDIR		S_IFREG
61185222Ssam *	next 	  >		  >		NULL
62185222Ssam *	parent	NULL		NULL		NULL
63185222Ssam *	child	NULL		  v
64185222Ssam *
65185222Ssam *	name			"."		"ls"
66185222Ssam *	type			S_IFDIR		S_IFREG
67185222Ssam *	next			  >		NULL
68185222Ssam *	parent			  ^		^ (to "bin")
69185222Ssam *	child			NULL		NULL
70185222Ssam *
71185222Ssam * Notes:
72185222Ssam *	-   first always points to first entry, at current level, which
73185222Ssam *	    must be "." when the tree has been built; during build it may
74185222Ssam *	    not be if "." hasn't yet been found by readdir(2).
75185222Ssam */
76185222Ssam
77185222Ssamenum fi_flags {
78185222Ssam	FI_SIZED =	1<<0,		/* inode sized */
79185222Ssam	FI_ALLOCATED =	1<<1,		/* fsinode->ino allocated */
80185222Ssam	FI_WRITTEN =	1<<2,		/* inode written */
81185222Ssam};
82185222Ssam
83185222Ssamtypedef struct {
84185222Ssam	uint32_t	 ino;		/* inode number used on target fs */
85185222Ssam	uint32_t	 nlink;		/* number of links to this entry */
86185222Ssam	enum fi_flags	 flags;		/* flags used by fs specific code */
87185222Ssam	struct stat	 st;		/* stat entry */
88185222Ssam} fsinode;
89185222Ssam
90185222Ssamtypedef struct _fsnode {
91185222Ssam	struct _fsnode	*parent;	/* parent (NULL if root) */
92185222Ssam	struct _fsnode	*child;		/* child (if type == S_IFDIR) */
93185222Ssam	struct _fsnode	*next;		/* next */
94185222Ssam	struct _fsnode	*first;		/* first node of current level (".") */
95185222Ssam	uint32_t	 type;		/* type of entry */
96185222Ssam	fsinode		*inode;		/* actual inode data */
97185222Ssam	char		*symlink;	/* symlink target */
98223306Smarcel	char		*contents;	/* file to provide contents */
99230795Sjkim	const char	*root;		/* root path */
100230795Sjkim	char		*path;		/* directory name */
101185222Ssam	char		*name;		/* file name */
102185222Ssam	int		flags;		/* misc flags */
103185222Ssam} fsnode;
104185222Ssam
105185222Ssam#define	FSNODE_F_HASSPEC	0x01	/* fsnode has a spec entry */
106223306Smarcel#define	FSNODE_F_OPTIONAL	0x02	/* fsnode is optional */
107185222Ssam
108185222Ssam/*
109332978Sbenno * option_t - contains option name, description, pointer to location to store
110332978Sbenno * result, and range checks for the result. Used to simplify fs specific
111332978Sbenno * option setting
112332978Sbenno */
113332978Sbennotypedef enum {
114332978Sbenno	OPT_STRARRAY,
115332978Sbenno	OPT_STRPTR,
116332978Sbenno	OPT_STRBUF,
117332978Sbenno	OPT_BOOL,
118332978Sbenno	OPT_INT8,
119332978Sbenno	OPT_INT16,
120332978Sbenno	OPT_INT32,
121332978Sbenno	OPT_INT64
122332978Sbenno} opttype_t;
123332978Sbenno
124332978Sbennotypedef struct {
125332978Sbenno	char		letter;		/* option letter NUL for none */
126332978Sbenno	const char	*name;		/* option name */
127332978Sbenno	void		*value;		/* where to stuff the value */
128332978Sbenno	opttype_t	type;		/* type of entry */
129332978Sbenno	long long	minimum;	/* minimum for value */
130332978Sbenno	long long	maximum;	/* maximum for value */
131332978Sbenno	const char	*desc;		/* option description */
132332978Sbenno} option_t;
133332978Sbenno
134332978Sbenno/*
135185222Ssam * fsinfo_t - contains various settings and parameters pertaining to
136185222Ssam * the image, including current settings, global options, and fs
137185222Ssam * specific options
138185222Ssam */
139332978Sbennotypedef struct makefs_fsinfo {
140185222Ssam		/* current settings */
141185222Ssam	off_t	size;		/* total size */
142185222Ssam	off_t	inodes;		/* number of inodes */
143185222Ssam	uint32_t curinode;	/* current inode */
144185222Ssam
145185222Ssam		/* image settings */
146185222Ssam	int	fd;		/* file descriptor of image */
147185222Ssam	void	*superblock;	/* superblock */
148185222Ssam	int	onlyspec;	/* only add entries in specfile */
149185222Ssam
150185222Ssam
151185222Ssam		/* global options */
152185222Ssam	off_t	minsize;	/* minimum size image should be */
153185222Ssam	off_t	maxsize;	/* maximum size image can be */
154185222Ssam	off_t	freefiles;	/* free file entries to leave */
155321595Semaste	off_t	freeblocks;	/* free blocks to leave */
156321595Semaste	off_t	roundup;	/* round image size up to this value */
157185222Ssam	int	freefilepc;	/* free file % */
158185222Ssam	int	freeblockpc;	/* free block % */
159185222Ssam	int	needswap;	/* non-zero if byte swapping needed */
160185222Ssam	int	sectorsize;	/* sector size */
161239562Shrs	int	sparse;		/* sparse image, don't fill it with zeros */
162185222Ssam
163214921Scognet	void	*fs_specific;	/* File system specific additions. */
164332978Sbenno	option_t *fs_options;	/* File system specific options */
165185222Ssam} fsinfo_t;
166185222Ssam
167185222Ssam
168214921Scognetvoid		apply_specfile(const char *, const char *, fsnode *, int);
169230795Sjkimvoid		dump_fsnodes(fsnode *);
170185222Ssamconst char *	inode_type(mode_t);
171223306Smarcelfsnode *	read_mtree(const char *, fsnode *);
172332978Sbennoint		set_option(const option_t *, const char *, char *, size_t);
173332978Sbennoint		set_option_var(const option_t *, const char *, const char *,
174332978Sbenno    char *, size_t);
175230795Sjkimfsnode *	walk_dir(const char *, const char *, fsnode *, fsnode *);
176214921Scognetvoid		free_fsnodes(fsnode *);
177332978Sbennooption_t *	copy_opts(const option_t *);
178185222Ssam
179214921Scognetvoid		ffs_prep_opts(fsinfo_t *);
180185222Ssamint		ffs_parse_opts(const char *, fsinfo_t *);
181214921Scognetvoid		ffs_cleanup_opts(fsinfo_t *);
182185222Ssamvoid		ffs_makefs(const char *, const char *, fsnode *, fsinfo_t *);
183185222Ssam
184214921Scognetvoid		cd9660_prep_opts(fsinfo_t *);
185214921Scognetint		cd9660_parse_opts(const char *, fsinfo_t *);
186214921Scognetvoid		cd9660_cleanup_opts(fsinfo_t *);
187214921Scognetvoid		cd9660_makefs(const char *, const char *, fsnode *, fsinfo_t *);
188185222Ssam
189185222Ssam
190185222Ssamextern	u_int		debug;
191247041Sbrooksextern	int		dupsok;
192185222Ssamextern	struct timespec	start_time;
193301879Semasteextern struct stat stampst;
194185222Ssam
195185222Ssam/*
196185222Ssam * If -x is specified, we want to exclude nodes which do not appear
197185222Ssam * in the spec file.
198185222Ssam */
199185222Ssam#define	FSNODE_EXCLUDE_P(opts, fsnode)	\
200185222Ssam	((opts)->onlyspec != 0 && ((fsnode)->flags & FSNODE_F_HASSPEC) == 0)
201185222Ssam
202185222Ssam#define	DEBUG_TIME			0x00000001
203185222Ssam		/* debug bits 1..3 unused at this time */
204185222Ssam#define	DEBUG_WALK_DIR			0x00000010
205185222Ssam#define	DEBUG_WALK_DIR_NODE		0x00000020
206185222Ssam#define	DEBUG_WALK_DIR_LINKCHECK	0x00000040
207185222Ssam#define	DEBUG_DUMP_FSNODES		0x00000080
208185222Ssam#define	DEBUG_DUMP_FSNODES_VERBOSE	0x00000100
209185222Ssam#define	DEBUG_FS_PARSE_OPTS		0x00000200
210185222Ssam#define	DEBUG_FS_MAKEFS			0x00000400
211185222Ssam#define	DEBUG_FS_VALIDATE		0x00000800
212185222Ssam#define	DEBUG_FS_CREATE_IMAGE		0x00001000
213185222Ssam#define	DEBUG_FS_SIZE_DIR		0x00002000
214185222Ssam#define	DEBUG_FS_SIZE_DIR_NODE		0x00004000
215185222Ssam#define	DEBUG_FS_SIZE_DIR_ADD_DIRENT	0x00008000
216185222Ssam#define	DEBUG_FS_POPULATE		0x00010000
217185222Ssam#define	DEBUG_FS_POPULATE_DIRBUF	0x00020000
218185222Ssam#define	DEBUG_FS_POPULATE_NODE		0x00040000
219185222Ssam#define	DEBUG_FS_WRITE_FILE		0x00080000
220185222Ssam#define	DEBUG_FS_WRITE_FILE_BLOCK	0x00100000
221185222Ssam#define	DEBUG_FS_MAKE_DIRBUF		0x00200000
222185222Ssam#define	DEBUG_FS_WRITE_INODE		0x00400000
223185222Ssam#define	DEBUG_BUF_BREAD			0x00800000
224185222Ssam#define	DEBUG_BUF_BWRITE		0x01000000
225185222Ssam#define	DEBUG_BUF_GETBLK		0x02000000
226185222Ssam#define	DEBUG_APPLY_SPECFILE		0x04000000
227185222Ssam#define	DEBUG_APPLY_SPECENTRY		0x08000000
228214921Scognet#define	DEBUG_APPLY_SPECONLY		0x10000000
229185222Ssam
230185222Ssam
231185222Ssam#define	TIMER_START(x)				\
232185222Ssam	if (debug & DEBUG_TIME)			\
233185222Ssam		gettimeofday(&(x), NULL)
234185222Ssam
235185222Ssam#define	TIMER_RESULTS(x,d)				\
236185222Ssam	if (debug & DEBUG_TIME) {			\
237185222Ssam		struct timeval end, td;			\
238185222Ssam		gettimeofday(&end, NULL);		\
239185222Ssam		timersub(&end, &(x), &td);		\
240214921Scognet		printf("%s took %lld.%06ld seconds\n",	\
241214921Scognet		    (d), (long long)td.tv_sec,		\
242214921Scognet		    (long)td.tv_usec);			\
243185222Ssam	}
244185222Ssam
245185222Ssam
246185222Ssam#ifndef	DEFAULT_FSTYPE
247185222Ssam#define	DEFAULT_FSTYPE	"ffs"
248185222Ssam#endif
249185222Ssam
250185222Ssam
251185222Ssam/*
252185222Ssam *	ffs specific settings
253185222Ssam *	---------------------
254185222Ssam */
255185222Ssam
256185222Ssam#define	FFS_EI		/* for opposite endian support in ffs headers */
257185222Ssam
258186261Ssam/*
259186261Ssam * Write-arounds/compat shims for endian-agnostic support.
260186261Ssam * These belong in the kernel if/when it's possible to mount
261186261Ssam * filesystems w/ either byte order.
262186261Ssam */
263185222Ssam
264186261Ssam/*
265186261Ssam * File system internal flags, also in fs_flags.
266186261Ssam * (Pick highest number to avoid conflicts with others)
267186261Ssam */
268214921Scognet#define        FS_SWAPPED      0x80000000      /* file system is endian swapped */
269214921Scognet#define        FS_INTERNAL     0x80000000      /* mask for internal flags */
270186261Ssam
271214921Scognet#define        FS_ISCLEAN      1
272186261Ssam
273214921Scognet#define        DINODE1_SIZE    (sizeof(struct ufs1_dinode))
274214921Scognet#define        DINODE2_SIZE    (sizeof(struct ufs2_dinode))
275186261Ssam
276214921Scognet#define MAXSYMLINKLEN_UFS1     ((NDADDR + NIADDR) * sizeof(ufs1_daddr_t))
277214921Scognet#define MAXSYMLINKLEN_UFS2     ((NDADDR + NIADDR) * sizeof(ufs2_daddr_t))
278186261Ssam
279186261Ssam#if (BYTE_ORDER == LITTLE_ENDIAN)
280214921Scognet#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
281214921Scognet    (((oldfmt) && !(needswap)) ?       \
282186261Ssam    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
283186261Ssam#else
284214921Scognet#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
285214921Scognet    (((oldfmt) && (needswap)) ?                \
286186261Ssam    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
287186261Ssam#endif
288186261Ssam
289214921Scognet#define        cg_chkmagic_swap(cgp, ns) \
290186264Ssam    (ufs_rw32((cgp)->cg_magic, (ns)) == CG_MAGIC)
291214921Scognet#define        cg_inosused_swap(cgp, ns) \
292186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_iusedoff, (ns))))
293214921Scognet#define        cg_blksfree_swap(cgp, ns) \
294186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_freeoff, (ns))))
295214921Scognet#define        cg_clustersfree_swap(cgp, ns) \
296186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_clusteroff, (ns))))
297214921Scognet#define        cg_clustersum_swap(cgp, ns) \
298186261Ssam    ((int32_t *)((uintptr_t)(cgp) + ufs_rw32((cgp)->cg_clustersumoff, ns)))
299186261Ssam
300186261Ssamstruct fs;
301214921Scognetvoid   ffs_fragacct_swap(struct fs *, int, int32_t [], int, int);
302186261Ssam
303247052Sbrooksfsinode *link_check(fsinode *);
304247052Sbrooks
305185222Ssam#endif	/* _MAKEFS_H */
306