makefs.h revision 186269
1169718Skan/*	$NetBSD: makefs.h,v 1.14 2004/06/20 22:20:18 jmc Exp $	*/
2169718Skan
3169718Skan/*
4169718Skan * Copyright (c) 2001 Wasabi Systems, Inc.
5169718Skan * All rights reserved.
6169718Skan *
7169718Skan * Written by Luke Mewburn for Wasabi Systems, Inc.
8169718Skan *
9169718Skan * Redistribution and use in source and binary forms, with or without
10169718Skan * modification, are permitted provided that the following conditions
11169718Skan * are met:
12169718Skan * 1. Redistributions of source code must retain the above copyright
13169718Skan *    notice, this list of conditions and the following disclaimer.
14169718Skan * 2. Redistributions in binary form must reproduce the above copyright
15169718Skan *    notice, this list of conditions and the following disclaimer in the
16169718Skan *    documentation and/or other materials provided with the distribution.
17169718Skan * 3. All advertising materials mentioning features or use of this software
18169718Skan *    must display the following acknowledgement:
19169718Skan *      This product includes software developed for the NetBSD Project by
20169718Skan *      Wasabi Systems, Inc.
21169718Skan * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22169718Skan *    or promote products derived from this software without specific prior
23169718Skan *    written permission.
24169718Skan *
25169718Skan * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26169718Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27169718Skan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28169718Skan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29169718Skan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30169718Skan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31169718Skan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32169718Skan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33169718Skan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34169718Skan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35169718Skan * POSSIBILITY OF SUCH DAMAGE.
36169718Skan */
37169718Skan
38169718Skan#ifndef	_MAKEFS_H
39169718Skan#define	_MAKEFS_H
40169718Skan
41169718Skan#include <sys/stat.h>
42169718Skan#include <err.h>
43169718Skan
44169718Skan/*
45169718Skan * fsnode -
46169718Skan *	a component of the tree; contains a filename, a pointer to
47169718Skan *	fsinode, optional symlink name, and tree pointers
48 *
49 * fsinode -
50 *	equivalent to an inode, containing target file system inode number,
51 *	refcount (nlink), and stat buffer
52 *
53 * A tree of fsnodes looks like this:
54 *
55 *	name	"."		"bin"		"netbsd"
56 *	type	S_IFDIR		S_IFDIR		S_IFREG
57 *	next 	  >		  >		NULL
58 *	parent	NULL		NULL		NULL
59 *	child	NULL		  v
60 *
61 *	name			"."		"ls"
62 *	type			S_IFDIR		S_IFREG
63 *	next			  >		NULL
64 *	parent			  ^		^ (to "bin")
65 *	child			NULL		NULL
66 *
67 * Notes:
68 *	-   first always points to first entry, at current level, which
69 *	    must be "." when the tree has been built; during build it may
70 *	    not be if "." hasn't yet been found by readdir(2).
71 */
72
73enum fi_flags {
74	FI_SIZED =	1<<0,		/* inode sized */
75	FI_ALLOCATED =	1<<1,		/* fsinode->ino allocated */
76	FI_WRITTEN =	1<<2,		/* inode written */
77};
78
79typedef struct {
80	uint32_t	 ino;		/* inode number used on target fs */
81	uint32_t	 nlink;		/* number of links to this entry */
82	enum fi_flags	 flags;		/* flags used by fs specific code */
83	struct stat	 st;		/* stat entry */
84} fsinode;
85
86typedef struct _fsnode {
87	struct _fsnode	*parent;	/* parent (NULL if root) */
88	struct _fsnode	*child;		/* child (if type == S_IFDIR) */
89	struct _fsnode	*next;		/* next */
90	struct _fsnode	*first;		/* first node of current level (".") */
91	uint32_t	 type;		/* type of entry */
92	fsinode		*inode;		/* actual inode data */
93	char		*symlink;	/* symlink target */
94	char		*name;		/* file name */
95	int		flags;		/* misc flags */
96} fsnode;
97
98#define	FSNODE_F_HASSPEC	0x01	/* fsnode has a spec entry */
99
100/*
101 * fsinfo_t - contains various settings and parameters pertaining to
102 * the image, including current settings, global options, and fs
103 * specific options
104 */
105typedef struct {
106		/* current settings */
107	off_t	size;		/* total size */
108	off_t	inodes;		/* number of inodes */
109	uint32_t curinode;	/* current inode */
110
111		/* image settings */
112	int	fd;		/* file descriptor of image */
113	void	*superblock;	/* superblock */
114	int	onlyspec;	/* only add entries in specfile */
115
116
117		/* global options */
118	off_t	minsize;	/* minimum size image should be */
119	off_t	maxsize;	/* maximum size image can be */
120	off_t	freefiles;	/* free file entries to leave */
121	int	freefilepc;	/* free file % */
122	off_t	freeblocks;	/* free blocks to leave */
123	int	freeblockpc;	/* free block % */
124	int	needswap;	/* non-zero if byte swapping needed */
125	int	sectorsize;	/* sector size */
126
127		/* ffs specific options */
128	int	bsize;		/* block size */
129	int	fsize;		/* fragment size */
130	int	cpg;		/* cylinders per group */
131	int	cpgflg;		/* cpg was specified by user */
132	int	density;	/* bytes per inode */
133	int	ntracks;	/* number of tracks */
134	int	nsectors;	/* number of sectors */
135	int	rpm;		/* rpm */
136	int	minfree;	/* free space threshold */
137	int	optimization;	/* optimization (space or time) */
138	int	maxcontig;	/* max contiguous blocks to allocate */
139	int	rotdelay;	/* rotational delay between blocks */
140	int	maxbpg;		/* maximum blocks per file in a cyl group */
141	int	nrpos;		/* # of distinguished rotational positions */
142	int	avgfilesize;	/* expected average file size */
143	int	avgfpdir;	/* expected # of files per directory */
144	int	version;	/* filesystem version (1 = FFS, 2 = UFS2) */
145	int	maxbsize;	/* maximum extent size */
146	int	maxblkspercg;	/* max # of blocks per cylinder group */
147			/* XXX: support `old' file systems ? */
148} fsinfo_t;
149
150
151/*
152 * option_t - contains option name, description, pointer to location to store
153 * result, and range checks for the result. Used to simplify fs specific
154 * option setting
155 */
156typedef struct {
157	const char	*name;		/* option name */
158	int		*value;		/* where to stuff the value */
159	int		minimum;	/* minimum for value */
160	int		maximum;	/* maximum for value */
161	const char	*desc;		/* option description */
162} option_t;
163
164
165void		apply_specfile(const char *, const char *, fsnode *);
166void		dump_fsnodes(const char *, fsnode *);
167const char *	inode_type(mode_t);
168int		set_option(option_t *, const char *, const char *);
169fsnode *	walk_dir(const char *, fsnode *);
170
171int		ffs_parse_opts(const char *, fsinfo_t *);
172void		ffs_makefs(const char *, const char *, fsnode *, fsinfo_t *);
173
174
175
176extern	u_int		debug;
177extern	struct timespec	start_time;
178
179/*
180 * If -x is specified, we want to exclude nodes which do not appear
181 * in the spec file.
182 */
183#define	FSNODE_EXCLUDE_P(opts, fsnode)	\
184	((opts)->onlyspec != 0 && ((fsnode)->flags & FSNODE_F_HASSPEC) == 0)
185
186#define	DEBUG_TIME			0x00000001
187		/* debug bits 1..3 unused at this time */
188#define	DEBUG_WALK_DIR			0x00000010
189#define	DEBUG_WALK_DIR_NODE		0x00000020
190#define	DEBUG_WALK_DIR_LINKCHECK	0x00000040
191#define	DEBUG_DUMP_FSNODES		0x00000080
192#define	DEBUG_DUMP_FSNODES_VERBOSE	0x00000100
193#define	DEBUG_FS_PARSE_OPTS		0x00000200
194#define	DEBUG_FS_MAKEFS			0x00000400
195#define	DEBUG_FS_VALIDATE		0x00000800
196#define	DEBUG_FS_CREATE_IMAGE		0x00001000
197#define	DEBUG_FS_SIZE_DIR		0x00002000
198#define	DEBUG_FS_SIZE_DIR_NODE		0x00004000
199#define	DEBUG_FS_SIZE_DIR_ADD_DIRENT	0x00008000
200#define	DEBUG_FS_POPULATE		0x00010000
201#define	DEBUG_FS_POPULATE_DIRBUF	0x00020000
202#define	DEBUG_FS_POPULATE_NODE		0x00040000
203#define	DEBUG_FS_WRITE_FILE		0x00080000
204#define	DEBUG_FS_WRITE_FILE_BLOCK	0x00100000
205#define	DEBUG_FS_MAKE_DIRBUF		0x00200000
206#define	DEBUG_FS_WRITE_INODE		0x00400000
207#define	DEBUG_BUF_BREAD			0x00800000
208#define	DEBUG_BUF_BWRITE		0x01000000
209#define	DEBUG_BUF_GETBLK		0x02000000
210#define	DEBUG_APPLY_SPECFILE		0x04000000
211#define	DEBUG_APPLY_SPECENTRY		0x08000000
212
213
214#define	TIMER_START(x)				\
215	if (debug & DEBUG_TIME)			\
216		gettimeofday(&(x), NULL)
217
218#define	TIMER_RESULTS(x,d)				\
219	if (debug & DEBUG_TIME) {			\
220		struct timeval end, td;			\
221		gettimeofday(&end, NULL);		\
222		timersub(&end, &(x), &td);		\
223		printf("%s took %ld.%06ld seconds\n",	\
224		    (d), (long) td.tv_sec, (long) td.tv_usec);	\
225	}
226
227
228#ifndef	DEFAULT_FSTYPE
229#define	DEFAULT_FSTYPE	"ffs"
230#endif
231
232
233/*
234 *	ffs specific settings
235 *	---------------------
236 */
237
238#define	FFS_EI		/* for opposite endian support in ffs headers */
239
240/*
241 * Write-arounds/compat shims for endian-agnostic support.
242 * These belong in the kernel if/when it's possible to mount
243 * filesystems w/ either byte order.
244 */
245
246/*
247 * File system internal flags, also in fs_flags.
248 * (Pick highest number to avoid conflicts with others)
249 */
250#define	FS_SWAPPED	0x80000000	/* file system is endian swapped */
251#define	FS_INTERNAL	0x80000000	/* mask for internal flags */
252
253#define	FS_ISCLEAN	1
254
255#define	DINODE1_SIZE	(sizeof(struct ufs1_dinode))
256#define	DINODE2_SIZE	(sizeof(struct ufs2_dinode))
257
258#define MAXSYMLINKLEN_UFS1	((NDADDR + NIADDR) * sizeof(ufs1_daddr_t))
259#define MAXSYMLINKLEN_UFS2	((NDADDR + NIADDR) * sizeof(ufs2_daddr_t))
260
261#if (BYTE_ORDER == LITTLE_ENDIAN)
262#define DIRSIZ_SWAP(oldfmt, dp, needswap)	\
263    (((oldfmt) && !(needswap)) ?	\
264    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
265#else
266#define DIRSIZ_SWAP(oldfmt, dp, needswap)	\
267    (((oldfmt) && (needswap)) ?		\
268    DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
269#endif
270
271#define	cg_chkmagic_swap(cgp, ns) \
272    (ufs_rw32((cgp)->cg_magic, (ns)) == CG_MAGIC)
273#define	cg_inosused_swap(cgp, ns) \
274    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_iusedoff, (ns))))
275#define	cg_blksfree_swap(cgp, ns) \
276    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_freeoff, (ns))))
277#define	cg_clustersfree_swap(cgp, ns) \
278    ((u_int8_t *)((u_int8_t *)(cgp) + ufs_rw32((cgp)->cg_clusteroff, (ns))))
279#define	cg_clustersum_swap(cgp, ns) \
280    ((int32_t *)((uintptr_t)(cgp) + ufs_rw32((cgp)->cg_clustersumoff, ns)))
281
282struct fs;
283void	ffs_fragacct_swap(struct fs *, int, int32_t [], int, int);
284
285/*
286 * Declarations for compat routines.
287 */
288long long strsuftoll(const char *, const char *, long long, long long);
289long long strsuftollx(const char *, const char *,
290			long long, long long, char *, size_t);
291
292struct passwd;
293int uid_from_user(const char *, uid_t *);
294int pwcache_userdb(int (*)(int), void (*)(void),
295		struct passwd * (*)(const char *), struct passwd * (*)(uid_t));
296struct group;
297int gid_from_group(const char *, gid_t *);
298int pwcache_groupdb(int (*)(int), void (*)(void),
299		struct group * (*)(const char *), struct group * (*)(gid_t));
300
301int setup_getid(const char *dir);
302
303#endif	/* _MAKEFS_H */
304