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