1 /* $FreeBSD$ */ 2 /* $NetBSD: msdosfs_fat.c,v 1.28 1997/11/17 15:36:49 ws Exp $ */ 3 4 /*- 5 * SPDX-License-Identifier: BSD-4-Clause 6 * 7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 9 * All rights reserved. 10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by TooLs GmbH. 23 * 4. The name of TooLs GmbH may not be used to endorse or promote products 24 * derived from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 /*- 38 * Written by Paul Popelka (paulp@uts.amdahl.com) 39 * 40 * You can do anything you want with this software, just don't say you wrote 41 * it, and don't remove this notice. 42 * 43 * This software is provided "as is". 44 * 45 * The author supplies this software to be publicly redistributed on the 46 * understanding that the author is not responsible for the correct 47 * functioning of this software in any circumstances and is not liable for 48 * any damages caused by this software. 49 * 50 * October 1992 51 */ 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/buf.h> 56 #include <sys/mount.h> 57 #include <sys/vmmeter.h> 58 #include <sys/vnode.h> 59 60 #include <fs/msdosfs/bpb.h> 61 #include <fs/msdosfs/direntry.h> 62 #include <fs/msdosfs/denode.h> 63 #include <fs/msdosfs/fat.h> 64 #include <fs/msdosfs/msdosfsmount.h> 65 66 #define FULL_RUN ((u_int)0xffffffff) 67 68 static int chainalloc(struct msdosfsmount *pmp, u_long start, 69 u_long count, u_long fillwith, u_long *retcluster, 70 u_long *got); 71 static int chainlength(struct msdosfsmount *pmp, u_long start, 72 u_long count); 73 static void fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, 74 u_long *sizep, u_long *bop); 75 static int fatchain(struct msdosfsmount *pmp, u_long start, u_long count, 76 u_long fillwith); 77 static void fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, 78 u_long *fsrcnp); 79 static void updatefats(struct msdosfsmount *pmp, struct buf *bp, 80 u_long fatbn); 81 static __inline void 82 usemap_alloc(struct msdosfsmount *pmp, u_long cn); 83 static int usemap_free(struct msdosfsmount *pmp, u_long cn); 84 static int clusteralloc1(struct msdosfsmount *pmp, u_long start, 85 u_long count, u_long fillwith, u_long *retcluster, 86 u_long *got); 87 88 static void 89 fatblock(struct msdosfsmount *pmp, u_long ofs, u_long *bnp, u_long *sizep, 90 u_long *bop) 91 { 92 u_long bn, size; 93 94 bn = ofs / pmp->pm_fatblocksize * pmp->pm_fatblocksec; 95 size = min(pmp->pm_fatblocksec, pmp->pm_FATsecs - bn) 96 * DEV_BSIZE; 97 bn += pmp->pm_fatblk + pmp->pm_curfat * pmp->pm_FATsecs; 98 99 if (bnp) 100 *bnp = bn; 101 if (sizep) 102 *sizep = size; 103 if (bop) 104 *bop = ofs % pmp->pm_fatblocksize; 105 } 106 107 /* 108 * Map the logical cluster number of a file into a physical disk sector 109 * that is filesystem relative. 110 * 111 * dep - address of denode representing the file of interest 112 * findcn - file relative cluster whose filesystem relative cluster number 113 * and/or block number are/is to be found 114 * bnp - address of where to place the filesystem relative block number. 115 * If this pointer is null then don't return this quantity. 116 * cnp - address of where to place the filesystem relative cluster number. 117 * If this pointer is null then don't return this quantity. 118 * sp - pointer to returned block size 119 * 120 * NOTE: Either bnp or cnp must be non-null. 121 * This function has one side effect. If the requested file relative cluster 122 * is beyond the end of file, then the actual number of clusters in the file 123 * is returned in *cnp. This is useful for determining how long a directory is. 124 * If cnp is null, nothing is returned. 125 */ 126 int 127 pcbmap(struct denode *dep, u_long findcn, daddr_t *bnp, u_long *cnp, int *sp) 128 { 129 int error; 130 u_long i; 131 u_long cn; 132 u_long prevcn = 0; /* XXX: prevcn could be used unititialized */ 133 u_long byteoffset; 134 u_long bn; 135 u_long bo; 136 struct buf *bp = NULL; 137 u_long bp_bn = -1; 138 struct msdosfsmount *pmp = dep->de_pmp; 139 u_long bsize; 140 141 KASSERT(bnp != NULL || cnp != NULL || sp != NULL, 142 ("pcbmap: extra call")); 143 ASSERT_VOP_ELOCKED(DETOV(dep), "pcbmap"); 144 145 cn = dep->de_StartCluster; 146 /* 147 * The "file" that makes up the root directory is contiguous, 148 * permanently allocated, of fixed size, and is not made up of 149 * clusters. If the cluster number is beyond the end of the root 150 * directory, then return the number of clusters in the file. 151 */ 152 if (cn == MSDOSFSROOT) { 153 if (dep->de_Attributes & ATTR_DIRECTORY) { 154 if (de_cn2off(pmp, findcn) >= dep->de_FileSize) { 155 if (cnp) 156 *cnp = de_bn2cn(pmp, pmp->pm_rootdirsize); 157 return (E2BIG); 158 } 159 if (bnp) 160 *bnp = pmp->pm_rootdirblk + de_cn2bn(pmp, findcn); 161 if (cnp) 162 *cnp = MSDOSFSROOT; 163 if (sp) 164 *sp = min(pmp->pm_bpcluster, 165 dep->de_FileSize - de_cn2off(pmp, findcn)); 166 return (0); 167 } else { /* just an empty file */ 168 if (cnp) 169 *cnp = 0; 170 return (E2BIG); 171 } 172 } 173 174 /* 175 * All other files do I/O in cluster sized blocks 176 */ 177 if (sp) 178 *sp = pmp->pm_bpcluster; 179 180 /* 181 * Rummage around in the FAT cache, maybe we can avoid tromping 182 * through every FAT entry for the file. And, keep track of how far 183 * off the cache was from where we wanted to be. 184 */ 185 i = 0; 186 fc_lookup(dep, findcn, &i, &cn); 187 188 /* 189 * Handle all other files or directories the normal way. 190 */ 191 for (; i < findcn; i++) { 192 /* 193 * Stop with all reserved clusters, not just with EOF. 194 */ 195 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 196 goto hiteof; 197 byteoffset = FATOFS(pmp, cn); 198 fatblock(pmp, byteoffset, &bn, &bsize, &bo); 199 if (bn != bp_bn) { 200 if (bp) 201 brelse(bp); 202 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 203 if (error) { 204 return (error); 205 } 206 bp_bn = bn; 207 } 208 prevcn = cn; 209 if (bo >= bsize) { 210 if (bp) 211 brelse(bp); 212 return (EIO); 213 } 214 if (FAT32(pmp)) 215 cn = getulong(bp->b_data + bo); 216 else 217 cn = getushort(bp->b_data + bo); 218 if (FAT12(pmp) && (prevcn & 1)) 219 cn >>= 4; 220 cn &= pmp->pm_fatmask; 221 222 /* 223 * Force the special cluster numbers 224 * to be the same for all cluster sizes 225 * to let the rest of msdosfs handle 226 * all cases the same. 227 */ 228 if ((cn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 229 cn |= ~pmp->pm_fatmask; 230 } 231 232 if (!MSDOSFSEOF(pmp, cn)) { 233 if (bp) 234 brelse(bp); 235 if (bnp) 236 *bnp = cntobn(pmp, cn); 237 if (cnp) 238 *cnp = cn; 239 fc_setcache(dep, FC_LASTMAP, i, cn); 240 return (0); 241 } 242 243 hiteof:; 244 if (cnp) 245 *cnp = i; 246 if (bp) 247 brelse(bp); 248 /* update last file cluster entry in the FAT cache */ 249 fc_setcache(dep, FC_LASTFC, i - 1, prevcn); 250 return (E2BIG); 251 } 252 253 /* 254 * Find the closest entry in the FAT cache to the cluster we are looking 255 * for. 256 */ 257 static void 258 fc_lookup(struct denode *dep, u_long findcn, u_long *frcnp, u_long *fsrcnp) 259 { 260 int i; 261 u_long cn; 262 struct fatcache *closest = NULL; 263 264 ASSERT_VOP_LOCKED(DETOV(dep), "fc_lookup"); 265 266 for (i = 0; i < FC_SIZE; i++) { 267 cn = dep->de_fc[i].fc_frcn; 268 if (cn != FCE_EMPTY && cn <= findcn) { 269 if (closest == NULL || cn > closest->fc_frcn) 270 closest = &dep->de_fc[i]; 271 } 272 } 273 if (closest) { 274 *frcnp = closest->fc_frcn; 275 *fsrcnp = closest->fc_fsrcn; 276 } 277 } 278 279 /* 280 * Purge the FAT cache in denode dep of all entries relating to file 281 * relative cluster frcn and beyond. 282 */ 283 void 284 fc_purge(struct denode *dep, u_int frcn) 285 { 286 int i; 287 struct fatcache *fcp; 288 289 ASSERT_VOP_ELOCKED(DETOV(dep), "fc_purge"); 290 291 fcp = dep->de_fc; 292 for (i = 0; i < FC_SIZE; i++, fcp++) { 293 if (fcp->fc_frcn >= frcn) 294 fcp->fc_frcn = FCE_EMPTY; 295 } 296 } 297 298 /* 299 * Update the FAT. 300 * If mirroring the FAT, update all copies, with the first copy as last. 301 * Else update only the current FAT (ignoring the others). 302 * 303 * pmp - msdosfsmount structure for filesystem to update 304 * bp - addr of modified FAT block 305 * fatbn - block number relative to begin of filesystem of the modified FAT block. 306 */ 307 static void 308 updatefats(struct msdosfsmount *pmp, struct buf *bp, u_long fatbn) 309 { 310 struct buf *bpn; 311 int cleanfat, i; 312 313 #ifdef MSDOSFS_DEBUG 314 printf("updatefats(pmp %p, bp %p, fatbn %lu)\n", pmp, bp, fatbn); 315 #endif 316 317 if (pmp->pm_flags & MSDOSFS_FATMIRROR) { 318 /* 319 * Now copy the block(s) of the modified FAT to the other copies of 320 * the FAT and write them out. This is faster than reading in the 321 * other FATs and then writing them back out. This could tie up 322 * the FAT for quite a while. Preventing others from accessing it. 323 * To prevent us from going after the FAT quite so much we use 324 * delayed writes, unless they specified "synchronous" when the 325 * filesystem was mounted. If synch is asked for then use 326 * bwrite()'s and really slow things down. 327 */ 328 if (fatbn != pmp->pm_fatblk || FAT12(pmp)) 329 cleanfat = 0; 330 else if (FAT16(pmp)) 331 cleanfat = 16; 332 else 333 cleanfat = 32; 334 for (i = 1; i < pmp->pm_FATs; i++) { 335 fatbn += pmp->pm_FATsecs; 336 /* getblk() never fails */ 337 bpn = getblk(pmp->pm_devvp, fatbn, bp->b_bcount, 338 0, 0, 0); 339 memcpy(bpn->b_data, bp->b_data, bp->b_bcount); 340 /* Force the clean bit on in the other copies. */ 341 if (cleanfat == 16) 342 ((uint8_t *)bpn->b_data)[3] |= 0x80; 343 else if (cleanfat == 32) 344 ((uint8_t *)bpn->b_data)[7] |= 0x08; 345 if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS) 346 bwrite(bpn); 347 else 348 bdwrite(bpn); 349 } 350 } 351 352 /* 353 * Write out the first (or current) FAT last. 354 */ 355 if (pmp->pm_mountp->mnt_flag & MNT_SYNCHRONOUS) 356 bwrite(bp); 357 else 358 bdwrite(bp); 359 } 360 361 /* 362 * Updating entries in 12 bit FATs is a pain in the butt. 363 * 364 * The following picture shows where nibbles go when moving from a 12 bit 365 * cluster number into the appropriate bytes in the FAT. 366 * 367 * byte m byte m+1 byte m+2 368 * +----+----+ +----+----+ +----+----+ 369 * | 0 1 | | 2 3 | | 4 5 | FAT bytes 370 * +----+----+ +----+----+ +----+----+ 371 * 372 * +----+----+----+ +----+----+----+ 373 * | 3 0 1 | | 4 5 2 | 374 * +----+----+----+ +----+----+----+ 375 * cluster n cluster n+1 376 * 377 * Where n is even. m = n + (n >> 2) 378 * 379 */ 380 static __inline void 381 usemap_alloc(struct msdosfsmount *pmp, u_long cn) 382 { 383 384 MSDOSFS_ASSERT_MP_LOCKED(pmp); 385 386 KASSERT(cn <= pmp->pm_maxcluster, ("cn too large %lu %lu", cn, 387 pmp->pm_maxcluster)); 388 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0, 389 ("usemap_alloc on ro msdosfs mount")); 390 KASSERT((pmp->pm_inusemap[cn / N_INUSEBITS] & 391 (1U << (cn % N_INUSEBITS))) == 0, 392 ("Allocating used sector %ld %ld %x", cn, cn % N_INUSEBITS, 393 (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS])); 394 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS); 395 KASSERT(pmp->pm_freeclustercount > 0, ("usemap_alloc: too little")); 396 pmp->pm_freeclustercount--; 397 pmp->pm_flags |= MSDOSFS_FSIMOD; 398 } 399 400 static int 401 usemap_free(struct msdosfsmount *pmp, u_long cn) 402 { 403 404 MSDOSFS_ASSERT_MP_LOCKED(pmp); 405 406 KASSERT(cn <= pmp->pm_maxcluster, ("cn too large %lu %lu", cn, 407 pmp->pm_maxcluster)); 408 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0, 409 ("usemap_free on ro msdosfs mount")); 410 if ((pmp->pm_inusemap[cn / N_INUSEBITS] & 411 (1U << (cn % N_INUSEBITS))) == 0) { 412 printf("%s: Freeing unused sector %ld %ld %x\n", 413 pmp->pm_mountp->mnt_stat.f_mntonname, cn, cn % N_INUSEBITS, 414 (unsigned)pmp->pm_inusemap[cn / N_INUSEBITS]); 415 msdosfs_integrity_error(pmp); 416 return (EINTEGRITY); 417 } 418 pmp->pm_freeclustercount++; 419 pmp->pm_flags |= MSDOSFS_FSIMOD; 420 pmp->pm_inusemap[cn / N_INUSEBITS] &= ~(1U << (cn % N_INUSEBITS)); 421 return (0); 422 } 423 424 void 425 clusterfree(struct msdosfsmount *pmp, u_long cluster) 426 { 427 int error; 428 u_long oldcn; 429 430 error = fatentry(FAT_GET_AND_SET, pmp, cluster, &oldcn, MSDOSFSFREE); 431 if (error != 0) 432 return; 433 /* 434 * If the cluster was successfully marked free, then update 435 * the count of free clusters, and turn off the "allocated" 436 * bit in the "in use" cluster bit map. 437 */ 438 MSDOSFS_LOCK_MP(pmp); 439 error = usemap_free(pmp, cluster); 440 MSDOSFS_UNLOCK_MP(pmp); 441 } 442 443 /* 444 * Get or Set or 'Get and Set' the cluster'th entry in the FAT. 445 * 446 * function - whether to get or set a FAT entry 447 * pmp - address of the msdosfsmount structure for the filesystem 448 * whose FAT is to be manipulated. 449 * cn - which cluster is of interest 450 * oldcontents - address of a word that is to receive the contents of the 451 * cluster'th entry if this is a get function 452 * newcontents - the new value to be written into the cluster'th element of 453 * the FAT if this is a set function. 454 * 455 * This function can also be used to free a cluster by setting the FAT entry 456 * for a cluster to 0. 457 * 458 * All copies of the FAT are updated if this is a set function. NOTE: If 459 * fatentry() marks a cluster as free it does not update the inusemap in 460 * the msdosfsmount structure. This is left to the caller. 461 */ 462 int 463 fatentry(int function, struct msdosfsmount *pmp, u_long cn, u_long *oldcontents, 464 u_long newcontents) 465 { 466 int error; 467 u_long readcn; 468 u_long bn, bo, bsize, byteoffset; 469 struct buf *bp; 470 471 #ifdef MSDOSFS_DEBUG 472 printf("fatentry(func %d, pmp %p, clust %lu, oldcon %p, newcon %lx)\n", 473 function, pmp, cn, oldcontents, newcontents); 474 #endif 475 476 #ifdef DIAGNOSTIC 477 /* 478 * Be sure they asked us to do something. 479 */ 480 if ((function & (FAT_SET | FAT_GET)) == 0) { 481 #ifdef MSDOSFS_DEBUG 482 printf("fatentry(): function code doesn't specify get or set\n"); 483 #endif 484 return (EINVAL); 485 } 486 487 /* 488 * If they asked us to return a cluster number but didn't tell us 489 * where to put it, give them an error. 490 */ 491 if ((function & FAT_GET) && oldcontents == NULL) { 492 #ifdef MSDOSFS_DEBUG 493 printf("fatentry(): get function with no place to put result\n"); 494 #endif 495 return (EINVAL); 496 } 497 #endif 498 499 /* 500 * Be sure the requested cluster is in the filesystem. 501 */ 502 if (cn < CLUST_FIRST || cn > pmp->pm_maxcluster) 503 return (EINVAL); 504 505 byteoffset = FATOFS(pmp, cn); 506 fatblock(pmp, byteoffset, &bn, &bsize, &bo); 507 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 508 if (error) { 509 return (error); 510 } 511 512 if (function & FAT_GET) { 513 if (FAT32(pmp)) 514 readcn = getulong(bp->b_data + bo); 515 else 516 readcn = getushort(bp->b_data + bo); 517 if (FAT12(pmp) & (cn & 1)) 518 readcn >>= 4; 519 readcn &= pmp->pm_fatmask; 520 /* map reserved FAT entries to same values for all FATs */ 521 if ((readcn | ~pmp->pm_fatmask) >= CLUST_RSRVD) 522 readcn |= ~pmp->pm_fatmask; 523 *oldcontents = readcn; 524 } 525 if (function & FAT_SET) { 526 switch (pmp->pm_fatmask) { 527 case FAT12_MASK: 528 readcn = getushort(bp->b_data + bo); 529 if (cn & 1) { 530 readcn &= 0x000f; 531 readcn |= newcontents << 4; 532 } else { 533 readcn &= 0xf000; 534 readcn |= newcontents & 0xfff; 535 } 536 putushort(bp->b_data + bo, readcn); 537 break; 538 case FAT16_MASK: 539 putushort(bp->b_data + bo, newcontents); 540 break; 541 case FAT32_MASK: 542 /* 543 * According to spec we have to retain the 544 * high order bits of the FAT entry. 545 */ 546 readcn = getulong(bp->b_data + bo); 547 readcn &= ~FAT32_MASK; 548 readcn |= newcontents & FAT32_MASK; 549 putulong(bp->b_data + bo, readcn); 550 break; 551 } 552 updatefats(pmp, bp, bn); 553 bp = NULL; 554 pmp->pm_fmod = 1; 555 } 556 if (bp) 557 brelse(bp); 558 return (0); 559 } 560 561 /* 562 * Update a contiguous cluster chain 563 * 564 * pmp - mount point 565 * start - first cluster of chain 566 * count - number of clusters in chain 567 * fillwith - what to write into FAT entry of last cluster 568 */ 569 static int 570 fatchain(struct msdosfsmount *pmp, u_long start, u_long count, u_long fillwith) 571 { 572 int error; 573 u_long bn, bo, bsize, byteoffset, readcn, newc; 574 struct buf *bp; 575 576 #ifdef MSDOSFS_DEBUG 577 printf("fatchain(pmp %p, start %lu, count %lu, fillwith %lx)\n", 578 pmp, start, count, fillwith); 579 #endif 580 /* 581 * Be sure the clusters are in the filesystem. 582 */ 583 if (start < CLUST_FIRST || start + count - 1 > pmp->pm_maxcluster) 584 return (EINVAL); 585 586 while (count > 0) { 587 byteoffset = FATOFS(pmp, start); 588 fatblock(pmp, byteoffset, &bn, &bsize, &bo); 589 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 590 if (error) { 591 return (error); 592 } 593 while (count > 0) { 594 start++; 595 newc = --count > 0 ? start : fillwith; 596 switch (pmp->pm_fatmask) { 597 case FAT12_MASK: 598 readcn = getushort(bp->b_data + bo); 599 if (start & 1) { 600 readcn &= 0xf000; 601 readcn |= newc & 0xfff; 602 } else { 603 readcn &= 0x000f; 604 readcn |= newc << 4; 605 } 606 putushort(bp->b_data + bo, readcn); 607 bo++; 608 if (!(start & 1)) 609 bo++; 610 break; 611 case FAT16_MASK: 612 putushort(bp->b_data + bo, newc); 613 bo += 2; 614 break; 615 case FAT32_MASK: 616 readcn = getulong(bp->b_data + bo); 617 readcn &= ~pmp->pm_fatmask; 618 readcn |= newc & pmp->pm_fatmask; 619 putulong(bp->b_data + bo, readcn); 620 bo += 4; 621 break; 622 } 623 if (bo >= bsize) 624 break; 625 } 626 updatefats(pmp, bp, bn); 627 } 628 pmp->pm_fmod = 1; 629 return (0); 630 } 631 632 /* 633 * Check the length of a free cluster chain starting at start. 634 * 635 * pmp - mount point 636 * start - start of chain 637 * count - maximum interesting length 638 */ 639 static int 640 chainlength(struct msdosfsmount *pmp, u_long start, u_long count) 641 { 642 u_long idx, max_idx; 643 u_int map; 644 u_long len; 645 646 MSDOSFS_ASSERT_MP_LOCKED(pmp); 647 648 if (start > pmp->pm_maxcluster) 649 return (0); 650 max_idx = pmp->pm_maxcluster / N_INUSEBITS; 651 idx = start / N_INUSEBITS; 652 start %= N_INUSEBITS; 653 map = pmp->pm_inusemap[idx]; 654 map &= ~((1U << start) - 1); 655 if (map) { 656 len = ffs(map) - 1 - start; 657 len = MIN(len, count); 658 if (start + len > pmp->pm_maxcluster) 659 len = pmp->pm_maxcluster - start + 1; 660 return (len); 661 } 662 len = N_INUSEBITS - start; 663 if (len >= count) { 664 len = count; 665 if (start + len > pmp->pm_maxcluster) 666 len = pmp->pm_maxcluster - start + 1; 667 return (len); 668 } 669 while (++idx <= max_idx) { 670 if (len >= count) 671 break; 672 map = pmp->pm_inusemap[idx]; 673 if (map) { 674 len += ffs(map) - 1; 675 break; 676 } 677 len += N_INUSEBITS; 678 } 679 len = MIN(len, count); 680 if (start + len > pmp->pm_maxcluster) 681 len = pmp->pm_maxcluster - start + 1; 682 return (len); 683 } 684 685 /* 686 * Allocate contigous free clusters. 687 * 688 * pmp - mount point. 689 * start - start of cluster chain. 690 * count - number of clusters to allocate. 691 * fillwith - put this value into the FAT entry for the 692 * last allocated cluster. 693 * retcluster - put the first allocated cluster's number here. 694 * got - how many clusters were actually allocated. 695 */ 696 static int 697 chainalloc(struct msdosfsmount *pmp, u_long start, u_long count, 698 u_long fillwith, u_long *retcluster, u_long *got) 699 { 700 int error; 701 u_long cl, n; 702 703 MSDOSFS_ASSERT_MP_LOCKED(pmp); 704 KASSERT((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0, 705 ("chainalloc on ro msdosfs mount")); 706 707 for (cl = start, n = count; n-- > 0;) 708 usemap_alloc(pmp, cl++); 709 pmp->pm_nxtfree = start + count; 710 if (pmp->pm_nxtfree > pmp->pm_maxcluster) 711 pmp->pm_nxtfree = CLUST_FIRST; 712 pmp->pm_flags |= MSDOSFS_FSIMOD; 713 error = fatchain(pmp, start, count, fillwith); 714 if (error != 0) { 715 for (cl = start, n = count; n-- > 0;) 716 (void)usemap_free(pmp, cl++); 717 return (error); 718 } 719 #ifdef MSDOSFS_DEBUG 720 printf("clusteralloc(): allocated cluster chain at %lu (%lu clusters)\n", 721 start, count); 722 #endif 723 if (retcluster) 724 *retcluster = start; 725 if (got) 726 *got = count; 727 return (0); 728 } 729 730 /* 731 * Allocate contiguous free clusters. 732 * 733 * pmp - mount point. 734 * start - preferred start of cluster chain. 735 * count - number of clusters requested. 736 * fillwith - put this value into the FAT entry for the 737 * last allocated cluster. 738 * retcluster - put the first allocated cluster's number here. 739 * got - how many clusters were actually allocated. 740 */ 741 int 742 clusteralloc(struct msdosfsmount *pmp, u_long start, u_long count, 743 u_long fillwith, u_long *retcluster, u_long *got) 744 { 745 int error; 746 747 MSDOSFS_LOCK_MP(pmp); 748 error = clusteralloc1(pmp, start, count, fillwith, retcluster, got); 749 MSDOSFS_UNLOCK_MP(pmp); 750 return (error); 751 } 752 753 static int 754 clusteralloc1(struct msdosfsmount *pmp, u_long start, u_long count, 755 u_long fillwith, u_long *retcluster, u_long *got) 756 { 757 u_long idx; 758 u_long len, newst, foundl, cn, l; 759 u_long foundcn = 0; /* XXX: foundcn could be used unititialized */ 760 u_int map; 761 762 MSDOSFS_ASSERT_MP_LOCKED(pmp); 763 764 #ifdef MSDOSFS_DEBUG 765 printf("clusteralloc(): find %lu clusters\n", count); 766 #endif 767 if (start) { 768 if ((len = chainlength(pmp, start, count)) >= count) 769 return (chainalloc(pmp, start, count, fillwith, retcluster, got)); 770 } else 771 len = 0; 772 773 newst = pmp->pm_nxtfree; 774 foundl = 0; 775 776 for (cn = newst; cn <= pmp->pm_maxcluster;) { 777 idx = cn / N_INUSEBITS; 778 map = pmp->pm_inusemap[idx]; 779 map |= (1U << (cn % N_INUSEBITS)) - 1; 780 if (map != FULL_RUN) { 781 cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1; 782 if ((l = chainlength(pmp, cn, count)) >= count) 783 return (chainalloc(pmp, cn, count, fillwith, retcluster, got)); 784 if (l > foundl) { 785 foundcn = cn; 786 foundl = l; 787 } 788 cn += l + 1; 789 continue; 790 } 791 cn += N_INUSEBITS - cn % N_INUSEBITS; 792 } 793 for (cn = 0; cn < newst;) { 794 idx = cn / N_INUSEBITS; 795 map = pmp->pm_inusemap[idx]; 796 map |= (1U << (cn % N_INUSEBITS)) - 1; 797 if (map != FULL_RUN) { 798 cn = idx * N_INUSEBITS + ffs(map ^ FULL_RUN) - 1; 799 if ((l = chainlength(pmp, cn, count)) >= count) 800 return (chainalloc(pmp, cn, count, fillwith, retcluster, got)); 801 if (l > foundl) { 802 foundcn = cn; 803 foundl = l; 804 } 805 cn += l + 1; 806 continue; 807 } 808 cn += N_INUSEBITS - cn % N_INUSEBITS; 809 } 810 811 if (!foundl) 812 return (ENOSPC); 813 814 if (len) 815 return (chainalloc(pmp, start, len, fillwith, retcluster, got)); 816 else 817 return (chainalloc(pmp, foundcn, foundl, fillwith, retcluster, got)); 818 } 819 820 /* 821 * Free a chain of clusters. 822 * 823 * pmp - address of the msdosfs mount structure for the filesystem 824 * containing the cluster chain to be freed. 825 * startcluster - number of the 1st cluster in the chain of clusters to be 826 * freed. 827 */ 828 int 829 freeclusterchain(struct msdosfsmount *pmp, u_long cluster) 830 { 831 int error; 832 struct buf *bp = NULL; 833 u_long bn, bo, bsize, byteoffset; 834 u_long readcn, lbn = -1; 835 836 MSDOSFS_LOCK_MP(pmp); 837 while (cluster >= CLUST_FIRST && cluster <= pmp->pm_maxcluster) { 838 byteoffset = FATOFS(pmp, cluster); 839 fatblock(pmp, byteoffset, &bn, &bsize, &bo); 840 if (lbn != bn) { 841 if (bp) 842 updatefats(pmp, bp, lbn); 843 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 844 if (error) { 845 MSDOSFS_UNLOCK_MP(pmp); 846 return (error); 847 } 848 lbn = bn; 849 } 850 error = usemap_free(pmp, cluster); 851 if (error != 0) { 852 updatefats(pmp, bp, lbn); 853 MSDOSFS_UNLOCK_MP(pmp); 854 return (error); 855 } 856 switch (pmp->pm_fatmask) { 857 case FAT12_MASK: 858 readcn = getushort(bp->b_data + bo); 859 if (cluster & 1) { 860 cluster = readcn >> 4; 861 readcn &= 0x000f; 862 readcn |= MSDOSFSFREE << 4; 863 } else { 864 cluster = readcn; 865 readcn &= 0xf000; 866 readcn |= MSDOSFSFREE & 0xfff; 867 } 868 putushort(bp->b_data + bo, readcn); 869 break; 870 case FAT16_MASK: 871 cluster = getushort(bp->b_data + bo); 872 putushort(bp->b_data + bo, MSDOSFSFREE); 873 break; 874 case FAT32_MASK: 875 cluster = getulong(bp->b_data + bo); 876 putulong(bp->b_data + bo, 877 (MSDOSFSFREE & FAT32_MASK) | (cluster & ~FAT32_MASK)); 878 break; 879 } 880 cluster &= pmp->pm_fatmask; 881 if ((cluster | ~pmp->pm_fatmask) >= CLUST_RSRVD) 882 cluster |= pmp->pm_fatmask; 883 } 884 if (bp) 885 updatefats(pmp, bp, bn); 886 MSDOSFS_UNLOCK_MP(pmp); 887 return (0); 888 } 889 890 /* 891 * Read in FAT blocks looking for free clusters. For every free cluster 892 * found turn off its corresponding bit in the pm_inusemap. 893 */ 894 int 895 fillinusemap(struct msdosfsmount *pmp) 896 { 897 struct buf *bp; 898 u_long bn, bo, bsize, byteoffset, cn, readcn; 899 int error; 900 901 MSDOSFS_ASSERT_MP_LOCKED(pmp); 902 bp = NULL; 903 904 /* 905 * Mark all clusters in use, we mark the free ones in the FAT scan 906 * loop further down. 907 */ 908 for (cn = 0; cn < (pmp->pm_maxcluster + N_INUSEBITS) / N_INUSEBITS; cn++) 909 pmp->pm_inusemap[cn] = FULL_RUN; 910 911 /* 912 * Figure how many free clusters are in the filesystem by ripping 913 * through the FAT counting the number of entries whose content is 914 * zero. These represent free clusters. 915 */ 916 pmp->pm_freeclustercount = 0; 917 for (cn = 0; cn <= pmp->pm_maxcluster; cn++) { 918 byteoffset = FATOFS(pmp, cn); 919 bo = byteoffset % pmp->pm_fatblocksize; 920 if (bo == 0) { 921 /* Read new FAT block */ 922 if (bp != NULL) 923 brelse(bp); 924 fatblock(pmp, byteoffset, &bn, &bsize, NULL); 925 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 926 if (error != 0) 927 return (error); 928 } 929 if (FAT32(pmp)) 930 readcn = getulong(bp->b_data + bo); 931 else 932 readcn = getushort(bp->b_data + bo); 933 if (FAT12(pmp) && (cn & 1)) 934 readcn >>= 4; 935 readcn &= pmp->pm_fatmask; 936 937 /* 938 * Check if the FAT ID matches the BPB's media descriptor and 939 * all other bits are set to 1. 940 */ 941 if (cn == 0 && readcn != ((pmp->pm_fatmask & 0xffffff00) | 942 pmp->pm_bpb.bpbMedia)) { 943 #ifdef MSDOSFS_DEBUG 944 printf("mountmsdosfs(): Media descriptor in BPB" 945 "does not match FAT ID\n"); 946 #endif 947 brelse(bp); 948 return (EINVAL); 949 } else if (readcn == CLUST_FREE) { 950 error = usemap_free(pmp, cn); 951 if (error != 0) { 952 brelse(bp); 953 return (error); 954 } 955 } 956 } 957 if (bp != NULL) 958 brelse(bp); 959 960 for (cn = pmp->pm_maxcluster + 1; cn < (pmp->pm_maxcluster + 961 N_INUSEBITS) / N_INUSEBITS; cn++) 962 pmp->pm_inusemap[cn / N_INUSEBITS] |= 1U << (cn % N_INUSEBITS); 963 964 return (0); 965 } 966 967 /* 968 * Allocate a new cluster and chain it onto the end of the file. 969 * 970 * dep - the file to extend 971 * count - number of clusters to allocate 972 * bpp - where to return the address of the buf header for the first new 973 * file block 974 * ncp - where to put cluster number of the first newly allocated cluster 975 * If this pointer is 0, do not return the cluster number. 976 * flags - see fat.h 977 * 978 * NOTE: This function is not responsible for turning on the DE_UPDATE bit of 979 * the de_flag field of the denode and it does not change the de_FileSize 980 * field. This is left for the caller to do. 981 */ 982 int 983 extendfile(struct denode *dep, u_long count, struct buf **bpp, u_long *ncp, 984 int flags) 985 { 986 int error; 987 u_long frcn; 988 u_long cn, got; 989 struct msdosfsmount *pmp = dep->de_pmp; 990 struct buf *bp; 991 daddr_t blkno; 992 993 /* 994 * Don't try to extend the root directory 995 */ 996 if (dep->de_StartCluster == MSDOSFSROOT 997 && (dep->de_Attributes & ATTR_DIRECTORY)) { 998 #ifdef MSDOSFS_DEBUG 999 printf("extendfile(): attempt to extend root directory\n"); 1000 #endif 1001 return (ENOSPC); 1002 } 1003 1004 /* 1005 * If the "file's last cluster" cache entry is empty, and the file 1006 * is not empty, then fill the cache entry by calling pcbmap(). 1007 */ 1008 if (dep->de_fc[FC_LASTFC].fc_frcn == FCE_EMPTY && 1009 dep->de_StartCluster != 0) { 1010 error = pcbmap(dep, 0xffff, 0, &cn, 0); 1011 /* we expect it to return E2BIG */ 1012 if (error != E2BIG) 1013 return (error); 1014 } 1015 1016 dep->de_fc[FC_NEXTTOLASTFC].fc_frcn = 1017 dep->de_fc[FC_LASTFC].fc_frcn; 1018 dep->de_fc[FC_NEXTTOLASTFC].fc_fsrcn = 1019 dep->de_fc[FC_LASTFC].fc_fsrcn; 1020 while (count > 0) { 1021 /* 1022 * Allocate a new cluster chain and cat onto the end of the 1023 * file. If the file is empty we make de_StartCluster point 1024 * to the new block. Note that de_StartCluster being 0 is 1025 * sufficient to be sure the file is empty since we exclude 1026 * attempts to extend the root directory above, and the root 1027 * dir is the only file with a startcluster of 0 that has 1028 * blocks allocated (sort of). 1029 */ 1030 if (dep->de_StartCluster == 0) 1031 cn = 0; 1032 else 1033 cn = dep->de_fc[FC_LASTFC].fc_fsrcn + 1; 1034 error = clusteralloc(pmp, cn, count, CLUST_EOFE, &cn, &got); 1035 if (error) 1036 return (error); 1037 1038 count -= got; 1039 1040 /* 1041 * Give them the filesystem relative cluster number if they want 1042 * it. 1043 */ 1044 if (ncp) { 1045 *ncp = cn; 1046 ncp = NULL; 1047 } 1048 1049 if (dep->de_StartCluster == 0) { 1050 dep->de_StartCluster = cn; 1051 frcn = 0; 1052 } else { 1053 error = fatentry(FAT_SET, pmp, 1054 dep->de_fc[FC_LASTFC].fc_fsrcn, 1055 0, cn); 1056 if (error) { 1057 clusterfree(pmp, cn); 1058 return (error); 1059 } 1060 frcn = dep->de_fc[FC_LASTFC].fc_frcn + 1; 1061 } 1062 1063 /* 1064 * Update the "last cluster of the file" entry in the 1065 * denode's FAT cache. 1066 */ 1067 fc_setcache(dep, FC_LASTFC, frcn + got - 1, cn + got - 1); 1068 1069 if (flags & DE_CLEAR) { 1070 while (got-- > 0) { 1071 /* 1072 * Get the buf header for the new block of the file. 1073 */ 1074 if (dep->de_Attributes & ATTR_DIRECTORY) 1075 bp = getblk(pmp->pm_devvp, 1076 cntobn(pmp, cn++), 1077 pmp->pm_bpcluster, 0, 0, 0); 1078 else { 1079 bp = getblk(DETOV(dep), 1080 frcn++, 1081 pmp->pm_bpcluster, 0, 0, 0); 1082 /* 1083 * Do the bmap now, as in msdosfs_write 1084 */ 1085 if (pcbmap(dep, 1086 bp->b_lblkno, 1087 &blkno, 0, 0)) 1088 bp->b_blkno = -1; 1089 if (bp->b_blkno == -1) 1090 panic("extendfile: pcbmap"); 1091 else 1092 bp->b_blkno = blkno; 1093 } 1094 clrbuf(bp); 1095 if (bpp) { 1096 *bpp = bp; 1097 bpp = NULL; 1098 } else { 1099 bdwrite(bp); 1100 } 1101 if (vm_page_count_severe() || 1102 buf_dirty_count_severe()) 1103 vn_fsync_buf(DETOV(dep), MNT_WAIT); 1104 } 1105 } 1106 } 1107 1108 return (0); 1109 } 1110 1111 /*- 1112 * Routine to mark a FAT16 or FAT32 volume as "clean" or "dirty" by 1113 * manipulating the upper bit of the FAT entry for cluster 1. Note that 1114 * this bit is not defined for FAT12 volumes, which are always assumed to 1115 * be clean. 1116 * 1117 * The fatentry() routine only works on cluster numbers that a file could 1118 * occupy, so it won't manipulate the entry for cluster 1. So we have to do 1119 * it here. The code was stolen from fatentry() and tailored for cluster 1. 1120 * 1121 * Inputs: 1122 * pmp The MS-DOS volume to mark 1123 * dirty Non-zero if the volume should be marked dirty; zero if it 1124 * should be marked clean 1125 * 1126 * Result: 1127 * 0 Success 1128 * EROFS Volume is read-only 1129 * ? (other errors from called routines) 1130 */ 1131 int 1132 markvoldirty_upgrade(struct msdosfsmount *pmp, bool dirty, bool rw_upgrade) 1133 { 1134 struct buf *bp; 1135 u_long bn, bo, bsize, byteoffset, fatval; 1136 int error; 1137 1138 /* 1139 * FAT12 does not support a "clean" bit, so don't do anything for 1140 * FAT12. 1141 */ 1142 if (FAT12(pmp)) 1143 return (0); 1144 1145 /* 1146 * Can't change the bit on a read-only filesystem, except as part of 1147 * ro->rw upgrade. 1148 */ 1149 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) != 0 && !rw_upgrade) 1150 return (EROFS); 1151 1152 /* 1153 * Fetch the block containing the FAT entry. It is given by the 1154 * pseudo-cluster 1. 1155 */ 1156 byteoffset = FATOFS(pmp, 1); 1157 fatblock(pmp, byteoffset, &bn, &bsize, &bo); 1158 error = bread(pmp->pm_devvp, bn, bsize, NOCRED, &bp); 1159 if (error) 1160 return (error); 1161 1162 /* 1163 * Get the current value of the FAT entry and set/clear the relevant 1164 * bit. Dirty means clear the "clean" bit; clean means set the 1165 * "clean" bit. 1166 */ 1167 if (FAT32(pmp)) { 1168 /* FAT32 uses bit 27. */ 1169 fatval = getulong(&bp->b_data[bo]); 1170 if (dirty) 1171 fatval &= 0xF7FFFFFF; 1172 else 1173 fatval |= 0x08000000; 1174 putulong(&bp->b_data[bo], fatval); 1175 } else { 1176 /* Must be FAT16; use bit 15. */ 1177 fatval = getushort(&bp->b_data[bo]); 1178 if (dirty) 1179 fatval &= 0x7FFF; 1180 else 1181 fatval |= 0x8000; 1182 putushort(&bp->b_data[bo], fatval); 1183 } 1184 1185 /* 1186 * The concern here is that a devvp may be readonly, without reporting 1187 * itself as such through the usual channels. In that case, we'd like 1188 * it if attempting to mount msdosfs rw didn't panic the system. 1189 * 1190 * markvoldirty is invoked as the first write on backing devvps when 1191 * either msdosfs is mounted for the first time, or a ro mount is 1192 * upgraded to rw. 1193 * 1194 * In either event, if a write error occurs dirtying the volume: 1195 * - No user data has been permitted to be written to cache yet. 1196 * - We can abort the high-level operation (mount, or ro->rw) safely. 1197 * - We don't derive any benefit from leaving a zombie dirty buf in 1198 * the cache that can not be cleaned or evicted. 1199 * 1200 * So, mark B_INVALONERR to have bwrite() -> brelse() detect that 1201 * condition and force-invalidate our write to the block if it occurs. 1202 * 1203 * PR 210316 provides more context on the discovery and diagnosis of 1204 * the problem, as well as earlier attempts to solve it. 1205 */ 1206 bp->b_flags |= B_INVALONERR; 1207 1208 /* Write out the modified FAT block synchronously. */ 1209 return (bwrite(bp)); 1210 } 1211