1/*
2 * Copyright (C) 2004 Free Software Foundation, Inc.
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Marshall
7 * Kirk McKusick and Network Associates Laboratories, the Security
8 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
9 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
10 * research program
11 *
12 * Copyright (c) 1982, 1989, 1993
13 *	The Regents of the University of California.  All rights reserved.
14 * (c) UNIX System Laboratories, Inc.
15 * All or some portions of this file are derived from material licensed
16 * to the University of California by American Telephone and Telegraph
17 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
18 * the permission of UNIX System Laboratories, Inc.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 *    notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 *    notice, this list of conditions and the following disclaimer in the
27 *    documentation and/or other materials provided with the distribution.
28 * 3. The names of the authors may not be used to endorse or promote
29 *    products derived from this software without specific prior written
30 *    permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 *	@(#)dinode.h	8.3 (Berkeley) 1/21/94
45 * $FreeBSD: src/sys/ufs/ufs/dinode.h,v 1.11 2002/07/16 22:36:00 mckusick Exp $
46 */
47
48#ifndef _GRUB_UFS2_H_
49#define _GRUB_UFS2_H_
50
51typedef signed char            grub_int8_t;
52typedef signed short           grub_int16_t;
53typedef signed int             grub_int32_t;
54typedef signed long long int   grub_int64_t;
55typedef unsigned char          grub_uint8_t;
56typedef unsigned short         grub_uint16_t;
57typedef unsigned int           grub_uint32_t;
58typedef unsigned long long int grub_uint64_t;
59
60typedef grub_uint8_t                grub_u_char;
61typedef grub_uint32_t               grub_u_int;
62
63typedef grub_uint8_t                grub_u_int8_t;
64typedef grub_uint16_t               grub_u_int16_t;
65typedef grub_uint32_t               grub_u_int32_t;
66typedef grub_uint64_t               grub_u_int64_t;
67
68#define i_size di_size
69
70
71#define DEV_BSIZE 512
72
73/*
74 * The root inode is the root of the filesystem.  Inode 0 can't be used for
75 * normal purposes and historically bad blocks were linked to inode 1, thus
76 * the root inode is 2.  (Inode 1 is no longer used for this purpose, however
77 * numerous dump tapes make this assumption, so we are stuck with it).
78 */
79#define	ROOTINO	((grub_ino_t)2)
80
81/*
82 * The size of physical and logical block numbers and time fields in UFS.
83 */
84typedef grub_int32_t ufs1_daddr_t;
85typedef	grub_int64_t	ufs2_daddr_t;
86typedef grub_int64_t ufs_lbn_t;
87typedef grub_int64_t ufs_time_t;
88
89/* inode number */
90typedef grub_uint32_t      grub_ino_t;
91
92/* File permissions. */
93#define	IEXEC		0000100		/* Executable. */
94#define	IWRITE		0000200		/* Writeable. */
95#define	IREAD		0000400		/* Readable. */
96#define	ISVTX		0001000		/* Sticky bit. */
97#define	ISGID		0002000		/* Set-gid. */
98#define	ISUID		0004000		/* Set-uid. */
99
100/* File types. */
101#define	IFMT		0170000		/* Mask of file type. */
102#define	IFIFO		0010000		/* Named pipe (fifo). */
103#define	IFCHR		0020000		/* Character device. */
104#define	IFDIR		0040000		/* Directory file. */
105#define	IFBLK		0060000		/* Block device. */
106#define	IFREG		0100000		/* Regular file. */
107#define	IFLNK		0120000		/* Symbolic link. */
108#define	IFSOCK		0140000		/* UNIX domain socket. */
109#define	IFWHT		0160000		/* Whiteout. */
110
111/*
112 * A dinode contains all the meta-data associated with a UFS2 file.
113 * This structure defines the on-disk format of a dinode. Since
114 * this structure describes an on-disk structure, all its fields
115 * are defined by types with precise widths.
116 */
117
118#define	NXADDR	2			/* External addresses in inode. */
119#define	NDADDR	12			/* Direct addresses in inode. */
120#define	NIADDR	3			/* Indirect addresses in inode. */
121
122struct ufs1_dinode {
123	grub_u_int16_t       di_mode;        /*   0: IFMT, permissions; see below. */
124	grub_int16_t         di_nlink;       /*   2: File link count. */
125	union {
126		grub_u_int16_t oldids[2];    /*   4: Ffs: old user and group ids. */
127	} di_u;
128	grub_u_int64_t       di_size;        /*   8: File byte count. */
129	grub_int32_t         di_atime;       /*  16: Last access time. */
130	grub_int32_t         di_atimensec;   /*  20: Last access time. */
131	grub_int32_t         di_mtime;       /*  24: Last modified time. */
132	grub_int32_t         di_mtimensec;   /*  28: Last modified time. */
133	grub_int32_t         di_ctime;       /*  32: Last inode change time. */
134	grub_int32_t         di_ctimensec;   /*  36: Last inode change time. */
135	ufs1_daddr_t    di_db[NDADDR];  /*  40: Direct disk blocks. */
136	ufs1_daddr_t    di_ib[NIADDR];  /*  88: Indirect disk blocks. */
137	grub_u_int32_t       di_flags;       /* 100: Status flags (chflags). */
138	grub_int32_t         di_blocks;      /* 104: Blocks actually held. */
139	grub_int32_t         di_gen;         /* 108: Generation number. */
140	grub_u_int32_t       di_uid;         /* 112: File owner. */
141	grub_u_int32_t       di_gid;         /* 116: File group. */
142	grub_int32_t         di_spare[2];    /* 120: Reserved; currently unused */
143};
144
145struct ufs2_dinode {
146	grub_u_int16_t	di_mode;	/*   0: IFMT, permissions; see below. */
147	grub_int16_t		di_nlink;	/*   2: File link count. */
148	grub_u_int32_t	di_uid;		/*   4: File owner. */
149	grub_u_int32_t	di_gid;		/*   8: File group. */
150	grub_u_int32_t	di_blksize;	/*  12: Inode blocksize. */
151	grub_u_int64_t	di_size;	/*  16: File byte count. */
152	grub_u_int64_t	di_blocks;	/*  24: Bytes actually held. */
153	ufs_time_t	di_atime;	/*  32: Last access time. */
154	ufs_time_t	di_mtime;	/*  40: Last modified time. */
155	ufs_time_t	di_ctime;	/*  48: Last inode change time. */
156	ufs_time_t	di_birthtime;	/*  56: Inode creation time. */
157	grub_int32_t		di_mtimensec;	/*  64: Last modified time. */
158	grub_int32_t		di_atimensec;	/*  68: Last access time. */
159	grub_int32_t		di_ctimensec;	/*  72: Last inode change time. */
160	grub_int32_t		di_birthnsec;	/*  76: Inode creation time. */
161	grub_int32_t		di_gen;		/*  80: Generation number. */
162	grub_u_int32_t	di_kernflags;	/*  84: Kernel flags. */
163	grub_u_int32_t	di_flags;	/*  88: Status flags (chflags). */
164	grub_int32_t		di_extsize;	/*  92: External attributes block. */
165	ufs2_daddr_t	di_extb[NXADDR];/*  96: External attributes block. */
166	ufs2_daddr_t	di_db[NDADDR];	/* 112: Direct disk blocks. */
167	ufs2_daddr_t	di_ib[NIADDR];	/* 208: Indirect disk blocks. */
168	grub_int64_t		di_spare[3];	/* 232: Reserved; currently unused */
169};
170
171#define	MAXNAMLEN	255
172
173struct	direct {
174	grub_u_int32_t d_ino;		/* inode number of entry */
175	grub_u_int16_t d_reclen;		/* length of this record */
176	grub_u_int8_t  d_type; 		/* file type, see below */
177	grub_u_int8_t  d_namlen;		/* length of string in d_name */
178	char	  d_name[MAXNAMLEN + 1];/* name with length <= MAXNAMLEN */
179};
180
181/*
182 * File types
183 */
184#define DT_UNKNOWN       0
185#define DT_FIFO          1
186#define DT_CHR           2
187#define DT_DIR           4
188#define DT_BLK           6
189#define DT_REG           8
190#define DT_LNK          10
191#define DT_SOCK         12
192#define DT_WHT          14
193
194/*
195 * Superblock offsets
196 */
197#define SBLOCK_FLOPPY        0
198#define SBLOCK_UFS1       8192
199#define SBLOCK_UFS2      65536
200#define SBLOCK_PIGGY    262144
201#define SBLOCKSIZE        8192
202#define SBLOCKSEARCH \
203	{ SBLOCK_UFS2, SBLOCK_UFS1, SBLOCK_FLOPPY, SBLOCK_PIGGY, -1 }
204
205#define MAXMNTLEN	512
206
207#define	NOCSPTRS	((128 / sizeof(void *)) - 4)
208
209/*
210 * The maximum number of snapshot nodes that can be associated
211 * with each filesystem. This limit affects only the number of
212 * snapshot files that can be recorded within the superblock so
213 * that they can be found when the filesystem is mounted. However,
214 * maintaining too many will slow the filesystem performance, so
215 * having this limit is a good idea.
216 */
217#define FSMAXSNAP 20
218
219/*
220 * Per cylinder group information; summarized in blocks allocated
221 * from first cylinder group data blocks.  These blocks have to be
222 * read in from fs_csaddr (size fs_cssize) in addition to the
223 * super block.
224 */
225struct csum {
226	grub_int32_t	cs_ndir;		/* number of directories */
227	grub_int32_t	cs_nbfree;		/* number of free blocks */
228	grub_int32_t	cs_nifree;		/* number of free inodes */
229	grub_int32_t	cs_nffree;		/* number of free frags */
230};
231
232struct csum_total {
233	grub_int64_t	cs_ndir;		/* number of directories */
234	grub_int64_t	cs_nbfree;		/* number of free blocks */
235	grub_int64_t	cs_nifree;		/* number of free inodes */
236	grub_int64_t	cs_nffree;		/* number of free frags */
237	grub_int64_t	cs_numclusters;		/* number of free clusters */
238	grub_int64_t	cs_spare[3];		/* future expansion */
239};
240
241/*
242 * Super block for an FFS filesystem.
243 */
244struct fs {
245	grub_int32_t	 fs_firstfield;		/* historic filesystem linked list, */
246	grub_int32_t	 fs_unused_1;		/*     used for incore super blocks */
247	grub_int32_t	 fs_sblkno;		/* offset of super-block in filesys */
248	grub_int32_t	 fs_cblkno;		/* offset of cyl-block in filesys */
249	grub_int32_t	 fs_iblkno;		/* offset of inode-blocks in filesys */
250	grub_int32_t	 fs_dblkno;		/* offset of first data after cg */
251	grub_int32_t	 fs_old_cgoffset;	/* cylinder group offset in cylinder */
252	grub_int32_t	 fs_old_cgmask;		/* used to calc mod fs_ntrak */
253	grub_int32_t  fs_old_time;		/* last time written */
254	grub_int32_t	 fs_old_size;		/* number of blocks in fs */
255	grub_int32_t	 fs_old_dsize;		/* number of data blocks in fs */
256	grub_int32_t	 fs_ncg;		/* number of cylinder groups */
257	grub_int32_t	 fs_bsize;		/* size of basic blocks in fs */
258	grub_int32_t	 fs_fsize;		/* size of frag blocks in fs */
259	grub_int32_t	 fs_frag;		/* number of frags in a block in fs */
260/* these are configuration parameters */
261	grub_int32_t	 fs_minfree;		/* minimum percentage of free blocks */
262	grub_int32_t	 fs_old_rotdelay;	/* num of ms for optimal next block */
263	grub_int32_t	 fs_old_rps;		/* disk revolutions per second */
264/* these fields can be computed from the others */
265	grub_int32_t	 fs_bmask;		/* ``blkoff'' calc of blk offsets */
266	grub_int32_t	 fs_fmask;		/* ``fragoff'' calc of frag offsets */
267	grub_int32_t	 fs_bshift;		/* ``lblkno'' calc of logical blkno */
268	grub_int32_t	 fs_fshift;		/* ``numfrags'' calc number of frags */
269/* these are configuration parameters */
270	grub_int32_t	 fs_maxcontig;		/* max number of contiguous blks */
271	grub_int32_t	 fs_maxbpg;		/* max number of blks per cyl group */
272/* these fields can be computed from the others */
273	grub_int32_t	 fs_fragshift;		/* block to frag shift */
274	grub_int32_t	 fs_fsbtodb;		/* fsbtodb and dbtofsb shift constant */
275	grub_int32_t	 fs_sbsize;		/* actual size of super block */
276	grub_int32_t	 fs_spare1[2];		/* old fs_csmask */
277					/* old fs_csshift */
278	grub_int32_t	 fs_nindir;		/* value of NINDIR */
279	grub_int32_t	 fs_inopb;		/* value of INOPB */
280	grub_int32_t	 fs_old_nspf;		/* value of NSPF */
281/* yet another configuration parameter */
282	grub_int32_t	 fs_optim;		/* optimization preference, see below */
283	grub_int32_t	 fs_old_npsect;		/* # sectors/track including spares */
284	grub_int32_t	 fs_old_interleave;	/* hardware sector interleave */
285	grub_int32_t	 fs_old_trackskew;	/* sector 0 skew, per track */
286	grub_int32_t	 fs_id[2];		/* unique filesystem id */
287/* sizes determined by number of cylinder groups and their sizes */
288	grub_int32_t	 fs_old_csaddr;		/* blk addr of cyl grp summary area */
289	grub_int32_t	 fs_cssize;		/* size of cyl grp summary area */
290	grub_int32_t	 fs_cgsize;		/* cylinder group size */
291	grub_int32_t	 fs_spare2;		/* old fs_ntrak */
292	grub_int32_t	 fs_old_nsect;		/* sectors per track */
293	grub_int32_t  fs_old_spc;		/* sectors per cylinder */
294	grub_int32_t	 fs_old_ncyl;		/* cylinders in filesystem */
295	grub_int32_t	 fs_old_cpg;		/* cylinders per group */
296	grub_int32_t	 fs_ipg;		/* inodes per group */
297	grub_int32_t	 fs_fpg;		/* blocks per group * fs_frag */
298/* this data must be re-computed after crashes */
299	struct	csum fs_old_cstotal;	/* cylinder summary information */
300/* these fields are cleared at mount time */
301	grub_int8_t   fs_fmod;		/* super block modified flag */
302	grub_int8_t   fs_clean;		/* filesystem is clean flag */
303	grub_int8_t 	 fs_ronly;		/* mounted read-only flag */
304	grub_int8_t   fs_old_flags;		/* old FS_ flags */
305	grub_u_char	 fs_fsmnt[MAXMNTLEN];	/* name mounted on */
306/* these fields retain the current block allocation info */
307	grub_int32_t	 fs_cgrotor;		/* last cg searched */
308	void 	*fs_ocsp[NOCSPTRS];	/* padding; was list of fs_cs buffers */
309	grub_u_int8_t *fs_contigdirs;	/* # of contiguously allocated dirs */
310	struct	csum *fs_csp;		/* cg summary info buffer for fs_cs */
311	grub_int32_t	*fs_maxcluster;		/* max cluster in each cyl group */
312	grub_u_int	*fs_active;		/* used by snapshots to track fs */
313	grub_int32_t	 fs_old_cpc;		/* cyl per cycle in postbl */
314	grub_int32_t	 fs_maxbsize;		/* maximum blocking factor permitted */
315	grub_int64_t	 fs_sparecon64[17];	/* old rotation block list head */
316	grub_int64_t	 fs_sblockloc;		/* byte offset of standard superblock */
317	struct	csum_total fs_cstotal;	/* cylinder summary information */
318	ufs_time_t fs_time;		/* last time written */
319	grub_int64_t	 fs_size;		/* number of blocks in fs */
320	grub_int64_t	 fs_dsize;		/* number of data blocks in fs */
321	ufs2_daddr_t fs_csaddr;		/* blk addr of cyl grp summary area */
322	grub_int64_t	 fs_pendingblocks;	/* blocks in process of being freed */
323	grub_int32_t	 fs_pendinginodes;	/* inodes in process of being freed */
324	grub_int32_t	 fs_snapinum[FSMAXSNAP];/* list of snapshot inode numbers */
325	grub_int32_t	 fs_avgfilesize;	/* expected average file size */
326	grub_int32_t	 fs_avgfpdir;		/* expected # of files per directory */
327	grub_int32_t	 fs_save_cgsize;	/* save real cg size to use fs_bsize */
328	grub_int32_t	 fs_sparecon32[26];	/* reserved for future constants */
329	grub_int32_t  fs_flags;		/* see FS_ flags below */
330	grub_int32_t	 fs_contigsumsize;	/* size of cluster summary array */
331	grub_int32_t	 fs_maxsymlinklen;	/* max length of an internal symlink */
332	grub_int32_t	 fs_old_inodefmt;	/* format of on-disk inodes */
333	grub_u_int64_t fs_maxfilesize;	/* maximum representable file size */
334	grub_int64_t	 fs_qbmask;		/* ~fs_bmask for use with 64-bit size */
335	grub_int64_t	 fs_qfmask;		/* ~fs_fmask for use with 64-bit size */
336	grub_int32_t	 fs_state;		/* validate fs_clean field */
337	grub_int32_t	 fs_old_postblformat;	/* format of positional layout tables */
338	grub_int32_t	 fs_old_nrpos;		/* number of rotational positions */
339	grub_int32_t	 fs_spare5[2];		/* old fs_postbloff */
340					/* old fs_rotbloff */
341	grub_int32_t	 fs_magic;		/* magic number */
342};
343
344/*
345 * Filesystem identification
346 */
347#define FS_UFS1_MAGIC   0x011954        /* UFS1 fast filesystem magic number */
348#define	FS_UFS2_MAGIC	0x19540119	/* UFS2 fast filesystem magic number */
349
350/*
351 * Turn filesystem block numbers into disk block addresses.
352 * This maps filesystem blocks to device size blocks.
353 */
354#define fsbtodb(fs, b)	((b) << (fs)->fs_fsbtodb)
355#define	dbtofsb(fs, b)	((b) >> (fs)->fs_fsbtodb)
356
357/*
358 * Cylinder group macros to locate things in cylinder groups.
359 * They calc filesystem addresses of cylinder group data structures.
360 */
361#define	cgbase(fs, c)	((ufs2_daddr_t)((fs)->fs_fpg * (c)))
362#define	cgimin(fs, c)	(cgstart(fs, c) + (fs)->fs_iblkno)	/* inode blk */
363#define cgstart(fs, c)							\
364       ((fs)->fs_magic == FS_UFS2_MAGIC ? cgbase(fs, c) :		\
365       (cgbase(fs, c) + (fs)->fs_old_cgoffset * ((c) & ~((fs)->fs_old_cgmask))))
366
367/*
368 * Macros for handling inode numbers:
369 *     inode number to filesystem block offset.
370 *     inode number to cylinder group number.
371 *     inode number to filesystem block address.
372 */
373#define	ino_to_cg(fs, x)	((x) / (fs)->fs_ipg)
374#define	ino_to_fsba(fs, x)						\
375	((ufs2_daddr_t)(cgimin(fs, ino_to_cg(fs, x)) +			\
376	    (blkstofrags((fs), (((x) % (fs)->fs_ipg) / INOPB(fs))))))
377#define	ino_to_fsbo(fs, x)	((x) % INOPB(fs))
378
379/*
380 * The following macros optimize certain frequently calculated
381 * quantities by using shifts and masks in place of divisions
382 * modulos and multiplications.
383 */
384#define blkoff(fs, loc)		/* calculates (loc % fs->fs_bsize) */ \
385	((loc) & (fs)->fs_qbmask)
386
387/* Use this only when `blk' is known to be small, e.g., < NDADDR. */
388#define smalllblktosize(fs, blk)    /* calculates (blk * fs->fs_bsize) */ \
389	((blk) << (fs)->fs_bshift)
390
391
392#define lblkno(fs, loc)		/* calculates (loc / fs->fs_bsize) */ \
393	((loc) >> (fs)->fs_bshift)
394
395#define fragroundup(fs, size)	/* calculates roundup(size, fs->fs_fsize) */ \
396	(((size) + (fs)->fs_qfmask) & (fs)->fs_fmask)
397
398#define fragstoblks(fs, frags)	/* calculates (frags / fs->fs_frag) */ \
399	((frags) >> (fs)->fs_fragshift)
400#define blkstofrags(fs, blks)	/* calculates (blks * fs->fs_frag) */ \
401	((blks) << (fs)->fs_fragshift)
402#define fragnum(fs, fsb)	/* calculates (fsb % fs->fs_frag) */ \
403	((fsb) & ((fs)->fs_frag - 1))
404#define blknum(fs, fsb)		/* calculates rounddown(fsb, fs->fs_frag) */ \
405	((fsb) &~ ((fs)->fs_frag - 1))
406
407/*
408 * Determining the size of a file block in the filesystem.
409 */
410#define blksize(fs, ip, lbn) \
411	(((lbn) >= NDADDR || (ip)->i_size >= smalllblktosize(fs, (lbn) + 1)) \
412	    ? (fs)->fs_bsize \
413	    : (fragroundup(fs, blkoff(fs, (ip)->i_size))))
414#define sblksize(fs, size, lbn) \
415	(((lbn) >= NDADDR || (size) >= ((lbn) + 1) << (fs)->fs_bshift) \
416	  ? (fs)->fs_bsize \
417	  : (fragroundup(fs, blkoff(fs, (size)))))
418
419
420/*
421 * Number of inodes in a secondary storage block/fragment.
422 */
423#define	INOPB(fs)	((fs)->fs_inopb)
424#define	INOPF(fs)	((fs)->fs_inopb >> (fs)->fs_fragshift)
425
426/*
427 * Number of indirects in a filesystem block.
428 */
429#define	NINDIR(fs)	((fs)->fs_nindir)
430
431#define FS_UNCLEAN    0x01      /* filesystem not clean at mount */
432#define FS_DOSOFTDEP  0x02      /* filesystem using soft dependencies */
433#define FS_NEEDSFSCK  0x04      /* filesystem needs sync fsck before mount */
434#define FS_INDEXDIRS  0x08      /* kernel supports indexed directories */
435#define FS_ACLS       0x10      /* file system has ACLs enabled */
436#define FS_MULTILABEL 0x20      /* file system is MAC multi-label */
437#define FS_FLAGS_UPDATED 0x80   /* flags have been moved to new location */
438
439#endif /* _GRUB_UFS2_H_ */
440