Whamcloud - gitweb
Mass conversion of all copyright messages to Oracle.
[fs/lustre-release.git] / libcfs / libcfs / darwin / darwin-module.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <mach/mach_types.h>
38 #include <string.h>
39 #include <sys/file.h>
40 #include <sys/conf.h>
41 #include <miscfs/devfs/devfs.h>
42
43 #define DEBUG_SUBSYSTEM S_LNET
44 #include <libcfs/libcfs.h>
45
46 int libcfs_ioctl_getdata(char *buf, char *end, void *arg)
47 {
48         struct libcfs_ioctl_hdr *hdr;
49         struct libcfs_ioctl_data *data;
50         int err = 0;
51         ENTRY;
52
53         hdr = (struct libcfs_ioctl_hdr *)buf;
54         data = (struct libcfs_ioctl_data *)buf;
55         /* libcfs_ioctl_data has been copied in by ioctl of osx */
56         memcpy(buf, arg, sizeof(struct libcfs_ioctl_data));
57
58         if (hdr->ioc_version != LIBCFS_IOCTL_VERSION) {
59                 CERROR("LIBCFS: version mismatch kernel vs application\n");
60                 RETURN(-EINVAL);
61         }
62
63         if (hdr->ioc_len + buf >= end) {
64                 CERROR("LIBCFS: user buffer exceeds kernel buffer\n");
65                 RETURN(-EINVAL);
66         }
67
68         if (hdr->ioc_len < sizeof(struct libcfs_ioctl_data)) {
69                 CERROR("LIBCFS: user buffer too small for ioctl\n");
70                 RETURN(-EINVAL);
71         }
72         buf += size_round(sizeof(*data));
73
74         if (data->ioc_inllen1) {
75                 err = copy_from_user(buf, data->ioc_inlbuf1, size_round(data->ioc_inllen1));
76                 if (err)
77                         RETURN(err);
78                 data->ioc_inlbuf1 = buf;
79                 buf += size_round(data->ioc_inllen1);
80         }
81
82         if (data->ioc_inllen2) {
83                 copy_from_user(buf, data->ioc_inlbuf2, size_round(data->ioc_inllen2));
84                 if (err)
85                         RETURN(err);
86                 data->ioc_inlbuf2 = buf;
87         }
88
89         RETURN(err);
90 }
91
92 int libcfs_ioctl_popdata(void *arg, void *data, int size)
93 {
94         /* 
95          * system call will copy out ioctl arg to user space
96          */
97         memcpy(arg, data, size);
98         return 0;
99 }
100
101 extern struct cfs_psdev_ops             libcfs_psdev_ops;
102 struct libcfs_device_userstate          *mdev_state[16];
103
104 static int
105 libcfs_psdev_open(dev_t dev, int flags, int devtype, struct proc *p)
106 {
107         struct  libcfs_device_userstate *mstat = NULL;
108         int     rc = 0;
109         int     devid;
110         devid = minor(dev);
111
112         if (devid > 16) return (ENXIO);
113
114         if (libcfs_psdev_ops.p_open != NULL)
115                 rc = -libcfs_psdev_ops.p_open(0, &mstat);
116         else
117                 rc = EPERM;
118         if (rc == 0)
119                 mdev_state[devid] = mstat;
120         return rc;
121 }
122
123 static int
124 libcfs_psdev_close(dev_t dev, int flags, int mode, struct proc *p)
125 {
126         int     devid;
127         devid = minor(dev);
128         int     rc = 0;
129
130         if (devid > 16) return (ENXIO);
131
132         if (libcfs_psdev_ops.p_close != NULL)
133                 rc = -libcfs_psdev_ops.p_close(0, mdev_state[devid]);
134         else
135                 rc = EPERM;
136         if (rc == 0)
137                 mdev_state[devid] = NULL;
138         return rc;
139 }
140
141 static int
142 libcfs_ioctl (dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
143 {
144         int rc = 0;
145         struct cfs_psdev_file    pfile;
146         int     devid;
147         devid = minor(dev);
148         
149         if (devid > 16) return (ENXIO);
150
151         if (!is_suser())
152                 return (EPERM);
153         
154         pfile.off = 0;
155         pfile.private_data = mdev_state[devid];
156
157         if (libcfs_psdev_ops.p_ioctl != NULL)
158                 rc = -libcfs_psdev_ops.p_ioctl(&pfile, cmd, (void *)arg);
159         else
160                 rc = EPERM;
161         return rc;
162 }
163
164 static struct cdevsw libcfs_devsw =
165 {
166         .d_open     = libcfs_psdev_open,
167         .d_close    = libcfs_psdev_close,
168         .d_read     = eno_rdwrt,
169         .d_write    = eno_rdwrt,
170         .d_ioctl    = libcfs_ioctl,
171         .d_stop     = eno_stop,
172         .d_reset    = eno_reset,
173         .d_ttys     = NULL,
174         .d_select   = eno_select,
175         .d_mmap     = eno_mmap,
176         .d_strategy = eno_strat,
177         .d_getc     = eno_getc,
178         .d_putc     = eno_putc,
179         .d_type     = 0
180 };
181
182 cfs_psdev_t libcfs_dev = {
183         -1,
184         NULL,
185         "lnet",
186         &libcfs_devsw,
187         NULL
188 };
189
190 extern spinlock_t trace_cpu_serializer;
191 extern void cfs_sync_init(void);
192 extern void cfs_sync_fini(void);
193 extern int cfs_sysctl_init(void);
194 extern void cfs_sysctl_fini(void);
195 extern int cfs_mem_init(void);
196 extern int cfs_mem_fini(void);
197 extern void raw_page_death_row_clean(void);
198 extern void cfs_thread_agent_init(void);
199 extern void cfs_thread_agent_fini(void);
200 extern void cfs_symbol_init(void);
201 extern void cfs_symbol_fini(void);
202
203 int libcfs_arch_init(void)
204 {
205         cfs_sync_init();
206         cfs_sysctl_init();
207         cfs_mem_init();
208         cfs_thread_agent_init();
209         cfs_symbol_init();
210
211         spin_lock_init(&trace_cpu_serializer);
212
213         return 0;
214 }
215
216 void libcfs_arch_cleanup(void)
217 {
218         spin_lock_done(&trace_cpu_serializer);
219
220         cfs_symbol_fini();
221         cfs_thread_agent_fini();
222         cfs_mem_fini();
223         cfs_sysctl_fini();
224         cfs_sync_fini();
225 }