Merge from vendor branch GDB:
[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.31 2006/07/19 06:08:13 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 <netproto/ncp/ncp.h>
51 #include <netproto/ncp/ncp_conn.h>
52 #include <netproto/ncp/ncp_subr.h>
53 #include <netproto/ncp/nwerror.h>
54 #include <netproto/ncp/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_old_create_args *);
64 static int nwfs_mknod(struct vop_old_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_old_remove_args *);
74 static int nwfs_link(struct vop_old_link_args *);
75 static int nwfs_lookup(struct vop_old_lookup_args *);
76 static int nwfs_rename(struct vop_old_rename_args *);
77 static int nwfs_mkdir(struct vop_old_mkdir_args *);
78 static int nwfs_rmdir(struct vop_old_rmdir_args *);
79 static int nwfs_symlink(struct vop_old_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 struct vop_ops nwfs_vnode_vops = {
88         .vop_default =          vop_defaultop,
89         .vop_access =           nwfs_access,
90         .vop_bmap =             nwfs_bmap,
91         .vop_open =             nwfs_open,
92         .vop_close =            nwfs_close,
93         .vop_old_create =       nwfs_create,
94         .vop_fsync =            nwfs_fsync,
95         .vop_getattr =          nwfs_getattr,
96         .vop_getpages =         nwfs_getpages,
97         .vop_putpages =         nwfs_putpages,
98         .vop_ioctl =            nwfs_ioctl,
99         .vop_inactive =         nwfs_inactive,
100         .vop_islocked =         vop_stdislocked,
101         .vop_old_link =         nwfs_link,
102         .vop_lock =             vop_stdlock,
103         .vop_old_lookup =       nwfs_lookup,
104         .vop_old_mkdir =        nwfs_mkdir,
105         .vop_old_mknod =        nwfs_mknod,
106         .vop_pathconf =         nwfs_pathconf,
107         .vop_print =            nwfs_print,
108         .vop_read =             nwfs_read,
109         .vop_readdir =          nwfs_readdir,
110         .vop_reclaim =          nwfs_reclaim,
111         .vop_old_remove =       nwfs_remove,
112         .vop_old_rename =       nwfs_rename,
113         .vop_old_rmdir =        nwfs_rmdir,
114         .vop_setattr =          nwfs_setattr,
115         .vop_strategy =         nwfs_strategy,
116         .vop_old_symlink =      nwfs_symlink,
117         .vop_unlock =           vop_stdunlock,
118         .vop_write =            nwfs_write
119 };
120
121 /*
122  * nwfs_access vnode op
123  * for now just return ok
124  *
125  * nwfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
126  *              struct thread *a_td)
127  */
128 static int
129 nwfs_access(struct vop_access_args *ap)
130 {
131         struct vnode *vp = ap->a_vp;
132         struct ucred *cred = ap->a_cred;
133         u_int mode = ap->a_mode;
134         struct nwmount *nmp = VTONWFS(vp);
135         int error = 0;
136
137         NCPVNDEBUG("\n");
138         if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
139                 switch (vp->v_type) {
140                     case VREG: case VDIR: case VLNK:
141                         return (EROFS);
142                     default:
143                         break;
144                 }
145         }
146         if (cred->cr_uid == 0)
147                 return 0;
148         if (cred->cr_uid != nmp->m.uid) {
149                 mode >>= 3;
150                 if (!groupmember(nmp->m.gid, cred))
151                         mode >>= 3;
152         }
153         error = (((vp->v_type == VREG) ? nmp->m.file_mode : nmp->m.dir_mode) & mode) == mode ? 0 : EACCES;
154         return error;
155 }
156 /*
157  * nwfs_open vnode op
158  *
159  * nwfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
160  */
161 /* ARGSUSED */
162 static int
163 nwfs_open(struct vop_open_args *ap)
164 {
165         thread_t td = curthread; /* XXX */
166         struct vnode *vp = ap->a_vp;
167         int mode = ap->a_mode;
168         struct nwnode *np = VTONW(vp);
169         struct ncp_open_info no;
170         struct nwmount *nmp = VTONWFS(vp);
171         struct vattr vattr;
172         int error, nwm;
173
174         NCPVNDEBUG("%s,%d\n",np->n_name, np->opened);
175         if (vp->v_type != VREG && vp->v_type != VDIR) { 
176                 NCPFATAL("open vtype = %d\n", vp->v_type);
177                 return (EACCES);
178         }
179         if (vp->v_type == VDIR) return 0;       /* nothing to do now */
180         if (np->n_flag & NMODIFIED) {
181                 if ((error = nwfs_vinvalbuf(vp, V_SAVE, 1)) == EINTR)
182                         return (error);
183                 np->n_atime = 0;
184                 error = VOP_GETATTR(vp, &vattr);
185                 if (error) 
186                         return (error);
187                 np->n_mtime = vattr.va_mtime.tv_sec;
188         } else {
189                 error = VOP_GETATTR(vp, &vattr);
190                 if (error) 
191                         return (error);
192                 if (np->n_mtime != vattr.va_mtime.tv_sec) {
193                         if ((error = nwfs_vinvalbuf(vp, V_SAVE, 1)) == EINTR)
194                                 return (error);
195                         np->n_mtime = vattr.va_mtime.tv_sec;
196                 }
197         }
198         if (np->opened) {
199                 np->opened++;
200                 return (vop_stdopen(ap));
201         }
202         nwm = AR_READ;
203         if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
204                 nwm |= AR_WRITE;
205         error = ncp_open_create_file_or_subdir(nmp, vp, 0, NULL, OC_MODE_OPEN,
206                                                0, nwm, &no, td, ap->a_cred);
207         if (error) {
208                 if (mode & FWRITE)
209                         return EACCES;
210                 nwm = AR_READ;
211                 error = ncp_open_create_file_or_subdir(nmp, vp, 0, NULL, OC_MODE_OPEN, 0,
212                                                    nwm, &no, td, ap->a_cred);
213         }
214         np->n_atime = 0;
215         if (error == 0) {
216                 np->opened++;
217                 np->n_fh = no.fh;
218                 np->n_origfh = no.origfh;
219                 error = vop_stdopen(ap);
220         }
221         return (error);
222 }
223
224 /*
225  * nwfs_close(struct vnode *a_vp, int a_fflag,
226  *            struct ucred *a_cred, struct thread *a_td)
227  */
228 static int
229 nwfs_close(struct vop_close_args *ap)
230 {
231         thread_t td = curthread; /* XXX */
232         struct vnode *vp = ap->a_vp;
233         struct nwnode *np = VTONW(vp);
234         int error;
235
236         NCPVNDEBUG("name=%s,td=%p,c=%d\n",np->n_name,ap->a_td,np->opened);
237
238         error = 0;
239         if (vp->v_type == VDIR)
240                 goto done;
241         if (np->opened == 0)
242                 goto done;
243         error = nwfs_vinvalbuf(vp, V_SAVE, 1);
244         if (np->opened == 0) {
245                 error = 0;      /* huh? */
246                 goto done;
247         }
248         if (--np->opened == 0) {
249                 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, 
250                    td, proc0.p_ucred);
251         } 
252         np->n_atime = 0;
253 done:
254         vop_stdclose(ap);
255         return (error);
256 }
257
258 /*
259  * nwfs_getattr call from vfs.
260  *
261  * nwfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
262  *              struct thread *a_td)
263  */
264 static int
265 nwfs_getattr(struct vop_getattr_args *ap)
266 {
267         thread_t td = curthread; /* XXX */
268         struct vnode *vp = ap->a_vp;
269         struct nwnode *np = VTONW(vp);
270         struct vattr *va=ap->a_vap;
271         struct nwmount *nmp = VTONWFS(vp);
272         struct nw_entry_info fattr;
273         int error;
274         u_int32_t oldsize;
275
276         NCPVNDEBUG("%lx:%d: '%s' %d\n", (long)vp, nmp->n_volume, np->n_name, (vp->v_flag & VROOT) != 0);
277         error = nwfs_attr_cachelookup(vp,va);
278         if (!error) return 0;
279         NCPVNDEBUG("not in cache\n");
280         oldsize = np->n_size;
281         if (np->n_flag & NVOLUME) {
282                 error = ncp_obtain_info(nmp, np->n_fid.f_id, 0, NULL, &fattr,
283                     td,proc0.p_ucred);
284         } else {
285                 error = ncp_obtain_info(nmp, np->n_fid.f_parent, np->n_nmlen, 
286                     np->n_name, &fattr, td, proc0.p_ucred);
287         }
288         if (error) {
289                 NCPVNDEBUG("error %d\n", error);
290                 return error;
291         }
292         nwfs_attr_cacheenter(vp, &fattr);
293         *va = np->n_vattr;
294         if (np->opened)
295                 np->n_size = oldsize;
296         return (0);
297 }
298 /*
299  * nwfs_setattr call from vfs.
300  *
301  * nwfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
302  */
303 static int
304 nwfs_setattr(struct vop_setattr_args *ap)
305 {
306         thread_t td = curthread; /* XXX */
307         struct vnode *vp = ap->a_vp;
308         struct nwnode *np = VTONW(vp);
309         struct vattr *vap = ap->a_vap;
310         u_quad_t tsize=0;
311         int error = 0;
312
313         NCPVNDEBUG("\n");
314         if (vap->va_flags != VNOVAL)
315                 return (EOPNOTSUPP);
316         /*
317          * Disallow write attempts if the filesystem is mounted read-only.
318          */
319         if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL || 
320              vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
321              vap->va_mode != (mode_t)VNOVAL) &&(vp->v_mount->mnt_flag & MNT_RDONLY))
322                 return (EROFS);
323         if (vap->va_size != VNOVAL) {
324                 switch (vp->v_type) {
325                 case VDIR:
326                         return (EISDIR);
327                 case VREG:
328                         /*
329                          * Disallow write attempts if the filesystem is
330                          * mounted read-only.
331                          */
332                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
333                                 return (EROFS);
334                         vnode_pager_setsize(vp, (u_long)vap->va_size);
335                         tsize = np->n_size;
336                         np->n_size = vap->va_size;
337                         break;
338                 default:
339                         return EINVAL;
340                 };
341         }
342         error = ncp_setattr(vp, vap, ap->a_cred, td);
343         if (error && vap->va_size != VNOVAL) {
344                 np->n_size = tsize;
345                 vnode_pager_setsize(vp, (u_long)tsize);
346         }
347         np->n_atime = 0;        /* invalidate cache */
348         VOP_GETATTR(vp, vap);
349         np->n_mtime = vap->va_mtime.tv_sec;
350         return (0);
351 }
352
353 /*
354  * nwfs_read call.
355  *
356  * nwfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
357  *           struct ucred *a_cred)
358  */
359 static int
360 nwfs_read(struct vop_read_args *ap)
361 {
362         struct vnode *vp = ap->a_vp;
363         struct uio *uio=ap->a_uio;
364         int error;
365         NCPVNDEBUG("nwfs_read:\n");
366
367         if (vp->v_type != VREG && vp->v_type != VDIR)
368                 return (EPERM);
369         error = nwfs_readvnode(vp, uio, ap->a_cred);
370         return error;
371 }
372
373 /*
374  * nwfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
375  *            struct ucred *a_cred)
376  */
377 static int
378 nwfs_write(struct vop_write_args *ap)
379 {
380         struct vnode *vp = ap->a_vp;
381         struct uio *uio = ap->a_uio;
382         int error;
383
384         NCPVNDEBUG("%d,ofs=%d,sz=%d\n",vp->v_type, (int)uio->uio_offset, uio->uio_resid);
385
386         if (vp->v_type != VREG)
387                 return (EPERM);
388         error = nwfs_writevnode(vp, uio, ap->a_cred,ap->a_ioflag);
389         return(error);
390 }
391 /*
392  * nwfs_create call
393  * Create a regular file. On entry the directory to contain the file being
394  * created is locked.  We must release before we return. 
395  *
396  * nwfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
397  *              struct componentname *a_cnpl, struct vattr *a_vap)
398  */
399 static int
400 nwfs_create(struct vop_old_create_args *ap)
401 {
402         struct vnode *dvp = ap->a_dvp;
403         struct vattr *vap = ap->a_vap;
404         struct vnode **vpp=ap->a_vpp;
405         struct componentname *cnp = ap->a_cnp;
406         struct vnode *vp = (struct vnode *)0;
407         int error = 0, fmode;
408         struct vattr vattr;
409         struct nwnode *np;
410         struct ncp_open_info no;
411         struct nwmount *nmp=VTONWFS(dvp);
412         ncpfid fid;
413         
414
415         NCPVNDEBUG("\n");
416         *vpp = NULL;
417         if (vap->va_type == VSOCK)
418                 return (EOPNOTSUPP);
419         if ((error = VOP_GETATTR(dvp, &vattr))) {
420                 return (error);
421         }
422         fmode = AR_READ | AR_WRITE;
423 /*      if (vap->va_vaflags & VA_EXCLUSIVE)
424                 fmode |= AR_DENY_READ | AR_DENY_WRITE;*/
425         
426         error = ncp_open_create_file_or_subdir(nmp,dvp,cnp->cn_namelen,cnp->cn_nameptr, 
427                            OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
428                            0, fmode, &no, cnp->cn_td, cnp->cn_cred);
429         if (!error) {
430                 error = ncp_close_file(NWFSTOCONN(nmp), &no.fh, cnp->cn_td,cnp->cn_cred);
431                 fid.f_parent = VTONW(dvp)->n_fid.f_id;
432                 fid.f_id = no.fattr.dirEntNum;
433                 error = nwfs_nget(VTOVFS(dvp), fid, &no.fattr, dvp, &vp);
434                 if (!error) {
435                         np = VTONW(vp);
436                         np->opened = 0;
437                         *vpp = vp;
438                 }
439         }
440         return (error);
441 }
442
443 /*
444  * nwfs_remove call. It isn't possible to emulate UFS behaivour because
445  * NetWare doesn't allow delete/rename operations on an opened file.
446  *
447  * nwfs_remove(struct vnode *a_dvp,
448  *             struct vnode *a_vp, struct componentname *a_cnp)
449  */
450 static int
451 nwfs_remove(struct vop_old_remove_args *ap)
452 {
453         struct vnode *vp = ap->a_vp;
454         struct vnode *dvp = ap->a_dvp;
455         struct componentname *cnp = ap->a_cnp;
456         struct nwnode *np = VTONW(vp);
457         struct nwmount *nmp = VTONWFS(vp);
458         int error;
459
460         if (vp->v_type == VDIR || np->opened || vp->v_usecount != 1) {
461                 error = EPERM;
462         } else if (!ncp_conn_valid(NWFSTOCONN(nmp))) {
463                 error = EIO;
464         } else {
465                 error = ncp_DeleteNSEntry(nmp, VTONW(dvp)->n_fid.f_id,
466                     cnp->cn_namelen,cnp->cn_nameptr,cnp->cn_td,cnp->cn_cred);
467                 if (error == 0)
468                         np->n_flag |= NSHOULDFREE;
469                 else if (error == 0x899c)
470                         error = EACCES;
471         }
472         return (error);
473 }
474
475 /*
476  * nwfs_file rename call
477  *
478  * nwfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
479  *              struct componentname *a_fcnp, struct vnode *a_tdvp,
480  *              struct vnode *a_tvp, struct componentname *a_tcnp)
481  */
482 static int
483 nwfs_rename(struct vop_old_rename_args *ap)
484 {
485         struct vnode *fvp = ap->a_fvp;
486         struct vnode *tvp = ap->a_tvp;
487         struct vnode *fdvp = ap->a_fdvp;
488         struct vnode *tdvp = ap->a_tdvp;
489         struct componentname *tcnp = ap->a_tcnp;
490         struct componentname *fcnp = ap->a_fcnp;
491         struct nwmount *nmp=VTONWFS(fvp);
492         u_int16_t oldtype = 6;
493         int error=0;
494
495         /* Check for cross-device rename */
496         if ((fvp->v_mount != tdvp->v_mount) ||
497             (tvp && (fvp->v_mount != tvp->v_mount))) {
498                 error = EXDEV;
499                 goto out;
500         }
501
502         if (tvp && tvp->v_usecount > 1) {
503                 error = EBUSY;
504                 goto out;
505         }
506         if (tvp && tvp != fvp) {
507                 error = ncp_DeleteNSEntry(nmp, VTONW(tdvp)->n_fid.f_id,
508                     tcnp->cn_namelen, tcnp->cn_nameptr, 
509                     tcnp->cn_td, tcnp->cn_cred);
510                 if (error == 0x899c) error = EACCES;
511                 if (error)
512                         goto out;
513         }
514         if (fvp->v_type == VDIR) {
515                 oldtype |= NW_TYPE_SUBDIR;
516         } else if (fvp->v_type == VREG) {
517                 oldtype |= NW_TYPE_FILE;
518         } else
519                 return EINVAL;
520         error = ncp_nsrename(NWFSTOCONN(nmp), nmp->n_volume, nmp->name_space, 
521                 oldtype, &nmp->m.nls,
522                 VTONW(fdvp)->n_fid.f_id, fcnp->cn_nameptr, fcnp->cn_namelen,
523                 VTONW(tdvp)->n_fid.f_id, tcnp->cn_nameptr, tcnp->cn_namelen,
524                 tcnp->cn_td,tcnp->cn_cred);
525
526         if (error == 0x8992)
527                 error = EEXIST;
528 out:
529         if (tdvp == tvp)
530                 vrele(tdvp);
531         else
532                 vput(tdvp);
533         if (tvp)
534                 vput(tvp);
535         vrele(fdvp);
536         vrele(fvp);
537         nwfs_attr_cacheremove(fdvp);
538         nwfs_attr_cacheremove(tdvp);
539         /*
540          * Need to get rid of old vnodes, because netware will change
541          * file id on rename
542          */
543         vgone(fvp);
544         if (tvp)
545                 vgone(tvp);
546         /*
547          * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
548          */
549         if (error == ENOENT)
550                 error = 0;
551         return (error);
552 }
553
554 /*
555  * nwfs hard link create call
556  * Netware filesystems don't know what links are.
557  *
558  * nwfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
559  *           struct componentname *a_cnp)
560  */
561 static int
562 nwfs_link(struct vop_old_link_args *ap)
563 {
564         return EOPNOTSUPP;
565 }
566
567 /*
568  * nwfs_symlink link create call
569  * Netware filesystems don't know what symlinks are.
570  *
571  * nwfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
572  *              struct componentname *a_cnp, struct vattr *a_vap,
573  *              char *a_target)
574  */
575 static int
576 nwfs_symlink(struct vop_old_symlink_args *ap)
577 {
578         return (EOPNOTSUPP);
579 }
580
581 static int nwfs_mknod(struct vop_old_mknod_args *ap)
582 {
583         return (EOPNOTSUPP);
584 }
585
586 /*
587  * nwfs_mkdir call
588  *
589  * nwfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
590  *              struct componentname *a_cnp, struct vattr *a_vap)
591  */
592 static int
593 nwfs_mkdir(struct vop_old_mkdir_args *ap)
594 {
595         struct vnode *dvp = ap->a_dvp;
596 /*      struct vattr *vap = ap->a_vap;*/
597         struct componentname *cnp = ap->a_cnp;
598         int len=cnp->cn_namelen;
599         struct ncp_open_info no;
600         struct nwnode *np;
601         struct vnode *newvp = (struct vnode *)0;
602         ncpfid fid;
603         int error = 0;
604         struct vattr vattr;
605         char *name=cnp->cn_nameptr;
606
607         if ((error = VOP_GETATTR(dvp, &vattr))) {
608                 return (error);
609         }       
610         if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.')))) {
611                 return EEXIST;
612         }
613         if (ncp_open_create_file_or_subdir(VTONWFS(dvp),dvp, cnp->cn_namelen,
614                         cnp->cn_nameptr,OC_MODE_CREATE, aDIR, 0xffff,
615                         &no, cnp->cn_td, cnp->cn_cred) != 0) {
616                 error = EACCES;
617         } else {
618                 error = 0;
619         }
620         if (!error) {
621                 fid.f_parent = VTONW(dvp)->n_fid.f_id;
622                 fid.f_id = no.fattr.dirEntNum;
623                 error = nwfs_nget(VTOVFS(dvp), fid, &no.fattr, dvp, &newvp);
624                 if (!error) {
625                         np = VTONW(newvp);
626                         newvp->v_type = VDIR;
627                         *ap->a_vpp = newvp;
628                 }
629         }
630         return (error);
631 }
632
633 /*
634  * nwfs_remove directory call
635  *
636  * nwfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
637  *            struct componentname *a_cnp)
638  */
639 static int
640 nwfs_rmdir(struct vop_old_rmdir_args *ap)
641 {
642         struct vnode *vp = ap->a_vp;
643         struct vnode *dvp = ap->a_dvp;
644         struct componentname *cnp = ap->a_cnp;
645         struct nwnode *np = VTONW(vp);
646         struct nwmount *nmp = VTONWFS(vp);
647         struct nwnode *dnp = VTONW(dvp);
648         int error = EIO;
649
650         if (dvp == vp)
651                 return EINVAL;
652
653         error = ncp_DeleteNSEntry(nmp, dnp->n_fid.f_id, 
654                 cnp->cn_namelen, cnp->cn_nameptr,cnp->cn_td,cnp->cn_cred);
655         if (error == 0)
656                 np->n_flag |= NSHOULDFREE;
657         else if (error == NWE_DIR_NOT_EMPTY)
658                 error = ENOTEMPTY;
659         dnp->n_flag |= NMODIFIED;
660         nwfs_attr_cacheremove(dvp);
661         return (error);
662 }
663
664 /*
665  * nwfs_readdir call
666  *
667  * nwfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
668  *              int *a_eofflag, u_long *a_cookies, int a_ncookies)
669  */
670 static int
671 nwfs_readdir(struct vop_readdir_args *ap)
672 {
673         struct vnode *vp = ap->a_vp;
674         struct uio *uio = ap->a_uio;
675         int error;
676
677         if (vp->v_type != VDIR)
678                 return (EPERM);
679         if (ap->a_ncookies) {
680                 printf("nwfs_readdir: no support for cookies now...");
681                 return (EOPNOTSUPP);
682         }
683
684         error = nwfs_readvnode(vp, uio, ap->a_cred);
685         return error;
686 }
687
688 /*
689  * nwfs_fsync(struct vnode *a_vp, struct ucred *a_cred,
690  *            int a_waitfor, struct thread *a_td)
691  */
692 /* ARGSUSED */
693 static int
694 nwfs_fsync(struct vop_fsync_args *ap)
695 {
696 /*      return (nfs_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/
697     return (0);
698 }
699
700 /*
701  * nwfs_print(struct vnode *a_vp)
702  */
703 /* ARGSUSED */
704 static int
705 nwfs_print(struct vop_print_args *ap)
706 {
707         struct vnode *vp = ap->a_vp;
708         struct nwnode *np = VTONW(vp);
709
710         printf("nwfs node: name = '%s', fid = %d, pfid = %d\n",
711             np->n_name, np->n_fid.f_id, np->n_fid.f_parent);
712         return (0);
713 }
714
715 /*
716  * nwfs_pathconf(struct vnode *vp, int name, register_t *retval)
717  */
718 static int
719 nwfs_pathconf(struct vop_pathconf_args *ap)
720 {
721         int name=ap->a_name, error=0;
722         register_t *retval=ap->a_retval;
723         
724         switch(name){
725                 case _PC_LINK_MAX:
726                         *retval=0;
727                         break;
728                 case _PC_NAME_MAX:
729                         *retval=NCP_MAX_FILENAME; /* XXX from nwfsnode */
730                         break;
731                 case _PC_PATH_MAX:
732                         *retval=NCP_MAXPATHLEN; /* XXX from nwfsnode */
733                         break;
734                 default:
735                         error=EINVAL;
736         }
737         return(error);
738 }
739
740 /*
741  * nwfs_strategy(struct vnode *a_vp, struct bio *a_bio)
742  */
743 static int
744 nwfs_strategy(struct vop_strategy_args *ap)
745 {
746         struct bio *bio = ap->a_bio;
747         struct buf *bp = bio->bio_buf;
748         int error = 0;
749         struct thread *td = NULL;
750
751         NCPVNDEBUG("\n");
752         if ((bp->b_flags & B_ASYNC) == 0)
753                 td = curthread;         /* YYY dunno if this is legal */
754         /*
755          * If the op is asynchronous and an i/o daemon is waiting
756          * queue the request, wake it up and wait for completion
757          * otherwise just do it ourselves.
758          */
759         if ((bp->b_flags & B_ASYNC) == 0 )
760                 error = nwfs_doio(ap->a_vp, bio, proc0.p_ucred, td);
761         return (error);
762 }
763
764 /*
765  * nwfs_bmap(struct vnode *a_vp, off_t a_loffset, struct vnode **a_vpp,
766  *           off_t *a_doffsetp, int *a_runp, int *a_runb)
767  */
768 static int
769 nwfs_bmap(struct vop_bmap_args *ap)
770 {
771         struct vnode *vp = ap->a_vp;
772
773         if (ap->a_vpp != NULL)
774                 *ap->a_vpp = vp;
775         if (ap->a_doffsetp != NULL)
776                 *ap->a_doffsetp = ap->a_loffset;
777         if (ap->a_runp != NULL)
778                 *ap->a_runp = 0;
779         if (ap->a_runb != NULL)
780                 *ap->a_runb = 0;
781         return (0);
782 }
783
784 int
785 nwfs_nget(struct mount *mp, ncpfid fid, const struct nw_entry_info *fap,
786           struct vnode *dvp, struct vnode **vpp)
787 {
788         int error;
789         struct nwnode *newnp;
790         struct vnode *vp;
791
792         *vpp = NULL;
793         error = nwfs_allocvp(mp, fid, &vp);
794         if (error)
795                 return error;
796         newnp = VTONW(vp);
797         if (fap) {
798                 newnp->n_attr = fap->attributes;
799                 vp->v_type = newnp->n_attr & aDIR ? VDIR : VREG;
800                 nwfs_attr_cacheenter(vp, fap);
801         }
802         if (dvp) {
803                 newnp->n_parent = VTONW(dvp)->n_fid;
804                 if ((newnp->n_flag & NNEW) && vp->v_type == VDIR) {
805                         if ((dvp->v_flag & VROOT) == 0) {
806                                 newnp->n_refparent = 1;
807                                 vref(dvp);      /* vhold */
808                         }
809                 }
810         } else {
811                 if ((newnp->n_flag & NNEW) && vp->v_type == VREG)
812                         printf("new vnode '%s' borned without parent ?\n",newnp->n_name);
813         }
814         newnp->n_flag &= ~NNEW;
815         *vpp = vp;
816         return 0;
817 }
818
819 /*
820  * How to keep the brain busy ...
821  * Currently lookup routine can make two lookup for vnode. This can be
822  * avoided by reorg the code.
823  *
824  * nwfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
825  *             struct componentname *a_cnp)
826  */
827 int
828 nwfs_lookup(struct vop_old_lookup_args *ap)
829 {
830         struct componentname *cnp = ap->a_cnp;
831         struct vnode *dvp = ap->a_dvp;
832         struct vnode **vpp = ap->a_vpp;
833         int flags = cnp->cn_flags;
834         struct vnode *vp;
835         struct nwmount *nmp;
836         struct mount *mp = dvp->v_mount;
837         struct nwnode *dnp, *npp;
838         struct nw_entry_info fattr, *fap;
839         ncpfid fid;
840         int nameiop=cnp->cn_nameiop;
841         int lockparent, wantparent, error = 0, notfound;
842         struct thread *td = cnp->cn_td;
843         char _name[cnp->cn_namelen+1];
844         bcopy(cnp->cn_nameptr,_name,cnp->cn_namelen);
845         _name[cnp->cn_namelen]=0;
846         
847         if (dvp->v_type != VDIR)
848                 return (ENOTDIR);
849         if ((flags & CNP_ISDOTDOT) && (dvp->v_flag & VROOT)) {
850                 printf("nwfs_lookup: invalid '..'\n");
851                 return EIO;
852         }
853
854         NCPVNDEBUG("%d '%s' in '%s' id=d\n", nameiop, _name, 
855                 VTONW(dvp)->n_name/*, VTONW(dvp)->n_name*/);
856
857         if ((mp->mnt_flag & MNT_RDONLY) && nameiop != NAMEI_LOOKUP)
858                 return (EROFS);
859         if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred)))
860                 return (error);
861         lockparent = flags & CNP_LOCKPARENT;
862         wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
863         nmp = VFSTONWFS(mp);
864         dnp = VTONW(dvp);
865 /*
866 printf("dvp %d:%d:%d\n", (int)mp, (int)dvp->v_flag & VROOT, (int)flags & CNP_ISDOTDOT);
867 */
868         error = ncp_pathcheck(cnp->cn_nameptr, cnp->cn_namelen, &nmp->m.nls, 
869             (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) && (nmp->m.nls.opt & NWHP_NOSTRICT) == 0);
870         if (error) 
871             return ENOENT;
872
873         error = 0;
874         *vpp = NULLVP;
875         fap = NULL;
876         if (flags & CNP_ISDOTDOT) {
877                 if (NWCMPF(&dnp->n_parent, &nmp->n_rootent)) {
878                         fid = nmp->n_rootent;
879                         fap = NULL;
880                         notfound = 0;
881                 } else {
882                         error = nwfs_lookupnp(nmp, dnp->n_parent, td, &npp);
883                         if (error) {
884                                 return error;
885                         }
886                         fid = dnp->n_parent;
887                         fap = &fattr;
888                         /*np = *npp;*/
889                         notfound = ncp_obtain_info(nmp, npp->n_dosfid,
890                             0, NULL, fap, td, cnp->cn_cred);
891                 }
892         } else {
893                 fap = &fattr;
894                 notfound = ncp_lookup(dvp, cnp->cn_namelen, cnp->cn_nameptr,
895                         fap, td, cnp->cn_cred);
896                 fid.f_id = fap->dirEntNum;
897                 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
898                         fid.f_parent = dnp->n_fid.f_parent;
899                 } else
900                         fid.f_parent = dnp->n_fid.f_id;
901                 NCPVNDEBUG("call to ncp_lookup returned=%d\n",notfound);
902         }
903         if (notfound && notfound < 0x80 )
904                 return (notfound);      /* hard error */
905         if (notfound) { /* entry not found */
906                 /* Handle RENAME or CREATE case... */
907                 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) && wantparent) {
908                         if (!lockparent)
909                                 VOP_UNLOCK(dvp, 0);
910                         return (EJUSTRETURN);
911                 }
912                 return ENOENT;
913         }/* else {
914                 NCPVNDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
915         }*/
916         /* handle DELETE case ... */
917         if (nameiop == NAMEI_DELETE) {  /* delete last component */
918                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
919                 if (error) return (error);
920                 if (NWCMPF(&dnp->n_fid, &fid)) {        /* we found ourselfs */
921                         vref(dvp);
922                         *vpp = dvp;
923                         return 0;
924                 }
925                 error = nwfs_nget(mp, fid, fap, dvp, &vp);
926                 if (error) return (error);
927                 *vpp = vp;
928                 if (!lockparent) VOP_UNLOCK(dvp, 0);
929                 return (0);
930         }
931         if (nameiop == NAMEI_RENAME && wantparent) {
932                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
933                 if (error) return (error);
934                 if (NWCMPF(&dnp->n_fid, &fid)) return EISDIR;
935                 error = nwfs_nget(mp, fid, fap, dvp, &vp);
936                 if (error) return (error);
937                 *vpp = vp;
938                 if (!lockparent)
939                         VOP_UNLOCK(dvp, 0);
940                 return (0);
941         }
942         if (flags & CNP_ISDOTDOT) {
943                 VOP_UNLOCK(dvp, 0);     /* race to get the inode */
944                 error = nwfs_nget(mp, fid, NULL, NULL, &vp);
945                 if (error) {
946                         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
947                         return (error);
948                 }
949                 if (lockparent && (error = vn_lock(dvp, LK_EXCLUSIVE))) {
950                         vput(vp);
951                         return (error);
952                 }
953                 *vpp = vp;
954         } else if (NWCMPF(&dnp->n_fid, &fid)) {
955                 vref(dvp);
956                 *vpp = dvp;
957         } else {
958                 error = nwfs_nget(mp, fid, fap, dvp, &vp);
959                 if (error) return (error);
960                 *vpp = vp;
961                 NCPVNDEBUG("lookup: getnewvp!\n");
962                 if (!lockparent)
963                         VOP_UNLOCK(dvp, 0);
964         }
965 #if 0
966         /* XXX MOVE TO NREMOVE */
967         if ((cnp->cn_flags & CNP_MAKEENTRY)) {
968                 VTONW(*vpp)->n_ctime = VTONW(*vpp)->n_vattr.va_ctime.tv_sec;
969                 /* XXX */
970         }
971 #endif
972         return (0);
973 }