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