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*2720Sfrankho * Common Development and Distribution License (the "License"). 6*2720Sfrankho * 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*2720Sfrankho * 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/t_lock.h> 300Sstevel@tonic-gate #include <sys/errno.h> 310Sstevel@tonic-gate #include <sys/sysmacros.h> 320Sstevel@tonic-gate #include <sys/buf.h> 330Sstevel@tonic-gate #include <sys/systm.h> 340Sstevel@tonic-gate #include <sys/vfs.h> 350Sstevel@tonic-gate #include <sys/vnode.h> 360Sstevel@tonic-gate #include <sys/kmem.h> 370Sstevel@tonic-gate #include <sys/proc.h> 380Sstevel@tonic-gate #include <sys/cred.h> 390Sstevel@tonic-gate #include <sys/cmn_err.h> 400Sstevel@tonic-gate #include <sys/debug.h> 410Sstevel@tonic-gate #include <vm/pvn.h> 420Sstevel@tonic-gate #include <sys/fs/pc_label.h> 430Sstevel@tonic-gate #include <sys/fs/pc_fs.h> 440Sstevel@tonic-gate #include <sys/fs/pc_dir.h> 450Sstevel@tonic-gate #include <sys/fs/pc_node.h> 460Sstevel@tonic-gate #include <sys/dirent.h> 470Sstevel@tonic-gate #include <sys/fdio.h> 480Sstevel@tonic-gate #include <sys/file.h> 490Sstevel@tonic-gate #include <sys/conf.h> 500Sstevel@tonic-gate 510Sstevel@tonic-gate struct pchead pcfhead[NPCHASH]; 520Sstevel@tonic-gate struct pchead pcdhead[NPCHASH]; 530Sstevel@tonic-gate 540Sstevel@tonic-gate extern krwlock_t pcnodes_lock; 550Sstevel@tonic-gate 560Sstevel@tonic-gate static int pc_getentryblock(struct pcnode *, struct buf **); 570Sstevel@tonic-gate static int syncpcp(struct pcnode *, int); 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* 600Sstevel@tonic-gate * fake entry for root directory, since this does not have a parent 610Sstevel@tonic-gate * pointing to it. 620Sstevel@tonic-gate */ 63*2720Sfrankho struct pcdir pcfs_rootdirentry = { 640Sstevel@tonic-gate "", 650Sstevel@tonic-gate "", 660Sstevel@tonic-gate PCA_DIR 670Sstevel@tonic-gate }; 680Sstevel@tonic-gate 690Sstevel@tonic-gate void 700Sstevel@tonic-gate pc_init(void) 710Sstevel@tonic-gate { 720Sstevel@tonic-gate struct pchead *hdp, *hfp; 730Sstevel@tonic-gate int i; 740Sstevel@tonic-gate for (i = 0; i < NPCHASH; i++) { 750Sstevel@tonic-gate hdp = &pcdhead[i]; 760Sstevel@tonic-gate hfp = &pcfhead[i]; 770Sstevel@tonic-gate hdp->pch_forw = (struct pcnode *)hdp; 780Sstevel@tonic-gate hdp->pch_back = (struct pcnode *)hdp; 790Sstevel@tonic-gate hfp->pch_forw = (struct pcnode *)hfp; 800Sstevel@tonic-gate hfp->pch_back = (struct pcnode *)hfp; 810Sstevel@tonic-gate } 820Sstevel@tonic-gate } 830Sstevel@tonic-gate 840Sstevel@tonic-gate struct pcnode * 850Sstevel@tonic-gate pc_getnode( 860Sstevel@tonic-gate struct pcfs *fsp, /* filsystem for node */ 870Sstevel@tonic-gate daddr_t blkno, /* phys block no of dir entry */ 880Sstevel@tonic-gate int offset, /* offset of dir entry in block */ 890Sstevel@tonic-gate struct pcdir *ep) /* node dir entry */ 900Sstevel@tonic-gate { 910Sstevel@tonic-gate struct pcnode *pcp; 920Sstevel@tonic-gate struct pchead *hp; 930Sstevel@tonic-gate struct vnode *vp; 940Sstevel@tonic-gate pc_cluster32_t scluster; 950Sstevel@tonic-gate 960Sstevel@tonic-gate ASSERT(fsp->pcfs_flags & PCFS_LOCKED); 970Sstevel@tonic-gate if (ep == (struct pcdir *)0) { 98*2720Sfrankho ep = &pcfs_rootdirentry; 990Sstevel@tonic-gate scluster = 0; 1000Sstevel@tonic-gate } else { 1010Sstevel@tonic-gate scluster = pc_getstartcluster(fsp, ep); 1020Sstevel@tonic-gate } 1030Sstevel@tonic-gate /* 1040Sstevel@tonic-gate * First look for active nodes. 1050Sstevel@tonic-gate * File nodes are identified by the location (blkno, offset) of 1060Sstevel@tonic-gate * its directory entry. 1070Sstevel@tonic-gate * Directory nodes are identified by the starting cluster number 1080Sstevel@tonic-gate * for the entries. 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate if (ep->pcd_attr & PCA_DIR) { 1110Sstevel@tonic-gate hp = &pcdhead[PCDHASH(fsp, scluster)]; 1120Sstevel@tonic-gate rw_enter(&pcnodes_lock, RW_READER); 1130Sstevel@tonic-gate for (pcp = hp->pch_forw; 1140Sstevel@tonic-gate pcp != (struct pcnode *)hp; pcp = pcp->pc_forw) { 1150Sstevel@tonic-gate if ((fsp == VFSTOPCFS(PCTOV(pcp)->v_vfsp)) && 1160Sstevel@tonic-gate (scluster == pcp->pc_scluster)) { 1170Sstevel@tonic-gate VN_HOLD(PCTOV(pcp)); 1180Sstevel@tonic-gate rw_exit(&pcnodes_lock); 1190Sstevel@tonic-gate return (pcp); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate rw_exit(&pcnodes_lock); 1230Sstevel@tonic-gate } else { 1240Sstevel@tonic-gate hp = &pcfhead[PCFHASH(fsp, blkno, offset)]; 1250Sstevel@tonic-gate rw_enter(&pcnodes_lock, RW_READER); 1260Sstevel@tonic-gate for (pcp = hp->pch_forw; 1270Sstevel@tonic-gate pcp != (struct pcnode *)hp; pcp = pcp->pc_forw) { 1280Sstevel@tonic-gate if ((fsp == VFSTOPCFS(PCTOV(pcp)->v_vfsp)) && 1290Sstevel@tonic-gate ((pcp->pc_flags & PC_INVAL) == 0) && 1300Sstevel@tonic-gate (blkno == pcp->pc_eblkno) && 1310Sstevel@tonic-gate (offset == pcp->pc_eoffset)) { 1320Sstevel@tonic-gate VN_HOLD(PCTOV(pcp)); 1330Sstevel@tonic-gate rw_exit(&pcnodes_lock); 1340Sstevel@tonic-gate return (pcp); 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate rw_exit(&pcnodes_lock); 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate /* 1400Sstevel@tonic-gate * Cannot find node in active list. Allocate memory for a new node 1410Sstevel@tonic-gate * initialize it, and put it on the active list. 1420Sstevel@tonic-gate */ 1430Sstevel@tonic-gate pcp = kmem_alloc(sizeof (struct pcnode), KM_SLEEP); 1440Sstevel@tonic-gate bzero(pcp, sizeof (struct pcnode)); 1450Sstevel@tonic-gate vp = vn_alloc(KM_SLEEP); 1460Sstevel@tonic-gate pcp->pc_vn = vp; 1470Sstevel@tonic-gate pcp->pc_entry = *ep; 1480Sstevel@tonic-gate pcp->pc_eblkno = blkno; 1490Sstevel@tonic-gate pcp->pc_eoffset = offset; 1500Sstevel@tonic-gate pcp->pc_scluster = scluster; 1510Sstevel@tonic-gate pcp->pc_lcluster = scluster; 1520Sstevel@tonic-gate pcp->pc_lindex = 0; 1530Sstevel@tonic-gate pcp->pc_flags = 0; 1540Sstevel@tonic-gate if (ep->pcd_attr & PCA_DIR) { 1550Sstevel@tonic-gate vn_setops(vp, pcfs_dvnodeops); 1560Sstevel@tonic-gate vp->v_type = VDIR; 1570Sstevel@tonic-gate if (scluster == 0) { 1580Sstevel@tonic-gate vp->v_flag = VROOT; 1590Sstevel@tonic-gate blkno = offset = 0; 1600Sstevel@tonic-gate if (IS_FAT32(fsp)) { 1610Sstevel@tonic-gate pcp->pc_size = pc_fileclsize(fsp, 1620Sstevel@tonic-gate fsp->pcfs_rdirstart) * fsp->pcfs_clsize; 1630Sstevel@tonic-gate } else { 1640Sstevel@tonic-gate pcp->pc_size = 1650Sstevel@tonic-gate fsp->pcfs_rdirsec * fsp->pcfs_secsize; 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate } else 1680Sstevel@tonic-gate pcp->pc_size = pc_fileclsize(fsp, scluster) * 1690Sstevel@tonic-gate fsp->pcfs_clsize; 1700Sstevel@tonic-gate } else { 1710Sstevel@tonic-gate vn_setops(vp, pcfs_fvnodeops); 1720Sstevel@tonic-gate vp->v_type = VREG; 1730Sstevel@tonic-gate vp->v_flag = VNOSWAP; 1740Sstevel@tonic-gate fsp->pcfs_frefs++; 1750Sstevel@tonic-gate pcp->pc_size = ltohi(ep->pcd_size); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate fsp->pcfs_nrefs++; 178*2720Sfrankho VFS_HOLD(PCFSTOVFS(fsp)); 1790Sstevel@tonic-gate vp->v_data = (caddr_t)pcp; 1800Sstevel@tonic-gate vp->v_vfsp = PCFSTOVFS(fsp); 1810Sstevel@tonic-gate vn_exists(vp); 1820Sstevel@tonic-gate rw_enter(&pcnodes_lock, RW_WRITER); 1830Sstevel@tonic-gate insque(pcp, hp); 1840Sstevel@tonic-gate rw_exit(&pcnodes_lock); 1850Sstevel@tonic-gate return (pcp); 1860Sstevel@tonic-gate } 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate int 1890Sstevel@tonic-gate syncpcp(struct pcnode *pcp, int flags) 1900Sstevel@tonic-gate { 1910Sstevel@tonic-gate int err; 1920Sstevel@tonic-gate if (!vn_has_cached_data(PCTOV(pcp))) 1930Sstevel@tonic-gate err = 0; 1940Sstevel@tonic-gate else 1950Sstevel@tonic-gate err = VOP_PUTPAGE(PCTOV(pcp), (offset_t)0, (uint_t)0, 1960Sstevel@tonic-gate flags, (struct cred *)0); 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate return (err); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate void 2020Sstevel@tonic-gate pc_rele(struct pcnode *pcp) 2030Sstevel@tonic-gate { 2040Sstevel@tonic-gate struct pcfs *fsp; 2050Sstevel@tonic-gate struct vnode *vp; 2060Sstevel@tonic-gate int err; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate vp = PCTOV(pcp); 2090Sstevel@tonic-gate PC_DPRINTF1(8, "pc_rele vp=0x%p\n", (void *)vp); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate fsp = VFSTOPCFS(vp->v_vfsp); 2120Sstevel@tonic-gate ASSERT(fsp->pcfs_flags & PCFS_LOCKED); 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate rw_enter(&pcnodes_lock, RW_WRITER); 2150Sstevel@tonic-gate pcp->pc_flags |= PC_RELEHOLD; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate retry: 2180Sstevel@tonic-gate if (vp->v_type != VDIR && (pcp->pc_flags & PC_INVAL) == 0) { 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * If the file was removed while active it may be safely 2210Sstevel@tonic-gate * truncated now. 2220Sstevel@tonic-gate */ 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate if (pcp->pc_entry.pcd_filename[0] == PCD_ERASED) { 2250Sstevel@tonic-gate (void) pc_truncate(pcp, 0); 2260Sstevel@tonic-gate } else if (pcp->pc_flags & PC_CHG) { 2270Sstevel@tonic-gate (void) pc_nodeupdate(pcp); 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate err = syncpcp(pcp, B_INVAL); 2300Sstevel@tonic-gate if (err) { 2310Sstevel@tonic-gate (void) syncpcp(pcp, B_INVAL|B_FORCE); 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate if (vn_has_cached_data(vp)) { 2350Sstevel@tonic-gate /* 2360Sstevel@tonic-gate * pvn_vplist_dirty will abort all old pages 2370Sstevel@tonic-gate */ 2380Sstevel@tonic-gate (void) pvn_vplist_dirty(vp, (u_offset_t)0, 2390Sstevel@tonic-gate pcfs_putapage, B_INVAL, (struct cred *)NULL); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate (void) pc_syncfat(fsp); 2430Sstevel@tonic-gate mutex_enter(&vp->v_lock); 2440Sstevel@tonic-gate if (vn_has_cached_data(vp)) { 2450Sstevel@tonic-gate mutex_exit(&vp->v_lock); 2460Sstevel@tonic-gate goto retry; 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate ASSERT(!vn_has_cached_data(vp)); 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate vp->v_count--; /* release our hold from vn_rele */ 2510Sstevel@tonic-gate if (vp->v_count > 0) { /* Is this check still needed? */ 2520Sstevel@tonic-gate PC_DPRINTF1(3, "pc_rele: pcp=0x%p HELD AGAIN!\n", (void *)pcp); 2530Sstevel@tonic-gate mutex_exit(&vp->v_lock); 2540Sstevel@tonic-gate pcp->pc_flags &= ~PC_RELEHOLD; 2550Sstevel@tonic-gate rw_exit(&pcnodes_lock); 2560Sstevel@tonic-gate return; 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate remque(pcp); 2600Sstevel@tonic-gate rw_exit(&pcnodes_lock); 2610Sstevel@tonic-gate if ((vp->v_type == VREG) && !(pcp->pc_flags & PC_INVAL)) { 2620Sstevel@tonic-gate fsp->pcfs_frefs--; 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate fsp->pcfs_nrefs--; 265*2720Sfrankho VFS_RELE(vp->v_vfsp); 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate if (fsp->pcfs_nrefs < 0) { 2680Sstevel@tonic-gate panic("pc_rele: nrefs count"); 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate if (fsp->pcfs_frefs < 0) { 2710Sstevel@tonic-gate panic("pc_rele: frefs count"); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate mutex_exit(&vp->v_lock); 2750Sstevel@tonic-gate vn_invalid(vp); 2760Sstevel@tonic-gate vn_free(vp); 2770Sstevel@tonic-gate kmem_free(pcp, sizeof (struct pcnode)); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate /* 2810Sstevel@tonic-gate * Mark a pcnode as modified with the current time. 2820Sstevel@tonic-gate */ 2830Sstevel@tonic-gate void 2840Sstevel@tonic-gate pc_mark_mod(struct pcnode *pcp) 2850Sstevel@tonic-gate { 2860Sstevel@tonic-gate timestruc_t now; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate if (PCTOV(pcp)->v_type == VREG) { 2890Sstevel@tonic-gate gethrestime(&now); 290*2720Sfrankho if (pc_tvtopct(&now, &pcp->pc_entry.pcd_mtime)) 291*2720Sfrankho PC_DPRINTF1(2, "pc_mark_mod failed timestamp " 292*2720Sfrankho "conversion, curtime = %lld\n", 293*2720Sfrankho (long long)now.tv_sec); 2940Sstevel@tonic-gate pcp->pc_flags |= PC_CHG; 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* 2990Sstevel@tonic-gate * Mark a pcnode as accessed with the current time. 3000Sstevel@tonic-gate */ 3010Sstevel@tonic-gate void 3020Sstevel@tonic-gate pc_mark_acc(struct pcnode *pcp) 3030Sstevel@tonic-gate { 304*2720Sfrankho struct pctime pt = { 0, 0 }; 3050Sstevel@tonic-gate timestruc_t now; 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate if (PCTOV(pcp)->v_type == VREG) { 3080Sstevel@tonic-gate gethrestime(&now); 309*2720Sfrankho if (pc_tvtopct(&now, &pcp->pc_entry.pcd_mtime)) 310*2720Sfrankho PC_DPRINTF1(2, "pc_mark_acc failed timestamp " 311*2720Sfrankho "conversion, curtime = %lld\n", 312*2720Sfrankho (long long)now.tv_sec); 3130Sstevel@tonic-gate pcp->pc_entry.pcd_ladate = pt.pct_date; 3140Sstevel@tonic-gate pcp->pc_flags |= PC_CHG; 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate /* 3190Sstevel@tonic-gate * Truncate a file to a length. 3200Sstevel@tonic-gate * Node must be locked. 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate int 3230Sstevel@tonic-gate pc_truncate(struct pcnode *pcp, uint_t length) 3240Sstevel@tonic-gate { 3250Sstevel@tonic-gate struct pcfs *fsp; 3260Sstevel@tonic-gate struct vnode *vp; 3270Sstevel@tonic-gate int error = 0; 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate PC_DPRINTF3(4, "pc_truncate pcp=0x%p, len=%u, size=%u\n", 3300Sstevel@tonic-gate (void *)pcp, length, pcp->pc_size); 3310Sstevel@tonic-gate vp = PCTOV(pcp); 3320Sstevel@tonic-gate if (pcp->pc_flags & PC_INVAL) 3330Sstevel@tonic-gate return (EIO); 3340Sstevel@tonic-gate fsp = VFSTOPCFS(vp->v_vfsp); 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * directories are always truncated to zero and are not marked 3370Sstevel@tonic-gate */ 3380Sstevel@tonic-gate if (vp->v_type == VDIR) { 3390Sstevel@tonic-gate error = pc_bfree(pcp, 0); 3400Sstevel@tonic-gate return (error); 3410Sstevel@tonic-gate } 3420Sstevel@tonic-gate /* 3430Sstevel@tonic-gate * If length is the same as the current size 3440Sstevel@tonic-gate * just mark the pcnode and return. 3450Sstevel@tonic-gate */ 3460Sstevel@tonic-gate if (length > pcp->pc_size) { 3470Sstevel@tonic-gate daddr_t bno; 3480Sstevel@tonic-gate uint_t llcn; 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate /* 3510Sstevel@tonic-gate * We are extending a file. 3520Sstevel@tonic-gate * Extend it with _one_ call to pc_balloc (no holes) 3530Sstevel@tonic-gate * since we don't need to use the block number(s). 3540Sstevel@tonic-gate */ 3550Sstevel@tonic-gate if ((daddr_t)howmany((offset_t)pcp->pc_size, fsp->pcfs_clsize) < 3560Sstevel@tonic-gate (llcn = (daddr_t)howmany((offset_t)length, 3570Sstevel@tonic-gate fsp->pcfs_clsize))) { 3580Sstevel@tonic-gate error = pc_balloc(pcp, (daddr_t)(llcn - 1), 1, &bno); 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate if (error) { 3610Sstevel@tonic-gate PC_DPRINTF1(2, "pc_truncate: error=%d\n", error); 3620Sstevel@tonic-gate /* 3630Sstevel@tonic-gate * probably ran out disk space; 3640Sstevel@tonic-gate * determine current file size 3650Sstevel@tonic-gate */ 3660Sstevel@tonic-gate pcp->pc_size = fsp->pcfs_clsize * 3670Sstevel@tonic-gate pc_fileclsize(fsp, pcp->pc_scluster); 3680Sstevel@tonic-gate } else 3690Sstevel@tonic-gate pcp->pc_size = length; 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate } else if (length < pcp->pc_size) { 3720Sstevel@tonic-gate /* 3730Sstevel@tonic-gate * We are shrinking a file. 3740Sstevel@tonic-gate * Free blocks after the block that length points to. 3750Sstevel@tonic-gate */ 3760Sstevel@tonic-gate if (pc_blkoff(fsp, length) == 0) { 3770Sstevel@tonic-gate /* 3780Sstevel@tonic-gate * Truncation to a block (cluster size) boundary only 3790Sstevel@tonic-gate * requires us to invalidate everything after the new 3800Sstevel@tonic-gate * end of the file. 3810Sstevel@tonic-gate */ 3820Sstevel@tonic-gate (void) pvn_vplist_dirty(PCTOV(pcp), (u_offset_t)length, 3830Sstevel@tonic-gate pcfs_putapage, B_INVAL | B_TRUNC, CRED()); 3840Sstevel@tonic-gate } else { 3850Sstevel@tonic-gate /* 3860Sstevel@tonic-gate * pvn_vpzero() cannot deal with more than MAXBSIZE 3870Sstevel@tonic-gate * chunks. Since the FAT clustersize can get larger 3880Sstevel@tonic-gate * than that, we'll zero from the new length to the 3890Sstevel@tonic-gate * end of the cluster for clustersizes smaller than 3900Sstevel@tonic-gate * MAXBSIZE - or the end of the MAXBSIZE block in 3910Sstevel@tonic-gate * case we've got a large clustersize. 3920Sstevel@tonic-gate */ 3930Sstevel@tonic-gate size_t nbytes = 3940Sstevel@tonic-gate roundup(length, MIN(fsp->pcfs_clsize, MAXBSIZE)) - 3950Sstevel@tonic-gate length; 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate pvn_vpzero(PCTOV(pcp), (u_offset_t)length, nbytes); 3980Sstevel@tonic-gate (void) pvn_vplist_dirty(PCTOV(pcp), 3990Sstevel@tonic-gate (u_offset_t)length + nbytes, 4000Sstevel@tonic-gate pcfs_putapage, B_INVAL | B_TRUNC, CRED()); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate error = pc_bfree(pcp, 4030Sstevel@tonic-gate (pc_cluster32_t)howmany((offset_t)length, 4040Sstevel@tonic-gate fsp->pcfs_clsize)); 4050Sstevel@tonic-gate pcp->pc_size = length; 4060Sstevel@tonic-gate } 4070Sstevel@tonic-gate pc_mark_mod(pcp); 4080Sstevel@tonic-gate return (error); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate /* 4120Sstevel@tonic-gate * Get block for entry. 4130Sstevel@tonic-gate */ 4140Sstevel@tonic-gate static int 4150Sstevel@tonic-gate pc_getentryblock(struct pcnode *pcp, struct buf **bpp) 4160Sstevel@tonic-gate { 4170Sstevel@tonic-gate struct pcfs *fsp; 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate PC_DPRINTF0(7, "pc_getentryblock "); 4200Sstevel@tonic-gate fsp = VFSTOPCFS(PCTOV(pcp)->v_vfsp); 4210Sstevel@tonic-gate if (pcp->pc_eblkno >= fsp->pcfs_datastart || 4220Sstevel@tonic-gate (pcp->pc_eblkno - fsp->pcfs_rdirstart) < 4230Sstevel@tonic-gate (fsp->pcfs_rdirsec & ~(fsp->pcfs_spcl - 1))) { 4240Sstevel@tonic-gate *bpp = bread(fsp->pcfs_xdev, 4250Sstevel@tonic-gate pc_dbdaddr(fsp, pcp->pc_eblkno), fsp->pcfs_clsize); 4260Sstevel@tonic-gate } else { 4270Sstevel@tonic-gate *bpp = bread(fsp->pcfs_xdev, 4280Sstevel@tonic-gate pc_dbdaddr(fsp, pcp->pc_eblkno), 4290Sstevel@tonic-gate (int)(fsp->pcfs_datastart-pcp->pc_eblkno) * 4300Sstevel@tonic-gate fsp->pcfs_secsize); 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate if ((*bpp)->b_flags & B_ERROR) { 4330Sstevel@tonic-gate PC_DPRINTF0(1, "pc_getentryblock: error "); 4340Sstevel@tonic-gate brelse(*bpp); 4350Sstevel@tonic-gate pc_mark_irrecov(fsp); 4360Sstevel@tonic-gate return (EIO); 4370Sstevel@tonic-gate } 4380Sstevel@tonic-gate return (0); 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate /* 4420Sstevel@tonic-gate * Sync all data associated with a file. 4430Sstevel@tonic-gate * Flush all the blocks in the buffer cache out to disk, sync the FAT and 4440Sstevel@tonic-gate * update the directory entry. 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate int 4470Sstevel@tonic-gate pc_nodesync(struct pcnode *pcp) 4480Sstevel@tonic-gate { 4490Sstevel@tonic-gate struct pcfs *fsp; 4500Sstevel@tonic-gate int err; 4510Sstevel@tonic-gate struct vnode *vp; 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate PC_DPRINTF1(7, "pc_nodesync pcp=0x%p\n", (void *)pcp); 4540Sstevel@tonic-gate vp = PCTOV(pcp); 4550Sstevel@tonic-gate fsp = VFSTOPCFS(vp->v_vfsp); 4560Sstevel@tonic-gate err = 0; 4570Sstevel@tonic-gate if (pcp->pc_flags & PC_MOD) { 4580Sstevel@tonic-gate /* 4590Sstevel@tonic-gate * Flush all data blocks from buffer cache and 4600Sstevel@tonic-gate * update the FAT which points to the data. 4610Sstevel@tonic-gate */ 4620Sstevel@tonic-gate if (err = syncpcp(pcp, 0)) { /* %% ?? how to handle error? */ 4630Sstevel@tonic-gate if (err == ENOMEM) 4640Sstevel@tonic-gate return (err); 4650Sstevel@tonic-gate else { 4660Sstevel@tonic-gate pc_mark_irrecov(fsp); 4670Sstevel@tonic-gate return (EIO); 4680Sstevel@tonic-gate } 4690Sstevel@tonic-gate } 4700Sstevel@tonic-gate pcp->pc_flags &= ~PC_MOD; 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate /* 4730Sstevel@tonic-gate * update the directory entry 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate if (pcp->pc_flags & PC_CHG) 4760Sstevel@tonic-gate (void) pc_nodeupdate(pcp); 4770Sstevel@tonic-gate return (err); 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * Update the node's directory entry. 4820Sstevel@tonic-gate */ 4830Sstevel@tonic-gate int 4840Sstevel@tonic-gate pc_nodeupdate(struct pcnode *pcp) 4850Sstevel@tonic-gate { 4860Sstevel@tonic-gate struct buf *bp; 4870Sstevel@tonic-gate int error; 4880Sstevel@tonic-gate struct vnode *vp; 4890Sstevel@tonic-gate struct pcfs *fsp; 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate vp = PCTOV(pcp); 4920Sstevel@tonic-gate fsp = VFSTOPCFS(vp->v_vfsp); 4930Sstevel@tonic-gate if (IS_FAT32(fsp) && (vp->v_flag & VROOT)) { 4940Sstevel@tonic-gate /* no node to update */ 4950Sstevel@tonic-gate pcp->pc_flags &= ~(PC_CHG | PC_MOD | PC_ACC); 4960Sstevel@tonic-gate return (0); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate if (vp->v_flag & VROOT) { 4990Sstevel@tonic-gate panic("pc_nodeupdate"); 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate if (pcp->pc_flags & PC_INVAL) 5020Sstevel@tonic-gate return (0); 5030Sstevel@tonic-gate PC_DPRINTF3(7, "pc_nodeupdate pcp=0x%p, bn=%ld, off=%d\n", (void *)pcp, 5040Sstevel@tonic-gate pcp->pc_eblkno, pcp->pc_eoffset); 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate if (error = pc_getentryblock(pcp, &bp)) { 5070Sstevel@tonic-gate return (error); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate if (vp->v_type == VREG) { 5100Sstevel@tonic-gate if (pcp->pc_flags & PC_CHG) 5110Sstevel@tonic-gate pcp->pc_entry.pcd_attr |= PCA_ARCH; 5120Sstevel@tonic-gate pcp->pc_entry.pcd_size = htoli(pcp->pc_size); 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate pc_setstartcluster(fsp, &pcp->pc_entry, pcp->pc_scluster); 5150Sstevel@tonic-gate *((struct pcdir *)(bp->b_un.b_addr + pcp->pc_eoffset)) = pcp->pc_entry; 5160Sstevel@tonic-gate bwrite2(bp); 5170Sstevel@tonic-gate error = geterror(bp); 5180Sstevel@tonic-gate if (error) 5190Sstevel@tonic-gate error = EIO; 5200Sstevel@tonic-gate brelse(bp); 5210Sstevel@tonic-gate if (error) { 5220Sstevel@tonic-gate PC_DPRINTF0(1, "pc_nodeupdate ERROR\n"); 5230Sstevel@tonic-gate pc_mark_irrecov(VFSTOPCFS(vp->v_vfsp)); 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate pcp->pc_flags &= ~(PC_CHG | PC_MOD | PC_ACC); 5260Sstevel@tonic-gate return (error); 5270Sstevel@tonic-gate } 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate /* 5300Sstevel@tonic-gate * Verify that the disk in the drive is the same one that we 5310Sstevel@tonic-gate * got the pcnode from. 5320Sstevel@tonic-gate * MUST be called with node unlocked. 5330Sstevel@tonic-gate */ 5340Sstevel@tonic-gate /* ARGSUSED */ 5350Sstevel@tonic-gate int 5360Sstevel@tonic-gate pc_verify(struct pcfs *fsp) 5370Sstevel@tonic-gate { 5380Sstevel@tonic-gate int fdstatus = 0; 5390Sstevel@tonic-gate int error = 0; 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate if (!fsp || fsp->pcfs_flags & PCFS_IRRECOV) 5420Sstevel@tonic-gate return (EIO); 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate if (!(fsp->pcfs_flags & PCFS_NOCHK) && fsp->pcfs_fatp) { 5450Sstevel@tonic-gate PC_DPRINTF1(4, "pc_verify fsp=0x%p\n", (void *)fsp); 5460Sstevel@tonic-gate error = cdev_ioctl(fsp->pcfs_vfs->vfs_dev, 5470Sstevel@tonic-gate FDGETCHANGE, (intptr_t)&fdstatus, FNATIVE|FKIOCTL, 5480Sstevel@tonic-gate NULL, NULL); 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate if (error) { 5510Sstevel@tonic-gate if (error == ENOTTY || error == ENXIO) { 5520Sstevel@tonic-gate error = 0; 5530Sstevel@tonic-gate } else { 5540Sstevel@tonic-gate PC_DPRINTF1(1, 5550Sstevel@tonic-gate "pc_verify: FDGETCHANGE ioctl failed: %d\n", 5560Sstevel@tonic-gate error); 5570Sstevel@tonic-gate pc_mark_irrecov(fsp); 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate } else if (fsp->pcfs_fatjustread) { 5600Sstevel@tonic-gate /* 5610Sstevel@tonic-gate * Ignore the results of the ioctl if we just 5620Sstevel@tonic-gate * read the FAT. There is a good chance that 5630Sstevel@tonic-gate * the disk changed bit will be on, because 5640Sstevel@tonic-gate * we've just mounted and we don't want to 5650Sstevel@tonic-gate * give a false positive that the sky is falling. 5660Sstevel@tonic-gate */ 5670Sstevel@tonic-gate fsp->pcfs_fatjustread = 0; 5680Sstevel@tonic-gate } else { 5690Sstevel@tonic-gate /* 5700Sstevel@tonic-gate * Oddly enough we can't check just one flag here. The 5710Sstevel@tonic-gate * x86 floppy driver sets a different flag 5720Sstevel@tonic-gate * (FDGC_DETECTED) than the sparc driver does. 5730Sstevel@tonic-gate * I think this MAY be a bug, and I filed 4165938 5740Sstevel@tonic-gate * to get someone to look at the behavior 5750Sstevel@tonic-gate * a bit more closely. In the meantime, my testing and 5760Sstevel@tonic-gate * code examination seem to indicate it is safe to 5770Sstevel@tonic-gate * check for either bit being set. 5780Sstevel@tonic-gate */ 5790Sstevel@tonic-gate if (fdstatus & (FDGC_HISTORY | FDGC_DETECTED)) { 5800Sstevel@tonic-gate PC_DPRINTF0(1, "pc_verify: change detected\n"); 5810Sstevel@tonic-gate pc_mark_irrecov(fsp); 5820Sstevel@tonic-gate } 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate if (!(error || fsp->pcfs_fatp)) { 5860Sstevel@tonic-gate error = pc_getfat(fsp); 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate return (error); 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate /* 5930Sstevel@tonic-gate * The disk has changed, pulling the rug out from beneath us. 5940Sstevel@tonic-gate * Mark the FS as being in an irrecoverable state. 5950Sstevel@tonic-gate * In a short while we'll clean up. 5960Sstevel@tonic-gate */ 5970Sstevel@tonic-gate void 5980Sstevel@tonic-gate pc_mark_irrecov(struct pcfs *fsp) 5990Sstevel@tonic-gate { 6000Sstevel@tonic-gate if (!(fsp->pcfs_flags & PCFS_NOCHK)) { 6010Sstevel@tonic-gate if (pc_lockfs(fsp, 1, 0)) { 6020Sstevel@tonic-gate /* 6030Sstevel@tonic-gate * Locking failed, which currently would 6040Sstevel@tonic-gate * only happen if the FS were already 6050Sstevel@tonic-gate * marked as hosed. If another reason for 6060Sstevel@tonic-gate * failure were to arise in the future, this 6070Sstevel@tonic-gate * routine would have to change. 6080Sstevel@tonic-gate */ 6090Sstevel@tonic-gate return; 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate fsp->pcfs_flags |= PCFS_IRRECOV; 6130Sstevel@tonic-gate cmn_err(CE_WARN, 6140Sstevel@tonic-gate "Disk was changed during an update or\n" 6150Sstevel@tonic-gate "an irrecoverable error was encountered.\n" 6160Sstevel@tonic-gate "File damage is possible. To prevent further\n" 6170Sstevel@tonic-gate "damage, this pcfs instance will now be frozen.\n" 6180Sstevel@tonic-gate "Use umount(1M) to release the instance.\n"); 6190Sstevel@tonic-gate (void) pc_unlockfs(fsp); 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate /* 6240Sstevel@tonic-gate * The disk has been changed! 6250Sstevel@tonic-gate */ 6260Sstevel@tonic-gate void 6270Sstevel@tonic-gate pc_diskchanged(struct pcfs *fsp) 6280Sstevel@tonic-gate { 629*2720Sfrankho struct pcnode *pcp, *npcp = NULL; 630*2720Sfrankho struct pchead *hp; 631*2720Sfrankho struct vnode *vp; 632*2720Sfrankho extern vfs_t EIO_vfs; 633*2720Sfrankho struct vfs *vfsp; 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate /* 6360Sstevel@tonic-gate * Eliminate all pcnodes (dir & file) associated with this fs. 6370Sstevel@tonic-gate * If the node is internal, ie, no references outside of 6380Sstevel@tonic-gate * pcfs itself, then release the associated vnode structure. 6390Sstevel@tonic-gate * Invalidate the in core FAT. 6400Sstevel@tonic-gate * Invalidate cached data blocks and blocks waiting for I/O. 6410Sstevel@tonic-gate */ 6420Sstevel@tonic-gate PC_DPRINTF1(1, "pc_diskchanged fsp=0x%p\n", (void *)fsp); 6430Sstevel@tonic-gate 644*2720Sfrankho vfsp = PCFSTOVFS(fsp); 645*2720Sfrankho 6460Sstevel@tonic-gate for (hp = pcdhead; hp < &pcdhead[NPCHASH]; hp++) { 6470Sstevel@tonic-gate for (pcp = hp->pch_forw; 6480Sstevel@tonic-gate pcp != (struct pcnode *)hp; pcp = npcp) { 6490Sstevel@tonic-gate npcp = pcp -> pc_forw; 6500Sstevel@tonic-gate vp = PCTOV(pcp); 651*2720Sfrankho if ((vp->v_vfsp == vfsp) && 6520Sstevel@tonic-gate !(pcp->pc_flags & PC_RELEHOLD)) { 6530Sstevel@tonic-gate mutex_enter(&(vp)->v_lock); 6540Sstevel@tonic-gate if (vp->v_count > 0) { 6550Sstevel@tonic-gate mutex_exit(&(vp)->v_lock); 6560Sstevel@tonic-gate continue; 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate mutex_exit(&(vp)->v_lock); 6590Sstevel@tonic-gate VN_HOLD(vp); 6600Sstevel@tonic-gate remque(pcp); 6610Sstevel@tonic-gate vp->v_data = NULL; 6620Sstevel@tonic-gate vp->v_vfsp = &EIO_vfs; 6630Sstevel@tonic-gate vp->v_type = VBAD; 6640Sstevel@tonic-gate VN_RELE(vp); 665*2720Sfrankho if (!(pcp->pc_flags & PC_EXTERNAL)) { 666*2720Sfrankho (void) pvn_vplist_dirty(vp, 667*2720Sfrankho (u_offset_t)0, pcfs_putapage, 668*2720Sfrankho B_INVAL | B_TRUNC, 669*2720Sfrankho (struct cred *)NULL); 6700Sstevel@tonic-gate vn_free(vp); 671*2720Sfrankho } 6720Sstevel@tonic-gate kmem_free(pcp, sizeof (struct pcnode)); 6730Sstevel@tonic-gate fsp->pcfs_nrefs --; 674*2720Sfrankho VFS_RELE(vfsp); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate for (hp = pcfhead; fsp->pcfs_frefs && hp < &pcfhead[NPCHASH]; hp++) { 6790Sstevel@tonic-gate for (pcp = hp->pch_forw; fsp->pcfs_frefs && 6800Sstevel@tonic-gate pcp != (struct pcnode *)hp; pcp = npcp) { 6810Sstevel@tonic-gate npcp = pcp -> pc_forw; 6820Sstevel@tonic-gate vp = PCTOV(pcp); 683*2720Sfrankho if ((vp->v_vfsp == vfsp) && 6840Sstevel@tonic-gate !(pcp->pc_flags & PC_RELEHOLD)) { 6850Sstevel@tonic-gate mutex_enter(&(vp)->v_lock); 6860Sstevel@tonic-gate if (vp->v_count > 0) { 6870Sstevel@tonic-gate mutex_exit(&(vp)->v_lock); 6880Sstevel@tonic-gate continue; 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate mutex_exit(&(vp)->v_lock); 6910Sstevel@tonic-gate VN_HOLD(vp); 6920Sstevel@tonic-gate remque(pcp); 6930Sstevel@tonic-gate vp->v_data = NULL; 6940Sstevel@tonic-gate vp->v_vfsp = &EIO_vfs; 6950Sstevel@tonic-gate vp->v_type = VBAD; 6960Sstevel@tonic-gate VN_RELE(vp); 697*2720Sfrankho if (!(pcp->pc_flags & PC_EXTERNAL)) { 698*2720Sfrankho (void) pvn_vplist_dirty(vp, 699*2720Sfrankho (u_offset_t)0, pcfs_putapage, 700*2720Sfrankho B_INVAL | B_TRUNC, 701*2720Sfrankho (struct cred *)NULL); 7020Sstevel@tonic-gate vn_free(vp); 703*2720Sfrankho } 7040Sstevel@tonic-gate kmem_free(pcp, sizeof (struct pcnode)); 7050Sstevel@tonic-gate fsp->pcfs_frefs --; 7060Sstevel@tonic-gate fsp->pcfs_nrefs --; 707*2720Sfrankho VFS_RELE(vfsp); 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate #ifdef undef 7120Sstevel@tonic-gate if (fsp->pcfs_frefs) { 7130Sstevel@tonic-gate rw_exit(&pcnodes_lock); 7140Sstevel@tonic-gate panic("pc_diskchanged: frefs"); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate if (fsp->pcfs_nrefs) { 7170Sstevel@tonic-gate rw_exit(&pcnodes_lock); 7180Sstevel@tonic-gate panic("pc_diskchanged: nrefs"); 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate #endif 721*2720Sfrankho if (!(vfsp->vfs_flag & VFS_UNMOUNTED) && 722*2720Sfrankho fsp->pcfs_fatp != (uchar_t *)0) { 7230Sstevel@tonic-gate pc_invalfat(fsp); 7240Sstevel@tonic-gate } else { 7250Sstevel@tonic-gate binval(fsp->pcfs_xdev); 7260Sstevel@tonic-gate } 7270Sstevel@tonic-gate } 728