DEVFS - Bring Alex's GSOC DEVFS core into the main tree
[dragonfly.git] / sys / vfs / devfs / devfs.h
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #ifndef _VFS_DEVFS_H_
35 #define _VFS_DEVFS_H_
36
37 #if !defined(_KERNEL) && !defined(_KERNEL_STRUCTURES)
38 #error "This file should not be included by userland programs."
39 #endif
40
41 #ifndef _SYS_QUEUE_H_
42 #include <sys/queue.h>
43 #endif
44 #ifndef _SYS_LOCK_H_
45 #include <sys/lock.h>
46 #endif
47 #ifndef _SYS_CONF_H_
48 #include <sys/conf.h>
49 #endif
50 #ifndef _SYS_MSGPORT2_H_
51 //#include <sys/msgport2.h>
52 #endif
53 #ifndef _SYS_MSGPORT_H_
54 #include <sys/msgport.h>
55 #endif
56 #ifndef _SYS_DIRENT_H_
57 #include <sys/dirent.h>
58 #endif
59 #ifndef _SYS_DEVICE_H_
60 #include <sys/device.h>
61 #endif
62 #ifndef _SYS_UCRED_H_
63 #include <sys/ucred.h>
64 #endif
65
66
67 typedef enum {
68         Proot,          /* the filesystem root */
69         Plink,
70         Preg,
71         Pdir,
72         Pdev
73 } devfs_nodetype;
74
75 /*
76 struct devfs_dev {
77         cdev_t          d_devt;
78         uint16_t        d_flags;
79         uint16_t        d_class;
80
81         //XXX: do I need a use count so I know when it is safe to
82         //              unload the module or detach or whatever?
83         TAILQ_ENTRY(devfs_dev) link;
84 };
85 */
86
87 struct devfs_node {
88         cdev_t  d_dev;                                  /* device assoicated with this node */
89
90         struct mount            *mp;            /* mount point of this node */
91         struct dirent           d_dir;          /* dirent data (name, inode, ...) */
92         struct vnode            *v_node;        /* assoicated vnode */
93         struct devfs_node       *parent;        /* parent of this node */
94         devfs_nodetype          node_type;      /* devfs node type */
95
96         u_int64_t       refs;           /* number of open references */
97         size_t          nchildren;      /* number of children of a parent */
98         u_int64_t       cookie_jar;     /* cookie pool for children */
99         u_int64_t       cookie;         /* directory entry cookie for readdir */
100
101         struct devfs_node       *link_target;   /* target of this autolink-type node */
102         size_t          nlinks;                                 /* number of links that point to this node */
103
104         char            *symlink_name;          /* symlink name for readlink */
105         size_t          symlink_namelen;        /* symlink name length for readlink */
106
107         u_short         mode;           /* files access mode and type */
108         uid_t           uid;            /* owner user id */
109         gid_t           gid;            /* owner group id */
110         u_long          flags;
111
112         struct timespec atime;  /* time of last access */
113         struct timespec mtime;  /* time of last modification */
114         struct timespec ctime;  /* time file changed */
115
116
117
118         /* Other members */
119         TAILQ_ENTRY(devfs_node)         link;
120         TAILQ_HEAD(, devfs_node)        list;   /* linked list of children */
121 };
122
123 struct devfs_orphan {
124         struct devfs_node       *node;
125
126         TAILQ_ENTRY(devfs_orphan)       link;
127 };
128
129 struct devfs_mnt_data {
130         TAILQ_HEAD(, devfs_orphan)      orphan_list;
131
132         struct devfs_node       *root_node;
133         struct mount            *mp;
134
135         uint32_t        mnt_type;
136         size_t          leak_count;
137
138         int             jailed;
139         size_t  mntonnamelen;
140         TAILQ_ENTRY(devfs_mnt_data)     link;
141 };
142
143 struct devfs_clone_handler {
144         char            name[128];
145         u_char          namlen;
146         d_clone_t       *nhandler;
147
148         TAILQ_ENTRY(devfs_clone_handler)        link;
149 };
150
151
152 struct devfs_alias {
153         char    name[PATH_MAX + 1];
154         cdev_t  dev_target;
155
156         TAILQ_ENTRY(devfs_alias)        link;
157 };
158
159
160 typedef struct devfs_msg {
161         struct lwkt_msg hdr;
162         __uint32_t              id;
163
164         union {
165                 struct {
166                         cdev_t  dev;
167                         uid_t   uid;
168                         gid_t   gid;
169                         int             perms;
170                 } __m_dev;
171                 struct {
172                         struct devfs_mnt_data *mnt;
173                 } __m_mnt;
174                 struct {
175                         char            *name;
176                         d_clone_t       *nhandler;
177                 } __m_chandler;
178                 struct {
179                         void *load;
180                 } __m_gen;
181                 struct {
182                         void *resp;
183                 } __m_resp;
184                 struct {
185                         udev_t  udev;
186                 } __m_udev;
187                 struct {
188                         cdev_t  cdev;
189                 } __m_cdev;
190                 struct {
191                         char *name;
192                 } __m_name;
193                 struct {
194                         char *basename;
195                         u_char unit;
196                         struct vnode *vp;
197                 } __m_clone;
198                 struct {
199                         struct devfs_node *node;
200                 } __m_node;
201                 struct {
202                         char *name;
203                         char *target;
204                         struct mount *mp;
205                 } __m_link;
206                 struct {
207                         struct dev_ops *ops;
208                         int minor;
209                 } __m_ops;
210         } __m_u;
211
212 #define m_chandler      __m_u.__m_chandler
213 #define m_mnt   __m_u.__m_mnt.mnt
214 #define m_load  __m_u.__m_gen.load
215 #define m_response      __m_u.__m_resp.resp
216 #define m_dev   __m_u.__m_dev
217 #define m_link  __m_u.__m_link
218 #define m_udev  __m_u.__m_udev.udev
219 #define m_cdev  __m_u.__m_cdev.cdev
220 #define m_name  __m_u.__m_name.name
221 #define m_clone __m_u.__m_clone
222 #define m_node  __m_u.__m_node.node
223 #define m_ops   __m_u.__m_ops
224
225 } *devfs_msg_t;
226
227 typedef struct devfs_core_args {
228     thread_t     td;
229 } *devfs_core_args_t;
230
231
232 TAILQ_HEAD(devfs_node_head, devfs_node);
233 TAILQ_HEAD(devfs_dev_head, cdev);
234 TAILQ_HEAD(devfs_mnt_head, devfs_mnt_data);
235 TAILQ_HEAD(devfs_chandler_head, devfs_clone_handler);
236 TAILQ_HEAD(devfs_alias_head, devfs_alias);
237
238 typedef void (devfs_scan_t)(cdev_t);
239
240
241 #define DEVFS_NODE(x)                   ((struct devfs_node *)((x)->v_data))
242 #define DEVFS_MNTDATA(x)                ((struct devfs_mnt_data *)((x)->mnt_data))
243 #define DEVFS_ORPHANLIST(x)             (&(DEVFS_MNTDATA(x)->orphan_list))
244 #define DEVFS_DENODE_HEAD(x)    (&((x)->list))
245 #define DEVFS_ISDIGIT(x)                ((x >= '0') && (x <= '9'))
246
247 #define DEVFS_DEFAULT_MODE      ((VREAD|VWRITE|VEXEC) | ((VREAD|VEXEC)>>3) | ((VREAD|VEXEC)>>6)); /* -rwxr-xr-x */
248 #define DEVFS_DEFAULT_UID       0       /* root */
249 #define DEVFS_DEFAULT_GID       5       /* operator */
250
251 //#define DEVFS_DEFAULT_FLAGS 0
252
253 /*
254  * debug levels
255  */
256 #define DEVFS_DEBUG_SHOW                0x00
257 #define DEVFS_DEBUG_WARNING             0x01
258 #define DEVFS_DEBUG_INFO                0x02
259 #define DEVFS_DEBUG_DEBUG               0x03
260
261 /*
262  * Message ids
263  */
264 #define DEVFS_TERMINATE_CORE    0x01
265 #define DEVFS_DEVICE_CREATE             0x02
266 #define DEVFS_DEVICE_DESTROY    0x03
267 #define DEVFS_MOUNT_ADD                 0x04
268 #define DEVFS_MOUNT_DEL                 0x05
269 #define DEVFS_CREATE_ALL_DEV    0x06
270 #define DEVFS_DESTROY_SUBNAMES  0x07
271 #define DEVFS_DESTROY_DEV_BY_OPS 0x08
272 #define DEVFS_CHANDLER_ADD              0x09
273 #define DEVFS_CHANDLER_DEL              0x0A
274 #define DEVFS_FIND_DEVICE_BY_UDEV 0x0B
275 #define DEVFS_FIND_DEVICE_BY_NAME 0x0C
276 #define DEVFS_MAKE_ALIAS                0x0D
277 #define DEVFS_APPLY_RULES               0x0F
278 #define DEVFS_RESET_RULES               0x10
279 #define DEVFS_SCAN_CALLBACK             0x11
280 #define DEVFS_SYNC                              0x99
281
282 /*
283  * Node flags
284  */
285 #define DEVFS_NODE_LINKED               0x01    /* Node is linked into topology */
286 #define DEVFS_USER_CREATED              0x02    /* Node was user-created */
287 #define DEVFS_NO_TRACE                  0x04    /* Don't trace orphanage */
288 #define DEVFS_CLONED                    0x08    /* Node was created by the clone code */
289 #define DEVFS_HIDDEN                    0x10    /* Makes node inaccessible, apart from already allocated vnodes*/
290 #define DEVFS_INVISIBLE                 0x20    /* Makes node invisible in a readdir() */
291 #define DEVFS_PTY                               0x40    /* Node is linked to a PTY device */
292 //#define DEVFS_LINK                            0x20
293
294
295 /*
296  * Clone helper stuff
297  */
298 #define DEVFS_UNIT_HSIZE        64      /* power of 2 */
299 #define DEVFS_UNIT_HMASK        (DEVFS_UNIT_HSIZE - 1)
300 #define DEVFS_CLONE_HASHLIST(name)      devfs_ ## name ## _clone_hashlist
301 #define DEVFS_DECLARE_CLONE_HASHLIST(name)      struct devfs_unit_hash* DEVFS_CLONE_HASHLIST(name) [DEVFS_UNIT_HSIZE]
302
303
304 #define DEVFS_BITMAP_INITIAL_SIZE 1
305 #define DEVFS_CLONE_BITMAP(name)        devfs_ ## name ## _clone_bitmap
306 #define DEVFS_DECLARE_CLONE_BITMAP(name)        struct devfs_bitmap DEVFS_CLONE_BITMAP(name)
307 #define devfs_clone_bitmap_put  devfs_clone_bitmap_rst
308
309 struct devfs_bitmap {
310         int     chunks;
311         unsigned long *bitmap;
312 };
313
314 struct devfs_unit_hash {
315         struct devfs_unit_hash  *next;
316         int     unit_no;
317
318                 cdev_t  dev;
319 };
320
321 struct devfs_clone_helper {
322         DEVFS_DECLARE_CLONE_HASHLIST(generic);
323         DEVFS_DECLARE_CLONE_BITMAP(generic);
324 };
325
326 #define DEVFS_CLONE_HELPER(name)        devfs_ ## name ## _clone_helper
327 #define DEVFS_DECLARE_CLONE_HELPER(name)        static struct devfs_clone_helper DEVFS_CLONE_HELPER(name)
328
329
330 void devfs_clone_bitmap_init(struct devfs_bitmap *);
331 void devfs_clone_bitmap_uninit(struct devfs_bitmap *);
332 void devfs_clone_bitmap_resize(struct devfs_bitmap *, int);
333 int devfs_clone_bitmap_fff(struct devfs_bitmap *);
334 void devfs_clone_bitmap_set(struct devfs_bitmap *, int);
335 void devfs_clone_bitmap_rst(struct devfs_bitmap *, int);
336 int devfs_clone_bitmap_get(struct devfs_bitmap *, int);
337 int devfs_clone_bitmap_chk(struct devfs_bitmap *, int);
338
339 void devfs_clone_helper_init(struct devfs_clone_helper *);
340 void devfs_clone_helper_uninit(struct devfs_clone_helper *);
341 int devfs_clone_helper_insert(struct devfs_clone_helper *, cdev_t);
342 int devfs_clone_helper_remove(struct devfs_clone_helper *, int);
343
344
345 /*
346  * Prototypes
347  */
348 int devfs_debug(int level, char *fmt, ...);
349 int devfs_allocv(struct vnode **, struct devfs_node *);
350 struct devfs_node *devfs_allocp(devfs_nodetype, char *, struct devfs_node *, struct mount *, cdev_t);
351 int devfs_allocvp(struct mount *, struct vnode **, devfs_nodetype, char *, struct devfs_node *, cdev_t);
352
353 int devfs_freep(struct devfs_node *);
354 int devfs_reaperp(struct devfs_node *);
355
356 int devfs_unlinkp(struct devfs_node *);
357
358 void devfs_tracer_add_orphan(struct devfs_node *);
359 void devfs_tracer_del_orphan(struct devfs_node *);
360 size_t devfs_tracer_orphan_count(struct mount *, int);
361
362 int devfs_set_perms(struct devfs_node *, uid_t, gid_t, u_short, u_long);
363 int devfs_gc(struct devfs_node *);
364
365 int devfs_create_dev(cdev_t, uid_t, gid_t, int);
366 int devfs_destroy_dev(cdev_t);
367
368 devfs_msg_t devfs_msg_send_sync(uint32_t, devfs_msg_t);
369 __uint32_t devfs_msg_send(uint32_t, devfs_msg_t);
370 __uint32_t devfs_msg_send_dev(uint32_t, cdev_t dev, uid_t, gid_t, int);
371 __uint32_t devfs_msg_send_mount(uint32_t, struct devfs_mnt_data *);
372 __uint32_t devfs_msg_send_ops(uint32_t, struct dev_ops *, int);
373 __uint32_t devfs_msg_send_chandler(uint32_t, char *, d_clone_t);
374 __uint32_t devfs_msg_send_generic(uint32_t, void *);
375 __uint32_t devfs_msg_send_name(uint32_t, char *);
376 __uint32_t devfs_msg_send_link(uint32_t, char *, char *, struct mount *);
377
378 devfs_msg_t devfs_msg_get(void);
379 int devfs_msg_put(devfs_msg_t);
380
381 int devfs_mount_add(struct devfs_mnt_data *);
382 int devfs_mount_del(struct devfs_mnt_data *);
383
384 int devfs_create_all_dev(struct devfs_node *);
385
386 struct devfs_node *devfs_resolve_or_create_path(struct devfs_node *, char *, int);
387 int devfs_resolve_name_path(char *, char *, char **, char **);
388 struct devfs_node *devfs_create_device_node(struct devfs_node *, cdev_t, char *, char *, ...);
389
390 int devfs_destroy_device_node(struct devfs_node *, cdev_t);
391 int devfs_destroy_subnames(char *);
392 int devfs_destroy_dev_by_ops(struct dev_ops *, int);
393 struct devfs_node *devfs_find_device_node(struct devfs_node *, cdev_t);
394 struct devfs_node *devfs_find_device_node_by_name(struct devfs_node *, char *);
395
396 cdev_t devfs_new_cdev(struct dev_ops *, int);
397 int devfs_destroy_cdev(cdev_t);
398
399 cdev_t devfs_find_device_by_name(const char *, ...);
400 cdev_t devfs_find_device_by_udev(udev_t);
401
402 int devfs_clone_handler_add(char *, d_clone_t *);
403 int devfs_clone_handler_del(char *);
404 int devfs_clone(char *, size_t *, cdev_t *, int, struct ucred *);
405
406 int devfs_link_dev(cdev_t);
407 int devfs_unlink_dev(cdev_t);
408
409 int devfs_make_alias(char *, cdev_t);
410
411 int devfs_alias_create(char *name_orig, struct devfs_node *target);
412
413 int devfs_apply_rules(char *);
414 int devfs_reset_rules(char *);
415
416 int devfs_scan_callback(devfs_scan_t *);
417 int devfs_node_to_path(struct devfs_node *, char *);
418
419 void devfs_config(void *);
420 #endif /* _VFS_DEVFS_H_ */