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