makefs.h revision 230795
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: head/usr.sbin/makefs/makefs.h 230795 2012-01-31 00:32:37Z jkim $
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 */
132185222Ssam
133214921Scognet	void	*fs_specific;	/* File system specific additions. */
134185222Ssam} fsinfo_t;
135185222Ssam
136185222Ssam
137185222Ssam/*
138185222Ssam * option_t - contains option name, description, pointer to location to store
139185222Ssam * result, and range checks for the result. Used to simplify fs specific
140185222Ssam * option setting
141185222Ssam */
142185222Ssamtypedef struct {
143185222Ssam	const char	*name;		/* option name */
144185222Ssam	int		*value;		/* where to stuff the value */
145185222Ssam	int		minimum;	/* minimum for value */
146185222Ssam	int		maximum;	/* maximum for value */
147185222Ssam	const char	*desc;		/* option description */
148185222Ssam} option_t;
149185222Ssam
150185222Ssam
151214921Scognetvoid		apply_specfile(const char *, const char *, fsnode *, int);
152230795Sjkimvoid		dump_fsnodes(fsnode *);
153185222Ssamconst char *	inode_type(mode_t);
154223306Smarcelfsnode *	read_mtree(const char *, fsnode *);
155185222Ssamint		set_option(option_t *, const char *, const char *);
156230795Sjkimfsnode *	walk_dir(const char *, const char *, fsnode *, fsnode *);
157214921Scognetvoid		free_fsnodes(fsnode *);
158185222Ssam
159214921Scognetvoid		ffs_prep_opts(fsinfo_t *);
160185222Ssamint		ffs_parse_opts(const char *, fsinfo_t *);
161214921Scognetvoid		ffs_cleanup_opts(fsinfo_t *);
162185222Ssamvoid		ffs_makefs(const char *, const char *, fsnode *, fsinfo_t *);
163185222Ssam
164214921Scognetvoid		cd9660_prep_opts(fsinfo_t *);
165214921Scognetint		cd9660_parse_opts(const char *, fsinfo_t *);
166214921Scognetvoid		cd9660_cleanup_opts(fsinfo_t *);
167214921Scognetvoid		cd9660_makefs(const char *, const char *, fsnode *, fsinfo_t *);
168185222Ssam
169185222Ssam
170185222Ssamextern	u_int		debug;
171185222Ssamextern	struct timespec	start_time;
172185222Ssam
173185222Ssam/*
174185222Ssam * If -x is specified, we want to exclude nodes which do not appear
175185222Ssam * in the spec file.
176185222Ssam */
177185222Ssam#define	FSNODE_EXCLUDE_P(opts, fsnode)	\
178185222Ssam	((opts)->onlyspec != 0 && ((fsnode)->flags & FSNODE_F_HASSPEC) == 0)
179185222Ssam
180185222Ssam#define	DEBUG_TIME			0x00000001
181185222Ssam		/* debug bits 1..3 unused at this time */
182185222Ssam#define	DEBUG_WALK_DIR			0x00000010
183185222Ssam#define	DEBUG_WALK_DIR_NODE		0x00000020
184185222Ssam#define	DEBUG_WALK_DIR_LINKCHECK	0x00000040
185185222Ssam#define	DEBUG_DUMP_FSNODES		0x00000080
186185222Ssam#define	DEBUG_DUMP_FSNODES_VERBOSE	0x00000100
187185222Ssam#define	DEBUG_FS_PARSE_OPTS		0x00000200
188185222Ssam#define	DEBUG_FS_MAKEFS			0x00000400
189185222Ssam#define	DEBUG_FS_VALIDATE		0x00000800
190185222Ssam#define	DEBUG_FS_CREATE_IMAGE		0x00001000
191185222Ssam#define	DEBUG_FS_SIZE_DIR		0x00002000
192185222Ssam#define	DEBUG_FS_SIZE_DIR_NODE		0x00004000
193185222Ssam#define	DEBUG_FS_SIZE_DIR_ADD_DIRENT	0x00008000
194185222Ssam#define	DEBUG_FS_POPULATE		0x00010000
195185222Ssam#define	DEBUG_FS_POPULATE_DIRBUF	0x00020000
196185222Ssam#define	DEBUG_FS_POPULATE_NODE		0x00040000
197185222Ssam#define	DEBUG_FS_WRITE_FILE		0x00080000
198185222Ssam#define	DEBUG_FS_WRITE_FILE_BLOCK	0x00100000
199185222Ssam#define	DEBUG_FS_MAKE_DIRBUF		0x00200000
200185222Ssam#define	DEBUG_FS_WRITE_INODE		0x00400000
201185222Ssam#define	DEBUG_BUF_BREAD			0x00800000
202185222Ssam#define	DEBUG_BUF_BWRITE		0x01000000
203185222Ssam#define	DEBUG_BUF_GETBLK		0x02000000
204185222Ssam#define	DEBUG_APPLY_SPECFILE		0x04000000
205185222Ssam#define	DEBUG_APPLY_SPECENTRY		0x08000000
206214921Scognet#define	DEBUG_APPLY_SPECONLY		0x10000000
207185222Ssam
208185222Ssam
209185222Ssam#define	TIMER_START(x)				\
210185222Ssam	if (debug & DEBUG_TIME)			\
211185222Ssam		gettimeofday(&(x), NULL)
212185222Ssam
213185222Ssam#define	TIMER_RESULTS(x,d)				\
214185222Ssam	if (debug & DEBUG_TIME) {			\
215185222Ssam		struct timeval end, td;			\
216185222Ssam		gettimeofday(&end, NULL);		\
217185222Ssam		timersub(&end, &(x), &td);		\
218214921Scognet		printf("%s took %lld.%06ld seconds\n",	\
219214921Scognet		    (d), (long long)td.tv_sec,		\
220214921Scognet		    (long)td.tv_usec);			\
221185222Ssam	}
222185222Ssam
223185222Ssam
224185222Ssam#ifndef	DEFAULT_FSTYPE
225185222Ssam#define	DEFAULT_FSTYPE	"ffs"
226185222Ssam#endif
227185222Ssam
228185222Ssam
229185222Ssam/*
230185222Ssam *	ffs specific settings
231185222Ssam *	---------------------
232185222Ssam */
233185222Ssam
234185222Ssam#define	FFS_EI		/* for opposite endian support in ffs headers */
235185222Ssam
236186261Ssam/*
237186261Ssam * Write-arounds/compat shims for endian-agnostic support.
238186261Ssam * These belong in the kernel if/when it's possible to mount
239186261Ssam * filesystems w/ either byte order.
240186261Ssam */
241185222Ssam
242186261Ssam/*
243186261Ssam * File system internal flags, also in fs_flags.
244186261Ssam * (Pick highest number to avoid conflicts with others)
245186261Ssam */
246214921Scognet#define        FS_SWAPPED      0x80000000      /* file system is endian swapped */
247214921Scognet#define        FS_INTERNAL     0x80000000      /* mask for internal flags */
248186261Ssam
249214921Scognet#define        FS_ISCLEAN      1
250186261Ssam
251214921Scognet#define        DINODE1_SIZE    (sizeof(struct ufs1_dinode))
252214921Scognet#define        DINODE2_SIZE    (sizeof(struct ufs2_dinode))
253186261Ssam
254214921Scognet#define MAXSYMLINKLEN_UFS1     ((NDADDR + NIADDR) * sizeof(ufs1_daddr_t))
255214921Scognet#define MAXSYMLINKLEN_UFS2     ((NDADDR + NIADDR) * sizeof(ufs2_daddr_t))
256186261Ssam
257186261Ssam#if (BYTE_ORDER == LITTLE_ENDIAN)
258214921Scognet#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
259214921Scognet    (((oldfmt) && !(needswap)) ?       \
260186261Ssam    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
261186261Ssam#else
262214921Scognet#define DIRSIZ_SWAP(oldfmt, dp, needswap)      \
263214921Scognet    (((oldfmt) && (needswap)) ?                \
264186261Ssam    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
265186261Ssam#endif
266186261Ssam
267214921Scognet#define        cg_chkmagic_swap(cgp, ns) \
268186264Ssam    (ufs_rw32((cgp)->cg_magic, (ns)) == CG_MAGIC)
269214921Scognet#define        cg_inosused_swap(cgp, ns) \
270186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_iusedoff, (ns))))
271214921Scognet#define        cg_blksfree_swap(cgp, ns) \
272186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_freeoff, (ns))))
273214921Scognet#define        cg_clustersfree_swap(cgp, ns) \
274186269Ssam    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_clusteroff, (ns))))
275214921Scognet#define        cg_clustersum_swap(cgp, ns) \
276186261Ssam    ((int32_t *)((uintptr_t)(cgp) + ufs_rw32((cgp)->cg_clustersumoff, ns)))
277186261Ssam
278186261Ssamstruct fs;
279214921Scognetvoid   ffs_fragacct_swap(struct fs *, int, int32_t [], int, int);
280186261Ssam
281186261Ssam/*
282186261Ssam * Declarations for compat routines.
283186261Ssam */
284186261Ssamlong long strsuftoll(const char *, const char *, long long, long long);
285186261Ssamlong long strsuftollx(const char *, const char *,
286214921Scognet                       long long, long long, char *, size_t);
287186261Ssam
288186261Ssamstruct passwd;
289186261Ssamint uid_from_user(const char *, uid_t *);
290186261Ssamint pwcache_userdb(int (*)(int), void (*)(void),
291214921Scognet               struct passwd * (*)(const char *), struct passwd * (*)(uid_t));
292186261Ssamstruct group;
293186261Ssamint gid_from_group(const char *, gid_t *);
294186261Ssamint pwcache_groupdb(int (*)(int), void (*)(void),
295214921Scognet               struct group * (*)(const char *), struct group * (*)(gid_t));
296186261Ssam
297186261Ssamint setup_getid(const char *dir);
298186261Ssam
299185222Ssam#endif	/* _MAKEFS_H */
300