xref: /dragonfly/sys/vfs/ntfs/ntfs.h (revision 5868d2b9)
1 /*	$NetBSD: ntfs.h,v 1.9 1999/10/31 19:45:26 jdolecek 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  * $FreeBSD: src/sys/ntfs/ntfs.h,v 1.8.2.2 2001/10/12 22:08:49 semenu Exp $
29  */
30 
31 /*#define NTFS_DEBUG 1*/
32 #if defined(__NetBSD__) && defined(_KERNEL) && !defined(_LKM)
33 #include "opt_ntfs.h"
34 #endif
35 #if defined(__DragonFly__)
36 #include <sys/thread2.h>
37 #endif
38 
39 typedef u_int64_t cn_t;
40 typedef u_int16_t wchar;
41 
42 #pragma pack(1)
43 #define BBSIZE			1024
44 #define	BBOFF			((off_t)(0))
45 #define	BBLOCK			((daddr_t)(0))
46 #define	NTFS_MFTINO		0
47 #define	NTFS_VOLUMEINO		3
48 #define	NTFS_ATTRDEFINO		4
49 #define	NTFS_ROOTINO		5
50 #define	NTFS_BITMAPINO		6
51 #define	NTFS_BOOTINO		7
52 #define	NTFS_BADCLUSINO		8
53 #define	NTFS_UPCASEINO		10
54 #define NTFS_MAXFILENAME	255
55 
56 struct fixuphdr {
57 	u_int32_t       fh_magic;
58 	u_int16_t       fh_foff;
59 	u_int16_t       fh_fnum;
60 };
61 
62 #define NTFS_AF_INRUN	0x00000001
63 struct attrhdr {
64 	u_int32_t       a_type;
65 	u_int32_t       reclen;
66 	u_int8_t        a_flag;
67 	u_int8_t        a_namelen;
68 	u_int8_t        a_nameoff;
69 	u_int8_t        reserved1;
70 	u_int8_t        a_compression;
71 	u_int8_t        reserved2;
72 	u_int16_t       a_index;
73 };
74 #define NTFS_A_STD	0x10
75 #define NTFS_A_ATTRLIST	0x20
76 #define NTFS_A_NAME	0x30
77 #define NTFS_A_VOLUMENAME	0x60
78 #define NTFS_A_DATA	0x80
79 #define	NTFS_A_INDXROOT	0x90
80 #define	NTFS_A_INDX	0xA0
81 #define NTFS_A_INDXBITMAP 0xB0
82 
83 #define NTFS_MAXATTRNAME	255
84 struct attr {
85 	struct attrhdr  a_hdr;
86 	union {
87 		struct {
88 			u_int16_t       a_datalen;
89 			u_int16_t       reserved1;
90 			u_int16_t       a_dataoff;
91 			u_int16_t       a_indexed;
92 		}               a_S_r;
93 		struct {
94 			cn_t            a_vcnstart;
95 			cn_t            a_vcnend;
96 			u_int16_t       a_dataoff;
97 			u_int16_t       a_compressalg;
98 			u_int32_t       reserved1;
99 			u_int64_t       a_allocated;
100 			u_int64_t       a_datalen;
101 			u_int64_t       a_initialized;
102 		}               a_S_nr;
103 	}               a_S;
104 };
105 #define a_r	a_S.a_S_r
106 #define a_nr	a_S.a_S_nr
107 
108 typedef struct {
109 	u_int64_t       t_create;
110 	u_int64_t       t_write;
111 	u_int64_t       t_mftwrite;
112 	u_int64_t       t_access;
113 }               ntfs_times_t;
114 
115 #define NTFS_FFLAG_RDONLY	0x01LL
116 #define NTFS_FFLAG_HIDDEN	0x02LL
117 #define NTFS_FFLAG_SYSTEM	0x04LL
118 #define NTFS_FFLAG_ARCHIVE	0x20LL
119 #define NTFS_FFLAG_COMPRESSED	0x0800LL
120 #define NTFS_FFLAG_DIR		0x10000000LL
121 
122 struct attr_name {
123 	u_int32_t       n_pnumber;	/* Parent ntnode */
124 	u_int32_t       reserved;
125 	ntfs_times_t    n_times;
126 	u_int64_t       n_size;
127 	u_int64_t       n_attrsz;
128 	u_int64_t       n_flag;
129 	u_int8_t        n_namelen;
130 	u_int8_t        n_nametype;
131 	u_int16_t       n_name[1];
132 };
133 
134 #define NTFS_IRFLAG_INDXALLOC	0x00000001
135 struct attr_indexroot {
136 	u_int32_t       ir_unkn1;	/* always 0x30 */
137 	u_int32_t       ir_unkn2;	/* always 0x1 */
138 	u_int32_t       ir_size;/* ??? */
139 	u_int32_t       ir_unkn3;	/* number of cluster */
140 	u_int32_t       ir_unkn4;	/* always 0x10 */
141 	u_int32_t       ir_datalen;	/* sizeof simething */
142 	u_int32_t       ir_allocated;	/* same as above */
143 	u_int16_t       ir_flag;/* ?? always 1 */
144 	u_int16_t       ir_unkn7;
145 };
146 
147 struct attr_attrlist {
148 	u_int32_t       al_type;	/* Attribute type */
149 	u_int16_t       reclen;		/* length of this entry */
150 	u_int8_t        al_namelen;	/* Attribute name len */
151 	u_int8_t        al_nameoff;	/* Name offset from entry start */
152 	u_int64_t       al_vcnstart;	/* VCN number */
153 	u_int32_t       al_inumber;	/* Parent ntnode */
154 	u_int32_t       reserved;
155 	u_int16_t       al_index;	/* Attribute index in MFT record */
156 	u_int16_t       al_name[1];	/* Name */
157 };
158 
159 #define	NTFS_INDXMAGIC	(u_int32_t)(0x58444E49)
160 struct attr_indexalloc {
161 	struct fixuphdr ia_fixup;
162 	u_int64_t       unknown1;
163 	cn_t            ia_bufcn;
164 	u_int16_t       ia_hdrsize;
165 	u_int16_t       unknown2;
166 	u_int32_t       ia_inuse;
167 	u_int32_t       ia_allocated;
168 };
169 
170 #define	NTFS_IEFLAG_SUBNODE	0x00000001
171 #define	NTFS_IEFLAG_LAST	0x00000002
172 
173 struct attr_indexentry {
174 	u_int32_t       ie_number;
175 	u_int32_t       unknown1;
176 	u_int16_t       reclen;
177 	u_int16_t       ie_size;
178 	u_int32_t       ie_flag;/* 1 - has subnodes, 2 - last */
179 	u_int32_t       ie_fpnumber;
180 	u_int32_t       unknown2;
181 	ntfs_times_t    ie_ftimes;
182 	u_int64_t       ie_fallocated;
183 	u_int64_t       ie_fsize;
184 	u_int64_t       ie_fflag;
185 	u_int8_t        ie_fnamelen;
186 	u_int8_t        ie_fnametype;
187 	wchar           ie_fname[NTFS_MAXFILENAME];
188 	/* cn_t		ie_bufcn;	 buffer with subnodes */
189 };
190 
191 #define	NTFS_FILEMAGIC	(u_int32_t)(0x454C4946)
192 #define	NTFS_FRFLAG_DIR	0x0002
193 struct filerec {
194 	struct fixuphdr fr_fixup;
195 	u_int8_t        reserved[8];
196 	u_int16_t       fr_seqnum;	/* Sequence number */
197 	u_int16_t       fr_nlink;
198 	u_int16_t       fr_attroff;	/* offset to attributes */
199 	u_int16_t       fr_flags;	/* 1-nonresident attr, 2-directory */
200 	u_int32_t       fr_size;/* hdr + attributes */
201 	u_int32_t       fr_allocated;	/* allocated length of record */
202 	u_int64_t       fr_mainrec;	/* main record */
203 	u_int16_t       fr_attrnum;	/* maximum attr number + 1 ??? */
204 };
205 
206 #define	NTFS_ATTRNAME_MAXLEN	0x40
207 #define	NTFS_ADFLAG_NONRES	0x0080	/* Attrib can be non resident */
208 #define	NTFS_ADFLAG_INDEX	0x0002	/* Attrib can be indexed */
209 struct attrdef {
210 	wchar		ad_name[NTFS_ATTRNAME_MAXLEN];
211 	u_int32_t	ad_type;
212 	u_int32_t	reserved1[2];
213 	u_int32_t	ad_flag;
214 	u_int64_t	ad_minlen;
215 	u_int64_t	ad_maxlen;	/* -1 for nonlimited */
216 };
217 
218 struct ntvattrdef {
219 	char		ad_name[0x40];
220 	int		ad_namelen;
221 	u_int32_t	ad_type;
222 };
223 
224 #define	NTFS_BBID	"NTFS    "
225 #define	NTFS_BBIDLEN	8
226 struct bootfile {
227 	u_int8_t        reserved1[3];	/* asm jmp near ... */
228 	u_int8_t        bf_sysid[8];	/* 'NTFS    ' */
229 	u_int16_t       bf_bps;		/* bytes per sector */
230 	u_int8_t        bf_spc;		/* sectors per cluster */
231 	u_int8_t        reserved2[7];	/* unused (zeroed) */
232 	u_int8_t        bf_media;	/* media desc. (0xF8) */
233 	u_int8_t        reserved3[2];
234 	u_int16_t       bf_spt;		/* sectors per track */
235 	u_int16_t       bf_heads;	/* number of heads */
236 	u_int8_t        reserver4[12];
237 	u_int64_t       bf_spv;		/* sectors per volume */
238 	cn_t            bf_mftcn;	/* $MFT cluster number */
239 	cn_t            bf_mftmirrcn;	/* $MFTMirr cn */
240 	u_int8_t        bf_mftrecsz;	/* MFT record size (clust) */
241 					/* 0xF6 inducates 1/4 */
242 	u_int32_t       bf_ibsz;	/* index buffer size */
243 	u_int32_t       bf_volsn;	/* volume ser. num. */
244 };
245 
246 #define	NTFS_SYSNODESNUM	0x0B
247 struct ntfsmount {
248 	struct mount   *ntm_mountp;	/* filesystem vfs structure */
249 	struct bootfile ntm_bootfile;
250 	cdev_t           ntm_dev;	/* device mounted */
251 	struct vnode   *ntm_devvp;	/* block device mounted vnode */
252 	struct vnode   *ntm_sysvn[NTFS_SYSNODESNUM];
253 	u_int32_t       ntm_bpmftrec;
254 	uid_t           ntm_uid;
255 	gid_t           ntm_gid;
256 	mode_t          ntm_mode;
257 	u_long          ntm_flag;
258 	cn_t		ntm_cfree;
259 	struct ntvattrdef *ntm_ad;
260 	int		ntm_adnum;
261 	struct netexport ntm_export;	/* export information */
262 	wchar *		ntm_82u;	/* 8bit to Unicode */
263 	char **		ntm_u28;	/* Unicode to 8 bit */
264 	void *          ntm_ic_l2u;     /* Local to Unicode (iconv) */
265 	void *          ntm_ic_u2l;     /* Unicode to Local (iconv) */
266 };
267 
268 #define ntm_mftcn	ntm_bootfile.bf_mftcn
269 #define ntm_mftmirrcn	ntm_bootfile.bf_mftmirrcn
270 #define	ntm_mftrecsz	ntm_bootfile.bf_mftrecsz
271 #define	ntm_spc		ntm_bootfile.bf_spc
272 #define	ntm_bps		ntm_bootfile.bf_bps
273 
274 #pragma pack()
275 
276 #define	NTFS_NEXTREC(s, type) ((type)(((caddr_t) s) + (s)->reclen))
277 
278 /* Convert mount ptr to ntfsmount ptr. */
279 #define VFSTONTFS(mp)	((struct ntfsmount *)((mp)->mnt_data))
280 #define VTONT(v)	FTONT(VTOF(v))
281 #define	VTOF(v)		((struct fnode *)((v)->v_data))
282 #define	FTOV(f)		((f)->f_vp)
283 #define	FTONT(f)	((f)->f_ip)
284 
285 /*
286  * Misc block conversion macros.  The 'doff' macros convert to linear disk
287  * byte offsets suitable for bread, getblk, etc.
288  */
289 #define ntfs_cntobn(cn)	(daddr_t)((cn) * (ntmp->ntm_spc))
290 #define ntfs_cntodoff(cn) ((off_t)(cn) * (ntmp)->ntm_spc * (ntmp)->ntm_bps)
291 #define ntfs_cntob(cn)	((off_t)(cn) * (ntmp)->ntm_spc * (ntmp)->ntm_bps)
292 #define ntfs_btocn(off)	(cn_t)((off) / ((ntmp)->ntm_spc * (ntmp)->ntm_bps))
293 #define ntfs_btocl(off)	(cn_t)((off + ntfs_cntob(1) - 1) / ((ntmp)->ntm_spc * (ntmp)->ntm_bps))
294 #define ntfs_btocnoff(off)	(off_t)((off) % ((ntmp)->ntm_spc * (ntmp)->ntm_bps))
295 #define ntfs_bntob(bn)	(daddr_t)((bn) * (ntmp)->ntm_bps)
296 #define ntfs_bntodoff(bn)	((off_t)(bn) * (ntmp)->ntm_bps)
297 
298 #define	ntfs_bpbl	(daddr_t)((ntmp)->ntm_bps)
299 
300 #ifdef MALLOC_DECLARE
301 MALLOC_DECLARE(M_NTFSMNT);
302 MALLOC_DECLARE(M_NTFSNTNODE);
303 MALLOC_DECLARE(M_NTFSFNODE);
304 MALLOC_DECLARE(M_NTFSDIR);
305 MALLOC_DECLARE(M_NTFSNTHASH);
306 #endif
307 
308 #define VOP__UNLOCK(a, b)	vn_unlock((a))
309 #define VGET(a, b)		vget((a), (b))
310 #define VN_LOCK(a, b)		vn_lock((a), (b))
311 #define	LOCKMGR(a, b)		lockmgr((a), (b))
312 
313 #if defined(NTFS_DEBUG)
314 #define dprintf(a) kprintf a
315 #if NTFS_DEBUG > 1
316 #define ddprintf(a) kprintf a
317 #else
318 #define ddprintf(a)
319 #endif
320 #else
321 #define dprintf(a)
322 #define ddprintf(a)
323 #endif
324 
325