xref: /onnv-gate/usr/src/uts/common/fs/pcfs/pc_dir.c (revision 4723:a67d7aa1f485)
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 /*
22*4723Sksn  * 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>
38*4723Sksn #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,
178*4723Sksn 			    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,
3900Sstevel@tonic-gate 	enum vtype type)
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),
414*4723Sksn 		    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) {
4700Sstevel@tonic-gate 			vnevent_rmdir(PCTOV(pcp));
4710Sstevel@tonic-gate 		} else {
4720Sstevel@tonic-gate 			vnevent_remove(PCTOV(pcp));
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 */
5590Sstevel@tonic-gate 	char *tnm)			/* target file name */
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	struct pcnode *pcp;	/* pcnode we are trying to rename */
5620Sstevel@tonic-gate 	struct pcnode *tpcp;	/* pcnode that's in our way */
5631268Sbatschul 	struct pcslot slot;
5640Sstevel@tonic-gate 	int error;
5650Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
5660Sstevel@tonic-gate 	struct vnode *svp = NULL;
5670Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
5680Sstevel@tonic-gate 	int filecasechange = 0;
5690Sstevel@tonic-gate 	int oldisdir = 0;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	PC_DPRINTF3(4, "pc_rename(0x%p, %s, %s)\n", (void *)dp, snm, tnm);
5720Sstevel@tonic-gate 	/*
5730Sstevel@tonic-gate 	 * Leading spaces are not allowed in DOS.
5740Sstevel@tonic-gate 	 */
5750Sstevel@tonic-gate 	if (*tnm == ' ')
5760Sstevel@tonic-gate 		return (EINVAL);
5770Sstevel@tonic-gate 	/*
5780Sstevel@tonic-gate 	 * No dot or dotdot.
5790Sstevel@tonic-gate 	 */
5800Sstevel@tonic-gate 	if (PC_NAME_IS_DOT(snm) || PC_NAME_IS_DOTDOT(snm) ||
5810Sstevel@tonic-gate 	    PC_NAME_IS_DOT(tnm) || PC_NAME_IS_DOTDOT(tnm))
5820Sstevel@tonic-gate 		return (EINVAL);
5830Sstevel@tonic-gate 	/*
5840Sstevel@tonic-gate 	 * Get the source node.  We'll jump back to here if trying to
5850Sstevel@tonic-gate 	 * move on top of an existing file, after deleting that file.
5860Sstevel@tonic-gate 	 */
5870Sstevel@tonic-gate top:
5880Sstevel@tonic-gate 	error = pc_findentry(dp, snm, &slot, NULL);
5890Sstevel@tonic-gate 	if (error) {
5900Sstevel@tonic-gate 		return (error);
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate 	pcp = pc_getnode(VFSTOPCFS(vp->v_vfsp),
5930Sstevel@tonic-gate 	    slot.sl_blkno, slot.sl_offset, slot.sl_ep);
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	brelse(slot.sl_bp);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if (pcp)
5980Sstevel@tonic-gate 		svp = PCTOV(pcp);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * is the rename invalid, i.e. rename("a", "a/a")
6020Sstevel@tonic-gate 	 */
6030Sstevel@tonic-gate 	if (pcp == tdp) {
6040Sstevel@tonic-gate 		if (svp)
6050Sstevel@tonic-gate 			VN_RELE(svp);
6060Sstevel@tonic-gate 		return (EINVAL);
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	/*
6100Sstevel@tonic-gate 	 * Are we just changing the case of an existing name?
6110Sstevel@tonic-gate 	 */
6120Sstevel@tonic-gate 	if ((dp->pc_scluster == tdp->pc_scluster) &&
6130Sstevel@tonic-gate 	    (strcasecmp(snm, tnm) == 0)) {
6140Sstevel@tonic-gate 		filecasechange = 1;
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	oldisdir = pcp->pc_entry.pcd_attr & PCA_DIR;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	/*
6200Sstevel@tonic-gate 	 * see if the target exists
6210Sstevel@tonic-gate 	 */
6220Sstevel@tonic-gate 	error = pc_findentry(tdp, tnm, &slot, NULL);
6230Sstevel@tonic-gate 	if (error == 0 && filecasechange == 0) {
6240Sstevel@tonic-gate 		/*
6250Sstevel@tonic-gate 		 * Target exists.  If it's a file, delete it.  If it's
6260Sstevel@tonic-gate 		 * a directory, bail.
6270Sstevel@tonic-gate 		 */
6280Sstevel@tonic-gate 		int newisdir;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 		tpcp = pc_getnode(VFSTOPCFS(vp->v_vfsp),
6310Sstevel@tonic-gate 		    slot.sl_blkno, slot.sl_offset, slot.sl_ep);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		newisdir = tpcp->pc_entry.pcd_attr & PCA_DIR;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		brelse(slot.sl_bp);
6360Sstevel@tonic-gate 		vnevent_rename_dest(PCTOV(tpcp));
6370Sstevel@tonic-gate 		VN_RELE(PCTOV(tpcp));
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		/*
6400Sstevel@tonic-gate 		 * Error cases (from rename(2)):
6410Sstevel@tonic-gate 		 * old is dir, new is dir: EEXIST
6420Sstevel@tonic-gate 		 * old is dir, new is nondir: ENOTDIR
6430Sstevel@tonic-gate 		 * old is nondir, new is dir: EISDIR
6440Sstevel@tonic-gate 		 */
6450Sstevel@tonic-gate 		if (!newisdir) {
6460Sstevel@tonic-gate 			if (oldisdir) {
6470Sstevel@tonic-gate 				error = ENOTDIR;
6480Sstevel@tonic-gate 			} else {
6490Sstevel@tonic-gate 				/* nondir/nondir, remove target */
6500Sstevel@tonic-gate 				error = pc_dirremove(tdp, tnm,
6510Sstevel@tonic-gate 				    (struct vnode *)NULL, VREG);
6520Sstevel@tonic-gate 				if (error == 0) {
6530Sstevel@tonic-gate 					VN_RELE(PCTOV(pcp));
6540Sstevel@tonic-gate 					goto top;
6550Sstevel@tonic-gate 				}
6560Sstevel@tonic-gate 			}
6570Sstevel@tonic-gate 		} else if (oldisdir) {
6580Sstevel@tonic-gate 			/* dir/dir, remove target */
6590Sstevel@tonic-gate 			error = pc_dirremove(tdp, tnm,
6600Sstevel@tonic-gate 			    (struct vnode *)NULL, VDIR);
6610Sstevel@tonic-gate 			if (error == 0) {
6620Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
6630Sstevel@tonic-gate 				goto top;
6640Sstevel@tonic-gate 			}
6650Sstevel@tonic-gate 			/* Follow rename(2)'s spec... */
6660Sstevel@tonic-gate 			if (error == ENOTEMPTY) {
6670Sstevel@tonic-gate 				error = EEXIST;
6680Sstevel@tonic-gate 			}
6690Sstevel@tonic-gate 		} else {
6700Sstevel@tonic-gate 			/* nondir/dir, bail */
6710Sstevel@tonic-gate 			error = EISDIR;
6720Sstevel@tonic-gate 		}
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	if ((error == 0) || (error == ENOENT)) {
6760Sstevel@tonic-gate 		offset_t lfn_offset = -1;
6770Sstevel@tonic-gate 		int	blkno;
6780Sstevel@tonic-gate 		struct pcdir *direntries;
6790Sstevel@tonic-gate 		struct pcdir *ep;
6800Sstevel@tonic-gate 		int	ndirentries;
6810Sstevel@tonic-gate 		pc_cluster16_t pct_lo;
6820Sstevel@tonic-gate 		pc_cluster16_t pct_hi;
6830Sstevel@tonic-gate 		offset_t offset;
6840Sstevel@tonic-gate 		int	boff;
6850Sstevel@tonic-gate 		struct buf *bp = NULL;
6860Sstevel@tonic-gate 		uchar_t	attr;
6870Sstevel@tonic-gate 		int	size;
6880Sstevel@tonic-gate 		struct pctime mtime;
6890Sstevel@tonic-gate 		struct pctime crtime;
6900Sstevel@tonic-gate 		uchar_t	ntattr;
6910Sstevel@tonic-gate 		ushort_t ladate;
6920Sstevel@tonic-gate 		ushort_t eattr;
6930Sstevel@tonic-gate 		uchar_t	crtime_msec;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 		/*
6960Sstevel@tonic-gate 		 * Rename the source.
6970Sstevel@tonic-gate 		 */
6980Sstevel@tonic-gate 		/*
6990Sstevel@tonic-gate 		 * Delete the old name, and create a new name.
7000Sstevel@tonic-gate 		 */
7010Sstevel@tonic-gate 		if (filecasechange == 1 && error == 0)
7020Sstevel@tonic-gate 			brelse(slot.sl_bp);
7030Sstevel@tonic-gate 		ndirentries = direntries_needed(tdp, tnm);
7040Sstevel@tonic-gate 		if (ndirentries == -1) {
7050Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7060Sstevel@tonic-gate 			return (EINVAL);
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 		/*
7090Sstevel@tonic-gate 		 * first see if we have enough space to create the new
7100Sstevel@tonic-gate 		 * name before destroying the old one.
7110Sstevel@tonic-gate 		 */
7120Sstevel@tonic-gate 		offset = pc_find_free_space(tdp, ndirentries);
7130Sstevel@tonic-gate 		if (offset == -1) {
7140Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7150Sstevel@tonic-gate 			return (ENOSPC);
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		error = pc_findentry(dp, snm, &slot, &lfn_offset);
7190Sstevel@tonic-gate 		if (error) {
7200Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7210Sstevel@tonic-gate 			return (error);
7220Sstevel@tonic-gate 		}
7230Sstevel@tonic-gate 		pct_lo = slot.sl_ep->pcd_scluster_lo;
7240Sstevel@tonic-gate 		if (IS_FAT32(fsp))
7250Sstevel@tonic-gate 			pct_hi = slot.sl_ep->un.pcd_scluster_hi;
7260Sstevel@tonic-gate 		else
7270Sstevel@tonic-gate 			eattr = slot.sl_ep->un.pcd_eattr;
7280Sstevel@tonic-gate 		size = slot.sl_ep->pcd_size;
7290Sstevel@tonic-gate 		attr = slot.sl_ep->pcd_attr;
7300Sstevel@tonic-gate 		mtime = slot.sl_ep->pcd_mtime;
7310Sstevel@tonic-gate 		crtime = slot.sl_ep->pcd_crtime;
7320Sstevel@tonic-gate 		crtime_msec = slot.sl_ep->pcd_crtime_msec;
7330Sstevel@tonic-gate 		ntattr = slot.sl_ep->pcd_ntattr;
7340Sstevel@tonic-gate 		ladate = slot.sl_ep->pcd_ladate;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 		if (lfn_offset != -1) {
7370Sstevel@tonic-gate 			brelse(slot.sl_bp);
7380Sstevel@tonic-gate 			error = pc_remove_long_fn(dp, lfn_offset);
7390Sstevel@tonic-gate 			if (error) {
7400Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
7410Sstevel@tonic-gate 				pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
7420Sstevel@tonic-gate 				return (error);
7430Sstevel@tonic-gate 			}
7440Sstevel@tonic-gate 		} else {
7450Sstevel@tonic-gate 			slot.sl_ep->pcd_filename[0] =
7460Sstevel@tonic-gate 			    pcp->pc_entry.pcd_filename[0] = PCD_ERASED;
7470Sstevel@tonic-gate 			bwrite2(slot.sl_bp);
7480Sstevel@tonic-gate 			error = geterror(slot.sl_bp);
7490Sstevel@tonic-gate 			brelse(slot.sl_bp);
7500Sstevel@tonic-gate 		}
7510Sstevel@tonic-gate 		if (error) {
7520Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7530Sstevel@tonic-gate 			pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
7540Sstevel@tonic-gate 			return (EIO);
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 		/*
7580Sstevel@tonic-gate 		 * Make an entry from the supplied attributes.
7590Sstevel@tonic-gate 		 */
7600Sstevel@tonic-gate 		direntries = pc_name_to_pcdir(tdp, tnm, ndirentries, &error);
7610Sstevel@tonic-gate 		if (direntries == NULL) {
7620Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7630Sstevel@tonic-gate 			return (error);
7640Sstevel@tonic-gate 		}
7650Sstevel@tonic-gate 		error = pc_makedirentry(tdp, direntries, ndirentries, NULL,
7660Sstevel@tonic-gate 		    offset);
7670Sstevel@tonic-gate 		kmem_free(direntries, ndirentries * sizeof (struct pcdir));
7680Sstevel@tonic-gate 		if (error) {
7690Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7700Sstevel@tonic-gate 			return (error);
7710Sstevel@tonic-gate 		}
7720Sstevel@tonic-gate 		/* advance to short name */
7730Sstevel@tonic-gate 		offset += (ndirentries - 1)  * sizeof (struct pcdir);
7740Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
7750Sstevel@tonic-gate 		error = pc_blkatoff(tdp, offset, &bp, &ep);
7760Sstevel@tonic-gate 		if (error) {
7770Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7780Sstevel@tonic-gate 			return (error);
7790Sstevel@tonic-gate 		}
7800Sstevel@tonic-gate 		blkno = pc_daddrdb(fsp, bp->b_blkno);
7810Sstevel@tonic-gate 		ep->pcd_scluster_lo = pct_lo;
7820Sstevel@tonic-gate 		if (IS_FAT32(fsp))
7830Sstevel@tonic-gate 			ep->un.pcd_scluster_hi = pct_hi;
7840Sstevel@tonic-gate 		else
7850Sstevel@tonic-gate 			ep->un.pcd_eattr = eattr;
7860Sstevel@tonic-gate 		ep->pcd_size = size;
7870Sstevel@tonic-gate 		ep->pcd_attr = attr;
7880Sstevel@tonic-gate 		ep->pcd_mtime = mtime;
7890Sstevel@tonic-gate 		ep->pcd_crtime = crtime;
7900Sstevel@tonic-gate 		ep->pcd_crtime_msec = crtime_msec;
7910Sstevel@tonic-gate 		ep->pcd_ntattr = ntattr;
7920Sstevel@tonic-gate 		ep->pcd_ladate = ladate;
7930Sstevel@tonic-gate 		bwrite2(bp);
7940Sstevel@tonic-gate 		error = geterror(bp);
7950Sstevel@tonic-gate 		pcp->pc_eblkno = blkno;
7960Sstevel@tonic-gate 		pcp->pc_eoffset = boff;
7970Sstevel@tonic-gate 		pcp->pc_entry = *ep;
7980Sstevel@tonic-gate 		pcp->pc_flags |= PC_CHG;
7990Sstevel@tonic-gate 		brelse(bp);
8000Sstevel@tonic-gate 		if (error) {
8010Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
8020Sstevel@tonic-gate 			pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
8030Sstevel@tonic-gate 			return (EIO);
8040Sstevel@tonic-gate 		}
8050Sstevel@tonic-gate 		/* No need to fix ".." if we're renaming within a dir */
8060Sstevel@tonic-gate 		if (oldisdir && dp != tdp) {
8070Sstevel@tonic-gate 			if ((error = pc_dirfixdotdot(pcp, dp, tdp)) != 0) {
8080Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
8090Sstevel@tonic-gate 				return (error);
8100Sstevel@tonic-gate 			}
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 		if ((error = pc_nodeupdate(pcp)) != 0) {
8130Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
8140Sstevel@tonic-gate 			return (error);
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate out:
8180Sstevel@tonic-gate 	vnevent_rename_src(PCTOV(pcp));
8190Sstevel@tonic-gate 	VN_RELE(PCTOV(pcp));
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	return (error);
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate /*
8250Sstevel@tonic-gate  * Fix the ".." entry of the child directory so that it points to the
8260Sstevel@tonic-gate  * new parent directory instead of the old one.
8270Sstevel@tonic-gate  */
8280Sstevel@tonic-gate static int
8290Sstevel@tonic-gate pc_dirfixdotdot(struct pcnode *dp,	/* child directory being moved */
8300Sstevel@tonic-gate 	struct pcnode *opdp,		/* old parent directory */
8310Sstevel@tonic-gate 	struct pcnode *npdp)		/* new parent directory */
8320Sstevel@tonic-gate {
8330Sstevel@tonic-gate 	pc_cluster32_t cn;
8340Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
8350Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
8360Sstevel@tonic-gate 	int error = 0;
8370Sstevel@tonic-gate 	struct buf *bp = NULL;
8380Sstevel@tonic-gate 	struct pcdir *ep = NULL;
8390Sstevel@tonic-gate 	struct pcdir *tep = NULL;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	/*
8420Sstevel@tonic-gate 	 * set the new child's ".." directory entry starting cluster to
8430Sstevel@tonic-gate 	 * point to the new parent's starting cluster
8440Sstevel@tonic-gate 	 */
8450Sstevel@tonic-gate 	ASSERT(opdp != npdp);
8460Sstevel@tonic-gate 	error = pc_blkatoff(dp, (offset_t)0, &bp, &ep);
8470Sstevel@tonic-gate 	if (error) {
8480Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_dirfixdotdot: error in blkatoff\n");
8490Sstevel@tonic-gate 		return (error);
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 	tep = ep;
8520Sstevel@tonic-gate 	ep++;
8530Sstevel@tonic-gate 	if (!PC_SHORTNAME_IS_DOT(tep->pcd_filename) &&
8540Sstevel@tonic-gate 	    !PC_SHORTNAME_IS_DOTDOT(ep->pcd_filename)) {
8550Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_dirfixdotdot: mangled directory entry\n");
8560Sstevel@tonic-gate 		error = ENOTDIR;
8570Sstevel@tonic-gate 		return (error);
8580Sstevel@tonic-gate 	}
8590Sstevel@tonic-gate 	cn = pc_getstartcluster(fsp, &npdp->pc_entry);
8600Sstevel@tonic-gate 	pc_setstartcluster(fsp, ep, cn);
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	bwrite2(bp);
8630Sstevel@tonic-gate 	error = geterror(bp);
8640Sstevel@tonic-gate 	brelse(bp);
8650Sstevel@tonic-gate 	if (error) {
8660Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_dirfixdotdot: error in write\n");
8670Sstevel@tonic-gate 		pc_mark_irrecov(fsp);
8680Sstevel@tonic-gate 		return (EIO);
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 	return (0);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate /*
8750Sstevel@tonic-gate  * Search a directory for an entry.
8760Sstevel@tonic-gate  * The directory should be locked as this routine
8770Sstevel@tonic-gate  * will sleep on I/O while searching.
8780Sstevel@tonic-gate  */
8790Sstevel@tonic-gate static int
8800Sstevel@tonic-gate pc_findentry(
8810Sstevel@tonic-gate 	struct pcnode *dp,		/* parent directory */
8820Sstevel@tonic-gate 	char *namep,			/* name to lookup */
8831268Sbatschul 	struct pcslot *slotp,
8840Sstevel@tonic-gate 	offset_t *lfn_offset)
8850Sstevel@tonic-gate {
8860Sstevel@tonic-gate 	offset_t offset;
8870Sstevel@tonic-gate 	struct pcdir *ep = NULL;
8880Sstevel@tonic-gate 	int boff;
8890Sstevel@tonic-gate 	int error;
8900Sstevel@tonic-gate 	struct vnode *vp;
8910Sstevel@tonic-gate 	struct pcfs *fsp;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	vp = PCTOV(dp);
8940Sstevel@tonic-gate 	PC_DPRINTF2(6, "pc_findentry: looking for %s in dir 0x%p\n", namep,
8950Sstevel@tonic-gate 	    (void *)dp);
8960Sstevel@tonic-gate 	slotp->sl_status = SL_NONE;
8970Sstevel@tonic-gate 	if (!(dp->pc_entry.pcd_attr & PCA_DIR)) {
8980Sstevel@tonic-gate 		return (ENOTDIR);
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 	/*
9010Sstevel@tonic-gate 	 * Verify that the dp is still valid on the disk
9020Sstevel@tonic-gate 	 */
9030Sstevel@tonic-gate 	fsp = VFSTOPCFS(vp->v_vfsp);
9040Sstevel@tonic-gate 	error = pc_verify(fsp);
9050Sstevel@tonic-gate 	if (error)
9060Sstevel@tonic-gate 		return (error);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	slotp->sl_bp = NULL;
9090Sstevel@tonic-gate 	offset = 0;
9100Sstevel@tonic-gate 	for (;;) {
9110Sstevel@tonic-gate 		/*
9120Sstevel@tonic-gate 		 * If offset is on a block boundary,
9130Sstevel@tonic-gate 		 * read in the next directory block.
9140Sstevel@tonic-gate 		 * Release previous if it exists.
9150Sstevel@tonic-gate 		 */
9160Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
9170Sstevel@tonic-gate 		if (boff == 0 || slotp->sl_bp == NULL ||
9180Sstevel@tonic-gate 		    boff >= slotp->sl_bp->b_bcount) {
9190Sstevel@tonic-gate 			if (slotp->sl_bp != NULL) {
9200Sstevel@tonic-gate 				brelse(slotp->sl_bp);
9210Sstevel@tonic-gate 				slotp->sl_bp = NULL;
9220Sstevel@tonic-gate 			}
9230Sstevel@tonic-gate 			error = pc_blkatoff(dp, offset, &slotp->sl_bp, &ep);
9240Sstevel@tonic-gate 			if (error == ENOENT && slotp->sl_status == SL_NONE) {
9250Sstevel@tonic-gate 				slotp->sl_status = SL_EXTEND;
9260Sstevel@tonic-gate 				slotp->sl_offset = (int)offset;
9270Sstevel@tonic-gate 			}
9280Sstevel@tonic-gate 			if (error)
9290Sstevel@tonic-gate 				return (error);
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 		if ((ep->pcd_filename[0] == PCD_UNUSED) ||
9320Sstevel@tonic-gate 		    (ep->pcd_filename[0] == PCD_ERASED)) {
9330Sstevel@tonic-gate 			/*
9340Sstevel@tonic-gate 			 * note empty slots, in case name is not found
9350Sstevel@tonic-gate 			 */
9360Sstevel@tonic-gate 			if (slotp->sl_status == SL_NONE) {
9370Sstevel@tonic-gate 				slotp->sl_status = SL_FOUND;
9380Sstevel@tonic-gate 				slotp->sl_blkno = pc_daddrdb(fsp,
9390Sstevel@tonic-gate 				    slotp->sl_bp->b_blkno);
9400Sstevel@tonic-gate 				slotp->sl_offset = boff;
9410Sstevel@tonic-gate 			}
9420Sstevel@tonic-gate 			/*
9430Sstevel@tonic-gate 			 * If unused we've hit the end of the directory
9440Sstevel@tonic-gate 			 */
9450Sstevel@tonic-gate 			if (ep->pcd_filename[0] == PCD_UNUSED)
9460Sstevel@tonic-gate 				break;
9470Sstevel@tonic-gate 			offset += sizeof (struct pcdir);
9480Sstevel@tonic-gate 			ep++;
9490Sstevel@tonic-gate 			continue;
9500Sstevel@tonic-gate 		}
9510Sstevel@tonic-gate 		if (PCDL_IS_LFN(ep)) {
9520Sstevel@tonic-gate 			offset_t t = offset;
9530Sstevel@tonic-gate 			if (pc_match_long_fn(dp, namep, &ep,
9540Sstevel@tonic-gate 			    slotp, &offset) == 0) {
9550Sstevel@tonic-gate 				if (lfn_offset != NULL)
9560Sstevel@tonic-gate 					*lfn_offset = t;
9570Sstevel@tonic-gate 				return (0);
9580Sstevel@tonic-gate 			}
9590Sstevel@tonic-gate 			continue;
9600Sstevel@tonic-gate 		}
9610Sstevel@tonic-gate 		if (pc_match_short_fn(dp, namep, &ep, slotp, &offset) == 0)
9620Sstevel@tonic-gate 			return (0);
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 	if (slotp->sl_bp != NULL) {
9650Sstevel@tonic-gate 		brelse(slotp->sl_bp);
9660Sstevel@tonic-gate 		slotp->sl_bp = NULL;
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate 	return (ENOENT);
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate /*
9720Sstevel@tonic-gate  * Obtain the block at offset "offset" in file pcp.
9730Sstevel@tonic-gate  */
9740Sstevel@tonic-gate int
9750Sstevel@tonic-gate pc_blkatoff(
9760Sstevel@tonic-gate 	struct pcnode *pcp,
9770Sstevel@tonic-gate 	offset_t offset,
9780Sstevel@tonic-gate 	struct buf **bpp,
9790Sstevel@tonic-gate 	struct pcdir **epp)
9800Sstevel@tonic-gate {
9810Sstevel@tonic-gate 	struct pcfs *fsp;
9820Sstevel@tonic-gate 	struct buf *bp;
9830Sstevel@tonic-gate 	int size;
9840Sstevel@tonic-gate 	int error;
9850Sstevel@tonic-gate 	daddr_t bn;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	fsp = VFSTOPCFS(PCTOV(pcp)->v_vfsp);
9880Sstevel@tonic-gate 	size = pc_blksize(fsp, pcp, offset);
9890Sstevel@tonic-gate 	if (pc_blkoff(fsp, offset) >= size) {
9900Sstevel@tonic-gate 		PC_DPRINTF0(5, "pc_blkatoff: ENOENT\n");
9910Sstevel@tonic-gate 		return (ENOENT);
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 	error = pc_bmap(pcp, pc_lblkno(fsp, offset), &bn, (uint_t *)0);
9940Sstevel@tonic-gate 	if (error)
9950Sstevel@tonic-gate 		return (error);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	bp = bread(fsp->pcfs_xdev, bn, size);
9980Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
9990Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_blkatoff: error\n");
10000Sstevel@tonic-gate 		brelse(bp);
10010Sstevel@tonic-gate 		pc_mark_irrecov(fsp);
10020Sstevel@tonic-gate 		return (EIO);
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 	if (epp) {
10050Sstevel@tonic-gate 		*epp =
10060Sstevel@tonic-gate 		    (struct pcdir *)(bp->b_un.b_addr + pc_blkoff(fsp, offset));
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 	*bpp = bp;
10090Sstevel@tonic-gate 	return (0);
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate /*
10130Sstevel@tonic-gate  * Parse user filename into the pc form of "filename.extension".
10140Sstevel@tonic-gate  * If names are too long for the format (and enable_long_filenames is set)
10150Sstevel@tonic-gate  * it returns EINVAL (since either this name was read from the disk (so
10160Sstevel@tonic-gate  * it must fit), _or_ we're trying to match a long file name (so we
10170Sstevel@tonic-gate  * should fail).  Tests for characters that are invalid in PCDOS and
10180Sstevel@tonic-gate  * converts to upper case (unless foldcase is 0).
10190Sstevel@tonic-gate  */
10200Sstevel@tonic-gate static int
10210Sstevel@tonic-gate pc_parsename(
10220Sstevel@tonic-gate 	char *namep,
10230Sstevel@tonic-gate 	char *fnamep,
10240Sstevel@tonic-gate 	char *fextp)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	int n;
10270Sstevel@tonic-gate 	char c;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	n = PCFNAMESIZE;
10300Sstevel@tonic-gate 	c = *namep++;
10310Sstevel@tonic-gate 	if (c == 0)
10320Sstevel@tonic-gate 		return (EINVAL);
10330Sstevel@tonic-gate 	if (c == '.') {
10340Sstevel@tonic-gate 		/*
10350Sstevel@tonic-gate 		 * check for "." and "..".
10360Sstevel@tonic-gate 		 */
10370Sstevel@tonic-gate 		*fnamep++ = c;
10380Sstevel@tonic-gate 		n--;
10390Sstevel@tonic-gate 		if (c = *namep++) {
10400Sstevel@tonic-gate 			if ((c != '.') || (c = *namep)) /* ".x" or "..x" */
10410Sstevel@tonic-gate 				return (EINVAL);
10420Sstevel@tonic-gate 			*fnamep++ = '.';
10430Sstevel@tonic-gate 			n--;
10440Sstevel@tonic-gate 		}
10450Sstevel@tonic-gate 	} else {
10460Sstevel@tonic-gate 		/*
10470Sstevel@tonic-gate 		 * filename up to '.'
10480Sstevel@tonic-gate 		 */
10490Sstevel@tonic-gate 		do {
10500Sstevel@tonic-gate 			if (n-- > 0) {
10510Sstevel@tonic-gate 				c = toupper(c);
10520Sstevel@tonic-gate 				if (!pc_validchar(c))
10530Sstevel@tonic-gate 					return (EINVAL);
10540Sstevel@tonic-gate 				*fnamep++ = c;
10550Sstevel@tonic-gate 			} else {
10560Sstevel@tonic-gate 				/* not short */
10570Sstevel@tonic-gate 				if (enable_long_filenames)
10580Sstevel@tonic-gate 					return (EINVAL);
10590Sstevel@tonic-gate 			}
10600Sstevel@tonic-gate 		} while ((c = *namep++) != '\0' && c != '.');
10610Sstevel@tonic-gate 	}
10620Sstevel@tonic-gate 	while (n-- > 0) {		/* fill with blanks */
10630Sstevel@tonic-gate 		*fnamep++ = ' ';
10640Sstevel@tonic-gate 	}
10650Sstevel@tonic-gate 	/*
10660Sstevel@tonic-gate 	 * remainder is extension
10670Sstevel@tonic-gate 	 */
10680Sstevel@tonic-gate 	n = PCFEXTSIZE;
10690Sstevel@tonic-gate 	if (c == '.') {
10700Sstevel@tonic-gate 		while ((c = *namep++) != '\0' && n--) {
10710Sstevel@tonic-gate 			c = toupper(c);
10720Sstevel@tonic-gate 			if (!pc_validchar(c))
10730Sstevel@tonic-gate 				return (EINVAL);
10740Sstevel@tonic-gate 			*fextp++ = c;
10750Sstevel@tonic-gate 		}
10760Sstevel@tonic-gate 		if (enable_long_filenames && (c != '\0')) {
10770Sstevel@tonic-gate 			/* not short */
10780Sstevel@tonic-gate 			return (EINVAL);
10790Sstevel@tonic-gate 		}
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 	while (n-- > 0) {		/* fill with blanks */
10820Sstevel@tonic-gate 		*fextp++ = ' ';
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 	return (0);
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate /*
10880Sstevel@tonic-gate  * Match a long filename entry with 'namep'. Also return failure
10890Sstevel@tonic-gate  * if the long filename isn't valid.
10900Sstevel@tonic-gate  */
10910Sstevel@tonic-gate int
10920Sstevel@tonic-gate pc_match_long_fn(struct pcnode *pcp, char *namep, struct pcdir **epp,
10931268Sbatschul     struct pcslot *slotp, offset_t *offset)
10940Sstevel@tonic-gate {
10950Sstevel@tonic-gate 	struct pcdir *ep = (struct pcdir *)*epp;
10960Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
10970Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
10980Sstevel@tonic-gate 	int	error = 0;
10990Sstevel@tonic-gate 	char	lfn[PCMAXNAMLEN+1];
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	error = pc_extract_long_fn(pcp, lfn, epp, offset, &slotp->sl_bp);
11020Sstevel@tonic-gate 	if (error) {
11030Sstevel@tonic-gate 		if (error == EINVAL) {
11040Sstevel@tonic-gate 			return (ENOENT);
11050Sstevel@tonic-gate 		} else
11060Sstevel@tonic-gate 			return (error);
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 	ep = *epp;
11090Sstevel@tonic-gate 	if (strcasecmp(lfn, namep) == 0) {
11100Sstevel@tonic-gate 		/* match */
11110Sstevel@tonic-gate 		slotp->sl_flags = 0;
11120Sstevel@tonic-gate 		slotp->sl_blkno = pc_daddrdb(fsp, slotp->sl_bp->b_blkno);
11130Sstevel@tonic-gate 		slotp->sl_offset = pc_blkoff(fsp, *offset);
11140Sstevel@tonic-gate 		slotp->sl_ep = ep;
11150Sstevel@tonic-gate 		return (0);
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate 	*offset += sizeof (struct pcdir);
11180Sstevel@tonic-gate 	ep++;
11190Sstevel@tonic-gate 	*epp = ep;
11200Sstevel@tonic-gate 	return (ENOENT);
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate /*
11240Sstevel@tonic-gate  * Match a short filename entry with namep.
11250Sstevel@tonic-gate  */
11260Sstevel@tonic-gate int
11270Sstevel@tonic-gate pc_match_short_fn(struct pcnode *pcp, char *namep, struct pcdir **epp,
11281268Sbatschul     struct pcslot *slotp, offset_t *offset)
11290Sstevel@tonic-gate {
11300Sstevel@tonic-gate 	char fname[PCFNAMESIZE];
11310Sstevel@tonic-gate 	char fext[PCFEXTSIZE];
11320Sstevel@tonic-gate 	struct pcdir *ep = *epp;
11330Sstevel@tonic-gate 	int	error;
11340Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
11350Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
11360Sstevel@tonic-gate 	int boff = pc_blkoff(fsp, *offset);
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	if (PCA_IS_HIDDEN(fsp, ep->pcd_attr)) {
11390Sstevel@tonic-gate 		*offset += sizeof (struct pcdir);
11400Sstevel@tonic-gate 		ep++;
11410Sstevel@tonic-gate 		*epp = ep;
11420Sstevel@tonic-gate 		return (ENOENT);
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	error = pc_parsename(namep, fname, fext);
11460Sstevel@tonic-gate 	if (error) {
11470Sstevel@tonic-gate 		*offset += sizeof (struct pcdir);
11480Sstevel@tonic-gate 		ep++;
11490Sstevel@tonic-gate 		*epp = ep;
11500Sstevel@tonic-gate 		return (error);
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	if ((bcmp(fname, ep->pcd_filename, PCFNAMESIZE) == 0) &&
11540Sstevel@tonic-gate 	    (bcmp(fext, ep->pcd_ext, PCFEXTSIZE) == 0)) {
11550Sstevel@tonic-gate 		/*
11560Sstevel@tonic-gate 		 * found the file
11570Sstevel@tonic-gate 		 */
11580Sstevel@tonic-gate 		if (fname[0] == '.') {
11590Sstevel@tonic-gate 			if (fname[1] == '.')
11600Sstevel@tonic-gate 				slotp->sl_flags = SL_DOTDOT;
11610Sstevel@tonic-gate 			else
11620Sstevel@tonic-gate 				slotp->sl_flags = SL_DOT;
11630Sstevel@tonic-gate 		} else {
11640Sstevel@tonic-gate 			slotp->sl_flags = 0;
11650Sstevel@tonic-gate 		}
11660Sstevel@tonic-gate 		slotp->sl_blkno =
11670Sstevel@tonic-gate 		    pc_daddrdb(fsp, slotp->sl_bp->b_blkno);
11680Sstevel@tonic-gate 		slotp->sl_offset = boff;
11690Sstevel@tonic-gate 		slotp->sl_ep = ep;
11700Sstevel@tonic-gate 		return (0);
11710Sstevel@tonic-gate 	}
11720Sstevel@tonic-gate 	*offset += sizeof (struct pcdir);
11730Sstevel@tonic-gate 	ep++;
11740Sstevel@tonic-gate 	*epp = ep;
11750Sstevel@tonic-gate 	return (ENOENT);
11760Sstevel@tonic-gate }
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate /*
11790Sstevel@tonic-gate  * Remove a long filename entry starting at lfn_offset. It must be
11800Sstevel@tonic-gate  * a valid entry or we wouldn't have gotten here. Also remove the
11810Sstevel@tonic-gate  * short filename entry.
11820Sstevel@tonic-gate  */
11830Sstevel@tonic-gate static int
11840Sstevel@tonic-gate pc_remove_long_fn(struct pcnode *pcp, offset_t lfn_offset)
11850Sstevel@tonic-gate {
11860Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
11870Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
11880Sstevel@tonic-gate 	int boff;
11890Sstevel@tonic-gate 	struct buf *bp = NULL;
11900Sstevel@tonic-gate 	struct pcdir *ep = NULL;
11910Sstevel@tonic-gate 	int	error = 0;
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	/*
11940Sstevel@tonic-gate 	 * if we're in here, we know that the lfn is in the proper format
11950Sstevel@tonic-gate 	 * of <series-of-lfn-entries> followed by <sfn-entry>
11960Sstevel@tonic-gate 	 */
11970Sstevel@tonic-gate 	for (;;) {
11980Sstevel@tonic-gate 		boff = pc_blkoff(fsp, lfn_offset);
11990Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
12000Sstevel@tonic-gate 			if (bp != NULL) {
12010Sstevel@tonic-gate 				bwrite2(bp);
12020Sstevel@tonic-gate 				error = geterror(bp);
12030Sstevel@tonic-gate 				brelse(bp);
12040Sstevel@tonic-gate 				if (error)
12050Sstevel@tonic-gate 					return (error);
12060Sstevel@tonic-gate 				bp = NULL;
12070Sstevel@tonic-gate 			}
12080Sstevel@tonic-gate 			error = pc_blkatoff(pcp, lfn_offset, &bp, &ep);
12090Sstevel@tonic-gate 			if (error)
12100Sstevel@tonic-gate 				return (error);
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 		if (!PCDL_IS_LFN(ep)) {
12130Sstevel@tonic-gate 			/* done */
12140Sstevel@tonic-gate 			break;
12150Sstevel@tonic-gate 		}
12160Sstevel@tonic-gate 		/* zap it */
12170Sstevel@tonic-gate 		ep->pcd_filename[0] = PCD_ERASED;
12180Sstevel@tonic-gate 		ep->pcd_attr = 0;
12190Sstevel@tonic-gate 		lfn_offset += sizeof (struct pcdir);
12200Sstevel@tonic-gate 		ep++;
12210Sstevel@tonic-gate 	}
12220Sstevel@tonic-gate 	/* now we're on the short entry */
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	ep->pcd_filename[0] = PCD_ERASED;
12250Sstevel@tonic-gate 	ep->pcd_attr = 0;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	if (bp != NULL) {
12280Sstevel@tonic-gate 		bwrite2(bp);
12290Sstevel@tonic-gate 		error = geterror(bp);
12300Sstevel@tonic-gate 		brelse(bp);
12310Sstevel@tonic-gate 		if (error)
12320Sstevel@tonic-gate 			return (error);
12330Sstevel@tonic-gate 	}
12340Sstevel@tonic-gate 	return (0);
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate /*
12380Sstevel@tonic-gate  * Find (and allocate) space in the directory denoted by
12390Sstevel@tonic-gate  * 'pcp'. for 'ndirentries' pcdir structures.
12400Sstevel@tonic-gate  * Return the offset at which to start, or -1 for failure.
12410Sstevel@tonic-gate  */
12420Sstevel@tonic-gate static offset_t
12430Sstevel@tonic-gate pc_find_free_space(struct pcnode *pcp, int ndirentries)
12440Sstevel@tonic-gate {
12450Sstevel@tonic-gate 	offset_t offset = 0;
12460Sstevel@tonic-gate 	offset_t spaceneeded = ndirentries * sizeof (struct pcdir);
12470Sstevel@tonic-gate 	offset_t spaceoffset;
12480Sstevel@tonic-gate 	offset_t spaceavail = 0;
12490Sstevel@tonic-gate 	int boff;
12500Sstevel@tonic-gate 	struct buf *bp = NULL;
12510Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
12520Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
12530Sstevel@tonic-gate 	struct pcdir *ep;
12540Sstevel@tonic-gate 	int	error;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	spaceoffset = offset;
12570Sstevel@tonic-gate 	while (spaceneeded > spaceavail) {
12580Sstevel@tonic-gate 		/*
12590Sstevel@tonic-gate 		 * If offset is on a block boundary,
12600Sstevel@tonic-gate 		 * read in the next directory block.
12610Sstevel@tonic-gate 		 * Release previous if it exists.
12620Sstevel@tonic-gate 		 */
12630Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
12640Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
12650Sstevel@tonic-gate 			if (bp != NULL) {
12660Sstevel@tonic-gate 				brelse(bp);
12670Sstevel@tonic-gate 				bp = NULL;
12680Sstevel@tonic-gate 			}
12690Sstevel@tonic-gate 			error = pc_blkatoff(pcp, offset, &bp, &ep);
12700Sstevel@tonic-gate 			if (error == ENOENT) {
12710Sstevel@tonic-gate 				daddr_t bn;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 				/* extend directory */
12740Sstevel@tonic-gate 				if (!IS_FAT32(fsp) && (vp->v_flag & VROOT))
12750Sstevel@tonic-gate 					return (-1);
12760Sstevel@tonic-gate 				while (spaceneeded > spaceavail) {
12770Sstevel@tonic-gate 					error = pc_balloc(pcp,
12780Sstevel@tonic-gate 					    pc_lblkno(fsp, offset), 1, &bn);
12790Sstevel@tonic-gate 					if (error)
12800Sstevel@tonic-gate 						return (-1);
12810Sstevel@tonic-gate 					pcp->pc_size += fsp->pcfs_clsize;
12820Sstevel@tonic-gate 					spaceavail += fsp->pcfs_clsize;
12830Sstevel@tonic-gate 					offset += fsp->pcfs_clsize;
12840Sstevel@tonic-gate 				}
12850Sstevel@tonic-gate 				return (spaceoffset);
12860Sstevel@tonic-gate 			}
12870Sstevel@tonic-gate 			if (error)
12880Sstevel@tonic-gate 				return (-1);
12890Sstevel@tonic-gate 		}
12900Sstevel@tonic-gate 		if ((ep->pcd_filename[0] == PCD_UNUSED) ||
12910Sstevel@tonic-gate 		    (ep->pcd_filename[0] == PCD_ERASED)) {
12920Sstevel@tonic-gate 			offset += sizeof (struct pcdir);
12930Sstevel@tonic-gate 			spaceavail += sizeof (struct pcdir);
12940Sstevel@tonic-gate 			ep++;
12950Sstevel@tonic-gate 			continue;
12960Sstevel@tonic-gate 		}
12970Sstevel@tonic-gate 		offset += sizeof (struct pcdir);
12980Sstevel@tonic-gate 		spaceavail = 0;
12990Sstevel@tonic-gate 		spaceoffset = offset;
13000Sstevel@tonic-gate 		ep++;
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate 	if (bp != NULL) {
13030Sstevel@tonic-gate 		brelse(bp);
13040Sstevel@tonic-gate 	}
13050Sstevel@tonic-gate 	return (spaceoffset);
13060Sstevel@tonic-gate }
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate /*
13090Sstevel@tonic-gate  * Return how many long filename entries are needed.
13100Sstevel@tonic-gate  * A maximum of PCLFNCHUNKSIZE characters per entry, plus one for a
13110Sstevel@tonic-gate  * short filename.
13120Sstevel@tonic-gate  */
13130Sstevel@tonic-gate static int
13140Sstevel@tonic-gate direntries_needed(struct pcnode *dp, char *namep)
13150Sstevel@tonic-gate {
13160Sstevel@tonic-gate 	struct pcdir ep;
1317*4723Sksn 	uint16_t *w2_str;
1318*4723Sksn 	size_t  u8l, u16l;
1319*4723Sksn 	int ret;
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	if (enable_long_filenames == 0) {
13220Sstevel@tonic-gate 		return (1);
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate 	if (pc_is_short_file_name(namep, 0)) {
13250Sstevel@tonic-gate 		(void) pc_parsename(namep, ep.pcd_filename, ep.pcd_ext);
13260Sstevel@tonic-gate 		if (!shortname_exists(dp, ep.pcd_filename, ep.pcd_ext)) {
13270Sstevel@tonic-gate 			return (1);
13280Sstevel@tonic-gate 		}
13290Sstevel@tonic-gate 	}
1330*4723Sksn 	if (pc_valid_long_fn(namep, 1)) {
1331*4723Sksn 		/*
1332*4723Sksn 		 * convert to UTF-16 or UNICODE for calculating the entries
1333*4723Sksn 		 * needed. Conversion will consume at the most 512 bytes
1334*4723Sksn 		 */
1335*4723Sksn 		u16l = PCMAXNAMLEN + 1;
1336*4723Sksn 		w2_str = (uint16_t *)kmem_zalloc(PCMAXNAM_UTF16, KM_SLEEP);
1337*4723Sksn 		u8l = strlen(namep);
1338*4723Sksn 		ret = uconv_u8tou16((const uchar_t *)namep, &u8l,
1339*4723Sksn 		    w2_str, &u16l, UCONV_OUT_LITTLE_ENDIAN);
1340*4723Sksn 		kmem_free((caddr_t)w2_str, PCMAXNAM_UTF16);
1341*4723Sksn 		if (ret == 0) {
1342*4723Sksn 			ret = 1 + u16l / PCLFNCHUNKSIZE;
1343*4723Sksn 			if (u16l % PCLFNCHUNKSIZE != 0)
1344*4723Sksn 				ret++;
1345*4723Sksn 			return (ret);
1346*4723Sksn 		}
13470Sstevel@tonic-gate 	}
13480Sstevel@tonic-gate 	return (-1);
13490Sstevel@tonic-gate }
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate /*
13520Sstevel@tonic-gate  * Allocate and return an array of pcdir structures for the passed-in
13530Sstevel@tonic-gate  * name. ndirentries tells how many are required (including the short
13540Sstevel@tonic-gate  * filename entry). Just allocate and fill them in properly here so they
13550Sstevel@tonic-gate  * can be written out.
13560Sstevel@tonic-gate  */
13570Sstevel@tonic-gate static struct pcdir *
13580Sstevel@tonic-gate pc_name_to_pcdir(struct pcnode *dp, char *namep, int ndirentries, int *errret)
13590Sstevel@tonic-gate {
13600Sstevel@tonic-gate 	struct pcdir *bpcdir;
13610Sstevel@tonic-gate 	struct pcdir *ep;
13620Sstevel@tonic-gate 	struct pcdir_lfn *lep;
13630Sstevel@tonic-gate 	int	i;
13640Sstevel@tonic-gate 	uchar_t	cksum;
13650Sstevel@tonic-gate 	int	nchars;
13660Sstevel@tonic-gate 	int	error = 0;
13670Sstevel@tonic-gate 	char	*nameend;
1368*4723Sksn 	uint16_t *w2_str;
1369*4723Sksn 	size_t  u8l, u16l;
1370*4723Sksn 	int ret;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	bpcdir = kmem_zalloc(ndirentries * sizeof (struct pcdir), KM_SLEEP);
13730Sstevel@tonic-gate 	ep = &bpcdir[ndirentries - 1];
13740Sstevel@tonic-gate 	if (ndirentries == 1) {
13750Sstevel@tonic-gate 		(void) pc_parsename(namep, ep->pcd_filename, ep->pcd_ext);
13760Sstevel@tonic-gate 		return (bpcdir);
13770Sstevel@tonic-gate 	}
1378*4723Sksn 
1379*4723Sksn 	/* Here we need to convert to UTF-16 or UNICODE for writing */
1380*4723Sksn 
1381*4723Sksn 	u16l = PCMAXNAMLEN + 1;
1382*4723Sksn 	w2_str = (uint16_t *)kmem_zalloc(PCMAXNAM_UTF16, KM_SLEEP);
1383*4723Sksn 	u8l = strlen(namep);
1384*4723Sksn 	ret = uconv_u8tou16((const uchar_t *)namep, &u8l, w2_str, &u16l,
1385*4723Sksn 	    UCONV_OUT_LITTLE_ENDIAN);
1386*4723Sksn 	if (ret != 0) {
1387*4723Sksn 		kmem_free((caddr_t)w2_str, PCMAXNAM_UTF16);
1388*4723Sksn 		*errret = ret;
1389*4723Sksn 		return (NULL);
13900Sstevel@tonic-gate 	}
1391*4723Sksn 	nameend = (char *)(w2_str + u16l);
1392*4723Sksn 	u16l %= PCLFNCHUNKSIZE;
1393*4723Sksn 	if (u16l != 0) {
1394*4723Sksn 		nchars = u16l + 1;
1395*4723Sksn 		nameend += 2;
1396*4723Sksn 	} else {
1397*4723Sksn 		nchars = PCLFNCHUNKSIZE;
1398*4723Sksn 	}
1399*4723Sksn 	nchars *= sizeof (uint16_t);
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	/* short file name */
14020Sstevel@tonic-gate 	error = generate_short_name(dp, namep, ep);
14030Sstevel@tonic-gate 	if (error) {
14040Sstevel@tonic-gate 		kmem_free(bpcdir, ndirentries * sizeof (struct pcdir));
14050Sstevel@tonic-gate 		*errret = error;
14060Sstevel@tonic-gate 		return (NULL);
14070Sstevel@tonic-gate 	}
14080Sstevel@tonic-gate 	cksum = pc_checksum_long_fn(ep->pcd_filename, ep->pcd_ext);
14090Sstevel@tonic-gate 	for (i = 0; i < (ndirentries - 1); i++) {
14100Sstevel@tonic-gate 		/* long file name */
14110Sstevel@tonic-gate 		nameend -= nchars;
14120Sstevel@tonic-gate 		lep = (struct pcdir_lfn *)&bpcdir[i];
14130Sstevel@tonic-gate 		set_long_fn_chunk(lep, nameend, nchars);
14140Sstevel@tonic-gate 		lep->pcdl_attr = PCDL_LFN_BITS;
14150Sstevel@tonic-gate 		lep->pcdl_checksum = cksum;
14160Sstevel@tonic-gate 		lep->pcdl_ordinal = (uchar_t)(ndirentries - i - 1);
1417*4723Sksn 		nchars = PCLFNCHUNKSIZE * sizeof (uint16_t);
14180Sstevel@tonic-gate 	}
1419*4723Sksn 	kmem_free((caddr_t)w2_str, PCMAXNAM_UTF16);
14200Sstevel@tonic-gate 	lep = (struct pcdir_lfn *)&bpcdir[0];
14210Sstevel@tonic-gate 	lep->pcdl_ordinal |= 0x40;
14220Sstevel@tonic-gate 	return (bpcdir);
14230Sstevel@tonic-gate }
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate static int
14260Sstevel@tonic-gate generate_short_name(struct pcnode *dp, char *namep, struct pcdir *inep)
14270Sstevel@tonic-gate {
14280Sstevel@tonic-gate 	int	rev;
14290Sstevel@tonic-gate 	int	nchars;
14300Sstevel@tonic-gate 	int	i, j;
14310Sstevel@tonic-gate 	char	*dot = NULL;
14320Sstevel@tonic-gate 	char	fname[PCFNAMESIZE+1];
14330Sstevel@tonic-gate 	char	fext[PCFEXTSIZE+1];
14340Sstevel@tonic-gate 	char	scratch[8];
14350Sstevel@tonic-gate 	int	error = 0;
14361268Sbatschul 	struct	pcslot slot;
14370Sstevel@tonic-gate 	char	shortname[20];
14380Sstevel@tonic-gate 	int	force_tilde = 0;
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	/*
14410Sstevel@tonic-gate 	 * generate a unique short file name based on the long input name.
14420Sstevel@tonic-gate 	 *
14430Sstevel@tonic-gate 	 * Say, for "This is a very long filename.txt" generate
14440Sstevel@tonic-gate 	 * "THISIS~1.TXT", or "THISIS~2.TXT" if that's already there.
14450Sstevel@tonic-gate 	 * Skip invalid short name characters in the long name, plus
14460Sstevel@tonic-gate 	 * a couple NT skips (space and reverse backslash).
14470Sstevel@tonic-gate 	 *
14480Sstevel@tonic-gate 	 * Unfortunately, since this name would be hidden by the normal
14490Sstevel@tonic-gate 	 * lookup routine, we need to look for it ourselves. But luckily
14500Sstevel@tonic-gate 	 * we don't need to look at the lfn entries themselves.
14510Sstevel@tonic-gate 	 */
14520Sstevel@tonic-gate 	force_tilde = !pc_is_short_file_name(namep, 1);
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	/*
14550Sstevel@tonic-gate 	 * Strip off leading invalid characters.
14560Sstevel@tonic-gate 	 * We need this because names like '.login' are now ok, but the
14570Sstevel@tonic-gate 	 * short name needs to be something like LOGIN~1.
14580Sstevel@tonic-gate 	 */
14590Sstevel@tonic-gate 	for (; *namep != '\0'; namep++) {
14600Sstevel@tonic-gate 		if (*namep == ' ')
14610Sstevel@tonic-gate 			continue;
14620Sstevel@tonic-gate 		if (!pc_validchar(*namep) && !pc_validchar(toupper(*namep)))
14630Sstevel@tonic-gate 			continue;
14640Sstevel@tonic-gate 		break;
14650Sstevel@tonic-gate 	}
14660Sstevel@tonic-gate 	dot = strrchr(namep, '.');
14670Sstevel@tonic-gate 	if (dot != NULL) {
14680Sstevel@tonic-gate 		dot++;
14690Sstevel@tonic-gate 		for (j = 0, i = 0; j < PCFEXTSIZE; i++) {
14700Sstevel@tonic-gate 			if (dot[i] == '\0')
14710Sstevel@tonic-gate 				break;
14720Sstevel@tonic-gate 			/* skip valid, but not generally good characters */
14730Sstevel@tonic-gate 			if (dot[i] == ' ' || dot[i] == '\\')
14740Sstevel@tonic-gate 				continue;
14750Sstevel@tonic-gate 			if (pc_validchar(dot[i]))
14760Sstevel@tonic-gate 				fext[j++] = dot[i];
14770Sstevel@tonic-gate 			else if (pc_validchar(toupper(dot[i])))
14780Sstevel@tonic-gate 				fext[j++] = toupper(dot[i]);
14790Sstevel@tonic-gate 		}
14800Sstevel@tonic-gate 		for (i = j; i < PCFEXTSIZE; i++)
14810Sstevel@tonic-gate 			fext[i] = ' ';
14820Sstevel@tonic-gate 		dot--;
14830Sstevel@tonic-gate 	} else {
14840Sstevel@tonic-gate 		for (i = 0; i < PCFEXTSIZE; i++) {
14850Sstevel@tonic-gate 			fext[i] = ' ';
14860Sstevel@tonic-gate 		}
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 	/*
14890Sstevel@tonic-gate 	 * We know we're a long name, not a short name (or we wouldn't
14900Sstevel@tonic-gate 	 * be here at all. But if uppercasing ourselves would be a short
14910Sstevel@tonic-gate 	 * name, then we can possibly avoid the ~N format.
14920Sstevel@tonic-gate 	 */
14930Sstevel@tonic-gate 	if (!force_tilde)
14940Sstevel@tonic-gate 		rev = 0;
14950Sstevel@tonic-gate 	else
14960Sstevel@tonic-gate 		rev = 1;
14970Sstevel@tonic-gate 	for (;;) {
14980Sstevel@tonic-gate 		bzero(fname, sizeof (fname));
14990Sstevel@tonic-gate 		nchars = PCFNAMESIZE;
15000Sstevel@tonic-gate 		if (rev) {
15010Sstevel@tonic-gate 			nchars--; /* ~ */
15020Sstevel@tonic-gate 			i = rev;
15030Sstevel@tonic-gate 			do {
15040Sstevel@tonic-gate 				nchars--;
15050Sstevel@tonic-gate 				i /= 10;
15060Sstevel@tonic-gate 			} while (i);
15070Sstevel@tonic-gate 			if (nchars <= 0) {
15080Sstevel@tonic-gate 				return (ENOSPC);
15090Sstevel@tonic-gate 			}
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 		for (j = 0, i = 0; j < nchars; i++) {
15120Sstevel@tonic-gate 			if ((&namep[i] == dot) || (namep[i] == '\0'))
15130Sstevel@tonic-gate 				break;
15140Sstevel@tonic-gate 			/* skip valid, but not generally good characters */
15150Sstevel@tonic-gate 			if (namep[i] == ' ' || namep[i] == '\\')
15160Sstevel@tonic-gate 				continue;
15170Sstevel@tonic-gate 			if (pc_validchar(namep[i]))
15180Sstevel@tonic-gate 				fname[j++] = namep[i];
15190Sstevel@tonic-gate 			else if (pc_validchar(toupper(namep[i])))
15200Sstevel@tonic-gate 				fname[j++] = toupper(namep[i]);
15210Sstevel@tonic-gate 		}
15220Sstevel@tonic-gate 		if (rev) {
15230Sstevel@tonic-gate 			(void) sprintf(scratch, "~%d", rev);
15240Sstevel@tonic-gate 			(void) strcat(fname, scratch);
15250Sstevel@tonic-gate 		}
15260Sstevel@tonic-gate 		for (i = strlen(fname); i < PCFNAMESIZE; i++)
15270Sstevel@tonic-gate 			fname[i] = ' ';
15280Sstevel@tonic-gate 		/* now see if it exists */
15290Sstevel@tonic-gate 		(void) pc_fname_ext_to_name(shortname, fname, fext, 0);
15300Sstevel@tonic-gate 		error = pc_findentry(dp, shortname, &slot, NULL);
15310Sstevel@tonic-gate 		if (error == 0) {
15320Sstevel@tonic-gate 			/* found it */
15330Sstevel@tonic-gate 			brelse(slot.sl_bp);
15340Sstevel@tonic-gate 			rev++;
15350Sstevel@tonic-gate 			continue;
15360Sstevel@tonic-gate 		}
15370Sstevel@tonic-gate 		if (!shortname_exists(dp, fname, fext))
15380Sstevel@tonic-gate 			break;
15390Sstevel@tonic-gate 		rev++;
15400Sstevel@tonic-gate 	}
15410Sstevel@tonic-gate 	(void) strncpy(inep->pcd_filename, fname, PCFNAMESIZE);
15420Sstevel@tonic-gate 	(void) strncpy(inep->pcd_ext, fext, PCFEXTSIZE);
15430Sstevel@tonic-gate 	return (0);
15440Sstevel@tonic-gate }
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate /*
15470Sstevel@tonic-gate  * Returns 1 if the passed-in filename is a short name, 0 if not.
15480Sstevel@tonic-gate  */
15490Sstevel@tonic-gate static int
15500Sstevel@tonic-gate pc_is_short_file_name(char *namep, int foldcase)
15510Sstevel@tonic-gate {
15520Sstevel@tonic-gate 	int	i;
15530Sstevel@tonic-gate 	char	c;
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	for (i = 0; i < PCFNAMESIZE; i++, namep++) {
15560Sstevel@tonic-gate 		if (*namep == '\0')
15570Sstevel@tonic-gate 			return (1);
15580Sstevel@tonic-gate 		if (*namep == '.')
15590Sstevel@tonic-gate 			break;
15600Sstevel@tonic-gate 		if (foldcase)
15610Sstevel@tonic-gate 			c = toupper(*namep);
15620Sstevel@tonic-gate 		else
15630Sstevel@tonic-gate 			c = *namep;
15640Sstevel@tonic-gate 		if (!pc_validchar(c))
15650Sstevel@tonic-gate 			return (0);
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate 	if (*namep == '\0')
15680Sstevel@tonic-gate 		return (1);
15690Sstevel@tonic-gate 	if (*namep != '.')
15700Sstevel@tonic-gate 		return (0);
15710Sstevel@tonic-gate 	namep++;
15720Sstevel@tonic-gate 	for (i = 0; i < PCFEXTSIZE; i++, namep++) {
15730Sstevel@tonic-gate 		if (*namep == '\0')
15740Sstevel@tonic-gate 			return (1);
15750Sstevel@tonic-gate 		if (foldcase)
15760Sstevel@tonic-gate 			c = toupper(*namep);
15770Sstevel@tonic-gate 		else
15780Sstevel@tonic-gate 			c = *namep;
15790Sstevel@tonic-gate 		if (!pc_validchar(c))
15800Sstevel@tonic-gate 			return (0);
15810Sstevel@tonic-gate 	}
15820Sstevel@tonic-gate 	/* we should be done. If not... */
15830Sstevel@tonic-gate 	if (*namep == '\0')
15840Sstevel@tonic-gate 		return (1);
15850Sstevel@tonic-gate 	return (0);
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate }
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate /*
15900Sstevel@tonic-gate  * We call this when we want to see if a short filename already exists
15910Sstevel@tonic-gate  * in the filesystem as part of a long filename. When creating a short
15920Sstevel@tonic-gate  * name (FILENAME.TXT from the user, or when generating one for a long
15930Sstevel@tonic-gate  * filename), we cannot allow one that is part of a long filename.
15940Sstevel@tonic-gate  * pc_findentry will find all the names that are visible (long or short),
15950Sstevel@tonic-gate  * but will not crack any long filename entries.
15960Sstevel@tonic-gate  */
15970Sstevel@tonic-gate static int
15980Sstevel@tonic-gate shortname_exists(struct pcnode *dp, char *fname, char *fext)
15990Sstevel@tonic-gate {
16000Sstevel@tonic-gate 	struct buf *bp = NULL;
16010Sstevel@tonic-gate 	int	offset = 0;
16020Sstevel@tonic-gate 	int	match = 0;
16030Sstevel@tonic-gate 	struct pcdir *ep;
16040Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
16050Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
16060Sstevel@tonic-gate 	int	boff;
16070Sstevel@tonic-gate 	int	error = 0;
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	for (;;) {
16100Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
16110Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
16120Sstevel@tonic-gate 			if (bp != NULL) {
16130Sstevel@tonic-gate 				brelse(bp);
16140Sstevel@tonic-gate 				bp = NULL;
16150Sstevel@tonic-gate 			}
16160Sstevel@tonic-gate 			error = pc_blkatoff(dp, offset, &bp, &ep);
16170Sstevel@tonic-gate 			if (error == ENOENT)
16180Sstevel@tonic-gate 				break;
16190Sstevel@tonic-gate 			if (error) {
16200Sstevel@tonic-gate 				return (1);
16210Sstevel@tonic-gate 			}
16220Sstevel@tonic-gate 		}
16230Sstevel@tonic-gate 		if (PCDL_IS_LFN(ep) ||
16240Sstevel@tonic-gate 		    (ep->pcd_filename[0] == PCD_ERASED)) {
16250Sstevel@tonic-gate 			offset += sizeof (struct pcdir);
16260Sstevel@tonic-gate 			ep++;
16270Sstevel@tonic-gate 			continue;
16280Sstevel@tonic-gate 		}
16290Sstevel@tonic-gate 		if (ep->pcd_filename[0] == PCD_UNUSED)
16300Sstevel@tonic-gate 			break;
16310Sstevel@tonic-gate 		/*
16320Sstevel@tonic-gate 		 * in use, and a short file name (either standalone
16330Sstevel@tonic-gate 		 * or associated with a long name
16340Sstevel@tonic-gate 		 */
16350Sstevel@tonic-gate 		if ((bcmp(fname, ep->pcd_filename, PCFNAMESIZE) == 0) &&
16360Sstevel@tonic-gate 		    (bcmp(fext, ep->pcd_ext, PCFEXTSIZE) == 0)) {
16370Sstevel@tonic-gate 			match = 1;
16380Sstevel@tonic-gate 			break;
16390Sstevel@tonic-gate 		}
16400Sstevel@tonic-gate 		offset += sizeof (struct pcdir);
16410Sstevel@tonic-gate 		ep++;
16420Sstevel@tonic-gate 	}
16430Sstevel@tonic-gate 	if (bp) {
16440Sstevel@tonic-gate 		brelse(bp);
16450Sstevel@tonic-gate 		bp = NULL;
16460Sstevel@tonic-gate 	}
16470Sstevel@tonic-gate 	return (match);
16480Sstevel@tonic-gate }
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate pc_cluster32_t
16510Sstevel@tonic-gate pc_getstartcluster(struct pcfs *fsp, struct pcdir *ep)
16520Sstevel@tonic-gate {
16530Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
16540Sstevel@tonic-gate 		pc_cluster32_t cn;
16550Sstevel@tonic-gate 		pc_cluster16_t hi16;
16560Sstevel@tonic-gate 		pc_cluster16_t lo16;
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 		hi16 = ltohs(ep->un.pcd_scluster_hi);
16590Sstevel@tonic-gate 		lo16 = ltohs(ep->pcd_scluster_lo);
16600Sstevel@tonic-gate 		cn = (hi16 << 16) | lo16;
16610Sstevel@tonic-gate 		return (cn);
16620Sstevel@tonic-gate 	} else {
16630Sstevel@tonic-gate 		return (ltohs(ep->pcd_scluster_lo));
16640Sstevel@tonic-gate 	}
16650Sstevel@tonic-gate }
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate void
16680Sstevel@tonic-gate pc_setstartcluster(struct pcfs *fsp, struct pcdir *ep, pc_cluster32_t cln)
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
16710Sstevel@tonic-gate 		pc_cluster16_t hi16;
16720Sstevel@tonic-gate 		pc_cluster16_t lo16;
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 		hi16 = (cln >> 16) & 0xFFFF;
16750Sstevel@tonic-gate 		lo16 = cln & 0xFFFF;
16760Sstevel@tonic-gate 		ep->un.pcd_scluster_hi = htols(hi16);
16770Sstevel@tonic-gate 		ep->pcd_scluster_lo = htols(lo16);
16780Sstevel@tonic-gate 	} else {
16790Sstevel@tonic-gate 		pc_cluster16_t cln16;
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 		cln16 = (pc_cluster16_t)cln;
16820Sstevel@tonic-gate 		ep->pcd_scluster_lo = htols(cln16);
16830Sstevel@tonic-gate 	}
16840Sstevel@tonic-gate }
1685