1c3aac50fSPeter Wemm /* $FreeBSD$ */ 2952a6212SJordan K. Hubbard /* $NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $ */ 327a0bc89SDoug Rabson 427a0bc89SDoug Rabson /*- 5952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 6952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 727a0bc89SDoug Rabson * All rights reserved. 827a0bc89SDoug Rabson * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 927a0bc89SDoug Rabson * 1027a0bc89SDoug Rabson * Redistribution and use in source and binary forms, with or without 1127a0bc89SDoug Rabson * modification, are permitted provided that the following conditions 1227a0bc89SDoug Rabson * are met: 1327a0bc89SDoug Rabson * 1. Redistributions of source code must retain the above copyright 1427a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer. 1527a0bc89SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 1627a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer in the 1727a0bc89SDoug Rabson * documentation and/or other materials provided with the distribution. 1827a0bc89SDoug Rabson * 3. All advertising materials mentioning features or use of this software 1927a0bc89SDoug Rabson * must display the following acknowledgement: 2027a0bc89SDoug Rabson * This product includes software developed by TooLs GmbH. 2127a0bc89SDoug Rabson * 4. The name of TooLs GmbH may not be used to endorse or promote products 2227a0bc89SDoug Rabson * derived from this software without specific prior written permission. 2327a0bc89SDoug Rabson * 2427a0bc89SDoug Rabson * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2527a0bc89SDoug Rabson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2627a0bc89SDoug Rabson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2727a0bc89SDoug Rabson * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2827a0bc89SDoug Rabson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2927a0bc89SDoug Rabson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3027a0bc89SDoug Rabson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3127a0bc89SDoug Rabson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3227a0bc89SDoug Rabson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3327a0bc89SDoug Rabson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3427a0bc89SDoug Rabson */ 35d167cf6fSWarner Losh /*- 3627a0bc89SDoug Rabson * Written by Paul Popelka (paulp@uts.amdahl.com) 3727a0bc89SDoug Rabson * 3827a0bc89SDoug Rabson * You can do anything you want with this software, just don't say you wrote 3927a0bc89SDoug Rabson * it, and don't remove this notice. 4027a0bc89SDoug Rabson * 4127a0bc89SDoug Rabson * This software is provided "as is". 4227a0bc89SDoug Rabson * 4327a0bc89SDoug Rabson * The author supplies this software to be publicly redistributed on the 4427a0bc89SDoug Rabson * understanding that the author is not responsible for the correct 4527a0bc89SDoug Rabson * functioning of this software in any circumstances and is not liable for 4627a0bc89SDoug Rabson * any damages caused by this software. 4727a0bc89SDoug Rabson * 4827a0bc89SDoug Rabson * October 1992 4927a0bc89SDoug Rabson */ 5027a0bc89SDoug Rabson 5127a0bc89SDoug Rabson #include <sys/param.h> 5227a0bc89SDoug Rabson #include <sys/systm.h> 5327a0bc89SDoug Rabson #include <sys/buf.h> 545696c6e0SBruce Evans #include <sys/mount.h> 555696c6e0SBruce Evans #include <sys/vnode.h> 5627a0bc89SDoug Rabson 571166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h> 581166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h> 591166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h> 601166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h> 615696c6e0SBruce Evans #include <fs/msdosfs/msdosfsmount.h> 6227a0bc89SDoug Rabson 6327a0bc89SDoug Rabson /* 6427a0bc89SDoug Rabson * Fat cache stats. 6527a0bc89SDoug Rabson */ 66303b270bSEivind Eklund static int fc_fileextends; /* # of file extends */ 67303b270bSEivind Eklund static int fc_lfcempty; /* # of time last file cluster cache entry 6827a0bc89SDoug Rabson * was empty */ 69303b270bSEivind Eklund static int fc_bmapcalls; /* # of times pcbmap was called */ 7027a0bc89SDoug Rabson 7127a0bc89SDoug Rabson #define LMMAX 20 72303b270bSEivind Eklund static int fc_lmdistance[LMMAX];/* counters for how far off the last 7327a0bc89SDoug Rabson * cluster mapped entry was. */ 74303b270bSEivind Eklund static int fc_largedistance; /* off by more than LMMAX */ 75752945d6STom Rhodes static int fc_wherefrom, fc_whereto, fc_lastclust; 76752945d6STom Rhodes static int pm_fatblocksize; 7727a0bc89SDoug Rabson 7811caded3SAlfred Perlstein static int chainalloc(struct msdosfsmount *pmp, u_long start, 79b76d0b32SBruce Evans u_long count, u_long fillwith, u_long *retcluster, 80b76d0b32SBruce Evans u_long *got); 8111caded3SAlfred Perlstein static int chainlength(struct msdosfsmount *pmp, u_long start, 8211caded3SAlfred Perlstein u_long count); 83b76d0b32SBruce Evans static void fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, 84b76d0b32SBruce Evans u_long *sizep, u_long *bop); 85b76d0b32SBruce Evans static int fatchain(struct msdosfsmount *pmp, u_long start, u_long count, 86b76d0b32SBruce Evans u_long fillwith); 87b76d0b32SBruce Evans static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, 88b76d0b32SBruce Evans u_long *fsrcnp); 8911caded3SAlfred Perlstein static void updatefats(struct msdosfsmount *pmp, struct buf *bp, 9011caded3SAlfred Perlstein u_long fatbn); 91c1087c13SBruce Evans static __inline void 9211caded3SAlfred Perlstein usemap_alloc(struct msdosfsmount *pmp, u_long cn); 93c1087c13SBruce Evans static __inline void 9411caded3SAlfred Perlstein usemap_free(struct msdosfsmount *pmp, u_long cn); 9558c27bcfSBruce Evans 9627a0bc89SDoug Rabson static void 9727a0bc89SDoug Rabson fatblock(pmp, ofs, bnp, sizep, bop) 9827a0bc89SDoug Rabson struct msdosfsmount *pmp; 9927a0bc89SDoug Rabson u_long ofs; 10027a0bc89SDoug Rabson u_long *bnp; 10127a0bc89SDoug Rabson u_long *sizep; 10227a0bc89SDoug Rabson u_long *bop; 10327a0bc89SDoug Rabson { 10427a0bc89SDoug Rabson u_long bn, size; 10527a0bc89SDoug Rabson 10627a0bc89SDoug Rabson bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec; 10727a0bc89SDoug Rabson size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn) 10801f6cfbaSYoshihiro Takahashi * DEV_BSIZE; 109952a6212SJordan K. Hubbard bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs; 110952a6212SJordan K. Hubbard 11127a0bc89SDoug Rabson if (bnp) 11227a0bc89SDoug Rabson *bnp = bn; 11327a0bc89SDoug Rabson if (sizep) 11427a0bc89SDoug Rabson *sizep = size; 11527a0bc89SDoug Rabson if (bop) 11627a0bc89SDoug Rabson *bop = ofs % pmp->pm_fatblocksize; 117752945d6STom Rhodes pm_fatblocksize = pmp->pm_fatblocksize; 11827a0bc89SDoug Rabson } 11927a0bc89SDoug Rabson 12027a0bc89SDoug Rabson /* 12127a0bc89SDoug Rabson * Map the logical cluster number of a file into a physical disk sector 12227a0bc89SDoug Rabson * that is filesystem relative. 12327a0bc89SDoug Rabson * 12427a0bc89SDoug Rabson * dep - address of denode representing the file of interest 12527a0bc89SDoug Rabson * findcn - file relative cluster whose filesystem relative cluster number 12627a0bc89SDoug Rabson * and/or block number are/is to be found 12727a0bc89SDoug Rabson * bnp - address of where to place the filesystem relative block number. 12827a0bc89SDoug Rabson * If this pointer is null then don't return this quantity. 12927a0bc89SDoug Rabson * cnp - address of where to place the filesystem relative cluster number. 13027a0bc89SDoug Rabson * If this pointer is null then don't return this quantity. 13127a0bc89SDoug Rabson * 13227a0bc89SDoug Rabson * NOTE: Either bnp or cnp must be non-null. 13327a0bc89SDoug Rabson * This function has one side effect. If the requested file relative cluster 13427a0bc89SDoug Rabson * is beyond the end of file, then the actual number of clusters in the file 13527a0bc89SDoug Rabson * is returned in *cnp. This is useful for determining how long a directory is. 13627a0bc89SDoug Rabson * If cnp is null, nothing is returned. 13727a0bc89SDoug Rabson */ 13827a0bc89SDoug Rabson int 139952a6212SJordan K. Hubbard pcbmap(dep, findcn, bnp, cnp, sp) 14027a0bc89SDoug Rabson struct denode *dep; 14127a0bc89SDoug Rabson u_long findcn; /* file relative cluster to get */ 14227a0bc89SDoug Rabson daddr_t *bnp; /* returned filesys relative blk number */ 14327a0bc89SDoug Rabson u_long *cnp; /* returned cluster number */ 144952a6212SJordan K. Hubbard int *sp; /* returned block size */ 14527a0bc89SDoug Rabson { 14627a0bc89SDoug Rabson int error; 14727a0bc89SDoug Rabson u_long i; 14827a0bc89SDoug Rabson u_long cn; 149952a6212SJordan K. Hubbard u_long prevcn = 0; /* XXX: prevcn could be used unititialized */ 15027a0bc89SDoug Rabson u_long byteoffset; 15127a0bc89SDoug Rabson u_long bn; 15227a0bc89SDoug Rabson u_long bo; 15327a0bc89SDoug Rabson struct buf *bp = NULL; 15427a0bc89SDoug Rabson u_long bp_bn = -1; 15527a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 15627a0bc89SDoug Rabson u_long bsize; 15727a0bc89SDoug Rabson 15827a0bc89SDoug Rabson fc_bmapcalls++; 15927a0bc89SDoug Rabson 16027a0bc89SDoug Rabson /* 16127a0bc89SDoug Rabson * If they don't give us someplace to return a value then don't 16227a0bc89SDoug Rabson * bother doing anything. 16327a0bc89SDoug Rabson */ 164952a6212SJordan K. Hubbard if (bnp == NULL && cnp == NULL && sp == NULL) 165952a6212SJordan K. Hubbard return (0); 16627a0bc89SDoug Rabson 16727a0bc89SDoug Rabson cn = dep->de_StartCluster; 16827a0bc89SDoug Rabson /* 16927a0bc89SDoug Rabson * The "file" that makes up the root directory is contiguous, 17027a0bc89SDoug Rabson * permanently allocated, of fixed size, and is not made up of 17127a0bc89SDoug Rabson * clusters. If the cluster number is beyond the end of the root 17227a0bc89SDoug Rabson * directory, then return the number of clusters in the file. 17327a0bc89SDoug Rabson */ 17427a0bc89SDoug Rabson if (cn == MSDOSFSROOT) { 17527a0bc89SDoug Rabson if (dep->de_Attributes & ATTR_DIRECTORY) { 176952a6212SJordan K. Hubbard if (de_cn2off(pmp, findcn) >= dep->de_FileSize) { 17727a0bc89SDoug Rabson if (cnp) 178952a6212SJordan K. Hubbard *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize); 179952a6212SJordan K. Hubbard return (E2BIG); 18027a0bc89SDoug Rabson } 18127a0bc89SDoug Rabson if (bnp) 182952a6212SJordan K. Hubbard *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn); 18327a0bc89SDoug Rabson if (cnp) 18427a0bc89SDoug Rabson *cnp = MSDOSFSROOT; 185952a6212SJordan K. Hubbard if (sp) 186952a6212SJordan K. Hubbard *sp = min(pmp->pm_bpcluster, 187952a6212SJordan K. Hubbard dep->de_FileSize - de_cn2off(pmp, findcn)); 188952a6212SJordan K. Hubbard return (0); 18927a0bc89SDoug Rabson } else { /* just an empty file */ 19027a0bc89SDoug Rabson if (cnp) 19127a0bc89SDoug Rabson *cnp = 0; 192952a6212SJordan K. Hubbard return (E2BIG); 19327a0bc89SDoug Rabson } 19427a0bc89SDoug Rabson } 19527a0bc89SDoug Rabson 19627a0bc89SDoug Rabson /* 197952a6212SJordan K. Hubbard * All other files do I/O in cluster sized blocks 198952a6212SJordan K. Hubbard */ 199952a6212SJordan K. Hubbard if (sp) 200952a6212SJordan K. Hubbard *sp = pmp->pm_bpcluster; 201952a6212SJordan K. Hubbard 202952a6212SJordan K. Hubbard /* 20327a0bc89SDoug Rabson * Rummage around in the fat cache, maybe we can avoid tromping 20427a0bc89SDoug Rabson * thru every fat entry for the file. And, keep track of how far 20527a0bc89SDoug Rabson * off the cache was from where we wanted to be. 20627a0bc89SDoug Rabson */ 20727a0bc89SDoug Rabson i = 0; 20827a0bc89SDoug Rabson fc_lookup(dep, findcn, &i, &cn); 209752945d6STom Rhodes if ((bn = findcn - i) >= LMMAX) { 21027a0bc89SDoug Rabson fc_largedistance++; 211752945d6STom Rhodes fc_wherefrom = i; 212752945d6STom Rhodes fc_whereto = findcn; 213752945d6STom Rhodes fc_lastclust = dep->de_fc[FC_LASTFC].fc_frcn; 214752945d6STom Rhodes } else 21527a0bc89SDoug Rabson fc_lmdistance[bn]++; 21627a0bc89SDoug Rabson 21727a0bc89SDoug Rabson /* 21827a0bc89SDoug Rabson * Handle all other files or directories the normal way. 21927a0bc89SDoug Rabson */ 22027a0bc89SDoug Rabson for (; i < findcn; i++) { 221952a6212SJordan K. Hubbard /* 222952a6212SJordan K. Hubbard * Stop with all reserved clusters, not just with EOF. 223952a6212SJordan K. Hubbard */ 224952a6212SJordan K. Hubbard if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 22527a0bc89SDoug Rabson goto hiteof; 22627a0bc89SDoug Rabson byteoffset = FATOFS(pmp, cn); 22727a0bc89SDoug Rabson fatblock(pmp, byteoffset, &bn, &bsize, &bo); 22827a0bc89SDoug Rabson if (bn != bp_bn) { 22927a0bc89SDoug Rabson if (bp) 23027a0bc89SDoug Rabson brelse(bp); 23127a0bc89SDoug Rabson error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 232952a6212SJordan K. Hubbard if (error) { 233952a6212SJordan K. Hubbard brelse(bp); 234952a6212SJordan K. Hubbard return (error); 235952a6212SJordan K. Hubbard } 23627a0bc89SDoug Rabson bp_bn = bn; 23727a0bc89SDoug Rabson } 23827a0bc89SDoug Rabson prevcn = cn; 239d23af19aSTim J. Robbins if (bo >= bsize) { 240d23af19aSTim J. Robbins if (bp) 241d23af19aSTim J. Robbins brelse(bp); 242d23af19aSTim J. Robbins return (EIO); 243d23af19aSTim J. Robbins } 244952a6212SJordan K. Hubbard if (FAT32(pmp)) 245952a6212SJordan K. Hubbard cn = getulong(&bp->b_data[bo]); 246952a6212SJordan K. Hubbard else 24727a0bc89SDoug Rabson cn = getushort(&bp->b_data[bo]); 248952a6212SJordan K. Hubbard if (FAT12(pmp) && (prevcn & 1)) 24927a0bc89SDoug Rabson cn >>= 4; 250952a6212SJordan K. Hubbard cn &= pmp->pm_fatmask; 251952a6212SJordan K. Hubbard 25227a0bc89SDoug Rabson /* 253952a6212SJordan K. Hubbard * Force the special cluster numbers 254952a6212SJordan K. Hubbard * to be the same for all cluster sizes 255952a6212SJordan K. Hubbard * to let the rest of msdosfs handle 256952a6212SJordan K. Hubbard * all cases the same. 25727a0bc89SDoug Rabson */ 258952a6212SJordan K. Hubbard if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 259952a6212SJordan K. Hubbard cn |= ~pmp->pm_fatmask; 26027a0bc89SDoug Rabson } 26127a0bc89SDoug Rabson 262952a6212SJordan K. Hubbard if (!MSDOSFSEOF(pmp, cn)) { 26327a0bc89SDoug Rabson if (bp) 26427a0bc89SDoug Rabson brelse(bp); 26527a0bc89SDoug Rabson if (bnp) 26627a0bc89SDoug Rabson *bnp = cntobn(pmp, cn); 26727a0bc89SDoug Rabson if (cnp) 26827a0bc89SDoug Rabson *cnp = cn; 26927a0bc89SDoug Rabson fc_setcache(dep, FC_LASTMAP, i, cn); 270952a6212SJordan K. Hubbard return (0); 27127a0bc89SDoug Rabson } 27227a0bc89SDoug Rabson 27327a0bc89SDoug Rabson hiteof:; 27427a0bc89SDoug Rabson if (cnp) 27527a0bc89SDoug Rabson *cnp = i; 27627a0bc89SDoug Rabson if (bp) 27727a0bc89SDoug Rabson brelse(bp); 27827a0bc89SDoug Rabson /* update last file cluster entry in the fat cache */ 27927a0bc89SDoug Rabson fc_setcache(dep, FC_LASTFC, i - 1, prevcn); 280952a6212SJordan K. Hubbard return (E2BIG); 28127a0bc89SDoug Rabson } 28227a0bc89SDoug Rabson 28327a0bc89SDoug Rabson /* 28427a0bc89SDoug Rabson * Find the closest entry in the fat cache to the cluster we are looking 28527a0bc89SDoug Rabson * for. 28627a0bc89SDoug Rabson */ 2877fefffeeSPoul-Henning Kamp static void 288c3c6d51eSPoul-Henning Kamp fc_lookup(dep, findcn, frcnp, fsrcnp) 28927a0bc89SDoug Rabson struct denode *dep; 29027a0bc89SDoug Rabson u_long findcn; 29127a0bc89SDoug Rabson u_long *frcnp; 29227a0bc89SDoug Rabson u_long *fsrcnp; 29327a0bc89SDoug Rabson { 29427a0bc89SDoug Rabson int i; 29527a0bc89SDoug Rabson u_long cn; 29627a0bc89SDoug Rabson struct fatcache *closest = 0; 29727a0bc89SDoug Rabson 29827a0bc89SDoug Rabson for (i = 0; i < FC_SIZE; i++) { 29927a0bc89SDoug Rabson cn = dep->de_fc[i].fc_frcn; 30027a0bc89SDoug Rabson if (cn != FCE_EMPTY && cn <= findcn) { 30127a0bc89SDoug Rabson if (closest == 0 || cn > closest->fc_frcn) 30227a0bc89SDoug Rabson closest = &dep->de_fc[i]; 30327a0bc89SDoug Rabson } 30427a0bc89SDoug Rabson } 30527a0bc89SDoug Rabson if (closest) { 30627a0bc89SDoug Rabson *frcnp = closest->fc_frcn; 30727a0bc89SDoug Rabson *fsrcnp = closest->fc_fsrcn; 30827a0bc89SDoug Rabson } 30927a0bc89SDoug Rabson } 31027a0bc89SDoug Rabson 31127a0bc89SDoug Rabson /* 31227a0bc89SDoug Rabson * Purge the fat cache in denode dep of all entries relating to file 31327a0bc89SDoug Rabson * relative cluster frcn and beyond. 31427a0bc89SDoug Rabson */ 315952a6212SJordan K. Hubbard void 316952a6212SJordan K. Hubbard fc_purge(dep, frcn) 31727a0bc89SDoug Rabson struct denode *dep; 31827a0bc89SDoug Rabson u_int frcn; 31927a0bc89SDoug Rabson { 32027a0bc89SDoug Rabson int i; 32127a0bc89SDoug Rabson struct fatcache *fcp; 32227a0bc89SDoug Rabson 32327a0bc89SDoug Rabson fcp = dep->de_fc; 32427a0bc89SDoug Rabson for (i = 0; i < FC_SIZE; i++, fcp++) { 32527a0bc89SDoug Rabson if (fcp->fc_frcn >= frcn) 32627a0bc89SDoug Rabson fcp->fc_frcn = FCE_EMPTY; 32727a0bc89SDoug Rabson } 32827a0bc89SDoug Rabson } 32927a0bc89SDoug Rabson 33027a0bc89SDoug Rabson /* 331952a6212SJordan K. Hubbard * Update the fat. 332952a6212SJordan K. Hubbard * If mirroring the fat, update all copies, with the first copy as last. 333952a6212SJordan K. Hubbard * Else update only the current fat (ignoring the others). 33427a0bc89SDoug Rabson * 33527a0bc89SDoug Rabson * pmp - msdosfsmount structure for filesystem to update 33627a0bc89SDoug Rabson * bp - addr of modified fat block 33727a0bc89SDoug Rabson * fatbn - block number relative to begin of filesystem of the modified fat block. 33827a0bc89SDoug Rabson */ 339a98ca469SPoul-Henning Kamp static void 34027a0bc89SDoug Rabson updatefats(pmp, bp, fatbn) 34127a0bc89SDoug Rabson struct msdosfsmount *pmp; 34227a0bc89SDoug Rabson struct buf *bp; 34327a0bc89SDoug Rabson u_long fatbn; 34427a0bc89SDoug Rabson { 34527a0bc89SDoug Rabson int i; 34627a0bc89SDoug Rabson struct buf *bpn; 34727a0bc89SDoug Rabson 34827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 349952a6212SJordan K. Hubbard printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn); 35027a0bc89SDoug Rabson #endif 35127a0bc89SDoug Rabson 35227a0bc89SDoug Rabson /* 353952a6212SJordan K. Hubbard * If we have an FSInfo block, update it. 354952a6212SJordan K. Hubbard */ 355952a6212SJordan K. Hubbard if (pmp->pm_fsinfo) { 356952a6212SJordan K. Hubbard u_long cn = pmp->pm_nxtfree; 357952a6212SJordan K. Hubbard 358952a6212SJordan K. Hubbard if (pmp->pm_freeclustercount 359952a6212SJordan K. Hubbard && (pmp->pm_inusemap[cn / N_INUSEBITS] 360952a6212SJordan K. Hubbard & (1 << (cn % N_INUSEBITS)))) { 361952a6212SJordan K. Hubbard /* 362952a6212SJordan K. Hubbard * The cluster indicated in FSInfo isn't free 363952a6212SJordan K. Hubbard * any longer. Got get a new free one. 364952a6212SJordan K. Hubbard */ 36532d3966fSAndrey A. Chernov for (cn = 0; cn < pmp->pm_maxcluster; cn += N_INUSEBITS) 366952a6212SJordan K. Hubbard if (pmp->pm_inusemap[cn / N_INUSEBITS] != (u_int)-1) 367952a6212SJordan K. Hubbard break; 368952a6212SJordan K. Hubbard pmp->pm_nxtfree = cn 369952a6212SJordan K. Hubbard + ffs(pmp->pm_inusemap[cn / N_INUSEBITS] 370952a6212SJordan K. Hubbard ^ (u_int)-1) - 1; 371952a6212SJordan K. Hubbard } 37237269429SBruce Evans if (bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec, 37301f6cfbaSYoshihiro Takahashi NOCRED, &bpn) != 0) { 374952a6212SJordan K. Hubbard /* 375952a6212SJordan K. Hubbard * Ignore the error, but turn off FSInfo update for the future. 376952a6212SJordan K. Hubbard */ 377952a6212SJordan K. Hubbard pmp->pm_fsinfo = 0; 378952a6212SJordan K. Hubbard brelse(bpn); 379952a6212SJordan K. Hubbard } else { 380952a6212SJordan K. Hubbard struct fsinfo *fp = (struct fsinfo *)bpn->b_data; 381952a6212SJordan K. Hubbard 382952a6212SJordan K. Hubbard putulong(fp->fsinfree, pmp->pm_freeclustercount); 383952a6212SJordan K. Hubbard putulong(fp->fsinxtfree, pmp->pm_nxtfree); 384952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT) 385952a6212SJordan K. Hubbard bwrite(bpn); 386952a6212SJordan K. Hubbard else 387952a6212SJordan K. Hubbard bdwrite(bpn); 388952a6212SJordan K. Hubbard } 389952a6212SJordan K. Hubbard } 390952a6212SJordan K. Hubbard 391952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFS_FATMIRROR) { 392952a6212SJordan K. Hubbard /* 39327a0bc89SDoug Rabson * Now copy the block(s) of the modified fat to the other copies of 39427a0bc89SDoug Rabson * the fat and write them out. This is faster than reading in the 39527a0bc89SDoug Rabson * other fats and then writing them back out. This could tie up 39627a0bc89SDoug Rabson * the fat for quite a while. Preventing others from accessing it. 39727a0bc89SDoug Rabson * To prevent us from going after the fat quite so much we use 39827a0bc89SDoug Rabson * delayed writes, unless they specfied "synchronous" when the 39927a0bc89SDoug Rabson * filesystem was mounted. If synch is asked for then use 40027a0bc89SDoug Rabson * bwrite()'s and really slow things down. 40127a0bc89SDoug Rabson */ 40227a0bc89SDoug Rabson for (i = 1; i < pmp->pm_FATs; i++) { 40327a0bc89SDoug Rabson fatbn += pmp->pm_FATsecs; 40427a0bc89SDoug Rabson /* getblk() never fails */ 4057261f5f6SJeff Roberson bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount, 4067261f5f6SJeff Roberson 0, 0, 0); 40727a0bc89SDoug Rabson bcopy(bp->b_data, bpn->b_data, bp->b_bcount); 408952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT) 40927a0bc89SDoug Rabson bwrite(bpn); 41027a0bc89SDoug Rabson else 41127a0bc89SDoug Rabson bdwrite(bpn); 41227a0bc89SDoug Rabson } 413952a6212SJordan K. Hubbard } 414952a6212SJordan K. Hubbard 41527a0bc89SDoug Rabson /* 416952a6212SJordan K. Hubbard * Write out the first (or current) fat last. 41727a0bc89SDoug Rabson */ 418952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_WAITONFAT) 41927a0bc89SDoug Rabson bwrite(bp); 42027a0bc89SDoug Rabson else 42127a0bc89SDoug Rabson bdwrite(bp); 422952a6212SJordan K. Hubbard /* 423952a6212SJordan K. Hubbard * Maybe update fsinfo sector here? 424952a6212SJordan K. Hubbard */ 42527a0bc89SDoug Rabson } 42627a0bc89SDoug Rabson 42727a0bc89SDoug Rabson /* 42827a0bc89SDoug Rabson * Updating entries in 12 bit fats is a pain in the butt. 42927a0bc89SDoug Rabson * 43027a0bc89SDoug Rabson * The following picture shows where nibbles go when moving from a 12 bit 43127a0bc89SDoug Rabson * cluster number into the appropriate bytes in the FAT. 43227a0bc89SDoug Rabson * 43327a0bc89SDoug Rabson * byte m byte m+1 byte m+2 43427a0bc89SDoug Rabson * +----+----+ +----+----+ +----+----+ 43527a0bc89SDoug Rabson * | 0 1 | | 2 3 | | 4 5 | FAT bytes 43627a0bc89SDoug Rabson * +----+----+ +----+----+ +----+----+ 43727a0bc89SDoug Rabson * 43827a0bc89SDoug Rabson * +----+----+----+ +----+----+----+ 43927a0bc89SDoug Rabson * | 3 0 1 | | 4 5 2 | 44027a0bc89SDoug Rabson * +----+----+----+ +----+----+----+ 44127a0bc89SDoug Rabson * cluster n cluster n+1 44227a0bc89SDoug Rabson * 44327a0bc89SDoug Rabson * Where n is even. m = n + (n >> 2) 44427a0bc89SDoug Rabson * 44527a0bc89SDoug Rabson */ 446c1087c13SBruce Evans static __inline void 44727a0bc89SDoug Rabson usemap_alloc(pmp, cn) 44827a0bc89SDoug Rabson struct msdosfsmount *pmp; 44927a0bc89SDoug Rabson u_long cn; 45027a0bc89SDoug Rabson { 451952a6212SJordan K. Hubbard 452952a6212SJordan K. Hubbard pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS); 45327a0bc89SDoug Rabson pmp->pm_freeclustercount--; 45427a0bc89SDoug Rabson } 45527a0bc89SDoug Rabson 456c1087c13SBruce Evans static __inline void 45727a0bc89SDoug Rabson usemap_free(pmp, cn) 45827a0bc89SDoug Rabson struct msdosfsmount *pmp; 45927a0bc89SDoug Rabson u_long cn; 46027a0bc89SDoug Rabson { 461952a6212SJordan K. Hubbard 46227a0bc89SDoug Rabson pmp->pm_freeclustercount++; 46327a0bc89SDoug Rabson pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS)); 46427a0bc89SDoug Rabson } 46527a0bc89SDoug Rabson 46627a0bc89SDoug Rabson int 46727a0bc89SDoug Rabson clusterfree(pmp, cluster, oldcnp) 46827a0bc89SDoug Rabson struct msdosfsmount *pmp; 46927a0bc89SDoug Rabson u_long cluster; 47027a0bc89SDoug Rabson u_long *oldcnp; 47127a0bc89SDoug Rabson { 47227a0bc89SDoug Rabson int error; 47327a0bc89SDoug Rabson u_long oldcn; 47427a0bc89SDoug Rabson 475952a6212SJordan K. Hubbard usemap_free(pmp, cluster); 47627a0bc89SDoug Rabson error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE); 477952a6212SJordan K. Hubbard if (error) { 478952a6212SJordan K. Hubbard usemap_alloc(pmp, cluster); 479952a6212SJordan K. Hubbard return (error); 480952a6212SJordan K. Hubbard } 48127a0bc89SDoug Rabson /* 48227a0bc89SDoug Rabson * If the cluster was successfully marked free, then update 48327a0bc89SDoug Rabson * the count of free clusters, and turn off the "allocated" 48427a0bc89SDoug Rabson * bit in the "in use" cluster bit map. 48527a0bc89SDoug Rabson */ 48627a0bc89SDoug Rabson if (oldcnp) 48727a0bc89SDoug Rabson *oldcnp = oldcn; 488952a6212SJordan K. Hubbard return (0); 48927a0bc89SDoug Rabson } 49027a0bc89SDoug Rabson 49127a0bc89SDoug Rabson /* 49227a0bc89SDoug Rabson * Get or Set or 'Get and Set' the cluster'th entry in the fat. 49327a0bc89SDoug Rabson * 49427a0bc89SDoug Rabson * function - whether to get or set a fat entry 49527a0bc89SDoug Rabson * pmp - address of the msdosfsmount structure for the filesystem 49627a0bc89SDoug Rabson * whose fat is to be manipulated. 49727a0bc89SDoug Rabson * cn - which cluster is of interest 49827a0bc89SDoug Rabson * oldcontents - address of a word that is to receive the contents of the 49927a0bc89SDoug Rabson * cluster'th entry if this is a get function 50027a0bc89SDoug Rabson * newcontents - the new value to be written into the cluster'th element of 50127a0bc89SDoug Rabson * the fat if this is a set function. 50227a0bc89SDoug Rabson * 50327a0bc89SDoug Rabson * This function can also be used to free a cluster by setting the fat entry 50427a0bc89SDoug Rabson * for a cluster to 0. 50527a0bc89SDoug Rabson * 50627a0bc89SDoug Rabson * All copies of the fat are updated if this is a set function. NOTE: If 50727a0bc89SDoug Rabson * fatentry() marks a cluster as free it does not update the inusemap in 50827a0bc89SDoug Rabson * the msdosfsmount structure. This is left to the caller. 50927a0bc89SDoug Rabson */ 51027a0bc89SDoug Rabson int 51127a0bc89SDoug Rabson fatentry(function, pmp, cn, oldcontents, newcontents) 51227a0bc89SDoug Rabson int function; 51327a0bc89SDoug Rabson struct msdosfsmount *pmp; 51427a0bc89SDoug Rabson u_long cn; 51527a0bc89SDoug Rabson u_long *oldcontents; 51627a0bc89SDoug Rabson u_long newcontents; 51727a0bc89SDoug Rabson { 51827a0bc89SDoug Rabson int error; 51927a0bc89SDoug Rabson u_long readcn; 52027a0bc89SDoug Rabson u_long bn, bo, bsize, byteoffset; 52127a0bc89SDoug Rabson struct buf *bp; 52227a0bc89SDoug Rabson 523952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG 524952a6212SJordan K. Hubbard printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n", 525952a6212SJordan K. Hubbard function, pmp, cn, oldcontents, newcontents); 526952a6212SJordan K. Hubbard #endif 52727a0bc89SDoug Rabson 52827a0bc89SDoug Rabson #ifdef DIAGNOSTIC 52927a0bc89SDoug Rabson /* 53027a0bc89SDoug Rabson * Be sure they asked us to do something. 53127a0bc89SDoug Rabson */ 53227a0bc89SDoug Rabson if ((function & (FAT_SET | FAT_GET)) == 0) { 53327a0bc89SDoug Rabson printf("fatentry(): function code doesn't specify get or set\n"); 534952a6212SJordan K. Hubbard return (EINVAL); 53527a0bc89SDoug Rabson } 53627a0bc89SDoug Rabson 53727a0bc89SDoug Rabson /* 53827a0bc89SDoug Rabson * If they asked us to return a cluster number but didn't tell us 53927a0bc89SDoug Rabson * where to put it, give them an error. 54027a0bc89SDoug Rabson */ 54127a0bc89SDoug Rabson if ((function & FAT_GET) && oldcontents == NULL) { 54227a0bc89SDoug Rabson printf("fatentry(): get function with no place to put result\n"); 543952a6212SJordan K. Hubbard return (EINVAL); 54427a0bc89SDoug Rabson } 54527a0bc89SDoug Rabson #endif 54627a0bc89SDoug Rabson 54727a0bc89SDoug Rabson /* 54827a0bc89SDoug Rabson * Be sure the requested cluster is in the filesystem. 54927a0bc89SDoug Rabson */ 55027a0bc89SDoug Rabson if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster) 551952a6212SJordan K. Hubbard return (EINVAL); 55227a0bc89SDoug Rabson 55327a0bc89SDoug Rabson byteoffset = FATOFS(pmp, cn); 55427a0bc89SDoug Rabson fatblock(pmp, byteoffset, &bn, &bsize, &bo); 555c3c6d51eSPoul-Henning Kamp error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 556952a6212SJordan K. Hubbard if (error) { 557952a6212SJordan K. Hubbard brelse(bp); 558952a6212SJordan K. Hubbard return (error); 559952a6212SJordan K. Hubbard } 56027a0bc89SDoug Rabson 56127a0bc89SDoug Rabson if (function & FAT_GET) { 562952a6212SJordan K. Hubbard if (FAT32(pmp)) 563952a6212SJordan K. Hubbard readcn = getulong(&bp->b_data[bo]); 564952a6212SJordan K. Hubbard else 56527a0bc89SDoug Rabson readcn = getushort(&bp->b_data[bo]); 566952a6212SJordan K. Hubbard if (FAT12(pmp) & (cn & 1)) 56727a0bc89SDoug Rabson readcn >>= 4; 568952a6212SJordan K. Hubbard readcn &= pmp->pm_fatmask; 569952a6212SJordan K. Hubbard /* map reserved fat entries to same values for all fats */ 570952a6212SJordan K. Hubbard if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 571952a6212SJordan K. Hubbard readcn |= ~pmp->pm_fatmask; 57227a0bc89SDoug Rabson *oldcontents = readcn; 57327a0bc89SDoug Rabson } 57427a0bc89SDoug Rabson if (function & FAT_SET) { 575952a6212SJordan K. Hubbard switch (pmp->pm_fatmask) { 576952a6212SJordan K. Hubbard case FAT12_MASK: 57727a0bc89SDoug Rabson readcn = getushort(&bp->b_data[bo]); 57827a0bc89SDoug Rabson if (cn & 1) { 57927a0bc89SDoug Rabson readcn &= 0x000f; 58027a0bc89SDoug Rabson readcn |= newcontents << 4; 58127a0bc89SDoug Rabson } else { 58227a0bc89SDoug Rabson readcn &= 0xf000; 58327a0bc89SDoug Rabson readcn |= newcontents & 0xfff; 58427a0bc89SDoug Rabson } 58527a0bc89SDoug Rabson putushort(&bp->b_data[bo], readcn); 586952a6212SJordan K. Hubbard break; 587952a6212SJordan K. Hubbard case FAT16_MASK: 58827a0bc89SDoug Rabson putushort(&bp->b_data[bo], newcontents); 589952a6212SJordan K. Hubbard break; 590952a6212SJordan K. Hubbard case FAT32_MASK: 591952a6212SJordan K. Hubbard /* 592952a6212SJordan K. Hubbard * According to spec we have to retain the 593952a6212SJordan K. Hubbard * high order bits of the fat entry. 594952a6212SJordan K. Hubbard */ 595952a6212SJordan K. Hubbard readcn = getulong(&bp->b_data[bo]); 596952a6212SJordan K. Hubbard readcn &= ~FAT32_MASK; 597952a6212SJordan K. Hubbard readcn |= newcontents & FAT32_MASK; 598952a6212SJordan K. Hubbard putulong(&bp->b_data[bo], readcn); 599952a6212SJordan K. Hubbard break; 600952a6212SJordan K. Hubbard } 60127a0bc89SDoug Rabson updatefats(pmp, bp, bn); 60227a0bc89SDoug Rabson bp = NULL; 60327a0bc89SDoug Rabson pmp->pm_fmod = 1; 60427a0bc89SDoug Rabson } 60527a0bc89SDoug Rabson if (bp) 60627a0bc89SDoug Rabson brelse(bp); 607952a6212SJordan K. Hubbard return (0); 60827a0bc89SDoug Rabson } 60927a0bc89SDoug Rabson 61027a0bc89SDoug Rabson /* 61127a0bc89SDoug Rabson * Update a contiguous cluster chain 61227a0bc89SDoug Rabson * 61327a0bc89SDoug Rabson * pmp - mount point 61427a0bc89SDoug Rabson * start - first cluster of chain 61527a0bc89SDoug Rabson * count - number of clusters in chain 61627a0bc89SDoug Rabson * fillwith - what to write into fat entry of last cluster 61727a0bc89SDoug Rabson */ 61827a0bc89SDoug Rabson static int 61927a0bc89SDoug Rabson fatchain(pmp, start, count, fillwith) 62027a0bc89SDoug Rabson struct msdosfsmount *pmp; 62127a0bc89SDoug Rabson u_long start; 62227a0bc89SDoug Rabson u_long count; 62327a0bc89SDoug Rabson u_long fillwith; 62427a0bc89SDoug Rabson { 62527a0bc89SDoug Rabson int error; 62627a0bc89SDoug Rabson u_long bn, bo, bsize, byteoffset, readcn, newc; 62727a0bc89SDoug Rabson struct buf *bp; 62827a0bc89SDoug Rabson 62927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 630952a6212SJordan K. Hubbard printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n", 63127a0bc89SDoug Rabson pmp, start, count, fillwith); 63227a0bc89SDoug Rabson #endif 63327a0bc89SDoug Rabson /* 63427a0bc89SDoug Rabson * Be sure the clusters are in the filesystem. 63527a0bc89SDoug Rabson */ 63627a0bc89SDoug Rabson if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster) 637952a6212SJordan K. Hubbard return (EINVAL); 63827a0bc89SDoug Rabson 63927a0bc89SDoug Rabson while (count > 0) { 64027a0bc89SDoug Rabson byteoffset = FATOFS(pmp, start); 64127a0bc89SDoug Rabson fatblock(pmp, byteoffset, &bn, &bsize, &bo); 642c3c6d51eSPoul-Henning Kamp error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 643952a6212SJordan K. Hubbard if (error) { 644952a6212SJordan K. Hubbard brelse(bp); 645952a6212SJordan K. Hubbard return (error); 646952a6212SJordan K. Hubbard } 64727a0bc89SDoug Rabson while (count > 0) { 64827a0bc89SDoug Rabson start++; 64927a0bc89SDoug Rabson newc = --count > 0 ? start : fillwith; 650952a6212SJordan K. Hubbard switch (pmp->pm_fatmask) { 651952a6212SJordan K. Hubbard case FAT12_MASK: 65227a0bc89SDoug Rabson readcn = getushort(&bp->b_data[bo]); 65327a0bc89SDoug Rabson if (start & 1) { 65427a0bc89SDoug Rabson readcn &= 0xf000; 65527a0bc89SDoug Rabson readcn |= newc & 0xfff; 65627a0bc89SDoug Rabson } else { 65727a0bc89SDoug Rabson readcn &= 0x000f; 65827a0bc89SDoug Rabson readcn |= newc << 4; 65927a0bc89SDoug Rabson } 66027a0bc89SDoug Rabson putushort(&bp->b_data[bo], readcn); 66127a0bc89SDoug Rabson bo++; 66227a0bc89SDoug Rabson if (!(start & 1)) 66327a0bc89SDoug Rabson bo++; 664952a6212SJordan K. Hubbard break; 665952a6212SJordan K. Hubbard case FAT16_MASK: 66627a0bc89SDoug Rabson putushort(&bp->b_data[bo], newc); 66727a0bc89SDoug Rabson bo += 2; 668952a6212SJordan K. Hubbard break; 669952a6212SJordan K. Hubbard case FAT32_MASK: 670952a6212SJordan K. Hubbard readcn = getulong(&bp->b_data[bo]); 671952a6212SJordan K. Hubbard readcn &= ~pmp->pm_fatmask; 672952a6212SJordan K. Hubbard readcn |= newc & pmp->pm_fatmask; 673952a6212SJordan K. Hubbard putulong(&bp->b_data[bo], readcn); 674952a6212SJordan K. Hubbard bo += 4; 675952a6212SJordan K. Hubbard break; 67627a0bc89SDoug Rabson } 67727a0bc89SDoug Rabson if (bo >= bsize) 67827a0bc89SDoug Rabson break; 67927a0bc89SDoug Rabson } 68027a0bc89SDoug Rabson updatefats(pmp, bp, bn); 68127a0bc89SDoug Rabson } 68227a0bc89SDoug Rabson pmp->pm_fmod = 1; 683952a6212SJordan K. Hubbard return (0); 68427a0bc89SDoug Rabson } 68527a0bc89SDoug Rabson 68627a0bc89SDoug Rabson /* 68727a0bc89SDoug Rabson * Check the length of a free cluster chain starting at start. 68827a0bc89SDoug Rabson * 68927a0bc89SDoug Rabson * pmp - mount point 69027a0bc89SDoug Rabson * start - start of chain 69127a0bc89SDoug Rabson * count - maximum interesting length 69227a0bc89SDoug Rabson */ 6937fefffeeSPoul-Henning Kamp static int 69427a0bc89SDoug Rabson chainlength(pmp, start, count) 69527a0bc89SDoug Rabson struct msdosfsmount *pmp; 69627a0bc89SDoug Rabson u_long start; 69727a0bc89SDoug Rabson u_long count; 69827a0bc89SDoug Rabson { 69927a0bc89SDoug Rabson u_long idx, max_idx; 70027a0bc89SDoug Rabson u_int map; 70127a0bc89SDoug Rabson u_long len; 70227a0bc89SDoug Rabson 70327a0bc89SDoug Rabson max_idx = pmp->pm_maxcluster / N_INUSEBITS; 70427a0bc89SDoug Rabson idx = start / N_INUSEBITS; 70527a0bc89SDoug Rabson start %= N_INUSEBITS; 70627a0bc89SDoug Rabson map = pmp->pm_inusemap[idx]; 70727a0bc89SDoug Rabson map &= ~((1 << start) - 1); 70827a0bc89SDoug Rabson if (map) { 70927a0bc89SDoug Rabson len = ffs(map) - 1 - start; 710952a6212SJordan K. Hubbard return (len > count ? count : len); 71127a0bc89SDoug Rabson } 71227a0bc89SDoug Rabson len = N_INUSEBITS - start; 71327a0bc89SDoug Rabson if (len >= count) 714952a6212SJordan K. Hubbard return (count); 71527a0bc89SDoug Rabson while (++idx <= max_idx) { 71627a0bc89SDoug Rabson if (len >= count) 71727a0bc89SDoug Rabson break; 718c3c6d51eSPoul-Henning Kamp map = pmp->pm_inusemap[idx]; 719c3c6d51eSPoul-Henning Kamp if (map) { 72027a0bc89SDoug Rabson len += ffs(map) - 1; 72127a0bc89SDoug Rabson break; 72227a0bc89SDoug Rabson } 72327a0bc89SDoug Rabson len += N_INUSEBITS; 72427a0bc89SDoug Rabson } 725952a6212SJordan K. Hubbard return (len > count ? count : len); 72627a0bc89SDoug Rabson } 72727a0bc89SDoug Rabson 72827a0bc89SDoug Rabson /* 72927a0bc89SDoug Rabson * Allocate contigous free clusters. 73027a0bc89SDoug Rabson * 73127a0bc89SDoug Rabson * pmp - mount point. 73227a0bc89SDoug Rabson * start - start of cluster chain. 73327a0bc89SDoug Rabson * count - number of clusters to allocate. 73427a0bc89SDoug Rabson * fillwith - put this value into the fat entry for the 73527a0bc89SDoug Rabson * last allocated cluster. 73627a0bc89SDoug Rabson * retcluster - put the first allocated cluster's number here. 73727a0bc89SDoug Rabson * got - how many clusters were actually allocated. 73827a0bc89SDoug Rabson */ 7397fefffeeSPoul-Henning Kamp static int 74027a0bc89SDoug Rabson chainalloc(pmp, start, count, fillwith, retcluster, got) 74127a0bc89SDoug Rabson struct msdosfsmount *pmp; 74227a0bc89SDoug Rabson u_long start; 74327a0bc89SDoug Rabson u_long count; 74427a0bc89SDoug Rabson u_long fillwith; 74527a0bc89SDoug Rabson u_long *retcluster; 74627a0bc89SDoug Rabson u_long *got; 74727a0bc89SDoug Rabson { 74827a0bc89SDoug Rabson int error; 749952a6212SJordan K. Hubbard u_long cl, n; 750952a6212SJordan K. Hubbard 751952a6212SJordan K. Hubbard for (cl = start, n = count; n-- > 0;) 752952a6212SJordan K. Hubbard usemap_alloc(pmp, cl++); 75327a0bc89SDoug Rabson 75427a0bc89SDoug Rabson error = fatchain(pmp, start, count, fillwith); 755952a6212SJordan K. Hubbard if (error != 0) 756952a6212SJordan K. Hubbard return (error); 75727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 758952a6212SJordan K. Hubbard printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n", 75927a0bc89SDoug Rabson start, count); 76027a0bc89SDoug Rabson #endif 76127a0bc89SDoug Rabson if (retcluster) 76227a0bc89SDoug Rabson *retcluster = start; 76327a0bc89SDoug Rabson if (got) 76427a0bc89SDoug Rabson *got = count; 7658e55bfafSBruce Evans pmp->pm_nxtfree = start + count; 7668e55bfafSBruce Evans if (pmp->pm_nxtfree > pmp->pm_maxcluster) 7678e55bfafSBruce Evans pmp->pm_nxtfree = CLUST_FIRST; 768952a6212SJordan K. Hubbard return (0); 76927a0bc89SDoug Rabson } 77027a0bc89SDoug Rabson 77127a0bc89SDoug Rabson /* 77227a0bc89SDoug Rabson * Allocate contiguous free clusters. 77327a0bc89SDoug Rabson * 77427a0bc89SDoug Rabson * pmp - mount point. 77527a0bc89SDoug Rabson * start - preferred start of cluster chain. 77627a0bc89SDoug Rabson * count - number of clusters requested. 77727a0bc89SDoug Rabson * fillwith - put this value into the fat entry for the 77827a0bc89SDoug Rabson * last allocated cluster. 77927a0bc89SDoug Rabson * retcluster - put the first allocated cluster's number here. 78027a0bc89SDoug Rabson * got - how many clusters were actually allocated. 78127a0bc89SDoug Rabson */ 78227a0bc89SDoug Rabson int 78327a0bc89SDoug Rabson clusteralloc(pmp, start, count, fillwith, retcluster, got) 78427a0bc89SDoug Rabson struct msdosfsmount *pmp; 78527a0bc89SDoug Rabson u_long start; 78627a0bc89SDoug Rabson u_long count; 78727a0bc89SDoug Rabson u_long fillwith; 78827a0bc89SDoug Rabson u_long *retcluster; 78927a0bc89SDoug Rabson u_long *got; 79027a0bc89SDoug Rabson { 79127a0bc89SDoug Rabson u_long idx; 792952a6212SJordan K. Hubbard u_long len, newst, foundl, cn, l; 793952a6212SJordan K. Hubbard u_long foundcn = 0; /* XXX: foundcn could be used unititialized */ 79427a0bc89SDoug Rabson u_int map; 79527a0bc89SDoug Rabson 79627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 797952a6212SJordan K. Hubbard printf("clusteralloc(): find %lu clusters\n",count); 79827a0bc89SDoug Rabson #endif 79927a0bc89SDoug Rabson if (start) { 80027a0bc89SDoug Rabson if ((len = chainlength(pmp, start, count)) >= count) 801952a6212SJordan K. Hubbard return (chainalloc(pmp, start, count, fillwith, retcluster, got)); 8027cd5051bSPoul-Henning Kamp } else 80327a0bc89SDoug Rabson len = 0; 80427a0bc89SDoug Rabson 8058e55bfafSBruce Evans newst = pmp->pm_nxtfree; 80627a0bc89SDoug Rabson foundl = 0; 80727a0bc89SDoug Rabson 80827a0bc89SDoug Rabson for (cn = newst; cn <= pmp->pm_maxcluster;) { 80927a0bc89SDoug Rabson idx = cn / N_INUSEBITS; 81027a0bc89SDoug Rabson map = pmp->pm_inusemap[idx]; 81127a0bc89SDoug Rabson map |= (1 << (cn % N_INUSEBITS)) - 1; 81227a0bc89SDoug Rabson if (map != (u_int)-1) { 81327a0bc89SDoug Rabson cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1; 81427a0bc89SDoug Rabson if ((l = chainlength(pmp, cn, count)) >= count) 815952a6212SJordan K. Hubbard return (chainalloc(pmp, cn, count, fillwith, retcluster, got)); 81627a0bc89SDoug Rabson if (l > foundl) { 81727a0bc89SDoug Rabson foundcn = cn; 81827a0bc89SDoug Rabson foundl = l; 81927a0bc89SDoug Rabson } 82027a0bc89SDoug Rabson cn += l + 1; 82127a0bc89SDoug Rabson continue; 82227a0bc89SDoug Rabson } 82327a0bc89SDoug Rabson cn += N_INUSEBITS - cn % N_INUSEBITS; 82427a0bc89SDoug Rabson } 82527a0bc89SDoug Rabson for (cn = 0; cn < newst;) { 82627a0bc89SDoug Rabson idx = cn / N_INUSEBITS; 82727a0bc89SDoug Rabson map = pmp->pm_inusemap[idx]; 82827a0bc89SDoug Rabson map |= (1 << (cn % N_INUSEBITS)) - 1; 82927a0bc89SDoug Rabson if (map != (u_int)-1) { 83027a0bc89SDoug Rabson cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1; 83127a0bc89SDoug Rabson if ((l = chainlength(pmp, cn, count)) >= count) 832952a6212SJordan K. Hubbard return (chainalloc(pmp, cn, count, fillwith, retcluster, got)); 83327a0bc89SDoug Rabson if (l > foundl) { 83427a0bc89SDoug Rabson foundcn = cn; 83527a0bc89SDoug Rabson foundl = l; 83627a0bc89SDoug Rabson } 83727a0bc89SDoug Rabson cn += l + 1; 83827a0bc89SDoug Rabson continue; 83927a0bc89SDoug Rabson } 84027a0bc89SDoug Rabson cn += N_INUSEBITS - cn % N_INUSEBITS; 84127a0bc89SDoug Rabson } 84227a0bc89SDoug Rabson 84327a0bc89SDoug Rabson if (!foundl) 844952a6212SJordan K. Hubbard return (ENOSPC); 84527a0bc89SDoug Rabson 84627a0bc89SDoug Rabson if (len) 847952a6212SJordan K. Hubbard return (chainalloc(pmp, start, len, fillwith, retcluster, got)); 84827a0bc89SDoug Rabson else 849952a6212SJordan K. Hubbard return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got)); 85027a0bc89SDoug Rabson } 85127a0bc89SDoug Rabson 85227a0bc89SDoug Rabson 85327a0bc89SDoug Rabson /* 85427a0bc89SDoug Rabson * Free a chain of clusters. 85527a0bc89SDoug Rabson * 85627a0bc89SDoug Rabson * pmp - address of the msdosfs mount structure for the filesystem 85727a0bc89SDoug Rabson * containing the cluster chain to be freed. 85827a0bc89SDoug Rabson * startcluster - number of the 1st cluster in the chain of clusters to be 85927a0bc89SDoug Rabson * freed. 86027a0bc89SDoug Rabson */ 86127a0bc89SDoug Rabson int 86227a0bc89SDoug Rabson freeclusterchain(pmp, cluster) 86327a0bc89SDoug Rabson struct msdosfsmount *pmp; 86427a0bc89SDoug Rabson u_long cluster; 86527a0bc89SDoug Rabson { 866952a6212SJordan K. Hubbard int error; 86727a0bc89SDoug Rabson struct buf *bp = NULL; 86827a0bc89SDoug Rabson u_long bn, bo, bsize, byteoffset; 86927a0bc89SDoug Rabson u_long readcn, lbn = -1; 87027a0bc89SDoug Rabson 87127a0bc89SDoug Rabson while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) { 87227a0bc89SDoug Rabson byteoffset = FATOFS(pmp, cluster); 87327a0bc89SDoug Rabson fatblock(pmp, byteoffset, &bn, &bsize, &bo); 87427a0bc89SDoug Rabson if (lbn != bn) { 87527a0bc89SDoug Rabson if (bp) 87624d4540cSBruce Evans updatefats(pmp, bp, lbn); 877c3c6d51eSPoul-Henning Kamp error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 878952a6212SJordan K. Hubbard if (error) { 879952a6212SJordan K. Hubbard brelse(bp); 880952a6212SJordan K. Hubbard return (error); 881952a6212SJordan K. Hubbard } 88227a0bc89SDoug Rabson lbn = bn; 88327a0bc89SDoug Rabson } 88427a0bc89SDoug Rabson usemap_free(pmp, cluster); 885952a6212SJordan K. Hubbard switch (pmp->pm_fatmask) { 886952a6212SJordan K. Hubbard case FAT12_MASK: 88727a0bc89SDoug Rabson readcn = getushort(&bp->b_data[bo]); 88827a0bc89SDoug Rabson if (cluster & 1) { 88927a0bc89SDoug Rabson cluster = readcn >> 4; 89027a0bc89SDoug Rabson readcn &= 0x000f; 89127a0bc89SDoug Rabson readcn |= MSDOSFSFREE << 4; 89227a0bc89SDoug Rabson } else { 89327a0bc89SDoug Rabson cluster = readcn; 89427a0bc89SDoug Rabson readcn &= 0xf000; 89527a0bc89SDoug Rabson readcn |= MSDOSFSFREE & 0xfff; 89627a0bc89SDoug Rabson } 89727a0bc89SDoug Rabson putushort(&bp->b_data[bo], readcn); 898952a6212SJordan K. Hubbard break; 899952a6212SJordan K. Hubbard case FAT16_MASK: 900952a6212SJordan K. Hubbard cluster = getushort(&bp->b_data[bo]); 90127a0bc89SDoug Rabson putushort(&bp->b_data[bo], MSDOSFSFREE); 902952a6212SJordan K. Hubbard break; 903952a6212SJordan K. Hubbard case FAT32_MASK: 904952a6212SJordan K. Hubbard cluster = getulong(&bp->b_data[bo]); 905952a6212SJordan K. Hubbard putulong(&bp->b_data[bo], 906952a6212SJordan K. Hubbard (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK)); 907952a6212SJordan K. Hubbard break; 90827a0bc89SDoug Rabson } 909952a6212SJordan K. Hubbard cluster &= pmp->pm_fatmask; 910952a6212SJordan K. Hubbard if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD) 911952a6212SJordan K. Hubbard cluster |= pmp->pm_fatmask; 91227a0bc89SDoug Rabson } 91327a0bc89SDoug Rabson if (bp) 91427a0bc89SDoug Rabson updatefats(pmp, bp, bn); 915952a6212SJordan K. Hubbard return (0); 91627a0bc89SDoug Rabson } 91727a0bc89SDoug Rabson 91827a0bc89SDoug Rabson /* 91927a0bc89SDoug Rabson * Read in fat blocks looking for free clusters. For every free cluster 92027a0bc89SDoug Rabson * found turn off its corresponding bit in the pm_inusemap. 92127a0bc89SDoug Rabson */ 92227a0bc89SDoug Rabson int 92327a0bc89SDoug Rabson fillinusemap(pmp) 92427a0bc89SDoug Rabson struct msdosfsmount *pmp; 92527a0bc89SDoug Rabson { 92627a0bc89SDoug Rabson struct buf *bp = NULL; 92727a0bc89SDoug Rabson u_long cn, readcn; 92827a0bc89SDoug Rabson int error; 92927a0bc89SDoug Rabson u_long bn, bo, bsize, byteoffset; 93027a0bc89SDoug Rabson 93127a0bc89SDoug Rabson /* 93227a0bc89SDoug Rabson * Mark all clusters in use, we mark the free ones in the fat scan 93327a0bc89SDoug Rabson * loop further down. 93427a0bc89SDoug Rabson */ 93527a0bc89SDoug Rabson for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++) 93627a0bc89SDoug Rabson pmp->pm_inusemap[cn] = (u_int)-1; 93727a0bc89SDoug Rabson 93827a0bc89SDoug Rabson /* 93927a0bc89SDoug Rabson * Figure how many free clusters are in the filesystem by ripping 94027a0bc89SDoug Rabson * through the fat counting the number of entries whose content is 94127a0bc89SDoug Rabson * zero. These represent free clusters. 94227a0bc89SDoug Rabson */ 94327a0bc89SDoug Rabson pmp->pm_freeclustercount = 0; 94427a0bc89SDoug Rabson for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) { 94527a0bc89SDoug Rabson byteoffset = FATOFS(pmp, cn); 94627a0bc89SDoug Rabson bo = byteoffset % pmp->pm_fatblocksize; 94727a0bc89SDoug Rabson if (!bo || !bp) { 94827a0bc89SDoug Rabson /* Read new FAT block */ 94927a0bc89SDoug Rabson if (bp) 95027a0bc89SDoug Rabson brelse(bp); 95127a0bc89SDoug Rabson fatblock(pmp, byteoffset, &bn, &bsize, NULL); 95227a0bc89SDoug Rabson error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 953952a6212SJordan K. Hubbard if (error) { 954952a6212SJordan K. Hubbard brelse(bp); 955952a6212SJordan K. Hubbard return (error); 95627a0bc89SDoug Rabson } 957952a6212SJordan K. Hubbard } 958952a6212SJordan K. Hubbard if (FAT32(pmp)) 959952a6212SJordan K. Hubbard readcn = getulong(&bp->b_data[bo]); 960952a6212SJordan K. Hubbard else 96127a0bc89SDoug Rabson readcn = getushort(&bp->b_data[bo]); 962952a6212SJordan K. Hubbard if (FAT12(pmp) && (cn & 1)) 96327a0bc89SDoug Rabson readcn >>= 4; 964952a6212SJordan K. Hubbard readcn &= pmp->pm_fatmask; 96527a0bc89SDoug Rabson 96627a0bc89SDoug Rabson if (readcn == 0) 96727a0bc89SDoug Rabson usemap_free(pmp, cn); 96827a0bc89SDoug Rabson } 96927a0bc89SDoug Rabson brelse(bp); 970952a6212SJordan K. Hubbard return (0); 97127a0bc89SDoug Rabson } 97227a0bc89SDoug Rabson 97327a0bc89SDoug Rabson /* 97427a0bc89SDoug Rabson * Allocate a new cluster and chain it onto the end of the file. 97527a0bc89SDoug Rabson * 97627a0bc89SDoug Rabson * dep - the file to extend 97727a0bc89SDoug Rabson * count - number of clusters to allocate 97827a0bc89SDoug Rabson * bpp - where to return the address of the buf header for the first new 97927a0bc89SDoug Rabson * file block 98027a0bc89SDoug Rabson * ncp - where to put cluster number of the first newly allocated cluster 98127a0bc89SDoug Rabson * If this pointer is 0, do not return the cluster number. 98227a0bc89SDoug Rabson * flags - see fat.h 98327a0bc89SDoug Rabson * 98427a0bc89SDoug Rabson * NOTE: This function is not responsible for turning on the DE_UPDATE bit of 98527a0bc89SDoug Rabson * the de_flag field of the denode and it does not change the de_FileSize 98627a0bc89SDoug Rabson * field. This is left for the caller to do. 98727a0bc89SDoug Rabson */ 98827a0bc89SDoug Rabson int 98927a0bc89SDoug Rabson extendfile(dep, count, bpp, ncp, flags) 99027a0bc89SDoug Rabson struct denode *dep; 99127a0bc89SDoug Rabson u_long count; 99227a0bc89SDoug Rabson struct buf **bpp; 99327a0bc89SDoug Rabson u_long *ncp; 99427a0bc89SDoug Rabson int flags; 99527a0bc89SDoug Rabson { 996952a6212SJordan K. Hubbard int error; 99727a0bc89SDoug Rabson u_long frcn; 99827a0bc89SDoug Rabson u_long cn, got; 99927a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 100027a0bc89SDoug Rabson struct buf *bp; 10010d2af521SKirk McKusick daddr_t blkno; 100227a0bc89SDoug Rabson 100327a0bc89SDoug Rabson /* 100427a0bc89SDoug Rabson * Don't try to extend the root directory 100527a0bc89SDoug Rabson */ 1006952a6212SJordan K. Hubbard if (dep->de_StartCluster == MSDOSFSROOT 1007952a6212SJordan K. Hubbard && (dep->de_Attributes & ATTR_DIRECTORY)) { 100827a0bc89SDoug Rabson printf("extendfile(): attempt to extend root directory\n"); 1009952a6212SJordan K. Hubbard return (ENOSPC); 101027a0bc89SDoug Rabson } 101127a0bc89SDoug Rabson 101227a0bc89SDoug Rabson /* 101327a0bc89SDoug Rabson * If the "file's last cluster" cache entry is empty, and the file 101427a0bc89SDoug Rabson * is not empty, then fill the cache entry by calling pcbmap(). 101527a0bc89SDoug Rabson */ 101627a0bc89SDoug Rabson fc_fileextends++; 101727a0bc89SDoug Rabson if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY && 101827a0bc89SDoug Rabson dep->de_StartCluster != 0) { 101927a0bc89SDoug Rabson fc_lfcempty++; 1020952a6212SJordan K. Hubbard error = pcbmap(dep, 0xffff, 0, &cn, 0); 102127a0bc89SDoug Rabson /* we expect it to return E2BIG */ 102227a0bc89SDoug Rabson if (error != E2BIG) 1023952a6212SJordan K. Hubbard return (error); 102427a0bc89SDoug Rabson } 102527a0bc89SDoug Rabson 1026752945d6STom Rhodes fc_last_to_nexttolast(dep); 102727a0bc89SDoug Rabson while (count > 0) { 102827a0bc89SDoug Rabson /* 1029952a6212SJordan K. Hubbard * Allocate a new cluster chain and cat onto the end of the 1030952a6212SJordan K. Hubbard * file. * If the file is empty we make de_StartCluster point 1031952a6212SJordan K. Hubbard * to the new block. Note that de_StartCluster being 0 is 1032952a6212SJordan K. Hubbard * sufficient to be sure the file is empty since we exclude 1033952a6212SJordan K. Hubbard * attempts to extend the root directory above, and the root 1034952a6212SJordan K. Hubbard * dir is the only file with a startcluster of 0 that has 1035952a6212SJordan K. Hubbard * blocks allocated (sort of). 103627a0bc89SDoug Rabson */ 103727a0bc89SDoug Rabson if (dep->de_StartCluster == 0) 103827a0bc89SDoug Rabson cn = 0; 103927a0bc89SDoug Rabson else 104027a0bc89SDoug Rabson cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1; 1041c3c6d51eSPoul-Henning Kamp error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got); 1042c3c6d51eSPoul-Henning Kamp if (error) 1043952a6212SJordan K. Hubbard return (error); 104427a0bc89SDoug Rabson 104527a0bc89SDoug Rabson count -= got; 104627a0bc89SDoug Rabson 104727a0bc89SDoug Rabson /* 104827a0bc89SDoug Rabson * Give them the filesystem relative cluster number if they want 104927a0bc89SDoug Rabson * it. 105027a0bc89SDoug Rabson */ 105127a0bc89SDoug Rabson if (ncp) { 105227a0bc89SDoug Rabson *ncp = cn; 105327a0bc89SDoug Rabson ncp = NULL; 105427a0bc89SDoug Rabson } 105527a0bc89SDoug Rabson 105627a0bc89SDoug Rabson if (dep->de_StartCluster == 0) { 105727a0bc89SDoug Rabson dep->de_StartCluster = cn; 105827a0bc89SDoug Rabson frcn = 0; 105927a0bc89SDoug Rabson } else { 1060952a6212SJordan K. Hubbard error = fatentry(FAT_SET, pmp, 1061952a6212SJordan K. Hubbard dep->de_fc[FC_LASTFC].fc_fsrcn, 106227a0bc89SDoug Rabson 0, cn); 106327a0bc89SDoug Rabson if (error) { 106427a0bc89SDoug Rabson clusterfree(pmp, cn, NULL); 1065952a6212SJordan K. Hubbard return (error); 106627a0bc89SDoug Rabson } 106727a0bc89SDoug Rabson frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1; 106827a0bc89SDoug Rabson } 106927a0bc89SDoug Rabson 107027a0bc89SDoug Rabson /* 107127a0bc89SDoug Rabson * Update the "last cluster of the file" entry in the denode's fat 107227a0bc89SDoug Rabson * cache. 107327a0bc89SDoug Rabson */ 107427a0bc89SDoug Rabson fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1); 107527a0bc89SDoug Rabson 107627a0bc89SDoug Rabson if (flags & DE_CLEAR) { 107727a0bc89SDoug Rabson while (got-- > 0) { 107827a0bc89SDoug Rabson /* 107927a0bc89SDoug Rabson * Get the buf header for the new block of the file. 108027a0bc89SDoug Rabson */ 108127a0bc89SDoug Rabson if (dep->de_Attributes & ATTR_DIRECTORY) 10827261f5f6SJeff Roberson bp = getblk(pmp->pm_devvp, 10837261f5f6SJeff Roberson cntobn(pmp, cn++), 10847261f5f6SJeff Roberson pmp->pm_bpcluster, 0, 0, 0); 108527a0bc89SDoug Rabson else { 10867261f5f6SJeff Roberson bp = getblk(DETOV(dep), 10877261f5f6SJeff Roberson de_cn2bn(pmp, frcn++), 10887261f5f6SJeff Roberson pmp->pm_bpcluster, 0, 0, 0); 108927a0bc89SDoug Rabson /* 109027a0bc89SDoug Rabson * Do the bmap now, as in msdosfs_write 109127a0bc89SDoug Rabson */ 1092952a6212SJordan K. Hubbard if (pcbmap(dep, 1093952a6212SJordan K. Hubbard de_bn2cn(pmp, bp->b_lblkno), 10940d2af521SKirk McKusick &blkno, 0, 0)) 109527a0bc89SDoug Rabson bp->b_blkno = -1; 109627a0bc89SDoug Rabson if (bp->b_blkno == -1) 109727a0bc89SDoug Rabson panic("extendfile: pcbmap"); 10980d2af521SKirk McKusick else 10990d2af521SKirk McKusick bp->b_blkno = blkno; 110027a0bc89SDoug Rabson } 1101d34b0a1bSBruce Evans vfs_bio_clrbuf(bp); 110227a0bc89SDoug Rabson if (bpp) { 110327a0bc89SDoug Rabson *bpp = bp; 110427a0bc89SDoug Rabson bpp = NULL; 1105952a6212SJordan K. Hubbard } else 1106952a6212SJordan K. Hubbard bdwrite(bp); 110727a0bc89SDoug Rabson } 110827a0bc89SDoug Rabson } 110927a0bc89SDoug Rabson } 111027a0bc89SDoug Rabson 1111952a6212SJordan K. Hubbard return (0); 111227a0bc89SDoug Rabson } 1113cede1f56STom Rhodes 1114392dbea3SBruce Evans /*- 1115392dbea3SBruce Evans * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by 1116392dbea3SBruce Evans * manipulating the upper bit of the FAT entry for cluster 1. Note that 1117392dbea3SBruce Evans * this bit is not defined for FAT12 volumes, which are always assumed to 1118392dbea3SBruce Evans * be dirty. 1119cede1f56STom Rhodes * 1120392dbea3SBruce Evans * The fatentry() routine only works on cluster numbers that a file could 1121392dbea3SBruce Evans * occupy, so it won't manipulate the entry for cluster 1. So we have to do 1122392dbea3SBruce Evans * it here. The code was stolen from fatentry() and tailored for cluster 1. 1123cede1f56STom Rhodes * 1124cede1f56STom Rhodes * Inputs: 1125cede1f56STom Rhodes * pmp The MS-DOS volume to mark 1126392dbea3SBruce Evans * dirty Non-zero if the volume should be marked dirty; zero if it 1127392dbea3SBruce Evans * should be marked clean 1128cede1f56STom Rhodes * 1129cede1f56STom Rhodes * Result: 1130cede1f56STom Rhodes * 0 Success 1131cede1f56STom Rhodes * EROFS Volume is read-only 1132cede1f56STom Rhodes * ? (other errors from called routines) 1133cede1f56STom Rhodes */ 1134392dbea3SBruce Evans int 1135392dbea3SBruce Evans markvoldirty(struct msdosfsmount *pmp, int dirty) 1136cede1f56STom Rhodes { 1137cede1f56STom Rhodes struct buf *bp; 1138392dbea3SBruce Evans u_long bn, bo, bsize, byteoffset, fatval; 1139392dbea3SBruce Evans int error; 1140cede1f56STom Rhodes 1141392dbea3SBruce Evans /* 1142392dbea3SBruce Evans * FAT12 does not support a "clean" bit, so don't do anything for 1143392dbea3SBruce Evans * FAT12. 1144392dbea3SBruce Evans */ 1145cede1f56STom Rhodes if (FAT12(pmp)) 1146392dbea3SBruce Evans return (0); 1147cede1f56STom Rhodes 1148392dbea3SBruce Evans /* Can't change the bit on a read-only filesystem. */ 1149cede1f56STom Rhodes if (pmp->pm_flags & MSDOSFSMNT_RONLY) 1150392dbea3SBruce Evans return (EROFS); 1151cede1f56STom Rhodes 1152392dbea3SBruce Evans /* 1153392dbea3SBruce Evans * Fetch the block containing the FAT entry. It is given by the 1154392dbea3SBruce Evans * pseudo-cluster 1. 1155392dbea3SBruce Evans */ 1156392dbea3SBruce Evans byteoffset = FATOFS(pmp, 1); 1157cede1f56STom Rhodes fatblock(pmp, byteoffset, &bn, &bsize, &bo); 1158cede1f56STom Rhodes error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 1159cede1f56STom Rhodes if (error) { 1160cede1f56STom Rhodes brelse(bp); 1161cede1f56STom Rhodes return (error); 1162cede1f56STom Rhodes } 1163cede1f56STom Rhodes 1164392dbea3SBruce Evans /* 1165392dbea3SBruce Evans * Get the current value of the FAT entry and set/clear the relevant 1166392dbea3SBruce Evans * bit. Dirty means clear the "clean" bit; clean means set the 1167392dbea3SBruce Evans * "clean" bit. 1168392dbea3SBruce Evans */ 1169cede1f56STom Rhodes if (FAT32(pmp)) { 1170392dbea3SBruce Evans /* FAT32 uses bit 27. */ 1171cede1f56STom Rhodes fatval = getulong(&bp->b_data[bo]); 1172cede1f56STom Rhodes if (dirty) 1173392dbea3SBruce Evans fatval &= 0xF7FFFFFF; 1174cede1f56STom Rhodes else 1175392dbea3SBruce Evans fatval |= 0x08000000; 1176cede1f56STom Rhodes putulong(&bp->b_data[bo], fatval); 1177392dbea3SBruce Evans } else { 1178392dbea3SBruce Evans /* Must be FAT16; use bit 15. */ 1179cede1f56STom Rhodes fatval = getushort(&bp->b_data[bo]); 1180cede1f56STom Rhodes if (dirty) 1181392dbea3SBruce Evans fatval &= 0x7FFF; 1182cede1f56STom Rhodes else 1183392dbea3SBruce Evans fatval |= 0x8000; 1184cede1f56STom Rhodes putushort(&bp->b_data[bo], fatval); 1185cede1f56STom Rhodes } 1186cede1f56STom Rhodes 1187392dbea3SBruce Evans /* Write out the modified FAT block synchronously. */ 1188392dbea3SBruce Evans return (bwrite(bp)); 1189cede1f56STom Rhodes } 1190