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