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