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