Modify kern/makesyscall.sh to prefix all kernel system call procedures
[dragonfly.git] / sys / vfs / portal / portal_vnops.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software donated to Berkeley by
6 * Jan-Simon Pendry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)portal_vnops.c 8.14 (Berkeley) 5/21/95
37 *
38 * $FreeBSD: src/sys/miscfs/portal/portal_vnops.c,v 1.38 1999/12/21 06:29:00 chris Exp $
753fd850 39 * $DragonFly: src/sys/vfs/portal/portal_vnops.c,v 1.28 2006/06/05 07:26:11 dillon Exp $
984263bc
MD
40 */
41
42/*
43 * Portal Filesystem
44 */
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/sysproto.h>
49#include <sys/kernel.h>
50#include <sys/time.h>
51#include <sys/proc.h>
52#include <sys/filedesc.h>
53#include <sys/vnode.h>
54#include <sys/fcntl.h>
55#include <sys/file.h>
56#include <sys/stat.h>
57#include <sys/mount.h>
58#include <sys/malloc.h>
59#include <sys/namei.h>
60#include <sys/mbuf.h>
e4700d00 61#include <sys/resourcevar.h>
984263bc
MD
62#include <sys/socket.h>
63#include <sys/socketvar.h>
64#include <sys/un.h>
65#include <sys/unpcb.h>
1f2de5d4 66#include "portal.h"
984263bc 67
165dba55
DR
68#include <sys/thread2.h>
69
984263bc
MD
70static int portal_fileid = PORTAL_ROOTFILEID+1;
71
a6ee311a
RG
72static int portal_badop (void);
73static void portal_closefd (struct thread *td, int fd);
74static int portal_connect (struct socket *so, struct socket *so2);
75static int portal_getattr (struct vop_getattr_args *ap);
76static int portal_inactive (struct vop_inactive_args *ap);
e62afb5f 77static int portal_lookup (struct vop_old_lookup_args *ap);
a6ee311a
RG
78static int portal_open (struct vop_open_args *ap);
79static int portal_print (struct vop_print_args *ap);
80static int portal_readdir (struct vop_readdir_args *ap);
81static int portal_reclaim (struct vop_reclaim_args *ap);
82static int portal_setattr (struct vop_setattr_args *ap);
984263bc
MD
83
84static void
1f03da75 85portal_closefd(struct thread *td, int fd)
984263bc
MD
86{
87 int error;
88 struct close_args ua;
89
90 ua.fd = fd;
753fd850 91 error = sys_close(&ua);
984263bc
MD
92 /*
93 * We should never get an error, and there isn't anything
94 * we could do if we got one, so just print a message.
95 */
96 if (error)
97 printf("portal_closefd: error = %d\n", error);
98}
99
100/*
101 * vp is the current namei directory
102 * cnp is the name to locate in that directory...
1f03da75
CP
103 *
104 * portal_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
105 * struct componentname *a_cnp)
984263bc
MD
106 */
107static int
e62afb5f 108portal_lookup(struct vop_old_lookup_args *ap)
984263bc
MD
109{
110 struct componentname *cnp = ap->a_cnp;
111 struct vnode **vpp = ap->a_vpp;
112 struct vnode *dvp = ap->a_dvp;
113 char *pname = cnp->cn_nameptr;
114 struct portalnode *pt;
115 int error;
116 struct vnode *fvp = 0;
117 char *path;
118 int size;
119
120 *vpp = NULLVP;
121
2b69e610 122 if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)
984263bc
MD
123 return (EROFS);
124
125 if (cnp->cn_namelen == 1 && *pname == '.') {
126 *vpp = dvp;
597aea93 127 vref(dvp);
984263bc
MD
128 /*VOP_LOCK(dvp);*/
129 return (0);
130 }
131
132 /*
133 * Do the MALLOC before the getnewvnode since doing so afterward
134 * might cause a bogus v_data pointer to get dereferenced
135 * elsewhere if MALLOC should block.
136 */
137 MALLOC(pt, struct portalnode *, sizeof(struct portalnode),
138 M_TEMP, M_WAITOK);
139
6ddb7618 140 error = getnewvnode(VT_PORTAL, dvp->v_mount, &fvp, 0, 0);
984263bc
MD
141 if (error) {
142 FREE(pt, M_TEMP);
143 goto bad;
144 }
145 fvp->v_type = VREG;
146 fvp->v_data = pt;
57f7b636 147
984263bc
MD
148 /*
149 * Save all of the remaining pathname and
150 * advance the namei next pointer to the end
151 * of the string.
152 */
153 for (size = 0, path = pname; *path; path++)
154 size++;
155 cnp->cn_consume = size - cnp->cn_namelen;
156
157 pt->pt_arg = malloc(size+1, M_TEMP, M_WAITOK);
158 pt->pt_size = size+1;
159 bcopy(pname, pt->pt_arg, pt->pt_size);
160 pt->pt_fileid = portal_fileid++;
161
162 *vpp = fvp;
5fd012e0 163 vx_unlock(fvp);
984263bc
MD
164 /*VOP_LOCK(fvp);*/
165 return (0);
166
167bad:;
168 if (fvp)
169 vrele(fvp);
170 return (error);
171}
172
173static int
1f03da75 174portal_connect(struct socket *so, struct socket *so2)
984263bc
MD
175{
176 /* from unp_connect, bypassing the namei stuff... */
177 struct socket *so3;
178 struct unpcb *unp2;
179 struct unpcb *unp3;
180
181 if (so2 == 0)
182 return (ECONNREFUSED);
183
184 if (so->so_type != so2->so_type)
185 return (EPROTOTYPE);
186
187 if ((so2->so_options & SO_ACCEPTCONN) == 0)
188 return (ECONNREFUSED);
189
190 if ((so3 = sonewconn(so2, 0)) == 0)
191 return (ECONNREFUSED);
192
e606b589
JH
193 unp2 = so2->so_pcb;
194 unp3 = so3->so_pcb;
984263bc
MD
195 if (unp2->unp_addr)
196 unp3->unp_addr = (struct sockaddr_un *)
cfa2ba21 197 dup_sockaddr((struct sockaddr *)unp2->unp_addr);
984263bc
MD
198 so2 = so3;
199
200 return (unp_connect2(so, so2));
201}
202
1f03da75
CP
203/*
204 * portal_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
205 * struct thread *a_td)
206 */
984263bc 207static int
1f03da75 208portal_open(struct vop_open_args *ap)
984263bc
MD
209{
210 struct socket *so = 0;
211 struct portalnode *pt;
87de5057 212 struct thread *td = curthread;
984263bc 213 struct vnode *vp = ap->a_vp;
984263bc
MD
214 struct uio auio;
215 struct iovec aiov[2];
216 int res;
217 struct mbuf *cm = 0;
218 struct cmsghdr *cmsg;
219 int newfds;
220 int *ip;
221 int fd;
222 int error;
223 int len;
224 struct portalmount *fmp;
225 struct file *fp;
226 struct portal_cred pcred;
227
228 /*
229 * Nothing to do when opening the root node.
230 */
231 if (vp->v_flag & VROOT)
8ddc6004 232 return (vop_stdopen(ap));
984263bc
MD
233
234 /*
235 * Can't be opened unless the caller is set up
236 * to deal with the side effects. Check for this
237 * by testing whether the p_dupfd has been set.
238 */
dadab5e9 239 KKASSERT(td->td_proc);
fbfe4e7d 240 if (td->td_lwp->lwp_dupfd >= 0)
984263bc
MD
241 return (ENODEV);
242
243 pt = VTOPORTAL(vp);
244 fmp = VFSTOPORTAL(vp->v_mount);
245
246 /*
247 * Create a new socket.
248 */
87de5057 249 error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, td);
984263bc
MD
250 if (error)
251 goto bad;
252
253 /*
254 * Reserve some buffer space
255 */
256 res = pt->pt_size + sizeof(pcred) + 512; /* XXX */
e4700d00 257 error = soreserve(so, res, res, &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
984263bc
MD
258 if (error)
259 goto bad;
260
261 /*
262 * Kick off connection
263 */
264 error = portal_connect(so, (struct socket *)fmp->pm_server->f_data);
265 if (error)
266 goto bad;
267
268 /*
269 * Wait for connection to complete
270 */
271 /*
272 * XXX: Since the mount point is holding a reference on the
273 * underlying server socket, it is not easy to find out whether
274 * the server process is still running. To handle this problem
275 * we loop waiting for the new socket to be connected (something
276 * which will only happen if the server is still running) or for
277 * the reference count on the server socket to drop to 1, which
278 * will happen if the server dies. Sleep for 5 second intervals
279 * and keep polling the reference count. XXX.
280 */
165dba55 281 crit_enter();
984263bc
MD
282 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
283 if (fmp->pm_server->f_count == 1) {
284 error = ECONNREFUSED;
165dba55 285 crit_exit();
984263bc
MD
286 goto bad;
287 }
377d4740 288 (void) tsleep((caddr_t) &so->so_timeo, 0, "portalcon", 5 * hz);
984263bc 289 }
165dba55 290 crit_exit();
984263bc
MD
291
292 if (so->so_error) {
293 error = so->so_error;
294 goto bad;
295 }
296
297 /*
298 * Set miscellaneous flags
299 */
300 so->so_rcv.sb_timeo = 0;
301 so->so_snd.sb_timeo = 0;
302 so->so_rcv.sb_flags |= SB_NOINTR;
303 so->so_snd.sb_flags |= SB_NOINTR;
304
305
306 pcred.pcr_flag = ap->a_mode;
307 pcred.pcr_uid = ap->a_cred->cr_uid;
308 pcred.pcr_ngroups = ap->a_cred->cr_ngroups;
309 bcopy(ap->a_cred->cr_groups, pcred.pcr_groups, NGROUPS * sizeof(gid_t));
310 aiov[0].iov_base = (caddr_t) &pcred;
311 aiov[0].iov_len = sizeof(pcred);
312 aiov[1].iov_base = pt->pt_arg;
313 aiov[1].iov_len = pt->pt_size;
314 auio.uio_iov = aiov;
315 auio.uio_iovcnt = 2;
316 auio.uio_rw = UIO_WRITE;
317 auio.uio_segflg = UIO_SYSSPACE;
dadab5e9 318 auio.uio_td = td;
984263bc
MD
319 auio.uio_offset = 0;
320 auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
321
322 error = sosend(so, (struct sockaddr *) 0, &auio,
dadab5e9 323 (struct mbuf *) 0, (struct mbuf *) 0, 0, td);
984263bc
MD
324 if (error)
325 goto bad;
326
327 len = auio.uio_resid = sizeof(int);
328 do {
329 struct mbuf *m = 0;
330 int flags = MSG_WAITALL;
331 error = soreceive(so, (struct sockaddr **) 0, &auio,
332 &m, &cm, &flags);
333 if (error)
334 goto bad;
335
336 /*
337 * Grab an error code from the mbuf.
338 */
339 if (m) {
340 m = m_pullup(m, sizeof(int)); /* Needed? */
341 if (m) {
342 error = *(mtod(m, int *));
343 m_freem(m);
344 } else {
345 error = EINVAL;
346 }
347 } else {
348 if (cm == 0) {
349 error = ECONNRESET; /* XXX */
350#ifdef notdef
351 break;
352#endif
353 }
354 }
355 } while (cm == 0 && auio.uio_resid == len && !error);
356
357 if (cm == 0)
358 goto bad;
359
360 if (auio.uio_resid) {
361 error = 0;
362#ifdef notdef
363 error = EMSGSIZE;
364 goto bad;
365#endif
366 }
367
368 /*
369 * XXX: Break apart the control message, and retrieve the
370 * received file descriptor. Note that more than one descriptor
371 * may have been received, or that the rights chain may have more
372 * than a single mbuf in it. What to do?
373 */
374 cmsg = mtod(cm, struct cmsghdr *);
375 newfds = (cmsg->cmsg_len - sizeof(*cmsg)) / sizeof (int);
376 if (newfds == 0) {
377 error = ECONNREFUSED;
378 goto bad;
379 }
380 /*
381 * At this point the rights message consists of a control message
382 * header, followed by a data region containing a vector of
383 * integer file descriptors. The fds were allocated by the action
384 * of receiving the control message.
385 */
386 ip = (int *) (cmsg + 1);
387 fd = *ip++;
388 if (newfds > 1) {
389 /*
390 * Close extra fds.
391 */
392 int i;
393 printf("portal_open: %d extra fds\n", newfds - 1);
394 for (i = 1; i < newfds; i++) {
dadab5e9 395 portal_closefd(td, *ip);
984263bc
MD
396 ip++;
397 }
398 }
399
400 /*
401 * Check that the mode the file is being opened for is a subset
402 * of the mode of the existing descriptor.
403 */
dadab5e9 404 KKASSERT(td->td_proc);
0679adc4 405 fp = td->td_proc->p_fd->fd_files[fd].fp;
984263bc 406 if (((ap->a_mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
dadab5e9 407 portal_closefd(td, fd);
984263bc
MD
408 error = EACCES;
409 goto bad;
410 }
411
412 /*
413 * Save the dup fd in the proc structure then return the
414 * special error code (ENXIO) which causes magic things to
415 * happen in vn_open. The whole concept is, well, hmmm.
416 */
fbfe4e7d 417 td->td_lwp->lwp_dupfd = fd;
8ddc6004 418 vop_stdopen(ap);
984263bc
MD
419 error = ENXIO;
420
421bad:;
422 /*
423 * And discard the control message.
424 */
425 if (cm) {
426 m_freem(cm);
427 }
428
429 if (so) {
430 soshutdown(so, 2);
431 soclose(so);
432 }
433 return (error);
434}
435
1f03da75
CP
436/*
437 * portal_getattr(struct vnode *a_vp, struct vattr *a_vap,
438 * struct ucred *a_cred, struct thread *a_td)
439 */
984263bc 440static int
1f03da75 441portal_getattr(struct vop_getattr_args *ap)
984263bc
MD
442{
443 struct vnode *vp = ap->a_vp;
444 struct vattr *vap = ap->a_vap;
445
446 bzero(vap, sizeof(*vap));
447 vattr_null(vap);
448 vap->va_uid = 0;
449 vap->va_gid = 0;
450 vap->va_size = DEV_BSIZE;
451 vap->va_blocksize = DEV_BSIZE;
452 nanotime(&vap->va_atime);
453 vap->va_mtime = vap->va_atime;
454 vap->va_ctime = vap->va_mtime;
455 vap->va_gen = 0;
456 vap->va_flags = 0;
457 vap->va_rdev = 0;
458 /* vap->va_qbytes = 0; */
459 vap->va_bytes = 0;
460 /* vap->va_qsize = 0; */
461 if (vp->v_flag & VROOT) {
462 vap->va_type = VDIR;
463 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
464 S_IRGRP|S_IWGRP|S_IXGRP|
465 S_IROTH|S_IWOTH|S_IXOTH;
466 vap->va_nlink = 2;
467 vap->va_fileid = 2;
468 } else {
469 vap->va_type = VREG;
470 vap->va_mode = S_IRUSR|S_IWUSR|
471 S_IRGRP|S_IWGRP|
472 S_IROTH|S_IWOTH;
473 vap->va_nlink = 1;
474 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
475 }
476 return (0);
477}
478
1f03da75
CP
479/*
480 * portal_setattr(struct vnode *a_vp, struct vattr *a_vap,
481 * struct ucred *a_cred, struct thread *a_td)
482 */
984263bc 483static int
1f03da75 484portal_setattr(struct vop_setattr_args *ap)
984263bc 485{
984263bc
MD
486 /*
487 * Can't mess with the root vnode
488 */
489 if (ap->a_vp->v_flag & VROOT)
490 return (EACCES);
491
492 if (ap->a_vap->va_flags != VNOVAL)
493 return (EOPNOTSUPP);
494
495 return (0);
496}
497
498/*
499 * Fake readdir, just return empty directory.
500 * It is hard to deal with '.' and '..' so don't bother.
1f03da75
CP
501 *
502 * portal_readdir(struct vnode *a_vp, struct uio *a_uio,
503 * struct ucred *a_cred, int *a_eofflag,
504 * u_long *a_cookies, int a_ncookies)
984263bc
MD
505 */
506static int
1f03da75 507portal_readdir(struct vop_readdir_args *ap)
984263bc 508{
984263bc
MD
509 /*
510 * We don't allow exporting portal mounts, and currently local
511 * requests do not need cookies.
512 */
513 if (ap->a_ncookies)
514 panic("portal_readdir: not hungry");
515
516 return (0);
517}
518
1f03da75
CP
519/*
520 * portal_inactive(struct vnode *a_vp, struct thread *a_td)
521 */
984263bc 522static int
1f03da75 523portal_inactive(struct vop_inactive_args *ap)
984263bc 524{
984263bc
MD
525 return (0);
526}
527
1f03da75
CP
528/*
529 * portal_reclaim(struct vnode *a_vp)
530 */
984263bc 531static int
1f03da75 532portal_reclaim(struct vop_reclaim_args *ap)
984263bc
MD
533{
534 struct portalnode *pt = VTOPORTAL(ap->a_vp);
535
536 if (pt->pt_arg) {
537 free((caddr_t) pt->pt_arg, M_TEMP);
538 pt->pt_arg = 0;
539 }
540 FREE(ap->a_vp->v_data, M_TEMP);
541 ap->a_vp->v_data = 0;
542
543 return (0);
544}
545
546
547/*
548 * Print out the contents of a Portal vnode.
1f03da75
CP
549 *
550 * portal_print(struct vnode *a_vp)
984263bc
MD
551 */
552/* ARGSUSED */
553static int
1f03da75 554portal_print(struct vop_print_args *ap)
984263bc 555{
984263bc
MD
556 printf("tag VT_PORTAL, portal vnode\n");
557 return (0);
558}
559
560
561/*
562 * Portal "should never get here" operation
563 */
564static int
1f03da75 565portal_badop(void)
984263bc
MD
566{
567
568 panic("portal: bad op");
569 /* NOTREACHED */
570}
571
0961aa92 572struct vnodeopv_entry_desc portal_vnodeop_entries[] = {
2d3e977e
MD
573 { &vop_default_desc, vop_defaultop },
574 { &vop_access_desc, vop_null },
625ddaba
JS
575 { &vop_bmap_desc, (vnodeopv_entry_t) portal_badop },
576 { &vop_getattr_desc, (vnodeopv_entry_t) portal_getattr },
577 { &vop_inactive_desc, (vnodeopv_entry_t) portal_inactive },
e62afb5f 578 { &vop_old_lookup_desc, (vnodeopv_entry_t) portal_lookup },
625ddaba
JS
579 { &vop_open_desc, (vnodeopv_entry_t) portal_open },
580 { &vop_pathconf_desc, (vnodeopv_entry_t) vop_stdpathconf },
581 { &vop_print_desc, (vnodeopv_entry_t) portal_print },
582 { &vop_readdir_desc, (vnodeopv_entry_t) portal_readdir },
583 { &vop_reclaim_desc, (vnodeopv_entry_t) portal_reclaim },
584 { &vop_setattr_desc, (vnodeopv_entry_t) portal_setattr },
984263bc
MD
585 { NULL, NULL }
586};
984263bc 587