2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/ioccom.h>
38 #include <sys/malloc.h>
39 #include <sys/file.h> /* Must come after sys/malloc.h */
40 #include <sys/filedesc.h>
44 #include <sys/select.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
53 #include <netsmb/smb.h>
54 #include <netsmb/smb_conn.h>
55 #include <netsmb/smb_subr.h>
56 #include <netsmb/smb_dev.h>
58 #define SMB_GETDEV(dev) ((struct smb_dev*)(dev)->si_drv1)
59 #define SMB_CHECKMINOR(dev) do { \
60 sdp = SMB_GETDEV(dev); \
61 if (sdp == NULL) return ENXIO; \
64 static d_open_t nsmb_dev_open;
65 static d_close_t nsmb_dev_close;
66 static d_ioctl_t nsmb_dev_ioctl;
68 MODULE_DEPEND(netsmb, libiconv, 1, 1, 2);
69 MODULE_VERSION(netsmb, NSMB_VERSION);
71 static int smb_version = NSMB_VERSION;
74 SYSCTL_DECL(_net_smb);
75 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
77 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
81 int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
84 static struct cdevsw nsmb_cdevsw = {
85 .d_version = D_VERSION,
86 .d_flags = D_NEEDGIANT | D_NEEDMINOR,
87 .d_open = nsmb_dev_open,
88 .d_close = nsmb_dev_close,
89 .d_ioctl = nsmb_dev_ioctl,
93 static eventhandler_tag nsmb_dev_tag;
94 static struct clonedevs *nsmb_clones;
97 nsmb_dev_clone(void *arg, struct ucred *cred, char *name, int namelen,
105 if (strcmp(name, NSMB_NAME) == 0)
107 else if (dev_stdclone(name, NULL, NSMB_NAME, &u) != 1)
109 i = clone_create(&nsmb_clones, &nsmb_cdevsw, &u, dev, 0);
111 *dev = make_dev(&nsmb_cdevsw, u, UID_ROOT, GID_WHEEL, 0600,
112 "%s%d", NSMB_NAME, u);
115 (*dev)->si_flags |= SI_CHEAPCLONE;
121 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
124 struct ucred *cred = td->td_ucred;
127 sdp = SMB_GETDEV(dev);
128 if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
131 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
132 dev->si_drv1 = (void*)sdp;
135 * XXX: this is just crazy - make a device for an already passed device...
136 * someone should take care of it.
138 if ((dev->si_flags & SI_NAMED) == 0)
139 make_dev(&nsmb_cdevsw, dev2unit(dev), cred->cr_uid,
140 cred->cr_gid, 0700, NSMB_NAME"%d", dev2unit(dev));
141 bzero(sdp, sizeof(*sdp));
143 STAILQ_INIT(&sdp->sd_rqlist);
144 STAILQ_INIT(&sdp->sd_rplist);
145 bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
149 sdp->sd_flags |= NSMBFL_OPEN;
155 nsmb_dev_close(struct cdev *dev, int flag, int fmt, struct thread *td)
159 struct smb_share *ssp;
160 struct smb_cred *scred;
163 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
166 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
168 free(scred, M_NSMBDEV);
171 smb_makescred(scred, td, NULL);
174 smb_share_rele(ssp, scred);
177 smb_vc_rele(vcp, scred);
179 smb_flushq(&sdp->sd_rqlist);
180 smb_flushq(&sdp->sd_rplist);
183 free(sdp, M_NSMBDEV);
184 destroy_dev_sched(dev);
186 free(scred, M_NSMBDEV);
192 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
196 struct smb_share *ssp;
197 struct smb_cred *scred;
201 if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
204 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
205 smb_makescred(scred, td, NULL);
207 case SMBIOC_OPENSESSION:
212 error = smb_usr_opensession((struct smbioc_ossn*)data,
217 smb_vc_unlock(vcp, 0);
218 sdp->sd_level = SMBL_VC;
220 case SMBIOC_OPENSHARE:
225 if (sdp->sd_vc == NULL) {
229 error = smb_usr_openshare(sdp->sd_vc,
230 (struct smbioc_oshare*)data, scred, &ssp);
234 smb_share_unlock(ssp, 0);
235 sdp->sd_level = SMBL_SHARE;
238 if (sdp->sd_share == NULL) {
242 error = smb_usr_simplerequest(sdp->sd_share,
243 (struct smbioc_rq*)data, scred);
246 if (sdp->sd_share == NULL) {
250 error = smb_usr_t2request(sdp->sd_share,
251 (struct smbioc_t2rq*)data, scred);
253 case SMBIOC_SETFLAGS: {
254 struct smbioc_flags *fl = (struct smbioc_flags*)data;
257 if (fl->ioc_level == SMBL_VC) {
258 if (fl->ioc_mask & SMBV_PERMANENT) {
259 on = fl->ioc_flags & SMBV_PERMANENT;
260 if ((vcp = sdp->sd_vc) == NULL) {
264 error = smb_vc_get(vcp, LK_EXCLUSIVE, scred);
267 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
268 vcp->obj.co_flags |= SMBV_PERMANENT;
270 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
271 vcp->obj.co_flags &= ~SMBV_PERMANENT;
272 smb_vc_rele(vcp, scred);
274 smb_vc_put(vcp, scred);
277 } else if (fl->ioc_level == SMBL_SHARE) {
278 if (fl->ioc_mask & SMBS_PERMANENT) {
279 on = fl->ioc_flags & SMBS_PERMANENT;
280 if ((ssp = sdp->sd_share) == NULL) {
284 error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
287 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
288 ssp->obj.co_flags |= SMBS_PERMANENT;
290 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
291 ssp->obj.co_flags &= ~SMBS_PERMANENT;
292 smb_share_rele(ssp, scred);
294 smb_share_put(ssp, scred);
303 if (sdp->sd_vc || sdp->sd_share) {
309 error = smb_usr_lookup((struct smbioc_lookup*)data, scred, &vcp, &ssp);
314 smb_vc_unlock(vcp, 0);
315 sdp->sd_level = SMBL_VC;
319 smb_share_unlock(ssp, 0);
320 sdp->sd_level = SMBL_SHARE;
323 case SMBIOC_READ: case SMBIOC_WRITE: {
324 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
328 if ((ssp = sdp->sd_share) == NULL) {
332 iov.iov_base = rwrq->ioc_base;
333 iov.iov_len = rwrq->ioc_cnt;
336 auio.uio_offset = rwrq->ioc_offset;
337 auio.uio_resid = rwrq->ioc_cnt;
338 auio.uio_segflg = UIO_USERSPACE;
339 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
341 if (cmd == SMBIOC_READ)
342 error = smb_read(ssp, rwrq->ioc_fh, &auio, scred);
344 error = smb_write(ssp, rwrq->ioc_fh, &auio, scred);
345 rwrq->ioc_cnt -= auio.uio_resid;
352 free(scred, M_NSMBDEV);
357 nsmb_dev_load(module_t mod, int cmd, void *arg)
363 error = smb_sm_init();
366 error = smb_iod_init();
371 clone_setup(&nsmb_clones);
372 nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000);
376 error = smb_sm_done();
379 EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag);
380 drain_dev_clone_events();
381 clone_cleanup(&nsmb_clones);
382 destroy_dev_drain(&nsmb_cdevsw);
391 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
394 * Convert a file descriptor to appropriate smb_share pointer
397 nsmb_getfp(struct filedesc* fdp, int fd, int flag)
402 if (fd < 0 || fd >= fdp->fd_nfiles ||
403 (fp = fdp->fd_ofiles[fd]) == NULL ||
404 (fp->f_flag & flag) == 0) {
405 FILEDESC_SUNLOCK(fdp);
409 FILEDESC_SUNLOCK(fdp);
414 smb_dev2share(int fd, int mode, struct smb_cred *scred,
415 struct smb_share **sspp)
420 struct smb_share *ssp;
424 fp = nsmb_getfp(scred->scr_td->td_proc->p_fd, fd, FREAD | FWRITE);
429 fdrop(fp, curthread);
432 if (vp->v_type != VCHR) {
433 fdrop(fp, curthread);
440 fdrop(fp, curthread);
443 error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
446 fdrop(fp, curthread);