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