1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2000-2001 Boris Popov 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 #include <sys/param.h> 29 #include <sys/systm.h> 30 #include <sys/namei.h> 31 #include <sys/kernel.h> 32 #include <sys/proc.h> 33 #include <sys/bio.h> 34 #include <sys/buf.h> 35 #include <sys/fcntl.h> 36 #include <sys/mount.h> 37 #include <sys/unistd.h> 38 #include <sys/vnode.h> 39 #include <sys/limits.h> 40 #include <sys/lockf.h> 41 #include <sys/stat.h> 42 43 #include <vm/vm.h> 44 #include <vm/vm_extern.h> 45 46 #include <netsmb/smb.h> 47 #include <netsmb/smb_conn.h> 48 #include <netsmb/smb_subr.h> 49 50 #include <fs/smbfs/smbfs.h> 51 #include <fs/smbfs/smbfs_node.h> 52 #include <fs/smbfs/smbfs_subr.h> 53 54 /* 55 * Prototypes for SMBFS vnode operations 56 */ 57 static vop_create_t smbfs_create; 58 static vop_mknod_t smbfs_mknod; 59 static vop_open_t smbfs_open; 60 static vop_close_t smbfs_close; 61 static vop_access_t smbfs_access; 62 static vop_getattr_t smbfs_getattr; 63 static vop_setattr_t smbfs_setattr; 64 static vop_read_t smbfs_read; 65 static vop_write_t smbfs_write; 66 static vop_fsync_t smbfs_fsync; 67 static vop_remove_t smbfs_remove; 68 static vop_link_t smbfs_link; 69 static vop_lookup_t smbfs_lookup; 70 static vop_rename_t smbfs_rename; 71 static vop_mkdir_t smbfs_mkdir; 72 static vop_rmdir_t smbfs_rmdir; 73 static vop_symlink_t smbfs_symlink; 74 static vop_readdir_t smbfs_readdir; 75 static vop_strategy_t smbfs_strategy; 76 static vop_print_t smbfs_print; 77 static vop_pathconf_t smbfs_pathconf; 78 static vop_advlock_t smbfs_advlock; 79 static vop_getextattr_t smbfs_getextattr; 80 81 struct vop_vector smbfs_vnodeops = { 82 .vop_default = &default_vnodeops, 83 84 .vop_access = smbfs_access, 85 .vop_advlock = smbfs_advlock, 86 .vop_close = smbfs_close, 87 .vop_create = smbfs_create, 88 .vop_fsync = smbfs_fsync, 89 .vop_getattr = smbfs_getattr, 90 .vop_getextattr = smbfs_getextattr, 91 .vop_getpages = smbfs_getpages, 92 .vop_inactive = smbfs_inactive, 93 .vop_ioctl = smbfs_ioctl, 94 .vop_link = smbfs_link, 95 .vop_lookup = smbfs_lookup, 96 .vop_mkdir = smbfs_mkdir, 97 .vop_mknod = smbfs_mknod, 98 .vop_open = smbfs_open, 99 .vop_pathconf = smbfs_pathconf, 100 .vop_print = smbfs_print, 101 .vop_putpages = smbfs_putpages, 102 .vop_read = smbfs_read, 103 .vop_readdir = smbfs_readdir, 104 .vop_reclaim = smbfs_reclaim, 105 .vop_remove = smbfs_remove, 106 .vop_rename = smbfs_rename, 107 .vop_rmdir = smbfs_rmdir, 108 .vop_setattr = smbfs_setattr, 109 /* .vop_setextattr = smbfs_setextattr,*/ 110 .vop_strategy = smbfs_strategy, 111 .vop_symlink = smbfs_symlink, 112 .vop_write = smbfs_write, 113 }; 114 VFS_VOP_VECTOR_REGISTER(smbfs_vnodeops); 115 116 static int 117 smbfs_access(struct vop_access_args *ap) 118 { 119 struct vnode *vp = ap->a_vp; 120 accmode_t accmode = ap->a_accmode; 121 mode_t mpmode; 122 struct smbmount *smp = VTOSMBFS(vp); 123 124 SMBVDEBUG("\n"); 125 if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) { 126 switch (vp->v_type) { 127 case VREG: case VDIR: case VLNK: 128 return EROFS; 129 default: 130 break; 131 } 132 } 133 mpmode = vp->v_type == VREG ? smp->sm_file_mode : smp->sm_dir_mode; 134 return (vaccess(vp->v_type, mpmode, smp->sm_uid, 135 smp->sm_gid, ap->a_accmode, ap->a_cred)); 136 } 137 138 /* ARGSUSED */ 139 static int 140 smbfs_open(struct vop_open_args *ap) 141 { 142 struct vnode *vp = ap->a_vp; 143 struct smbnode *np = VTOSMB(vp); 144 struct smb_cred *scred; 145 struct vattr vattr; 146 int mode = ap->a_mode; 147 int error, accmode; 148 149 SMBVDEBUG("%s,%d\n", np->n_name, (np->n_flag & NOPEN) != 0); 150 if (vp->v_type != VREG && vp->v_type != VDIR) { 151 SMBFSERR("open eacces vtype=%d\n", vp->v_type); 152 return EACCES; 153 } 154 if (vp->v_type == VDIR) { 155 np->n_flag |= NOPEN; 156 return 0; 157 } 158 if (np->n_flag & NMODIFIED) { 159 if ((error = smbfs_vinvalbuf(vp, ap->a_td)) == EINTR) 160 return error; 161 smbfs_attr_cacheremove(vp); 162 error = VOP_GETATTR(vp, &vattr, ap->a_cred); 163 if (error) 164 return error; 165 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec; 166 } else { 167 error = VOP_GETATTR(vp, &vattr, ap->a_cred); 168 if (error) 169 return error; 170 if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) { 171 error = smbfs_vinvalbuf(vp, ap->a_td); 172 if (error == EINTR) 173 return error; 174 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec; 175 } 176 } 177 if ((np->n_flag & NOPEN) != 0) 178 return 0; 179 /* 180 * Use DENYNONE to give unixy semantics of permitting 181 * everything not forbidden by permissions. Ie denial 182 * is up to server with clients/openers needing to use 183 * advisory locks for further control. 184 */ 185 accmode = SMB_SM_DENYNONE|SMB_AM_OPENREAD; 186 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) 187 accmode = SMB_SM_DENYNONE|SMB_AM_OPENRW; 188 scred = smbfs_malloc_scred(); 189 smb_makescred(scred, ap->a_td, ap->a_cred); 190 error = smbfs_smb_open(np, accmode, scred); 191 if (error) { 192 if (mode & FWRITE) 193 return EACCES; 194 else if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 195 accmode = SMB_SM_DENYNONE|SMB_AM_OPENREAD; 196 error = smbfs_smb_open(np, accmode, scred); 197 } 198 } 199 if (error == 0) { 200 np->n_flag |= NOPEN; 201 vnode_create_vobject(ap->a_vp, vattr.va_size, ap->a_td); 202 } 203 smbfs_attr_cacheremove(vp); 204 smbfs_free_scred(scred); 205 return error; 206 } 207 208 static int 209 smbfs_close(struct vop_close_args *ap) 210 { 211 struct vnode *vp = ap->a_vp; 212 struct thread *td = ap->a_td; 213 struct smbnode *np = VTOSMB(vp); 214 struct smb_cred *scred; 215 216 if (vp->v_type == VDIR && (np->n_flag & NOPEN) != 0 && 217 np->n_dirseq != NULL) { 218 scred = smbfs_malloc_scred(); 219 smb_makescred(scred, td, ap->a_cred); 220 smbfs_findclose(np->n_dirseq, scred); 221 smbfs_free_scred(scred); 222 np->n_dirseq = NULL; 223 } 224 return 0; 225 } 226 227 /* 228 * smbfs_getattr call from vfs. 229 */ 230 static int 231 smbfs_getattr(struct vop_getattr_args *ap) 232 { 233 struct vnode *vp = ap->a_vp; 234 struct smbnode *np = VTOSMB(vp); 235 struct vattr *va=ap->a_vap; 236 struct smbfattr fattr; 237 struct smb_cred *scred; 238 u_quad_t oldsize; 239 int error; 240 241 SMBVDEBUG("%lx: '%s' %d\n", (long)vp, np->n_name, (vp->v_vflag & VV_ROOT) != 0); 242 error = smbfs_attr_cachelookup(vp, va); 243 if (!error) 244 return 0; 245 SMBVDEBUG("not in the cache\n"); 246 scred = smbfs_malloc_scred(); 247 smb_makescred(scred, curthread, ap->a_cred); 248 oldsize = np->n_size; 249 error = smbfs_smb_lookup(np, NULL, 0, &fattr, scred); 250 if (error) { 251 SMBVDEBUG("error %d\n", error); 252 smbfs_free_scred(scred); 253 return error; 254 } 255 smbfs_attr_cacheenter(vp, &fattr); 256 smbfs_attr_cachelookup(vp, va); 257 if (np->n_flag & NOPEN) 258 np->n_size = oldsize; 259 smbfs_free_scred(scred); 260 return 0; 261 } 262 263 static int 264 smbfs_setattr(struct vop_setattr_args *ap) 265 { 266 struct vnode *vp = ap->a_vp; 267 struct smbnode *np = VTOSMB(vp); 268 struct vattr *vap = ap->a_vap; 269 struct timespec *mtime, *atime; 270 struct smb_cred *scred; 271 struct smb_share *ssp = np->n_mount->sm_share; 272 struct smb_vc *vcp = SSTOVC(ssp); 273 struct thread *td = curthread; 274 u_quad_t tsize = 0; 275 int isreadonly, doclose, error = 0; 276 int old_n_dosattr; 277 278 SMBVDEBUG("\n"); 279 isreadonly = (vp->v_mount->mnt_flag & MNT_RDONLY); 280 /* 281 * Disallow write attempts if the filesystem is mounted read-only. 282 */ 283 if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL || 284 vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL || 285 vap->va_mode != (mode_t)VNOVAL || vap->va_flags != VNOVAL) && 286 isreadonly) 287 return EROFS; 288 289 /* 290 * We only support setting four flags. Don't allow setting others. 291 * 292 * We map UF_READONLY to SMB_FA_RDONLY, unlike the MacOS X version 293 * of this code, which maps both UF_IMMUTABLE AND SF_IMMUTABLE to 294 * SMB_FA_RDONLY. The immutable flags have different semantics 295 * than readonly, which is the reason for the difference. 296 */ 297 if (vap->va_flags != VNOVAL) { 298 if (vap->va_flags & ~(UF_HIDDEN|UF_SYSTEM|UF_ARCHIVE| 299 UF_READONLY)) 300 return EINVAL; 301 } 302 303 scred = smbfs_malloc_scred(); 304 smb_makescred(scred, td, ap->a_cred); 305 if (vap->va_size != VNOVAL) { 306 switch (vp->v_type) { 307 case VDIR: 308 error = EISDIR; 309 goto out; 310 case VREG: 311 break; 312 default: 313 error = EINVAL; 314 goto out; 315 } 316 if (isreadonly) { 317 error = EROFS; 318 goto out; 319 } 320 doclose = 0; 321 vnode_pager_setsize(vp, (u_long)vap->va_size); 322 tsize = np->n_size; 323 np->n_size = vap->va_size; 324 if ((np->n_flag & NOPEN) == 0) { 325 error = smbfs_smb_open(np, 326 SMB_SM_DENYNONE|SMB_AM_OPENRW, 327 scred); 328 if (error == 0) 329 doclose = 1; 330 } 331 if (error == 0) 332 error = smbfs_smb_setfsize(np, 333 (int64_t)vap->va_size, scred); 334 if (doclose) 335 smbfs_smb_close(ssp, np->n_fid, NULL, scred); 336 if (error) { 337 np->n_size = tsize; 338 vnode_pager_setsize(vp, (u_long)tsize); 339 goto out; 340 } 341 } 342 if ((vap->va_flags != VNOVAL) || (vap->va_mode != (mode_t)VNOVAL)) { 343 old_n_dosattr = np->n_dosattr; 344 345 if (vap->va_mode != (mode_t)VNOVAL) { 346 if (vap->va_mode & S_IWUSR) 347 np->n_dosattr &= ~SMB_FA_RDONLY; 348 else 349 np->n_dosattr |= SMB_FA_RDONLY; 350 } 351 352 if (vap->va_flags != VNOVAL) { 353 if (vap->va_flags & UF_HIDDEN) 354 np->n_dosattr |= SMB_FA_HIDDEN; 355 else 356 np->n_dosattr &= ~SMB_FA_HIDDEN; 357 358 if (vap->va_flags & UF_SYSTEM) 359 np->n_dosattr |= SMB_FA_SYSTEM; 360 else 361 np->n_dosattr &= ~SMB_FA_SYSTEM; 362 363 if (vap->va_flags & UF_ARCHIVE) 364 np->n_dosattr |= SMB_FA_ARCHIVE; 365 else 366 np->n_dosattr &= ~SMB_FA_ARCHIVE; 367 368 /* 369 * We only support setting the immutable / readonly 370 * bit for regular files. According to comments in 371 * the MacOS X version of this code, supporting the 372 * readonly bit on directories doesn't do the same 373 * thing in Windows as in Unix. 374 */ 375 if (vp->v_type == VREG) { 376 if (vap->va_flags & UF_READONLY) 377 np->n_dosattr |= SMB_FA_RDONLY; 378 else 379 np->n_dosattr &= ~SMB_FA_RDONLY; 380 } 381 } 382 383 if (np->n_dosattr != old_n_dosattr) { 384 error = smbfs_smb_setpattr(np, np->n_dosattr, NULL, scred); 385 if (error) 386 goto out; 387 } 388 } 389 mtime = atime = NULL; 390 if (vap->va_mtime.tv_sec != VNOVAL) 391 mtime = &vap->va_mtime; 392 if (vap->va_atime.tv_sec != VNOVAL) 393 atime = &vap->va_atime; 394 if (mtime != atime) { 395 if (vap->va_vaflags & VA_UTIMES_NULL) { 396 error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td); 397 if (error) 398 error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td); 399 } else 400 error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td); 401 #if 0 402 if (mtime == NULL) 403 mtime = &np->n_mtime; 404 if (atime == NULL) 405 atime = &np->n_atime; 406 #endif 407 /* 408 * If file is opened, then we can use handle based calls. 409 * If not, use path based ones. 410 */ 411 if ((np->n_flag & NOPEN) == 0) { 412 if (vcp->vc_flags & SMBV_WIN95) { 413 error = VOP_OPEN(vp, FWRITE, ap->a_cred, td, 414 NULL); 415 if (!error) { 416 /* error = smbfs_smb_setfattrNT(np, 0, 417 mtime, atime, scred); 418 VOP_GETATTR(vp, &vattr, ap->a_cred); */ 419 if (mtime) 420 np->n_mtime = *mtime; 421 VOP_CLOSE(vp, FWRITE, ap->a_cred, td); 422 } 423 } else if ((vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS)) { 424 error = smbfs_smb_setptime2(np, mtime, atime, 0, scred); 425 /* error = smbfs_smb_setpattrNT(np, 0, mtime, atime, scred);*/ 426 } else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN2_0) { 427 error = smbfs_smb_setptime2(np, mtime, atime, 0, scred); 428 } else { 429 error = smbfs_smb_setpattr(np, 0, mtime, scred); 430 } 431 } else { 432 if (vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS) { 433 error = smbfs_smb_setfattrNT(np, 0, mtime, atime, scred); 434 } else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN1_0) { 435 error = smbfs_smb_setftime(np, mtime, atime, scred); 436 } else { 437 /* 438 * I have no idea how to handle this for core 439 * level servers. The possible solution is to 440 * update mtime after file is closed. 441 */ 442 SMBERROR("can't update times on an opened file\n"); 443 } 444 } 445 } 446 /* 447 * Invalidate attribute cache in case if server doesn't set 448 * required attributes. 449 */ 450 smbfs_attr_cacheremove(vp); /* invalidate cache */ 451 VOP_GETATTR(vp, vap, ap->a_cred); 452 np->n_mtime.tv_sec = vap->va_mtime.tv_sec; 453 out: 454 smbfs_free_scred(scred); 455 return error; 456 } 457 /* 458 * smbfs_read call. 459 */ 460 static int 461 smbfs_read(struct vop_read_args *ap) 462 { 463 struct vnode *vp = ap->a_vp; 464 struct uio *uio = ap->a_uio; 465 466 SMBVDEBUG("\n"); 467 if (vp->v_type != VREG && vp->v_type != VDIR) 468 return EPERM; 469 return smbfs_readvnode(vp, uio, ap->a_cred, NULL); 470 } 471 472 static int 473 smbfs_write(struct vop_write_args *ap) 474 { 475 struct vnode *vp = ap->a_vp; 476 struct uio *uio = ap->a_uio; 477 478 SMBVDEBUG("%d,ofs=%jd,sz=%zd\n",vp->v_type, (intmax_t)uio->uio_offset, 479 uio->uio_resid); 480 if (vp->v_type != VREG) 481 return (EPERM); 482 return smbfs_writevnode(vp, uio, ap->a_cred,ap->a_ioflag); 483 } 484 /* 485 * smbfs_create call 486 * Create a regular file. On entry the directory to contain the file being 487 * created is locked. We must release before we return. We must also free 488 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error. 489 */ 490 static int 491 smbfs_create(struct vop_create_args *ap) 492 { 493 struct vnode *dvp = ap->a_dvp; 494 struct vattr *vap = ap->a_vap; 495 struct vnode **vpp=ap->a_vpp; 496 struct componentname *cnp = ap->a_cnp; 497 struct smbnode *dnp = VTOSMB(dvp); 498 struct vnode *vp; 499 struct vattr vattr; 500 struct smbfattr fattr; 501 struct smb_cred *scred; 502 char *name = cnp->cn_nameptr; 503 int nmlen = cnp->cn_namelen; 504 int error; 505 506 SMBVDEBUG("\n"); 507 *vpp = NULL; 508 if (vap->va_type != VREG) 509 return EOPNOTSUPP; 510 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred))) 511 return error; 512 scred = smbfs_malloc_scred(); 513 smb_makescred(scred, curthread, cnp->cn_cred); 514 515 error = smbfs_smb_create(dnp, name, nmlen, scred); 516 if (error) 517 goto out; 518 error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, scred); 519 if (error) 520 goto out; 521 error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, &vp); 522 if (error) 523 goto out; 524 *vpp = vp; 525 if (cnp->cn_flags & MAKEENTRY) 526 cache_enter(dvp, vp, cnp); 527 out: 528 smbfs_free_scred(scred); 529 return error; 530 } 531 532 static int 533 smbfs_remove(struct vop_remove_args *ap) 534 { 535 struct vnode *vp = ap->a_vp; 536 /* struct vnode *dvp = ap->a_dvp;*/ 537 struct componentname *cnp = ap->a_cnp; 538 struct smbnode *np = VTOSMB(vp); 539 struct smb_cred *scred; 540 int error; 541 542 if (vp->v_type == VDIR || (np->n_flag & NOPEN) != 0 || vrefcnt(vp) != 1) 543 return EPERM; 544 scred = smbfs_malloc_scred(); 545 smb_makescred(scred, curthread, cnp->cn_cred); 546 error = smbfs_smb_delete(np, scred); 547 if (error == 0) 548 np->n_flag |= NGONE; 549 cache_purge(vp); 550 smbfs_free_scred(scred); 551 return error; 552 } 553 554 /* 555 * smbfs_file rename call 556 */ 557 static int 558 smbfs_rename(struct vop_rename_args *ap) 559 { 560 struct vnode *fvp = ap->a_fvp; 561 struct vnode *tvp = ap->a_tvp; 562 struct vnode *fdvp = ap->a_fdvp; 563 struct vnode *tdvp = ap->a_tdvp; 564 struct componentname *tcnp = ap->a_tcnp; 565 /* struct componentname *fcnp = ap->a_fcnp;*/ 566 struct smb_cred *scred; 567 #ifdef notnow 568 u_int16_t flags = 6; 569 #endif 570 int error=0; 571 572 scred = NULL; 573 /* Check for cross-device rename */ 574 if ((fvp->v_mount != tdvp->v_mount) || 575 (tvp && (fvp->v_mount != tvp->v_mount))) { 576 error = EXDEV; 577 goto out; 578 } 579 580 if (tvp && vrefcnt(tvp) > 1) { 581 error = EBUSY; 582 goto out; 583 } 584 #ifdef notnow 585 flags = 0x10; /* verify all writes */ 586 #endif 587 if (fvp->v_type == VDIR) { 588 #ifdef notnow 589 flags |= 2; 590 #endif 591 } else if (fvp->v_type == VREG) { 592 #ifdef notnow 593 flags |= 1; 594 #endif 595 } else { 596 return EINVAL; 597 } 598 scred = smbfs_malloc_scred(); 599 smb_makescred(scred, curthread, tcnp->cn_cred); 600 /* 601 * It seems that Samba doesn't implement SMB_COM_MOVE call... 602 */ 603 #ifdef notnow 604 if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) { 605 error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp), 606 tcnp->cn_nameptr, tcnp->cn_namelen, flags, scred); 607 } else 608 #endif 609 { 610 /* 611 * We have to do the work atomicaly 612 */ 613 if (tvp && tvp != fvp) { 614 error = smbfs_smb_delete(VTOSMB(tvp), scred); 615 if (error) 616 goto out_cacherem; 617 VTOSMB(fvp)->n_flag |= NGONE; 618 } 619 error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp), 620 tcnp->cn_nameptr, tcnp->cn_namelen, scred); 621 } 622 623 if (fvp->v_type == VDIR) { 624 if (tvp != NULL && tvp->v_type == VDIR) 625 cache_purge(tdvp); 626 cache_purge(fdvp); 627 } 628 629 out_cacherem: 630 smbfs_attr_cacheremove(fdvp); 631 smbfs_attr_cacheremove(tdvp); 632 out: 633 smbfs_free_scred(scred); 634 if (tdvp == tvp) 635 vrele(tdvp); 636 else 637 vput(tdvp); 638 if (tvp) 639 vput(tvp); 640 vrele(fdvp); 641 vrele(fvp); 642 #ifdef possible_mistake 643 vgone(fvp); 644 if (tvp) 645 vgone(tvp); 646 #endif 647 return error; 648 } 649 650 /* 651 * somtime it will come true... 652 */ 653 static int 654 smbfs_link(struct vop_link_args *ap) 655 { 656 return EOPNOTSUPP; 657 } 658 659 /* 660 * smbfs_symlink link create call. 661 * Sometime it will be functional... 662 */ 663 static int 664 smbfs_symlink(struct vop_symlink_args *ap) 665 { 666 return EOPNOTSUPP; 667 } 668 669 static int 670 smbfs_mknod(struct vop_mknod_args *ap) 671 { 672 return EOPNOTSUPP; 673 } 674 675 static int 676 smbfs_mkdir(struct vop_mkdir_args *ap) 677 { 678 struct vnode *dvp = ap->a_dvp; 679 /* struct vattr *vap = ap->a_vap;*/ 680 struct vnode *vp; 681 struct componentname *cnp = ap->a_cnp; 682 struct smbnode *dnp = VTOSMB(dvp); 683 struct vattr vattr; 684 struct smb_cred *scred; 685 struct smbfattr fattr; 686 char *name = cnp->cn_nameptr; 687 int len = cnp->cn_namelen; 688 int error; 689 690 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred))) { 691 return error; 692 } 693 if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.')))) 694 return EEXIST; 695 scred = smbfs_malloc_scred(); 696 smb_makescred(scred, curthread, cnp->cn_cred); 697 error = smbfs_smb_mkdir(dnp, name, len, scred); 698 if (error) 699 goto out; 700 error = smbfs_smb_lookup(dnp, name, len, &fattr, scred); 701 if (error) 702 goto out; 703 error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp); 704 if (error) 705 goto out; 706 *ap->a_vpp = vp; 707 out: 708 smbfs_free_scred(scred); 709 return error; 710 } 711 712 /* 713 * smbfs_remove directory call 714 */ 715 static int 716 smbfs_rmdir(struct vop_rmdir_args *ap) 717 { 718 struct vnode *vp = ap->a_vp; 719 struct vnode *dvp = ap->a_dvp; 720 struct componentname *cnp = ap->a_cnp; 721 /* struct smbmount *smp = VTOSMBFS(vp);*/ 722 struct smbnode *dnp = VTOSMB(dvp); 723 struct smbnode *np = VTOSMB(vp); 724 struct smb_cred *scred; 725 int error; 726 727 if (dvp == vp) 728 return EINVAL; 729 730 scred = smbfs_malloc_scred(); 731 smb_makescred(scred, curthread, cnp->cn_cred); 732 error = smbfs_smb_rmdir(np, scred); 733 if (error == 0) 734 np->n_flag |= NGONE; 735 dnp->n_flag |= NMODIFIED; 736 smbfs_attr_cacheremove(dvp); 737 /* cache_purge(dvp);*/ 738 cache_purge(vp); 739 smbfs_free_scred(scred); 740 return error; 741 } 742 743 /* 744 * smbfs_readdir call 745 */ 746 static int 747 smbfs_readdir(struct vop_readdir_args *ap) 748 { 749 struct vnode *vp = ap->a_vp; 750 struct uio *uio = ap->a_uio; 751 752 if (vp->v_type != VDIR) 753 return (EPERM); 754 #ifdef notnow 755 if (ap->a_ncookies) { 756 printf("smbfs_readdir: no support for cookies now..."); 757 return (EOPNOTSUPP); 758 } 759 #endif 760 return (smbfs_readvnode(vp, uio, ap->a_cred, ap->a_eofflag)); 761 } 762 763 /* ARGSUSED */ 764 static int 765 smbfs_fsync(struct vop_fsync_args *ap) 766 { 767 /* return (smb_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/ 768 return (0); 769 } 770 771 static 772 int smbfs_print(struct vop_print_args *ap) 773 { 774 struct vnode *vp = ap->a_vp; 775 struct smbnode *np = VTOSMB(vp); 776 777 if (np == NULL) { 778 printf("no smbnode data\n"); 779 return (0); 780 } 781 printf("\tname = %s, parent = %p, open = %d\n", np->n_name, 782 np->n_parent ? np->n_parent : NULL, (np->n_flag & NOPEN) != 0); 783 return (0); 784 } 785 786 static int 787 smbfs_pathconf(struct vop_pathconf_args *ap) 788 { 789 struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp)); 790 struct smb_vc *vcp = SSTOVC(smp->sm_share); 791 long *retval = ap->a_retval; 792 int error = 0; 793 794 switch (ap->a_name) { 795 case _PC_FILESIZEBITS: 796 if (vcp->vc_sopt.sv_caps & (SMB_CAP_LARGE_READX | 797 SMB_CAP_LARGE_WRITEX)) 798 *retval = 64; 799 else 800 *retval = 32; 801 break; 802 case _PC_NAME_MAX: 803 *retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12; 804 break; 805 case _PC_PATH_MAX: 806 *retval = 800; /* XXX: a correct one ? */ 807 break; 808 case _PC_NO_TRUNC: 809 *retval = 1; 810 break; 811 case _PC_HAS_HIDDENSYSTEM: 812 *retval = 1; 813 break; 814 default: 815 error = vop_stdpathconf(ap); 816 } 817 return error; 818 } 819 820 static int 821 smbfs_strategy(struct vop_strategy_args *ap) 822 { 823 struct buf *bp=ap->a_bp; 824 struct ucred *cr; 825 struct thread *td; 826 827 SMBVDEBUG("\n"); 828 if (bp->b_flags & B_ASYNC) 829 td = (struct thread *)0; 830 else 831 td = curthread; /* XXX */ 832 if (bp->b_iocmd == BIO_READ) 833 cr = bp->b_rcred; 834 else 835 cr = bp->b_wcred; 836 837 if ((bp->b_flags & B_ASYNC) == 0 ) 838 (void)smbfs_doio(ap->a_vp, bp, cr, td); 839 return (0); 840 } 841 842 int 843 smbfs_ioctl(struct vop_ioctl_args *ap) 844 { 845 return ENOTTY; 846 } 847 848 static char smbfs_atl[] = "rhsvda"; 849 static int 850 smbfs_getextattr(struct vop_getextattr_args *ap) 851 /* { 852 IN struct vnode *a_vp; 853 IN char *a_name; 854 INOUT struct uio *a_uio; 855 IN struct ucred *a_cred; 856 IN struct thread *a_td; 857 }; 858 */ 859 { 860 struct vnode *vp = ap->a_vp; 861 struct thread *td = ap->a_td; 862 struct ucred *cred = ap->a_cred; 863 struct uio *uio = ap->a_uio; 864 const char *name = ap->a_name; 865 struct smbnode *np = VTOSMB(vp); 866 struct vattr vattr; 867 char buf[10]; 868 int i, attr, error; 869 870 error = VOP_ACCESS(vp, VREAD, cred, td); 871 if (error) 872 return error; 873 error = VOP_GETATTR(vp, &vattr, cred); 874 if (error) 875 return error; 876 if (strcmp(name, "dosattr") == 0) { 877 attr = np->n_dosattr; 878 for (i = 0; i < 6; i++, attr >>= 1) 879 buf[i] = (attr & 1) ? smbfs_atl[i] : '-'; 880 buf[i] = 0; 881 error = uiomove(buf, i, uio); 882 } else 883 error = EINVAL; 884 return error; 885 } 886 887 /* 888 * Since we expected to support F_GETLK (and SMB protocol has no such function), 889 * it is necessary to use lf_advlock(). It would be nice if this function had 890 * a callback mechanism because it will help to improve a level of consistency. 891 */ 892 int 893 smbfs_advlock(struct vop_advlock_args *ap) 894 { 895 struct vnode *vp = ap->a_vp; 896 struct smbnode *np = VTOSMB(vp); 897 struct flock *fl = ap->a_fl; 898 caddr_t id = (caddr_t)1 /* ap->a_id */; 899 /* int flags = ap->a_flags;*/ 900 struct thread *td = curthread; 901 struct smb_cred *scred; 902 u_quad_t size; 903 off_t start, end, oadd; 904 int error, lkop; 905 906 if (vp->v_type == VDIR) { 907 /* 908 * SMB protocol have no support for directory locking. 909 * Although locks can be processed on local machine, I don't 910 * think that this is a good idea, because some programs 911 * can work wrong assuming directory is locked. So, we just 912 * return 'operation not supported 913 */ 914 return EOPNOTSUPP; 915 } 916 size = np->n_size; 917 switch (fl->l_whence) { 918 case SEEK_SET: 919 case SEEK_CUR: 920 start = fl->l_start; 921 break; 922 923 case SEEK_END: 924 if (size > OFF_MAX || 925 (fl->l_start > 0 && size > OFF_MAX - fl->l_start)) 926 return EOVERFLOW; 927 start = size + fl->l_start; 928 break; 929 930 default: 931 return EINVAL; 932 } 933 if (start < 0) 934 return EINVAL; 935 if (fl->l_len < 0) { 936 if (start == 0) 937 return EINVAL; 938 end = start - 1; 939 start += fl->l_len; 940 if (start < 0) 941 return EINVAL; 942 } else if (fl->l_len == 0) 943 end = -1; 944 else { 945 oadd = fl->l_len - 1; 946 if (oadd > OFF_MAX - start) 947 return EOVERFLOW; 948 end = start + oadd; 949 } 950 scred = smbfs_malloc_scred(); 951 smb_makescred(scred, td, td->td_ucred); 952 switch (ap->a_op) { 953 case F_SETLK: 954 switch (fl->l_type) { 955 case F_WRLCK: 956 lkop = SMB_LOCK_EXCL; 957 break; 958 case F_RDLCK: 959 lkop = SMB_LOCK_SHARED; 960 break; 961 case F_UNLCK: 962 lkop = SMB_LOCK_RELEASE; 963 break; 964 default: 965 smbfs_free_scred(scred); 966 return EINVAL; 967 } 968 error = lf_advlock(ap, &vp->v_lockf, size); 969 if (error) 970 break; 971 lkop = SMB_LOCK_EXCL; 972 error = smbfs_smb_lock(np, lkop, id, start, end, scred); 973 if (error) { 974 int oldtype = fl->l_type; 975 fl->l_type = F_UNLCK; 976 ap->a_op = F_UNLCK; 977 lf_advlock(ap, &vp->v_lockf, size); 978 fl->l_type = oldtype; 979 } 980 break; 981 case F_UNLCK: 982 lf_advlock(ap, &vp->v_lockf, size); 983 error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, scred); 984 break; 985 case F_GETLK: 986 error = lf_advlock(ap, &vp->v_lockf, size); 987 break; 988 default: 989 smbfs_free_scred(scred); 990 return EINVAL; 991 } 992 smbfs_free_scred(scred); 993 return error; 994 } 995 996 static int 997 smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop) 998 { 999 static const char *badchars = "*/:<>?"; 1000 static const char *badchars83 = " +|,[]=;"; 1001 const char *cp; 1002 int i, error; 1003 1004 /* 1005 * Backslash characters, being a path delimiter, are prohibited 1006 * within a path component even for LOOKUP operations. 1007 */ 1008 if (strchr(name, '\\') != NULL) 1009 return ENOENT; 1010 1011 if (nameiop == LOOKUP) 1012 return 0; 1013 error = ENOENT; 1014 if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) { 1015 /* 1016 * Name should conform 8.3 format 1017 */ 1018 if (nmlen > 12) 1019 return ENAMETOOLONG; 1020 cp = strchr(name, '.'); 1021 if (cp == NULL) 1022 return error; 1023 if (cp == name || (cp - name) > 8) 1024 return error; 1025 cp = strchr(cp + 1, '.'); 1026 if (cp != NULL) 1027 return error; 1028 for (cp = name, i = 0; i < nmlen; i++, cp++) 1029 if (strchr(badchars83, *cp) != NULL) 1030 return error; 1031 } 1032 for (cp = name, i = 0; i < nmlen; i++, cp++) 1033 if (strchr(badchars, *cp) != NULL) 1034 return error; 1035 return 0; 1036 } 1037 1038 /* 1039 * Things go even weird without fixed inode numbers... 1040 */ 1041 int 1042 smbfs_lookup(struct vop_lookup_args *ap) 1043 { 1044 struct componentname *cnp = ap->a_cnp; 1045 struct thread *td = curthread; 1046 struct vnode *dvp = ap->a_dvp; 1047 struct vnode **vpp = ap->a_vpp; 1048 struct vnode *vp; 1049 struct smbmount *smp; 1050 struct mount *mp = dvp->v_mount; 1051 struct smbnode *dnp; 1052 struct smbfattr fattr, *fap; 1053 struct smb_cred *scred; 1054 char *name = cnp->cn_nameptr; 1055 uint64_t flags = cnp->cn_flags; 1056 int nameiop = cnp->cn_nameiop; 1057 int nmlen = cnp->cn_namelen; 1058 int error, islastcn, isdot; 1059 int killit; 1060 1061 SMBVDEBUG("\n"); 1062 if (dvp->v_type != VDIR) 1063 return ENOTDIR; 1064 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) { 1065 SMBFSERR("invalid '..'\n"); 1066 return EIO; 1067 } 1068 islastcn = flags & ISLASTCN; 1069 if (islastcn && (mp->mnt_flag & MNT_RDONLY) && (nameiop != LOOKUP)) 1070 return EROFS; 1071 error = vn_dir_check_exec(dvp, cnp); 1072 if (error != 0) 1073 return error; 1074 smp = VFSTOSMBFS(mp); 1075 dnp = VTOSMB(dvp); 1076 isdot = (nmlen == 1 && name[0] == '.'); 1077 1078 error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop); 1079 1080 if (error) 1081 return ENOENT; 1082 1083 error = cache_lookup(dvp, vpp, cnp, NULL, NULL); 1084 SMBVDEBUG("cache_lookup returned %d\n", error); 1085 if (error > 0) 1086 return error; 1087 if (error) { /* name was found */ 1088 struct vattr vattr; 1089 1090 killit = 0; 1091 vp = *vpp; 1092 error = VOP_GETATTR(vp, &vattr, cnp->cn_cred); 1093 /* 1094 * If the file type on the server is inconsistent 1095 * with what it was when we created the vnode, 1096 * kill the bogus vnode now and fall through to 1097 * the code below to create a new one with the 1098 * right type. 1099 */ 1100 if (error == 0 && 1101 ((vp->v_type == VDIR && 1102 (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) == 0) || 1103 (vp->v_type == VREG && 1104 (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) != 0))) 1105 killit = 1; 1106 else if (error == 0 1107 /* && vattr.va_ctime.tv_sec == VTOSMB(vp)->n_ctime*/) { 1108 SMBVDEBUG("use cached vnode\n"); 1109 return (0); 1110 } 1111 cache_purge(vp); 1112 /* 1113 * XXX This is not quite right, if '.' is 1114 * inconsistent, we really need to start the lookup 1115 * all over again. Hopefully there is some other 1116 * guarantee that prevents this case from happening. 1117 */ 1118 if (killit && vp != dvp) 1119 vgone(vp); 1120 if (vp != dvp) 1121 vput(vp); 1122 else 1123 vrele(vp); 1124 *vpp = NULLVP; 1125 } 1126 /* 1127 * entry is not in the cache or has been expired 1128 */ 1129 error = 0; 1130 *vpp = NULLVP; 1131 scred = smbfs_malloc_scred(); 1132 smb_makescred(scred, td, cnp->cn_cred); 1133 fap = &fattr; 1134 if (flags & ISDOTDOT) { 1135 /* 1136 * In the DOTDOT case, don't go over-the-wire 1137 * in order to request attributes. We already 1138 * know it's a directory and subsequent call to 1139 * smbfs_getattr() will restore consistency. 1140 * 1141 */ 1142 SMBVDEBUG("smbfs_smb_lookup: dotdot\n"); 1143 } else if (isdot) { 1144 error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred); 1145 SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error); 1146 } 1147 else { 1148 error = smbfs_smb_lookup(dnp, name, nmlen, fap, scred); 1149 SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error); 1150 } 1151 if (error && error != ENOENT) 1152 goto out; 1153 if (error) { /* entry not found */ 1154 /* 1155 * Handle RENAME or CREATE case... 1156 */ 1157 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) { 1158 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 1159 if (error) 1160 goto out; 1161 error = EJUSTRETURN; 1162 goto out; 1163 } 1164 error = ENOENT; 1165 goto out; 1166 }/* else { 1167 SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum); 1168 }*/ 1169 /* 1170 * handle DELETE case ... 1171 */ 1172 if (nameiop == DELETE && islastcn) { /* delete last component */ 1173 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 1174 if (error) 1175 goto out; 1176 if (isdot) { 1177 VREF(dvp); 1178 *vpp = dvp; 1179 goto out; 1180 } 1181 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp); 1182 if (error) 1183 goto out; 1184 *vpp = vp; 1185 goto out; 1186 } 1187 if (nameiop == RENAME && islastcn) { 1188 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 1189 if (error) 1190 goto out; 1191 if (isdot) { 1192 error = EISDIR; 1193 goto out; 1194 } 1195 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp); 1196 if (error) 1197 goto out; 1198 *vpp = vp; 1199 goto out; 1200 } 1201 if (flags & ISDOTDOT) { 1202 mp = dvp->v_mount; 1203 error = vfs_busy(mp, MBF_NOWAIT); 1204 if (error != 0) { 1205 vfs_ref(mp); 1206 VOP_UNLOCK(dvp); 1207 error = vfs_busy(mp, 0); 1208 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1209 vfs_rel(mp); 1210 if (error) { 1211 error = ENOENT; 1212 goto out; 1213 } 1214 if (VN_IS_DOOMED(dvp)) { 1215 vfs_unbusy(mp); 1216 error = ENOENT; 1217 goto out; 1218 } 1219 } 1220 VOP_UNLOCK(dvp); 1221 error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp); 1222 vfs_unbusy(mp); 1223 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1224 if (VN_IS_DOOMED(dvp)) { 1225 if (error == 0) 1226 vput(vp); 1227 error = ENOENT; 1228 } 1229 if (error) 1230 goto out; 1231 *vpp = vp; 1232 } else if (isdot) { 1233 vref(dvp); 1234 *vpp = dvp; 1235 } else { 1236 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp); 1237 if (error) 1238 goto out; 1239 *vpp = vp; 1240 SMBVDEBUG("lookup: getnewvp!\n"); 1241 } 1242 if ((cnp->cn_flags & MAKEENTRY)/* && !islastcn*/) { 1243 /* VTOSMB(*vpp)->n_ctime = VTOSMB(*vpp)->n_vattr.va_ctime.tv_sec;*/ 1244 cache_enter(dvp, *vpp, cnp); 1245 } 1246 out: 1247 smbfs_free_scred(scred); 1248 return (error); 1249 } 1250