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); 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 int error; 752 753 if (vp->v_type != VDIR) 754 return (EPERM); 755 #ifdef notnow 756 if (ap->a_ncookies) { 757 printf("smbfs_readdir: no support for cookies now..."); 758 return (EOPNOTSUPP); 759 } 760 #endif 761 error = smbfs_readvnode(vp, uio, ap->a_cred); 762 return error; 763 } 764 765 /* ARGSUSED */ 766 static int 767 smbfs_fsync(struct vop_fsync_args *ap) 768 { 769 /* return (smb_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/ 770 return (0); 771 } 772 773 static 774 int smbfs_print(struct vop_print_args *ap) 775 { 776 struct vnode *vp = ap->a_vp; 777 struct smbnode *np = VTOSMB(vp); 778 779 if (np == NULL) { 780 printf("no smbnode data\n"); 781 return (0); 782 } 783 printf("\tname = %s, parent = %p, open = %d\n", np->n_name, 784 np->n_parent ? np->n_parent : NULL, (np->n_flag & NOPEN) != 0); 785 return (0); 786 } 787 788 static int 789 smbfs_pathconf(struct vop_pathconf_args *ap) 790 { 791 struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp)); 792 struct smb_vc *vcp = SSTOVC(smp->sm_share); 793 long *retval = ap->a_retval; 794 int error = 0; 795 796 switch (ap->a_name) { 797 case _PC_FILESIZEBITS: 798 if (vcp->vc_sopt.sv_caps & (SMB_CAP_LARGE_READX | 799 SMB_CAP_LARGE_WRITEX)) 800 *retval = 64; 801 else 802 *retval = 32; 803 break; 804 case _PC_NAME_MAX: 805 *retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12; 806 break; 807 case _PC_PATH_MAX: 808 *retval = 800; /* XXX: a correct one ? */ 809 break; 810 case _PC_NO_TRUNC: 811 *retval = 1; 812 break; 813 default: 814 error = vop_stdpathconf(ap); 815 } 816 return error; 817 } 818 819 static int 820 smbfs_strategy(struct vop_strategy_args *ap) 821 { 822 struct buf *bp=ap->a_bp; 823 struct ucred *cr; 824 struct thread *td; 825 826 SMBVDEBUG("\n"); 827 if (bp->b_flags & B_ASYNC) 828 td = (struct thread *)0; 829 else 830 td = curthread; /* XXX */ 831 if (bp->b_iocmd == BIO_READ) 832 cr = bp->b_rcred; 833 else 834 cr = bp->b_wcred; 835 836 if ((bp->b_flags & B_ASYNC) == 0 ) 837 (void)smbfs_doio(ap->a_vp, bp, cr, td); 838 return (0); 839 } 840 841 int 842 smbfs_ioctl(struct vop_ioctl_args *ap) 843 { 844 return ENOTTY; 845 } 846 847 static char smbfs_atl[] = "rhsvda"; 848 static int 849 smbfs_getextattr(struct vop_getextattr_args *ap) 850 /* { 851 IN struct vnode *a_vp; 852 IN char *a_name; 853 INOUT struct uio *a_uio; 854 IN struct ucred *a_cred; 855 IN struct thread *a_td; 856 }; 857 */ 858 { 859 struct vnode *vp = ap->a_vp; 860 struct thread *td = ap->a_td; 861 struct ucred *cred = ap->a_cred; 862 struct uio *uio = ap->a_uio; 863 const char *name = ap->a_name; 864 struct smbnode *np = VTOSMB(vp); 865 struct vattr vattr; 866 char buf[10]; 867 int i, attr, error; 868 869 error = VOP_ACCESS(vp, VREAD, cred, td); 870 if (error) 871 return error; 872 error = VOP_GETATTR(vp, &vattr, cred); 873 if (error) 874 return error; 875 if (strcmp(name, "dosattr") == 0) { 876 attr = np->n_dosattr; 877 for (i = 0; i < 6; i++, attr >>= 1) 878 buf[i] = (attr & 1) ? smbfs_atl[i] : '-'; 879 buf[i] = 0; 880 error = uiomove(buf, i, uio); 881 } else 882 error = EINVAL; 883 return error; 884 } 885 886 /* 887 * Since we expected to support F_GETLK (and SMB protocol has no such function), 888 * it is necessary to use lf_advlock(). It would be nice if this function had 889 * a callback mechanism because it will help to improve a level of consistency. 890 */ 891 int 892 smbfs_advlock(struct vop_advlock_args *ap) 893 { 894 struct vnode *vp = ap->a_vp; 895 struct smbnode *np = VTOSMB(vp); 896 struct flock *fl = ap->a_fl; 897 caddr_t id = (caddr_t)1 /* ap->a_id */; 898 /* int flags = ap->a_flags;*/ 899 struct thread *td = curthread; 900 struct smb_cred *scred; 901 u_quad_t size; 902 off_t start, end, oadd; 903 int error, lkop; 904 905 if (vp->v_type == VDIR) { 906 /* 907 * SMB protocol have no support for directory locking. 908 * Although locks can be processed on local machine, I don't 909 * think that this is a good idea, because some programs 910 * can work wrong assuming directory is locked. So, we just 911 * return 'operation not supported 912 */ 913 return EOPNOTSUPP; 914 } 915 size = np->n_size; 916 switch (fl->l_whence) { 917 case SEEK_SET: 918 case SEEK_CUR: 919 start = fl->l_start; 920 break; 921 922 case SEEK_END: 923 if (size > OFF_MAX || 924 (fl->l_start > 0 && size > OFF_MAX - fl->l_start)) 925 return EOVERFLOW; 926 start = size + fl->l_start; 927 break; 928 929 default: 930 return EINVAL; 931 } 932 if (start < 0) 933 return EINVAL; 934 if (fl->l_len < 0) { 935 if (start == 0) 936 return EINVAL; 937 end = start - 1; 938 start += fl->l_len; 939 if (start < 0) 940 return EINVAL; 941 } else if (fl->l_len == 0) 942 end = -1; 943 else { 944 oadd = fl->l_len - 1; 945 if (oadd > OFF_MAX - start) 946 return EOVERFLOW; 947 end = start + oadd; 948 } 949 scred = smbfs_malloc_scred(); 950 smb_makescred(scred, td, td->td_ucred); 951 switch (ap->a_op) { 952 case F_SETLK: 953 switch (fl->l_type) { 954 case F_WRLCK: 955 lkop = SMB_LOCK_EXCL; 956 break; 957 case F_RDLCK: 958 lkop = SMB_LOCK_SHARED; 959 break; 960 case F_UNLCK: 961 lkop = SMB_LOCK_RELEASE; 962 break; 963 default: 964 smbfs_free_scred(scred); 965 return EINVAL; 966 } 967 error = lf_advlock(ap, &vp->v_lockf, size); 968 if (error) 969 break; 970 lkop = SMB_LOCK_EXCL; 971 error = smbfs_smb_lock(np, lkop, id, start, end, scred); 972 if (error) { 973 int oldtype = fl->l_type; 974 fl->l_type = F_UNLCK; 975 ap->a_op = F_UNLCK; 976 lf_advlock(ap, &vp->v_lockf, size); 977 fl->l_type = oldtype; 978 } 979 break; 980 case F_UNLCK: 981 lf_advlock(ap, &vp->v_lockf, size); 982 error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, scred); 983 break; 984 case F_GETLK: 985 error = lf_advlock(ap, &vp->v_lockf, size); 986 break; 987 default: 988 smbfs_free_scred(scred); 989 return EINVAL; 990 } 991 smbfs_free_scred(scred); 992 return error; 993 } 994 995 static int 996 smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop) 997 { 998 static const char *badchars = "*/:<>?"; 999 static const char *badchars83 = " +|,[]=;"; 1000 const char *cp; 1001 int i, error; 1002 1003 /* 1004 * Backslash characters, being a path delimiter, are prohibited 1005 * within a path component even for LOOKUP operations. 1006 */ 1007 if (strchr(name, '\\') != NULL) 1008 return ENOENT; 1009 1010 if (nameiop == LOOKUP) 1011 return 0; 1012 error = ENOENT; 1013 if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) { 1014 /* 1015 * Name should conform 8.3 format 1016 */ 1017 if (nmlen > 12) 1018 return ENAMETOOLONG; 1019 cp = strchr(name, '.'); 1020 if (cp == NULL) 1021 return error; 1022 if (cp == name || (cp - name) > 8) 1023 return error; 1024 cp = strchr(cp + 1, '.'); 1025 if (cp != NULL) 1026 return error; 1027 for (cp = name, i = 0; i < nmlen; i++, cp++) 1028 if (strchr(badchars83, *cp) != NULL) 1029 return error; 1030 } 1031 for (cp = name, i = 0; i < nmlen; i++, cp++) 1032 if (strchr(badchars, *cp) != NULL) 1033 return error; 1034 return 0; 1035 } 1036 1037 /* 1038 * Things go even weird without fixed inode numbers... 1039 */ 1040 int 1041 smbfs_lookup(struct vop_lookup_args *ap) 1042 { 1043 struct componentname *cnp = ap->a_cnp; 1044 struct thread *td = curthread; 1045 struct vnode *dvp = ap->a_dvp; 1046 struct vnode **vpp = ap->a_vpp; 1047 struct vnode *vp; 1048 struct smbmount *smp; 1049 struct mount *mp = dvp->v_mount; 1050 struct smbnode *dnp; 1051 struct smbfattr fattr, *fap; 1052 struct smb_cred *scred; 1053 char *name = cnp->cn_nameptr; 1054 int flags = cnp->cn_flags; 1055 int nameiop = cnp->cn_nameiop; 1056 int nmlen = cnp->cn_namelen; 1057 int error, islastcn, isdot; 1058 int killit; 1059 1060 SMBVDEBUG("\n"); 1061 if (dvp->v_type != VDIR) 1062 return ENOTDIR; 1063 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) { 1064 SMBFSERR("invalid '..'\n"); 1065 return EIO; 1066 } 1067 islastcn = flags & ISLASTCN; 1068 if (islastcn && (mp->mnt_flag & MNT_RDONLY) && (nameiop != LOOKUP)) 1069 return EROFS; 1070 error = vn_dir_check_exec(dvp, cnp); 1071 if (error != 0) 1072 return error; 1073 smp = VFSTOSMBFS(mp); 1074 dnp = VTOSMB(dvp); 1075 isdot = (nmlen == 1 && name[0] == '.'); 1076 1077 error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop); 1078 1079 if (error) 1080 return ENOENT; 1081 1082 error = cache_lookup(dvp, vpp, cnp, NULL, NULL); 1083 SMBVDEBUG("cache_lookup returned %d\n", error); 1084 if (error > 0) 1085 return error; 1086 if (error) { /* name was found */ 1087 struct vattr vattr; 1088 1089 killit = 0; 1090 vp = *vpp; 1091 error = VOP_GETATTR(vp, &vattr, cnp->cn_cred); 1092 /* 1093 * If the file type on the server is inconsistent 1094 * with what it was when we created the vnode, 1095 * kill the bogus vnode now and fall through to 1096 * the code below to create a new one with the 1097 * right type. 1098 */ 1099 if (error == 0 && 1100 ((vp->v_type == VDIR && 1101 (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) == 0) || 1102 (vp->v_type == VREG && 1103 (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) != 0))) 1104 killit = 1; 1105 else if (error == 0 1106 /* && vattr.va_ctime.tv_sec == VTOSMB(vp)->n_ctime*/) { 1107 SMBVDEBUG("use cached vnode\n"); 1108 return (0); 1109 } 1110 cache_purge(vp); 1111 /* 1112 * XXX This is not quite right, if '.' is 1113 * inconsistent, we really need to start the lookup 1114 * all over again. Hopefully there is some other 1115 * guarantee that prevents this case from happening. 1116 */ 1117 if (killit && vp != dvp) 1118 vgone(vp); 1119 if (vp != dvp) 1120 vput(vp); 1121 else 1122 vrele(vp); 1123 *vpp = NULLVP; 1124 } 1125 /* 1126 * entry is not in the cache or has been expired 1127 */ 1128 error = 0; 1129 *vpp = NULLVP; 1130 scred = smbfs_malloc_scred(); 1131 smb_makescred(scred, td, cnp->cn_cred); 1132 fap = &fattr; 1133 if (flags & ISDOTDOT) { 1134 /* 1135 * In the DOTDOT case, don't go over-the-wire 1136 * in order to request attributes. We already 1137 * know it's a directory and subsequent call to 1138 * smbfs_getattr() will restore consistency. 1139 * 1140 */ 1141 SMBVDEBUG("smbfs_smb_lookup: dotdot\n"); 1142 } else if (isdot) { 1143 error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred); 1144 SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error); 1145 } 1146 else { 1147 error = smbfs_smb_lookup(dnp, name, nmlen, fap, scred); 1148 SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error); 1149 } 1150 if (error && error != ENOENT) 1151 goto out; 1152 if (error) { /* entry not found */ 1153 /* 1154 * Handle RENAME or CREATE case... 1155 */ 1156 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) { 1157 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 1158 if (error) 1159 goto out; 1160 error = EJUSTRETURN; 1161 goto out; 1162 } 1163 error = ENOENT; 1164 goto out; 1165 }/* else { 1166 SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum); 1167 }*/ 1168 /* 1169 * handle DELETE case ... 1170 */ 1171 if (nameiop == DELETE && islastcn) { /* delete last component */ 1172 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 1173 if (error) 1174 goto out; 1175 if (isdot) { 1176 VREF(dvp); 1177 *vpp = dvp; 1178 goto out; 1179 } 1180 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp); 1181 if (error) 1182 goto out; 1183 *vpp = vp; 1184 goto out; 1185 } 1186 if (nameiop == RENAME && islastcn) { 1187 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td); 1188 if (error) 1189 goto out; 1190 if (isdot) { 1191 error = EISDIR; 1192 goto out; 1193 } 1194 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp); 1195 if (error) 1196 goto out; 1197 *vpp = vp; 1198 goto out; 1199 } 1200 if (flags & ISDOTDOT) { 1201 mp = dvp->v_mount; 1202 error = vfs_busy(mp, MBF_NOWAIT); 1203 if (error != 0) { 1204 vfs_ref(mp); 1205 VOP_UNLOCK(dvp); 1206 error = vfs_busy(mp, 0); 1207 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1208 vfs_rel(mp); 1209 if (error) { 1210 error = ENOENT; 1211 goto out; 1212 } 1213 if (VN_IS_DOOMED(dvp)) { 1214 vfs_unbusy(mp); 1215 error = ENOENT; 1216 goto out; 1217 } 1218 } 1219 VOP_UNLOCK(dvp); 1220 error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp); 1221 vfs_unbusy(mp); 1222 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1223 if (VN_IS_DOOMED(dvp)) { 1224 if (error == 0) 1225 vput(vp); 1226 error = ENOENT; 1227 } 1228 if (error) 1229 goto out; 1230 *vpp = vp; 1231 } else if (isdot) { 1232 vref(dvp); 1233 *vpp = dvp; 1234 } else { 1235 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp); 1236 if (error) 1237 goto out; 1238 *vpp = vp; 1239 SMBVDEBUG("lookup: getnewvp!\n"); 1240 } 1241 if ((cnp->cn_flags & MAKEENTRY)/* && !islastcn*/) { 1242 /* VTOSMB(*vpp)->n_ctime = VTOSMB(*vpp)->n_vattr.va_ctime.tv_sec;*/ 1243 cache_enter(dvp, *vpp, cnp); 1244 } 1245 out: 1246 smbfs_free_scred(scred); 1247 return (error); 1248 } 1249