DEVFS - move devfs headers to sys/sys; fix consumers
[dragonfly.git] / sys / sys / 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 _SYS_DEVFS_H_
35 #define _SYS_DEVFS_H_
36
37 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
38
39 #ifndef _SYS_QUEUE_H_
40 #include <sys/queue.h>
41 #endif
42 #ifndef _SYS_LOCK_H_
43 #include <sys/lock.h>
44 #endif
45 #ifndef _SYS_CONF_H_
46 #include <sys/conf.h>
47 #endif
48 #ifndef _SYS_MSGPORT_H_
49 #include <sys/msgport.h>
50 #endif
51 #ifndef _SYS_DIRENT_H_
52 #include <sys/dirent.h>
53 #endif
54 #ifndef _SYS_DEVICE_H_
55 #include <sys/device.h>
56 #endif
57 #ifndef _SYS_UCRED_H_
58 #include <sys/ucred.h>
59 #endif
60
61
62 typedef enum {
63         Proot,          /* the filesystem root */
64         Plink,
65         Preg,
66         Pdir,
67         Pdev
68 } devfs_nodetype;
69
70 struct devfs_dirent {
71         ino_t           d_ino;          /* file number of entry */
72         uint16_t        d_namlen;       /* strlen(d_name) */
73         uint8_t         d_type;         /* file type */
74         char            *d_name;
75 };
76
77 struct devfs_fid {
78         uint16_t        fid_len;        /* Length of structure. */
79         uint16_t        fid_pad;        /* Force 32-bit alignment. */
80         uint32_t        fid_gen;
81         ino_t           fid_ino;        /* File number (ino). */
82 };
83
84 struct devfs_node {
85         cdev_t          d_dev;          /* device assoicated with this node */
86
87         struct mount    *mp;            /* mount point of this node */
88         struct devfs_dirent d_dir;      /* dirent data (name, inode, ...) */
89         struct vnode    *v_node;        /* assoicated vnode */
90         struct devfs_node *parent;      /* parent of this node */
91         devfs_nodetype  node_type;      /* devfs node type */
92
93         u_int64_t       refs;           /* number of open references */
94         size_t          nchildren;      /* number of children of a parent */
95         u_int64_t       cookie_jar;     /* cookie pool for children */
96         u_int64_t       cookie;         /* directory entry cookie for readdir */
97
98         struct devfs_node *link_target; /* target of this autolink-type node */
99         size_t          nlinks;         /* hard links */
100
101         char            *symlink_name;  /* symlink name for readlink */
102         size_t          symlink_namelen; /* symlink name length for readlink */
103
104         u_short         mode;           /* files access mode and type */
105         uid_t           uid;            /* owner user id */
106         gid_t           gid;            /* owner group id */
107         u_long          flags;
108
109         struct timespec atime;          /* time of last access */
110         struct timespec mtime;          /* time of last modification */
111         struct timespec ctime;          /* time file changed */
112
113         TAILQ_ENTRY(devfs_node) link;
114         TAILQ_HEAD(, devfs_node) list;  /* linked list of children */
115 };
116
117 struct devfs_orphan {
118         struct devfs_node *node;
119         TAILQ_ENTRY(devfs_orphan) link;
120 };
121
122 struct devfs_mnt_data {
123         TAILQ_HEAD(, devfs_orphan) orphan_list;
124         TAILQ_ENTRY(devfs_mnt_data) link;
125
126         struct devfs_node *root_node;
127         struct mount    *mp;
128         uint32_t        mnt_type;
129         long            leak_count;
130         int             jailed;
131         size_t          mntonnamelen;
132 };
133
134 struct devfs_clone_handler {
135         char            *name;
136         u_char          namlen;
137         d_clone_t       *nhandler;
138         TAILQ_ENTRY(devfs_clone_handler) link;
139 };
140
141
142 struct devfs_alias {
143         char            *name;
144         size_t          namlen;
145         cdev_t          dev_target;
146         TAILQ_ENTRY(devfs_alias) link;
147 };
148
149
150 typedef struct devfs_msg {
151         struct lwkt_msg hdr;
152
153         union {
154                 struct {
155                         cdev_t  dev;
156                         uid_t   uid;
157                         gid_t   gid;
158                         int     perms;
159                 } __m_dev;
160                 struct {
161                         struct devfs_mnt_data *mnt;
162                 } __m_mnt;
163                 struct {
164                         const char      *name;
165                         d_clone_t       *nhandler;
166                 } __m_chandler;
167                 struct {
168                         void *load;
169                 } __m_gen;
170                 struct {
171                         void *resp;
172                 } __m_resp;
173                 struct {
174                         udev_t  udev;
175                 } __m_udev;
176                 struct {
177                         cdev_t  cdev;
178                 } __m_cdev;
179                 struct {
180                         char    *name;
181                 } __m_name;
182                 struct {
183                         char    *basename;
184                         u_char  unit;
185                         struct vnode *vp;
186                 } __m_clone;
187                 struct {
188                         struct devfs_node *node;
189                 } __m_node;
190                 struct {
191                         char    *name;
192                         char    *target;
193                         struct mount *mp;
194                 } __m_link;
195                 struct {
196                         struct dev_ops *ops;
197                         int     minor;
198                 } __m_ops;
199                 struct {
200                         char    *name;
201                         uint32_t        flag;
202                 } __m_flags;
203                 struct {
204                         ino_t   ino;
205                         struct vnode *vp;
206                         struct mount *mp;
207                 } __m_ino;
208         } __m_u;
209 } *devfs_msg_t;
210
211 #define mdv_chandler    __m_u.__m_chandler
212 #define mdv_mnt         __m_u.__m_mnt.mnt
213 #define mdv_load        __m_u.__m_gen.load
214 #define mdv_response    __m_u.__m_resp.resp
215 #define mdv_dev         __m_u.__m_dev
216 #define mdv_link        __m_u.__m_link
217 #define mdv_udev        __m_u.__m_udev.udev
218 #define mdv_cdev        __m_u.__m_cdev.cdev
219 #define mdv_name        __m_u.__m_name.name
220 #define mdv_clone       __m_u.__m_clone
221 #define mdv_node        __m_u.__m_node.node
222 #define mdv_ops         __m_u.__m_ops
223 #define mdv_flags       __m_u.__m_flags
224 #define mdv_ino         __m_u.__m_ino
225
226
227 typedef struct devfs_core_args {
228         thread_t     td;
229 } *devfs_core_args_t;
230
231 TAILQ_HEAD(devfs_node_head, devfs_node);
232 TAILQ_HEAD(devfs_dev_head, cdev);
233 TAILQ_HEAD(devfs_mnt_head, devfs_mnt_data);
234 TAILQ_HEAD(devfs_chandler_head, devfs_clone_handler);
235 TAILQ_HEAD(devfs_alias_head, devfs_alias);
236
237 typedef void (devfs_scan_t)(cdev_t);
238 typedef void* (devfs_iterate_callback_t)(struct devfs_node *, void *);
239
240 #define DEVFS_NODE(x)           ((struct devfs_node *)((x)->v_data))
241 #define DEVFS_MNTDATA(x)        ((struct devfs_mnt_data *)((x)->mnt_data))
242 #define DEVFS_ORPHANLIST(x)     (&(DEVFS_MNTDATA(x)->orphan_list))
243 #define DEVFS_DENODE_HEAD(x)    (&((x)->list))
244 #define DEVFS_ISDIGIT(x)        ((x >= '0') && (x <= '9'))
245
246 /*
247  * -rwxr-xr-x
248  */
249 #define DEVFS_DEFAULT_MODE      ((VREAD|VWRITE|VEXEC) | ((VREAD|VEXEC)>>3) | \
250                                  ((VREAD|VEXEC)>>6));
251
252 #define DEVFS_DEFAULT_UID       0       /* root */
253 #define DEVFS_DEFAULT_GID       5       /* operator */
254
255 /*
256  * debug levels
257  */
258 #define DEVFS_DEBUG_SHOW        0x00
259 #define DEVFS_DEBUG_WARNING     0x01
260 #define DEVFS_DEBUG_INFO        0x02
261 #define DEVFS_DEBUG_DEBUG       0x03
262
263 /*
264  * Message ids
265  */
266 #define DEVFS_TERMINATE_CORE            0x01
267 #define DEVFS_DEVICE_CREATE             0x02
268 #define DEVFS_DEVICE_DESTROY            0x03
269 #define DEVFS_MOUNT_ADD                 0x04
270 #define DEVFS_MOUNT_DEL                 0x05
271 #define DEVFS_CREATE_ALL_DEV            0x06
272 #define DEVFS_DESTROY_SUBNAMES          0x07
273 #define DEVFS_DESTROY_DEV_BY_OPS        0x08
274 #define DEVFS_CHANDLER_ADD              0x09
275 #define DEVFS_CHANDLER_DEL              0x0A
276 #define DEVFS_FIND_DEVICE_BY_UDEV       0x0B
277 #define DEVFS_FIND_DEVICE_BY_NAME       0x0C
278 #define DEVFS_MAKE_ALIAS                0x0D
279 #define DEVFS_APPLY_RULES               0x0F
280 #define DEVFS_RESET_RULES               0x10
281 #define DEVFS_SCAN_CALLBACK             0x11
282 #define DEVFS_CLR_SUBNAMES_FLAG         0x12
283 #define DEVFS_DESTROY_SUBNAMES_WO_FLAG  0x13
284 #define DEVFS_INODE_TO_VNODE            0x14
285 #define DEVFS_SYNC                      0x99
286
287 /*
288  * Node flags
289  *
290  * HIDDEN       Makes node inaccessible, apart from already allocated vnodes
291  * INVISIBLE    Makes node invisible in a readdir()
292  */
293 #define DEVFS_NODE_LINKED               0x001   /* Linked into topology */
294 #define DEVFS_USER_CREATED              0x002   /* Node was user-created */
295 #define DEVFS_ORPHANED                  0x004   /* on orphan list */
296 #define DEVFS_CLONED                    0x008   /* Created by cloning code */
297 #define DEVFS_HIDDEN                    0x010   /* Makes node inaccessible */
298 #define DEVFS_INVISIBLE                 0x020   /* Makes node invisible */
299 #define DEVFS_PTY                       0x040   /* PTY device */
300 #define DEVFS_DESTROYED                 0x080   /* Sanity check */
301 #define DEVFS_RULE_CREATED              0x100   /* Node was rule-created */
302
303
304 /*
305  * Clone helper stuff
306  */
307 #define DEVFS_BITMAP_INITIAL_SIZE       1
308 #define DEVFS_CLONE_BITMAP(name)        devfs_ ## name ## _clone_bitmap
309 #define DEVFS_DECLARE_CLONE_BITMAP(name) \
310                                 struct devfs_bitmap DEVFS_CLONE_BITMAP(name)
311 #define devfs_clone_bitmap_put          devfs_clone_bitmap_rst
312
313 struct devfs_bitmap {
314         int             chunks;
315         unsigned long   *bitmap;
316 };
317
318 struct devfs_unit_hash {
319         struct devfs_unit_hash *next;
320         int             unit_no;
321         cdev_t          dev;
322 };
323
324 void devfs_clone_bitmap_init(struct devfs_bitmap *);
325 void devfs_clone_bitmap_uninit(struct devfs_bitmap *);
326 void devfs_clone_bitmap_resize(struct devfs_bitmap *, int);
327 int devfs_clone_bitmap_fff(struct devfs_bitmap *);
328 void devfs_clone_bitmap_set(struct devfs_bitmap *, int);
329 void devfs_clone_bitmap_rst(struct devfs_bitmap *, int);
330 int devfs_clone_bitmap_get(struct devfs_bitmap *, int);
331 int devfs_clone_bitmap_chk(struct devfs_bitmap *, int);
332
333 /*
334  * Prototypes
335  */
336 int devfs_debug(int level, char *fmt, ...);
337 int devfs_allocv(struct vnode **, struct devfs_node *);
338 struct devfs_node *devfs_allocp(devfs_nodetype, char *, struct devfs_node *,
339                                 struct mount *, cdev_t);
340 int devfs_allocvp(struct mount *, struct vnode **, devfs_nodetype, char *,
341                                 struct devfs_node *, cdev_t);
342
343 int devfs_freep(struct devfs_node *);
344
345 int devfs_unlinkp(struct devfs_node *);
346
347 void devfs_tracer_add_orphan(struct devfs_node *);
348 void devfs_tracer_del_orphan(struct devfs_node *);
349 size_t devfs_tracer_orphan_count(struct mount *, int);
350
351 int devfs_set_perms(struct devfs_node *, uid_t, gid_t, u_short, u_long);
352 int devfs_gc(struct devfs_node *);
353
354 int devfs_create_dev(cdev_t, uid_t, gid_t, int);
355 int devfs_destroy_dev(cdev_t);
356
357 devfs_msg_t devfs_msg_send_sync(uint32_t, devfs_msg_t);
358 void devfs_msg_send(uint32_t, devfs_msg_t);
359 void devfs_msg_send_dev(uint32_t, cdev_t dev, uid_t, gid_t, int);
360 void devfs_msg_send_mount(uint32_t, struct devfs_mnt_data *);
361 void devfs_msg_send_ops(uint32_t, struct dev_ops *, int);
362 void devfs_msg_send_chandler(uint32_t, char *, d_clone_t);
363 void devfs_msg_send_generic(uint32_t, void *);
364 void devfs_msg_send_name(uint32_t, char *);
365 void devfs_msg_send_link(uint32_t, char *, char *, struct mount *);
366
367 devfs_msg_t devfs_msg_get(void);
368 int devfs_msg_put(devfs_msg_t);
369
370 int devfs_mount_add(struct devfs_mnt_data *);
371 int devfs_mount_del(struct devfs_mnt_data *);
372
373 int devfs_create_all_dev(struct devfs_node *);
374
375 struct devfs_node *devfs_resolve_or_create_path(
376                                 struct devfs_node *, char *, int);
377 int devfs_resolve_name_path(char *, char *, char **, char **);
378 struct devfs_node *devfs_create_device_node(struct devfs_node *, cdev_t,
379                                 char *, char *, ...);
380
381 int devfs_destroy_device_node(struct devfs_node *, cdev_t);
382 int devfs_destroy_subnames(char *);
383 int devfs_destroy_dev_by_ops(struct dev_ops *, int);
384 struct devfs_node *devfs_find_device_node_by_name(struct devfs_node *, char *);
385
386 cdev_t devfs_new_cdev(struct dev_ops *, int);
387
388 cdev_t devfs_find_device_by_name(const char *, ...);
389 cdev_t devfs_find_device_by_udev(udev_t);
390
391 struct vnode *devfs_inode_to_vnode(struct mount *, ino_t);
392
393 int devfs_clone_handler_add(const char *, d_clone_t *);
394 int devfs_clone_handler_del(const char *);
395 cdev_t devfs_clone(cdev_t, const char *, size_t, int, struct ucred *);
396
397 int devfs_link_dev(cdev_t);
398
399 int devfs_make_alias(const char *, cdev_t);
400
401 int devfs_alias_create(char *, struct devfs_node *, int);
402
403 int devfs_apply_rules(char *);
404 int devfs_reset_rules(char *);
405
406 int devfs_scan_callback(devfs_scan_t *);
407 int devfs_node_to_path(struct devfs_node *, char *);
408
409 int devfs_clr_subnames_flag(char *, uint32_t);
410 int devfs_destroy_subnames_without_flag(char *, uint32_t);
411 int devfs_node_is_accessible(struct devfs_node *);
412
413 int devfs_reference_ops(struct dev_ops *);
414 void devfs_release_ops(struct dev_ops *);
415
416 void devfs_config(void);
417
418 void *devfs_iterate_topology(struct devfs_node *node,
419                 devfs_iterate_callback_t *callback, void *arg1);
420
421 void *devfs_find_device_node_callback(struct devfs_node *, cdev_t);
422
423 #endif /* KERNEL */
424
425 #define DEVFS_MNT_RULESET       0x01
426 #define DEVFS_MNT_JAIL          0x02
427
428 struct devfs_mount_info {
429         int flags;
430 };
431
432 #endif /* _SYS_DEVFS_H_ */