Adjust some comments with reality.
[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 $
b478fdce 39 * $DragonFly: src/sys/vfs/portal/portal_vnops.c,v 1.38 2007/08/08 00:12:52 swildner 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)
086c1d7e 97 kprintf("portal_closefd: error = %d\n", error);
984263bc
MD
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 return (0);
129 }
130
131 /*
132 * Do the MALLOC before the getnewvnode since doing so afterward
133 * might cause a bogus v_data pointer to get dereferenced
134 * elsewhere if MALLOC should block.
135 */
136 MALLOC(pt, struct portalnode *, sizeof(struct portalnode),
137 M_TEMP, M_WAITOK);
138
6ddb7618 139 error = getnewvnode(VT_PORTAL, dvp->v_mount, &fvp, 0, 0);
984263bc
MD
140 if (error) {
141 FREE(pt, M_TEMP);
142 goto bad;
143 }
144 fvp->v_type = VREG;
145 fvp->v_data = pt;
57f7b636 146
984263bc
MD
147 /*
148 * Save all of the remaining pathname and
149 * advance the namei next pointer to the end
150 * of the string.
151 */
152 for (size = 0, path = pname; *path; path++)
153 size++;
154 cnp->cn_consume = size - cnp->cn_namelen;
155
efda3bd0 156 pt->pt_arg = kmalloc(size+1, M_TEMP, M_WAITOK);
984263bc
MD
157 pt->pt_size = size+1;
158 bcopy(pname, pt->pt_arg, pt->pt_size);
159 pt->pt_fileid = portal_fileid++;
160
161 *vpp = fvp;
5fd012e0 162 vx_unlock(fvp);
984263bc
MD
163 return (0);
164
165bad:;
166 if (fvp)
167 vrele(fvp);
168 return (error);
169}
170
171static int
1f03da75 172portal_connect(struct socket *so, struct socket *so2)
984263bc
MD
173{
174 /* from unp_connect, bypassing the namei stuff... */
175 struct socket *so3;
176 struct unpcb *unp2;
177 struct unpcb *unp3;
178
179 if (so2 == 0)
180 return (ECONNREFUSED);
181
182 if (so->so_type != so2->so_type)
183 return (EPROTOTYPE);
184
185 if ((so2->so_options & SO_ACCEPTCONN) == 0)
186 return (ECONNREFUSED);
187
188 if ((so3 = sonewconn(so2, 0)) == 0)
189 return (ECONNREFUSED);
190
e606b589
JH
191 unp2 = so2->so_pcb;
192 unp3 = so3->so_pcb;
984263bc
MD
193 if (unp2->unp_addr)
194 unp3->unp_addr = (struct sockaddr_un *)
cfa2ba21 195 dup_sockaddr((struct sockaddr *)unp2->unp_addr);
984263bc
MD
196 so2 = so3;
197
198 return (unp_connect2(so, so2));
199}
200
1f03da75
CP
201/*
202 * portal_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
b478fdce 203 * struct file *a_fp)
1f03da75 204 */
984263bc 205static int
1f03da75 206portal_open(struct vop_open_args *ap)
984263bc
MD
207{
208 struct socket *so = 0;
209 struct portalnode *pt;
87de5057 210 struct thread *td = curthread;
984263bc 211 struct vnode *vp = ap->a_vp;
984263bc
MD
212 struct uio auio;
213 struct iovec aiov[2];
6d49aa6f 214 struct sockbuf sio;
984263bc
MD
215 int res;
216 struct mbuf *cm = 0;
217 struct cmsghdr *cmsg;
218 int newfds;
219 int *ip;
220 int fd;
221 int error;
222 int len;
223 struct portalmount *fmp;
224 struct file *fp;
225 struct portal_cred pcred;
226
227 /*
228 * Nothing to do when opening the root node.
229 */
230 if (vp->v_flag & VROOT)
8ddc6004 231 return (vop_stdopen(ap));
984263bc
MD
232
233 /*
234 * Can't be opened unless the caller is set up
235 * to deal with the side effects. Check for this
236 * by testing whether the p_dupfd has been set.
237 */
dadab5e9 238 KKASSERT(td->td_proc);
fbfe4e7d 239 if (td->td_lwp->lwp_dupfd >= 0)
984263bc
MD
240 return (ENODEV);
241
242 pt = VTOPORTAL(vp);
243 fmp = VFSTOPORTAL(vp->v_mount);
244
245 /*
246 * Create a new socket.
247 */
87de5057 248 error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, td);
984263bc
MD
249 if (error)
250 goto bad;
251
252 /*
253 * Reserve some buffer space
254 */
255 res = pt->pt_size + sizeof(pcred) + 512; /* XXX */
e4700d00 256 error = soreserve(so, res, res, &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
984263bc
MD
257 if (error)
258 goto bad;
259
260 /*
261 * Kick off connection
262 */
263 error = portal_connect(so, (struct socket *)fmp->pm_server->f_data);
264 if (error)
265 goto bad;
266
267 /*
268 * Wait for connection to complete
269 */
270 /*
271 * XXX: Since the mount point is holding a reference on the
272 * underlying server socket, it is not easy to find out whether
273 * the server process is still running. To handle this problem
274 * we loop waiting for the new socket to be connected (something
275 * which will only happen if the server is still running) or for
276 * the reference count on the server socket to drop to 1, which
277 * will happen if the server dies. Sleep for 5 second intervals
278 * and keep polling the reference count. XXX.
279 */
165dba55 280 crit_enter();
984263bc
MD
281 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
282 if (fmp->pm_server->f_count == 1) {
283 error = ECONNREFUSED;
165dba55 284 crit_exit();
984263bc
MD
285 goto bad;
286 }
377d4740 287 (void) tsleep((caddr_t) &so->so_timeo, 0, "portalcon", 5 * hz);
984263bc 288 }
165dba55 289 crit_exit();
984263bc
MD
290
291 if (so->so_error) {
292 error = so->so_error;
293 goto bad;
294 }
295
296 /*
297 * Set miscellaneous flags
298 */
6d49aa6f
MD
299 so->so_rcv.ssb_timeo = 0;
300 so->so_snd.ssb_timeo = 0;
301 so->so_rcv.ssb_flags |= SSB_NOINTR;
302 so->so_snd.ssb_flags |= SSB_NOINTR;
984263bc
MD
303
304
305 pcred.pcr_flag = ap->a_mode;
306 pcred.pcr_uid = ap->a_cred->cr_uid;
307 pcred.pcr_ngroups = ap->a_cred->cr_ngroups;
308 bcopy(ap->a_cred->cr_groups, pcred.pcr_groups, NGROUPS * sizeof(gid_t));
309 aiov[0].iov_base = (caddr_t) &pcred;
310 aiov[0].iov_len = sizeof(pcred);
311 aiov[1].iov_base = pt->pt_arg;
312 aiov[1].iov_len = pt->pt_size;
313 auio.uio_iov = aiov;
314 auio.uio_iovcnt = 2;
315 auio.uio_rw = UIO_WRITE;
316 auio.uio_segflg = UIO_SYSSPACE;
dadab5e9 317 auio.uio_td = td;
984263bc
MD
318 auio.uio_offset = 0;
319 auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
320
321 error = sosend(so, (struct sockaddr *) 0, &auio,
dadab5e9 322 (struct mbuf *) 0, (struct mbuf *) 0, 0, td);
984263bc
MD
323 if (error)
324 goto bad;
325
d8a9a23b 326 len = sizeof(int);
6d49aa6f 327 sbinit(&sio, len);
984263bc 328 do {
d8a9a23b
MD
329 struct mbuf *m;
330 int flags;
331
332 flags = MSG_WAITALL;
333 error = soreceive(so, NULL, NULL, &sio, &cm, &flags);
984263bc
MD
334 if (error)
335 goto bad;
336
337 /*
338 * Grab an error code from the mbuf.
339 */
6d49aa6f 340 if ((m = sio.sb_mb) != NULL) {
984263bc
MD
341 m = m_pullup(m, sizeof(int)); /* Needed? */
342 if (m) {
343 error = *(mtod(m, int *));
344 m_freem(m);
345 } else {
346 error = EINVAL;
347 }
348 } else {
d8a9a23b 349 if (cm == NULL) {
984263bc
MD
350 error = ECONNRESET; /* XXX */
351#ifdef notdef
352 break;
353#endif
354 }
355 }
6d49aa6f 356 } while (cm == NULL && sio.sb_cc == 0 && !error);
984263bc 357
d8a9a23b 358 if (cm == NULL)
984263bc
MD
359 goto bad;
360
361 if (auio.uio_resid) {
362 error = 0;
363#ifdef notdef
364 error = EMSGSIZE;
365 goto bad;
366#endif
367 }
368
369 /*
370 * XXX: Break apart the control message, and retrieve the
371 * received file descriptor. Note that more than one descriptor
372 * may have been received, or that the rights chain may have more
373 * than a single mbuf in it. What to do?
374 */
375 cmsg = mtod(cm, struct cmsghdr *);
376 newfds = (cmsg->cmsg_len - sizeof(*cmsg)) / sizeof (int);
377 if (newfds == 0) {
378 error = ECONNREFUSED;
379 goto bad;
380 }
381 /*
382 * At this point the rights message consists of a control message
383 * header, followed by a data region containing a vector of
384 * integer file descriptors. The fds were allocated by the action
385 * of receiving the control message.
386 */
387 ip = (int *) (cmsg + 1);
388 fd = *ip++;
389 if (newfds > 1) {
390 /*
391 * Close extra fds.
392 */
393 int i;
086c1d7e 394 kprintf("portal_open: %d extra fds\n", newfds - 1);
984263bc 395 for (i = 1; i < newfds; i++) {
dadab5e9 396 portal_closefd(td, *ip);
984263bc
MD
397 ip++;
398 }
399 }
400
401 /*
402 * Check that the mode the file is being opened for is a subset
403 * of the mode of the existing descriptor.
404 */
dadab5e9 405 KKASSERT(td->td_proc);
0679adc4 406 fp = td->td_proc->p_fd->fd_files[fd].fp;
984263bc 407 if (((ap->a_mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
dadab5e9 408 portal_closefd(td, fd);
984263bc
MD
409 error = EACCES;
410 goto bad;
411 }
412
413 /*
414 * Save the dup fd in the proc structure then return the
415 * special error code (ENXIO) which causes magic things to
416 * happen in vn_open. The whole concept is, well, hmmm.
417 */
fbfe4e7d 418 td->td_lwp->lwp_dupfd = fd;
8ddc6004 419 vop_stdopen(ap);
984263bc
MD
420 error = ENXIO;
421
422bad:;
423 /*
424 * And discard the control message.
425 */
426 if (cm) {
427 m_freem(cm);
428 }
429
430 if (so) {
e5d03018 431 soshutdown(so, SHUT_RDWR);
9ba76b73 432 soclose(so, FNONBLOCK);
984263bc
MD
433 }
434 return (error);
435}
436
1f03da75 437/*
b478fdce 438 * portal_getattr(struct vnode *a_vp, struct vattr *a_vap)
1f03da75 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;
0e9b9130
MD
457 vap->va_rmajor = VNOVAL;
458 vap->va_rminor = VNOVAL;
984263bc
MD
459 /* vap->va_qbytes = 0; */
460 vap->va_bytes = 0;
461 /* vap->va_qsize = 0; */
462 if (vp->v_flag & VROOT) {
463 vap->va_type = VDIR;
464 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
465 S_IRGRP|S_IWGRP|S_IXGRP|
466 S_IROTH|S_IWOTH|S_IXOTH;
467 vap->va_nlink = 2;
468 vap->va_fileid = 2;
469 } else {
470 vap->va_type = VREG;
471 vap->va_mode = S_IRUSR|S_IWUSR|
472 S_IRGRP|S_IWGRP|
473 S_IROTH|S_IWOTH;
474 vap->va_nlink = 1;
475 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
476 }
477 return (0);
478}
479
1f03da75
CP
480/*
481 * portal_setattr(struct vnode *a_vp, struct vattr *a_vap,
b478fdce 482 * struct ucred *a_cred)
1f03da75 483 */
984263bc 484static int
1f03da75 485portal_setattr(struct vop_setattr_args *ap)
984263bc 486{
984263bc
MD
487 /*
488 * Can't mess with the root vnode
489 */
490 if (ap->a_vp->v_flag & VROOT)
491 return (EACCES);
492
493 if (ap->a_vap->va_flags != VNOVAL)
494 return (EOPNOTSUPP);
495
496 return (0);
497}
498
499/*
500 * Fake readdir, just return empty directory.
501 * It is hard to deal with '.' and '..' so don't bother.
1f03da75
CP
502 *
503 * portal_readdir(struct vnode *a_vp, struct uio *a_uio,
504 * struct ucred *a_cred, int *a_eofflag,
505 * u_long *a_cookies, int a_ncookies)
984263bc
MD
506 */
507static int
1f03da75 508portal_readdir(struct vop_readdir_args *ap)
984263bc 509{
984263bc
MD
510 /*
511 * We don't allow exporting portal mounts, and currently local
512 * requests do not need cookies.
513 */
514 if (ap->a_ncookies)
515 panic("portal_readdir: not hungry");
516
517 return (0);
518}
519
1f03da75 520/*
b478fdce 521 * portal_inactive(struct vnode *a_vp)
1f03da75 522 */
984263bc 523static int
1f03da75 524portal_inactive(struct vop_inactive_args *ap)
984263bc 525{
984263bc
MD
526 return (0);
527}
528
1f03da75
CP
529/*
530 * portal_reclaim(struct vnode *a_vp)
531 */
984263bc 532static int
1f03da75 533portal_reclaim(struct vop_reclaim_args *ap)
984263bc
MD
534{
535 struct portalnode *pt = VTOPORTAL(ap->a_vp);
536
537 if (pt->pt_arg) {
efda3bd0 538 kfree((caddr_t) pt->pt_arg, M_TEMP);
984263bc
MD
539 pt->pt_arg = 0;
540 }
541 FREE(ap->a_vp->v_data, M_TEMP);
542 ap->a_vp->v_data = 0;
543
544 return (0);
545}
546
547
548/*
549 * Print out the contents of a Portal vnode.
1f03da75
CP
550 *
551 * portal_print(struct vnode *a_vp)
984263bc
MD
552 */
553/* ARGSUSED */
554static int
1f03da75 555portal_print(struct vop_print_args *ap)
984263bc 556{
086c1d7e 557 kprintf("tag VT_PORTAL, portal vnode\n");
984263bc
MD
558 return (0);
559}
560
561
562/*
563 * Portal "should never get here" operation
564 */
565static int
1f03da75 566portal_badop(void)
984263bc
MD
567{
568
569 panic("portal: bad op");
570 /* NOTREACHED */
571}
572
66a1ddf5
MD
573struct vop_ops portal_vnode_vops = {
574 .vop_default = vop_defaultop,
575 .vop_access = (void *)vop_null,
576 .vop_bmap = (void *)portal_badop,
577 .vop_getattr = portal_getattr,
578 .vop_inactive = portal_inactive,
579 .vop_old_lookup = portal_lookup,
580 .vop_open = portal_open,
581 .vop_pathconf = vop_stdpathconf,
582 .vop_print = portal_print,
583 .vop_readdir = portal_readdir,
584 .vop_reclaim = portal_reclaim,
585 .vop_setattr = portal_setattr
984263bc 586};
984263bc 587