kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / emulation / linux / i386 / linprocfs / linprocfs_subr.c
1 /*
2  * Copyright (c) 2000 Dag-Erling Coïdan Smørgrav
3  * Copyright (c) 1999 Pierre Beyssac
4  * Copyright (c) 1993 Jan-Simon Pendry
5  * Copyright (c) 1993
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      @(#)procfs_subr.c       8.6 (Berkeley) 5/14/95
40  *
41  * $FreeBSD: src/sys/i386/linux/linprocfs/linprocfs_subr.c,v 1.3.2.4 2001/06/25 19:46:47 pirzyk Exp $
42  * $DragonFly: src/sys/emulation/linux/i386/linprocfs/linprocfs_subr.c,v 1.5 2003/08/07 21:17:19 dillon Exp $
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include "linprocfs.h"
51
52 static struct pfsnode *pfshead;
53 static int pfsvplock;
54
55 extern int procfs_domem __P((struct proc *, struct proc *, struct pfsnode *pfsp, struct uio *uio));
56
57 /*
58  * allocate a pfsnode/vnode pair.  the vnode is
59  * referenced, but not locked.
60  *
61  * the pid, pfs_type, and mount point uniquely
62  * identify a pfsnode.  the mount point is needed
63  * because someone might mount this filesystem
64  * twice.
65  *
66  * all pfsnodes are maintained on a singly-linked
67  * list.  new nodes are only allocated when they cannot
68  * be found on this list.  entries on the list are
69  * removed when the vfs reclaim entry is called.
70  *
71  * a single lock is kept for the entire list.  this is
72  * needed because the getnewvnode() function can block
73  * waiting for a vnode to become free, in which case there
74  * may be more than one process trying to get the same
75  * vnode.  this lock is only taken if we are going to
76  * call getnewvnode, since the kernel itself is single-threaded.
77  *
78  * if an entry is found on the list, then call vget() to
79  * take a reference.  this is done because there may be
80  * zero references to it and so it needs to removed from
81  * the vnode free list.
82  */
83 int
84 linprocfs_allocvp(mp, vpp, pid, pfs_type)
85         struct mount *mp;
86         struct vnode **vpp;
87         long pid;
88         pfstype pfs_type;
89 {
90         struct thread *td = curthread;  /* XXX */
91         struct pfsnode *pfs;
92         struct vnode *vp;
93         struct pfsnode **pp;
94         int error;
95
96 loop:
97         for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
98                 vp = PFSTOV(pfs);
99                 if (pfs->pfs_pid == pid &&
100                     pfs->pfs_type == pfs_type &&
101                     vp->v_mount == mp) {
102                         if (vget(vp, 0, td))
103                                 goto loop;
104                         *vpp = vp;
105                         return (0);
106                 }
107         }
108
109         /*
110          * otherwise lock the vp list while we call getnewvnode
111          * since that can block.
112          */
113         if (pfsvplock & PROCFS_LOCKED) {
114                 pfsvplock |= PROCFS_WANT;
115                 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
116                 goto loop;
117         }
118         pfsvplock |= PROCFS_LOCKED;
119
120         /*
121          * Do the MALLOC before the getnewvnode since doing so afterward
122          * might cause a bogus v_data pointer to get dereferenced
123          * elsewhere if MALLOC should block.
124          */
125         MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
126
127         if ((error = getnewvnode(VT_PROCFS, mp, linprocfs_vnodeop_p, vpp)) != 0) {
128                 FREE(pfs, M_TEMP);
129                 goto out;
130         }
131         vp = *vpp;
132
133         vp->v_data = pfs;
134
135         pfs->pfs_next = 0;
136         pfs->pfs_pid = (pid_t) pid;
137         pfs->pfs_type = pfs_type;
138         pfs->pfs_vnode = vp;
139         pfs->pfs_flags = 0;
140         pfs->pfs_lockowner = NULL;
141         pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
142
143         switch (pfs_type) {
144         case Proot:     /* /proc = dr-xr-xr-x */
145                 pfs->pfs_mode = (VREAD|VEXEC) |
146                                 (VREAD|VEXEC) >> 3 |
147                                 (VREAD|VEXEC) >> 6;
148                 vp->v_type = VDIR;
149                 vp->v_flag = VROOT;
150                 break;
151
152         case Pself:     /* /proc/self = lr--r--r-- */
153                 pfs->pfs_mode = (VREAD) |
154                                 (VREAD >> 3) |
155                                 (VREAD >> 6);
156                 vp->v_type = VLNK;
157                 break;
158
159         case Pproc:
160                 pfs->pfs_mode = (VREAD|VEXEC) |
161                                 (VREAD|VEXEC) >> 3 |
162                                 (VREAD|VEXEC) >> 6;
163                 vp->v_type = VDIR;
164                 break;
165
166         case Pexe:
167                 pfs->pfs_mode = (VREAD|VEXEC) |
168                                 (VREAD|VEXEC) >> 3 |
169                                 (VREAD|VEXEC) >> 6;
170                 vp->v_type = VLNK;
171                 break;
172
173         case Pmem:
174                 pfs->pfs_mode = (VREAD|VWRITE) |
175                                 (VREAD) >> 3;;
176                 vp->v_type = VREG;
177                 break;
178
179         case Pprocstat:
180         case Pprocstatus:
181                 /* fallthrough */
182                 
183         case Pmeminfo:
184         case Pcpuinfo:
185         case Pstat:
186         case Puptime:
187         case Pversion:
188         case Ploadavg:
189                 pfs->pfs_mode = (VREAD) |
190                                 (VREAD >> 3) |
191                                 (VREAD >> 6);
192                 vp->v_type = VREG;
193                 break;
194
195         default:
196                 panic("linprocfs_allocvp");
197         }
198
199         /* add to procfs vnode list */
200         for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
201                 continue;
202         *pp = pfs;
203
204 out:
205         pfsvplock &= ~PROCFS_LOCKED;
206
207         if (pfsvplock & PROCFS_WANT) {
208                 pfsvplock &= ~PROCFS_WANT;
209                 wakeup((caddr_t) &pfsvplock);
210         }
211
212         return (error);
213 }
214
215 int
216 linprocfs_freevp(vp)
217         struct vnode *vp;
218 {
219         struct pfsnode **pfspp;
220         struct pfsnode *pfs = VTOPFS(vp);
221
222         for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
223                 if (*pfspp == pfs) {
224                         *pfspp = pfs->pfs_next;
225                         break;
226                 }
227         }
228
229         FREE(vp->v_data, M_TEMP);
230         vp->v_data = 0;
231         return (0);
232 }
233
234 int
235 linprocfs_rw(ap)
236         struct vop_read_args *ap;
237 {
238         struct vnode *vp = ap->a_vp;
239         struct uio *uio = ap->a_uio;
240         struct thread *td = uio->uio_td;
241         struct pfsnode *pfs = VTOPFS(vp);
242         struct proc *p;
243         struct proc *curp;
244         int rtval;
245
246         curp = td->td_proc;
247         KKASSERT(curp);
248
249         p = PFIND(pfs->pfs_pid);
250         if (p == 0)
251                 return (EINVAL);
252         if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE)
253                 return (EACCES);
254
255         while (pfs->pfs_lockowner) {
256                 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
257         }
258         pfs->pfs_lockowner = curthread;
259
260         switch (pfs->pfs_type) {
261         case Pmem:
262                 rtval = procfs_domem(curp, p, pfs, uio);
263                 break;
264         case Pprocstat:
265                 rtval = linprocfs_doprocstat(curp, p, pfs, uio);
266                 break;
267         case Pprocstatus:
268                 rtval = linprocfs_doprocstatus(curp, p, pfs, uio);
269                 break;
270         case Pmeminfo:
271                 rtval = linprocfs_domeminfo(curp, p, pfs, uio);
272                 break;
273         case Pcpuinfo:
274                 rtval = linprocfs_docpuinfo(curp, p, pfs, uio);
275                 break;
276         case Pstat:
277                 rtval = linprocfs_dostat(curp, p, pfs, uio);
278                 break;
279         case Puptime:
280                 rtval = linprocfs_douptime(curp, p, pfs, uio);
281                 break;
282         case Pversion:
283                 rtval = linprocfs_doversion(curp, p, pfs, uio);
284                 break;
285         case Ploadavg:
286                 rtval = linprocfs_doloadavg(curp, p, pfs, uio);
287                 break;
288         default:
289                 rtval = EOPNOTSUPP;
290                 break;
291         }
292         pfs->pfs_lockowner = NULL;
293         wakeup(&pfs->pfs_lockowner);
294         return rtval;
295 }
296
297 #if 0
298 /*
299  * Get a string from userland into (buf).  Strip a trailing
300  * nl character (to allow easy access from the shell).
301  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
302  * will automatically add a nul char at the end.
303  *
304  * Returns 0 on success or the following errors
305  *
306  * EINVAL:    file offset is non-zero.
307  * EMSGSIZE:  message is longer than kernel buffer
308  * EFAULT:    user i/o buffer is not addressable
309  */
310 int
311 vfs_getuserstr(uio, buf, buflenp)
312         struct uio *uio;
313         char *buf;
314         int *buflenp;
315 {
316         int xlen;
317         int error;
318
319         if (uio->uio_offset != 0)
320                 return (EINVAL);
321
322         xlen = *buflenp;
323
324         /* must be able to read the whole string in one go */
325         if (xlen < uio->uio_resid)
326                 return (EMSGSIZE);
327         xlen = uio->uio_resid;
328
329         if ((error = uiomove(buf, xlen, uio)) != 0)
330                 return (error);
331
332         /* allow multiple writes without seeks */
333         uio->uio_offset = 0;
334
335         /* cleanup string and remove trailing newline */
336         buf[xlen] = '\0';
337         xlen = strlen(buf);
338         if (xlen > 0 && buf[xlen-1] == '\n')
339                 buf[--xlen] = '\0';
340         *buflenp = xlen;
341
342         return (0);
343 }
344
345 vfs_namemap_t *
346 vfs_findname(nm, buf, buflen)
347         vfs_namemap_t *nm;
348         char *buf;
349         int buflen;
350 {
351
352         for (; nm->nm_name; nm++)
353                 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
354                         return (nm);
355
356         return (0);
357 }
358 #endif
359
360 void
361 linprocfs_exit(struct thread *td)
362 {
363         struct pfsnode *pfs;
364         pid_t pid = (td->td_proc) ? td->td_proc->p_pid : -1; /* YYY */
365
366         /*
367          * The reason for this loop is not obvious -- basicly,
368          * linprocfs_freevp(), which is called via vgone() (eventually),
369          * removes the specified procfs node from the pfshead list.
370          * It does this by *pfsp = pfs->pfs_next, meaning that it
371          * overwrites the node.  So when we do pfs = pfs->next, we
372          * end up skipping the node that replaces the one that was
373          * vgone'd.  Since it may have been the last one on the list,
374          * it may also have been set to null -- but *our* pfs pointer,
375          * here, doesn't see this.  So the loop starts from the beginning
376          * again.
377          *
378          * This is not a for() loop because the final event
379          * would be "pfs = pfs->pfs_next"; in the case where
380          * pfs is set to pfshead again, that would mean that
381          * pfshead is skipped over.
382          *
383          */
384         pfs = pfshead;
385         while (pfs) {
386                 if (pfs->pfs_pid == pid) {
387                         vgone(PFSTOV(pfs));
388                         pfs = pfshead;
389                 } else
390                         pfs = pfs->pfs_next;
391         }
392 }