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