1 /* $NetBSD: msdosfs_vnops.c,v 1.19 2017/04/13 17:10:12 christos Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 8 * All rights reserved. 9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by TooLs GmbH. 22 * 4. The name of TooLs GmbH may not be used to endorse or promote products 23 * derived from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 /*- 37 * Written by Paul Popelka (paulp@uts.amdahl.com) 38 * 39 * You can do anything you want with this software, just don't say you wrote 40 * it, and don't remove this notice. 41 * 42 * This software is provided "as is". 43 * 44 * The author supplies this software to be publicly redistributed on the 45 * understanding that the author is not responsible for the correct 46 * functioning of this software in any circumstances and is not liable for 47 * any damages caused by this software. 48 * 49 * October 1992 50 */ 51 52 #include <sys/cdefs.h> 53 __FBSDID("$FreeBSD$"); 54 55 #include <sys/param.h> 56 #include <sys/errno.h> 57 #include <sys/mman.h> 58 #include <sys/time.h> 59 60 #include <fcntl.h> 61 #include <stdbool.h> 62 #include <stdio.h> 63 #include <string.h> 64 #include <time.h> 65 #include <unistd.h> 66 67 #include "ffs/buf.h" 68 #include <fs/msdosfs/bpb.h> 69 #include "msdos/direntry.h" 70 #include <fs/msdosfs/denode.h> 71 #include <fs/msdosfs/fat.h> 72 #include <fs/msdosfs/msdosfsmount.h> 73 74 #include "makefs.h" 75 #include "msdos.h" 76 77 /* 78 * Some general notes: 79 * 80 * In the ufs filesystem the inodes, superblocks, and indirect blocks are 81 * read/written using the vnode for the filesystem. Blocks that represent 82 * the contents of a file are read/written using the vnode for the file 83 * (including directories when they are read/written as files). This 84 * presents problems for the dos filesystem because data that should be in 85 * an inode (if dos had them) resides in the directory itself. Since we 86 * must update directory entries without the benefit of having the vnode 87 * for the directory we must use the vnode for the filesystem. This means 88 * that when a directory is actually read/written (via read, write, or 89 * readdir, or seek) we must use the vnode for the filesystem instead of 90 * the vnode for the directory as would happen in ufs. This is to insure we 91 * retrieve the correct block from the buffer cache since the hash value is 92 * based upon the vnode address and the desired block number. 93 */ 94 95 static int msdosfs_wfile(const char *, struct denode *, fsnode *); 96 static void unix2fattime(const struct timespec *tsp, uint16_t *ddp, 97 uint16_t *dtp); 98 99 static void 100 msdosfs_times(struct denode *dep, const struct stat *st) 101 { 102 if (stampst.st_ino) 103 st = &stampst; 104 105 #ifdef HAVE_STRUCT_STAT_BIRTHTIME 106 unix2fattime(&st->st_birthtim, &dep->de_CDate, &dep->de_CTime); 107 #else 108 unix2fattime(&st->st_ctim, &dep->de_CDate, &dep->de_CTime); 109 #endif 110 unix2fattime(&st->st_atim, &dep->de_ADate, NULL); 111 unix2fattime(&st->st_mtim, &dep->de_MDate, &dep->de_MTime); 112 } 113 114 static void 115 unix2fattime(const struct timespec *tsp, uint16_t *ddp, uint16_t *dtp) 116 { 117 time_t t1; 118 struct tm lt = {0}; 119 120 t1 = tsp->tv_sec; 121 localtime_r(&t1, <); 122 123 unsigned long fat_time = ((lt.tm_year - 80) << 25) | 124 ((lt.tm_mon + 1) << 21) | 125 (lt.tm_mday << 16) | 126 (lt.tm_hour << 11) | 127 (lt.tm_min << 5) | 128 (lt.tm_sec >> 1); 129 130 if (ddp != NULL) 131 *ddp = (uint16_t)(fat_time >> 16); 132 if (dtp != NULL) 133 *dtp = (uint16_t)fat_time; 134 } 135 136 /* 137 * When we search a directory the blocks containing directory entries are 138 * read and examined. The directory entries contain information that would 139 * normally be in the inode of a unix filesystem. This means that some of 140 * a directory's contents may also be in memory resident denodes (sort of 141 * an inode). This can cause problems if we are searching while some other 142 * process is modifying a directory. To prevent one process from accessing 143 * incompletely modified directory information we depend upon being the 144 * sole owner of a directory block. bread/brelse provide this service. 145 * This being the case, when a process modifies a directory it must first 146 * acquire the disk block that contains the directory entry to be modified. 147 * Then update the disk block and the denode, and then write the disk block 148 * out to disk. This way disk blocks containing directory entries and in 149 * memory denode's will be in synch. 150 */ 151 static int 152 msdosfs_findslot(struct denode *dp, struct componentname *cnp) 153 { 154 daddr_t bn; 155 int error; 156 int slotcount; 157 int slotoffset = 0; 158 int frcn; 159 u_long cluster; 160 int blkoff; 161 u_int diroff; 162 int blsize; 163 struct msdosfsmount *pmp; 164 struct buf *bp = 0; 165 struct direntry *dep; 166 u_char dosfilename[12]; 167 int wincnt = 1; 168 int chksum = -1, chksum_ok; 169 int olddos = 1; 170 171 pmp = dp->de_pmp; 172 173 switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename, 174 cnp->cn_namelen, 0)) { 175 case 0: 176 return (EINVAL); 177 case 1: 178 break; 179 case 2: 180 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr, 181 cnp->cn_namelen) + 1; 182 break; 183 case 3: 184 olddos = 0; 185 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr, 186 cnp->cn_namelen) + 1; 187 break; 188 } 189 190 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 191 wincnt = 1; 192 193 /* 194 * Suppress search for slots unless creating 195 * file and at end of pathname, in which case 196 * we watch for a place to put the new file in 197 * case it doesn't already exist. 198 */ 199 slotcount = 0; 200 MSDOSFS_DPRINTF(("%s(): dos filename: %s\n", __func__, dosfilename)); 201 /* 202 * Search the directory pointed at by vdp for the name pointed at 203 * by cnp->cn_nameptr. 204 */ 205 /* 206 * The outer loop ranges over the clusters that make up the 207 * directory. Note that the root directory is different from all 208 * other directories. It has a fixed number of blocks that are not 209 * part of the pool of allocatable clusters. So, we treat it a 210 * little differently. The root directory starts at "cluster" 0. 211 */ 212 diroff = 0; 213 for (frcn = 0; diroff < dp->de_FileSize; frcn++) { 214 if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) { 215 if (error == E2BIG) 216 break; 217 return (error); 218 } 219 error = bread(pmp->pm_devvp, bn, blsize, 0, &bp); 220 if (error) { 221 return (error); 222 } 223 for (blkoff = 0; blkoff < blsize; 224 blkoff += sizeof(struct direntry), 225 diroff += sizeof(struct direntry)) { 226 dep = (struct direntry *)(bp->b_data + blkoff); 227 /* 228 * If the slot is empty and we are still looking 229 * for an empty then remember this one. If the 230 * slot is not empty then check to see if it 231 * matches what we are looking for. If the slot 232 * has never been filled with anything, then the 233 * remainder of the directory has never been used, 234 * so there is no point in searching it. 235 */ 236 if (dep->deName[0] == SLOT_EMPTY || 237 dep->deName[0] == SLOT_DELETED) { 238 /* 239 * Drop memory of previous long matches 240 */ 241 chksum = -1; 242 243 if (slotcount < wincnt) { 244 slotcount++; 245 slotoffset = diroff; 246 } 247 if (dep->deName[0] == SLOT_EMPTY) { 248 brelse(bp); 249 goto notfound; 250 } 251 } else { 252 /* 253 * If there wasn't enough space for our 254 * winentries, forget about the empty space 255 */ 256 if (slotcount < wincnt) 257 slotcount = 0; 258 259 /* 260 * Check for Win95 long filename entry 261 */ 262 if (dep->deAttributes == ATTR_WIN95) { 263 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 264 continue; 265 266 chksum = winChkName( 267 (const u_char *)cnp->cn_nameptr, 268 cnp->cn_namelen, 269 (struct winentry *)dep, chksum); 270 continue; 271 } 272 273 /* 274 * Ignore volume labels (anywhere, not just 275 * the root directory). 276 */ 277 if (dep->deAttributes & ATTR_VOLUME) { 278 chksum = -1; 279 continue; 280 } 281 282 /* 283 * Check for a checksum or name match 284 */ 285 chksum_ok = (chksum == winChksum(dep->deName)); 286 if (!chksum_ok 287 && (!olddos || memcmp(dosfilename, dep->deName, 11))) { 288 chksum = -1; 289 continue; 290 } 291 MSDOSFS_DPRINTF(("%s(): match blkoff %d, diroff %u\n", 292 __func__, blkoff, diroff)); 293 /* 294 * Remember where this directory 295 * entry came from for whoever did 296 * this lookup. 297 */ 298 dp->de_fndoffset = diroff; 299 dp->de_fndcnt = 0; 300 301 return EEXIST; 302 } 303 } /* for (blkoff = 0; .... */ 304 /* 305 * Release the buffer holding the directory cluster just 306 * searched. 307 */ 308 brelse(bp); 309 } /* for (frcn = 0; ; frcn++) */ 310 311 notfound: 312 /* 313 * We hold no disk buffers at this point. 314 */ 315 316 /* 317 * If we get here we didn't find the entry we were looking for. But 318 * that's ok if we are creating or renaming and are at the end of 319 * the pathname and the directory hasn't been removed. 320 */ 321 MSDOSFS_DPRINTF(("%s(): refcnt %ld, slotcount %d, slotoffset %d\n", 322 __func__, dp->de_refcnt, slotcount, slotoffset)); 323 /* 324 * Fixup the slot description to point to the place where 325 * we might put the new DOS direntry (putting the Win95 326 * long name entries before that) 327 */ 328 if (!slotcount) { 329 slotcount = 1; 330 slotoffset = diroff; 331 } 332 if (wincnt > slotcount) { 333 slotoffset += sizeof(struct direntry) * (wincnt - slotcount); 334 } 335 336 /* 337 * Return an indication of where the new directory 338 * entry should be put. 339 */ 340 dp->de_fndoffset = slotoffset; 341 dp->de_fndcnt = wincnt - 1; 342 343 /* 344 * We return with the directory locked, so that 345 * the parameters we set up above will still be 346 * valid if we actually decide to do a direnter(). 347 * We return ni_vp == NULL to indicate that the entry 348 * does not currently exist; we leave a pointer to 349 * the (locked) directory inode in ndp->ni_dvp. 350 * 351 * NB - if the directory is unlocked, then this 352 * information cannot be used. 353 */ 354 return 0; 355 } 356 357 /* 358 * Create a regular file. On entry the directory to contain the file being 359 * created is locked. We must release before we return. 360 */ 361 struct denode * 362 msdosfs_mkfile(const char *path, struct denode *pdep, fsnode *node) 363 { 364 struct componentname cn; 365 struct denode ndirent; 366 struct denode *dep; 367 int error; 368 struct stat *st = &node->inode->st; 369 370 cn.cn_nameptr = node->name; 371 cn.cn_namelen = strlen(node->name); 372 373 MSDOSFS_DPRINTF(("%s(name %s, mode 0%o size %zu)\n", 374 __func__, node->name, st->st_mode, (size_t)st->st_size)); 375 376 /* 377 * If this is the root directory and there is no space left we 378 * can't do anything. This is because the root directory can not 379 * change size. 380 */ 381 if (pdep->de_StartCluster == MSDOSFSROOT 382 && pdep->de_fndoffset >= pdep->de_FileSize) { 383 error = ENOSPC; 384 goto bad; 385 } 386 387 /* 388 * Create a directory entry for the file, then call createde() to 389 * have it installed. NOTE: DOS files are always executable. We 390 * use the absence of the owner write bit to make the file 391 * readonly. 392 */ 393 memset(&ndirent, 0, sizeof(ndirent)); 394 if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0) 395 goto bad; 396 397 ndirent.de_Attributes = (st->st_mode & S_IWUSR) ? 398 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY; 399 ndirent.de_StartCluster = 0; 400 ndirent.de_FileSize = 0; 401 ndirent.de_pmp = pdep->de_pmp; 402 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE; 403 msdosfs_times(&ndirent, &node->inode->st); 404 405 if ((error = msdosfs_findslot(pdep, &cn)) != 0) 406 goto bad; 407 if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0) 408 goto bad; 409 if ((error = msdosfs_wfile(path, dep, node)) != 0) 410 goto bad; 411 return dep; 412 413 bad: 414 errno = error; 415 return NULL; 416 } 417 static int 418 msdosfs_updatede(struct denode *dep) 419 { 420 struct buf *bp; 421 struct direntry *dirp; 422 int error; 423 424 dep->de_flag &= ~DE_MODIFIED; 425 error = readde(dep, &bp, &dirp); 426 if (error) 427 return error; 428 DE_EXTERNALIZE(dirp, dep); 429 error = bwrite(bp); 430 return error; 431 } 432 433 /* 434 * Write data to a file or directory. 435 */ 436 static int 437 msdosfs_wfile(const char *path, struct denode *dep, fsnode *node) 438 { 439 int error, fd; 440 size_t osize = dep->de_FileSize; 441 struct stat *st = &node->inode->st; 442 size_t nsize, offs; 443 struct msdosfsmount *pmp = dep->de_pmp; 444 struct buf *bp; 445 char *dat; 446 u_long cn = 0; 447 448 error = 0; /* XXX: gcc/vax */ 449 MSDOSFS_DPRINTF(("%s(diroff %lu, dirclust %lu, startcluster %lu)\n", 450 __func__, dep->de_diroffset, dep->de_dirclust, 451 dep->de_StartCluster)); 452 if (st->st_size == 0) 453 return 0; 454 455 /* Don't bother to try to write files larger than the fs limit */ 456 if (st->st_size > MSDOSFS_FILESIZE_MAX) 457 return EFBIG; 458 459 nsize = st->st_size; 460 MSDOSFS_DPRINTF(("%s(nsize=%zu, osize=%zu)\n", __func__, nsize, osize)); 461 if (nsize > osize) { 462 if ((error = deextend(dep, nsize, NULL)) != 0) 463 return error; 464 if ((error = msdosfs_updatede(dep)) != 0) 465 return error; 466 } 467 468 if ((fd = open(path, O_RDONLY)) == -1) { 469 error = errno; 470 fprintf(stderr, "open %s: %s\n", path, strerror(error)); 471 return error; 472 } 473 474 if ((dat = mmap(0, nsize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0)) 475 == MAP_FAILED) { 476 error = errno; 477 fprintf(stderr, "%s: mmap %s: %s\n", __func__, node->name, 478 strerror(error)); 479 close(fd); 480 goto out; 481 } 482 close(fd); 483 484 for (offs = 0; offs < nsize;) { 485 int blsize, cpsize; 486 daddr_t bn; 487 u_long on = offs & pmp->pm_crbomask; 488 489 if ((error = pcbmap(dep, cn++, &bn, NULL, &blsize)) != 0) { 490 MSDOSFS_DPRINTF(("%s: pcbmap %lu", 491 __func__, (unsigned long)bn)); 492 goto out; 493 } 494 495 MSDOSFS_DPRINTF(("%s(cn=%lu, bn=%llu, blsize=%d)\n", 496 __func__, cn, (unsigned long long)bn, blsize)); 497 if ((error = bread(pmp->pm_devvp, bn, blsize, 0, &bp)) != 0) { 498 MSDOSFS_DPRINTF(("bread %d\n", error)); 499 goto out; 500 } 501 cpsize = MIN((nsize - offs), blsize - on); 502 memcpy(bp->b_data + on, dat + offs, cpsize); 503 bwrite(bp); 504 offs += cpsize; 505 } 506 507 munmap(dat, nsize); 508 return 0; 509 out: 510 munmap(dat, nsize); 511 return error; 512 } 513 514 static const struct { 515 struct direntry dot; 516 struct direntry dotdot; 517 } dosdirtemplate = { 518 { ". ", /* the . entry */ 519 ATTR_DIRECTORY, /* file attribute */ 520 0, /* reserved */ 521 0, { 0, 0 }, { 0, 0 }, /* create time & date */ 522 { 0, 0 }, /* access date */ 523 { 0, 0 }, /* high bits of start cluster */ 524 { 210, 4 }, { 210, 4 }, /* modify time & date */ 525 { 0, 0 }, /* startcluster */ 526 { 0, 0, 0, 0 } /* filesize */ 527 }, 528 { ".. ", /* the .. entry */ 529 ATTR_DIRECTORY, /* file attribute */ 530 0, /* reserved */ 531 0, { 0, 0 }, { 0, 0 }, /* create time & date */ 532 { 0, 0 }, /* access date */ 533 { 0, 0 }, /* high bits of start cluster */ 534 { 210, 4 }, { 210, 4 }, /* modify time & date */ 535 { 0, 0 }, /* startcluster */ 536 { 0, 0, 0, 0 } /* filesize */ 537 } 538 }; 539 540 struct denode * 541 msdosfs_mkdire(const char *path, struct denode *pdep, fsnode *node) { 542 struct denode ndirent; 543 struct denode *dep; 544 struct componentname cn; 545 struct msdosfsmount *pmp = pdep->de_pmp; 546 int error; 547 u_long newcluster, pcl, bn; 548 struct direntry *denp; 549 struct buf *bp; 550 551 cn.cn_nameptr = node->name; 552 cn.cn_namelen = strlen(node->name); 553 /* 554 * If this is the root directory and there is no space left we 555 * can't do anything. This is because the root directory can not 556 * change size. 557 */ 558 if (pdep->de_StartCluster == MSDOSFSROOT 559 && pdep->de_fndoffset >= pdep->de_FileSize) { 560 error = ENOSPC; 561 goto bad2; 562 } 563 564 /* 565 * Allocate a cluster to hold the about to be created directory. 566 */ 567 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL); 568 if (error) 569 goto bad2; 570 571 memset(&ndirent, 0, sizeof(ndirent)); 572 ndirent.de_pmp = pmp; 573 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE; 574 msdosfs_times(&ndirent, &node->inode->st); 575 576 /* 577 * Now fill the cluster with the "." and ".." entries. And write 578 * the cluster to disk. This way it is there for the parent 579 * directory to be pointing at if there were a crash. 580 */ 581 bn = cntobn(pmp, newcluster); 582 MSDOSFS_DPRINTF(("%s(newcluster %lu, bn=%lu)\n", 583 __func__, newcluster, bn)); 584 /* always succeeds */ 585 bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0); 586 memset(bp->b_data, 0, pmp->pm_bpcluster); 587 memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate); 588 denp = (struct direntry *)bp->b_data; 589 putushort(denp[0].deStartCluster, newcluster); 590 putushort(denp[0].deCDate, ndirent.de_CDate); 591 putushort(denp[0].deCTime, ndirent.de_CTime); 592 denp[0].deCHundredth = ndirent.de_CHun; 593 putushort(denp[0].deADate, ndirent.de_ADate); 594 putushort(denp[0].deMDate, ndirent.de_MDate); 595 putushort(denp[0].deMTime, ndirent.de_MTime); 596 pcl = pdep->de_StartCluster; 597 MSDOSFS_DPRINTF(("%s(pcl %lu, rootdirblk=%lu)\n", __func__, pcl, 598 pmp->pm_rootdirblk)); 599 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk) 600 pcl = 0; 601 putushort(denp[1].deStartCluster, pcl); 602 putushort(denp[1].deCDate, ndirent.de_CDate); 603 putushort(denp[1].deCTime, ndirent.de_CTime); 604 denp[1].deCHundredth = ndirent.de_CHun; 605 putushort(denp[1].deADate, ndirent.de_ADate); 606 putushort(denp[1].deMDate, ndirent.de_MDate); 607 putushort(denp[1].deMTime, ndirent.de_MTime); 608 if (FAT32(pmp)) { 609 putushort(denp[0].deHighClust, newcluster >> 16); 610 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16); 611 } else { 612 putushort(denp[0].deHighClust, 0); 613 putushort(denp[1].deHighClust, 0); 614 } 615 616 if ((error = bwrite(bp)) != 0) 617 goto bad; 618 619 /* 620 * Now build up a directory entry pointing to the newly allocated 621 * cluster. This will be written to an empty slot in the parent 622 * directory. 623 */ 624 if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0) 625 goto bad; 626 627 ndirent.de_Attributes = ATTR_DIRECTORY; 628 ndirent.de_StartCluster = newcluster; 629 ndirent.de_FileSize = 0; 630 ndirent.de_pmp = pdep->de_pmp; 631 if ((error = msdosfs_findslot(pdep, &cn)) != 0) 632 goto bad; 633 if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0) 634 goto bad; 635 if ((error = msdosfs_updatede(dep)) != 0) 636 goto bad; 637 return dep; 638 639 bad: 640 clusterfree(pmp, newcluster, NULL); 641 bad2: 642 errno = error; 643 return NULL; 644 } 645