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