1/*	$OpenBSD: ntfs.h,v 1.19 2022/01/11 03:13:59 jsg 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*/
33typedef u_int64_t cn_t;
34typedef u_int16_t wchar;
35
36#define BBSIZE			1024
37#define	BBOFF			((off_t)(0))
38#define	BBLOCK			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
49/*
50 * UFS directories use 32bit inode numbers internally, regardless
51 * of what the system on top of it uses.
52 */
53typedef u_int32_t	ntfsino_t;
54
55struct fixuphdr {
56	u_int32_t       fh_magic;
57	u_int16_t       fh_foff;
58	u_int16_t       fh_fnum;
59} __packed;
60
61#define NTFS_AF_INRUN	0x00000001
62struct attrhdr {
63	u_int32_t       a_type;
64	u_int32_t       reclen;
65	u_int8_t        a_flag;
66	u_int8_t        a_namelen;
67	u_int8_t        a_nameoff;
68	u_int8_t        reserved1;
69	u_int8_t        a_compression;
70	u_int8_t        reserved2;
71	u_int16_t       a_index;
72} __packed;
73#define NTFS_A_STD	0x10
74#define NTFS_A_ATTRLIST	0x20
75#define NTFS_A_NAME	0x30
76#define NTFS_A_VOLUMENAME	0x60
77#define NTFS_A_DATA	0x80
78#define	NTFS_A_INDXROOT	0x90
79#define	NTFS_A_INDX	0xA0
80#define NTFS_A_INDXBITMAP 0xB0
81
82#define NTFS_MAXATTRNAME	255
83struct attr {
84	struct attrhdr  a_hdr;
85	union {
86		struct {
87			u_int16_t       a_datalen;
88			u_int16_t       reserved1;
89			u_int16_t       a_dataoff;
90			u_int16_t       a_indexed;
91		} __packed	a_S_r;
92		struct {
93			cn_t            a_vcnstart;
94			cn_t            a_vcnend;
95			u_int16_t       a_dataoff;
96			u_int16_t       a_compressalg;
97			u_int32_t       reserved1;
98			u_int64_t       a_allocated;
99			u_int64_t       a_datalen;
100			u_int64_t       a_initialized;
101		} __packed	a_S_nr;
102	}               a_S;
103} __packed;
104#define a_r	a_S.a_S_r
105#define a_nr	a_S.a_S_nr
106
107typedef struct {
108	u_int64_t       t_create;
109	u_int64_t       t_write;
110	u_int64_t       t_mftwrite;
111	u_int64_t       t_access;
112} __packed ntfs_times_t;
113
114#define NTFS_FFLAG_RDONLY	0x01LL
115#define NTFS_FFLAG_HIDDEN	0x02LL
116#define NTFS_FFLAG_SYSTEM	0x04LL
117#define NTFS_FFLAG_ARCHIVE	0x20LL
118#define NTFS_FFLAG_COMPRESSED	0x0800LL
119#define NTFS_FFLAG_DIR		0x10000000LL
120
121struct attr_name {
122	u_int32_t       n_pnumber;	/* Parent ntnode */
123	u_int32_t       reserved;
124	ntfs_times_t    n_times;
125	u_int64_t       n_size;
126	u_int64_t       n_attrsz;
127	u_int64_t       n_flag;
128	u_int8_t        n_namelen;
129	u_int8_t        n_nametype;
130	u_int16_t       n_name[1];
131} __packed;
132
133#define NTFS_IRFLAG_INDXALLOC	0x00000001
134struct attr_indexroot {
135	u_int32_t       ir_unkn1;	/* always 0x30 */
136	u_int32_t       ir_unkn2;	/* always 0x1 */
137	u_int32_t       ir_size;/* ??? */
138	u_int32_t       ir_unkn3;	/* number of cluster */
139	u_int32_t       ir_unkn4;	/* always 0x10 */
140	u_int32_t       ir_datalen;	/* sizeof something */
141	u_int32_t       ir_allocated;	/* same as above */
142	u_int16_t       ir_flag;/* ?? always 1 */
143	u_int16_t       ir_unkn7;
144} __packed;
145
146struct attr_attrlist {
147	u_int32_t       al_type;	/* Attribute type */
148	u_int16_t       reclen;		/* length of this entry */
149	u_int8_t        al_namelen;	/* Attribute name len */
150	u_int8_t        al_nameoff;	/* Name offset from entry start */
151	u_int64_t       al_vcnstart;	/* VCN number */
152	u_int32_t       al_inumber;	/* Parent ntnode */
153	u_int32_t       reserved;
154	u_int16_t       al_index;	/* Attribute index in MFT record */
155	u_int16_t       al_name[1];	/* Name */
156} __packed;
157
158#define	NTFS_INDXMAGIC	(u_int32_t)(0x58444E49)
159struct attr_indexalloc {
160	struct fixuphdr ia_fixup;
161	u_int64_t       unknown1;
162	cn_t            ia_bufcn;
163	u_int16_t       ia_hdrsize;
164	u_int16_t       unknown2;
165	u_int32_t       ia_inuse;
166	u_int32_t       ia_allocated;
167} __packed;
168
169#define	NTFS_IEFLAG_SUBNODE	0x00000001
170#define	NTFS_IEFLAG_LAST	0x00000002
171
172struct attr_indexentry {
173	u_int32_t       ie_number;
174	u_int32_t       unknown1;
175	u_int16_t       reclen;
176	u_int16_t       ie_size;
177	u_int32_t       ie_flag;/* 1 - has subnodes, 2 - last */
178	u_int32_t       ie_fpnumber;
179	u_int32_t       unknown2;
180	ntfs_times_t    ie_ftimes;
181	u_int64_t       ie_fallocated;
182	u_int64_t       ie_fsize;
183	u_int64_t       ie_fflag;
184	u_int8_t        ie_fnamelen;
185	u_int8_t        ie_fnametype;
186	wchar           ie_fname[NTFS_MAXFILENAME];
187	/* cn_t		ie_bufcn;	 buffer with subnodes */
188} __packed;
189
190#define	NTFS_FILEMAGIC	(u_int32_t)(0x454C4946)
191#define	NTFS_FRFLAG_DIR	0x0002
192struct filerec {
193	struct fixuphdr fr_fixup;
194	u_int8_t        reserved[8];
195	u_int16_t       fr_seqnum;	/* Sequence number */
196	u_int16_t       fr_nlink;
197	u_int16_t       fr_attroff;	/* offset to attributes */
198	u_int16_t       fr_flags;	/* 1-nonresident attr, 2-directory */
199	u_int32_t       fr_size;/* hdr + attributes */
200	u_int32_t       fr_allocated;	/* allocated length of record */
201	u_int64_t       fr_mainrec;	/* main record */
202	u_int16_t       fr_attrnum;	/* maximum attr number + 1 ??? */
203} __packed;
204
205#define	NTFS_ATTRNAME_MAXLEN	0x40
206#define	NTFS_ADFLAG_NONRES	0x0080	/* Attrib can be non resident */
207#define	NTFS_ADFLAG_INDEX	0x0002	/* Attrib can be indexed */
208struct attrdef {
209	wchar		ad_name[NTFS_ATTRNAME_MAXLEN];
210	u_int32_t	ad_type;
211	u_int32_t	reserved1[2];
212	u_int32_t	ad_flag;
213	u_int64_t	ad_minlen;
214	u_int64_t	ad_maxlen;	/* -1 for nonlimited */
215} __packed;
216
217struct ntvattrdef {
218	char		ad_name[0x40];
219	int		ad_namelen;
220	u_int32_t	ad_type;
221} __packed;
222
223#define	NTFS_BBID	"NTFS    "
224#define	NTFS_BBIDLEN	8
225struct bootfile {
226	u_int8_t        reserved1[3];	/* asm jmp near ... */
227	u_int8_t        bf_sysid[8];	/* 'NTFS    ' */
228	u_int16_t       bf_bps;		/* bytes per sector */
229	u_int8_t        bf_spc;		/* sectors per cluster */
230	u_int8_t        reserved2[7];	/* unused (zeroed) */
231	u_int8_t        bf_media;	/* media desc. (0xF8) */
232	u_int8_t        reserved3[2];
233	u_int16_t       bf_spt;		/* sectors per track */
234	u_int16_t       bf_heads;	/* number of heads */
235	u_int8_t        reserver4[12];
236	u_int64_t       bf_spv;		/* sectors per volume */
237	cn_t            bf_mftcn;	/* $MFT cluster number */
238	cn_t            bf_mftmirrcn;	/* $MFTMirr cn */
239	u_int8_t        bf_mftrecsz;	/* MFT record size (clust) */
240					/* 0xF6 indicates 1/4 */
241	u_int32_t       bf_ibsz;	/* index buffer size */
242	u_int32_t       bf_volsn;	/* volume ser. num. */
243} __packed;
244
245typedef wchar (ntfs_wget_func_t)(const char **);
246typedef int (ntfs_wput_func_t)(char *, size_t, wchar);
247typedef int (ntfs_wcmp_func_t)(wchar, wchar);
248
249/*
250 * Maximum number of ntnodes to keep in memory. We do not want to leave
251 * large data structures hanging off vnodes indefinitely and the data
252 * needed to reload the ntnode should already be in the buffer cache.
253 */
254#define LOADED_NTNODE_HI 16
255struct ntnode;
256TAILQ_HEAD(ntnodeq, ntnode);
257
258#define	NTFS_SYSNODESNUM	0x0B
259struct ntfsmount {
260	struct mount   *ntm_mountp;	/* filesystem vfs structure */
261	struct bootfile ntm_bootfile;
262	dev_t           ntm_dev;	/* device mounted */
263	struct vnode   *ntm_devvp;	/* block device mounted vnode */
264	struct vnode   *ntm_sysvn[NTFS_SYSNODESNUM];
265	u_int32_t       ntm_bpmftrec;
266	uid_t           ntm_uid;
267	gid_t           ntm_gid;
268	mode_t          ntm_mode;
269	u_long          ntm_flag;
270	cn_t		ntm_cfree;
271	struct ntvattrdef *ntm_ad;
272	int		ntm_adnum;
273	struct netexport ntm_export;	/* export information */
274	ntfs_wget_func_t *ntm_wget;	/* decode string to Unicode string */
275	ntfs_wput_func_t *ntm_wput;	/* encode Unicode string to string */
276	ntfs_wcmp_func_t *ntm_wcmp;	/* compare to wide characters */
277	int		ntm_ntnodes;	/* Number of loaded ntnodes. */
278	struct ntnodeq	ntm_ntnodeq;	/* Queue of ntnodes (LRU). */
279};
280
281#define ntm_mftcn	ntm_bootfile.bf_mftcn
282#define ntm_mftmirrcn	ntm_bootfile.bf_mftmirrcn
283#define	ntm_mftrecsz	ntm_bootfile.bf_mftrecsz
284#define	ntm_spc		ntm_bootfile.bf_spc
285#define	ntm_bps		ntm_bootfile.bf_bps
286
287#define	NTFS_NEXTREC(s, type) ((type)(((caddr_t) s) + (s)->reclen))
288
289/* Convert mount ptr to ntfsmount ptr. */
290#define VFSTONTFS(mp)	((struct ntfsmount *)((mp)->mnt_data))
291#define VTONT(v)	FTONT(VTOF(v))
292#define	VTOF(v)		((struct fnode *)((v)->v_data))
293#define	FTOV(f)		((f)->f_vp)
294#define	FTONT(f)	((f)->f_ip)
295#define ntfs_cntobn(cn)	(daddr_t)((cn) * (ntmp->ntm_spc))
296#define ntfs_cntob(cn)	(off_t)((cn) * (ntmp)->ntm_spc * (ntmp)->ntm_bps)
297#define ntfs_btocn(off)	(cn_t)((off) / ((ntmp)->ntm_spc * (ntmp)->ntm_bps))
298#define ntfs_btocl(off)	(cn_t)((off + ntfs_cntob(1) - 1) / ((ntmp)->ntm_spc * (ntmp)->ntm_bps))
299#define ntfs_btocnoff(off)	(off_t)((off) % ((ntmp)->ntm_spc * (ntmp)->ntm_bps))
300#define ntfs_bntob(bn)	(int32_t)((bn) * (ntmp)->ntm_bps)
301
302#ifdef _KERNEL
303#if defined(NTFS_DEBUG)
304extern int ntfs_debug;
305#define DNPRINTF(n, x...) do { if(ntfs_debug >= (n)) printf(x); } while(0)
306#define DPRINTF(x...) DNPRINTF(1, x)
307#define DDPRINTF(x...) DNPRINTF(2, x)
308#else /* NTFS_DEBUG */
309#define DNPRINTF(n, x...)
310#define DPRINTF(x...)
311#define DDPRINTF(x...)
312#endif
313
314extern const struct vops ntfs_vops;
315#endif
316