xref: /openbsd/sys/ntfs/ntfs_subr.c (revision f2997212)
1*f2997212Sjsing /*	$OpenBSD: ntfs_subr.c,v 1.34 2013/12/02 16:05:07 jsing 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>
35274476fcStedu #include <sys/proc.h>
3626f3deacStedu #include <sys/kernel.h>
3726f3deacStedu #include <sys/vnode.h>
3826f3deacStedu #include <sys/mount.h>
3926f3deacStedu #include <sys/buf.h>
4026f3deacStedu #include <sys/file.h>
4126f3deacStedu #include <sys/malloc.h>
42c74ceabfSoga #include <sys/rwlock.h>
43544451c3Sderaadt #include <sys/specdev.h>
4426f3deacStedu 
4526f3deacStedu /* #define NTFS_DEBUG 1 */
4626f3deacStedu #include <ntfs/ntfs.h>
4726f3deacStedu #include <ntfs/ntfsmount.h>
4826f3deacStedu #include <ntfs/ntfs_inode.h>
4926f3deacStedu #include <ntfs/ntfs_vfsops.h>
5026f3deacStedu #include <ntfs/ntfs_subr.h>
5126f3deacStedu #include <ntfs/ntfs_compr.h>
5226f3deacStedu #include <ntfs/ntfs_ihash.h>
5326f3deacStedu 
5426f3deacStedu #if defined(NTFS_DEBUG)
5526f3deacStedu int ntfs_debug = NTFS_DEBUG;
5626f3deacStedu #endif
5726f3deacStedu 
5826f3deacStedu /* Local struct used in ntfs_ntlookupfile() */
5926f3deacStedu struct ntfs_lookup_ctx {
6026f3deacStedu 	u_int32_t	aoff;
6126f3deacStedu 	u_int32_t	rdsize;
6226f3deacStedu 	cn_t		cn;
6326f3deacStedu 	struct ntfs_lookup_ctx *prev;
6426f3deacStedu };
6526f3deacStedu 
6605c5dae5Sjsing int ntfs_ntlookupattr(struct ntfsmount *, const char *, int, int *, char **);
6705c5dae5Sjsing int ntfs_findvattr(struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
6805c5dae5Sjsing int ntfs_uastricmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
6905c5dae5Sjsing int ntfs_uastrcmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
7026f3deacStedu 
71274476fcStedu /* table for mapping Unicode chars into uppercase; it's filled upon first
72274476fcStedu  * ntfs mount, freed upon last ntfs umount */
7326f3deacStedu static wchar *ntfs_toupper_tab;
7426f3deacStedu #define NTFS_U28(ch)		((((ch) & 0xE0) == 0) ? '_' : (ch) & 0xFF)
7526f3deacStedu #define NTFS_TOUPPER(ch)	(ntfs_toupper_tab[(unsigned char)(ch)])
76274476fcStedu struct rwlock ntfs_toupper_lock = RWLOCK_INITIALIZER("ntfs_toupper");
77274476fcStedu static signed int ntfs_toupper_usecount;
7826f3deacStedu 
7926f3deacStedu /* support macro for ntfs_ntvattrget() */
8026f3deacStedu #define NTFS_AALPCMP(aalp,type,name,namelen) (				\
8126f3deacStedu   (aalp->al_type == type) && (aalp->al_namelen == namelen) &&		\
8226f3deacStedu   !ntfs_uastrcmp(ntmp, aalp->al_name,aalp->al_namelen,name,namelen) )
8326f3deacStedu 
8426f3deacStedu /*
8526f3deacStedu  *
8626f3deacStedu  */
8726f3deacStedu int
881cc0505dSjsing ntfs_ntvattrrele(struct ntvattr *vap)
8926f3deacStedu {
90*f2997212Sjsing 	DPRINTF("ntfs_ntvattrrele: ino: %u, type: 0x%x\n",
91a1ea65c6Sjsing 	    vap->va_ip->i_number, vap->va_type);
9226f3deacStedu 
9326f3deacStedu 	ntfs_ntrele(vap->va_ip);
9426f3deacStedu 
9526f3deacStedu 	return (0);
9626f3deacStedu }
9726f3deacStedu 
9826f3deacStedu /*
9926f3deacStedu  * find the attribute in the ntnode
10026f3deacStedu  */
10105c5dae5Sjsing int
1021cc0505dSjsing ntfs_findvattr(struct ntfsmount *ntmp, struct ntnode *ip,
1031cc0505dSjsing     struct ntvattr **lvapp, struct ntvattr **vapp, u_int32_t type,
1041cc0505dSjsing     const char *name, size_t namelen, cn_t vcn)
10526f3deacStedu {
10626f3deacStedu 	int error;
10726f3deacStedu 	struct ntvattr *vap;
10826f3deacStedu 
10926f3deacStedu 	if((ip->i_flag & IN_LOADED) == 0) {
110*f2997212Sjsing 		DPRINTF("ntfs_findvattr: node not loaded, ino: %u\n",
111a1ea65c6Sjsing 		    ip->i_number);
11226f3deacStedu 		error = ntfs_loadntnode(ntmp,ip);
11326f3deacStedu 		if (error) {
11426f3deacStedu 			printf("ntfs_findvattr: FAILED TO LOAD INO: %d\n",
11526f3deacStedu 			       ip->i_number);
11626f3deacStedu 			return (error);
11726f3deacStedu 		}
1188fc6378aSjsing 	} else {
1198fc6378aSjsing 		/* Update LRU loaded list. */
1208fc6378aSjsing 		TAILQ_REMOVE(&ntmp->ntm_ntnodeq, ip, i_loaded);
1218fc6378aSjsing 		TAILQ_INSERT_HEAD(&ntmp->ntm_ntnodeq, ip, i_loaded);
12226f3deacStedu 	}
12326f3deacStedu 
12426f3deacStedu 	*lvapp = NULL;
12526f3deacStedu 	*vapp = NULL;
1261573508eSmiod 	LIST_FOREACH(vap, &ip->i_valist, va_list) {
127*f2997212Sjsing 		DDPRINTF("ntfs_findvattr: type: 0x%x, vcn: %llu - %llu\n",
128*f2997212Sjsing 		    vap->va_type, vap->va_vcnstart, vap->va_vcnend);
12926f3deacStedu 		if ((vap->va_type == type) &&
13026f3deacStedu 		    (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
13126f3deacStedu 		    (vap->va_namelen == namelen) &&
13226f3deacStedu 		    (strncmp(name, vap->va_name, namelen) == 0)) {
13326f3deacStedu 			*vapp = vap;
13426f3deacStedu 			ntfs_ntref(vap->va_ip);
13526f3deacStedu 			return (0);
13626f3deacStedu 		}
13726f3deacStedu 		if (vap->va_type == NTFS_A_ATTRLIST)
13826f3deacStedu 			*lvapp = vap;
13926f3deacStedu 	}
14026f3deacStedu 
14126f3deacStedu 	return (-1);
14226f3deacStedu }
14326f3deacStedu 
14426f3deacStedu /*
14526f3deacStedu  * Search attribute specified in ntnode (load ntnode if necessary).
14626f3deacStedu  * If not found but ATTR_A_ATTRLIST present, read it in and search through.
147cec6d865Smiod  * VOP_VGET node needed, and lookup through its ntnode (load if necessary).
14826f3deacStedu  *
14926f3deacStedu  * ntnode should be locked
15026f3deacStedu  */
15126f3deacStedu int
1521cc0505dSjsing ntfs_ntvattrget(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t type,
1531cc0505dSjsing     const char *name, cn_t vcn, struct ntvattr **vapp)
15426f3deacStedu {
15526f3deacStedu 	struct ntvattr *lvap = NULL;
15626f3deacStedu 	struct attr_attrlist *aalp;
15726f3deacStedu 	struct attr_attrlist *nextaalp;
15826f3deacStedu 	struct vnode   *newvp;
15926f3deacStedu 	struct ntnode  *newip;
16026f3deacStedu 	caddr_t         alpool;
16126f3deacStedu 	size_t		namelen, len;
16226f3deacStedu 	int             error;
16326f3deacStedu 
16426f3deacStedu 	*vapp = NULL;
16526f3deacStedu 
16626f3deacStedu 	if (name) {
167*f2997212Sjsing 		DPRINTF("ntfs_ntvattrget: ino: %u, type: 0x%x, name: %s, "
168*f2997212Sjsing 		    "vcn: %llu\n", ip->i_number, type, name, vcn);
16926f3deacStedu 		namelen = strlen(name);
17026f3deacStedu 	} else {
171*f2997212Sjsing 		DPRINTF("ntfs_ntvattrget: ino: %u, type: 0x%x, vcn: %llu\n",
172*f2997212Sjsing 		    ip->i_number, type, vcn);
17326f3deacStedu 		name = "";
17426f3deacStedu 		namelen = 0;
17526f3deacStedu 	}
17626f3deacStedu 
17726f3deacStedu 	error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
17826f3deacStedu 	if (error >= 0)
17926f3deacStedu 		return (error);
18026f3deacStedu 
18126f3deacStedu 	if (!lvap) {
182*f2997212Sjsing 		DPRINTF("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: ino: %u, "
183*f2997212Sjsing 		    "type: 0x%x, name: %s, vcn: %llu\n", ip->i_number, type,
184*f2997212Sjsing 		    name, vcn);
18526f3deacStedu 		return (ENOENT);
18626f3deacStedu 	}
18726f3deacStedu 	/* Scan $ATTRIBUTE_LIST for requested attribute */
18826f3deacStedu 	len = lvap->va_datalen;
189c6c302e5Stedu 	alpool = malloc(len, M_TEMP, M_WAITOK);
19026f3deacStedu 	error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
19126f3deacStedu 			NULL);
19226f3deacStedu 	if (error)
19326f3deacStedu 		goto out;
19426f3deacStedu 
19526f3deacStedu 	aalp = (struct attr_attrlist *) alpool;
19626f3deacStedu 	nextaalp = NULL;
19726f3deacStedu 
19826f3deacStedu 	for(; len > 0; aalp = nextaalp) {
199*f2997212Sjsing 		DPRINTF("ntfs_ntvattrget: attrlist: ino: %u, attr: 0x%x, "
200*f2997212Sjsing 		    "vcn: %llu\n", aalp->al_inumber, aalp->al_type,
201*f2997212Sjsing 		    aalp->al_vcnstart);
20226f3deacStedu 
20326f3deacStedu 		if (len > aalp->reclen) {
20426f3deacStedu 			nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
20526f3deacStedu 		} else {
20626f3deacStedu 			nextaalp = NULL;
20726f3deacStedu 		}
20826f3deacStedu 		len -= aalp->reclen;
20926f3deacStedu 
21026f3deacStedu 		if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
21126f3deacStedu 		    (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
21226f3deacStedu 		     NTFS_AALPCMP(nextaalp, type, name, namelen)))
21326f3deacStedu 			continue;
21426f3deacStedu 
215*f2997212Sjsing 		DPRINTF("ntfs_ntvattrget: attribute in ino: %u\n",
216a1ea65c6Sjsing 		    aalp->al_inumber);
21726f3deacStedu 
21826f3deacStedu 		/* this is not a main record, so we can't use just plain
21926f3deacStedu 		   vget() */
22026f3deacStedu 		error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
22126f3deacStedu 				NTFS_A_DATA, NULL, LK_EXCLUSIVE,
22226f3deacStedu 				VG_EXT, curproc, &newvp);
22326f3deacStedu 		if (error) {
22426f3deacStedu 			printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
22526f3deacStedu 			       aalp->al_inumber);
22626f3deacStedu 			goto out;
22726f3deacStedu 		}
22826f3deacStedu 		newip = VTONT(newvp);
22926f3deacStedu 		/* XXX have to lock ntnode */
23026f3deacStedu 		error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
23126f3deacStedu 				type, name, namelen, vcn);
23226f3deacStedu 		vput(newvp);
23326f3deacStedu 		if (error == 0)
23426f3deacStedu 			goto out;
23526f3deacStedu 		printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
23626f3deacStedu 		break;
23726f3deacStedu 	}
23826f3deacStedu 	error = ENOENT;
23926f3deacStedu 
240*f2997212Sjsing 	DPRINTF("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: ino: %u, type: 0x%x, "
241*f2997212Sjsing 	    "name: %.*s, vcn: %llu\n", ip->i_number, type,
242*f2997212Sjsing 	    (unsigned int)namelen, name, vcn);
24326f3deacStedu out:
24426f3deacStedu 	free(alpool, M_TEMP);
24526f3deacStedu 	return (error);
24626f3deacStedu }
24726f3deacStedu 
24826f3deacStedu /*
24926f3deacStedu  * Read ntnode from disk, make ntvattr list.
25026f3deacStedu  *
25126f3deacStedu  * ntnode should be locked
25226f3deacStedu  */
25326f3deacStedu int
2541cc0505dSjsing ntfs_loadntnode(struct ntfsmount *ntmp, struct ntnode *ip)
25526f3deacStedu {
2568fc6378aSjsing 	struct ntnode	*oip;
2578fc6378aSjsing 	struct ntvattr	*vap;
25826f3deacStedu 	struct filerec	*mfrp;
2598fc6378aSjsing 	struct attr	*ap;
2601abdbfdeSderaadt 	daddr_t		bn;
26126f3deacStedu  	int		error,off;
26226f3deacStedu 
263*f2997212Sjsing  	DPRINTF("ntfs_loadntnode: loading ino: %u\n", ip->i_number);
26426f3deacStedu 
2658fc6378aSjsing 	KASSERT((ip->i_flag & IN_LOADED) == 0);
2668fc6378aSjsing 
2678fc6378aSjsing 	if (ntmp->ntm_ntnodes >= LOADED_NTNODE_HI) {
2688fc6378aSjsing 		oip = TAILQ_LAST(&ntmp->ntm_ntnodeq, ntnodeq);
2698fc6378aSjsing 		TAILQ_REMOVE(&ntmp->ntm_ntnodeq, oip, i_loaded);
2708fc6378aSjsing 		ntmp->ntm_ntnodes--;
2718fc6378aSjsing 
272*f2997212Sjsing 		DPRINTF("ntfs_loadntnode: unloading ino: %u\n", oip->i_number);
2738fc6378aSjsing 
2748fc6378aSjsing 		KASSERT((oip->i_flag & IN_LOADED));
2758fc6378aSjsing 		oip->i_flag &= ~IN_LOADED;
2768fc6378aSjsing 		while ((vap = LIST_FIRST(&oip->i_valist)) != NULL) {
2778fc6378aSjsing 			LIST_REMOVE(vap, va_list);
2788fc6378aSjsing 			ntfs_freentvattr(vap);
2798fc6378aSjsing 		}
2808fc6378aSjsing 	}
2818fc6378aSjsing 
282c6c302e5Stedu  	mfrp = malloc(ntfs_bntob(ntmp->ntm_bpmftrec), M_TEMP, M_WAITOK);
28326f3deacStedu 
28426f3deacStedu 	if (ip->i_number < NTFS_SYSNODESNUM) {
28526f3deacStedu 		struct buf     *bp;
28626f3deacStedu 
287a1ea65c6Sjsing 		DPRINTF("ntfs_loadntnode: read system node\n");
28826f3deacStedu 
28926f3deacStedu 		bn = ntfs_cntobn(ntmp->ntm_mftcn) +
29026f3deacStedu 			ntmp->ntm_bpmftrec * ip->i_number;
29126f3deacStedu 
29293f62a9eStedu 		error = bread(ntmp->ntm_devvp, bn,
29393f62a9eStedu 		    ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
29426f3deacStedu 		if (error) {
29526f3deacStedu 			printf("ntfs_loadntnode: BREAD FAILED\n");
29626f3deacStedu 			brelse(bp);
29726f3deacStedu 			goto out;
29826f3deacStedu 		}
29926f3deacStedu 		memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
300c6c302e5Stedu 		brelse(bp);
30126f3deacStedu 	} else {
30226f3deacStedu 		struct vnode   *vp;
30326f3deacStedu 
30426f3deacStedu 		vp = ntmp->ntm_sysvn[NTFS_MFTINO];
30526f3deacStedu 		error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
30626f3deacStedu 			       ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
30726f3deacStedu 			       ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
30826f3deacStedu 		if (error) {
30926f3deacStedu 			printf("ntfs_loadntnode: ntfs_readattr failed\n");
31026f3deacStedu 			goto out;
31126f3deacStedu 		}
31226f3deacStedu 	}
31326f3deacStedu 
31426f3deacStedu 	/* Check if magic and fixups are correct */
31526f3deacStedu 	error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
31626f3deacStedu 				ntfs_bntob(ntmp->ntm_bpmftrec));
31726f3deacStedu 	if (error) {
31826f3deacStedu 		printf("ntfs_loadntnode: BAD MFT RECORD %d\n",
31926f3deacStedu 		       (u_int32_t) ip->i_number);
32026f3deacStedu 		goto out;
32126f3deacStedu 	}
32226f3deacStedu 
323*f2997212Sjsing 	DPRINTF("ntfs_loadntnode: load attrs for ino: %u\n", ip->i_number);
32426f3deacStedu 	off = mfrp->fr_attroff;
32526f3deacStedu 	ap = (struct attr *) ((caddr_t)mfrp + off);
32626f3deacStedu 
32726f3deacStedu 	LIST_INIT(&ip->i_valist);
32826f3deacStedu 
32926f3deacStedu 	while (ap->a_hdr.a_type != -1) {
3308fc6378aSjsing 		error = ntfs_attrtontvattr(ntmp, &vap, ap);
33126f3deacStedu 		if (error)
33226f3deacStedu 			break;
3338fc6378aSjsing 		vap->va_ip = ip;
33426f3deacStedu 
3358fc6378aSjsing 		LIST_INSERT_HEAD(&ip->i_valist, vap, va_list);
33626f3deacStedu 
33726f3deacStedu 		off += ap->a_hdr.reclen;
33826f3deacStedu 		ap = (struct attr *) ((caddr_t)mfrp + off);
33926f3deacStedu 	}
34026f3deacStedu 	if (error) {
34126f3deacStedu 		printf("ntfs_loadntnode: failed to load attr ino: %d\n",
34226f3deacStedu 		       ip->i_number);
34326f3deacStedu 		goto out;
34426f3deacStedu 	}
34526f3deacStedu 
34626f3deacStedu 	ip->i_mainrec = mfrp->fr_mainrec;
34726f3deacStedu 	ip->i_nlink = mfrp->fr_nlink;
34826f3deacStedu 	ip->i_frflag = mfrp->fr_flags;
34926f3deacStedu 
35026f3deacStedu 	ip->i_flag |= IN_LOADED;
35126f3deacStedu 
3528fc6378aSjsing 	/* Add to loaded list. */
3538fc6378aSjsing 	TAILQ_INSERT_HEAD(&ntmp->ntm_ntnodeq, ip, i_loaded);
3548fc6378aSjsing 	ntmp->ntm_ntnodes++;
3558fc6378aSjsing 
35626f3deacStedu out:
35726f3deacStedu 	free(mfrp, M_TEMP);
35826f3deacStedu 	return (error);
35926f3deacStedu }
36026f3deacStedu 
36126f3deacStedu /*
36226f3deacStedu  * Routine locks ntnode and increase usecount, just opposite of
36326f3deacStedu  * ntfs_ntput().
36426f3deacStedu  */
36526f3deacStedu int
3661cc0505dSjsing ntfs_ntget(struct ntnode *ip, struct proc *p)
36726f3deacStedu {
368*f2997212Sjsing 	DPRINTF("ntfs_ntget: get ntnode %u: %p, usecount: %d\n",
369a1ea65c6Sjsing 	    ip->i_number, ip, ip->i_usecount);
37026f3deacStedu 
37126f3deacStedu 	ip->i_usecount++;
3722752fedbSpedro 
373c74ceabfSoga 	rw_enter_write(&ip->i_lock);
37426f3deacStedu 
37526f3deacStedu 	return 0;
37626f3deacStedu }
37726f3deacStedu 
37826f3deacStedu /*
37926f3deacStedu  * Routine search ntnode in hash, if found: lock, inc usecount and return.
38026f3deacStedu  * If not in hash allocate structure for ntnode, prefill it, lock,
38126f3deacStedu  * inc count and return.
38226f3deacStedu  *
38326f3deacStedu  * ntnode returned locked
38426f3deacStedu  */
38526f3deacStedu int
38695a73601Sguenther ntfs_ntlookup(struct ntfsmount *ntmp, ntfsino_t ino, struct ntnode **ipp,
38726f3deacStedu     struct proc *p)
38826f3deacStedu {
38926f3deacStedu 	struct ntnode  *ip;
39026f3deacStedu 
391*f2997212Sjsing 	DPRINTF("ntfs_ntlookup: looking for ntnode %u\n", ino);
39226f3deacStedu 
39326f3deacStedu 	do {
39426f3deacStedu 		if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
39526f3deacStedu 			ntfs_ntget(ip, p);
396*f2997212Sjsing 			DPRINTF("ntfs_ntlookup: ntnode %u: %p, usecount: %d\n",
397a1ea65c6Sjsing 			    ino, ip, ip->i_usecount);
39826f3deacStedu 			*ipp = ip;
39926f3deacStedu 			return (0);
40026f3deacStedu 		}
401c74ceabfSoga 	} while (rw_enter(&ntfs_hashlock, RW_WRITE | RW_SLEEPFAIL));
40226f3deacStedu 
403c86f0003Skrw 	ip = malloc(sizeof(*ip), M_NTFSNTNODE, M_WAITOK | M_ZERO);
404*f2997212Sjsing 	DDPRINTF("ntfs_ntlookup: allocating ntnode: %u: %p\n", ino, ip);
40526f3deacStedu 
40626f3deacStedu 	/* Generic initialization */
40726f3deacStedu 	ip->i_devvp = ntmp->ntm_devvp;
40826f3deacStedu 	ip->i_dev = ntmp->ntm_dev;
40926f3deacStedu 	ip->i_number = ino;
41026f3deacStedu 	ip->i_mp = ntmp;
41126f3deacStedu 
41226f3deacStedu 	LIST_INIT(&ip->i_fnlist);
413627b2c48Sthib 	vref(ip->i_devvp);
41426f3deacStedu 
41526f3deacStedu 	/* init lock and lock the newborn ntnode */
416c74ceabfSoga 	rw_init(&ip->i_lock, "ntnode");
41726f3deacStedu 	ntfs_ntget(ip, p);
41826f3deacStedu 
41926f3deacStedu 	ntfs_nthashins(ip);
42026f3deacStedu 
421c74ceabfSoga 	rw_exit(&ntfs_hashlock);
42226f3deacStedu 
42326f3deacStedu 	*ipp = ip;
42426f3deacStedu 
425*f2997212Sjsing 	DPRINTF("ntfs_ntlookup: ntnode %u: %p, usecount: %d\n",
426a1ea65c6Sjsing 	    ino, ip, ip->i_usecount);
42726f3deacStedu 
42826f3deacStedu 	return (0);
42926f3deacStedu }
43026f3deacStedu 
43126f3deacStedu /*
43226f3deacStedu  * Decrement usecount of ntnode and unlock it, if usecount reach zero,
43326f3deacStedu  * deallocate ntnode.
43426f3deacStedu  *
43526f3deacStedu  * ntnode should be locked on entry, and unlocked on return.
43626f3deacStedu  */
43726f3deacStedu void
4381cc0505dSjsing ntfs_ntput(struct ntnode *ip, struct proc *p)
43926f3deacStedu {
4408fc6378aSjsing 	struct ntfsmount *ntmp = ip->i_mp;
44126f3deacStedu 	struct ntvattr *vap;
44226f3deacStedu 
443*f2997212Sjsing 	DPRINTF("ntfs_ntput: rele ntnode %u: %p, usecount: %d\n",
444a1ea65c6Sjsing 	    ip->i_number, ip, ip->i_usecount);
44526f3deacStedu 
44626f3deacStedu 	ip->i_usecount--;
44726f3deacStedu 
44826f3deacStedu #ifdef DIAGNOSTIC
44926f3deacStedu 	if (ip->i_usecount < 0) {
45026f3deacStedu 		panic("ntfs_ntput: ino: %d usecount: %d ",
45126f3deacStedu 		      ip->i_number,ip->i_usecount);
45226f3deacStedu 	}
45326f3deacStedu #endif
45426f3deacStedu 
4559b32ea3cSpat 	if (ip->i_usecount > 0) {
456c74ceabfSoga 		rw_exit_write(&ip->i_lock);
4579b32ea3cSpat 		return;
4589b32ea3cSpat 	}
45926f3deacStedu 
460*f2997212Sjsing 	DPRINTF("ntfs_ntput: deallocating ntnode: %u\n", ip->i_number);
46126f3deacStedu 
4629b32ea3cSpat 	if (LIST_FIRST(&ip->i_fnlist))
46326f3deacStedu 		panic("ntfs_ntput: ntnode has fnodes");
46426f3deacStedu 
46526f3deacStedu 	ntfs_nthashrem(ip);
46626f3deacStedu 
4678fc6378aSjsing 	/* Remove from loaded list. */
4688fc6378aSjsing 	if (ip->i_flag & IN_LOADED) {
4698fc6378aSjsing 		TAILQ_REMOVE(&ntmp->ntm_ntnodeq, ip, i_loaded);
4708fc6378aSjsing 		ntmp->ntm_ntnodes--;
4718fc6378aSjsing 	}
4728fc6378aSjsing 
4739b32ea3cSpat 	while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
47426f3deacStedu 		LIST_REMOVE(vap, va_list);
47526f3deacStedu 		ntfs_freentvattr(vap);
47626f3deacStedu 	}
4779b32ea3cSpat 
4789b32ea3cSpat 	vrele(ip->i_devvp);
4792f33a180Skrw 	free(ip, M_NTFSNTNODE);
48026f3deacStedu }
48126f3deacStedu 
48226f3deacStedu /*
48326f3deacStedu  * increment usecount of ntnode
48426f3deacStedu  */
48526f3deacStedu void
4861cc0505dSjsing ntfs_ntref(struct ntnode *ip)
48726f3deacStedu {
48826f3deacStedu 	ip->i_usecount++;
48926f3deacStedu 
490*f2997212Sjsing 	DPRINTF("ntfs_ntref: ino %u, usecount: %d\n",
491a1ea65c6Sjsing 	    ip->i_number, ip->i_usecount);
49226f3deacStedu }
49326f3deacStedu 
49426f3deacStedu /*
49526f3deacStedu  * Decrement usecount of ntnode.
49626f3deacStedu  */
49726f3deacStedu void
4981cc0505dSjsing ntfs_ntrele(struct ntnode *ip)
49926f3deacStedu {
500*f2997212Sjsing 	DPRINTF("ntfs_ntrele: rele ntnode %u: %p, usecount: %d\n",
501a1ea65c6Sjsing 	    ip->i_number, ip, ip->i_usecount);
50226f3deacStedu 
50326f3deacStedu 	ip->i_usecount--;
50426f3deacStedu 
50526f3deacStedu 	if (ip->i_usecount < 0)
50626f3deacStedu 		panic("ntfs_ntrele: ino: %d usecount: %d ",
50726f3deacStedu 		      ip->i_number,ip->i_usecount);
50826f3deacStedu }
50926f3deacStedu 
51026f3deacStedu /*
51126f3deacStedu  * Deallocate all memory allocated for ntvattr
51226f3deacStedu  */
51326f3deacStedu void
5141cc0505dSjsing ntfs_freentvattr(struct ntvattr *vap)
51526f3deacStedu {
51626f3deacStedu 	if (vap->va_flag & NTFS_AF_INRUN) {
51726f3deacStedu 		if (vap->va_vruncn)
51826f3deacStedu 			free(vap->va_vruncn, M_NTFSRUN);
51926f3deacStedu 		if (vap->va_vruncl)
52026f3deacStedu 			free(vap->va_vruncl, M_NTFSRUN);
52126f3deacStedu 	} else {
52226f3deacStedu 		if (vap->va_datap)
52326f3deacStedu 			free(vap->va_datap, M_NTFSRDATA);
52426f3deacStedu 	}
5252f33a180Skrw 	free(vap, M_NTFSNTVATTR);
52626f3deacStedu }
52726f3deacStedu 
52826f3deacStedu /*
52926f3deacStedu  * Convert disk image of attribute into ntvattr structure,
53026f3deacStedu  * runs are expanded also.
53126f3deacStedu  */
53226f3deacStedu int
5331cc0505dSjsing ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
53426f3deacStedu     struct attr *rap)
53526f3deacStedu {
53626f3deacStedu 	int             error, i;
53726f3deacStedu 	struct ntvattr *vap;
53826f3deacStedu 
53926f3deacStedu 	error = 0;
54026f3deacStedu 	*rvapp = NULL;
54126f3deacStedu 
542c86f0003Skrw 	vap = malloc(sizeof(*vap), M_NTFSNTVATTR, M_WAITOK | M_ZERO);
54326f3deacStedu 	vap->va_ip = NULL;
54426f3deacStedu 	vap->va_flag = rap->a_hdr.a_flag;
54526f3deacStedu 	vap->va_type = rap->a_hdr.a_type;
54626f3deacStedu 	vap->va_compression = rap->a_hdr.a_compression;
54726f3deacStedu 	vap->va_index = rap->a_hdr.a_index;
54826f3deacStedu 
549*f2997212Sjsing 	DDPRINTF("type: 0x%x, index: %u", vap->va_type, vap->va_index);
55026f3deacStedu 
55126f3deacStedu 	vap->va_namelen = rap->a_hdr.a_namelen;
55226f3deacStedu 	if (rap->a_hdr.a_namelen) {
55326f3deacStedu 		wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
554a1ea65c6Sjsing 		DDPRINTF(", name:[");
55526f3deacStedu 		for (i = 0; i < vap->va_namelen; i++) {
55626f3deacStedu 			vap->va_name[i] = unp[i];
557a1ea65c6Sjsing 			DDPRINTF("%c", vap->va_name[i]);
55826f3deacStedu 		}
559a1ea65c6Sjsing 		DDPRINTF("]");
56026f3deacStedu 	}
56126f3deacStedu 	if (vap->va_flag & NTFS_AF_INRUN) {
562a1ea65c6Sjsing 		DDPRINTF(", nonres.");
56326f3deacStedu 		vap->va_datalen = rap->a_nr.a_datalen;
56426f3deacStedu 		vap->va_allocated = rap->a_nr.a_allocated;
56526f3deacStedu 		vap->va_vcnstart = rap->a_nr.a_vcnstart;
56626f3deacStedu 		vap->va_vcnend = rap->a_nr.a_vcnend;
56726f3deacStedu 		vap->va_compressalg = rap->a_nr.a_compressalg;
56826f3deacStedu 		error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
56926f3deacStedu 				       &(vap->va_vruncnt),
57026f3deacStedu 				       (caddr_t) rap + rap->a_nr.a_dataoff);
57126f3deacStedu 	} else {
57226f3deacStedu 		vap->va_compressalg = 0;
573a1ea65c6Sjsing 		DDPRINTF(", res.");
57426f3deacStedu 		vap->va_datalen = rap->a_r.a_datalen;
57526f3deacStedu 		vap->va_allocated = rap->a_r.a_datalen;
57626f3deacStedu 		vap->va_vcnstart = 0;
57726f3deacStedu 		vap->va_vcnend = ntfs_btocn(vap->va_allocated);
578c6c302e5Stedu 		vap->va_datap = malloc(vap->va_datalen, M_NTFSRDATA, M_WAITOK);
57926f3deacStedu 		memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
58026f3deacStedu 		       rap->a_r.a_datalen);
58126f3deacStedu 	}
582*f2997212Sjsing 	DDPRINTF(", len: %u", vap->va_datalen);
58326f3deacStedu 
58426f3deacStedu 	if (error)
5852f33a180Skrw 		free(vap, M_NTFSNTVATTR);
58626f3deacStedu 	else
58726f3deacStedu 		*rvapp = vap;
58826f3deacStedu 
589a1ea65c6Sjsing 	DDPRINTF("\n");
59026f3deacStedu 
59126f3deacStedu 	return (error);
59226f3deacStedu }
59326f3deacStedu 
59426f3deacStedu /*
59526f3deacStedu  * Expand run into more utilizable and more memory eating format.
59626f3deacStedu  */
59726f3deacStedu int
5981cc0505dSjsing ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
59926f3deacStedu {
60026f3deacStedu 	u_int32_t       off;
60126f3deacStedu 	u_int32_t       sz, i;
60226f3deacStedu 	cn_t           *cn;
60326f3deacStedu 	cn_t           *cl;
60426f3deacStedu 	u_long		cnt;
60526f3deacStedu 	cn_t		prev;
60626f3deacStedu 	cn_t		tmp;
60726f3deacStedu 
60826f3deacStedu 	off = 0;
60926f3deacStedu 	cnt = 0;
61026f3deacStedu 	i = 0;
61126f3deacStedu 	while (run[off]) {
61226f3deacStedu 		off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
61326f3deacStedu 		cnt++;
61426f3deacStedu 	}
615c6c302e5Stedu 	cn = malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
616c6c302e5Stedu 	cl = malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
61726f3deacStedu 
61826f3deacStedu 	off = 0;
61926f3deacStedu 	cnt = 0;
62026f3deacStedu 	prev = 0;
62126f3deacStedu 	while (run[off]) {
62226f3deacStedu 
62326f3deacStedu 		sz = run[off++];
62426f3deacStedu 		cl[cnt] = 0;
62526f3deacStedu 
62626f3deacStedu 		for (i = 0; i < (sz & 0xF); i++)
62726f3deacStedu 			cl[cnt] += (u_int32_t) run[off++] << (i << 3);
62826f3deacStedu 
62926f3deacStedu 		sz >>= 4;
63026f3deacStedu 		if (run[off + sz - 1] & 0x80) {
63126f3deacStedu 			tmp = ((u_int64_t) - 1) << (sz << 3);
63226f3deacStedu 			for (i = 0; i < sz; i++)
63326f3deacStedu 				tmp |= (u_int64_t) run[off++] << (i << 3);
63426f3deacStedu 		} else {
63526f3deacStedu 			tmp = 0;
63626f3deacStedu 			for (i = 0; i < sz; i++)
63726f3deacStedu 				tmp |= (u_int64_t) run[off++] << (i << 3);
63826f3deacStedu 		}
63926f3deacStedu 		if (tmp)
64026f3deacStedu 			prev = cn[cnt] = prev + tmp;
64126f3deacStedu 		else
64226f3deacStedu 			cn[cnt] = tmp;
64326f3deacStedu 
64426f3deacStedu 		cnt++;
64526f3deacStedu 	}
64626f3deacStedu 	*rcnp = cn;
64726f3deacStedu 	*rclp = cl;
64826f3deacStedu 	*rcntp = cnt;
64926f3deacStedu 	return (0);
65026f3deacStedu }
65126f3deacStedu 
65226f3deacStedu /*
65326f3deacStedu  * Compare unicode and ascii string case insens.
65426f3deacStedu  */
65505c5dae5Sjsing int
6561cc0505dSjsing ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
6571cc0505dSjsing     const char *astr, size_t astrlen)
65826f3deacStedu {
65926f3deacStedu 	size_t  i;
66026f3deacStedu 	int             res;
66126f3deacStedu 	const char *astrend = astr + astrlen;
66226f3deacStedu 
66326f3deacStedu 	for (i = 0; i < ustrlen && astr < astrend; i++) {
66426f3deacStedu 		res = (*ntmp->ntm_wcmp)(NTFS_TOUPPER(ustr[i]),
66526f3deacStedu 				NTFS_TOUPPER((*ntmp->ntm_wget)(&astr)) );
66626f3deacStedu 		if (res)
66726f3deacStedu 			return res;
66826f3deacStedu 	}
66926f3deacStedu 
67026f3deacStedu 	if (i == ustrlen && astr == astrend)
67126f3deacStedu 		return 0;
67226f3deacStedu 	else if (i == ustrlen)
67326f3deacStedu 		return -1;
67426f3deacStedu 	else
67526f3deacStedu 		return 1;
67626f3deacStedu }
67726f3deacStedu 
67826f3deacStedu /*
67926f3deacStedu  * Compare unicode and ascii string case sens.
68026f3deacStedu  */
68105c5dae5Sjsing int
6821cc0505dSjsing ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
6831cc0505dSjsing     const char *astr, size_t astrlen)
68426f3deacStedu {
68526f3deacStedu 	size_t             i;
68626f3deacStedu 	int             res;
68726f3deacStedu 	const char *astrend = astr + astrlen;
68826f3deacStedu 
68926f3deacStedu 	for (i = 0; (i < ustrlen) && (astr < astrend); i++) {
69026f3deacStedu 		res = (*ntmp->ntm_wcmp)(ustr[i], (*ntmp->ntm_wget)(&astr));
69126f3deacStedu 		if (res)
69226f3deacStedu 			return res;
69326f3deacStedu 	}
69426f3deacStedu 
69526f3deacStedu 	if (i == ustrlen && astr == astrend)
69626f3deacStedu 		return 0;
69726f3deacStedu 	else if (i == ustrlen)
69826f3deacStedu 		return -1;
69926f3deacStedu 	else
70026f3deacStedu 		return 1;
70126f3deacStedu }
70226f3deacStedu 
70326f3deacStedu /*
70426f3deacStedu  * Search fnode in ntnode, if not found allocate and preinitialize.
70526f3deacStedu  *
70626f3deacStedu  * ntnode should be locked on entry.
70726f3deacStedu  */
70826f3deacStedu int
7091cc0505dSjsing ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
7101cc0505dSjsing     char *attrname, struct fnode **fpp)
71126f3deacStedu {
71226f3deacStedu 	struct fnode *fp;
71326f3deacStedu 
714*f2997212Sjsing 	DPRINTF("ntfs_fget: ino: %u, attrtype: 0x%x, attrname: %s\n",
715a1ea65c6Sjsing 	    ip->i_number, attrtype, attrname ? attrname : "");
71626f3deacStedu 	*fpp = NULL;
7171573508eSmiod 	LIST_FOREACH(fp, &ip->i_fnlist, f_fnlist) {
718*f2997212Sjsing 		DPRINTF("ntfs_fget: fnode: attrtype: %u, attrname: %s\n",
719a1ea65c6Sjsing 		    fp->f_attrtype, fp->f_attrname ? fp->f_attrname : "");
72026f3deacStedu 
72126f3deacStedu 		if ((attrtype == fp->f_attrtype) &&
72226f3deacStedu 		    ((!attrname && !fp->f_attrname) ||
72326f3deacStedu 		     (attrname && fp->f_attrname &&
72426f3deacStedu 		      !strcmp(attrname,fp->f_attrname)))){
725a1ea65c6Sjsing 			DPRINTF("ntfs_fget: found existed: %p\n", fp);
72626f3deacStedu 			*fpp = fp;
72726f3deacStedu 		}
72826f3deacStedu 	}
72926f3deacStedu 
73026f3deacStedu 	if (*fpp)
73126f3deacStedu 		return (0);
73226f3deacStedu 
733c86f0003Skrw 	fp = malloc(sizeof(*fp), M_NTFSFNODE, M_WAITOK | M_ZERO);
734a1ea65c6Sjsing 	DPRINTF("ntfs_fget: allocating fnode: %p\n", fp);
73526f3deacStedu 
73626f3deacStedu 	fp->f_ip = ip;
73726f3deacStedu 	fp->f_attrname = attrname;
73826f3deacStedu 	if (fp->f_attrname) fp->f_flag |= FN_AATTRNAME;
73926f3deacStedu 	fp->f_attrtype = attrtype;
74026f3deacStedu 
74126f3deacStedu 	ntfs_ntref(ip);
74226f3deacStedu 
74326f3deacStedu 	LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
74426f3deacStedu 
74526f3deacStedu 	*fpp = fp;
74626f3deacStedu 
74726f3deacStedu 	return (0);
74826f3deacStedu }
74926f3deacStedu 
75026f3deacStedu /*
75126f3deacStedu  * Deallocate fnode, remove it from ntnode's fnode list.
75226f3deacStedu  *
75326f3deacStedu  * ntnode should be locked.
75426f3deacStedu  */
75526f3deacStedu void
7561cc0505dSjsing ntfs_frele(struct fnode *fp)
75726f3deacStedu {
75826f3deacStedu 	struct ntnode *ip = FTONT(fp);
75926f3deacStedu 
760*f2997212Sjsing 	DPRINTF("ntfs_frele: fnode: %p for %u: %p\n", fp, ip->i_number, ip);
76126f3deacStedu 
762a1ea65c6Sjsing 	DPRINTF("ntfs_frele: deallocating fnode\n");
76326f3deacStedu 	LIST_REMOVE(fp,f_fnlist);
76426f3deacStedu 	if (fp->f_flag & FN_AATTRNAME)
765e8821f89Shshoexer 		free(fp->f_attrname, M_TEMP);
76626f3deacStedu 	if (fp->f_dirblbuf)
767e8821f89Shshoexer 		free(fp->f_dirblbuf, M_NTFSDIR);
7682f33a180Skrw 	free(fp, M_NTFSFNODE);
76926f3deacStedu 	ntfs_ntrele(ip);
77026f3deacStedu }
77126f3deacStedu 
77226f3deacStedu /*
77326f3deacStedu  * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
77426f3deacStedu  * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
77526f3deacStedu  * If $ATTR_TYPE not specified, ATTR_A_DATA assumed.
77626f3deacStedu  */
77705c5dae5Sjsing int
7781cc0505dSjsing ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
7791cc0505dSjsing     int *attrtype, char **attrname)
78026f3deacStedu {
78126f3deacStedu 	const char *sys;
78226f3deacStedu 	size_t syslen, i;
78326f3deacStedu 	struct ntvattrdef *adp;
78426f3deacStedu 
78526f3deacStedu 	if (namelen == 0)
78626f3deacStedu 		return (0);
78726f3deacStedu 
78826f3deacStedu 	if (name[0] == '$') {
78926f3deacStedu 		sys = name;
79026f3deacStedu 		for (syslen = 0; syslen < namelen; syslen++) {
79126f3deacStedu 			if(sys[syslen] == ':') {
79226f3deacStedu 				name++;
79326f3deacStedu 				namelen--;
79426f3deacStedu 				break;
79526f3deacStedu 			}
79626f3deacStedu 		}
79726f3deacStedu 		name += syslen;
79826f3deacStedu 		namelen -= syslen;
79926f3deacStedu 
80026f3deacStedu 		adp = ntmp->ntm_ad;
80126f3deacStedu 		for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
80226f3deacStedu 			if (syslen != adp->ad_namelen ||
80326f3deacStedu 			    strncmp(sys, adp->ad_name, syslen) != 0)
80426f3deacStedu 				continue;
80526f3deacStedu 
80626f3deacStedu 			*attrtype = adp->ad_type;
80726f3deacStedu 			goto out;
80826f3deacStedu 		}
80926f3deacStedu 		return (ENOENT);
81026f3deacStedu 	}
81126f3deacStedu 
81226f3deacStedu     out:
81326f3deacStedu 	if (namelen) {
814c6c302e5Stedu 		*attrname = malloc(namelen + 1, M_TEMP, M_WAITOK);
815c6c302e5Stedu 		memcpy(*attrname, name, namelen);
81626f3deacStedu 		(*attrname)[namelen] = '\0';
81726f3deacStedu 		*attrtype = NTFS_A_DATA;
81826f3deacStedu 	}
81926f3deacStedu 
82026f3deacStedu 	return (0);
82126f3deacStedu }
82226f3deacStedu 
82326f3deacStedu /*
8241cc0505dSjsing  * Lookup specified node for filename, matching cnp, return fnode filled.
82526f3deacStedu  */
82626f3deacStedu int
8271cc0505dSjsing ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
8281cc0505dSjsing     struct componentname *cnp, struct vnode **vpp, struct proc *p)
82926f3deacStedu {
83026f3deacStedu 	struct fnode   *fp = VTOF(vp);
83126f3deacStedu 	struct ntnode  *ip = FTONT(fp);
8326f6d0059Sjsing 	struct ntvattr *vap = NULL;	/* Root attribute */
83326f3deacStedu 	cn_t            cn = 0;	/* VCN in current attribute */
8346f6d0059Sjsing 	caddr_t         rdbuf = NULL;	/* Buffer to read directory's blocks */
83526f3deacStedu 	u_int32_t       blsize;
83626f3deacStedu 	u_int32_t       rdsize;	/* Length of data to read from current block */
83726f3deacStedu 	struct attr_indexentry *iep;
83826f3deacStedu 	int             error, res, anamelen, fnamelen;
83926f3deacStedu 	const char     *fname,*aname;
84026f3deacStedu 	u_int32_t       aoff;
84126f3deacStedu 	int attrtype = NTFS_A_DATA;
84226f3deacStedu 	char *attrname = NULL;
84326f3deacStedu 	struct fnode   *nfp;
84426f3deacStedu 	struct vnode   *nvp;
84526f3deacStedu 	enum vtype	f_type;
84626f3deacStedu 	int fullscan = 0;
84726f3deacStedu 	struct ntfs_lookup_ctx *lookup_ctx = NULL, *tctx;
84826f3deacStedu 
84926f3deacStedu 	error = ntfs_ntget(ip, p);
85026f3deacStedu 	if (error)
85126f3deacStedu 		return (error);
85226f3deacStedu 
85326f3deacStedu 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
8546f6d0059Sjsing 	if (error || (vap->va_flag & NTFS_AF_INRUN)) {
8556f6d0059Sjsing 		error = ENOTDIR;
8566f6d0059Sjsing 		goto fail;
8576f6d0059Sjsing 	}
85826f3deacStedu 
85926f3deacStedu 	/*
86026f3deacStedu 	 * Divide file name into: foofilefoofilefoofile[:attrspec]
86126f3deacStedu 	 * Store like this:       fname:fnamelen       [aname:anamelen]
86226f3deacStedu 	 */
86326f3deacStedu 	fname = cnp->cn_nameptr;
86426f3deacStedu 	aname = NULL;
86526f3deacStedu 	anamelen = 0;
86626f3deacStedu 	for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
86726f3deacStedu 		if(fname[fnamelen] == ':') {
86826f3deacStedu 			aname = fname + fnamelen + 1;
86926f3deacStedu 			anamelen = cnp->cn_namelen - fnamelen - 1;
870a1ea65c6Sjsing 			DPRINTF("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
871a1ea65c6Sjsing 			    fname, fnamelen, aname, anamelen);
87226f3deacStedu 			break;
87326f3deacStedu 		}
87426f3deacStedu 
87526f3deacStedu 	blsize = vap->va_a_iroot->ir_size;
876*f2997212Sjsing 	DPRINTF("ntfs_ntlookupfile: blksz: %u\n", blsize);
87726f3deacStedu 
878c6c302e5Stedu 	rdbuf = malloc(blsize, M_TEMP, M_WAITOK);
87926f3deacStedu 
88026f3deacStedu     loop:
88126f3deacStedu 	rdsize = vap->va_datalen;
882*f2997212Sjsing 	DPRINTF("ntfs_ntlookupfile: rdsz: %u\n", rdsize);
88326f3deacStedu 
88426f3deacStedu 	error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
88526f3deacStedu 			       0, rdsize, rdbuf, NULL);
88626f3deacStedu 	if (error)
88726f3deacStedu 		goto fail;
88826f3deacStedu 
88926f3deacStedu 	aoff = sizeof(struct attr_indexroot);
89026f3deacStedu 
89126f3deacStedu 	do {
89226f3deacStedu 		iep = (struct attr_indexentry *) (rdbuf + aoff);
89326f3deacStedu 
89426f3deacStedu 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
89526f3deacStedu 			aoff += iep->reclen,
89626f3deacStedu 			iep = (struct attr_indexentry *) (rdbuf + aoff))
89726f3deacStedu 		{
898*f2997212Sjsing 			DDPRINTF("scan: %u, %u\n", iep->ie_number,
899*f2997212Sjsing 			    iep->ie_fnametype);
90026f3deacStedu 
90126f3deacStedu 			/* check the name - the case-insensitive check
90226f3deacStedu 			 * has to come first, to break from this for loop
90326f3deacStedu 			 * if needed, so we can dive correctly */
90426f3deacStedu 			res = ntfs_uastricmp(ntmp, iep->ie_fname,
90526f3deacStedu 				iep->ie_fnamelen, fname, fnamelen);
90626f3deacStedu 			if (!fullscan) {
90726f3deacStedu 				if (res > 0) break;
90826f3deacStedu 				if (res < 0) continue;
90926f3deacStedu 			}
91026f3deacStedu 
91126f3deacStedu 			if (iep->ie_fnametype == 0 ||
91226f3deacStedu 			    !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
91326f3deacStedu 			{
91426f3deacStedu 				res = ntfs_uastrcmp(ntmp, iep->ie_fname,
91526f3deacStedu 					iep->ie_fnamelen, fname, fnamelen);
91626f3deacStedu 				if (res != 0 && !fullscan) continue;
91726f3deacStedu 			}
91826f3deacStedu 
91926f3deacStedu 			/* if we perform full scan, the file does not match
92026f3deacStedu 			 * and this is subnode, dive */
92126f3deacStedu 			if (fullscan && res != 0) {
92226f3deacStedu 			    if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
923e8821f89Shshoexer 				tctx = malloc(sizeof(struct ntfs_lookup_ctx),
92426f3deacStedu 					M_TEMP, M_WAITOK);
92526f3deacStedu 				tctx->aoff	= aoff + iep->reclen;
92626f3deacStedu 				tctx->rdsize	= rdsize;
92726f3deacStedu 				tctx->cn	= cn;
92826f3deacStedu 				tctx->prev	= lookup_ctx;
92926f3deacStedu 				lookup_ctx = tctx;
93026f3deacStedu 				break;
93126f3deacStedu 			    } else
93226f3deacStedu 				continue;
93326f3deacStedu 			}
93426f3deacStedu 
93526f3deacStedu 			if (aname) {
93626f3deacStedu 				error = ntfs_ntlookupattr(ntmp,
93726f3deacStedu 					aname, anamelen,
93826f3deacStedu 					&attrtype, &attrname);
93926f3deacStedu 				if (error)
94026f3deacStedu 					goto fail;
94126f3deacStedu 			}
94226f3deacStedu 
94326f3deacStedu 			/* Check if we've found ourselves */
94426f3deacStedu 			if ((iep->ie_number == ip->i_number) &&
94526f3deacStedu 			    (attrtype == fp->f_attrtype) &&
94626f3deacStedu 			    ((!attrname && !fp->f_attrname) ||
94726f3deacStedu 			     (attrname && fp->f_attrname &&
94826f3deacStedu 			      !strcmp(attrname, fp->f_attrname))))
94926f3deacStedu 			{
950627b2c48Sthib 				vref(vp);
95126f3deacStedu 				*vpp = vp;
95226f3deacStedu 				error = 0;
95326f3deacStedu 				goto fail;
95426f3deacStedu 			}
95526f3deacStedu 
95626f3deacStedu 			/* free the buffer returned by ntfs_ntlookupattr() */
95726f3deacStedu 			if (attrname) {
958e8821f89Shshoexer 				free(attrname, M_TEMP);
95926f3deacStedu 				attrname = NULL;
96026f3deacStedu 			}
96126f3deacStedu 
96226f3deacStedu 			/* vget node, but don't load it */
96326f3deacStedu 			error = ntfs_vgetex(ntmp->ntm_mountp,
96426f3deacStedu 				   iep->ie_number, attrtype, attrname,
96526f3deacStedu 				   LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
96626f3deacStedu 				   curproc, &nvp);
96726f3deacStedu 			if (error)
96826f3deacStedu 				goto fail;
96926f3deacStedu 
97026f3deacStedu 			nfp = VTOF(nvp);
97126f3deacStedu 
97226f3deacStedu 			if (nfp->f_flag & FN_VALID) {
97326f3deacStedu 				*vpp = nvp;
97426f3deacStedu 				goto fail;
97526f3deacStedu 			}
97626f3deacStedu 
97726f3deacStedu 			nfp->f_fflag = iep->ie_fflag;
97826f3deacStedu 			nfp->f_pnumber = iep->ie_fpnumber;
97926f3deacStedu 			nfp->f_times = iep->ie_ftimes;
98026f3deacStedu 
98126f3deacStedu 			if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
98226f3deacStedu 			   (nfp->f_attrtype == NTFS_A_DATA) &&
98326f3deacStedu 			   (nfp->f_attrname == NULL))
98426f3deacStedu 				f_type = VDIR;
98526f3deacStedu 			else
98626f3deacStedu 				f_type = VREG;
98726f3deacStedu 
98826f3deacStedu 			nvp->v_type = f_type;
98926f3deacStedu 
99026f3deacStedu 			if ((nfp->f_attrtype == NTFS_A_DATA) &&
99126f3deacStedu 			    (nfp->f_attrname == NULL))
99226f3deacStedu 			{
99326f3deacStedu 				/* Opening default attribute */
99426f3deacStedu 				nfp->f_size = iep->ie_fsize;
99526f3deacStedu 				nfp->f_allocated = iep->ie_fallocated;
99626f3deacStedu 				nfp->f_flag |= FN_PRELOADED;
99726f3deacStedu 			} else {
99826f3deacStedu 				error = ntfs_filesize(ntmp, nfp,
99926f3deacStedu 					    &nfp->f_size, &nfp->f_allocated);
100026f3deacStedu 				if (error) {
100126f3deacStedu 					vput(nvp);
100226f3deacStedu 					goto fail;
100326f3deacStedu 				}
100426f3deacStedu 			}
100526f3deacStedu 
100626f3deacStedu 			nfp->f_flag &= ~FN_VALID;
100726f3deacStedu 			*vpp = nvp;
100826f3deacStedu 			goto fail;
100926f3deacStedu 		}
101026f3deacStedu 
101126f3deacStedu 		/* Dive if possible */
101226f3deacStedu 		if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1013a1ea65c6Sjsing 			DPRINTF("ntfs_ntlookupfile: diving\n");
101426f3deacStedu 
101526f3deacStedu 			cn = *(cn_t *) (rdbuf + aoff +
101626f3deacStedu 					iep->reclen - sizeof(cn_t));
101726f3deacStedu 			rdsize = blsize;
101826f3deacStedu 
101926f3deacStedu 			error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
102026f3deacStedu 					ntfs_cntob(cn), rdsize, rdbuf, NULL);
102126f3deacStedu 			if (error)
102226f3deacStedu 				goto fail;
102326f3deacStedu 
102426f3deacStedu 			error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
102526f3deacStedu 						rdbuf, rdsize);
102626f3deacStedu 			if (error)
102726f3deacStedu 				goto fail;
102826f3deacStedu 
102926f3deacStedu 			aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
103026f3deacStedu 				0x18);
103126f3deacStedu 		} else if (fullscan && lookup_ctx) {
103226f3deacStedu 			cn = lookup_ctx->cn;
103326f3deacStedu 			aoff = lookup_ctx->aoff;
103426f3deacStedu 			rdsize = lookup_ctx->rdsize;
103526f3deacStedu 
103626f3deacStedu 			error = ntfs_readattr(ntmp, ip,
103726f3deacStedu 				(cn == 0) ? NTFS_A_INDXROOT : NTFS_A_INDX,
103826f3deacStedu 				"$I30", ntfs_cntob(cn), rdsize, rdbuf, NULL);
103926f3deacStedu 			if (error)
104026f3deacStedu 				goto fail;
104126f3deacStedu 
104226f3deacStedu 			if (cn != 0) {
104326f3deacStedu 				error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
104426f3deacStedu 						rdbuf, rdsize);
104526f3deacStedu 				if (error)
104626f3deacStedu 					goto fail;
104726f3deacStedu 			}
104826f3deacStedu 
104926f3deacStedu 			tctx = lookup_ctx;
105026f3deacStedu 			lookup_ctx = lookup_ctx->prev;
1051e8821f89Shshoexer 			free(tctx, M_TEMP);
105226f3deacStedu 		} else {
1053a1ea65c6Sjsing 			DPRINTF("ntfs_ntlookupfile: nowhere to dive :-(\n");
105426f3deacStedu 			error = ENOENT;
105526f3deacStedu 			break;
105626f3deacStedu 		}
105726f3deacStedu 	} while (1);
105826f3deacStedu 
105926f3deacStedu 	/* perform full scan if no entry was found */
106026f3deacStedu 	if (!fullscan && error == ENOENT) {
106126f3deacStedu 		fullscan = 1;
106226f3deacStedu 		cn = 0;		/* need zero, used by lookup_ctx */
106326f3deacStedu 
1064a1ea65c6Sjsing 		DDPRINTF("ntfs_ntlookupfile: fullscan performed for: %.*s\n",
1065*f2997212Sjsing 		    (unsigned int)fnamelen, fname);
106626f3deacStedu 		goto loop;
106726f3deacStedu 	}
106826f3deacStedu 
1069a1ea65c6Sjsing 	DPRINTF("finish\n");
107026f3deacStedu 
107126f3deacStedu fail:
10726f6d0059Sjsing 	if (vap)
10736f6d0059Sjsing 		ntfs_ntvattrrele(vap);
10746f6d0059Sjsing 	if (rdbuf)
10756f6d0059Sjsing 		free(rdbuf, M_TEMP);
107626f3deacStedu 	if (attrname)
1077e8821f89Shshoexer 		free(attrname, M_TEMP);
107826f3deacStedu 	if (lookup_ctx) {
107926f3deacStedu 		while(lookup_ctx) {
108026f3deacStedu 			tctx = lookup_ctx;
108126f3deacStedu 			lookup_ctx = lookup_ctx->prev;
1082e8821f89Shshoexer 			free(tctx, M_TEMP);
108326f3deacStedu 		}
108426f3deacStedu 	}
108526f3deacStedu 	ntfs_ntput(ip, p);
108626f3deacStedu 	return (error);
108726f3deacStedu }
108826f3deacStedu 
108926f3deacStedu /*
109026f3deacStedu  * Check if name type is permitted to show.
109126f3deacStedu  */
109226f3deacStedu int
10931cc0505dSjsing ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
109426f3deacStedu {
109526f3deacStedu 	if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
109626f3deacStedu 		return 1;
109726f3deacStedu 
109826f3deacStedu 	switch (iep->ie_fnametype) {
109926f3deacStedu 	case 2:
1100a1ea65c6Sjsing 		DDPRINTF("ntfs_isnamepermitted: skipped DOS name\n");
110126f3deacStedu 		return 0;
110226f3deacStedu 	case 0: case 1: case 3:
110326f3deacStedu 		return 1;
110426f3deacStedu 	default:
110526f3deacStedu 		printf("ntfs_isnamepermitted: " \
110626f3deacStedu 		       "WARNING! Unknown file name type: %d\n",
110726f3deacStedu 		       iep->ie_fnametype);
110826f3deacStedu 		break;
110926f3deacStedu 	}
111026f3deacStedu 	return 0;
111126f3deacStedu }
111226f3deacStedu 
111326f3deacStedu /*
111426f3deacStedu  * Read ntfs dir like stream of attr_indexentry, not like btree of them.
111526f3deacStedu  * This is done by scanning $BITMAP:$I30 for busy clusters and reading them.
111626f3deacStedu  * Of course $INDEX_ROOT:$I30 is read before. Last read values are stored in
111726f3deacStedu  * fnode, so we can skip toward record number num almost immediately.
111826f3deacStedu  * Anyway this is rather slow routine. The problem is that we don't know
111926f3deacStedu  * how many records are there in $INDEX_ALLOCATION:$I30 block.
112026f3deacStedu  */
112126f3deacStedu int
11221cc0505dSjsing ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp, u_int32_t num,
11231cc0505dSjsing     struct attr_indexentry **riepp, struct proc *p)
112426f3deacStedu {
112526f3deacStedu 	struct ntnode  *ip = FTONT(fp);
112626f3deacStedu 	struct ntvattr *vap = NULL;	/* IndexRoot attribute */
112726f3deacStedu 	struct ntvattr *bmvap = NULL;	/* BitMap attribute */
112826f3deacStedu 	struct ntvattr *iavap = NULL;	/* IndexAllocation attribute */
112926f3deacStedu 	caddr_t         rdbuf;		/* Buffer to read directory's blocks  */
11306978a3abSbrad 	u_int8_t       *bmp = NULL;	/* Bitmap */
113126f3deacStedu 	u_int32_t       blsize;		/* Index allocation size (2048) */
113226f3deacStedu 	u_int32_t       rdsize;		/* Length of data to read */
113326f3deacStedu 	u_int32_t       attrnum;	/* Current attribute type */
113426f3deacStedu 	u_int32_t       cpbl = 1;	/* Clusters per directory block */
113526f3deacStedu 	u_int32_t       blnum;
113626f3deacStedu 	struct attr_indexentry *iep;
113726f3deacStedu 	int             error = ENOENT;
113826f3deacStedu 	u_int32_t       aoff, cnum;
113926f3deacStedu 
1140*f2997212Sjsing 	DPRINTF("ntfs_ntreaddir: read ino: %u, num: %u\n", ip->i_number, num);
114126f3deacStedu 	error = ntfs_ntget(ip, p);
114226f3deacStedu 	if (error)
114326f3deacStedu 		return (error);
114426f3deacStedu 
114526f3deacStedu 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
11466f6d0059Sjsing 	if (error) {
11476f6d0059Sjsing 		error = ENOTDIR;
11486f6d0059Sjsing 		goto fail;
11496f6d0059Sjsing 	}
115026f3deacStedu 
115126f3deacStedu 	if (fp->f_dirblbuf == NULL) {
115226f3deacStedu 		fp->f_dirblsz = vap->va_a_iroot->ir_size;
1153c6c302e5Stedu 		fp->f_dirblbuf = malloc(MAX(vap->va_datalen,fp->f_dirblsz),
1154c6c302e5Stedu 		    M_NTFSDIR, M_WAITOK);
115526f3deacStedu 	}
115626f3deacStedu 
115726f3deacStedu 	blsize = fp->f_dirblsz;
115826f3deacStedu 	rdbuf = fp->f_dirblbuf;
115926f3deacStedu 
1160*f2997212Sjsing 	DPRINTF("ntfs_ntreaddir: rdbuf: %p, blsize: %u\n", rdbuf, blsize);
116126f3deacStedu 
116226f3deacStedu 	if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
116326f3deacStedu 		error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
116426f3deacStedu 					0, &bmvap);
116526f3deacStedu 		if (error) {
116626f3deacStedu 			error = ENOTDIR;
116726f3deacStedu 			goto fail;
116826f3deacStedu 		}
1169c6c302e5Stedu 		bmp = malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
117026f3deacStedu 		error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
117126f3deacStedu 				       bmvap->va_datalen, bmp, NULL);
117226f3deacStedu 		if (error)
117326f3deacStedu 			goto fail;
117426f3deacStedu 
117526f3deacStedu 		error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
117626f3deacStedu 					0, &iavap);
117726f3deacStedu 		if (error) {
117826f3deacStedu 			error = ENOTDIR;
117926f3deacStedu 			goto fail;
118026f3deacStedu 		}
118126f3deacStedu 		cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1182*f2997212Sjsing 		DPRINTF("ntfs_ntreaddir: indexalloc: %u, cpbl: %u\n",
1183a1ea65c6Sjsing 		    iavap->va_datalen, cpbl);
118426f3deacStedu 	} else {
1185a1ea65c6Sjsing 		DPRINTF("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n");
118626f3deacStedu 		iavap = bmvap = NULL;
118726f3deacStedu 		bmp = NULL;
118826f3deacStedu 	}
118926f3deacStedu 
119026f3deacStedu 	/* Try use previous values */
119126f3deacStedu 	if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
119226f3deacStedu 		attrnum = fp->f_lastdattr;
119326f3deacStedu 		aoff = fp->f_lastdoff;
119426f3deacStedu 		blnum = fp->f_lastdblnum;
119526f3deacStedu 		cnum = fp->f_lastdnum;
119626f3deacStedu 	} else {
119726f3deacStedu 		attrnum = NTFS_A_INDXROOT;
119826f3deacStedu 		aoff = sizeof(struct attr_indexroot);
119926f3deacStedu 		blnum = 0;
120026f3deacStedu 		cnum = 0;
120126f3deacStedu 	}
120226f3deacStedu 
120326f3deacStedu 	do {
1204*f2997212Sjsing 		DPRINTF("ntfs_ntreaddir: scan: 0x%x, %u, %u, %u, %u\n",
1205*f2997212Sjsing 		    attrnum, blnum, cnum, num, aoff);
120626f3deacStedu 		rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
120726f3deacStedu 		error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
120826f3deacStedu 				ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
120926f3deacStedu 		if (error)
121026f3deacStedu 			goto fail;
121126f3deacStedu 
121226f3deacStedu 		if (attrnum == NTFS_A_INDX) {
121326f3deacStedu 			error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
121426f3deacStedu 						rdbuf, rdsize);
121526f3deacStedu 			if (error)
121626f3deacStedu 				goto fail;
121726f3deacStedu 		}
121826f3deacStedu 		if (aoff == 0)
121926f3deacStedu 			aoff = (attrnum == NTFS_A_INDX) ?
122026f3deacStedu 				(0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
122126f3deacStedu 				sizeof(struct attr_indexroot);
122226f3deacStedu 
122326f3deacStedu 		iep = (struct attr_indexentry *) (rdbuf + aoff);
122426f3deacStedu 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
122526f3deacStedu 			aoff += iep->reclen,
122626f3deacStedu 			iep = (struct attr_indexentry *) (rdbuf + aoff))
122726f3deacStedu 		{
122826f3deacStedu 			if (!ntfs_isnamepermitted(ntmp, iep)) continue;
122926f3deacStedu 
123026f3deacStedu 			if (cnum >= num) {
123126f3deacStedu 				fp->f_lastdnum = cnum;
123226f3deacStedu 				fp->f_lastdoff = aoff;
123326f3deacStedu 				fp->f_lastdblnum = blnum;
123426f3deacStedu 				fp->f_lastdattr = attrnum;
123526f3deacStedu 
123626f3deacStedu 				*riepp = iep;
123726f3deacStedu 
123826f3deacStedu 				error = 0;
123926f3deacStedu 				goto fail;
124026f3deacStedu 			}
124126f3deacStedu 			cnum++;
124226f3deacStedu 		}
124326f3deacStedu 
124426f3deacStedu 		if (iavap) {
124526f3deacStedu 			if (attrnum == NTFS_A_INDXROOT)
124626f3deacStedu 				blnum = 0;
124726f3deacStedu 			else
124826f3deacStedu 				blnum++;
124926f3deacStedu 
125026f3deacStedu 			while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
12516978a3abSbrad 				if (bmp[blnum >> 3] & (1 << (blnum & 7)))
125226f3deacStedu 					break;
125326f3deacStedu 				blnum++;
125426f3deacStedu 			}
125526f3deacStedu 
125626f3deacStedu 			attrnum = NTFS_A_INDX;
125726f3deacStedu 			aoff = 0;
125826f3deacStedu 			if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
125926f3deacStedu 				break;
1260*f2997212Sjsing 			DPRINTF("ntfs_ntreaddir: blnum: %u\n", blnum);
126126f3deacStedu 		}
126226f3deacStedu 	} while (iavap);
126326f3deacStedu 
126426f3deacStedu 	*riepp = NULL;
126526f3deacStedu 	fp->f_lastdnum = 0;
126626f3deacStedu 
126726f3deacStedu fail:
126826f3deacStedu 	if (vap)
126926f3deacStedu 		ntfs_ntvattrrele(vap);
127026f3deacStedu 	if (bmvap)
127126f3deacStedu 		ntfs_ntvattrrele(bmvap);
127226f3deacStedu 	if (iavap)
127326f3deacStedu 		ntfs_ntvattrrele(iavap);
127426f3deacStedu 	if (bmp)
1275e8821f89Shshoexer 		free(bmp, M_TEMP);
127626f3deacStedu 	ntfs_ntput(ip, p);
12771ad3841aSjasper 
127826f3deacStedu 	return (error);
127926f3deacStedu }
128026f3deacStedu 
128126f3deacStedu /*
128226f3deacStedu  * Convert NTFS times that are in 100 ns units and begins from
128326f3deacStedu  * 1601 Jan 1 into unix times.
128426f3deacStedu  */
128526f3deacStedu struct timespec
12861cc0505dSjsing ntfs_nttimetounix(u_int64_t nt)
128726f3deacStedu {
128826f3deacStedu 	struct timespec t;
128926f3deacStedu 
129026f3deacStedu 	/* WindowNT times are in 100 ns and from 1601 Jan 1 */
129126f3deacStedu 	t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
129226f3deacStedu 	t.tv_sec = nt / (1000 * 1000 * 10) -
129326f3deacStedu 		369LL * 365LL * 24LL * 60LL * 60LL -
129426f3deacStedu 		89LL * 1LL * 24LL * 60LL * 60LL;
129526f3deacStedu 	return (t);
129626f3deacStedu }
129726f3deacStedu 
129826f3deacStedu /*
129926f3deacStedu  * Get file sizes from corresponding attribute.
130026f3deacStedu  *
130126f3deacStedu  * ntnode under fnode should be locked.
130226f3deacStedu  */
130326f3deacStedu int
13041cc0505dSjsing ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
130526f3deacStedu     u_int64_t *bytes)
130626f3deacStedu {
130726f3deacStedu 	struct ntvattr *vap;
130826f3deacStedu 	struct ntnode *ip = FTONT(fp);
130926f3deacStedu 	u_int64_t       sz, bn;
131026f3deacStedu 	int             error;
131126f3deacStedu 
1312*f2997212Sjsing 	DPRINTF("ntfs_filesize: ino: %u\n", ip->i_number);
131326f3deacStedu 
131426f3deacStedu 	error = ntfs_ntvattrget(ntmp, ip,
131526f3deacStedu 		fp->f_attrtype, fp->f_attrname, 0, &vap);
131626f3deacStedu 	if (error)
131726f3deacStedu 		return (error);
131826f3deacStedu 
131926f3deacStedu 	bn = vap->va_allocated;
132026f3deacStedu 	sz = vap->va_datalen;
132126f3deacStedu 
1322*f2997212Sjsing 	DPRINTF("ntfs_filesize: %llu bytes (%llu bytes allocated)\n", sz, bn);
132326f3deacStedu 
132426f3deacStedu 	if (size)
132526f3deacStedu 		*size = sz;
132626f3deacStedu 	if (bytes)
132726f3deacStedu 		*bytes = bn;
132826f3deacStedu 
132926f3deacStedu 	ntfs_ntvattrrele(vap);
133026f3deacStedu 
133126f3deacStedu 	return (0);
133226f3deacStedu }
133326f3deacStedu 
133426f3deacStedu /*
1335835c1779Sbrad  * This is one of the write routines.
133626f3deacStedu  */
133726f3deacStedu int
13381cc0505dSjsing ntfs_writeattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
13391cc0505dSjsing     u_int32_t attrnum, char *attrname, off_t roff, size_t rsize, void *rdata,
13401cc0505dSjsing     size_t *initp, struct uio *uio)
134126f3deacStedu {
134226f3deacStedu 	size_t          init;
134326f3deacStedu 	int             error = 0;
134426f3deacStedu 	off_t           off = roff, left = rsize, towrite;
134526f3deacStedu 	caddr_t         data = rdata;
134626f3deacStedu 	struct ntvattr *vap;
134726f3deacStedu 	*initp = 0;
134826f3deacStedu 
134926f3deacStedu 	while (left) {
135026f3deacStedu 		error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
135126f3deacStedu 					ntfs_btocn(off), &vap);
135226f3deacStedu 		if (error)
135326f3deacStedu 			return (error);
1354835c1779Sbrad 		towrite = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1355*f2997212Sjsing 		DDPRINTF("ntfs_writeattr_plain: o: %lld, s: %lld "
1356*f2997212Sjsing 		    "(%llu - %llu)\n", off, towrite,
1357*f2997212Sjsing 		    vap->va_vcnstart, vap->va_vcnend);
135826f3deacStedu 		error = ntfs_writentvattr_plain(ntmp, ip, vap,
135926f3deacStedu 					 off - ntfs_cntob(vap->va_vcnstart),
136026f3deacStedu 					 towrite, data, &init, uio);
136126f3deacStedu 		if (error) {
1362a1ea65c6Sjsing 			DPRINTF("ntfs_writeattr_plain: ntfs_writentvattr_plain "
1363a1ea65c6Sjsing 			    "failed: o: %d, s: %d\n",
1364a1ea65c6Sjsing 			    (u_int32_t)off, (u_int32_t)towrite);
1365a1ea65c6Sjsing 			DPRINTF("ntfs_writeattr_plain: attrib: %d - %d\n",
136626f3deacStedu 			    (u_int32_t)vap->va_vcnstart,
1367a1ea65c6Sjsing 			    (u_int32_t)vap->va_vcnend);
136826f3deacStedu 			ntfs_ntvattrrele(vap);
136926f3deacStedu 			break;
137026f3deacStedu 		}
137126f3deacStedu 		ntfs_ntvattrrele(vap);
137226f3deacStedu 		left -= towrite;
137326f3deacStedu 		off += towrite;
137426f3deacStedu 		data = data + towrite;
137526f3deacStedu 		*initp += init;
137626f3deacStedu 	}
137726f3deacStedu 
137826f3deacStedu 	return (error);
137926f3deacStedu }
138026f3deacStedu 
138126f3deacStedu /*
1382835c1779Sbrad  * This is one of the write routines.
138326f3deacStedu  *
138426f3deacStedu  * ntnode should be locked.
138526f3deacStedu  */
138626f3deacStedu int
13871cc0505dSjsing ntfs_writentvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
13881cc0505dSjsing     struct ntvattr *vap, off_t roff, size_t rsize, void *rdata, size_t *initp,
138926f3deacStedu     struct uio *uio)
139026f3deacStedu {
139126f3deacStedu 	int             error = 0;
139226f3deacStedu 	int             off;
139326f3deacStedu 	int             cnt;
139426f3deacStedu 	cn_t            ccn, ccl, cn, left, cl;
139526f3deacStedu 	caddr_t         data = rdata;
139626f3deacStedu 	struct buf     *bp;
139726f3deacStedu 	size_t          tocopy;
139826f3deacStedu 
139926f3deacStedu 	*initp = 0;
140026f3deacStedu 
140126f3deacStedu 	if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1402a1ea65c6Sjsing 		DPRINTF("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
140326f3deacStedu 		return ENOTTY;
140426f3deacStedu 	}
140526f3deacStedu 
1406a1ea65c6Sjsing 	DDPRINTF("ntfs_writentvattr_plain: data in run: %lu chains\n",
1407a1ea65c6Sjsing 	    vap->va_vruncnt);
140826f3deacStedu 
140926f3deacStedu 	off = roff;
141026f3deacStedu 	left = rsize;
141126f3deacStedu 	ccl = 0;
141226f3deacStedu 	ccn = 0;
141326f3deacStedu 	cnt = 0;
141426f3deacStedu 	for (; left && (cnt < vap->va_vruncnt); cnt++) {
141526f3deacStedu 		ccn = vap->va_vruncn[cnt];
141626f3deacStedu 		ccl = vap->va_vruncl[cnt];
141726f3deacStedu 
1418*f2997212Sjsing 		DDPRINTF("ntfs_writentvattr_plain: left %llu, cn: 0x%llx, "
1419*f2997212Sjsing 		    "cl: %llu, off: %lld\n", left, ccn, ccl, off);
142026f3deacStedu 
142126f3deacStedu 		if (ntfs_cntob(ccl) < off) {
142226f3deacStedu 			off -= ntfs_cntob(ccl);
142326f3deacStedu 			cnt++;
142426f3deacStedu 			continue;
142526f3deacStedu 		}
142626f3deacStedu 		if (!ccn && ip->i_number != NTFS_BOOTINO)
142726f3deacStedu 			continue; /* XXX */
142826f3deacStedu 
142926f3deacStedu 		ccl -= ntfs_btocn(off);
143026f3deacStedu 		cn = ccn + ntfs_btocn(off);
143126f3deacStedu 		off = ntfs_btocnoff(off);
143226f3deacStedu 
143326f3deacStedu 		while (left && ccl) {
1434835c1779Sbrad 			/*
1435835c1779Sbrad 			 * Always read and write single clusters at a time -
1436835c1779Sbrad 			 * we need to avoid requesting differently-sized
1437835c1779Sbrad 			 * blocks at the same disk offsets to avoid
1438835c1779Sbrad 			 * confusing the buffer cache.
1439835c1779Sbrad 			 */
1440835c1779Sbrad 			tocopy = MIN(left, ntfs_cntob(1) - off);
144126f3deacStedu 			cl = ntfs_btocl(tocopy + off);
1442835c1779Sbrad 			KASSERT(cl == 1 && tocopy <= ntfs_cntob(1));
1443*f2997212Sjsing 			DDPRINTF("ntfs_writentvattr_plain: write: cn: 0x%llx "
1444*f2997212Sjsing 			    "cl: %llu, off: %lld len: %llu, left: %llu\n",
1445*f2997212Sjsing 			    cn, cl, off, tocopy, left);
144626f3deacStedu 			if ((off == 0) && (tocopy == ntfs_cntob(cl)))
144726f3deacStedu 			{
144826f3deacStedu 				bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
144926f3deacStedu 					    ntfs_cntob(cl), 0, 0);
145026f3deacStedu 				clrbuf(bp);
145126f3deacStedu 			} else {
145226f3deacStedu 				error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
145393f62a9eStedu 					      ntfs_cntob(cl), &bp);
145426f3deacStedu 				if (error) {
145526f3deacStedu 					brelse(bp);
145626f3deacStedu 					return (error);
145726f3deacStedu 				}
145826f3deacStedu 			}
1459fca26c8fSmiod 			if (uio) {
1460fca26c8fSmiod 				error = uiomove(bp->b_data + off, tocopy, uio);
1461fca26c8fSmiod 				if (error != 0)
1462fca26c8fSmiod 					break;
1463fca26c8fSmiod 			} else
146426f3deacStedu 				memcpy(bp->b_data + off, data, tocopy);
146526f3deacStedu 			bawrite(bp);
146626f3deacStedu 			data = data + tocopy;
146726f3deacStedu 			*initp += tocopy;
146826f3deacStedu 			off = 0;
146926f3deacStedu 			left -= tocopy;
147026f3deacStedu 			cn += cl;
147126f3deacStedu 			ccl -= cl;
147226f3deacStedu 		}
147326f3deacStedu 	}
147426f3deacStedu 
1475fca26c8fSmiod 	if (left && error == 0) {
147626f3deacStedu 		printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
147726f3deacStedu 		error = EINVAL;
147826f3deacStedu 	}
147926f3deacStedu 
148026f3deacStedu 	return (error);
148126f3deacStedu }
148226f3deacStedu 
148326f3deacStedu /*
1484835c1779Sbrad  * This is one of the read routines.
148526f3deacStedu  *
148626f3deacStedu  * ntnode should be locked.
148726f3deacStedu  */
148826f3deacStedu int
14891cc0505dSjsing ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
14901cc0505dSjsing     struct ntvattr *vap, off_t roff, size_t rsize, void *rdata, size_t *initp,
149126f3deacStedu     struct uio *uio)
149226f3deacStedu {
149326f3deacStedu 	int             error = 0;
149426f3deacStedu 	int             off;
149526f3deacStedu 
149626f3deacStedu 	*initp = 0;
149726f3deacStedu 	if (vap->va_flag & NTFS_AF_INRUN) {
149826f3deacStedu 		int             cnt;
149926f3deacStedu 		cn_t            ccn, ccl, cn, left, cl;
150026f3deacStedu 		caddr_t         data = rdata;
150126f3deacStedu 		struct buf     *bp;
150226f3deacStedu 		size_t          tocopy;
150326f3deacStedu 
1504a1ea65c6Sjsing 		DDPRINTF("ntfs_readntvattr_plain: data in run: %lu chains\n",
1505a1ea65c6Sjsing 		    vap->va_vruncnt);
150626f3deacStedu 
150726f3deacStedu 		off = roff;
150826f3deacStedu 		left = rsize;
150926f3deacStedu 		ccl = 0;
151026f3deacStedu 		ccn = 0;
151126f3deacStedu 		cnt = 0;
151226f3deacStedu 		while (left && (cnt < vap->va_vruncnt)) {
151326f3deacStedu 			ccn = vap->va_vruncn[cnt];
151426f3deacStedu 			ccl = vap->va_vruncl[cnt];
151526f3deacStedu 
1516*f2997212Sjsing 			DDPRINTF("ntfs_readntvattr_plain: left %llu, "
1517*f2997212Sjsing 			    "cn: 0x%llx, cl: %llu, off: %lld\n",
1518*f2997212Sjsing 			    left, ccn, ccl, off);
151926f3deacStedu 
152026f3deacStedu 			if (ntfs_cntob(ccl) < off) {
152126f3deacStedu 				off -= ntfs_cntob(ccl);
152226f3deacStedu 				cnt++;
152326f3deacStedu 				continue;
152426f3deacStedu 			}
152526f3deacStedu 			if (ccn || ip->i_number == NTFS_BOOTINO) {
152626f3deacStedu 				ccl -= ntfs_btocn(off);
152726f3deacStedu 				cn = ccn + ntfs_btocn(off);
152826f3deacStedu 				off = ntfs_btocnoff(off);
152926f3deacStedu 
153026f3deacStedu 				while (left && ccl) {
153126f3deacStedu 					/*
1532835c1779Sbrad 					 * Always read single clusters at a
1533835c1779Sbrad 					 * time - we need to avoid reading
1534835c1779Sbrad 					 * differently-sized blocks at the
1535835c1779Sbrad 					 * same disk offsets to avoid
1536835c1779Sbrad 					 * confusing the buffer cache.
153726f3deacStedu 					 */
1538835c1779Sbrad 					tocopy = MIN(left,
1539835c1779Sbrad 					    ntfs_cntob(1) - off);
1540835c1779Sbrad 					cl = ntfs_btocl(tocopy + off);
1541835c1779Sbrad 					KASSERT(cl == 1 &&
1542835c1779Sbrad 					    tocopy <= ntfs_cntob(1));
154326f3deacStedu 
1544a1ea65c6Sjsing 					DDPRINTF("ntfs_readntvattr_plain: "
1545*f2997212Sjsing 					    "read: cn: 0x%llx cl: %llu, "
1546*f2997212Sjsing 					    "off: %lld, len: %llu, "
1547*f2997212Sjsing 					    "left: %llu\n",
1548*f2997212Sjsing 					    cn, cl, off, tocopy, left);
154926f3deacStedu 					error = bread(ntmp->ntm_devvp,
155026f3deacStedu 						      ntfs_cntobn(cn),
155126f3deacStedu 						      ntfs_cntob(cl),
155293f62a9eStedu 						      &bp);
155326f3deacStedu 					if (error) {
155426f3deacStedu 						brelse(bp);
155526f3deacStedu 						return (error);
155626f3deacStedu 					}
155726f3deacStedu 					if (uio) {
1558fca26c8fSmiod 						error = uiomove(bp->b_data + off,
155926f3deacStedu 							tocopy, uio);
1560fca26c8fSmiod 						if (error != 0)
1561fca26c8fSmiod 							break;
156226f3deacStedu 					} else {
156326f3deacStedu 						memcpy(data, bp->b_data + off,
156426f3deacStedu 							tocopy);
156526f3deacStedu 					}
156626f3deacStedu 					brelse(bp);
156726f3deacStedu 					data = data + tocopy;
156826f3deacStedu 					*initp += tocopy;
156926f3deacStedu 					off = 0;
157026f3deacStedu 					left -= tocopy;
157126f3deacStedu 					cn += cl;
157226f3deacStedu 					ccl -= cl;
157326f3deacStedu 				}
157426f3deacStedu 			} else {
1575835c1779Sbrad 				tocopy = MIN(left, ntfs_cntob(ccl) - off);
1576a1ea65c6Sjsing 				DDPRINTF("ntfs_readntvattr_plain: hole: "
1577*f2997212Sjsing 				    "ccn: 0x%llx ccl: %llu, off: %lld, "
1578*f2997212Sjsing 				    "len: %llu, left: %llu\n",
1579*f2997212Sjsing 				    ccn, ccl, off, tocopy, left);
158026f3deacStedu 				left -= tocopy;
158126f3deacStedu 				off = 0;
158226f3deacStedu 				if (uio) {
158326f3deacStedu 					size_t remains = tocopy;
1584fca26c8fSmiod 					for(; remains; remains--) {
1585fca26c8fSmiod 						error = uiomove("", 1, uio);
1586fca26c8fSmiod 						if (error != 0)
1587fca26c8fSmiod 							break;
1588fca26c8fSmiod 					}
158926f3deacStedu 				} else
159026f3deacStedu 					bzero(data, tocopy);
159126f3deacStedu 				data = data + tocopy;
159226f3deacStedu 			}
159326f3deacStedu 			cnt++;
1594fca26c8fSmiod 			if (error != 0)
1595fca26c8fSmiod 				break;
159626f3deacStedu 		}
1597fca26c8fSmiod 		if (left && error == 0) {
159826f3deacStedu 			printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
159926f3deacStedu 			error = E2BIG;
160026f3deacStedu 		}
160126f3deacStedu 	} else {
1602a1ea65c6Sjsing 		DDPRINTF("ntfs_readnvattr_plain: data is in mft record\n");
160326f3deacStedu 		if (uio)
1604fca26c8fSmiod 			error = uiomove(vap->va_datap + roff, rsize, uio);
160526f3deacStedu 		else
160626f3deacStedu 			memcpy(rdata, vap->va_datap + roff, rsize);
160726f3deacStedu 		*initp += rsize;
160826f3deacStedu 	}
160926f3deacStedu 
161026f3deacStedu 	return (error);
161126f3deacStedu }
161226f3deacStedu 
161326f3deacStedu /*
161426f3deacStedu  * This is one of read routines.
161526f3deacStedu  */
161626f3deacStedu int
16171cc0505dSjsing ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
16181cc0505dSjsing     u_int32_t attrnum, char *attrname, off_t roff, size_t rsize, void *rdata,
16191cc0505dSjsing     size_t *initp, struct uio *uio)
162026f3deacStedu {
162126f3deacStedu 	size_t          init;
162226f3deacStedu 	int             error = 0;
162326f3deacStedu 	off_t           off = roff, left = rsize, toread;
162426f3deacStedu 	caddr_t         data = rdata;
162526f3deacStedu 	struct ntvattr *vap;
162626f3deacStedu 	*initp = 0;
162726f3deacStedu 
162826f3deacStedu 	while (left) {
162926f3deacStedu 		error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
163026f3deacStedu 					ntfs_btocn(off), &vap);
163126f3deacStedu 		if (error)
163226f3deacStedu 			return (error);
1633835c1779Sbrad 		toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1634*f2997212Sjsing 		DDPRINTF("ntfs_readattr_plain: o: %lld, s: %lld "
1635*f2997212Sjsing 		    "(%llu - %llu)\n", off, toread,
1636*f2997212Sjsing 		    vap->va_vcnstart, vap->va_vcnend);
163726f3deacStedu 		error = ntfs_readntvattr_plain(ntmp, ip, vap,
163826f3deacStedu 					 off - ntfs_cntob(vap->va_vcnstart),
163926f3deacStedu 					 toread, data, &init, uio);
164026f3deacStedu 		if (error) {
164126f3deacStedu 			printf("ntfs_readattr_plain: " \
164226f3deacStedu 			       "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
164326f3deacStedu 			       (u_int32_t) off, (u_int32_t) toread);
164426f3deacStedu 			printf("ntfs_readattr_plain: attrib: %d - %d\n",
164526f3deacStedu 			       (u_int32_t) vap->va_vcnstart,
164626f3deacStedu 			       (u_int32_t) vap->va_vcnend);
164726f3deacStedu 			ntfs_ntvattrrele(vap);
164826f3deacStedu 			break;
164926f3deacStedu 		}
165026f3deacStedu 		ntfs_ntvattrrele(vap);
165126f3deacStedu 		left -= toread;
165226f3deacStedu 		off += toread;
165326f3deacStedu 		data = data + toread;
165426f3deacStedu 		*initp += init;
165526f3deacStedu 	}
165626f3deacStedu 
165726f3deacStedu 	return (error);
165826f3deacStedu }
165926f3deacStedu 
166026f3deacStedu /*
166126f3deacStedu  * This is one of read routines.
166226f3deacStedu  */
166326f3deacStedu int
16641cc0505dSjsing ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
16651cc0505dSjsing     char *attrname, off_t roff, size_t rsize, void *rdata, struct uio *uio)
166626f3deacStedu {
166726f3deacStedu 	int             error = 0;
166826f3deacStedu 	struct ntvattr *vap;
166926f3deacStedu 	size_t          init;
167026f3deacStedu 
1671*f2997212Sjsing 	DDPRINTF("ntfs_readattr: reading %u: 0x%x, from %lld size %zu bytes\n",
1672*f2997212Sjsing 	    ip->i_number, attrnum, roff, rsize);
167326f3deacStedu 
167426f3deacStedu 	error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
167526f3deacStedu 	if (error)
167626f3deacStedu 		return (error);
167726f3deacStedu 
167826f3deacStedu 	if ((roff > vap->va_datalen) ||
167926f3deacStedu 	    (roff + rsize > vap->va_datalen)) {
1680*f2997212Sjsing 		printf("ntfs_readattr: offset too big: %lld (%lld) > %u\n",
1681*f2997212Sjsing 		    roff, roff + rsize, vap->va_datalen);
168226f3deacStedu 		ntfs_ntvattrrele(vap);
168326f3deacStedu 		return (E2BIG);
168426f3deacStedu 	}
168526f3deacStedu 	if (vap->va_compression && vap->va_compressalg) {
168626f3deacStedu 		u_int8_t       *cup;
168726f3deacStedu 		u_int8_t       *uup;
168826f3deacStedu 		off_t           off = roff, left = rsize, tocopy;
168926f3deacStedu 		caddr_t         data = rdata;
169026f3deacStedu 		cn_t            cn;
169126f3deacStedu 
1692*f2997212Sjsing 		DDPRINTF("ntfs_ntreadattr: compression: %u\n",
1693a1ea65c6Sjsing 		    vap->va_compressalg);
169426f3deacStedu 
1695e8821f89Shshoexer 		cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1696e8821f89Shshoexer 		    M_WAITOK);
1697e8821f89Shshoexer 		uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1698e8821f89Shshoexer 		    M_WAITOK);
169926f3deacStedu 
170026f3deacStedu 		cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
170126f3deacStedu 		off = roff - ntfs_cntob(cn);
170226f3deacStedu 
170326f3deacStedu 		while (left) {
170426f3deacStedu 			error = ntfs_readattr_plain(ntmp, ip, attrnum,
170526f3deacStedu 						  attrname, ntfs_cntob(cn),
170626f3deacStedu 					          ntfs_cntob(NTFS_COMPUNIT_CL),
170726f3deacStedu 						  cup, &init, NULL);
170826f3deacStedu 			if (error)
170926f3deacStedu 				break;
171026f3deacStedu 
1711835c1779Sbrad 			tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
171226f3deacStedu 
171326f3deacStedu 			if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
171426f3deacStedu 				if (uio)
1715fca26c8fSmiod 					error = uiomove(cup + off, tocopy, uio);
171626f3deacStedu 				else
171726f3deacStedu 					memcpy(data, cup + off, tocopy);
171826f3deacStedu 			} else if (init == 0) {
171926f3deacStedu 				if (uio) {
172026f3deacStedu 					size_t remains = tocopy;
1721fca26c8fSmiod 					for(; remains; remains--) {
1722fca26c8fSmiod 						error = uiomove("", 1, uio);
1723fca26c8fSmiod 						if (error != 0)
1724fca26c8fSmiod 							break;
1725fca26c8fSmiod 					}
172626f3deacStedu 				}
172726f3deacStedu 				else
172826f3deacStedu 					bzero(data, tocopy);
172926f3deacStedu 			} else {
173026f3deacStedu 				error = ntfs_uncompunit(ntmp, uup, cup);
173126f3deacStedu 				if (error)
173226f3deacStedu 					break;
173326f3deacStedu 				if (uio)
1734fca26c8fSmiod 					error = uiomove(uup + off, tocopy, uio);
173526f3deacStedu 				else
173626f3deacStedu 					memcpy(data, uup + off, tocopy);
173726f3deacStedu 			}
1738fca26c8fSmiod 			if (error)
1739fca26c8fSmiod 				break;
174026f3deacStedu 
174126f3deacStedu 			left -= tocopy;
174226f3deacStedu 			data = data + tocopy;
174326f3deacStedu 			off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
174426f3deacStedu 			cn += NTFS_COMPUNIT_CL;
174526f3deacStedu 		}
174626f3deacStedu 
1747e8821f89Shshoexer 		free(uup, M_NTFSDECOMP);
1748e8821f89Shshoexer 		free(cup, M_NTFSDECOMP);
174926f3deacStedu 	} else
175026f3deacStedu 		error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
175126f3deacStedu 					     roff, rsize, rdata, &init, uio);
175226f3deacStedu 	ntfs_ntvattrrele(vap);
175326f3deacStedu 	return (error);
175426f3deacStedu }
175526f3deacStedu 
175626f3deacStedu #if UNUSED_CODE
175726f3deacStedu int
17581cc0505dSjsing ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
175926f3deacStedu {
176026f3deacStedu 	u_int8_t        sz;
176126f3deacStedu 	int             i;
176226f3deacStedu 
176326f3deacStedu 	if (NULL == run) {
176426f3deacStedu 		printf("ntfs_parsetun: run == NULL\n");
176526f3deacStedu 		return (EINVAL);
176626f3deacStedu 	}
176726f3deacStedu 	sz = run[(*off)++];
176826f3deacStedu 	if (0 == sz) {
176926f3deacStedu 		printf("ntfs_parserun: trying to go out of run\n");
177026f3deacStedu 		return (E2BIG);
177126f3deacStedu 	}
177226f3deacStedu 	*cl = 0;
177326f3deacStedu 	if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
177426f3deacStedu 		printf("ntfs_parserun: " \
177526f3deacStedu 		       "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
177626f3deacStedu 		       sz, len, *off);
177726f3deacStedu 		return (EINVAL);
177826f3deacStedu 	}
177926f3deacStedu 	for (i = 0; i < (sz & 0xF); i++)
178026f3deacStedu 		*cl += (u_int32_t) run[(*off)++] << (i << 3);
178126f3deacStedu 
178226f3deacStedu 	sz >>= 4;
178326f3deacStedu 	if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
178426f3deacStedu 		printf("ntfs_parserun: " \
178526f3deacStedu 		       "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
178626f3deacStedu 		       sz, len, *off);
178726f3deacStedu 		return (EINVAL);
178826f3deacStedu 	}
178926f3deacStedu 	for (i = 0; i < (sz & 0xF); i++)
179026f3deacStedu 		*cn += (u_int32_t) run[(*off)++] << (i << 3);
179126f3deacStedu 
179226f3deacStedu 	return (0);
179326f3deacStedu }
179426f3deacStedu #endif
179526f3deacStedu 
179626f3deacStedu /*
179726f3deacStedu  * Process fixup routine on given buffer.
179826f3deacStedu  */
179926f3deacStedu int
18001cc0505dSjsing ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
180126f3deacStedu     size_t len)
180226f3deacStedu {
180326f3deacStedu 	struct fixuphdr *fhp = (struct fixuphdr *) buf;
180426f3deacStedu 	int             i;
180526f3deacStedu 	u_int16_t       fixup;
180626f3deacStedu 	u_int16_t      *fxp;
180726f3deacStedu 	u_int16_t      *cfxp;
180826f3deacStedu 
180926f3deacStedu 	if (fhp->fh_magic != magic) {
181026f3deacStedu 		printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
181126f3deacStedu 		       fhp->fh_magic, magic);
181226f3deacStedu 		return (EINVAL);
181326f3deacStedu 	}
181426f3deacStedu 	if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
181526f3deacStedu 		printf("ntfs_procfixups: " \
181626f3deacStedu 		       "bad fixups number: %d for %ld bytes block\n",
181726f3deacStedu 		       fhp->fh_fnum, (long)len);	/* XXX printf kludge */
181826f3deacStedu 		return (EINVAL);
181926f3deacStedu 	}
182026f3deacStedu 	if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
182126f3deacStedu 		printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
182226f3deacStedu 		return (EINVAL);
182326f3deacStedu 	}
182426f3deacStedu 	fxp = (u_int16_t *) (buf + fhp->fh_foff);
182526f3deacStedu 	cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
182626f3deacStedu 	fixup = *fxp++;
182726f3deacStedu 	for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
182826f3deacStedu 		if (*cfxp != fixup) {
182926f3deacStedu 			printf("ntfs_procfixups: fixup %d doesn't match\n", i);
183026f3deacStedu 			return (EINVAL);
183126f3deacStedu 		}
183226f3deacStedu 		*cfxp = *fxp;
1833835c1779Sbrad 		cfxp = (u_int16_t *)((caddr_t)cfxp + ntmp->ntm_bps);
183426f3deacStedu 	}
183526f3deacStedu 	return (0);
183626f3deacStedu }
183726f3deacStedu 
183826f3deacStedu #if UNUSED_CODE
183926f3deacStedu int
18401cc0505dSjsing ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
184126f3deacStedu     cn_t vcn)
184226f3deacStedu {
184326f3deacStedu 	cn_t            ccn = 0;
184426f3deacStedu 	cn_t            ccl = 0;
184526f3deacStedu 	u_long          off = 0;
184626f3deacStedu 	int             error = 0;
184726f3deacStedu 
184826f3deacStedu #if NTFS_DEBUG
184926f3deacStedu 	int             i;
1850846c180eSbrad 	printf("ntfs_runtocn: run: %p, %ld bytes, vcn:%ld\n",
185126f3deacStedu 		run, len, (u_long) vcn);
185226f3deacStedu 	printf("ntfs_runtocn: run: ");
185326f3deacStedu 	for (i = 0; i < len; i++)
185426f3deacStedu 		printf("0x%02x ", run[i]);
185526f3deacStedu 	printf("\n");
185626f3deacStedu #endif
185726f3deacStedu 
185826f3deacStedu 	if (NULL == run) {
185926f3deacStedu 		printf("ntfs_runtocn: run == NULL\n");
186026f3deacStedu 		return (EINVAL);
186126f3deacStedu 	}
186226f3deacStedu 	do {
186326f3deacStedu 		if (run[off] == 0) {
186426f3deacStedu 			printf("ntfs_runtocn: vcn too big\n");
186526f3deacStedu 			return (E2BIG);
186626f3deacStedu 		}
186726f3deacStedu 		vcn -= ccl;
186826f3deacStedu 		error = ntfs_parserun(&ccn, &ccl, run, len, &off);
186926f3deacStedu 		if (error) {
187026f3deacStedu 			printf("ntfs_runtocn: ntfs_parserun failed\n");
187126f3deacStedu 			return (error);
187226f3deacStedu 		}
187326f3deacStedu 	} while (ccl <= vcn);
187426f3deacStedu 	*cn = ccn + vcn;
187526f3deacStedu 	return (0);
187626f3deacStedu }
187726f3deacStedu #endif
187826f3deacStedu 
187926f3deacStedu /*
1880274476fcStedu  * this initializes toupper table & dependant variables to be ready for
1881274476fcStedu  * later work
1882274476fcStedu  */
1883274476fcStedu void
18841cc0505dSjsing ntfs_toupper_init(void)
1885274476fcStedu {
1886274476fcStedu 	ntfs_toupper_tab = (wchar *) NULL;
1887274476fcStedu 	ntfs_toupper_usecount = 0;
1888274476fcStedu }
1889274476fcStedu 
1890274476fcStedu /*
1891274476fcStedu  * if the ntfs_toupper_tab[] is filled already, just raise use count;
1892274476fcStedu  * otherwise read the data from the filesystem we are currently mounting
189326f3deacStedu  */
189426f3deacStedu int
18951cc0505dSjsing ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp, struct proc *p)
189626f3deacStedu {
189726f3deacStedu 	int error = 0;
189826f3deacStedu 	struct vnode *vp;
189926f3deacStedu 
1900274476fcStedu 	/* get exclusive access */
1901274476fcStedu 	rw_enter_write(&ntfs_toupper_lock);
1902274476fcStedu 
190326f3deacStedu 	/* only read the translation data from a file if it hasn't been
190426f3deacStedu 	 * read already */
190526f3deacStedu 	if (ntfs_toupper_tab)
190626f3deacStedu 		goto out;
190726f3deacStedu 
190826f3deacStedu 	/*
190926f3deacStedu 	 * Read in Unicode lowercase -> uppercase translation file.
191026f3deacStedu 	 * XXX for now, just the first 256 entries are used anyway,
191126f3deacStedu 	 * so don't bother reading more
191226f3deacStedu 	 */
1913274476fcStedu 	ntfs_toupper_tab = malloc(256 * 256 * sizeof(wchar), M_NTFSRDATA,
1914274476fcStedu 	    M_WAITOK);
191526f3deacStedu 
191626f3deacStedu 	if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
191726f3deacStedu 		goto out;
1918274476fcStedu 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1919274476fcStedu 			0, 256*256*sizeof(wchar), (char *) ntfs_toupper_tab,
1920274476fcStedu 			NULL);
192126f3deacStedu 	vput(vp);
192226f3deacStedu 
1923274476fcStedu     out:
1924274476fcStedu 	ntfs_toupper_usecount++;
1925274476fcStedu 	rw_exit_write(&ntfs_toupper_lock);
1926274476fcStedu 	return (error);
1927e7116e76Stedu }
1928e7116e76Stedu 
1929274476fcStedu /*
1930274476fcStedu  * lower the use count and if it reaches zero, free the memory
1931274476fcStedu  * tied by toupper table
1932274476fcStedu  */
1933274476fcStedu void
19341cc0505dSjsing ntfs_toupper_unuse(struct proc *p)
1935274476fcStedu {
1936274476fcStedu 	/* get exclusive access */
1937274476fcStedu 	rw_enter_write(&ntfs_toupper_lock);
1938e7116e76Stedu 
1939274476fcStedu 	ntfs_toupper_usecount--;
1940274476fcStedu 	if (ntfs_toupper_usecount == 0) {
1941274476fcStedu 		free(ntfs_toupper_tab, M_NTFSRDATA);
1942274476fcStedu 		ntfs_toupper_tab = NULL;
1943274476fcStedu 	}
1944274476fcStedu #ifdef DIAGNOSTIC
1945274476fcStedu 	else if (ntfs_toupper_usecount < 0) {
1946274476fcStedu 		panic("ntfs_toupper_unuse(): use count negative: %d",
1947274476fcStedu 			ntfs_toupper_usecount);
1948274476fcStedu 	}
1949274476fcStedu #endif
1950274476fcStedu 
1951274476fcStedu 	/* release the lock */
1952274476fcStedu 	rw_exit_write(&ntfs_toupper_lock);
195326f3deacStedu }
1954