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