Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sys / netproto / smb / smb_dev.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 2000-2001 Boris Popov
3 * 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. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: src/sys/netsmb/smb_dev.c,v 1.2.2.1 2001/05/22 08:32:33 bp Exp $
b13267a5 33 * $DragonFly: src/sys/netproto/smb/smb_dev.c,v 1.16 2006/09/10 01:26:40 dillon Exp $
984263bc
MD
34 */
35#include <sys/param.h>
36#include <sys/kernel.h>
37#include <sys/systm.h>
38#include <sys/conf.h>
fef8985e 39#include <sys/device.h>
984263bc
MD
40#include <sys/fcntl.h>
41#include <sys/ioccom.h>
42#include <sys/malloc.h>
43#include <sys/file.h> /* Must come after sys/malloc.h */
44#include <sys/poll.h>
45#include <sys/proc.h>
46#include <sys/select.h>
47#include <sys/socket.h>
48#include <sys/socketvar.h>
49#include <sys/sysctl.h>
50#include <sys/uio.h>
51#include <sys/vnode.h>
636ae7e8 52#include <sys/thread2.h>
984263bc
MD
53
54#include <net/if.h>
55
1f2de5d4
MD
56#include "smb.h"
57#include "smb_conn.h"
58#include "smb_subr.h"
59#include "smb_dev.h"
984263bc
MD
60
61#define SMB_GETDEV(dev) ((struct smb_dev*)(dev)->si_drv1)
62#define SMB_CHECKMINOR(dev) do { \
63 sdp = SMB_GETDEV(dev); \
64 if (sdp == NULL) return ENXIO; \
65 } while(0)
66
67static d_open_t nsmb_dev_open;
68static d_close_t nsmb_dev_close;
69static d_read_t nsmb_dev_read;
70static d_write_t nsmb_dev_write;
71static d_ioctl_t nsmb_dev_ioctl;
72static d_poll_t nsmb_dev_poll;
73
74MODULE_VERSION(netsmb, NSMB_VERSION);
75
76#define SI_NAMED 0
77
78static int smb_version = NSMB_VERSION;
79
80
81SYSCTL_DECL(_net_smb);
82SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
83
84static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
85
86
87/*
88int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
89*/
90
fef8985e
MD
91static struct dev_ops nsmb_ops = {
92 { NSMB_NAME, NSMB_MAJOR, 0 },
93 .d_open = nsmb_dev_open,
94 .d_close = nsmb_dev_close,
95 .d_read = nsmb_dev_read,
96 .d_write = nsmb_dev_write,
97 .d_ioctl = nsmb_dev_ioctl,
98 .d_poll = nsmb_dev_poll,
984263bc
MD
99};
100
101
102static int
fef8985e 103nsmb_dev_open(struct dev_open_args *ap)
984263bc 104{
b13267a5 105 cdev_t dev = ap->a_head.a_dev;
984263bc 106 struct smb_dev *sdp;
dadab5e9 107
984263bc
MD
108 sdp = SMB_GETDEV(dev);
109 if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
110 return EBUSY;
111 if (sdp == NULL) {
efda3bd0 112 sdp = kmalloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
984263bc
MD
113 dev->si_drv1 = (void*)sdp;
114 }
115 /*
e4c9c0c8
MD
116 * XXX: this is just crazy - make a device for an already passed
117 * device... someone should take care of it.
984263bc 118 */
e4c9c0c8 119 if ((dev->si_flags & SI_NAMED) == 0) {
fef8985e
MD
120 make_dev(&nsmb_ops, minor(dev),
121 ap->a_cred->cr_uid, ap->a_cred->cr_gid,
e4c9c0c8
MD
122 0700, NSMB_NAME"%d", lminor(dev));
123 }
984263bc
MD
124 bzero(sdp, sizeof(*sdp));
125/*
126 STAILQ_INIT(&sdp->sd_rqlist);
127 STAILQ_INIT(&sdp->sd_rplist);
128 bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
129*/
636ae7e8 130 crit_enter();
984263bc
MD
131 sdp->sd_level = -1;
132 sdp->sd_flags |= NSMBFL_OPEN;
636ae7e8 133 crit_exit();
984263bc
MD
134 return 0;
135}
136
137static int
fef8985e 138nsmb_dev_close(struct dev_close_args *ap)
984263bc 139{
b13267a5 140 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
141 struct smb_dev *sdp;
142 struct smb_vc *vcp;
143 struct smb_share *ssp;
144 struct smb_cred scred;
984263bc
MD
145
146 SMB_CHECKMINOR(dev);
636ae7e8 147 crit_enter();
984263bc 148 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
636ae7e8 149 crit_exit();
984263bc
MD
150 return EBADF;
151 }
fef8985e 152 smb_makescred(&scred, curthread, NULL);
984263bc
MD
153 ssp = sdp->sd_share;
154 if (ssp != NULL)
155 smb_share_rele(ssp, &scred);
156 vcp = sdp->sd_vc;
157 if (vcp != NULL)
158 smb_vc_rele(vcp, &scred);
159/*
160 smb_flushq(&sdp->sd_rqlist);
161 smb_flushq(&sdp->sd_rplist);
162*/
163 dev->si_drv1 = NULL;
efda3bd0 164 kfree(sdp, M_NSMBDEV);
636ae7e8 165 crit_exit();
984263bc
MD
166 return 0;
167}
168
169
170static int
fef8985e 171nsmb_dev_ioctl(struct dev_ioctl_args *ap)
984263bc 172{
b13267a5 173 cdev_t dev = ap->a_head.a_dev;
fef8985e 174 caddr_t data = ap->a_data;
984263bc
MD
175 struct smb_dev *sdp;
176 struct smb_vc *vcp;
177 struct smb_share *ssp;
178 struct smb_cred scred;
179 int error = 0;
180
181 SMB_CHECKMINOR(dev);
182 if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
183 return EBADF;
184
fef8985e
MD
185 smb_makescred(&scred, NULL, ap->a_cred);
186 switch (ap->a_cmd) {
984263bc
MD
187 case SMBIOC_OPENSESSION:
188 if (sdp->sd_vc)
189 return EISCONN;
190 error = smb_usr_opensession((struct smbioc_ossn*)data,
191 &scred, &vcp);
192 if (error)
193 break;
194 sdp->sd_vc = vcp;
fef8985e 195 smb_vc_unlock(vcp, 0);
984263bc
MD
196 sdp->sd_level = SMBL_VC;
197 break;
198 case SMBIOC_OPENSHARE:
199 if (sdp->sd_share)
200 return EISCONN;
201 if (sdp->sd_vc == NULL)
202 return ENOTCONN;
203 error = smb_usr_openshare(sdp->sd_vc,
204 (struct smbioc_oshare*)data, &scred, &ssp);
205 if (error)
206 break;
207 sdp->sd_share = ssp;
fef8985e 208 smb_share_unlock(ssp, 0);
984263bc
MD
209 sdp->sd_level = SMBL_SHARE;
210 break;
211 case SMBIOC_REQUEST:
212 if (sdp->sd_share == NULL)
213 return ENOTCONN;
214 error = smb_usr_simplerequest(sdp->sd_share,
215 (struct smbioc_rq*)data, &scred);
216 break;
217 case SMBIOC_T2RQ:
218 if (sdp->sd_share == NULL)
219 return ENOTCONN;
220 error = smb_usr_t2request(sdp->sd_share,
221 (struct smbioc_t2rq*)data, &scred);
222 break;
223 case SMBIOC_SETFLAGS: {
224 struct smbioc_flags *fl = (struct smbioc_flags*)data;
225 int on;
226
227 if (fl->ioc_level == SMBL_VC) {
228 if (fl->ioc_mask & SMBV_PERMANENT) {
229 on = fl->ioc_flags & SMBV_PERMANENT;
230 if ((vcp = sdp->sd_vc) == NULL)
231 return ENOTCONN;
232 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
233 if (error)
234 break;
235 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
236 vcp->obj.co_flags |= SMBV_PERMANENT;
fef8985e 237 smb_vc_ref(vcp);
984263bc
MD
238 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
239 vcp->obj.co_flags &= ~SMBV_PERMANENT;
240 smb_vc_rele(vcp, &scred);
241 }
242 smb_vc_put(vcp, &scred);
243 } else
244 error = EINVAL;
245 } else if (fl->ioc_level == SMBL_SHARE) {
246 if (fl->ioc_mask & SMBS_PERMANENT) {
247 on = fl->ioc_flags & SMBS_PERMANENT;
248 if ((ssp = sdp->sd_share) == NULL)
249 return ENOTCONN;
250 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
251 if (error)
252 break;
253 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
254 ssp->obj.co_flags |= SMBS_PERMANENT;
fef8985e 255 smb_share_ref(ssp);
984263bc
MD
256 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
257 ssp->obj.co_flags &= ~SMBS_PERMANENT;
258 smb_share_rele(ssp, &scred);
259 }
260 smb_share_put(ssp, &scred);
261 } else
262 error = EINVAL;
263 break;
264 } else
265 error = EINVAL;
266 break;
267 }
268 case SMBIOC_LOOKUP:
269 if (sdp->sd_vc || sdp->sd_share)
270 return EISCONN;
271 vcp = NULL;
272 ssp = NULL;
273 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
274 if (error)
275 break;
276 if (vcp) {
277 sdp->sd_vc = vcp;
fef8985e 278 smb_vc_unlock(vcp, 0);
984263bc
MD
279 sdp->sd_level = SMBL_VC;
280 }
281 if (ssp) {
282 sdp->sd_share = ssp;
fef8985e 283 smb_share_unlock(ssp, 0);
984263bc
MD
284 sdp->sd_level = SMBL_SHARE;
285 }
286 break;
287 case SMBIOC_READ: case SMBIOC_WRITE: {
288 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
289 struct uio auio;
290 struct iovec iov;
291
292 if ((ssp = sdp->sd_share) == NULL)
293 return ENOTCONN;
294 iov.iov_base = rwrq->ioc_base;
295 iov.iov_len = rwrq->ioc_cnt;
296 auio.uio_iov = &iov;
297 auio.uio_iovcnt = 1;
298 auio.uio_offset = rwrq->ioc_offset;
299 auio.uio_resid = rwrq->ioc_cnt;
300 auio.uio_segflg = UIO_USERSPACE;
fef8985e
MD
301 auio.uio_rw = (ap->a_cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
302 auio.uio_td = curthread;
303 if (ap->a_cmd == SMBIOC_READ)
984263bc
MD
304 error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
305 else
306 error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
307 rwrq->ioc_cnt -= auio.uio_resid;
308 break;
309 }
310 default:
311 error = ENODEV;
312 }
313 return error;
314}
315
316static int
fef8985e 317nsmb_dev_read(struct dev_read_args *ap)
984263bc
MD
318{
319 return EACCES;
320}
321
322static int
fef8985e 323nsmb_dev_write(struct dev_write_args *ap)
984263bc
MD
324{
325 return EACCES;
326}
327
328static int
fef8985e 329nsmb_dev_poll(struct dev_poll_args *ap)
984263bc
MD
330{
331 return ENODEV;
332}
333
334static int
335nsmb_dev_load(module_t mod, int cmd, void *arg)
336{
337 int error = 0;
338
339 switch (cmd) {
340 case MOD_LOAD:
984263bc
MD
341 error = smb_sm_init();
342 if (error)
343 break;
344 error = smb_iod_init();
345 if (error) {
346 smb_sm_done();
347 break;
348 }
fef8985e 349 dev_ops_add(&nsmb_ops, 0, 0);
984263bc
MD
350 printf("netsmb_dev: loaded\n");
351 break;
352 case MOD_UNLOAD:
353 smb_iod_done();
354 error = smb_sm_done();
355 error = 0;
fef8985e 356 dev_ops_remove(&nsmb_ops, 0, 0);
984263bc
MD
357 printf("netsmb_dev: unloaded\n");
358 break;
359 default:
360 error = EINVAL;
361 break;
362 }
363 return error;
364}
365
366DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
367
984263bc
MD
368int
369smb_dev2share(int fd, int mode, struct smb_cred *scred,
370 struct smb_share **sspp)
371{
372 struct file *fp;
373 struct vnode *vp;
374 struct smb_dev *sdp;
375 struct smb_share *ssp;
b13267a5 376 cdev_t dev;
984263bc
MD
377 int error;
378
dadab5e9
MD
379 KKASSERT(scred->scr_td->td_proc);
380
228b401d
MD
381 fp = holdfp(scred->scr_td->td_proc->p_fd, fd, FREAD|FWRITE);
382 if (fp == NULL)
984263bc 383 return EBADF;
228b401d 384
984263bc 385 vp = (struct vnode*)fp->f_data;
228b401d
MD
386 if (vp == NULL) {
387 error = EBADF;
388 goto done;
389 }
984263bc 390 dev = vn_todev(vp);
5711af4f 391 if (dev == NOCDEV) {
228b401d
MD
392 error = EBADF;
393 goto done;
394 }
984263bc
MD
395 SMB_CHECKMINOR(dev);
396 ssp = sdp->sd_share;
228b401d
MD
397 if (ssp == NULL) {
398 error = ENOTCONN;
399 goto done;
400 }
984263bc 401 error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
228b401d
MD
402 if (error == 0)
403 *sspp = ssp;
404done:
405 fdrop(fp);
406 return (error);
984263bc
MD
407}
408