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