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