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
smbfs_access(struct vop_access_args * ap)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
smbfs_open(struct vop_open_args * ap)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
smbfs_close(struct vop_close_args * ap)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
smbfs_getattr(struct vop_getattr_args * ap)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
smbfs_setattr(struct vop_setattr_args * ap)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
smbfs_read(struct vop_read_args * ap)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
smbfs_write(struct vop_write_args * ap)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, or
489 * only if the SAVESTART bit in cn_flags is clear on success.
490 */
491 static int
smbfs_create(struct vop_create_args * ap)492 smbfs_create(struct vop_create_args *ap)
493 {
494 struct vnode *dvp = ap->a_dvp;
495 struct vattr *vap = ap->a_vap;
496 struct vnode **vpp=ap->a_vpp;
497 struct componentname *cnp = ap->a_cnp;
498 struct smbnode *dnp = VTOSMB(dvp);
499 struct vnode *vp;
500 struct vattr vattr;
501 struct smbfattr fattr;
502 struct smb_cred *scred;
503 char *name = cnp->cn_nameptr;
504 int nmlen = cnp->cn_namelen;
505 int error;
506
507 SMBVDEBUG("\n");
508 *vpp = NULL;
509 if (vap->va_type != VREG)
510 return EOPNOTSUPP;
511 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
512 return error;
513 scred = smbfs_malloc_scred();
514 smb_makescred(scred, cnp->cn_thread, cnp->cn_cred);
515
516 error = smbfs_smb_create(dnp, name, nmlen, scred);
517 if (error)
518 goto out;
519 error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, scred);
520 if (error)
521 goto out;
522 error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, &vp);
523 if (error)
524 goto out;
525 *vpp = vp;
526 if (cnp->cn_flags & MAKEENTRY)
527 cache_enter(dvp, vp, cnp);
528 out:
529 smbfs_free_scred(scred);
530 return error;
531 }
532
533 static int
smbfs_remove(struct vop_remove_args * ap)534 smbfs_remove(struct vop_remove_args *ap)
535 {
536 struct vnode *vp = ap->a_vp;
537 /* struct vnode *dvp = ap->a_dvp;*/
538 struct componentname *cnp = ap->a_cnp;
539 struct smbnode *np = VTOSMB(vp);
540 struct smb_cred *scred;
541 int error;
542
543 if (vp->v_type == VDIR || (np->n_flag & NOPEN) != 0 || vrefcnt(vp) != 1)
544 return EPERM;
545 scred = smbfs_malloc_scred();
546 smb_makescred(scred, cnp->cn_thread, cnp->cn_cred);
547 error = smbfs_smb_delete(np, scred);
548 if (error == 0)
549 np->n_flag |= NGONE;
550 cache_purge(vp);
551 smbfs_free_scred(scred);
552 return error;
553 }
554
555 /*
556 * smbfs_file rename call
557 */
558 static int
smbfs_rename(struct vop_rename_args * ap)559 smbfs_rename(struct vop_rename_args *ap)
560 {
561 struct vnode *fvp = ap->a_fvp;
562 struct vnode *tvp = ap->a_tvp;
563 struct vnode *fdvp = ap->a_fdvp;
564 struct vnode *tdvp = ap->a_tdvp;
565 struct componentname *tcnp = ap->a_tcnp;
566 /* struct componentname *fcnp = ap->a_fcnp;*/
567 struct smb_cred *scred;
568 u_int16_t flags = 6;
569 int error=0;
570
571 scred = NULL;
572 /* Check for cross-device rename */
573 if ((fvp->v_mount != tdvp->v_mount) ||
574 (tvp && (fvp->v_mount != tvp->v_mount))) {
575 error = EXDEV;
576 goto out;
577 }
578
579 if (tvp && vrefcnt(tvp) > 1) {
580 error = EBUSY;
581 goto out;
582 }
583 flags = 0x10; /* verify all writes */
584 if (fvp->v_type == VDIR) {
585 flags |= 2;
586 } else if (fvp->v_type == VREG) {
587 flags |= 1;
588 } else {
589 return EINVAL;
590 }
591 scred = smbfs_malloc_scred();
592 smb_makescred(scred, tcnp->cn_thread, tcnp->cn_cred);
593 /*
594 * It seems that Samba doesn't implement SMB_COM_MOVE call...
595 */
596 #ifdef notnow
597 if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) {
598 error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp),
599 tcnp->cn_nameptr, tcnp->cn_namelen, flags, scred);
600 } else
601 #endif
602 {
603 /*
604 * We have to do the work atomicaly
605 */
606 if (tvp && tvp != fvp) {
607 error = smbfs_smb_delete(VTOSMB(tvp), scred);
608 if (error)
609 goto out_cacherem;
610 VTOSMB(fvp)->n_flag |= NGONE;
611 }
612 error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp),
613 tcnp->cn_nameptr, tcnp->cn_namelen, scred);
614 }
615
616 if (fvp->v_type == VDIR) {
617 if (tvp != NULL && tvp->v_type == VDIR)
618 cache_purge(tdvp);
619 cache_purge(fdvp);
620 }
621
622 out_cacherem:
623 smbfs_attr_cacheremove(fdvp);
624 smbfs_attr_cacheremove(tdvp);
625 out:
626 smbfs_free_scred(scred);
627 if (tdvp == tvp)
628 vrele(tdvp);
629 else
630 vput(tdvp);
631 if (tvp)
632 vput(tvp);
633 vrele(fdvp);
634 vrele(fvp);
635 #ifdef possible_mistake
636 vgone(fvp);
637 if (tvp)
638 vgone(tvp);
639 #endif
640 return error;
641 }
642
643 /*
644 * somtime it will come true...
645 */
646 static int
smbfs_link(struct vop_link_args * ap)647 smbfs_link(struct vop_link_args *ap)
648 {
649 return EOPNOTSUPP;
650 }
651
652 /*
653 * smbfs_symlink link create call.
654 * Sometime it will be functional...
655 */
656 static int
smbfs_symlink(struct vop_symlink_args * ap)657 smbfs_symlink(struct vop_symlink_args *ap)
658 {
659 return EOPNOTSUPP;
660 }
661
662 static int
smbfs_mknod(struct vop_mknod_args * ap)663 smbfs_mknod(struct vop_mknod_args *ap)
664 {
665 return EOPNOTSUPP;
666 }
667
668 static int
smbfs_mkdir(struct vop_mkdir_args * ap)669 smbfs_mkdir(struct vop_mkdir_args *ap)
670 {
671 struct vnode *dvp = ap->a_dvp;
672 /* struct vattr *vap = ap->a_vap;*/
673 struct vnode *vp;
674 struct componentname *cnp = ap->a_cnp;
675 struct smbnode *dnp = VTOSMB(dvp);
676 struct vattr vattr;
677 struct smb_cred *scred;
678 struct smbfattr fattr;
679 char *name = cnp->cn_nameptr;
680 int len = cnp->cn_namelen;
681 int error;
682
683 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred))) {
684 return error;
685 }
686 if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.'))))
687 return EEXIST;
688 scred = smbfs_malloc_scred();
689 smb_makescred(scred, cnp->cn_thread, cnp->cn_cred);
690 error = smbfs_smb_mkdir(dnp, name, len, scred);
691 if (error)
692 goto out;
693 error = smbfs_smb_lookup(dnp, name, len, &fattr, scred);
694 if (error)
695 goto out;
696 error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp);
697 if (error)
698 goto out;
699 *ap->a_vpp = vp;
700 out:
701 smbfs_free_scred(scred);
702 return error;
703 }
704
705 /*
706 * smbfs_remove directory call
707 */
708 static int
smbfs_rmdir(struct vop_rmdir_args * ap)709 smbfs_rmdir(struct vop_rmdir_args *ap)
710 {
711 struct vnode *vp = ap->a_vp;
712 struct vnode *dvp = ap->a_dvp;
713 struct componentname *cnp = ap->a_cnp;
714 /* struct smbmount *smp = VTOSMBFS(vp);*/
715 struct smbnode *dnp = VTOSMB(dvp);
716 struct smbnode *np = VTOSMB(vp);
717 struct smb_cred *scred;
718 int error;
719
720 if (dvp == vp)
721 return EINVAL;
722
723 scred = smbfs_malloc_scred();
724 smb_makescred(scred, cnp->cn_thread, cnp->cn_cred);
725 error = smbfs_smb_rmdir(np, scred);
726 if (error == 0)
727 np->n_flag |= NGONE;
728 dnp->n_flag |= NMODIFIED;
729 smbfs_attr_cacheremove(dvp);
730 /* cache_purge(dvp);*/
731 cache_purge(vp);
732 smbfs_free_scred(scred);
733 return error;
734 }
735
736 /*
737 * smbfs_readdir call
738 */
739 static int
smbfs_readdir(struct vop_readdir_args * ap)740 smbfs_readdir(struct vop_readdir_args *ap)
741 {
742 struct vnode *vp = ap->a_vp;
743 struct uio *uio = ap->a_uio;
744 int error;
745
746 if (vp->v_type != VDIR)
747 return (EPERM);
748 #ifdef notnow
749 if (ap->a_ncookies) {
750 printf("smbfs_readdir: no support for cookies now...");
751 return (EOPNOTSUPP);
752 }
753 #endif
754 error = smbfs_readvnode(vp, uio, ap->a_cred);
755 return error;
756 }
757
758 /* ARGSUSED */
759 static int
smbfs_fsync(struct vop_fsync_args * ap)760 smbfs_fsync(struct vop_fsync_args *ap)
761 {
762 /* return (smb_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/
763 return (0);
764 }
765
766 static
smbfs_print(struct vop_print_args * ap)767 int smbfs_print(struct vop_print_args *ap)
768 {
769 struct vnode *vp = ap->a_vp;
770 struct smbnode *np = VTOSMB(vp);
771
772 if (np == NULL) {
773 printf("no smbnode data\n");
774 return (0);
775 }
776 printf("\tname = %s, parent = %p, open = %d\n", np->n_name,
777 np->n_parent ? np->n_parent : NULL, (np->n_flag & NOPEN) != 0);
778 return (0);
779 }
780
781 static int
smbfs_pathconf(struct vop_pathconf_args * ap)782 smbfs_pathconf(struct vop_pathconf_args *ap)
783 {
784 struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp));
785 struct smb_vc *vcp = SSTOVC(smp->sm_share);
786 long *retval = ap->a_retval;
787 int error = 0;
788
789 switch (ap->a_name) {
790 case _PC_FILESIZEBITS:
791 if (vcp->vc_sopt.sv_caps & (SMB_CAP_LARGE_READX |
792 SMB_CAP_LARGE_WRITEX))
793 *retval = 64;
794 else
795 *retval = 32;
796 break;
797 case _PC_NAME_MAX:
798 *retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12;
799 break;
800 case _PC_PATH_MAX:
801 *retval = 800; /* XXX: a correct one ? */
802 break;
803 case _PC_NO_TRUNC:
804 *retval = 1;
805 break;
806 default:
807 error = vop_stdpathconf(ap);
808 }
809 return error;
810 }
811
812 static int
smbfs_strategy(struct vop_strategy_args * ap)813 smbfs_strategy(struct vop_strategy_args *ap)
814 {
815 struct buf *bp=ap->a_bp;
816 struct ucred *cr;
817 struct thread *td;
818
819 SMBVDEBUG("\n");
820 if (bp->b_flags & B_ASYNC)
821 td = (struct thread *)0;
822 else
823 td = curthread; /* XXX */
824 if (bp->b_iocmd == BIO_READ)
825 cr = bp->b_rcred;
826 else
827 cr = bp->b_wcred;
828
829 if ((bp->b_flags & B_ASYNC) == 0 )
830 (void)smbfs_doio(ap->a_vp, bp, cr, td);
831 return (0);
832 }
833
834 int
smbfs_ioctl(struct vop_ioctl_args * ap)835 smbfs_ioctl(struct vop_ioctl_args *ap)
836 {
837 return ENOTTY;
838 }
839
840 static char smbfs_atl[] = "rhsvda";
841 static int
smbfs_getextattr(struct vop_getextattr_args * ap)842 smbfs_getextattr(struct vop_getextattr_args *ap)
843 /* {
844 IN struct vnode *a_vp;
845 IN char *a_name;
846 INOUT struct uio *a_uio;
847 IN struct ucred *a_cred;
848 IN struct thread *a_td;
849 };
850 */
851 {
852 struct vnode *vp = ap->a_vp;
853 struct thread *td = ap->a_td;
854 struct ucred *cred = ap->a_cred;
855 struct uio *uio = ap->a_uio;
856 const char *name = ap->a_name;
857 struct smbnode *np = VTOSMB(vp);
858 struct vattr vattr;
859 char buf[10];
860 int i, attr, error;
861
862 error = VOP_ACCESS(vp, VREAD, cred, td);
863 if (error)
864 return error;
865 error = VOP_GETATTR(vp, &vattr, cred);
866 if (error)
867 return error;
868 if (strcmp(name, "dosattr") == 0) {
869 attr = np->n_dosattr;
870 for (i = 0; i < 6; i++, attr >>= 1)
871 buf[i] = (attr & 1) ? smbfs_atl[i] : '-';
872 buf[i] = 0;
873 error = uiomove(buf, i, uio);
874 } else
875 error = EINVAL;
876 return error;
877 }
878
879 /*
880 * Since we expected to support F_GETLK (and SMB protocol has no such function),
881 * it is necessary to use lf_advlock(). It would be nice if this function had
882 * a callback mechanism because it will help to improve a level of consistency.
883 */
884 int
smbfs_advlock(struct vop_advlock_args * ap)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;
893 struct smb_cred *scred;
894 u_quad_t size;
895 off_t start, end, oadd;
896 int error, lkop;
897
898 if (vp->v_type == VDIR) {
899 /*
900 * SMB protocol have no support for directory locking.
901 * Although locks can be processed on local machine, I don't
902 * think that this is a good idea, because some programs
903 * can work wrong assuming directory is locked. So, we just
904 * return 'operation not supported
905 */
906 return EOPNOTSUPP;
907 }
908 size = np->n_size;
909 switch (fl->l_whence) {
910 case SEEK_SET:
911 case SEEK_CUR:
912 start = fl->l_start;
913 break;
914
915 case SEEK_END:
916 if (size > OFF_MAX ||
917 (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
918 return EOVERFLOW;
919 start = size + fl->l_start;
920 break;
921
922 default:
923 return EINVAL;
924 }
925 if (start < 0)
926 return EINVAL;
927 if (fl->l_len < 0) {
928 if (start == 0)
929 return EINVAL;
930 end = start - 1;
931 start += fl->l_len;
932 if (start < 0)
933 return EINVAL;
934 } else if (fl->l_len == 0)
935 end = -1;
936 else {
937 oadd = fl->l_len - 1;
938 if (oadd > OFF_MAX - start)
939 return EOVERFLOW;
940 end = start + oadd;
941 }
942 scred = smbfs_malloc_scred();
943 smb_makescred(scred, td, td->td_ucred);
944 switch (ap->a_op) {
945 case F_SETLK:
946 switch (fl->l_type) {
947 case F_WRLCK:
948 lkop = SMB_LOCK_EXCL;
949 break;
950 case F_RDLCK:
951 lkop = SMB_LOCK_SHARED;
952 break;
953 case F_UNLCK:
954 lkop = SMB_LOCK_RELEASE;
955 break;
956 default:
957 smbfs_free_scred(scred);
958 return EINVAL;
959 }
960 error = lf_advlock(ap, &vp->v_lockf, size);
961 if (error)
962 break;
963 lkop = SMB_LOCK_EXCL;
964 error = smbfs_smb_lock(np, lkop, id, start, end, scred);
965 if (error) {
966 int oldtype = fl->l_type;
967 fl->l_type = F_UNLCK;
968 ap->a_op = F_UNLCK;
969 lf_advlock(ap, &vp->v_lockf, size);
970 fl->l_type = oldtype;
971 }
972 break;
973 case F_UNLCK:
974 lf_advlock(ap, &vp->v_lockf, size);
975 error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, scred);
976 break;
977 case F_GETLK:
978 error = lf_advlock(ap, &vp->v_lockf, size);
979 break;
980 default:
981 smbfs_free_scred(scred);
982 return EINVAL;
983 }
984 smbfs_free_scred(scred);
985 return error;
986 }
987
988 static int
smbfs_pathcheck(struct smbmount * smp,const char * name,int nmlen,int nameiop)989 smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
990 {
991 static const char *badchars = "*/:<>?";
992 static const char *badchars83 = " +|,[]=;";
993 const char *cp;
994 int i, error;
995
996 /*
997 * Backslash characters, being a path delimiter, are prohibited
998 * within a path component even for LOOKUP operations.
999 */
1000 if (strchr(name, '\\') != NULL)
1001 return ENOENT;
1002
1003 if (nameiop == LOOKUP)
1004 return 0;
1005 error = ENOENT;
1006 if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) {
1007 /*
1008 * Name should conform 8.3 format
1009 */
1010 if (nmlen > 12)
1011 return ENAMETOOLONG;
1012 cp = strchr(name, '.');
1013 if (cp == NULL)
1014 return error;
1015 if (cp == name || (cp - name) > 8)
1016 return error;
1017 cp = strchr(cp + 1, '.');
1018 if (cp != NULL)
1019 return error;
1020 for (cp = name, i = 0; i < nmlen; i++, cp++)
1021 if (strchr(badchars83, *cp) != NULL)
1022 return error;
1023 }
1024 for (cp = name, i = 0; i < nmlen; i++, cp++)
1025 if (strchr(badchars, *cp) != NULL)
1026 return error;
1027 return 0;
1028 }
1029
1030 /*
1031 * Things go even weird without fixed inode numbers...
1032 */
1033 int
smbfs_lookup(struct vop_lookup_args * ap)1034 smbfs_lookup(struct vop_lookup_args *ap)
1035 {
1036 struct componentname *cnp = ap->a_cnp;
1037 struct thread *td = cnp->cn_thread;
1038 struct vnode *dvp = ap->a_dvp;
1039 struct vnode **vpp = ap->a_vpp;
1040 struct vnode *vp;
1041 struct smbmount *smp;
1042 struct mount *mp = dvp->v_mount;
1043 struct smbnode *dnp;
1044 struct smbfattr fattr, *fap;
1045 struct smb_cred *scred;
1046 char *name = cnp->cn_nameptr;
1047 int flags = cnp->cn_flags;
1048 int nameiop = cnp->cn_nameiop;
1049 int nmlen = cnp->cn_namelen;
1050 int error, islastcn, isdot;
1051 int killit;
1052
1053 SMBVDEBUG("\n");
1054 if (dvp->v_type != VDIR)
1055 return ENOTDIR;
1056 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) {
1057 SMBFSERR("invalid '..'\n");
1058 return EIO;
1059 }
1060 islastcn = flags & ISLASTCN;
1061 if (islastcn && (mp->mnt_flag & MNT_RDONLY) && (nameiop != LOOKUP))
1062 return EROFS;
1063 error = vn_dir_check_exec(dvp, cnp);
1064 if (error != 0)
1065 return error;
1066 smp = VFSTOSMBFS(mp);
1067 dnp = VTOSMB(dvp);
1068 isdot = (nmlen == 1 && name[0] == '.');
1069
1070 error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop);
1071
1072 if (error)
1073 return ENOENT;
1074
1075 error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
1076 SMBVDEBUG("cache_lookup returned %d\n", error);
1077 if (error > 0)
1078 return error;
1079 if (error) { /* name was found */
1080 struct vattr vattr;
1081
1082 killit = 0;
1083 vp = *vpp;
1084 error = VOP_GETATTR(vp, &vattr, cnp->cn_cred);
1085 /*
1086 * If the file type on the server is inconsistent
1087 * with what it was when we created the vnode,
1088 * kill the bogus vnode now and fall through to
1089 * the code below to create a new one with the
1090 * right type.
1091 */
1092 if (error == 0 &&
1093 ((vp->v_type == VDIR &&
1094 (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) == 0) ||
1095 (vp->v_type == VREG &&
1096 (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) != 0)))
1097 killit = 1;
1098 else if (error == 0
1099 /* && vattr.va_ctime.tv_sec == VTOSMB(vp)->n_ctime*/) {
1100 if (nameiop != LOOKUP && islastcn)
1101 cnp->cn_flags |= SAVENAME;
1102 SMBVDEBUG("use cached vnode\n");
1103 return (0);
1104 }
1105 cache_purge(vp);
1106 /*
1107 * XXX This is not quite right, if '.' is
1108 * inconsistent, we really need to start the lookup
1109 * all over again. Hopefully there is some other
1110 * guarantee that prevents this case from happening.
1111 */
1112 if (killit && vp != dvp)
1113 vgone(vp);
1114 if (vp != dvp)
1115 vput(vp);
1116 else
1117 vrele(vp);
1118 *vpp = NULLVP;
1119 }
1120 /*
1121 * entry is not in the cache or has been expired
1122 */
1123 error = 0;
1124 *vpp = NULLVP;
1125 scred = smbfs_malloc_scred();
1126 smb_makescred(scred, td, cnp->cn_cred);
1127 fap = &fattr;
1128 if (flags & ISDOTDOT) {
1129 /*
1130 * In the DOTDOT case, don't go over-the-wire
1131 * in order to request attributes. We already
1132 * know it's a directory and subsequent call to
1133 * smbfs_getattr() will restore consistency.
1134 *
1135 */
1136 SMBVDEBUG("smbfs_smb_lookup: dotdot\n");
1137 } else if (isdot) {
1138 error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred);
1139 SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1140 }
1141 else {
1142 error = smbfs_smb_lookup(dnp, name, nmlen, fap, scred);
1143 SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1144 }
1145 if (error && error != ENOENT)
1146 goto out;
1147 if (error) { /* entry not found */
1148 /*
1149 * Handle RENAME or CREATE case...
1150 */
1151 if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1152 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1153 if (error)
1154 goto out;
1155 cnp->cn_flags |= SAVENAME;
1156 error = EJUSTRETURN;
1157 goto out;
1158 }
1159 error = ENOENT;
1160 goto out;
1161 }/* else {
1162 SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
1163 }*/
1164 /*
1165 * handle DELETE case ...
1166 */
1167 if (nameiop == DELETE && islastcn) { /* delete last component */
1168 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1169 if (error)
1170 goto out;
1171 if (isdot) {
1172 VREF(dvp);
1173 *vpp = dvp;
1174 goto out;
1175 }
1176 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1177 if (error)
1178 goto out;
1179 *vpp = vp;
1180 cnp->cn_flags |= SAVENAME;
1181 goto out;
1182 }
1183 if (nameiop == RENAME && islastcn) {
1184 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1185 if (error)
1186 goto out;
1187 if (isdot) {
1188 error = EISDIR;
1189 goto out;
1190 }
1191 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1192 if (error)
1193 goto out;
1194 *vpp = vp;
1195 cnp->cn_flags |= SAVENAME;
1196 goto out;
1197 }
1198 if (flags & ISDOTDOT) {
1199 mp = dvp->v_mount;
1200 error = vfs_busy(mp, MBF_NOWAIT);
1201 if (error != 0) {
1202 vfs_ref(mp);
1203 VOP_UNLOCK(dvp);
1204 error = vfs_busy(mp, 0);
1205 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1206 vfs_rel(mp);
1207 if (error) {
1208 error = ENOENT;
1209 goto out;
1210 }
1211 if (VN_IS_DOOMED(dvp)) {
1212 vfs_unbusy(mp);
1213 error = ENOENT;
1214 goto out;
1215 }
1216 }
1217 VOP_UNLOCK(dvp);
1218 error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp);
1219 vfs_unbusy(mp);
1220 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1221 if (VN_IS_DOOMED(dvp)) {
1222 if (error == 0)
1223 vput(vp);
1224 error = ENOENT;
1225 }
1226 if (error)
1227 goto out;
1228 *vpp = vp;
1229 } else if (isdot) {
1230 vref(dvp);
1231 *vpp = dvp;
1232 } else {
1233 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1234 if (error)
1235 goto out;
1236 *vpp = vp;
1237 SMBVDEBUG("lookup: getnewvp!\n");
1238 }
1239 if ((cnp->cn_flags & MAKEENTRY)/* && !islastcn*/) {
1240 /* VTOSMB(*vpp)->n_ctime = VTOSMB(*vpp)->n_vattr.va_ctime.tv_sec;*/
1241 cache_enter(dvp, *vpp, cnp);
1242 }
1243 out:
1244 smbfs_free_scred(scred);
1245 return (error);
1246 }
1247