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