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