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 52720Sfrankho * Common Development and Distribution License (the "License"). 62720Sfrankho * 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*5121Sfrankho * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Routines to allocate and deallocate data blocks on the disk 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/param.h> 330Sstevel@tonic-gate #include <sys/errno.h> 340Sstevel@tonic-gate #include <sys/buf.h> 350Sstevel@tonic-gate #include <sys/vfs.h> 360Sstevel@tonic-gate #include <sys/vnode.h> 370Sstevel@tonic-gate #include <sys/cmn_err.h> 380Sstevel@tonic-gate #include <sys/debug.h> 390Sstevel@tonic-gate #include <sys/sysmacros.h> 400Sstevel@tonic-gate #include <sys/systm.h> 410Sstevel@tonic-gate #include <sys/fs/pc_label.h> 420Sstevel@tonic-gate #include <sys/fs/pc_fs.h> 430Sstevel@tonic-gate #include <sys/fs/pc_dir.h> 440Sstevel@tonic-gate #include <sys/fs/pc_node.h> 450Sstevel@tonic-gate 460Sstevel@tonic-gate static pc_cluster32_t pc_getcluster(struct pcfs *fsp, pc_cluster32_t cn); 470Sstevel@tonic-gate 480Sstevel@tonic-gate /* 490Sstevel@tonic-gate * Convert file logical block (cluster) numbers to disk block numbers. 500Sstevel@tonic-gate * Also return number of physically contiguous blocks if asked for. 510Sstevel@tonic-gate * Used for reading only. Use pc_balloc for writing. 520Sstevel@tonic-gate */ 530Sstevel@tonic-gate int 540Sstevel@tonic-gate pc_bmap( 550Sstevel@tonic-gate struct pcnode *pcp, /* pcnode for file */ 560Sstevel@tonic-gate daddr_t lcn, /* logical cluster no */ 570Sstevel@tonic-gate daddr_t *dbnp, /* ptr to phys block no */ 580Sstevel@tonic-gate uint_t *contigbp) /* ptr to number of contiguous bytes */ 590Sstevel@tonic-gate /* may be zero if not wanted */ 600Sstevel@tonic-gate { 610Sstevel@tonic-gate struct pcfs *fsp; /* pcfs that file is in */ 620Sstevel@tonic-gate struct vnode *vp; 630Sstevel@tonic-gate pc_cluster32_t cn, ncn; /* current, next cluster number */ 640Sstevel@tonic-gate daddr_t olcn = lcn; 650Sstevel@tonic-gate 660Sstevel@tonic-gate vp = PCTOV(pcp); 670Sstevel@tonic-gate fsp = VFSTOPCFS(vp->v_vfsp); 68*5121Sfrankho 690Sstevel@tonic-gate if (lcn < 0) 700Sstevel@tonic-gate return (ENOENT); 71*5121Sfrankho 72*5121Sfrankho /* 73*5121Sfrankho * FAT12 / FAT16 root directories are a continuous section on disk 74*5121Sfrankho * before the actual data clusters. Specialcase this here. 75*5121Sfrankho */ 760Sstevel@tonic-gate if (!IS_FAT32(fsp) && (vp->v_flag & VROOT)) { 77*5121Sfrankho daddr_t lbn; /* logical (disk) block number */ 780Sstevel@tonic-gate 790Sstevel@tonic-gate lbn = pc_cltodb(fsp, lcn); 800Sstevel@tonic-gate if (lbn >= fsp->pcfs_rdirsec) { 810Sstevel@tonic-gate PC_DPRINTF0(2, "pc_bmap: ENOENT1\n"); 820Sstevel@tonic-gate return (ENOENT); 830Sstevel@tonic-gate } 84*5121Sfrankho *dbnp = pc_dbdaddr(fsp, fsp->pcfs_rdirstart + lbn); 850Sstevel@tonic-gate if (contigbp) { 860Sstevel@tonic-gate ASSERT (*contigbp >= fsp->pcfs_secsize); 870Sstevel@tonic-gate *contigbp = MIN(*contigbp, 880Sstevel@tonic-gate fsp->pcfs_secsize * (fsp->pcfs_rdirsec - lbn)); 890Sstevel@tonic-gate } 90*5121Sfrankho return (0); 91*5121Sfrankho } 92*5121Sfrankho 93*5121Sfrankho if (lcn >= fsp->pcfs_ncluster) { 94*5121Sfrankho PC_DPRINTF0(2, "pc_bmap: ENOENT2\n"); 95*5121Sfrankho return (ENOENT); 96*5121Sfrankho } 97*5121Sfrankho if (vp->v_type == VREG && 98*5121Sfrankho (pcp->pc_size == 0 || 99*5121Sfrankho lcn >= (daddr_t)howmany((offset_t)pcp->pc_size, 100*5121Sfrankho fsp->pcfs_clsize))) { 101*5121Sfrankho PC_DPRINTF0(2, "pc_bmap: ENOENT3\n"); 102*5121Sfrankho return (ENOENT); 103*5121Sfrankho } 104*5121Sfrankho ncn = pcp->pc_scluster; 105*5121Sfrankho if (IS_FAT32(fsp) && ncn == 0) 106*5121Sfrankho ncn = fsp->pcfs_rdirstart; 1070Sstevel@tonic-gate 108*5121Sfrankho /* Do we have a cached index/cluster pair? */ 109*5121Sfrankho if (pcp->pc_lindex > 0 && lcn >= pcp->pc_lindex) { 110*5121Sfrankho lcn -= pcp->pc_lindex; 111*5121Sfrankho ncn = pcp->pc_lcluster; 112*5121Sfrankho } 113*5121Sfrankho do { 114*5121Sfrankho cn = ncn; 115*5121Sfrankho if (!pc_validcl(fsp, cn)) { 116*5121Sfrankho if (IS_FAT32(fsp) && cn >= PCF_LASTCLUSTER32 && 117*5121Sfrankho vp->v_type == VDIR) { 118*5121Sfrankho PC_DPRINTF0(2, "pc_bmap: ENOENT4\n"); 119*5121Sfrankho return (ENOENT); 120*5121Sfrankho } else if (!IS_FAT32(fsp) && 121*5121Sfrankho cn >= PCF_LASTCLUSTER && 122*5121Sfrankho vp->v_type == VDIR) { 123*5121Sfrankho PC_DPRINTF0(2, "pc_bmap: ENOENT5\n"); 124*5121Sfrankho return (ENOENT); 125*5121Sfrankho } else { 126*5121Sfrankho PC_DPRINTF1(1, 127*5121Sfrankho "pc_bmap: badfs cn=%d\n", cn); 128*5121Sfrankho (void) pc_badfs(fsp); 129*5121Sfrankho return (EIO); 130*5121Sfrankho } 1310Sstevel@tonic-gate } 132*5121Sfrankho ncn = pc_getcluster(fsp, cn); 133*5121Sfrankho } while (lcn--); 1340Sstevel@tonic-gate 135*5121Sfrankho /* 136*5121Sfrankho * Cache this cluster, as we'll most likely visit the 137*5121Sfrankho * one after this next time. Considerably improves 138*5121Sfrankho * performance on sequential reads and writes. 139*5121Sfrankho */ 140*5121Sfrankho pcp->pc_lindex = olcn; 141*5121Sfrankho pcp->pc_lcluster = cn; 142*5121Sfrankho *dbnp = pc_cldaddr(fsp, cn); 143*5121Sfrankho 144*5121Sfrankho if (contigbp && *contigbp > fsp->pcfs_clsize) { 145*5121Sfrankho uint_t count = fsp->pcfs_clsize; 146*5121Sfrankho 147*5121Sfrankho while ((cn + 1) == ncn && count < *contigbp && 148*5121Sfrankho pc_validcl(fsp, ncn)) { 149*5121Sfrankho count += fsp->pcfs_clsize; 1500Sstevel@tonic-gate cn = ncn; 151*5121Sfrankho ncn = pc_getcluster(fsp, ncn); 1520Sstevel@tonic-gate } 153*5121Sfrankho *contigbp = count; 1540Sstevel@tonic-gate } 1550Sstevel@tonic-gate return (0); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate /* 1590Sstevel@tonic-gate * Allocate file logical blocks (clusters). 1600Sstevel@tonic-gate * Return disk address of last allocated cluster. 1610Sstevel@tonic-gate */ 1620Sstevel@tonic-gate int 1630Sstevel@tonic-gate pc_balloc( 1640Sstevel@tonic-gate struct pcnode *pcp, /* pcnode for file */ 1650Sstevel@tonic-gate daddr_t lcn, /* logical cluster no */ 1660Sstevel@tonic-gate int zwrite, /* zerofill blocks? */ 1670Sstevel@tonic-gate daddr_t *dbnp) /* ptr to phys block no */ 1680Sstevel@tonic-gate { 1690Sstevel@tonic-gate struct pcfs *fsp; /* pcfs that file is in */ 1700Sstevel@tonic-gate struct vnode *vp; 171*5121Sfrankho pc_cluster32_t cn; /* current cluster number */ 172*5121Sfrankho pc_cluster32_t ncn; /* next cluster number */ 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate vp = PCTOV(pcp); 1750Sstevel@tonic-gate fsp = VFSTOPCFS(vp -> v_vfsp); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate if (lcn < 0) { 1780Sstevel@tonic-gate return (EFBIG); 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 181*5121Sfrankho /* 182*5121Sfrankho * Again, FAT12/FAT16 root directories are not data clusters. 183*5121Sfrankho */ 1840Sstevel@tonic-gate if (!IS_FAT32(fsp) && (vp->v_flag & VROOT)) { 1850Sstevel@tonic-gate daddr_t lbn; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate lbn = pc_cltodb(fsp, lcn); 1880Sstevel@tonic-gate if (lbn >= fsp->pcfs_rdirsec) 1890Sstevel@tonic-gate return (ENOSPC); 1900Sstevel@tonic-gate *dbnp = pc_dbdaddr(fsp, fsp->pcfs_rdirstart + lbn); 191*5121Sfrankho return (0); 192*5121Sfrankho } 1930Sstevel@tonic-gate 194*5121Sfrankho if (lcn >= fsp->pcfs_ncluster) 195*5121Sfrankho return (ENOSPC); 196*5121Sfrankho if ((vp->v_type == VREG && pcp->pc_size == 0) || 197*5121Sfrankho (vp->v_type == VDIR && lcn == 0)) { 198*5121Sfrankho switch (cn = pc_alloccluster(fsp, 1)) { 199*5121Sfrankho case PCF_FREECLUSTER: 2000Sstevel@tonic-gate return (ENOSPC); 201*5121Sfrankho case PCF_ERRORCLUSTER: 202*5121Sfrankho return (EIO); 203*5121Sfrankho } 204*5121Sfrankho pcp->pc_scluster = cn; 205*5121Sfrankho } else { 206*5121Sfrankho cn = pcp->pc_scluster; 207*5121Sfrankho if (IS_FAT32(fsp) && cn == 0) 208*5121Sfrankho cn = fsp->pcfs_rdirstart; 209*5121Sfrankho if (!pc_validcl(fsp, cn)) { 210*5121Sfrankho PC_DPRINTF1(1, "pc_balloc: badfs cn=%d\n", cn); 211*5121Sfrankho (void) pc_badfs(fsp); 212*5121Sfrankho return (EIO); 213*5121Sfrankho } 214*5121Sfrankho } 215*5121Sfrankho 216*5121Sfrankho if (pcp->pc_lindex > 0 && lcn > pcp->pc_lindex) { 217*5121Sfrankho lcn -= pcp->pc_lindex; 218*5121Sfrankho cn = pcp->pc_lcluster; 219*5121Sfrankho } 220*5121Sfrankho while (lcn-- > 0) { 221*5121Sfrankho ncn = pc_getcluster(fsp, cn); 222*5121Sfrankho if ((IS_FAT32(fsp) && ncn >= PCF_LASTCLUSTER32) || 223*5121Sfrankho (!IS_FAT32(fsp) && ncn >= PCF_LASTCLUSTER)) { 224*5121Sfrankho /* 225*5121Sfrankho * Extend file (no holes). 226*5121Sfrankho */ 227*5121Sfrankho switch (ncn = pc_alloccluster(fsp, zwrite)) { 2280Sstevel@tonic-gate case PCF_FREECLUSTER: 2290Sstevel@tonic-gate return (ENOSPC); 2300Sstevel@tonic-gate case PCF_ERRORCLUSTER: 2310Sstevel@tonic-gate return (EIO); 2320Sstevel@tonic-gate } 233*5121Sfrankho pc_setcluster(fsp, cn, ncn); 234*5121Sfrankho } else if (!pc_validcl(fsp, ncn)) { 235*5121Sfrankho PC_DPRINTF1(1, 236*5121Sfrankho "pc_balloc: badfs ncn=%d\n", ncn); 237*5121Sfrankho (void) pc_badfs(fsp); 238*5121Sfrankho return (EIO); 2390Sstevel@tonic-gate } 240*5121Sfrankho cn = ncn; 2410Sstevel@tonic-gate } 242*5121Sfrankho /* 243*5121Sfrankho * Do not cache the new cluster/index values; when 244*5121Sfrankho * extending the file we're interested in the last 245*5121Sfrankho * written cluster and not the last cluster allocated. 246*5121Sfrankho */ 247*5121Sfrankho *dbnp = pc_cldaddr(fsp, cn); 248*5121Sfrankho 2490Sstevel@tonic-gate return (0); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * Free file cluster chain after the first skipcl clusters. 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate int 2560Sstevel@tonic-gate pc_bfree(struct pcnode *pcp, pc_cluster32_t skipcl) 2570Sstevel@tonic-gate { 2580Sstevel@tonic-gate struct pcfs *fsp; 2590Sstevel@tonic-gate pc_cluster32_t cn; 2600Sstevel@tonic-gate pc_cluster32_t ncn; 2610Sstevel@tonic-gate int n; 2620Sstevel@tonic-gate struct vnode *vp; 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate vp = PCTOV(pcp); 2650Sstevel@tonic-gate fsp = VFSTOPCFS(vp->v_vfsp); 2660Sstevel@tonic-gate if (!IS_FAT32(fsp) && (vp->v_flag & VROOT)) { 2670Sstevel@tonic-gate panic("pc_bfree"); 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (pcp->pc_size == 0 && vp->v_type == VREG) { 2710Sstevel@tonic-gate return (0); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate if (vp->v_type == VREG) { 2740Sstevel@tonic-gate n = (int)howmany((offset_t)pcp->pc_size, fsp->pcfs_clsize); 2750Sstevel@tonic-gate if (n > fsp->pcfs_ncluster) { 2760Sstevel@tonic-gate PC_DPRINTF1(1, "pc_bfree: badfs n=%d\n", n); 2770Sstevel@tonic-gate (void) pc_badfs(fsp); 2780Sstevel@tonic-gate return (EIO); 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate } else { 2810Sstevel@tonic-gate n = fsp->pcfs_ncluster; 2820Sstevel@tonic-gate } 2830Sstevel@tonic-gate cn = pcp->pc_scluster; 2840Sstevel@tonic-gate if (IS_FAT32(fsp) && cn == 0) 2850Sstevel@tonic-gate cn = fsp->pcfs_rdirstart; 2860Sstevel@tonic-gate if (skipcl == 0) { 2870Sstevel@tonic-gate if (IS_FAT32(fsp)) 2880Sstevel@tonic-gate pcp->pc_scluster = PCF_LASTCLUSTERMARK32; 2890Sstevel@tonic-gate else 2900Sstevel@tonic-gate pcp->pc_scluster = PCF_LASTCLUSTERMARK; 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate /* Invalidate last used cluster cache */ 2940Sstevel@tonic-gate pcp->pc_lindex = 0; 2950Sstevel@tonic-gate pcp->pc_lcluster = pcp->pc_scluster; 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate while (n--) { 2980Sstevel@tonic-gate if (!pc_validcl(fsp, cn)) { 2990Sstevel@tonic-gate PC_DPRINTF1(1, "pc_bfree: badfs cn=%d\n", cn); 3000Sstevel@tonic-gate (void) pc_badfs(fsp); 3010Sstevel@tonic-gate return (EIO); 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate ncn = pc_getcluster(fsp, cn); 3040Sstevel@tonic-gate if (skipcl == 0) { 3050Sstevel@tonic-gate pc_setcluster(fsp, cn, PCF_FREECLUSTER); 3060Sstevel@tonic-gate } else { 3070Sstevel@tonic-gate skipcl--; 3080Sstevel@tonic-gate if (skipcl == 0) { 3090Sstevel@tonic-gate if (IS_FAT32(fsp)) { 3100Sstevel@tonic-gate pc_setcluster(fsp, cn, 3110Sstevel@tonic-gate PCF_LASTCLUSTERMARK32); 3120Sstevel@tonic-gate } else 3130Sstevel@tonic-gate pc_setcluster(fsp, cn, 3140Sstevel@tonic-gate PCF_LASTCLUSTERMARK); 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate if (IS_FAT32(fsp) && ncn >= PCF_LASTCLUSTER32 && 3180Sstevel@tonic-gate vp->v_type == VDIR) 3190Sstevel@tonic-gate break; 3200Sstevel@tonic-gate if (!IS_FAT32(fsp) && ncn >= PCF_LASTCLUSTER && 3210Sstevel@tonic-gate vp->v_type == VDIR) 3220Sstevel@tonic-gate break; 3230Sstevel@tonic-gate cn = ncn; 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate return (0); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * Return the number of free blocks in the filesystem. 3300Sstevel@tonic-gate */ 3310Sstevel@tonic-gate int 3320Sstevel@tonic-gate pc_freeclusters(struct pcfs *fsp) 3330Sstevel@tonic-gate { 3340Sstevel@tonic-gate pc_cluster32_t cn; 3352720Sfrankho int free = 0; 3362720Sfrankho 3372720Sfrankho if (IS_FAT32(fsp) && 338*5121Sfrankho fsp->pcfs_fsinfo.fs_free_clusters != FSINFO_UNKNOWN) 339*5121Sfrankho return (fsp->pcfs_fsinfo.fs_free_clusters); 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate /* 3420Sstevel@tonic-gate * make sure the FAT is in core 3430Sstevel@tonic-gate */ 3440Sstevel@tonic-gate for (cn = PCF_FIRSTCLUSTER; 3452720Sfrankho (int)cn < fsp->pcfs_ncluster; cn++) { 3460Sstevel@tonic-gate if (pc_getcluster(fsp, cn) == PCF_FREECLUSTER) { 3470Sstevel@tonic-gate free++; 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate } 3502720Sfrankho 3512720Sfrankho if (IS_FAT32(fsp)) { 352*5121Sfrankho ASSERT(fsp->pcfs_fsinfo.fs_free_clusters == FSINFO_UNKNOWN); 353*5121Sfrankho fsp->pcfs_fsinfo.fs_free_clusters = free; 3542720Sfrankho } 3550Sstevel@tonic-gate return (free); 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* 3590Sstevel@tonic-gate * Cluster manipulation routines. 3600Sstevel@tonic-gate * FAT must be resident. 3610Sstevel@tonic-gate */ 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate /* 3640Sstevel@tonic-gate * Get the next cluster in the file cluster chain. 3650Sstevel@tonic-gate * cn = current cluster number in chain 3660Sstevel@tonic-gate */ 3670Sstevel@tonic-gate static pc_cluster32_t 3680Sstevel@tonic-gate pc_getcluster(struct pcfs *fsp, pc_cluster32_t cn) 3690Sstevel@tonic-gate { 3700Sstevel@tonic-gate unsigned char *fp; 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate if (fsp->pcfs_fatp == (uchar_t *)0 || !pc_validcl(fsp, cn)) 3730Sstevel@tonic-gate panic("pc_getcluster"); 3740Sstevel@tonic-gate 375*5121Sfrankho switch (fsp->pcfs_fattype) { 376*5121Sfrankho case FAT32: 3770Sstevel@tonic-gate fp = fsp->pcfs_fatp + (cn << 2); 3780Sstevel@tonic-gate cn = ltohi(*(pc_cluster32_t *)fp); 379*5121Sfrankho break; 380*5121Sfrankho case FAT16: 3810Sstevel@tonic-gate fp = fsp->pcfs_fatp + (cn << 1); 3820Sstevel@tonic-gate cn = ltohs(*(pc_cluster16_t *)fp); 383*5121Sfrankho break; 384*5121Sfrankho case FAT12: 3850Sstevel@tonic-gate fp = fsp->pcfs_fatp + (cn + (cn >> 1)); 3860Sstevel@tonic-gate if (cn & 01) { 3870Sstevel@tonic-gate cn = (((unsigned int)*fp++ & 0xf0) >> 4); 3880Sstevel@tonic-gate cn += (*fp << 4); 3890Sstevel@tonic-gate } else { 3900Sstevel@tonic-gate cn = *fp++; 3910Sstevel@tonic-gate cn += ((*fp & 0x0f) << 8); 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate if (cn >= PCF_12BCLUSTER) 3940Sstevel@tonic-gate cn |= PCF_RESCLUSTER; 395*5121Sfrankho break; 396*5121Sfrankho default: 397*5121Sfrankho pc_mark_irrecov(fsp); 398*5121Sfrankho cn = PCF_ERRORCLUSTER; 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate return (cn); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate /* 4040Sstevel@tonic-gate * Set a cluster in the FAT to a value. 4050Sstevel@tonic-gate * cn = cluster number to be set in FAT 4060Sstevel@tonic-gate * ncn = new value 4070Sstevel@tonic-gate */ 4080Sstevel@tonic-gate void 4090Sstevel@tonic-gate pc_setcluster(struct pcfs *fsp, pc_cluster32_t cn, pc_cluster32_t ncn) 4100Sstevel@tonic-gate { 4110Sstevel@tonic-gate unsigned char *fp; 412*5121Sfrankho pc_cluster16_t ncn16; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate if (fsp->pcfs_fatp == (uchar_t *)0 || !pc_validcl(fsp, cn)) 4150Sstevel@tonic-gate panic("pc_setcluster"); 4160Sstevel@tonic-gate fsp->pcfs_flags |= PCFS_FATMOD; 4170Sstevel@tonic-gate pc_mark_fat_updated(fsp, cn); 418*5121Sfrankho switch (fsp->pcfs_fattype) { 419*5121Sfrankho case FAT32: 4200Sstevel@tonic-gate fp = fsp->pcfs_fatp + (cn << 2); 4210Sstevel@tonic-gate *(pc_cluster32_t *)fp = htoli(ncn); 422*5121Sfrankho break; 423*5121Sfrankho case FAT16: 4240Sstevel@tonic-gate fp = fsp->pcfs_fatp + (cn << 1); 4250Sstevel@tonic-gate ncn16 = (pc_cluster16_t)ncn; 4260Sstevel@tonic-gate *(pc_cluster16_t *)fp = htols(ncn16); 427*5121Sfrankho break; 428*5121Sfrankho case FAT12: 4290Sstevel@tonic-gate fp = fsp->pcfs_fatp + (cn + (cn >> 1)); 4300Sstevel@tonic-gate if (cn & 01) { 4310Sstevel@tonic-gate *fp = (*fp & 0x0f) | ((ncn << 4) & 0xf0); 4320Sstevel@tonic-gate fp++; 4330Sstevel@tonic-gate *fp = (ncn >> 4) & 0xff; 4340Sstevel@tonic-gate } else { 4350Sstevel@tonic-gate *fp++ = ncn & 0xff; 4360Sstevel@tonic-gate *fp = (*fp & 0xf0) | ((ncn >> 8) & 0x0f); 4370Sstevel@tonic-gate } 438*5121Sfrankho break; 439*5121Sfrankho default: 440*5121Sfrankho pc_mark_irrecov(fsp); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate if (ncn == PCF_FREECLUSTER) { 4430Sstevel@tonic-gate fsp->pcfs_nxfrecls = PCF_FIRSTCLUSTER; 4440Sstevel@tonic-gate if (IS_FAT32(fsp)) { 445*5121Sfrankho if (fsp->pcfs_fsinfo.fs_free_clusters != 4460Sstevel@tonic-gate FSINFO_UNKNOWN) 447*5121Sfrankho fsp->pcfs_fsinfo.fs_free_clusters++; 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate } 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate /* 4530Sstevel@tonic-gate * Allocate a new cluster. 4540Sstevel@tonic-gate */ 4550Sstevel@tonic-gate pc_cluster32_t 4560Sstevel@tonic-gate pc_alloccluster( 4570Sstevel@tonic-gate struct pcfs *fsp, /* file sys to allocate in */ 4580Sstevel@tonic-gate int zwrite) /* boolean for writing zeroes */ 4590Sstevel@tonic-gate { 4600Sstevel@tonic-gate pc_cluster32_t cn; 4610Sstevel@tonic-gate int error; 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate if (fsp->pcfs_fatp == (uchar_t *)0) 4640Sstevel@tonic-gate panic("pc_addcluster: no FAT"); 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate for (cn = fsp->pcfs_nxfrecls; 4672720Sfrankho (int)cn < fsp->pcfs_ncluster; cn++) { 4680Sstevel@tonic-gate if (pc_getcluster(fsp, cn) == PCF_FREECLUSTER) { 4690Sstevel@tonic-gate struct buf *bp; 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate if (IS_FAT32(fsp)) { 4720Sstevel@tonic-gate pc_setcluster(fsp, cn, PCF_LASTCLUSTERMARK32); 473*5121Sfrankho if (fsp->pcfs_fsinfo.fs_free_clusters != 4740Sstevel@tonic-gate FSINFO_UNKNOWN) 475*5121Sfrankho fsp->pcfs_fsinfo.fs_free_clusters--; 4760Sstevel@tonic-gate } else 4770Sstevel@tonic-gate pc_setcluster(fsp, cn, PCF_LASTCLUSTERMARK); 4780Sstevel@tonic-gate if (zwrite) { 4790Sstevel@tonic-gate /* 4800Sstevel@tonic-gate * zero the new cluster 4810Sstevel@tonic-gate */ 4820Sstevel@tonic-gate bp = ngeteblk(fsp->pcfs_clsize); 4830Sstevel@tonic-gate bp->b_edev = fsp->pcfs_xdev; 4840Sstevel@tonic-gate bp->b_dev = cmpdev(bp->b_edev); 4850Sstevel@tonic-gate bp->b_blkno = pc_cldaddr(fsp, cn); 4860Sstevel@tonic-gate clrbuf(bp); 4870Sstevel@tonic-gate bwrite2(bp); 4880Sstevel@tonic-gate error = geterror(bp); 4890Sstevel@tonic-gate brelse(bp); 4900Sstevel@tonic-gate if (error) { 4910Sstevel@tonic-gate pc_mark_irrecov(fsp); 4920Sstevel@tonic-gate return (PCF_ERRORCLUSTER); 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate } 4950Sstevel@tonic-gate fsp->pcfs_nxfrecls = cn + 1; 4960Sstevel@tonic-gate return (cn); 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate return (PCF_FREECLUSTER); 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate /* 5030Sstevel@tonic-gate * Get the number of clusters used by a file or subdirectory 5040Sstevel@tonic-gate */ 5050Sstevel@tonic-gate int 5060Sstevel@tonic-gate pc_fileclsize( 5070Sstevel@tonic-gate struct pcfs *fsp, 5082972Sfrankho pc_cluster32_t startcl, pc_cluster32_t *ncl) 5090Sstevel@tonic-gate { 5100Sstevel@tonic-gate int count = 0; 5110Sstevel@tonic-gate 5122972Sfrankho *ncl = 0; 5132972Sfrankho for (count = 0; pc_validcl(fsp, startcl); 5142972Sfrankho startcl = pc_getcluster(fsp, startcl)) { 5152972Sfrankho if (count++ >= fsp->pcfs_ncluster) 5162972Sfrankho return (EIO); 5170Sstevel@tonic-gate } 5182972Sfrankho *ncl = (pc_cluster32_t)count; 5192972Sfrankho 5202972Sfrankho return (0); 5210Sstevel@tonic-gate } 522