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