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