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