1/*	$NetBSD: makefs.h,v 1.20 2008/12/28 21:51:46 christos Exp $	*/
2
3/*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 2001 Wasabi Systems, Inc.
7 * All rights reserved.
8 *
9 * Written by Luke Mewburn for Wasabi Systems, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *      This product includes software developed for the NetBSD Project by
22 *      Wasabi Systems, Inc.
23 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24 *    or promote products derived from this software without specific prior
25 *    written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 *
39 * $FreeBSD: stable/11/usr.sbin/makefs/makefs.h 332978 2018-04-25 01:20:25Z benno $
40 */
41
42#ifndef	_MAKEFS_H
43#define	_MAKEFS_H
44
45#include <sys/stat.h>
46#include <err.h>
47
48/*
49 * fsnode -
50 *	a component of the tree; contains a filename, a pointer to
51 *	fsinode, optional symlink name, and tree pointers
52 *
53 * fsinode -
54 *	equivalent to an inode, containing target file system inode number,
55 *	refcount (nlink), and stat buffer
56 *
57 * A tree of fsnodes looks like this:
58 *
59 *	name	"."		"bin"		"netbsd"
60 *	type	S_IFDIR		S_IFDIR		S_IFREG
61 *	next 	  >		  >		NULL
62 *	parent	NULL		NULL		NULL
63 *	child	NULL		  v
64 *
65 *	name			"."		"ls"
66 *	type			S_IFDIR		S_IFREG
67 *	next			  >		NULL
68 *	parent			  ^		^ (to "bin")
69 *	child			NULL		NULL
70 *
71 * Notes:
72 *	-   first always points to first entry, at current level, which
73 *	    must be "." when the tree has been built; during build it may
74 *	    not be if "." hasn't yet been found by readdir(2).
75 */
76
77enum fi_flags {
78	FI_SIZED =	1<<0,		/* inode sized */
79	FI_ALLOCATED =	1<<1,		/* fsinode->ino allocated */
80	FI_WRITTEN =	1<<2,		/* inode written */
81};
82
83typedef struct {
84	uint32_t	 ino;		/* inode number used on target fs */
85	uint32_t	 nlink;		/* number of links to this entry */
86	enum fi_flags	 flags;		/* flags used by fs specific code */
87	struct stat	 st;		/* stat entry */
88} fsinode;
89
90typedef struct _fsnode {
91	struct _fsnode	*parent;	/* parent (NULL if root) */
92	struct _fsnode	*child;		/* child (if type == S_IFDIR) */
93	struct _fsnode	*next;		/* next */
94	struct _fsnode	*first;		/* first node of current level (".") */
95	uint32_t	 type;		/* type of entry */
96	fsinode		*inode;		/* actual inode data */
97	char		*symlink;	/* symlink target */
98	char		*contents;	/* file to provide contents */
99	const char	*root;		/* root path */
100	char		*path;		/* directory name */
101	char		*name;		/* file name */
102	int		flags;		/* misc flags */
103} fsnode;
104
105#define	FSNODE_F_HASSPEC	0x01	/* fsnode has a spec entry */
106#define	FSNODE_F_OPTIONAL	0x02	/* fsnode is optional */
107
108/*
109 * option_t - contains option name, description, pointer to location to store
110 * result, and range checks for the result. Used to simplify fs specific
111 * option setting
112 */
113typedef enum {
114	OPT_STRARRAY,
115	OPT_STRPTR,
116	OPT_STRBUF,
117	OPT_BOOL,
118	OPT_INT8,
119	OPT_INT16,
120	OPT_INT32,
121	OPT_INT64
122} opttype_t;
123
124typedef struct {
125	char		letter;		/* option letter NUL for none */
126	const char	*name;		/* option name */
127	void		*value;		/* where to stuff the value */
128	opttype_t	type;		/* type of entry */
129	long long	minimum;	/* minimum for value */
130	long long	maximum;	/* maximum for value */
131	const char	*desc;		/* option description */
132} option_t;
133
134/*
135 * fsinfo_t - contains various settings and parameters pertaining to
136 * the image, including current settings, global options, and fs
137 * specific options
138 */
139typedef struct makefs_fsinfo {
140		/* current settings */
141	off_t	size;		/* total size */
142	off_t	inodes;		/* number of inodes */
143	uint32_t curinode;	/* current inode */
144
145		/* image settings */
146	int	fd;		/* file descriptor of image */
147	void	*superblock;	/* superblock */
148	int	onlyspec;	/* only add entries in specfile */
149
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	roundup;	/* round image size up to this value */
157	int	freefilepc;	/* free file % */
158	int	freeblockpc;	/* free block % */
159	int	needswap;	/* non-zero if byte swapping needed */
160	int	sectorsize;	/* sector size */
161	int	sparse;		/* sparse image, don't fill it with zeros */
162
163	void	*fs_specific;	/* File system specific additions. */
164	option_t *fs_options;	/* File system specific options */
165} fsinfo_t;
166
167
168void		apply_specfile(const char *, const char *, fsnode *, int);
169void		dump_fsnodes(fsnode *);
170const char *	inode_type(mode_t);
171fsnode *	read_mtree(const char *, fsnode *);
172int		set_option(const option_t *, const char *, char *, size_t);
173int		set_option_var(const option_t *, const char *, const char *,
174    char *, size_t);
175fsnode *	walk_dir(const char *, const char *, fsnode *, fsnode *);
176void		free_fsnodes(fsnode *);
177option_t *	copy_opts(const option_t *);
178
179void		ffs_prep_opts(fsinfo_t *);
180int		ffs_parse_opts(const char *, fsinfo_t *);
181void		ffs_cleanup_opts(fsinfo_t *);
182void		ffs_makefs(const char *, const char *, fsnode *, fsinfo_t *);
183
184void		cd9660_prep_opts(fsinfo_t *);
185int		cd9660_parse_opts(const char *, fsinfo_t *);
186void		cd9660_cleanup_opts(fsinfo_t *);
187void		cd9660_makefs(const char *, const char *, fsnode *, fsinfo_t *);
188
189
190extern	u_int		debug;
191extern	int		dupsok;
192extern	struct timespec	start_time;
193extern struct stat stampst;
194
195/*
196 * If -x is specified, we want to exclude nodes which do not appear
197 * in the spec file.
198 */
199#define	FSNODE_EXCLUDE_P(opts, fsnode)	\
200	((opts)->onlyspec != 0 && ((fsnode)->flags & FSNODE_F_HASSPEC) == 0)
201
202#define	DEBUG_TIME			0x00000001
203		/* debug bits 1..3 unused at this time */
204#define	DEBUG_WALK_DIR			0x00000010
205#define	DEBUG_WALK_DIR_NODE		0x00000020
206#define	DEBUG_WALK_DIR_LINKCHECK	0x00000040
207#define	DEBUG_DUMP_FSNODES		0x00000080
208#define	DEBUG_DUMP_FSNODES_VERBOSE	0x00000100
209#define	DEBUG_FS_PARSE_OPTS		0x00000200
210#define	DEBUG_FS_MAKEFS			0x00000400
211#define	DEBUG_FS_VALIDATE		0x00000800
212#define	DEBUG_FS_CREATE_IMAGE		0x00001000
213#define	DEBUG_FS_SIZE_DIR		0x00002000
214#define	DEBUG_FS_SIZE_DIR_NODE		0x00004000
215#define	DEBUG_FS_SIZE_DIR_ADD_DIRENT	0x00008000
216#define	DEBUG_FS_POPULATE		0x00010000
217#define	DEBUG_FS_POPULATE_DIRBUF	0x00020000
218#define	DEBUG_FS_POPULATE_NODE		0x00040000
219#define	DEBUG_FS_WRITE_FILE		0x00080000
220#define	DEBUG_FS_WRITE_FILE_BLOCK	0x00100000
221#define	DEBUG_FS_MAKE_DIRBUF		0x00200000
222#define	DEBUG_FS_WRITE_INODE		0x00400000
223#define	DEBUG_BUF_BREAD			0x00800000
224#define	DEBUG_BUF_BWRITE		0x01000000
225#define	DEBUG_BUF_GETBLK		0x02000000
226#define	DEBUG_APPLY_SPECFILE		0x04000000
227#define	DEBUG_APPLY_SPECENTRY		0x08000000
228#define	DEBUG_APPLY_SPECONLY		0x10000000
229
230
231#define	TIMER_START(x)				\
232	if (debug & DEBUG_TIME)			\
233		gettimeofday(&(x), NULL)
234
235#define	TIMER_RESULTS(x,d)				\
236	if (debug & DEBUG_TIME) {			\
237		struct timeval end, td;			\
238		gettimeofday(&end, NULL);		\
239		timersub(&end, &(x), &td);		\
240		printf("%s took %lld.%06ld seconds\n",	\
241		    (d), (long long)td.tv_sec,		\
242		    (long)td.tv_usec);			\
243	}
244
245
246#ifndef	DEFAULT_FSTYPE
247#define	DEFAULT_FSTYPE	"ffs"
248#endif
249
250
251/*
252 *	ffs specific settings
253 *	---------------------
254 */
255
256#define	FFS_EI		/* for opposite endian support in ffs headers */
257
258/*
259 * Write-arounds/compat shims for endian-agnostic support.
260 * These belong in the kernel if/when it's possible to mount
261 * filesystems w/ either byte order.
262 */
263
264/*
265 * File system internal flags, also in fs_flags.
266 * (Pick highest number to avoid conflicts with others)
267 */
268#define        FS_SWAPPED      0x80000000      /* file system is endian swapped */
269#define        FS_INTERNAL     0x80000000      /* mask for internal flags */
270
271#define        FS_ISCLEAN      1
272
273#define        DINODE1_SIZE    (sizeof(struct ufs1_dinode))
274#define        DINODE2_SIZE    (sizeof(struct ufs2_dinode))
275
276#define MAXSYMLINKLEN_UFS1     ((NDADDR + NIADDR) * sizeof(ufs1_daddr_t))
277#define MAXSYMLINKLEN_UFS2     ((NDADDR + NIADDR) * sizeof(ufs2_daddr_t))
278
279#if (BYTE_ORDER == LITTLE_ENDIAN)
280#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
281    (((oldfmt) && !(needswap)) ?       \
282    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
283#else
284#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
285    (((oldfmt) && (needswap)) ?                \
286    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
287#endif
288
289#define        cg_chkmagic_swap(cgp, ns) \
290    (ufs_rw32((cgp)->cg_magic, (ns)) == CG_MAGIC)
291#define        cg_inosused_swap(cgp, ns) \
292    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_iusedoff, (ns))))
293#define        cg_blksfree_swap(cgp, ns) \
294    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_freeoff, (ns))))
295#define        cg_clustersfree_swap(cgp, ns) \
296    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_clusteroff, (ns))))
297#define        cg_clustersum_swap(cgp, ns) \
298    ((int32_t *)((uintptr_t)(cgp) + ufs_rw32((cgp)->cg_clustersumoff, ns)))
299
300struct fs;
301void   ffs_fragacct_swap(struct fs *, int, int32_t [], int, int);
302
303fsinode *link_check(fsinode *);
304
305#endif	/* _MAKEFS_H */
306