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 * 4. Neither the name of the University nor the names of its 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 REGENTS 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 REGENTS 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 * @(#)portal_vnops.c 8.14 (Berkeley) 5/21/95
33 *
34 * $FreeBSD$
35 */
36
37 /*
38 * Portal Filesystem
39 */
40
41 #include <sys/param.h>
42 #include <sys/fcntl.h>
43 #include <sys/file.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/proc.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/stat.h>
55 #include <sys/sysproto.h>
56 #include <sys/systm.h>
57 #include <sys/un.h>
58 #include <sys/unpcb.h>
59 #include <sys/vnode.h>
60
61 #include <fs/portalfs/portal.h>
62
63 static int portal_fileid = PORTAL_ROOTFILEID+1;
64
65 static void portal_closefd(struct thread *td, int fd);
66 static int portal_connect(struct socket *so, struct socket *so2);
67 static int portal_getattr(struct vop_getattr_args *ap);
68 static int portal_lookup(struct vop_lookup_args *ap);
69 static int portal_open(struct vop_open_args *ap);
70 static int portal_readdir(struct vop_readdir_args *ap);
71 static int portal_reclaim(struct vop_reclaim_args *ap);
72 static int portal_setattr(struct vop_setattr_args *ap);
73
74 static void
75 portal_closefd(td, fd)
76 struct thread *td;
77 int fd;
78 {
79 int error;
80 struct close_args ua;
81
82 ua.fd = fd;
83 error = close(td, &ua);
84 /*
85 * We should never get an error, and there isn't anything
86 * we could do if we got one, so just print a message.
87 */
88 if (error)
89 printf("portal_closefd: error = %d\n", error);
90 }
91
92 /*
93 * vp is the current namei directory
94 * cnp is the name to locate in that directory...
95 */
96 static int
97 portal_lookup(ap)
98 struct vop_lookup_args /* {
99 struct vnode * a_dvp;
100 struct vnode ** a_vpp;
101 struct componentname * a_cnp;
102 } */ *ap;
103 {
104 struct componentname *cnp = ap->a_cnp;
105 struct vnode **vpp = ap->a_vpp;
106 struct vnode *dvp = ap->a_dvp;
107 char *pname = cnp->cn_nameptr;
108 struct thread *td = cnp->cn_thread;
109 struct portalnode *pt;
110 int error;
111 struct vnode *fvp = 0;
112 char *path;
113 int size;
114
115 *vpp = NULLVP;
116
117 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
118 return (EROFS);
119
120 if (cnp->cn_namelen == 1 && *pname == '.') {
121 *vpp = dvp;
122 VREF(dvp);
123 return (0);
124 }
125
126 /*
127 * Do the MALLOC before the getnewvnode since doing so afterward
128 * might cause a bogus v_data pointer to get dereferenced
129 * elsewhere if MALLOC should block.
130 */
131 MALLOC(pt, struct portalnode *, sizeof(struct portalnode),
132 M_TEMP, M_WAITOK);
133
134 error = getnewvnode("portal", dvp->v_mount, portal_vnodeop_p, &fvp);
135 if (error) {
136 FREE(pt, M_TEMP);
137 goto bad;
138 }
139 fvp->v_type = VREG;
140 fvp->v_data = pt;
141 /*
142 * Save all of the remaining pathname and
143 * advance the namei next pointer to the end
144 * of the string.
145 */
146 for (size = 0, path = pname; *path; path++)
147 size++;
148 cnp->cn_consume = size - cnp->cn_namelen;
149
150 pt->pt_arg = malloc(size+1, M_TEMP, M_WAITOK);
151 pt->pt_size = size+1;
152 bcopy(pname, pt->pt_arg, pt->pt_size);
153 pt->pt_fileid = portal_fileid++;
154
155 *vpp = fvp;
156 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, td);
157 /*
158 * As we are the last component of the path name, fix up
159 * the locking on the directory node.
160 */
161 if ((cnp->cn_flags & LOCKPARENT) == 0) {
162 VOP_UNLOCK(dvp, 0, td);
163 cnp->cn_flags |= PDIRUNLOCK;
164 }
165 return (0);
166
167 bad:;
168 if (fvp)
169 vrele(fvp);
170 return (error);
171 }
172
173 static int
174 portal_connect(so, so2)
175 struct socket *so;
176 struct socket *so2;
177 {
178 /* from unp_connect, bypassing the namei stuff... */
179 struct socket *so3;
180 struct unpcb *unp2;
181 struct unpcb *unp3;
182
183 if (so2 == 0)
184 return (ECONNREFUSED);
185
186 if (so->so_type != so2->so_type)
187 return (EPROTOTYPE);
188
189 if ((so2->so_options & SO_ACCEPTCONN) == 0)
190 return (ECONNREFUSED);
191
192 if ((so3 = sonewconn(so2, 0)) == 0)
193 return (ECONNREFUSED);
194
195 unp2 = sotounpcb(so2);
196 unp3 = sotounpcb(so3);
197 if (unp2->unp_addr)
198 unp3->unp_addr = (struct sockaddr_un *)
199 sodupsockaddr((struct sockaddr *)unp2->unp_addr,
200 M_NOWAIT);
201 so2 = so3;
202
203 return (uipc_connect2(so, so2));
204 }
205
206 static int
207 portal_open(ap)
208 struct vop_open_args /* {
209 struct vnode *a_vp;
210 int a_mode;
211 struct ucred *a_cred;
212 struct thread *a_td;
213 } */ *ap;
214 {
215 struct socket *so = 0;
216 struct portalnode *pt;
217 struct thread *td = ap->a_td;
218 struct vnode *vp = ap->a_vp;
219 struct uio auio;
220 struct iovec aiov[2];
221 int res;
222 struct mbuf *cm = 0;
223 struct cmsghdr *cmsg;
224 int newfds;
225 int *ip;
226 int fd;
227 int error;
228 int len;
229 struct portalmount *fmp;
230 struct file *fp;
231 struct portal_cred pcred;
232
233 /*
234 * Nothing to do when opening the root node.
235 */
236 if (vp->v_vflag & VV_ROOT)
237 return (0);
238
239 /*
240 * Can't be opened unless the caller is set up
241 * to deal with the side effects. Check for this
242 * by testing whether the p_dupfd has been set.
243 */
244 if (td->td_dupfd >= 0)
245 return (ENODEV);
246
247 pt = VTOPORTAL(vp);
248 fmp = VFSTOPORTAL(vp->v_mount);
249
250 /*
251 * Create a new socket.
252 */
253 error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, ap->a_td->td_ucred,
254 ap->a_td);
255 if (error)
256 goto bad;
257
258 /*
259 * Reserve some buffer space
260 */
261 res = pt->pt_size + sizeof(pcred) + 512; /* XXX */
262 error = soreserve(so, res, res);
263 if (error)
264 goto bad;
265
266 /*
267 * Kick off connection
268 */
269 error = portal_connect(so, fmp->pm_server->f_data);
270 if (error)
271 goto bad;
272
273 /*
274 * Wait for connection to complete
275 */
276 /*
277 * XXX: Since the mount point is holding a reference on the
278 * underlying server socket, it is not easy to find out whether
279 * the server process is still running. To handle this problem
280 * we loop waiting for the new socket to be connected (something
281 * which will only happen if the server is still running) or for
282 * the reference count on the server socket to drop to 1, which
283 * will happen if the server dies. Sleep for 5 second intervals
284 * and keep polling the reference count. XXX.
285 */
286 /* XXXRW: Locking? */
287 SOCK_LOCK(so);
288 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
289 if (fmp->pm_server->f_count == 1) {
290 SOCK_UNLOCK(so);
291 error = ECONNREFUSED;
292 goto bad;
293 }
294 (void) msleep((caddr_t) &so->so_timeo, SOCK_MTX(so), PSOCK,
295 "portalcon", 5 * hz);
296 }
297 SOCK_UNLOCK(so);
298
299 if (so->so_error) {
300 error = so->so_error;
301 goto bad;
302 }
303
304 /*
305 * Set miscellaneous flags
306 */
307 SOCKBUF_LOCK(&so->so_rcv);
308 so->so_rcv.sb_timeo = 0;
309 so->so_rcv.sb_flags |= SB_NOINTR;
310 SOCKBUF_UNLOCK(&so->so_rcv);
311 SOCKBUF_LOCK(&so->so_snd);
312 so->so_snd.sb_timeo = 0;
313 so->so_snd.sb_flags |= SB_NOINTR;
314 SOCKBUF_UNLOCK(&so->so_snd);
315
316
317 pcred.pcr_flag = ap->a_mode;
318 pcred.pcr_uid = ap->a_cred->cr_uid;
319 pcred.pcr_ngroups = ap->a_cred->cr_ngroups;
320 bcopy(ap->a_cred->cr_groups, pcred.pcr_groups, NGROUPS * sizeof(gid_t));
321 aiov[0].iov_base = (caddr_t) &pcred;
322 aiov[0].iov_len = sizeof(pcred);
323 aiov[1].iov_base = pt->pt_arg;
324 aiov[1].iov_len = pt->pt_size;
325 auio.uio_iov = aiov;
326 auio.uio_iovcnt = 2;
327 auio.uio_rw = UIO_WRITE;
328 auio.uio_segflg = UIO_SYSSPACE;
329 auio.uio_td = td;
330 auio.uio_offset = 0;
331 auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
332
333 error = sosend(so, (struct sockaddr *) 0, &auio,
334 (struct mbuf *) 0, (struct mbuf *) 0, 0 , td);
335 if (error)
336 goto bad;
337
338 len = auio.uio_resid = sizeof(int);
339 do {
340 struct mbuf *m = 0;
341 int flags = MSG_WAITALL;
342 error = soreceive(so, (struct sockaddr **) 0, &auio,
343 &m, &cm, &flags);
344 if (error)
345 goto bad;
346
347 /*
348 * Grab an error code from the mbuf.
349 */
350 if (m) {
351 m = m_pullup(m, sizeof(int)); /* Needed? */
352 if (m) {
353 error = *(mtod(m, int *));
354 m_freem(m);
355 } else {
356 error = EINVAL;
357 }
358 } else {
359 if (cm == 0) {
360 error = ECONNRESET; /* XXX */
361 #ifdef notdef
362 break;
363 #endif
364 }
365 }
366 } while (cm == 0 && auio.uio_resid == len && !error);
367
368 if (cm == 0)
369 goto bad;
370
371 if (auio.uio_resid) {
372 error = 0;
373 #ifdef notdef
374 error = EMSGSIZE;
375 goto bad;
376 #endif
377 }
378
379 /*
380 * XXX: Break apart the control message, and retrieve the
381 * received file descriptor. Note that more than one descriptor
382 * may have been received, or that the rights chain may have more
383 * than a single mbuf in it. What to do?
384 */
385 cmsg = mtod(cm, struct cmsghdr *);
386 newfds = (cmsg->cmsg_len - sizeof(*cmsg)) / sizeof (int);
387 if (newfds == 0) {
388 error = ECONNREFUSED;
389 goto bad;
390 }
391 /*
392 * At this point the rights message consists of a control message
393 * header, followed by a data region containing a vector of
394 * integer file descriptors. The fds were allocated by the action
395 * of receiving the control message.
396 */
397 ip = (int *) (cmsg + 1);
398 fd = *ip++;
399 if (newfds > 1) {
400 /*
401 * Close extra fds.
402 */
403 int i;
404 printf("portal_open: %d extra fds\n", newfds - 1);
405 for (i = 1; i < newfds; i++) {
406 portal_closefd(td, *ip);
407 ip++;
408 }
409 }
410
411 /*
412 * Check that the mode the file is being opened for is a subset
413 * of the mode of the existing descriptor.
414 */
415 if ((error = fget(td, fd, &fp)) != 0)
416 goto bad;
417 if (((ap->a_mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
418 fdrop(fp, td);
419 portal_closefd(td, fd);
420 error = EACCES;
421 goto bad;
422 }
423 fdrop(fp, td);
424
425 /*
426 * Save the dup fd in the proc structure then return the
427 * special error code (ENXIO) which causes magic things to
428 * happen in vn_open. The whole concept is, well, hmmm.
429 */
430 td->td_dupfd = fd;
431 error = ENXIO;
432
433 bad:;
434 /*
435 * And discard the control message.
436 */
437 if (cm) {
438 m_freem(cm);
439 }
440
441 if (so) {
442 soshutdown(so, 2);
443 soclose(so);
444 }
445 return (error);
446 }
447
448 static int
449 portal_getattr(ap)
450 struct vop_getattr_args /* {
451 struct vnode *a_vp;
452 struct vattr *a_vap;
453 struct ucred *a_cred;
454 struct thread *a_td;
455 } */ *ap;
456 {
457 struct vnode *vp = ap->a_vp;
458 struct vattr *vap = ap->a_vap;
459
460 bzero(vap, sizeof(*vap));
461 vattr_null(vap);
462 vap->va_uid = 0;
463 vap->va_gid = 0;
464 vap->va_size = DEV_BSIZE;
465 vap->va_blocksize = DEV_BSIZE;
466 nanotime(&vap->va_atime);
467 vap->va_mtime = vap->va_atime;
468 vap->va_ctime = vap->va_mtime;
469 vap->va_gen = 0;
470 vap->va_flags = 0;
471 vap->va_rdev = 0;
472 /* vap->va_qbytes = 0; */
473 vap->va_bytes = 0;
474 /* vap->va_qsize = 0; */
475 if (vp->v_vflag & VV_ROOT) {
476 vap->va_type = VDIR;
477 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
478 S_IRGRP|S_IWGRP|S_IXGRP|
479 S_IROTH|S_IWOTH|S_IXOTH;
480 vap->va_nlink = 2;
481 vap->va_fileid = 2;
482 } else {
483 vap->va_type = VREG;
484 vap->va_mode = S_IRUSR|S_IWUSR|
485 S_IRGRP|S_IWGRP|
486 S_IROTH|S_IWOTH;
487 vap->va_nlink = 1;
488 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
489 }
490 return (0);
491 }
492
493 static int
494 portal_setattr(ap)
495 struct vop_setattr_args /* {
496 struct vnode *a_vp;
497 struct vattr *a_vap;
498 struct ucred *a_cred;
499 struct thread *a_td;
500 } */ *ap;
501 {
502
503 /*
504 * Can't mess with the root vnode
505 */
506 if (ap->a_vp->v_vflag & VV_ROOT)
507 return (EACCES);
508
509 if (ap->a_vap->va_flags != VNOVAL)
510 return (EOPNOTSUPP);
511
512 return (0);
513 }
514
515 /*
516 * Fake readdir, just return empty directory.
517 * It is hard to deal with '.' and '..' so don't bother.
518 */
519 static int
520 portal_readdir(ap)
521 struct vop_readdir_args /* {
522 struct vnode *a_vp;
523 struct uio *a_uio;
524 struct ucred *a_cred;
525 int *a_eofflag;
526 u_long *a_cookies;
527 int a_ncookies;
528 } */ *ap;
529 {
530
531 /*
532 * We don't allow exporting portal mounts, and currently local
533 * requests do not need cookies.
534 */
535 if (ap->a_ncookies)
536 panic("portal_readdir: not hungry");
537
538 return (0);
539 }
540
541 static int
542 portal_reclaim(ap)
543 struct vop_reclaim_args /* {
544 struct vnode *a_vp;
545 } */ *ap;
546 {
547 struct portalnode *pt = VTOPORTAL(ap->a_vp);
548
549 if (pt->pt_arg) {
550 free((caddr_t) pt->pt_arg, M_TEMP);
551 pt->pt_arg = 0;
552 }
553 FREE(ap->a_vp->v_data, M_TEMP);
554 ap->a_vp->v_data = 0;
555
556 return (0);
557 }
558
559 vop_t **portal_vnodeop_p;
560 static struct vnodeopv_entry_desc portal_vnodeop_entries[] = {
561 { &vop_default_desc, (vop_t *) vop_defaultop },
562 { &vop_access_desc, (vop_t *) vop_null },
563 { &vop_getattr_desc, (vop_t *) portal_getattr },
564 { &vop_lookup_desc, (vop_t *) portal_lookup },
565 { &vop_open_desc, (vop_t *) portal_open },
566 { &vop_pathconf_desc, (vop_t *) vop_stdpathconf },
567 { &vop_readdir_desc, (vop_t *) portal_readdir },
568 { &vop_reclaim_desc, (vop_t *) portal_reclaim },
569 { &vop_setattr_desc, (vop_t *) portal_setattr },
570 { NULL, NULL }
571 };
572 static struct vnodeopv_desc portal_vnodeop_opv_desc =
573 { &portal_vnodeop_p, portal_vnodeop_entries };
574
575 VNODEOP_SET(portal_vnodeop_opv_desc);
Cache object: c7fb15a9c514be6bfaa2d2592d4ae78e
|