makefs.h revision 290589
1193323Sed/*	$NetBSD: makefs.h,v 1.20 2008/12/28 21:51:46 christos Exp $	*/
2193323Sed
3193323Sed/*
4193323Sed * Copyright (c) 2001 Wasabi Systems, Inc.
5193323Sed * All rights reserved.
6193323Sed *
7193323Sed * Written by Luke Mewburn for Wasabi Systems, Inc.
8193323Sed *
9193323Sed * Redistribution and use in source and binary forms, with or without
10193323Sed * modification, are permitted provided that the following conditions
11193323Sed * are met:
12193323Sed * 1. Redistributions of source code must retain the above copyright
13193323Sed *    notice, this list of conditions and the following disclaimer.
14193323Sed * 2. Redistributions in binary form must reproduce the above copyright
15193323Sed *    notice, this list of conditions and the following disclaimer in the
16193323Sed *    documentation and/or other materials provided with the distribution.
17193323Sed * 3. All advertising materials mentioning features or use of this software
18193323Sed *    must display the following acknowledgement:
19193323Sed *      This product includes software developed for the NetBSD Project by
20193323Sed *      Wasabi Systems, Inc.
21193323Sed * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22193323Sed *    or promote products derived from this software without specific prior
23296417Sdim *    written permission.
24193323Sed *
25193323Sed * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27193323Sed * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28193323Sed * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29193323Sed * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30193323Sed * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31243830Sdim * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32243830Sdim * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33193323Sed * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34193323Sed * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35193323Sed * POSSIBILITY OF SUCH DAMAGE.
36193323Sed *
37296417Sdim * $FreeBSD: stable/10/usr.sbin/makefs/makefs.h 290589 2015-11-09 08:59:55Z ngie $
38193323Sed */
39193323Sed
40193323Sed#ifndef	_MAKEFS_H
41193323Sed#define	_MAKEFS_H
42193323Sed
43193323Sed#include <sys/stat.h>
44193323Sed#include <err.h>
45193323Sed
46193323Sed/*
47193323Sed * fsnode -
48296417Sdim *	a component of the tree; contains a filename, a pointer to
49296417Sdim *	fsinode, optional symlink name, and tree pointers
50193323Sed *
51193323Sed * fsinode -
52243830Sdim *	equivalent to an inode, containing target file system inode number,
53193323Sed *	refcount (nlink), and stat buffer
54193323Sed *
55243830Sdim * A tree of fsnodes looks like this:
56193323Sed *
57193323Sed *	name	"."		"bin"		"netbsd"
58193323Sed *	type	S_IFDIR		S_IFDIR		S_IFREG
59193323Sed *	next 	  >		  >		NULL
60193323Sed *	parent	NULL		NULL		NULL
61296417Sdim *	child	NULL		  v
62296417Sdim *
63243830Sdim *	name			"."		"ls"
64193323Sed *	type			S_IFDIR		S_IFREG
65193323Sed *	next			  >		NULL
66193323Sed *	parent			  ^		^ (to "bin")
67193323Sed *	child			NULL		NULL
68243830Sdim *
69193323Sed * Notes:
70193323Sed *	-   first always points to first entry, at current level, which
71193323Sed *	    must be "." when the tree has been built; during build it may
72193323Sed *	    not be if "." hasn't yet been found by readdir(2).
73243830Sdim */
74193323Sed
75193323Sedenum fi_flags {
76193323Sed	FI_SIZED =	1<<0,		/* inode sized */
77193323Sed	FI_ALLOCATED =	1<<1,		/* fsinode->ino allocated */
78243830Sdim	FI_WRITTEN =	1<<2,		/* inode written */
79193323Sed};
80193323Sed
81193323Sedtypedef struct {
82193323Sed	uint32_t	 ino;		/* inode number used on target fs */
83243830Sdim	uint32_t	 nlink;		/* number of links to this entry */
84193323Sed	enum fi_flags	 flags;		/* flags used by fs specific code */
85193323Sed	struct stat	 st;		/* stat entry */
86193323Sed} fsinode;
87193323Sed
88243830Sdimtypedef struct _fsnode {
89193323Sed	struct _fsnode	*parent;	/* parent (NULL if root) */
90193323Sed	struct _fsnode	*child;		/* child (if type == S_IFDIR) */
91193323Sed	struct _fsnode	*next;		/* next */
92193323Sed	struct _fsnode	*first;		/* first node of current level (".") */
93296417Sdim	uint32_t	 type;		/* type of entry */
94296417Sdim	fsinode		*inode;		/* actual inode data */
95296417Sdim	char		*symlink;	/* symlink target */
96296417Sdim	char		*contents;	/* file to provide contents */
97296417Sdim	const char	*root;		/* root path */
98296417Sdim	char		*path;		/* directory name */
99296417Sdim	char		*name;		/* file name */
100296417Sdim	int		flags;		/* misc flags */
101296417Sdim} fsnode;
102296417Sdim
103296417Sdim#define	FSNODE_F_HASSPEC	0x01	/* fsnode has a spec entry */
104296417Sdim#define	FSNODE_F_OPTIONAL	0x02	/* fsnode is optional */
105296417Sdim
106296417Sdim/*
107296417Sdim * fsinfo_t - contains various settings and parameters pertaining to
108296417Sdim * the image, including current settings, global options, and fs
109296417Sdim * specific options
110296417Sdim */
111296417Sdimtypedef struct {
112296417Sdim		/* current settings */
113243830Sdim	off_t	size;		/* total size */
114193323Sed	off_t	inodes;		/* number of inodes */
115193323Sed	uint32_t curinode;	/* current inode */
116193323Sed
117193323Sed		/* image settings */
118193323Sed	int	fd;		/* file descriptor of image */
119243830Sdim	void	*superblock;	/* superblock */
120193323Sed	int	onlyspec;	/* only add entries in specfile */
121193323Sed
122193323Sed
123193323Sed		/* global options */
124193323Sed	off_t	minsize;	/* minimum size image should be */
125243830Sdim	off_t	maxsize;	/* maximum size image can be */
126243830Sdim	off_t	freefiles;	/* free file entries to leave */
127193323Sed	int	freefilepc;	/* free file % */
128280031Sdim	off_t	freeblocks;	/* free blocks to leave */
129193323Sed	int	freeblockpc;	/* free block % */
130193323Sed	int	needswap;	/* non-zero if byte swapping needed */
131193323Sed	int	sectorsize;	/* sector size */
132193323Sed	int	sparse;		/* sparse image, don't fill it with zeros */
133193323Sed	off_t	roundup;	/* round image size up to this value */
134243830Sdim
135193323Sed	void	*fs_specific;	/* File system specific additions. */
136193323Sed} fsinfo_t;
137193323Sed
138280031Sdim
139193323Sed/*
140193323Sed * option_t - contains option name, description, pointer to location to store
141193323Sed * result, and range checks for the result. Used to simplify fs specific
142243830Sdim * option setting
143218893Sdim */
144193323Sedtypedef struct {
145193323Sed	const char	*name;		/* option name */
146193323Sed	int		*value;		/* where to stuff the value */
147193323Sed	int		minimum;	/* minimum for value */
148193323Sed	int		maximum;	/* maximum for value */
149218893Sdim	const char	*desc;		/* option description */
150193323Sed} option_t;
151218893Sdim
152193323Sed
153193323Sedvoid		apply_specfile(const char *, const char *, fsnode *, int);
154243830Sdimvoid		dump_fsnodes(fsnode *);
155243830Sdimconst char *	inode_type(mode_t);
156243830Sdimfsnode *	read_mtree(const char *, fsnode *);
157243830Sdimint		set_option(option_t *, const char *, const char *);
158243830Sdimfsnode *	walk_dir(const char *, const char *, fsnode *, fsnode *);
159243830Sdimvoid		free_fsnodes(fsnode *);
160243830Sdim
161243830Sdimvoid		ffs_prep_opts(fsinfo_t *);
162243830Sdimint		ffs_parse_opts(const char *, fsinfo_t *);
163243830Sdimvoid		ffs_cleanup_opts(fsinfo_t *);
164243830Sdimvoid		ffs_makefs(const char *, const char *, fsnode *, fsinfo_t *);
165243830Sdim
166243830Sdimvoid		cd9660_prep_opts(fsinfo_t *);
167243830Sdimint		cd9660_parse_opts(const char *, fsinfo_t *);
168243830Sdimvoid		cd9660_cleanup_opts(fsinfo_t *);
169243830Sdimvoid		cd9660_makefs(const char *, const char *, fsnode *, fsinfo_t *);
170243830Sdim
171243830Sdim
172243830Sdimextern	u_int		debug;
173243830Sdimextern	int		dupsok;
174243830Sdimextern	struct timespec	start_time;
175243830Sdim
176243830Sdim/*
177193323Sed * If -x is specified, we want to exclude nodes which do not appear
178243830Sdim * in the spec file.
179243830Sdim */
180193323Sed#define	FSNODE_EXCLUDE_P(opts, fsnode)	\
181193323Sed	((opts)->onlyspec != 0 && ((fsnode)->flags & FSNODE_F_HASSPEC) == 0)
182193323Sed
183193323Sed#define	DEBUG_TIME			0x00000001
184243830Sdim		/* debug bits 1..3 unused at this time */
185193323Sed#define	DEBUG_WALK_DIR			0x00000010
186193323Sed#define	DEBUG_WALK_DIR_NODE		0x00000020
187193323Sed#define	DEBUG_WALK_DIR_LINKCHECK	0x00000040
188193323Sed#define	DEBUG_DUMP_FSNODES		0x00000080
189193323Sed#define	DEBUG_DUMP_FSNODES_VERBOSE	0x00000100
190243830Sdim#define	DEBUG_FS_PARSE_OPTS		0x00000200
191193323Sed#define	DEBUG_FS_MAKEFS			0x00000400
192193323Sed#define	DEBUG_FS_VALIDATE		0x00000800
193193323Sed#define	DEBUG_FS_CREATE_IMAGE		0x00001000
194193323Sed#define	DEBUG_FS_SIZE_DIR		0x00002000
195193323Sed#define	DEBUG_FS_SIZE_DIR_NODE		0x00004000
196296417Sdim#define	DEBUG_FS_SIZE_DIR_ADD_DIRENT	0x00008000
197261991Sdim#define	DEBUG_FS_POPULATE		0x00010000
198234353Sdim#define	DEBUG_FS_POPULATE_DIRBUF	0x00020000
199234353Sdim#define	DEBUG_FS_POPULATE_NODE		0x00040000
200234353Sdim#define	DEBUG_FS_WRITE_FILE		0x00080000
201234353Sdim#define	DEBUG_FS_WRITE_FILE_BLOCK	0x00100000
202193323Sed#define	DEBUG_FS_MAKE_DIRBUF		0x00200000
203210299Sed#define	DEBUG_FS_WRITE_INODE		0x00400000
204210299Sed#define	DEBUG_BUF_BREAD			0x00800000
205210299Sed#define	DEBUG_BUF_BWRITE		0x01000000
206210299Sed#define	DEBUG_BUF_GETBLK		0x02000000
207210299Sed#define	DEBUG_APPLY_SPECFILE		0x04000000
208210299Sed#define	DEBUG_APPLY_SPECENTRY		0x08000000
209210299Sed#define	DEBUG_APPLY_SPECONLY		0x10000000
210210299Sed
211193323Sed
212243830Sdim#define	TIMER_START(x)				\
213243830Sdim	if (debug & DEBUG_TIME)			\
214243830Sdim		gettimeofday(&(x), NULL)
215243830Sdim
216243830Sdim#define	TIMER_RESULTS(x,d)				\
217243830Sdim	if (debug & DEBUG_TIME) {			\
218243830Sdim		struct timeval end, td;			\
219243830Sdim		gettimeofday(&end, NULL);		\
220243830Sdim		timersub(&end, &(x), &td);		\
221243830Sdim		printf("%s took %lld.%06ld seconds\n",	\
222243830Sdim		    (d), (long long)td.tv_sec,		\
223243830Sdim		    (long)td.tv_usec);			\
224276479Sdim	}
225276479Sdim
226243830Sdim
227243830Sdim#ifndef	DEFAULT_FSTYPE
228243830Sdim#define	DEFAULT_FSTYPE	"ffs"
229243830Sdim#endif
230243830Sdim
231243830Sdim
232243830Sdim/*
233243830Sdim *	ffs specific settings
234193323Sed *	---------------------
235193323Sed */
236193323Sed
237193323Sed#define	FFS_EI		/* for opposite endian support in ffs headers */
238243830Sdim
239193323Sed/*
240193323Sed * Write-arounds/compat shims for endian-agnostic support.
241193323Sed * These belong in the kernel if/when it's possible to mount
242193323Sed * filesystems w/ either byte order.
243193323Sed */
244193323Sed
245243830Sdim/*
246193323Sed * File system internal flags, also in fs_flags.
247193323Sed * (Pick highest number to avoid conflicts with others)
248193323Sed */
249193323Sed#define        FS_SWAPPED      0x80000000      /* file system is endian swapped */
250193323Sed#define        FS_INTERNAL     0x80000000      /* mask for internal flags */
251193323Sed
252193323Sed#define        FS_ISCLEAN      1
253193323Sed
254193323Sed#define        DINODE1_SIZE    (sizeof(struct ufs1_dinode))
255193323Sed#define        DINODE2_SIZE    (sizeof(struct ufs2_dinode))
256
257#define MAXSYMLINKLEN_UFS1     ((NDADDR + NIADDR) * sizeof(ufs1_daddr_t))
258#define MAXSYMLINKLEN_UFS2     ((NDADDR + NIADDR) * sizeof(ufs2_daddr_t))
259
260#if (BYTE_ORDER == LITTLE_ENDIAN)
261#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
262    (((oldfmt) && !(needswap)) ?       \
263    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
264#else
265#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
266    (((oldfmt) && (needswap)) ?                \
267    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
268#endif
269
270#define        cg_chkmagic_swap(cgp, ns) \
271    (ufs_rw32((cgp)->cg_magic, (ns)) == CG_MAGIC)
272#define        cg_inosused_swap(cgp, ns) \
273    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_iusedoff, (ns))))
274#define        cg_blksfree_swap(cgp, ns) \
275    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_freeoff, (ns))))
276#define        cg_clustersfree_swap(cgp, ns) \
277    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_clusteroff, (ns))))
278#define        cg_clustersum_swap(cgp, ns) \
279    ((int32_t *)((uintptr_t)(cgp) + ufs_rw32((cgp)->cg_clustersumoff, ns)))
280
281struct fs;
282void   ffs_fragacct_swap(struct fs *, int, int32_t [], int, int);
283
284fsinode *link_check(fsinode *);
285
286#endif	/* _MAKEFS_H */
287