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