kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / vfs / nwfs / nwfs_vnops.c
1 /*
2  * Copyright (c) 1999, 2000 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/nwfs/nwfs_vnops.c,v 1.6.2.3 2001/03/14 11:26:59 bp Exp $
33  * $DragonFly: src/sys/vfs/nwfs/nwfs_vnops.c,v 1.6 2003/08/07 21:17:43 dillon Exp $
34  */
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/buf.h>
39 #include <sys/proc.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
46 #include <vm/vm.h>
47 #include <vm/vm_extern.h>
48 #include <vm/vm_zone.h>
49
50 #include <netncp/ncp.h>
51 #include <netncp/ncp_conn.h>
52 #include <netncp/ncp_subr.h>
53 #include <netncp/nwerror.h>
54 #include <netncp/ncp_nls.h>
55
56 #include "nwfs.h"
57 #include "nwfs_node.h"
58 #include "nwfs_subr.h"
59
60 /*
61  * Prototypes for NWFS vnode operations
62  */
63 static int nwfs_create(struct vop_create_args *);
64 static int nwfs_mknod(struct vop_mknod_args *);
65 static int nwfs_open(struct vop_open_args *);
66 static int nwfs_close(struct vop_close_args *);
67 static int nwfs_access(struct vop_access_args *);
68 static int nwfs_getattr(struct vop_getattr_args *);
69 static int nwfs_setattr(struct vop_setattr_args *);
70 static int nwfs_read(struct vop_read_args *);
71 static int nwfs_write(struct vop_write_args *);
72 static int nwfs_fsync(struct vop_fsync_args *);
73 static int nwfs_remove(struct vop_remove_args *);
74 static int nwfs_link(struct vop_link_args *);
75 static int nwfs_lookup(struct vop_lookup_args *);
76 static int nwfs_rename(struct vop_rename_args *);
77 static int nwfs_mkdir(struct vop_mkdir_args *);
78 static int nwfs_rmdir(struct vop_rmdir_args *);
79 static int nwfs_symlink(struct vop_symlink_args *);
80 static int nwfs_readdir(struct vop_readdir_args *);
81 static int nwfs_bmap(struct vop_bmap_args *);
82 static int nwfs_strategy(struct vop_strategy_args *);
83 static int nwfs_print(struct vop_print_args *);
84 static int nwfs_pathconf(struct vop_pathconf_args *ap);
85
86 /* Global vfs data structures for nwfs */
87 vop_t **nwfs_vnodeop_p;
88 static struct vnodeopv_entry_desc nwfs_vnodeop_entries[] = {
89         { &vop_default_desc,            (vop_t *) vop_defaultop },
90         { &vop_access_desc,             (vop_t *) nwfs_access },
91         { &vop_bmap_desc,               (vop_t *) nwfs_bmap },
92         { &vop_open_desc,               (vop_t *) nwfs_open },
93         { &vop_close_desc,              (vop_t *) nwfs_close },
94         { &vop_create_desc,             (vop_t *) nwfs_create },
95         { &vop_fsync_desc,              (vop_t *) nwfs_fsync },
96         { &vop_getattr_desc,            (vop_t *) nwfs_getattr },
97         { &vop_getpages_desc,           (vop_t *) nwfs_getpages },
98         { &vop_putpages_desc,           (vop_t *) nwfs_putpages },
99         { &vop_ioctl_desc,              (vop_t *) nwfs_ioctl },
100         { &vop_inactive_desc,           (vop_t *) nwfs_inactive },
101         { &vop_islocked_desc,           (vop_t *) vop_stdislocked },
102         { &vop_link_desc,               (vop_t *) nwfs_link },
103         { &vop_lock_desc,               (vop_t *) vop_stdlock },
104         { &vop_lookup_desc,             (vop_t *) nwfs_lookup },
105         { &vop_mkdir_desc,              (vop_t *) nwfs_mkdir },
106         { &vop_mknod_desc,              (vop_t *) nwfs_mknod },
107         { &vop_pathconf_desc,           (vop_t *) nwfs_pathconf },
108         { &vop_print_desc,              (vop_t *) nwfs_print },
109         { &vop_read_desc,               (vop_t *) nwfs_read },
110         { &vop_readdir_desc,            (vop_t *) nwfs_readdir },
111         { &vop_reclaim_desc,            (vop_t *) nwfs_reclaim },
112         { &vop_remove_desc,             (vop_t *) nwfs_remove },
113         { &vop_rename_desc,             (vop_t *) nwfs_rename },
114         { &vop_rmdir_desc,              (vop_t *) nwfs_rmdir },
115         { &vop_setattr_desc,            (vop_t *) nwfs_setattr },
116         { &vop_strategy_desc,           (vop_t *) nwfs_strategy },
117         { &vop_symlink_desc,            (vop_t *) nwfs_symlink },
118         { &vop_unlock_desc,             (vop_t *) vop_stdunlock },
119         { &vop_write_desc,              (vop_t *) nwfs_write },
120         { NULL, NULL }
121 };
122 static struct vnodeopv_desc nwfs_vnodeop_opv_desc =
123         { &nwfs_vnodeop_p, nwfs_vnodeop_entries };
124
125 VNODEOP_SET(nwfs_vnodeop_opv_desc);
126
127 /*
128  * nwfs_access vnode op
129  * for now just return ok
130  */
131 static int
132 nwfs_access(ap)
133         struct vop_access_args /* {
134                 struct vnode *a_vp;
135                 int  a_mode;
136                 struct ucred *a_cred;
137                 struct thread *a_td;
138         } */ *ap;
139 {
140         struct vnode *vp = ap->a_vp;
141         struct ucred *cred = ap->a_cred;
142         u_int mode = ap->a_mode;
143         struct nwmount *nmp = VTONWFS(vp);
144         int error = 0;
145
146         NCPVNDEBUG("\n");
147         if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
148                 switch (vp->v_type) {
149                     case VREG: case VDIR: case VLNK:
150                         return (EROFS);
151                     default:
152                         break;
153                 }
154         }
155         if (cred->cr_uid == 0)
156                 return 0;
157         if (cred->cr_uid != nmp->m.uid) {
158                 mode >>= 3;
159                 if (!groupmember(nmp->m.gid, cred))
160                         mode >>= 3;
161         }
162         error = (((vp->v_type == VREG) ? nmp->m.file_mode : nmp->m.dir_mode) & mode) == mode ? 0 : EACCES;
163         return error;
164 }
165 /*
166  * nwfs_open vnode op
167  */
168 /* ARGSUSED */
169 static int
170 nwfs_open(ap)
171         struct vop_open_args /* {
172                 struct vnode *a_vp;
173                 int  a_mode;
174                 struct ucred *a_cred;
175                 struct thread *a_td;
176         } */ *ap;
177 {
178         struct vnode *vp = ap->a_vp;
179         int mode = ap->a_mode;
180         struct nwnode *np = VTONW(vp);
181         struct ncp_open_info no;
182         struct nwmount *nmp = VTONWFS(vp);
183         struct vattr vattr;
184         int error, nwm;
185
186         NCPVNDEBUG("%s,%d\n",np->n_name, np->opened);
187         if (vp->v_type != VREG && vp->v_type != VDIR) { 
188                 NCPFATAL("open vtype = %d\n", vp->v_type);
189                 return (EACCES);
190         }
191         if (vp->v_type == VDIR) return 0;       /* nothing to do now */
192         if (np->n_flag & NMODIFIED) {
193                 if ((error = nwfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
194                         return (error);
195                 np->n_atime = 0;
196                 error = VOP_GETATTR(vp, &vattr, ap->a_td);
197                 if (error) return (error);
198                 np->n_mtime = vattr.va_mtime.tv_sec;
199         } else {
200                 error = VOP_GETATTR(vp, &vattr, ap->a_td);
201                 if (error) return (error);
202                 if (np->n_mtime != vattr.va_mtime.tv_sec) {
203                         if ((error = nwfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
204                                 return (error);
205                         np->n_mtime = vattr.va_mtime.tv_sec;
206                 }
207         }
208         if (np->opened) {
209                 np->opened++;
210                 return 0;
211         }
212         nwm = AR_READ;
213         if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
214                 nwm |= AR_WRITE;
215         error = ncp_open_create_file_or_subdir(nmp, vp, 0, NULL, OC_MODE_OPEN,
216                                                0, nwm, &no, ap->a_td, ap->a_cred);
217         if (error) {
218                 if (mode & FWRITE)
219                         return EACCES;
220                 nwm = AR_READ;
221                 error = ncp_open_create_file_or_subdir(nmp, vp, 0, NULL, OC_MODE_OPEN, 0,
222                                                    nwm, &no, ap->a_td,ap->a_cred);
223         }
224         if (!error) {
225                 np->opened++;
226                 np->n_fh = no.fh;
227                 np->n_origfh = no.origfh;
228         }
229         np->n_atime = 0;
230         return (error);
231 }
232
233 static int
234 nwfs_close(ap)
235         struct vop_close_args /* {
236                 struct vnodeop_desc *a_desc;
237                 struct vnode *a_vp;
238                 int  a_fflag;
239                 struct ucred *a_cred;
240                 struct thread *a_td;
241         } */ *ap;
242 {
243         struct vnode *vp = ap->a_vp;
244         struct nwnode *np = VTONW(vp);
245         int error;
246
247         NCPVNDEBUG("name=%s,td=%p,c=%d\n",np->n_name,ap->a_td,np->opened);
248
249         if (vp->v_type == VDIR) return 0;       /* nothing to do now */
250         error = 0;
251         lwkt_gettoken(&vp->v_interlock);
252         if (np->opened == 0) {
253                 lwkt_reltoken(&vp->v_interlock);
254                 return 0;
255         }
256         lwkt_reltoken(&vp->v_interlock);
257         error = nwfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
258         lwkt_gettoken(&vp->v_interlock);
259         if (np->opened == 0) {
260                 lwkt_reltoken(&vp->v_interlock);
261                 return 0;
262         }
263         if (--np->opened == 0) {
264                 lwkt_reltoken(&vp->v_interlock);
265                 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, 
266                    ap->a_td, proc0.p_ucred);
267         } else {
268                 lwkt_reltoken(&vp->v_interlock);
269         }
270         np->n_atime = 0;
271         return (error);
272 }
273
274 /*
275  * nwfs_getattr call from vfs.
276  */
277 static int
278 nwfs_getattr(ap)
279         struct vop_getattr_args /* {
280                 struct vnode *a_vp;
281                 struct vattr *a_vap;
282                 struct ucred *a_cred;
283                 struct thread *a_td;
284         } */ *ap;
285 {
286         struct vnode *vp = ap->a_vp;
287         struct nwnode *np = VTONW(vp);
288         struct vattr *va=ap->a_vap;
289         struct nwmount *nmp = VTONWFS(vp);
290         struct nw_entry_info fattr;
291         int error;
292         u_int32_t oldsize;
293
294         NCPVNDEBUG("%lx:%d: '%s' %d\n", (long)vp, nmp->n_volume, np->n_name, (vp->v_flag & VROOT) != 0);
295         error = nwfs_attr_cachelookup(vp,va);
296         if (!error) return 0;
297         NCPVNDEBUG("not in cache\n");
298         oldsize = np->n_size;
299         if (np->n_flag & NVOLUME) {
300                 error = ncp_obtain_info(nmp, np->n_fid.f_id, 0, NULL, &fattr,
301                     ap->a_td,proc0.p_ucred);
302         } else {
303                 error = ncp_obtain_info(nmp, np->n_fid.f_parent, np->n_nmlen, 
304                     np->n_name, &fattr, ap->a_td, proc0.p_ucred);
305         }
306         if (error) {
307                 NCPVNDEBUG("error %d\n", error);
308                 return error;
309         }
310         nwfs_attr_cacheenter(vp, &fattr);
311         *va = np->n_vattr;
312         if (np->opened)
313                 np->n_size = oldsize;
314         return (0);
315 }
316 /*
317  * nwfs_setattr call from vfs.
318  */
319 static int
320 nwfs_setattr(ap)
321         struct vop_setattr_args /* {
322                 struct vnode *a_vp;
323                 struct vattr *a_vap;
324                 struct ucred *a_cred;
325                 struct thread *a_td;
326         } */ *ap;
327 {
328         struct vnode *vp = ap->a_vp;
329         struct nwnode *np = VTONW(vp);
330         struct vattr *vap = ap->a_vap;
331         u_quad_t tsize=0;
332         int error = 0;
333
334         NCPVNDEBUG("\n");
335         if (vap->va_flags != VNOVAL)
336                 return (EOPNOTSUPP);
337         /*
338          * Disallow write attempts if the filesystem is mounted read-only.
339          */
340         if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL || 
341              vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
342              vap->va_mode != (mode_t)VNOVAL) &&(vp->v_mount->mnt_flag & MNT_RDONLY))
343                 return (EROFS);
344         if (vap->va_size != VNOVAL) {
345                 switch (vp->v_type) {
346                 case VDIR:
347                         return (EISDIR);
348                 case VREG:
349                         /*
350                          * Disallow write attempts if the filesystem is
351                          * mounted read-only.
352                          */
353                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
354                                 return (EROFS);
355                         vnode_pager_setsize(vp, (u_long)vap->va_size);
356                         tsize = np->n_size;
357                         np->n_size = vap->va_size;
358                         break;
359                 default:
360                         return EINVAL;
361                 };
362         }
363         error = ncp_setattr(vp, vap, ap->a_cred, ap->a_td);
364         if (error && vap->va_size != VNOVAL) {
365                 np->n_size = tsize;
366                 vnode_pager_setsize(vp, (u_long)tsize);
367         }
368         np->n_atime = 0;        /* invalidate cache */
369         VOP_GETATTR(vp, vap, ap->a_td);
370         np->n_mtime = vap->va_mtime.tv_sec;
371         return (0);
372 }
373 /*
374  * nwfs_read call.
375  */
376 static int
377 nwfs_read(ap)
378         struct vop_read_args /* {
379                 struct vnode *a_vp;
380                 struct uio *a_uio;
381                 int  a_ioflag;
382                 struct ucred *a_cred;
383         } */ *ap;
384 {
385         struct vnode *vp = ap->a_vp;
386         struct uio *uio=ap->a_uio;
387         int error;
388         NCPVNDEBUG("nwfs_read:\n");
389
390         if (vp->v_type != VREG && vp->v_type != VDIR)
391                 return (EPERM);
392         error = nwfs_readvnode(vp, uio, ap->a_cred);
393         return error;
394 }
395
396 static int
397 nwfs_write(ap)
398         struct vop_write_args /* {
399                 struct vnode *a_vp;
400                 struct uio *a_uio;
401                 int  a_ioflag;
402                 struct ucred *a_cred;
403         } */ *ap;
404 {
405         struct vnode *vp = ap->a_vp;
406         struct uio *uio = ap->a_uio;
407         int error;
408
409         NCPVNDEBUG("%d,ofs=%d,sz=%d\n",vp->v_type, (int)uio->uio_offset, uio->uio_resid);
410
411         if (vp->v_type != VREG)
412                 return (EPERM);
413         error = nwfs_writevnode(vp, uio, ap->a_cred,ap->a_ioflag);
414         return(error);
415 }
416 /*
417  * nwfs_create call
418  * Create a regular file. On entry the directory to contain the file being
419  * created is locked.  We must release before we return. We must also free
420  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
421  * only if the SAVESTART bit in cn_flags is clear on success.
422  */
423 static int
424 nwfs_create(ap)
425         struct vop_create_args /* {
426                 struct vnode *a_dvp;
427                 struct vnode **a_vpp;
428                 struct componentname *a_cnp;
429                 struct vattr *a_vap;
430         } */ *ap;
431 {
432         struct vnode *dvp = ap->a_dvp;
433         struct vattr *vap = ap->a_vap;
434         struct vnode **vpp=ap->a_vpp;
435         struct componentname *cnp = ap->a_cnp;
436         struct vnode *vp = (struct vnode *)0;
437         int error = 0, fmode;
438         struct vattr vattr;
439         struct nwnode *np;
440         struct ncp_open_info no;
441         struct nwmount *nmp=VTONWFS(dvp);
442         ncpfid fid;
443         
444
445         NCPVNDEBUG("\n");
446         *vpp = NULL;
447         if (vap->va_type == VSOCK)
448                 return (EOPNOTSUPP);
449         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td))) {
450                 return (error);
451         }
452         fmode = AR_READ | AR_WRITE;
453 /*      if (vap->va_vaflags & VA_EXCLUSIVE)
454                 fmode |= AR_DENY_READ | AR_DENY_WRITE;*/
455         
456         error = ncp_open_create_file_or_subdir(nmp,dvp,cnp->cn_namelen,cnp->cn_nameptr, 
457                            OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
458                            0, fmode, &no, cnp->cn_td, cnp->cn_cred);
459         if (!error) {
460                 error = ncp_close_file(NWFSTOCONN(nmp), &no.fh, cnp->cn_td,cnp->cn_cred);
461                 fid.f_parent = VTONW(dvp)->n_fid.f_id;
462                 fid.f_id = no.fattr.dirEntNum;
463                 error = nwfs_nget(VTOVFS(dvp), fid, &no.fattr, dvp, &vp);
464                 if (!error) {
465                         np = VTONW(vp);
466                         np->opened = 0;
467                         *vpp = vp;
468                 }
469                 if (cnp->cn_flags & MAKEENTRY)
470                         cache_enter(dvp, vp, cnp);
471         }
472         return (error);
473 }
474
475 /*
476  * nwfs_remove call. It isn't possible to emulate UFS behaivour because
477  * NetWare doesn't allow delete/rename operations on an opened file.
478  */
479 static int
480 nwfs_remove(ap)
481         struct vop_remove_args /* {
482                 struct vnodeop_desc *a_desc;
483                 struct vnode * a_dvp;
484                 struct vnode * a_vp;
485                 struct componentname * a_cnp;
486         } */ *ap;
487 {
488         struct vnode *vp = ap->a_vp;
489         struct vnode *dvp = ap->a_dvp;
490         struct componentname *cnp = ap->a_cnp;
491         struct nwnode *np = VTONW(vp);
492         struct nwmount *nmp = VTONWFS(vp);
493         int error;
494
495         if (vp->v_type == VDIR || np->opened || vp->v_usecount != 1) {
496                 error = EPERM;
497         } else if (!ncp_conn_valid(NWFSTOCONN(nmp))) {
498                 error = EIO;
499         } else {
500                 cache_purge(vp);
501                 error = ncp_DeleteNSEntry(nmp, VTONW(dvp)->n_fid.f_id,
502                     cnp->cn_namelen,cnp->cn_nameptr,cnp->cn_td,cnp->cn_cred);
503                 if (error == 0)
504                         np->n_flag |= NSHOULDFREE;
505                 else if (error == 0x899c)
506                         error = EACCES;
507         }
508         return (error);
509 }
510
511 /*
512  * nwfs_file rename call
513  */
514 static int
515 nwfs_rename(ap)
516         struct vop_rename_args  /* {
517                 struct vnode *a_fdvp;
518                 struct vnode *a_fvp;
519                 struct componentname *a_fcnp;
520                 struct vnode *a_tdvp;
521                 struct vnode *a_tvp;
522                 struct componentname *a_tcnp;
523         } */ *ap;
524 {
525         struct vnode *fvp = ap->a_fvp;
526         struct vnode *tvp = ap->a_tvp;
527         struct vnode *fdvp = ap->a_fdvp;
528         struct vnode *tdvp = ap->a_tdvp;
529         struct componentname *tcnp = ap->a_tcnp;
530         struct componentname *fcnp = ap->a_fcnp;
531         struct nwmount *nmp=VTONWFS(fvp);
532         u_int16_t oldtype = 6;
533         int error=0;
534
535         /* Check for cross-device rename */
536         if ((fvp->v_mount != tdvp->v_mount) ||
537             (tvp && (fvp->v_mount != tvp->v_mount))) {
538                 error = EXDEV;
539                 goto out;
540         }
541
542         if (tvp && tvp->v_usecount > 1) {
543                 error = EBUSY;
544                 goto out;
545         }
546         if (tvp && tvp != fvp) {
547                 error = ncp_DeleteNSEntry(nmp, VTONW(tdvp)->n_fid.f_id,
548                     tcnp->cn_namelen, tcnp->cn_nameptr, 
549                     tcnp->cn_td, tcnp->cn_cred);
550                 if (error == 0x899c) error = EACCES;
551                 if (error)
552                         goto out;
553         }
554         if (fvp->v_type == VDIR) {
555                 oldtype |= NW_TYPE_SUBDIR;
556         } else if (fvp->v_type == VREG) {
557                 oldtype |= NW_TYPE_FILE;
558         } else
559                 return EINVAL;
560         error = ncp_nsrename(NWFSTOCONN(nmp), nmp->n_volume, nmp->name_space, 
561                 oldtype, &nmp->m.nls,
562                 VTONW(fdvp)->n_fid.f_id, fcnp->cn_nameptr, fcnp->cn_namelen,
563                 VTONW(tdvp)->n_fid.f_id, tcnp->cn_nameptr, tcnp->cn_namelen,
564                 tcnp->cn_td,tcnp->cn_cred);
565
566         if (error == 0x8992)
567                 error = EEXIST;
568         if (fvp->v_type == VDIR) {
569                 if (tvp != NULL && tvp->v_type == VDIR)
570                         cache_purge(tdvp);
571                 cache_purge(fdvp);
572         }
573 out:
574         if (tdvp == tvp)
575                 vrele(tdvp);
576         else
577                 vput(tdvp);
578         if (tvp)
579                 vput(tvp);
580         vrele(fdvp);
581         vrele(fvp);
582         nwfs_attr_cacheremove(fdvp);
583         nwfs_attr_cacheremove(tdvp);
584         /*
585          * Need to get rid of old vnodes, because netware will change
586          * file id on rename
587          */
588         vgone(fvp);
589         if (tvp)
590                 vgone(tvp);
591         /*
592          * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
593          */
594         if (error == ENOENT)
595                 error = 0;
596         return (error);
597 }
598
599 /*
600  * nwfs hard link create call
601  * Netware filesystems don't know what links are.
602  */
603 static int
604 nwfs_link(ap)
605         struct vop_link_args /* {
606                 struct vnode *a_tdvp;
607                 struct vnode *a_vp;
608                 struct componentname *a_cnp;
609         } */ *ap;
610 {
611         return EOPNOTSUPP;
612 }
613
614 /*
615  * nwfs_symlink link create call
616  * Netware filesystems don't know what symlinks are.
617  */
618 static int
619 nwfs_symlink(ap)
620         struct vop_symlink_args /* {
621                 struct vnode *a_dvp;
622                 struct vnode **a_vpp;
623                 struct componentname *a_cnp;
624                 struct vattr *a_vap;
625                 char *a_target;
626         } */ *ap;
627 {
628         return (EOPNOTSUPP);
629 }
630
631 static int nwfs_mknod(ap) 
632         struct vop_mknod_args /* {
633         } */ *ap;
634 {
635         return (EOPNOTSUPP);
636 }
637
638 /*
639  * nwfs_mkdir call
640  */
641 static int
642 nwfs_mkdir(ap)
643         struct vop_mkdir_args /* {
644                 struct vnode *a_dvp;
645                 struct vnode **a_vpp;
646                 struct componentname *a_cnp;
647                 struct vattr *a_vap;
648         } */ *ap;
649 {
650         struct vnode *dvp = ap->a_dvp;
651 /*      struct vattr *vap = ap->a_vap;*/
652         struct componentname *cnp = ap->a_cnp;
653         int len=cnp->cn_namelen;
654         struct ncp_open_info no;
655         struct nwnode *np;
656         struct vnode *newvp = (struct vnode *)0;
657         ncpfid fid;
658         int error = 0;
659         struct vattr vattr;
660         char *name=cnp->cn_nameptr;
661
662         if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td))) {
663                 return (error);
664         }       
665         if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.')))) {
666                 return EEXIST;
667         }
668         if (ncp_open_create_file_or_subdir(VTONWFS(dvp),dvp, cnp->cn_namelen,
669                         cnp->cn_nameptr,OC_MODE_CREATE, aDIR, 0xffff,
670                         &no, cnp->cn_td, cnp->cn_cred) != 0) {
671                 error = EACCES;
672         } else {
673                 error = 0;
674         }
675         if (!error) {
676                 fid.f_parent = VTONW(dvp)->n_fid.f_id;
677                 fid.f_id = no.fattr.dirEntNum;
678                 error = nwfs_nget(VTOVFS(dvp), fid, &no.fattr, dvp, &newvp);
679                 if (!error) {
680                         np = VTONW(newvp);
681                         newvp->v_type = VDIR;
682                         *ap->a_vpp = newvp;
683                 }
684         }
685         return (error);
686 }
687
688 /*
689  * nwfs_remove directory call
690  */
691 static int
692 nwfs_rmdir(ap)
693         struct vop_rmdir_args /* {
694                 struct vnode *a_dvp;
695                 struct vnode *a_vp;
696                 struct componentname *a_cnp;
697         } */ *ap;
698 {
699         struct vnode *vp = ap->a_vp;
700         struct vnode *dvp = ap->a_dvp;
701         struct componentname *cnp = ap->a_cnp;
702         struct nwnode *np = VTONW(vp);
703         struct nwmount *nmp = VTONWFS(vp);
704         struct nwnode *dnp = VTONW(dvp);
705         int error = EIO;
706
707         if (dvp == vp)
708                 return EINVAL;
709
710         error = ncp_DeleteNSEntry(nmp, dnp->n_fid.f_id, 
711                 cnp->cn_namelen, cnp->cn_nameptr,cnp->cn_td,cnp->cn_cred);
712         if (error == 0)
713                 np->n_flag |= NSHOULDFREE;
714         else if (error == NWE_DIR_NOT_EMPTY)
715                 error = ENOTEMPTY;
716         dnp->n_flag |= NMODIFIED;
717         nwfs_attr_cacheremove(dvp);
718         cache_purge(dvp);
719         cache_purge(vp);
720         return (error);
721 }
722
723 /*
724  * nwfs_readdir call
725  */
726 static int
727 nwfs_readdir(ap)
728         struct vop_readdir_args /* {
729                 struct vnode *a_vp;
730                 struct uio *a_uio;
731                 struct ucred *a_cred;
732                 int *a_eofflag;
733                 u_long *a_cookies;
734                 int a_ncookies;
735         } */ *ap;
736 {
737         struct vnode *vp = ap->a_vp;
738         struct uio *uio = ap->a_uio;
739         int error;
740
741         if (vp->v_type != VDIR)
742                 return (EPERM);
743         if (ap->a_ncookies) {
744                 printf("nwfs_readdir: no support for cookies now...");
745                 return (EOPNOTSUPP);
746         }
747
748         error = nwfs_readvnode(vp, uio, ap->a_cred);
749         return error;
750 }
751 /* ARGSUSED */
752 static int
753 nwfs_fsync(ap)
754         struct vop_fsync_args /* {
755                 struct vnodeop_desc *a_desc;
756                 struct vnode * a_vp;
757                 struct ucred * a_cred;
758                 int  a_waitfor;
759                 struct thread *a_td;
760         } */ *ap;
761 {
762 /*      return (nfs_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/
763     return (0);
764 }
765
766 /* ARGSUSED */
767 static 
768 int nwfs_print (ap) 
769         struct vop_print_args /* {
770                 struct vnode *a_vp;
771         } */ *ap;
772 {
773         struct vnode *vp = ap->a_vp;
774         struct nwnode *np = VTONW(vp);
775
776         printf("nwfs node: name = '%s', fid = %d, pfid = %d\n",
777             np->n_name, np->n_fid.f_id, np->n_fid.f_parent);
778         return (0);
779 }
780
781 static int nwfs_pathconf (ap)
782         struct vop_pathconf_args  /* {
783         struct vnode *vp;
784         int name;
785         register_t *retval;
786         } */ *ap;
787 {
788         int name=ap->a_name, error=0;
789         register_t *retval=ap->a_retval;
790         
791         switch(name){
792                 case _PC_LINK_MAX:
793                         *retval=0;
794                         break;
795                 case _PC_NAME_MAX:
796                         *retval=NCP_MAX_FILENAME; /* XXX from nwfsnode */
797                         break;
798                 case _PC_PATH_MAX:
799                         *retval=NCP_MAXPATHLEN; /* XXX from nwfsnode */
800                         break;
801                 default:
802                         error=EINVAL;
803         }
804         return(error);
805 }
806
807 static int nwfs_strategy (ap) 
808         struct vop_strategy_args /* {
809         struct buf *a_bp
810         } */ *ap;
811 {
812         struct buf *bp=ap->a_bp;
813         int error = 0;
814         struct thread *td = NULL;
815
816         NCPVNDEBUG("\n");
817         if (bp->b_flags & B_PHYS)
818                 panic("nwfs physio");
819         if ((bp->b_flags & B_ASYNC) == 0)
820                 td = curthread;         /* YYY dunno if this is legal */
821         /*
822          * If the op is asynchronous and an i/o daemon is waiting
823          * queue the request, wake it up and wait for completion
824          * otherwise just do it ourselves.
825          */
826         if ((bp->b_flags & B_ASYNC) == 0 )
827                 error = nwfs_doio(bp, proc0.p_ucred, td);
828         return (error);
829 }
830
831 static int
832 nwfs_bmap(ap)
833         struct vop_bmap_args /* {
834                 struct vnode *a_vp;
835                 daddr_t  a_bn;
836                 struct vnode **a_vpp;
837                 daddr_t *a_bnp;
838                 int *a_runp;
839                 int *a_runb;
840         } */ *ap;
841 {
842         struct vnode *vp = ap->a_vp;
843
844         if (ap->a_vpp != NULL)
845                 *ap->a_vpp = vp;
846         if (ap->a_bnp != NULL)
847                 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
848         if (ap->a_runp != NULL)
849                 *ap->a_runp = 0;
850         if (ap->a_runb != NULL)
851                 *ap->a_runb = 0;
852         return (0);
853 }
854
855 int
856 nwfs_nget(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
857           struct vnode *dvp, struct vnode **vpp)
858 {
859         int error;
860         struct nwnode *newnp;
861         struct vnode *vp;
862
863         *vpp = NULL;
864         error = nwfs_allocvp(mp, fid, &vp);
865         if (error)
866                 return error;
867         newnp = VTONW(vp);
868         if (fap) {
869                 newnp->n_attr = fap->attributes;
870                 vp->v_type = newnp->n_attr & aDIR ? VDIR : VREG;
871                 nwfs_attr_cacheenter(vp, fap);
872         }
873         if (dvp) {
874                 newnp->n_parent = VTONW(dvp)->n_fid;
875                 if ((newnp->n_flag & NNEW) && vp->v_type == VDIR) {
876                         if ((dvp->v_flag & VROOT) == 0) {
877                                 newnp->n_refparent = 1;
878                                 vref(dvp);      /* vhold */
879                         }
880                 }
881         } else {
882                 if ((newnp->n_flag & NNEW) && vp->v_type == VREG)
883                         printf("new vnode '%s' borned without parent ?\n",newnp->n_name);
884         }
885         newnp->n_flag &= ~NNEW;
886         *vpp = vp;
887         return 0;
888 }
889
890 /*
891  * How to keep the brain busy ...
892  * Currently lookup routine can make two lookup for vnode. This can be
893  * avoided by reorg the code.
894  */
895 int
896 nwfs_lookup(ap)
897         struct vop_lookup_args /* {
898                 struct vnodeop_desc *a_desc;
899                 struct vnode *a_dvp;
900                 struct vnode **a_vpp;
901                 struct componentname *a_cnp;
902         } */ *ap;
903 {
904         struct componentname *cnp = ap->a_cnp;
905         struct vnode *dvp = ap->a_dvp;
906         struct vnode **vpp = ap->a_vpp;
907         int flags = cnp->cn_flags;
908         struct vnode *vp;
909         struct nwmount *nmp;
910         struct mount *mp = dvp->v_mount;
911         struct nwnode *dnp, *npp;
912         struct nw_entry_info fattr, *fap;
913         ncpfid fid;
914         int nameiop=cnp->cn_nameiop, islastcn;
915         int lockparent, wantparent, error = 0, notfound;
916         struct thread *td = cnp->cn_td;
917         char _name[cnp->cn_namelen+1];
918         bcopy(cnp->cn_nameptr,_name,cnp->cn_namelen);
919         _name[cnp->cn_namelen]=0;
920         
921         if (dvp->v_type != VDIR)
922                 return (ENOTDIR);
923         if ((flags & ISDOTDOT) && (dvp->v_flag & VROOT)) {
924                 printf("nwfs_lookup: invalid '..'\n");
925                 return EIO;
926         }
927
928         NCPVNDEBUG("%d '%s' in '%s' id=d\n", nameiop, _name, 
929                 VTONW(dvp)->n_name/*, VTONW(dvp)->n_name*/);
930
931         islastcn = flags & ISLASTCN;
932         if (islastcn && (mp->mnt_flag & MNT_RDONLY) && (nameiop != LOOKUP))
933                 return (EROFS);
934         if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)))
935                 return (error);
936         lockparent = flags & LOCKPARENT;
937         wantparent = flags & (LOCKPARENT|WANTPARENT);
938         nmp = VFSTONWFS(mp);
939         dnp = VTONW(dvp);
940 /*
941 printf("dvp %d:%d:%d\n", (int)mp, (int)dvp->v_flag & VROOT, (int)flags & ISDOTDOT);
942 */
943         error = ncp_pathcheck(cnp->cn_nameptr, cnp->cn_namelen, &nmp->m.nls, 
944             (nameiop == CREATE || nameiop == RENAME) && (nmp->m.nls.opt & NWHP_NOSTRICT) == 0);
945         if (error) 
946             return ENOENT;
947
948         error = cache_lookup(dvp, vpp, cnp);
949         NCPVNDEBUG("cache_lookup returned %d\n",error);
950         if (error > 0)
951                 return error;
952         if (error) {            /* name was found */
953                 struct vattr vattr;
954                 int vpid;
955
956                 vp = *vpp;
957                 vpid = vp->v_id;
958                 if (dvp == vp) {        /* lookup on current */
959                         vref(vp);
960                         error = 0;
961                         NCPVNDEBUG("cached '.'");
962                 } else if (flags & ISDOTDOT) {
963                         VOP_UNLOCK(dvp, 0, td); /* unlock parent */
964                         error = vget(vp, LK_EXCLUSIVE, td);
965                         if (!error && lockparent && islastcn)
966                                 error = vn_lock(dvp, LK_EXCLUSIVE, td);
967                 } else {
968                         error = vget(vp, LK_EXCLUSIVE, td);
969                         if (!lockparent || error || !islastcn)
970                                 VOP_UNLOCK(dvp, 0, td);
971                 }
972                 if (!error) {
973                         if (vpid == vp->v_id) {
974                            if (!VOP_GETATTR(vp, &vattr, td)
975                             && vattr.va_ctime.tv_sec == VTONW(vp)->n_ctime) {
976                                 if (nameiop != LOOKUP && islastcn)
977                                         cnp->cn_flags |= SAVENAME;
978                                 NCPVNDEBUG("use cached vnode");
979                                 return (0);
980                            }
981                            cache_purge(vp);
982                         }
983                         vput(vp);
984                         if (lockparent && dvp != vp && islastcn)
985                                 VOP_UNLOCK(dvp, 0, td);
986                 }
987                 error = vn_lock(dvp, LK_EXCLUSIVE, td);
988                 *vpp = NULLVP;
989                 if (error)
990                         return (error);
991         }
992         /* not in cache, so ...  */
993         error = 0;
994         *vpp = NULLVP;
995         fap = NULL;
996         if (flags & ISDOTDOT) {
997                 if (NWCMPF(&dnp->n_parent, &nmp->n_rootent)) {
998                         fid = nmp->n_rootent;
999                         fap = NULL;
1000                         notfound = 0;
1001                 } else {
1002                         error = nwfs_lookupnp(nmp, dnp->n_parent, td, &npp);
1003                         if (error) {
1004                                 return error;
1005                         }
1006                         fid = dnp->n_parent;
1007                         fap = &fattr;
1008                         /*np = *npp;*/
1009                         notfound = ncp_obtain_info(nmp, npp->n_dosfid,
1010                             0, NULL, fap, td, cnp->cn_cred);
1011                 }
1012         } else {
1013                 fap = &fattr;
1014                 notfound = ncp_lookup(dvp, cnp->cn_namelen, cnp->cn_nameptr,
1015                         fap, td, cnp->cn_cred);
1016                 fid.f_id = fap->dirEntNum;
1017                 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1018                         fid.f_parent = dnp->n_fid.f_parent;
1019                 } else
1020                         fid.f_parent = dnp->n_fid.f_id;
1021                 NCPVNDEBUG("call to ncp_lookup returned=%d\n",notfound);
1022         }
1023         if (notfound && notfound < 0x80 )
1024                 return (notfound);      /* hard error */
1025         if (notfound) { /* entry not found */
1026                 /* Handle RENAME or CREATE case... */
1027                 if ((nameiop == CREATE || nameiop == RENAME) && wantparent && islastcn) {
1028                         cnp->cn_flags |= SAVENAME;
1029                         if (!lockparent)
1030                                 VOP_UNLOCK(dvp, 0, td);
1031                         return (EJUSTRETURN);
1032                 }
1033                 return ENOENT;
1034         }/* else {
1035                 NCPVNDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
1036         }*/
1037         /* handle DELETE case ... */
1038         if (nameiop == DELETE && islastcn) {    /* delete last component */
1039                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, cnp->cn_td);
1040                 if (error) return (error);
1041                 if (NWCMPF(&dnp->n_fid, &fid)) {        /* we found ourselfs */
1042                         VREF(dvp);
1043                         *vpp = dvp;
1044                         return 0;
1045                 }
1046                 error = nwfs_nget(mp, fid, fap, dvp, &vp);
1047                 if (error) return (error);
1048                 *vpp = vp;
1049                 cnp->cn_flags |= SAVENAME;      /* I free it later */
1050                 if (!lockparent) VOP_UNLOCK(dvp,0,td);
1051                 return (0);
1052         }
1053         if (nameiop == RENAME && islastcn && wantparent) {
1054                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, cnp->cn_td);
1055                 if (error) return (error);
1056                 if (NWCMPF(&dnp->n_fid, &fid)) return EISDIR;
1057                 error = nwfs_nget(mp, fid, fap, dvp, &vp);
1058                 if (error) return (error);
1059                 *vpp = vp;
1060                 cnp->cn_flags |= SAVENAME;
1061                 if (!lockparent)
1062                         VOP_UNLOCK(dvp,0,td);
1063                 return (0);
1064         }
1065         if (flags & ISDOTDOT) {
1066                 VOP_UNLOCK(dvp, 0, td);         /* race to get the inode */
1067                 error = nwfs_nget(mp, fid, NULL, NULL, &vp);
1068                 if (error) {
1069                         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1070                         return (error);
1071                 }
1072                 if (lockparent && islastcn &&
1073                     (error = vn_lock(dvp, LK_EXCLUSIVE, td))) {
1074                         vput(vp);
1075                         return (error);
1076                 }
1077                 *vpp = vp;
1078         } else if (NWCMPF(&dnp->n_fid, &fid)) {
1079                 vref(dvp);
1080                 *vpp = dvp;
1081         } else {
1082                 error = nwfs_nget(mp, fid, fap, dvp, &vp);
1083                 if (error) return (error);
1084                 *vpp = vp;
1085                 NCPVNDEBUG("lookup: getnewvp!\n");
1086                 if (!lockparent || !islastcn)
1087                         VOP_UNLOCK(dvp, 0, td);
1088         }
1089         if ((cnp->cn_flags & MAKEENTRY)/* && !islastcn*/) {
1090                 VTONW(*vpp)->n_ctime = VTONW(*vpp)->n_vattr.va_ctime.tv_sec;
1091                 cache_enter(dvp, *vpp, cnp);
1092         }
1093         return (0);
1094 }