xref: /titanic_41/usr/src/uts/common/fs/pcfs/pc_dir.c (revision 5f07900130cd2b5468379ec18a09057406079819)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55b024a5bSbatschul  * Common Development and Distribution License (the "License").
65b024a5bSbatschul  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21342440ecSPrasad Singamsetty 
227c478bd9Sstevel@tonic-gate /*
23342440ecSPrasad Singamsetty  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/errno.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/buf.h>
327c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
374a37d755Sksn #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/fs/pc_label.h>
397c478bd9Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
407c478bd9Sstevel@tonic-gate #include <sys/fs/pc_dir.h>
417c478bd9Sstevel@tonic-gate #include <sys/fs/pc_node.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static int pc_makedirentry(struct pcnode *dp, struct pcdir *direntries,
447c478bd9Sstevel@tonic-gate     int	ndirentries, struct vattr *vap, offset_t offset);
457c478bd9Sstevel@tonic-gate static int pc_dirempty(struct pcnode *);
465b024a5bSbatschul static int pc_findentry(struct pcnode *, char *, struct pcslot *, offset_t *);
477c478bd9Sstevel@tonic-gate static int pc_parsename(char *, char *, char *);
487c478bd9Sstevel@tonic-gate static int pc_remove_long_fn(struct pcnode *pcp,
497c478bd9Sstevel@tonic-gate     offset_t lfn_offset);
507c478bd9Sstevel@tonic-gate static int generate_short_name(struct pcnode *dp, char *namep,
517c478bd9Sstevel@tonic-gate     struct pcdir *ep);
527c478bd9Sstevel@tonic-gate static struct pcdir *pc_name_to_pcdir(struct pcnode *dp, char *namep,
537c478bd9Sstevel@tonic-gate     int ndirentries, int *errret);
547c478bd9Sstevel@tonic-gate static offset_t pc_find_free_space(struct pcnode *pcp, int ndirentries);
557c478bd9Sstevel@tonic-gate static int direntries_needed(struct pcnode *dp, char *namep);
567c478bd9Sstevel@tonic-gate static int pc_is_short_file_name(char *namep, int foldcase);
577c478bd9Sstevel@tonic-gate static int shortname_exists(struct pcnode *dp, char *fname, char *fext);
587c478bd9Sstevel@tonic-gate static int pc_dirfixdotdot(struct pcnode *cdp, struct pcnode *opdp,
597c478bd9Sstevel@tonic-gate     struct pcnode *npdp);
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Tunables
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate int enable_long_filenames = 1;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Lookup a name in a directory. Return a pointer to the pc_node
677c478bd9Sstevel@tonic-gate  * which represents the entry.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate int
pc_dirlook(struct pcnode * dp,char * namep,struct pcnode ** pcpp)707c478bd9Sstevel@tonic-gate pc_dirlook(
717c478bd9Sstevel@tonic-gate 	struct pcnode *dp,		/* parent directory */
727c478bd9Sstevel@tonic-gate 	char *namep,			/* name to lookup */
737c478bd9Sstevel@tonic-gate 	struct pcnode **pcpp)		/* result */
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	struct vnode *vp;
765b024a5bSbatschul 	struct pcslot slot;
777c478bd9Sstevel@tonic-gate 	int error;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	PC_DPRINTF2(4, "pc_dirlook (dp %p name %s)\n", (void *)dp, namep);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (!(dp->pc_entry.pcd_attr & PCA_DIR)) {
827c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 	vp = PCTOV(dp);
857c478bd9Sstevel@tonic-gate 	/*
867c478bd9Sstevel@tonic-gate 	 * check now for changed disk, before any return(0)
877c478bd9Sstevel@tonic-gate 	 */
887c478bd9Sstevel@tonic-gate 	if (error = pc_verify(VFSTOPCFS(vp->v_vfsp)))
897c478bd9Sstevel@tonic-gate 		return (error);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * Null component name is synonym for directory being searched.
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 	if (*namep == '\0') {
957c478bd9Sstevel@tonic-gate 		VN_HOLD(vp);
967c478bd9Sstevel@tonic-gate 		*pcpp = dp;
977c478bd9Sstevel@tonic-gate 		return (0);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	/*
1007c478bd9Sstevel@tonic-gate 	 * The root directory does not have "." and ".." entries,
1017c478bd9Sstevel@tonic-gate 	 * so they are faked here.
1027c478bd9Sstevel@tonic-gate 	 */
1037c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VROOT) {
1047c478bd9Sstevel@tonic-gate 		if (bcmp(namep, ".", 2) == 0 || bcmp(namep, "..", 3) == 0) {
1057c478bd9Sstevel@tonic-gate 			VN_HOLD(vp);
1067c478bd9Sstevel@tonic-gate 			*pcpp = dp;
1077c478bd9Sstevel@tonic-gate 			return (0);
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	error = pc_findentry(dp, namep, &slot, NULL);
1117c478bd9Sstevel@tonic-gate 	if (error == 0) {
1127c478bd9Sstevel@tonic-gate 		*pcpp = pc_getnode(VFSTOPCFS(vp->v_vfsp),
1137c478bd9Sstevel@tonic-gate 		    slot.sl_blkno, slot.sl_offset, slot.sl_ep);
1147c478bd9Sstevel@tonic-gate 		brelse(slot.sl_bp);
1157c478bd9Sstevel@tonic-gate 		PC_DPRINTF1(4, "pc_dirlook: FOUND pcp=%p\n", (void *)*pcpp);
1167c478bd9Sstevel@tonic-gate 	} else if (error == EINVAL) {
1177c478bd9Sstevel@tonic-gate 		error = ENOENT;
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	return (error);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Enter a name in a directory.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate int
pc_direnter(struct pcnode * dp,char * namep,struct vattr * vap,struct pcnode ** pcpp)1267c478bd9Sstevel@tonic-gate pc_direnter(
1277c478bd9Sstevel@tonic-gate 	struct pcnode *dp,		/* directory to make entry in */
1287c478bd9Sstevel@tonic-gate 	char *namep,			/* name of entry */
1297c478bd9Sstevel@tonic-gate 	struct vattr *vap,		/* attributes of new entry */
1307c478bd9Sstevel@tonic-gate 	struct pcnode **pcpp)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	int error;
1335b024a5bSbatschul 	struct pcslot slot;
1347c478bd9Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
1357c478bd9Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
1367c478bd9Sstevel@tonic-gate 	offset_t offset;
137342440ecSPrasad Singamsetty 	daddr_t	blkno;
1387c478bd9Sstevel@tonic-gate 	int	boff;
1397c478bd9Sstevel@tonic-gate 	struct buf *bp = NULL;
1407c478bd9Sstevel@tonic-gate 	struct pcdir *ep;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	PC_DPRINTF4(4, "pc_dirent(dp %p, name %s, vap %p, pcpp %p\n",
1437c478bd9Sstevel@tonic-gate 	    (void *)dp, namep, (void *)vap, (void *)pcpp);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if (pcpp != NULL)
1467c478bd9Sstevel@tonic-gate 		*pcpp = NULL;
1477c478bd9Sstevel@tonic-gate 	/*
1487c478bd9Sstevel@tonic-gate 	 * Leading spaces are not allowed in DOS.
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	if (*namep == ' ')
1517c478bd9Sstevel@tonic-gate 		return (EINVAL);
1527c478bd9Sstevel@tonic-gate 	/*
1537c478bd9Sstevel@tonic-gate 	 * If name is "." or "..", just look it up.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	if (PC_NAME_IS_DOT(namep) || PC_NAME_IS_DOTDOT(namep)) {
1567c478bd9Sstevel@tonic-gate 		if (pcpp) {
1577c478bd9Sstevel@tonic-gate 			error = pc_dirlook(dp, namep, pcpp);
1587c478bd9Sstevel@tonic-gate 			if (error)
1597c478bd9Sstevel@tonic-gate 				return (error);
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 		return (EEXIST);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	if (PCA_IS_HIDDEN(fsp, dp->pc_entry.pcd_attr)) {
1647c478bd9Sstevel@tonic-gate 		return (EPERM);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	/*
1677c478bd9Sstevel@tonic-gate 	 * Make sure directory has not been removed while fs was unlocked.
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	if (dp->pc_entry.pcd_filename[0] == PCD_ERASED) {
1707c478bd9Sstevel@tonic-gate 		return (ENOENT);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	error = pc_findentry(dp, namep, &slot, NULL);
1737c478bd9Sstevel@tonic-gate 	if (error == 0) {
1747c478bd9Sstevel@tonic-gate 		if (pcpp) {
1757c478bd9Sstevel@tonic-gate 			*pcpp =
1767c478bd9Sstevel@tonic-gate 			    pc_getnode(fsp, slot.sl_blkno, slot.sl_offset,
1777c478bd9Sstevel@tonic-gate 			    slot.sl_ep);
1787c478bd9Sstevel@tonic-gate 			error = EEXIST;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 		brelse(slot.sl_bp);
1817c478bd9Sstevel@tonic-gate 	} else if (error == ENOENT) {
1827c478bd9Sstevel@tonic-gate 		struct pcdir *direntries;
1837c478bd9Sstevel@tonic-gate 		int	ndirentries;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		/*
1867c478bd9Sstevel@tonic-gate 		 * The entry does not exist. Check write permission in
1877c478bd9Sstevel@tonic-gate 		 * directory to see if entry can be created.
1887c478bd9Sstevel@tonic-gate 		 */
1897c478bd9Sstevel@tonic-gate 		if (dp->pc_entry.pcd_attr & PCA_RDONLY) {
1907c478bd9Sstevel@tonic-gate 			return (EPERM);
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 		error = 0;
1937c478bd9Sstevel@tonic-gate 		/*
1947c478bd9Sstevel@tonic-gate 		 * Make sure there is a slot.
1957c478bd9Sstevel@tonic-gate 		 */
1967c478bd9Sstevel@tonic-gate 		if (slot.sl_status == SL_NONE)
1977c478bd9Sstevel@tonic-gate 			panic("pc_direnter: no slot\n");
1987c478bd9Sstevel@tonic-gate 		ndirentries = direntries_needed(dp, namep);
1997c478bd9Sstevel@tonic-gate 		if (ndirentries == -1) {
2007c478bd9Sstevel@tonic-gate 			return (EINVAL);
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		offset = pc_find_free_space(dp, ndirentries);
2047c478bd9Sstevel@tonic-gate 		if (offset == -1) {
2057c478bd9Sstevel@tonic-gate 			return (ENOSPC);
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		/*
2097c478bd9Sstevel@tonic-gate 		 * Make an entry from the supplied attributes.
2107c478bd9Sstevel@tonic-gate 		 */
2117c478bd9Sstevel@tonic-gate 		direntries = pc_name_to_pcdir(dp, namep, ndirentries, &error);
2127c478bd9Sstevel@tonic-gate 		if (direntries == NULL) {
2137c478bd9Sstevel@tonic-gate 			return (error);
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		error = pc_makedirentry(dp, direntries, ndirentries, vap,
2167c478bd9Sstevel@tonic-gate 		    offset);
2177c478bd9Sstevel@tonic-gate 		kmem_free(direntries, ndirentries * sizeof (struct pcdir));
2187c478bd9Sstevel@tonic-gate 		if (error) {
2197c478bd9Sstevel@tonic-gate 			return (error);
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 		offset += (ndirentries - 1)  * sizeof (struct pcdir);
2227c478bd9Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
2237c478bd9Sstevel@tonic-gate 		error = pc_blkatoff(dp, offset, &bp, &ep);
2247c478bd9Sstevel@tonic-gate 		if (error) {
2257c478bd9Sstevel@tonic-gate 			return (error);
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 		blkno = pc_daddrdb(fsp, bp->b_blkno);
2287c478bd9Sstevel@tonic-gate 		/*
2297c478bd9Sstevel@tonic-gate 		 * Get a pcnode for the new entry.
2307c478bd9Sstevel@tonic-gate 		 */
2317c478bd9Sstevel@tonic-gate 		*pcpp = pc_getnode(fsp, blkno, boff, ep);
2327c478bd9Sstevel@tonic-gate 		brelse(bp);
2337c478bd9Sstevel@tonic-gate 		if (vap->va_type == VDIR)
2347c478bd9Sstevel@tonic-gate 			(*pcpp)->pc_size = fsp->pcfs_clsize;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		/*
2377c478bd9Sstevel@tonic-gate 		 * Write out the new entry in the parent directory.
2387c478bd9Sstevel@tonic-gate 		 */
2397c478bd9Sstevel@tonic-gate 		error = pc_syncfat(fsp);
2407c478bd9Sstevel@tonic-gate 		if (!error) {
2417c478bd9Sstevel@tonic-gate 			error = pc_nodeupdate(*pcpp);
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	return (error);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * Template for "." and ".." directory entries.
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate static struct {
2517c478bd9Sstevel@tonic-gate 	struct pcdir t_dot;		/* dot entry */
2527c478bd9Sstevel@tonic-gate 	struct pcdir t_dotdot;		/* dotdot entry */
2537c478bd9Sstevel@tonic-gate } dirtemplate = {
2547c478bd9Sstevel@tonic-gate 	{
2557c478bd9Sstevel@tonic-gate 		".       ",
2567c478bd9Sstevel@tonic-gate 		"   ",
2577c478bd9Sstevel@tonic-gate 		PCA_DIR
2587c478bd9Sstevel@tonic-gate 	},
2597c478bd9Sstevel@tonic-gate 	{
2607c478bd9Sstevel@tonic-gate 		"..      ",
2617c478bd9Sstevel@tonic-gate 		"   ",
2627c478bd9Sstevel@tonic-gate 		PCA_DIR
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate };
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Convert an attributes structure into the short filename entry
2687c478bd9Sstevel@tonic-gate  * and write out the whole entry.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate static int
pc_makedirentry(struct pcnode * dp,struct pcdir * direntries,int ndirentries,struct vattr * vap,offset_t offset)2717c478bd9Sstevel@tonic-gate pc_makedirentry(struct pcnode *dp, struct pcdir *direntries,
2727c478bd9Sstevel@tonic-gate     int ndirentries, struct vattr *vap, offset_t offset)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
2757c478bd9Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
2767c478bd9Sstevel@tonic-gate 	int error;
2777c478bd9Sstevel@tonic-gate 	struct pcdir *ep;
2787c478bd9Sstevel@tonic-gate 	int	boff;
2797c478bd9Sstevel@tonic-gate 	int	i;
2807c478bd9Sstevel@tonic-gate 	struct buf *bp = NULL;
2817c478bd9Sstevel@tonic-gate 	timestruc_t now;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (vap != NULL && vap->va_mask & (AT_ATIME|AT_MTIME))
2847c478bd9Sstevel@tonic-gate 		return (EOPNOTSUPP);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	ep = &direntries[ndirentries - 1];
2877c478bd9Sstevel@tonic-gate 	gethrestime(&now);
288264a6e74Sfrankho 	if (error = pc_tvtopct(&now, &ep->pcd_mtime))
289264a6e74Sfrankho 		return (error);
290264a6e74Sfrankho 
2917c478bd9Sstevel@tonic-gate 	ep->pcd_crtime = ep->pcd_mtime;
2927c478bd9Sstevel@tonic-gate 	ep->pcd_ladate = ep->pcd_mtime.pct_date;
2937c478bd9Sstevel@tonic-gate 	ep->pcd_crtime_msec = 0;
2947c478bd9Sstevel@tonic-gate 	ep->pcd_size = 0;
2957c478bd9Sstevel@tonic-gate 	ep->pcd_attr = 0;
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * Fields we don't use.
2987c478bd9Sstevel@tonic-gate 	 */
2997c478bd9Sstevel@tonic-gate 	ep->pcd_ntattr = 0;
3007c478bd9Sstevel@tonic-gate 	if (!IS_FAT32(fsp))
3017c478bd9Sstevel@tonic-gate 		ep->un.pcd_eattr = 0;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	if (vap && ((vap->va_mode & 0222) == 0))
3047c478bd9Sstevel@tonic-gate 		ep->pcd_attr |=  PCA_RDONLY;
3057c478bd9Sstevel@tonic-gate 	if (vap && (vap->va_type == VDIR)) {
3067c478bd9Sstevel@tonic-gate 		pc_cluster32_t cn;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		ep->pcd_attr |= PCA_DIR;
3097c478bd9Sstevel@tonic-gate 		/*
3107c478bd9Sstevel@tonic-gate 		 * Make dot and dotdot entries for a new directory.
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 		cn = pc_alloccluster(fsp, 0);
3137c478bd9Sstevel@tonic-gate 		switch (cn) {
3147c478bd9Sstevel@tonic-gate 		case PCF_FREECLUSTER:
3157c478bd9Sstevel@tonic-gate 			return (ENOSPC);
3167c478bd9Sstevel@tonic-gate 		case PCF_ERRORCLUSTER:
3177c478bd9Sstevel@tonic-gate 			return (EIO);
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 		bp = ngeteblk(fsp->pcfs_clsize);
3207c478bd9Sstevel@tonic-gate 		bp->b_edev = fsp->pcfs_xdev;
3217c478bd9Sstevel@tonic-gate 		bp->b_dev = cmpdev(bp->b_edev);
3227c478bd9Sstevel@tonic-gate 		bp->b_blkno = pc_cldaddr(fsp, cn);
3237c478bd9Sstevel@tonic-gate 		clrbuf(bp);
3247c478bd9Sstevel@tonic-gate 		pc_setstartcluster(fsp, ep, cn);
3257c478bd9Sstevel@tonic-gate 		pc_setstartcluster(fsp, &dirtemplate.t_dot, cn);
3267c478bd9Sstevel@tonic-gate 		cn = pc_getstartcluster(fsp, &dp->pc_entry);
3277c478bd9Sstevel@tonic-gate 		pc_setstartcluster(fsp, &dirtemplate.t_dotdot, cn);
3287c478bd9Sstevel@tonic-gate 		dirtemplate.t_dot.pcd_mtime =
3297c478bd9Sstevel@tonic-gate 		    dirtemplate.t_dotdot.pcd_mtime = ep->pcd_mtime;
3307c478bd9Sstevel@tonic-gate 		dirtemplate.t_dot.pcd_crtime =
3317c478bd9Sstevel@tonic-gate 		    dirtemplate.t_dotdot.pcd_crtime = ep->pcd_crtime;
3327c478bd9Sstevel@tonic-gate 		dirtemplate.t_dot.pcd_ladate =
3337c478bd9Sstevel@tonic-gate 		    dirtemplate.t_dotdot.pcd_ladate = ep->pcd_ladate;
3347c478bd9Sstevel@tonic-gate 		dirtemplate.t_dot.pcd_crtime_msec =
3357c478bd9Sstevel@tonic-gate 		    dirtemplate.t_dotdot.pcd_crtime_msec = 0;
3367c478bd9Sstevel@tonic-gate 		bcopy(&dirtemplate,
3377c478bd9Sstevel@tonic-gate 		    bp->b_un.b_addr, sizeof (dirtemplate));
3387c478bd9Sstevel@tonic-gate 		bwrite2(bp);
3397c478bd9Sstevel@tonic-gate 		error = geterror(bp);
3407c478bd9Sstevel@tonic-gate 		brelse(bp);
3417c478bd9Sstevel@tonic-gate 		if (error) {
3427c478bd9Sstevel@tonic-gate 			PC_DPRINTF0(1, "pc_makedirentry error");
3437c478bd9Sstevel@tonic-gate 			pc_mark_irrecov(fsp);
3447c478bd9Sstevel@tonic-gate 			return (EIO);
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 	} else {
3477c478bd9Sstevel@tonic-gate 		pc_setstartcluster(fsp, ep, 0);
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 	bp = NULL;
3507c478bd9Sstevel@tonic-gate 	for (i = 0, ep = NULL; i < ndirentries; i++, ep++) {
3517c478bd9Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
3527c478bd9Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
3537c478bd9Sstevel@tonic-gate 			if (bp != NULL) {
3547c478bd9Sstevel@tonic-gate 				/* always modified */
3557c478bd9Sstevel@tonic-gate 				bwrite2(bp);
3567c478bd9Sstevel@tonic-gate 				error = geterror(bp);
3577c478bd9Sstevel@tonic-gate 				brelse(bp);
3587c478bd9Sstevel@tonic-gate 				if (error)
3597c478bd9Sstevel@tonic-gate 					return (error);
3607c478bd9Sstevel@tonic-gate 				bp = NULL;
3617c478bd9Sstevel@tonic-gate 			}
3627c478bd9Sstevel@tonic-gate 			error = pc_blkatoff(dp, offset, &bp, &ep);
3637c478bd9Sstevel@tonic-gate 			if (error)
3647c478bd9Sstevel@tonic-gate 				return (error);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		*ep = direntries[i];
3687c478bd9Sstevel@tonic-gate 		offset += sizeof (struct pcdir);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	if (bp != NULL) {
3717c478bd9Sstevel@tonic-gate 		/* always modified */
3727c478bd9Sstevel@tonic-gate 		bwrite2(bp);
3737c478bd9Sstevel@tonic-gate 		error = geterror(bp);
3747c478bd9Sstevel@tonic-gate 		brelse(bp);
3757c478bd9Sstevel@tonic-gate 		if (error)
3767c478bd9Sstevel@tonic-gate 			return (error);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 	return (0);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * Remove a name from a directory.
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate int
pc_dirremove(struct pcnode * dp,char * namep,struct vnode * cdir,enum vtype type,caller_context_t * ctp)3857c478bd9Sstevel@tonic-gate pc_dirremove(
3867c478bd9Sstevel@tonic-gate 	struct pcnode *dp,
3877c478bd9Sstevel@tonic-gate 	char *namep,
3887c478bd9Sstevel@tonic-gate 	struct vnode *cdir,
389da6c28aaSamw 	enum vtype type,
390da6c28aaSamw 	caller_context_t *ctp)
3917c478bd9Sstevel@tonic-gate {
3925b024a5bSbatschul 	struct pcslot slot;
3937c478bd9Sstevel@tonic-gate 	struct pcnode *pcp;
3947c478bd9Sstevel@tonic-gate 	int error;
3957c478bd9Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
3967c478bd9Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
3977c478bd9Sstevel@tonic-gate 	offset_t lfn_offset = -1;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	PC_DPRINTF2(4, "pc_dirremove (dp %p name %s)\n", (void *)dp, namep);
4007c478bd9Sstevel@tonic-gate 	if ((dp->pc_entry.pcd_attr & PCA_RDONLY) ||
4017c478bd9Sstevel@tonic-gate 	    PCA_IS_HIDDEN(fsp, dp->pc_entry.pcd_attr)) {
4027c478bd9Sstevel@tonic-gate 		return (EPERM);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	error = pc_findentry(dp, namep, &slot, &lfn_offset);
4057c478bd9Sstevel@tonic-gate 	if (error)
4067c478bd9Sstevel@tonic-gate 		return (error);
4077c478bd9Sstevel@tonic-gate 	if (slot.sl_flags == SL_DOT) {
4087c478bd9Sstevel@tonic-gate 		error = EINVAL;
4097c478bd9Sstevel@tonic-gate 	} else if (slot.sl_flags == SL_DOTDOT) {
4107c478bd9Sstevel@tonic-gate 		error = ENOTEMPTY;
4117c478bd9Sstevel@tonic-gate 	} else {
4127c478bd9Sstevel@tonic-gate 		pcp =
4137c478bd9Sstevel@tonic-gate 		    pc_getnode(VFSTOPCFS(vp->v_vfsp),
4147c478bd9Sstevel@tonic-gate 		    slot.sl_blkno, slot.sl_offset, slot.sl_ep);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	if (error) {
4177c478bd9Sstevel@tonic-gate 		brelse(slot.sl_bp);
4187c478bd9Sstevel@tonic-gate 		return (error);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 	if (type == VDIR) {
4217c478bd9Sstevel@tonic-gate 		if (pcp->pc_entry.pcd_attr & PCA_DIR) {
4227c478bd9Sstevel@tonic-gate 			if (PCTOV(pcp) == cdir)
4237c478bd9Sstevel@tonic-gate 				error = EINVAL;
4247c478bd9Sstevel@tonic-gate 			else if (!pc_dirempty(pcp))
4257c478bd9Sstevel@tonic-gate 				error = ENOTEMPTY;
4267c478bd9Sstevel@tonic-gate 		} else {
4277c478bd9Sstevel@tonic-gate 			error = ENOTDIR;
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 	} else {
4307c478bd9Sstevel@tonic-gate 		if (pcp->pc_entry.pcd_attr & PCA_DIR)
4317c478bd9Sstevel@tonic-gate 			error = EISDIR;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	if (error == 0) {
4347c478bd9Sstevel@tonic-gate 		/*
4357c478bd9Sstevel@tonic-gate 		 * Mark the in core node and on disk entry
4367c478bd9Sstevel@tonic-gate 		 * as removed. The slot may then be reused.
4377c478bd9Sstevel@tonic-gate 		 * The files clusters will be deallocated
4387c478bd9Sstevel@tonic-gate 		 * when the last reference goes away.
4397c478bd9Sstevel@tonic-gate 		 */
4407c478bd9Sstevel@tonic-gate 		pcp->pc_eblkno = -1;
4417c478bd9Sstevel@tonic-gate 		pcp->pc_entry.pcd_filename[0] = PCD_ERASED;
4427c478bd9Sstevel@tonic-gate 		if (lfn_offset != -1) {
4437c478bd9Sstevel@tonic-gate 			brelse(slot.sl_bp);
4447c478bd9Sstevel@tonic-gate 			error = pc_remove_long_fn(dp, lfn_offset);
4457c478bd9Sstevel@tonic-gate 			if (error) {
4467c478bd9Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
4477c478bd9Sstevel@tonic-gate 				pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
4487c478bd9Sstevel@tonic-gate 				return (EIO);
4497c478bd9Sstevel@tonic-gate 			}
4507c478bd9Sstevel@tonic-gate 		} else {
4517c478bd9Sstevel@tonic-gate 			slot.sl_ep->pcd_filename[0] = PCD_ERASED;
4527c478bd9Sstevel@tonic-gate 			bwrite2(slot.sl_bp);
4537c478bd9Sstevel@tonic-gate 			error = geterror(slot.sl_bp);
4547c478bd9Sstevel@tonic-gate 			brelse(slot.sl_bp);
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 		if (error) {
4577c478bd9Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
4587c478bd9Sstevel@tonic-gate 			pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
4597c478bd9Sstevel@tonic-gate 			return (EIO);
4607c478bd9Sstevel@tonic-gate 		} else if (type == VDIR) {
4617c478bd9Sstevel@tonic-gate 			error = pc_truncate(pcp, 0L);
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	} else {
4657c478bd9Sstevel@tonic-gate 		brelse(slot.sl_bp);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (error == 0) {
4697c478bd9Sstevel@tonic-gate 		if (type == VDIR) {
470da6c28aaSamw 			vnevent_rmdir(PCTOV(pcp), vp, namep, ctp);
4717c478bd9Sstevel@tonic-gate 		} else {
472da6c28aaSamw 			vnevent_remove(PCTOV(pcp), vp, namep, ctp);
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	VN_RELE(PCTOV(pcp));
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	return (error);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate /*
4827c478bd9Sstevel@tonic-gate  * Determine whether a directory is empty.
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate static int
pc_dirempty(struct pcnode * pcp)4857c478bd9Sstevel@tonic-gate pc_dirempty(struct pcnode *pcp)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	struct buf *bp;
4887c478bd9Sstevel@tonic-gate 	struct pcdir *ep;
4897c478bd9Sstevel@tonic-gate 	offset_t offset;
4907c478bd9Sstevel@tonic-gate 	int boff;
4917c478bd9Sstevel@tonic-gate 	char c;
4927c478bd9Sstevel@tonic-gate 	int error;
4937c478bd9Sstevel@tonic-gate 	struct vnode *vp;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	vp = PCTOV(pcp);
4967c478bd9Sstevel@tonic-gate 	bp = NULL;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	offset = 0;
4997c478bd9Sstevel@tonic-gate 	for (;;) {
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		/*
5027c478bd9Sstevel@tonic-gate 		 * If offset is on a block boundary,
5037c478bd9Sstevel@tonic-gate 		 * read in the next directory block.
5047c478bd9Sstevel@tonic-gate 		 * Release previous if it exists.
5057c478bd9Sstevel@tonic-gate 		 */
5067c478bd9Sstevel@tonic-gate 		boff = pc_blkoff(VFSTOPCFS(vp->v_vfsp), offset);
5077c478bd9Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
5087c478bd9Sstevel@tonic-gate 			if (bp != NULL)
5097c478bd9Sstevel@tonic-gate 				brelse(bp);
5107c478bd9Sstevel@tonic-gate 			if (error = pc_blkatoff(pcp, offset, &bp, &ep)) {
5117c478bd9Sstevel@tonic-gate 				return (error);
5127c478bd9Sstevel@tonic-gate 			}
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 		if (PCDL_IS_LFN(ep)) {
5157c478bd9Sstevel@tonic-gate 			error = pc_extract_long_fn(pcp, NULL, &ep, &offset,
5167c478bd9Sstevel@tonic-gate 			    &bp);
5177c478bd9Sstevel@tonic-gate 			/*
5187c478bd9Sstevel@tonic-gate 			 * EINVAL means the lfn was invalid, so start with
5197c478bd9Sstevel@tonic-gate 			 * the next entry. Otherwise, an error occurred _or_
5207c478bd9Sstevel@tonic-gate 			 * the lfn is valid, either of which means the
5217c478bd9Sstevel@tonic-gate 			 * directory is not empty.
5227c478bd9Sstevel@tonic-gate 			 */
5237c478bd9Sstevel@tonic-gate 			if (error == EINVAL)
5247c478bd9Sstevel@tonic-gate 				continue;
5257c478bd9Sstevel@tonic-gate 			else {
5267c478bd9Sstevel@tonic-gate 				if (bp)
5277c478bd9Sstevel@tonic-gate 					brelse(bp);
5287c478bd9Sstevel@tonic-gate 				return (error);
5297c478bd9Sstevel@tonic-gate 			}
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 		c = ep->pcd_filename[0];
5327c478bd9Sstevel@tonic-gate 		if (c == PCD_UNUSED)
5337c478bd9Sstevel@tonic-gate 			break;
5347c478bd9Sstevel@tonic-gate 		if ((c != '.') && (c != PCD_ERASED)) {
5357c478bd9Sstevel@tonic-gate 			brelse(bp);
5367c478bd9Sstevel@tonic-gate 			return (0);
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 		if ((c == '.') && !PC_SHORTNAME_IS_DOT(ep->pcd_filename) &&
5397c478bd9Sstevel@tonic-gate 		    !PC_SHORTNAME_IS_DOTDOT(ep->pcd_filename)) {
5407c478bd9Sstevel@tonic-gate 			brelse(bp);
5417c478bd9Sstevel@tonic-gate 			return (0);
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		ep++;
5447c478bd9Sstevel@tonic-gate 		offset += sizeof (struct pcdir);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 	if (bp != NULL)
5477c478bd9Sstevel@tonic-gate 		brelse(bp);
5487c478bd9Sstevel@tonic-gate 	return (1);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate /*
5527c478bd9Sstevel@tonic-gate  * Rename a file.
5537c478bd9Sstevel@tonic-gate  */
5547c478bd9Sstevel@tonic-gate int
pc_rename(struct pcnode * dp,struct pcnode * tdp,char * snm,char * tnm,caller_context_t * ctp)5557c478bd9Sstevel@tonic-gate pc_rename(
5567c478bd9Sstevel@tonic-gate 	struct pcnode *dp,		/* parent directory */
5577c478bd9Sstevel@tonic-gate 	struct pcnode *tdp,		/* target directory */
5587c478bd9Sstevel@tonic-gate 	char *snm,			/* source file name */
559da6c28aaSamw 	char *tnm,			/* target file name */
560da6c28aaSamw 	caller_context_t *ctp)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	struct pcnode *pcp;	/* pcnode we are trying to rename */
5637c478bd9Sstevel@tonic-gate 	struct pcnode *tpcp;	/* pcnode that's in our way */
5645b024a5bSbatschul 	struct pcslot slot;
5657c478bd9Sstevel@tonic-gate 	int error;
5667c478bd9Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
5677c478bd9Sstevel@tonic-gate 	struct vnode *svp = NULL;
5687c478bd9Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
5697c478bd9Sstevel@tonic-gate 	int filecasechange = 0;
5707c478bd9Sstevel@tonic-gate 	int oldisdir = 0;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	PC_DPRINTF3(4, "pc_rename(0x%p, %s, %s)\n", (void *)dp, snm, tnm);
5737c478bd9Sstevel@tonic-gate 	/*
5747c478bd9Sstevel@tonic-gate 	 * Leading spaces are not allowed in DOS.
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 	if (*tnm == ' ')
5777c478bd9Sstevel@tonic-gate 		return (EINVAL);
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 * No dot or dotdot.
5807c478bd9Sstevel@tonic-gate 	 */
5817c478bd9Sstevel@tonic-gate 	if (PC_NAME_IS_DOT(snm) || PC_NAME_IS_DOTDOT(snm) ||
5827c478bd9Sstevel@tonic-gate 	    PC_NAME_IS_DOT(tnm) || PC_NAME_IS_DOTDOT(tnm))
5837c478bd9Sstevel@tonic-gate 		return (EINVAL);
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * Get the source node.  We'll jump back to here if trying to
5867c478bd9Sstevel@tonic-gate 	 * move on top of an existing file, after deleting that file.
5877c478bd9Sstevel@tonic-gate 	 */
5887c478bd9Sstevel@tonic-gate top:
5897c478bd9Sstevel@tonic-gate 	error = pc_findentry(dp, snm, &slot, NULL);
5907c478bd9Sstevel@tonic-gate 	if (error) {
5917c478bd9Sstevel@tonic-gate 		return (error);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 	pcp = pc_getnode(VFSTOPCFS(vp->v_vfsp),
5947c478bd9Sstevel@tonic-gate 	    slot.sl_blkno, slot.sl_offset, slot.sl_ep);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	brelse(slot.sl_bp);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if (pcp)
5997c478bd9Sstevel@tonic-gate 		svp = PCTOV(pcp);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	/*
6027c478bd9Sstevel@tonic-gate 	 * is the rename invalid, i.e. rename("a", "a/a")
6037c478bd9Sstevel@tonic-gate 	 */
6047c478bd9Sstevel@tonic-gate 	if (pcp == tdp) {
6057c478bd9Sstevel@tonic-gate 		if (svp)
6067c478bd9Sstevel@tonic-gate 			VN_RELE(svp);
6077c478bd9Sstevel@tonic-gate 		return (EINVAL);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/*
6117c478bd9Sstevel@tonic-gate 	 * Are we just changing the case of an existing name?
6127c478bd9Sstevel@tonic-gate 	 */
6137c478bd9Sstevel@tonic-gate 	if ((dp->pc_scluster == tdp->pc_scluster) &&
614*5f079001SOwen Roberts 	    (u8_strcmp(snm, tnm, 0, U8_STRCMP_CI_UPPER, U8_UNICODE_LATEST,
615*5f079001SOwen Roberts 	    &error) == 0)) {
6167c478bd9Sstevel@tonic-gate 		filecasechange = 1;
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
619*5f079001SOwen Roberts 	/*
620*5f079001SOwen Roberts 	 * u8_strcmp detected an illegal character
621*5f079001SOwen Roberts 	 */
622*5f079001SOwen Roberts 	if (error)
623*5f079001SOwen Roberts 		return (EINVAL);
624*5f079001SOwen Roberts 
6257c478bd9Sstevel@tonic-gate 	oldisdir = pcp->pc_entry.pcd_attr & PCA_DIR;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/*
6287c478bd9Sstevel@tonic-gate 	 * see if the target exists
6297c478bd9Sstevel@tonic-gate 	 */
6307c478bd9Sstevel@tonic-gate 	error = pc_findentry(tdp, tnm, &slot, NULL);
6317c478bd9Sstevel@tonic-gate 	if (error == 0 && filecasechange == 0) {
6327c478bd9Sstevel@tonic-gate 		/*
6337c478bd9Sstevel@tonic-gate 		 * Target exists.  If it's a file, delete it.  If it's
6347c478bd9Sstevel@tonic-gate 		 * a directory, bail.
6357c478bd9Sstevel@tonic-gate 		 */
6367c478bd9Sstevel@tonic-gate 		int newisdir;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		tpcp = pc_getnode(VFSTOPCFS(vp->v_vfsp),
6397c478bd9Sstevel@tonic-gate 		    slot.sl_blkno, slot.sl_offset, slot.sl_ep);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		newisdir = tpcp->pc_entry.pcd_attr & PCA_DIR;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		brelse(slot.sl_bp);
644da6c28aaSamw 		vnevent_rename_dest(PCTOV(tpcp), PCTOV(tdp), tnm, ctp);
6457c478bd9Sstevel@tonic-gate 		VN_RELE(PCTOV(tpcp));
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		/*
6487c478bd9Sstevel@tonic-gate 		 * Error cases (from rename(2)):
6497c478bd9Sstevel@tonic-gate 		 * old is dir, new is dir: EEXIST
6507c478bd9Sstevel@tonic-gate 		 * old is dir, new is nondir: ENOTDIR
6517c478bd9Sstevel@tonic-gate 		 * old is nondir, new is dir: EISDIR
6527c478bd9Sstevel@tonic-gate 		 */
6537c478bd9Sstevel@tonic-gate 		if (!newisdir) {
6547c478bd9Sstevel@tonic-gate 			if (oldisdir) {
6557c478bd9Sstevel@tonic-gate 				error = ENOTDIR;
6567c478bd9Sstevel@tonic-gate 			} else {
6577c478bd9Sstevel@tonic-gate 				/* nondir/nondir, remove target */
6587c478bd9Sstevel@tonic-gate 				error = pc_dirremove(tdp, tnm,
659da6c28aaSamw 				    (struct vnode *)NULL, VREG, ctp);
6607c478bd9Sstevel@tonic-gate 				if (error == 0) {
6617c478bd9Sstevel@tonic-gate 					VN_RELE(PCTOV(pcp));
6627c478bd9Sstevel@tonic-gate 					goto top;
6637c478bd9Sstevel@tonic-gate 				}
6647c478bd9Sstevel@tonic-gate 			}
6657c478bd9Sstevel@tonic-gate 		} else if (oldisdir) {
6667c478bd9Sstevel@tonic-gate 			/* dir/dir, remove target */
6677c478bd9Sstevel@tonic-gate 			error = pc_dirremove(tdp, tnm,
668da6c28aaSamw 			    (struct vnode *)NULL, VDIR, ctp);
6697c478bd9Sstevel@tonic-gate 			if (error == 0) {
6707c478bd9Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
6717c478bd9Sstevel@tonic-gate 				goto top;
6727c478bd9Sstevel@tonic-gate 			}
6737c478bd9Sstevel@tonic-gate 			/* Follow rename(2)'s spec... */
6747c478bd9Sstevel@tonic-gate 			if (error == ENOTEMPTY) {
6757c478bd9Sstevel@tonic-gate 				error = EEXIST;
6767c478bd9Sstevel@tonic-gate 			}
6777c478bd9Sstevel@tonic-gate 		} else {
6787c478bd9Sstevel@tonic-gate 			/* nondir/dir, bail */
6797c478bd9Sstevel@tonic-gate 			error = EISDIR;
6807c478bd9Sstevel@tonic-gate 		}
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if ((error == 0) || (error == ENOENT)) {
6847c478bd9Sstevel@tonic-gate 		offset_t lfn_offset = -1;
685342440ecSPrasad Singamsetty 		daddr_t	blkno;
6867c478bd9Sstevel@tonic-gate 		struct pcdir *direntries;
6877c478bd9Sstevel@tonic-gate 		struct pcdir *ep;
6887c478bd9Sstevel@tonic-gate 		int	ndirentries;
6897c478bd9Sstevel@tonic-gate 		pc_cluster16_t pct_lo;
6907c478bd9Sstevel@tonic-gate 		pc_cluster16_t pct_hi;
6917c478bd9Sstevel@tonic-gate 		offset_t offset;
6927c478bd9Sstevel@tonic-gate 		int	boff;
6937c478bd9Sstevel@tonic-gate 		struct buf *bp = NULL;
6947c478bd9Sstevel@tonic-gate 		uchar_t	attr;
6957c478bd9Sstevel@tonic-gate 		int	size;
6967c478bd9Sstevel@tonic-gate 		struct pctime mtime;
6977c478bd9Sstevel@tonic-gate 		struct pctime crtime;
6987c478bd9Sstevel@tonic-gate 		uchar_t	ntattr;
6997c478bd9Sstevel@tonic-gate 		ushort_t ladate;
7007c478bd9Sstevel@tonic-gate 		ushort_t eattr;
7017c478bd9Sstevel@tonic-gate 		uchar_t	crtime_msec;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		/*
7047c478bd9Sstevel@tonic-gate 		 * Rename the source.
7057c478bd9Sstevel@tonic-gate 		 */
7067c478bd9Sstevel@tonic-gate 		/*
7077c478bd9Sstevel@tonic-gate 		 * Delete the old name, and create a new name.
7087c478bd9Sstevel@tonic-gate 		 */
7097c478bd9Sstevel@tonic-gate 		if (filecasechange == 1 && error == 0)
7107c478bd9Sstevel@tonic-gate 			brelse(slot.sl_bp);
7117c478bd9Sstevel@tonic-gate 		ndirentries = direntries_needed(tdp, tnm);
7127c478bd9Sstevel@tonic-gate 		if (ndirentries == -1) {
7137c478bd9Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7147c478bd9Sstevel@tonic-gate 			return (EINVAL);
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 		/*
7177c478bd9Sstevel@tonic-gate 		 * first see if we have enough space to create the new
7187c478bd9Sstevel@tonic-gate 		 * name before destroying the old one.
7197c478bd9Sstevel@tonic-gate 		 */
7207c478bd9Sstevel@tonic-gate 		offset = pc_find_free_space(tdp, ndirentries);
7217c478bd9Sstevel@tonic-gate 		if (offset == -1) {
7227c478bd9Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7237c478bd9Sstevel@tonic-gate 			return (ENOSPC);
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		error = pc_findentry(dp, snm, &slot, &lfn_offset);
7277c478bd9Sstevel@tonic-gate 		if (error) {
7287c478bd9Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7297c478bd9Sstevel@tonic-gate 			return (error);
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 		pct_lo = slot.sl_ep->pcd_scluster_lo;
7327c478bd9Sstevel@tonic-gate 		if (IS_FAT32(fsp))
7337c478bd9Sstevel@tonic-gate 			pct_hi = slot.sl_ep->un.pcd_scluster_hi;
7347c478bd9Sstevel@tonic-gate 		else
7357c478bd9Sstevel@tonic-gate 			eattr = slot.sl_ep->un.pcd_eattr;
7367c478bd9Sstevel@tonic-gate 		size = slot.sl_ep->pcd_size;
7377c478bd9Sstevel@tonic-gate 		attr = slot.sl_ep->pcd_attr;
7387c478bd9Sstevel@tonic-gate 		mtime = slot.sl_ep->pcd_mtime;
7397c478bd9Sstevel@tonic-gate 		crtime = slot.sl_ep->pcd_crtime;
7407c478bd9Sstevel@tonic-gate 		crtime_msec = slot.sl_ep->pcd_crtime_msec;
7417c478bd9Sstevel@tonic-gate 		ntattr = slot.sl_ep->pcd_ntattr;
7427c478bd9Sstevel@tonic-gate 		ladate = slot.sl_ep->pcd_ladate;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		if (lfn_offset != -1) {
7457c478bd9Sstevel@tonic-gate 			brelse(slot.sl_bp);
7467c478bd9Sstevel@tonic-gate 			error = pc_remove_long_fn(dp, lfn_offset);
7477c478bd9Sstevel@tonic-gate 			if (error) {
7487c478bd9Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
7497c478bd9Sstevel@tonic-gate 				pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
7507c478bd9Sstevel@tonic-gate 				return (error);
7517c478bd9Sstevel@tonic-gate 			}
7527c478bd9Sstevel@tonic-gate 		} else {
7537c478bd9Sstevel@tonic-gate 			slot.sl_ep->pcd_filename[0] =
7547c478bd9Sstevel@tonic-gate 			    pcp->pc_entry.pcd_filename[0] = PCD_ERASED;
7557c478bd9Sstevel@tonic-gate 			bwrite2(slot.sl_bp);
7567c478bd9Sstevel@tonic-gate 			error = geterror(slot.sl_bp);
7577c478bd9Sstevel@tonic-gate 			brelse(slot.sl_bp);
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 		if (error) {
7607c478bd9Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7617c478bd9Sstevel@tonic-gate 			pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
7627c478bd9Sstevel@tonic-gate 			return (EIO);
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		/*
7667c478bd9Sstevel@tonic-gate 		 * Make an entry from the supplied attributes.
7677c478bd9Sstevel@tonic-gate 		 */
7687c478bd9Sstevel@tonic-gate 		direntries = pc_name_to_pcdir(tdp, tnm, ndirentries, &error);
7697c478bd9Sstevel@tonic-gate 		if (direntries == NULL) {
7707c478bd9Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7717c478bd9Sstevel@tonic-gate 			return (error);
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 		error = pc_makedirentry(tdp, direntries, ndirentries, NULL,
7747c478bd9Sstevel@tonic-gate 		    offset);
7757c478bd9Sstevel@tonic-gate 		kmem_free(direntries, ndirentries * sizeof (struct pcdir));
7767c478bd9Sstevel@tonic-gate 		if (error) {
7777c478bd9Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7787c478bd9Sstevel@tonic-gate 			return (error);
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 		/* advance to short name */
7817c478bd9Sstevel@tonic-gate 		offset += (ndirentries - 1)  * sizeof (struct pcdir);
7827c478bd9Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
7837c478bd9Sstevel@tonic-gate 		error = pc_blkatoff(tdp, offset, &bp, &ep);
7847c478bd9Sstevel@tonic-gate 		if (error) {
7857c478bd9Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
7867c478bd9Sstevel@tonic-gate 			return (error);
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 		blkno = pc_daddrdb(fsp, bp->b_blkno);
7897c478bd9Sstevel@tonic-gate 		ep->pcd_scluster_lo = pct_lo;
7907c478bd9Sstevel@tonic-gate 		if (IS_FAT32(fsp))
7917c478bd9Sstevel@tonic-gate 			ep->un.pcd_scluster_hi = pct_hi;
7927c478bd9Sstevel@tonic-gate 		else
7937c478bd9Sstevel@tonic-gate 			ep->un.pcd_eattr = eattr;
7947c478bd9Sstevel@tonic-gate 		ep->pcd_size = size;
7957c478bd9Sstevel@tonic-gate 		ep->pcd_attr = attr;
7967c478bd9Sstevel@tonic-gate 		ep->pcd_mtime = mtime;
7977c478bd9Sstevel@tonic-gate 		ep->pcd_crtime = crtime;
7987c478bd9Sstevel@tonic-gate 		ep->pcd_crtime_msec = crtime_msec;
7997c478bd9Sstevel@tonic-gate 		ep->pcd_ntattr = ntattr;
8007c478bd9Sstevel@tonic-gate 		ep->pcd_ladate = ladate;
8017c478bd9Sstevel@tonic-gate 		bwrite2(bp);
8027c478bd9Sstevel@tonic-gate 		error = geterror(bp);
8037c478bd9Sstevel@tonic-gate 		pcp->pc_eblkno = blkno;
8047c478bd9Sstevel@tonic-gate 		pcp->pc_eoffset = boff;
8057c478bd9Sstevel@tonic-gate 		pcp->pc_entry = *ep;
8067c478bd9Sstevel@tonic-gate 		pcp->pc_flags |= PC_CHG;
8077c478bd9Sstevel@tonic-gate 		brelse(bp);
8087c478bd9Sstevel@tonic-gate 		if (error) {
8097c478bd9Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
8107c478bd9Sstevel@tonic-gate 			pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp));
8117c478bd9Sstevel@tonic-gate 			return (EIO);
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		/* No need to fix ".." if we're renaming within a dir */
8147c478bd9Sstevel@tonic-gate 		if (oldisdir && dp != tdp) {
8157c478bd9Sstevel@tonic-gate 			if ((error = pc_dirfixdotdot(pcp, dp, tdp)) != 0) {
8167c478bd9Sstevel@tonic-gate 				VN_RELE(PCTOV(pcp));
8177c478bd9Sstevel@tonic-gate 				return (error);
8187c478bd9Sstevel@tonic-gate 			}
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 		if ((error = pc_nodeupdate(pcp)) != 0) {
8217c478bd9Sstevel@tonic-gate 			VN_RELE(PCTOV(pcp));
8227c478bd9Sstevel@tonic-gate 			return (error);
8237c478bd9Sstevel@tonic-gate 		}
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate out:
826da6c28aaSamw 	vnevent_rename_src(PCTOV(pcp), PCTOV(dp), snm, ctp);
827df2381bfSpraks 	if (dp != tdp) {
828da6c28aaSamw 		vnevent_rename_dest_dir(PCTOV(tdp), ctp);
829df2381bfSpraks 	}
830df2381bfSpraks 
8317c478bd9Sstevel@tonic-gate 	VN_RELE(PCTOV(pcp));
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	return (error);
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate /*
8377c478bd9Sstevel@tonic-gate  * Fix the ".." entry of the child directory so that it points to the
8387c478bd9Sstevel@tonic-gate  * new parent directory instead of the old one.
8397c478bd9Sstevel@tonic-gate  */
8407c478bd9Sstevel@tonic-gate static int
pc_dirfixdotdot(struct pcnode * dp,struct pcnode * opdp,struct pcnode * npdp)8417c478bd9Sstevel@tonic-gate pc_dirfixdotdot(struct pcnode *dp,	/* child directory being moved */
8427c478bd9Sstevel@tonic-gate 	struct pcnode *opdp,		/* old parent directory */
8437c478bd9Sstevel@tonic-gate 	struct pcnode *npdp)		/* new parent directory */
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	pc_cluster32_t cn;
8467c478bd9Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
8477c478bd9Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
8487c478bd9Sstevel@tonic-gate 	int error = 0;
8497c478bd9Sstevel@tonic-gate 	struct buf *bp = NULL;
8507c478bd9Sstevel@tonic-gate 	struct pcdir *ep = NULL;
8517c478bd9Sstevel@tonic-gate 	struct pcdir *tep = NULL;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	/*
8547c478bd9Sstevel@tonic-gate 	 * set the new child's ".." directory entry starting cluster to
8557c478bd9Sstevel@tonic-gate 	 * point to the new parent's starting cluster
8567c478bd9Sstevel@tonic-gate 	 */
8577c478bd9Sstevel@tonic-gate 	ASSERT(opdp != npdp);
8587c478bd9Sstevel@tonic-gate 	error = pc_blkatoff(dp, (offset_t)0, &bp, &ep);
8597c478bd9Sstevel@tonic-gate 	if (error) {
8607c478bd9Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_dirfixdotdot: error in blkatoff\n");
8617c478bd9Sstevel@tonic-gate 		return (error);
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 	tep = ep;
8647c478bd9Sstevel@tonic-gate 	ep++;
8657c478bd9Sstevel@tonic-gate 	if (!PC_SHORTNAME_IS_DOT(tep->pcd_filename) &&
8667c478bd9Sstevel@tonic-gate 	    !PC_SHORTNAME_IS_DOTDOT(ep->pcd_filename)) {
8677c478bd9Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_dirfixdotdot: mangled directory entry\n");
8687c478bd9Sstevel@tonic-gate 		error = ENOTDIR;
8697c478bd9Sstevel@tonic-gate 		return (error);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 	cn = pc_getstartcluster(fsp, &npdp->pc_entry);
8727c478bd9Sstevel@tonic-gate 	pc_setstartcluster(fsp, ep, cn);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	bwrite2(bp);
8757c478bd9Sstevel@tonic-gate 	error = geterror(bp);
8767c478bd9Sstevel@tonic-gate 	brelse(bp);
8777c478bd9Sstevel@tonic-gate 	if (error) {
8787c478bd9Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_dirfixdotdot: error in write\n");
8797c478bd9Sstevel@tonic-gate 		pc_mark_irrecov(fsp);
8807c478bd9Sstevel@tonic-gate 		return (EIO);
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 	return (0);
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate /*
8877c478bd9Sstevel@tonic-gate  * Search a directory for an entry.
8887c478bd9Sstevel@tonic-gate  * The directory should be locked as this routine
8897c478bd9Sstevel@tonic-gate  * will sleep on I/O while searching.
8907c478bd9Sstevel@tonic-gate  */
8917c478bd9Sstevel@tonic-gate static int
pc_findentry(struct pcnode * dp,char * namep,struct pcslot * slotp,offset_t * lfn_offset)8927c478bd9Sstevel@tonic-gate pc_findentry(
8937c478bd9Sstevel@tonic-gate 	struct pcnode *dp,		/* parent directory */
8947c478bd9Sstevel@tonic-gate 	char *namep,			/* name to lookup */
8955b024a5bSbatschul 	struct pcslot *slotp,
8967c478bd9Sstevel@tonic-gate 	offset_t *lfn_offset)
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate 	offset_t offset;
8997c478bd9Sstevel@tonic-gate 	struct pcdir *ep = NULL;
9007c478bd9Sstevel@tonic-gate 	int boff;
9017c478bd9Sstevel@tonic-gate 	int error;
9027c478bd9Sstevel@tonic-gate 	struct vnode *vp;
9037c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	vp = PCTOV(dp);
9067c478bd9Sstevel@tonic-gate 	PC_DPRINTF2(6, "pc_findentry: looking for %s in dir 0x%p\n", namep,
9077c478bd9Sstevel@tonic-gate 	    (void *)dp);
9087c478bd9Sstevel@tonic-gate 	slotp->sl_status = SL_NONE;
9097c478bd9Sstevel@tonic-gate 	if (!(dp->pc_entry.pcd_attr & PCA_DIR)) {
9107c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 	/*
9137c478bd9Sstevel@tonic-gate 	 * Verify that the dp is still valid on the disk
9147c478bd9Sstevel@tonic-gate 	 */
9157c478bd9Sstevel@tonic-gate 	fsp = VFSTOPCFS(vp->v_vfsp);
9167c478bd9Sstevel@tonic-gate 	error = pc_verify(fsp);
9177c478bd9Sstevel@tonic-gate 	if (error)
9187c478bd9Sstevel@tonic-gate 		return (error);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	slotp->sl_bp = NULL;
9217c478bd9Sstevel@tonic-gate 	offset = 0;
9227c478bd9Sstevel@tonic-gate 	for (;;) {
9237c478bd9Sstevel@tonic-gate 		/*
9247c478bd9Sstevel@tonic-gate 		 * If offset is on a block boundary,
9257c478bd9Sstevel@tonic-gate 		 * read in the next directory block.
9267c478bd9Sstevel@tonic-gate 		 * Release previous if it exists.
9277c478bd9Sstevel@tonic-gate 		 */
9287c478bd9Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
9297c478bd9Sstevel@tonic-gate 		if (boff == 0 || slotp->sl_bp == NULL ||
9307c478bd9Sstevel@tonic-gate 		    boff >= slotp->sl_bp->b_bcount) {
9317c478bd9Sstevel@tonic-gate 			if (slotp->sl_bp != NULL) {
9327c478bd9Sstevel@tonic-gate 				brelse(slotp->sl_bp);
9337c478bd9Sstevel@tonic-gate 				slotp->sl_bp = NULL;
9347c478bd9Sstevel@tonic-gate 			}
9357c478bd9Sstevel@tonic-gate 			error = pc_blkatoff(dp, offset, &slotp->sl_bp, &ep);
9367c478bd9Sstevel@tonic-gate 			if (error == ENOENT && slotp->sl_status == SL_NONE) {
9377c478bd9Sstevel@tonic-gate 				slotp->sl_status = SL_EXTEND;
9387c478bd9Sstevel@tonic-gate 				slotp->sl_offset = (int)offset;
9397c478bd9Sstevel@tonic-gate 			}
9407c478bd9Sstevel@tonic-gate 			if (error)
9417c478bd9Sstevel@tonic-gate 				return (error);
9427c478bd9Sstevel@tonic-gate 		}
9437c478bd9Sstevel@tonic-gate 		if ((ep->pcd_filename[0] == PCD_UNUSED) ||
9447c478bd9Sstevel@tonic-gate 		    (ep->pcd_filename[0] == PCD_ERASED)) {
9457c478bd9Sstevel@tonic-gate 			/*
9467c478bd9Sstevel@tonic-gate 			 * note empty slots, in case name is not found
9477c478bd9Sstevel@tonic-gate 			 */
9487c478bd9Sstevel@tonic-gate 			if (slotp->sl_status == SL_NONE) {
9497c478bd9Sstevel@tonic-gate 				slotp->sl_status = SL_FOUND;
9507c478bd9Sstevel@tonic-gate 				slotp->sl_blkno = pc_daddrdb(fsp,
9517c478bd9Sstevel@tonic-gate 				    slotp->sl_bp->b_blkno);
9527c478bd9Sstevel@tonic-gate 				slotp->sl_offset = boff;
9537c478bd9Sstevel@tonic-gate 			}
9547c478bd9Sstevel@tonic-gate 			/*
9557c478bd9Sstevel@tonic-gate 			 * If unused we've hit the end of the directory
9567c478bd9Sstevel@tonic-gate 			 */
9577c478bd9Sstevel@tonic-gate 			if (ep->pcd_filename[0] == PCD_UNUSED)
9587c478bd9Sstevel@tonic-gate 				break;
9597c478bd9Sstevel@tonic-gate 			offset += sizeof (struct pcdir);
9607c478bd9Sstevel@tonic-gate 			ep++;
9617c478bd9Sstevel@tonic-gate 			continue;
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 		if (PCDL_IS_LFN(ep)) {
9647c478bd9Sstevel@tonic-gate 			offset_t t = offset;
9657c478bd9Sstevel@tonic-gate 			if (pc_match_long_fn(dp, namep, &ep,
9667c478bd9Sstevel@tonic-gate 			    slotp, &offset) == 0) {
9677c478bd9Sstevel@tonic-gate 				if (lfn_offset != NULL)
9687c478bd9Sstevel@tonic-gate 					*lfn_offset = t;
9697c478bd9Sstevel@tonic-gate 				return (0);
9707c478bd9Sstevel@tonic-gate 			}
9717c478bd9Sstevel@tonic-gate 			continue;
9727c478bd9Sstevel@tonic-gate 		}
9737c478bd9Sstevel@tonic-gate 		if (pc_match_short_fn(dp, namep, &ep, slotp, &offset) == 0)
9747c478bd9Sstevel@tonic-gate 			return (0);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 	if (slotp->sl_bp != NULL) {
9777c478bd9Sstevel@tonic-gate 		brelse(slotp->sl_bp);
9787c478bd9Sstevel@tonic-gate 		slotp->sl_bp = NULL;
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 	return (ENOENT);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate /*
9847c478bd9Sstevel@tonic-gate  * Obtain the block at offset "offset" in file pcp.
9857c478bd9Sstevel@tonic-gate  */
9867c478bd9Sstevel@tonic-gate int
pc_blkatoff(struct pcnode * pcp,offset_t offset,struct buf ** bpp,struct pcdir ** epp)9877c478bd9Sstevel@tonic-gate pc_blkatoff(
9887c478bd9Sstevel@tonic-gate 	struct pcnode *pcp,
9897c478bd9Sstevel@tonic-gate 	offset_t offset,
9907c478bd9Sstevel@tonic-gate 	struct buf **bpp,
9917c478bd9Sstevel@tonic-gate 	struct pcdir **epp)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate 	struct pcfs *fsp;
9947c478bd9Sstevel@tonic-gate 	struct buf *bp;
9957c478bd9Sstevel@tonic-gate 	int size;
9967c478bd9Sstevel@tonic-gate 	int error;
9977c478bd9Sstevel@tonic-gate 	daddr_t bn;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	fsp = VFSTOPCFS(PCTOV(pcp)->v_vfsp);
10007c478bd9Sstevel@tonic-gate 	size = pc_blksize(fsp, pcp, offset);
10017c478bd9Sstevel@tonic-gate 	if (pc_blkoff(fsp, offset) >= size) {
10027c478bd9Sstevel@tonic-gate 		PC_DPRINTF0(5, "pc_blkatoff: ENOENT\n");
10037c478bd9Sstevel@tonic-gate 		return (ENOENT);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 	error = pc_bmap(pcp, pc_lblkno(fsp, offset), &bn, (uint_t *)0);
10067c478bd9Sstevel@tonic-gate 	if (error)
10077c478bd9Sstevel@tonic-gate 		return (error);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	bp = bread(fsp->pcfs_xdev, bn, size);
10107c478bd9Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
10117c478bd9Sstevel@tonic-gate 		PC_DPRINTF0(1, "pc_blkatoff: error\n");
10127c478bd9Sstevel@tonic-gate 		brelse(bp);
10137c478bd9Sstevel@tonic-gate 		pc_mark_irrecov(fsp);
10147c478bd9Sstevel@tonic-gate 		return (EIO);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 	if (epp) {
10177c478bd9Sstevel@tonic-gate 		*epp =
10187c478bd9Sstevel@tonic-gate 		    (struct pcdir *)(bp->b_un.b_addr + pc_blkoff(fsp, offset));
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 	*bpp = bp;
10217c478bd9Sstevel@tonic-gate 	return (0);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate /*
10257c478bd9Sstevel@tonic-gate  * Parse user filename into the pc form of "filename.extension".
10267c478bd9Sstevel@tonic-gate  * If names are too long for the format (and enable_long_filenames is set)
10277c478bd9Sstevel@tonic-gate  * it returns EINVAL (since either this name was read from the disk (so
10287c478bd9Sstevel@tonic-gate  * it must fit), _or_ we're trying to match a long file name (so we
10297c478bd9Sstevel@tonic-gate  * should fail).  Tests for characters that are invalid in PCDOS and
10307c478bd9Sstevel@tonic-gate  * converts to upper case (unless foldcase is 0).
10317c478bd9Sstevel@tonic-gate  */
10327c478bd9Sstevel@tonic-gate static int
pc_parsename(char * namep,char * fnamep,char * fextp)10337c478bd9Sstevel@tonic-gate pc_parsename(
10347c478bd9Sstevel@tonic-gate 	char *namep,
10357c478bd9Sstevel@tonic-gate 	char *fnamep,
10367c478bd9Sstevel@tonic-gate 	char *fextp)
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	int n;
10397c478bd9Sstevel@tonic-gate 	char c;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	n = PCFNAMESIZE;
10427c478bd9Sstevel@tonic-gate 	c = *namep++;
10437c478bd9Sstevel@tonic-gate 	if (c == 0)
10447c478bd9Sstevel@tonic-gate 		return (EINVAL);
10457c478bd9Sstevel@tonic-gate 	if (c == '.') {
10467c478bd9Sstevel@tonic-gate 		/*
10477c478bd9Sstevel@tonic-gate 		 * check for "." and "..".
10487c478bd9Sstevel@tonic-gate 		 */
10497c478bd9Sstevel@tonic-gate 		*fnamep++ = c;
10507c478bd9Sstevel@tonic-gate 		n--;
10517c478bd9Sstevel@tonic-gate 		if (c = *namep++) {
10527c478bd9Sstevel@tonic-gate 			if ((c != '.') || (c = *namep)) /* ".x" or "..x" */
10537c478bd9Sstevel@tonic-gate 				return (EINVAL);
10547c478bd9Sstevel@tonic-gate 			*fnamep++ = '.';
10557c478bd9Sstevel@tonic-gate 			n--;
10567c478bd9Sstevel@tonic-gate 		}
10577c478bd9Sstevel@tonic-gate 	} else {
10587c478bd9Sstevel@tonic-gate 		/*
10597c478bd9Sstevel@tonic-gate 		 * filename up to '.'
10607c478bd9Sstevel@tonic-gate 		 */
10617c478bd9Sstevel@tonic-gate 		do {
10627c478bd9Sstevel@tonic-gate 			if (n-- > 0) {
10637c478bd9Sstevel@tonic-gate 				c = toupper(c);
10647c478bd9Sstevel@tonic-gate 				if (!pc_validchar(c))
10657c478bd9Sstevel@tonic-gate 					return (EINVAL);
10667c478bd9Sstevel@tonic-gate 				*fnamep++ = c;
10677c478bd9Sstevel@tonic-gate 			} else {
10687c478bd9Sstevel@tonic-gate 				/* not short */
10697c478bd9Sstevel@tonic-gate 				if (enable_long_filenames)
10707c478bd9Sstevel@tonic-gate 					return (EINVAL);
10717c478bd9Sstevel@tonic-gate 			}
10727c478bd9Sstevel@tonic-gate 		} while ((c = *namep++) != '\0' && c != '.');
10737c478bd9Sstevel@tonic-gate 	}
10747c478bd9Sstevel@tonic-gate 	while (n-- > 0) {		/* fill with blanks */
10757c478bd9Sstevel@tonic-gate 		*fnamep++ = ' ';
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 	/*
10787c478bd9Sstevel@tonic-gate 	 * remainder is extension
10797c478bd9Sstevel@tonic-gate 	 */
10807c478bd9Sstevel@tonic-gate 	n = PCFEXTSIZE;
10817c478bd9Sstevel@tonic-gate 	if (c == '.') {
10827c478bd9Sstevel@tonic-gate 		while ((c = *namep++) != '\0' && n--) {
10837c478bd9Sstevel@tonic-gate 			c = toupper(c);
10847c478bd9Sstevel@tonic-gate 			if (!pc_validchar(c))
10857c478bd9Sstevel@tonic-gate 				return (EINVAL);
10867c478bd9Sstevel@tonic-gate 			*fextp++ = c;
10877c478bd9Sstevel@tonic-gate 		}
10887c478bd9Sstevel@tonic-gate 		if (enable_long_filenames && (c != '\0')) {
10897c478bd9Sstevel@tonic-gate 			/* not short */
10907c478bd9Sstevel@tonic-gate 			return (EINVAL);
10917c478bd9Sstevel@tonic-gate 		}
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 	while (n-- > 0) {		/* fill with blanks */
10947c478bd9Sstevel@tonic-gate 		*fextp++ = ' ';
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 	return (0);
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate /*
11007c478bd9Sstevel@tonic-gate  * Match a long filename entry with 'namep'. Also return failure
11017c478bd9Sstevel@tonic-gate  * if the long filename isn't valid.
11027c478bd9Sstevel@tonic-gate  */
11037c478bd9Sstevel@tonic-gate int
pc_match_long_fn(struct pcnode * pcp,char * namep,struct pcdir ** epp,struct pcslot * slotp,offset_t * offset)11047c478bd9Sstevel@tonic-gate pc_match_long_fn(struct pcnode *pcp, char *namep, struct pcdir **epp,
11055b024a5bSbatschul     struct pcslot *slotp, offset_t *offset)
11067c478bd9Sstevel@tonic-gate {
11077c478bd9Sstevel@tonic-gate 	struct pcdir *ep = (struct pcdir *)*epp;
11087c478bd9Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
11097c478bd9Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
11107c478bd9Sstevel@tonic-gate 	int	error = 0;
11117c478bd9Sstevel@tonic-gate 	char	lfn[PCMAXNAMLEN+1];
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	error = pc_extract_long_fn(pcp, lfn, epp, offset, &slotp->sl_bp);
11147c478bd9Sstevel@tonic-gate 	if (error) {
11157c478bd9Sstevel@tonic-gate 		if (error == EINVAL) {
11167c478bd9Sstevel@tonic-gate 			return (ENOENT);
11177c478bd9Sstevel@tonic-gate 		} else
11187c478bd9Sstevel@tonic-gate 			return (error);
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 	ep = *epp;
1121*5f079001SOwen Roberts 	if ((u8_strcmp(lfn, namep, 0, U8_STRCMP_CI_UPPER,
1122*5f079001SOwen Roberts 	    U8_UNICODE_LATEST, &error) == 0) && (error == 0)) {
11237c478bd9Sstevel@tonic-gate 		/* match */
11247c478bd9Sstevel@tonic-gate 		slotp->sl_flags = 0;
11257c478bd9Sstevel@tonic-gate 		slotp->sl_blkno = pc_daddrdb(fsp, slotp->sl_bp->b_blkno);
11267c478bd9Sstevel@tonic-gate 		slotp->sl_offset = pc_blkoff(fsp, *offset);
11277c478bd9Sstevel@tonic-gate 		slotp->sl_ep = ep;
11287c478bd9Sstevel@tonic-gate 		return (0);
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate 	*offset += sizeof (struct pcdir);
11317c478bd9Sstevel@tonic-gate 	ep++;
11327c478bd9Sstevel@tonic-gate 	*epp = ep;
1133*5f079001SOwen Roberts 	/* If u8_strcmp detected an error it's sufficient to rtn ENOENT */
11347c478bd9Sstevel@tonic-gate 	return (ENOENT);
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate /*
11387c478bd9Sstevel@tonic-gate  * Match a short filename entry with namep.
11397c478bd9Sstevel@tonic-gate  */
11407c478bd9Sstevel@tonic-gate int
pc_match_short_fn(struct pcnode * pcp,char * namep,struct pcdir ** epp,struct pcslot * slotp,offset_t * offset)11417c478bd9Sstevel@tonic-gate pc_match_short_fn(struct pcnode *pcp, char *namep, struct pcdir **epp,
11425b024a5bSbatschul     struct pcslot *slotp, offset_t *offset)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	char fname[PCFNAMESIZE];
11457c478bd9Sstevel@tonic-gate 	char fext[PCFEXTSIZE];
11467c478bd9Sstevel@tonic-gate 	struct pcdir *ep = *epp;
11477c478bd9Sstevel@tonic-gate 	int	error;
11487c478bd9Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
11497c478bd9Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
11507c478bd9Sstevel@tonic-gate 	int boff = pc_blkoff(fsp, *offset);
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	if (PCA_IS_HIDDEN(fsp, ep->pcd_attr)) {
11537c478bd9Sstevel@tonic-gate 		*offset += sizeof (struct pcdir);
11547c478bd9Sstevel@tonic-gate 		ep++;
11557c478bd9Sstevel@tonic-gate 		*epp = ep;
11567c478bd9Sstevel@tonic-gate 		return (ENOENT);
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	error = pc_parsename(namep, fname, fext);
11607c478bd9Sstevel@tonic-gate 	if (error) {
11617c478bd9Sstevel@tonic-gate 		*offset += sizeof (struct pcdir);
11627c478bd9Sstevel@tonic-gate 		ep++;
11637c478bd9Sstevel@tonic-gate 		*epp = ep;
11647c478bd9Sstevel@tonic-gate 		return (error);
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	if ((bcmp(fname, ep->pcd_filename, PCFNAMESIZE) == 0) &&
11687c478bd9Sstevel@tonic-gate 	    (bcmp(fext, ep->pcd_ext, PCFEXTSIZE) == 0)) {
11697c478bd9Sstevel@tonic-gate 		/*
11707c478bd9Sstevel@tonic-gate 		 * found the file
11717c478bd9Sstevel@tonic-gate 		 */
11727c478bd9Sstevel@tonic-gate 		if (fname[0] == '.') {
11737c478bd9Sstevel@tonic-gate 			if (fname[1] == '.')
11747c478bd9Sstevel@tonic-gate 				slotp->sl_flags = SL_DOTDOT;
11757c478bd9Sstevel@tonic-gate 			else
11767c478bd9Sstevel@tonic-gate 				slotp->sl_flags = SL_DOT;
11777c478bd9Sstevel@tonic-gate 		} else {
11787c478bd9Sstevel@tonic-gate 			slotp->sl_flags = 0;
11797c478bd9Sstevel@tonic-gate 		}
11807c478bd9Sstevel@tonic-gate 		slotp->sl_blkno =
11817c478bd9Sstevel@tonic-gate 		    pc_daddrdb(fsp, slotp->sl_bp->b_blkno);
11827c478bd9Sstevel@tonic-gate 		slotp->sl_offset = boff;
11837c478bd9Sstevel@tonic-gate 		slotp->sl_ep = ep;
11847c478bd9Sstevel@tonic-gate 		return (0);
11857c478bd9Sstevel@tonic-gate 	}
11867c478bd9Sstevel@tonic-gate 	*offset += sizeof (struct pcdir);
11877c478bd9Sstevel@tonic-gate 	ep++;
11887c478bd9Sstevel@tonic-gate 	*epp = ep;
11897c478bd9Sstevel@tonic-gate 	return (ENOENT);
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate /*
11937c478bd9Sstevel@tonic-gate  * Remove a long filename entry starting at lfn_offset. It must be
11947c478bd9Sstevel@tonic-gate  * a valid entry or we wouldn't have gotten here. Also remove the
11957c478bd9Sstevel@tonic-gate  * short filename entry.
11967c478bd9Sstevel@tonic-gate  */
11977c478bd9Sstevel@tonic-gate static int
pc_remove_long_fn(struct pcnode * pcp,offset_t lfn_offset)11987c478bd9Sstevel@tonic-gate pc_remove_long_fn(struct pcnode *pcp, offset_t lfn_offset)
11997c478bd9Sstevel@tonic-gate {
12007c478bd9Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
12017c478bd9Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
12027c478bd9Sstevel@tonic-gate 	int boff;
12037c478bd9Sstevel@tonic-gate 	struct buf *bp = NULL;
12047c478bd9Sstevel@tonic-gate 	struct pcdir *ep = NULL;
12057c478bd9Sstevel@tonic-gate 	int	error = 0;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	/*
12087c478bd9Sstevel@tonic-gate 	 * if we're in here, we know that the lfn is in the proper format
12097c478bd9Sstevel@tonic-gate 	 * of <series-of-lfn-entries> followed by <sfn-entry>
12107c478bd9Sstevel@tonic-gate 	 */
12117c478bd9Sstevel@tonic-gate 	for (;;) {
12127c478bd9Sstevel@tonic-gate 		boff = pc_blkoff(fsp, lfn_offset);
12137c478bd9Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
12147c478bd9Sstevel@tonic-gate 			if (bp != NULL) {
12157c478bd9Sstevel@tonic-gate 				bwrite2(bp);
12167c478bd9Sstevel@tonic-gate 				error = geterror(bp);
12177c478bd9Sstevel@tonic-gate 				brelse(bp);
12187c478bd9Sstevel@tonic-gate 				if (error)
12197c478bd9Sstevel@tonic-gate 					return (error);
12207c478bd9Sstevel@tonic-gate 				bp = NULL;
12217c478bd9Sstevel@tonic-gate 			}
12227c478bd9Sstevel@tonic-gate 			error = pc_blkatoff(pcp, lfn_offset, &bp, &ep);
12237c478bd9Sstevel@tonic-gate 			if (error)
12247c478bd9Sstevel@tonic-gate 				return (error);
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 		if (!PCDL_IS_LFN(ep)) {
12277c478bd9Sstevel@tonic-gate 			/* done */
12287c478bd9Sstevel@tonic-gate 			break;
12297c478bd9Sstevel@tonic-gate 		}
12307c478bd9Sstevel@tonic-gate 		/* zap it */
12317c478bd9Sstevel@tonic-gate 		ep->pcd_filename[0] = PCD_ERASED;
12327c478bd9Sstevel@tonic-gate 		ep->pcd_attr = 0;
12337c478bd9Sstevel@tonic-gate 		lfn_offset += sizeof (struct pcdir);
12347c478bd9Sstevel@tonic-gate 		ep++;
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 	/* now we're on the short entry */
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	ep->pcd_filename[0] = PCD_ERASED;
12397c478bd9Sstevel@tonic-gate 	ep->pcd_attr = 0;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	if (bp != NULL) {
12427c478bd9Sstevel@tonic-gate 		bwrite2(bp);
12437c478bd9Sstevel@tonic-gate 		error = geterror(bp);
12447c478bd9Sstevel@tonic-gate 		brelse(bp);
12457c478bd9Sstevel@tonic-gate 		if (error)
12467c478bd9Sstevel@tonic-gate 			return (error);
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 	return (0);
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate /*
12527c478bd9Sstevel@tonic-gate  * Find (and allocate) space in the directory denoted by
12537c478bd9Sstevel@tonic-gate  * 'pcp'. for 'ndirentries' pcdir structures.
12547c478bd9Sstevel@tonic-gate  * Return the offset at which to start, or -1 for failure.
12557c478bd9Sstevel@tonic-gate  */
12567c478bd9Sstevel@tonic-gate static offset_t
pc_find_free_space(struct pcnode * pcp,int ndirentries)12577c478bd9Sstevel@tonic-gate pc_find_free_space(struct pcnode *pcp, int ndirentries)
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate 	offset_t offset = 0;
12607c478bd9Sstevel@tonic-gate 	offset_t spaceneeded = ndirentries * sizeof (struct pcdir);
12617c478bd9Sstevel@tonic-gate 	offset_t spaceoffset;
12627c478bd9Sstevel@tonic-gate 	offset_t spaceavail = 0;
12637c478bd9Sstevel@tonic-gate 	int boff;
12647c478bd9Sstevel@tonic-gate 	struct buf *bp = NULL;
12657c478bd9Sstevel@tonic-gate 	struct vnode *vp = PCTOV(pcp);
12667c478bd9Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
12677c478bd9Sstevel@tonic-gate 	struct pcdir *ep;
12687c478bd9Sstevel@tonic-gate 	int	error;
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	spaceoffset = offset;
12717c478bd9Sstevel@tonic-gate 	while (spaceneeded > spaceavail) {
12727c478bd9Sstevel@tonic-gate 		/*
12737c478bd9Sstevel@tonic-gate 		 * If offset is on a block boundary,
12747c478bd9Sstevel@tonic-gate 		 * read in the next directory block.
12757c478bd9Sstevel@tonic-gate 		 * Release previous if it exists.
12767c478bd9Sstevel@tonic-gate 		 */
12777c478bd9Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
12787c478bd9Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
12797c478bd9Sstevel@tonic-gate 			if (bp != NULL) {
12807c478bd9Sstevel@tonic-gate 				brelse(bp);
12817c478bd9Sstevel@tonic-gate 				bp = NULL;
12827c478bd9Sstevel@tonic-gate 			}
12837c478bd9Sstevel@tonic-gate 			error = pc_blkatoff(pcp, offset, &bp, &ep);
12847c478bd9Sstevel@tonic-gate 			if (error == ENOENT) {
12857c478bd9Sstevel@tonic-gate 				daddr_t bn;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 				/* extend directory */
12887c478bd9Sstevel@tonic-gate 				if (!IS_FAT32(fsp) && (vp->v_flag & VROOT))
12897c478bd9Sstevel@tonic-gate 					return (-1);
12907c478bd9Sstevel@tonic-gate 				while (spaceneeded > spaceavail) {
12917c478bd9Sstevel@tonic-gate 					error = pc_balloc(pcp,
12927c478bd9Sstevel@tonic-gate 					    pc_lblkno(fsp, offset), 1, &bn);
12937c478bd9Sstevel@tonic-gate 					if (error)
12947c478bd9Sstevel@tonic-gate 						return (-1);
12957c478bd9Sstevel@tonic-gate 					pcp->pc_size += fsp->pcfs_clsize;
12967c478bd9Sstevel@tonic-gate 					spaceavail += fsp->pcfs_clsize;
12977c478bd9Sstevel@tonic-gate 					offset += fsp->pcfs_clsize;
12987c478bd9Sstevel@tonic-gate 				}
12997c478bd9Sstevel@tonic-gate 				return (spaceoffset);
13007c478bd9Sstevel@tonic-gate 			}
13017c478bd9Sstevel@tonic-gate 			if (error)
13027c478bd9Sstevel@tonic-gate 				return (-1);
13037c478bd9Sstevel@tonic-gate 		}
13047c478bd9Sstevel@tonic-gate 		if ((ep->pcd_filename[0] == PCD_UNUSED) ||
13057c478bd9Sstevel@tonic-gate 		    (ep->pcd_filename[0] == PCD_ERASED)) {
13067c478bd9Sstevel@tonic-gate 			offset += sizeof (struct pcdir);
13077c478bd9Sstevel@tonic-gate 			spaceavail += sizeof (struct pcdir);
13087c478bd9Sstevel@tonic-gate 			ep++;
13097c478bd9Sstevel@tonic-gate 			continue;
13107c478bd9Sstevel@tonic-gate 		}
13117c478bd9Sstevel@tonic-gate 		offset += sizeof (struct pcdir);
13127c478bd9Sstevel@tonic-gate 		spaceavail = 0;
13137c478bd9Sstevel@tonic-gate 		spaceoffset = offset;
13147c478bd9Sstevel@tonic-gate 		ep++;
13157c478bd9Sstevel@tonic-gate 	}
13167c478bd9Sstevel@tonic-gate 	if (bp != NULL) {
13177c478bd9Sstevel@tonic-gate 		brelse(bp);
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 	return (spaceoffset);
13207c478bd9Sstevel@tonic-gate }
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate /*
13237c478bd9Sstevel@tonic-gate  * Return how many long filename entries are needed.
13247c478bd9Sstevel@tonic-gate  * A maximum of PCLFNCHUNKSIZE characters per entry, plus one for a
13257c478bd9Sstevel@tonic-gate  * short filename.
13267c478bd9Sstevel@tonic-gate  */
13277c478bd9Sstevel@tonic-gate static int
direntries_needed(struct pcnode * dp,char * namep)13287c478bd9Sstevel@tonic-gate direntries_needed(struct pcnode *dp, char *namep)
13297c478bd9Sstevel@tonic-gate {
13307c478bd9Sstevel@tonic-gate 	struct pcdir ep;
13314a37d755Sksn 	uint16_t *w2_str;
13324a37d755Sksn 	size_t  u8l, u16l;
13334a37d755Sksn 	int ret;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	if (enable_long_filenames == 0) {
13367c478bd9Sstevel@tonic-gate 		return (1);
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 	if (pc_is_short_file_name(namep, 0)) {
13397c478bd9Sstevel@tonic-gate 		(void) pc_parsename(namep, ep.pcd_filename, ep.pcd_ext);
13407c478bd9Sstevel@tonic-gate 		if (!shortname_exists(dp, ep.pcd_filename, ep.pcd_ext)) {
13417c478bd9Sstevel@tonic-gate 			return (1);
13427c478bd9Sstevel@tonic-gate 		}
13437c478bd9Sstevel@tonic-gate 	}
13444a37d755Sksn 	if (pc_valid_long_fn(namep, 1)) {
13454a37d755Sksn 		/*
13464a37d755Sksn 		 * convert to UTF-16 or UNICODE for calculating the entries
13474a37d755Sksn 		 * needed. Conversion will consume at the most 512 bytes
13484a37d755Sksn 		 */
13494a37d755Sksn 		u16l = PCMAXNAMLEN + 1;
13504a37d755Sksn 		w2_str = (uint16_t *)kmem_zalloc(PCMAXNAM_UTF16, KM_SLEEP);
13514a37d755Sksn 		u8l = strlen(namep);
13524a37d755Sksn 		ret = uconv_u8tou16((const uchar_t *)namep, &u8l,
13534a37d755Sksn 		    w2_str, &u16l, UCONV_OUT_LITTLE_ENDIAN);
13544a37d755Sksn 		kmem_free((caddr_t)w2_str, PCMAXNAM_UTF16);
13554a37d755Sksn 		if (ret == 0) {
13564a37d755Sksn 			ret = 1 + u16l / PCLFNCHUNKSIZE;
13574a37d755Sksn 			if (u16l % PCLFNCHUNKSIZE != 0)
13584a37d755Sksn 				ret++;
13594a37d755Sksn 			return (ret);
13604a37d755Sksn 		}
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 	return (-1);
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate /*
13667c478bd9Sstevel@tonic-gate  * Allocate and return an array of pcdir structures for the passed-in
13677c478bd9Sstevel@tonic-gate  * name. ndirentries tells how many are required (including the short
13687c478bd9Sstevel@tonic-gate  * filename entry). Just allocate and fill them in properly here so they
13697c478bd9Sstevel@tonic-gate  * can be written out.
13707c478bd9Sstevel@tonic-gate  */
13717c478bd9Sstevel@tonic-gate static struct pcdir *
pc_name_to_pcdir(struct pcnode * dp,char * namep,int ndirentries,int * errret)13727c478bd9Sstevel@tonic-gate pc_name_to_pcdir(struct pcnode *dp, char *namep, int ndirentries, int *errret)
13737c478bd9Sstevel@tonic-gate {
13747c478bd9Sstevel@tonic-gate 	struct pcdir *bpcdir;
13757c478bd9Sstevel@tonic-gate 	struct pcdir *ep;
13767c478bd9Sstevel@tonic-gate 	struct pcdir_lfn *lep;
13777c478bd9Sstevel@tonic-gate 	int	i;
13787c478bd9Sstevel@tonic-gate 	uchar_t	cksum;
13797c478bd9Sstevel@tonic-gate 	int	nchars;
13807c478bd9Sstevel@tonic-gate 	int	error = 0;
13817c478bd9Sstevel@tonic-gate 	char	*nameend;
13824a37d755Sksn 	uint16_t *w2_str;
13834a37d755Sksn 	size_t  u8l, u16l;
13844a37d755Sksn 	int ret;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	bpcdir = kmem_zalloc(ndirentries * sizeof (struct pcdir), KM_SLEEP);
13877c478bd9Sstevel@tonic-gate 	ep = &bpcdir[ndirentries - 1];
13887c478bd9Sstevel@tonic-gate 	if (ndirentries == 1) {
13897c478bd9Sstevel@tonic-gate 		(void) pc_parsename(namep, ep->pcd_filename, ep->pcd_ext);
13907c478bd9Sstevel@tonic-gate 		return (bpcdir);
13917c478bd9Sstevel@tonic-gate 	}
13924a37d755Sksn 
13934a37d755Sksn 	/* Here we need to convert to UTF-16 or UNICODE for writing */
13944a37d755Sksn 
13954a37d755Sksn 	u16l = PCMAXNAMLEN + 1;
13964a37d755Sksn 	w2_str = (uint16_t *)kmem_zalloc(PCMAXNAM_UTF16, KM_SLEEP);
13974a37d755Sksn 	u8l = strlen(namep);
13984a37d755Sksn 	ret = uconv_u8tou16((const uchar_t *)namep, &u8l, w2_str, &u16l,
13994a37d755Sksn 	    UCONV_OUT_LITTLE_ENDIAN);
14004a37d755Sksn 	if (ret != 0) {
14014a37d755Sksn 		kmem_free((caddr_t)w2_str, PCMAXNAM_UTF16);
14024a37d755Sksn 		*errret = ret;
14034a37d755Sksn 		return (NULL);
14047c478bd9Sstevel@tonic-gate 	}
14054a37d755Sksn 	nameend = (char *)(w2_str + u16l);
14064a37d755Sksn 	u16l %= PCLFNCHUNKSIZE;
14074a37d755Sksn 	if (u16l != 0) {
14084a37d755Sksn 		nchars = u16l + 1;
14094a37d755Sksn 		nameend += 2;
14104a37d755Sksn 	} else {
14114a37d755Sksn 		nchars = PCLFNCHUNKSIZE;
14124a37d755Sksn 	}
14134a37d755Sksn 	nchars *= sizeof (uint16_t);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	/* short file name */
14167c478bd9Sstevel@tonic-gate 	error = generate_short_name(dp, namep, ep);
14177c478bd9Sstevel@tonic-gate 	if (error) {
14187c478bd9Sstevel@tonic-gate 		kmem_free(bpcdir, ndirentries * sizeof (struct pcdir));
14197c478bd9Sstevel@tonic-gate 		*errret = error;
14207c478bd9Sstevel@tonic-gate 		return (NULL);
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 	cksum = pc_checksum_long_fn(ep->pcd_filename, ep->pcd_ext);
14237c478bd9Sstevel@tonic-gate 	for (i = 0; i < (ndirentries - 1); i++) {
14247c478bd9Sstevel@tonic-gate 		/* long file name */
14257c478bd9Sstevel@tonic-gate 		nameend -= nchars;
14267c478bd9Sstevel@tonic-gate 		lep = (struct pcdir_lfn *)&bpcdir[i];
14277c478bd9Sstevel@tonic-gate 		set_long_fn_chunk(lep, nameend, nchars);
14287c478bd9Sstevel@tonic-gate 		lep->pcdl_attr = PCDL_LFN_BITS;
14297c478bd9Sstevel@tonic-gate 		lep->pcdl_checksum = cksum;
14307c478bd9Sstevel@tonic-gate 		lep->pcdl_ordinal = (uchar_t)(ndirentries - i - 1);
14314a37d755Sksn 		nchars = PCLFNCHUNKSIZE * sizeof (uint16_t);
14327c478bd9Sstevel@tonic-gate 	}
14334a37d755Sksn 	kmem_free((caddr_t)w2_str, PCMAXNAM_UTF16);
14347c478bd9Sstevel@tonic-gate 	lep = (struct pcdir_lfn *)&bpcdir[0];
14357c478bd9Sstevel@tonic-gate 	lep->pcdl_ordinal |= 0x40;
14367c478bd9Sstevel@tonic-gate 	return (bpcdir);
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate static int
generate_short_name(struct pcnode * dp,char * namep,struct pcdir * inep)14407c478bd9Sstevel@tonic-gate generate_short_name(struct pcnode *dp, char *namep, struct pcdir *inep)
14417c478bd9Sstevel@tonic-gate {
14427c478bd9Sstevel@tonic-gate 	int	rev;
14437c478bd9Sstevel@tonic-gate 	int	nchars;
14447c478bd9Sstevel@tonic-gate 	int	i, j;
14457c478bd9Sstevel@tonic-gate 	char	*dot = NULL;
14467c478bd9Sstevel@tonic-gate 	char	fname[PCFNAMESIZE+1];
14477c478bd9Sstevel@tonic-gate 	char	fext[PCFEXTSIZE+1];
14487c478bd9Sstevel@tonic-gate 	char	scratch[8];
14497c478bd9Sstevel@tonic-gate 	int	error = 0;
14505b024a5bSbatschul 	struct	pcslot slot;
14517c478bd9Sstevel@tonic-gate 	char	shortname[20];
14527c478bd9Sstevel@tonic-gate 	int	force_tilde = 0;
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	/*
14557c478bd9Sstevel@tonic-gate 	 * generate a unique short file name based on the long input name.
14567c478bd9Sstevel@tonic-gate 	 *
14577c478bd9Sstevel@tonic-gate 	 * Say, for "This is a very long filename.txt" generate
14587c478bd9Sstevel@tonic-gate 	 * "THISIS~1.TXT", or "THISIS~2.TXT" if that's already there.
14597c478bd9Sstevel@tonic-gate 	 * Skip invalid short name characters in the long name, plus
14607c478bd9Sstevel@tonic-gate 	 * a couple NT skips (space and reverse backslash).
14617c478bd9Sstevel@tonic-gate 	 *
14627c478bd9Sstevel@tonic-gate 	 * Unfortunately, since this name would be hidden by the normal
14637c478bd9Sstevel@tonic-gate 	 * lookup routine, we need to look for it ourselves. But luckily
14647c478bd9Sstevel@tonic-gate 	 * we don't need to look at the lfn entries themselves.
14657c478bd9Sstevel@tonic-gate 	 */
14667c478bd9Sstevel@tonic-gate 	force_tilde = !pc_is_short_file_name(namep, 1);
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	/*
14697c478bd9Sstevel@tonic-gate 	 * Strip off leading invalid characters.
14707c478bd9Sstevel@tonic-gate 	 * We need this because names like '.login' are now ok, but the
14717c478bd9Sstevel@tonic-gate 	 * short name needs to be something like LOGIN~1.
14727c478bd9Sstevel@tonic-gate 	 */
14737c478bd9Sstevel@tonic-gate 	for (; *namep != '\0'; namep++) {
14747c478bd9Sstevel@tonic-gate 		if (*namep == ' ')
14757c478bd9Sstevel@tonic-gate 			continue;
14767c478bd9Sstevel@tonic-gate 		if (!pc_validchar(*namep) && !pc_validchar(toupper(*namep)))
14777c478bd9Sstevel@tonic-gate 			continue;
14787c478bd9Sstevel@tonic-gate 		break;
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate 	dot = strrchr(namep, '.');
14817c478bd9Sstevel@tonic-gate 	if (dot != NULL) {
14827c478bd9Sstevel@tonic-gate 		dot++;
14837c478bd9Sstevel@tonic-gate 		for (j = 0, i = 0; j < PCFEXTSIZE; i++) {
14847c478bd9Sstevel@tonic-gate 			if (dot[i] == '\0')
14857c478bd9Sstevel@tonic-gate 				break;
14867c478bd9Sstevel@tonic-gate 			/* skip valid, but not generally good characters */
14877c478bd9Sstevel@tonic-gate 			if (dot[i] == ' ' || dot[i] == '\\')
14887c478bd9Sstevel@tonic-gate 				continue;
14897c478bd9Sstevel@tonic-gate 			if (pc_validchar(dot[i]))
14907c478bd9Sstevel@tonic-gate 				fext[j++] = dot[i];
14917c478bd9Sstevel@tonic-gate 			else if (pc_validchar(toupper(dot[i])))
14927c478bd9Sstevel@tonic-gate 				fext[j++] = toupper(dot[i]);
14937c478bd9Sstevel@tonic-gate 		}
14947c478bd9Sstevel@tonic-gate 		for (i = j; i < PCFEXTSIZE; i++)
14957c478bd9Sstevel@tonic-gate 			fext[i] = ' ';
14967c478bd9Sstevel@tonic-gate 		dot--;
14977c478bd9Sstevel@tonic-gate 	} else {
14987c478bd9Sstevel@tonic-gate 		for (i = 0; i < PCFEXTSIZE; i++) {
14997c478bd9Sstevel@tonic-gate 			fext[i] = ' ';
15007c478bd9Sstevel@tonic-gate 		}
15017c478bd9Sstevel@tonic-gate 	}
15027c478bd9Sstevel@tonic-gate 	/*
15037c478bd9Sstevel@tonic-gate 	 * We know we're a long name, not a short name (or we wouldn't
15047c478bd9Sstevel@tonic-gate 	 * be here at all. But if uppercasing ourselves would be a short
15057c478bd9Sstevel@tonic-gate 	 * name, then we can possibly avoid the ~N format.
15067c478bd9Sstevel@tonic-gate 	 */
15077c478bd9Sstevel@tonic-gate 	if (!force_tilde)
15087c478bd9Sstevel@tonic-gate 		rev = 0;
15097c478bd9Sstevel@tonic-gate 	else
15107c478bd9Sstevel@tonic-gate 		rev = 1;
15117c478bd9Sstevel@tonic-gate 	for (;;) {
15127c478bd9Sstevel@tonic-gate 		bzero(fname, sizeof (fname));
15137c478bd9Sstevel@tonic-gate 		nchars = PCFNAMESIZE;
15147c478bd9Sstevel@tonic-gate 		if (rev) {
15157c478bd9Sstevel@tonic-gate 			nchars--; /* ~ */
15167c478bd9Sstevel@tonic-gate 			i = rev;
15177c478bd9Sstevel@tonic-gate 			do {
15187c478bd9Sstevel@tonic-gate 				nchars--;
15197c478bd9Sstevel@tonic-gate 				i /= 10;
15207c478bd9Sstevel@tonic-gate 			} while (i);
15217c478bd9Sstevel@tonic-gate 			if (nchars <= 0) {
15227c478bd9Sstevel@tonic-gate 				return (ENOSPC);
15237c478bd9Sstevel@tonic-gate 			}
15247c478bd9Sstevel@tonic-gate 		}
15257c478bd9Sstevel@tonic-gate 		for (j = 0, i = 0; j < nchars; i++) {
15267c478bd9Sstevel@tonic-gate 			if ((&namep[i] == dot) || (namep[i] == '\0'))
15277c478bd9Sstevel@tonic-gate 				break;
15287c478bd9Sstevel@tonic-gate 			/* skip valid, but not generally good characters */
15297c478bd9Sstevel@tonic-gate 			if (namep[i] == ' ' || namep[i] == '\\')
15307c478bd9Sstevel@tonic-gate 				continue;
15317c478bd9Sstevel@tonic-gate 			if (pc_validchar(namep[i]))
15327c478bd9Sstevel@tonic-gate 				fname[j++] = namep[i];
15337c478bd9Sstevel@tonic-gate 			else if (pc_validchar(toupper(namep[i])))
15347c478bd9Sstevel@tonic-gate 				fname[j++] = toupper(namep[i]);
15357c478bd9Sstevel@tonic-gate 		}
15367c478bd9Sstevel@tonic-gate 		if (rev) {
15377c478bd9Sstevel@tonic-gate 			(void) sprintf(scratch, "~%d", rev);
15387c478bd9Sstevel@tonic-gate 			(void) strcat(fname, scratch);
15397c478bd9Sstevel@tonic-gate 		}
15407c478bd9Sstevel@tonic-gate 		for (i = strlen(fname); i < PCFNAMESIZE; i++)
15417c478bd9Sstevel@tonic-gate 			fname[i] = ' ';
15427c478bd9Sstevel@tonic-gate 		/* now see if it exists */
15437c478bd9Sstevel@tonic-gate 		(void) pc_fname_ext_to_name(shortname, fname, fext, 0);
15447c478bd9Sstevel@tonic-gate 		error = pc_findentry(dp, shortname, &slot, NULL);
15457c478bd9Sstevel@tonic-gate 		if (error == 0) {
15467c478bd9Sstevel@tonic-gate 			/* found it */
15477c478bd9Sstevel@tonic-gate 			brelse(slot.sl_bp);
15487c478bd9Sstevel@tonic-gate 			rev++;
15497c478bd9Sstevel@tonic-gate 			continue;
15507c478bd9Sstevel@tonic-gate 		}
15517c478bd9Sstevel@tonic-gate 		if (!shortname_exists(dp, fname, fext))
15527c478bd9Sstevel@tonic-gate 			break;
15537c478bd9Sstevel@tonic-gate 		rev++;
15547c478bd9Sstevel@tonic-gate 	}
15557c478bd9Sstevel@tonic-gate 	(void) strncpy(inep->pcd_filename, fname, PCFNAMESIZE);
15567c478bd9Sstevel@tonic-gate 	(void) strncpy(inep->pcd_ext, fext, PCFEXTSIZE);
15577c478bd9Sstevel@tonic-gate 	return (0);
15587c478bd9Sstevel@tonic-gate }
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate /*
15617c478bd9Sstevel@tonic-gate  * Returns 1 if the passed-in filename is a short name, 0 if not.
15627c478bd9Sstevel@tonic-gate  */
15637c478bd9Sstevel@tonic-gate static int
pc_is_short_file_name(char * namep,int foldcase)15647c478bd9Sstevel@tonic-gate pc_is_short_file_name(char *namep, int foldcase)
15657c478bd9Sstevel@tonic-gate {
15667c478bd9Sstevel@tonic-gate 	int	i;
15677c478bd9Sstevel@tonic-gate 	char	c;
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	for (i = 0; i < PCFNAMESIZE; i++, namep++) {
15707c478bd9Sstevel@tonic-gate 		if (*namep == '\0')
15717c478bd9Sstevel@tonic-gate 			return (1);
15727c478bd9Sstevel@tonic-gate 		if (*namep == '.')
15737c478bd9Sstevel@tonic-gate 			break;
15747c478bd9Sstevel@tonic-gate 		if (foldcase)
15757c478bd9Sstevel@tonic-gate 			c = toupper(*namep);
15767c478bd9Sstevel@tonic-gate 		else
15777c478bd9Sstevel@tonic-gate 			c = *namep;
15787c478bd9Sstevel@tonic-gate 		if (!pc_validchar(c))
15797c478bd9Sstevel@tonic-gate 			return (0);
15807c478bd9Sstevel@tonic-gate 	}
15817c478bd9Sstevel@tonic-gate 	if (*namep == '\0')
15827c478bd9Sstevel@tonic-gate 		return (1);
15837c478bd9Sstevel@tonic-gate 	if (*namep != '.')
15847c478bd9Sstevel@tonic-gate 		return (0);
15857c478bd9Sstevel@tonic-gate 	namep++;
15867c478bd9Sstevel@tonic-gate 	for (i = 0; i < PCFEXTSIZE; i++, namep++) {
15877c478bd9Sstevel@tonic-gate 		if (*namep == '\0')
15887c478bd9Sstevel@tonic-gate 			return (1);
15897c478bd9Sstevel@tonic-gate 		if (foldcase)
15907c478bd9Sstevel@tonic-gate 			c = toupper(*namep);
15917c478bd9Sstevel@tonic-gate 		else
15927c478bd9Sstevel@tonic-gate 			c = *namep;
15937c478bd9Sstevel@tonic-gate 		if (!pc_validchar(c))
15947c478bd9Sstevel@tonic-gate 			return (0);
15957c478bd9Sstevel@tonic-gate 	}
15967c478bd9Sstevel@tonic-gate 	/* we should be done. If not... */
15977c478bd9Sstevel@tonic-gate 	if (*namep == '\0')
15987c478bd9Sstevel@tonic-gate 		return (1);
15997c478bd9Sstevel@tonic-gate 	return (0);
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate }
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate /*
16047c478bd9Sstevel@tonic-gate  * We call this when we want to see if a short filename already exists
16057c478bd9Sstevel@tonic-gate  * in the filesystem as part of a long filename. When creating a short
16067c478bd9Sstevel@tonic-gate  * name (FILENAME.TXT from the user, or when generating one for a long
16077c478bd9Sstevel@tonic-gate  * filename), we cannot allow one that is part of a long filename.
16087c478bd9Sstevel@tonic-gate  * pc_findentry will find all the names that are visible (long or short),
16097c478bd9Sstevel@tonic-gate  * but will not crack any long filename entries.
16107c478bd9Sstevel@tonic-gate  */
16117c478bd9Sstevel@tonic-gate static int
shortname_exists(struct pcnode * dp,char * fname,char * fext)16127c478bd9Sstevel@tonic-gate shortname_exists(struct pcnode *dp, char *fname, char *fext)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate 	struct buf *bp = NULL;
16157c478bd9Sstevel@tonic-gate 	int	offset = 0;
16167c478bd9Sstevel@tonic-gate 	int	match = 0;
16177c478bd9Sstevel@tonic-gate 	struct pcdir *ep;
16187c478bd9Sstevel@tonic-gate 	struct vnode *vp = PCTOV(dp);
16197c478bd9Sstevel@tonic-gate 	struct pcfs *fsp = VFSTOPCFS(vp->v_vfsp);
16207c478bd9Sstevel@tonic-gate 	int	boff;
16217c478bd9Sstevel@tonic-gate 	int	error = 0;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	for (;;) {
16247c478bd9Sstevel@tonic-gate 		boff = pc_blkoff(fsp, offset);
16257c478bd9Sstevel@tonic-gate 		if (boff == 0 || bp == NULL || boff >= bp->b_bcount) {
16267c478bd9Sstevel@tonic-gate 			if (bp != NULL) {
16277c478bd9Sstevel@tonic-gate 				brelse(bp);
16287c478bd9Sstevel@tonic-gate 				bp = NULL;
16297c478bd9Sstevel@tonic-gate 			}
16307c478bd9Sstevel@tonic-gate 			error = pc_blkatoff(dp, offset, &bp, &ep);
16317c478bd9Sstevel@tonic-gate 			if (error == ENOENT)
16327c478bd9Sstevel@tonic-gate 				break;
16337c478bd9Sstevel@tonic-gate 			if (error) {
16347c478bd9Sstevel@tonic-gate 				return (1);
16357c478bd9Sstevel@tonic-gate 			}
16367c478bd9Sstevel@tonic-gate 		}
16377c478bd9Sstevel@tonic-gate 		if (PCDL_IS_LFN(ep) ||
16387c478bd9Sstevel@tonic-gate 		    (ep->pcd_filename[0] == PCD_ERASED)) {
16397c478bd9Sstevel@tonic-gate 			offset += sizeof (struct pcdir);
16407c478bd9Sstevel@tonic-gate 			ep++;
16417c478bd9Sstevel@tonic-gate 			continue;
16427c478bd9Sstevel@tonic-gate 		}
16437c478bd9Sstevel@tonic-gate 		if (ep->pcd_filename[0] == PCD_UNUSED)
16447c478bd9Sstevel@tonic-gate 			break;
16457c478bd9Sstevel@tonic-gate 		/*
16467c478bd9Sstevel@tonic-gate 		 * in use, and a short file name (either standalone
16477c478bd9Sstevel@tonic-gate 		 * or associated with a long name
16487c478bd9Sstevel@tonic-gate 		 */
16497c478bd9Sstevel@tonic-gate 		if ((bcmp(fname, ep->pcd_filename, PCFNAMESIZE) == 0) &&
16507c478bd9Sstevel@tonic-gate 		    (bcmp(fext, ep->pcd_ext, PCFEXTSIZE) == 0)) {
16517c478bd9Sstevel@tonic-gate 			match = 1;
16527c478bd9Sstevel@tonic-gate 			break;
16537c478bd9Sstevel@tonic-gate 		}
16547c478bd9Sstevel@tonic-gate 		offset += sizeof (struct pcdir);
16557c478bd9Sstevel@tonic-gate 		ep++;
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 	if (bp) {
16587c478bd9Sstevel@tonic-gate 		brelse(bp);
16597c478bd9Sstevel@tonic-gate 		bp = NULL;
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 	return (match);
16627c478bd9Sstevel@tonic-gate }
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate pc_cluster32_t
pc_getstartcluster(struct pcfs * fsp,struct pcdir * ep)16657c478bd9Sstevel@tonic-gate pc_getstartcluster(struct pcfs *fsp, struct pcdir *ep)
16667c478bd9Sstevel@tonic-gate {
16677c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
16687c478bd9Sstevel@tonic-gate 		pc_cluster32_t cn;
16697c478bd9Sstevel@tonic-gate 		pc_cluster16_t hi16;
16707c478bd9Sstevel@tonic-gate 		pc_cluster16_t lo16;
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 		hi16 = ltohs(ep->un.pcd_scluster_hi);
16737c478bd9Sstevel@tonic-gate 		lo16 = ltohs(ep->pcd_scluster_lo);
16747c478bd9Sstevel@tonic-gate 		cn = (hi16 << 16) | lo16;
16757c478bd9Sstevel@tonic-gate 		return (cn);
16767c478bd9Sstevel@tonic-gate 	} else {
16777c478bd9Sstevel@tonic-gate 		return (ltohs(ep->pcd_scluster_lo));
16787c478bd9Sstevel@tonic-gate 	}
16797c478bd9Sstevel@tonic-gate }
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate void
pc_setstartcluster(struct pcfs * fsp,struct pcdir * ep,pc_cluster32_t cln)16827c478bd9Sstevel@tonic-gate pc_setstartcluster(struct pcfs *fsp, struct pcdir *ep, pc_cluster32_t cln)
16837c478bd9Sstevel@tonic-gate {
16847c478bd9Sstevel@tonic-gate 	if (IS_FAT32(fsp)) {
16857c478bd9Sstevel@tonic-gate 		pc_cluster16_t hi16;
16867c478bd9Sstevel@tonic-gate 		pc_cluster16_t lo16;
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 		hi16 = (cln >> 16) & 0xFFFF;
16897c478bd9Sstevel@tonic-gate 		lo16 = cln & 0xFFFF;
16907c478bd9Sstevel@tonic-gate 		ep->un.pcd_scluster_hi = htols(hi16);
16917c478bd9Sstevel@tonic-gate 		ep->pcd_scluster_lo = htols(lo16);
16927c478bd9Sstevel@tonic-gate 	} else {
16937c478bd9Sstevel@tonic-gate 		pc_cluster16_t cln16;
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 		cln16 = (pc_cluster16_t)cln;
16967c478bd9Sstevel@tonic-gate 		ep->pcd_scluster_lo = htols(cln16);
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate }
1699