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