xref: /onnv-gate/usr/src/uts/common/fs/pcfs/pc_dir.c (revision 5331:3047ad28a67b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51268Sbatschul  * Common Development and Distribution License (the "License").
61268Sbatschul  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
224723Sksn  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/buf.h>
330Sstevel@tonic-gate #include <sys/vfs.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/vnode.h>
360Sstevel@tonic-gate #include <sys/debug.h>
370Sstevel@tonic-gate #include <sys/cmn_err.h>
384723Sksn #include <sys/sunddi.h>
390Sstevel@tonic-gate #include <sys/fs/pc_label.h>
400Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
410Sstevel@tonic-gate #include <sys/fs/pc_dir.h>
420Sstevel@tonic-gate #include <sys/fs/pc_node.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static int pc_makedirentry(struct pcnode *dp, struct pcdir *direntries,
450Sstevel@tonic-gate     int	ndirentries, struct vattr *vap, offset_t offset);
460Sstevel@tonic-gate static int pc_dirempty(struct pcnode *);
471268Sbatschul static int pc_findentry(struct pcnode *, char *, struct pcslot *, offset_t *);
480Sstevel@tonic-gate static int pc_parsename(char *, char *, char *);
490Sstevel@tonic-gate static int pc_remove_long_fn(struct pcnode *pcp,
500Sstevel@tonic-gate     offset_t lfn_offset);
510Sstevel@tonic-gate static int generate_short_name(struct pcnode *dp, char *namep,
520Sstevel@tonic-gate     struct pcdir *ep);
530Sstevel@tonic-gate static struct pcdir *pc_name_to_pcdir(struct pcnode *dp, char *namep,
540Sstevel@tonic-gate     int ndirentries, int *errret);
550Sstevel@tonic-gate static offset_t pc_find_free_space(struct pcnode *pcp, int ndirentries);
560Sstevel@tonic-gate static int direntries_needed(struct pcnode *dp, char *namep);
570Sstevel@tonic-gate static int pc_is_short_file_name(char *namep, int foldcase);
580Sstevel@tonic-gate static int shortname_exists(struct pcnode *dp, char *fname, char *fext);
590Sstevel@tonic-gate static int pc_dirfixdotdot(struct pcnode *cdp, struct pcnode *opdp,
600Sstevel@tonic-gate     struct pcnode *npdp);
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * Tunables
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate int enable_long_filenames = 1;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Lookup a name in a directory. Return a pointer to the pc_node
680Sstevel@tonic-gate  * which represents the entry.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate int
710Sstevel@tonic-gate pc_dirlook(
720Sstevel@tonic-gate 	struct pcnode *dp,		/* parent directory */
730Sstevel@tonic-gate 	char *namep,			/* name to lookup */
740Sstevel@tonic-gate 	struct pcnode **pcpp)		/* result */
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	struct vnode *vp;
771268Sbatschul 	struct pcslot slot;
780Sstevel@tonic-gate 	int error;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	PC_DPRINTF2(4, "pc_dirlook (dp %p name %s)\n", (void *)dp, namep);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (!(dp->pc_entry.pcd_attr & PCA_DIR)) {
830Sstevel@tonic-gate 		return (ENOTDIR);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	vp = PCTOV(dp);
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * check now for changed disk, before any return(0)
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	if (error = pc_verify(VFSTOPCFS(vp->v_vfsp)))
900Sstevel@tonic-gate 		return (error);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	/*
930Sstevel@tonic-gate 	 * Null component name is synonym for directory being searched.
940Sstevel@tonic-gate 	 */
950Sstevel@tonic-gate 	if (*namep == '\0') {
960Sstevel@tonic-gate 		VN_HOLD(vp);
970Sstevel@tonic-gate 		*pcpp = dp;
980Sstevel@tonic-gate 		return (0);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 	/*
1010Sstevel@tonic-gate 	 * The root directory does not have "." and ".." entries,
1020Sstevel@tonic-gate 	 * so they are faked here.
1030Sstevel@tonic-gate 	 */
1040Sstevel@tonic-gate 	if (vp->v_flag & VROOT) {
1050Sstevel@tonic-gate 		if (bcmp(namep, ".", 2) == 0 || bcmp(namep, "..", 3) == 0) {
1060Sstevel@tonic-gate 			VN_HOLD(vp);
1070Sstevel@tonic-gate 			*pcpp = dp;
1080Sstevel@tonic-gate 			return (0);
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	error = pc_findentry(dp, namep, &slot, NULL);
1120Sstevel@tonic-gate 	if (error == 0) {
1130Sstevel@tonic-gate 		*pcpp = pc_getnode(VFSTOPCFS(vp->v_vfsp),
1140Sstevel@tonic-gate 		    slot.sl_blkno, slot.sl_offset, slot.sl_ep);
1150Sstevel@tonic-gate 		brelse(slot.sl_bp);
1160Sstevel@tonic-gate 		PC_DPRINTF1(4, "pc_dirlook: FOUND pcp=%p\n", (void *)*pcpp);
1170Sstevel@tonic-gate 	} else if (error == EINVAL) {
1180Sstevel@tonic-gate 		error = ENOENT;
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 	return (error);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * Enter a name in a directory.
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate int
1270Sstevel@tonic-gate pc_direnter(
1280Sstevel@tonic-gate 	struct pcnode *dp,		/* directory to make entry in */
1290Sstevel@tonic-gate 	char *namep,			/* name of entry */
1300Sstevel@tonic-gate 	struct vattr *vap,		/* attributes of new entry */
1310Sstevel@tonic-gate 	struct pcnode **pcpp)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	int error;
1341268Sbatschul 	struct pcslot slot;
1350Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
1360Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
1370Sstevel@tonic-gate 	offset_t offset;
1380Sstevel@tonic-gate 	int	blkno;
1390Sstevel@tonic-gate 	int	boff;
1400Sstevel@tonic-gate 	struct buf *bp = NULL;
1410Sstevel@tonic-gate 	struct pcdir *ep;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	PC_DPRINTF4(4, "pc_dirent(dp %p, name %s, vap %p, pcpp %p\n",
1440Sstevel@tonic-gate 	    (void *)dp, namep, (void *)vap, (void *)pcpp);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if (pcpp != NULL)
1470Sstevel@tonic-gate 		*pcpp = NULL;
1480Sstevel@tonic-gate 	/*
1490Sstevel@tonic-gate 	 * Leading spaces are not allowed in DOS.
1500Sstevel@tonic-gate 	 */
1510Sstevel@tonic-gate 	if (*namep == ' ')
1520Sstevel@tonic-gate 		return (EINVAL);
1530Sstevel@tonic-gate 	/*
1540Sstevel@tonic-gate 	 * If name is "." or "..", just look it up.
1550Sstevel@tonic-gate 	 */
1560Sstevel@tonic-gate 	if (PC_NAME_IS_DOT(namep) || PC_NAME_IS_DOTDOT(namep)) {
1570Sstevel@tonic-gate 		if (pcpp) {
1580Sstevel@tonic-gate 			error = pc_dirlook(dp, namep, pcpp);
1590Sstevel@tonic-gate 			if (error)
1600Sstevel@tonic-gate 				return (error);
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 		return (EEXIST);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	if (PCA_IS_HIDDEN(fsp, dp->pc_entry.pcd_attr)) {
1650Sstevel@tonic-gate 		return (EPERM);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 * Make sure directory has not been removed while fs was unlocked.
1690Sstevel@tonic-gate 	 */
1700Sstevel@tonic-gate 	if (dp->pc_entry.pcd_filename[0] == PCD_ERASED) {
1710Sstevel@tonic-gate 		return (ENOENT);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	error = pc_findentry(dp, namep, &slot, NULL);
1740Sstevel@tonic-gate 	if (error == 0) {
1750Sstevel@tonic-gate 		if (pcpp) {
1760Sstevel@tonic-gate 			*pcpp =
1770Sstevel@tonic-gate 			    pc_getnode(fsp, slot.sl_blkno, slot.sl_offset,
1784723Sksn 			    slot.sl_ep);
1790Sstevel@tonic-gate 			error = EEXIST;
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 		brelse(slot.sl_bp);
1820Sstevel@tonic-gate 	} else if (error == ENOENT) {
1830Sstevel@tonic-gate 		struct pcdir *direntries;
1840Sstevel@tonic-gate 		int	ndirentries;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		/*
1870Sstevel@tonic-gate 		 * The entry does not exist. Check write permission in
1880Sstevel@tonic-gate 		 * directory to see if entry can be created.
1890Sstevel@tonic-gate 		 */
1900Sstevel@tonic-gate 		if (dp->pc_entry.pcd_attr & PCA_RDONLY) {
1910Sstevel@tonic-gate 			return (EPERM);
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 		error = 0;
1940Sstevel@tonic-gate 		/*
1950Sstevel@tonic-gate 		 * Make sure there is a slot.
1960Sstevel@tonic-gate 		 */
1970Sstevel@tonic-gate 		if (slot.sl_status == SL_NONE)
1980Sstevel@tonic-gate 			panic("pc_direnter: no slot\n");
1990Sstevel@tonic-gate 		ndirentries = direntries_needed(dp, namep);
2000Sstevel@tonic-gate 		if (ndirentries == -1) {
2010Sstevel@tonic-gate 			return (EINVAL);
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		offset = pc_find_free_space(dp, ndirentries);
2050Sstevel@tonic-gate 		if (offset == -1) {
2060Sstevel@tonic-gate 			return (ENOSPC);
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		/*
2100Sstevel@tonic-gate 		 * Make an entry from the supplied attributes.
2110Sstevel@tonic-gate 		 */
2120Sstevel@tonic-gate 		direntries = pc_name_to_pcdir(dp, namep, ndirentries, &error);
2130Sstevel@tonic-gate 		if (direntries == NULL) {
2140Sstevel@tonic-gate 			return (error);
2150Sstevel@tonic-gate 		}
2160Sstevel@tonic-gate 		error = pc_makedirentry(dp, direntries, ndirentries, vap,
2170Sstevel@tonic-gate 		    offset);
2180Sstevel@tonic-gate 		kmem_free(direntries, ndirentries * sizeof (struct pcdir));
2190Sstevel@tonic-gate 		if (error) {
2200Sstevel@tonic-gate 			return (error);
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 		offset += (ndirentries - 1)  * sizeof (struct pcdir);
2230Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
2240Sstevel@tonic-gate 		error = pc_blkatoff(dp, offset, &bp, &ep);
2250Sstevel@tonic-gate 		if (error) {
2260Sstevel@tonic-gate 			return (error);
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 		blkno = pc_daddrdb(fsp, bp->b_blkno);
2290Sstevel@tonic-gate 		/*
2300Sstevel@tonic-gate 		 * Get a pcnode for the new entry.
2310Sstevel@tonic-gate 		 */
2320Sstevel@tonic-gate 		*pcpp = pc_getnode(fsp, blkno, boff, ep);
2330Sstevel@tonic-gate 		brelse(bp);
2340Sstevel@tonic-gate 		if (vap->va_type == VDIR)
2350Sstevel@tonic-gate 			(*pcpp)->pc_size = fsp->pcfs_clsize;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		/*
2380Sstevel@tonic-gate 		 * Write out the new entry in the parent directory.
2390Sstevel@tonic-gate 		 */
2400Sstevel@tonic-gate 		error = pc_syncfat(fsp);
2410Sstevel@tonic-gate 		if (!error) {
2420Sstevel@tonic-gate 			error = pc_nodeupdate(*pcpp);
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	return (error);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * Template for "." and ".." directory entries.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate static struct {
2520Sstevel@tonic-gate 	struct pcdir t_dot;		/* dot entry */
2530Sstevel@tonic-gate 	struct pcdir t_dotdot;		/* dotdot entry */
2540Sstevel@tonic-gate } dirtemplate = {
2550Sstevel@tonic-gate 	{
2560Sstevel@tonic-gate 		".       ",
2570Sstevel@tonic-gate 		"   ",
2580Sstevel@tonic-gate 		PCA_DIR
2590Sstevel@tonic-gate 	},
2600Sstevel@tonic-gate 	{
2610Sstevel@tonic-gate 		"..      ",
2620Sstevel@tonic-gate 		"   ",
2630Sstevel@tonic-gate 		PCA_DIR
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate };
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * Convert an attributes structure into the short filename entry
2690Sstevel@tonic-gate  * and write out the whole entry.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate static int
2720Sstevel@tonic-gate pc_makedirentry(struct pcnode *dp, struct pcdir *direntries,
2730Sstevel@tonic-gate     int ndirentries, struct vattr *vap, offset_t offset)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
2760Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
2770Sstevel@tonic-gate 	int error;
2780Sstevel@tonic-gate 	struct pcdir *ep;
2790Sstevel@tonic-gate 	int	boff;
2800Sstevel@tonic-gate 	int	i;
2810Sstevel@tonic-gate 	struct buf *bp = NULL;
2820Sstevel@tonic-gate 	timestruc_t now;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (vap != NULL && vap->va_mask & (AT_ATIME|AT_MTIME))
2850Sstevel@tonic-gate 		return (EOPNOTSUPP);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	ep = &direntries[ndirentries - 1];
2880Sstevel@tonic-gate 	gethrestime(&now);
2892720Sfrankho 	if (error = pc_tvtopct(&now, &ep->pcd_mtime))
2902720Sfrankho 		return (error);
2912720Sfrankho 
2920Sstevel@tonic-gate 	ep->pcd_crtime = ep->pcd_mtime;
2930Sstevel@tonic-gate 	ep->pcd_ladate = ep->pcd_mtime.pct_date;
2940Sstevel@tonic-gate 	ep->pcd_crtime_msec = 0;
2950Sstevel@tonic-gate 	ep->pcd_size = 0;
2960Sstevel@tonic-gate 	ep->pcd_attr = 0;
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * Fields we don't use.
2990Sstevel@tonic-gate 	 */
3000Sstevel@tonic-gate 	ep->pcd_ntattr = 0;
3010Sstevel@tonic-gate 	if (!IS_FAT32(fsp))
3020Sstevel@tonic-gate 		ep->un.pcd_eattr = 0;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	if (vap && ((vap->va_mode & 0222) == 0))
3050Sstevel@tonic-gate 		ep->pcd_attr |=  PCA_RDONLY;
3060Sstevel@tonic-gate 	if (vap && (vap->va_type == VDIR)) {
3070Sstevel@tonic-gate 		pc_cluster32_t cn;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		ep->pcd_attr |= PCA_DIR;
3100Sstevel@tonic-gate 		/*
3110Sstevel@tonic-gate 		 * Make dot and dotdot entries for a new directory.
3120Sstevel@tonic-gate 		 */
3130Sstevel@tonic-gate 		cn = pc_alloccluster(fsp, 0);
3140Sstevel@tonic-gate 		switch (cn) {
3150Sstevel@tonic-gate 		case PCF_FREECLUSTER:
3160Sstevel@tonic-gate 			return (ENOSPC);
3170Sstevel@tonic-gate 		case PCF_ERRORCLUSTER:
3180Sstevel@tonic-gate 			return (EIO);
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 		bp = ngeteblk(fsp->pcfs_clsize);
3210Sstevel@tonic-gate 		bp->b_edev = fsp->pcfs_xdev;
3220Sstevel@tonic-gate 		bp->b_dev = cmpdev(bp->b_edev);
3230Sstevel@tonic-gate 		bp->b_blkno = pc_cldaddr(fsp, cn);
3240Sstevel@tonic-gate 		clrbuf(bp);
3250Sstevel@tonic-gate 		pc_setstartcluster(fsp, ep, cn);
3260Sstevel@tonic-gate 		pc_setstartcluster(fsp, &dirtemplate.t_dot, cn);
3270Sstevel@tonic-gate 		cn = pc_getstartcluster(fsp, &dp->pc_entry);
3280Sstevel@tonic-gate 		pc_setstartcluster(fsp, &dirtemplate.t_dotdot, cn);
3290Sstevel@tonic-gate 		dirtemplate.t_dot.pcd_mtime =
3300Sstevel@tonic-gate 		    dirtemplate.t_dotdot.pcd_mtime = ep->pcd_mtime;
3310Sstevel@tonic-gate 		dirtemplate.t_dot.pcd_crtime =
3320Sstevel@tonic-gate 		    dirtemplate.t_dotdot.pcd_crtime = ep->pcd_crtime;
3330Sstevel@tonic-gate 		dirtemplate.t_dot.pcd_ladate =
3340Sstevel@tonic-gate 		    dirtemplate.t_dotdot.pcd_ladate = ep->pcd_ladate;
3350Sstevel@tonic-gate 		dirtemplate.t_dot.pcd_crtime_msec =
3360Sstevel@tonic-gate 		    dirtemplate.t_dotdot.pcd_crtime_msec = 0;
3370Sstevel@tonic-gate 		bcopy(&dirtemplate,
3380Sstevel@tonic-gate 		    bp->b_un.b_addr, sizeof (dirtemplate));
3390Sstevel@tonic-gate 		bwrite2(bp);
3400Sstevel@tonic-gate 		error = geterror(bp);
3410Sstevel@tonic-gate 		brelse(bp);
3420Sstevel@tonic-gate 		if (error) {
3430Sstevel@tonic-gate 			PC_DPRINTF0(1, "pc_makedirentry error");
3440Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
3450Sstevel@tonic-gate 			return (EIO);
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 	} else {
3480Sstevel@tonic-gate 		pc_setstartcluster(fsp, ep, 0);
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 	bp = NULL;
3510Sstevel@tonic-gate 	for (i = 0, ep = NULL; i < ndirentries; i++, ep++) {
3520Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
3530Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
3540Sstevel@tonic-gate 			if (bp != NULL) {
3550Sstevel@tonic-gate 				/* always modified */
3560Sstevel@tonic-gate 				bwrite2(bp);
3570Sstevel@tonic-gate 				error = geterror(bp);
3580Sstevel@tonic-gate 				brelse(bp);
3590Sstevel@tonic-gate 				if (error)
3600Sstevel@tonic-gate 					return (error);
3610Sstevel@tonic-gate 				bp = NULL;
3620Sstevel@tonic-gate 			}
3630Sstevel@tonic-gate 			error = pc_blkatoff(dp, offset, &bp, &ep);
3640Sstevel@tonic-gate 			if (error)
3650Sstevel@tonic-gate 				return (error);
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		*ep = direntries[i];
3690Sstevel@tonic-gate 		offset += sizeof (struct pcdir);
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 	if (bp != NULL) {
3720Sstevel@tonic-gate 		/* always modified */
3730Sstevel@tonic-gate 		bwrite2(bp);
3740Sstevel@tonic-gate 		error = geterror(bp);
3750Sstevel@tonic-gate 		brelse(bp);
3760Sstevel@tonic-gate 		if (error)
3770Sstevel@tonic-gate 			return (error);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 	return (0);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate  * Remove a name from a directory.
3840Sstevel@tonic-gate  */
3850Sstevel@tonic-gate int
3860Sstevel@tonic-gate pc_dirremove(
3870Sstevel@tonic-gate 	struct pcnode *dp,
3880Sstevel@tonic-gate 	char *namep,
3890Sstevel@tonic-gate 	struct vnode *cdir,
390*5331Samw 	enum vtype type,
391*5331Samw 	caller_context_t *ctp)
3920Sstevel@tonic-gate {
3931268Sbatschul 	struct pcslot slot;
3940Sstevel@tonic-gate 	struct pcnode *pcp;
3950Sstevel@tonic-gate 	int error;
3960Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
3970Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
3980Sstevel@tonic-gate 	offset_t lfn_offset = -1;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	PC_DPRINTF2(4, "pc_dirremove (dp %p name %s)\n", (void *)dp, namep);
4010Sstevel@tonic-gate 	if ((dp->pc_entry.pcd_attr & PCA_RDONLY) ||
4020Sstevel@tonic-gate 	    PCA_IS_HIDDEN(fsp, dp->pc_entry.pcd_attr)) {
4030Sstevel@tonic-gate 		return (EPERM);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	error = pc_findentry(dp, namep, &slot, &lfn_offset);
4060Sstevel@tonic-gate 	if (error)
4070Sstevel@tonic-gate 		return (error);
4080Sstevel@tonic-gate 	if (slot.sl_flags == SL_DOT) {
4090Sstevel@tonic-gate 		error = EINVAL;
4100Sstevel@tonic-gate 	} else if (slot.sl_flags == SL_DOTDOT) {
4110Sstevel@tonic-gate 		error = ENOTEMPTY;
4120Sstevel@tonic-gate 	} else {
4130Sstevel@tonic-gate 		pcp =
4140Sstevel@tonic-gate 		    pc_getnode(VFSTOPCFS(vp->v_vfsp),
4154723Sksn 		    slot.sl_blkno, slot.sl_offset, slot.sl_ep);
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 	if (error) {
4180Sstevel@tonic-gate 		brelse(slot.sl_bp);
4190Sstevel@tonic-gate 		return (error);
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 	if (type == VDIR) {
4220Sstevel@tonic-gate 		if (pcp->pc_entry.pcd_attr & PCA_DIR) {
4230Sstevel@tonic-gate 			if (PCTOV(pcp) == cdir)
4240Sstevel@tonic-gate 				error = EINVAL;
4250Sstevel@tonic-gate 			else if (!pc_dirempty(pcp))
4260Sstevel@tonic-gate 				error = ENOTEMPTY;
4270Sstevel@tonic-gate 		} else {
4280Sstevel@tonic-gate 			error = ENOTDIR;
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 	} else {
4310Sstevel@tonic-gate 		if (pcp->pc_entry.pcd_attr & PCA_DIR)
4320Sstevel@tonic-gate 			error = EISDIR;
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 	if (error == 0) {
4350Sstevel@tonic-gate 		/*
4360Sstevel@tonic-gate 		 * Mark the in core node and on disk entry
4370Sstevel@tonic-gate 		 * as removed. The slot may then be reused.
4380Sstevel@tonic-gate 		 * The files clusters will be deallocated
4390Sstevel@tonic-gate 		 * when the last reference goes away.
4400Sstevel@tonic-gate 		 */
4410Sstevel@tonic-gate 		pcp->pc_eblkno = -1;
4420Sstevel@tonic-gate 		pcp->pc_entry.pcd_filename[0] = PCD_ERASED;
4430Sstevel@tonic-gate 		if (lfn_offset != -1) {
4440Sstevel@tonic-gate 			brelse(slot.sl_bp);
4450Sstevel@tonic-gate 			error = pc_remove_long_fn(dp, lfn_offset);
4460Sstevel@tonic-gate 			if (error) {
4470Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
4480Sstevel@tonic-gate 				pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
4490Sstevel@tonic-gate 				return (EIO);
4500Sstevel@tonic-gate 			}
4510Sstevel@tonic-gate 		} else {
4520Sstevel@tonic-gate 			slot.sl_ep->pcd_filename[0] = PCD_ERASED;
4530Sstevel@tonic-gate 			bwrite2(slot.sl_bp);
4540Sstevel@tonic-gate 			error = geterror(slot.sl_bp);
4550Sstevel@tonic-gate 			brelse(slot.sl_bp);
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 		if (error) {
4580Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
4590Sstevel@tonic-gate 			pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
4600Sstevel@tonic-gate 			return (EIO);
4610Sstevel@tonic-gate 		} else if (type == VDIR) {
4620Sstevel@tonic-gate 			error = pc_truncate(pcp, 0L);
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	} else {
4660Sstevel@tonic-gate 		brelse(slot.sl_bp);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (error == 0) {
4700Sstevel@tonic-gate 		if (type == VDIR) {
471*5331Samw 			vnevent_rmdir(PCTOV(pcp), vp, namep, ctp);
4720Sstevel@tonic-gate 		} else {
473*5331Samw 			vnevent_remove(PCTOV(pcp), vp, namep, ctp);
4740Sstevel@tonic-gate 		}
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	VN_RELE(PCTOV(pcp));
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	return (error);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate /*
4830Sstevel@tonic-gate  * Determine whether a directory is empty.
4840Sstevel@tonic-gate  */
4850Sstevel@tonic-gate static int
4860Sstevel@tonic-gate pc_dirempty(struct pcnode *pcp)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	struct buf *bp;
4890Sstevel@tonic-gate 	struct pcdir *ep;
4900Sstevel@tonic-gate 	offset_t offset;
4910Sstevel@tonic-gate 	int boff;
4920Sstevel@tonic-gate 	char c;
4930Sstevel@tonic-gate 	int error;
4940Sstevel@tonic-gate 	struct vnode *vp;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	vp = PCTOV(pcp);
4970Sstevel@tonic-gate 	bp = NULL;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	offset = 0;
5000Sstevel@tonic-gate 	for (;;) {
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		/*
5030Sstevel@tonic-gate 		 * If offset is on a block boundary,
5040Sstevel@tonic-gate 		 * read in the next directory block.
5050Sstevel@tonic-gate 		 * Release previous if it exists.
5060Sstevel@tonic-gate 		 */
5070Sstevel@tonic-gate 		boff = pc_blkoff(VFSTOPCFS(vp->v_vfsp), offset);
5080Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
5090Sstevel@tonic-gate 			if (bp != NULL)
5100Sstevel@tonic-gate 				brelse(bp);
5110Sstevel@tonic-gate 			if (error = pc_blkatoff(pcp, offset, &bp, &ep)) {
5120Sstevel@tonic-gate 				return (error);
5130Sstevel@tonic-gate 			}
5140Sstevel@tonic-gate 		}
5150Sstevel@tonic-gate 		if (PCDL_IS_LFN(ep)) {
5160Sstevel@tonic-gate 			error = pc_extract_long_fn(pcp, NULL, &ep, &offset,
5170Sstevel@tonic-gate 			    &bp);
5180Sstevel@tonic-gate 			/*
5190Sstevel@tonic-gate 			 * EINVAL means the lfn was invalid, so start with
5200Sstevel@tonic-gate 			 * the next entry. Otherwise, an error occurred _or_
5210Sstevel@tonic-gate 			 * the lfn is valid, either of which means the
5220Sstevel@tonic-gate 			 * directory is not empty.
5230Sstevel@tonic-gate 			 */
5240Sstevel@tonic-gate 			if (error == EINVAL)
5250Sstevel@tonic-gate 				continue;
5260Sstevel@tonic-gate 			else {
5270Sstevel@tonic-gate 				if (bp)
5280Sstevel@tonic-gate 					brelse(bp);
5290Sstevel@tonic-gate 				return (error);
5300Sstevel@tonic-gate 			}
5310Sstevel@tonic-gate 		}
5320Sstevel@tonic-gate 		c = ep->pcd_filename[0];
5330Sstevel@tonic-gate 		if (c == PCD_UNUSED)
5340Sstevel@tonic-gate 			break;
5350Sstevel@tonic-gate 		if ((c != '.') && (c != PCD_ERASED)) {
5360Sstevel@tonic-gate 			brelse(bp);
5370Sstevel@tonic-gate 			return (0);
5380Sstevel@tonic-gate 		}
5390Sstevel@tonic-gate 		if ((c == '.') && !PC_SHORTNAME_IS_DOT(ep->pcd_filename) &&
5400Sstevel@tonic-gate 		    !PC_SHORTNAME_IS_DOTDOT(ep->pcd_filename)) {
5410Sstevel@tonic-gate 			brelse(bp);
5420Sstevel@tonic-gate 			return (0);
5430Sstevel@tonic-gate 		}
5440Sstevel@tonic-gate 		ep++;
5450Sstevel@tonic-gate 		offset += sizeof (struct pcdir);
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 	if (bp != NULL)
5480Sstevel@tonic-gate 		brelse(bp);
5490Sstevel@tonic-gate 	return (1);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate /*
5530Sstevel@tonic-gate  * Rename a file.
5540Sstevel@tonic-gate  */
5550Sstevel@tonic-gate int
5560Sstevel@tonic-gate pc_rename(
5570Sstevel@tonic-gate 	struct pcnode *dp,		/* parent directory */
5580Sstevel@tonic-gate 	struct pcnode *tdp,		/* target directory */
5590Sstevel@tonic-gate 	char *snm,			/* source file name */
560*5331Samw 	char *tnm,			/* target file name */
561*5331Samw 	caller_context_t *ctp)
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate 	struct pcnode *pcp;	/* pcnode we are trying to rename */
5640Sstevel@tonic-gate 	struct pcnode *tpcp;	/* pcnode that's in our way */
5651268Sbatschul 	struct pcslot slot;
5660Sstevel@tonic-gate 	int error;
5670Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
5680Sstevel@tonic-gate 	struct vnode *svp = NULL;
5690Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
5700Sstevel@tonic-gate 	int filecasechange = 0;
5710Sstevel@tonic-gate 	int oldisdir = 0;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	PC_DPRINTF3(4, "pc_rename(0x%p, %s, %s)\n", (void *)dp, snm, tnm);
5740Sstevel@tonic-gate 	/*
5750Sstevel@tonic-gate 	 * Leading spaces are not allowed in DOS.
5760Sstevel@tonic-gate 	 */
5770Sstevel@tonic-gate 	if (*tnm == ' ')
5780Sstevel@tonic-gate 		return (EINVAL);
5790Sstevel@tonic-gate 	/*
5800Sstevel@tonic-gate 	 * No dot or dotdot.
5810Sstevel@tonic-gate 	 */
5820Sstevel@tonic-gate 	if (PC_NAME_IS_DOT(snm) || PC_NAME_IS_DOTDOT(snm) ||
5830Sstevel@tonic-gate 	    PC_NAME_IS_DOT(tnm) || PC_NAME_IS_DOTDOT(tnm))
5840Sstevel@tonic-gate 		return (EINVAL);
5850Sstevel@tonic-gate 	/*
5860Sstevel@tonic-gate 	 * Get the source node.  We'll jump back to here if trying to
5870Sstevel@tonic-gate 	 * move on top of an existing file, after deleting that file.
5880Sstevel@tonic-gate 	 */
5890Sstevel@tonic-gate top:
5900Sstevel@tonic-gate 	error = pc_findentry(dp, snm, &slot, NULL);
5910Sstevel@tonic-gate 	if (error) {
5920Sstevel@tonic-gate 		return (error);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 	pcp = pc_getnode(VFSTOPCFS(vp->v_vfsp),
5950Sstevel@tonic-gate 	    slot.sl_blkno, slot.sl_offset, slot.sl_ep);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	brelse(slot.sl_bp);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	if (pcp)
6000Sstevel@tonic-gate 		svp = PCTOV(pcp);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/*
6030Sstevel@tonic-gate 	 * is the rename invalid, i.e. rename("a", "a/a")
6040Sstevel@tonic-gate 	 */
6050Sstevel@tonic-gate 	if (pcp == tdp) {
6060Sstevel@tonic-gate 		if (svp)
6070Sstevel@tonic-gate 			VN_RELE(svp);
6080Sstevel@tonic-gate 		return (EINVAL);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	/*
6120Sstevel@tonic-gate 	 * Are we just changing the case of an existing name?
6130Sstevel@tonic-gate 	 */
6140Sstevel@tonic-gate 	if ((dp->pc_scluster == tdp->pc_scluster) &&
6150Sstevel@tonic-gate 	    (strcasecmp(snm, tnm) == 0)) {
6160Sstevel@tonic-gate 		filecasechange = 1;
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	oldisdir = pcp->pc_entry.pcd_attr & PCA_DIR;
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	/*
6220Sstevel@tonic-gate 	 * see if the target exists
6230Sstevel@tonic-gate 	 */
6240Sstevel@tonic-gate 	error = pc_findentry(tdp, tnm, &slot, NULL);
6250Sstevel@tonic-gate 	if (error == 0 && filecasechange == 0) {
6260Sstevel@tonic-gate 		/*
6270Sstevel@tonic-gate 		 * Target exists.  If it's a file, delete it.  If it's
6280Sstevel@tonic-gate 		 * a directory, bail.
6290Sstevel@tonic-gate 		 */
6300Sstevel@tonic-gate 		int newisdir;
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 		tpcp = pc_getnode(VFSTOPCFS(vp->v_vfsp),
6330Sstevel@tonic-gate 		    slot.sl_blkno, slot.sl_offset, slot.sl_ep);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		newisdir = tpcp->pc_entry.pcd_attr & PCA_DIR;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		brelse(slot.sl_bp);
638*5331Samw 		vnevent_rename_dest(PCTOV(tpcp), PCTOV(tdp), tnm, ctp);
6390Sstevel@tonic-gate 		VN_RELE(PCTOV(tpcp));
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		/*
6420Sstevel@tonic-gate 		 * Error cases (from rename(2)):
6430Sstevel@tonic-gate 		 * old is dir, new is dir: EEXIST
6440Sstevel@tonic-gate 		 * old is dir, new is nondir: ENOTDIR
6450Sstevel@tonic-gate 		 * old is nondir, new is dir: EISDIR
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		if (!newisdir) {
6480Sstevel@tonic-gate 			if (oldisdir) {
6490Sstevel@tonic-gate 				error = ENOTDIR;
6500Sstevel@tonic-gate 			} else {
6510Sstevel@tonic-gate 				/* nondir/nondir, remove target */
6520Sstevel@tonic-gate 				error = pc_dirremove(tdp, tnm,
653*5331Samw 				    (struct vnode *)NULL, VREG, ctp);
6540Sstevel@tonic-gate 				if (error == 0) {
6550Sstevel@tonic-gate 					VN_RELE(PCTOV(pcp));
6560Sstevel@tonic-gate 					goto top;
6570Sstevel@tonic-gate 				}
6580Sstevel@tonic-gate 			}
6590Sstevel@tonic-gate 		} else if (oldisdir) {
6600Sstevel@tonic-gate 			/* dir/dir, remove target */
6610Sstevel@tonic-gate 			error = pc_dirremove(tdp, tnm,
662*5331Samw 			    (struct vnode *)NULL, VDIR, ctp);
6630Sstevel@tonic-gate 			if (error == 0) {
6640Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
6650Sstevel@tonic-gate 				goto top;
6660Sstevel@tonic-gate 			}
6670Sstevel@tonic-gate 			/* Follow rename(2)'s spec... */
6680Sstevel@tonic-gate 			if (error == ENOTEMPTY) {
6690Sstevel@tonic-gate 				error = EEXIST;
6700Sstevel@tonic-gate 			}
6710Sstevel@tonic-gate 		} else {
6720Sstevel@tonic-gate 			/* nondir/dir, bail */
6730Sstevel@tonic-gate 			error = EISDIR;
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if ((error == 0) || (error == ENOENT)) {
6780Sstevel@tonic-gate 		offset_t lfn_offset = -1;
6790Sstevel@tonic-gate 		int	blkno;
6800Sstevel@tonic-gate 		struct pcdir *direntries;
6810Sstevel@tonic-gate 		struct pcdir *ep;
6820Sstevel@tonic-gate 		int	ndirentries;
6830Sstevel@tonic-gate 		pc_cluster16_t pct_lo;
6840Sstevel@tonic-gate 		pc_cluster16_t pct_hi;
6850Sstevel@tonic-gate 		offset_t offset;
6860Sstevel@tonic-gate 		int	boff;
6870Sstevel@tonic-gate 		struct buf *bp = NULL;
6880Sstevel@tonic-gate 		uchar_t	attr;
6890Sstevel@tonic-gate 		int	size;
6900Sstevel@tonic-gate 		struct pctime mtime;
6910Sstevel@tonic-gate 		struct pctime crtime;
6920Sstevel@tonic-gate 		uchar_t	ntattr;
6930Sstevel@tonic-gate 		ushort_t ladate;
6940Sstevel@tonic-gate 		ushort_t eattr;
6950Sstevel@tonic-gate 		uchar_t	crtime_msec;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		/*
6980Sstevel@tonic-gate 		 * Rename the source.
6990Sstevel@tonic-gate 		 */
7000Sstevel@tonic-gate 		/*
7010Sstevel@tonic-gate 		 * Delete the old name, and create a new name.
7020Sstevel@tonic-gate 		 */
7030Sstevel@tonic-gate 		if (filecasechange == 1 && error == 0)
7040Sstevel@tonic-gate 			brelse(slot.sl_bp);
7050Sstevel@tonic-gate 		ndirentries = direntries_needed(tdp, tnm);
7060Sstevel@tonic-gate 		if (ndirentries == -1) {
7070Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7080Sstevel@tonic-gate 			return (EINVAL);
7090Sstevel@tonic-gate 		}
7100Sstevel@tonic-gate 		/*
7110Sstevel@tonic-gate 		 * first see if we have enough space to create the new
7120Sstevel@tonic-gate 		 * name before destroying the old one.
7130Sstevel@tonic-gate 		 */
7140Sstevel@tonic-gate 		offset = pc_find_free_space(tdp, ndirentries);
7150Sstevel@tonic-gate 		if (offset == -1) {
7160Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7170Sstevel@tonic-gate 			return (ENOSPC);
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 		error = pc_findentry(dp, snm, &slot, &lfn_offset);
7210Sstevel@tonic-gate 		if (error) {
7220Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7230Sstevel@tonic-gate 			return (error);
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 		pct_lo = slot.sl_ep->pcd_scluster_lo;
7260Sstevel@tonic-gate 		if (IS_FAT32(fsp))
7270Sstevel@tonic-gate 			pct_hi = slot.sl_ep->un.pcd_scluster_hi;
7280Sstevel@tonic-gate 		else
7290Sstevel@tonic-gate 			eattr = slot.sl_ep->un.pcd_eattr;
7300Sstevel@tonic-gate 		size = slot.sl_ep->pcd_size;
7310Sstevel@tonic-gate 		attr = slot.sl_ep->pcd_attr;
7320Sstevel@tonic-gate 		mtime = slot.sl_ep->pcd_mtime;
7330Sstevel@tonic-gate 		crtime = slot.sl_ep->pcd_crtime;
7340Sstevel@tonic-gate 		crtime_msec = slot.sl_ep->pcd_crtime_msec;
7350Sstevel@tonic-gate 		ntattr = slot.sl_ep->pcd_ntattr;
7360Sstevel@tonic-gate 		ladate = slot.sl_ep->pcd_ladate;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		if (lfn_offset != -1) {
7390Sstevel@tonic-gate 			brelse(slot.sl_bp);
7400Sstevel@tonic-gate 			error = pc_remove_long_fn(dp, lfn_offset);
7410Sstevel@tonic-gate 			if (error) {
7420Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
7430Sstevel@tonic-gate 				pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
7440Sstevel@tonic-gate 				return (error);
7450Sstevel@tonic-gate 			}
7460Sstevel@tonic-gate 		} else {
7470Sstevel@tonic-gate 			slot.sl_ep->pcd_filename[0] =
7480Sstevel@tonic-gate 			    pcp->pc_entry.pcd_filename[0] = PCD_ERASED;
7490Sstevel@tonic-gate 			bwrite2(slot.sl_bp);
7500Sstevel@tonic-gate 			error = geterror(slot.sl_bp);
7510Sstevel@tonic-gate 			brelse(slot.sl_bp);
7520Sstevel@tonic-gate 		}
7530Sstevel@tonic-gate 		if (error) {
7540Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7550Sstevel@tonic-gate 			pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
7560Sstevel@tonic-gate 			return (EIO);
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 		/*
7600Sstevel@tonic-gate 		 * Make an entry from the supplied attributes.
7610Sstevel@tonic-gate 		 */
7620Sstevel@tonic-gate 		direntries = pc_name_to_pcdir(tdp, tnm, ndirentries, &error);
7630Sstevel@tonic-gate 		if (direntries == NULL) {
7640Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7650Sstevel@tonic-gate 			return (error);
7660Sstevel@tonic-gate 		}
7670Sstevel@tonic-gate 		error = pc_makedirentry(tdp, direntries, ndirentries, NULL,
7680Sstevel@tonic-gate 		    offset);
7690Sstevel@tonic-gate 		kmem_free(direntries, ndirentries * sizeof (struct pcdir));
7700Sstevel@tonic-gate 		if (error) {
7710Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7720Sstevel@tonic-gate 			return (error);
7730Sstevel@tonic-gate 		}
7740Sstevel@tonic-gate 		/* advance to short name */
7750Sstevel@tonic-gate 		offset += (ndirentries - 1)  * sizeof (struct pcdir);
7760Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
7770Sstevel@tonic-gate 		error = pc_blkatoff(tdp, offset, &bp, &ep);
7780Sstevel@tonic-gate 		if (error) {
7790Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7800Sstevel@tonic-gate 			return (error);
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 		blkno = pc_daddrdb(fsp, bp->b_blkno);
7830Sstevel@tonic-gate 		ep->pcd_scluster_lo = pct_lo;
7840Sstevel@tonic-gate 		if (IS_FAT32(fsp))
7850Sstevel@tonic-gate 			ep->un.pcd_scluster_hi = pct_hi;
7860Sstevel@tonic-gate 		else
7870Sstevel@tonic-gate 			ep->un.pcd_eattr = eattr;
7880Sstevel@tonic-gate 		ep->pcd_size = size;
7890Sstevel@tonic-gate 		ep->pcd_attr = attr;
7900Sstevel@tonic-gate 		ep->pcd_mtime = mtime;
7910Sstevel@tonic-gate 		ep->pcd_crtime = crtime;
7920Sstevel@tonic-gate 		ep->pcd_crtime_msec = crtime_msec;
7930Sstevel@tonic-gate 		ep->pcd_ntattr = ntattr;
7940Sstevel@tonic-gate 		ep->pcd_ladate = ladate;
7950Sstevel@tonic-gate 		bwrite2(bp);
7960Sstevel@tonic-gate 		error = geterror(bp);
7970Sstevel@tonic-gate 		pcp->pc_eblkno = blkno;
7980Sstevel@tonic-gate 		pcp->pc_eoffset = boff;
7990Sstevel@tonic-gate 		pcp->pc_entry = *ep;
8000Sstevel@tonic-gate 		pcp->pc_flags |= PC_CHG;
8010Sstevel@tonic-gate 		brelse(bp);
8020Sstevel@tonic-gate 		if (error) {
8030Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
8040Sstevel@tonic-gate 			pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
8050Sstevel@tonic-gate 			return (EIO);
8060Sstevel@tonic-gate 		}
8070Sstevel@tonic-gate 		/* No need to fix ".." if we're renaming within a dir */
8080Sstevel@tonic-gate 		if (oldisdir && dp != tdp) {
8090Sstevel@tonic-gate 			if ((error = pc_dirfixdotdot(pcp, dp, tdp)) != 0) {
8100Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
8110Sstevel@tonic-gate 				return (error);
8120Sstevel@tonic-gate 			}
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 		if ((error = pc_nodeupdate(pcp)) != 0) {
8150Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
8160Sstevel@tonic-gate 			return (error);
8170Sstevel@tonic-gate 		}
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate out:
820*5331Samw 	vnevent_rename_src(PCTOV(pcp), PCTOV(dp), snm, ctp);
8214863Spraks 	if (dp != tdp) {
822*5331Samw 		vnevent_rename_dest_dir(PCTOV(tdp), ctp);
8234863Spraks 	}
8244863Spraks 
8250Sstevel@tonic-gate 	VN_RELE(PCTOV(pcp));
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	return (error);
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate /*
8310Sstevel@tonic-gate  * Fix the ".." entry of the child directory so that it points to the
8320Sstevel@tonic-gate  * new parent directory instead of the old one.
8330Sstevel@tonic-gate  */
8340Sstevel@tonic-gate static int
8350Sstevel@tonic-gate pc_dirfixdotdot(struct pcnode *dp,	/* child directory being moved */
8360Sstevel@tonic-gate 	struct pcnode *opdp,		/* old parent directory */
8370Sstevel@tonic-gate 	struct pcnode *npdp)		/* new parent directory */
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate 	pc_cluster32_t cn;
8400Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
8410Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
8420Sstevel@tonic-gate 	int error = 0;
8430Sstevel@tonic-gate 	struct buf *bp = NULL;
8440Sstevel@tonic-gate 	struct pcdir *ep = NULL;
8450Sstevel@tonic-gate 	struct pcdir *tep = NULL;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	/*
8480Sstevel@tonic-gate 	 * set the new child's ".." directory entry starting cluster to
8490Sstevel@tonic-gate 	 * point to the new parent's starting cluster
8500Sstevel@tonic-gate 	 */
8510Sstevel@tonic-gate 	ASSERT(opdp != npdp);
8520Sstevel@tonic-gate 	error = pc_blkatoff(dp, (offset_t)0, &bp, &ep);
8530Sstevel@tonic-gate 	if (error) {
8540Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_dirfixdotdot: error in blkatoff\n");
8550Sstevel@tonic-gate 		return (error);
8560Sstevel@tonic-gate 	}
8570Sstevel@tonic-gate 	tep = ep;
8580Sstevel@tonic-gate 	ep++;
8590Sstevel@tonic-gate 	if (!PC_SHORTNAME_IS_DOT(tep->pcd_filename) &&
8600Sstevel@tonic-gate 	    !PC_SHORTNAME_IS_DOTDOT(ep->pcd_filename)) {
8610Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_dirfixdotdot: mangled directory entry\n");
8620Sstevel@tonic-gate 		error = ENOTDIR;
8630Sstevel@tonic-gate 		return (error);
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 	cn = pc_getstartcluster(fsp, &npdp->pc_entry);
8660Sstevel@tonic-gate 	pc_setstartcluster(fsp, ep, cn);
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	bwrite2(bp);
8690Sstevel@tonic-gate 	error = geterror(bp);
8700Sstevel@tonic-gate 	brelse(bp);
8710Sstevel@tonic-gate 	if (error) {
8720Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_dirfixdotdot: error in write\n");
8730Sstevel@tonic-gate 		pc_mark_irrecov(fsp);
8740Sstevel@tonic-gate 		return (EIO);
8750Sstevel@tonic-gate 	}
8760Sstevel@tonic-gate 	return (0);
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate /*
8810Sstevel@tonic-gate  * Search a directory for an entry.
8820Sstevel@tonic-gate  * The directory should be locked as this routine
8830Sstevel@tonic-gate  * will sleep on I/O while searching.
8840Sstevel@tonic-gate  */
8850Sstevel@tonic-gate static int
8860Sstevel@tonic-gate pc_findentry(
8870Sstevel@tonic-gate 	struct pcnode *dp,		/* parent directory */
8880Sstevel@tonic-gate 	char *namep,			/* name to lookup */
8891268Sbatschul 	struct pcslot *slotp,
8900Sstevel@tonic-gate 	offset_t *lfn_offset)
8910Sstevel@tonic-gate {
8920Sstevel@tonic-gate 	offset_t offset;
8930Sstevel@tonic-gate 	struct pcdir *ep = NULL;
8940Sstevel@tonic-gate 	int boff;
8950Sstevel@tonic-gate 	int error;
8960Sstevel@tonic-gate 	struct vnode *vp;
8970Sstevel@tonic-gate 	struct pcfs *fsp;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	vp = PCTOV(dp);
9000Sstevel@tonic-gate 	PC_DPRINTF2(6, "pc_findentry: looking for %s in dir 0x%p\n", namep,
9010Sstevel@tonic-gate 	    (void *)dp);
9020Sstevel@tonic-gate 	slotp->sl_status = SL_NONE;
9030Sstevel@tonic-gate 	if (!(dp->pc_entry.pcd_attr & PCA_DIR)) {
9040Sstevel@tonic-gate 		return (ENOTDIR);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 	/*
9070Sstevel@tonic-gate 	 * Verify that the dp is still valid on the disk
9080Sstevel@tonic-gate 	 */
9090Sstevel@tonic-gate 	fsp = VFSTOPCFS(vp->v_vfsp);
9100Sstevel@tonic-gate 	error = pc_verify(fsp);
9110Sstevel@tonic-gate 	if (error)
9120Sstevel@tonic-gate 		return (error);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	slotp->sl_bp = NULL;
9150Sstevel@tonic-gate 	offset = 0;
9160Sstevel@tonic-gate 	for (;;) {
9170Sstevel@tonic-gate 		/*
9180Sstevel@tonic-gate 		 * If offset is on a block boundary,
9190Sstevel@tonic-gate 		 * read in the next directory block.
9200Sstevel@tonic-gate 		 * Release previous if it exists.
9210Sstevel@tonic-gate 		 */
9220Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
9230Sstevel@tonic-gate 		if (boff == 0 || slotp->sl_bp == NULL ||
9240Sstevel@tonic-gate 		    boff >= slotp->sl_bp->b_bcount) {
9250Sstevel@tonic-gate 			if (slotp->sl_bp != NULL) {
9260Sstevel@tonic-gate 				brelse(slotp->sl_bp);
9270Sstevel@tonic-gate 				slotp->sl_bp = NULL;
9280Sstevel@tonic-gate 			}
9290Sstevel@tonic-gate 			error = pc_blkatoff(dp, offset, &slotp->sl_bp, &ep);
9300Sstevel@tonic-gate 			if (error == ENOENT && slotp->sl_status == SL_NONE) {
9310Sstevel@tonic-gate 				slotp->sl_status = SL_EXTEND;
9320Sstevel@tonic-gate 				slotp->sl_offset = (int)offset;
9330Sstevel@tonic-gate 			}
9340Sstevel@tonic-gate 			if (error)
9350Sstevel@tonic-gate 				return (error);
9360Sstevel@tonic-gate 		}
9370Sstevel@tonic-gate 		if ((ep->pcd_filename[0] == PCD_UNUSED) ||
9380Sstevel@tonic-gate 		    (ep->pcd_filename[0] == PCD_ERASED)) {
9390Sstevel@tonic-gate 			/*
9400Sstevel@tonic-gate 			 * note empty slots, in case name is not found
9410Sstevel@tonic-gate 			 */
9420Sstevel@tonic-gate 			if (slotp->sl_status == SL_NONE) {
9430Sstevel@tonic-gate 				slotp->sl_status = SL_FOUND;
9440Sstevel@tonic-gate 				slotp->sl_blkno = pc_daddrdb(fsp,
9450Sstevel@tonic-gate 				    slotp->sl_bp->b_blkno);
9460Sstevel@tonic-gate 				slotp->sl_offset = boff;
9470Sstevel@tonic-gate 			}
9480Sstevel@tonic-gate 			/*
9490Sstevel@tonic-gate 			 * If unused we've hit the end of the directory
9500Sstevel@tonic-gate 			 */
9510Sstevel@tonic-gate 			if (ep->pcd_filename[0] == PCD_UNUSED)
9520Sstevel@tonic-gate 				break;
9530Sstevel@tonic-gate 			offset += sizeof (struct pcdir);
9540Sstevel@tonic-gate 			ep++;
9550Sstevel@tonic-gate 			continue;
9560Sstevel@tonic-gate 		}
9570Sstevel@tonic-gate 		if (PCDL_IS_LFN(ep)) {
9580Sstevel@tonic-gate 			offset_t t = offset;
9590Sstevel@tonic-gate 			if (pc_match_long_fn(dp, namep, &ep,
9600Sstevel@tonic-gate 			    slotp, &offset) == 0) {
9610Sstevel@tonic-gate 				if (lfn_offset != NULL)
9620Sstevel@tonic-gate 					*lfn_offset = t;
9630Sstevel@tonic-gate 				return (0);
9640Sstevel@tonic-gate 			}
9650Sstevel@tonic-gate 			continue;
9660Sstevel@tonic-gate 		}
9670Sstevel@tonic-gate 		if (pc_match_short_fn(dp, namep, &ep, slotp, &offset) == 0)
9680Sstevel@tonic-gate 			return (0);
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 	if (slotp->sl_bp != NULL) {
9710Sstevel@tonic-gate 		brelse(slotp->sl_bp);
9720Sstevel@tonic-gate 		slotp->sl_bp = NULL;
9730Sstevel@tonic-gate 	}
9740Sstevel@tonic-gate 	return (ENOENT);
9750Sstevel@tonic-gate }
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate /*
9780Sstevel@tonic-gate  * Obtain the block at offset "offset" in file pcp.
9790Sstevel@tonic-gate  */
9800Sstevel@tonic-gate int
9810Sstevel@tonic-gate pc_blkatoff(
9820Sstevel@tonic-gate 	struct pcnode *pcp,
9830Sstevel@tonic-gate 	offset_t offset,
9840Sstevel@tonic-gate 	struct buf **bpp,
9850Sstevel@tonic-gate 	struct pcdir **epp)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 	struct pcfs *fsp;
9880Sstevel@tonic-gate 	struct buf *bp;
9890Sstevel@tonic-gate 	int size;
9900Sstevel@tonic-gate 	int error;
9910Sstevel@tonic-gate 	daddr_t bn;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	fsp = VFSTOPCFS(PCTOV(pcp)->v_vfsp);
9940Sstevel@tonic-gate 	size = pc_blksize(fsp, pcp, offset);
9950Sstevel@tonic-gate 	if (pc_blkoff(fsp, offset) >= size) {
9960Sstevel@tonic-gate 		PC_DPRINTF0(5, "pc_blkatoff: ENOENT\n");
9970Sstevel@tonic-gate 		return (ENOENT);
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 	error = pc_bmap(pcp, pc_lblkno(fsp, offset), &bn, (uint_t *)0);
10000Sstevel@tonic-gate 	if (error)
10010Sstevel@tonic-gate 		return (error);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	bp = bread(fsp->pcfs_xdev, bn, size);
10040Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
10050Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_blkatoff: error\n");
10060Sstevel@tonic-gate 		brelse(bp);
10070Sstevel@tonic-gate 		pc_mark_irrecov(fsp);
10080Sstevel@tonic-gate 		return (EIO);
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 	if (epp) {
10110Sstevel@tonic-gate 		*epp =
10120Sstevel@tonic-gate 		    (struct pcdir *)(bp->b_un.b_addr + pc_blkoff(fsp, offset));
10130Sstevel@tonic-gate 	}
10140Sstevel@tonic-gate 	*bpp = bp;
10150Sstevel@tonic-gate 	return (0);
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate /*
10190Sstevel@tonic-gate  * Parse user filename into the pc form of "filename.extension".
10200Sstevel@tonic-gate  * If names are too long for the format (and enable_long_filenames is set)
10210Sstevel@tonic-gate  * it returns EINVAL (since either this name was read from the disk (so
10220Sstevel@tonic-gate  * it must fit), _or_ we're trying to match a long file name (so we
10230Sstevel@tonic-gate  * should fail).  Tests for characters that are invalid in PCDOS and
10240Sstevel@tonic-gate  * converts to upper case (unless foldcase is 0).
10250Sstevel@tonic-gate  */
10260Sstevel@tonic-gate static int
10270Sstevel@tonic-gate pc_parsename(
10280Sstevel@tonic-gate 	char *namep,
10290Sstevel@tonic-gate 	char *fnamep,
10300Sstevel@tonic-gate 	char *fextp)
10310Sstevel@tonic-gate {
10320Sstevel@tonic-gate 	int n;
10330Sstevel@tonic-gate 	char c;
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	n = PCFNAMESIZE;
10360Sstevel@tonic-gate 	c = *namep++;
10370Sstevel@tonic-gate 	if (c == 0)
10380Sstevel@tonic-gate 		return (EINVAL);
10390Sstevel@tonic-gate 	if (c == '.') {
10400Sstevel@tonic-gate 		/*
10410Sstevel@tonic-gate 		 * check for "." and "..".
10420Sstevel@tonic-gate 		 */
10430Sstevel@tonic-gate 		*fnamep++ = c;
10440Sstevel@tonic-gate 		n--;
10450Sstevel@tonic-gate 		if (c = *namep++) {
10460Sstevel@tonic-gate 			if ((c != '.') || (c = *namep)) /* ".x" or "..x" */
10470Sstevel@tonic-gate 				return (EINVAL);
10480Sstevel@tonic-gate 			*fnamep++ = '.';
10490Sstevel@tonic-gate 			n--;
10500Sstevel@tonic-gate 		}
10510Sstevel@tonic-gate 	} else {
10520Sstevel@tonic-gate 		/*
10530Sstevel@tonic-gate 		 * filename up to '.'
10540Sstevel@tonic-gate 		 */
10550Sstevel@tonic-gate 		do {
10560Sstevel@tonic-gate 			if (n-- > 0) {
10570Sstevel@tonic-gate 				c = toupper(c);
10580Sstevel@tonic-gate 				if (!pc_validchar(c))
10590Sstevel@tonic-gate 					return (EINVAL);
10600Sstevel@tonic-gate 				*fnamep++ = c;
10610Sstevel@tonic-gate 			} else {
10620Sstevel@tonic-gate 				/* not short */
10630Sstevel@tonic-gate 				if (enable_long_filenames)
10640Sstevel@tonic-gate 					return (EINVAL);
10650Sstevel@tonic-gate 			}
10660Sstevel@tonic-gate 		} while ((c = *namep++) != '\0' && c != '.');
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 	while (n-- > 0) {		/* fill with blanks */
10690Sstevel@tonic-gate 		*fnamep++ = ' ';
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 	/*
10720Sstevel@tonic-gate 	 * remainder is extension
10730Sstevel@tonic-gate 	 */
10740Sstevel@tonic-gate 	n = PCFEXTSIZE;
10750Sstevel@tonic-gate 	if (c == '.') {
10760Sstevel@tonic-gate 		while ((c = *namep++) != '\0' && n--) {
10770Sstevel@tonic-gate 			c = toupper(c);
10780Sstevel@tonic-gate 			if (!pc_validchar(c))
10790Sstevel@tonic-gate 				return (EINVAL);
10800Sstevel@tonic-gate 			*fextp++ = c;
10810Sstevel@tonic-gate 		}
10820Sstevel@tonic-gate 		if (enable_long_filenames && (c != '\0')) {
10830Sstevel@tonic-gate 			/* not short */
10840Sstevel@tonic-gate 			return (EINVAL);
10850Sstevel@tonic-gate 		}
10860Sstevel@tonic-gate 	}
10870Sstevel@tonic-gate 	while (n-- > 0) {		/* fill with blanks */
10880Sstevel@tonic-gate 		*fextp++ = ' ';
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 	return (0);
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate /*
10940Sstevel@tonic-gate  * Match a long filename entry with 'namep'. Also return failure
10950Sstevel@tonic-gate  * if the long filename isn't valid.
10960Sstevel@tonic-gate  */
10970Sstevel@tonic-gate int
10980Sstevel@tonic-gate pc_match_long_fn(struct pcnode *pcp, char *namep, struct pcdir **epp,
10991268Sbatschul     struct pcslot *slotp, offset_t *offset)
11000Sstevel@tonic-gate {
11010Sstevel@tonic-gate 	struct pcdir *ep = (struct pcdir *)*epp;
11020Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
11030Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
11040Sstevel@tonic-gate 	int	error = 0;
11050Sstevel@tonic-gate 	char	lfn[PCMAXNAMLEN+1];
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	error = pc_extract_long_fn(pcp, lfn, epp, offset, &slotp->sl_bp);
11080Sstevel@tonic-gate 	if (error) {
11090Sstevel@tonic-gate 		if (error == EINVAL) {
11100Sstevel@tonic-gate 			return (ENOENT);
11110Sstevel@tonic-gate 		} else
11120Sstevel@tonic-gate 			return (error);
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate 	ep = *epp;
11150Sstevel@tonic-gate 	if (strcasecmp(lfn, namep) == 0) {
11160Sstevel@tonic-gate 		/* match */
11170Sstevel@tonic-gate 		slotp->sl_flags = 0;
11180Sstevel@tonic-gate 		slotp->sl_blkno = pc_daddrdb(fsp, slotp->sl_bp->b_blkno);
11190Sstevel@tonic-gate 		slotp->sl_offset = pc_blkoff(fsp, *offset);
11200Sstevel@tonic-gate 		slotp->sl_ep = ep;
11210Sstevel@tonic-gate 		return (0);
11220Sstevel@tonic-gate 	}
11230Sstevel@tonic-gate 	*offset += sizeof (struct pcdir);
11240Sstevel@tonic-gate 	ep++;
11250Sstevel@tonic-gate 	*epp = ep;
11260Sstevel@tonic-gate 	return (ENOENT);
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate /*
11300Sstevel@tonic-gate  * Match a short filename entry with namep.
11310Sstevel@tonic-gate  */
11320Sstevel@tonic-gate int
11330Sstevel@tonic-gate pc_match_short_fn(struct pcnode *pcp, char *namep, struct pcdir **epp,
11341268Sbatschul     struct pcslot *slotp, offset_t *offset)
11350Sstevel@tonic-gate {
11360Sstevel@tonic-gate 	char fname[PCFNAMESIZE];
11370Sstevel@tonic-gate 	char fext[PCFEXTSIZE];
11380Sstevel@tonic-gate 	struct pcdir *ep = *epp;
11390Sstevel@tonic-gate 	int	error;
11400Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
11410Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
11420Sstevel@tonic-gate 	int boff = pc_blkoff(fsp, *offset);
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	if (PCA_IS_HIDDEN(fsp, ep->pcd_attr)) {
11450Sstevel@tonic-gate 		*offset += sizeof (struct pcdir);
11460Sstevel@tonic-gate 		ep++;
11470Sstevel@tonic-gate 		*epp = ep;
11480Sstevel@tonic-gate 		return (ENOENT);
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	error = pc_parsename(namep, fname, fext);
11520Sstevel@tonic-gate 	if (error) {
11530Sstevel@tonic-gate 		*offset += sizeof (struct pcdir);
11540Sstevel@tonic-gate 		ep++;
11550Sstevel@tonic-gate 		*epp = ep;
11560Sstevel@tonic-gate 		return (error);
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	if ((bcmp(fname, ep->pcd_filename, PCFNAMESIZE) == 0) &&
11600Sstevel@tonic-gate 	    (bcmp(fext, ep->pcd_ext, PCFEXTSIZE) == 0)) {
11610Sstevel@tonic-gate 		/*
11620Sstevel@tonic-gate 		 * found the file
11630Sstevel@tonic-gate 		 */
11640Sstevel@tonic-gate 		if (fname[0] == '.') {
11650Sstevel@tonic-gate 			if (fname[1] == '.')
11660Sstevel@tonic-gate 				slotp->sl_flags = SL_DOTDOT;
11670Sstevel@tonic-gate 			else
11680Sstevel@tonic-gate 				slotp->sl_flags = SL_DOT;
11690Sstevel@tonic-gate 		} else {
11700Sstevel@tonic-gate 			slotp->sl_flags = 0;
11710Sstevel@tonic-gate 		}
11720Sstevel@tonic-gate 		slotp->sl_blkno =
11730Sstevel@tonic-gate 		    pc_daddrdb(fsp, slotp->sl_bp->b_blkno);
11740Sstevel@tonic-gate 		slotp->sl_offset = boff;
11750Sstevel@tonic-gate 		slotp->sl_ep = ep;
11760Sstevel@tonic-gate 		return (0);
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 	*offset += sizeof (struct pcdir);
11790Sstevel@tonic-gate 	ep++;
11800Sstevel@tonic-gate 	*epp = ep;
11810Sstevel@tonic-gate 	return (ENOENT);
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate /*
11850Sstevel@tonic-gate  * Remove a long filename entry starting at lfn_offset. It must be
11860Sstevel@tonic-gate  * a valid entry or we wouldn't have gotten here. Also remove the
11870Sstevel@tonic-gate  * short filename entry.
11880Sstevel@tonic-gate  */
11890Sstevel@tonic-gate static int
11900Sstevel@tonic-gate pc_remove_long_fn(struct pcnode *pcp, offset_t lfn_offset)
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
11930Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
11940Sstevel@tonic-gate 	int boff;
11950Sstevel@tonic-gate 	struct buf *bp = NULL;
11960Sstevel@tonic-gate 	struct pcdir *ep = NULL;
11970Sstevel@tonic-gate 	int	error = 0;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	/*
12000Sstevel@tonic-gate 	 * if we're in here, we know that the lfn is in the proper format
12010Sstevel@tonic-gate 	 * of <series-of-lfn-entries> followed by <sfn-entry>
12020Sstevel@tonic-gate 	 */
12030Sstevel@tonic-gate 	for (;;) {
12040Sstevel@tonic-gate 		boff = pc_blkoff(fsp, lfn_offset);
12050Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
12060Sstevel@tonic-gate 			if (bp != NULL) {
12070Sstevel@tonic-gate 				bwrite2(bp);
12080Sstevel@tonic-gate 				error = geterror(bp);
12090Sstevel@tonic-gate 				brelse(bp);
12100Sstevel@tonic-gate 				if (error)
12110Sstevel@tonic-gate 					return (error);
12120Sstevel@tonic-gate 				bp = NULL;
12130Sstevel@tonic-gate 			}
12140Sstevel@tonic-gate 			error = pc_blkatoff(pcp, lfn_offset, &bp, &ep);
12150Sstevel@tonic-gate 			if (error)
12160Sstevel@tonic-gate 				return (error);
12170Sstevel@tonic-gate 		}
12180Sstevel@tonic-gate 		if (!PCDL_IS_LFN(ep)) {
12190Sstevel@tonic-gate 			/* done */
12200Sstevel@tonic-gate 			break;
12210Sstevel@tonic-gate 		}
12220Sstevel@tonic-gate 		/* zap it */
12230Sstevel@tonic-gate 		ep->pcd_filename[0] = PCD_ERASED;
12240Sstevel@tonic-gate 		ep->pcd_attr = 0;
12250Sstevel@tonic-gate 		lfn_offset += sizeof (struct pcdir);
12260Sstevel@tonic-gate 		ep++;
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 	/* now we're on the short entry */
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	ep->pcd_filename[0] = PCD_ERASED;
12310Sstevel@tonic-gate 	ep->pcd_attr = 0;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if (bp != NULL) {
12340Sstevel@tonic-gate 		bwrite2(bp);
12350Sstevel@tonic-gate 		error = geterror(bp);
12360Sstevel@tonic-gate 		brelse(bp);
12370Sstevel@tonic-gate 		if (error)
12380Sstevel@tonic-gate 			return (error);
12390Sstevel@tonic-gate 	}
12400Sstevel@tonic-gate 	return (0);
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate /*
12440Sstevel@tonic-gate  * Find (and allocate) space in the directory denoted by
12450Sstevel@tonic-gate  * 'pcp'. for 'ndirentries' pcdir structures.
12460Sstevel@tonic-gate  * Return the offset at which to start, or -1 for failure.
12470Sstevel@tonic-gate  */
12480Sstevel@tonic-gate static offset_t
12490Sstevel@tonic-gate pc_find_free_space(struct pcnode *pcp, int ndirentries)
12500Sstevel@tonic-gate {
12510Sstevel@tonic-gate 	offset_t offset = 0;
12520Sstevel@tonic-gate 	offset_t spaceneeded = ndirentries * sizeof (struct pcdir);
12530Sstevel@tonic-gate 	offset_t spaceoffset;
12540Sstevel@tonic-gate 	offset_t spaceavail = 0;
12550Sstevel@tonic-gate 	int boff;
12560Sstevel@tonic-gate 	struct buf *bp = NULL;
12570Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
12580Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
12590Sstevel@tonic-gate 	struct pcdir *ep;
12600Sstevel@tonic-gate 	int	error;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	spaceoffset = offset;
12630Sstevel@tonic-gate 	while (spaceneeded > spaceavail) {
12640Sstevel@tonic-gate 		/*
12650Sstevel@tonic-gate 		 * If offset is on a block boundary,
12660Sstevel@tonic-gate 		 * read in the next directory block.
12670Sstevel@tonic-gate 		 * Release previous if it exists.
12680Sstevel@tonic-gate 		 */
12690Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
12700Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
12710Sstevel@tonic-gate 			if (bp != NULL) {
12720Sstevel@tonic-gate 				brelse(bp);
12730Sstevel@tonic-gate 				bp = NULL;
12740Sstevel@tonic-gate 			}
12750Sstevel@tonic-gate 			error = pc_blkatoff(pcp, offset, &bp, &ep);
12760Sstevel@tonic-gate 			if (error == ENOENT) {
12770Sstevel@tonic-gate 				daddr_t bn;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 				/* extend directory */
12800Sstevel@tonic-gate 				if (!IS_FAT32(fsp) && (vp->v_flag & VROOT))
12810Sstevel@tonic-gate 					return (-1);
12820Sstevel@tonic-gate 				while (spaceneeded > spaceavail) {
12830Sstevel@tonic-gate 					error = pc_balloc(pcp,
12840Sstevel@tonic-gate 					    pc_lblkno(fsp, offset), 1, &bn);
12850Sstevel@tonic-gate 					if (error)
12860Sstevel@tonic-gate 						return (-1);
12870Sstevel@tonic-gate 					pcp->pc_size += fsp->pcfs_clsize;
12880Sstevel@tonic-gate 					spaceavail += fsp->pcfs_clsize;
12890Sstevel@tonic-gate 					offset += fsp->pcfs_clsize;
12900Sstevel@tonic-gate 				}
12910Sstevel@tonic-gate 				return (spaceoffset);
12920Sstevel@tonic-gate 			}
12930Sstevel@tonic-gate 			if (error)
12940Sstevel@tonic-gate 				return (-1);
12950Sstevel@tonic-gate 		}
12960Sstevel@tonic-gate 		if ((ep->pcd_filename[0] == PCD_UNUSED) ||
12970Sstevel@tonic-gate 		    (ep->pcd_filename[0] == PCD_ERASED)) {
12980Sstevel@tonic-gate 			offset += sizeof (struct pcdir);
12990Sstevel@tonic-gate 			spaceavail += sizeof (struct pcdir);
13000Sstevel@tonic-gate 			ep++;
13010Sstevel@tonic-gate 			continue;
13020Sstevel@tonic-gate 		}
13030Sstevel@tonic-gate 		offset += sizeof (struct pcdir);
13040Sstevel@tonic-gate 		spaceavail = 0;
13050Sstevel@tonic-gate 		spaceoffset = offset;
13060Sstevel@tonic-gate 		ep++;
13070Sstevel@tonic-gate 	}
13080Sstevel@tonic-gate 	if (bp != NULL) {
13090Sstevel@tonic-gate 		brelse(bp);
13100Sstevel@tonic-gate 	}
13110Sstevel@tonic-gate 	return (spaceoffset);
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate /*
13150Sstevel@tonic-gate  * Return how many long filename entries are needed.
13160Sstevel@tonic-gate  * A maximum of PCLFNCHUNKSIZE characters per entry, plus one for a
13170Sstevel@tonic-gate  * short filename.
13180Sstevel@tonic-gate  */
13190Sstevel@tonic-gate static int
13200Sstevel@tonic-gate direntries_needed(struct pcnode *dp, char *namep)
13210Sstevel@tonic-gate {
13220Sstevel@tonic-gate 	struct pcdir ep;
13234723Sksn 	uint16_t *w2_str;
13244723Sksn 	size_t  u8l, u16l;
13254723Sksn 	int ret;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	if (enable_long_filenames == 0) {
13280Sstevel@tonic-gate 		return (1);
13290Sstevel@tonic-gate 	}
13300Sstevel@tonic-gate 	if (pc_is_short_file_name(namep, 0)) {
13310Sstevel@tonic-gate 		(void) pc_parsename(namep, ep.pcd_filename, ep.pcd_ext);
13320Sstevel@tonic-gate 		if (!shortname_exists(dp, ep.pcd_filename, ep.pcd_ext)) {
13330Sstevel@tonic-gate 			return (1);
13340Sstevel@tonic-gate 		}
13350Sstevel@tonic-gate 	}
13364723Sksn 	if (pc_valid_long_fn(namep, 1)) {
13374723Sksn 		/*
13384723Sksn 		 * convert to UTF-16 or UNICODE for calculating the entries
13394723Sksn 		 * needed. Conversion will consume at the most 512 bytes
13404723Sksn 		 */
13414723Sksn 		u16l = PCMAXNAMLEN + 1;
13424723Sksn 		w2_str = (uint16_t *)kmem_zalloc(PCMAXNAM_UTF16, KM_SLEEP);
13434723Sksn 		u8l = strlen(namep);
13444723Sksn 		ret = uconv_u8tou16((const uchar_t *)namep, &u8l,
13454723Sksn 		    w2_str, &u16l, UCONV_OUT_LITTLE_ENDIAN);
13464723Sksn 		kmem_free((caddr_t)w2_str, PCMAXNAM_UTF16);
13474723Sksn 		if (ret == 0) {
13484723Sksn 			ret = 1 + u16l / PCLFNCHUNKSIZE;
13494723Sksn 			if (u16l % PCLFNCHUNKSIZE != 0)
13504723Sksn 				ret++;
13514723Sksn 			return (ret);
13524723Sksn 		}
13530Sstevel@tonic-gate 	}
13540Sstevel@tonic-gate 	return (-1);
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate /*
13580Sstevel@tonic-gate  * Allocate and return an array of pcdir structures for the passed-in
13590Sstevel@tonic-gate  * name. ndirentries tells how many are required (including the short
13600Sstevel@tonic-gate  * filename entry). Just allocate and fill them in properly here so they
13610Sstevel@tonic-gate  * can be written out.
13620Sstevel@tonic-gate  */
13630Sstevel@tonic-gate static struct pcdir *
13640Sstevel@tonic-gate pc_name_to_pcdir(struct pcnode *dp, char *namep, int ndirentries, int *errret)
13650Sstevel@tonic-gate {
13660Sstevel@tonic-gate 	struct pcdir *bpcdir;
13670Sstevel@tonic-gate 	struct pcdir *ep;
13680Sstevel@tonic-gate 	struct pcdir_lfn *lep;
13690Sstevel@tonic-gate 	int	i;
13700Sstevel@tonic-gate 	uchar_t	cksum;
13710Sstevel@tonic-gate 	int	nchars;
13720Sstevel@tonic-gate 	int	error = 0;
13730Sstevel@tonic-gate 	char	*nameend;
13744723Sksn 	uint16_t *w2_str;
13754723Sksn 	size_t  u8l, u16l;
13764723Sksn 	int ret;
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	bpcdir = kmem_zalloc(ndirentries * sizeof (struct pcdir), KM_SLEEP);
13790Sstevel@tonic-gate 	ep = &bpcdir[ndirentries - 1];
13800Sstevel@tonic-gate 	if (ndirentries == 1) {
13810Sstevel@tonic-gate 		(void) pc_parsename(namep, ep->pcd_filename, ep->pcd_ext);
13820Sstevel@tonic-gate 		return (bpcdir);
13830Sstevel@tonic-gate 	}
13844723Sksn 
13854723Sksn 	/* Here we need to convert to UTF-16 or UNICODE for writing */
13864723Sksn 
13874723Sksn 	u16l = PCMAXNAMLEN + 1;
13884723Sksn 	w2_str = (uint16_t *)kmem_zalloc(PCMAXNAM_UTF16, KM_SLEEP);
13894723Sksn 	u8l = strlen(namep);
13904723Sksn 	ret = uconv_u8tou16((const uchar_t *)namep, &u8l, w2_str, &u16l,
13914723Sksn 	    UCONV_OUT_LITTLE_ENDIAN);
13924723Sksn 	if (ret != 0) {
13934723Sksn 		kmem_free((caddr_t)w2_str, PCMAXNAM_UTF16);
13944723Sksn 		*errret = ret;
13954723Sksn 		return (NULL);
13960Sstevel@tonic-gate 	}
13974723Sksn 	nameend = (char *)(w2_str + u16l);
13984723Sksn 	u16l %= PCLFNCHUNKSIZE;
13994723Sksn 	if (u16l != 0) {
14004723Sksn 		nchars = u16l + 1;
14014723Sksn 		nameend += 2;
14024723Sksn 	} else {
14034723Sksn 		nchars = PCLFNCHUNKSIZE;
14044723Sksn 	}
14054723Sksn 	nchars *= sizeof (uint16_t);
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	/* short file name */
14080Sstevel@tonic-gate 	error = generate_short_name(dp, namep, ep);
14090Sstevel@tonic-gate 	if (error) {
14100Sstevel@tonic-gate 		kmem_free(bpcdir, ndirentries * sizeof (struct pcdir));
14110Sstevel@tonic-gate 		*errret = error;
14120Sstevel@tonic-gate 		return (NULL);
14130Sstevel@tonic-gate 	}
14140Sstevel@tonic-gate 	cksum = pc_checksum_long_fn(ep->pcd_filename, ep->pcd_ext);
14150Sstevel@tonic-gate 	for (i = 0; i < (ndirentries - 1); i++) {
14160Sstevel@tonic-gate 		/* long file name */
14170Sstevel@tonic-gate 		nameend -= nchars;
14180Sstevel@tonic-gate 		lep = (struct pcdir_lfn *)&bpcdir[i];
14190Sstevel@tonic-gate 		set_long_fn_chunk(lep, nameend, nchars);
14200Sstevel@tonic-gate 		lep->pcdl_attr = PCDL_LFN_BITS;
14210Sstevel@tonic-gate 		lep->pcdl_checksum = cksum;
14220Sstevel@tonic-gate 		lep->pcdl_ordinal = (uchar_t)(ndirentries - i - 1);
14234723Sksn 		nchars = PCLFNCHUNKSIZE * sizeof (uint16_t);
14240Sstevel@tonic-gate 	}
14254723Sksn 	kmem_free((caddr_t)w2_str, PCMAXNAM_UTF16);
14260Sstevel@tonic-gate 	lep = (struct pcdir_lfn *)&bpcdir[0];
14270Sstevel@tonic-gate 	lep->pcdl_ordinal |= 0x40;
14280Sstevel@tonic-gate 	return (bpcdir);
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate static int
14320Sstevel@tonic-gate generate_short_name(struct pcnode *dp, char *namep, struct pcdir *inep)
14330Sstevel@tonic-gate {
14340Sstevel@tonic-gate 	int	rev;
14350Sstevel@tonic-gate 	int	nchars;
14360Sstevel@tonic-gate 	int	i, j;
14370Sstevel@tonic-gate 	char	*dot = NULL;
14380Sstevel@tonic-gate 	char	fname[PCFNAMESIZE+1];
14390Sstevel@tonic-gate 	char	fext[PCFEXTSIZE+1];
14400Sstevel@tonic-gate 	char	scratch[8];
14410Sstevel@tonic-gate 	int	error = 0;
14421268Sbatschul 	struct	pcslot slot;
14430Sstevel@tonic-gate 	char	shortname[20];
14440Sstevel@tonic-gate 	int	force_tilde = 0;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	/*
14470Sstevel@tonic-gate 	 * generate a unique short file name based on the long input name.
14480Sstevel@tonic-gate 	 *
14490Sstevel@tonic-gate 	 * Say, for "This is a very long filename.txt" generate
14500Sstevel@tonic-gate 	 * "THISIS~1.TXT", or "THISIS~2.TXT" if that's already there.
14510Sstevel@tonic-gate 	 * Skip invalid short name characters in the long name, plus
14520Sstevel@tonic-gate 	 * a couple NT skips (space and reverse backslash).
14530Sstevel@tonic-gate 	 *
14540Sstevel@tonic-gate 	 * Unfortunately, since this name would be hidden by the normal
14550Sstevel@tonic-gate 	 * lookup routine, we need to look for it ourselves. But luckily
14560Sstevel@tonic-gate 	 * we don't need to look at the lfn entries themselves.
14570Sstevel@tonic-gate 	 */
14580Sstevel@tonic-gate 	force_tilde = !pc_is_short_file_name(namep, 1);
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	/*
14610Sstevel@tonic-gate 	 * Strip off leading invalid characters.
14620Sstevel@tonic-gate 	 * We need this because names like '.login' are now ok, but the
14630Sstevel@tonic-gate 	 * short name needs to be something like LOGIN~1.
14640Sstevel@tonic-gate 	 */
14650Sstevel@tonic-gate 	for (; *namep != '\0'; namep++) {
14660Sstevel@tonic-gate 		if (*namep == ' ')
14670Sstevel@tonic-gate 			continue;
14680Sstevel@tonic-gate 		if (!pc_validchar(*namep) && !pc_validchar(toupper(*namep)))
14690Sstevel@tonic-gate 			continue;
14700Sstevel@tonic-gate 		break;
14710Sstevel@tonic-gate 	}
14720Sstevel@tonic-gate 	dot = strrchr(namep, '.');
14730Sstevel@tonic-gate 	if (dot != NULL) {
14740Sstevel@tonic-gate 		dot++;
14750Sstevel@tonic-gate 		for (j = 0, i = 0; j < PCFEXTSIZE; i++) {
14760Sstevel@tonic-gate 			if (dot[i] == '\0')
14770Sstevel@tonic-gate 				break;
14780Sstevel@tonic-gate 			/* skip valid, but not generally good characters */
14790Sstevel@tonic-gate 			if (dot[i] == ' ' || dot[i] == '\\')
14800Sstevel@tonic-gate 				continue;
14810Sstevel@tonic-gate 			if (pc_validchar(dot[i]))
14820Sstevel@tonic-gate 				fext[j++] = dot[i];
14830Sstevel@tonic-gate 			else if (pc_validchar(toupper(dot[i])))
14840Sstevel@tonic-gate 				fext[j++] = toupper(dot[i]);
14850Sstevel@tonic-gate 		}
14860Sstevel@tonic-gate 		for (i = j; i < PCFEXTSIZE; i++)
14870Sstevel@tonic-gate 			fext[i] = ' ';
14880Sstevel@tonic-gate 		dot--;
14890Sstevel@tonic-gate 	} else {
14900Sstevel@tonic-gate 		for (i = 0; i < PCFEXTSIZE; i++) {
14910Sstevel@tonic-gate 			fext[i] = ' ';
14920Sstevel@tonic-gate 		}
14930Sstevel@tonic-gate 	}
14940Sstevel@tonic-gate 	/*
14950Sstevel@tonic-gate 	 * We know we're a long name, not a short name (or we wouldn't
14960Sstevel@tonic-gate 	 * be here at all. But if uppercasing ourselves would be a short
14970Sstevel@tonic-gate 	 * name, then we can possibly avoid the ~N format.
14980Sstevel@tonic-gate 	 */
14990Sstevel@tonic-gate 	if (!force_tilde)
15000Sstevel@tonic-gate 		rev = 0;
15010Sstevel@tonic-gate 	else
15020Sstevel@tonic-gate 		rev = 1;
15030Sstevel@tonic-gate 	for (;;) {
15040Sstevel@tonic-gate 		bzero(fname, sizeof (fname));
15050Sstevel@tonic-gate 		nchars = PCFNAMESIZE;
15060Sstevel@tonic-gate 		if (rev) {
15070Sstevel@tonic-gate 			nchars--; /* ~ */
15080Sstevel@tonic-gate 			i = rev;
15090Sstevel@tonic-gate 			do {
15100Sstevel@tonic-gate 				nchars--;
15110Sstevel@tonic-gate 				i /= 10;
15120Sstevel@tonic-gate 			} while (i);
15130Sstevel@tonic-gate 			if (nchars <= 0) {
15140Sstevel@tonic-gate 				return (ENOSPC);
15150Sstevel@tonic-gate 			}
15160Sstevel@tonic-gate 		}
15170Sstevel@tonic-gate 		for (j = 0, i = 0; j < nchars; i++) {
15180Sstevel@tonic-gate 			if ((&namep[i] == dot) || (namep[i] == '\0'))
15190Sstevel@tonic-gate 				break;
15200Sstevel@tonic-gate 			/* skip valid, but not generally good characters */
15210Sstevel@tonic-gate 			if (namep[i] == ' ' || namep[i] == '\\')
15220Sstevel@tonic-gate 				continue;
15230Sstevel@tonic-gate 			if (pc_validchar(namep[i]))
15240Sstevel@tonic-gate 				fname[j++] = namep[i];
15250Sstevel@tonic-gate 			else if (pc_validchar(toupper(namep[i])))
15260Sstevel@tonic-gate 				fname[j++] = toupper(namep[i]);
15270Sstevel@tonic-gate 		}
15280Sstevel@tonic-gate 		if (rev) {
15290Sstevel@tonic-gate 			(void) sprintf(scratch, "~%d", rev);
15300Sstevel@tonic-gate 			(void) strcat(fname, scratch);
15310Sstevel@tonic-gate 		}
15320Sstevel@tonic-gate 		for (i = strlen(fname); i < PCFNAMESIZE; i++)
15330Sstevel@tonic-gate 			fname[i] = ' ';
15340Sstevel@tonic-gate 		/* now see if it exists */
15350Sstevel@tonic-gate 		(void) pc_fname_ext_to_name(shortname, fname, fext, 0);
15360Sstevel@tonic-gate 		error = pc_findentry(dp, shortname, &slot, NULL);
15370Sstevel@tonic-gate 		if (error == 0) {
15380Sstevel@tonic-gate 			/* found it */
15390Sstevel@tonic-gate 			brelse(slot.sl_bp);
15400Sstevel@tonic-gate 			rev++;
15410Sstevel@tonic-gate 			continue;
15420Sstevel@tonic-gate 		}
15430Sstevel@tonic-gate 		if (!shortname_exists(dp, fname, fext))
15440Sstevel@tonic-gate 			break;
15450Sstevel@tonic-gate 		rev++;
15460Sstevel@tonic-gate 	}
15470Sstevel@tonic-gate 	(void) strncpy(inep->pcd_filename, fname, PCFNAMESIZE);
15480Sstevel@tonic-gate 	(void) strncpy(inep->pcd_ext, fext, PCFEXTSIZE);
15490Sstevel@tonic-gate 	return (0);
15500Sstevel@tonic-gate }
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate /*
15530Sstevel@tonic-gate  * Returns 1 if the passed-in filename is a short name, 0 if not.
15540Sstevel@tonic-gate  */
15550Sstevel@tonic-gate static int
15560Sstevel@tonic-gate pc_is_short_file_name(char *namep, int foldcase)
15570Sstevel@tonic-gate {
15580Sstevel@tonic-gate 	int	i;
15590Sstevel@tonic-gate 	char	c;
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	for (i = 0; i < PCFNAMESIZE; i++, namep++) {
15620Sstevel@tonic-gate 		if (*namep == '\0')
15630Sstevel@tonic-gate 			return (1);
15640Sstevel@tonic-gate 		if (*namep == '.')
15650Sstevel@tonic-gate 			break;
15660Sstevel@tonic-gate 		if (foldcase)
15670Sstevel@tonic-gate 			c = toupper(*namep);
15680Sstevel@tonic-gate 		else
15690Sstevel@tonic-gate 			c = *namep;
15700Sstevel@tonic-gate 		if (!pc_validchar(c))
15710Sstevel@tonic-gate 			return (0);
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 	if (*namep == '\0')
15740Sstevel@tonic-gate 		return (1);
15750Sstevel@tonic-gate 	if (*namep != '.')
15760Sstevel@tonic-gate 		return (0);
15770Sstevel@tonic-gate 	namep++;
15780Sstevel@tonic-gate 	for (i = 0; i < PCFEXTSIZE; i++, namep++) {
15790Sstevel@tonic-gate 		if (*namep == '\0')
15800Sstevel@tonic-gate 			return (1);
15810Sstevel@tonic-gate 		if (foldcase)
15820Sstevel@tonic-gate 			c = toupper(*namep);
15830Sstevel@tonic-gate 		else
15840Sstevel@tonic-gate 			c = *namep;
15850Sstevel@tonic-gate 		if (!pc_validchar(c))
15860Sstevel@tonic-gate 			return (0);
15870Sstevel@tonic-gate 	}
15880Sstevel@tonic-gate 	/* we should be done. If not... */
15890Sstevel@tonic-gate 	if (*namep == '\0')
15900Sstevel@tonic-gate 		return (1);
15910Sstevel@tonic-gate 	return (0);
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate /*
15960Sstevel@tonic-gate  * We call this when we want to see if a short filename already exists
15970Sstevel@tonic-gate  * in the filesystem as part of a long filename. When creating a short
15980Sstevel@tonic-gate  * name (FILENAME.TXT from the user, or when generating one for a long
15990Sstevel@tonic-gate  * filename), we cannot allow one that is part of a long filename.
16000Sstevel@tonic-gate  * pc_findentry will find all the names that are visible (long or short),
16010Sstevel@tonic-gate  * but will not crack any long filename entries.
16020Sstevel@tonic-gate  */
16030Sstevel@tonic-gate static int
16040Sstevel@tonic-gate shortname_exists(struct pcnode *dp, char *fname, char *fext)
16050Sstevel@tonic-gate {
16060Sstevel@tonic-gate 	struct buf *bp = NULL;
16070Sstevel@tonic-gate 	int	offset = 0;
16080Sstevel@tonic-gate 	int	match = 0;
16090Sstevel@tonic-gate 	struct pcdir *ep;
16100Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
16110Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
16120Sstevel@tonic-gate 	int	boff;
16130Sstevel@tonic-gate 	int	error = 0;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	for (;;) {
16160Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
16170Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
16180Sstevel@tonic-gate 			if (bp != NULL) {
16190Sstevel@tonic-gate 				brelse(bp);
16200Sstevel@tonic-gate 				bp = NULL;
16210Sstevel@tonic-gate 			}
16220Sstevel@tonic-gate 			error = pc_blkatoff(dp, offset, &bp, &ep);
16230Sstevel@tonic-gate 			if (error == ENOENT)
16240Sstevel@tonic-gate 				break;
16250Sstevel@tonic-gate 			if (error) {
16260Sstevel@tonic-gate 				return (1);
16270Sstevel@tonic-gate 			}
16280Sstevel@tonic-gate 		}
16290Sstevel@tonic-gate 		if (PCDL_IS_LFN(ep) ||
16300Sstevel@tonic-gate 		    (ep->pcd_filename[0] == PCD_ERASED)) {
16310Sstevel@tonic-gate 			offset += sizeof (struct pcdir);
16320Sstevel@tonic-gate 			ep++;
16330Sstevel@tonic-gate 			continue;
16340Sstevel@tonic-gate 		}
16350Sstevel@tonic-gate 		if (ep->pcd_filename[0] == PCD_UNUSED)
16360Sstevel@tonic-gate 			break;
16370Sstevel@tonic-gate 		/*
16380Sstevel@tonic-gate 		 * in use, and a short file name (either standalone
16390Sstevel@tonic-gate 		 * or associated with a long name
16400Sstevel@tonic-gate 		 */
16410Sstevel@tonic-gate 		if ((bcmp(fname, ep->pcd_filename, PCFNAMESIZE) == 0) &&
16420Sstevel@tonic-gate 		    (bcmp(fext, ep->pcd_ext, PCFEXTSIZE) == 0)) {
16430Sstevel@tonic-gate 			match = 1;
16440Sstevel@tonic-gate 			break;
16450Sstevel@tonic-gate 		}
16460Sstevel@tonic-gate 		offset += sizeof (struct pcdir);
16470Sstevel@tonic-gate 		ep++;
16480Sstevel@tonic-gate 	}
16490Sstevel@tonic-gate 	if (bp) {
16500Sstevel@tonic-gate 		brelse(bp);
16510Sstevel@tonic-gate 		bp = NULL;
16520Sstevel@tonic-gate 	}
16530Sstevel@tonic-gate 	return (match);
16540Sstevel@tonic-gate }
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate pc_cluster32_t
16570Sstevel@tonic-gate pc_getstartcluster(struct pcfs *fsp, struct pcdir *ep)
16580Sstevel@tonic-gate {
16590Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
16600Sstevel@tonic-gate 		pc_cluster32_t cn;
16610Sstevel@tonic-gate 		pc_cluster16_t hi16;
16620Sstevel@tonic-gate 		pc_cluster16_t lo16;
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 		hi16 = ltohs(ep->un.pcd_scluster_hi);
16650Sstevel@tonic-gate 		lo16 = ltohs(ep->pcd_scluster_lo);
16660Sstevel@tonic-gate 		cn = (hi16 << 16) | lo16;
16670Sstevel@tonic-gate 		return (cn);
16680Sstevel@tonic-gate 	} else {
16690Sstevel@tonic-gate 		return (ltohs(ep->pcd_scluster_lo));
16700Sstevel@tonic-gate 	}
16710Sstevel@tonic-gate }
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate void
16740Sstevel@tonic-gate pc_setstartcluster(struct pcfs *fsp, struct pcdir *ep, pc_cluster32_t cln)
16750Sstevel@tonic-gate {
16760Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
16770Sstevel@tonic-gate 		pc_cluster16_t hi16;
16780Sstevel@tonic-gate 		pc_cluster16_t lo16;
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 		hi16 = (cln >> 16) & 0xFFFF;
16810Sstevel@tonic-gate 		lo16 = cln & 0xFFFF;
16820Sstevel@tonic-gate 		ep->un.pcd_scluster_hi = htols(hi16);
16830Sstevel@tonic-gate 		ep->pcd_scluster_lo = htols(lo16);
16840Sstevel@tonic-gate 	} else {
16850Sstevel@tonic-gate 		pc_cluster16_t cln16;
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 		cln16 = (pc_cluster16_t)cln;
16880Sstevel@tonic-gate 		ep->pcd_scluster_lo = htols(cln16);
16890Sstevel@tonic-gate 	}
16900Sstevel@tonic-gate }
1691