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