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