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