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