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