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