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