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