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