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