1 /* $FreeBSD$ */ 2 /* $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $ */ 3 4 /*- 5 * SPDX-License-Identifier: BSD-4-Clause 6 * 7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 9 * All rights reserved. 10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by TooLs GmbH. 23 * 4. The name of TooLs GmbH may not be used to endorse or promote products 24 * derived from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 /*- 38 * Written by Paul Popelka (paulp@uts.amdahl.com) 39 * 40 * You can do anything you want with this software, just don't say you wrote 41 * it, and don't remove this notice. 42 * 43 * This software is provided "as is". 44 * 45 * The author supplies this software to be publicly redistributed on the 46 * understanding that the author is not responsible for the correct 47 * functioning of this software in any circumstances and is not liable for 48 * any damages caused by this software. 49 * 50 * October 1992 51 */ 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */ 56 #include <sys/kernel.h> 57 #include <sys/stat.h> 58 #include <sys/buf.h> 59 #include <sys/proc.h> 60 #include <sys/priv.h> 61 #include <sys/namei.h> 62 #include <sys/mount.h> 63 #include <sys/unistd.h> 64 #include <sys/vnode.h> 65 #include <sys/malloc.h> 66 #include <sys/dirent.h> 67 #include <sys/signalvar.h> 68 69 #include <vm/vm.h> 70 #include <vm/vm_extern.h> 71 #include <vm/vm_zone.h> 72 #include <vm/vnode_pager.h> 73 74 #include <sys/buf2.h> 75 76 #include <machine/inttypes.h> 77 78 #include "bpb.h" 79 #include "direntry.h" 80 #include "denode.h" 81 #include "msdosfsmount.h" 82 #include "fat.h" 83 84 #define DOS_FILESIZE_MAX 0xffffffff 85 86 /* 87 * Some general notes: 88 * 89 * In the ufs filesystem the inodes, superblocks, and indirect blocks are 90 * read/written using the vnode for the filesystem. Blocks that represent 91 * the contents of a file are read/written using the vnode for the file 92 * (including directories when they are read/written as files). This 93 * presents problems for the dos filesystem because data that should be in 94 * an inode (if dos had them) resides in the directory itself. Since we 95 * must update directory entries without the benefit of having the vnode 96 * for the directory we must use the vnode for the filesystem. This means 97 * that when a directory is actually read/written (via read, write, or 98 * readdir, or seek) we must use the vnode for the filesystem instead of 99 * the vnode for the directory as would happen in ufs. This is to insure we 100 * retrieve the correct block from the buffer cache since the hash value is 101 * based upon the vnode address and the desired block number. 102 */ 103 104 /* 105 * Create a regular file. On entry the directory to contain the file being 106 * created is locked. We must release before we return. We must also free 107 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or 108 * only if the SAVESTART bit in cn_flags is clear on success. 109 */ 110 static int 111 msdosfs_create(struct vop_old_create_args *ap) 112 { 113 struct componentname *cnp = ap->a_cnp; 114 struct denode ndirent; 115 struct denode *dep; 116 struct denode *pdep = VTODE(ap->a_dvp); 117 struct timespec ts; 118 int error; 119 120 mprintf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap); 121 122 /* 123 * If this is the root directory and there is no space left we 124 * can't do anything. This is because the root directory can not 125 * change size. 126 */ 127 if (pdep->de_StartCluster == MSDOSFSROOT 128 && pdep->de_fndoffset >= pdep->de_FileSize) { 129 error = ENOSPC; 130 goto bad; 131 } 132 133 /* 134 * Create a directory entry for the file, then call createde() to 135 * have it installed. NOTE: DOS files are always executable. We 136 * use the absence of the owner write bit to make the file 137 * readonly. 138 */ 139 bzero(&ndirent, sizeof(ndirent)); 140 error = uniqdosname(pdep, cnp, ndirent.de_Name); 141 if (error) 142 goto bad; 143 144 ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ? 145 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY; 146 ndirent.de_LowerCase = 0; 147 ndirent.de_StartCluster = 0; 148 ndirent.de_FileSize = 0; 149 ndirent.de_dev = pdep->de_dev; 150 ndirent.de_devvp = pdep->de_devvp; 151 ndirent.de_pmp = pdep->de_pmp; 152 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE; 153 getnanotime(&ts); 154 DETIMES(&ndirent, &ts, &ts, &ts); 155 error = createde(&ndirent, pdep, &dep, cnp); 156 if (error) 157 goto bad; 158 *ap->a_vpp = DETOV(dep); 159 return (0); 160 161 bad: 162 return (error); 163 } 164 165 static int msdosfs_mkdir(struct vop_old_mkdir_args *); 166 167 static int 168 msdosfs_mknod(struct vop_old_mknod_args *ap) 169 { 170 switch (ap->a_vap->va_type) { 171 case VDIR: 172 return (msdosfs_mkdir((struct vop_old_mkdir_args *)ap)); 173 case VREG: 174 return (msdosfs_create((struct vop_old_create_args *)ap)); 175 default: 176 return (EINVAL); 177 } 178 /* NOTREACHED */ 179 } 180 181 static int 182 msdosfs_open(struct vop_open_args *ap) 183 { 184 return(vop_stdopen(ap)); 185 } 186 187 static int 188 msdosfs_close(struct vop_close_args *ap) 189 { 190 struct vnode *vp = ap->a_vp; 191 struct denode *dep = VTODE(vp); 192 struct timespec ts; 193 194 if (VREFCNT(vp) > 1) { 195 getnanotime(&ts); 196 DETIMES(dep, &ts, &ts, &ts); 197 } 198 return (vop_stdclose(ap)); 199 } 200 201 static int 202 msdosfs_access(struct vop_access_args *ap) 203 { 204 struct denode *dep = VTODE(ap->a_vp); 205 struct msdosfsmount *pmp = dep->de_pmp; 206 mode_t file_mode; 207 208 file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) | 209 ((dep->de_Attributes & ATTR_READONLY) ? 210 0 : (S_IWUSR|S_IWGRP|S_IWOTH)); 211 file_mode &= (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask); 212 213 return (vop_helper_access(ap, pmp->pm_uid, pmp->pm_gid, file_mode, 0)); 214 } 215 216 static int 217 msdosfs_getattr(struct vop_getattr_args *ap) 218 { 219 struct denode *dep = VTODE(ap->a_vp); 220 struct msdosfsmount *pmp = dep->de_pmp; 221 struct vattr *vap = ap->a_vap; 222 mode_t mode; 223 struct timespec ts; 224 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry); 225 u_long fileid; 226 227 getnanotime(&ts); 228 DETIMES(dep, &ts, &ts, &ts); 229 vap->va_fsid = dev2udev(dep->de_dev); 230 /* 231 * The following computation of the fileid must be the same as that 232 * used in msdosfs_readdir() to compute d_fileno. If not, pwd 233 * doesn't work. 234 */ 235 if (dep->de_Attributes & ATTR_DIRECTORY) { 236 fileid = xcntobn(pmp, dep->de_StartCluster) * dirsperblk; 237 if (dep->de_StartCluster == MSDOSFSROOT) 238 fileid = 1; 239 } else { 240 fileid = xcntobn(pmp, dep->de_dirclust) * dirsperblk; 241 if (dep->de_dirclust == MSDOSFSROOT) 242 fileid = roottobn(pmp, 0) * dirsperblk; 243 fileid += dep->de_diroffset / sizeof(struct direntry); 244 } 245 vap->va_fileid = fileid; 246 if ((dep->de_Attributes & ATTR_READONLY) == 0) 247 mode = S_IRWXU|S_IRWXG|S_IRWXO; 248 else 249 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; 250 vap->va_mode = mode & 251 (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask); 252 vap->va_uid = pmp->pm_uid; 253 vap->va_gid = pmp->pm_gid; 254 vap->va_nlink = 1; 255 vap->va_rmajor = VNOVAL; 256 vap->va_rminor = VNOVAL; 257 vap->va_size = dep->de_FileSize; 258 dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime); 259 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) { 260 dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime); 261 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, 262 &vap->va_ctime); 263 } else { 264 vap->va_atime = vap->va_mtime; 265 vap->va_ctime = vap->va_mtime; 266 } 267 vap->va_flags = 0; 268 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0) 269 vap->va_flags |= SF_ARCHIVED; 270 vap->va_gen = 0; 271 vap->va_blocksize = pmp->pm_bpcluster; 272 vap->va_bytes = 273 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask; 274 vap->va_type = ap->a_vp->v_type; 275 vap->va_filerev = dep->de_modrev; 276 return (0); 277 } 278 279 static int 280 msdosfs_setattr(struct vop_setattr_args *ap) 281 { 282 struct vnode *vp = ap->a_vp; 283 struct denode *dep = VTODE(vp); 284 struct msdosfsmount *pmp = dep->de_pmp; 285 struct vattr *vap = ap->a_vap; 286 struct ucred *cred = ap->a_cred; 287 int error = 0; 288 289 mprintf("msdosfs_setattr(): vp %p, vap %p, cred %p\n", vp, vap, cred); 290 291 /* 292 * Check for unsettable attributes. 293 */ 294 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 295 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 296 (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) || 297 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 298 mprintf("msdosfs_setattr(): returning EINVAL\n"); 299 mprintf(" va_type %u, va_nlink %"PRIx64", va_fsid %x, va_fileid %"PRIx64"\n", 300 vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid); 301 mprintf(" va_blocksize %lx, va_rmajor %x, va_bytes %"PRIx64", va_gen %"PRIx64"\n", 302 vap->va_blocksize, vap->va_rmajor, vap->va_bytes, 303 vap->va_gen); 304 mprintf(" va_uid %x, va_gid %x\n", 305 vap->va_uid, vap->va_gid); 306 return (EINVAL); 307 } 308 if (vap->va_flags != VNOVAL) { 309 if (vp->v_mount->mnt_flag & MNT_RDONLY) 310 return (EROFS); 311 if (cred->cr_uid != pmp->pm_uid && 312 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0))) 313 return (error); 314 /* 315 * We are very inconsistent about handling unsupported 316 * attributes. We ignored the access time and the 317 * read and execute bits. We were strict for the other 318 * attributes. 319 * 320 * Here we are strict, stricter than ufs in not allowing 321 * users to attempt to set SF_SETTABLE bits or anyone to 322 * set unsupported bits. However, we ignore attempts to 323 * set ATTR_ARCHIVE for directories `cp -pr' from a more 324 * sensible file system attempts it a lot. 325 */ 326 if (cred->cr_uid != 0) { 327 if (vap->va_flags & SF_SETTABLE) 328 return EPERM; 329 } 330 if (vap->va_flags & ~SF_ARCHIVED) 331 return EOPNOTSUPP; 332 if (vap->va_flags & SF_ARCHIVED) 333 dep->de_Attributes &= ~ATTR_ARCHIVE; 334 else if (!(dep->de_Attributes & ATTR_DIRECTORY)) 335 dep->de_Attributes |= ATTR_ARCHIVE; 336 dep->de_flag |= DE_MODIFIED; 337 } 338 339 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { 340 uid_t uid; 341 gid_t gid; 342 343 if (vp->v_mount->mnt_flag & MNT_RDONLY) 344 return (EROFS); 345 uid = vap->va_uid; 346 if (uid == (uid_t)VNOVAL) 347 uid = pmp->pm_uid; 348 gid = vap->va_gid; 349 if (gid == (gid_t)VNOVAL) 350 gid = pmp->pm_gid; 351 if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid || 352 (gid != pmp->pm_gid && !groupmember(gid, cred))) && 353 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0))) 354 return error; 355 if (uid != pmp->pm_uid || gid != pmp->pm_gid) 356 return EINVAL; 357 } 358 359 if (vap->va_size != VNOVAL) { 360 /* 361 * Disallow write attempts on read-only file systems; 362 * unless the file is a socket, fifo, or a block or 363 * character device resident on the file system. 364 */ 365 switch (vp->v_type) { 366 case VDIR: 367 return (EISDIR); 368 /* NOT REACHED */ 369 case VLNK: 370 case VREG: 371 if (vp->v_mount->mnt_flag & MNT_RDONLY) 372 return (EROFS); 373 break; 374 default: 375 break; 376 } 377 error = detrunc(dep, vap->va_size, 0); 378 if (error) 379 return error; 380 } 381 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 382 if (vp->v_mount->mnt_flag & MNT_RDONLY) 383 return (EROFS); 384 if (cred->cr_uid != pmp->pm_uid && 385 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) && 386 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || 387 (error = VOP_EACCESS(vp, VWRITE, cred)))) 388 return (error); 389 if (vp->v_type != VDIR) { 390 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 && 391 vap->va_atime.tv_sec != VNOVAL) { 392 dep->de_flag &= ~DE_ACCESS; 393 unix2dostime(&vap->va_atime, &dep->de_ADate, 394 NULL, NULL); 395 } 396 if (vap->va_mtime.tv_sec != VNOVAL) { 397 dep->de_flag &= ~DE_UPDATE; 398 unix2dostime(&vap->va_mtime, &dep->de_MDate, 399 &dep->de_MTime, NULL); 400 } 401 dep->de_Attributes |= ATTR_ARCHIVE; 402 dep->de_flag |= DE_MODIFIED; 403 } 404 } 405 /* 406 * DOS files only have the ability to have their writability 407 * attribute set, so we use the owner write bit to set the readonly 408 * attribute. 409 */ 410 if (vap->va_mode != (mode_t)VNOVAL) { 411 if (vp->v_mount->mnt_flag & MNT_RDONLY) 412 return (EROFS); 413 if (cred->cr_uid != pmp->pm_uid && 414 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0))) 415 return (error); 416 if (vp->v_type != VDIR) { 417 /* We ignore the read and execute bits. */ 418 if (vap->va_mode & VWRITE) 419 dep->de_Attributes &= ~ATTR_READONLY; 420 else 421 dep->de_Attributes |= ATTR_READONLY; 422 dep->de_Attributes |= ATTR_ARCHIVE; 423 dep->de_flag |= DE_MODIFIED; 424 } 425 } 426 return (deupdat(dep, 1)); 427 } 428 429 static int 430 msdosfs_read(struct vop_read_args *ap) 431 { 432 int error = 0; 433 int blsize; 434 int isadir; 435 size_t orig_resid; 436 u_int n; 437 u_long diff; 438 u_long on; 439 daddr_t lbn; 440 daddr_t rablock; 441 off_t raoffset; 442 off_t loffset; 443 int rasize; 444 int seqcount; 445 struct buf *bp; 446 struct vnode *vp = ap->a_vp; 447 struct denode *dep = VTODE(vp); 448 struct msdosfsmount *pmp = dep->de_pmp; 449 struct uio *uio = ap->a_uio; 450 451 if (uio->uio_offset < 0) 452 return (EINVAL); 453 454 if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX) 455 return (0); 456 /* 457 * If they didn't ask for any data, then we are done. 458 */ 459 orig_resid = uio->uio_resid; 460 if (orig_resid == 0) 461 return (0); 462 463 seqcount = ap->a_ioflag >> IO_SEQSHIFT; 464 465 isadir = dep->de_Attributes & ATTR_DIRECTORY; 466 do { 467 if (uio->uio_offset >= dep->de_FileSize) 468 break; 469 470 /* 471 * note: lbn is a cluster number, not a device block number. 472 */ 473 lbn = de_off2cn(pmp, uio->uio_offset); 474 loffset = de_cn2doff(pmp, lbn); 475 476 /* 477 * If we are operating on a directory file then be sure to 478 * do i/o with the vnode for the filesystem instead of the 479 * vnode for the directory. 480 */ 481 if (isadir) { 482 /* 483 * convert cluster # to block #. lbn is a 484 * device block number after this. 485 */ 486 error = pcbmap(dep, lbn, &lbn, NULL, &blsize); 487 loffset = de_bntodoff(pmp, lbn); 488 if (error == E2BIG) { 489 error = EINVAL; 490 break; 491 } else if (error) 492 break; 493 error = bread(pmp->pm_devvp, loffset, blsize, &bp); 494 } else { 495 blsize = pmp->pm_bpcluster; 496 rablock = lbn + 1; 497 raoffset = de_cn2doff(pmp, rablock); 498 if (seqcount > 1 && 499 raoffset < dep->de_FileSize) { 500 rasize = pmp->pm_bpcluster; 501 error = breadn(vp, loffset, blsize, 502 &raoffset, &rasize, 1, &bp); 503 } else { 504 error = bread(vp, loffset, blsize, &bp); 505 } 506 } 507 if (error) { 508 brelse(bp); 509 break; 510 } 511 on = uio->uio_offset & pmp->pm_crbomask; 512 diff = pmp->pm_bpcluster - on; 513 n = szmin(uio->uio_resid, diff); 514 diff = dep->de_FileSize - uio->uio_offset; 515 if (diff < n) 516 n = diff; 517 diff = blsize - bp->b_resid; 518 if (diff < n) 519 n = diff; 520 error = uiomovebp(bp, bp->b_data + on, (size_t)n, uio); 521 brelse(bp); 522 } while (error == 0 && uio->uio_resid > 0 && n != 0); 523 524 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) && 525 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 526 dep->de_flag |= DE_ACCESS; 527 return (error); 528 } 529 530 /* 531 * Write data to a file or directory. 532 */ 533 static int 534 msdosfs_write(struct vop_write_args *ap) 535 { 536 int n; 537 int croffset; 538 size_t resid; 539 u_long osize; 540 int error = 0; 541 u_long count; 542 daddr_t cn, lastcn; 543 struct buf *bp; 544 int ioflag = ap->a_ioflag; 545 struct uio *uio = ap->a_uio; 546 struct thread *td = uio->uio_td; 547 struct vnode *vp = ap->a_vp; 548 struct vnode *thisvp; 549 struct denode *dep = VTODE(vp); 550 struct msdosfsmount *pmp = dep->de_pmp; 551 struct proc *p = (td ? td->td_proc : NULL); 552 struct lwp *lp = (td ? td->td_lwp : NULL); 553 554 mprintf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n", 555 vp, uio, ioflag, ap->a_cred); 556 mprintf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n", 557 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster); 558 559 switch (vp->v_type) { 560 case VREG: 561 if (ioflag & IO_APPEND) 562 uio->uio_offset = dep->de_FileSize; 563 thisvp = vp; 564 break; 565 case VDIR: 566 return EISDIR; 567 default: 568 panic("msdosfs_write(): bad file type"); 569 } 570 571 if (uio->uio_offset < 0) 572 return (EFBIG); 573 574 if (uio->uio_resid == 0) 575 return (0); 576 577 /* 578 * If they've exceeded their filesize limit, tell them about it. 579 */ 580 if (p && 581 ((uoff_t)uio->uio_offset + uio->uio_resid > 582 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) { 583 lwpsignal(p, lp, SIGXFSZ); 584 return (EFBIG); 585 } 586 587 if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX) 588 return (EFBIG); 589 if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX) 590 return (EFBIG); 591 592 /* 593 * If the offset we are starting the write at is beyond the end of 594 * the file, then they've done a seek. Unix filesystems allow 595 * files with holes in them, DOS doesn't so we must fill the hole 596 * with zeroed blocks. 597 */ 598 if (uio->uio_offset > dep->de_FileSize) { 599 error = deextend(dep, uio->uio_offset); 600 if (error) 601 return (error); 602 } 603 604 /* 605 * Remember some values in case the write fails. 606 */ 607 resid = uio->uio_resid; 608 osize = dep->de_FileSize; 609 610 /* 611 * If we write beyond the end of the file, extend it to its ultimate 612 * size ahead of the time to hopefully get a contiguous area. 613 */ 614 if (uio->uio_offset + resid > osize) { 615 count = de_clcount(pmp, uio->uio_offset + resid) - 616 de_clcount(pmp, osize); 617 error = extendfile(dep, count, NULL, NULL, 0); 618 if (error && (error != ENOSPC || (ioflag & IO_UNIT))) 619 goto errexit; 620 lastcn = dep->de_fc[FC_LASTFC].fc_frcn; 621 } else 622 lastcn = de_clcount(pmp, osize) - 1; 623 624 do { 625 if (de_off2cn(pmp, uio->uio_offset) > lastcn) { 626 error = ENOSPC; 627 break; 628 } 629 630 croffset = uio->uio_offset & pmp->pm_crbomask; 631 n = (int)szmin(uio->uio_resid, pmp->pm_bpcluster - croffset); 632 if (uio->uio_offset + n > dep->de_FileSize) { 633 dep->de_FileSize = uio->uio_offset + n; 634 /* The object size needs to be set before buffer is allocated */ 635 vnode_pager_setsize(vp, dep->de_FileSize); 636 } 637 638 /* 639 * If either the whole cluster gets written, or we write 640 * the cluster from its start beyond EOF, then no need to 641 * read data from disk. 642 * 643 * If UIO_NOCOPY is set we have to do a read-before-write 644 * to fill in any missing pieces of the buffer since no 645 * actual overwrite will occur. 646 */ 647 cn = de_off2cn(pmp, uio->uio_offset); 648 if ((uio->uio_offset & pmp->pm_crbomask) == 0 649 && uio->uio_segflg != UIO_NOCOPY 650 && (de_off2cn(pmp, uio->uio_offset + uio->uio_resid) 651 > de_off2cn(pmp, uio->uio_offset) 652 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) { 653 bp = getblk(thisvp, de_cn2doff(pmp, cn), 654 pmp->pm_bpcluster, 0, 0); 655 clrbuf(bp); 656 /* 657 * Do the bmap now, since pcbmap needs buffers 658 * for the FAT table. (see msdosfs_strategy) 659 */ 660 if (bp->b_bio2.bio_offset == NOOFFSET) { 661 daddr_t lblkno = de_off2cn(pmp, bp->b_loffset); 662 daddr_t dblkno; 663 664 error = pcbmap(dep, lblkno, 665 &dblkno, NULL, NULL); 666 if (error || dblkno == (daddr_t)-1) { 667 bp->b_bio2.bio_offset = NOOFFSET; 668 } else { 669 bp->b_bio2.bio_offset = de_bntodoff(pmp, 670 dblkno); 671 } 672 } 673 if (bp->b_bio2.bio_offset == NOOFFSET) { 674 brelse(bp); 675 if (!error) 676 error = EIO; /* XXX */ 677 break; 678 } 679 } else { 680 /* 681 * The block we need to write into exists, so read 682 * it in. 683 */ 684 error = bread(thisvp, de_cn2doff(pmp, cn), 685 pmp->pm_bpcluster, &bp); 686 if (error) { 687 brelse(bp); 688 break; 689 } 690 } 691 692 /* 693 * Should these vnode_pager_* functions be done on dir 694 * files? 695 */ 696 697 /* 698 * Copy the data from user space into the buf header. 699 */ 700 error = uiomovebp(bp, bp->b_data + croffset, (size_t)n, uio); 701 if (error) { 702 brelse(bp); 703 break; 704 } 705 706 /* 707 * If they want this synchronous then write it and wait for 708 * it. Otherwise, if on a cluster boundary write it 709 * asynchronously so we can move on to the next block 710 * without delay. Otherwise do a delayed write because we 711 * may want to write somemore into the block later. 712 */ 713 if (ioflag & IO_SYNC) 714 bwrite(bp); 715 else if (n + croffset == pmp->pm_bpcluster) 716 bawrite(bp); 717 else 718 bdwrite(bp); 719 dep->de_flag |= DE_UPDATE; 720 } while (error == 0 && uio->uio_resid > 0); 721 722 /* 723 * If the write failed and they want us to, truncate the file back 724 * to the size it was before the write was attempted. 725 */ 726 errexit: 727 if (error) { 728 if (ioflag & IO_UNIT) { 729 detrunc(dep, osize, ioflag & IO_SYNC); 730 uio->uio_offset -= resid - uio->uio_resid; 731 uio->uio_resid = resid; 732 } else { 733 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC); 734 if (uio->uio_resid != resid) 735 error = 0; 736 } 737 } else if (ioflag & IO_SYNC) 738 error = deupdat(dep, 1); 739 return (error); 740 } 741 742 /* 743 * Flush the blocks of a file to disk. 744 * 745 * This function is worthless for vnodes that represent directories. Maybe we 746 * could just do a sync if they try an fsync on a directory file. 747 */ 748 static int 749 msdosfs_fsync(struct vop_fsync_args *ap) 750 { 751 struct vnode *vp = ap->a_vp; 752 753 /* 754 * Flush all dirty buffers associated with a vnode. 755 */ 756 #ifdef DIAGNOSTIC 757 loop: 758 #endif 759 vfsync(vp, ap->a_waitfor, 0, NULL, NULL); 760 #ifdef DIAGNOSTIC 761 if (ap->a_waitfor == MNT_WAIT && !RB_EMPTY(&vp->v_rbdirty_tree)) { 762 vprint("msdosfs_fsync: dirty", vp); 763 goto loop; 764 } 765 #endif 766 return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT)); 767 } 768 769 static int 770 msdosfs_remove(struct vop_old_remove_args *ap) 771 { 772 struct denode *dep = VTODE(ap->a_vp); 773 struct denode *ddep = VTODE(ap->a_dvp); 774 int error; 775 776 if (ap->a_vp->v_type == VDIR) 777 error = EPERM; 778 else 779 error = removede(ddep, dep); 780 mprintf("msdosfs_remove(), dep %p, v_refcnt 0x%08x\n", 781 dep, ap->a_vp->v_refcnt); 782 return (error); 783 } 784 785 /* 786 * DOS filesystems don't know what links are. But since we already called 787 * msdosfs_lookup() with create and lockparent, the parent is locked so we 788 * have to free it before we return the error. 789 */ 790 static int 791 msdosfs_link(struct vop_old_link_args *ap) 792 { 793 return (EOPNOTSUPP); 794 } 795 796 /* 797 * Renames on files require moving the denode to a new hash queue since the 798 * denode's location is used to compute which hash queue to put the file 799 * in. Unless it is a rename in place. For example "mv a b". 800 * 801 * What follows is the basic algorithm: 802 * 803 * if (file move) { 804 * if (dest file exists) { 805 * remove dest file 806 * } 807 * if (dest and src in same directory) { 808 * rewrite name in existing directory slot 809 * } else { 810 * write new entry in dest directory 811 * update offset and dirclust in denode 812 * move denode to new hash chain 813 * clear old directory entry 814 * } 815 * } else { 816 * directory move 817 * if (dest directory exists) { 818 * if (dest is not empty) { 819 * return ENOTEMPTY 820 * } 821 * remove dest directory 822 * } 823 * if (dest and src in same directory) { 824 * rewrite name in existing entry 825 * } else { 826 * be sure dest is not a child of src directory 827 * write entry in dest directory 828 * update "." and ".." in moved directory 829 * clear old directory entry for moved directory 830 * } 831 * } 832 * 833 * On entry: 834 * source's parent directory is unlocked 835 * source file or directory is unlocked 836 * destination's parent directory is locked 837 * destination file or directory is locked if it exists 838 * 839 * On exit: 840 * all denodes should be released 841 * 842 * Notes: 843 * I'm not sure how the memory containing the pathnames pointed at by the 844 * componentname structures is freed, there may be some memory bleeding 845 * for each rename done. 846 */ 847 static int 848 msdosfs_rename(struct vop_old_rename_args *ap) 849 { 850 struct vnode *tdvp = ap->a_tdvp; 851 struct vnode *fvp = ap->a_fvp; 852 struct vnode *fdvp = ap->a_fdvp; 853 struct vnode *tvp = ap->a_tvp; 854 struct componentname *tcnp = ap->a_tcnp; 855 struct componentname *fcnp = ap->a_fcnp; 856 struct denode *ip, *xp, *dp, *zp; 857 u_char toname[11], oldname[11]; 858 u_long from_diroffset, to_diroffset; 859 u_char to_count; 860 int doingdirectory = 0, newparent = 0; 861 int error; 862 u_long cn; 863 daddr_t bn; 864 struct msdosfsmount *pmp; 865 struct direntry *dotdotp; 866 struct buf *bp; 867 868 pmp = VFSTOMSDOSFS(fdvp->v_mount); 869 870 /* 871 * Check for cross-device rename. 872 */ 873 if ((fvp->v_mount != tdvp->v_mount) || 874 (tvp && (fvp->v_mount != tvp->v_mount))) { 875 error = EXDEV; 876 abortit: 877 if (tdvp == tvp) 878 vrele(tdvp); 879 else 880 vput(tdvp); 881 if (tvp) 882 vput(tvp); 883 vrele(fdvp); 884 vrele(fvp); 885 return (error); 886 } 887 888 /* 889 * If source and dest are the same, do nothing. 890 */ 891 if (tvp == fvp) { 892 error = 0; 893 goto abortit; 894 } 895 896 /* 897 * fvp, fdvp are unlocked, tvp, tdvp are locked. Lock fvp and note 898 * that we have to unlock it to use the abortit target. 899 */ 900 error = vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM); 901 if (error) 902 goto abortit; 903 dp = VTODE(fdvp); 904 ip = VTODE(fvp); 905 906 /* 907 * Be sure we are not renaming ".", "..", or an alias of ".". This 908 * leads to a crippled directory tree. It's pretty tough to do a 909 * "ls" or "pwd" with the "." directory entry missing, and "cd .." 910 * doesn't work if the ".." entry is missing. 911 */ 912 if (ip->de_Attributes & ATTR_DIRECTORY) { 913 /* 914 * Avoid ".", "..", and aliases of "." for obvious reasons. 915 */ 916 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') || 917 dp == ip || 918 (fcnp->cn_flags & CNP_ISDOTDOT) || 919 (tcnp->cn_flags & CNP_ISDOTDOT) || 920 (ip->de_flag & DE_RENAME)) { 921 vn_unlock(fvp); 922 error = EINVAL; 923 goto abortit; 924 } 925 ip->de_flag |= DE_RENAME; 926 doingdirectory++; 927 } 928 929 /* 930 * fvp locked, fdvp unlocked, tvp, tdvp locked. DE_RENAME only 931 * set if doingdirectory. We will get fvp unlocked in fairly 932 * short order. dp and xp must be setup and fvp must be unlocked 933 * for the out and bad targets to work properly. 934 */ 935 dp = VTODE(tdvp); 936 xp = tvp ? VTODE(tvp) : NULL; 937 938 /* 939 * Remember direntry place to use for destination 940 */ 941 to_diroffset = dp->de_fndoffset; 942 to_count = dp->de_fndcnt; 943 944 /* 945 * If ".." must be changed (ie the directory gets a new 946 * parent) then the source directory must not be in the 947 * directory hierarchy above the target, as this would 948 * orphan everything below the source directory. Also 949 * the user must have write permission in the source so 950 * as to be able to change "..". We must repeat the call 951 * to namei, as the parent directory is unlocked by the 952 * call to doscheckpath(). 953 */ 954 error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred); 955 vn_unlock(fvp); 956 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster) 957 newparent = 1; 958 959 /* 960 * ok. fvp, fdvp unlocked, tvp, tdvp locked. tvp may be NULL. 961 * DE_RENAME only set if doingdirectory. 962 */ 963 if (doingdirectory && newparent) { 964 if (error) /* write access check above */ 965 goto bad; 966 if (xp != NULL) { 967 vput(tvp); 968 xp = NULL; 969 } 970 /* 971 * checkpath vput's tdvp (VTOI(dp)) on return no matter what, 972 * get an extra ref so we wind up with just an unlocked, ref'd 973 * tdvp. The 'out' target skips tvp and tdvp cleanups (tdvp 974 * isn't locked so we can't use the out target). 975 */ 976 vref(tdvp); 977 error = doscheckpath(ip, dp); 978 tcnp->cn_flags |= CNP_PDIRUNLOCK; 979 if (error) { 980 vrele(tdvp); 981 goto out; 982 } 983 /* 984 * relookup no longer messes with the ref count. tdvp must 985 * be unlocked on entry and on success will be locked on 986 * return. 987 */ 988 error = relookup(tdvp, &tvp, tcnp); 989 if (error) { 990 if (tcnp->cn_flags & CNP_PDIRUNLOCK) 991 vrele(tdvp); 992 else 993 vput(tdvp); 994 goto out; 995 } 996 997 /* 998 * tvp and tdvp are now locked again. 999 */ 1000 dp = VTODE(tdvp); 1001 xp = tvp ? VTODE(tvp) : NULL; 1002 } 1003 1004 /* 1005 * tvp and tdvp are now locked again, the 'bad' target can be used 1006 * to clean them up again. Delete an existant target and clean 1007 * up tvp. Set xp to NULL to indicate that tvp has been cleaned up. 1008 */ 1009 if (xp != NULL) { 1010 /* 1011 * Target must be empty if a directory and have no links 1012 * to it. Also, ensure source and target are compatible 1013 * (both directories, or both not directories). 1014 */ 1015 if (xp->de_Attributes & ATTR_DIRECTORY) { 1016 if (!dosdirempty(xp)) { 1017 error = ENOTEMPTY; 1018 goto bad; 1019 } 1020 if (!doingdirectory) { 1021 error = ENOTDIR; 1022 goto bad; 1023 } 1024 } else if (doingdirectory) { 1025 error = EISDIR; 1026 goto bad; 1027 } 1028 error = removede(dp, xp); 1029 if (error) 1030 goto bad; 1031 vput(tvp); 1032 xp = NULL; 1033 tvp = NULL; 1034 } 1035 1036 /* 1037 * Convert the filename in tcnp into a dos filename. We copy this 1038 * into the denode and directory entry for the destination 1039 * file/directory. 1040 */ 1041 error = uniqdosname(VTODE(tdvp), tcnp, toname); 1042 if (error) 1043 goto bad; 1044 1045 /* 1046 * Since from wasn't locked at various places above, we have to do 1047 * a relookup here. If the target and source are the same directory 1048 * we have to unlock the target directory in order to safely relookup 1049 * the source, because relookup expects its directory to be unlocked. 1050 * 1051 * Note that ap->a_fvp is still valid and ref'd. Any cleanup must 1052 * now take that into account. 1053 * 1054 * The tdvp locking issues make this a real mess. 1055 */ 1056 fcnp->cn_flags &= ~CNP_MODMASK; 1057 fcnp->cn_flags |= CNP_LOCKPARENT; 1058 if (newparent == 0) 1059 vn_unlock(tdvp); 1060 error = relookup(fdvp, &fvp, fcnp); 1061 if (error || fvp == NULL) { 1062 /* 1063 * From name has disappeared. Note: fdvp might == tdvp. 1064 * 1065 * DE_RENAME is only set if doingdirectory. 1066 */ 1067 if (doingdirectory) 1068 panic("rename: lost dir entry"); 1069 if (fcnp->cn_flags & CNP_PDIRUNLOCK) 1070 vrele(fdvp); 1071 else 1072 vput(fdvp); 1073 if (newparent == 0) 1074 vrele(tdvp); 1075 else 1076 vput(tdvp); 1077 vrele(ap->a_fvp); 1078 return(0); 1079 } 1080 1081 /* 1082 * No error occured. tdvp, fdvp and fvp are all locked. If 1083 * newparent was 0 be aware that fdvp == tdvp. tvp has been cleaned 1084 * up. ap->a_fvp is still refd. 1085 */ 1086 xp = VTODE(fvp); 1087 zp = VTODE(fdvp); 1088 from_diroffset = zp->de_fndoffset; 1089 1090 /* 1091 * Ensure that the directory entry still exists and has not 1092 * changed till now. If the source is a file the entry may 1093 * have been unlinked or renamed. In either case there is 1094 * no further work to be done. If the source is a directory 1095 * then it cannot have been rmdir'ed or renamed; this is 1096 * prohibited by the DE_RENAME flag. 1097 * 1098 * DE_RENAME is only set if doingdirectory. 1099 */ 1100 if (xp != ip) { 1101 if (doingdirectory) 1102 panic("rename: lost dir entry"); 1103 goto done; 1104 } else { 1105 u_long new_dirclust; 1106 u_long new_diroffset; 1107 1108 /* 1109 * First write a new entry in the destination 1110 * directory and mark the entry in the source directory 1111 * as deleted. Then move the denode to the correct hash 1112 * chain for its new location in the filesystem. And, if 1113 * we moved a directory, then update its .. entry to point 1114 * to the new parent directory. 1115 */ 1116 bcopy(ip->de_Name, oldname, 11); 1117 bcopy(toname, ip->de_Name, 11); /* update denode */ 1118 dp->de_fndoffset = to_diroffset; 1119 dp->de_fndcnt = to_count; 1120 error = createde(ip, dp, NULL, tcnp); 1121 if (error) { 1122 bcopy(oldname, ip->de_Name, 11); 1123 goto done; 1124 } 1125 ip->de_refcnt++; 1126 zp->de_fndoffset = from_diroffset; 1127 error = removede(zp, ip); 1128 if (error) { 1129 /* XXX should really panic here, fs is corrupt */ 1130 goto done; 1131 } 1132 if (!doingdirectory) { 1133 error = pcbmap(dp, de_cluster(pmp, to_diroffset), 1134 NULL, &new_dirclust, NULL); 1135 if (error) { 1136 /* XXX should really panic here, fs is corrupt */ 1137 goto done; 1138 } 1139 if (new_dirclust == MSDOSFSROOT) 1140 new_diroffset = to_diroffset; 1141 else 1142 new_diroffset = to_diroffset & pmp->pm_crbomask; 1143 msdosfs_reinsert(ip, new_dirclust, new_diroffset); 1144 } 1145 } 1146 1147 /* 1148 * If we moved a directory to a new parent directory, then we must 1149 * fixup the ".." entry in the moved directory. 1150 */ 1151 if (doingdirectory && newparent) { 1152 cn = ip->de_StartCluster; 1153 if (cn == MSDOSFSROOT) { 1154 /* this should never happen */ 1155 panic("msdosfs_rename(): updating .. in root directory?"); 1156 } else { 1157 bn = xcntobn(pmp, cn); 1158 } 1159 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), 1160 pmp->pm_bpcluster, &bp); 1161 if (error) { 1162 /* XXX should really panic here, fs is corrupt */ 1163 brelse(bp); 1164 goto done; 1165 } 1166 dotdotp = (struct direntry *)bp->b_data + 1; 1167 putushort(dotdotp->deStartCluster, dp->de_StartCluster); 1168 if (FAT32(pmp)) 1169 putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16); 1170 error = bwrite(bp); 1171 if (error) { 1172 /* XXX should really panic here, fs is corrupt */ 1173 goto done; 1174 } 1175 } 1176 1177 /* 1178 * done case fvp, fdvp, tdvp are locked. ap->a_fvp is refd 1179 */ 1180 done: 1181 if (doingdirectory) 1182 ip->de_flag &= ~DE_RENAME; /* XXX fvp not locked */ 1183 vput(fvp); 1184 if (newparent) 1185 vput(fdvp); 1186 else 1187 vrele(fdvp); 1188 vput(tdvp); 1189 vrele(ap->a_fvp); 1190 return (error); 1191 1192 /* 1193 * 'bad' target: xp governs tvp. tvp and tdvp arel ocked, fdvp and fvp 1194 * are not locked. ip points to fvp's inode which may have DE_RENAME 1195 * set. 1196 */ 1197 bad: 1198 if (xp) 1199 vput(tvp); 1200 vput(tdvp); 1201 out: 1202 /* 1203 * 'out' target: tvp and tdvp have already been cleaned up. 1204 */ 1205 if (doingdirectory) 1206 ip->de_flag &= ~DE_RENAME; 1207 vrele(fdvp); 1208 vrele(fvp); 1209 return (error); 1210 1211 } 1212 1213 static struct { 1214 struct direntry dot; 1215 struct direntry dotdot; 1216 } dosdirtemplate = { 1217 { ". ", /* the . entry */ 1218 ATTR_DIRECTORY, /* file attribute */ 1219 0, /* reserved */ 1220 0, { 0, 0 }, { 0, 0 }, /* create time & date */ 1221 { 0, 0 }, /* access date */ 1222 { 0, 0 }, /* high bits of start cluster */ 1223 { 210, 4 }, { 210, 4 }, /* modify time & date */ 1224 { 0, 0 }, /* startcluster */ 1225 { 0, 0, 0, 0 } /* filesize */ 1226 }, 1227 { ".. ", /* the .. entry */ 1228 ATTR_DIRECTORY, /* file attribute */ 1229 0, /* reserved */ 1230 0, { 0, 0 }, { 0, 0 }, /* create time & date */ 1231 { 0, 0 }, /* access date */ 1232 { 0, 0 }, /* high bits of start cluster */ 1233 { 210, 4 }, { 210, 4 }, /* modify time & date */ 1234 { 0, 0 }, /* startcluster */ 1235 { 0, 0, 0, 0 } /* filesize */ 1236 } 1237 }; 1238 1239 static int 1240 msdosfs_mkdir(struct vop_old_mkdir_args *ap) 1241 { 1242 struct componentname *cnp = ap->a_cnp; 1243 struct denode *dep; 1244 struct denode *pdep = VTODE(ap->a_dvp); 1245 struct direntry *denp; 1246 struct msdosfsmount *pmp = pdep->de_pmp; 1247 struct buf *bp; 1248 u_long newcluster, pcl; 1249 int bn; 1250 int error; 1251 struct denode ndirent; 1252 struct timespec ts; 1253 1254 /* 1255 * If this is the root directory and there is no space left we 1256 * can't do anything. This is because the root directory can not 1257 * change size. 1258 */ 1259 if (pdep->de_StartCluster == MSDOSFSROOT 1260 && pdep->de_fndoffset >= pdep->de_FileSize) { 1261 error = ENOSPC; 1262 goto bad2; 1263 } 1264 1265 /* 1266 * Allocate a cluster to hold the about to be created directory. 1267 */ 1268 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL); 1269 if (error) 1270 goto bad2; 1271 1272 bzero(&ndirent, sizeof(ndirent)); 1273 ndirent.de_pmp = pmp; 1274 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE; 1275 getnanotime(&ts); 1276 DETIMES(&ndirent, &ts, &ts, &ts); 1277 1278 /* 1279 * Now fill the cluster with the "." and ".." entries. And write 1280 * the cluster to disk. This way it is there for the parent 1281 * directory to be pointing at if there were a crash. 1282 */ 1283 bn = xcntobn(pmp, newcluster); 1284 /* always succeeds */ 1285 bp = getblk(pmp->pm_devvp, de_bntodoff(pmp, bn), 1286 pmp->pm_bpcluster, 0, 0); 1287 bzero(bp->b_data, pmp->pm_bpcluster); 1288 bcopy(&dosdirtemplate, bp->b_data, sizeof(dosdirtemplate)); 1289 denp = (struct direntry *)bp->b_data; 1290 putushort(denp[0].deStartCluster, newcluster); 1291 putushort(denp[0].deCDate, ndirent.de_CDate); 1292 putushort(denp[0].deCTime, ndirent.de_CTime); 1293 denp[0].deCHundredth = ndirent.de_CHun; 1294 putushort(denp[0].deADate, ndirent.de_ADate); 1295 putushort(denp[0].deMDate, ndirent.de_MDate); 1296 putushort(denp[0].deMTime, ndirent.de_MTime); 1297 pcl = pdep->de_StartCluster; 1298 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk) 1299 pcl = 0; 1300 putushort(denp[1].deStartCluster, pcl); 1301 putushort(denp[1].deCDate, ndirent.de_CDate); 1302 putushort(denp[1].deCTime, ndirent.de_CTime); 1303 denp[1].deCHundredth = ndirent.de_CHun; 1304 putushort(denp[1].deADate, ndirent.de_ADate); 1305 putushort(denp[1].deMDate, ndirent.de_MDate); 1306 putushort(denp[1].deMTime, ndirent.de_MTime); 1307 if (FAT32(pmp)) { 1308 putushort(denp[0].deHighClust, newcluster >> 16); 1309 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16); 1310 } 1311 1312 error = bwrite(bp); 1313 if (error) 1314 goto bad; 1315 1316 /* 1317 * Now build up a directory entry pointing to the newly allocated 1318 * cluster. This will be written to an empty slot in the parent 1319 * directory. 1320 */ 1321 error = uniqdosname(pdep, cnp, ndirent.de_Name); 1322 if (error) 1323 goto bad; 1324 1325 ndirent.de_Attributes = ATTR_DIRECTORY; 1326 ndirent.de_LowerCase = 0; 1327 ndirent.de_StartCluster = newcluster; 1328 ndirent.de_FileSize = 0; 1329 ndirent.de_dev = pdep->de_dev; 1330 ndirent.de_devvp = pdep->de_devvp; 1331 error = createde(&ndirent, pdep, &dep, cnp); 1332 if (error) 1333 goto bad; 1334 *ap->a_vpp = DETOV(dep); 1335 return (0); 1336 1337 bad: 1338 clusterfree(pmp, newcluster, NULL); 1339 bad2: 1340 return (error); 1341 } 1342 1343 static int 1344 msdosfs_rmdir(struct vop_old_rmdir_args *ap) 1345 { 1346 struct vnode *vp = ap->a_vp; 1347 struct vnode *dvp = ap->a_dvp; 1348 struct denode *ip, *dp; 1349 int error; 1350 1351 ip = VTODE(vp); 1352 dp = VTODE(dvp); 1353 1354 /* 1355 * Verify the directory is empty (and valid). 1356 * (Rmdir ".." won't be valid since 1357 * ".." will contain a reference to 1358 * the current directory and thus be 1359 * non-empty.) 1360 */ 1361 error = 0; 1362 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) { 1363 error = ENOTEMPTY; 1364 goto out; 1365 } 1366 /* 1367 * Delete the entry from the directory. For dos filesystems this 1368 * gets rid of the directory entry on disk, the in memory copy 1369 * still exists but the de_refcnt is <= 0. This prevents it from 1370 * being found by deget(). When the vput() on dep is done we give 1371 * up access and eventually msdosfs_reclaim() will be called which 1372 * will remove it from the denode cache. 1373 */ 1374 error = removede(dp, ip); 1375 if (error) 1376 goto out; 1377 /* 1378 * This is where we decrement the link count in the parent 1379 * directory. Since dos filesystems don't do this we just purge 1380 * the name cache. 1381 */ 1382 vn_unlock(dvp); 1383 /* 1384 * Truncate the directory that is being deleted. 1385 */ 1386 error = detrunc(ip, (u_long)0, IO_SYNC); 1387 1388 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1389 out: 1390 return (error); 1391 } 1392 1393 /* 1394 * DOS filesystems don't know what symlinks are. 1395 */ 1396 static int 1397 msdosfs_symlink(struct vop_old_symlink_args *ap) 1398 { 1399 return (EOPNOTSUPP); 1400 } 1401 1402 static int 1403 msdosfs_readdir(struct vop_readdir_args *ap) 1404 { 1405 struct mbnambuf nb; 1406 int error = 0; 1407 int diff; 1408 long n; 1409 int blsize; 1410 long on; 1411 u_long cn; 1412 u_long dirsperblk; 1413 long bias = 0; 1414 daddr_t bn, lbn; 1415 struct buf *bp; 1416 struct denode *dep; 1417 struct msdosfsmount *pmp; 1418 struct direntry *dentp; 1419 struct uio *uio = ap->a_uio; 1420 off_t *cookies = NULL; 1421 int ncookies = 0; 1422 off_t offset, off; 1423 int chksum = -1; 1424 ino_t d_ino; 1425 uint16_t d_namlen; 1426 uint8_t d_type; 1427 char *d_name_storage = NULL; 1428 char *d_name = NULL; 1429 1430 error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM); 1431 if (error) 1432 return (error); 1433 1434 dep = VTODE(ap->a_vp); 1435 pmp = dep->de_pmp; 1436 1437 mprintf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n", 1438 ap->a_vp, uio, ap->a_cred, ap->a_eofflag); 1439 1440 /* 1441 * msdosfs_readdir() won't operate properly on regular files since 1442 * it does i/o only with the the filesystem vnode, and hence can 1443 * retrieve the wrong block from the buffer cache for a plain file. 1444 * So, fail attempts to readdir() on a plain file. 1445 */ 1446 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) { 1447 error = ENOTDIR; 1448 goto done; 1449 } 1450 1451 /* 1452 * If the user buffer is smaller than the size of one dos directory 1453 * entry or the file offset is not a multiple of the size of a 1454 * directory entry, then we fail the read. 1455 */ 1456 off = offset = uio->uio_offset; 1457 if (uio->uio_resid < sizeof(struct direntry) || 1458 (offset & (sizeof(struct direntry) - 1))) { 1459 error = EINVAL; 1460 goto done; 1461 } 1462 1463 if (ap->a_ncookies) { 1464 ncookies = uio->uio_resid / 16 + 1; 1465 if (ncookies > 1024) 1466 ncookies = 1024; 1467 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK); 1468 *ap->a_cookies = cookies; 1469 *ap->a_ncookies = ncookies; 1470 } 1471 1472 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry); 1473 1474 /* 1475 * If they are reading from the root directory then, we simulate 1476 * the . and .. entries since these don't exist in the root 1477 * directory. We also set the offset bias to make up for having to 1478 * simulate these entries. By this I mean that at file offset 64 we 1479 * read the first entry in the root directory that lives on disk. 1480 */ 1481 if (dep->de_StartCluster == MSDOSFSROOT 1482 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) { 1483 bias = 2 * sizeof(struct direntry); 1484 if (offset < bias) { 1485 for (n = (int)offset / sizeof(struct direntry); n < 2; 1486 n++) { 1487 if (FAT32(pmp)) 1488 d_ino = xcntobn(pmp, pmp->pm_rootdirblk) 1489 * dirsperblk; 1490 else 1491 d_ino = 1; 1492 d_type = DT_DIR; 1493 if (n == 0) { 1494 d_namlen = 1; 1495 d_name = "."; 1496 } else if (n == 1) { 1497 d_namlen = 2; 1498 d_name = ".."; 1499 } 1500 if (vop_write_dirent(&error, uio, d_ino, d_type, 1501 d_namlen, d_name)) 1502 goto out; 1503 if (error) 1504 goto out; 1505 offset += sizeof(struct direntry); 1506 off = offset; 1507 if (cookies) { 1508 *cookies++ = offset; 1509 if (--ncookies <= 0) 1510 goto out; 1511 } 1512 } 1513 } 1514 } 1515 1516 d_name_storage = kmalloc(WIN_MAXLEN, M_TEMP, M_WAITOK); 1517 mbnambuf_init(&nb); 1518 off = offset; 1519 1520 while (uio->uio_resid > 0) { 1521 lbn = de_off2cn(pmp, offset - bias); 1522 on = (offset - bias) & pmp->pm_crbomask; 1523 n = szmin(pmp->pm_bpcluster - on, uio->uio_resid); 1524 diff = dep->de_FileSize - (offset - bias); 1525 if (diff <= 0) 1526 break; 1527 n = min(n, diff); 1528 error = pcbmap(dep, lbn, &bn, &cn, &blsize); 1529 if (error) 1530 break; 1531 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp); 1532 if (error) { 1533 brelse(bp); 1534 kfree(d_name_storage, M_TEMP); 1535 goto done; 1536 } 1537 n = min(n, blsize - bp->b_resid); 1538 1539 /* 1540 * Convert from dos directory entries to fs-independent 1541 * directory entries. 1542 */ 1543 for (dentp = (struct direntry *)(bp->b_data + on); 1544 (char *)dentp < bp->b_data + on + n; 1545 dentp++, offset += sizeof(struct direntry)) { 1546 d_name = d_name_storage; 1547 d_namlen = 0; 1548 /* 1549 * If this is an unused entry, we can stop. 1550 */ 1551 if (dentp->deName[0] == SLOT_EMPTY) { 1552 brelse(bp); 1553 goto out; 1554 } 1555 /* 1556 * Skip deleted entries. 1557 */ 1558 if (dentp->deName[0] == SLOT_DELETED) { 1559 chksum = -1; 1560 mbnambuf_init(&nb); 1561 continue; 1562 } 1563 /* 1564 * Handle Win95 long directory entries 1565 */ 1566 if (dentp->deAttributes == ATTR_WIN95) { 1567 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 1568 continue; 1569 chksum = win2unixfn(&nb, 1570 (struct winentry *)dentp, 1571 chksum, pmp); 1572 continue; 1573 } 1574 1575 /* 1576 * Skip volume labels 1577 */ 1578 if (dentp->deAttributes & ATTR_VOLUME) { 1579 chksum = -1; 1580 mbnambuf_init(&nb); 1581 continue; 1582 } 1583 /* 1584 * This computation of d_ino must match 1585 * the computation of va_fileid in 1586 * msdosfs_getattr. 1587 */ 1588 if (dentp->deAttributes & ATTR_DIRECTORY) { 1589 d_ino = getushort(dentp->deStartCluster); 1590 if (FAT32(pmp)) 1591 d_ino |= getushort(dentp->deHighClust) << 16; 1592 /* if this is the root directory */ 1593 if (d_ino != MSDOSFSROOT) 1594 d_ino = xcntobn(pmp, d_ino) * dirsperblk; 1595 else if (FAT32(pmp)) 1596 d_ino = xcntobn(pmp, pmp->pm_rootdirblk) 1597 * dirsperblk; 1598 else 1599 d_ino = 1; 1600 d_type = DT_DIR; 1601 } else { 1602 d_ino = offset / sizeof(struct direntry); 1603 d_type = DT_REG; 1604 } 1605 if (chksum != winChksum(dentp->deName)) { 1606 d_namlen = dos2unixfn(dentp->deName, 1607 (u_char *)d_name, 1608 dentp->deLowerCase | 1609 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ? 1610 (LCASE_BASE | LCASE_EXT) : 0), 1611 pmp); 1612 mbnambuf_init(&nb); 1613 } else { 1614 mbnambuf_flush(&nb, d_name, &d_namlen); 1615 } 1616 chksum = -1; 1617 if (vop_write_dirent(&error, uio, d_ino, d_type, 1618 d_namlen, d_name)) { 1619 brelse(bp); 1620 goto out; 1621 } 1622 if (error) { 1623 brelse(bp); 1624 goto out; 1625 } 1626 1627 if (cookies) { 1628 *cookies++ = offset + sizeof(struct direntry); 1629 if (--ncookies <= 0) { 1630 brelse(bp); 1631 goto out; 1632 } 1633 } 1634 off = offset + sizeof(struct direntry); 1635 } 1636 brelse(bp); 1637 } 1638 out: 1639 if (d_name_storage != NULL) 1640 kfree(d_name_storage, M_TEMP); 1641 1642 /* Subtract unused cookies */ 1643 if (ap->a_ncookies) 1644 *ap->a_ncookies -= ncookies; 1645 1646 uio->uio_offset = off; 1647 1648 /* 1649 * Set the eofflag (NFS uses it) 1650 */ 1651 if (ap->a_eofflag) { 1652 if (dep->de_FileSize - (offset - bias) <= 0) 1653 *ap->a_eofflag = 1; 1654 else 1655 *ap->a_eofflag = 0; 1656 } 1657 done: 1658 vn_unlock(ap->a_vp); 1659 return (error); 1660 } 1661 1662 /* 1663 * vp - address of vnode file the file 1664 * bn - which cluster we are interested in mapping to a filesystem block number. 1665 * vpp - returns the vnode for the block special file holding the filesystem 1666 * containing the file of interest 1667 * bnp - address of where to return the filesystem relative block number 1668 */ 1669 static int 1670 msdosfs_bmap(struct vop_bmap_args *ap) 1671 { 1672 struct denode *dep = VTODE(ap->a_vp); 1673 struct msdosfsmount *pmp = dep->de_pmp; 1674 daddr_t lbn; 1675 daddr_t dbn; 1676 int error; 1677 1678 if (ap->a_doffsetp == NULL) 1679 return (0); 1680 if (ap->a_runp) { 1681 /* 1682 * Sequential clusters should be counted here. 1683 */ 1684 *ap->a_runp = 0; 1685 } 1686 if (ap->a_runb) { 1687 *ap->a_runb = 0; 1688 } 1689 KKASSERT(((int)ap->a_loffset & ((1 << pmp->pm_cnshift) - 1)) == 0); 1690 lbn = de_off2cn(pmp, ap->a_loffset); 1691 error = pcbmap(dep, lbn, &dbn, NULL, NULL); 1692 if (error || dbn == (daddr_t)-1) { 1693 *ap->a_doffsetp = NOOFFSET; 1694 } else { 1695 *ap->a_doffsetp = de_bntodoff(pmp, dbn); 1696 } 1697 return (error); 1698 } 1699 1700 static int 1701 msdosfs_strategy(struct vop_strategy_args *ap) 1702 { 1703 struct bio *bio = ap->a_bio; 1704 struct bio *nbio; 1705 struct buf *bp = bio->bio_buf; 1706 struct vnode *vp = ap->a_vp; 1707 struct denode *dep = VTODE(vp); 1708 struct msdosfsmount *pmp = dep->de_pmp; 1709 int error = 0; 1710 daddr_t dblkno; 1711 1712 if (vp->v_type == VBLK || vp->v_type == VCHR) 1713 panic("msdosfs_strategy: spec"); 1714 /* 1715 * If we don't already know the filesystem relative block number 1716 * then get it using pcbmap(). If pcbmap() returns the block 1717 * number as -1 then we've got a hole in the file. DOS filesystems 1718 * don't allow files with holes, so we shouldn't ever see this. 1719 */ 1720 nbio = push_bio(bio); 1721 if (nbio->bio_offset == NOOFFSET) { 1722 error = pcbmap(dep, de_off2cn(pmp, bio->bio_offset), 1723 &dblkno, NULL, NULL); 1724 if (error) { 1725 bp->b_error = error; 1726 bp->b_flags |= B_ERROR; 1727 /* I/O was never started on nbio, must biodone(bio) */ 1728 biodone(bio); 1729 return (error); 1730 } 1731 if (dblkno == (daddr_t)-1) { 1732 nbio->bio_offset = NOOFFSET; 1733 vfs_bio_clrbuf(bp); 1734 } else { 1735 nbio->bio_offset = de_bntodoff(pmp, dblkno); 1736 } 1737 } 1738 if (nbio->bio_offset == NOOFFSET) { 1739 /* I/O was never started on nbio, must biodone(bio) */ 1740 biodone(bio); 1741 return (0); 1742 } 1743 /* 1744 * Read/write the block from/to the disk that contains the desired 1745 * file block. 1746 */ 1747 vn_strategy(dep->de_devvp, nbio); 1748 return (0); 1749 } 1750 1751 static int 1752 msdosfs_print(struct vop_print_args *ap) 1753 { 1754 struct denode *dep = VTODE(ap->a_vp); 1755 1756 kprintf("tag VT_MSDOSFS, startcluster %lu, dircluster %lu, " 1757 "diroffset %lu, dev %d, %d", 1758 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset, 1759 major(dep->de_dev), minor(dep->de_dev)); 1760 lockmgr_printinfo(&ap->a_vp->v_lock); 1761 return (0); 1762 } 1763 1764 static int 1765 msdosfs_pathconf(struct vop_pathconf_args *ap) 1766 { 1767 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp; 1768 1769 switch (ap->a_name) { 1770 case _PC_CHOWN_RESTRICTED: 1771 *ap->a_retval = 1; 1772 return (0); 1773 case _PC_FILESIZEBITS: 1774 *ap->a_retval = FAT12(pmp) ? 32 : 33; 1775 return (0); 1776 case _PC_LINK_MAX: 1777 *ap->a_retval = 1; 1778 return (0); 1779 case _PC_NAME_MAX: 1780 *ap->a_retval = 1781 pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12; 1782 return (0); 1783 case _PC_NO_TRUNC: 1784 *ap->a_retval = 0; 1785 return (0); 1786 case _PC_PATH_MAX: 1787 *ap->a_retval = PATH_MAX; 1788 return (0); 1789 default: 1790 return (EINVAL); 1791 } 1792 /* NOTREACHED */ 1793 } 1794 1795 /* Global vfs data structures for msdosfs */ 1796 struct vop_ops msdosfs_vnode_vops = { 1797 .vop_default = vop_defaultop, 1798 .vop_access = msdosfs_access, 1799 .vop_bmap = msdosfs_bmap, 1800 .vop_old_lookup = msdosfs_lookup, 1801 .vop_open = msdosfs_open, 1802 .vop_close = msdosfs_close, 1803 .vop_old_create = msdosfs_create, 1804 .vop_fsync = msdosfs_fsync, 1805 .vop_getattr = msdosfs_getattr, 1806 .vop_inactive = msdosfs_inactive, 1807 .vop_old_link = msdosfs_link, 1808 .vop_old_mkdir = msdosfs_mkdir, 1809 .vop_old_mknod = msdosfs_mknod, 1810 .vop_pathconf = msdosfs_pathconf, 1811 .vop_print = msdosfs_print, 1812 .vop_read = msdosfs_read, 1813 .vop_readdir = msdosfs_readdir, 1814 .vop_reclaim = msdosfs_reclaim, 1815 .vop_old_remove = msdosfs_remove, 1816 .vop_old_rename = msdosfs_rename, 1817 .vop_old_rmdir = msdosfs_rmdir, 1818 .vop_setattr = msdosfs_setattr, 1819 .vop_strategy = msdosfs_strategy, 1820 .vop_old_symlink = msdosfs_symlink, 1821 .vop_write = msdosfs_write, 1822 .vop_getpages = vop_stdgetpages, 1823 .vop_putpages = vop_stdputpages 1824 }; 1825