ntfs.h revision 47060
1/*	$NetBSD: ntfs.h,v 1.2 1999/05/06 15:43:17 christos Exp $	*/
2
3/*-
4 * Copyright (c) 1998, 1999 Semen Ustimenko
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	$Id: ntfs.h,v 1.4 1999/04/20 21:06:41 semenu Exp $
29 */
30
31/*#define NTFS_DEBUG 1*/
32
33typedef u_int64_t cn_t;
34typedef u_int16_t wchar;
35
36#pragma pack(1)
37#define BBSIZE			1024
38#define	BBOFF			((off_t)(0))
39#define	BBLOCK			((daddr_t)(0))
40#define	NTFS_MFTINO		0
41#define	NTFS_VOLUMEINO		3
42#define	NTFS_ATTRDEFINO		4
43#define	NTFS_ROOTINO		5
44#define	NTFS_BITMAPINO		6
45#define	NTFS_BOOTINO		7
46#define	NTFS_BADCLUSINO		8
47#define	NTFS_UPCASEINO		10
48#define NTFS_MAXFILENAME	255
49
50struct fixuphdr {
51	u_int32_t       fh_magic;
52	u_int16_t       fh_foff;
53	u_int16_t       fh_fnum;
54};
55
56#define NTFS_AF_INRUN	0x00000001
57struct attrhdr {
58	u_int32_t       a_type;
59	u_int32_t       reclen;
60	u_int8_t        a_flag;
61	u_int8_t        a_namelen;
62	u_int8_t        a_nameoff;
63	u_int8_t        reserved1;
64	u_int8_t        a_compression;
65	u_int8_t        reserved2;
66	u_int16_t       a_index;
67};
68#define NTFS_A_STD	0x10
69#define NTFS_A_ATTRLIST	0x20
70#define NTFS_A_NAME	0x30
71#define NTFS_A_VOLUMENAME	0x60
72#define NTFS_A_DATA	0x80
73#define	NTFS_A_INDXROOT	0x90
74#define	NTFS_A_INDX	0xA0
75#define NTFS_A_INDXBITMAP 0xB0
76
77#define NTFS_MAXATTRNAME	255
78struct attr {
79	struct attrhdr  a_hdr;
80	union {
81		struct {
82			u_int16_t       a_datalen;
83			u_int16_t       reserved1;
84			u_int16_t       a_dataoff;
85			u_int16_t       a_indexed;
86		}               a_S_r;
87		struct {
88			cn_t            a_vcnstart;
89			cn_t            a_vcnend;
90			u_int16_t       a_dataoff;
91			u_int16_t       a_compressalg;
92			u_int32_t       reserved1;
93			u_int64_t       a_allocated;
94			u_int64_t       a_datalen;
95			u_int64_t       a_initialized;
96		}               a_S_nr;
97	}               a_S;
98};
99#define a_r	a_S.a_S_r
100#define a_nr	a_S.a_S_nr
101
102typedef struct {
103	u_int64_t       t_create;
104	u_int64_t       t_write;
105	u_int64_t       t_mftwrite;
106	u_int64_t       t_access;
107}               ntfs_times_t;
108
109#define NTFS_FFLAG_RDONLY	0x01LL
110#define NTFS_FFLAG_HIDDEN	0x02LL
111#define NTFS_FFLAG_SYSTEM	0x04LL
112#define NTFS_FFLAG_ARCHIVE	0x20LL
113#define NTFS_FFLAG_COMPRESSED	0x0800LL
114#define NTFS_FFLAG_DIR		0x10000000LL
115
116struct attr_name {
117	u_int32_t       n_pnumber;	/* Parent ntnode */
118	u_int32_t       reserved;
119	ntfs_times_t    n_times;
120	u_int64_t       n_size;
121	u_int64_t       n_attrsz;
122	u_int64_t       n_flag;
123	u_int8_t        n_namelen;
124	u_int8_t        n_nametype;
125	u_int16_t       n_name[1];
126};
127
128#define NTFS_IRFLAG_INDXALLOC	0x00000001
129struct attr_indexroot {
130	u_int32_t       ir_unkn1;	/* always 0x30 */
131	u_int32_t       ir_unkn2;	/* always 0x1 */
132	u_int32_t       ir_size;/* ??? */
133	u_int32_t       ir_unkn3;	/* number of cluster */
134	u_int32_t       ir_unkn4;	/* always 0x10 */
135	u_int32_t       ir_datalen;	/* sizeof simething */
136	u_int32_t       ir_allocated;	/* same as above */
137	u_int16_t       ir_flag;/* ?? always 1 */
138	u_int16_t       ir_unkn7;
139};
140
141struct attr_attrlist {
142	u_int32_t       al_type;	/* Attribute type */
143	u_int16_t       reclen;		/* length of this entry */
144	u_int8_t        al_namelen;	/* Attribute name len */
145	u_int8_t        al_nameoff;	/* Name offset from entry start */
146	u_int64_t       al_vcnstart;	/* VCN number */
147	u_int32_t       al_inumber;	/* Parent ntnode */
148	u_int32_t       reserved;
149	u_int16_t       al_index;	/* Attribute index in MFT record */
150	u_int16_t       al_name[1];	/* Name */
151};
152
153#define	NTFS_INDXMAGIC	(u_int32_t)(0x58444E49)
154struct attr_indexalloc {
155	struct fixuphdr ia_fixup;
156	u_int64_t       unknown1;
157	cn_t            ia_bufcn;
158	u_int16_t       ia_hdrsize;
159	u_int16_t       unknown2;
160	u_int32_t       ia_inuse;
161	u_int32_t       ia_allocated;
162};
163
164#define	NTFS_IEFLAG_SUBNODE	0x00000001
165#define	NTFS_IEFLAG_LAST	0x00000002
166
167struct attr_indexentry {
168	u_int32_t       ie_number;
169	u_int32_t       unknown1;
170	u_int16_t       reclen;
171	u_int16_t       ie_size;
172	u_int32_t       ie_flag;/* 1 - has subnodes, 2 - last */
173	u_int32_t       ie_fpnumber;
174	u_int32_t       unknown2;
175	ntfs_times_t    ie_ftimes;
176	u_int64_t       ie_fallocated;
177	u_int64_t       ie_fsize;
178	u_int64_t       ie_fflag;
179	u_int8_t        ie_fnamelen;
180	u_int8_t        ie_fnametype;
181	wchar           ie_fname[NTFS_MAXFILENAME];
182	/* cn_t		ie_bufcn;	 buffer with subnodes */
183};
184
185#define	NTFS_FILEMAGIC	(u_int32_t)(0x454C4946)
186#define	NTFS_FRFLAG_DIR	0x0002
187struct filerec {
188	struct fixuphdr fr_fixup;
189	u_int8_t        reserved[8];
190	u_int16_t       fr_seqnum;	/* Sequence number */
191	u_int16_t       fr_nlink;
192	u_int16_t       fr_attroff;	/* offset to attributes */
193	u_int16_t       fr_flags;	/* 1-nonresident attr, 2-directory */
194	u_int32_t       fr_size;/* hdr + attributes */
195	u_int32_t       fr_allocated;	/* allocated length of record */
196	u_int64_t       fr_mainrec;	/* main record */
197	u_int16_t       fr_attrnum;	/* maximum attr number + 1 ??? */
198};
199
200#define	NTFS_ATTRNAME_MAXLEN	0x40
201#define	NTFS_ADFLAG_NONRES	0x0080	/* Attrib can be non resident */
202#define	NTFS_ADFLAG_INDEX	0x0002	/* Attrib can be indexed */
203struct attrdef {
204	wchar		ad_name[NTFS_ATTRNAME_MAXLEN];
205	u_int32_t	ad_type;
206	u_int32_t	reserved1[2];
207	u_int32_t	ad_flag;
208	u_int64_t	ad_minlen;
209	u_int64_t	ad_maxlen;	/* -1 for nonlimited */
210};
211
212struct ntvattrdef {
213	char		ad_name[0x40];
214	int		ad_namelen;
215	u_int32_t	ad_type;
216};
217
218#define	NTFS_BBID	"NTFS    "
219#define	NTFS_BBIDLEN	8
220struct bootfile {
221	u_int8_t        reserved1[3];	/* asm jmp near ... */
222	u_int8_t        bf_sysid[8];	/* 'NTFS    ' */
223	u_int16_t       bf_bps;		/* bytes per sector */
224	u_int8_t        bf_spc;		/* sectors per cluster */
225	u_int8_t        reserved2[7];	/* unused (zeroed) */
226	u_int8_t        bf_media;	/* media desc. (0xF8) */
227	u_int8_t        reserved3[2];
228	u_int16_t       bf_spt;		/* sectors per track */
229	u_int16_t       bf_heads;	/* number of heads */
230	u_int8_t        reserver4[12];
231	u_int64_t       bf_spv;		/* sectors per volume */
232	cn_t            bf_mftcn;	/* $MFT cluster number */
233	cn_t            bf_mftmirrcn;	/* $MFTMirr cn */
234	u_int8_t        bf_mftrecsz;	/* MFT record size (clust) */
235					/* 0xF6 inducates 1/4 */
236	u_int32_t       bf_ibsz;	/* index buffer size */
237	u_int32_t       bf_volsn;	/* volume ser. num. */
238};
239
240#define	NTFS_SYSNODESNUM	0x0B
241struct ntfsmount {
242	struct mount   *ntm_mountp;
243	struct bootfile ntm_bootfile;
244	dev_t           ntm_dev;
245	struct vnode   *ntm_devvp;
246	struct vnode   *ntm_sysvn[NTFS_SYSNODESNUM];
247	wchar          *ntm_upcase;
248	u_int32_t       ntm_bpmftrec;
249	uid_t           ntm_uid;
250	gid_t           ntm_gid;
251	mode_t          ntm_mode;
252	u_long          ntm_flag;
253	cn_t		ntm_cfree;
254	struct ntvattrdef *ntm_ad;
255	int		ntm_adnum;
256};
257
258#define ntm_mftcn	ntm_bootfile.bf_mftcn
259#define ntm_mftmirrcn	ntm_bootfile.bf_mftmirrcn
260#define	ntm_mftrecsz	ntm_bootfile.bf_mftrecsz
261#define	ntm_spc		ntm_bootfile.bf_spc
262#define	ntm_bps		ntm_bootfile.bf_bps
263
264#pragma pack()
265
266#define	NTFS_NEXTREC(s, type) ((type)(((caddr_t) s) + (s)->reclen))
267
268/* Convert mount ptr to ntfsmount ptr. */
269#define VFSTONTFS(mp)	((struct ntfsmount *)((mp)->mnt_data))
270#define VTONT(v)	FTONT(VTOF(v))
271#define	VTOF(v)		((struct fnode *)((v)->v_data))
272#define	FTOV(f)		((f)->f_vp)
273#define	FTONT(f)	((f)->f_ip)
274#define ntfs_cntobn(cn)	(daddr_t)((cn) * (ntmp->ntm_spc))
275#define ntfs_cntob(cn)	(off_t)((cn) * (ntmp)->ntm_spc * (ntmp)->ntm_bps)
276#define ntfs_btocn(off)	(cn_t)((off) / ((ntmp)->ntm_spc * (ntmp)->ntm_bps))
277#define ntfs_btocl(off)	(cn_t)((off + ntfs_cntob(1) - 1) / ((ntmp)->ntm_spc * (ntmp)->ntm_bps))
278#define ntfs_btocnoff(off)	(off_t)((off) % ((ntmp)->ntm_spc * (ntmp)->ntm_bps))
279#define ntfs_bntob(bn)	(daddr_t)((bn) * (ntmp)->ntm_bps)
280
281#define	ntfs_bpbl	(daddr_t)((ntmp)->ntm_bps)
282
283#if __FreeBSD_version >= 300000
284MALLOC_DECLARE(M_NTFSMNT);
285MALLOC_DECLARE(M_NTFSNTNODE);
286MALLOC_DECLARE(M_NTFSFNODE);
287MALLOC_DECLARE(M_NTFSDIR);
288MALLOC_DECLARE(M_NTFSNTHASH);
289#endif
290
291#ifdef __NetBSD__
292#define MALLOC_DEFINE(a, b, c)
293#define M_NTFSNTHASH	M_TEMP
294#define M_NTFSNTVATTR	M_TEMP
295#define M_NTFSRDATA	M_TEMP
296#define M_NTFSRUN	M_TEMP
297#define M_NTFSDECOMP	M_TEMP
298#define M_NTFSMNT	M_TEMP
299#define M_NTFSNTNODE	M_TEMP
300#define M_NTFSFNODE	M_TEMP
301#define M_NTFSDIR	M_TEMP
302typedef int (vop_t) __P((void *));
303#define HASHINIT(a, b, c, d)	hashinit((a), (b), (c), (d))
304#define bqrelse(bp)		brelse(bp)
305#define VOP__LOCK(a, b, c)	VOP_LOCK((a), (b) ? LK_EXCLUSIVE : LK_SHARED)
306#define VOP__UNLOCK(a, b, c)	VOP_UNLOCK((a), 0)
307#define VGET(a, b, c)		vget((a), LK_EXCLUSIVE)
308#define VN_LOCK(a, b, c)	vn_lock((a), LK_EXCLUSIVE)
309#else
310#define HASHINIT(a, b, c, d)	hashinit((a), (b), (d))
311#define VOP__LOCK(a, b, c)	VOP_LOCK((a), (b), (c))
312#define VOP__UNLOCK(a, b, c)	VOP_UNLOCK((a), (b), (c))
313#define VGET(a, b, c)		vget((a), (b), (c))
314#define VN_LOCK(a, b, c)	vn_lock((a), (b), (c))
315#endif
316
317#if defined(NTFS_DEBUG)
318#define dprintf(a) printf a
319#if NTFS_DEBUG > 1
320#define ddprintf(a) printf a
321#else
322#define ddprintf(a)
323#endif
324#else
325#define dprintf(a)
326#define ddprintf(a)
327#endif
328
329extern vop_t  **ntfs_vnodeop_p;
330