198dc8da5SEd Maste /* $FreeBSD$ */ 298dc8da5SEd Maste /* $NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $ */ 398dc8da5SEd Maste 498dc8da5SEd Maste /*- 598dc8da5SEd Maste * SPDX-License-Identifier: BSD-4-Clause 698dc8da5SEd Maste * 798dc8da5SEd Maste * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 898dc8da5SEd Maste * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 998dc8da5SEd Maste * All rights reserved. 1098dc8da5SEd Maste * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 1198dc8da5SEd Maste * 1298dc8da5SEd Maste * Redistribution and use in source and binary forms, with or without 1398dc8da5SEd Maste * modification, are permitted provided that the following conditions 1498dc8da5SEd Maste * are met: 1598dc8da5SEd Maste * 1. Redistributions of source code must retain the above copyright 1698dc8da5SEd Maste * notice, this list of conditions and the following disclaimer. 1798dc8da5SEd Maste * 2. Redistributions in binary form must reproduce the above copyright 1898dc8da5SEd Maste * notice, this list of conditions and the following disclaimer in the 1998dc8da5SEd Maste * documentation and/or other materials provided with the distribution. 2098dc8da5SEd Maste * 3. All advertising materials mentioning features or use of this software 2198dc8da5SEd Maste * must display the following acknowledgement: 2298dc8da5SEd Maste * This product includes software developed by TooLs GmbH. 2398dc8da5SEd Maste * 4. The name of TooLs GmbH may not be used to endorse or promote products 2498dc8da5SEd Maste * derived from this software without specific prior written permission. 2598dc8da5SEd Maste * 2698dc8da5SEd Maste * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2798dc8da5SEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2898dc8da5SEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2998dc8da5SEd Maste * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3098dc8da5SEd Maste * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 3198dc8da5SEd Maste * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3298dc8da5SEd Maste * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3398dc8da5SEd Maste * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3498dc8da5SEd Maste * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3598dc8da5SEd Maste * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3698dc8da5SEd Maste */ 3798dc8da5SEd Maste /*- 3898dc8da5SEd Maste * Written by Paul Popelka (paulp@uts.amdahl.com) 3998dc8da5SEd Maste * 4098dc8da5SEd Maste * You can do anything you want with this software, just don't say you wrote 4198dc8da5SEd Maste * it, and don't remove this notice. 4298dc8da5SEd Maste * 4398dc8da5SEd Maste * This software is provided "as is". 4498dc8da5SEd Maste * 4598dc8da5SEd Maste * The author supplies this software to be publicly redistributed on the 4698dc8da5SEd Maste * understanding that the author is not responsible for the correct 4798dc8da5SEd Maste * functioning of this software in any circumstances and is not liable for 4898dc8da5SEd Maste * any damages caused by this software. 4998dc8da5SEd Maste * 5098dc8da5SEd Maste * October 1992 5198dc8da5SEd Maste */ 5298dc8da5SEd Maste 5398dc8da5SEd Maste #include <sys/param.h> 5498dc8da5SEd Maste #include <sys/errno.h> 5598dc8da5SEd Maste 5698dc8da5SEd Maste #include <assert.h> 57aaa38524SConrad Meyer #include <stdbool.h> 5898dc8da5SEd Maste #include <stdio.h> 5998dc8da5SEd Maste #include <string.h> 6098dc8da5SEd Maste #include <strings.h> 6198dc8da5SEd Maste 62476b0ab7SEd Maste #include <fs/msdosfs/bpb.h> 63*ba2cfa80SAlex Richardson #include "msdos/denode.h" 64476b0ab7SEd Maste #include <fs/msdosfs/fat.h> 65840aca28SEd Maste #include <fs/msdosfs/msdosfsmount.h> 6698dc8da5SEd Maste 6798dc8da5SEd Maste #include "makefs.h" 6898dc8da5SEd Maste #include "msdos.h" 6998dc8da5SEd Maste 7098dc8da5SEd Maste #define FULL_RUN ((u_int)0xffffffff) 7198dc8da5SEd Maste #define SYNCHRONOUS_WRITES(pmp) 1 7298dc8da5SEd Maste 7398dc8da5SEd Maste static int chainalloc(struct msdosfsmount *pmp, u_long start, 7498dc8da5SEd Maste u_long count, u_long fillwith, u_long *retcluster, 7598dc8da5SEd Maste u_long *got); 7698dc8da5SEd Maste static int chainlength(struct msdosfsmount *pmp, u_long start, 7798dc8da5SEd Maste u_long count); 7898dc8da5SEd Maste static void fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, 7998dc8da5SEd Maste u_long *sizep, u_long *bop); 8098dc8da5SEd Maste static int fatchain(struct msdosfsmount *pmp, u_long start, u_long count, 8198dc8da5SEd Maste u_long fillwith); 8298dc8da5SEd Maste static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, 8398dc8da5SEd Maste u_long *fsrcnp); 84d485c77fSKonstantin Belousov static void updatefats(struct msdosfsmount *pmp, struct m_buf *bp, 8598dc8da5SEd Maste u_long fatbn); 8698dc8da5SEd Maste static __inline void 8798dc8da5SEd Maste usemap_alloc(struct msdosfsmount *pmp, u_long cn); 8898dc8da5SEd Maste static __inline void 8998dc8da5SEd Maste usemap_free(struct msdosfsmount *pmp, u_long cn); 9098dc8da5SEd Maste static int clusteralloc1(struct msdosfsmount *pmp, u_long start, 9198dc8da5SEd Maste u_long count, u_long fillwith, u_long *retcluster, 9298dc8da5SEd Maste u_long *got); 9398dc8da5SEd Maste 9498dc8da5SEd Maste static void 9598dc8da5SEd Maste fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep, 9698dc8da5SEd Maste u_long *bop) 9798dc8da5SEd Maste { 9898dc8da5SEd Maste u_long bn, size; 9998dc8da5SEd Maste 10098dc8da5SEd Maste bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec; 10198dc8da5SEd Maste size = MIN(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn) 10298dc8da5SEd Maste * DEV_BSIZE; 10398dc8da5SEd Maste bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs; 10498dc8da5SEd Maste 10598dc8da5SEd Maste if (bnp) 10698dc8da5SEd Maste *bnp = bn; 10798dc8da5SEd Maste if (sizep) 10898dc8da5SEd Maste *sizep = size; 10998dc8da5SEd Maste if (bop) 11098dc8da5SEd Maste *bop = ofs % pmp->pm_fatblocksize; 11198dc8da5SEd Maste } 11298dc8da5SEd Maste 11398dc8da5SEd Maste /* 11498dc8da5SEd Maste * Map the logical cluster number of a file into a physical disk sector 11598dc8da5SEd Maste * that is filesystem relative. 11698dc8da5SEd Maste * 11798dc8da5SEd Maste * dep - address of denode representing the file of interest 11898dc8da5SEd Maste * findcn - file relative cluster whose filesystem relative cluster number 11998dc8da5SEd Maste * and/or block number are/is to be found 12098dc8da5SEd Maste * bnp - address of where to place the filesystem relative block number. 12198dc8da5SEd Maste * If this pointer is null then don't return this quantity. 12298dc8da5SEd Maste * cnp - address of where to place the filesystem relative cluster number. 12398dc8da5SEd Maste * If this pointer is null then don't return this quantity. 12498dc8da5SEd Maste * sp - pointer to returned block size 12598dc8da5SEd Maste * 12698dc8da5SEd Maste * NOTE: Either bnp or cnp must be non-null. 12798dc8da5SEd Maste * This function has one side effect. If the requested file relative cluster 12898dc8da5SEd Maste * is beyond the end of file, then the actual number of clusters in the file 12998dc8da5SEd Maste * is returned in *cnp. This is useful for determining how long a directory is. 13098dc8da5SEd Maste * If cnp is null, nothing is returned. 13198dc8da5SEd Maste */ 13298dc8da5SEd Maste int 13398dc8da5SEd Maste pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp) 13498dc8da5SEd Maste { 13598dc8da5SEd Maste int error; 13698dc8da5SEd Maste u_long i; 13798dc8da5SEd Maste u_long cn; 13898dc8da5SEd Maste u_long prevcn = 0; /* XXX: prevcn could be used unititialized */ 13998dc8da5SEd Maste u_long byteoffset; 14098dc8da5SEd Maste u_long bn; 14198dc8da5SEd Maste u_long bo; 142d485c77fSKonstantin Belousov struct m_buf *bp = NULL; 14398dc8da5SEd Maste u_long bp_bn = -1; 14498dc8da5SEd Maste struct msdosfsmount *pmp = dep->de_pmp; 14598dc8da5SEd Maste u_long bsize; 14698dc8da5SEd Maste 14798dc8da5SEd Maste assert(bnp != NULL || cnp != NULL || sp != NULL); 14898dc8da5SEd Maste 14998dc8da5SEd Maste cn = dep->de_StartCluster; 15098dc8da5SEd Maste /* 15198dc8da5SEd Maste * The "file" that makes up the root directory is contiguous, 15298dc8da5SEd Maste * permanently allocated, of fixed size, and is not made up of 15398dc8da5SEd Maste * clusters. If the cluster number is beyond the end of the root 15498dc8da5SEd Maste * directory, then return the number of clusters in the file. 15598dc8da5SEd Maste */ 15698dc8da5SEd Maste if (cn == MSDOSFSROOT) { 15798dc8da5SEd Maste if (dep->de_Attributes & ATTR_DIRECTORY) { 15898dc8da5SEd Maste if (de_cn2off(pmp, findcn) >= dep->de_FileSize) { 15998dc8da5SEd Maste if (cnp) 16098dc8da5SEd Maste *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize); 16198dc8da5SEd Maste return (E2BIG); 16298dc8da5SEd Maste } 16398dc8da5SEd Maste if (bnp) 16498dc8da5SEd Maste *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn); 16598dc8da5SEd Maste if (cnp) 16698dc8da5SEd Maste *cnp = MSDOSFSROOT; 16798dc8da5SEd Maste if (sp) 16898dc8da5SEd Maste *sp = MIN(pmp->pm_bpcluster, 16998dc8da5SEd Maste dep->de_FileSize - de_cn2off(pmp, findcn)); 17098dc8da5SEd Maste return (0); 17198dc8da5SEd Maste } else { /* just an empty file */ 17298dc8da5SEd Maste if (cnp) 17398dc8da5SEd Maste *cnp = 0; 17498dc8da5SEd Maste return (E2BIG); 17598dc8da5SEd Maste } 17698dc8da5SEd Maste } 17798dc8da5SEd Maste 17898dc8da5SEd Maste /* 17998dc8da5SEd Maste * All other files do I/O in cluster sized blocks 18098dc8da5SEd Maste */ 18198dc8da5SEd Maste if (sp) 18298dc8da5SEd Maste *sp = pmp->pm_bpcluster; 18398dc8da5SEd Maste 18498dc8da5SEd Maste /* 18598dc8da5SEd Maste * Rummage around in the FAT cache, maybe we can avoid tromping 18698dc8da5SEd Maste * through every FAT entry for the file. And, keep track of how far 18798dc8da5SEd Maste * off the cache was from where we wanted to be. 18898dc8da5SEd Maste */ 18998dc8da5SEd Maste i = 0; 19098dc8da5SEd Maste fc_lookup(dep, findcn, &i, &cn); 19198dc8da5SEd Maste 19298dc8da5SEd Maste /* 19398dc8da5SEd Maste * Handle all other files or directories the normal way. 19498dc8da5SEd Maste */ 19598dc8da5SEd Maste for (; i < findcn; i++) { 19698dc8da5SEd Maste /* 19798dc8da5SEd Maste * Stop with all reserved clusters, not just with EOF. 19898dc8da5SEd Maste */ 19998dc8da5SEd Maste if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 20098dc8da5SEd Maste goto hiteof; 20198dc8da5SEd Maste byteoffset = FATOFS(pmp, cn); 20298dc8da5SEd Maste fatblock(pmp, byteoffset, &bn, &bsize, &bo); 20398dc8da5SEd Maste if (bn != bp_bn) { 20498dc8da5SEd Maste if (bp) 20598dc8da5SEd Maste brelse(bp); 206d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, bsize, 207d485c77fSKonstantin Belousov NOCRED, &bp); 20898dc8da5SEd Maste if (error) { 20998dc8da5SEd Maste brelse(bp); 21098dc8da5SEd Maste return (error); 21198dc8da5SEd Maste } 21298dc8da5SEd Maste bp_bn = bn; 21398dc8da5SEd Maste } 21498dc8da5SEd Maste prevcn = cn; 21598dc8da5SEd Maste if (bo >= bsize) { 21698dc8da5SEd Maste if (bp) 21798dc8da5SEd Maste brelse(bp); 21898dc8da5SEd Maste return (EIO); 21998dc8da5SEd Maste } 22098dc8da5SEd Maste if (FAT32(pmp)) 221ef633620SEd Maste cn = getulong(bp->b_data + bo); 22298dc8da5SEd Maste else 223ef633620SEd Maste cn = getushort(bp->b_data + bo); 22498dc8da5SEd Maste if (FAT12(pmp) && (prevcn & 1)) 22598dc8da5SEd Maste cn >>= 4; 22698dc8da5SEd Maste cn &= pmp->pm_fatmask; 22798dc8da5SEd Maste 22898dc8da5SEd Maste /* 22998dc8da5SEd Maste * Force the special cluster numbers 23098dc8da5SEd Maste * to be the same for all cluster sizes 23198dc8da5SEd Maste * to let the rest of msdosfs handle 23298dc8da5SEd Maste * all cases the same. 23398dc8da5SEd Maste */ 23498dc8da5SEd Maste if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 23598dc8da5SEd Maste cn |= ~pmp->pm_fatmask; 23698dc8da5SEd Maste } 23798dc8da5SEd Maste 23898dc8da5SEd Maste if (!MSDOSFSEOF(pmp, cn)) { 23998dc8da5SEd Maste if (bp) 24098dc8da5SEd Maste brelse(bp); 24198dc8da5SEd Maste if (bnp) 24298dc8da5SEd Maste *bnp = cntobn(pmp, cn); 24398dc8da5SEd Maste if (cnp) 24498dc8da5SEd Maste *cnp = cn; 24598dc8da5SEd Maste fc_setcache(dep, FC_LASTMAP, i, cn); 24698dc8da5SEd Maste return (0); 24798dc8da5SEd Maste } 24898dc8da5SEd Maste 24998dc8da5SEd Maste hiteof:; 25098dc8da5SEd Maste if (cnp) 25198dc8da5SEd Maste *cnp = i; 25298dc8da5SEd Maste if (bp) 25398dc8da5SEd Maste brelse(bp); 25498dc8da5SEd Maste /* update last file cluster entry in the FAT cache */ 25598dc8da5SEd Maste fc_setcache(dep, FC_LASTFC, i - 1, prevcn); 25698dc8da5SEd Maste return (E2BIG); 25798dc8da5SEd Maste } 25898dc8da5SEd Maste 25998dc8da5SEd Maste /* 26098dc8da5SEd Maste * Find the closest entry in the FAT cache to the cluster we are looking 26198dc8da5SEd Maste * for. 26298dc8da5SEd Maste */ 26398dc8da5SEd Maste static void 26498dc8da5SEd Maste fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp) 26598dc8da5SEd Maste { 26698dc8da5SEd Maste int i; 26798dc8da5SEd Maste u_long cn; 26898dc8da5SEd Maste struct fatcache *closest = NULL; 26998dc8da5SEd Maste 27098dc8da5SEd Maste for (i = 0; i < FC_SIZE; i++) { 27198dc8da5SEd Maste cn = dep->de_fc[i].fc_frcn; 27298dc8da5SEd Maste if (cn != FCE_EMPTY && cn <= findcn) { 27398dc8da5SEd Maste if (closest == NULL || cn > closest->fc_frcn) 27498dc8da5SEd Maste closest = &dep->de_fc[i]; 27598dc8da5SEd Maste } 27698dc8da5SEd Maste } 27798dc8da5SEd Maste if (closest) { 27898dc8da5SEd Maste *frcnp = closest->fc_frcn; 27998dc8da5SEd Maste *fsrcnp = closest->fc_fsrcn; 28098dc8da5SEd Maste } 28198dc8da5SEd Maste } 28298dc8da5SEd Maste 28398dc8da5SEd Maste /* 28498dc8da5SEd Maste * Purge the FAT cache in denode dep of all entries relating to file 28598dc8da5SEd Maste * relative cluster frcn and beyond. 28698dc8da5SEd Maste */ 28798dc8da5SEd Maste void 28898dc8da5SEd Maste fc_purge(struct denode *dep, u_int frcn) 28998dc8da5SEd Maste { 29098dc8da5SEd Maste int i; 29198dc8da5SEd Maste struct fatcache *fcp; 29298dc8da5SEd Maste 29398dc8da5SEd Maste fcp = dep->de_fc; 29498dc8da5SEd Maste for (i = 0; i < FC_SIZE; i++, fcp++) { 29598dc8da5SEd Maste if (fcp->fc_frcn >= frcn) 29698dc8da5SEd Maste fcp->fc_frcn = FCE_EMPTY; 29798dc8da5SEd Maste } 29898dc8da5SEd Maste } 29998dc8da5SEd Maste 30098dc8da5SEd Maste /* 30198dc8da5SEd Maste * Update the FAT. 30298dc8da5SEd Maste * If mirroring the FAT, update all copies, with the first copy as last. 30398dc8da5SEd Maste * Else update only the current FAT (ignoring the others). 30498dc8da5SEd Maste * 30598dc8da5SEd Maste * pmp - msdosfsmount structure for filesystem to update 30698dc8da5SEd Maste * bp - addr of modified FAT block 30798dc8da5SEd Maste * fatbn - block number relative to begin of filesystem of the modified FAT block. 30898dc8da5SEd Maste */ 30998dc8da5SEd Maste static void 310d485c77fSKonstantin Belousov updatefats(struct msdosfsmount *pmp, struct m_buf *bp, u_long fatbn) 31198dc8da5SEd Maste { 312d485c77fSKonstantin Belousov struct m_buf *bpn; 31398dc8da5SEd Maste int cleanfat, i; 31498dc8da5SEd Maste 31598dc8da5SEd Maste #ifdef MSDOSFS_DEBUG 31698dc8da5SEd Maste printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn); 31798dc8da5SEd Maste #endif 31898dc8da5SEd Maste 31998dc8da5SEd Maste if (pmp->pm_flags & MSDOSFS_FATMIRROR) { 32098dc8da5SEd Maste /* 32198dc8da5SEd Maste * Now copy the block(s) of the modified FAT to the other copies of 32298dc8da5SEd Maste * the FAT and write them out. This is faster than reading in the 32398dc8da5SEd Maste * other FATs and then writing them back out. This could tie up 32498dc8da5SEd Maste * the FAT for quite a while. Preventing others from accessing it. 32598dc8da5SEd Maste * To prevent us from going after the FAT quite so much we use 32698dc8da5SEd Maste * delayed writes, unless they specified "synchronous" when the 32798dc8da5SEd Maste * filesystem was mounted. If synch is asked for then use 32898dc8da5SEd Maste * bwrite()'s and really slow things down. 32998dc8da5SEd Maste */ 33098dc8da5SEd Maste if (fatbn != pmp->pm_fatblk || FAT12(pmp)) 33198dc8da5SEd Maste cleanfat = 0; 33298dc8da5SEd Maste else if (FAT16(pmp)) 33398dc8da5SEd Maste cleanfat = 16; 33498dc8da5SEd Maste else 33598dc8da5SEd Maste cleanfat = 32; 33698dc8da5SEd Maste for (i = 1; i < pmp->pm_FATs; i++) { 33798dc8da5SEd Maste fatbn += pmp->pm_FATsecs; 33898dc8da5SEd Maste /* getblk() never fails */ 339d485c77fSKonstantin Belousov bpn = getblk((void *)pmp->pm_devvp, fatbn, 340d485c77fSKonstantin Belousov bp->b_bcount, 0, 0, 0); 34198dc8da5SEd Maste memcpy(bpn->b_data, bp->b_data, bp->b_bcount); 34298dc8da5SEd Maste /* Force the clean bit on in the other copies. */ 34398dc8da5SEd Maste if (cleanfat == 16) 34498dc8da5SEd Maste ((uint8_t *)bpn->b_data)[3] |= 0x80; 34598dc8da5SEd Maste else if (cleanfat == 32) 34698dc8da5SEd Maste ((uint8_t *)bpn->b_data)[7] |= 0x08; 34798dc8da5SEd Maste if (SYNCHRONOUS_WRITES(pmp)) 34898dc8da5SEd Maste bwrite(bpn); 34998dc8da5SEd Maste else 35098dc8da5SEd Maste bdwrite(bpn); 35198dc8da5SEd Maste } 35298dc8da5SEd Maste } 35398dc8da5SEd Maste 35498dc8da5SEd Maste /* 35598dc8da5SEd Maste * Write out the first (or current) FAT last. 35698dc8da5SEd Maste */ 35798dc8da5SEd Maste if (SYNCHRONOUS_WRITES(pmp)) 35898dc8da5SEd Maste bwrite(bp); 35998dc8da5SEd Maste else 36098dc8da5SEd Maste bdwrite(bp); 36198dc8da5SEd Maste } 36298dc8da5SEd Maste 36398dc8da5SEd Maste /* 36498dc8da5SEd Maste * Updating entries in 12 bit FATs is a pain in the butt. 36598dc8da5SEd Maste * 36698dc8da5SEd Maste * The following picture shows where nibbles go when moving from a 12 bit 36798dc8da5SEd Maste * cluster number into the appropriate bytes in the FAT. 36898dc8da5SEd Maste * 36998dc8da5SEd Maste * byte m byte m+1 byte m+2 37098dc8da5SEd Maste * +----+----+ +----+----+ +----+----+ 37198dc8da5SEd Maste * | 0 1 | | 2 3 | | 4 5 | FAT bytes 37298dc8da5SEd Maste * +----+----+ +----+----+ +----+----+ 37398dc8da5SEd Maste * 37498dc8da5SEd Maste * +----+----+----+ +----+----+----+ 37598dc8da5SEd Maste * | 3 0 1 | | 4 5 2 | 37698dc8da5SEd Maste * +----+----+----+ +----+----+----+ 37798dc8da5SEd Maste * cluster n cluster n+1 37898dc8da5SEd Maste * 37998dc8da5SEd Maste * Where n is even. m = n + (n >> 2) 38098dc8da5SEd Maste * 38198dc8da5SEd Maste */ 38298dc8da5SEd Maste static __inline void 38398dc8da5SEd Maste usemap_alloc(struct msdosfsmount *pmp, u_long cn) 38498dc8da5SEd Maste { 38598dc8da5SEd Maste 38698dc8da5SEd Maste assert(cn <= pmp->pm_maxcluster); 38798dc8da5SEd Maste assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0); 38898dc8da5SEd Maste assert((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS))) 38998dc8da5SEd Maste == 0); 39098dc8da5SEd Maste assert(pmp->pm_freeclustercount > 0); 39198dc8da5SEd Maste 39298dc8da5SEd Maste pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS); 39398dc8da5SEd Maste pmp->pm_freeclustercount--; 39498dc8da5SEd Maste pmp->pm_flags |= MSDOSFS_FSIMOD; 39598dc8da5SEd Maste } 39698dc8da5SEd Maste 39798dc8da5SEd Maste static __inline void 39898dc8da5SEd Maste usemap_free(struct msdosfsmount *pmp, u_long cn) 39998dc8da5SEd Maste { 40098dc8da5SEd Maste 40198dc8da5SEd Maste assert(cn <= pmp->pm_maxcluster); 40298dc8da5SEd Maste assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0); 40398dc8da5SEd Maste assert((pmp->pm_inusemap[cn / N_INUSEBITS] & (1 << (cn % N_INUSEBITS))) 40498dc8da5SEd Maste != 0); 40598dc8da5SEd Maste 40698dc8da5SEd Maste pmp->pm_freeclustercount++; 40798dc8da5SEd Maste pmp->pm_flags |= MSDOSFS_FSIMOD; 40898dc8da5SEd Maste pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1U << (cn % N_INUSEBITS)); 40998dc8da5SEd Maste } 41098dc8da5SEd Maste 41198dc8da5SEd Maste int 41298dc8da5SEd Maste clusterfree(struct msdosfsmount *pmp, u_long cluster, u_long *oldcnp) 41398dc8da5SEd Maste { 41498dc8da5SEd Maste int error; 41598dc8da5SEd Maste u_long oldcn; 41698dc8da5SEd Maste 41798dc8da5SEd Maste error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE); 41898dc8da5SEd Maste if (error) 41998dc8da5SEd Maste return (error); 42098dc8da5SEd Maste /* 42198dc8da5SEd Maste * If the cluster was successfully marked free, then update 42298dc8da5SEd Maste * the count of free clusters, and turn off the "allocated" 42398dc8da5SEd Maste * bit in the "in use" cluster bit map. 42498dc8da5SEd Maste */ 42598dc8da5SEd Maste usemap_free(pmp, cluster); 42698dc8da5SEd Maste if (oldcnp) 42798dc8da5SEd Maste *oldcnp = oldcn; 42898dc8da5SEd Maste return (0); 42998dc8da5SEd Maste } 43098dc8da5SEd Maste 43198dc8da5SEd Maste /* 43298dc8da5SEd Maste * Get or Set or 'Get and Set' the cluster'th entry in the FAT. 43398dc8da5SEd Maste * 43498dc8da5SEd Maste * function - whether to get or set a FAT entry 43598dc8da5SEd Maste * pmp - address of the msdosfsmount structure for the filesystem 43698dc8da5SEd Maste * whose FAT is to be manipulated. 43798dc8da5SEd Maste * cn - which cluster is of interest 43898dc8da5SEd Maste * oldcontents - address of a word that is to receive the contents of the 43998dc8da5SEd Maste * cluster'th entry if this is a get function 44098dc8da5SEd Maste * newcontents - the new value to be written into the cluster'th element of 44198dc8da5SEd Maste * the FAT if this is a set function. 44298dc8da5SEd Maste * 44398dc8da5SEd Maste * This function can also be used to free a cluster by setting the FAT entry 44498dc8da5SEd Maste * for a cluster to 0. 44598dc8da5SEd Maste * 44698dc8da5SEd Maste * All copies of the FAT are updated if this is a set function. NOTE: If 44798dc8da5SEd Maste * fatentry() marks a cluster as free it does not update the inusemap in 44898dc8da5SEd Maste * the msdosfsmount structure. This is left to the caller. 44998dc8da5SEd Maste */ 45098dc8da5SEd Maste int 45198dc8da5SEd Maste fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents, 45298dc8da5SEd Maste u_long newcontents) 45398dc8da5SEd Maste { 45498dc8da5SEd Maste int error; 45598dc8da5SEd Maste u_long readcn; 45698dc8da5SEd Maste u_long bn, bo, bsize, byteoffset; 457d485c77fSKonstantin Belousov struct m_buf *bp; 45898dc8da5SEd Maste 45998dc8da5SEd Maste #ifdef MSDOSFS_DEBUG 46098dc8da5SEd Maste printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n", 46198dc8da5SEd Maste function, pmp, cn, oldcontents, newcontents); 46298dc8da5SEd Maste #endif 46398dc8da5SEd Maste 46498dc8da5SEd Maste #ifdef DIAGNOSTIC 46598dc8da5SEd Maste /* 46698dc8da5SEd Maste * Be sure they asked us to do something. 46798dc8da5SEd Maste */ 46898dc8da5SEd Maste if ((function & (FAT_SET | FAT_GET)) == 0) { 46998dc8da5SEd Maste #ifdef MSDOSFS_DEBUG 47098dc8da5SEd Maste printf("fatentry(): function code doesn't specify get or set\n"); 47198dc8da5SEd Maste #endif 47298dc8da5SEd Maste return (EINVAL); 47398dc8da5SEd Maste } 47498dc8da5SEd Maste 47598dc8da5SEd Maste /* 47698dc8da5SEd Maste * If they asked us to return a cluster number but didn't tell us 47798dc8da5SEd Maste * where to put it, give them an error. 47898dc8da5SEd Maste */ 47998dc8da5SEd Maste if ((function & FAT_GET) && oldcontents == NULL) { 48098dc8da5SEd Maste #ifdef MSDOSFS_DEBUG 48198dc8da5SEd Maste printf("fatentry(): get function with no place to put result\n"); 48298dc8da5SEd Maste #endif 48398dc8da5SEd Maste return (EINVAL); 48498dc8da5SEd Maste } 48598dc8da5SEd Maste #endif 48698dc8da5SEd Maste 48798dc8da5SEd Maste /* 48898dc8da5SEd Maste * Be sure the requested cluster is in the filesystem. 48998dc8da5SEd Maste */ 49098dc8da5SEd Maste if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster) 49198dc8da5SEd Maste return (EINVAL); 49298dc8da5SEd Maste 49398dc8da5SEd Maste byteoffset = FATOFS(pmp, cn); 49498dc8da5SEd Maste fatblock(pmp, byteoffset, &bn, &bsize, &bo); 495d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, bsize, NOCRED, &bp); 49698dc8da5SEd Maste if (error) { 49798dc8da5SEd Maste brelse(bp); 49898dc8da5SEd Maste return (error); 49998dc8da5SEd Maste } 50098dc8da5SEd Maste 50198dc8da5SEd Maste if (function & FAT_GET) { 50298dc8da5SEd Maste if (FAT32(pmp)) 503ef633620SEd Maste readcn = getulong(bp->b_data + bo); 50498dc8da5SEd Maste else 505ef633620SEd Maste readcn = getushort(bp->b_data + bo); 50698dc8da5SEd Maste if (FAT12(pmp) & (cn & 1)) 50798dc8da5SEd Maste readcn >>= 4; 50898dc8da5SEd Maste readcn &= pmp->pm_fatmask; 50998dc8da5SEd Maste /* map reserved FAT entries to same values for all FATs */ 51098dc8da5SEd Maste if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 51198dc8da5SEd Maste readcn |= ~pmp->pm_fatmask; 51298dc8da5SEd Maste *oldcontents = readcn; 51398dc8da5SEd Maste } 51498dc8da5SEd Maste if (function & FAT_SET) { 51598dc8da5SEd Maste switch (pmp->pm_fatmask) { 51698dc8da5SEd Maste case FAT12_MASK: 517ef633620SEd Maste readcn = getushort(bp->b_data + bo); 51898dc8da5SEd Maste if (cn & 1) { 51998dc8da5SEd Maste readcn &= 0x000f; 52098dc8da5SEd Maste readcn |= newcontents << 4; 52198dc8da5SEd Maste } else { 52298dc8da5SEd Maste readcn &= 0xf000; 52398dc8da5SEd Maste readcn |= newcontents & 0xfff; 52498dc8da5SEd Maste } 525ef633620SEd Maste putushort(bp->b_data + bo, readcn); 52698dc8da5SEd Maste break; 52798dc8da5SEd Maste case FAT16_MASK: 528ef633620SEd Maste putushort(bp->b_data + bo, newcontents); 52998dc8da5SEd Maste break; 53098dc8da5SEd Maste case FAT32_MASK: 53198dc8da5SEd Maste /* 53298dc8da5SEd Maste * According to spec we have to retain the 53398dc8da5SEd Maste * high order bits of the FAT entry. 53498dc8da5SEd Maste */ 535ef633620SEd Maste readcn = getulong(bp->b_data + bo); 53698dc8da5SEd Maste readcn &= ~FAT32_MASK; 53798dc8da5SEd Maste readcn |= newcontents & FAT32_MASK; 538ef633620SEd Maste putulong(bp->b_data + bo, readcn); 53998dc8da5SEd Maste break; 54098dc8da5SEd Maste } 54198dc8da5SEd Maste updatefats(pmp, bp, bn); 54298dc8da5SEd Maste bp = NULL; 54398dc8da5SEd Maste pmp->pm_fmod = 1; 54498dc8da5SEd Maste } 54598dc8da5SEd Maste if (bp) 54698dc8da5SEd Maste brelse(bp); 54798dc8da5SEd Maste return (0); 54898dc8da5SEd Maste } 54998dc8da5SEd Maste 55098dc8da5SEd Maste /* 55198dc8da5SEd Maste * Update a contiguous cluster chain 55298dc8da5SEd Maste * 55398dc8da5SEd Maste * pmp - mount point 55498dc8da5SEd Maste * start - first cluster of chain 55598dc8da5SEd Maste * count - number of clusters in chain 55698dc8da5SEd Maste * fillwith - what to write into FAT entry of last cluster 55798dc8da5SEd Maste */ 55898dc8da5SEd Maste static int 55998dc8da5SEd Maste fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith) 56098dc8da5SEd Maste { 56198dc8da5SEd Maste int error; 56298dc8da5SEd Maste u_long bn, bo, bsize, byteoffset, readcn, newc; 563d485c77fSKonstantin Belousov struct m_buf *bp; 56498dc8da5SEd Maste 56598dc8da5SEd Maste #ifdef MSDOSFS_DEBUG 56698dc8da5SEd Maste printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n", 56798dc8da5SEd Maste pmp, start, count, fillwith); 56898dc8da5SEd Maste #endif 56998dc8da5SEd Maste /* 57098dc8da5SEd Maste * Be sure the clusters are in the filesystem. 57198dc8da5SEd Maste */ 57298dc8da5SEd Maste if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster) 57398dc8da5SEd Maste return (EINVAL); 57498dc8da5SEd Maste 57598dc8da5SEd Maste while (count > 0) { 57698dc8da5SEd Maste byteoffset = FATOFS(pmp, start); 57798dc8da5SEd Maste fatblock(pmp, byteoffset, &bn, &bsize, &bo); 578d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, bsize, NOCRED, &bp); 57998dc8da5SEd Maste if (error) { 58098dc8da5SEd Maste brelse(bp); 58198dc8da5SEd Maste return (error); 58298dc8da5SEd Maste } 58398dc8da5SEd Maste while (count > 0) { 58498dc8da5SEd Maste start++; 58598dc8da5SEd Maste newc = --count > 0 ? start : fillwith; 58698dc8da5SEd Maste switch (pmp->pm_fatmask) { 58798dc8da5SEd Maste case FAT12_MASK: 588ef633620SEd Maste readcn = getushort(bp->b_data + bo); 58998dc8da5SEd Maste if (start & 1) { 59098dc8da5SEd Maste readcn &= 0xf000; 59198dc8da5SEd Maste readcn |= newc & 0xfff; 59298dc8da5SEd Maste } else { 59398dc8da5SEd Maste readcn &= 0x000f; 59498dc8da5SEd Maste readcn |= newc << 4; 59598dc8da5SEd Maste } 596ef633620SEd Maste putushort(bp->b_data + bo, readcn); 59798dc8da5SEd Maste bo++; 59898dc8da5SEd Maste if (!(start & 1)) 59998dc8da5SEd Maste bo++; 60098dc8da5SEd Maste break; 60198dc8da5SEd Maste case FAT16_MASK: 602ef633620SEd Maste putushort(bp->b_data + bo, newc); 60398dc8da5SEd Maste bo += 2; 60498dc8da5SEd Maste break; 60598dc8da5SEd Maste case FAT32_MASK: 606ef633620SEd Maste readcn = getulong(bp->b_data + bo); 60798dc8da5SEd Maste readcn &= ~pmp->pm_fatmask; 60898dc8da5SEd Maste readcn |= newc & pmp->pm_fatmask; 609ef633620SEd Maste putulong(bp->b_data + bo, readcn); 61098dc8da5SEd Maste bo += 4; 61198dc8da5SEd Maste break; 61298dc8da5SEd Maste } 61398dc8da5SEd Maste if (bo >= bsize) 61498dc8da5SEd Maste break; 61598dc8da5SEd Maste } 61698dc8da5SEd Maste updatefats(pmp, bp, bn); 61798dc8da5SEd Maste } 61898dc8da5SEd Maste pmp->pm_fmod = 1; 61998dc8da5SEd Maste return (0); 62098dc8da5SEd Maste } 62198dc8da5SEd Maste 62298dc8da5SEd Maste /* 62398dc8da5SEd Maste * Check the length of a free cluster chain starting at start. 62498dc8da5SEd Maste * 62598dc8da5SEd Maste * pmp - mount point 62698dc8da5SEd Maste * start - start of chain 62798dc8da5SEd Maste * count - maximum interesting length 62898dc8da5SEd Maste */ 62998dc8da5SEd Maste static int 63098dc8da5SEd Maste chainlength(struct msdosfsmount *pmp, u_long start, u_long count) 63198dc8da5SEd Maste { 63298dc8da5SEd Maste u_long idx, max_idx; 63398dc8da5SEd Maste u_int map; 63498dc8da5SEd Maste u_long len; 63598dc8da5SEd Maste 63698dc8da5SEd Maste if (start > pmp->pm_maxcluster) 63798dc8da5SEd Maste return (0); 63898dc8da5SEd Maste max_idx = pmp->pm_maxcluster / N_INUSEBITS; 63998dc8da5SEd Maste idx = start / N_INUSEBITS; 64098dc8da5SEd Maste start %= N_INUSEBITS; 64198dc8da5SEd Maste map = pmp->pm_inusemap[idx]; 64298dc8da5SEd Maste map &= ~((1 << start) - 1); 64398dc8da5SEd Maste if (map) { 64498dc8da5SEd Maste len = ffs(map) - 1 - start; 64598dc8da5SEd Maste len = MIN(len, count); 64698dc8da5SEd Maste if (start + len > pmp->pm_maxcluster) 64798dc8da5SEd Maste len = pmp->pm_maxcluster - start + 1; 64898dc8da5SEd Maste return (len); 64998dc8da5SEd Maste } 65098dc8da5SEd Maste len = N_INUSEBITS - start; 65198dc8da5SEd Maste if (len >= count) { 65298dc8da5SEd Maste len = count; 65398dc8da5SEd Maste if (start + len > pmp->pm_maxcluster) 65498dc8da5SEd Maste len = pmp->pm_maxcluster - start + 1; 65598dc8da5SEd Maste return (len); 65698dc8da5SEd Maste } 65798dc8da5SEd Maste while (++idx <= max_idx) { 65898dc8da5SEd Maste if (len >= count) 65998dc8da5SEd Maste break; 66098dc8da5SEd Maste map = pmp->pm_inusemap[idx]; 66198dc8da5SEd Maste if (map) { 66298dc8da5SEd Maste len += ffs(map) - 1; 66398dc8da5SEd Maste break; 66498dc8da5SEd Maste } 66598dc8da5SEd Maste len += N_INUSEBITS; 66698dc8da5SEd Maste } 66798dc8da5SEd Maste len = MIN(len, count); 66898dc8da5SEd Maste if (start + len > pmp->pm_maxcluster) 66998dc8da5SEd Maste len = pmp->pm_maxcluster - start + 1; 67098dc8da5SEd Maste return (len); 67198dc8da5SEd Maste } 67298dc8da5SEd Maste 67398dc8da5SEd Maste /* 67498dc8da5SEd Maste * Allocate contigous free clusters. 67598dc8da5SEd Maste * 67698dc8da5SEd Maste * pmp - mount point. 67798dc8da5SEd Maste * start - start of cluster chain. 67898dc8da5SEd Maste * count - number of clusters to allocate. 67998dc8da5SEd Maste * fillwith - put this value into the FAT entry for the 68098dc8da5SEd Maste * last allocated cluster. 68198dc8da5SEd Maste * retcluster - put the first allocated cluster's number here. 68298dc8da5SEd Maste * got - how many clusters were actually allocated. 68398dc8da5SEd Maste */ 68498dc8da5SEd Maste static int 68598dc8da5SEd Maste chainalloc(struct msdosfsmount *pmp, u_long start, u_long count, 68698dc8da5SEd Maste u_long fillwith, u_long *retcluster, u_long *got) 68798dc8da5SEd Maste { 68898dc8da5SEd Maste int error; 68998dc8da5SEd Maste u_long cl, n; 69098dc8da5SEd Maste 69198dc8da5SEd Maste assert((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0); 69298dc8da5SEd Maste 69398dc8da5SEd Maste for (cl = start, n = count; n-- > 0;) 69498dc8da5SEd Maste usemap_alloc(pmp, cl++); 69598dc8da5SEd Maste pmp->pm_nxtfree = start + count; 69698dc8da5SEd Maste if (pmp->pm_nxtfree > pmp->pm_maxcluster) 69798dc8da5SEd Maste pmp->pm_nxtfree = CLUST_FIRST; 69898dc8da5SEd Maste pmp->pm_flags |= MSDOSFS_FSIMOD; 69998dc8da5SEd Maste error = fatchain(pmp, start, count, fillwith); 70098dc8da5SEd Maste if (error != 0) { 70198dc8da5SEd Maste for (cl = start, n = count; n-- > 0;) 70298dc8da5SEd Maste usemap_free(pmp, cl++); 70398dc8da5SEd Maste return (error); 70498dc8da5SEd Maste } 70598dc8da5SEd Maste #ifdef MSDOSFS_DEBUG 70698dc8da5SEd Maste printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n", 70798dc8da5SEd Maste start, count); 70898dc8da5SEd Maste #endif 70998dc8da5SEd Maste if (retcluster) 71098dc8da5SEd Maste *retcluster = start; 71198dc8da5SEd Maste if (got) 71298dc8da5SEd Maste *got = count; 71398dc8da5SEd Maste return (0); 71498dc8da5SEd Maste } 71598dc8da5SEd Maste 71698dc8da5SEd Maste /* 71798dc8da5SEd Maste * Allocate contiguous free clusters. 71898dc8da5SEd Maste * 71998dc8da5SEd Maste * pmp - mount point. 72098dc8da5SEd Maste * start - preferred start of cluster chain. 72198dc8da5SEd Maste * count - number of clusters requested. 72298dc8da5SEd Maste * fillwith - put this value into the FAT entry for the 72398dc8da5SEd Maste * last allocated cluster. 72498dc8da5SEd Maste * retcluster - put the first allocated cluster's number here. 72598dc8da5SEd Maste * got - how many clusters were actually allocated. 72698dc8da5SEd Maste */ 72798dc8da5SEd Maste int 72898dc8da5SEd Maste clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count, 72998dc8da5SEd Maste u_long fillwith, u_long *retcluster, u_long *got) 73098dc8da5SEd Maste { 73198dc8da5SEd Maste int error; 73298dc8da5SEd Maste 73398dc8da5SEd Maste error = clusteralloc1(pmp, start, count, fillwith, retcluster, got); 73498dc8da5SEd Maste return (error); 73598dc8da5SEd Maste } 73698dc8da5SEd Maste 73798dc8da5SEd Maste static int 73898dc8da5SEd Maste clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count, 73998dc8da5SEd Maste u_long fillwith, u_long *retcluster, u_long *got) 74098dc8da5SEd Maste { 74198dc8da5SEd Maste u_long idx; 74298dc8da5SEd Maste u_long len, newst, foundl, cn, l; 74398dc8da5SEd Maste u_long foundcn = 0; /* XXX: foundcn could be used unititialized */ 74498dc8da5SEd Maste u_int map; 74598dc8da5SEd Maste 74698dc8da5SEd Maste MSDOSFS_DPRINTF(("clusteralloc(): find %lu clusters\n", count)); 74798dc8da5SEd Maste 74898dc8da5SEd Maste if (start) { 74998dc8da5SEd Maste if ((len = chainlength(pmp, start, count)) >= count) 75098dc8da5SEd Maste return (chainalloc(pmp, start, count, fillwith, retcluster, got)); 75198dc8da5SEd Maste } else 75298dc8da5SEd Maste len = 0; 75398dc8da5SEd Maste 75498dc8da5SEd Maste newst = pmp->pm_nxtfree; 75598dc8da5SEd Maste foundl = 0; 75698dc8da5SEd Maste 75798dc8da5SEd Maste for (cn = newst; cn <= pmp->pm_maxcluster;) { 75898dc8da5SEd Maste idx = cn / N_INUSEBITS; 75998dc8da5SEd Maste map = pmp->pm_inusemap[idx]; 76098dc8da5SEd Maste map |= (1U << (cn % N_INUSEBITS)) - 1; 76198dc8da5SEd Maste if (map != FULL_RUN) { 76298dc8da5SEd Maste cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1; 76398dc8da5SEd Maste if ((l = chainlength(pmp, cn, count)) >= count) 76498dc8da5SEd Maste return (chainalloc(pmp, cn, count, fillwith, retcluster, got)); 76598dc8da5SEd Maste if (l > foundl) { 76698dc8da5SEd Maste foundcn = cn; 76798dc8da5SEd Maste foundl = l; 76898dc8da5SEd Maste } 76998dc8da5SEd Maste cn += l + 1; 77098dc8da5SEd Maste continue; 77198dc8da5SEd Maste } 77298dc8da5SEd Maste cn += N_INUSEBITS - cn % N_INUSEBITS; 77398dc8da5SEd Maste } 77498dc8da5SEd Maste for (cn = 0; cn < newst;) { 77598dc8da5SEd Maste idx = cn / N_INUSEBITS; 77698dc8da5SEd Maste map = pmp->pm_inusemap[idx]; 77798dc8da5SEd Maste map |= (1U << (cn % N_INUSEBITS)) - 1; 77898dc8da5SEd Maste if (map != FULL_RUN) { 77998dc8da5SEd Maste cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1; 78098dc8da5SEd Maste if ((l = chainlength(pmp, cn, count)) >= count) 78198dc8da5SEd Maste return (chainalloc(pmp, cn, count, fillwith, retcluster, got)); 78298dc8da5SEd Maste if (l > foundl) { 78398dc8da5SEd Maste foundcn = cn; 78498dc8da5SEd Maste foundl = l; 78598dc8da5SEd Maste } 78698dc8da5SEd Maste cn += l + 1; 78798dc8da5SEd Maste continue; 78898dc8da5SEd Maste } 78998dc8da5SEd Maste cn += N_INUSEBITS - cn % N_INUSEBITS; 79098dc8da5SEd Maste } 79198dc8da5SEd Maste 79298dc8da5SEd Maste if (!foundl) 79398dc8da5SEd Maste return (ENOSPC); 79498dc8da5SEd Maste 79598dc8da5SEd Maste if (len) 79698dc8da5SEd Maste return (chainalloc(pmp, start, len, fillwith, retcluster, got)); 79798dc8da5SEd Maste else 79898dc8da5SEd Maste return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got)); 79998dc8da5SEd Maste } 80098dc8da5SEd Maste 80198dc8da5SEd Maste 80298dc8da5SEd Maste /* 80398dc8da5SEd Maste * Free a chain of clusters. 80498dc8da5SEd Maste * 80598dc8da5SEd Maste * pmp - address of the msdosfs mount structure for the filesystem 80698dc8da5SEd Maste * containing the cluster chain to be freed. 80798dc8da5SEd Maste * startcluster - number of the 1st cluster in the chain of clusters to be 80898dc8da5SEd Maste * freed. 80998dc8da5SEd Maste */ 81098dc8da5SEd Maste int 81198dc8da5SEd Maste freeclusterchain(struct msdosfsmount *pmp, u_long cluster) 81298dc8da5SEd Maste { 81398dc8da5SEd Maste int error; 814d485c77fSKonstantin Belousov struct m_buf *bp = NULL; 81598dc8da5SEd Maste u_long bn, bo, bsize, byteoffset; 81698dc8da5SEd Maste u_long readcn, lbn = -1; 81798dc8da5SEd Maste 81898dc8da5SEd Maste while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) { 81998dc8da5SEd Maste byteoffset = FATOFS(pmp, cluster); 82098dc8da5SEd Maste fatblock(pmp, byteoffset, &bn, &bsize, &bo); 82198dc8da5SEd Maste if (lbn != bn) { 82298dc8da5SEd Maste if (bp) 82398dc8da5SEd Maste updatefats(pmp, bp, lbn); 824d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, bsize, 825d485c77fSKonstantin Belousov NOCRED, &bp); 82698dc8da5SEd Maste if (error) { 82798dc8da5SEd Maste brelse(bp); 82898dc8da5SEd Maste return (error); 82998dc8da5SEd Maste } 83098dc8da5SEd Maste lbn = bn; 83198dc8da5SEd Maste } 83298dc8da5SEd Maste usemap_free(pmp, cluster); 83398dc8da5SEd Maste switch (pmp->pm_fatmask) { 83498dc8da5SEd Maste case FAT12_MASK: 835ef633620SEd Maste readcn = getushort(bp->b_data + bo); 83698dc8da5SEd Maste if (cluster & 1) { 83798dc8da5SEd Maste cluster = readcn >> 4; 83898dc8da5SEd Maste readcn &= 0x000f; 83998dc8da5SEd Maste readcn |= MSDOSFSFREE << 4; 84098dc8da5SEd Maste } else { 84198dc8da5SEd Maste cluster = readcn; 84298dc8da5SEd Maste readcn &= 0xf000; 84398dc8da5SEd Maste readcn |= MSDOSFSFREE & 0xfff; 84498dc8da5SEd Maste } 845ef633620SEd Maste putushort(bp->b_data + bo, readcn); 84698dc8da5SEd Maste break; 84798dc8da5SEd Maste case FAT16_MASK: 848ef633620SEd Maste cluster = getushort(bp->b_data + bo); 849ef633620SEd Maste putushort(bp->b_data + bo, MSDOSFSFREE); 85098dc8da5SEd Maste break; 85198dc8da5SEd Maste case FAT32_MASK: 852ef633620SEd Maste cluster = getulong(bp->b_data + bo); 853ef633620SEd Maste putulong(bp->b_data + bo, 85498dc8da5SEd Maste (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK)); 85598dc8da5SEd Maste break; 85698dc8da5SEd Maste } 85798dc8da5SEd Maste cluster &= pmp->pm_fatmask; 85898dc8da5SEd Maste if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD) 85998dc8da5SEd Maste cluster |= pmp->pm_fatmask; 86098dc8da5SEd Maste } 86198dc8da5SEd Maste if (bp) 86298dc8da5SEd Maste updatefats(pmp, bp, bn); 86398dc8da5SEd Maste return (0); 86498dc8da5SEd Maste } 86598dc8da5SEd Maste 86698dc8da5SEd Maste /* 86798dc8da5SEd Maste * Read in FAT blocks looking for free clusters. For every free cluster 86898dc8da5SEd Maste * found turn off its corresponding bit in the pm_inusemap. 86998dc8da5SEd Maste */ 87098dc8da5SEd Maste int 87198dc8da5SEd Maste fillinusemap(struct msdosfsmount *pmp) 87298dc8da5SEd Maste { 873d485c77fSKonstantin Belousov struct m_buf *bp; 87498dc8da5SEd Maste u_long bn, bo, bsize, byteoffset, cn, readcn; 87598dc8da5SEd Maste int error; 87698dc8da5SEd Maste 8772037e988SEd Maste bp = NULL; 8782037e988SEd Maste 87998dc8da5SEd Maste /* 88098dc8da5SEd Maste * Mark all clusters in use, we mark the free ones in the FAT scan 88198dc8da5SEd Maste * loop further down. 88298dc8da5SEd Maste */ 88398dc8da5SEd Maste for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++) 88498dc8da5SEd Maste pmp->pm_inusemap[cn] = FULL_RUN; 88598dc8da5SEd Maste 88698dc8da5SEd Maste /* 88798dc8da5SEd Maste * Figure how many free clusters are in the filesystem by ripping 88898dc8da5SEd Maste * through the FAT counting the number of entries whose content is 88998dc8da5SEd Maste * zero. These represent free clusters. 89098dc8da5SEd Maste */ 89198dc8da5SEd Maste pmp->pm_freeclustercount = 0; 89259e9b369SEd Maste for (cn = 0; cn <= pmp->pm_maxcluster; cn++) { 89398dc8da5SEd Maste byteoffset = FATOFS(pmp, cn); 89498dc8da5SEd Maste bo = byteoffset % pmp->pm_fatblocksize; 89559e9b369SEd Maste if (bo == 0) { 89698dc8da5SEd Maste /* Read new FAT block */ 89798dc8da5SEd Maste if (bp != NULL) 89898dc8da5SEd Maste brelse(bp); 89998dc8da5SEd Maste fatblock(pmp, byteoffset, &bn, &bsize, NULL); 900d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, bsize, 901d485c77fSKonstantin Belousov NOCRED, &bp); 90259e9b369SEd Maste if (error != 0) 90398dc8da5SEd Maste return (error); 90498dc8da5SEd Maste } 90598dc8da5SEd Maste if (FAT32(pmp)) 906ef633620SEd Maste readcn = getulong(bp->b_data + bo); 90798dc8da5SEd Maste else 908ef633620SEd Maste readcn = getushort(bp->b_data + bo); 90998dc8da5SEd Maste if (FAT12(pmp) && (cn & 1)) 91098dc8da5SEd Maste readcn >>= 4; 91198dc8da5SEd Maste readcn &= pmp->pm_fatmask; 91298dc8da5SEd Maste 91359e9b369SEd Maste /* 91459e9b369SEd Maste * Check if the FAT ID matches the BPB's media descriptor and 91559e9b369SEd Maste * all other bits are set to 1. 91659e9b369SEd Maste */ 91759e9b369SEd Maste if (cn == 0 && readcn != ((pmp->pm_fatmask & 0xffffff00) | 91859e9b369SEd Maste pmp->pm_bpb.bpbMedia)) { 91959e9b369SEd Maste #ifdef MSDOSFS_DEBUG 92059e9b369SEd Maste printf("mountmsdosfs(): Media descriptor in BPB" 92159e9b369SEd Maste "does not match FAT ID\n"); 92259e9b369SEd Maste #endif 92359e9b369SEd Maste brelse(bp); 92459e9b369SEd Maste return (EINVAL); 92559e9b369SEd Maste } else if (readcn == CLUST_FREE) 92698dc8da5SEd Maste usemap_free(pmp, cn); 92798dc8da5SEd Maste } 92898dc8da5SEd Maste if (bp != NULL) 92998dc8da5SEd Maste brelse(bp); 93098dc8da5SEd Maste 93198dc8da5SEd Maste for (cn = pmp->pm_maxcluster + 1; cn < (pmp->pm_maxcluster + 93298dc8da5SEd Maste N_INUSEBITS) / N_INUSEBITS; cn++) 93398dc8da5SEd Maste pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS); 93498dc8da5SEd Maste 93598dc8da5SEd Maste return (0); 93698dc8da5SEd Maste } 93798dc8da5SEd Maste 93898dc8da5SEd Maste /* 93998dc8da5SEd Maste * Allocate a new cluster and chain it onto the end of the file. 94098dc8da5SEd Maste * 94198dc8da5SEd Maste * dep - the file to extend 94298dc8da5SEd Maste * count - number of clusters to allocate 94398dc8da5SEd Maste * bpp - where to return the address of the buf header for the first new 94498dc8da5SEd Maste * file block 94598dc8da5SEd Maste * ncp - where to put cluster number of the first newly allocated cluster 94698dc8da5SEd Maste * If this pointer is 0, do not return the cluster number. 94798dc8da5SEd Maste * flags - see fat.h 94898dc8da5SEd Maste * 94998dc8da5SEd Maste * NOTE: This function is not responsible for turning on the DE_UPDATE bit of 95098dc8da5SEd Maste * the de_flag field of the denode and it does not change the de_FileSize 95198dc8da5SEd Maste * field. This is left for the caller to do. 95298dc8da5SEd Maste */ 95398dc8da5SEd Maste int 954d485c77fSKonstantin Belousov m_extendfile(struct denode *dep, u_long count, struct m_buf **bpp, u_long *ncp, 95598dc8da5SEd Maste int flags) 95698dc8da5SEd Maste { 95798dc8da5SEd Maste int error; 95898dc8da5SEd Maste u_long frcn; 95998dc8da5SEd Maste u_long cn, got; 96098dc8da5SEd Maste struct msdosfsmount *pmp = dep->de_pmp; 961d485c77fSKonstantin Belousov struct m_buf *bp; 96298dc8da5SEd Maste 96398dc8da5SEd Maste /* 96498dc8da5SEd Maste * Don't try to extend the root directory 96598dc8da5SEd Maste */ 96698dc8da5SEd Maste if (dep->de_StartCluster == MSDOSFSROOT 96798dc8da5SEd Maste && (dep->de_Attributes & ATTR_DIRECTORY)) { 96898dc8da5SEd Maste #ifdef MSDOSFS_DEBUG 96998dc8da5SEd Maste printf("extendfile(): attempt to extend root directory\n"); 97098dc8da5SEd Maste #endif 97198dc8da5SEd Maste return (ENOSPC); 97298dc8da5SEd Maste } 97398dc8da5SEd Maste 97498dc8da5SEd Maste /* 97598dc8da5SEd Maste * If the "file's last cluster" cache entry is empty, and the file 97698dc8da5SEd Maste * is not empty, then fill the cache entry by calling pcbmap(). 97798dc8da5SEd Maste */ 97898dc8da5SEd Maste if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY && 97998dc8da5SEd Maste dep->de_StartCluster != 0) { 98098dc8da5SEd Maste error = pcbmap(dep, 0xffff, 0, &cn, 0); 98198dc8da5SEd Maste /* we expect it to return E2BIG */ 98298dc8da5SEd Maste if (error != E2BIG) 98398dc8da5SEd Maste return (error); 98498dc8da5SEd Maste } 98598dc8da5SEd Maste 98698dc8da5SEd Maste dep->de_fc[FC_NEXTTOLASTFC].fc_frcn = 98798dc8da5SEd Maste dep->de_fc[FC_LASTFC].fc_frcn; 98898dc8da5SEd Maste dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn = 98998dc8da5SEd Maste dep->de_fc[FC_LASTFC].fc_fsrcn; 99098dc8da5SEd Maste while (count > 0) { 99198dc8da5SEd Maste /* 99298dc8da5SEd Maste * Allocate a new cluster chain and cat onto the end of the 99398dc8da5SEd Maste * file. If the file is empty we make de_StartCluster point 99498dc8da5SEd Maste * to the new block. Note that de_StartCluster being 0 is 99598dc8da5SEd Maste * sufficient to be sure the file is empty since we exclude 99698dc8da5SEd Maste * attempts to extend the root directory above, and the root 99798dc8da5SEd Maste * dir is the only file with a startcluster of 0 that has 99898dc8da5SEd Maste * blocks allocated (sort of). 99998dc8da5SEd Maste */ 100098dc8da5SEd Maste if (dep->de_StartCluster == 0) 100198dc8da5SEd Maste cn = 0; 100298dc8da5SEd Maste else 100398dc8da5SEd Maste cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1; 100498dc8da5SEd Maste error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got); 100598dc8da5SEd Maste if (error) 100698dc8da5SEd Maste return (error); 100798dc8da5SEd Maste 100898dc8da5SEd Maste count -= got; 100998dc8da5SEd Maste 101098dc8da5SEd Maste /* 101198dc8da5SEd Maste * Give them the filesystem relative cluster number if they want 101298dc8da5SEd Maste * it. 101398dc8da5SEd Maste */ 101498dc8da5SEd Maste if (ncp) { 101598dc8da5SEd Maste *ncp = cn; 101698dc8da5SEd Maste ncp = NULL; 101798dc8da5SEd Maste } 101898dc8da5SEd Maste 101998dc8da5SEd Maste if (dep->de_StartCluster == 0) { 102098dc8da5SEd Maste dep->de_StartCluster = cn; 102198dc8da5SEd Maste frcn = 0; 102298dc8da5SEd Maste } else { 102398dc8da5SEd Maste error = fatentry(FAT_SET, pmp, 102498dc8da5SEd Maste dep->de_fc[FC_LASTFC].fc_fsrcn, 102598dc8da5SEd Maste 0, cn); 102698dc8da5SEd Maste if (error) { 102798dc8da5SEd Maste clusterfree(pmp, cn, NULL); 102898dc8da5SEd Maste return (error); 102998dc8da5SEd Maste } 103098dc8da5SEd Maste frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1; 103198dc8da5SEd Maste } 103298dc8da5SEd Maste 103398dc8da5SEd Maste /* 103498dc8da5SEd Maste * Update the "last cluster of the file" entry in the 103598dc8da5SEd Maste * denode's FAT cache. 103698dc8da5SEd Maste */ 103798dc8da5SEd Maste fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1); 103898dc8da5SEd Maste 103998dc8da5SEd Maste if ((flags & DE_CLEAR) && 104098dc8da5SEd Maste (dep->de_Attributes & ATTR_DIRECTORY)) { 104198dc8da5SEd Maste while (got-- > 0) { 1042d485c77fSKonstantin Belousov bp = getblk((void *)pmp->pm_devvp, 104398dc8da5SEd Maste cntobn(pmp, cn++), 104498dc8da5SEd Maste pmp->pm_bpcluster, 0, 0, 0); 104598dc8da5SEd Maste clrbuf(bp); 104698dc8da5SEd Maste if (bpp) { 104798dc8da5SEd Maste *bpp = bp; 104898dc8da5SEd Maste bpp = NULL; 104998dc8da5SEd Maste } else { 105098dc8da5SEd Maste bdwrite(bp); 105198dc8da5SEd Maste } 105298dc8da5SEd Maste } 105398dc8da5SEd Maste } 105498dc8da5SEd Maste } 105598dc8da5SEd Maste 105698dc8da5SEd Maste return (0); 105798dc8da5SEd Maste } 1058