Style(9):
[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 $
32b5c502 33 * $DragonFly: src/sys/vfs/nwfs/nwfs_vnops.c,v 1.19 2004/11/03 22:07:21 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 */
0961aa92 87struct vnodeopv_entry_desc nwfs_vnodeop_entries[] = {
2d3e977e
MD
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 },
984263bc
MD
119 { NULL, NULL }
120};
984263bc
MD
121
122/*
123 * nwfs_access vnode op
124 * for now just return ok
a9bb80e0
CP
125 *
126 * nwfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
127 * struct thread *a_td)
984263bc
MD
128 */
129static int
a9bb80e0 130nwfs_access(struct vop_access_args *ap)
984263bc
MD
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
a9bb80e0
CP
159 *
160 * nwfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
161 * struct thread *a_td)
984263bc
MD
162 */
163/* ARGSUSED */
164static int
a9bb80e0 165nwfs_open(struct vop_open_args *ap)
984263bc
MD
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) {
3b568787 182 if ((error = nwfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
984263bc
MD
183 return (error);
184 np->n_atime = 0;
3b568787 185 error = VOP_GETATTR(vp, &vattr, ap->a_td);
984263bc
MD
186 if (error) return (error);
187 np->n_mtime = vattr.va_mtime.tv_sec;
188 } else {
3b568787 189 error = VOP_GETATTR(vp, &vattr, ap->a_td);
984263bc
MD
190 if (error) return (error);
191 if (np->n_mtime != vattr.va_mtime.tv_sec) {
3b568787 192 if ((error = nwfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
984263bc
MD
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,
dadab5e9 205 0, nwm, &no, ap->a_td, ap->a_cred);
984263bc
MD
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,
dadab5e9 211 nwm, &no, ap->a_td,ap->a_cred);
984263bc
MD
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
a9bb80e0
CP
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 */
984263bc 226static int
a9bb80e0 227nwfs_close(struct vop_close_args *ap)
984263bc
MD
228{
229 struct vnode *vp = ap->a_vp;
230 struct nwnode *np = VTONW(vp);
231 int error;
232
dadab5e9 233 NCPVNDEBUG("name=%s,td=%p,c=%d\n",np->n_name,ap->a_td,np->opened);
984263bc
MD
234
235 if (vp->v_type == VDIR) return 0; /* nothing to do now */
236 error = 0;
984263bc 237 if (np->opened == 0) {
984263bc
MD
238 return 0;
239 }
3b568787 240 error = nwfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
984263bc 241 if (np->opened == 0) {
984263bc
MD
242 return 0;
243 }
244 if (--np->opened == 0) {
984263bc 245 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh,
3b568787 246 ap->a_td, proc0.p_ucred);
5fd012e0 247 }
984263bc
MD
248 np->n_atime = 0;
249 return (error);
250}
251
252/*
253 * nwfs_getattr call from vfs.
a9bb80e0
CP
254 *
255 * nwfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
256 * struct thread *a_td)
984263bc
MD
257 */
258static int
a9bb80e0 259nwfs_getattr(struct vop_getattr_args *ap)
984263bc
MD
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,
3b568787 276 ap->a_td,proc0.p_ucred);
984263bc
MD
277 } else {
278 error = ncp_obtain_info(nmp, np->n_fid.f_parent, np->n_nmlen,
3b568787 279 np->n_name, &fattr, ap->a_td, proc0.p_ucred);
984263bc
MD
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.
a9bb80e0
CP
293 *
294 * nwfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
295 * struct thread *a_td)
984263bc
MD
296 */
297static int
a9bb80e0 298nwfs_setattr(struct vop_setattr_args *ap)
984263bc
MD
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 }
dadab5e9 335 error = ncp_setattr(vp, vap, ap->a_cred, ap->a_td);
984263bc
MD
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 */
3b568787 341 VOP_GETATTR(vp, vap, ap->a_td);
984263bc
MD
342 np->n_mtime = vap->va_mtime.tv_sec;
343 return (0);
344}
a9bb80e0 345
984263bc
MD
346/*
347 * nwfs_read call.
a9bb80e0
CP
348 *
349 * nwfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
350 * struct ucred *a_cred)
984263bc
MD
351 */
352static int
a9bb80e0 353nwfs_read(struct vop_read_args *ap)
984263bc
MD
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
a9bb80e0
CP
366/*
367 * nwfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
368 * struct ucred *a_cred)
369 */
984263bc 370static int
a9bb80e0 371nwfs_write(struct vop_write_args *ap)
984263bc
MD
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. We must also free
388 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
389 * only if the SAVESTART bit in cn_flags is clear on success.
a9bb80e0
CP
390 *
391 * nwfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
392 * struct componentname *a_cnpl, struct vattr *a_vap)
984263bc
MD
393 */
394static int
a9bb80e0 395nwfs_create(struct vop_create_args *ap)
984263bc
MD
396{
397 struct vnode *dvp = ap->a_dvp;
398 struct vattr *vap = ap->a_vap;
399 struct vnode **vpp=ap->a_vpp;
400 struct componentname *cnp = ap->a_cnp;
401 struct vnode *vp = (struct vnode *)0;
402 int error = 0, fmode;
403 struct vattr vattr;
404 struct nwnode *np;
405 struct ncp_open_info no;
406 struct nwmount *nmp=VTONWFS(dvp);
407 ncpfid fid;
408
409
410 NCPVNDEBUG("\n");
411 *vpp = NULL;
412 if (vap->va_type == VSOCK)
413 return (EOPNOTSUPP);
3b568787 414 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td))) {
984263bc
MD
415 return (error);
416 }
417 fmode = AR_READ | AR_WRITE;
418/* if (vap->va_vaflags & VA_EXCLUSIVE)
419 fmode |= AR_DENY_READ | AR_DENY_WRITE;*/
420
421 error = ncp_open_create_file_or_subdir(nmp,dvp,cnp->cn_namelen,cnp->cn_nameptr,
422 OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE,
dadab5e9 423 0, fmode, &no, cnp->cn_td, cnp->cn_cred);
984263bc 424 if (!error) {
dadab5e9 425 error = ncp_close_file(NWFSTOCONN(nmp), &no.fh, cnp->cn_td,cnp->cn_cred);
984263bc
MD
426 fid.f_parent = VTONW(dvp)->n_fid.f_id;
427 fid.f_id = no.fattr.dirEntNum;
428 error = nwfs_nget(VTOVFS(dvp), fid, &no.fattr, dvp, &vp);
429 if (!error) {
430 np = VTONW(vp);
431 np->opened = 0;
432 *vpp = vp;
433 }
2b69e610 434 if (cnp->cn_flags & CNP_MAKEENTRY)
21739618 435 cache_enter(dvp, vp, cnp);
984263bc
MD
436 }
437 return (error);
438}
439
440/*
441 * nwfs_remove call. It isn't possible to emulate UFS behaivour because
442 * NetWare doesn't allow delete/rename operations on an opened file.
a9bb80e0
CP
443 *
444 * nwfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
445 * struct vnode *a_vp, struct componentname *a_cnp)
984263bc
MD
446 */
447static int
a9bb80e0 448nwfs_remove(struct vop_remove_args *ap)
984263bc
MD
449{
450 struct vnode *vp = ap->a_vp;
451 struct vnode *dvp = ap->a_dvp;
452 struct componentname *cnp = ap->a_cnp;
453 struct nwnode *np = VTONW(vp);
454 struct nwmount *nmp = VTONWFS(vp);
455 int error;
456
457 if (vp->v_type == VDIR || np->opened || vp->v_usecount != 1) {
458 error = EPERM;
459 } else if (!ncp_conn_valid(NWFSTOCONN(nmp))) {
460 error = EIO;
461 } else {
462 cache_purge(vp);
463 error = ncp_DeleteNSEntry(nmp, VTONW(dvp)->n_fid.f_id,
dadab5e9 464 cnp->cn_namelen,cnp->cn_nameptr,cnp->cn_td,cnp->cn_cred);
984263bc
MD
465 if (error == 0)
466 np->n_flag |= NSHOULDFREE;
467 else if (error == 0x899c)
468 error = EACCES;
469 }
470 return (error);
471}
472
473/*
474 * nwfs_file rename call
a9bb80e0
CP
475 *
476 * nwfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
477 * struct componentname *a_fcnp, struct vnode *a_tdvp,
478 * struct vnode *a_tvp, struct componentname *a_tcnp)
984263bc
MD
479 */
480static int
a9bb80e0 481nwfs_rename(struct vop_rename_args *ap)
984263bc
MD
482{
483 struct vnode *fvp = ap->a_fvp;
484 struct vnode *tvp = ap->a_tvp;
485 struct vnode *fdvp = ap->a_fdvp;
486 struct vnode *tdvp = ap->a_tdvp;
487 struct componentname *tcnp = ap->a_tcnp;
488 struct componentname *fcnp = ap->a_fcnp;
489 struct nwmount *nmp=VTONWFS(fvp);
490 u_int16_t oldtype = 6;
491 int error=0;
492
493 /* Check for cross-device rename */
494 if ((fvp->v_mount != tdvp->v_mount) ||
495 (tvp && (fvp->v_mount != tvp->v_mount))) {
496 error = EXDEV;
497 goto out;
498 }
499
500 if (tvp && tvp->v_usecount > 1) {
501 error = EBUSY;
502 goto out;
503 }
504 if (tvp && tvp != fvp) {
505 error = ncp_DeleteNSEntry(nmp, VTONW(tdvp)->n_fid.f_id,
506 tcnp->cn_namelen, tcnp->cn_nameptr,
dadab5e9 507 tcnp->cn_td, tcnp->cn_cred);
984263bc
MD
508 if (error == 0x899c) error = EACCES;
509 if (error)
510 goto out;
511 }
512 if (fvp->v_type == VDIR) {
513 oldtype |= NW_TYPE_SUBDIR;
514 } else if (fvp->v_type == VREG) {
515 oldtype |= NW_TYPE_FILE;
516 } else
517 return EINVAL;
518 error = ncp_nsrename(NWFSTOCONN(nmp), nmp->n_volume, nmp->name_space,
519 oldtype, &nmp->m.nls,
520 VTONW(fdvp)->n_fid.f_id, fcnp->cn_nameptr, fcnp->cn_namelen,
521 VTONW(tdvp)->n_fid.f_id, tcnp->cn_nameptr, tcnp->cn_namelen,
dadab5e9 522 tcnp->cn_td,tcnp->cn_cred);
984263bc
MD
523
524 if (error == 0x8992)
525 error = EEXIST;
526 if (fvp->v_type == VDIR) {
527 if (tvp != NULL && tvp->v_type == VDIR)
528 cache_purge(tdvp);
529 cache_purge(fdvp);
530 }
531out:
532 if (tdvp == tvp)
533 vrele(tdvp);
534 else
535 vput(tdvp);
536 if (tvp)
537 vput(tvp);
538 vrele(fdvp);
539 vrele(fvp);
540 nwfs_attr_cacheremove(fdvp);
541 nwfs_attr_cacheremove(tdvp);
542 /*
543 * Need to get rid of old vnodes, because netware will change
544 * file id on rename
545 */
546 vgone(fvp);
547 if (tvp)
548 vgone(tvp);
549 /*
550 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
551 */
552 if (error == ENOENT)
553 error = 0;
554 return (error);
555}
556
557/*
558 * nwfs hard link create call
559 * Netware filesystems don't know what links are.
a9bb80e0
CP
560 *
561 * nwfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
562 * struct componentname *a_cnp)
984263bc
MD
563 */
564static int
a9bb80e0 565nwfs_link(struct vop_link_args *ap)
984263bc
MD
566{
567 return EOPNOTSUPP;
568}
569
570/*
571 * nwfs_symlink link create call
572 * Netware filesystems don't know what symlinks are.
a9bb80e0
CP
573 *
574 * nwfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
575 * struct componentname *a_cnp, struct vattr *a_vap,
576 * char *a_target)
984263bc
MD
577 */
578static int
a9bb80e0 579nwfs_symlink(struct vop_symlink_args *ap)
984263bc
MD
580{
581 return (EOPNOTSUPP);
582}
583
a9bb80e0 584static int nwfs_mknod(struct vop_mknod_args *ap)
984263bc
MD
585{
586 return (EOPNOTSUPP);
587}
588
589/*
590 * nwfs_mkdir call
a9bb80e0
CP
591 *
592 * nwfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
593 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
594 */
595static int
a9bb80e0 596nwfs_mkdir(struct vop_mkdir_args *ap)
984263bc
MD
597{
598 struct vnode *dvp = ap->a_dvp;
599/* struct vattr *vap = ap->a_vap;*/
600 struct componentname *cnp = ap->a_cnp;
601 int len=cnp->cn_namelen;
602 struct ncp_open_info no;
603 struct nwnode *np;
604 struct vnode *newvp = (struct vnode *)0;
605 ncpfid fid;
606 int error = 0;
607 struct vattr vattr;
608 char *name=cnp->cn_nameptr;
609
3b568787 610 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td))) {
984263bc
MD
611 return (error);
612 }
613 if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.')))) {
614 return EEXIST;
615 }
616 if (ncp_open_create_file_or_subdir(VTONWFS(dvp),dvp, cnp->cn_namelen,
617 cnp->cn_nameptr,OC_MODE_CREATE, aDIR, 0xffff,
dadab5e9 618 &no, cnp->cn_td, cnp->cn_cred) != 0) {
984263bc
MD
619 error = EACCES;
620 } else {
621 error = 0;
622 }
623 if (!error) {
624 fid.f_parent = VTONW(dvp)->n_fid.f_id;
625 fid.f_id = no.fattr.dirEntNum;
626 error = nwfs_nget(VTOVFS(dvp), fid, &no.fattr, dvp, &newvp);
627 if (!error) {
628 np = VTONW(newvp);
629 newvp->v_type = VDIR;
630 *ap->a_vpp = newvp;
631 }
632 }
633 return (error);
634}
635
636/*
637 * nwfs_remove directory call
a9bb80e0
CP
638 *
639 * nwfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
640 * struct componentname *a_cnp)
984263bc
MD
641 */
642static int
a9bb80e0 643nwfs_rmdir(struct vop_rmdir_args *ap)
984263bc
MD
644{
645 struct vnode *vp = ap->a_vp;
646 struct vnode *dvp = ap->a_dvp;
647 struct componentname *cnp = ap->a_cnp;
648 struct nwnode *np = VTONW(vp);
649 struct nwmount *nmp = VTONWFS(vp);
650 struct nwnode *dnp = VTONW(dvp);
651 int error = EIO;
652
653 if (dvp == vp)
654 return EINVAL;
655
656 error = ncp_DeleteNSEntry(nmp, dnp->n_fid.f_id,
dadab5e9 657 cnp->cn_namelen, cnp->cn_nameptr,cnp->cn_td,cnp->cn_cred);
984263bc
MD
658 if (error == 0)
659 np->n_flag |= NSHOULDFREE;
660 else if (error == NWE_DIR_NOT_EMPTY)
661 error = ENOTEMPTY;
662 dnp->n_flag |= NMODIFIED;
663 nwfs_attr_cacheremove(dvp);
984263bc
MD
664 cache_purge(vp);
665 return (error);
666}
667
668/*
669 * nwfs_readdir call
a9bb80e0
CP
670 *
671 * nwfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
672 * int *a_eofflag, u_long *a_cookies, int a_ncookies)
984263bc
MD
673 */
674static int
a9bb80e0 675nwfs_readdir(struct vop_readdir_args *ap)
984263bc
MD
676{
677 struct vnode *vp = ap->a_vp;
678 struct uio *uio = ap->a_uio;
679 int error;
680
681 if (vp->v_type != VDIR)
682 return (EPERM);
683 if (ap->a_ncookies) {
684 printf("nwfs_readdir: no support for cookies now...");
685 return (EOPNOTSUPP);
686 }
687
688 error = nwfs_readvnode(vp, uio, ap->a_cred);
689 return error;
690}
a9bb80e0
CP
691
692/*
693 * nwfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
694 * struct ucred *a_cred, int a_waitfor, struct thread *a_td)
695 */
984263bc
MD
696/* ARGSUSED */
697static int
a9bb80e0 698nwfs_fsync(struct vop_fsync_args *ap)
984263bc 699{
dadab5e9 700/* return (nfs_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/
984263bc
MD
701 return (0);
702}
703
a9bb80e0
CP
704/*
705 * nwfs_print(struct vnode *a_vp)
706 */
984263bc 707/* ARGSUSED */
a9bb80e0
CP
708static int
709nwfs_print(struct vop_print_args *ap)
984263bc
MD
710{
711 struct vnode *vp = ap->a_vp;
712 struct nwnode *np = VTONW(vp);
713
714 printf("nwfs node: name = '%s', fid = %d, pfid = %d\n",
715 np->n_name, np->n_fid.f_id, np->n_fid.f_parent);
716 return (0);
717}
718
a9bb80e0
CP
719/*
720 * nwfs_pathconf(struct vnode *vp, int name, register_t *retval)
721 */
722static int
723nwfs_pathconf(struct vop_pathconf_args *ap)
984263bc
MD
724{
725 int name=ap->a_name, error=0;
726 register_t *retval=ap->a_retval;
727
728 switch(name){
729 case _PC_LINK_MAX:
730 *retval=0;
731 break;
732 case _PC_NAME_MAX:
733 *retval=NCP_MAX_FILENAME; /* XXX from nwfsnode */
734 break;
735 case _PC_PATH_MAX:
736 *retval=NCP_MAXPATHLEN; /* XXX from nwfsnode */
737 break;
738 default:
739 error=EINVAL;
740 }
741 return(error);
742}
743
a9bb80e0
CP
744/*
745 * nwfs_strategy(struct buf *a_bp)
746 */
747static int
748nwfs_strategy(struct vop_strategy_args *ap)
984263bc
MD
749{
750 struct buf *bp=ap->a_bp;
984263bc 751 int error = 0;
dadab5e9 752 struct thread *td = NULL;
984263bc
MD
753
754 NCPVNDEBUG("\n");
755 if (bp->b_flags & B_PHYS)
756 panic("nwfs physio");
dadab5e9
MD
757 if ((bp->b_flags & B_ASYNC) == 0)
758 td = curthread; /* YYY dunno if this is legal */
984263bc
MD
759 /*
760 * If the op is asynchronous and an i/o daemon is waiting
761 * queue the request, wake it up and wait for completion
762 * otherwise just do it ourselves.
763 */
764 if ((bp->b_flags & B_ASYNC) == 0 )
3b568787 765 error = nwfs_doio(bp, proc0.p_ucred, td);
984263bc
MD
766 return (error);
767}
768
a9bb80e0
CP
769/*
770 * nwfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
771 * daddr_t *a_bnp, int *a_runp, int *a_runb)
772 */
984263bc 773static int
a9bb80e0 774nwfs_bmap(struct vop_bmap_args *ap)
984263bc
MD
775{
776 struct vnode *vp = ap->a_vp;
777
778 if (ap->a_vpp != NULL)
779 *ap->a_vpp = vp;
780 if (ap->a_bnp != NULL)
781 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
782 if (ap->a_runp != NULL)
783 *ap->a_runp = 0;
784 if (ap->a_runb != NULL)
785 *ap->a_runb = 0;
786 return (0);
787}
788
789int
790nwfs_nget(struct mount *mp, ncpfid fid, struct nw_entry_info *fap,
791 struct vnode *dvp, struct vnode **vpp)
792{
793 int error;
794 struct nwnode *newnp;
795 struct vnode *vp;
796
797 *vpp = NULL;
798 error = nwfs_allocvp(mp, fid, &vp);
799 if (error)
800 return error;
801 newnp = VTONW(vp);
802 if (fap) {
803 newnp->n_attr = fap->attributes;
804 vp->v_type = newnp->n_attr & aDIR ? VDIR : VREG;
805 nwfs_attr_cacheenter(vp, fap);
806 }
807 if (dvp) {
808 newnp->n_parent = VTONW(dvp)->n_fid;
809 if ((newnp->n_flag & NNEW) && vp->v_type == VDIR) {
810 if ((dvp->v_flag & VROOT) == 0) {
811 newnp->n_refparent = 1;
812 vref(dvp); /* vhold */
813 }
814 }
815 } else {
816 if ((newnp->n_flag & NNEW) && vp->v_type == VREG)
817 printf("new vnode '%s' borned without parent ?\n",newnp->n_name);
818 }
819 newnp->n_flag &= ~NNEW;
820 *vpp = vp;
821 return 0;
822}
823
824/*
825 * How to keep the brain busy ...
826 * Currently lookup routine can make two lookup for vnode. This can be
827 * avoided by reorg the code.
a9bb80e0
CP
828 *
829 * nwfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
830 * struct vnode **a_vpp, struct componentname *a_cnp)
984263bc
MD
831 */
832int
a9bb80e0 833nwfs_lookup(struct vop_lookup_args *ap)
984263bc
MD
834{
835 struct componentname *cnp = ap->a_cnp;
836 struct vnode *dvp = ap->a_dvp;
837 struct vnode **vpp = ap->a_vpp;
838 int flags = cnp->cn_flags;
839 struct vnode *vp;
840 struct nwmount *nmp;
841 struct mount *mp = dvp->v_mount;
842 struct nwnode *dnp, *npp;
843 struct nw_entry_info fattr, *fap;
844 ncpfid fid;
845 int nameiop=cnp->cn_nameiop, islastcn;
846 int lockparent, wantparent, error = 0, notfound;
dadab5e9 847 struct thread *td = cnp->cn_td;
984263bc
MD
848 char _name[cnp->cn_namelen+1];
849 bcopy(cnp->cn_nameptr,_name,cnp->cn_namelen);
850 _name[cnp->cn_namelen]=0;
851
852 if (dvp->v_type != VDIR)
853 return (ENOTDIR);
2b69e610 854 if ((flags & CNP_ISDOTDOT) && (dvp->v_flag & VROOT)) {
984263bc
MD
855 printf("nwfs_lookup: invalid '..'\n");
856 return EIO;
857 }
858
859 NCPVNDEBUG("%d '%s' in '%s' id=d\n", nameiop, _name,
860 VTONW(dvp)->n_name/*, VTONW(dvp)->n_name*/);
861
2b69e610
MD
862 islastcn = flags & CNP_ISLASTCN;
863 if (islastcn && (mp->mnt_flag & MNT_RDONLY) && (nameiop != NAMEI_LOOKUP))
984263bc 864 return (EROFS);
dadab5e9 865 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)))
984263bc 866 return (error);
2b69e610
MD
867 lockparent = flags & CNP_LOCKPARENT;
868 wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
984263bc
MD
869 nmp = VFSTONWFS(mp);
870 dnp = VTONW(dvp);
871/*
2b69e610 872printf("dvp %d:%d:%d\n", (int)mp, (int)dvp->v_flag & VROOT, (int)flags & CNP_ISDOTDOT);
984263bc
MD
873*/
874 error = ncp_pathcheck(cnp->cn_nameptr, cnp->cn_namelen, &nmp->m.nls,
2b69e610 875 (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) && (nmp->m.nls.opt & NWHP_NOSTRICT) == 0);
984263bc
MD
876 if (error)
877 return ENOENT;
878
14c92d03 879 error = cache_lookup(dvp, vpp, cnp);
984263bc
MD
880 NCPVNDEBUG("cache_lookup returned %d\n",error);
881 if (error > 0)
882 return error;
883 if (error) { /* name was found */
884 struct vattr vattr;
885 int vpid;
886
887 vp = *vpp;
888 vpid = vp->v_id;
889 if (dvp == vp) { /* lookup on current */
67773eb3 890 /* vp already ref'd */
984263bc
MD
891 error = 0;
892 NCPVNDEBUG("cached '.'");
2b69e610 893 } else if (flags & CNP_ISDOTDOT) {
5fd012e0
MD
894 VOP_UNLOCK(dvp, 0, td); /* unlock parent */
895 error = vget(vp, LK_EXCLUSIVE, td);
67773eb3 896 vrele(vp);
984263bc 897 if (!error && lockparent && islastcn)
5fd012e0 898 error = vn_lock(dvp, LK_EXCLUSIVE, td);
984263bc 899 } else {
5fd012e0 900 error = vget(vp, LK_EXCLUSIVE, td);
67773eb3 901 vrele(vp);
984263bc 902 if (!lockparent || error || !islastcn)
5fd012e0 903 VOP_UNLOCK(dvp, 0, td);
984263bc
MD
904 }
905 if (!error) {
906 if (vpid == vp->v_id) {
3b568787 907 if (!VOP_GETATTR(vp, &vattr, td)
984263bc 908 && vattr.va_ctime.tv_sec == VTONW(vp)->n_ctime) {
2b69e610
MD
909 if (nameiop != NAMEI_LOOKUP && islastcn)
910 cnp->cn_flags |= CNP_SAVENAME;
984263bc
MD
911 NCPVNDEBUG("use cached vnode");
912 return (0);
913 }
914 cache_purge(vp);
915 }
916 vput(vp);
917 if (lockparent && dvp != vp && islastcn)
5fd012e0 918 VOP_UNLOCK(dvp, 0, td);
984263bc 919 }
5fd012e0 920 error = vn_lock(dvp, LK_EXCLUSIVE, td);
984263bc
MD
921 *vpp = NULLVP;
922 if (error)
923 return (error);
924 }
925 /* not in cache, so ... */
926 error = 0;
927 *vpp = NULLVP;
928 fap = NULL;
2b69e610 929 if (flags & CNP_ISDOTDOT) {
984263bc
MD
930 if (NWCMPF(&dnp->n_parent, &nmp->n_rootent)) {
931 fid = nmp->n_rootent;
932 fap = NULL;
933 notfound = 0;
934 } else {
dadab5e9 935 error = nwfs_lookupnp(nmp, dnp->n_parent, td, &npp);
984263bc
MD
936 if (error) {
937 return error;
938 }
939 fid = dnp->n_parent;
940 fap = &fattr;
941 /*np = *npp;*/
942 notfound = ncp_obtain_info(nmp, npp->n_dosfid,
dadab5e9 943 0, NULL, fap, td, cnp->cn_cred);
984263bc
MD
944 }
945 } else {
946 fap = &fattr;
947 notfound = ncp_lookup(dvp, cnp->cn_namelen, cnp->cn_nameptr,
dadab5e9 948 fap, td, cnp->cn_cred);
984263bc
MD
949 fid.f_id = fap->dirEntNum;
950 if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
951 fid.f_parent = dnp->n_fid.f_parent;
952 } else
953 fid.f_parent = dnp->n_fid.f_id;
954 NCPVNDEBUG("call to ncp_lookup returned=%d\n",notfound);
955 }
956 if (notfound && notfound < 0x80 )
957 return (notfound); /* hard error */
958 if (notfound) { /* entry not found */
959 /* Handle RENAME or CREATE case... */
2b69e610
MD
960 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) && wantparent && islastcn) {
961 cnp->cn_flags |= CNP_SAVENAME;
984263bc 962 if (!lockparent)
5fd012e0 963 VOP_UNLOCK(dvp, 0, td);
984263bc
MD
964 return (EJUSTRETURN);
965 }
966 return ENOENT;
967 }/* else {
968 NCPVNDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
969 }*/
970 /* handle DELETE case ... */
2b69e610 971 if (nameiop == NAMEI_DELETE && islastcn) { /* delete last component */
dadab5e9 972 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, cnp->cn_td);
984263bc
MD
973 if (error) return (error);
974 if (NWCMPF(&dnp->n_fid, &fid)) { /* we found ourselfs */
597aea93 975 vref(dvp);
984263bc
MD
976 *vpp = dvp;
977 return 0;
978 }
979 error = nwfs_nget(mp, fid, fap, dvp, &vp);
980 if (error) return (error);
981 *vpp = vp;
2b69e610 982 cnp->cn_flags |= CNP_SAVENAME; /* I free it later */
5fd012e0 983 if (!lockparent) VOP_UNLOCK(dvp, 0, td);
984263bc
MD
984 return (0);
985 }
2b69e610 986 if (nameiop == NAMEI_RENAME && islastcn && wantparent) {
dadab5e9 987 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, cnp->cn_td);
984263bc
MD
988 if (error) return (error);
989 if (NWCMPF(&dnp->n_fid, &fid)) return EISDIR;
990 error = nwfs_nget(mp, fid, fap, dvp, &vp);
991 if (error) return (error);
992 *vpp = vp;
2b69e610 993 cnp->cn_flags |= CNP_SAVENAME;
984263bc 994 if (!lockparent)
5fd012e0 995 VOP_UNLOCK(dvp, 0, td);
984263bc
MD
996 return (0);
997 }
2b69e610 998 if (flags & CNP_ISDOTDOT) {
5fd012e0 999 VOP_UNLOCK(dvp, 0, td); /* race to get the inode */
984263bc
MD
1000 error = nwfs_nget(mp, fid, NULL, NULL, &vp);
1001 if (error) {
5fd012e0 1002 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
1003 return (error);
1004 }
1005 if (lockparent && islastcn &&
5fd012e0 1006 (error = vn_lock(dvp, LK_EXCLUSIVE, td))) {
984263bc
MD
1007 vput(vp);
1008 return (error);
1009 }
1010 *vpp = vp;
1011 } else if (NWCMPF(&dnp->n_fid, &fid)) {
1012 vref(dvp);
1013 *vpp = dvp;
1014 } else {
1015 error = nwfs_nget(mp, fid, fap, dvp, &vp);
1016 if (error) return (error);
1017 *vpp = vp;
1018 NCPVNDEBUG("lookup: getnewvp!\n");
1019 if (!lockparent || !islastcn)
5fd012e0 1020 VOP_UNLOCK(dvp, 0, td);
984263bc 1021 }
2b69e610 1022 if ((cnp->cn_flags & CNP_MAKEENTRY)/* && !islastcn*/) {
984263bc 1023 VTONW(*vpp)->n_ctime = VTONW(*vpp)->n_vattr.va_ctime.tv_sec;
21739618 1024 cache_enter(dvp, *vpp, cnp);
984263bc
MD
1025 }
1026 return (0);
1027}