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.
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.
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
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.11 2005/06/22 01:33:31 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.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 */
45 #include <sys/select.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/vnode.h>
51 #include <sys/thread2.h>
60 #define SMB_GETDEV(dev) ((struct smb_dev*)(dev)->si_drv1)
61 #define SMB_CHECKMINOR(dev) do { \
62 sdp = SMB_GETDEV(dev); \
63 if (sdp == NULL) return ENXIO; \
66 static d_open_t nsmb_dev_open;
67 static d_close_t nsmb_dev_close;
68 static d_read_t nsmb_dev_read;
69 static d_write_t nsmb_dev_write;
70 static d_ioctl_t nsmb_dev_ioctl;
71 static d_poll_t nsmb_dev_poll;
73 MODULE_VERSION(netsmb, NSMB_VERSION);
77 static int smb_version = NSMB_VERSION;
80 SYSCTL_DECL(_net_smb);
81 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
83 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
87 int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
90 static struct cdevsw nsmb_cdevsw = {
97 /* open */ nsmb_dev_open,
98 /* close */ nsmb_dev_close,
99 /* read */ nsmb_dev_read,
100 /* write */ nsmb_dev_write,
101 /* ioctl */ nsmb_dev_ioctl,
102 /* poll */ nsmb_dev_poll,
104 /* strategy */ nostrategy,
111 nsmb_dev_open(dev_t dev, int oflags, int devtype, d_thread_t *td)
113 struct proc *p = td->td_proc;
120 sdp = SMB_GETDEV(dev);
121 if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
124 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
125 dev->si_drv1 = (void*)sdp;
128 * XXX: this is just crazy - make a device for an already passed
129 * device... someone should take care of it.
131 if ((dev->si_flags & SI_NAMED) == 0) {
132 make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid,
133 0700, NSMB_NAME"%d", lminor(dev));
135 bzero(sdp, sizeof(*sdp));
137 STAILQ_INIT(&sdp->sd_rqlist);
138 STAILQ_INIT(&sdp->sd_rplist);
139 bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
143 sdp->sd_flags |= NSMBFL_OPEN;
149 nsmb_dev_close(dev_t dev, int flag, int fmt, d_thread_t *td)
153 struct smb_share *ssp;
154 struct smb_cred scred;
158 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
162 smb_makescred(&scred, td, NULL);
165 smb_share_rele(ssp, &scred);
168 smb_vc_rele(vcp, &scred);
170 smb_flushq(&sdp->sd_rqlist);
171 smb_flushq(&sdp->sd_rplist);
174 free(sdp, M_NSMBDEV);
181 nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
185 struct smb_share *ssp;
186 struct smb_cred scred;
190 if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
193 smb_makescred(&scred, td, NULL);
195 case SMBIOC_OPENSESSION:
198 error = smb_usr_opensession((struct smbioc_ossn*)data,
203 smb_vc_unlock(vcp, 0, td);
204 sdp->sd_level = SMBL_VC;
206 case SMBIOC_OPENSHARE:
209 if (sdp->sd_vc == NULL)
211 error = smb_usr_openshare(sdp->sd_vc,
212 (struct smbioc_oshare*)data, &scred, &ssp);
216 smb_share_unlock(ssp, 0, td);
217 sdp->sd_level = SMBL_SHARE;
220 if (sdp->sd_share == NULL)
222 error = smb_usr_simplerequest(sdp->sd_share,
223 (struct smbioc_rq*)data, &scred);
226 if (sdp->sd_share == NULL)
228 error = smb_usr_t2request(sdp->sd_share,
229 (struct smbioc_t2rq*)data, &scred);
231 case SMBIOC_SETFLAGS: {
232 struct smbioc_flags *fl = (struct smbioc_flags*)data;
235 if (fl->ioc_level == SMBL_VC) {
236 if (fl->ioc_mask & SMBV_PERMANENT) {
237 on = fl->ioc_flags & SMBV_PERMANENT;
238 if ((vcp = sdp->sd_vc) == NULL)
240 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
243 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
244 vcp->obj.co_flags |= SMBV_PERMANENT;
246 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
247 vcp->obj.co_flags &= ~SMBV_PERMANENT;
248 smb_vc_rele(vcp, &scred);
250 smb_vc_put(vcp, &scred);
253 } else if (fl->ioc_level == SMBL_SHARE) {
254 if (fl->ioc_mask & SMBS_PERMANENT) {
255 on = fl->ioc_flags & SMBS_PERMANENT;
256 if ((ssp = sdp->sd_share) == NULL)
258 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
261 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
262 ssp->obj.co_flags |= SMBS_PERMANENT;
263 smb_share_ref(ssp, td);
264 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
265 ssp->obj.co_flags &= ~SMBS_PERMANENT;
266 smb_share_rele(ssp, &scred);
268 smb_share_put(ssp, &scred);
277 if (sdp->sd_vc || sdp->sd_share)
281 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
286 smb_vc_unlock(vcp, 0, td);
287 sdp->sd_level = SMBL_VC;
291 smb_share_unlock(ssp, 0, td);
292 sdp->sd_level = SMBL_SHARE;
295 case SMBIOC_READ: case SMBIOC_WRITE: {
296 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
300 if ((ssp = sdp->sd_share) == NULL)
302 iov.iov_base = rwrq->ioc_base;
303 iov.iov_len = rwrq->ioc_cnt;
306 auio.uio_offset = rwrq->ioc_offset;
307 auio.uio_resid = rwrq->ioc_cnt;
308 auio.uio_segflg = UIO_USERSPACE;
309 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
311 if (cmd == SMBIOC_READ)
312 error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
314 error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
315 rwrq->ioc_cnt -= auio.uio_resid;
325 nsmb_dev_read(dev_t dev, struct uio *uio, int flag)
331 nsmb_dev_write(dev_t dev, struct uio *uio, int flag)
337 nsmb_dev_poll(dev_t dev, int events, d_thread_t *td)
343 nsmb_dev_load(module_t mod, int cmd, void *arg)
349 error = smb_sm_init();
352 error = smb_iod_init();
357 cdevsw_add(&nsmb_cdevsw, 0, 0);
358 printf("netsmb_dev: loaded\n");
362 error = smb_sm_done();
364 cdevsw_remove(&nsmb_cdevsw, 0, 0);
365 printf("netsmb_dev: unloaded\n");
374 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
378 * Convert a file descriptor to appropriate smb_share pointer
381 nsmb_getfp(struct filedesc* fdp, int fd, int flag)
385 if (((u_int)fd) >= fdp->fd_nfiles ||
386 (fp = fdp->fd_files[fd].fp) == NULL ||
387 (fp->f_flag & flag) == 0)
393 smb_dev2share(int fd, int mode, struct smb_cred *scred,
394 struct smb_share **sspp)
399 struct smb_share *ssp;
403 KKASSERT(scred->scr_td->td_proc);
405 if ((fp = nsmb_getfp(scred->scr_td->td_proc->p_fd,
406 fd, FREAD | FWRITE)) == NULL) {
409 vp = (struct vnode*)fp->f_data;
419 error = smb_share_get(ssp, LK_EXCLUSIVE, scred);