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 6311caded3SAlfred Perlstein static int chainalloc(struct msdosfsmount *pmp, u_long start, 64b76d0b32SBruce Evans u_long count, u_long fillwith, u_long *retcluster, 65b76d0b32SBruce Evans u_long *got); 6611caded3SAlfred Perlstein static int chainlength(struct msdosfsmount *pmp, u_long start, 6711caded3SAlfred Perlstein u_long count); 68b76d0b32SBruce Evans static void fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, 69b76d0b32SBruce Evans u_long *sizep, u_long *bop); 70b76d0b32SBruce Evans static int fatchain(struct msdosfsmount *pmp, u_long start, u_long count, 71b76d0b32SBruce Evans u_long fillwith); 72b76d0b32SBruce Evans static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, 73b76d0b32SBruce Evans u_long *fsrcnp); 7411caded3SAlfred Perlstein static void updatefats(struct msdosfsmount *pmp, struct buf *bp, 7511caded3SAlfred Perlstein u_long fatbn); 76c1087c13SBruce Evans static __inline void 7711caded3SAlfred Perlstein usemap_alloc(struct msdosfsmount *pmp, u_long cn); 78c1087c13SBruce Evans static __inline void 7911caded3SAlfred Perlstein usemap_free(struct msdosfsmount *pmp, u_long cn); 806be1a4ccSKonstantin Belousov static int clusteralloc1(struct msdosfsmount *pmp, u_long start, 816be1a4ccSKonstantin Belousov u_long count, u_long fillwith, u_long *retcluster, 826be1a4ccSKonstantin Belousov u_long *got); 8358c27bcfSBruce Evans 8427a0bc89SDoug Rabson static void 8510c9700fSEd Maste fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep, 8610c9700fSEd Maste u_long *bop) 8727a0bc89SDoug Rabson { 8827a0bc89SDoug Rabson u_long bn, size; 8927a0bc89SDoug Rabson 9027a0bc89SDoug Rabson bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec; 9127a0bc89SDoug Rabson size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn) 9201f6cfbaSYoshihiro Takahashi * DEV_BSIZE; 93952a6212SJordan K. Hubbard bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs; 94952a6212SJordan K. Hubbard 9527a0bc89SDoug Rabson if (bnp) 9627a0bc89SDoug Rabson *bnp = bn; 9727a0bc89SDoug Rabson if (sizep) 9827a0bc89SDoug Rabson *sizep = size; 9927a0bc89SDoug Rabson if (bop) 10027a0bc89SDoug Rabson *bop = ofs % pmp->pm_fatblocksize; 10127a0bc89SDoug Rabson } 10227a0bc89SDoug Rabson 10327a0bc89SDoug Rabson /* 10427a0bc89SDoug Rabson * Map the logical cluster number of a file into a physical disk sector 10527a0bc89SDoug Rabson * that is filesystem relative. 10627a0bc89SDoug Rabson * 10727a0bc89SDoug Rabson * dep - address of denode representing the file of interest 10827a0bc89SDoug Rabson * findcn - file relative cluster whose filesystem relative cluster number 10927a0bc89SDoug Rabson * and/or block number are/is to be found 11027a0bc89SDoug Rabson * bnp - address of where to place the filesystem relative block number. 11127a0bc89SDoug Rabson * If this pointer is null then don't return this quantity. 11227a0bc89SDoug Rabson * cnp - address of where to place the filesystem relative cluster number. 11327a0bc89SDoug Rabson * If this pointer is null then don't return this quantity. 1144882501bSEd Maste * sp - pointer to returned block size 11527a0bc89SDoug Rabson * 11627a0bc89SDoug Rabson * NOTE: Either bnp or cnp must be non-null. 11727a0bc89SDoug Rabson * This function has one side effect. If the requested file relative cluster 11827a0bc89SDoug Rabson * is beyond the end of file, then the actual number of clusters in the file 11927a0bc89SDoug Rabson * is returned in *cnp. This is useful for determining how long a directory is. 12027a0bc89SDoug Rabson * If cnp is null, nothing is returned. 12127a0bc89SDoug Rabson */ 12227a0bc89SDoug Rabson int 12310c9700fSEd Maste pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp) 12427a0bc89SDoug Rabson { 12527a0bc89SDoug Rabson int error; 12627a0bc89SDoug Rabson u_long i; 12727a0bc89SDoug Rabson u_long cn; 128952a6212SJordan K. Hubbard u_long prevcn = 0; /* XXX: prevcn could be used unititialized */ 12927a0bc89SDoug Rabson u_long byteoffset; 13027a0bc89SDoug Rabson u_long bn; 13127a0bc89SDoug Rabson u_long bo; 13227a0bc89SDoug Rabson struct buf *bp = NULL; 13327a0bc89SDoug Rabson u_long bp_bn = -1; 13427a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 13527a0bc89SDoug Rabson u_long bsize; 13627a0bc89SDoug Rabson 137ef6a2be3SKonstantin Belousov KASSERT(bnp != NULL || cnp != NULL || sp != NULL, 138ef6a2be3SKonstantin Belousov ("pcbmap: extra call")); 139ef6a2be3SKonstantin Belousov ASSERT_VOP_ELOCKED(DETOV(dep), "pcbmap"); 14027a0bc89SDoug Rabson 14127a0bc89SDoug Rabson cn = dep->de_StartCluster; 14227a0bc89SDoug Rabson /* 14327a0bc89SDoug Rabson * The "file" that makes up the root directory is contiguous, 14427a0bc89SDoug Rabson * permanently allocated, of fixed size, and is not made up of 14527a0bc89SDoug Rabson * clusters. If the cluster number is beyond the end of the root 14627a0bc89SDoug Rabson * directory, then return the number of clusters in the file. 14727a0bc89SDoug Rabson */ 14827a0bc89SDoug Rabson if (cn == MSDOSFSROOT) { 14927a0bc89SDoug Rabson if (dep->de_Attributes & ATTR_DIRECTORY) { 150952a6212SJordan K. Hubbard if (de_cn2off(pmp, findcn) >= dep->de_FileSize) { 15127a0bc89SDoug Rabson if (cnp) 152952a6212SJordan K. Hubbard *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize); 153952a6212SJordan K. Hubbard return (E2BIG); 15427a0bc89SDoug Rabson } 15527a0bc89SDoug Rabson if (bnp) 156952a6212SJordan K. Hubbard *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn); 15727a0bc89SDoug Rabson if (cnp) 15827a0bc89SDoug Rabson *cnp = MSDOSFSROOT; 159952a6212SJordan K. Hubbard if (sp) 160952a6212SJordan K. Hubbard *sp = min(pmp->pm_bpcluster, 161952a6212SJordan K. Hubbard dep->de_FileSize - de_cn2off(pmp, findcn)); 162952a6212SJordan K. Hubbard return (0); 16327a0bc89SDoug Rabson } else { /* just an empty file */ 16427a0bc89SDoug Rabson if (cnp) 16527a0bc89SDoug Rabson *cnp = 0; 166952a6212SJordan K. Hubbard return (E2BIG); 16727a0bc89SDoug Rabson } 16827a0bc89SDoug Rabson } 16927a0bc89SDoug Rabson 17027a0bc89SDoug Rabson /* 171952a6212SJordan K. Hubbard * All other files do I/O in cluster sized blocks 172952a6212SJordan K. Hubbard */ 173952a6212SJordan K. Hubbard if (sp) 174952a6212SJordan K. Hubbard *sp = pmp->pm_bpcluster; 175952a6212SJordan K. Hubbard 176952a6212SJordan K. Hubbard /* 17727a0bc89SDoug Rabson * Rummage around in the fat cache, maybe we can avoid tromping 17827a0bc89SDoug Rabson * thru every fat entry for the file. And, keep track of how far 17927a0bc89SDoug Rabson * off the cache was from where we wanted to be. 18027a0bc89SDoug Rabson */ 18127a0bc89SDoug Rabson i = 0; 18227a0bc89SDoug Rabson fc_lookup(dep, findcn, &i, &cn); 18327a0bc89SDoug Rabson 18427a0bc89SDoug Rabson /* 18527a0bc89SDoug Rabson * Handle all other files or directories the normal way. 18627a0bc89SDoug Rabson */ 18727a0bc89SDoug Rabson for (; i < findcn; i++) { 188952a6212SJordan K. Hubbard /* 189952a6212SJordan K. Hubbard * Stop with all reserved clusters, not just with EOF. 190952a6212SJordan K. Hubbard */ 191952a6212SJordan K. Hubbard if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 19227a0bc89SDoug Rabson goto hiteof; 19327a0bc89SDoug Rabson byteoffset = FATOFS(pmp, cn); 19427a0bc89SDoug Rabson fatblock(pmp, byteoffset, &bn, &bsize, &bo); 19527a0bc89SDoug Rabson if (bn != bp_bn) { 19627a0bc89SDoug Rabson if (bp) 19727a0bc89SDoug Rabson brelse(bp); 19827a0bc89SDoug Rabson error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 199952a6212SJordan K. Hubbard if (error) { 200952a6212SJordan K. Hubbard brelse(bp); 201952a6212SJordan K. Hubbard return (error); 202952a6212SJordan K. Hubbard } 20327a0bc89SDoug Rabson bp_bn = bn; 20427a0bc89SDoug Rabson } 20527a0bc89SDoug Rabson prevcn = cn; 206d23af19aSTim J. Robbins if (bo >= bsize) { 207d23af19aSTim J. Robbins if (bp) 208d23af19aSTim J. Robbins brelse(bp); 209d23af19aSTim J. Robbins return (EIO); 210d23af19aSTim J. Robbins } 211952a6212SJordan K. Hubbard if (FAT32(pmp)) 212952a6212SJordan K. Hubbard cn = getulong(&bp->b_data[bo]); 213952a6212SJordan K. Hubbard else 21427a0bc89SDoug Rabson cn = getushort(&bp->b_data[bo]); 215952a6212SJordan K. Hubbard if (FAT12(pmp) && (prevcn & 1)) 21627a0bc89SDoug Rabson cn >>= 4; 217952a6212SJordan K. Hubbard cn &= pmp->pm_fatmask; 218952a6212SJordan K. Hubbard 21927a0bc89SDoug Rabson /* 220952a6212SJordan K. Hubbard * Force the special cluster numbers 221952a6212SJordan K. Hubbard * to be the same for all cluster sizes 222952a6212SJordan K. Hubbard * to let the rest of msdosfs handle 223952a6212SJordan K. Hubbard * all cases the same. 22427a0bc89SDoug Rabson */ 225952a6212SJordan K. Hubbard if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 226952a6212SJordan K. Hubbard cn |= ~pmp->pm_fatmask; 22727a0bc89SDoug Rabson } 22827a0bc89SDoug Rabson 229952a6212SJordan K. Hubbard if (!MSDOSFSEOF(pmp, cn)) { 23027a0bc89SDoug Rabson if (bp) 23127a0bc89SDoug Rabson brelse(bp); 23227a0bc89SDoug Rabson if (bnp) 23327a0bc89SDoug Rabson *bnp = cntobn(pmp, cn); 23427a0bc89SDoug Rabson if (cnp) 23527a0bc89SDoug Rabson *cnp = cn; 23627a0bc89SDoug Rabson fc_setcache(dep, FC_LASTMAP, i, cn); 237952a6212SJordan K. Hubbard return (0); 23827a0bc89SDoug Rabson } 23927a0bc89SDoug Rabson 24027a0bc89SDoug Rabson hiteof:; 24127a0bc89SDoug Rabson if (cnp) 24227a0bc89SDoug Rabson *cnp = i; 24327a0bc89SDoug Rabson if (bp) 24427a0bc89SDoug Rabson brelse(bp); 24527a0bc89SDoug Rabson /* update last file cluster entry in the fat cache */ 24627a0bc89SDoug Rabson fc_setcache(dep, FC_LASTFC, i - 1, prevcn); 247952a6212SJordan K. Hubbard return (E2BIG); 24827a0bc89SDoug Rabson } 24927a0bc89SDoug Rabson 25027a0bc89SDoug Rabson /* 25127a0bc89SDoug Rabson * Find the closest entry in the fat cache to the cluster we are looking 25227a0bc89SDoug Rabson * for. 25327a0bc89SDoug Rabson */ 2547fefffeeSPoul-Henning Kamp static void 25510c9700fSEd Maste fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp) 25627a0bc89SDoug Rabson { 25727a0bc89SDoug Rabson int i; 25827a0bc89SDoug Rabson u_long cn; 25927a0bc89SDoug Rabson struct fatcache *closest = 0; 26027a0bc89SDoug Rabson 261ef6a2be3SKonstantin Belousov ASSERT_VOP_LOCKED(DETOV(dep), "fc_lookup"); 262ef6a2be3SKonstantin Belousov 26327a0bc89SDoug Rabson for (i = 0; i < FC_SIZE; i++) { 26427a0bc89SDoug Rabson cn = dep->de_fc[i].fc_frcn; 26527a0bc89SDoug Rabson if (cn != FCE_EMPTY && cn <= findcn) { 26627a0bc89SDoug Rabson if (closest == 0 || cn > closest->fc_frcn) 26727a0bc89SDoug Rabson closest = &dep->de_fc[i]; 26827a0bc89SDoug Rabson } 26927a0bc89SDoug Rabson } 27027a0bc89SDoug Rabson if (closest) { 27127a0bc89SDoug Rabson *frcnp = closest->fc_frcn; 27227a0bc89SDoug Rabson *fsrcnp = closest->fc_fsrcn; 27327a0bc89SDoug Rabson } 27427a0bc89SDoug Rabson } 27527a0bc89SDoug Rabson 27627a0bc89SDoug Rabson /* 27727a0bc89SDoug Rabson * Purge the fat cache in denode dep of all entries relating to file 27827a0bc89SDoug Rabson * relative cluster frcn and beyond. 27927a0bc89SDoug Rabson */ 280952a6212SJordan K. Hubbard void 28110c9700fSEd Maste fc_purge(struct denode *dep, u_int frcn) 28227a0bc89SDoug Rabson { 28327a0bc89SDoug Rabson int i; 28427a0bc89SDoug Rabson struct fatcache *fcp; 28527a0bc89SDoug Rabson 286ef6a2be3SKonstantin Belousov ASSERT_VOP_ELOCKED(DETOV(dep), "fc_purge"); 287ef6a2be3SKonstantin Belousov 28827a0bc89SDoug Rabson fcp = dep->de_fc; 28927a0bc89SDoug Rabson for (i = 0; i < FC_SIZE; i++, fcp++) { 29027a0bc89SDoug Rabson if (fcp->fc_frcn >= frcn) 29127a0bc89SDoug Rabson fcp->fc_frcn = FCE_EMPTY; 29227a0bc89SDoug Rabson } 29327a0bc89SDoug Rabson } 29427a0bc89SDoug Rabson 29527a0bc89SDoug Rabson /* 296952a6212SJordan K. Hubbard * Update the fat. 297952a6212SJordan K. Hubbard * If mirroring the fat, update all copies, with the first copy as last. 298952a6212SJordan K. Hubbard * Else update only the current fat (ignoring the others). 29927a0bc89SDoug Rabson * 30027a0bc89SDoug Rabson * pmp - msdosfsmount structure for filesystem to update 30127a0bc89SDoug Rabson * bp - addr of modified fat block 30227a0bc89SDoug Rabson * fatbn - block number relative to begin of filesystem of the modified fat block. 30327a0bc89SDoug Rabson */ 304a98ca469SPoul-Henning Kamp static void 30510c9700fSEd Maste updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn) 30627a0bc89SDoug Rabson { 30727a0bc89SDoug Rabson struct buf *bpn; 30879fb7dd1SKonstantin Belousov int cleanfat, i; 30927a0bc89SDoug Rabson 31027a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 311952a6212SJordan K. Hubbard printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn); 31227a0bc89SDoug Rabson #endif 31327a0bc89SDoug Rabson 314952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFS_FATMIRROR) { 315952a6212SJordan K. Hubbard /* 31627a0bc89SDoug Rabson * Now copy the block(s) of the modified fat to the other copies of 31727a0bc89SDoug Rabson * the fat and write them out. This is faster than reading in the 31827a0bc89SDoug Rabson * other fats and then writing them back out. This could tie up 31927a0bc89SDoug Rabson * the fat for quite a while. Preventing others from accessing it. 32027a0bc89SDoug Rabson * To prevent us from going after the fat quite so much we use 32127a0bc89SDoug Rabson * delayed writes, unless they specfied "synchronous" when the 32227a0bc89SDoug Rabson * filesystem was mounted. If synch is asked for then use 32327a0bc89SDoug Rabson * bwrite()'s and really slow things down. 32427a0bc89SDoug Rabson */ 32579fb7dd1SKonstantin Belousov if (fatbn != pmp->pm_fatblk || FAT12(pmp)) 32679fb7dd1SKonstantin Belousov cleanfat = 0; 32779fb7dd1SKonstantin Belousov else if (FAT16(pmp)) 32879fb7dd1SKonstantin Belousov cleanfat = 16; 32979fb7dd1SKonstantin Belousov else 33079fb7dd1SKonstantin Belousov cleanfat = 32; 33127a0bc89SDoug Rabson for (i = 1; i < pmp->pm_FATs; i++) { 33227a0bc89SDoug Rabson fatbn += pmp->pm_FATsecs; 33327a0bc89SDoug Rabson /* getblk() never fails */ 3347261f5f6SJeff Roberson bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount, 3357261f5f6SJeff Roberson 0, 0, 0); 33627a0bc89SDoug Rabson bcopy(bp->b_data, bpn->b_data, bp->b_bcount); 33779fb7dd1SKonstantin Belousov /* Force the clean bit on in the other copies. */ 33879fb7dd1SKonstantin Belousov if (cleanfat == 16) 33979fb7dd1SKonstantin Belousov ((u_int8_t *)bpn->b_data)[3] |= 0x80; 34079fb7dd1SKonstantin Belousov else if (cleanfat == 32) 34179fb7dd1SKonstantin Belousov ((u_int8_t *)bpn->b_data)[7] |= 0x08; 34211fca81cSKonstantin Belousov if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS) 34327a0bc89SDoug Rabson bwrite(bpn); 34427a0bc89SDoug Rabson else 34527a0bc89SDoug Rabson bdwrite(bpn); 34627a0bc89SDoug Rabson } 347952a6212SJordan K. Hubbard } 348952a6212SJordan K. Hubbard 34927a0bc89SDoug Rabson /* 350952a6212SJordan K. Hubbard * Write out the first (or current) fat last. 35127a0bc89SDoug Rabson */ 35211fca81cSKonstantin Belousov if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS) 35327a0bc89SDoug Rabson bwrite(bp); 35427a0bc89SDoug Rabson else 35527a0bc89SDoug Rabson bdwrite(bp); 35627a0bc89SDoug Rabson } 35727a0bc89SDoug Rabson 35827a0bc89SDoug Rabson /* 35927a0bc89SDoug Rabson * Updating entries in 12 bit fats is a pain in the butt. 36027a0bc89SDoug Rabson * 36127a0bc89SDoug Rabson * The following picture shows where nibbles go when moving from a 12 bit 36227a0bc89SDoug Rabson * cluster number into the appropriate bytes in the FAT. 36327a0bc89SDoug Rabson * 36427a0bc89SDoug Rabson * byte m byte m+1 byte m+2 36527a0bc89SDoug Rabson * +----+----+ +----+----+ +----+----+ 36627a0bc89SDoug Rabson * | 0 1 | | 2 3 | | 4 5 | FAT bytes 36727a0bc89SDoug Rabson * +----+----+ +----+----+ +----+----+ 36827a0bc89SDoug Rabson * 36927a0bc89SDoug Rabson * +----+----+----+ +----+----+----+ 37027a0bc89SDoug Rabson * | 3 0 1 | | 4 5 2 | 37127a0bc89SDoug Rabson * +----+----+----+ +----+----+----+ 37227a0bc89SDoug Rabson * cluster n cluster n+1 37327a0bc89SDoug Rabson * 37427a0bc89SDoug Rabson * Where n is even. m = n + (n >> 2) 37527a0bc89SDoug Rabson * 37627a0bc89SDoug Rabson */ 377c1087c13SBruce Evans static __inline void 37810c9700fSEd Maste usemap_alloc(struct msdosfsmount *pmp, u_long cn) 37927a0bc89SDoug Rabson { 380952a6212SJordan K. Hubbard 3816be1a4ccSKonstantin Belousov MSDOSFS_ASSERT_MP_LOCKED(pmp); 382eb739c7cSKonstantin Belousov 383*420d65d9SKonstantin Belousov KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0, 384*420d65d9SKonstantin Belousov ("usemap_alloc on ro msdosfs mount")); 385eb739c7cSKonstantin Belousov KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS))) 386eb739c7cSKonstantin Belousov == 0, ("Allocating used sector %ld %ld %x", cn, cn % N_INUSEBITS, 387eb739c7cSKonstantin Belousov (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS])); 388952a6212SJordan K. Hubbard pmp->pm_inusemap[cn / N_INUSEBITS] |= 1 << (cn % N_INUSEBITS); 389eb739c7cSKonstantin Belousov KASSERT(pmp->pm_freeclustercount > 0, ("usemap_alloc: too little")); 39027a0bc89SDoug Rabson pmp->pm_freeclustercount--; 391bb7ca822SKonstantin Belousov pmp->pm_flags |= MSDOSFS_FSIMOD; 39227a0bc89SDoug Rabson } 39327a0bc89SDoug Rabson 394c1087c13SBruce Evans static __inline void 39510c9700fSEd Maste usemap_free(struct msdosfsmount *pmp, u_long cn) 39627a0bc89SDoug Rabson { 397952a6212SJordan K. Hubbard 3986be1a4ccSKonstantin Belousov MSDOSFS_ASSERT_MP_LOCKED(pmp); 399*420d65d9SKonstantin Belousov KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0, 400*420d65d9SKonstantin Belousov ("usemap_free on ro msdosfs mount")); 40127a0bc89SDoug Rabson pmp->pm_freeclustercount++; 402bb7ca822SKonstantin Belousov pmp->pm_flags |= MSDOSFS_FSIMOD; 403eb739c7cSKonstantin Belousov KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS))) 404eb739c7cSKonstantin Belousov != 0, ("Freeing unused sector %ld %ld %x", cn, cn % N_INUSEBITS, 405eb739c7cSKonstantin Belousov (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS])); 40627a0bc89SDoug Rabson pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1 << (cn % N_INUSEBITS)); 40727a0bc89SDoug Rabson } 40827a0bc89SDoug Rabson 40927a0bc89SDoug Rabson int 41010c9700fSEd Maste clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp) 41127a0bc89SDoug Rabson { 41227a0bc89SDoug Rabson int error; 41327a0bc89SDoug Rabson u_long oldcn; 41427a0bc89SDoug Rabson 41527a0bc89SDoug Rabson error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE); 4166be1a4ccSKonstantin Belousov if (error) 417952a6212SJordan K. Hubbard return (error); 41827a0bc89SDoug Rabson /* 41927a0bc89SDoug Rabson * If the cluster was successfully marked free, then update 42027a0bc89SDoug Rabson * the count of free clusters, and turn off the "allocated" 42127a0bc89SDoug Rabson * bit in the "in use" cluster bit map. 42227a0bc89SDoug Rabson */ 4236be1a4ccSKonstantin Belousov MSDOSFS_LOCK_MP(pmp); 4246be1a4ccSKonstantin Belousov usemap_free(pmp, cluster); 4256be1a4ccSKonstantin Belousov MSDOSFS_UNLOCK_MP(pmp); 42627a0bc89SDoug Rabson if (oldcnp) 42727a0bc89SDoug Rabson *oldcnp = oldcn; 428952a6212SJordan K. Hubbard return (0); 42927a0bc89SDoug Rabson } 43027a0bc89SDoug Rabson 43127a0bc89SDoug Rabson /* 43227a0bc89SDoug Rabson * Get or Set or 'Get and Set' the cluster'th entry in the fat. 43327a0bc89SDoug Rabson * 43427a0bc89SDoug Rabson * function - whether to get or set a fat entry 43527a0bc89SDoug Rabson * pmp - address of the msdosfsmount structure for the filesystem 43627a0bc89SDoug Rabson * whose fat is to be manipulated. 43727a0bc89SDoug Rabson * cn - which cluster is of interest 43827a0bc89SDoug Rabson * oldcontents - address of a word that is to receive the contents of the 43927a0bc89SDoug Rabson * cluster'th entry if this is a get function 44027a0bc89SDoug Rabson * newcontents - the new value to be written into the cluster'th element of 44127a0bc89SDoug Rabson * the fat if this is a set function. 44227a0bc89SDoug Rabson * 44327a0bc89SDoug Rabson * This function can also be used to free a cluster by setting the fat entry 44427a0bc89SDoug Rabson * for a cluster to 0. 44527a0bc89SDoug Rabson * 44627a0bc89SDoug Rabson * All copies of the fat are updated if this is a set function. NOTE: If 44727a0bc89SDoug Rabson * fatentry() marks a cluster as free it does not update the inusemap in 44827a0bc89SDoug Rabson * the msdosfsmount structure. This is left to the caller. 44927a0bc89SDoug Rabson */ 45027a0bc89SDoug Rabson int 4514882501bSEd Maste fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents, 4524882501bSEd Maste u_long newcontents) 45327a0bc89SDoug Rabson { 45427a0bc89SDoug Rabson int error; 45527a0bc89SDoug Rabson u_long readcn; 45627a0bc89SDoug Rabson u_long bn, bo, bsize, byteoffset; 45727a0bc89SDoug Rabson struct buf *bp; 45827a0bc89SDoug Rabson 459952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG 460952a6212SJordan K. Hubbard printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n", 461952a6212SJordan K. Hubbard function, pmp, cn, oldcontents, newcontents); 462952a6212SJordan K. Hubbard #endif 46327a0bc89SDoug Rabson 46427a0bc89SDoug Rabson #ifdef DIAGNOSTIC 46527a0bc89SDoug Rabson /* 46627a0bc89SDoug Rabson * Be sure they asked us to do something. 46727a0bc89SDoug Rabson */ 46827a0bc89SDoug Rabson if ((function & (FAT_SET | FAT_GET)) == 0) { 46954cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 47027a0bc89SDoug Rabson printf("fatentry(): function code doesn't specify get or set\n"); 47154cf9198SKonstantin Belousov #endif 472952a6212SJordan K. Hubbard return (EINVAL); 47327a0bc89SDoug Rabson } 47427a0bc89SDoug Rabson 47527a0bc89SDoug Rabson /* 47627a0bc89SDoug Rabson * If they asked us to return a cluster number but didn't tell us 47727a0bc89SDoug Rabson * where to put it, give them an error. 47827a0bc89SDoug Rabson */ 47927a0bc89SDoug Rabson if ((function & FAT_GET) && oldcontents == NULL) { 48054cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 48127a0bc89SDoug Rabson printf("fatentry(): get function with no place to put result\n"); 48254cf9198SKonstantin Belousov #endif 483952a6212SJordan K. Hubbard return (EINVAL); 48427a0bc89SDoug Rabson } 48527a0bc89SDoug Rabson #endif 48627a0bc89SDoug Rabson 48727a0bc89SDoug Rabson /* 48827a0bc89SDoug Rabson * Be sure the requested cluster is in the filesystem. 48927a0bc89SDoug Rabson */ 49027a0bc89SDoug Rabson if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster) 491952a6212SJordan K. Hubbard return (EINVAL); 49227a0bc89SDoug Rabson 49327a0bc89SDoug Rabson byteoffset = FATOFS(pmp, cn); 49427a0bc89SDoug Rabson fatblock(pmp, byteoffset, &bn, &bsize, &bo); 495c3c6d51eSPoul-Henning Kamp error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 496952a6212SJordan K. Hubbard if (error) { 497952a6212SJordan K. Hubbard brelse(bp); 498952a6212SJordan K. Hubbard return (error); 499952a6212SJordan K. Hubbard } 50027a0bc89SDoug Rabson 50127a0bc89SDoug Rabson if (function & FAT_GET) { 502952a6212SJordan K. Hubbard if (FAT32(pmp)) 503952a6212SJordan K. Hubbard readcn = getulong(&bp->b_data[bo]); 504952a6212SJordan K. Hubbard else 50527a0bc89SDoug Rabson readcn = getushort(&bp->b_data[bo]); 506952a6212SJordan K. Hubbard if (FAT12(pmp) & (cn & 1)) 50727a0bc89SDoug Rabson readcn >>= 4; 508952a6212SJordan K. Hubbard readcn &= pmp->pm_fatmask; 509952a6212SJordan K. Hubbard /* map reserved fat entries to same values for all fats */ 510952a6212SJordan K. Hubbard if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 511952a6212SJordan K. Hubbard readcn |= ~pmp->pm_fatmask; 51227a0bc89SDoug Rabson *oldcontents = readcn; 51327a0bc89SDoug Rabson } 51427a0bc89SDoug Rabson if (function & FAT_SET) { 515952a6212SJordan K. Hubbard switch (pmp->pm_fatmask) { 516952a6212SJordan K. Hubbard case FAT12_MASK: 51727a0bc89SDoug Rabson readcn = getushort(&bp->b_data[bo]); 51827a0bc89SDoug Rabson if (cn & 1) { 51927a0bc89SDoug Rabson readcn &= 0x000f; 52027a0bc89SDoug Rabson readcn |= newcontents << 4; 52127a0bc89SDoug Rabson } else { 52227a0bc89SDoug Rabson readcn &= 0xf000; 52327a0bc89SDoug Rabson readcn |= newcontents & 0xfff; 52427a0bc89SDoug Rabson } 52527a0bc89SDoug Rabson putushort(&bp->b_data[bo], readcn); 526952a6212SJordan K. Hubbard break; 527952a6212SJordan K. Hubbard case FAT16_MASK: 52827a0bc89SDoug Rabson putushort(&bp->b_data[bo], newcontents); 529952a6212SJordan K. Hubbard break; 530952a6212SJordan K. Hubbard case FAT32_MASK: 531952a6212SJordan K. Hubbard /* 532952a6212SJordan K. Hubbard * According to spec we have to retain the 533952a6212SJordan K. Hubbard * high order bits of the fat entry. 534952a6212SJordan K. Hubbard */ 535952a6212SJordan K. Hubbard readcn = getulong(&bp->b_data[bo]); 536952a6212SJordan K. Hubbard readcn &= ~FAT32_MASK; 537952a6212SJordan K. Hubbard readcn |= newcontents & FAT32_MASK; 538952a6212SJordan K. Hubbard putulong(&bp->b_data[bo], readcn); 539952a6212SJordan K. Hubbard break; 540952a6212SJordan K. Hubbard } 54127a0bc89SDoug Rabson updatefats(pmp, bp, bn); 54227a0bc89SDoug Rabson bp = NULL; 54327a0bc89SDoug Rabson pmp->pm_fmod = 1; 54427a0bc89SDoug Rabson } 54527a0bc89SDoug Rabson if (bp) 54627a0bc89SDoug Rabson brelse(bp); 547952a6212SJordan K. Hubbard return (0); 54827a0bc89SDoug Rabson } 54927a0bc89SDoug Rabson 55027a0bc89SDoug Rabson /* 55127a0bc89SDoug Rabson * Update a contiguous cluster chain 55227a0bc89SDoug Rabson * 55327a0bc89SDoug Rabson * pmp - mount point 55427a0bc89SDoug Rabson * start - first cluster of chain 55527a0bc89SDoug Rabson * count - number of clusters in chain 55627a0bc89SDoug Rabson * fillwith - what to write into fat entry of last cluster 55727a0bc89SDoug Rabson */ 55827a0bc89SDoug Rabson static int 55910c9700fSEd Maste fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith) 56027a0bc89SDoug Rabson { 56127a0bc89SDoug Rabson int error; 56227a0bc89SDoug Rabson u_long bn, bo, bsize, byteoffset, readcn, newc; 56327a0bc89SDoug Rabson struct buf *bp; 56427a0bc89SDoug Rabson 56527a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 566952a6212SJordan K. Hubbard printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n", 56727a0bc89SDoug Rabson pmp, start, count, fillwith); 56827a0bc89SDoug Rabson #endif 56927a0bc89SDoug Rabson /* 57027a0bc89SDoug Rabson * Be sure the clusters are in the filesystem. 57127a0bc89SDoug Rabson */ 57227a0bc89SDoug Rabson if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster) 573952a6212SJordan K. Hubbard return (EINVAL); 57427a0bc89SDoug Rabson 57527a0bc89SDoug Rabson while (count > 0) { 57627a0bc89SDoug Rabson byteoffset = FATOFS(pmp, start); 57727a0bc89SDoug Rabson fatblock(pmp, byteoffset, &bn, &bsize, &bo); 578c3c6d51eSPoul-Henning Kamp error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 579952a6212SJordan K. Hubbard if (error) { 580952a6212SJordan K. Hubbard brelse(bp); 581952a6212SJordan K. Hubbard return (error); 582952a6212SJordan K. Hubbard } 58327a0bc89SDoug Rabson while (count > 0) { 58427a0bc89SDoug Rabson start++; 58527a0bc89SDoug Rabson newc = --count > 0 ? start : fillwith; 586952a6212SJordan K. Hubbard switch (pmp->pm_fatmask) { 587952a6212SJordan K. Hubbard case FAT12_MASK: 58827a0bc89SDoug Rabson readcn = getushort(&bp->b_data[bo]); 58927a0bc89SDoug Rabson if (start & 1) { 59027a0bc89SDoug Rabson readcn &= 0xf000; 59127a0bc89SDoug Rabson readcn |= newc & 0xfff; 59227a0bc89SDoug Rabson } else { 59327a0bc89SDoug Rabson readcn &= 0x000f; 59427a0bc89SDoug Rabson readcn |= newc << 4; 59527a0bc89SDoug Rabson } 59627a0bc89SDoug Rabson putushort(&bp->b_data[bo], readcn); 59727a0bc89SDoug Rabson bo++; 59827a0bc89SDoug Rabson if (!(start & 1)) 59927a0bc89SDoug Rabson bo++; 600952a6212SJordan K. Hubbard break; 601952a6212SJordan K. Hubbard case FAT16_MASK: 60227a0bc89SDoug Rabson putushort(&bp->b_data[bo], newc); 60327a0bc89SDoug Rabson bo += 2; 604952a6212SJordan K. Hubbard break; 605952a6212SJordan K. Hubbard case FAT32_MASK: 606952a6212SJordan K. Hubbard readcn = getulong(&bp->b_data[bo]); 607952a6212SJordan K. Hubbard readcn &= ~pmp->pm_fatmask; 608952a6212SJordan K. Hubbard readcn |= newc & pmp->pm_fatmask; 609952a6212SJordan K. Hubbard putulong(&bp->b_data[bo], readcn); 610952a6212SJordan K. Hubbard bo += 4; 611952a6212SJordan K. Hubbard break; 61227a0bc89SDoug Rabson } 61327a0bc89SDoug Rabson if (bo >= bsize) 61427a0bc89SDoug Rabson break; 61527a0bc89SDoug Rabson } 61627a0bc89SDoug Rabson updatefats(pmp, bp, bn); 61727a0bc89SDoug Rabson } 61827a0bc89SDoug Rabson pmp->pm_fmod = 1; 619952a6212SJordan K. Hubbard return (0); 62027a0bc89SDoug Rabson } 62127a0bc89SDoug Rabson 62227a0bc89SDoug Rabson /* 62327a0bc89SDoug Rabson * Check the length of a free cluster chain starting at start. 62427a0bc89SDoug Rabson * 62527a0bc89SDoug Rabson * pmp - mount point 62627a0bc89SDoug Rabson * start - start of chain 62727a0bc89SDoug Rabson * count - maximum interesting length 62827a0bc89SDoug Rabson */ 6297fefffeeSPoul-Henning Kamp static int 63010c9700fSEd Maste chainlength(struct msdosfsmount *pmp, u_long start, u_long count) 63127a0bc89SDoug Rabson { 63227a0bc89SDoug Rabson u_long idx, max_idx; 63327a0bc89SDoug Rabson u_int map; 63427a0bc89SDoug Rabson u_long len; 63527a0bc89SDoug Rabson 6366be1a4ccSKonstantin Belousov MSDOSFS_ASSERT_MP_LOCKED(pmp); 6376be1a4ccSKonstantin Belousov 63827a0bc89SDoug Rabson max_idx = pmp->pm_maxcluster / N_INUSEBITS; 63927a0bc89SDoug Rabson idx = start / N_INUSEBITS; 64027a0bc89SDoug Rabson start %= N_INUSEBITS; 64127a0bc89SDoug Rabson map = pmp->pm_inusemap[idx]; 64227a0bc89SDoug Rabson map &= ~((1 << start) - 1); 64327a0bc89SDoug Rabson if (map) { 64427a0bc89SDoug Rabson len = ffs(map) - 1 - start; 645952a6212SJordan K. Hubbard return (len > count ? count : len); 64627a0bc89SDoug Rabson } 64727a0bc89SDoug Rabson len = N_INUSEBITS - start; 64827a0bc89SDoug Rabson if (len >= count) 649952a6212SJordan K. Hubbard return (count); 65027a0bc89SDoug Rabson while (++idx <= max_idx) { 65127a0bc89SDoug Rabson if (len >= count) 65227a0bc89SDoug Rabson break; 653c3c6d51eSPoul-Henning Kamp map = pmp->pm_inusemap[idx]; 654c3c6d51eSPoul-Henning Kamp if (map) { 65527a0bc89SDoug Rabson len += ffs(map) - 1; 65627a0bc89SDoug Rabson break; 65727a0bc89SDoug Rabson } 65827a0bc89SDoug Rabson len += N_INUSEBITS; 65927a0bc89SDoug Rabson } 660952a6212SJordan K. Hubbard return (len > count ? count : len); 66127a0bc89SDoug Rabson } 66227a0bc89SDoug Rabson 66327a0bc89SDoug Rabson /* 66427a0bc89SDoug Rabson * Allocate contigous free clusters. 66527a0bc89SDoug Rabson * 66627a0bc89SDoug Rabson * pmp - mount point. 66727a0bc89SDoug Rabson * start - start of cluster chain. 66827a0bc89SDoug Rabson * count - number of clusters to allocate. 66927a0bc89SDoug Rabson * fillwith - put this value into the fat entry for the 67027a0bc89SDoug Rabson * last allocated cluster. 67127a0bc89SDoug Rabson * retcluster - put the first allocated cluster's number here. 67227a0bc89SDoug Rabson * got - how many clusters were actually allocated. 67327a0bc89SDoug Rabson */ 6747fefffeeSPoul-Henning Kamp static int 67510c9700fSEd Maste chainalloc(struct msdosfsmount *pmp, u_long start, u_long count, 67610c9700fSEd Maste u_long fillwith, u_long *retcluster, u_long *got) 67727a0bc89SDoug Rabson { 67827a0bc89SDoug Rabson int error; 679952a6212SJordan K. Hubbard u_long cl, n; 680952a6212SJordan K. Hubbard 6816be1a4ccSKonstantin Belousov MSDOSFS_ASSERT_MP_LOCKED(pmp); 682*420d65d9SKonstantin Belousov KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0, 683*420d65d9SKonstantin Belousov ("chainalloc on ro msdosfs mount")); 6846be1a4ccSKonstantin Belousov 685952a6212SJordan K. Hubbard for (cl = start, n = count; n-- > 0;) 686952a6212SJordan K. Hubbard usemap_alloc(pmp, cl++); 687bb7ca822SKonstantin Belousov pmp->pm_nxtfree = start + count; 688bb7ca822SKonstantin Belousov if (pmp->pm_nxtfree > pmp->pm_maxcluster) 689bb7ca822SKonstantin Belousov pmp->pm_nxtfree = CLUST_FIRST; 690bb7ca822SKonstantin Belousov pmp->pm_flags |= MSDOSFS_FSIMOD; 69127a0bc89SDoug Rabson error = fatchain(pmp, start, count, fillwith); 692952a6212SJordan K. Hubbard if (error != 0) 693952a6212SJordan K. Hubbard return (error); 69427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 695952a6212SJordan K. Hubbard printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n", 69627a0bc89SDoug Rabson start, count); 69727a0bc89SDoug Rabson #endif 69827a0bc89SDoug Rabson if (retcluster) 69927a0bc89SDoug Rabson *retcluster = start; 70027a0bc89SDoug Rabson if (got) 70127a0bc89SDoug Rabson *got = count; 702952a6212SJordan K. Hubbard return (0); 70327a0bc89SDoug Rabson } 70427a0bc89SDoug Rabson 70527a0bc89SDoug Rabson /* 70627a0bc89SDoug Rabson * Allocate contiguous free clusters. 70727a0bc89SDoug Rabson * 70827a0bc89SDoug Rabson * pmp - mount point. 70927a0bc89SDoug Rabson * start - preferred start of cluster chain. 71027a0bc89SDoug Rabson * count - number of clusters requested. 71127a0bc89SDoug Rabson * fillwith - put this value into the fat entry for the 71227a0bc89SDoug Rabson * last allocated cluster. 71327a0bc89SDoug Rabson * retcluster - put the first allocated cluster's number here. 71427a0bc89SDoug Rabson * got - how many clusters were actually allocated. 71527a0bc89SDoug Rabson */ 71627a0bc89SDoug Rabson int 7176be1a4ccSKonstantin Belousov clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count, 7186be1a4ccSKonstantin Belousov u_long fillwith, u_long *retcluster, u_long *got) 7196be1a4ccSKonstantin Belousov { 7206be1a4ccSKonstantin Belousov int error; 7216be1a4ccSKonstantin Belousov 7226be1a4ccSKonstantin Belousov MSDOSFS_LOCK_MP(pmp); 7236be1a4ccSKonstantin Belousov error = clusteralloc1(pmp, start, count, fillwith, retcluster, got); 7246be1a4ccSKonstantin Belousov MSDOSFS_UNLOCK_MP(pmp); 7256be1a4ccSKonstantin Belousov return (error); 7266be1a4ccSKonstantin Belousov } 7276be1a4ccSKonstantin Belousov 7286be1a4ccSKonstantin Belousov static int 7296be1a4ccSKonstantin Belousov clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count, 7306be1a4ccSKonstantin Belousov u_long fillwith, u_long *retcluster, u_long *got) 73127a0bc89SDoug Rabson { 73227a0bc89SDoug Rabson u_long idx; 733952a6212SJordan K. Hubbard u_long len, newst, foundl, cn, l; 734952a6212SJordan K. Hubbard u_long foundcn = 0; /* XXX: foundcn could be used unititialized */ 73527a0bc89SDoug Rabson u_int map; 73627a0bc89SDoug Rabson 7376be1a4ccSKonstantin Belousov MSDOSFS_ASSERT_MP_LOCKED(pmp); 7386be1a4ccSKonstantin Belousov 73927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 740952a6212SJordan K. Hubbard printf("clusteralloc(): find %lu clusters\n", count); 74127a0bc89SDoug Rabson #endif 74227a0bc89SDoug Rabson if (start) { 74327a0bc89SDoug Rabson if ((len = chainlength(pmp, start, count)) >= count) 744952a6212SJordan K. Hubbard return (chainalloc(pmp, start, count, fillwith, retcluster, got)); 7457cd5051bSPoul-Henning Kamp } else 74627a0bc89SDoug Rabson len = 0; 74727a0bc89SDoug Rabson 7488e55bfafSBruce Evans newst = pmp->pm_nxtfree; 74927a0bc89SDoug Rabson foundl = 0; 75027a0bc89SDoug Rabson 75127a0bc89SDoug Rabson for (cn = newst; cn <= pmp->pm_maxcluster;) { 75227a0bc89SDoug Rabson idx = cn / N_INUSEBITS; 75327a0bc89SDoug Rabson map = pmp->pm_inusemap[idx]; 75427a0bc89SDoug Rabson map |= (1 << (cn % N_INUSEBITS)) - 1; 75527a0bc89SDoug Rabson if (map != (u_int)-1) { 75627a0bc89SDoug Rabson cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1; 75727a0bc89SDoug Rabson if ((l = chainlength(pmp, cn, count)) >= count) 758952a6212SJordan K. Hubbard return (chainalloc(pmp, cn, count, fillwith, retcluster, got)); 75927a0bc89SDoug Rabson if (l > foundl) { 76027a0bc89SDoug Rabson foundcn = cn; 76127a0bc89SDoug Rabson foundl = l; 76227a0bc89SDoug Rabson } 76327a0bc89SDoug Rabson cn += l + 1; 76427a0bc89SDoug Rabson continue; 76527a0bc89SDoug Rabson } 76627a0bc89SDoug Rabson cn += N_INUSEBITS - cn % N_INUSEBITS; 76727a0bc89SDoug Rabson } 76827a0bc89SDoug Rabson for (cn = 0; cn < newst;) { 76927a0bc89SDoug Rabson idx = cn / N_INUSEBITS; 77027a0bc89SDoug Rabson map = pmp->pm_inusemap[idx]; 77127a0bc89SDoug Rabson map |= (1 << (cn % N_INUSEBITS)) - 1; 77227a0bc89SDoug Rabson if (map != (u_int)-1) { 77327a0bc89SDoug Rabson cn = idx * N_INUSEBITS + ffs(map^(u_int)-1) - 1; 77427a0bc89SDoug Rabson if ((l = chainlength(pmp, cn, count)) >= count) 775952a6212SJordan K. Hubbard return (chainalloc(pmp, cn, count, fillwith, retcluster, got)); 77627a0bc89SDoug Rabson if (l > foundl) { 77727a0bc89SDoug Rabson foundcn = cn; 77827a0bc89SDoug Rabson foundl = l; 77927a0bc89SDoug Rabson } 78027a0bc89SDoug Rabson cn += l + 1; 78127a0bc89SDoug Rabson continue; 78227a0bc89SDoug Rabson } 78327a0bc89SDoug Rabson cn += N_INUSEBITS - cn % N_INUSEBITS; 78427a0bc89SDoug Rabson } 78527a0bc89SDoug Rabson 78627a0bc89SDoug Rabson if (!foundl) 787952a6212SJordan K. Hubbard return (ENOSPC); 78827a0bc89SDoug Rabson 78927a0bc89SDoug Rabson if (len) 790952a6212SJordan K. Hubbard return (chainalloc(pmp, start, len, fillwith, retcluster, got)); 79127a0bc89SDoug Rabson else 792952a6212SJordan K. Hubbard return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got)); 79327a0bc89SDoug Rabson } 79427a0bc89SDoug Rabson 79527a0bc89SDoug Rabson 79627a0bc89SDoug Rabson /* 79727a0bc89SDoug Rabson * Free a chain of clusters. 79827a0bc89SDoug Rabson * 79927a0bc89SDoug Rabson * pmp - address of the msdosfs mount structure for the filesystem 80027a0bc89SDoug Rabson * containing the cluster chain to be freed. 80127a0bc89SDoug Rabson * startcluster - number of the 1st cluster in the chain of clusters to be 80227a0bc89SDoug Rabson * freed. 80327a0bc89SDoug Rabson */ 80427a0bc89SDoug Rabson int 80510c9700fSEd Maste freeclusterchain(struct msdosfsmount *pmp, u_long cluster) 80627a0bc89SDoug Rabson { 807952a6212SJordan K. Hubbard int error; 80827a0bc89SDoug Rabson struct buf *bp = NULL; 80927a0bc89SDoug Rabson u_long bn, bo, bsize, byteoffset; 81027a0bc89SDoug Rabson u_long readcn, lbn = -1; 81127a0bc89SDoug Rabson 8126be1a4ccSKonstantin Belousov MSDOSFS_LOCK_MP(pmp); 81327a0bc89SDoug Rabson while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) { 81427a0bc89SDoug Rabson byteoffset = FATOFS(pmp, cluster); 81527a0bc89SDoug Rabson fatblock(pmp, byteoffset, &bn, &bsize, &bo); 81627a0bc89SDoug Rabson if (lbn != bn) { 81727a0bc89SDoug Rabson if (bp) 81824d4540cSBruce Evans updatefats(pmp, bp, lbn); 819c3c6d51eSPoul-Henning Kamp error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 820952a6212SJordan K. Hubbard if (error) { 821952a6212SJordan K. Hubbard brelse(bp); 8226be1a4ccSKonstantin Belousov MSDOSFS_UNLOCK_MP(pmp); 823952a6212SJordan K. Hubbard return (error); 824952a6212SJordan K. Hubbard } 82527a0bc89SDoug Rabson lbn = bn; 82627a0bc89SDoug Rabson } 82727a0bc89SDoug Rabson usemap_free(pmp, cluster); 828952a6212SJordan K. Hubbard switch (pmp->pm_fatmask) { 829952a6212SJordan K. Hubbard case FAT12_MASK: 83027a0bc89SDoug Rabson readcn = getushort(&bp->b_data[bo]); 83127a0bc89SDoug Rabson if (cluster & 1) { 83227a0bc89SDoug Rabson cluster = readcn >> 4; 83327a0bc89SDoug Rabson readcn &= 0x000f; 83427a0bc89SDoug Rabson readcn |= MSDOSFSFREE << 4; 83527a0bc89SDoug Rabson } else { 83627a0bc89SDoug Rabson cluster = readcn; 83727a0bc89SDoug Rabson readcn &= 0xf000; 83827a0bc89SDoug Rabson readcn |= MSDOSFSFREE & 0xfff; 83927a0bc89SDoug Rabson } 84027a0bc89SDoug Rabson putushort(&bp->b_data[bo], readcn); 841952a6212SJordan K. Hubbard break; 842952a6212SJordan K. Hubbard case FAT16_MASK: 843952a6212SJordan K. Hubbard cluster = getushort(&bp->b_data[bo]); 84427a0bc89SDoug Rabson putushort(&bp->b_data[bo], MSDOSFSFREE); 845952a6212SJordan K. Hubbard break; 846952a6212SJordan K. Hubbard case FAT32_MASK: 847952a6212SJordan K. Hubbard cluster = getulong(&bp->b_data[bo]); 848952a6212SJordan K. Hubbard putulong(&bp->b_data[bo], 849952a6212SJordan K. Hubbard (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK)); 850952a6212SJordan K. Hubbard break; 85127a0bc89SDoug Rabson } 852952a6212SJordan K. Hubbard cluster &= pmp->pm_fatmask; 853952a6212SJordan K. Hubbard if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD) 854952a6212SJordan K. Hubbard cluster |= pmp->pm_fatmask; 85527a0bc89SDoug Rabson } 85627a0bc89SDoug Rabson if (bp) 85727a0bc89SDoug Rabson updatefats(pmp, bp, bn); 8586be1a4ccSKonstantin Belousov MSDOSFS_UNLOCK_MP(pmp); 859952a6212SJordan K. Hubbard return (0); 86027a0bc89SDoug Rabson } 86127a0bc89SDoug Rabson 86227a0bc89SDoug Rabson /* 86327a0bc89SDoug Rabson * Read in fat blocks looking for free clusters. For every free cluster 86427a0bc89SDoug Rabson * found turn off its corresponding bit in the pm_inusemap. 86527a0bc89SDoug Rabson */ 86627a0bc89SDoug Rabson int 86710c9700fSEd Maste fillinusemap(struct msdosfsmount *pmp) 86827a0bc89SDoug Rabson { 86927a0bc89SDoug Rabson struct buf *bp = NULL; 87027a0bc89SDoug Rabson u_long cn, readcn; 87127a0bc89SDoug Rabson int error; 87227a0bc89SDoug Rabson u_long bn, bo, bsize, byteoffset; 87327a0bc89SDoug Rabson 8746be1a4ccSKonstantin Belousov MSDOSFS_ASSERT_MP_LOCKED(pmp); 8756be1a4ccSKonstantin Belousov 87627a0bc89SDoug Rabson /* 87727a0bc89SDoug Rabson * Mark all clusters in use, we mark the free ones in the fat scan 87827a0bc89SDoug Rabson * loop further down. 87927a0bc89SDoug Rabson */ 88027a0bc89SDoug Rabson for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++) 88127a0bc89SDoug Rabson pmp->pm_inusemap[cn] = (u_int)-1; 88227a0bc89SDoug Rabson 88327a0bc89SDoug Rabson /* 88427a0bc89SDoug Rabson * Figure how many free clusters are in the filesystem by ripping 88527a0bc89SDoug Rabson * through the fat counting the number of entries whose content is 88627a0bc89SDoug Rabson * zero. These represent free clusters. 88727a0bc89SDoug Rabson */ 88827a0bc89SDoug Rabson pmp->pm_freeclustercount = 0; 88927a0bc89SDoug Rabson for (cn = CLUST_FIRST; cn <= pmp->pm_maxcluster; cn++) { 89027a0bc89SDoug Rabson byteoffset = FATOFS(pmp, cn); 89127a0bc89SDoug Rabson bo = byteoffset % pmp->pm_fatblocksize; 89227a0bc89SDoug Rabson if (!bo || !bp) { 89327a0bc89SDoug Rabson /* Read new FAT block */ 89427a0bc89SDoug Rabson if (bp) 89527a0bc89SDoug Rabson brelse(bp); 89627a0bc89SDoug Rabson fatblock(pmp, byteoffset, &bn, &bsize, NULL); 89727a0bc89SDoug Rabson error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 898952a6212SJordan K. Hubbard if (error) { 899952a6212SJordan K. Hubbard brelse(bp); 900952a6212SJordan K. Hubbard return (error); 90127a0bc89SDoug Rabson } 902952a6212SJordan K. Hubbard } 903952a6212SJordan K. Hubbard if (FAT32(pmp)) 904952a6212SJordan K. Hubbard readcn = getulong(&bp->b_data[bo]); 905952a6212SJordan K. Hubbard else 90627a0bc89SDoug Rabson readcn = getushort(&bp->b_data[bo]); 907952a6212SJordan K. Hubbard if (FAT12(pmp) && (cn & 1)) 90827a0bc89SDoug Rabson readcn >>= 4; 909952a6212SJordan K. Hubbard readcn &= pmp->pm_fatmask; 91027a0bc89SDoug Rabson 91127a0bc89SDoug Rabson if (readcn == 0) 91227a0bc89SDoug Rabson usemap_free(pmp, cn); 91327a0bc89SDoug Rabson } 9143c8b687fSKonstantin Belousov if (bp != NULL) 91527a0bc89SDoug Rabson brelse(bp); 916952a6212SJordan K. Hubbard return (0); 91727a0bc89SDoug Rabson } 91827a0bc89SDoug Rabson 91927a0bc89SDoug Rabson /* 92027a0bc89SDoug Rabson * Allocate a new cluster and chain it onto the end of the file. 92127a0bc89SDoug Rabson * 92227a0bc89SDoug Rabson * dep - the file to extend 92327a0bc89SDoug Rabson * count - number of clusters to allocate 92427a0bc89SDoug Rabson * bpp - where to return the address of the buf header for the first new 92527a0bc89SDoug Rabson * file block 92627a0bc89SDoug Rabson * ncp - where to put cluster number of the first newly allocated cluster 92727a0bc89SDoug Rabson * If this pointer is 0, do not return the cluster number. 92827a0bc89SDoug Rabson * flags - see fat.h 92927a0bc89SDoug Rabson * 93027a0bc89SDoug Rabson * NOTE: This function is not responsible for turning on the DE_UPDATE bit of 93127a0bc89SDoug Rabson * the de_flag field of the denode and it does not change the de_FileSize 93227a0bc89SDoug Rabson * field. This is left for the caller to do. 93327a0bc89SDoug Rabson */ 93427a0bc89SDoug Rabson int 93510c9700fSEd Maste extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp, 93610c9700fSEd Maste int flags) 93727a0bc89SDoug Rabson { 938952a6212SJordan K. Hubbard int error; 93927a0bc89SDoug Rabson u_long frcn; 94027a0bc89SDoug Rabson u_long cn, got; 94127a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 94227a0bc89SDoug Rabson struct buf *bp; 9430d2af521SKirk McKusick daddr_t blkno; 94427a0bc89SDoug Rabson 94527a0bc89SDoug Rabson /* 94627a0bc89SDoug Rabson * Don't try to extend the root directory 94727a0bc89SDoug Rabson */ 948952a6212SJordan K. Hubbard if (dep->de_StartCluster == MSDOSFSROOT 949952a6212SJordan K. Hubbard && (dep->de_Attributes & ATTR_DIRECTORY)) { 95054cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 95127a0bc89SDoug Rabson printf("extendfile(): attempt to extend root directory\n"); 95254cf9198SKonstantin Belousov #endif 953952a6212SJordan K. Hubbard return (ENOSPC); 95427a0bc89SDoug Rabson } 95527a0bc89SDoug Rabson 95627a0bc89SDoug Rabson /* 95727a0bc89SDoug Rabson * If the "file's last cluster" cache entry is empty, and the file 95827a0bc89SDoug Rabson * is not empty, then fill the cache entry by calling pcbmap(). 95927a0bc89SDoug Rabson */ 96027a0bc89SDoug Rabson if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY && 96127a0bc89SDoug Rabson dep->de_StartCluster != 0) { 962952a6212SJordan K. Hubbard error = pcbmap(dep, 0xffff, 0, &cn, 0); 96327a0bc89SDoug Rabson /* we expect it to return E2BIG */ 96427a0bc89SDoug Rabson if (error != E2BIG) 965952a6212SJordan K. Hubbard return (error); 96627a0bc89SDoug Rabson } 96727a0bc89SDoug Rabson 968ededffc0STom Rhodes dep->de_fc[FC_NEXTTOLASTFC].fc_frcn = 969ededffc0STom Rhodes dep->de_fc[FC_LASTFC].fc_frcn; 970ededffc0STom Rhodes dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn = 971ededffc0STom Rhodes dep->de_fc[FC_LASTFC].fc_fsrcn; 97227a0bc89SDoug Rabson while (count > 0) { 97327a0bc89SDoug Rabson /* 974952a6212SJordan K. Hubbard * Allocate a new cluster chain and cat onto the end of the 975952a6212SJordan K. Hubbard * file. * If the file is empty we make de_StartCluster point 976952a6212SJordan K. Hubbard * to the new block. Note that de_StartCluster being 0 is 977952a6212SJordan K. Hubbard * sufficient to be sure the file is empty since we exclude 978952a6212SJordan K. Hubbard * attempts to extend the root directory above, and the root 979952a6212SJordan K. Hubbard * dir is the only file with a startcluster of 0 that has 980952a6212SJordan K. Hubbard * blocks allocated (sort of). 98127a0bc89SDoug Rabson */ 98227a0bc89SDoug Rabson if (dep->de_StartCluster == 0) 98327a0bc89SDoug Rabson cn = 0; 98427a0bc89SDoug Rabson else 98527a0bc89SDoug Rabson cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1; 986c3c6d51eSPoul-Henning Kamp error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got); 987c3c6d51eSPoul-Henning Kamp if (error) 988952a6212SJordan K. Hubbard return (error); 98927a0bc89SDoug Rabson 99027a0bc89SDoug Rabson count -= got; 99127a0bc89SDoug Rabson 99227a0bc89SDoug Rabson /* 99327a0bc89SDoug Rabson * Give them the filesystem relative cluster number if they want 99427a0bc89SDoug Rabson * it. 99527a0bc89SDoug Rabson */ 99627a0bc89SDoug Rabson if (ncp) { 99727a0bc89SDoug Rabson *ncp = cn; 99827a0bc89SDoug Rabson ncp = NULL; 99927a0bc89SDoug Rabson } 100027a0bc89SDoug Rabson 100127a0bc89SDoug Rabson if (dep->de_StartCluster == 0) { 100227a0bc89SDoug Rabson dep->de_StartCluster = cn; 100327a0bc89SDoug Rabson frcn = 0; 100427a0bc89SDoug Rabson } else { 1005952a6212SJordan K. Hubbard error = fatentry(FAT_SET, pmp, 1006952a6212SJordan K. Hubbard dep->de_fc[FC_LASTFC].fc_fsrcn, 100727a0bc89SDoug Rabson 0, cn); 100827a0bc89SDoug Rabson if (error) { 100927a0bc89SDoug Rabson clusterfree(pmp, cn, NULL); 1010952a6212SJordan K. Hubbard return (error); 101127a0bc89SDoug Rabson } 101227a0bc89SDoug Rabson frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1; 101327a0bc89SDoug Rabson } 101427a0bc89SDoug Rabson 101527a0bc89SDoug Rabson /* 101627a0bc89SDoug Rabson * Update the "last cluster of the file" entry in the denode's fat 101727a0bc89SDoug Rabson * cache. 101827a0bc89SDoug Rabson */ 101927a0bc89SDoug Rabson fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1); 102027a0bc89SDoug Rabson 102127a0bc89SDoug Rabson if (flags & DE_CLEAR) { 102227a0bc89SDoug Rabson while (got-- > 0) { 102327a0bc89SDoug Rabson /* 102427a0bc89SDoug Rabson * Get the buf header for the new block of the file. 102527a0bc89SDoug Rabson */ 102627a0bc89SDoug Rabson if (dep->de_Attributes & ATTR_DIRECTORY) 10277261f5f6SJeff Roberson bp = getblk(pmp->pm_devvp, 10287261f5f6SJeff Roberson cntobn(pmp, cn++), 10297261f5f6SJeff Roberson pmp->pm_bpcluster, 0, 0, 0); 103027a0bc89SDoug Rabson else { 10317261f5f6SJeff Roberson bp = getblk(DETOV(dep), 103267c7bbf3SKonstantin Belousov frcn++, 10337261f5f6SJeff Roberson pmp->pm_bpcluster, 0, 0, 0); 103427a0bc89SDoug Rabson /* 103527a0bc89SDoug Rabson * Do the bmap now, as in msdosfs_write 103627a0bc89SDoug Rabson */ 1037952a6212SJordan K. Hubbard if (pcbmap(dep, 103867c7bbf3SKonstantin Belousov bp->b_lblkno, 10390d2af521SKirk McKusick &blkno, 0, 0)) 104027a0bc89SDoug Rabson bp->b_blkno = -1; 104127a0bc89SDoug Rabson if (bp->b_blkno == -1) 104227a0bc89SDoug Rabson panic("extendfile: pcbmap"); 10430d2af521SKirk McKusick else 10440d2af521SKirk McKusick bp->b_blkno = blkno; 104527a0bc89SDoug Rabson } 1046d34b0a1bSBruce Evans vfs_bio_clrbuf(bp); 104727a0bc89SDoug Rabson if (bpp) { 104827a0bc89SDoug Rabson *bpp = bp; 104927a0bc89SDoug Rabson bpp = NULL; 1050952a6212SJordan K. Hubbard } else 1051952a6212SJordan K. Hubbard bdwrite(bp); 105227a0bc89SDoug Rabson } 105327a0bc89SDoug Rabson } 105427a0bc89SDoug Rabson } 105527a0bc89SDoug Rabson 1056952a6212SJordan K. Hubbard return (0); 105727a0bc89SDoug Rabson } 1058cede1f56STom Rhodes 1059392dbea3SBruce Evans /*- 1060392dbea3SBruce Evans * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by 1061392dbea3SBruce Evans * manipulating the upper bit of the FAT entry for cluster 1. Note that 1062392dbea3SBruce Evans * this bit is not defined for FAT12 volumes, which are always assumed to 1063a26b949fSKonstantin Belousov * be clean. 1064cede1f56STom Rhodes * 1065392dbea3SBruce Evans * The fatentry() routine only works on cluster numbers that a file could 1066392dbea3SBruce Evans * occupy, so it won't manipulate the entry for cluster 1. So we have to do 1067392dbea3SBruce Evans * it here. The code was stolen from fatentry() and tailored for cluster 1. 1068cede1f56STom Rhodes * 1069cede1f56STom Rhodes * Inputs: 1070cede1f56STom Rhodes * pmp The MS-DOS volume to mark 1071392dbea3SBruce Evans * dirty Non-zero if the volume should be marked dirty; zero if it 1072392dbea3SBruce Evans * should be marked clean 1073cede1f56STom Rhodes * 1074cede1f56STom Rhodes * Result: 1075cede1f56STom Rhodes * 0 Success 1076cede1f56STom Rhodes * EROFS Volume is read-only 1077cede1f56STom Rhodes * ? (other errors from called routines) 1078cede1f56STom Rhodes */ 1079392dbea3SBruce Evans int 1080392dbea3SBruce Evans markvoldirty(struct msdosfsmount *pmp, int dirty) 1081cede1f56STom Rhodes { 1082cede1f56STom Rhodes struct buf *bp; 1083392dbea3SBruce Evans u_long bn, bo, bsize, byteoffset, fatval; 1084392dbea3SBruce Evans int error; 1085cede1f56STom Rhodes 1086392dbea3SBruce Evans /* 1087392dbea3SBruce Evans * FAT12 does not support a "clean" bit, so don't do anything for 1088392dbea3SBruce Evans * FAT12. 1089392dbea3SBruce Evans */ 1090cede1f56STom Rhodes if (FAT12(pmp)) 1091392dbea3SBruce Evans return (0); 1092cede1f56STom Rhodes 1093392dbea3SBruce Evans /* Can't change the bit on a read-only filesystem. */ 1094cede1f56STom Rhodes if (pmp->pm_flags & MSDOSFSMNT_RONLY) 1095392dbea3SBruce Evans return (EROFS); 1096cede1f56STom Rhodes 1097392dbea3SBruce Evans /* 1098392dbea3SBruce Evans * Fetch the block containing the FAT entry. It is given by the 1099392dbea3SBruce Evans * pseudo-cluster 1. 1100392dbea3SBruce Evans */ 1101392dbea3SBruce Evans byteoffset = FATOFS(pmp, 1); 1102cede1f56STom Rhodes fatblock(pmp, byteoffset, &bn, &bsize, &bo); 1103cede1f56STom Rhodes error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 1104cede1f56STom Rhodes if (error) { 1105cede1f56STom Rhodes brelse(bp); 1106cede1f56STom Rhodes return (error); 1107cede1f56STom Rhodes } 1108cede1f56STom Rhodes 1109392dbea3SBruce Evans /* 1110392dbea3SBruce Evans * Get the current value of the FAT entry and set/clear the relevant 1111392dbea3SBruce Evans * bit. Dirty means clear the "clean" bit; clean means set the 1112392dbea3SBruce Evans * "clean" bit. 1113392dbea3SBruce Evans */ 1114cede1f56STom Rhodes if (FAT32(pmp)) { 1115392dbea3SBruce Evans /* FAT32 uses bit 27. */ 1116cede1f56STom Rhodes fatval = getulong(&bp->b_data[bo]); 1117cede1f56STom Rhodes if (dirty) 1118392dbea3SBruce Evans fatval &= 0xF7FFFFFF; 1119cede1f56STom Rhodes else 1120392dbea3SBruce Evans fatval |= 0x08000000; 1121cede1f56STom Rhodes putulong(&bp->b_data[bo], fatval); 1122392dbea3SBruce Evans } else { 1123392dbea3SBruce Evans /* Must be FAT16; use bit 15. */ 1124cede1f56STom Rhodes fatval = getushort(&bp->b_data[bo]); 1125cede1f56STom Rhodes if (dirty) 1126392dbea3SBruce Evans fatval &= 0x7FFF; 1127cede1f56STom Rhodes else 1128392dbea3SBruce Evans fatval |= 0x8000; 1129cede1f56STom Rhodes putushort(&bp->b_data[bo], fatval); 1130cede1f56STom Rhodes } 1131cede1f56STom Rhodes 1132392dbea3SBruce Evans /* Write out the modified FAT block synchronously. */ 1133392dbea3SBruce Evans return (bwrite(bp)); 1134cede1f56STom Rhodes } 1135