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