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