kernel - pipe locks are not needed in the kqueue event code
[dragonfly.git] / sys / vfs / smbfs / smbfs_vnops.c
blob20150067fb5387cdeda52baf4db315f68c6b3abe
1 /*
2 * Copyright (c) 2000-2001 Boris Popov
3 * All rights reserved.
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.
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.
32 * $FreeBSD: src/sys/fs/smbfs/smbfs_vnops.c,v 1.2.2.8 2003/04/04 08:57:23 tjr Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/priv.h>
39 #include <sys/namei.h>
40 #include <sys/fcntl.h>
41 #include <sys/mount.h>
42 #include <sys/unistd.h>
43 #include <sys/vnode.h>
44 #include <sys/lockf.h>
46 #include <vm/vm.h>
47 #include <vm/vm_extern.h>
48 #include <vm/vm_zone.h>
51 #include <netproto/smb/smb.h>
52 #include <netproto/smb/smb_conn.h>
53 #include <netproto/smb/smb_subr.h>
55 #include "smbfs.h"
56 #include "smbfs_node.h"
57 #include "smbfs_subr.h"
59 #include <sys/buf.h>
62 * Prototypes for SMBFS vnode operations
64 static int smbfs_create(struct vop_old_create_args *);
65 static int smbfs_mknod(struct vop_old_mknod_args *);
66 static int smbfs_open(struct vop_open_args *);
67 static int smbfs_closel(struct vop_close_args *);
68 static int smbfs_access(struct vop_access_args *);
69 static int smbfs_getattr(struct vop_getattr_args *);
70 static int smbfs_setattr(struct vop_setattr_args *);
71 static int smbfs_read(struct vop_read_args *);
72 static int smbfs_write(struct vop_write_args *);
73 static int smbfs_fsync(struct vop_fsync_args *);
74 static int smbfs_remove(struct vop_old_remove_args *);
75 static int smbfs_link(struct vop_old_link_args *);
76 static int smbfs_lookup(struct vop_old_lookup_args *);
77 static int smbfs_rename(struct vop_old_rename_args *);
78 static int smbfs_mkdir(struct vop_old_mkdir_args *);
79 static int smbfs_rmdir(struct vop_old_rmdir_args *);
80 static int smbfs_symlink(struct vop_old_symlink_args *);
81 static int smbfs_readdir(struct vop_readdir_args *);
82 static int smbfs_bmap(struct vop_bmap_args *);
83 static int smbfs_strategy(struct vop_strategy_args *);
84 static int smbfs_print(struct vop_print_args *);
85 static int smbfs_pathconf(struct vop_pathconf_args *ap);
86 static int smbfs_advlock(struct vop_advlock_args *);
87 static int smbfs_getextattr(struct vop_getextattr_args *ap);
89 struct vop_ops smbfs_vnode_vops = {
90 .vop_default = vop_defaultop,
91 .vop_access = smbfs_access,
92 .vop_advlock = smbfs_advlock,
93 .vop_bmap = smbfs_bmap,
94 .vop_close = smbfs_closel,
95 .vop_old_create = smbfs_create,
96 .vop_fsync = smbfs_fsync,
97 .vop_getattr = smbfs_getattr,
98 .vop_getpages = smbfs_getpages,
99 .vop_inactive = smbfs_inactive,
100 .vop_ioctl = smbfs_ioctl,
101 .vop_old_link = smbfs_link,
102 .vop_old_lookup = smbfs_lookup,
103 .vop_old_mkdir = smbfs_mkdir,
104 .vop_old_mknod = smbfs_mknod,
105 .vop_open = smbfs_open,
106 .vop_pathconf = smbfs_pathconf,
107 .vop_print = smbfs_print,
108 .vop_putpages = smbfs_putpages,
109 .vop_read = smbfs_read,
110 .vop_readdir = smbfs_readdir,
111 .vop_reclaim = smbfs_reclaim,
112 .vop_old_remove = smbfs_remove,
113 .vop_old_rename = smbfs_rename,
114 .vop_old_rmdir = smbfs_rmdir,
115 .vop_setattr = smbfs_setattr,
116 .vop_strategy = smbfs_strategy,
117 .vop_old_symlink = smbfs_symlink,
118 .vop_write = smbfs_write,
119 .vop_getextattr = smbfs_getextattr
120 /* .vop_setextattr = smbfs_setextattr */
124 * smbfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
126 static int
127 smbfs_access(struct vop_access_args *ap)
129 struct vnode *vp = ap->a_vp;
130 struct smbmount *smp = VTOSMBFS(vp);
131 int mode;
132 int error;
134 SMBVDEBUG("\n");
135 mode = ((vp->v_type == VREG) ?
136 smp->sm_args.file_mode : smp->sm_args.dir_mode);
137 error = vop_helper_access(ap, smp->sm_args.uid, smp->sm_args.gid,
138 mode, 0);
139 return (error);
143 * smbfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
144 * struct file *a_fp)
146 /* ARGSUSED */
147 static int
148 smbfs_open(struct vop_open_args *ap)
150 struct vnode *vp = ap->a_vp;
151 struct smbnode *np = VTOSMB(vp);
152 struct smb_cred scred;
153 struct vattr vattr;
154 int mode = ap->a_mode;
155 int error, accmode;
157 SMBVDEBUG("%s,%d\n", np->n_name, np->n_opencount);
158 if (vp->v_type != VREG && vp->v_type != VDIR) {
159 SMBFSERR("open eacces vtype=%d\n", vp->v_type);
160 return EACCES;
162 if (vp->v_type == VDIR) {
163 if (np->n_opencount == 0)
164 np->n_cached_cred = crhold(ap->a_cred);
165 np->n_opencount++;
166 return (vop_stdopen(ap));
168 if (np->n_flag & NMODIFIED) {
169 if ((error = smbfs_vinvalbuf(vp, V_SAVE, 1)) == EINTR)
170 return error;
171 smbfs_attr_cacheremove(vp);
172 error = VOP_GETATTR(vp, &vattr);
173 if (error)
174 return error;
175 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
176 } else {
177 error = VOP_GETATTR(vp, &vattr);
178 if (error)
179 return error;
180 if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
181 error = smbfs_vinvalbuf(vp, V_SAVE, 1);
182 if (error == EINTR)
183 return error;
184 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
187 if (np->n_opencount) {
188 np->n_opencount++;
189 return (vop_stdopen(ap));
191 accmode = SMB_AM_OPENREAD;
192 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
193 accmode = SMB_AM_OPENRW;
194 smb_makescred(&scred, curthread, ap->a_cred);
195 error = smbfs_smb_open(np, accmode, &scred);
196 if (error) {
197 if (mode & FWRITE)
198 return EACCES;
199 accmode = SMB_AM_OPENREAD;
200 error = smbfs_smb_open(np, accmode, &scred);
202 if (!error) {
203 np->n_cached_cred = crhold(ap->a_cred);
204 np->n_opencount++;
206 smbfs_attr_cacheremove(vp);
207 if (error == 0)
208 vop_stdopen(ap);
209 return error;
212 static int
213 smbfs_closel(struct vop_close_args *ap)
215 struct vnode *vp = ap->a_vp;
216 struct smbnode *np = VTOSMB(vp);
217 struct smb_cred scred;
218 struct vattr vattr;
219 int error;
221 SMBVDEBUG("name=%s, pid=%d, c=%d\n",
222 np->n_name, p->p_pid, np->n_opencount);
223 vn_lock(vp, LK_UPGRADE | LK_RETRY);
225 smb_makescred(&scred, curthread, proc0.p_ucred);
226 error = 0;
228 if (np->n_opencount == 0) {
229 if (vp->v_type != VDIR)
230 SMBERROR("Negative opencount\n");
231 goto done;
233 np->n_opencount--;
234 if (vp->v_type == VDIR) {
235 if (np->n_opencount)
236 goto done;
237 if (np->n_dirseq) {
238 smbfs_findclose(np->n_dirseq, &scred);
239 np->n_dirseq = NULL;
241 } else {
242 error = smbfs_vinvalbuf(vp, V_SAVE, 1);
243 if (np->n_opencount)
244 goto done;
245 VOP_GETATTR(vp, &vattr);
246 error = smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
247 &np->n_mtime, &scred);
249 crfree(np->n_cached_cred);
250 np->n_cached_cred = NULL;
251 smbfs_attr_cacheremove(vp);
252 done:
253 vop_stdclose(ap);
254 return error;
258 * smbfs_getattr call from vfs.
260 * smbfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
262 static int
263 smbfs_getattr(struct vop_getattr_args *ap)
265 struct vnode *vp = ap->a_vp;
266 struct smbnode *np = VTOSMB(vp);
267 struct vattr *va=ap->a_vap;
268 struct smbfattr fattr;
269 struct smb_cred scred;
270 u_quad_t oldsize;
271 int error;
273 SMBVDEBUG("%lx: '%s' %d\n", (long)vp, np->n_name, (vp->v_flag & VROOT) != 0);
274 error = smbfs_attr_cachelookup(vp, va);
275 if (!error)
276 return 0;
277 SMBVDEBUG("not in the cache\n");
278 smb_makescred(&scred, curthread, proc0.p_ucred);
279 oldsize = np->n_size;
280 error = smbfs_smb_lookup(np, NULL, 0, &fattr, &scred);
281 if (error) {
282 SMBVDEBUG("error %d\n", error);
283 return error;
285 smbfs_attr_cacheenter(vp, &fattr);
286 smbfs_attr_cachelookup(vp, va);
287 if (np->n_opencount)
288 np->n_size = oldsize;
289 return 0;
293 * smbfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
295 static int
296 smbfs_setattr(struct vop_setattr_args *ap)
298 struct vnode *vp = ap->a_vp;
299 struct smbnode *np = VTOSMB(vp);
300 struct vattr *vap = ap->a_vap;
301 struct timespec *mtime, *atime;
302 struct smb_cred scred;
303 struct smb_share *ssp = np->n_mount->sm_share;
304 struct smb_vc *vcp = SSTOVC(ssp);
305 u_quad_t tsize = 0;
306 int isreadonly, doclose, error = 0;
308 SMBVDEBUG("\n");
309 if (vap->va_flags != VNOVAL)
310 return EOPNOTSUPP;
311 isreadonly = (vp->v_mount->mnt_flag & MNT_RDONLY);
313 * Disallow write attempts if the filesystem is mounted read-only.
315 if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL ||
316 vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
317 vap->va_mode != (mode_t)VNOVAL) && isreadonly)
318 return EROFS;
319 smb_makescred(&scred, curthread, ap->a_cred);
320 if (vap->va_size != VNOVAL) {
321 switch (vp->v_type) {
322 case VDIR:
323 return EISDIR;
324 case VREG:
325 break;
326 default:
327 return EINVAL;
329 if (isreadonly)
330 return EROFS;
331 doclose = 0;
332 vnode_pager_setsize(vp, (u_long)vap->va_size);
333 tsize = np->n_size;
334 np->n_size = vap->va_size;
335 if (np->n_opencount == 0) {
336 error = smbfs_smb_open(np, SMB_AM_OPENRW, &scred);
337 if (error == 0)
338 doclose = 1;
340 if (error == 0)
341 error = smbfs_smb_setfsize(np, vap->va_size, &scred);
342 if (doclose)
343 smbfs_smb_close(ssp, np->n_fid, NULL, &scred);
344 if (error) {
345 np->n_size = tsize;
346 vnode_pager_setsize(vp, (u_long)tsize);
347 return error;
350 mtime = atime = NULL;
351 if (vap->va_mtime.tv_sec != VNOVAL)
352 mtime = &vap->va_mtime;
353 if (vap->va_atime.tv_sec != VNOVAL)
354 atime = &vap->va_atime;
355 if (mtime != atime) {
356 if (ap->a_cred->cr_uid != VTOSMBFS(vp)->sm_args.uid &&
357 (error = priv_check_cred(ap->a_cred, PRIV_VFS_SETATTR, 0)) &&
358 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
359 (error = VOP_EACCESS(vp, VWRITE, ap->a_cred))))
360 return (error);
361 #if 0
362 if (mtime == NULL)
363 mtime = &np->n_mtime;
364 if (atime == NULL)
365 atime = &np->n_atime;
366 #endif
368 * If file is opened, then we can use handle based calls.
369 * If not, use path based ones.
371 if (np->n_opencount == 0) {
372 if (vcp->vc_flags & SMBV_WIN95) {
373 error = VOP_OPEN(vp, FWRITE, ap->a_cred, NULL);
374 if (!error) {
375 /* error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
376 VOP_GETATTR(vp, &vattr);*/
377 if (mtime)
378 np->n_mtime = *mtime;
379 VOP_CLOSE(vp, FWRITE, NULL);
381 } else if ((vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS)) {
382 error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
383 /* error = smbfs_smb_setpattrNT(np, 0, mtime, atime, &scred);*/
384 } else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN2_0) {
385 error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
386 } else {
387 error = smbfs_smb_setpattr(np, 0, mtime, &scred);
389 } else {
390 if (vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS) {
391 error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
392 } else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN1_0) {
393 error = smbfs_smb_setftime(np, mtime, atime, &scred);
394 } else {
396 * I have no idea how to handle this for core
397 * level servers. The possible solution is to
398 * update mtime after file is closed.
400 SMBERROR("can't update times on an opened file\n");
405 * Invalidate attribute cache in case if server doesn't set
406 * required attributes.
408 smbfs_attr_cacheremove(vp); /* invalidate cache */
409 VOP_GETATTR(vp, vap);
410 np->n_mtime.tv_sec = vap->va_mtime.tv_sec;
411 return error;
414 * smbfs_read call.
416 * smbfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
417 * struct ucred *a_cred)
419 static int
420 smbfs_read(struct vop_read_args *ap)
422 struct vnode *vp = ap->a_vp;
423 struct uio *uio = ap->a_uio;
425 SMBVDEBUG("\n");
426 if (vp->v_type != VREG && vp->v_type != VDIR)
427 return EPERM;
428 return smbfs_readvnode(vp, uio, ap->a_cred);
432 * smbfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
433 * struct ucred *a_cred)
435 static int
436 smbfs_write(struct vop_write_args *ap)
438 struct vnode *vp = ap->a_vp;
439 struct uio *uio = ap->a_uio;
441 SMBVDEBUG("%d,ofs=%d,sz=%d\n",vp->v_type, (int)uio->uio_offset, uio->uio_resid);
442 if (vp->v_type != VREG)
443 return (EPERM);
444 return smbfs_writevnode(vp, uio, ap->a_cred,ap->a_ioflag);
447 * smbfs_create call
448 * Create a regular file. On entry the directory to contain the file being
449 * created is locked. We must release before we return.
451 * smbfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
452 * struct componentname *a_cnp, struct vattr *a_vap)
454 static int
455 smbfs_create(struct vop_old_create_args *ap)
457 struct vnode *dvp = ap->a_dvp;
458 struct vattr *vap = ap->a_vap;
459 struct vnode **vpp=ap->a_vpp;
460 struct componentname *cnp = ap->a_cnp;
461 struct smbnode *dnp = VTOSMB(dvp);
462 struct vnode *vp;
463 struct vattr vattr;
464 struct smbfattr fattr;
465 struct smb_cred scred;
466 char *name = cnp->cn_nameptr;
467 int nmlen = cnp->cn_namelen;
468 int error;
471 SMBVDEBUG("\n");
472 *vpp = NULL;
473 if (vap->va_type != VREG)
474 return EOPNOTSUPP;
475 if ((error = VOP_GETATTR(dvp, &vattr)))
476 return error;
477 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
479 error = smbfs_smb_create(dnp, name, nmlen, &scred);
480 if (error)
481 return error;
482 error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, &scred);
483 if (error)
484 return error;
485 error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, &vp);
486 if (error)
487 return error;
488 *vpp = vp;
489 return error;
493 * smbfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
494 * struct componentname *a_cnp)
496 static int
497 smbfs_remove(struct vop_old_remove_args *ap)
499 struct vnode *vp = ap->a_vp;
500 /* struct vnode *dvp = ap->a_dvp;*/
501 struct componentname *cnp = ap->a_cnp;
502 struct smbnode *np = VTOSMB(vp);
503 struct smb_cred scred;
504 int error;
506 if (vp->v_type == VDIR || np->n_opencount || VREFCNT(vp) > 1)
507 return EPERM;
508 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
509 error = smbfs_smb_delete(np, &scred);
510 return error;
514 * smbfs_file rename call
516 * smbfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
517 * struct componentname *a_fcnp, struct vnode *a_tdvp,
518 * struct vnode *a_tvp, struct componentname *a_tcnp)
520 static int
521 smbfs_rename(struct vop_old_rename_args *ap)
523 struct vnode *fvp = ap->a_fvp;
524 struct vnode *tvp = ap->a_tvp;
525 struct vnode *fdvp = ap->a_fdvp;
526 struct vnode *tdvp = ap->a_tdvp;
527 struct componentname *tcnp = ap->a_tcnp;
528 /* struct componentname *fcnp = ap->a_fcnp;*/
529 struct smb_cred scred;
530 u_int16_t flags = 6;
531 int error=0;
533 /* Check for cross-device rename */
534 if ((fvp->v_mount != tdvp->v_mount) ||
535 (tvp && (fvp->v_mount != tvp->v_mount))) {
536 error = EXDEV;
537 goto out;
540 if (tvp && VREFCNT(tvp) > 1) {
541 error = EBUSY;
542 goto out;
544 flags = 0x10; /* verify all writes */
545 if (fvp->v_type == VDIR) {
546 flags |= 2;
547 } else if (fvp->v_type == VREG) {
548 flags |= 1;
549 } else {
550 error = EINVAL;
551 goto out;
553 smb_makescred(&scred, tcnp->cn_td, tcnp->cn_cred);
555 * It seems that Samba doesn't implement SMB_COM_MOVE call...
557 #ifdef notnow
558 if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) {
559 error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp),
560 tcnp->cn_nameptr, tcnp->cn_namelen, flags, &scred);
561 } else
562 #endif
565 * We have to do the work atomicaly
567 if (tvp && tvp != fvp) {
568 error = smbfs_smb_delete(VTOSMB(tvp), &scred);
569 if (error)
570 goto out_cacherem;
572 error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp),
573 tcnp->cn_nameptr, tcnp->cn_namelen,
574 &scred);
575 if (error == 0) {
576 smbfs_attr_cacherename(fvp,
577 tcnp->cn_nameptr,
578 tcnp->cn_namelen);
582 out_cacherem:
583 smbfs_attr_cacheremove(fdvp);
584 smbfs_attr_cacheremove(tdvp);
585 out:
586 if (tdvp == tvp)
587 vrele(tdvp);
588 else
589 vput(tdvp);
590 if (tvp)
591 vput(tvp);
592 vrele(fdvp);
593 vrele(fvp);
594 #ifdef possible_mistake
595 #error x
596 vgone_vxlocked(fvp);
597 if (tvp)
598 vgone_vxlocked(tvp);
599 #endif
600 return error;
604 * somtime it will come true...
606 * smbfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
607 * struct componentname *a_cnp)
609 static int
610 smbfs_link(struct vop_old_link_args *ap)
612 return EOPNOTSUPP;
616 * smbfs_symlink link create call.
617 * Sometime it will be functional...
619 * smbfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
620 * struct componentname *a_cnp, struct vattr *a_vap,
621 * char *a_target)
623 static int
624 smbfs_symlink(struct vop_old_symlink_args *ap)
626 return EOPNOTSUPP;
629 static int
630 smbfs_mknod(struct vop_old_mknod_args *ap)
632 return EOPNOTSUPP;
636 * smbfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
637 * struct componentname *a_cnp, struct vattr *a_vap)
639 static int
640 smbfs_mkdir(struct vop_old_mkdir_args *ap)
642 struct vnode *dvp = ap->a_dvp;
643 /* struct vattr *vap = ap->a_vap;*/
644 struct vnode *vp;
645 struct componentname *cnp = ap->a_cnp;
646 struct smbnode *dnp = VTOSMB(dvp);
647 struct vattr vattr;
648 struct smb_cred scred;
649 struct smbfattr fattr;
650 char *name = cnp->cn_nameptr;
651 int len = cnp->cn_namelen;
652 int error;
654 if ((error = VOP_GETATTR(dvp, &vattr))) {
655 return error;
657 if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.'))))
658 return EEXIST;
659 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
660 error = smbfs_smb_mkdir(dnp, name, len, &scred);
661 if (error)
662 return error;
663 error = smbfs_smb_lookup(dnp, name, len, &fattr, &scred);
664 if (error)
665 return error;
666 error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp);
667 if (error)
668 return error;
669 *ap->a_vpp = vp;
670 return 0;
674 * smbfs_remove directory call
676 * smbfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
677 * struct componentname *a_cnp)
679 static int
680 smbfs_rmdir(struct vop_old_rmdir_args *ap)
682 struct vnode *vp = ap->a_vp;
683 struct vnode *dvp = ap->a_dvp;
684 struct componentname *cnp = ap->a_cnp;
685 /* struct smbmount *smp = VTOSMBFS(vp);*/
686 struct smbnode *dnp = VTOSMB(dvp);
687 struct smbnode *np = VTOSMB(vp);
688 struct smb_cred scred;
689 int error;
691 if (dvp == vp)
692 return EINVAL;
694 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
695 error = smbfs_smb_rmdir(np, &scred);
696 dnp->n_flag |= NMODIFIED;
697 smbfs_attr_cacheremove(dvp);
698 return error;
702 * smbfs_readdir call
704 * smbfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
705 * int *a_eofflag, off_t *a_cookies, int a_ncookies)
707 static int
708 smbfs_readdir(struct vop_readdir_args *ap)
710 struct vnode *vp = ap->a_vp;
711 struct uio *uio = ap->a_uio;
712 int error;
714 if (vp->v_type != VDIR)
715 return (EPERM);
716 #ifdef notnow
717 if (ap->a_ncookies) {
718 kprintf("smbfs_readdir: no support for cookies now...");
719 return (EOPNOTSUPP);
721 #endif
722 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
723 if (error == 0) {
724 error = smbfs_readvnode(vp, uio, ap->a_cred);
725 vn_unlock(vp);
727 return error;
731 * smbfs_fsync(struct vnode *a_vp, int a_waitfor)
733 /* ARGSUSED */
734 static int
735 smbfs_fsync(struct vop_fsync_args *ap)
737 /* return (smb_flush(ap->a_vp, ap->a_waitfor, curthread, 1));*/
738 return (0);
742 * smbfs_print(struct vnode *a_vp)
744 static int
745 smbfs_print(struct vop_print_args *ap)
747 struct vnode *vp = ap->a_vp;
748 struct smbnode *np = VTOSMB(vp);
750 if (np == NULL) {
751 kprintf("no smbnode data\n");
752 return (0);
754 kprintf("tag VT_SMBFS, name = %s, parent = %p, opencount = %d",
755 np->n_name, np->n_parent ? np->n_parent : NULL,
756 np->n_opencount);
757 lockmgr_printinfo(&vp->v_lock);
758 kprintf("\n");
759 return (0);
763 * smbfs_pathconf(struct vnode *vp, int name, register_t *retval)
765 static int
766 smbfs_pathconf(struct vop_pathconf_args *ap)
768 struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp));
769 struct smb_vc *vcp = SSTOVC(smp->sm_share);
770 register_t *retval = ap->a_retval;
771 int error = 0;
773 switch (ap->a_name) {
774 case _PC_LINK_MAX:
775 *retval = 0;
776 break;
777 case _PC_NAME_MAX:
778 *retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12;
779 break;
780 case _PC_PATH_MAX:
781 *retval = 800; /* XXX: a correct one ? */
782 break;
783 default:
784 error = EINVAL;
786 return error;
790 * smbfs_strategy(struct vnode *a_vp, struct bio *a_bio)
792 static int
793 smbfs_strategy(struct vop_strategy_args *ap)
795 struct bio *bio = ap->a_bio;
796 struct thread *td = NULL;
797 int error = 0;
799 SMBVDEBUG("\n");
800 if (bio->bio_flags & BIO_SYNC)
801 td = curthread; /* XXX */
803 if (bio->bio_flags & BIO_SYNC)
804 error = smbfs_doio(ap->a_vp, bio, proc0.p_ucred, td);
805 return error;
809 * smbfs_bmap(struct vnode *a_vp, off_t a_loffset,
810 * off_t *a_doffsetp, int *a_runp, int *a_runb)
812 static int
813 smbfs_bmap(struct vop_bmap_args *ap)
815 if (ap->a_doffsetp != NULL)
816 *ap->a_doffsetp = ap->a_loffset;
817 if (ap->a_runp != NULL)
818 *ap->a_runp = 0;
819 if (ap->a_runb != NULL)
820 *ap->a_runb = 0;
821 return (0);
825 * smbfs_ioctl(struct vnode *a_vp, u_long a_command, caddr_t a_data,
826 * int fflag, struct ucred *cred, struct proc *p)
829 smbfs_ioctl(struct vop_ioctl_args *ap)
831 return EINVAL;
834 static char smbfs_atl[] = "rhsvda";
837 * smbfs_getextattr(struct vnode *a_vp, char *a_name, struct uio *a_uio,
838 * struct ucred *a_cred)
840 static int
841 smbfs_getextattr(struct vop_getextattr_args *ap)
843 struct vnode *vp = ap->a_vp;
844 struct ucred *cred = ap->a_cred;
845 struct uio *uio = ap->a_uio;
846 const char *name = ap->a_attrname;
847 struct smbnode *np = VTOSMB(vp);
848 struct vattr vattr;
849 char buf[10];
850 int i, attr, error;
852 error = VOP_EACCESS(vp, VREAD, cred);
853 if (error)
854 return error;
855 error = VOP_GETATTR(vp, &vattr);
856 if (error)
857 return error;
858 if (strcmp(name, "dosattr") == 0) {
859 attr = np->n_dosattr;
860 for (i = 0; i < 6; i++, attr >>= 1)
861 buf[i] = (attr & 1) ? smbfs_atl[i] : '-';
862 buf[i] = 0;
863 error = uiomove(buf, i, uio);
865 } else
866 error = EINVAL;
867 return error;
871 * Since we expected to support F_GETLK (and SMB protocol has no such function),
872 * it is necessary to use lf_advlock(). It would be nice if this function had
873 * a callback mechanism because it will help to improve a level of consistency.
875 * smbfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
876 * struct flock *a_fl, int a_flags)
879 smbfs_advlock(struct vop_advlock_args *ap)
881 struct vnode *vp = ap->a_vp;
882 struct smbnode *np = VTOSMB(vp);
883 struct flock *fl = ap->a_fl;
884 caddr_t id = (caddr_t)1 /* ap->a_id */;
885 /* int flags = ap->a_flags;*/
886 struct thread *td = curthread; /* XXX */
887 struct smb_cred scred;
888 off_t start, end, size;
889 int error, lkop;
891 if (vp->v_type == VDIR) {
893 * SMB protocol have no support for directory locking.
894 * Although locks can be processed on local machine, I don't
895 * think that this is a good idea, because some programs
896 * can work wrong assuming directory is locked. So, we just
897 * return 'operation not supported
899 return EOPNOTSUPP;
901 size = np->n_size;
902 switch (fl->l_whence) {
903 case SEEK_SET:
904 case SEEK_CUR:
905 start = fl->l_start;
906 break;
907 case SEEK_END:
908 start = fl->l_start + size;
909 default:
910 return EINVAL;
912 if (start < 0)
913 return EINVAL;
914 if (fl->l_len == 0)
915 end = -1;
916 else {
917 end = start + fl->l_len - 1;
918 if (end < start)
919 return EINVAL;
921 smb_makescred(&scred, td, td->td_proc ? td->td_proc->p_ucred : NULL);
922 switch (ap->a_op) {
923 case F_SETLK:
924 switch (fl->l_type) {
925 case F_WRLCK:
926 lkop = SMB_LOCK_EXCL;
927 break;
928 case F_RDLCK:
929 lkop = SMB_LOCK_SHARED;
930 break;
931 case F_UNLCK:
932 lkop = SMB_LOCK_RELEASE;
933 break;
934 default:
935 return EINVAL;
937 error = lf_advlock(ap, &np->n_lockf, size);
938 if (error)
939 break;
940 lkop = SMB_LOCK_EXCL;
941 error = smbfs_smb_lock(np, lkop, id, start, end, &scred);
942 if (error) {
943 ap->a_op = F_UNLCK;
944 lf_advlock(ap, &np->n_lockf, size);
946 break;
947 case F_UNLCK:
948 lf_advlock(ap, &np->n_lockf, size);
949 error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, &scred);
950 break;
951 case F_GETLK:
952 error = lf_advlock(ap, &np->n_lockf, size);
953 break;
954 default:
955 return EINVAL;
957 return error;
960 static int
961 smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
963 static const char *badchars = "*/:<>;?";
964 static const char *badchars83 = " +|,";
965 const char *cp;
966 int i, error;
969 * Backslash characters, being a path delimiter, are prohibited
970 * within a path component even for LOOKUP operations.
972 if (index(name, '\\') != NULL)
973 return ENOENT;
975 if (nameiop == NAMEI_LOOKUP)
976 return 0;
977 error = ENOENT;
978 if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) {
980 * Name should conform 8.3 format
982 if (nmlen > 12)
983 return ENAMETOOLONG;
984 cp = index(name, '.');
985 if (cp == NULL)
986 return error;
987 if (cp == name || (cp - name) > 8)
988 return error;
989 cp = index(cp + 1, '.');
990 if (cp != NULL)
991 return error;
992 for (cp = name, i = 0; i < nmlen; i++, cp++)
993 if (index(badchars83, *cp) != NULL)
994 return error;
996 for (cp = name, i = 0; i < nmlen; i++, cp++)
997 if (index(badchars, *cp) != NULL)
998 return error;
999 return 0;
1003 * Things go even weird without fixed inode numbers...
1005 * smbfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1006 * struct componentname *a_cnp)
1009 smbfs_lookup(struct vop_old_lookup_args *ap)
1011 struct componentname *cnp = ap->a_cnp;
1012 struct thread *td = cnp->cn_td;
1013 struct vnode *dvp = ap->a_dvp;
1014 struct vnode **vpp = ap->a_vpp;
1015 struct vnode *vp;
1016 struct smbmount *smp;
1017 struct mount *mp = dvp->v_mount;
1018 struct smbnode *dnp;
1019 struct smbfattr fattr, *fap;
1020 struct smb_cred scred;
1021 char *name = cnp->cn_nameptr;
1022 int flags = cnp->cn_flags;
1023 int nameiop = cnp->cn_nameiop;
1024 int nmlen = cnp->cn_namelen;
1025 int lockparent, wantparent, error, isdot;
1027 SMBVDEBUG("\n");
1028 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1029 *vpp = NULL;
1030 if (dvp->v_type != VDIR)
1031 return ENOTDIR;
1032 if ((flags & CNP_ISDOTDOT) && (dvp->v_flag & VROOT)) {
1033 SMBFSERR("invalid '..'\n");
1034 return EIO;
1036 #ifdef SMB_VNODE_DEBUG
1038 char *cp, c;
1040 cp = name + nmlen;
1041 c = *cp;
1042 *cp = 0;
1043 SMBVDEBUG("%d '%s' in '%s' id=d\n", nameiop, name,
1044 VTOSMB(dvp)->n_name);
1045 *cp = c;
1047 #endif
1048 if ((mp->mnt_flag & MNT_RDONLY) && nameiop != NAMEI_LOOKUP)
1049 return EROFS;
1050 if ((error = VOP_EACCESS(dvp, VEXEC, cnp->cn_cred)) != 0)
1051 return error;
1052 lockparent = flags & CNP_LOCKPARENT;
1053 wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
1054 smp = VFSTOSMBFS(mp);
1055 dnp = VTOSMB(dvp);
1056 isdot = (nmlen == 1 && name[0] == '.');
1058 error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop);
1060 if (error)
1061 return ENOENT;
1063 error = 0;
1064 smb_makescred(&scred, td, cnp->cn_cred);
1065 fap = &fattr;
1066 if (flags & CNP_ISDOTDOT) {
1067 error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
1068 &scred);
1069 SMBVDEBUG("result of dotdot lookup: %d\n", error);
1070 } else {
1071 fap = &fattr;
1072 error = smbfs_smb_lookup(dnp, name, nmlen, fap, &scred);
1073 /* if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')*/
1074 SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1076 if (error && error != ENOENT)
1077 return error;
1078 if (error) { /* entry not found */
1080 * Handle RENAME or CREATE case...
1082 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) && wantparent) {
1083 error = VOP_EACCESS(dvp, VWRITE, cnp->cn_cred);
1084 if (error)
1085 return error;
1086 if (!lockparent) {
1087 vn_unlock(dvp);
1088 cnp->cn_flags |= CNP_PDIRUNLOCK;
1090 return (EJUSTRETURN);
1092 return ENOENT;
1093 }/* else {
1094 SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
1097 * handle DELETE case ...
1099 if (nameiop == NAMEI_DELETE) { /* delete last component */
1100 error = VOP_EACCESS(dvp, VWRITE, cnp->cn_cred);
1101 if (error)
1102 return error;
1103 if (isdot) {
1104 vref(dvp);
1105 *vpp = dvp;
1106 return 0;
1108 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1109 if (error)
1110 return error;
1111 *vpp = vp;
1112 if (!lockparent) {
1113 vn_unlock(dvp);
1114 cnp->cn_flags |= CNP_PDIRUNLOCK;
1116 return 0;
1118 if (nameiop == NAMEI_RENAME && wantparent) {
1119 error = VOP_EACCESS(dvp, VWRITE, cnp->cn_cred);
1120 if (error)
1121 return error;
1122 if (isdot)
1123 return EISDIR;
1124 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1125 if (error)
1126 return error;
1127 *vpp = vp;
1128 if (!lockparent) {
1129 vn_unlock(dvp);
1130 cnp->cn_flags |= CNP_PDIRUNLOCK;
1132 return 0;
1134 if (flags & CNP_ISDOTDOT) {
1135 vn_unlock(dvp);
1136 error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp);
1137 if (error) {
1138 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1139 return error;
1141 if (lockparent) {
1142 error = vn_lock(dvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
1143 if (error) {
1144 cnp->cn_flags |= CNP_PDIRUNLOCK;
1145 vput(vp);
1146 return error;
1149 *vpp = vp;
1150 } else if (isdot) {
1151 vref(dvp);
1152 *vpp = dvp;
1153 } else {
1154 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1155 if (error)
1156 return error;
1157 *vpp = vp;
1158 SMBVDEBUG("lookup: getnewvp!\n");
1159 if (!lockparent) {
1160 vn_unlock(dvp);
1161 cnp->cn_flags |= CNP_PDIRUNLOCK;
1164 return 0;