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