]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/portalfs/portal_vnops.c
This commit was generated by cvs2svn to compensate for changes in r165009,
[FreeBSD/FreeBSD.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         return (0);
158
159 bad:;
160         if (fvp)
161                 vrele(fvp);
162         return (error);
163 }
164
165 static int
166 portal_connect(so, so2)
167         struct socket *so;
168         struct socket *so2;
169 {
170         /* from unp_connect, bypassing the namei stuff... */
171         struct socket *so3;
172         struct unpcb *unp2;
173         struct unpcb *unp3;
174
175         if (so2 == 0)
176                 return (ECONNREFUSED);
177
178         if (so->so_type != so2->so_type)
179                 return (EPROTOTYPE);
180
181         if ((so2->so_options & SO_ACCEPTCONN) == 0)
182                 return (ECONNREFUSED);
183
184         if ((so3 = sonewconn(so2, 0)) == 0)
185                 return (ECONNREFUSED);
186
187         unp2 = sotounpcb(so2);
188         unp3 = sotounpcb(so3);
189         if (unp2->unp_addr)
190                 unp3->unp_addr = (struct sockaddr_un *)
191                     sodupsockaddr((struct sockaddr *)unp2->unp_addr,
192                     M_NOWAIT);
193         so2 = so3;
194
195         return (uipc_connect2(so, so2));
196 }
197
198 static int
199 portal_open(ap)
200         struct vop_open_args /* {
201                 struct vnode *a_vp;
202                 int  a_mode;
203                 struct ucred *a_cred;
204                 struct thread *a_td;
205         } */ *ap;
206 {
207         struct socket *so = 0;
208         struct portalnode *pt;
209         struct thread *td = ap->a_td;
210         struct vnode *vp = ap->a_vp;
211         struct uio auio;
212         struct iovec aiov[2];
213         int res;
214         struct mbuf *cm = 0;
215         struct cmsghdr *cmsg;
216         int newfds;
217         int *ip;
218         int fd;
219         int error;
220         int len;
221         struct portalmount *fmp;
222         struct file *fp;
223         struct portal_cred pcred;
224
225         /*
226          * Nothing to do when opening the root node.
227          */
228         if (vp->v_vflag & VV_ROOT)
229                 return (0);
230
231         /*
232          * Can't be opened unless the caller is set up
233          * to deal with the side effects.  Check for this
234          * by testing whether td_dupfd has been set.
235          */
236         if (td->td_dupfd >= 0)
237                 return (ENODEV);
238
239         pt = VTOPORTAL(vp);
240         fmp = VFSTOPORTAL(vp->v_mount);
241
242         /*
243          * Create a new socket.
244          */
245         error = socreate(AF_UNIX, &so, SOCK_STREAM, 0, ap->a_td->td_ucred,
246             ap->a_td);
247         if (error)
248                 goto bad;
249
250         /*
251          * Reserve some buffer space
252          */
253         res = pt->pt_size + sizeof(pcred) + 512;        /* XXX */
254         error = soreserve(so, res, res);
255         if (error)
256                 goto bad;
257
258         /*
259          * Kick off connection
260          */
261         error = portal_connect(so, fmp->pm_server->f_data);
262         if (error)
263                 goto bad;
264
265         /*
266          * Wait for connection to complete
267          */
268         /*
269          * XXX: Since the mount point is holding a reference on the
270          * underlying server socket, it is not easy to find out whether
271          * the server process is still running.  To handle this problem
272          * we loop waiting for the new socket to be connected (something
273          * which will only happen if the server is still running) or for
274          * the reference count on the server socket to drop to 1, which
275          * will happen if the server dies.  Sleep for 5 second intervals
276          * and keep polling the reference count.   XXX.
277          */
278         /* XXXRW: Locking? */
279         SOCK_LOCK(so);
280         while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
281                 if (fmp->pm_server->f_count == 1) {
282                         SOCK_UNLOCK(so);
283                         error = ECONNREFUSED;
284                         goto bad;
285                 }
286                 (void) msleep((caddr_t) &so->so_timeo, SOCK_MTX(so), PSOCK,
287                     "portalcon", 5 * hz);
288         }
289         SOCK_UNLOCK(so);
290
291         if (so->so_error) {
292                 error = so->so_error;
293                 goto bad;
294         }
295
296         /*
297          * Set miscellaneous flags
298          */
299         SOCKBUF_LOCK(&so->so_rcv);
300         so->so_rcv.sb_timeo = 0;
301         so->so_rcv.sb_flags |= SB_NOINTR;
302         SOCKBUF_UNLOCK(&so->so_rcv);
303         SOCKBUF_LOCK(&so->so_snd);
304         so->so_snd.sb_timeo = 0;
305         so->so_snd.sb_flags |= SB_NOINTR;
306         SOCKBUF_UNLOCK(&so->so_snd);
307
308
309         pcred.pcr_flag = ap->a_mode;
310         pcred.pcr_uid = ap->a_cred->cr_uid;
311         pcred.pcr_ngroups = ap->a_cred->cr_ngroups;
312         bcopy(ap->a_cred->cr_groups, pcred.pcr_groups, NGROUPS * sizeof(gid_t));
313         aiov[0].iov_base = (caddr_t) &pcred;
314         aiov[0].iov_len = sizeof(pcred);
315         aiov[1].iov_base = pt->pt_arg;
316         aiov[1].iov_len = pt->pt_size;
317         auio.uio_iov = aiov;
318         auio.uio_iovcnt = 2;
319         auio.uio_rw = UIO_WRITE;
320         auio.uio_segflg = UIO_SYSSPACE;
321         auio.uio_td = td;
322         auio.uio_offset = 0;
323         auio.uio_resid = aiov[0].iov_len + aiov[1].iov_len;
324
325         error = sosend(so, (struct sockaddr *) 0, &auio,
326                         (struct mbuf *) 0, (struct mbuf *) 0, 0 , td);
327         if (error)
328                 goto bad;
329
330         len = auio.uio_resid = sizeof(int);
331         do {
332                 struct mbuf *m = 0;
333                 int flags = MSG_WAITALL;
334                 error = soreceive(so, (struct sockaddr **) 0, &auio,
335                                         &m, &cm, &flags);
336                 if (error)
337                         goto bad;
338
339                 /*
340                  * Grab an error code from the mbuf.
341                  */
342                 if (m) {
343                         m = m_pullup(m, sizeof(int));   /* Needed? */
344                         if (m) {
345                                 error = *(mtod(m, int *));
346                                 m_freem(m);
347                         } else {
348                                 error = EINVAL;
349                         }
350                 } else {
351                         if (cm == 0) {
352                                 error = ECONNRESET;      /* XXX */
353 #ifdef notdef
354                                 break;
355 #endif
356                         }
357                 }
358         } while (cm == 0 && auio.uio_resid == len && !error);
359
360         if (cm == 0)
361                 goto bad;
362
363         if (auio.uio_resid) {
364                 error = 0;
365 #ifdef notdef
366                 error = EMSGSIZE;
367                 goto bad;
368 #endif
369         }
370
371         /*
372          * XXX: Break apart the control message, and retrieve the
373          * received file descriptor.  Note that more than one descriptor
374          * may have been received, or that the rights chain may have more
375          * than a single mbuf in it.  What to do?
376          */
377         cmsg = mtod(cm, struct cmsghdr *);
378         newfds = (cmsg->cmsg_len - sizeof(*cmsg)) / sizeof (int);
379         if (newfds == 0) {
380                 error = ECONNREFUSED;
381                 goto bad;
382         }
383         /*
384          * At this point the rights message consists of a control message
385          * header, followed by a data region containing a vector of
386          * integer file descriptors.  The fds were allocated by the action
387          * of receiving the control message.
388          */
389         ip = (int *) (cmsg + 1);
390         fd = *ip++;
391         if (newfds > 1) {
392                 /*
393                  * Close extra fds.
394                  */
395                 int i;
396                 printf("portal_open: %d extra fds\n", newfds - 1);
397                 for (i = 1; i < newfds; i++) {
398                         portal_closefd(td, *ip);
399                         ip++;
400                 }
401         }
402
403         /*
404          * Check that the mode the file is being opened for is a subset
405          * of the mode of the existing descriptor.
406          */
407         if ((error = fget(td, fd, &fp)) != 0)
408                 goto bad;
409         if (((ap->a_mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
410                 fdrop(fp, td);
411                 portal_closefd(td, fd);
412                 error = EACCES;
413                 goto bad;
414         }
415         fdrop(fp, td);
416
417         /*
418          * Save the dup fd in the proc structure then return the
419          * special error code (ENXIO) which causes magic things to
420          * happen in vn_open.  The whole concept is, well, hmmm.
421          */
422         td->td_dupfd = fd;
423         error = ENXIO;
424
425 bad:;
426         /*
427          * And discard the control message.
428          */
429         if (cm) {
430                 m_freem(cm);
431         }
432
433         if (so) {
434                 soshutdown(so, 2);
435                 soclose(so);
436         }
437         return (error);
438 }
439
440 static int
441 portal_getattr(ap)
442         struct vop_getattr_args /* {
443                 struct vnode *a_vp;
444                 struct vattr *a_vap;
445                 struct ucred *a_cred;
446                 struct thread *a_td;
447         } */ *ap;
448 {
449         struct vnode *vp = ap->a_vp;
450         struct vattr *vap = ap->a_vap;
451
452         bzero(vap, sizeof(*vap));
453         vattr_null(vap);
454         vap->va_uid = 0;
455         vap->va_gid = 0;
456         vap->va_size = DEV_BSIZE;
457         vap->va_blocksize = DEV_BSIZE;
458         nanotime(&vap->va_atime);
459         vap->va_mtime = vap->va_atime;
460         vap->va_ctime = vap->va_mtime;
461         vap->va_gen = 0;
462         vap->va_flags = 0;
463         vap->va_rdev = 0;
464         /* vap->va_qbytes = 0; */
465         vap->va_bytes = 0;
466         /* vap->va_qsize = 0; */
467         if (vp->v_vflag & VV_ROOT) {
468                 vap->va_type = VDIR;
469                 vap->va_mode = S_IRUSR|S_IWUSR|S_IXUSR|
470                                 S_IRGRP|S_IWGRP|S_IXGRP|
471                                 S_IROTH|S_IWOTH|S_IXOTH;
472                 vap->va_nlink = 2;
473                 vap->va_fileid = 2;
474         } else {
475                 vap->va_type = VREG;
476                 vap->va_mode = S_IRUSR|S_IWUSR|
477                                 S_IRGRP|S_IWGRP|
478                                 S_IROTH|S_IWOTH;
479                 vap->va_nlink = 1;
480                 vap->va_fileid = VTOPORTAL(vp)->pt_fileid;
481         }
482         return (0);
483 }
484
485 static int
486 portal_setattr(ap)
487         struct vop_setattr_args /* {
488                 struct vnode *a_vp;
489                 struct vattr *a_vap;
490                 struct ucred *a_cred;
491                 struct thread *a_td;
492         } */ *ap;
493 {
494
495         /*
496          * Can't mess with the root vnode
497          */
498         if (ap->a_vp->v_vflag & VV_ROOT)
499                 return (EACCES);
500
501         if (ap->a_vap->va_flags != VNOVAL)
502                 return (EOPNOTSUPP);
503
504         return (0);
505 }
506
507 /*
508  * Fake readdir, just return empty directory.
509  * It is hard to deal with '.' and '..' so don't bother.
510  */
511 static int
512 portal_readdir(ap)
513         struct vop_readdir_args /* {
514                 struct vnode *a_vp;
515                 struct uio *a_uio;
516                 struct ucred *a_cred;
517                 int *a_eofflag;
518                 u_long *a_cookies;
519                 int a_ncookies;
520         } */ *ap;
521 {
522
523         /*
524          * We don't allow exporting portal mounts, and currently local
525          * requests do not need cookies.
526          */
527         if (ap->a_ncookies)
528                 panic("portal_readdir: not hungry");
529
530         return (0);
531 }
532
533 static int
534 portal_reclaim(ap)
535         struct vop_reclaim_args /* {
536                 struct vnode *a_vp;
537         } */ *ap;
538 {
539         struct portalnode *pt = VTOPORTAL(ap->a_vp);
540
541         if (pt->pt_arg) {
542                 free((caddr_t) pt->pt_arg, M_TEMP);
543                 pt->pt_arg = 0;
544         }
545         FREE(ap->a_vp->v_data, M_TEMP);
546         ap->a_vp->v_data = 0;
547
548         return (0);
549 }
550
551 struct vop_vector portal_vnodeops = {
552         .vop_default =          &default_vnodeops,
553
554         .vop_access =           VOP_NULL,
555         .vop_getattr =          portal_getattr,
556         .vop_lookup =           portal_lookup,
557         .vop_open =             portal_open,
558         .vop_pathconf =         vop_stdpathconf,
559         .vop_readdir =          portal_readdir,
560         .vop_reclaim =          portal_reclaim,
561         .vop_setattr =          portal_setattr,
562 };