xref: /openbsd/sys/ntfs/ntfs_subr.c (revision 0d297f47)
1 /*	$OpenBSD: ntfs_subr.c,v 1.52 2022/01/11 03:13:59 jsg Exp $	*/
2 /*	$NetBSD: ntfs_subr.c,v 1.4 2003/04/10 21:37:32 jdolecek Exp $	*/
3 
4 /*-
5  * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org)
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	Id: ntfs_subr.c,v 1.4 1999/05/12 09:43:01 semenu Exp
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/namei.h>
35 #include <sys/kernel.h>
36 #include <sys/vnode.h>
37 #include <sys/lock.h>
38 #include <sys/mount.h>
39 #include <sys/buf.h>
40 #include <sys/malloc.h>
41 #include <sys/rwlock.h>
42 #include <sys/specdev.h>
43 
44 /* #define NTFS_DEBUG 1 */
45 #include <ntfs/ntfs.h>
46 #include <ntfs/ntfsmount.h>
47 #include <ntfs/ntfs_inode.h>
48 #include <ntfs/ntfs_vfsops.h>
49 #include <ntfs/ntfs_subr.h>
50 #include <ntfs/ntfs_compr.h>
51 #include <ntfs/ntfs_ihash.h>
52 
53 #if defined(NTFS_DEBUG)
54 int ntfs_debug = NTFS_DEBUG;
55 #endif
56 
57 /* Local struct used in ntfs_ntlookupfile() */
58 struct ntfs_lookup_ctx {
59 	u_int32_t	aoff;
60 	u_int32_t	rdsize;
61 	cn_t		cn;
62 	struct ntfs_lookup_ctx *prev;
63 };
64 
65 int ntfs_ntlookupattr(struct ntfsmount *, const char *, int, int *, char **);
66 int ntfs_findvattr(struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
67 int ntfs_uastricmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
68 int ntfs_uastrcmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
69 
70 /* table for mapping Unicode chars into uppercase; it's filled upon first
71  * ntfs mount, freed upon last ntfs umount */
72 static wchar *ntfs_toupper_tab;
73 #define NTFS_U28(ch)		((((ch) & 0xE0) == 0) ? '_' : (ch) & 0xFF)
74 #define NTFS_TOUPPER(ch)	(ntfs_toupper_tab[(unsigned char)(ch)])
75 struct rwlock ntfs_toupper_lock = RWLOCK_INITIALIZER("ntfs_toupper");
76 static signed int ntfs_toupper_usecount;
77 
78 /* support macro for ntfs_ntvattrget() */
79 #define NTFS_AALPCMP(aalp,type,name,namelen) (				\
80   (aalp->al_type == type) && (aalp->al_namelen == namelen) &&		\
81   !ntfs_uastrcmp(ntmp, aalp->al_name,aalp->al_namelen,name,namelen) )
82 
83 /*
84  *
85  */
86 int
ntfs_ntvattrrele(struct ntvattr * vap)87 ntfs_ntvattrrele(struct ntvattr *vap)
88 {
89 	DPRINTF("ntfs_ntvattrrele: ino: %u, type: 0x%x\n",
90 	    vap->va_ip->i_number, vap->va_type);
91 
92 	ntfs_ntrele(vap->va_ip);
93 
94 	return (0);
95 }
96 
97 /*
98  * find the attribute in the ntnode
99  */
100 int
ntfs_findvattr(struct ntfsmount * ntmp,struct ntnode * ip,struct ntvattr ** lvapp,struct ntvattr ** vapp,u_int32_t type,const char * name,size_t namelen,cn_t vcn)101 ntfs_findvattr(struct ntfsmount *ntmp, struct ntnode *ip,
102     struct ntvattr **lvapp, struct ntvattr **vapp, u_int32_t type,
103     const char *name, size_t namelen, cn_t vcn)
104 {
105 	int error;
106 	struct ntvattr *vap;
107 
108 	if((ip->i_flag & IN_LOADED) == 0) {
109 		DPRINTF("ntfs_findvattr: node not loaded, ino: %u\n",
110 		    ip->i_number);
111 		error = ntfs_loadntnode(ntmp,ip);
112 		if (error) {
113 			printf("ntfs_findvattr: FAILED TO LOAD INO: %d\n",
114 			       ip->i_number);
115 			return (error);
116 		}
117 	} else {
118 		/* Update LRU loaded list. */
119 		TAILQ_REMOVE(&ntmp->ntm_ntnodeq, ip, i_loaded);
120 		TAILQ_INSERT_HEAD(&ntmp->ntm_ntnodeq, ip, i_loaded);
121 	}
122 
123 	*lvapp = NULL;
124 	*vapp = NULL;
125 	LIST_FOREACH(vap, &ip->i_valist, va_list) {
126 		DDPRINTF("ntfs_findvattr: type: 0x%x, vcn: %llu - %llu\n",
127 		    vap->va_type, vap->va_vcnstart, vap->va_vcnend);
128 		if ((vap->va_type == type) &&
129 		    (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
130 		    (vap->va_namelen == namelen) &&
131 		    (strncmp(name, vap->va_name, namelen) == 0)) {
132 			*vapp = vap;
133 			ntfs_ntref(vap->va_ip);
134 			return (0);
135 		}
136 		if (vap->va_type == NTFS_A_ATTRLIST)
137 			*lvapp = vap;
138 	}
139 
140 	return (-1);
141 }
142 
143 /*
144  * Search attribute specified in ntnode (load ntnode if necessary).
145  * If not found but ATTR_A_ATTRLIST present, read it in and search through.
146  * VOP_VGET node needed, and lookup through its ntnode (load if necessary).
147  *
148  * ntnode should be locked
149  */
150 int
ntfs_ntvattrget(struct ntfsmount * ntmp,struct ntnode * ip,u_int32_t type,const char * name,cn_t vcn,struct ntvattr ** vapp)151 ntfs_ntvattrget(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t type,
152     const char *name, cn_t vcn, struct ntvattr **vapp)
153 {
154 	struct ntvattr *lvap = NULL;
155 	struct attr_attrlist *aalp;
156 	struct attr_attrlist *nextaalp;
157 	struct vnode   *newvp;
158 	struct ntnode  *newip;
159 	caddr_t         alpool;
160 	size_t		namelen, len;
161 	int             error;
162 
163 	*vapp = NULL;
164 
165 	if (name) {
166 		DPRINTF("ntfs_ntvattrget: ino: %u, type: 0x%x, name: %s, "
167 		    "vcn: %llu\n", ip->i_number, type, name, vcn);
168 		namelen = strlen(name);
169 	} else {
170 		DPRINTF("ntfs_ntvattrget: ino: %u, type: 0x%x, vcn: %llu\n",
171 		    ip->i_number, type, vcn);
172 		name = "";
173 		namelen = 0;
174 	}
175 
176 	error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
177 	if (error >= 0)
178 		return (error);
179 
180 	if (!lvap) {
181 		DPRINTF("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: ino: %u, "
182 		    "type: 0x%x, name: %s, vcn: %llu\n", ip->i_number, type,
183 		    name, vcn);
184 		return (ENOENT);
185 	}
186 	/* Scan $ATTRIBUTE_LIST for requested attribute */
187 	len = lvap->va_datalen;
188 	alpool = malloc(len, M_TEMP, M_WAITOK);
189 	error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
190 			NULL);
191 	if (error)
192 		goto out;
193 
194 	aalp = (struct attr_attrlist *) alpool;
195 	nextaalp = NULL;
196 
197 	for(; len > 0; aalp = nextaalp) {
198 		DPRINTF("ntfs_ntvattrget: attrlist: ino: %u, attr: 0x%x, "
199 		    "vcn: %llu\n", aalp->al_inumber, aalp->al_type,
200 		    aalp->al_vcnstart);
201 
202 		if (len > aalp->reclen) {
203 			nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
204 		} else {
205 			nextaalp = NULL;
206 		}
207 		len -= aalp->reclen;
208 
209 		if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
210 		    (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
211 		     NTFS_AALPCMP(nextaalp, type, name, namelen)))
212 			continue;
213 
214 		DPRINTF("ntfs_ntvattrget: attribute in ino: %u\n",
215 		    aalp->al_inumber);
216 
217 		/* this is not a main record, so we can't use just plain
218 		   vget() */
219 		error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
220 				NTFS_A_DATA, NULL, LK_EXCLUSIVE,
221 				VG_EXT, &newvp);
222 		if (error) {
223 			printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
224 			       aalp->al_inumber);
225 			goto out;
226 		}
227 		newip = VTONT(newvp);
228 		/* XXX have to lock ntnode */
229 		error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
230 				type, name, namelen, vcn);
231 		vput(newvp);
232 		if (error == 0)
233 			goto out;
234 		printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
235 		break;
236 	}
237 	error = ENOENT;
238 
239 	DPRINTF("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: ino: %u, type: 0x%x, "
240 	    "name: %.*s, vcn: %llu\n", ip->i_number, type,
241 	    (unsigned int)namelen, name, vcn);
242 out:
243 	free(alpool, M_TEMP, 0);
244 	return (error);
245 }
246 
247 /*
248  * Read ntnode from disk, make ntvattr list.
249  *
250  * ntnode should be locked
251  */
252 int
ntfs_loadntnode(struct ntfsmount * ntmp,struct ntnode * ip)253 ntfs_loadntnode(struct ntfsmount *ntmp, struct ntnode *ip)
254 {
255 	struct ntnode	*oip;
256 	struct ntvattr	*vap;
257 	struct filerec	*mfrp;
258 	struct attr	*ap;
259 	daddr_t		bn;
260  	int		error,off;
261 
262  	DPRINTF("ntfs_loadntnode: loading ino: %u\n", ip->i_number);
263 
264 	KASSERT((ip->i_flag & IN_LOADED) == 0);
265 
266 	if (ntmp->ntm_ntnodes >= LOADED_NTNODE_HI) {
267 		oip = TAILQ_LAST(&ntmp->ntm_ntnodeq, ntnodeq);
268 		TAILQ_REMOVE(&ntmp->ntm_ntnodeq, oip, i_loaded);
269 		ntmp->ntm_ntnodes--;
270 
271 		DPRINTF("ntfs_loadntnode: unloading ino: %u\n", oip->i_number);
272 
273 		KASSERT((oip->i_flag & IN_LOADED));
274 		oip->i_flag &= ~IN_LOADED;
275 		while ((vap = LIST_FIRST(&oip->i_valist)) != NULL) {
276 			LIST_REMOVE(vap, va_list);
277 			ntfs_freentvattr(vap);
278 		}
279 	}
280 
281  	mfrp = malloc(ntfs_bntob(ntmp->ntm_bpmftrec), M_TEMP, M_WAITOK);
282 
283 	if (ip->i_number < NTFS_SYSNODESNUM) {
284 		struct buf     *bp;
285 
286 		DPRINTF("ntfs_loadntnode: read system node\n");
287 
288 		bn = ntfs_cntobn(ntmp->ntm_mftcn) +
289 			ntmp->ntm_bpmftrec * ip->i_number;
290 
291 		error = bread(ntmp->ntm_devvp, bn,
292 		    ntfs_bntob(ntmp->ntm_bpmftrec), &bp);
293 		if (error) {
294 			printf("ntfs_loadntnode: BREAD FAILED\n");
295 			brelse(bp);
296 			goto out;
297 		}
298 		memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
299 		brelse(bp);
300 	} else {
301 		struct vnode   *vp;
302 
303 		vp = ntmp->ntm_sysvn[NTFS_MFTINO];
304 		error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
305 			       ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
306 			       ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
307 		if (error) {
308 			printf("ntfs_loadntnode: ntfs_readattr failed\n");
309 			goto out;
310 		}
311 	}
312 
313 	/* Check if magic and fixups are correct */
314 	error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
315 				ntfs_bntob(ntmp->ntm_bpmftrec));
316 	if (error) {
317 		printf("ntfs_loadntnode: BAD MFT RECORD %d\n",
318 		       (u_int32_t) ip->i_number);
319 		goto out;
320 	}
321 
322 	DPRINTF("ntfs_loadntnode: load attrs for ino: %u\n", ip->i_number);
323 	off = mfrp->fr_attroff;
324 	ap = (struct attr *) ((caddr_t)mfrp + off);
325 
326 	LIST_INIT(&ip->i_valist);
327 
328 	while (ap->a_hdr.a_type != -1) {
329 		error = ntfs_attrtontvattr(ntmp, &vap, ap);
330 		if (error)
331 			break;
332 		vap->va_ip = ip;
333 
334 		LIST_INSERT_HEAD(&ip->i_valist, vap, va_list);
335 
336 		off += ap->a_hdr.reclen;
337 		ap = (struct attr *) ((caddr_t)mfrp + off);
338 	}
339 	if (error) {
340 		printf("ntfs_loadntnode: failed to load attr ino: %d\n",
341 		       ip->i_number);
342 		goto out;
343 	}
344 
345 	ip->i_mainrec = mfrp->fr_mainrec;
346 	ip->i_nlink = mfrp->fr_nlink;
347 	ip->i_frflag = mfrp->fr_flags;
348 
349 	ip->i_flag |= IN_LOADED;
350 
351 	/* Add to loaded list. */
352 	TAILQ_INSERT_HEAD(&ntmp->ntm_ntnodeq, ip, i_loaded);
353 	ntmp->ntm_ntnodes++;
354 
355 out:
356 	free(mfrp, M_TEMP, 0);
357 	return (error);
358 }
359 
360 /*
361  * Routine locks ntnode and increase usecount, just opposite of
362  * ntfs_ntput().
363  */
364 int
ntfs_ntget(struct ntnode * ip)365 ntfs_ntget(struct ntnode *ip)
366 {
367 	DPRINTF("ntfs_ntget: get ntnode %u: %p, usecount: %d\n",
368 	    ip->i_number, ip, ip->i_usecount);
369 
370 	ip->i_usecount++;
371 
372 	rw_enter_write(&ip->i_lock);
373 
374 	return 0;
375 }
376 
377 /*
378  * Routine search ntnode in hash, if found: lock, inc usecount and return.
379  * If not in hash allocate structure for ntnode, prefill it, lock,
380  * inc count and return.
381  *
382  * ntnode returned locked
383  */
384 int
ntfs_ntlookup(struct ntfsmount * ntmp,ntfsino_t ino,struct ntnode ** ipp)385 ntfs_ntlookup(struct ntfsmount *ntmp, ntfsino_t ino, struct ntnode **ipp)
386 {
387 	struct ntnode  *ip;
388 
389 	DPRINTF("ntfs_ntlookup: looking for ntnode %u\n", ino);
390 
391 	do {
392 		if ((ip = ntfs_nthashlookup(ntmp->ntm_dev, ino)) != NULL) {
393 			ntfs_ntget(ip);
394 			DPRINTF("ntfs_ntlookup: ntnode %u: %p, usecount: %d\n",
395 			    ino, ip, ip->i_usecount);
396 			*ipp = ip;
397 			return (0);
398 		}
399 	} while (rw_enter(&ntfs_hashlock, RW_WRITE | RW_SLEEPFAIL));
400 
401 	ip = malloc(sizeof(*ip), M_NTFSNTNODE, M_WAITOK | M_ZERO);
402 	DDPRINTF("ntfs_ntlookup: allocating ntnode: %u: %p\n", ino, ip);
403 
404 	/* Generic initialization */
405 	ip->i_devvp = ntmp->ntm_devvp;
406 	ip->i_dev = ntmp->ntm_dev;
407 	ip->i_number = ino;
408 	ip->i_mp = ntmp;
409 
410 	LIST_INIT(&ip->i_fnlist);
411 	vref(ip->i_devvp);
412 
413 	/* init lock and lock the newborn ntnode */
414 	rw_init(&ip->i_lock, "ntnode");
415 	ntfs_ntget(ip);
416 
417 	ntfs_nthashins(ip);
418 
419 	rw_exit(&ntfs_hashlock);
420 
421 	*ipp = ip;
422 
423 	DPRINTF("ntfs_ntlookup: ntnode %u: %p, usecount: %d\n",
424 	    ino, ip, ip->i_usecount);
425 
426 	return (0);
427 }
428 
429 /*
430  * Decrement usecount of ntnode and unlock it, if usecount reach zero,
431  * deallocate ntnode.
432  *
433  * ntnode should be locked on entry, and unlocked on return.
434  */
435 void
ntfs_ntput(struct ntnode * ip)436 ntfs_ntput(struct ntnode *ip)
437 {
438 	struct ntfsmount *ntmp = ip->i_mp;
439 	struct ntvattr *vap;
440 
441 	DPRINTF("ntfs_ntput: rele ntnode %u: %p, usecount: %d\n",
442 	    ip->i_number, ip, ip->i_usecount);
443 
444 	ip->i_usecount--;
445 
446 #ifdef DIAGNOSTIC
447 	if (ip->i_usecount < 0) {
448 		panic("ntfs_ntput: ino: %d usecount: %d ",
449 		      ip->i_number,ip->i_usecount);
450 	}
451 #endif
452 
453 	if (ip->i_usecount > 0) {
454 		rw_exit_write(&ip->i_lock);
455 		return;
456 	}
457 
458 	DPRINTF("ntfs_ntput: deallocating ntnode: %u\n", ip->i_number);
459 
460 	if (LIST_FIRST(&ip->i_fnlist))
461 		panic("ntfs_ntput: ntnode has fnodes");
462 
463 	ntfs_nthashrem(ip);
464 
465 	/* Remove from loaded list. */
466 	if (ip->i_flag & IN_LOADED) {
467 		TAILQ_REMOVE(&ntmp->ntm_ntnodeq, ip, i_loaded);
468 		ntmp->ntm_ntnodes--;
469 	}
470 
471 	while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
472 		LIST_REMOVE(vap, va_list);
473 		ntfs_freentvattr(vap);
474 	}
475 
476 	vrele(ip->i_devvp);
477 	free(ip, M_NTFSNTNODE, 0);
478 }
479 
480 /*
481  * increment usecount of ntnode
482  */
483 void
ntfs_ntref(struct ntnode * ip)484 ntfs_ntref(struct ntnode *ip)
485 {
486 	ip->i_usecount++;
487 
488 	DPRINTF("ntfs_ntref: ino %u, usecount: %d\n",
489 	    ip->i_number, ip->i_usecount);
490 }
491 
492 /*
493  * Decrement usecount of ntnode.
494  */
495 void
ntfs_ntrele(struct ntnode * ip)496 ntfs_ntrele(struct ntnode *ip)
497 {
498 	DPRINTF("ntfs_ntrele: rele ntnode %u: %p, usecount: %d\n",
499 	    ip->i_number, ip, ip->i_usecount);
500 
501 	ip->i_usecount--;
502 
503 	if (ip->i_usecount < 0)
504 		panic("ntfs_ntrele: ino: %d usecount: %d ",
505 		      ip->i_number,ip->i_usecount);
506 }
507 
508 /*
509  * Deallocate all memory allocated for ntvattr
510  */
511 void
ntfs_freentvattr(struct ntvattr * vap)512 ntfs_freentvattr(struct ntvattr *vap)
513 {
514 	if (vap->va_flag & NTFS_AF_INRUN) {
515 		if (vap->va_vruncn)
516 			free(vap->va_vruncn, M_NTFSRUN, 0);
517 		if (vap->va_vruncl)
518 			free(vap->va_vruncl, M_NTFSRUN, 0);
519 	} else {
520 		if (vap->va_datap)
521 			free(vap->va_datap, M_NTFSRDATA, 0);
522 	}
523 	free(vap, M_NTFSNTVATTR, 0);
524 }
525 
526 /*
527  * Convert disk image of attribute into ntvattr structure,
528  * runs are expanded also.
529  */
530 int
ntfs_attrtontvattr(struct ntfsmount * ntmp,struct ntvattr ** rvapp,struct attr * rap)531 ntfs_attrtontvattr(struct ntfsmount *ntmp, struct ntvattr **rvapp,
532     struct attr *rap)
533 {
534 	int             error, i;
535 	struct ntvattr *vap;
536 
537 	error = 0;
538 	*rvapp = NULL;
539 
540 	vap = malloc(sizeof(*vap), M_NTFSNTVATTR, M_WAITOK | M_ZERO);
541 	vap->va_ip = NULL;
542 	vap->va_flag = rap->a_hdr.a_flag;
543 	vap->va_type = rap->a_hdr.a_type;
544 	vap->va_compression = rap->a_hdr.a_compression;
545 	vap->va_index = rap->a_hdr.a_index;
546 
547 	DDPRINTF("type: 0x%x, index: %u", vap->va_type, vap->va_index);
548 
549 	vap->va_namelen = rap->a_hdr.a_namelen;
550 	if (rap->a_hdr.a_namelen) {
551 		wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
552 		DDPRINTF(", name:[");
553 		for (i = 0; i < vap->va_namelen; i++) {
554 			vap->va_name[i] = unp[i];
555 			DDPRINTF("%c", vap->va_name[i]);
556 		}
557 		DDPRINTF("]");
558 	}
559 	if (vap->va_flag & NTFS_AF_INRUN) {
560 		DDPRINTF(", nonres.");
561 		vap->va_datalen = rap->a_nr.a_datalen;
562 		vap->va_allocated = rap->a_nr.a_allocated;
563 		vap->va_vcnstart = rap->a_nr.a_vcnstart;
564 		vap->va_vcnend = rap->a_nr.a_vcnend;
565 		vap->va_compressalg = rap->a_nr.a_compressalg;
566 		error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
567 				       &(vap->va_vruncnt),
568 				       (caddr_t) rap + rap->a_nr.a_dataoff);
569 	} else {
570 		vap->va_compressalg = 0;
571 		DDPRINTF(", res.");
572 		vap->va_datalen = rap->a_r.a_datalen;
573 		vap->va_allocated = rap->a_r.a_datalen;
574 		vap->va_vcnstart = 0;
575 		vap->va_vcnend = ntfs_btocn(vap->va_allocated);
576 		vap->va_datap = malloc(vap->va_datalen, M_NTFSRDATA, M_WAITOK);
577 		memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
578 		       rap->a_r.a_datalen);
579 	}
580 	DDPRINTF(", len: %llu", vap->va_datalen);
581 
582 	if (error)
583 		free(vap, M_NTFSNTVATTR, 0);
584 	else
585 		*rvapp = vap;
586 
587 	DDPRINTF("\n");
588 
589 	return (error);
590 }
591 
592 /*
593  * Expand run into more utilizable and more memory eating format.
594  */
595 int
ntfs_runtovrun(cn_t ** rcnp,cn_t ** rclp,u_long * rcntp,u_int8_t * run)596 ntfs_runtovrun(cn_t **rcnp, cn_t **rclp, u_long *rcntp, u_int8_t *run)
597 {
598 	u_int32_t       off;
599 	u_int32_t       sz, i;
600 	cn_t           *cn;
601 	cn_t           *cl;
602 	u_long		cnt;
603 	cn_t		prev;
604 	cn_t		tmp;
605 
606 	off = 0;
607 	cnt = 0;
608 	while (run[off]) {
609 		off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
610 		cnt++;
611 	}
612 	cn = mallocarray(cnt, sizeof(cn_t), M_NTFSRUN, M_WAITOK);
613 	cl = mallocarray(cnt, sizeof(cn_t), M_NTFSRUN, M_WAITOK);
614 
615 	off = 0;
616 	cnt = 0;
617 	prev = 0;
618 	while (run[off]) {
619 
620 		sz = run[off++];
621 		cl[cnt] = 0;
622 
623 		for (i = 0; i < (sz & 0xF); i++)
624 			cl[cnt] += (u_int32_t) run[off++] << (i << 3);
625 
626 		sz >>= 4;
627 		if (run[off + sz - 1] & 0x80) {
628 			tmp = ((u_int64_t) - 1) << (sz << 3);
629 			for (i = 0; i < sz; i++)
630 				tmp |= (u_int64_t) run[off++] << (i << 3);
631 		} else {
632 			tmp = 0;
633 			for (i = 0; i < sz; i++)
634 				tmp |= (u_int64_t) run[off++] << (i << 3);
635 		}
636 		if (tmp)
637 			prev = cn[cnt] = prev + tmp;
638 		else
639 			cn[cnt] = tmp;
640 
641 		cnt++;
642 	}
643 	*rcnp = cn;
644 	*rclp = cl;
645 	*rcntp = cnt;
646 	return (0);
647 }
648 
649 /*
650  * Compare unicode and ascii string case insens.
651  */
652 int
ntfs_uastricmp(struct ntfsmount * ntmp,const wchar * ustr,size_t ustrlen,const char * astr,size_t astrlen)653 ntfs_uastricmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
654     const char *astr, size_t astrlen)
655 {
656 	size_t  i;
657 	int             res;
658 	const char *astrend = astr + astrlen;
659 
660 	for (i = 0; i < ustrlen && astr < astrend; i++) {
661 		res = (*ntmp->ntm_wcmp)(NTFS_TOUPPER(ustr[i]),
662 				NTFS_TOUPPER((*ntmp->ntm_wget)(&astr)) );
663 		if (res)
664 			return res;
665 	}
666 
667 	if (i == ustrlen && astr == astrend)
668 		return 0;
669 	else if (i == ustrlen)
670 		return -1;
671 	else
672 		return 1;
673 }
674 
675 /*
676  * Compare unicode and ascii string case sens.
677  */
678 int
ntfs_uastrcmp(struct ntfsmount * ntmp,const wchar * ustr,size_t ustrlen,const char * astr,size_t astrlen)679 ntfs_uastrcmp(struct ntfsmount *ntmp, const wchar *ustr, size_t ustrlen,
680     const char *astr, size_t astrlen)
681 {
682 	size_t             i;
683 	int             res;
684 	const char *astrend = astr + astrlen;
685 
686 	for (i = 0; (i < ustrlen) && (astr < astrend); i++) {
687 		res = (*ntmp->ntm_wcmp)(ustr[i], (*ntmp->ntm_wget)(&astr));
688 		if (res)
689 			return res;
690 	}
691 
692 	if (i == ustrlen && astr == astrend)
693 		return 0;
694 	else if (i == ustrlen)
695 		return -1;
696 	else
697 		return 1;
698 }
699 
700 /*
701  * Search fnode in ntnode, if not found allocate and preinitialize.
702  *
703  * ntnode should be locked on entry.
704  */
705 int
ntfs_fget(struct ntfsmount * ntmp,struct ntnode * ip,int attrtype,char * attrname,struct fnode ** fpp)706 ntfs_fget(struct ntfsmount *ntmp, struct ntnode *ip, int attrtype,
707     char *attrname, struct fnode **fpp)
708 {
709 	struct fnode *fp;
710 
711 	DPRINTF("ntfs_fget: ino: %u, attrtype: 0x%x, attrname: %s\n",
712 	    ip->i_number, attrtype, attrname ? attrname : "");
713 	*fpp = NULL;
714 	LIST_FOREACH(fp, &ip->i_fnlist, f_fnlist) {
715 		DPRINTF("ntfs_fget: fnode: attrtype: %u, attrname: %s\n",
716 		    fp->f_attrtype, fp->f_attrname ? fp->f_attrname : "");
717 
718 		if ((attrtype == fp->f_attrtype) &&
719 		    ((!attrname && !fp->f_attrname) ||
720 		     (attrname && fp->f_attrname &&
721 		      !strcmp(attrname,fp->f_attrname)))){
722 			DPRINTF("ntfs_fget: found existed: %p\n", fp);
723 			*fpp = fp;
724 		}
725 	}
726 
727 	if (*fpp)
728 		return (0);
729 
730 	fp = malloc(sizeof(*fp), M_NTFSFNODE, M_WAITOK | M_ZERO);
731 	DPRINTF("ntfs_fget: allocating fnode: %p\n", fp);
732 
733 	fp->f_ip = ip;
734 	fp->f_attrname = attrname;
735 	if (fp->f_attrname) fp->f_flag |= FN_AATTRNAME;
736 	fp->f_attrtype = attrtype;
737 
738 	ntfs_ntref(ip);
739 
740 	LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
741 
742 	*fpp = fp;
743 
744 	return (0);
745 }
746 
747 /*
748  * Deallocate fnode, remove it from ntnode's fnode list.
749  *
750  * ntnode should be locked.
751  */
752 void
ntfs_frele(struct fnode * fp)753 ntfs_frele(struct fnode *fp)
754 {
755 	struct ntnode *ip = FTONT(fp);
756 
757 	DPRINTF("ntfs_frele: fnode: %p for %u: %p\n", fp, ip->i_number, ip);
758 
759 	DPRINTF("ntfs_frele: deallocating fnode\n");
760 	LIST_REMOVE(fp,f_fnlist);
761 	if (fp->f_flag & FN_AATTRNAME)
762 		free(fp->f_attrname, M_TEMP, 0);
763 	if (fp->f_dirblbuf)
764 		free(fp->f_dirblbuf, M_NTFSDIR, 0);
765 	free(fp, M_NTFSFNODE, 0);
766 	ntfs_ntrele(ip);
767 }
768 
769 /*
770  * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
771  * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
772  * If $ATTR_TYPE not specified, ATTR_A_DATA assumed.
773  */
774 int
ntfs_ntlookupattr(struct ntfsmount * ntmp,const char * name,int namelen,int * attrtype,char ** attrname)775 ntfs_ntlookupattr(struct ntfsmount *ntmp, const char *name, int namelen,
776     int *attrtype, char **attrname)
777 {
778 	const char *sys;
779 	size_t syslen, i;
780 	struct ntvattrdef *adp;
781 
782 	if (namelen == 0)
783 		return (0);
784 
785 	if (name[0] == '$') {
786 		sys = name;
787 		for (syslen = 0; syslen < namelen; syslen++) {
788 			if(sys[syslen] == ':') {
789 				name++;
790 				namelen--;
791 				break;
792 			}
793 		}
794 		name += syslen;
795 		namelen -= syslen;
796 
797 		adp = ntmp->ntm_ad;
798 		for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
799 			if (syslen != adp->ad_namelen ||
800 			    strncmp(sys, adp->ad_name, syslen) != 0)
801 				continue;
802 
803 			*attrtype = adp->ad_type;
804 			goto out;
805 		}
806 		return (ENOENT);
807 	}
808 
809     out:
810 	if (namelen) {
811 		*attrname = malloc(namelen + 1, M_TEMP, M_WAITOK);
812 		memcpy(*attrname, name, namelen);
813 		(*attrname)[namelen] = '\0';
814 		*attrtype = NTFS_A_DATA;
815 	}
816 
817 	return (0);
818 }
819 
820 /*
821  * Lookup specified node for filename, matching cnp, return fnode filled.
822  */
823 int
ntfs_ntlookupfile(struct ntfsmount * ntmp,struct vnode * vp,struct componentname * cnp,struct vnode ** vpp)824 ntfs_ntlookupfile(struct ntfsmount *ntmp, struct vnode *vp,
825     struct componentname *cnp, struct vnode **vpp)
826 {
827 	struct fnode   *fp = VTOF(vp);
828 	struct ntnode  *ip = FTONT(fp);
829 	struct ntvattr *vap = NULL;	/* Root attribute */
830 	cn_t            cn = 0;	/* VCN in current attribute */
831 	caddr_t         rdbuf = NULL;	/* Buffer to read directory's blocks */
832 	u_int32_t       blsize;
833 	u_int32_t       rdsize;	/* Length of data to read from current block */
834 	struct attr_indexentry *iep;
835 	int             error, res, anamelen, fnamelen;
836 	const char     *fname,*aname;
837 	u_int32_t       aoff;
838 	int attrtype = NTFS_A_DATA;
839 	char *attrname = NULL;
840 	struct fnode   *nfp;
841 	struct vnode   *nvp;
842 	enum vtype	f_type;
843 	int fullscan = 0;
844 	struct ntfs_lookup_ctx *lookup_ctx = NULL, *tctx;
845 
846 	error = ntfs_ntget(ip);
847 	if (error)
848 		return (error);
849 
850 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
851 	if (error || (vap->va_flag & NTFS_AF_INRUN)) {
852 		error = ENOTDIR;
853 		goto fail;
854 	}
855 
856 	/*
857 	 * Divide file name into: foofilefoofilefoofile[:attrspec]
858 	 * Store like this:       fname:fnamelen       [aname:anamelen]
859 	 */
860 	fname = cnp->cn_nameptr;
861 	aname = NULL;
862 	anamelen = 0;
863 	for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
864 		if(fname[fnamelen] == ':') {
865 			aname = fname + fnamelen + 1;
866 			anamelen = cnp->cn_namelen - fnamelen - 1;
867 			DPRINTF("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
868 			    fname, fnamelen, aname, anamelen);
869 			break;
870 		}
871 
872 	blsize = vap->va_a_iroot->ir_size;
873 	DPRINTF("ntfs_ntlookupfile: blksz: %u\n", blsize);
874 
875 	rdbuf = malloc(blsize, M_TEMP, M_WAITOK);
876 
877     loop:
878 	rdsize = vap->va_datalen;
879 	DPRINTF("ntfs_ntlookupfile: rdsz: %u\n", rdsize);
880 
881 	error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
882 			       0, rdsize, rdbuf, NULL);
883 	if (error)
884 		goto fail;
885 
886 	aoff = sizeof(struct attr_indexroot);
887 
888 	do {
889 		iep = (struct attr_indexentry *) (rdbuf + aoff);
890 
891 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
892 			aoff += iep->reclen,
893 			iep = (struct attr_indexentry *) (rdbuf + aoff))
894 		{
895 			DDPRINTF("scan: %u, %u\n", iep->ie_number,
896 			    iep->ie_fnametype);
897 
898 			/* check the name - the case-insensitive check
899 			 * has to come first, to break from this for loop
900 			 * if needed, so we can dive correctly */
901 			res = ntfs_uastricmp(ntmp, iep->ie_fname,
902 				iep->ie_fnamelen, fname, fnamelen);
903 			if (!fullscan) {
904 				if (res > 0) break;
905 				if (res < 0) continue;
906 			}
907 
908 			if (iep->ie_fnametype == 0 ||
909 			    !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
910 			{
911 				res = ntfs_uastrcmp(ntmp, iep->ie_fname,
912 					iep->ie_fnamelen, fname, fnamelen);
913 				if (res != 0 && !fullscan) continue;
914 			}
915 
916 			/* if we perform full scan, the file does not match
917 			 * and this is subnode, dive */
918 			if (fullscan && res != 0) {
919 			    if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
920 				tctx = malloc(sizeof(struct ntfs_lookup_ctx),
921 					M_TEMP, M_WAITOK);
922 				tctx->aoff	= aoff + iep->reclen;
923 				tctx->rdsize	= rdsize;
924 				tctx->cn	= cn;
925 				tctx->prev	= lookup_ctx;
926 				lookup_ctx = tctx;
927 				break;
928 			    } else
929 				continue;
930 			}
931 
932 			if (aname) {
933 				error = ntfs_ntlookupattr(ntmp,
934 					aname, anamelen,
935 					&attrtype, &attrname);
936 				if (error)
937 					goto fail;
938 			}
939 
940 			/* Check if we've found ourselves */
941 			if ((iep->ie_number == ip->i_number) &&
942 			    (attrtype == fp->f_attrtype) &&
943 			    ((!attrname && !fp->f_attrname) ||
944 			     (attrname && fp->f_attrname &&
945 			      !strcmp(attrname, fp->f_attrname))))
946 			{
947 				vref(vp);
948 				*vpp = vp;
949 				error = 0;
950 				goto fail;
951 			}
952 
953 			/* free the buffer returned by ntfs_ntlookupattr() */
954 			if (attrname) {
955 				free(attrname, M_TEMP, 0);
956 				attrname = NULL;
957 			}
958 
959 			/* vget node, but don't load it */
960 			error = ntfs_vgetex(ntmp->ntm_mountp,
961 				   iep->ie_number, attrtype, attrname,
962 				   LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
963 				   &nvp);
964 			if (error)
965 				goto fail;
966 
967 			nfp = VTOF(nvp);
968 
969 			if (nfp->f_flag & FN_VALID) {
970 				*vpp = nvp;
971 				goto fail;
972 			}
973 
974 			nfp->f_fflag = iep->ie_fflag;
975 			nfp->f_pnumber = iep->ie_fpnumber;
976 			nfp->f_times = iep->ie_ftimes;
977 
978 			if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
979 			   (nfp->f_attrtype == NTFS_A_DATA) &&
980 			   (nfp->f_attrname == NULL))
981 				f_type = VDIR;
982 			else
983 				f_type = VREG;
984 
985 			nvp->v_type = f_type;
986 
987 			if ((nfp->f_attrtype == NTFS_A_DATA) &&
988 			    (nfp->f_attrname == NULL))
989 			{
990 				/* Opening default attribute */
991 				nfp->f_size = iep->ie_fsize;
992 				nfp->f_allocated = iep->ie_fallocated;
993 				nfp->f_flag |= FN_PRELOADED;
994 			} else {
995 				error = ntfs_filesize(ntmp, nfp,
996 					    &nfp->f_size, &nfp->f_allocated);
997 				if (error) {
998 					vput(nvp);
999 					goto fail;
1000 				}
1001 			}
1002 
1003 			nfp->f_flag &= ~FN_VALID;
1004 			*vpp = nvp;
1005 			goto fail;
1006 		}
1007 
1008 		/* Dive if possible */
1009 		if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1010 			DPRINTF("ntfs_ntlookupfile: diving\n");
1011 
1012 			cn = *(cn_t *) (rdbuf + aoff +
1013 					iep->reclen - sizeof(cn_t));
1014 			rdsize = blsize;
1015 
1016 			error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1017 					ntfs_cntob(cn), rdsize, rdbuf, NULL);
1018 			if (error)
1019 				goto fail;
1020 
1021 			error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1022 						rdbuf, rdsize);
1023 			if (error)
1024 				goto fail;
1025 
1026 			aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1027 				0x18);
1028 		} else if (fullscan && lookup_ctx) {
1029 			cn = lookup_ctx->cn;
1030 			aoff = lookup_ctx->aoff;
1031 			rdsize = lookup_ctx->rdsize;
1032 
1033 			error = ntfs_readattr(ntmp, ip,
1034 				(cn == 0) ? NTFS_A_INDXROOT : NTFS_A_INDX,
1035 				"$I30", ntfs_cntob(cn), rdsize, rdbuf, NULL);
1036 			if (error)
1037 				goto fail;
1038 
1039 			if (cn != 0) {
1040 				error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1041 						rdbuf, rdsize);
1042 				if (error)
1043 					goto fail;
1044 			}
1045 
1046 			tctx = lookup_ctx;
1047 			lookup_ctx = lookup_ctx->prev;
1048 			free(tctx, M_TEMP, 0);
1049 		} else {
1050 			DPRINTF("ntfs_ntlookupfile: nowhere to dive :-(\n");
1051 			error = ENOENT;
1052 			break;
1053 		}
1054 	} while (1);
1055 
1056 	if (error == ENOENT) {
1057 		/* perform full scan if no entry was found */
1058 		if (!fullscan) {
1059 			fullscan = 1;
1060 			cn = 0;		/* need zero, used by lookup_ctx */
1061 
1062 			DDPRINTF("ntfs_ntlookupfile: fullscan performed for: %.*s\n",
1063 			    (unsigned int)fnamelen, fname);
1064 			goto loop;
1065 		}
1066 
1067 		if ((cnp->cn_flags & ISLASTCN) &&
1068 		    (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
1069 			error = EJUSTRETURN;
1070 	}
1071 
1072 	DPRINTF("finish\n");
1073 
1074 fail:
1075 	if (vap)
1076 		ntfs_ntvattrrele(vap);
1077 	if (rdbuf)
1078 		free(rdbuf, M_TEMP, 0);
1079 	if (attrname)
1080 		free(attrname, M_TEMP, 0);
1081 	if (lookup_ctx) {
1082 		while(lookup_ctx) {
1083 			tctx = lookup_ctx;
1084 			lookup_ctx = lookup_ctx->prev;
1085 			free(tctx, M_TEMP, 0);
1086 		}
1087 	}
1088 	ntfs_ntput(ip);
1089 	return (error);
1090 }
1091 
1092 /*
1093  * Check if name type is permitted to show.
1094  */
1095 int
ntfs_isnamepermitted(struct ntfsmount * ntmp,struct attr_indexentry * iep)1096 ntfs_isnamepermitted(struct ntfsmount *ntmp, struct attr_indexentry *iep)
1097 {
1098 	if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1099 		return 1;
1100 
1101 	switch (iep->ie_fnametype) {
1102 	case 2:
1103 		DDPRINTF("ntfs_isnamepermitted: skipped DOS name\n");
1104 		return 0;
1105 	case 0: case 1: case 3:
1106 		return 1;
1107 	default:
1108 		printf("ntfs_isnamepermitted: " \
1109 		       "WARNING! Unknown file name type: %d\n",
1110 		       iep->ie_fnametype);
1111 		break;
1112 	}
1113 	return 0;
1114 }
1115 
1116 /*
1117  * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1118  * This is done by scanning $BITMAP:$I30 for busy clusters and reading them.
1119  * Of course $INDEX_ROOT:$I30 is read before. Last read values are stored in
1120  * fnode, so we can skip toward record number num almost immediately.
1121  * Anyway this is rather slow routine. The problem is that we don't know
1122  * how many records are there in $INDEX_ALLOCATION:$I30 block.
1123  */
1124 int
ntfs_ntreaddir(struct ntfsmount * ntmp,struct fnode * fp,u_int32_t num,struct attr_indexentry ** riepp,struct proc * p)1125 ntfs_ntreaddir(struct ntfsmount *ntmp, struct fnode *fp, u_int32_t num,
1126     struct attr_indexentry **riepp, struct proc *p)
1127 {
1128 	struct ntnode  *ip = FTONT(fp);
1129 	struct ntvattr *vap = NULL;	/* IndexRoot attribute */
1130 	struct ntvattr *bmvap = NULL;	/* BitMap attribute */
1131 	struct ntvattr *iavap = NULL;	/* IndexAllocation attribute */
1132 	caddr_t         rdbuf;		/* Buffer to read directory's blocks  */
1133 	u_int8_t       *bmp = NULL;	/* Bitmap */
1134 	u_int32_t       blsize;		/* Index allocation size (2048) */
1135 	u_int32_t       rdsize;		/* Length of data to read */
1136 	u_int32_t       attrnum;	/* Current attribute type */
1137 	u_int32_t       cpbl = 1;	/* Clusters per directory block */
1138 	u_int32_t       blnum;
1139 	struct attr_indexentry *iep;
1140 	int             error = ENOENT;
1141 	u_int32_t       aoff, cnum;
1142 
1143 	DPRINTF("ntfs_ntreaddir: read ino: %u, num: %u\n", ip->i_number, num);
1144 	error = ntfs_ntget(ip);
1145 	if (error)
1146 		return (error);
1147 
1148 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1149 	if (error) {
1150 		error = ENOTDIR;
1151 		goto fail;
1152 	}
1153 
1154 	if (fp->f_dirblbuf == NULL) {
1155 		fp->f_dirblsz = vap->va_a_iroot->ir_size;
1156 		fp->f_dirblbuf = malloc(MAX(vap->va_datalen,fp->f_dirblsz),
1157 		    M_NTFSDIR, M_WAITOK);
1158 	}
1159 
1160 	blsize = fp->f_dirblsz;
1161 	rdbuf = fp->f_dirblbuf;
1162 
1163 	DPRINTF("ntfs_ntreaddir: rdbuf: %p, blsize: %u\n", rdbuf, blsize);
1164 
1165 	if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1166 		error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1167 					0, &bmvap);
1168 		if (error) {
1169 			error = ENOTDIR;
1170 			goto fail;
1171 		}
1172 		bmp = malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
1173 		error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1174 				       bmvap->va_datalen, bmp, NULL);
1175 		if (error)
1176 			goto fail;
1177 
1178 		error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1179 					0, &iavap);
1180 		if (error) {
1181 			error = ENOTDIR;
1182 			goto fail;
1183 		}
1184 		cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1185 		DPRINTF("ntfs_ntreaddir: indexalloc: %llu, cpbl: %u\n",
1186 		    iavap->va_datalen, cpbl);
1187 	} else {
1188 		DPRINTF("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n");
1189 		iavap = bmvap = NULL;
1190 		bmp = NULL;
1191 	}
1192 
1193 	/* Try use previous values */
1194 	if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1195 		attrnum = fp->f_lastdattr;
1196 		aoff = fp->f_lastdoff;
1197 		blnum = fp->f_lastdblnum;
1198 		cnum = fp->f_lastdnum;
1199 	} else {
1200 		attrnum = NTFS_A_INDXROOT;
1201 		aoff = sizeof(struct attr_indexroot);
1202 		blnum = 0;
1203 		cnum = 0;
1204 	}
1205 
1206 	do {
1207 		DPRINTF("ntfs_ntreaddir: scan: 0x%x, %u, %u, %u, %u\n",
1208 		    attrnum, blnum, cnum, num, aoff);
1209 		rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1210 		error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1211 				ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1212 		if (error)
1213 			goto fail;
1214 
1215 		if (attrnum == NTFS_A_INDX) {
1216 			error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1217 						rdbuf, rdsize);
1218 			if (error)
1219 				goto fail;
1220 		}
1221 		if (aoff == 0)
1222 			aoff = (attrnum == NTFS_A_INDX) ?
1223 				(0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1224 				sizeof(struct attr_indexroot);
1225 
1226 		iep = (struct attr_indexentry *) (rdbuf + aoff);
1227 		for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1228 			aoff += iep->reclen,
1229 			iep = (struct attr_indexentry *) (rdbuf + aoff))
1230 		{
1231 			if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1232 
1233 			if (cnum >= num) {
1234 				fp->f_lastdnum = cnum;
1235 				fp->f_lastdoff = aoff;
1236 				fp->f_lastdblnum = blnum;
1237 				fp->f_lastdattr = attrnum;
1238 
1239 				*riepp = iep;
1240 
1241 				error = 0;
1242 				goto fail;
1243 			}
1244 			cnum++;
1245 		}
1246 
1247 		if (iavap) {
1248 			if (attrnum == NTFS_A_INDXROOT)
1249 				blnum = 0;
1250 			else
1251 				blnum++;
1252 
1253 			while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1254 				if (bmp[blnum >> 3] & (1 << (blnum & 7)))
1255 					break;
1256 				blnum++;
1257 			}
1258 
1259 			attrnum = NTFS_A_INDX;
1260 			aoff = 0;
1261 			if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1262 				break;
1263 			DPRINTF("ntfs_ntreaddir: blnum: %u\n", blnum);
1264 		}
1265 	} while (iavap);
1266 
1267 	*riepp = NULL;
1268 	fp->f_lastdnum = 0;
1269 
1270 fail:
1271 	if (vap)
1272 		ntfs_ntvattrrele(vap);
1273 	if (bmvap)
1274 		ntfs_ntvattrrele(bmvap);
1275 	if (iavap)
1276 		ntfs_ntvattrrele(iavap);
1277 	if (bmp)
1278 		free(bmp, M_TEMP, 0);
1279 	ntfs_ntput(ip);
1280 
1281 	return (error);
1282 }
1283 
1284 /*
1285  * Convert NTFS times that are in 100 ns units and begins from
1286  * 1601 Jan 1 into unix times.
1287  */
1288 struct timespec
ntfs_nttimetounix(u_int64_t nt)1289 ntfs_nttimetounix(u_int64_t nt)
1290 {
1291 	struct timespec t;
1292 
1293 	/* Windows NT times are in 100 ns and from 1601 Jan 1 */
1294 	t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1295 	t.tv_sec = nt / (1000 * 1000 * 10) -
1296 		369LL * 365LL * 24LL * 60LL * 60LL -
1297 		89LL * 1LL * 24LL * 60LL * 60LL;
1298 	return (t);
1299 }
1300 
1301 /*
1302  * Get file sizes from corresponding attribute.
1303  *
1304  * ntnode under fnode should be locked.
1305  */
1306 int
ntfs_filesize(struct ntfsmount * ntmp,struct fnode * fp,u_int64_t * size,u_int64_t * bytes)1307 ntfs_filesize(struct ntfsmount *ntmp, struct fnode *fp, u_int64_t *size,
1308     u_int64_t *bytes)
1309 {
1310 	struct ntvattr *vap;
1311 	struct ntnode *ip = FTONT(fp);
1312 	u_int64_t       sz, bn;
1313 	int             error;
1314 
1315 	DPRINTF("ntfs_filesize: ino: %u\n", ip->i_number);
1316 
1317 	error = ntfs_ntvattrget(ntmp, ip,
1318 		fp->f_attrtype, fp->f_attrname, 0, &vap);
1319 	if (error)
1320 		return (error);
1321 
1322 	bn = vap->va_allocated;
1323 	sz = vap->va_datalen;
1324 
1325 	DPRINTF("ntfs_filesize: %llu bytes (%llu bytes allocated)\n", sz, bn);
1326 
1327 	if (size)
1328 		*size = sz;
1329 	if (bytes)
1330 		*bytes = bn;
1331 
1332 	ntfs_ntvattrrele(vap);
1333 
1334 	return (0);
1335 }
1336 
1337 /*
1338  * This is one of the read routines.
1339  *
1340  * ntnode should be locked.
1341  */
1342 int
ntfs_readntvattr_plain(struct ntfsmount * ntmp,struct ntnode * ip,struct ntvattr * vap,off_t roff,size_t rsize,void * rdata,size_t * initp,struct uio * uio)1343 ntfs_readntvattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1344     struct ntvattr *vap, off_t roff, size_t rsize, void *rdata, size_t *initp,
1345     struct uio *uio)
1346 {
1347 	int             error = 0;
1348 	off_t		off;
1349 
1350 	*initp = 0;
1351 	if (vap->va_flag & NTFS_AF_INRUN) {
1352 		int             cnt;
1353 		cn_t            ccn, ccl, cn, cl;
1354 		caddr_t         data = rdata;
1355 		struct buf     *bp;
1356 		size_t          left, tocopy;
1357 
1358 		DDPRINTF("ntfs_readntvattr_plain: data in run: %lu chains\n",
1359 		    vap->va_vruncnt);
1360 
1361 		off = roff;
1362 		left = rsize;
1363 		ccl = 0;
1364 		ccn = 0;
1365 		cnt = 0;
1366 		while (left && (cnt < vap->va_vruncnt)) {
1367 			ccn = vap->va_vruncn[cnt];
1368 			ccl = vap->va_vruncl[cnt];
1369 
1370 			DDPRINTF("ntfs_readntvattr_plain: left %zu, "
1371 			    "cn: 0x%llx, cl: %llu, off: %lld\n",
1372 			    left, ccn, ccl, off);
1373 
1374 			if (ntfs_cntob(ccl) < off) {
1375 				off -= ntfs_cntob(ccl);
1376 				cnt++;
1377 				continue;
1378 			}
1379 			if (ccn || ip->i_number == NTFS_BOOTINO) {
1380 				ccl -= ntfs_btocn(off);
1381 				cn = ccn + ntfs_btocn(off);
1382 				off = ntfs_btocnoff(off);
1383 
1384 				while (left && ccl) {
1385 					/*
1386 					 * Always read single clusters at a
1387 					 * time - we need to avoid reading
1388 					 * differently-sized blocks at the
1389 					 * same disk offsets to avoid
1390 					 * confusing the buffer cache.
1391 					 */
1392 					tocopy = MIN(left,
1393 					    ntfs_cntob(1) - off);
1394 					cl = ntfs_btocl(tocopy + off);
1395 					KASSERT(cl == 1 &&
1396 					    tocopy <= ntfs_cntob(1));
1397 
1398 					DDPRINTF("ntfs_readntvattr_plain: "
1399 					    "read: cn: 0x%llx cl: %llu, "
1400 					    "off: %lld, len: %zu, "
1401 					    "left: %zu\n",
1402 					    cn, cl, off, tocopy, left);
1403 					error = bread(ntmp->ntm_devvp,
1404 						      ntfs_cntobn(cn),
1405 						      ntfs_cntob(cl),
1406 						      &bp);
1407 					if (error) {
1408 						brelse(bp);
1409 						return (error);
1410 					}
1411 					if (uio) {
1412 						error = uiomove(bp->b_data + off,
1413 							tocopy, uio);
1414 						if (error != 0)
1415 							break;
1416 					} else {
1417 						memcpy(data, bp->b_data + off,
1418 							tocopy);
1419 					}
1420 					brelse(bp);
1421 					data = data + tocopy;
1422 					*initp += tocopy;
1423 					off = 0;
1424 					left -= tocopy;
1425 					cn += cl;
1426 					ccl -= cl;
1427 				}
1428 			} else {
1429 				tocopy = MIN(left, ntfs_cntob(ccl) - off);
1430 				DDPRINTF("ntfs_readntvattr_plain: hole: "
1431 				    "ccn: 0x%llx ccl: %llu, off: %lld, "
1432 				    "len: %zu, left: %zu\n",
1433 				    ccn, ccl, off, tocopy, left);
1434 				left -= tocopy;
1435 				off = 0;
1436 				if (uio) {
1437 					size_t remains = tocopy;
1438 					for(; remains; remains--) {
1439 						error = uiomove("", 1, uio);
1440 						if (error != 0)
1441 							break;
1442 					}
1443 				} else
1444 					bzero(data, tocopy);
1445 				data = data + tocopy;
1446 			}
1447 			cnt++;
1448 			if (error != 0)
1449 				break;
1450 		}
1451 		if (left && error == 0) {
1452 			printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1453 			error = E2BIG;
1454 		}
1455 	} else {
1456 		DDPRINTF("ntfs_readnvattr_plain: data is in mft record\n");
1457 		if (uio)
1458 			error = uiomove(vap->va_datap + roff, rsize, uio);
1459 		else
1460 			memcpy(rdata, vap->va_datap + roff, rsize);
1461 		*initp += rsize;
1462 	}
1463 
1464 	return (error);
1465 }
1466 
1467 /*
1468  * This is one of read routines.
1469  */
1470 int
ntfs_readattr_plain(struct ntfsmount * ntmp,struct ntnode * ip,u_int32_t attrnum,char * attrname,off_t roff,size_t rsize,void * rdata,size_t * initp,struct uio * uio)1471 ntfs_readattr_plain(struct ntfsmount *ntmp, struct ntnode *ip,
1472     u_int32_t attrnum, char *attrname, off_t roff, size_t rsize, void *rdata,
1473     size_t *initp, struct uio *uio)
1474 {
1475 	size_t          init;
1476 	int             error = 0;
1477 	off_t           off = roff;
1478 	size_t		left = rsize, toread;
1479 	caddr_t         data = rdata;
1480 	struct ntvattr *vap;
1481 	*initp = 0;
1482 
1483 	while (left) {
1484 		error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1485 					ntfs_btocn(off), &vap);
1486 		if (error)
1487 			return (error);
1488 		toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1489 		DDPRINTF("ntfs_readattr_plain: o: %lld, s: %zu "
1490 		    "(%llu - %llu)\n", off, toread,
1491 		    vap->va_vcnstart, vap->va_vcnend);
1492 		error = ntfs_readntvattr_plain(ntmp, ip, vap,
1493 					 off - ntfs_cntob(vap->va_vcnstart),
1494 					 toread, data, &init, uio);
1495 		if (error) {
1496 			printf("ntfs_readattr_plain: ntfs_readntvattr_plain "
1497 			    "failed: o: %lld, s: %zu\n", off, toread);
1498 			printf("ntfs_readattr_plain: attrib: %llu - %llu\n",
1499 			       vap->va_vcnstart, vap->va_vcnend);
1500 			ntfs_ntvattrrele(vap);
1501 			break;
1502 		}
1503 		ntfs_ntvattrrele(vap);
1504 		left -= toread;
1505 		off += toread;
1506 		data = data + toread;
1507 		*initp += init;
1508 	}
1509 
1510 	return (error);
1511 }
1512 
1513 /*
1514  * This is one of read routines.
1515  */
1516 int
ntfs_readattr(struct ntfsmount * ntmp,struct ntnode * ip,u_int32_t attrnum,char * attrname,off_t roff,size_t rsize,void * rdata,struct uio * uio)1517 ntfs_readattr(struct ntfsmount *ntmp, struct ntnode *ip, u_int32_t attrnum,
1518     char *attrname, off_t roff, size_t rsize, void *rdata, struct uio *uio)
1519 {
1520 	int             error = 0;
1521 	struct ntvattr *vap;
1522 	size_t          init;
1523 
1524 	DDPRINTF("ntfs_readattr: reading %u: 0x%x, from %lld size %zu bytes\n",
1525 	    ip->i_number, attrnum, roff, rsize);
1526 
1527 	error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1528 	if (error)
1529 		return (error);
1530 
1531 	if ((roff > vap->va_datalen) ||
1532 	    (roff + rsize > vap->va_datalen)) {
1533 		printf("ntfs_readattr: offset too big: %lld (%lld) > %llu\n",
1534 		    roff, roff + rsize, vap->va_datalen);
1535 		ntfs_ntvattrrele(vap);
1536 		return (E2BIG);
1537 	}
1538 	if (vap->va_compression && vap->va_compressalg) {
1539 		u_int8_t       *cup;
1540 		u_int8_t       *uup;
1541 		off_t           off = roff;
1542 		caddr_t         data = rdata;
1543 		cn_t            cn;
1544 		size_t		left = rsize, tocopy;
1545 
1546 		DDPRINTF("ntfs_ntreadattr: compression: %u\n",
1547 		    vap->va_compressalg);
1548 
1549 		cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1550 		    M_WAITOK);
1551 		uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL), M_NTFSDECOMP,
1552 		    M_WAITOK);
1553 
1554 		cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1555 		off = roff - ntfs_cntob(cn);
1556 
1557 		while (left) {
1558 			error = ntfs_readattr_plain(ntmp, ip, attrnum,
1559 						  attrname, ntfs_cntob(cn),
1560 					          ntfs_cntob(NTFS_COMPUNIT_CL),
1561 						  cup, &init, NULL);
1562 			if (error)
1563 				break;
1564 
1565 			tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1566 
1567 			if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1568 				if (uio)
1569 					error = uiomove(cup + off, tocopy, uio);
1570 				else
1571 					memcpy(data, cup + off, tocopy);
1572 			} else if (init == 0) {
1573 				if (uio) {
1574 					size_t remains = tocopy;
1575 					for(; remains; remains--) {
1576 						error = uiomove("", 1, uio);
1577 						if (error != 0)
1578 							break;
1579 					}
1580 				}
1581 				else
1582 					bzero(data, tocopy);
1583 			} else {
1584 				error = ntfs_uncompunit(ntmp, uup, cup);
1585 				if (error)
1586 					break;
1587 				if (uio)
1588 					error = uiomove(uup + off, tocopy, uio);
1589 				else
1590 					memcpy(data, uup + off, tocopy);
1591 			}
1592 			if (error)
1593 				break;
1594 
1595 			left -= tocopy;
1596 			data = data + tocopy;
1597 			off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1598 			cn += NTFS_COMPUNIT_CL;
1599 		}
1600 
1601 		free(uup, M_NTFSDECOMP, 0);
1602 		free(cup, M_NTFSDECOMP, 0);
1603 	} else
1604 		error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1605 					     roff, rsize, rdata, &init, uio);
1606 	ntfs_ntvattrrele(vap);
1607 	return (error);
1608 }
1609 
1610 #if UNUSED_CODE
1611 int
ntfs_parserun(cn_t * cn,cn_t * cl,u_int8_t * run,u_long len,u_long * off)1612 ntfs_parserun(cn_t *cn, cn_t *cl, u_int8_t *run, u_long len, u_long *off)
1613 {
1614 	u_int8_t        sz;
1615 	int             i;
1616 
1617 	if (NULL == run) {
1618 		printf("ntfs_parsetun: run == NULL\n");
1619 		return (EINVAL);
1620 	}
1621 	sz = run[(*off)++];
1622 	if (0 == sz) {
1623 		printf("ntfs_parserun: trying to go out of run\n");
1624 		return (E2BIG);
1625 	}
1626 	*cl = 0;
1627 	if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1628 		printf("ntfs_parserun: " \
1629 		       "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1630 		       sz, len, *off);
1631 		return (EINVAL);
1632 	}
1633 	for (i = 0; i < (sz & 0xF); i++)
1634 		*cl += (u_int32_t) run[(*off)++] << (i << 3);
1635 
1636 	sz >>= 4;
1637 	if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1638 		printf("ntfs_parserun: " \
1639 		       "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1640 		       sz, len, *off);
1641 		return (EINVAL);
1642 	}
1643 	for (i = 0; i < (sz & 0xF); i++)
1644 		*cn += (u_int32_t) run[(*off)++] << (i << 3);
1645 
1646 	return (0);
1647 }
1648 #endif
1649 
1650 /*
1651  * Process fixup routine on given buffer.
1652  */
1653 int
ntfs_procfixups(struct ntfsmount * ntmp,u_int32_t magic,caddr_t buf,size_t len)1654 ntfs_procfixups(struct ntfsmount *ntmp, u_int32_t magic, caddr_t buf,
1655     size_t len)
1656 {
1657 	struct fixuphdr *fhp = (struct fixuphdr *) buf;
1658 	int             i;
1659 	u_int16_t       fixup;
1660 	u_int16_t      *fxp;
1661 	u_int16_t      *cfxp;
1662 
1663 	if (fhp->fh_magic != magic) {
1664 		printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1665 		       fhp->fh_magic, magic);
1666 		return (EINVAL);
1667 	}
1668 	if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1669 		printf("ntfs_procfixups: " \
1670 		       "bad fixups number: %d for %ld bytes block\n",
1671 		       fhp->fh_fnum, (long)len);	/* XXX printf kludge */
1672 		return (EINVAL);
1673 	}
1674 	if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1675 		printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1676 		return (EINVAL);
1677 	}
1678 	fxp = (u_int16_t *) (buf + fhp->fh_foff);
1679 	cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1680 	fixup = *fxp++;
1681 	for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1682 		if (*cfxp != fixup) {
1683 			printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1684 			return (EINVAL);
1685 		}
1686 		*cfxp = *fxp;
1687 		cfxp = (u_int16_t *)((caddr_t)cfxp + ntmp->ntm_bps);
1688 	}
1689 	return (0);
1690 }
1691 
1692 #if UNUSED_CODE
1693 int
ntfs_runtocn(cn_t * cn,struct ntfsmount * ntmp,u_int8_t * run,u_long len,cn_t vcn)1694 ntfs_runtocn(cn_t *cn, struct ntfsmount *ntmp, u_int8_t *run, u_long len,
1695     cn_t vcn)
1696 {
1697 	cn_t            ccn = 0;
1698 	cn_t            ccl = 0;
1699 	u_long          off = 0;
1700 	int             error = 0;
1701 
1702 #if NTFS_DEBUG
1703 	int             i;
1704 	printf("ntfs_runtocn: run: %p, %ld bytes, vcn:%ld\n",
1705 		run, len, (u_long) vcn);
1706 	printf("ntfs_runtocn: run: ");
1707 	for (i = 0; i < len; i++)
1708 		printf("0x%02x ", run[i]);
1709 	printf("\n");
1710 #endif
1711 
1712 	if (NULL == run) {
1713 		printf("ntfs_runtocn: run == NULL\n");
1714 		return (EINVAL);
1715 	}
1716 	do {
1717 		if (run[off] == 0) {
1718 			printf("ntfs_runtocn: vcn too big\n");
1719 			return (E2BIG);
1720 		}
1721 		vcn -= ccl;
1722 		error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1723 		if (error) {
1724 			printf("ntfs_runtocn: ntfs_parserun failed\n");
1725 			return (error);
1726 		}
1727 	} while (ccl <= vcn);
1728 	*cn = ccn + vcn;
1729 	return (0);
1730 }
1731 #endif
1732 
1733 /*
1734  * if the ntfs_toupper_tab[] is filled already, just raise use count;
1735  * otherwise read the data from the filesystem we are currently mounting
1736  */
1737 int
ntfs_toupper_use(struct mount * mp,struct ntfsmount * ntmp,struct proc * p)1738 ntfs_toupper_use(struct mount *mp, struct ntfsmount *ntmp, struct proc *p)
1739 {
1740 	int error = 0;
1741 	struct vnode *vp;
1742 
1743 	/* get exclusive access */
1744 	rw_enter_write(&ntfs_toupper_lock);
1745 
1746 	/* only read the translation data from a file if it hasn't been
1747 	 * read already */
1748 	if (ntfs_toupper_tab)
1749 		goto out;
1750 
1751 	/*
1752 	 * Read in Unicode lowercase -> uppercase translation file.
1753 	 * XXX for now, just the first 256 entries are used anyway,
1754 	 * so don't bother reading more
1755 	 */
1756 	ntfs_toupper_tab = malloc(256 * 256 * sizeof(wchar), M_NTFSRDATA,
1757 	    M_WAITOK);
1758 
1759 	if ((error = VFS_VGET(mp, NTFS_UPCASEINO, &vp)))
1760 		goto out;
1761 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1762 			0, 256*256*sizeof(wchar), (char *) ntfs_toupper_tab,
1763 			NULL);
1764 	vput(vp);
1765 
1766     out:
1767 	ntfs_toupper_usecount++;
1768 	rw_exit_write(&ntfs_toupper_lock);
1769 	return (error);
1770 }
1771 
1772 /*
1773  * lower the use count and if it reaches zero, free the memory
1774  * tied by toupper table
1775  */
1776 void
ntfs_toupper_unuse(struct proc * p)1777 ntfs_toupper_unuse(struct proc *p)
1778 {
1779 	/* get exclusive access */
1780 	rw_enter_write(&ntfs_toupper_lock);
1781 
1782 	ntfs_toupper_usecount--;
1783 	if (ntfs_toupper_usecount == 0) {
1784 		free(ntfs_toupper_tab, M_NTFSRDATA, 0);
1785 		ntfs_toupper_tab = NULL;
1786 	}
1787 #ifdef DIAGNOSTIC
1788 	else if (ntfs_toupper_usecount < 0) {
1789 		panic("ntfs_toupper_unuse(): use count negative: %d",
1790 			ntfs_toupper_usecount);
1791 	}
1792 #endif
1793 
1794 	/* release the lock */
1795 	rw_exit_write(&ntfs_toupper_lock);
1796 }
1797