proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / netproto / smb / smb_dev.c
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 $
33  * $DragonFly: src/sys/netproto/smb/smb_dev.c,v 1.4 2003/06/25 03:56:06 dillon Exp $
34  */
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/conf.h>
39 #include <sys/fcntl.h>
40 #include <sys/ioccom.h>
41 #include <sys/malloc.h>
42 #include <sys/file.h>           /* Must come after sys/malloc.h */
43 #include <sys/poll.h>
44 #include <sys/proc.h>
45 #include <sys/select.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/uio.h>
50 #include <sys/vnode.h>
51
52 #include <net/if.h>
53
54 #include <netsmb/smb.h>
55 #include <netsmb/smb_conn.h>
56 #include <netsmb/smb_subr.h>
57 #include <netsmb/smb_dev.h>
58
59 #define SMB_GETDEV(dev)         ((struct smb_dev*)(dev)->si_drv1)
60 #define SMB_CHECKMINOR(dev)     do { \
61                                     sdp = SMB_GETDEV(dev); \
62                                     if (sdp == NULL) return ENXIO; \
63                                 } while(0)
64
65 static d_open_t  nsmb_dev_open;
66 static d_close_t nsmb_dev_close;
67 static d_read_t  nsmb_dev_read;
68 static d_write_t nsmb_dev_write;
69 static d_ioctl_t nsmb_dev_ioctl;
70 static d_poll_t  nsmb_dev_poll;
71
72 MODULE_VERSION(netsmb, NSMB_VERSION);
73
74 #define SI_NAMED        0
75
76 static int smb_version = NSMB_VERSION;
77
78
79 SYSCTL_DECL(_net_smb);
80 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
81
82 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
83
84
85 /*
86 int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
87 */
88
89 static struct cdevsw nsmb_cdevsw = {
90         /* open */      nsmb_dev_open,
91         /* close */     nsmb_dev_close,
92         /* read */      nsmb_dev_read,
93         /* write */     nsmb_dev_write,
94         /* ioctl */     nsmb_dev_ioctl,
95         /* poll */      nsmb_dev_poll,
96         /* mmap */      nommap,
97         /* strategy */  nostrategy,
98         /* name */      NSMB_NAME,
99         /* maj */       NSMB_MAJOR,
100         /* dump */      nodump,
101         /* psize */     nopsize,
102         /* flags */     0,
103         /* bmaj */      -1
104 };
105
106
107 static int
108 nsmb_dev_open(dev_t dev, int oflags, int devtype, d_thread_t *td)
109 {
110         struct proc *p = td->td_proc;
111         struct smb_dev *sdp;
112         struct ucred *cred;
113         int s;
114
115         KKASSERT(p != NULL);
116         cred = p->p_ucred;
117
118         sdp = SMB_GETDEV(dev);
119         if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
120                 return EBUSY;
121         if (sdp == NULL) {
122                 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
123                 dev->si_drv1 = (void*)sdp;
124         }
125         /*
126          * XXX: this is just crazy - make a device for an already passed device...
127          * someone should take care of it.
128          */
129         if ((dev->si_flags & SI_NAMED) == 0)
130                 make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700,
131                     NSMB_NAME"%d", lminor(dev));
132         bzero(sdp, sizeof(*sdp));
133 /*
134         STAILQ_INIT(&sdp->sd_rqlist);
135         STAILQ_INIT(&sdp->sd_rplist);
136         bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
137 */
138         s = splimp();
139         sdp->sd_level = -1;
140         sdp->sd_flags |= NSMBFL_OPEN;
141         splx(s);
142         return 0;
143 }
144
145 static int
146 nsmb_dev_close(dev_t dev, int flag, int fmt, d_thread_t *td)
147 {
148         struct smb_dev *sdp;
149         struct smb_vc *vcp;
150         struct smb_share *ssp;
151         struct smb_cred scred;
152         int s;
153
154         SMB_CHECKMINOR(dev);
155         s = splimp();
156         if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
157                 splx(s);
158                 return EBADF;
159         }
160         smb_makescred(&scred, td, NULL);
161         ssp = sdp->sd_share;
162         if (ssp != NULL)
163                 smb_share_rele(ssp, &scred);
164         vcp = sdp->sd_vc;
165         if (vcp != NULL)
166                 smb_vc_rele(vcp, &scred);
167 /*
168         smb_flushq(&sdp->sd_rqlist);
169         smb_flushq(&sdp->sd_rplist);
170 */
171         dev->si_drv1 = NULL;
172         free(sdp, M_NSMBDEV);
173         destroy_dev(dev);
174         splx(s);
175         return 0;
176 }
177
178
179 static int
180 nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
181 {
182         struct smb_dev *sdp;
183         struct smb_vc *vcp;
184         struct smb_share *ssp;
185         struct smb_cred scred;
186         int error = 0;
187
188         SMB_CHECKMINOR(dev);
189         if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
190                 return EBADF;
191
192         smb_makescred(&scred, td, NULL);
193         switch (cmd) {
194             case SMBIOC_OPENSESSION:
195                 if (sdp->sd_vc)
196                         return EISCONN;
197                 error = smb_usr_opensession((struct smbioc_ossn*)data,
198                     &scred, &vcp);
199                 if (error)
200                         break;
201                 sdp->sd_vc = vcp;
202                 smb_vc_unlock(vcp, 0, td);
203                 sdp->sd_level = SMBL_VC;
204                 break;
205             case SMBIOC_OPENSHARE:
206                 if (sdp->sd_share)
207                         return EISCONN;
208                 if (sdp->sd_vc == NULL)
209                         return ENOTCONN;
210                 error = smb_usr_openshare(sdp->sd_vc,
211                     (struct smbioc_oshare*)data, &scred, &ssp);
212                 if (error)
213                         break;
214                 sdp->sd_share = ssp;
215                 smb_share_unlock(ssp, 0, td);
216                 sdp->sd_level = SMBL_SHARE;
217                 break;
218             case SMBIOC_REQUEST:
219                 if (sdp->sd_share == NULL)
220                         return ENOTCONN;
221                 error = smb_usr_simplerequest(sdp->sd_share,
222                     (struct smbioc_rq*)data, &scred);
223                 break;
224             case SMBIOC_T2RQ:
225                 if (sdp->sd_share == NULL)
226                         return ENOTCONN;
227                 error = smb_usr_t2request(sdp->sd_share,
228                     (struct smbioc_t2rq*)data, &scred);
229                 break;
230             case SMBIOC_SETFLAGS: {
231                 struct smbioc_flags *fl = (struct smbioc_flags*)data;
232                 int on;
233         
234                 if (fl->ioc_level == SMBL_VC) {
235                         if (fl->ioc_mask & SMBV_PERMANENT) {
236                                 on = fl->ioc_flags & SMBV_PERMANENT;
237                                 if ((vcp = sdp->sd_vc) == NULL)
238                                         return ENOTCONN;
239                                 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
240                                 if (error)
241                                         break;
242                                 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
243                                         vcp->obj.co_flags |= SMBV_PERMANENT;
244                                         smb_vc_ref(vcp, td);
245                                 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
246                                         vcp->obj.co_flags &= ~SMBV_PERMANENT;
247                                         smb_vc_rele(vcp, &scred);
248                                 }
249                                 smb_vc_put(vcp, &scred);
250                         } else
251                                 error = EINVAL;
252                 } else if (fl->ioc_level == SMBL_SHARE) {
253                         if (fl->ioc_mask & SMBS_PERMANENT) {
254                                 on = fl->ioc_flags & SMBS_PERMANENT;
255                                 if ((ssp = sdp->sd_share) == NULL)
256                                         return ENOTCONN;
257                                 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
258                                 if (error)
259                                         break;
260                                 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
261                                         ssp->obj.co_flags |= SMBS_PERMANENT;
262                                         smb_share_ref(ssp, td);
263                                 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
264                                         ssp->obj.co_flags &= ~SMBS_PERMANENT;
265                                         smb_share_rele(ssp, &scred);
266                                 }
267                                 smb_share_put(ssp, &scred);
268                         } else
269                                 error = EINVAL;
270                         break;
271                 } else
272                         error = EINVAL;
273                 break;
274             }
275             case SMBIOC_LOOKUP:
276                 if (sdp->sd_vc || sdp->sd_share)
277                         return EISCONN;
278                 vcp = NULL;
279                 ssp = NULL;
280                 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
281                 if (error)
282                         break;
283                 if (vcp) {
284                         sdp->sd_vc = vcp;
285                         smb_vc_unlock(vcp, 0, td);
286                         sdp->sd_level = SMBL_VC;
287                 }
288                 if (ssp) {
289                         sdp->sd_share = ssp;
290                         smb_share_unlock(ssp, 0, td);
291                         sdp->sd_level = SMBL_SHARE;
292                 }
293                 break;
294             case SMBIOC_READ: case SMBIOC_WRITE: {
295                 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
296                 struct uio auio;
297                 struct iovec iov;
298         
299                 if ((ssp = sdp->sd_share) == NULL)
300                         return ENOTCONN;
301                 iov.iov_base = rwrq->ioc_base;
302                 iov.iov_len = rwrq->ioc_cnt;
303                 auio.uio_iov = &iov;
304                 auio.uio_iovcnt = 1;
305                 auio.uio_offset = rwrq->ioc_offset;
306                 auio.uio_resid = rwrq->ioc_cnt;
307                 auio.uio_segflg = UIO_USERSPACE;
308                 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
309                 auio.uio_td = td;
310                 if (cmd == SMBIOC_READ)
311                         error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
312                 else
313                         error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
314                 rwrq->ioc_cnt -= auio.uio_resid;
315                 break;
316             }
317             default:
318                 error = ENODEV;
319         }
320         return error;
321 }
322
323 static int
324 nsmb_dev_read(dev_t dev, struct uio *uio, int flag)
325 {
326         return EACCES;
327 }
328
329 static int
330 nsmb_dev_write(dev_t dev, struct uio *uio, int flag)
331 {
332         return EACCES;
333 }
334
335 static int
336 nsmb_dev_poll(dev_t dev, int events, d_thread_t *td)
337 {
338         return ENODEV;
339 }
340
341 static int
342 nsmb_dev_load(module_t mod, int cmd, void *arg)
343 {
344         int error = 0;
345
346         switch (cmd) {
347             case MOD_LOAD:
348                 error = smb_checksmp();
349                 if (error)
350                         break;
351                 error = smb_sm_init();
352                 if (error)
353                         break;
354                 error = smb_iod_init();
355                 if (error) {
356                         smb_sm_done();
357                         break;
358                 }
359                 cdevsw_add(&nsmb_cdevsw);
360                 printf("netsmb_dev: loaded\n");
361                 break;
362             case MOD_UNLOAD:
363                 smb_iod_done();
364                 error = smb_sm_done();
365                 error = 0;
366                 cdevsw_remove(&nsmb_cdevsw);
367                 printf("netsmb_dev: unloaded\n");
368                 break;
369             default:
370                 error = EINVAL;
371                 break;
372         }
373         return error;
374 }
375
376 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
377
378
379 /*
380  * Convert a file descriptor to appropriate smb_share pointer
381  */
382 static struct file*
383 nsmb_getfp(struct filedesc* fdp, int fd, int flag)
384 {
385         struct file* fp;
386
387         if (((u_int)fd) >= fdp->fd_nfiles ||
388             (fp = fdp->fd_ofiles[fd]) == NULL ||
389             (fp->f_flag & flag) == 0)
390                 return (NULL);
391         return (fp);
392 }
393
394 int
395 smb_dev2share(int fd, int mode, struct smb_cred *scred,
396         struct smb_share **sspp)
397 {
398         struct file *fp;
399         struct vnode *vp;
400         struct smb_dev *sdp;
401         struct smb_share *ssp;
402         dev_t dev;
403         int error;
404
405         KKASSERT(scred->scr_td->td_proc);
406
407         if ((fp = nsmb_getfp(scred->scr_td->td_proc->p_fd,
408             fd, FREAD | FWRITE)) == NULL) {
409                 return EBADF;
410         }
411         vp = (struct vnode*)fp->f_data;
412         if (vp == NULL)
413                 return EBADF;
414         dev = vn_todev(vp);
415         if (dev == NODEV)
416                 return EBADF;
417         SMB_CHECKMINOR(dev);
418         ssp = sdp->sd_share;
419         if (ssp == NULL)
420                 return ENOTCONN;
421         error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
422         if (error)
423                 return error;
424         *sspp = ssp;
425         return 0;
426 }
427