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