]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/fs/portalfs/portal_vnops.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 thread *td = cnp->cn_thread;
107         struct portalnode *pt;
108         int error;
109         struct vnode *fvp = 0;
110         char *path;
111         int size;
112
113         *vpp = NULLVP;
114
115         if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
116                 return (EROFS);
117
118         if (cnp->cn_namelen == 1 && *pname == '.') {
119                 *vpp = dvp;
120                 VREF(dvp);
121                 return (0);
122         }
123         KASSERT((cnp->cn_flags & ISDOTDOT) == 0,
124             ("portal_lookup: Can not handle dotdot lookups."));
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_vnodeops, &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         error = insmntque(fvp, dvp->v_mount);
158         if (error != 0) {
159                 *vpp = NULLVP;
160                 return (error);
161         }
162         return (0);
163
164 bad:;
165         if (fvp)
166                 vrele(fvp);
167         return (error);
168 }
169
170 static int
171 portal_connect(so, so2)
172         struct socket *so;
173         struct socket *so2;
174 {
175         /* from unp_connect, bypassing the namei stuff... */
176         struct socket *so3;
177         struct unpcb *unp2;
178         struct unpcb *unp3;
179
180         if (so2 == 0)
181                 return (ECONNREFUSED);
182
183         if (so->so_type != so2->so_type)
184                 return (EPROTOTYPE);
185
186         if ((so2->so_options & SO_ACCEPTCONN) == 0)
187                 return (ECONNREFUSED);
188
189         if ((so3 = sonewconn(so2, 0)) == 0)
190                 return (ECONNREFUSED);
191
192         unp2 = sotounpcb(so2);
193         unp3 = sotounpcb(so3);
194         if (unp2->unp_addr)
195                 unp3->unp_addr = (struct sockaddr_un *)
196                     sodupsockaddr((struct sockaddr *)unp2->unp_addr,
197                     M_NOWAIT);
198         so2 = so3;
199
200         return (soconnect2(so, so2));
201 }
202
203 static int
204 portal_open(ap)
205         struct vop_open_args /* {
206                 struct vnode *a_vp;
207                 int  a_mode;
208                 struct ucred *a_cred;
209                 struct thread *a_td;
210         } */ *ap;
211 {
212         struct socket *so = 0;
213         struct portalnode *pt;
214         struct thread *td = ap->a_td;
215         struct vnode *vp = ap->a_vp;
216         struct uio auio;
217         struct iovec aiov[2];
218         int res;
219         struct mbuf *cm = 0;
220         struct cmsghdr *cmsg;
221         int newfds;
222         int *ip;
223         int fd;
224         int error;
225         int len;
226         struct portalmount *fmp;
227         struct file *fp;
228         struct portal_cred pcred;
229
230         /*
231          * Nothing to do when opening the root node.
232          */
233         if (vp->v_vflag & VV_ROOT)
234                 return (0);
235
236         /*
237          * Can't be opened unless the caller is set up
238          * to deal with the side effects.  Check for this
239          * by testing whether td_dupfd has been set.
240          */
241         if (td->td_dupfd >= 0)
242                 return (ENODEV);
243
244         pt = VTOPORTAL(vp);
245         fmp = VFSTOPORTAL(vp->v_mount);
246
247         /*
248          * Create a new socket.
249          */
250         error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, ap->a_td->td_ucred,
251             ap->a_td);
252         if (error)
253                 goto bad;
254
255         /*
256          * Reserve some buffer space
257          */
258         res = pt->pt_size + sizeof(pcred) + 512;        /* XXX */
259         error = soreserve(so, res, res);
260         if (error)
261                 goto bad;
262
263         /*
264          * Kick off connection
265          */
266         error = portal_connect(so, fmp->pm_server->f_data);
267         if (error)
268                 goto bad;
269
270         /*
271          * Wait for connection to complete
272          */
273         /*
274          * XXX: Since the mount point is holding a reference on the
275          * underlying server socket, it is not easy to find out whether
276          * the server process is still running.  To handle this problem
277          * we loop waiting for the new socket to be connected (something
278          * which will only happen if the server is still running) or for
279          * the reference count on the server socket to drop to 1, which
280          * will happen if the server dies.  Sleep for 5 second intervals
281          * and keep polling the reference count.   XXX.
282          */
283         SOCK_LOCK(so);
284         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
285                 if (fmp->pm_server->f_count == 1) {
286                         SOCK_UNLOCK(so);
287                         error = ECONNREFUSED;
288                         goto bad;
289                 }
290                 (void) msleep((caddr_t) &so->so_timeo, SOCK_MTX(so), PSOCK,
291                     "portalcon", 5 * hz);
292         }
293         SOCK_UNLOCK(so);
294
295         if (so->so_error) {
296                 error = so->so_error;
297                 goto bad;
298         }
299
300         /*
301          * Set miscellaneous flags
302          */
303         SOCKBUF_LOCK(&so->so_rcv);
304         so->so_rcv.sb_timeo = 0;
305         so->so_rcv.sb_flags |= SB_NOINTR;
306         SOCKBUF_UNLOCK(&so->so_rcv);
307         SOCKBUF_LOCK(&so->so_snd);
308         so->so_snd.sb_timeo = 0;
309         so->so_snd.sb_flags |= SB_NOINTR;
310         SOCKBUF_UNLOCK(&so->so_snd);
311
312
313         pcred.pcr_flag = ap->a_mode;
314         pcred.pcr_uid = ap->a_cred->cr_uid;
315         pcred.pcr_ngroups = ap->a_cred->cr_ngroups;
316         bcopy(ap->a_cred->cr_groups, pcred.pcr_groups, 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                 struct thread *a_td;
451         } */ *ap;
452 {
453         struct vnode *vp = ap->a_vp;
454         struct vattr *vap = ap->a_vap;
455
456         vap->va_uid = 0;
457         vap->va_gid = 0;
458         vap->va_size = DEV_BSIZE;
459         vap->va_blocksize = DEV_BSIZE;
460         nanotime(&vap->va_atime);
461         vap->va_mtime = vap->va_atime;
462         vap->va_ctime = vap->va_mtime;
463         vap->va_gen = 0;
464         vap->va_flags = 0;
465         vap->va_rdev = NODEV;
466         /* vap->va_qbytes = 0; */
467         vap->va_bytes = 0;
468         vap->va_filerev = 0;
469         /* vap->va_qsize = 0; */
470         if (vp->v_vflag & VV_ROOT) {
471                 vap->va_type = VDIR;
472                 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
473                                 S_IRGRP|S_IWGRP|S_IXGRP|
474                                 S_IROTH|S_IWOTH|S_IXOTH;
475                 vap->va_nlink = 2;
476                 vap->va_fileid = 2;
477         } else {
478                 vap->va_type = VREG;
479                 vap->va_mode = S_IRUSR|S_IWUSR|
480                                 S_IRGRP|S_IWGRP|
481                                 S_IROTH|S_IWOTH;
482                 vap->va_nlink = 1;
483                 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
484         }
485         return (0);
486 }
487
488 static int
489 portal_setattr(ap)
490         struct vop_setattr_args /* {
491                 struct vnode *a_vp;
492                 struct vattr *a_vap;
493                 struct ucred *a_cred;
494                 struct thread *a_td;
495         } */ *ap;
496 {
497
498         /*
499          * Can't mess with the root vnode
500          */
501         if (ap->a_vp->v_vflag & VV_ROOT)
502                 return (EACCES);
503
504         if (ap->a_vap->va_flags != VNOVAL)
505                 return (EOPNOTSUPP);
506
507         return (0);
508 }
509
510 /*
511  * Fake readdir, just return empty directory.
512  * It is hard to deal with '.' and '..' so don't bother.
513  */
514 static int
515 portal_readdir(ap)
516         struct vop_readdir_args /* {
517                 struct vnode *a_vp;
518                 struct uio *a_uio;
519                 struct ucred *a_cred;
520                 int *a_eofflag;
521                 u_long *a_cookies;
522                 int a_ncookies;
523         } */ *ap;
524 {
525
526         /*
527          * We don't allow exporting portal mounts, and currently local
528          * requests do not need cookies.
529          */
530         if (ap->a_ncookies)
531                 panic("portal_readdir: not hungry");
532
533         return (0);
534 }
535
536 static int
537 portal_reclaim(ap)
538         struct vop_reclaim_args /* {
539                 struct vnode *a_vp;
540         } */ *ap;
541 {
542         struct portalnode *pt = VTOPORTAL(ap->a_vp);
543
544         if (pt->pt_arg) {
545                 free((caddr_t) pt->pt_arg, M_TEMP);
546                 pt->pt_arg = 0;
547         }
548         FREE(ap->a_vp->v_data, M_TEMP);
549         ap->a_vp->v_data = 0;
550
551         return (0);
552 }
553
554 struct vop_vector portal_vnodeops = {
555         .vop_default =          &default_vnodeops,
556
557         .vop_access =           VOP_NULL,
558         .vop_getattr =          portal_getattr,
559         .vop_lookup =           portal_lookup,
560         .vop_open =             portal_open,
561         .vop_pathconf =         vop_stdpathconf,
562         .vop_readdir =          portal_readdir,
563         .vop_reclaim =          portal_reclaim,
564         .vop_setattr =          portal_setattr,
565 };