xref: /openbsd-src/sys/ntfs/ntfs_subr.c (revision f702838acc78b3179b642afcbab26f85d45fd679)
1*f702838aSclaudio /*	$OpenBSD: ntfs_subr.c,v 1.53 2025/01/13 13:58:41 claudio Exp $	*/
226f3deacStedu /*	$NetBSD: ntfs_subr.c,v 1.4 2003/04/10 21:37:32 jdolecek Exp $	*/
326f3deacStedu 
426f3deacStedu /*-
526f3deacStedu  * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org)
626f3deacStedu  * All rights reserved.
726f3deacStedu  *
826f3deacStedu  * Redistribution and use in source and binary forms, with or without
926f3deacStedu  * modification, are permitted provided that the following conditions
1026f3deacStedu  * are met:
1126f3deacStedu  * 1. Redistributions of source code must retain the above copyright
1226f3deacStedu  *    notice, this list of conditions and the following disclaimer.
1326f3deacStedu  * 2. Redistributions in binary form must reproduce the above copyright
1426f3deacStedu  *    notice, this list of conditions and the following disclaimer in the
1526f3deacStedu  *    documentation and/or other materials provided with the distribution.
1626f3deacStedu  *
1726f3deacStedu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1826f3deacStedu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1926f3deacStedu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2026f3deacStedu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2126f3deacStedu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2226f3deacStedu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2326f3deacStedu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2426f3deacStedu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2526f3deacStedu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2626f3deacStedu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2726f3deacStedu  * SUCH DAMAGE.
2826f3deacStedu  *
2926f3deacStedu  *	Id: ntfs_subr.c,v 1.4 1999/05/12 09:43:01 semenu Exp
3026f3deacStedu  */
3126f3deacStedu 
3226f3deacStedu #include <sys/param.h>
3326f3deacStedu #include <sys/systm.h>
3426f3deacStedu #include <sys/namei.h>
3526f3deacStedu #include <sys/kernel.h>
3626f3deacStedu #include <sys/vnode.h>
370ae9dfeaStedu #include <sys/lock.h>
3826f3deacStedu #include <sys/mount.h>
3926f3deacStedu #include <sys/buf.h>
4026f3deacStedu #include <sys/malloc.h>
41c74ceabfSoga #include <sys/rwlock.h>
42544451c3Sderaadt #include <sys/specdev.h>
4326f3deacStedu 
4426f3deacStedu /* #define NTFS_DEBUG 1 */
4526f3deacStedu #include <ntfs/ntfs.h>
4626f3deacStedu #include <ntfs/ntfsmount.h>
4726f3deacStedu #include <ntfs/ntfs_inode.h>
4826f3deacStedu #include <ntfs/ntfs_vfsops.h>
4926f3deacStedu #include <ntfs/ntfs_subr.h>
5026f3deacStedu #include <ntfs/ntfs_compr.h>
5126f3deacStedu #include <ntfs/ntfs_ihash.h>
5226f3deacStedu 
5326f3deacStedu #if defined(NTFS_DEBUG)
5426f3deacStedu int ntfs_debug = NTFS_DEBUG;
5526f3deacStedu #endif
5626f3deacStedu 
5726f3deacStedu /* Local struct used in ntfs_ntlookupfile() */
5826f3deacStedu struct ntfs_lookup_ctx {
5926f3deacStedu 	u_int32_t	aoff;
6026f3deacStedu 	u_int32_t	rdsize;
6126f3deacStedu 	cn_t		cn;
6226f3deacStedu 	struct ntfs_lookup_ctx *prev;
6326f3deacStedu };
6426f3deacStedu 
6505c5dae5Sjsing int ntfs_ntlookupattr(struct ntfsmount *, const char *, int, int *, char **);
6605c5dae5Sjsing int ntfs_findvattr(struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
6705c5dae5Sjsing int ntfs_uastricmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
6805c5dae5Sjsing int ntfs_uastrcmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
6926f3deacStedu 
70274476fcStedu /* table for mapping Unicode chars into uppercase; it's filled upon first
71274476fcStedu  * ntfs mount, freed upon last ntfs umount */
7226f3deacStedu static wchar *ntfs_toupper_tab;
7326f3deacStedu #define NTFS_U28(ch)		((((ch) & 0xE0) == 0) ? '_' : (ch) & 0xFF)
7426f3deacStedu #define NTFS_TOUPPER(ch)	(ntfs_toupper_tab[(unsigned char)(ch)])
75274476fcStedu struct rwlock ntfs_toupper_lock = RWLOCK_INITIALIZER("ntfs_toupper");
76274476fcStedu static signed int ntfs_toupper_usecount;
7726f3deacStedu 
7826f3deacStedu /* support macro for ntfs_ntvattrget() */
7926f3deacStedu #define NTFS_AALPCMP(aalp,type,name,namelen) (				\
8026f3deacStedu   (aalp->al_type == type) && (aalp->al_namelen == namelen) &&		\
8126f3deacStedu   !ntfs_uastrcmp(ntmp, aalp->al_name,aalp->al_namelen,name,namelen) )
8226f3deacStedu 
8326f3deacStedu /*
8426f3deacStedu  *
8526f3deacStedu  */
8626f3deacStedu int
871cc0505dSjsing ntfs_ntvattrrele(struct ntvattr *vap)
8826f3deacStedu {
89f2997212Sjsing 	DPRINTF("ntfs_ntvattrrele: ino: %u, type: 0x%x\n",
90a1ea65c6Sjsing 	    vap->va_ip->i_number, vap->va_type);
9126f3deacStedu 
9226f3deacStedu 	ntfs_ntrele(vap->va_ip);
9326f3deacStedu 
9426f3deacStedu 	return (0);
9526f3deacStedu }
9626f3deacStedu 
9726f3deacStedu /*
9826f3deacStedu  * find the attribute in the ntnode
9926f3deacStedu  */
10005c5dae5Sjsing int
1011cc0505dSjsing ntfs_findvattr(struct ntfsmount *ntmp, struct ntnode *ip,
1021cc0505dSjsing     struct ntvattr **lvapp, struct ntvattr **vapp, u_int32_t type,
1031cc0505dSjsing     const char *name, size_t namelen, cn_t vcn)
10426f3deacStedu {
10526f3deacStedu 	int error;
10626f3deacStedu 	struct ntvattr *vap;
10726f3deacStedu 
10826f3deacStedu 	if((ip->i_flag & IN_LOADED) == 0) {
109f2997212Sjsing 		DPRINTF("ntfs_findvattr: node not loaded, ino: %u\n",
110a1ea65c6Sjsing 		    ip->i_number);
11126f3deacStedu 		error = ntfs_loadntnode(ntmp,ip);
11226f3deacStedu 		if (error) {
11326f3deacStedu 			printf("ntfs_findvattr: FAILED TO LOAD INO: %d\n",
11426f3deacStedu 			       ip->i_number);
11526f3deacStedu 			return (error);
11626f3deacStedu 		}
1178fc6378aSjsing 	} else {
1188fc6378aSjsing 		/* Update LRU loaded list. */
1198fc6378aSjsing 		TAILQ_REMOVE(&ntmp->ntm_ntnodeq, ip, i_loaded);
1208fc6378aSjsing 		TAILQ_INSERT_HEAD(&ntmp->ntm_ntnodeq, ip, i_loaded);
12126f3deacStedu 	}
12226f3deacStedu 
12326f3deacStedu 	*lvapp = NULL;
12426f3deacStedu 	*vapp = NULL;
1251573508eSmiod 	LIST_FOREACH(vap, &ip->i_valist, va_list) {
126f2997212Sjsing 		DDPRINTF("ntfs_findvattr: type: 0x%x, vcn: %llu - %llu\n",
127f2997212Sjsing 		    vap->va_type, vap->va_vcnstart, vap->va_vcnend);
12826f3deacStedu 		if ((vap->va_type == type) &&
12926f3deacStedu 		    (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
13026f3deacStedu 		    (vap->va_namelen == namelen) &&
13126f3deacStedu 		    (strncmp(name, vap->va_name, namelen) == 0)) {
13226f3deacStedu 			*vapp = vap;
13326f3deacStedu 			ntfs_ntref(vap->va_ip);
13426f3deacStedu 			return (0);
13526f3deacStedu 		}
13626f3deacStedu 		if (vap->va_type == NTFS_A_ATTRLIST)
13726f3deacStedu 			*lvapp = vap;
13826f3deacStedu 	}
13926f3deacStedu 
14026f3deacStedu 	return (-1);
14126f3deacStedu }
14226f3deacStedu 
14326f3deacStedu /*
14426f3deacStedu  * Search attribute specified in ntnode (load ntnode if necessary).
14526f3deacStedu  * If not found but ATTR_A_ATTRLIST present, read it in and search through.
146cec6d865Smiod  * VOP_VGET node needed, and lookup through its ntnode (load if necessary).
14726f3deacStedu  *
14826f3deacStedu  * ntnode should be locked
14926f3deacStedu  */
15026f3deacStedu int
1511cc0505dSjsing ntfs_ntvattrget(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t type,
1521cc0505dSjsing     const char *name, cn_t vcn, struct ntvattr **vapp)
15326f3deacStedu {
15426f3deacStedu 	struct ntvattr *lvap = NULL;
15526f3deacStedu 	struct attr_attrlist *aalp;
15626f3deacStedu 	struct attr_attrlist *nextaalp;
15726f3deacStedu 	struct vnode   *newvp;
15826f3deacStedu 	struct ntnode  *newip;
15926f3deacStedu 	caddr_t         alpool;
16026f3deacStedu 	size_t		namelen, len;
16126f3deacStedu 	int             error;
16226f3deacStedu 
16326f3deacStedu 	*vapp = NULL;
16426f3deacStedu 
16526f3deacStedu 	if (name) {
166f2997212Sjsing 		DPRINTF("ntfs_ntvattrget: ino: %u, type: 0x%x, name: %s, "
167f2997212Sjsing 		    "vcn: %llu\n", ip->i_number, type, name, vcn);
16826f3deacStedu 		namelen = strlen(name);
16926f3deacStedu 	} else {
170f2997212Sjsing 		DPRINTF("ntfs_ntvattrget: ino: %u, type: 0x%x, vcn: %llu\n",
171f2997212Sjsing 		    ip->i_number, type, vcn);
17226f3deacStedu 		name = "";
17326f3deacStedu 		namelen = 0;
17426f3deacStedu 	}
17526f3deacStedu 
17626f3deacStedu 	error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
17726f3deacStedu 	if (error >= 0)
17826f3deacStedu 		return (error);
17926f3deacStedu 
18026f3deacStedu 	if (!lvap) {
181f2997212Sjsing 		DPRINTF("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: ino: %u, "
182f2997212Sjsing 		    "type: 0x%x, name: %s, vcn: %llu\n", ip->i_number, type,
183f2997212Sjsing 		    name, vcn);
18426f3deacStedu 		return (ENOENT);
18526f3deacStedu 	}
18626f3deacStedu 	/* Scan $ATTRIBUTE_LIST for requested attribute */
18726f3deacStedu 	len = lvap->va_datalen;
188c6c302e5Stedu 	alpool = malloc(len, M_TEMP, M_WAITOK);
18926f3deacStedu 	error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
19026f3deacStedu 			NULL);
19126f3deacStedu 	if (error)
19226f3deacStedu 		goto out;
19326f3deacStedu 
19426f3deacStedu 	aalp = (struct attr_attrlist *) alpool;
19526f3deacStedu 	nextaalp = NULL;
19626f3deacStedu 
19726f3deacStedu 	for(; len > 0; aalp = nextaalp) {
198f2997212Sjsing 		DPRINTF("ntfs_ntvattrget: attrlist: ino: %u, attr: 0x%x, "
199f2997212Sjsing 		    "vcn: %llu\n", aalp->al_inumber, aalp->al_type,
200f2997212Sjsing 		    aalp->al_vcnstart);
20126f3deacStedu 
20226f3deacStedu 		if (len > aalp->reclen) {
20326f3deacStedu 			nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
20426f3deacStedu 		} else {
20526f3deacStedu 			nextaalp = NULL;
20626f3deacStedu 		}
20726f3deacStedu 		len -= aalp->reclen;
20826f3deacStedu 
20926f3deacStedu 		if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
21026f3deacStedu 		    (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
21126f3deacStedu 		     NTFS_AALPCMP(nextaalp, type, name, namelen)))
21226f3deacStedu 			continue;
21326f3deacStedu 
214f2997212Sjsing 		DPRINTF("ntfs_ntvattrget: attribute in ino: %u\n",
215a1ea65c6Sjsing 		    aalp->al_inumber);
21626f3deacStedu 
21726f3deacStedu 		/* this is not a main record, so we can't use just plain
21826f3deacStedu 		   vget() */
21926f3deacStedu 		error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
22026f3deacStedu 				NTFS_A_DATA, NULL, LK_EXCLUSIVE,
221db7aa982Smpi 				VG_EXT, &newvp);
22226f3deacStedu 		if (error) {
22326f3deacStedu 			printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
22426f3deacStedu 			       aalp->al_inumber);
22526f3deacStedu 			goto out;
22626f3deacStedu 		}
22726f3deacStedu 		newip = VTONT(newvp);
22826f3deacStedu 		/* XXX have to lock ntnode */
22926f3deacStedu 		error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
23026f3deacStedu 				type, name, namelen, vcn);
23126f3deacStedu 		vput(newvp);
23226f3deacStedu 		if (error == 0)
23326f3deacStedu 			goto out;
23426f3deacStedu 		printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
23526f3deacStedu 		break;
23626f3deacStedu 	}
23726f3deacStedu 	error = ENOENT;
23826f3deacStedu 
239f2997212Sjsing 	DPRINTF("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: ino: %u, type: 0x%x, "
240f2997212Sjsing 	    "name: %.*s, vcn: %llu\n", ip->i_number, type,
241f2997212Sjsing 	    (unsigned int)namelen, name, vcn);
24226f3deacStedu out:
243825c4e6aStedu 	free(alpool, M_TEMP, 0);
24426f3deacStedu 	return (error);
24526f3deacStedu }
24626f3deacStedu 
24726f3deacStedu /*
24826f3deacStedu  * Read ntnode from disk, make ntvattr list.
24926f3deacStedu  *
25026f3deacStedu  * ntnode should be locked
25126f3deacStedu  */
25226f3deacStedu int
2531cc0505dSjsing ntfs_loadntnode(struct ntfsmount *ntmp, struct ntnode *ip)
25426f3deacStedu {
2558fc6378aSjsing 	struct ntnode	*oip;
2568fc6378aSjsing 	struct ntvattr	*vap;
25726f3deacStedu 	struct filerec	*mfrp;
2588fc6378aSjsing 	struct attr	*ap;
2591abdbfdeSderaadt 	daddr_t		bn;
26026f3deacStedu  	int		error,off;
26126f3deacStedu 
262f2997212Sjsing  	DPRINTF("ntfs_loadntnode: loading ino: %u\n", ip->i_number);
26326f3deacStedu 
2648fc6378aSjsing 	KASSERT((ip->i_flag & IN_LOADED) == 0);
2658fc6378aSjsing 
2668fc6378aSjsing 	if (ntmp->ntm_ntnodes >= LOADED_NTNODE_HI) {
2678fc6378aSjsing 		oip = TAILQ_LAST(&ntmp->ntm_ntnodeq, ntnodeq);
2688fc6378aSjsing 		TAILQ_REMOVE(&ntmp->ntm_ntnodeq, oip, i_loaded);
2698fc6378aSjsing 		ntmp->ntm_ntnodes--;
2708fc6378aSjsing 
271f2997212Sjsing 		DPRINTF("ntfs_loadntnode: unloading ino: %u\n", oip->i_number);
2728fc6378aSjsing 
2738fc6378aSjsing 		KASSERT((oip->i_flag & IN_LOADED));
2748fc6378aSjsing 		oip->i_flag &= ~IN_LOADED;
2758fc6378aSjsing 		while ((vap = LIST_FIRST(&oip->i_valist)) != NULL) {
2768fc6378aSjsing 			LIST_REMOVE(vap, va_list);
2778fc6378aSjsing 			ntfs_freentvattr(vap);
2788fc6378aSjsing 		}
2798fc6378aSjsing 	}
2808fc6378aSjsing 
281c6c302e5Stedu  	mfrp = malloc(ntfs_bntob(ntmp->ntm_bpmftrec), M_TEMP, M_WAITOK);
28226f3deacStedu 
28326f3deacStedu 	if (ip->i_number < NTFS_SYSNODESNUM) {
28426f3deacStedu 		struct buf     *bp;
28526f3deacStedu 
286a1ea65c6Sjsing 		DPRINTF("ntfs_loadntnode: read system node\n");
28726f3deacStedu 
28826f3deacStedu 		bn = ntfs_cntobn(ntmp->ntm_mftcn) +
28926f3deacStedu 			ntmp->ntm_bpmftrec * ip->i_number;
29026f3deacStedu 
29193f62a9eStedu 		error = bread(ntmp->ntm_devvp, bn,
29293f62a9eStedu 		    ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
29326f3deacStedu 		if (error) {
29426f3deacStedu 			printf("ntfs_loadntnode: BREAD FAILED\n");
29526f3deacStedu 			brelse(bp);
29626f3deacStedu 			goto out;
29726f3deacStedu 		}
29826f3deacStedu 		memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
299c6c302e5Stedu 		brelse(bp);
30026f3deacStedu 	} else {
30126f3deacStedu 		struct vnode   *vp;
30226f3deacStedu 
30326f3deacStedu 		vp = ntmp->ntm_sysvn[NTFS_MFTINO];
30426f3deacStedu 		error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
30526f3deacStedu 			       ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
30626f3deacStedu 			       ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
30726f3deacStedu 		if (error) {
30826f3deacStedu 			printf("ntfs_loadntnode: ntfs_readattr failed\n");
30926f3deacStedu 			goto out;
31026f3deacStedu 		}
31126f3deacStedu 	}
31226f3deacStedu 
31326f3deacStedu 	/* Check if magic and fixups are correct */
31426f3deacStedu 	error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
31526f3deacStedu 				ntfs_bntob(ntmp->ntm_bpmftrec));
31626f3deacStedu 	if (error) {
31726f3deacStedu 		printf("ntfs_loadntnode: BAD MFT RECORD %d\n",
31826f3deacStedu 		       (u_int32_t) ip->i_number);
31926f3deacStedu 		goto out;
32026f3deacStedu 	}
32126f3deacStedu 
322f2997212Sjsing 	DPRINTF("ntfs_loadntnode: load attrs for ino: %u\n", ip->i_number);
32326f3deacStedu 	off = mfrp->fr_attroff;
32426f3deacStedu 	ap = (struct attr *) ((caddr_t)mfrp + off);
32526f3deacStedu 
32626f3deacStedu 	LIST_INIT(&ip->i_valist);
32726f3deacStedu 
32826f3deacStedu 	while (ap->a_hdr.a_type != -1) {
3298fc6378aSjsing 		error = ntfs_attrtontvattr(ntmp, &vap, ap);
33026f3deacStedu 		if (error)
33126f3deacStedu 			break;
3328fc6378aSjsing 		vap->va_ip = ip;
33326f3deacStedu 
3348fc6378aSjsing 		LIST_INSERT_HEAD(&ip->i_valist, vap, va_list);
33526f3deacStedu 
33626f3deacStedu 		off += ap->a_hdr.reclen;
33726f3deacStedu 		ap = (struct attr *) ((caddr_t)mfrp + off);
33826f3deacStedu 	}
33926f3deacStedu 	if (error) {
34026f3deacStedu 		printf("ntfs_loadntnode: failed to load attr ino: %d\n",
34126f3deacStedu 		       ip->i_number);
34226f3deacStedu 		goto out;
34326f3deacStedu 	}
34426f3deacStedu 
34526f3deacStedu 	ip->i_mainrec = mfrp->fr_mainrec;
34626f3deacStedu 	ip->i_nlink = mfrp->fr_nlink;
34726f3deacStedu 	ip->i_frflag = mfrp->fr_flags;
34826f3deacStedu 
34926f3deacStedu 	ip->i_flag |= IN_LOADED;
35026f3deacStedu 
3518fc6378aSjsing 	/* Add to loaded list. */
3528fc6378aSjsing 	TAILQ_INSERT_HEAD(&ntmp->ntm_ntnodeq, ip, i_loaded);
3538fc6378aSjsing 	ntmp->ntm_ntnodes++;
3548fc6378aSjsing 
35526f3deacStedu out:
356825c4e6aStedu 	free(mfrp, M_TEMP, 0);
35726f3deacStedu 	return (error);
35826f3deacStedu }
35926f3deacStedu 
36026f3deacStedu /*
36126f3deacStedu  * Routine locks ntnode and increase usecount, just opposite of
36226f3deacStedu  * ntfs_ntput().
36326f3deacStedu  */
36426f3deacStedu int
365db7aa982Smpi ntfs_ntget(struct ntnode *ip)
36626f3deacStedu {
367f2997212Sjsing 	DPRINTF("ntfs_ntget: get ntnode %u: %p, usecount: %d\n",
368a1ea65c6Sjsing 	    ip->i_number, ip, ip->i_usecount);
36926f3deacStedu 
37026f3deacStedu 	ip->i_usecount++;
3712752fedbSpedro 
372c74ceabfSoga 	rw_enter_write(&ip->i_lock);
37326f3deacStedu 
37426f3deacStedu 	return 0;
37526f3deacStedu }
37626f3deacStedu 
37726f3deacStedu /*
37826f3deacStedu  * Routine search ntnode in hash, if found: lock, inc usecount and return.
37926f3deacStedu  * If not in hash allocate structure for ntnode, prefill it, lock,
38026f3deacStedu  * inc count and return.
38126f3deacStedu  *
38226f3deacStedu  * ntnode returned locked
38326f3deacStedu  */
38426f3deacStedu int
385db7aa982Smpi ntfs_ntlookup(struct ntfsmount *ntmp, ntfsino_t ino, struct ntnode **ipp)
38626f3deacStedu {
38726f3deacStedu 	struct ntnode  *ip;
38826f3deacStedu 
389f2997212Sjsing 	DPRINTF("ntfs_ntlookup: looking for ntnode %u\n", ino);
39026f3deacStedu 
391*f702838aSclaudio  retry:
39226f3deacStedu 	if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
393db7aa982Smpi 		ntfs_ntget(ip);
394f2997212Sjsing 		DPRINTF("ntfs_ntlookup: ntnode %u: %p, usecount: %d\n",
395a1ea65c6Sjsing 		    ino, ip, ip->i_usecount);
39626f3deacStedu 		*ipp = ip;
39726f3deacStedu 		return (0);
39826f3deacStedu 	}
39926f3deacStedu 
400c86f0003Skrw 	ip = malloc(sizeof(*ip), M_NTFSNTNODE, M_WAITOK | M_ZERO);
401f2997212Sjsing 	DDPRINTF("ntfs_ntlookup: allocating ntnode: %u: %p\n", ino, ip);
40226f3deacStedu 
40326f3deacStedu 	/* Generic initialization */
40426f3deacStedu 	ip->i_devvp = ntmp->ntm_devvp;
40526f3deacStedu 	ip->i_dev = ntmp->ntm_dev;
40626f3deacStedu 	ip->i_number = ino;
40726f3deacStedu 	ip->i_mp = ntmp;
40826f3deacStedu 
40926f3deacStedu 	LIST_INIT(&ip->i_fnlist);
410*f702838aSclaudio 	LIST_INIT(&ip->i_valist);
411627b2c48Sthib 	vref(ip->i_devvp);
41226f3deacStedu 
41326f3deacStedu 	/* init lock and lock the newborn ntnode */
414c74ceabfSoga 	rw_init(&ip->i_lock, "ntnode");
415db7aa982Smpi 	ntfs_ntget(ip);
41626f3deacStedu 
417*f702838aSclaudio 	if (ntfs_nthashins(ip) != 0) {
418*f702838aSclaudio 		ntfs_ntput(ip);
419*f702838aSclaudio 		goto retry;
420*f702838aSclaudio 	}
42126f3deacStedu 
42226f3deacStedu 	*ipp = ip;
42326f3deacStedu 
424f2997212Sjsing 	DPRINTF("ntfs_ntlookup: ntnode %u: %p, usecount: %d\n",
425a1ea65c6Sjsing 	    ino, ip, ip->i_usecount);
42626f3deacStedu 
42726f3deacStedu 	return (0);
42826f3deacStedu }
42926f3deacStedu 
43026f3deacStedu /*
43126f3deacStedu  * Decrement usecount of ntnode and unlock it, if usecount reach zero,
43226f3deacStedu  * deallocate ntnode.
43326f3deacStedu  *
43426f3deacStedu  * ntnode should be locked on entry, and unlocked on return.
43526f3deacStedu  */
43626f3deacStedu void
437db7aa982Smpi ntfs_ntput(struct ntnode *ip)
43826f3deacStedu {
4398fc6378aSjsing 	struct ntfsmount *ntmp = ip->i_mp;
44026f3deacStedu 	struct ntvattr *vap;
44126f3deacStedu 
442f2997212Sjsing 	DPRINTF("ntfs_ntput: rele ntnode %u: %p, usecount: %d\n",
443a1ea65c6Sjsing 	    ip->i_number, ip, ip->i_usecount);
44426f3deacStedu 
44526f3deacStedu 	ip->i_usecount--;
44626f3deacStedu 
44726f3deacStedu #ifdef DIAGNOSTIC
44826f3deacStedu 	if (ip->i_usecount < 0) {
44926f3deacStedu 		panic("ntfs_ntput: ino: %d usecount: %d ",
45026f3deacStedu 		      ip->i_number,ip->i_usecount);
45126f3deacStedu 	}
45226f3deacStedu #endif
45326f3deacStedu 
4549b32ea3cSpat 	if (ip->i_usecount > 0) {
455c74ceabfSoga 		rw_exit_write(&ip->i_lock);
4569b32ea3cSpat 		return;
4579b32ea3cSpat 	}
45826f3deacStedu 
459f2997212Sjsing 	DPRINTF("ntfs_ntput: deallocating ntnode: %u\n", ip->i_number);
46026f3deacStedu 
4619b32ea3cSpat 	if (LIST_FIRST(&ip->i_fnlist))
46226f3deacStedu 		panic("ntfs_ntput: ntnode has fnodes");
46326f3deacStedu 
46426f3deacStedu 	ntfs_nthashrem(ip);
46526f3deacStedu 
4668fc6378aSjsing 	/* Remove from loaded list. */
4678fc6378aSjsing 	if (ip->i_flag & IN_LOADED) {
4688fc6378aSjsing 		TAILQ_REMOVE(&ntmp->ntm_ntnodeq, ip, i_loaded);
4698fc6378aSjsing 		ntmp->ntm_ntnodes--;
4708fc6378aSjsing 	}
4718fc6378aSjsing 
4729b32ea3cSpat 	while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
47326f3deacStedu 		LIST_REMOVE(vap, va_list);
47426f3deacStedu 		ntfs_freentvattr(vap);
47526f3deacStedu 	}
4769b32ea3cSpat 
4779b32ea3cSpat 	vrele(ip->i_devvp);
478825c4e6aStedu 	free(ip, M_NTFSNTNODE, 0);
47926f3deacStedu }
48026f3deacStedu 
48126f3deacStedu /*
48226f3deacStedu  * increment usecount of ntnode
48326f3deacStedu  */
48426f3deacStedu void
4851cc0505dSjsing ntfs_ntref(struct ntnode *ip)
48626f3deacStedu {
48726f3deacStedu 	ip->i_usecount++;
48826f3deacStedu 
489f2997212Sjsing 	DPRINTF("ntfs_ntref: ino %u, usecount: %d\n",
490a1ea65c6Sjsing 	    ip->i_number, ip->i_usecount);
49126f3deacStedu }
49226f3deacStedu 
49326f3deacStedu /*
49426f3deacStedu  * Decrement usecount of ntnode.
49526f3deacStedu  */
49626f3deacStedu void
4971cc0505dSjsing ntfs_ntrele(struct ntnode *ip)
49826f3deacStedu {
499f2997212Sjsing 	DPRINTF("ntfs_ntrele: rele ntnode %u: %p, usecount: %d\n",
500a1ea65c6Sjsing 	    ip->i_number, ip, ip->i_usecount);
50126f3deacStedu 
50226f3deacStedu 	ip->i_usecount--;
50326f3deacStedu 
50426f3deacStedu 	if (ip->i_usecount < 0)
50526f3deacStedu 		panic("ntfs_ntrele: ino: %d usecount: %d ",
50626f3deacStedu 		      ip->i_number,ip->i_usecount);
50726f3deacStedu }
50826f3deacStedu 
50926f3deacStedu /*
51026f3deacStedu  * Deallocate all memory allocated for ntvattr
51126f3deacStedu  */
51226f3deacStedu void
5131cc0505dSjsing ntfs_freentvattr(struct ntvattr *vap)
51426f3deacStedu {
51526f3deacStedu 	if (vap->va_flag & NTFS_AF_INRUN) {
51626f3deacStedu 		if (vap->va_vruncn)
517825c4e6aStedu 			free(vap->va_vruncn, M_NTFSRUN, 0);
51826f3deacStedu 		if (vap->va_vruncl)
519825c4e6aStedu 			free(vap->va_vruncl, M_NTFSRUN, 0);
52026f3deacStedu 	} else {
52126f3deacStedu 		if (vap->va_datap)
522825c4e6aStedu 			free(vap->va_datap, M_NTFSRDATA, 0);
52326f3deacStedu 	}
524825c4e6aStedu 	free(vap, M_NTFSNTVATTR, 0);
52526f3deacStedu }
52626f3deacStedu 
52726f3deacStedu /*
52826f3deacStedu  * Convert disk image of attribute into ntvattr structure,
52926f3deacStedu  * runs are expanded also.
53026f3deacStedu  */
53126f3deacStedu int
5321cc0505dSjsing ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
53326f3deacStedu     struct attr *rap)
53426f3deacStedu {
53526f3deacStedu 	int             error, i;
53626f3deacStedu 	struct ntvattr *vap;
53726f3deacStedu 
53826f3deacStedu 	error = 0;
53926f3deacStedu 	*rvapp = NULL;
54026f3deacStedu 
541c86f0003Skrw 	vap = malloc(sizeof(*vap), M_NTFSNTVATTR, M_WAITOK | M_ZERO);
54226f3deacStedu 	vap->va_ip = NULL;
54326f3deacStedu 	vap->va_flag = rap->a_hdr.a_flag;
54426f3deacStedu 	vap->va_type = rap->a_hdr.a_type;
54526f3deacStedu 	vap->va_compression = rap->a_hdr.a_compression;
54626f3deacStedu 	vap->va_index = rap->a_hdr.a_index;
54726f3deacStedu 
548f2997212Sjsing 	DDPRINTF("type: 0x%x, index: %u", vap->va_type, vap->va_index);
54926f3deacStedu 
55026f3deacStedu 	vap->va_namelen = rap->a_hdr.a_namelen;
55126f3deacStedu 	if (rap->a_hdr.a_namelen) {
55226f3deacStedu 		wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
553a1ea65c6Sjsing 		DDPRINTF(", name:[");
55426f3deacStedu 		for (i = 0; i < vap->va_namelen; i++) {
55526f3deacStedu 			vap->va_name[i] = unp[i];
556a1ea65c6Sjsing 			DDPRINTF("%c", vap->va_name[i]);
55726f3deacStedu 		}
558a1ea65c6Sjsing 		DDPRINTF("]");
55926f3deacStedu 	}
56026f3deacStedu 	if (vap->va_flag & NTFS_AF_INRUN) {
561a1ea65c6Sjsing 		DDPRINTF(", nonres.");
56226f3deacStedu 		vap->va_datalen = rap->a_nr.a_datalen;
56326f3deacStedu 		vap->va_allocated = rap->a_nr.a_allocated;
56426f3deacStedu 		vap->va_vcnstart = rap->a_nr.a_vcnstart;
56526f3deacStedu 		vap->va_vcnend = rap->a_nr.a_vcnend;
56626f3deacStedu 		vap->va_compressalg = rap->a_nr.a_compressalg;
56726f3deacStedu 		error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
56826f3deacStedu 				       &(vap->va_vruncnt),
56926f3deacStedu 				       (caddr_t) rap + rap->a_nr.a_dataoff);
57026f3deacStedu 	} else {
57126f3deacStedu 		vap->va_compressalg = 0;
572a1ea65c6Sjsing 		DDPRINTF(", res.");
57326f3deacStedu 		vap->va_datalen = rap->a_r.a_datalen;
57426f3deacStedu 		vap->va_allocated = rap->a_r.a_datalen;
57526f3deacStedu 		vap->va_vcnstart = 0;
57626f3deacStedu 		vap->va_vcnend = ntfs_btocn(vap->va_allocated);
577c6c302e5Stedu 		vap->va_datap = malloc(vap->va_datalen, M_NTFSRDATA, M_WAITOK);
57826f3deacStedu 		memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
57926f3deacStedu 		       rap->a_r.a_datalen);
58026f3deacStedu 	}
5815adbeedcSjsing 	DDPRINTF(", len: %llu", vap->va_datalen);
58226f3deacStedu 
58326f3deacStedu 	if (error)
584825c4e6aStedu 		free(vap, M_NTFSNTVATTR, 0);
58526f3deacStedu 	else
58626f3deacStedu 		*rvapp = vap;
58726f3deacStedu 
588a1ea65c6Sjsing 	DDPRINTF("\n");
58926f3deacStedu 
59026f3deacStedu 	return (error);
59126f3deacStedu }
59226f3deacStedu 
59326f3deacStedu /*
59426f3deacStedu  * Expand run into more utilizable and more memory eating format.
59526f3deacStedu  */
59626f3deacStedu int
5971cc0505dSjsing ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
59826f3deacStedu {
59926f3deacStedu 	u_int32_t       off;
60026f3deacStedu 	u_int32_t       sz, i;
60126f3deacStedu 	cn_t           *cn;
60226f3deacStedu 	cn_t           *cl;
60326f3deacStedu 	u_long		cnt;
60426f3deacStedu 	cn_t		prev;
60526f3deacStedu 	cn_t		tmp;
60626f3deacStedu 
60726f3deacStedu 	off = 0;
60826f3deacStedu 	cnt = 0;
60926f3deacStedu 	while (run[off]) {
61026f3deacStedu 		off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
61126f3deacStedu 		cnt++;
61226f3deacStedu 	}
613540e394aSdoug 	cn = mallocarray(cnt, sizeof(cn_t), M_NTFSRUN, M_WAITOK);
614540e394aSdoug 	cl = mallocarray(cnt, sizeof(cn_t), M_NTFSRUN, M_WAITOK);
61526f3deacStedu 
61626f3deacStedu 	off = 0;
61726f3deacStedu 	cnt = 0;
61826f3deacStedu 	prev = 0;
61926f3deacStedu 	while (run[off]) {
62026f3deacStedu 
62126f3deacStedu 		sz = run[off++];
62226f3deacStedu 		cl[cnt] = 0;
62326f3deacStedu 
62426f3deacStedu 		for (i = 0; i < (sz & 0xF); i++)
62526f3deacStedu 			cl[cnt] += (u_int32_t) run[off++] << (i << 3);
62626f3deacStedu 
62726f3deacStedu 		sz >>= 4;
62826f3deacStedu 		if (run[off + sz - 1] & 0x80) {
62926f3deacStedu 			tmp = ((u_int64_t) - 1) << (sz << 3);
63026f3deacStedu 			for (i = 0; i < sz; i++)
63126f3deacStedu 				tmp |= (u_int64_t) run[off++] << (i << 3);
63226f3deacStedu 		} else {
63326f3deacStedu 			tmp = 0;
63426f3deacStedu 			for (i = 0; i < sz; i++)
63526f3deacStedu 				tmp |= (u_int64_t) run[off++] << (i << 3);
63626f3deacStedu 		}
63726f3deacStedu 		if (tmp)
63826f3deacStedu 			prev = cn[cnt] = prev + tmp;
63926f3deacStedu 		else
64026f3deacStedu 			cn[cnt] = tmp;
64126f3deacStedu 
64226f3deacStedu 		cnt++;
64326f3deacStedu 	}
64426f3deacStedu 	*rcnp = cn;
64526f3deacStedu 	*rclp = cl;
64626f3deacStedu 	*rcntp = cnt;
64726f3deacStedu 	return (0);
64826f3deacStedu }
64926f3deacStedu 
65026f3deacStedu /*
65126f3deacStedu  * Compare unicode and ascii string case insens.
65226f3deacStedu  */
65305c5dae5Sjsing int
6541cc0505dSjsing ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
6551cc0505dSjsing     const char *astr, size_t astrlen)
65626f3deacStedu {
65726f3deacStedu 	size_t  i;
65826f3deacStedu 	int             res;
65926f3deacStedu 	const char *astrend = astr + astrlen;
66026f3deacStedu 
66126f3deacStedu 	for (i = 0; i < ustrlen && astr < astrend; i++) {
66226f3deacStedu 		res = (*ntmp->ntm_wcmp)(NTFS_TOUPPER(ustr[i]),
66326f3deacStedu 				NTFS_TOUPPER((*ntmp->ntm_wget)(&astr)) );
66426f3deacStedu 		if (res)
66526f3deacStedu 			return res;
66626f3deacStedu 	}
66726f3deacStedu 
66826f3deacStedu 	if (i == ustrlen && astr == astrend)
66926f3deacStedu 		return 0;
67026f3deacStedu 	else if (i == ustrlen)
67126f3deacStedu 		return -1;
67226f3deacStedu 	else
67326f3deacStedu 		return 1;
67426f3deacStedu }
67526f3deacStedu 
67626f3deacStedu /*
67726f3deacStedu  * Compare unicode and ascii string case sens.
67826f3deacStedu  */
67905c5dae5Sjsing int
6801cc0505dSjsing ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
6811cc0505dSjsing     const char *astr, size_t astrlen)
68226f3deacStedu {
68326f3deacStedu 	size_t             i;
68426f3deacStedu 	int             res;
68526f3deacStedu 	const char *astrend = astr + astrlen;
68626f3deacStedu 
68726f3deacStedu 	for (i = 0; (i < ustrlen) && (astr < astrend); i++) {
68826f3deacStedu 		res = (*ntmp->ntm_wcmp)(ustr[i], (*ntmp->ntm_wget)(&astr));
68926f3deacStedu 		if (res)
69026f3deacStedu 			return res;
69126f3deacStedu 	}
69226f3deacStedu 
69326f3deacStedu 	if (i == ustrlen && astr == astrend)
69426f3deacStedu 		return 0;
69526f3deacStedu 	else if (i == ustrlen)
69626f3deacStedu 		return -1;
69726f3deacStedu 	else
69826f3deacStedu 		return 1;
69926f3deacStedu }
70026f3deacStedu 
70126f3deacStedu /*
70226f3deacStedu  * Search fnode in ntnode, if not found allocate and preinitialize.
70326f3deacStedu  *
70426f3deacStedu  * ntnode should be locked on entry.
70526f3deacStedu  */
70626f3deacStedu int
7071cc0505dSjsing ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
7081cc0505dSjsing     char *attrname, struct fnode **fpp)
70926f3deacStedu {
71026f3deacStedu 	struct fnode *fp;
71126f3deacStedu 
712f2997212Sjsing 	DPRINTF("ntfs_fget: ino: %u, attrtype: 0x%x, attrname: %s\n",
713a1ea65c6Sjsing 	    ip->i_number, attrtype, attrname ? attrname : "");
71426f3deacStedu 	*fpp = NULL;
7151573508eSmiod 	LIST_FOREACH(fp, &ip->i_fnlist, f_fnlist) {
716f2997212Sjsing 		DPRINTF("ntfs_fget: fnode: attrtype: %u, attrname: %s\n",
717a1ea65c6Sjsing 		    fp->f_attrtype, fp->f_attrname ? fp->f_attrname : "");
71826f3deacStedu 
71926f3deacStedu 		if ((attrtype == fp->f_attrtype) &&
72026f3deacStedu 		    ((!attrname && !fp->f_attrname) ||
72126f3deacStedu 		     (attrname && fp->f_attrname &&
72226f3deacStedu 		      !strcmp(attrname,fp->f_attrname)))){
723a1ea65c6Sjsing 			DPRINTF("ntfs_fget: found existed: %p\n", fp);
72426f3deacStedu 			*fpp = fp;
72526f3deacStedu 		}
72626f3deacStedu 	}
72726f3deacStedu 
72826f3deacStedu 	if (*fpp)
72926f3deacStedu 		return (0);
73026f3deacStedu 
731c86f0003Skrw 	fp = malloc(sizeof(*fp), M_NTFSFNODE, M_WAITOK | M_ZERO);
732a1ea65c6Sjsing 	DPRINTF("ntfs_fget: allocating fnode: %p\n", fp);
73326f3deacStedu 
73426f3deacStedu 	fp->f_ip = ip;
73526f3deacStedu 	fp->f_attrname = attrname;
73626f3deacStedu 	if (fp->f_attrname) fp->f_flag |= FN_AATTRNAME;
73726f3deacStedu 	fp->f_attrtype = attrtype;
73826f3deacStedu 
73926f3deacStedu 	ntfs_ntref(ip);
74026f3deacStedu 
74126f3deacStedu 	LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
74226f3deacStedu 
74326f3deacStedu 	*fpp = fp;
74426f3deacStedu 
74526f3deacStedu 	return (0);
74626f3deacStedu }
74726f3deacStedu 
74826f3deacStedu /*
74926f3deacStedu  * Deallocate fnode, remove it from ntnode's fnode list.
75026f3deacStedu  *
75126f3deacStedu  * ntnode should be locked.
75226f3deacStedu  */
75326f3deacStedu void
7541cc0505dSjsing ntfs_frele(struct fnode *fp)
75526f3deacStedu {
75626f3deacStedu 	struct ntnode *ip = FTONT(fp);
75726f3deacStedu 
758f2997212Sjsing 	DPRINTF("ntfs_frele: fnode: %p for %u: %p\n", fp, ip->i_number, ip);
75926f3deacStedu 
760a1ea65c6Sjsing 	DPRINTF("ntfs_frele: deallocating fnode\n");
76126f3deacStedu 	LIST_REMOVE(fp,f_fnlist);
76226f3deacStedu 	if (fp->f_flag & FN_AATTRNAME)
763825c4e6aStedu 		free(fp->f_attrname, M_TEMP, 0);
76426f3deacStedu 	if (fp->f_dirblbuf)
765825c4e6aStedu 		free(fp->f_dirblbuf, M_NTFSDIR, 0);
766825c4e6aStedu 	free(fp, M_NTFSFNODE, 0);
76726f3deacStedu 	ntfs_ntrele(ip);
76826f3deacStedu }
76926f3deacStedu 
77026f3deacStedu /*
77126f3deacStedu  * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
77226f3deacStedu  * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
77326f3deacStedu  * If $ATTR_TYPE not specified, ATTR_A_DATA assumed.
77426f3deacStedu  */
77505c5dae5Sjsing int
7761cc0505dSjsing ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
7771cc0505dSjsing     int *attrtype, char **attrname)
77826f3deacStedu {
77926f3deacStedu 	const char *sys;
78026f3deacStedu 	size_t syslen, i;
78126f3deacStedu 	struct ntvattrdef *adp;
78226f3deacStedu 
78326f3deacStedu 	if (namelen == 0)
78426f3deacStedu 		return (0);
78526f3deacStedu 
78626f3deacStedu 	if (name[0] == '$') {
78726f3deacStedu 		sys = name;
78826f3deacStedu 		for (syslen = 0; syslen < namelen; syslen++) {
78926f3deacStedu 			if(sys[syslen] == ':') {
79026f3deacStedu 				name++;
79126f3deacStedu 				namelen--;
79226f3deacStedu 				break;
79326f3deacStedu 			}
79426f3deacStedu 		}
79526f3deacStedu 		name += syslen;
79626f3deacStedu 		namelen -= syslen;
79726f3deacStedu 
79826f3deacStedu 		adp = ntmp->ntm_ad;
79926f3deacStedu 		for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
80026f3deacStedu 			if (syslen != adp->ad_namelen ||
80126f3deacStedu 			    strncmp(sys, adp->ad_name, syslen) != 0)
80226f3deacStedu 				continue;
80326f3deacStedu 
80426f3deacStedu 			*attrtype = adp->ad_type;
80526f3deacStedu 			goto out;
80626f3deacStedu 		}
80726f3deacStedu 		return (ENOENT);
80826f3deacStedu 	}
80926f3deacStedu 
81026f3deacStedu     out:
81126f3deacStedu 	if (namelen) {
812c6c302e5Stedu 		*attrname = malloc(namelen + 1, M_TEMP, M_WAITOK);
813c6c302e5Stedu 		memcpy(*attrname, name, namelen);
81426f3deacStedu 		(*attrname)[namelen] = '\0';
81526f3deacStedu 		*attrtype = NTFS_A_DATA;
81626f3deacStedu 	}
81726f3deacStedu 
81826f3deacStedu 	return (0);
81926f3deacStedu }
82026f3deacStedu 
82126f3deacStedu /*
8221cc0505dSjsing  * Lookup specified node for filename, matching cnp, return fnode filled.
82326f3deacStedu  */
82426f3deacStedu int
8251cc0505dSjsing ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
826db7aa982Smpi     struct componentname *cnp, struct vnode **vpp)
82726f3deacStedu {
82826f3deacStedu 	struct fnode   *fp = VTOF(vp);
82926f3deacStedu 	struct ntnode  *ip = FTONT(fp);
8306f6d0059Sjsing 	struct ntvattr *vap = NULL;	/* Root attribute */
83126f3deacStedu 	cn_t            cn = 0;	/* VCN in current attribute */
8326f6d0059Sjsing 	caddr_t         rdbuf = NULL;	/* Buffer to read directory's blocks */
83326f3deacStedu 	u_int32_t       blsize;
83426f3deacStedu 	u_int32_t       rdsize;	/* Length of data to read from current block */
83526f3deacStedu 	struct attr_indexentry *iep;
83626f3deacStedu 	int             error, res, anamelen, fnamelen;
83726f3deacStedu 	const char     *fname,*aname;
83826f3deacStedu 	u_int32_t       aoff;
83926f3deacStedu 	int attrtype = NTFS_A_DATA;
84026f3deacStedu 	char *attrname = NULL;
84126f3deacStedu 	struct fnode   *nfp;
84226f3deacStedu 	struct vnode   *nvp;
84326f3deacStedu 	enum vtype	f_type;
84426f3deacStedu 	int fullscan = 0;
84526f3deacStedu 	struct ntfs_lookup_ctx *lookup_ctx = NULL, *tctx;
84626f3deacStedu 
847db7aa982Smpi 	error = ntfs_ntget(ip);
84826f3deacStedu 	if (error)
84926f3deacStedu 		return (error);
85026f3deacStedu 
85126f3deacStedu 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
8526f6d0059Sjsing 	if (error || (vap->va_flag & NTFS_AF_INRUN)) {
8536f6d0059Sjsing 		error = ENOTDIR;
8546f6d0059Sjsing 		goto fail;
8556f6d0059Sjsing 	}
85626f3deacStedu 
85726f3deacStedu 	/*
85826f3deacStedu 	 * Divide file name into: foofilefoofilefoofile[:attrspec]
85926f3deacStedu 	 * Store like this:       fname:fnamelen       [aname:anamelen]
86026f3deacStedu 	 */
86126f3deacStedu 	fname = cnp->cn_nameptr;
86226f3deacStedu 	aname = NULL;
86326f3deacStedu 	anamelen = 0;
86426f3deacStedu 	for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
86526f3deacStedu 		if(fname[fnamelen] == ':') {
86626f3deacStedu 			aname = fname + fnamelen + 1;
86726f3deacStedu 			anamelen = cnp->cn_namelen - fnamelen - 1;
868a1ea65c6Sjsing 			DPRINTF("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
869a1ea65c6Sjsing 			    fname, fnamelen, aname, anamelen);
87026f3deacStedu 			break;
87126f3deacStedu 		}
87226f3deacStedu 
87326f3deacStedu 	blsize = vap->va_a_iroot->ir_size;
874f2997212Sjsing 	DPRINTF("ntfs_ntlookupfile: blksz: %u\n", blsize);
87526f3deacStedu 
876c6c302e5Stedu 	rdbuf = malloc(blsize, M_TEMP, M_WAITOK);
87726f3deacStedu 
87826f3deacStedu     loop:
87926f3deacStedu 	rdsize = vap->va_datalen;
880f2997212Sjsing 	DPRINTF("ntfs_ntlookupfile: rdsz: %u\n", rdsize);
88126f3deacStedu 
88226f3deacStedu 	error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
88326f3deacStedu 			       0, rdsize, rdbuf, NULL);
88426f3deacStedu 	if (error)
88526f3deacStedu 		goto fail;
88626f3deacStedu 
88726f3deacStedu 	aoff = sizeof(struct attr_indexroot);
88826f3deacStedu 
88926f3deacStedu 	do {
89026f3deacStedu 		iep = (struct attr_indexentry *) (rdbuf + aoff);
89126f3deacStedu 
89226f3deacStedu 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
89326f3deacStedu 			aoff += iep->reclen,
89426f3deacStedu 			iep = (struct attr_indexentry *) (rdbuf + aoff))
89526f3deacStedu 		{
896f2997212Sjsing 			DDPRINTF("scan: %u, %u\n", iep->ie_number,
897f2997212Sjsing 			    iep->ie_fnametype);
89826f3deacStedu 
89926f3deacStedu 			/* check the name - the case-insensitive check
90026f3deacStedu 			 * has to come first, to break from this for loop
90126f3deacStedu 			 * if needed, so we can dive correctly */
90226f3deacStedu 			res = ntfs_uastricmp(ntmp, iep->ie_fname,
90326f3deacStedu 				iep->ie_fnamelen, fname, fnamelen);
90426f3deacStedu 			if (!fullscan) {
90526f3deacStedu 				if (res > 0) break;
90626f3deacStedu 				if (res < 0) continue;
90726f3deacStedu 			}
90826f3deacStedu 
90926f3deacStedu 			if (iep->ie_fnametype == 0 ||
91026f3deacStedu 			    !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
91126f3deacStedu 			{
91226f3deacStedu 				res = ntfs_uastrcmp(ntmp, iep->ie_fname,
91326f3deacStedu 					iep->ie_fnamelen, fname, fnamelen);
91426f3deacStedu 				if (res != 0 && !fullscan) continue;
91526f3deacStedu 			}
91626f3deacStedu 
91726f3deacStedu 			/* if we perform full scan, the file does not match
91826f3deacStedu 			 * and this is subnode, dive */
91926f3deacStedu 			if (fullscan && res != 0) {
92026f3deacStedu 			    if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
921e8821f89Shshoexer 				tctx = malloc(sizeof(struct ntfs_lookup_ctx),
92226f3deacStedu 					M_TEMP, M_WAITOK);
92326f3deacStedu 				tctx->aoff	= aoff + iep->reclen;
92426f3deacStedu 				tctx->rdsize	= rdsize;
92526f3deacStedu 				tctx->cn	= cn;
92626f3deacStedu 				tctx->prev	= lookup_ctx;
92726f3deacStedu 				lookup_ctx = tctx;
92826f3deacStedu 				break;
92926f3deacStedu 			    } else
93026f3deacStedu 				continue;
93126f3deacStedu 			}
93226f3deacStedu 
93326f3deacStedu 			if (aname) {
93426f3deacStedu 				error = ntfs_ntlookupattr(ntmp,
93526f3deacStedu 					aname, anamelen,
93626f3deacStedu 					&attrtype, &attrname);
93726f3deacStedu 				if (error)
93826f3deacStedu 					goto fail;
93926f3deacStedu 			}
94026f3deacStedu 
94126f3deacStedu 			/* Check if we've found ourselves */
94226f3deacStedu 			if ((iep->ie_number == ip->i_number) &&
94326f3deacStedu 			    (attrtype == fp->f_attrtype) &&
94426f3deacStedu 			    ((!attrname && !fp->f_attrname) ||
94526f3deacStedu 			     (attrname && fp->f_attrname &&
94626f3deacStedu 			      !strcmp(attrname, fp->f_attrname))))
94726f3deacStedu 			{
948627b2c48Sthib 				vref(vp);
94926f3deacStedu 				*vpp = vp;
95026f3deacStedu 				error = 0;
95126f3deacStedu 				goto fail;
95226f3deacStedu 			}
95326f3deacStedu 
95426f3deacStedu 			/* free the buffer returned by ntfs_ntlookupattr() */
95526f3deacStedu 			if (attrname) {
956825c4e6aStedu 				free(attrname, M_TEMP, 0);
95726f3deacStedu 				attrname = NULL;
95826f3deacStedu 			}
95926f3deacStedu 
96026f3deacStedu 			/* vget node, but don't load it */
96126f3deacStedu 			error = ntfs_vgetex(ntmp->ntm_mountp,
96226f3deacStedu 				   iep->ie_number, attrtype, attrname,
96326f3deacStedu 				   LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
964db7aa982Smpi 				   &nvp);
96526f3deacStedu 			if (error)
96626f3deacStedu 				goto fail;
96726f3deacStedu 
96826f3deacStedu 			nfp = VTOF(nvp);
96926f3deacStedu 
97026f3deacStedu 			if (nfp->f_flag & FN_VALID) {
97126f3deacStedu 				*vpp = nvp;
97226f3deacStedu 				goto fail;
97326f3deacStedu 			}
97426f3deacStedu 
97526f3deacStedu 			nfp->f_fflag = iep->ie_fflag;
97626f3deacStedu 			nfp->f_pnumber = iep->ie_fpnumber;
97726f3deacStedu 			nfp->f_times = iep->ie_ftimes;
97826f3deacStedu 
97926f3deacStedu 			if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
98026f3deacStedu 			   (nfp->f_attrtype == NTFS_A_DATA) &&
98126f3deacStedu 			   (nfp->f_attrname == NULL))
98226f3deacStedu 				f_type = VDIR;
98326f3deacStedu 			else
98426f3deacStedu 				f_type = VREG;
98526f3deacStedu 
98626f3deacStedu 			nvp->v_type = f_type;
98726f3deacStedu 
98826f3deacStedu 			if ((nfp->f_attrtype == NTFS_A_DATA) &&
98926f3deacStedu 			    (nfp->f_attrname == NULL))
99026f3deacStedu 			{
99126f3deacStedu 				/* Opening default attribute */
99226f3deacStedu 				nfp->f_size = iep->ie_fsize;
99326f3deacStedu 				nfp->f_allocated = iep->ie_fallocated;
99426f3deacStedu 				nfp->f_flag |= FN_PRELOADED;
99526f3deacStedu 			} else {
99626f3deacStedu 				error = ntfs_filesize(ntmp, nfp,
99726f3deacStedu 					    &nfp->f_size, &nfp->f_allocated);
99826f3deacStedu 				if (error) {
99926f3deacStedu 					vput(nvp);
100026f3deacStedu 					goto fail;
100126f3deacStedu 				}
100226f3deacStedu 			}
100326f3deacStedu 
100426f3deacStedu 			nfp->f_flag &= ~FN_VALID;
100526f3deacStedu 			*vpp = nvp;
100626f3deacStedu 			goto fail;
100726f3deacStedu 		}
100826f3deacStedu 
100926f3deacStedu 		/* Dive if possible */
101026f3deacStedu 		if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1011a1ea65c6Sjsing 			DPRINTF("ntfs_ntlookupfile: diving\n");
101226f3deacStedu 
101326f3deacStedu 			cn = *(cn_t *) (rdbuf + aoff +
101426f3deacStedu 					iep->reclen - sizeof(cn_t));
101526f3deacStedu 			rdsize = blsize;
101626f3deacStedu 
101726f3deacStedu 			error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
101826f3deacStedu 					ntfs_cntob(cn), rdsize, rdbuf, NULL);
101926f3deacStedu 			if (error)
102026f3deacStedu 				goto fail;
102126f3deacStedu 
102226f3deacStedu 			error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
102326f3deacStedu 						rdbuf, rdsize);
102426f3deacStedu 			if (error)
102526f3deacStedu 				goto fail;
102626f3deacStedu 
102726f3deacStedu 			aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
102826f3deacStedu 				0x18);
102926f3deacStedu 		} else if (fullscan && lookup_ctx) {
103026f3deacStedu 			cn = lookup_ctx->cn;
103126f3deacStedu 			aoff = lookup_ctx->aoff;
103226f3deacStedu 			rdsize = lookup_ctx->rdsize;
103326f3deacStedu 
103426f3deacStedu 			error = ntfs_readattr(ntmp, ip,
103526f3deacStedu 				(cn == 0) ? NTFS_A_INDXROOT : NTFS_A_INDX,
103626f3deacStedu 				"$I30", ntfs_cntob(cn), rdsize, rdbuf, NULL);
103726f3deacStedu 			if (error)
103826f3deacStedu 				goto fail;
103926f3deacStedu 
104026f3deacStedu 			if (cn != 0) {
104126f3deacStedu 				error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
104226f3deacStedu 						rdbuf, rdsize);
104326f3deacStedu 				if (error)
104426f3deacStedu 					goto fail;
104526f3deacStedu 			}
104626f3deacStedu 
104726f3deacStedu 			tctx = lookup_ctx;
104826f3deacStedu 			lookup_ctx = lookup_ctx->prev;
1049825c4e6aStedu 			free(tctx, M_TEMP, 0);
105026f3deacStedu 		} else {
1051a1ea65c6Sjsing 			DPRINTF("ntfs_ntlookupfile: nowhere to dive :-(\n");
105226f3deacStedu 			error = ENOENT;
105326f3deacStedu 			break;
105426f3deacStedu 		}
105526f3deacStedu 	} while (1);
105626f3deacStedu 
1057b0d9a5a6Snatano 	if (error == ENOENT) {
105826f3deacStedu 		/* perform full scan if no entry was found */
1059b0d9a5a6Snatano 		if (!fullscan) {
106026f3deacStedu 			fullscan = 1;
106126f3deacStedu 			cn = 0;		/* need zero, used by lookup_ctx */
106226f3deacStedu 
1063a1ea65c6Sjsing 			DDPRINTF("ntfs_ntlookupfile: fullscan performed for: %.*s\n",
1064f2997212Sjsing 			    (unsigned int)fnamelen, fname);
106526f3deacStedu 			goto loop;
106626f3deacStedu 		}
106726f3deacStedu 
1068b0d9a5a6Snatano 		if ((cnp->cn_flags & ISLASTCN) &&
1069b0d9a5a6Snatano 		    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
1070b0d9a5a6Snatano 			error = EJUSTRETURN;
1071b0d9a5a6Snatano 	}
1072b0d9a5a6Snatano 
1073a1ea65c6Sjsing 	DPRINTF("finish\n");
107426f3deacStedu 
107526f3deacStedu fail:
10766f6d0059Sjsing 	if (vap)
10776f6d0059Sjsing 		ntfs_ntvattrrele(vap);
10786f6d0059Sjsing 	if (rdbuf)
1079825c4e6aStedu 		free(rdbuf, M_TEMP, 0);
108026f3deacStedu 	if (attrname)
1081825c4e6aStedu 		free(attrname, M_TEMP, 0);
108226f3deacStedu 	if (lookup_ctx) {
108326f3deacStedu 		while(lookup_ctx) {
108426f3deacStedu 			tctx = lookup_ctx;
108526f3deacStedu 			lookup_ctx = lookup_ctx->prev;
1086825c4e6aStedu 			free(tctx, M_TEMP, 0);
108726f3deacStedu 		}
108826f3deacStedu 	}
1089db7aa982Smpi 	ntfs_ntput(ip);
109026f3deacStedu 	return (error);
109126f3deacStedu }
109226f3deacStedu 
109326f3deacStedu /*
109426f3deacStedu  * Check if name type is permitted to show.
109526f3deacStedu  */
109626f3deacStedu int
10971cc0505dSjsing ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
109826f3deacStedu {
109926f3deacStedu 	if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
110026f3deacStedu 		return 1;
110126f3deacStedu 
110226f3deacStedu 	switch (iep->ie_fnametype) {
110326f3deacStedu 	case 2:
1104a1ea65c6Sjsing 		DDPRINTF("ntfs_isnamepermitted: skipped DOS name\n");
110526f3deacStedu 		return 0;
110626f3deacStedu 	case 0: case 1: case 3:
110726f3deacStedu 		return 1;
110826f3deacStedu 	default:
110926f3deacStedu 		printf("ntfs_isnamepermitted: " \
111026f3deacStedu 		       "WARNING! Unknown file name type: %d\n",
111126f3deacStedu 		       iep->ie_fnametype);
111226f3deacStedu 		break;
111326f3deacStedu 	}
111426f3deacStedu 	return 0;
111526f3deacStedu }
111626f3deacStedu 
111726f3deacStedu /*
111826f3deacStedu  * Read ntfs dir like stream of attr_indexentry, not like btree of them.
111926f3deacStedu  * This is done by scanning $BITMAP:$I30 for busy clusters and reading them.
112026f3deacStedu  * Of course $INDEX_ROOT:$I30 is read before. Last read values are stored in
112126f3deacStedu  * fnode, so we can skip toward record number num almost immediately.
112226f3deacStedu  * Anyway this is rather slow routine. The problem is that we don't know
112326f3deacStedu  * how many records are there in $INDEX_ALLOCATION:$I30 block.
112426f3deacStedu  */
112526f3deacStedu int
11261cc0505dSjsing ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp, u_int32_t num,
11271cc0505dSjsing     struct attr_indexentry **riepp, struct proc *p)
112826f3deacStedu {
112926f3deacStedu 	struct ntnode  *ip = FTONT(fp);
113026f3deacStedu 	struct ntvattr *vap = NULL;	/* IndexRoot attribute */
113126f3deacStedu 	struct ntvattr *bmvap = NULL;	/* BitMap attribute */
113226f3deacStedu 	struct ntvattr *iavap = NULL;	/* IndexAllocation attribute */
113326f3deacStedu 	caddr_t         rdbuf;		/* Buffer to read directory's blocks  */
11346978a3abSbrad 	u_int8_t       *bmp = NULL;	/* Bitmap */
113526f3deacStedu 	u_int32_t       blsize;		/* Index allocation size (2048) */
113626f3deacStedu 	u_int32_t       rdsize;		/* Length of data to read */
113726f3deacStedu 	u_int32_t       attrnum;	/* Current attribute type */
113826f3deacStedu 	u_int32_t       cpbl = 1;	/* Clusters per directory block */
113926f3deacStedu 	u_int32_t       blnum;
114026f3deacStedu 	struct attr_indexentry *iep;
114126f3deacStedu 	int             error = ENOENT;
114226f3deacStedu 	u_int32_t       aoff, cnum;
114326f3deacStedu 
1144f2997212Sjsing 	DPRINTF("ntfs_ntreaddir: read ino: %u, num: %u\n", ip->i_number, num);
1145db7aa982Smpi 	error = ntfs_ntget(ip);
114626f3deacStedu 	if (error)
114726f3deacStedu 		return (error);
114826f3deacStedu 
114926f3deacStedu 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
11506f6d0059Sjsing 	if (error) {
11516f6d0059Sjsing 		error = ENOTDIR;
11526f6d0059Sjsing 		goto fail;
11536f6d0059Sjsing 	}
115426f3deacStedu 
115526f3deacStedu 	if (fp->f_dirblbuf == NULL) {
115626f3deacStedu 		fp->f_dirblsz = vap->va_a_iroot->ir_size;
1157c6c302e5Stedu 		fp->f_dirblbuf = malloc(MAX(vap->va_datalen,fp->f_dirblsz),
1158c6c302e5Stedu 		    M_NTFSDIR, M_WAITOK);
115926f3deacStedu 	}
116026f3deacStedu 
116126f3deacStedu 	blsize = fp->f_dirblsz;
116226f3deacStedu 	rdbuf = fp->f_dirblbuf;
116326f3deacStedu 
1164f2997212Sjsing 	DPRINTF("ntfs_ntreaddir: rdbuf: %p, blsize: %u\n", rdbuf, blsize);
116526f3deacStedu 
116626f3deacStedu 	if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
116726f3deacStedu 		error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
116826f3deacStedu 					0, &bmvap);
116926f3deacStedu 		if (error) {
117026f3deacStedu 			error = ENOTDIR;
117126f3deacStedu 			goto fail;
117226f3deacStedu 		}
1173c6c302e5Stedu 		bmp = malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
117426f3deacStedu 		error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
117526f3deacStedu 				       bmvap->va_datalen, bmp, NULL);
117626f3deacStedu 		if (error)
117726f3deacStedu 			goto fail;
117826f3deacStedu 
117926f3deacStedu 		error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
118026f3deacStedu 					0, &iavap);
118126f3deacStedu 		if (error) {
118226f3deacStedu 			error = ENOTDIR;
118326f3deacStedu 			goto fail;
118426f3deacStedu 		}
118526f3deacStedu 		cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
11865adbeedcSjsing 		DPRINTF("ntfs_ntreaddir: indexalloc: %llu, cpbl: %u\n",
1187a1ea65c6Sjsing 		    iavap->va_datalen, cpbl);
118826f3deacStedu 	} else {
1189a1ea65c6Sjsing 		DPRINTF("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n");
119026f3deacStedu 		iavap = bmvap = NULL;
119126f3deacStedu 		bmp = NULL;
119226f3deacStedu 	}
119326f3deacStedu 
119426f3deacStedu 	/* Try use previous values */
119526f3deacStedu 	if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
119626f3deacStedu 		attrnum = fp->f_lastdattr;
119726f3deacStedu 		aoff = fp->f_lastdoff;
119826f3deacStedu 		blnum = fp->f_lastdblnum;
119926f3deacStedu 		cnum = fp->f_lastdnum;
120026f3deacStedu 	} else {
120126f3deacStedu 		attrnum = NTFS_A_INDXROOT;
120226f3deacStedu 		aoff = sizeof(struct attr_indexroot);
120326f3deacStedu 		blnum = 0;
120426f3deacStedu 		cnum = 0;
120526f3deacStedu 	}
120626f3deacStedu 
120726f3deacStedu 	do {
1208f2997212Sjsing 		DPRINTF("ntfs_ntreaddir: scan: 0x%x, %u, %u, %u, %u\n",
1209f2997212Sjsing 		    attrnum, blnum, cnum, num, aoff);
121026f3deacStedu 		rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
121126f3deacStedu 		error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
121226f3deacStedu 				ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
121326f3deacStedu 		if (error)
121426f3deacStedu 			goto fail;
121526f3deacStedu 
121626f3deacStedu 		if (attrnum == NTFS_A_INDX) {
121726f3deacStedu 			error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
121826f3deacStedu 						rdbuf, rdsize);
121926f3deacStedu 			if (error)
122026f3deacStedu 				goto fail;
122126f3deacStedu 		}
122226f3deacStedu 		if (aoff == 0)
122326f3deacStedu 			aoff = (attrnum == NTFS_A_INDX) ?
122426f3deacStedu 				(0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
122526f3deacStedu 				sizeof(struct attr_indexroot);
122626f3deacStedu 
122726f3deacStedu 		iep = (struct attr_indexentry *) (rdbuf + aoff);
122826f3deacStedu 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
122926f3deacStedu 			aoff += iep->reclen,
123026f3deacStedu 			iep = (struct attr_indexentry *) (rdbuf + aoff))
123126f3deacStedu 		{
123226f3deacStedu 			if (!ntfs_isnamepermitted(ntmp, iep)) continue;
123326f3deacStedu 
123426f3deacStedu 			if (cnum >= num) {
123526f3deacStedu 				fp->f_lastdnum = cnum;
123626f3deacStedu 				fp->f_lastdoff = aoff;
123726f3deacStedu 				fp->f_lastdblnum = blnum;
123826f3deacStedu 				fp->f_lastdattr = attrnum;
123926f3deacStedu 
124026f3deacStedu 				*riepp = iep;
124126f3deacStedu 
124226f3deacStedu 				error = 0;
124326f3deacStedu 				goto fail;
124426f3deacStedu 			}
124526f3deacStedu 			cnum++;
124626f3deacStedu 		}
124726f3deacStedu 
124826f3deacStedu 		if (iavap) {
124926f3deacStedu 			if (attrnum == NTFS_A_INDXROOT)
125026f3deacStedu 				blnum = 0;
125126f3deacStedu 			else
125226f3deacStedu 				blnum++;
125326f3deacStedu 
125426f3deacStedu 			while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
12556978a3abSbrad 				if (bmp[blnum >> 3] & (1 << (blnum & 7)))
125626f3deacStedu 					break;
125726f3deacStedu 				blnum++;
125826f3deacStedu 			}
125926f3deacStedu 
126026f3deacStedu 			attrnum = NTFS_A_INDX;
126126f3deacStedu 			aoff = 0;
126226f3deacStedu 			if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
126326f3deacStedu 				break;
1264f2997212Sjsing 			DPRINTF("ntfs_ntreaddir: blnum: %u\n", blnum);
126526f3deacStedu 		}
126626f3deacStedu 	} while (iavap);
126726f3deacStedu 
126826f3deacStedu 	*riepp = NULL;
126926f3deacStedu 	fp->f_lastdnum = 0;
127026f3deacStedu 
127126f3deacStedu fail:
127226f3deacStedu 	if (vap)
127326f3deacStedu 		ntfs_ntvattrrele(vap);
127426f3deacStedu 	if (bmvap)
127526f3deacStedu 		ntfs_ntvattrrele(bmvap);
127626f3deacStedu 	if (iavap)
127726f3deacStedu 		ntfs_ntvattrrele(iavap);
127826f3deacStedu 	if (bmp)
1279825c4e6aStedu 		free(bmp, M_TEMP, 0);
1280db7aa982Smpi 	ntfs_ntput(ip);
12811ad3841aSjasper 
128226f3deacStedu 	return (error);
128326f3deacStedu }
128426f3deacStedu 
128526f3deacStedu /*
128626f3deacStedu  * Convert NTFS times that are in 100 ns units and begins from
128726f3deacStedu  * 1601 Jan 1 into unix times.
128826f3deacStedu  */
128926f3deacStedu struct timespec
12901cc0505dSjsing ntfs_nttimetounix(u_int64_t nt)
129126f3deacStedu {
129226f3deacStedu 	struct timespec t;
129326f3deacStedu 
12940d297f47Sjsg 	/* Windows NT times are in 100 ns and from 1601 Jan 1 */
129526f3deacStedu 	t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
129626f3deacStedu 	t.tv_sec = nt / (1000 * 1000 * 10) -
129726f3deacStedu 		369LL * 365LL * 24LL * 60LL * 60LL -
129826f3deacStedu 		89LL * 1LL * 24LL * 60LL * 60LL;
129926f3deacStedu 	return (t);
130026f3deacStedu }
130126f3deacStedu 
130226f3deacStedu /*
130326f3deacStedu  * Get file sizes from corresponding attribute.
130426f3deacStedu  *
130526f3deacStedu  * ntnode under fnode should be locked.
130626f3deacStedu  */
130726f3deacStedu int
13081cc0505dSjsing ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
130926f3deacStedu     u_int64_t *bytes)
131026f3deacStedu {
131126f3deacStedu 	struct ntvattr *vap;
131226f3deacStedu 	struct ntnode *ip = FTONT(fp);
131326f3deacStedu 	u_int64_t       sz, bn;
131426f3deacStedu 	int             error;
131526f3deacStedu 
1316f2997212Sjsing 	DPRINTF("ntfs_filesize: ino: %u\n", ip->i_number);
131726f3deacStedu 
131826f3deacStedu 	error = ntfs_ntvattrget(ntmp, ip,
131926f3deacStedu 		fp->f_attrtype, fp->f_attrname, 0, &vap);
132026f3deacStedu 	if (error)
132126f3deacStedu 		return (error);
132226f3deacStedu 
132326f3deacStedu 	bn = vap->va_allocated;
132426f3deacStedu 	sz = vap->va_datalen;
132526f3deacStedu 
1326f2997212Sjsing 	DPRINTF("ntfs_filesize: %llu bytes (%llu bytes allocated)\n", sz, bn);
132726f3deacStedu 
132826f3deacStedu 	if (size)
132926f3deacStedu 		*size = sz;
133026f3deacStedu 	if (bytes)
133126f3deacStedu 		*bytes = bn;
133226f3deacStedu 
133326f3deacStedu 	ntfs_ntvattrrele(vap);
133426f3deacStedu 
133526f3deacStedu 	return (0);
133626f3deacStedu }
133726f3deacStedu 
133826f3deacStedu /*
1339835c1779Sbrad  * This is one of the read routines.
134026f3deacStedu  *
134126f3deacStedu  * ntnode should be locked.
134226f3deacStedu  */
134326f3deacStedu int
13441cc0505dSjsing ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
13451cc0505dSjsing     struct ntvattr *vap, off_t roff, size_t rsize, void *rdata, size_t *initp,
134626f3deacStedu     struct uio *uio)
134726f3deacStedu {
134826f3deacStedu 	int             error = 0;
1349ac80acddSjsing 	off_t		off;
135026f3deacStedu 
135126f3deacStedu 	*initp = 0;
135226f3deacStedu 	if (vap->va_flag & NTFS_AF_INRUN) {
135326f3deacStedu 		int             cnt;
1354f05ab2cdSstefan 		cn_t            ccn, ccl, cn, cl;
135526f3deacStedu 		caddr_t         data = rdata;
135626f3deacStedu 		struct buf     *bp;
1357f05ab2cdSstefan 		size_t          left, tocopy;
135826f3deacStedu 
1359a1ea65c6Sjsing 		DDPRINTF("ntfs_readntvattr_plain: data in run: %lu chains\n",
1360a1ea65c6Sjsing 		    vap->va_vruncnt);
136126f3deacStedu 
136226f3deacStedu 		off = roff;
136326f3deacStedu 		left = rsize;
136426f3deacStedu 		ccl = 0;
136526f3deacStedu 		ccn = 0;
136626f3deacStedu 		cnt = 0;
136726f3deacStedu 		while (left && (cnt < vap->va_vruncnt)) {
136826f3deacStedu 			ccn = vap->va_vruncn[cnt];
136926f3deacStedu 			ccl = vap->va_vruncl[cnt];
137026f3deacStedu 
1371f05ab2cdSstefan 			DDPRINTF("ntfs_readntvattr_plain: left %zu, "
1372f2997212Sjsing 			    "cn: 0x%llx, cl: %llu, off: %lld\n",
1373f2997212Sjsing 			    left, ccn, ccl, off);
137426f3deacStedu 
137526f3deacStedu 			if (ntfs_cntob(ccl) < off) {
137626f3deacStedu 				off -= ntfs_cntob(ccl);
137726f3deacStedu 				cnt++;
137826f3deacStedu 				continue;
137926f3deacStedu 			}
138026f3deacStedu 			if (ccn || ip->i_number == NTFS_BOOTINO) {
138126f3deacStedu 				ccl -= ntfs_btocn(off);
138226f3deacStedu 				cn = ccn + ntfs_btocn(off);
138326f3deacStedu 				off = ntfs_btocnoff(off);
138426f3deacStedu 
138526f3deacStedu 				while (left && ccl) {
138626f3deacStedu 					/*
1387835c1779Sbrad 					 * Always read single clusters at a
1388835c1779Sbrad 					 * time - we need to avoid reading
1389835c1779Sbrad 					 * differently-sized blocks at the
1390835c1779Sbrad 					 * same disk offsets to avoid
1391835c1779Sbrad 					 * confusing the buffer cache.
139226f3deacStedu 					 */
1393835c1779Sbrad 					tocopy = MIN(left,
1394835c1779Sbrad 					    ntfs_cntob(1) - off);
1395835c1779Sbrad 					cl = ntfs_btocl(tocopy + off);
1396835c1779Sbrad 					KASSERT(cl == 1 &&
1397835c1779Sbrad 					    tocopy <= ntfs_cntob(1));
139826f3deacStedu 
1399a1ea65c6Sjsing 					DDPRINTF("ntfs_readntvattr_plain: "
1400f2997212Sjsing 					    "read: cn: 0x%llx cl: %llu, "
1401f05ab2cdSstefan 					    "off: %lld, len: %zu, "
1402f05ab2cdSstefan 					    "left: %zu\n",
1403f2997212Sjsing 					    cn, cl, off, tocopy, left);
140426f3deacStedu 					error = bread(ntmp->ntm_devvp,
140526f3deacStedu 						      ntfs_cntobn(cn),
140626f3deacStedu 						      ntfs_cntob(cl),
140793f62a9eStedu 						      &bp);
140826f3deacStedu 					if (error) {
140926f3deacStedu 						brelse(bp);
141026f3deacStedu 						return (error);
141126f3deacStedu 					}
141226f3deacStedu 					if (uio) {
1413f05ab2cdSstefan 						error = uiomove(bp->b_data + off,
141426f3deacStedu 							tocopy, uio);
1415fca26c8fSmiod 						if (error != 0)
1416fca26c8fSmiod 							break;
141726f3deacStedu 					} else {
141826f3deacStedu 						memcpy(data, bp->b_data + off,
141926f3deacStedu 							tocopy);
142026f3deacStedu 					}
142126f3deacStedu 					brelse(bp);
142226f3deacStedu 					data = data + tocopy;
142326f3deacStedu 					*initp += tocopy;
142426f3deacStedu 					off = 0;
142526f3deacStedu 					left -= tocopy;
142626f3deacStedu 					cn += cl;
142726f3deacStedu 					ccl -= cl;
142826f3deacStedu 				}
142926f3deacStedu 			} else {
1430835c1779Sbrad 				tocopy = MIN(left, ntfs_cntob(ccl) - off);
1431a1ea65c6Sjsing 				DDPRINTF("ntfs_readntvattr_plain: hole: "
1432f2997212Sjsing 				    "ccn: 0x%llx ccl: %llu, off: %lld, "
1433f05ab2cdSstefan 				    "len: %zu, left: %zu\n",
1434f2997212Sjsing 				    ccn, ccl, off, tocopy, left);
143526f3deacStedu 				left -= tocopy;
143626f3deacStedu 				off = 0;
143726f3deacStedu 				if (uio) {
143826f3deacStedu 					size_t remains = tocopy;
1439fca26c8fSmiod 					for(; remains; remains--) {
144068343589Smiod 						error = uiomove("", 1, uio);
1441fca26c8fSmiod 						if (error != 0)
1442fca26c8fSmiod 							break;
1443fca26c8fSmiod 					}
144426f3deacStedu 				} else
144526f3deacStedu 					bzero(data, tocopy);
144626f3deacStedu 				data = data + tocopy;
144726f3deacStedu 			}
144826f3deacStedu 			cnt++;
1449fca26c8fSmiod 			if (error != 0)
1450fca26c8fSmiod 				break;
145126f3deacStedu 		}
1452fca26c8fSmiod 		if (left && error == 0) {
145326f3deacStedu 			printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
145426f3deacStedu 			error = E2BIG;
145526f3deacStedu 		}
145626f3deacStedu 	} else {
1457a1ea65c6Sjsing 		DDPRINTF("ntfs_readnvattr_plain: data is in mft record\n");
145826f3deacStedu 		if (uio)
1459f05ab2cdSstefan 			error = uiomove(vap->va_datap + roff, rsize, uio);
146026f3deacStedu 		else
146126f3deacStedu 			memcpy(rdata, vap->va_datap + roff, rsize);
146226f3deacStedu 		*initp += rsize;
146326f3deacStedu 	}
146426f3deacStedu 
146526f3deacStedu 	return (error);
146626f3deacStedu }
146726f3deacStedu 
146826f3deacStedu /*
146926f3deacStedu  * This is one of read routines.
147026f3deacStedu  */
147126f3deacStedu int
14721cc0505dSjsing ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
14731cc0505dSjsing     u_int32_t attrnum, char *attrname, off_t roff, size_t rsize, void *rdata,
14741cc0505dSjsing     size_t *initp, struct uio *uio)
147526f3deacStedu {
147626f3deacStedu 	size_t          init;
147726f3deacStedu 	int             error = 0;
1478f05ab2cdSstefan 	off_t           off = roff;
1479f05ab2cdSstefan 	size_t		left = rsize, toread;
148026f3deacStedu 	caddr_t         data = rdata;
148126f3deacStedu 	struct ntvattr *vap;
148226f3deacStedu 	*initp = 0;
148326f3deacStedu 
148426f3deacStedu 	while (left) {
148526f3deacStedu 		error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
148626f3deacStedu 					ntfs_btocn(off), &vap);
148726f3deacStedu 		if (error)
148826f3deacStedu 			return (error);
1489835c1779Sbrad 		toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1490f05ab2cdSstefan 		DDPRINTF("ntfs_readattr_plain: o: %lld, s: %zu "
1491f2997212Sjsing 		    "(%llu - %llu)\n", off, toread,
1492f2997212Sjsing 		    vap->va_vcnstart, vap->va_vcnend);
149326f3deacStedu 		error = ntfs_readntvattr_plain(ntmp, ip, vap,
149426f3deacStedu 					 off - ntfs_cntob(vap->va_vcnstart),
149526f3deacStedu 					 toread, data, &init, uio);
149626f3deacStedu 		if (error) {
1497f05ab2cdSstefan 			printf("ntfs_readattr_plain: ntfs_readntvattr_plain "
1498f05ab2cdSstefan 			    "failed: o: %lld, s: %zu\n", off, toread);
1499f05ab2cdSstefan 			printf("ntfs_readattr_plain: attrib: %llu - %llu\n",
1500f05ab2cdSstefan 			       vap->va_vcnstart, vap->va_vcnend);
150126f3deacStedu 			ntfs_ntvattrrele(vap);
150226f3deacStedu 			break;
150326f3deacStedu 		}
150426f3deacStedu 		ntfs_ntvattrrele(vap);
150526f3deacStedu 		left -= toread;
150626f3deacStedu 		off += toread;
150726f3deacStedu 		data = data + toread;
150826f3deacStedu 		*initp += init;
150926f3deacStedu 	}
151026f3deacStedu 
151126f3deacStedu 	return (error);
151226f3deacStedu }
151326f3deacStedu 
151426f3deacStedu /*
151526f3deacStedu  * This is one of read routines.
151626f3deacStedu  */
151726f3deacStedu int
15181cc0505dSjsing ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
15191cc0505dSjsing     char *attrname, off_t roff, size_t rsize, void *rdata, struct uio *uio)
152026f3deacStedu {
152126f3deacStedu 	int             error = 0;
152226f3deacStedu 	struct ntvattr *vap;
152326f3deacStedu 	size_t          init;
152426f3deacStedu 
1525f2997212Sjsing 	DDPRINTF("ntfs_readattr: reading %u: 0x%x, from %lld size %zu bytes\n",
1526f2997212Sjsing 	    ip->i_number, attrnum, roff, rsize);
152726f3deacStedu 
152826f3deacStedu 	error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
152926f3deacStedu 	if (error)
153026f3deacStedu 		return (error);
153126f3deacStedu 
153226f3deacStedu 	if ((roff > vap->va_datalen) ||
153326f3deacStedu 	    (roff + rsize > vap->va_datalen)) {
15345adbeedcSjsing 		printf("ntfs_readattr: offset too big: %lld (%lld) > %llu\n",
1535f2997212Sjsing 		    roff, roff + rsize, vap->va_datalen);
153626f3deacStedu 		ntfs_ntvattrrele(vap);
153726f3deacStedu 		return (E2BIG);
153826f3deacStedu 	}
153926f3deacStedu 	if (vap->va_compression && vap->va_compressalg) {
154026f3deacStedu 		u_int8_t       *cup;
154126f3deacStedu 		u_int8_t       *uup;
1542f05ab2cdSstefan 		off_t           off = roff;
154326f3deacStedu 		caddr_t         data = rdata;
154426f3deacStedu 		cn_t            cn;
1545f05ab2cdSstefan 		size_t		left = rsize, tocopy;
154626f3deacStedu 
1547f2997212Sjsing 		DDPRINTF("ntfs_ntreadattr: compression: %u\n",
1548a1ea65c6Sjsing 		    vap->va_compressalg);
154926f3deacStedu 
1550e8821f89Shshoexer 		cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1551e8821f89Shshoexer 		    M_WAITOK);
1552e8821f89Shshoexer 		uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1553e8821f89Shshoexer 		    M_WAITOK);
155426f3deacStedu 
155526f3deacStedu 		cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
155626f3deacStedu 		off = roff - ntfs_cntob(cn);
155726f3deacStedu 
155826f3deacStedu 		while (left) {
155926f3deacStedu 			error = ntfs_readattr_plain(ntmp, ip, attrnum,
156026f3deacStedu 						  attrname, ntfs_cntob(cn),
156126f3deacStedu 					          ntfs_cntob(NTFS_COMPUNIT_CL),
156226f3deacStedu 						  cup, &init, NULL);
156326f3deacStedu 			if (error)
156426f3deacStedu 				break;
156526f3deacStedu 
1566835c1779Sbrad 			tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
156726f3deacStedu 
156826f3deacStedu 			if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
156926f3deacStedu 				if (uio)
1570f05ab2cdSstefan 					error = uiomove(cup + off, tocopy, uio);
157126f3deacStedu 				else
157226f3deacStedu 					memcpy(data, cup + off, tocopy);
157326f3deacStedu 			} else if (init == 0) {
157426f3deacStedu 				if (uio) {
157526f3deacStedu 					size_t remains = tocopy;
1576fca26c8fSmiod 					for(; remains; remains--) {
157768343589Smiod 						error = uiomove("", 1, uio);
1578fca26c8fSmiod 						if (error != 0)
1579fca26c8fSmiod 							break;
1580fca26c8fSmiod 					}
158126f3deacStedu 				}
158226f3deacStedu 				else
158326f3deacStedu 					bzero(data, tocopy);
158426f3deacStedu 			} else {
158526f3deacStedu 				error = ntfs_uncompunit(ntmp, uup, cup);
158626f3deacStedu 				if (error)
158726f3deacStedu 					break;
158826f3deacStedu 				if (uio)
1589f05ab2cdSstefan 					error = uiomove(uup + off, tocopy, uio);
159026f3deacStedu 				else
159126f3deacStedu 					memcpy(data, uup + off, tocopy);
159226f3deacStedu 			}
1593fca26c8fSmiod 			if (error)
1594fca26c8fSmiod 				break;
159526f3deacStedu 
159626f3deacStedu 			left -= tocopy;
159726f3deacStedu 			data = data + tocopy;
159826f3deacStedu 			off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
159926f3deacStedu 			cn += NTFS_COMPUNIT_CL;
160026f3deacStedu 		}
160126f3deacStedu 
1602825c4e6aStedu 		free(uup, M_NTFSDECOMP, 0);
1603825c4e6aStedu 		free(cup, M_NTFSDECOMP, 0);
160426f3deacStedu 	} else
160526f3deacStedu 		error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
160626f3deacStedu 					     roff, rsize, rdata, &init, uio);
160726f3deacStedu 	ntfs_ntvattrrele(vap);
160826f3deacStedu 	return (error);
160926f3deacStedu }
161026f3deacStedu 
161126f3deacStedu #if UNUSED_CODE
161226f3deacStedu int
16131cc0505dSjsing ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
161426f3deacStedu {
161526f3deacStedu 	u_int8_t        sz;
161626f3deacStedu 	int             i;
161726f3deacStedu 
161826f3deacStedu 	if (NULL == run) {
161926f3deacStedu 		printf("ntfs_parsetun: run == NULL\n");
162026f3deacStedu 		return (EINVAL);
162126f3deacStedu 	}
162226f3deacStedu 	sz = run[(*off)++];
162326f3deacStedu 	if (0 == sz) {
162426f3deacStedu 		printf("ntfs_parserun: trying to go out of run\n");
162526f3deacStedu 		return (E2BIG);
162626f3deacStedu 	}
162726f3deacStedu 	*cl = 0;
162826f3deacStedu 	if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
162926f3deacStedu 		printf("ntfs_parserun: " \
163026f3deacStedu 		       "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
163126f3deacStedu 		       sz, len, *off);
163226f3deacStedu 		return (EINVAL);
163326f3deacStedu 	}
163426f3deacStedu 	for (i = 0; i < (sz & 0xF); i++)
163526f3deacStedu 		*cl += (u_int32_t) run[(*off)++] << (i << 3);
163626f3deacStedu 
163726f3deacStedu 	sz >>= 4;
163826f3deacStedu 	if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
163926f3deacStedu 		printf("ntfs_parserun: " \
164026f3deacStedu 		       "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
164126f3deacStedu 		       sz, len, *off);
164226f3deacStedu 		return (EINVAL);
164326f3deacStedu 	}
164426f3deacStedu 	for (i = 0; i < (sz & 0xF); i++)
164526f3deacStedu 		*cn += (u_int32_t) run[(*off)++] << (i << 3);
164626f3deacStedu 
164726f3deacStedu 	return (0);
164826f3deacStedu }
164926f3deacStedu #endif
165026f3deacStedu 
165126f3deacStedu /*
165226f3deacStedu  * Process fixup routine on given buffer.
165326f3deacStedu  */
165426f3deacStedu int
16551cc0505dSjsing ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
165626f3deacStedu     size_t len)
165726f3deacStedu {
165826f3deacStedu 	struct fixuphdr *fhp = (struct fixuphdr *) buf;
165926f3deacStedu 	int             i;
166026f3deacStedu 	u_int16_t       fixup;
166126f3deacStedu 	u_int16_t      *fxp;
166226f3deacStedu 	u_int16_t      *cfxp;
166326f3deacStedu 
166426f3deacStedu 	if (fhp->fh_magic != magic) {
166526f3deacStedu 		printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
166626f3deacStedu 		       fhp->fh_magic, magic);
166726f3deacStedu 		return (EINVAL);
166826f3deacStedu 	}
166926f3deacStedu 	if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
167026f3deacStedu 		printf("ntfs_procfixups: " \
167126f3deacStedu 		       "bad fixups number: %d for %ld bytes block\n",
167226f3deacStedu 		       fhp->fh_fnum, (long)len);	/* XXX printf kludge */
167326f3deacStedu 		return (EINVAL);
167426f3deacStedu 	}
167526f3deacStedu 	if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
167626f3deacStedu 		printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
167726f3deacStedu 		return (EINVAL);
167826f3deacStedu 	}
167926f3deacStedu 	fxp = (u_int16_t *) (buf + fhp->fh_foff);
168026f3deacStedu 	cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
168126f3deacStedu 	fixup = *fxp++;
168226f3deacStedu 	for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
168326f3deacStedu 		if (*cfxp != fixup) {
168426f3deacStedu 			printf("ntfs_procfixups: fixup %d doesn't match\n", i);
168526f3deacStedu 			return (EINVAL);
168626f3deacStedu 		}
168726f3deacStedu 		*cfxp = *fxp;
1688835c1779Sbrad 		cfxp = (u_int16_t *)((caddr_t)cfxp + ntmp->ntm_bps);
168926f3deacStedu 	}
169026f3deacStedu 	return (0);
169126f3deacStedu }
169226f3deacStedu 
169326f3deacStedu #if UNUSED_CODE
169426f3deacStedu int
16951cc0505dSjsing ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
169626f3deacStedu     cn_t vcn)
169726f3deacStedu {
169826f3deacStedu 	cn_t            ccn = 0;
169926f3deacStedu 	cn_t            ccl = 0;
170026f3deacStedu 	u_long          off = 0;
170126f3deacStedu 	int             error = 0;
170226f3deacStedu 
170326f3deacStedu #if NTFS_DEBUG
170426f3deacStedu 	int             i;
1705846c180eSbrad 	printf("ntfs_runtocn: run: %p, %ld bytes, vcn:%ld\n",
170626f3deacStedu 		run, len, (u_long) vcn);
170726f3deacStedu 	printf("ntfs_runtocn: run: ");
170826f3deacStedu 	for (i = 0; i < len; i++)
170926f3deacStedu 		printf("0x%02x ", run[i]);
171026f3deacStedu 	printf("\n");
171126f3deacStedu #endif
171226f3deacStedu 
171326f3deacStedu 	if (NULL == run) {
171426f3deacStedu 		printf("ntfs_runtocn: run == NULL\n");
171526f3deacStedu 		return (EINVAL);
171626f3deacStedu 	}
171726f3deacStedu 	do {
171826f3deacStedu 		if (run[off] == 0) {
171926f3deacStedu 			printf("ntfs_runtocn: vcn too big\n");
172026f3deacStedu 			return (E2BIG);
172126f3deacStedu 		}
172226f3deacStedu 		vcn -= ccl;
172326f3deacStedu 		error = ntfs_parserun(&ccn, &ccl, run, len, &off);
172426f3deacStedu 		if (error) {
172526f3deacStedu 			printf("ntfs_runtocn: ntfs_parserun failed\n");
172626f3deacStedu 			return (error);
172726f3deacStedu 		}
172826f3deacStedu 	} while (ccl <= vcn);
172926f3deacStedu 	*cn = ccn + vcn;
173026f3deacStedu 	return (0);
173126f3deacStedu }
173226f3deacStedu #endif
173326f3deacStedu 
173426f3deacStedu /*
1735274476fcStedu  * if the ntfs_toupper_tab[] is filled already, just raise use count;
1736274476fcStedu  * otherwise read the data from the filesystem we are currently mounting
173726f3deacStedu  */
173826f3deacStedu int
17391cc0505dSjsing ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp, struct proc *p)
174026f3deacStedu {
174126f3deacStedu 	int error = 0;
174226f3deacStedu 	struct vnode *vp;
174326f3deacStedu 
1744274476fcStedu 	/* get exclusive access */
1745274476fcStedu 	rw_enter_write(&ntfs_toupper_lock);
1746274476fcStedu 
174726f3deacStedu 	/* only read the translation data from a file if it hasn't been
174826f3deacStedu 	 * read already */
174926f3deacStedu 	if (ntfs_toupper_tab)
175026f3deacStedu 		goto out;
175126f3deacStedu 
175226f3deacStedu 	/*
175326f3deacStedu 	 * Read in Unicode lowercase -> uppercase translation file.
175426f3deacStedu 	 * XXX for now, just the first 256 entries are used anyway,
175526f3deacStedu 	 * so don't bother reading more
175626f3deacStedu 	 */
1757265a1ec5Sdhill 	ntfs_toupper_tab = malloc(256 * 256 * sizeof(wchar), M_NTFSRDATA,
1758274476fcStedu 	    M_WAITOK);
175926f3deacStedu 
176026f3deacStedu 	if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
176126f3deacStedu 		goto out;
1762274476fcStedu 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1763274476fcStedu 			0, 256*256*sizeof(wchar), (char *) ntfs_toupper_tab,
1764274476fcStedu 			NULL);
176526f3deacStedu 	vput(vp);
176626f3deacStedu 
1767274476fcStedu     out:
1768274476fcStedu 	ntfs_toupper_usecount++;
1769274476fcStedu 	rw_exit_write(&ntfs_toupper_lock);
1770274476fcStedu 	return (error);
1771e7116e76Stedu }
1772e7116e76Stedu 
1773274476fcStedu /*
1774274476fcStedu  * lower the use count and if it reaches zero, free the memory
1775274476fcStedu  * tied by toupper table
1776274476fcStedu  */
1777274476fcStedu void
17781cc0505dSjsing ntfs_toupper_unuse(struct proc *p)
1779274476fcStedu {
1780274476fcStedu 	/* get exclusive access */
1781274476fcStedu 	rw_enter_write(&ntfs_toupper_lock);
1782e7116e76Stedu 
1783274476fcStedu 	ntfs_toupper_usecount--;
1784274476fcStedu 	if (ntfs_toupper_usecount == 0) {
1785825c4e6aStedu 		free(ntfs_toupper_tab, M_NTFSRDATA, 0);
1786274476fcStedu 		ntfs_toupper_tab = NULL;
1787274476fcStedu 	}
1788274476fcStedu #ifdef DIAGNOSTIC
1789274476fcStedu 	else if (ntfs_toupper_usecount < 0) {
1790274476fcStedu 		panic("ntfs_toupper_unuse(): use count negative: %d",
1791274476fcStedu 			ntfs_toupper_usecount);
1792274476fcStedu 	}
1793274476fcStedu #endif
1794274476fcStedu 
1795274476fcStedu 	/* release the lock */
1796274476fcStedu 	rw_exit_write(&ntfs_toupper_lock);
179726f3deacStedu }
1798