]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/fs/devfs/devfs_devs.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / fs / devfs / devfs_devs.c
1 /*-
2  * Copyright (c) 2000,2004
3  *      Poul-Henning Kamp.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Neither the name of the University nor the names of its contributors
11  *    may be used to endorse or promote products derived from this software
12  *    without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36
27  *
28  * $FreeBSD$
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/conf.h>
34 #include <sys/dirent.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/proc.h>
40 #include <sys/sx.h>
41 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
43
44 #include <sys/kdb.h>
45
46 #include <fs/devfs/devfs.h>
47 #include <fs/devfs/devfs_int.h>
48
49 #include <security/mac/mac_framework.h>
50
51 /*
52  * The one true (but secret) list of active devices in the system.
53  * Locked by dev_lock()/devmtx
54  */
55 struct cdev_priv_list cdevp_list = TAILQ_HEAD_INITIALIZER(cdevp_list);
56
57 struct unrhdr *devfs_inos;
58
59
60 static MALLOC_DEFINE(M_DEVFS2, "DEVFS2", "DEVFS data 2");
61 static MALLOC_DEFINE(M_DEVFS3, "DEVFS3", "DEVFS data 3");
62 static MALLOC_DEFINE(M_CDEVP, "DEVFS1", "DEVFS cdev_priv storage");
63
64 static SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem");
65
66 static unsigned devfs_generation;
67 SYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD,
68         &devfs_generation, 0, "DEVFS generation number");
69
70 unsigned devfs_rule_depth = 1;
71 SYSCTL_UINT(_vfs_devfs, OID_AUTO, rule_depth, CTLFLAG_RW,
72         &devfs_rule_depth, 0, "Max depth of ruleset include");
73
74 /*
75  * Helper sysctl for devname(3).  We're given a struct cdev * and return
76  * the name, if any, registered by the device driver.
77  */
78 static int
79 sysctl_devname(SYSCTL_HANDLER_ARGS)
80 {
81         int error;
82         dev_t ud;
83         struct cdev_priv *cdp;
84
85         error = SYSCTL_IN(req, &ud, sizeof (ud));
86         if (error)
87                 return (error);
88         if (ud == NODEV)
89                 return(EINVAL);
90 /*
91         ud ^ devfs_random();
92 */
93         dev_lock();
94         TAILQ_FOREACH(cdp, &cdevp_list, cdp_list)
95                 if (cdp->cdp_inode == ud)
96                         break;
97         dev_unlock();
98         if (cdp == NULL)
99                 return(ENOENT);
100         return(SYSCTL_OUT(req, cdp->cdp_c.si_name, strlen(cdp->cdp_c.si_name) + 1));
101         return (error);
102 }
103
104 SYSCTL_PROC(_kern, OID_AUTO, devname,
105     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY|CTLFLAG_MPSAFE,
106     NULL, 0, sysctl_devname, "", "devname(3) handler");
107
108 SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev, CTLFLAG_RD,
109     0, sizeof(struct cdev), "sizeof(struct cdev)");
110
111 SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev_priv, CTLFLAG_RD,
112     0, sizeof(struct cdev_priv), "sizeof(struct cdev_priv)");
113
114 struct cdev *
115 devfs_alloc(int flags)
116 {
117         struct cdev_priv *cdp;
118         struct cdev *cdev;
119         struct timespec ts;
120
121         cdp = malloc(sizeof *cdp, M_CDEVP, M_USE_RESERVE | M_ZERO |
122             ((flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK));
123         if (cdp == NULL)
124                 return (NULL);
125
126         cdp->cdp_dirents = &cdp->cdp_dirent0;
127         cdp->cdp_dirent0 = NULL;
128         cdp->cdp_maxdirent = 0;
129         cdp->cdp_inode = 0;
130
131         cdev = &cdp->cdp_c;
132
133         cdev->si_name = cdev->__si_namebuf;
134         LIST_INIT(&cdev->si_children);
135         vfs_timestamp(&ts);
136         cdev->si_atime = cdev->si_mtime = cdev->si_ctime = ts;
137         cdev->si_cred = NULL;
138
139         return (cdev);
140 }
141
142 void
143 devfs_free(struct cdev *cdev)
144 {
145         struct cdev_priv *cdp;
146
147         cdp = cdev2priv(cdev);
148         if (cdev->si_cred != NULL)
149                 crfree(cdev->si_cred);
150         if (cdp->cdp_inode > 0)
151                 free_unr(devfs_inos, cdp->cdp_inode);
152         if (cdp->cdp_maxdirent > 0) 
153                 free(cdp->cdp_dirents, M_DEVFS2);
154         free(cdp, M_CDEVP);
155 }
156
157 struct devfs_dirent *
158 devfs_find(struct devfs_dirent *dd, const char *name, int namelen)
159 {
160         struct devfs_dirent *de;
161
162         TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
163                 if (namelen != de->de_dirent->d_namlen)
164                         continue;
165                 if (bcmp(name, de->de_dirent->d_name, namelen) != 0)
166                         continue;
167                 break;
168         }
169         return (de);
170 }
171
172 struct devfs_dirent *
173 devfs_newdirent(char *name, int namelen)
174 {
175         int i;
176         struct devfs_dirent *de;
177         struct dirent d;
178
179         d.d_namlen = namelen;
180         i = sizeof (*de) + GENERIC_DIRSIZ(&d); 
181         de = malloc(i, M_DEVFS3, M_WAITOK | M_ZERO);
182         de->de_dirent = (struct dirent *)(de + 1);
183         de->de_dirent->d_namlen = namelen;
184         de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d);
185         bcopy(name, de->de_dirent->d_name, namelen);
186         de->de_dirent->d_name[namelen] = '\0';
187         vfs_timestamp(&de->de_ctime);
188         de->de_mtime = de->de_atime = de->de_ctime;
189         de->de_links = 1;
190         de->de_holdcnt = 1;
191 #ifdef MAC
192         mac_devfs_init(de);
193 #endif
194         return (de);
195 }
196
197 struct devfs_dirent *
198 devfs_vmkdir(struct devfs_mount *dmp, char *name, int namelen, struct devfs_dirent *dotdot, u_int inode)
199 {
200         struct devfs_dirent *dd;
201         struct devfs_dirent *de;
202
203         /* Create the new directory */
204         dd = devfs_newdirent(name, namelen);
205         TAILQ_INIT(&dd->de_dlist);
206         dd->de_dirent->d_type = DT_DIR;
207         dd->de_mode = 0555;
208         dd->de_links = 2;
209         dd->de_dir = dd;
210         if (inode != 0)
211                 dd->de_inode = inode;
212         else
213                 dd->de_inode = alloc_unr(devfs_inos);
214
215         /* Create the "." entry in the new directory */
216         de = devfs_newdirent(".", 1);
217         de->de_dirent->d_type = DT_DIR;
218         de->de_flags |= DE_DOT;
219         TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
220         de->de_dir = dd;
221
222         /* Create the ".." entry in the new directory */
223         de = devfs_newdirent("..", 2);
224         de->de_dirent->d_type = DT_DIR;
225         de->de_flags |= DE_DOTDOT;
226         TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
227         if (dotdot == NULL) {
228                 de->de_dir = dd;
229         } else {
230                 de->de_dir = dotdot;
231                 TAILQ_INSERT_TAIL(&dotdot->de_dlist, dd, de_list);
232                 dotdot->de_links++;
233         }
234
235 #ifdef MAC
236         mac_devfs_create_directory(dmp->dm_mount, name, namelen, dd);
237 #endif
238         return (dd);
239 }
240
241 void
242 devfs_dirent_free(struct devfs_dirent *de)
243 {
244         free(de, M_DEVFS3);
245 }
246
247 /*
248  * The caller needs to hold the dm for the duration of the call since
249  * dm->dm_lock may be temporary dropped.
250  */
251 void
252 devfs_delete(struct devfs_mount *dm, struct devfs_dirent *de, int vp_locked)
253 {
254         struct vnode *vp;
255
256         KASSERT((de->de_flags & DE_DOOMED) == 0,
257                 ("devfs_delete doomed dirent"));
258         de->de_flags |= DE_DOOMED;
259         mtx_lock(&devfs_de_interlock);
260         vp = de->de_vnode;
261         if (vp != NULL) {
262                 VI_LOCK(vp);
263                 mtx_unlock(&devfs_de_interlock);
264                 vholdl(vp);
265                 sx_unlock(&dm->dm_lock);
266                 if (!vp_locked)
267                         vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY);
268                 else
269                         VI_UNLOCK(vp);
270                 vgone(vp);
271                 if (!vp_locked)
272                         VOP_UNLOCK(vp, 0);
273                 vdrop(vp);
274                 sx_xlock(&dm->dm_lock);
275         } else
276                 mtx_unlock(&devfs_de_interlock);
277         if (de->de_symlink) {
278                 free(de->de_symlink, M_DEVFS);
279                 de->de_symlink = NULL;
280         }
281 #ifdef MAC
282         mac_devfs_destroy(de);
283 #endif
284         if (de->de_inode > DEVFS_ROOTINO) {
285                 free_unr(devfs_inos, de->de_inode);
286                 de->de_inode = 0;
287         }
288         if (DEVFS_DE_DROP(de))
289                 devfs_dirent_free(de);
290 }
291
292 /*
293  * Called on unmount.
294  * Recursively removes the entire tree.
295  * The caller needs to hold the dm for the duration of the call.
296  */
297
298 static void
299 devfs_purge(struct devfs_mount *dm, struct devfs_dirent *dd)
300 {
301         struct devfs_dirent *de;
302
303         sx_assert(&dm->dm_lock, SX_XLOCKED);
304         for (;;) {
305                 de = TAILQ_FIRST(&dd->de_dlist);
306                 if (de == NULL)
307                         break;
308                 TAILQ_REMOVE(&dd->de_dlist, de, de_list);
309                 if (de->de_flags & (DE_DOT|DE_DOTDOT))
310                         devfs_delete(dm, de, 0);
311                 else if (de->de_dirent->d_type == DT_DIR)
312                         devfs_purge(dm, de);
313                 else 
314                         devfs_delete(dm, de, 0);
315         }
316         devfs_delete(dm, dd, 0);
317 }
318
319 /*
320  * Each cdev_priv has an array of pointers to devfs_dirent which is indexed
321  * by the mount points dm_idx.
322  * This function extends the array when necessary, taking into account that
323  * the default array is 1 element and not malloc'ed.
324  */
325 static void
326 devfs_metoo(struct cdev_priv *cdp, struct devfs_mount *dm)
327 {
328         struct devfs_dirent **dep;
329         int siz;
330
331         siz = (dm->dm_idx + 1) * sizeof *dep;
332         dep = malloc(siz, M_DEVFS2, M_WAITOK | M_ZERO);
333         dev_lock();
334         if (dm->dm_idx <= cdp->cdp_maxdirent) {
335                 /* We got raced */
336                 dev_unlock();
337                 free(dep, M_DEVFS2);
338                 return;
339         } 
340         memcpy(dep, cdp->cdp_dirents, (cdp->cdp_maxdirent + 1) * sizeof *dep);
341         if (cdp->cdp_maxdirent > 0)
342                 free(cdp->cdp_dirents, M_DEVFS2);
343         cdp->cdp_dirents = dep;
344         /*
345          * XXX: if malloc told us how much we actually got this could
346          * XXX: be optimized.
347          */
348         cdp->cdp_maxdirent = dm->dm_idx;
349         dev_unlock();
350 }
351
352 /*
353  * The caller needs to hold the dm for the duration of the call.
354  */
355 static int
356 devfs_populate_loop(struct devfs_mount *dm, int cleanup)
357 {
358         struct cdev_priv *cdp;
359         struct devfs_dirent *de;
360         struct devfs_dirent *dd;
361         struct cdev *pdev;
362         int j;
363         char *q, *s;
364
365         sx_assert(&dm->dm_lock, SX_XLOCKED);
366         dev_lock();
367         TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
368
369                 KASSERT(cdp->cdp_dirents != NULL, ("NULL cdp_dirents"));
370
371                 /*
372                  * If we are unmounting, or the device has been destroyed,
373                  * clean up our dirent.
374                  */
375                 if ((cleanup || !(cdp->cdp_flags & CDP_ACTIVE)) &&
376                     dm->dm_idx <= cdp->cdp_maxdirent &&
377                     cdp->cdp_dirents[dm->dm_idx] != NULL) {
378                         de = cdp->cdp_dirents[dm->dm_idx];
379                         cdp->cdp_dirents[dm->dm_idx] = NULL;
380                         KASSERT(cdp == de->de_cdp,
381                             ("%s %d %s %p %p", __func__, __LINE__,
382                             cdp->cdp_c.si_name, cdp, de->de_cdp));
383                         KASSERT(de->de_dir != NULL, ("Null de->de_dir"));
384                         dev_unlock();
385
386                         TAILQ_REMOVE(&de->de_dir->de_dlist, de, de_list);
387                         de->de_cdp = NULL;
388                         de->de_inode = 0;
389                         devfs_delete(dm, de, 0);
390                         dev_lock();
391                         cdp->cdp_inuse--;
392                         dev_unlock();
393                         return (1);
394                 }
395                 /*
396                  * GC any lingering devices
397                  */
398                 if (!(cdp->cdp_flags & CDP_ACTIVE)) {
399                         if (cdp->cdp_inuse > 0)
400                                 continue;
401                         TAILQ_REMOVE(&cdevp_list, cdp, cdp_list);
402                         dev_unlock();
403                         dev_rel(&cdp->cdp_c);
404                         return (1);
405                 }
406                 /*
407                  * Don't create any new dirents if we are unmounting
408                  */
409                 if (cleanup)
410                         continue;
411                 KASSERT((cdp->cdp_flags & CDP_ACTIVE), ("Bogons, I tell ya'!"));
412
413                 if (dm->dm_idx <= cdp->cdp_maxdirent &&
414                     cdp->cdp_dirents[dm->dm_idx] != NULL) {
415                         de = cdp->cdp_dirents[dm->dm_idx];
416                         KASSERT(cdp == de->de_cdp, ("inconsistent cdp"));
417                         continue;
418                 }
419
420
421                 cdp->cdp_inuse++;
422                 dev_unlock();
423
424                 if (dm->dm_idx > cdp->cdp_maxdirent)
425                         devfs_metoo(cdp, dm);
426
427                 dd = dm->dm_rootdir;
428                 s = cdp->cdp_c.si_name;
429                 for (;;) {
430                         for (q = s; *q != '/' && *q != '\0'; q++)
431                                 continue;
432                         if (*q != '/')
433                                 break;
434                         de = devfs_find(dd, s, q - s);
435                         if (de == NULL)
436                                 de = devfs_vmkdir(dm, s, q - s, dd, 0);
437                         s = q + 1;
438                         dd = de;
439                 }
440
441                 de = devfs_newdirent(s, q - s);
442                 if (cdp->cdp_c.si_flags & SI_ALIAS) {
443                         de->de_uid = 0;
444                         de->de_gid = 0;
445                         de->de_mode = 0755;
446                         de->de_dirent->d_type = DT_LNK;
447                         pdev = cdp->cdp_c.si_parent;
448                         j = strlen(pdev->si_name) + 1;
449                         de->de_symlink = malloc(j, M_DEVFS, M_WAITOK);
450                         bcopy(pdev->si_name, de->de_symlink, j);
451                 } else {
452                         de->de_uid = cdp->cdp_c.si_uid;
453                         de->de_gid = cdp->cdp_c.si_gid;
454                         de->de_mode = cdp->cdp_c.si_mode;
455                         de->de_dirent->d_type = DT_CHR;
456                 }
457                 de->de_inode = cdp->cdp_inode;
458                 de->de_cdp = cdp;
459 #ifdef MAC
460                 mac_devfs_create_device(cdp->cdp_c.si_cred, dm->dm_mount,
461                     &cdp->cdp_c, de);
462 #endif
463                 de->de_dir = dd;
464                 TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
465                 devfs_rules_apply(dm, de);
466                 dev_lock();
467                 /* XXX: could check that cdp is still active here */
468                 KASSERT(cdp->cdp_dirents[dm->dm_idx] == NULL,
469                     ("%s %d\n", __func__, __LINE__));
470                 cdp->cdp_dirents[dm->dm_idx] = de;
471                 KASSERT(de->de_cdp != (void *)0xdeadc0de,
472                     ("%s %d\n", __func__, __LINE__));
473                 dev_unlock();
474                 return (1);
475         }
476         dev_unlock();
477         return (0);
478 }
479
480 /*
481  * The caller needs to hold the dm for the duration of the call.
482  */
483 void
484 devfs_populate(struct devfs_mount *dm)
485 {
486
487         sx_assert(&dm->dm_lock, SX_XLOCKED);
488         if (dm->dm_generation == devfs_generation)
489                 return;
490         while (devfs_populate_loop(dm, 0))
491                 continue;
492         dm->dm_generation = devfs_generation;
493 }
494
495 /*
496  * The caller needs to hold the dm for the duration of the call.
497  */
498 void
499 devfs_cleanup(struct devfs_mount *dm)
500 {
501
502         sx_assert(&dm->dm_lock, SX_XLOCKED);
503         while (devfs_populate_loop(dm, 1))
504                 continue;
505         devfs_purge(dm, dm->dm_rootdir);
506 }
507
508 /*
509  * devfs_create() and devfs_destroy() are called from kern_conf.c and
510  * in both cases the devlock() mutex is held, so no further locking
511  * is necesary and no sleeping allowed.
512  */
513
514 void
515 devfs_create(struct cdev *dev)
516 {
517         struct cdev_priv *cdp;
518
519         mtx_assert(&devmtx, MA_OWNED);
520         cdp = cdev2priv(dev);
521         cdp->cdp_flags |= CDP_ACTIVE;
522         cdp->cdp_inode = alloc_unrl(devfs_inos);
523         dev_refl(dev);
524         TAILQ_INSERT_TAIL(&cdevp_list, cdp, cdp_list);
525         devfs_generation++;
526 }
527
528 void
529 devfs_destroy(struct cdev *dev)
530 {
531         struct cdev_priv *cdp;
532
533         mtx_assert(&devmtx, MA_OWNED);
534         cdp = cdev2priv(dev);
535         cdp->cdp_flags &= ~CDP_ACTIVE;
536         devfs_generation++;
537 }
538
539 static void
540 devfs_devs_init(void *junk __unused)
541 {
542
543         devfs_inos = new_unrhdr(DEVFS_ROOTINO + 1, INT_MAX, &devmtx);
544 }
545
546 SYSINIT(devfs_devs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_devs_init, NULL);