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_iod.c,v 1.1.2.2 2002/04/23 03:45:01 bp Exp $
33 * $DragonFly: src/sys/netproto/smb/smb_iod.c,v 1.15 2007/02/03 17:05:58 corecode Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/kthread.h>
41 #include <sys/malloc.h>
43 #include <sys/unistd.h>
45 #include <sys/mplock2.h>
51 #include "smb_trantcp.h"
54 #define SMBIOD_SLEEP_TIMO 2
55 #define SMBIOD_PING_TIMO 60 /* seconds */
57 #define SMB_IOD_EVLOCKPTR(iod) (&(iod)->iod_evlock)
58 #define SMB_IOD_EVLOCK(iod) smb_sl_lock(&(iod)->iod_evlock)
59 #define SMB_IOD_EVUNLOCK(iod) smb_sl_unlock(&(iod)->iod_evlock)
60 #define SMB_IOD_EVINTERLOCK(iod) (&(iod)->iod_evlock)
62 #define SMB_IOD_RQLOCKPTR(iod) (&(iod)->iod_rqlock)
63 #define SMB_IOD_RQLOCK(iod) smb_sl_lock(&((iod)->iod_rqlock))
64 #define SMB_IOD_RQUNLOCK(iod) smb_sl_unlock(&(iod)->iod_rqlock)
65 #define SMB_IOD_RQINTERLOCK(iod) (&(iod)->iod_rqlock)
67 #define smb_iod_wakeup(iod) wakeup(&(iod)->iod_flags)
70 static MALLOC_DEFINE(M_SMBIOD, "SMBIOD", "SMB network io daemon");
72 static int smb_iod_next;
74 static int smb_iod_sendall(struct smbiod *iod);
75 static int smb_iod_disconnect(struct smbiod *iod);
76 static void smb_iod_thread(void *);
79 smb_iod_rqprocessed(struct smb_rq *rqp, int error)
82 rqp->sr_lerror = error;
84 rqp->sr_state = SMBRQ_NOTIFIED;
85 wakeup(&rqp->sr_state);
90 smb_iod_invrq(struct smbiod *iod)
95 * Invalidate all outstanding requests for this connection
98 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
100 /* this makes no sense whatsoever XXX */
101 if (rqp->sr_flags & SMBR_INTERNAL)
104 rqp->sr_flags |= SMBR_RESTART;
105 smb_iod_rqprocessed(rqp, ENOTCONN);
107 SMB_IOD_RQUNLOCK(iod);
111 smb_iod_closetran(struct smbiod *iod)
113 struct smb_vc *vcp = iod->iod_vc;
114 struct thread *td = iod->iod_td;
116 if (vcp->vc_tdata == NULL)
118 SMB_TRAN_DISCONNECT(vcp, td);
119 SMB_TRAN_DONE(vcp, td);
120 vcp->vc_tdata = NULL;
124 smb_iod_dead(struct smbiod *iod)
126 iod->iod_state = SMBIOD_ST_DEAD;
127 smb_iod_closetran(iod);
132 smb_iod_connect(struct smbiod *iod)
134 struct smb_vc *vcp = iod->iod_vc;
135 struct thread *td = iod->iod_td;
138 SMBIODEBUG("%d\n", iod->iod_state);
139 switch(iod->iod_state) {
140 case SMBIOD_ST_VCACTIVE:
141 SMBERROR("called for already opened connection\n");
144 return ENOTCONN; /* XXX: last error code ? */
151 error = SMB_TRAN_CREATE(vcp, td);
154 SMBIODEBUG("tcreate\n");
157 error = SMB_TRAN_BIND(vcp, vcp->vc_laddr, td);
161 SMBIODEBUG("tbind\n");
163 error = SMB_TRAN_CONNECT(vcp, vcp->vc_paddr, td);
166 SMB_TRAN_SETPARAM(vcp, SMBTP_SELECTID, &iod->iod_flags);
167 iod->iod_state = SMBIOD_ST_TRANACTIVE;
168 SMBIODEBUG("tconnect\n");
170 /* vcp->vc_mid = 0;*/
172 error = smb_smb_negotiate(vcp, &iod->iod_scred);
175 SMBIODEBUG("snegotiate\n");
177 error = smb_smb_ssnsetup(vcp, &iod->iod_scred);
180 iod->iod_state = SMBIOD_ST_VCACTIVE;
181 SMBIODEBUG("completed\n");
193 smb_iod_disconnect(struct smbiod *iod)
195 struct smb_vc *vcp = iod->iod_vc;
198 if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
199 smb_smb_ssnclose(vcp, &iod->iod_scred);
200 iod->iod_state = SMBIOD_ST_TRANACTIVE;
202 vcp->vc_smbuid = SMB_UID_UNKNOWN;
203 smb_iod_closetran(iod);
204 iod->iod_state = SMBIOD_ST_NOTCONN;
209 smb_iod_treeconnect(struct smbiod *iod, struct smb_share *ssp)
213 if (iod->iod_state != SMBIOD_ST_VCACTIVE) {
214 if (iod->iod_state != SMBIOD_ST_DEAD)
216 iod->iod_state = SMBIOD_ST_RECONNECT;
217 error = smb_iod_connect(iod);
221 SMBIODEBUG("tree reconnect\n");
223 ssp->ss_flags |= SMBS_RECONNECTING;
225 error = smb_smb_treeconnect(ssp, &iod->iod_scred);
227 ssp->ss_flags &= ~SMBS_RECONNECTING;
229 wakeup(&ssp->ss_vcgenid);
234 smb_iod_sendrq(struct smbiod *iod, struct smb_rq *rqp)
236 struct thread *td = iod->iod_td;
237 struct smb_vc *vcp = iod->iod_vc;
238 struct smb_share *ssp = rqp->sr_share;
242 SMBIODEBUG("iod_state = %d\n", iod->iod_state);
243 switch (iod->iod_state) {
244 case SMBIOD_ST_NOTCONN:
245 smb_iod_rqprocessed(rqp, ENOTCONN);
248 iod->iod_state = SMBIOD_ST_RECONNECT;
250 case SMBIOD_ST_RECONNECT:
255 if (rqp->sr_sendcnt == 0) {
256 #ifdef movedtoanotherplace
257 if (vcp->vc_maxmux != 0 && iod->iod_muxcnt >= vcp->vc_maxmux)
260 *rqp->sr_rqtid = htoles(ssp ? ssp->ss_tid : SMB_TID_UNKNOWN);
261 *rqp->sr_rquid = htoles(vcp ? vcp->vc_smbuid : 0);
262 mb_fixhdr(&rqp->sr_rq);
264 if (rqp->sr_sendcnt++ > 5) {
265 rqp->sr_flags |= SMBR_RESTART;
266 smb_iod_rqprocessed(rqp, rqp->sr_lerror);
268 * If all attempts to send a request failed, then
269 * something is seriously hosed.
273 SMBSDEBUG("M:%04x, P:%04x, U:%04x, T:%04x\n", rqp->sr_mid, 0, 0, 0);
274 m_dumpm(rqp->sr_rq.mb_top);
275 m = m_copym(rqp->sr_rq.mb_top, 0, M_COPYALL, MB_WAIT);
276 error = rqp->sr_lerror = m ? SMB_TRAN_SEND(vcp, m, td) : ENOBUFS;
278 getnanotime(&rqp->sr_timesent);
279 iod->iod_lastrqsent = rqp->sr_timesent;
280 rqp->sr_flags |= SMBR_SENT;
281 rqp->sr_state = SMBRQ_SENT;
285 * Check for fatal errors
287 if (SMB_TRAN_FATAL(vcp, error)) {
289 * No further attempts should be made
293 if (smb_rq_intr(rqp))
294 smb_iod_rqprocessed(rqp, EINTR);
299 * Process incoming packets
302 smb_iod_recvall(struct smbiod *iod)
304 struct smb_vc *vcp = iod->iod_vc;
305 struct thread *td = iod->iod_td;
312 switch (iod->iod_state) {
313 case SMBIOD_ST_NOTCONN:
315 case SMBIOD_ST_RECONNECT:
322 error = SMB_TRAN_RECV(vcp, &m, td);
323 if (error == EWOULDBLOCK)
325 if (SMB_TRAN_FATAL(vcp, error)) {
332 SMBERROR("tran return NULL without error\n");
336 m = m_pullup(m, SMB_HDRLEN);
338 continue; /* wait for a good packet */
340 * Now we got an entire and possibly invalid SMB packet.
341 * Be careful while parsing it.
344 hp = mtod(m, u_char*);
345 if (bcmp(hp, SMB_SIGNATURE, SMB_SIGLEN) != 0) {
349 mid = SMB_HDRMID(hp);
350 SMBSDEBUG("mid %04x\n", (u_int)mid);
352 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
353 if (rqp->sr_mid != mid)
356 if (rqp->sr_rp.md_top == NULL) {
357 md_initm(&rqp->sr_rp, m);
359 if (rqp->sr_flags & SMBR_MULTIPACKET) {
360 md_append_record(&rqp->sr_rp, m);
363 SMBERROR("duplicate response %d (ignored)\n", mid);
368 smb_iod_rqprocessed(rqp, 0);
371 SMB_IOD_RQUNLOCK(iod);
373 SMBERROR("drop resp with mid %d\n", (u_int)mid);
374 /* smb_printrqlist(vcp);*/
379 * check for interrupts
382 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
383 if (smb_proc_intr(rqp->sr_cred->scr_td)) {
384 smb_iod_rqprocessed(rqp, EINTR);
387 SMB_IOD_RQUNLOCK(iod);
392 smb_iod_request(struct smbiod *iod, int event, void *ident)
394 struct smbiod_event *evp;
398 evp = smb_zmalloc(sizeof(*evp), M_SMBIOD, M_WAITOK);
399 evp->ev_type = event;
400 evp->ev_ident = ident;
402 STAILQ_INSERT_TAIL(&iod->iod_evlist, evp, ev_link);
403 if ((event & SMBIOD_EV_SYNC) == 0) {
404 SMB_IOD_EVUNLOCK(iod);
409 smb_sleep(evp, SMB_IOD_EVINTERLOCK(iod), PDROP, "90evw", 0);
410 error = evp->ev_error;
411 kfree(evp, M_SMBIOD);
416 * Place request in the queue.
417 * Request from smbiod have a high priority.
420 smb_iod_addrq(struct smb_rq *rqp)
422 struct smb_vc *vcp = rqp->sr_vc;
423 struct smbiod *iod = vcp->vc_iod;
427 if (rqp->sr_cred->scr_td == iod->iod_td) {
428 rqp->sr_flags |= SMBR_INTERNAL;
430 TAILQ_INSERT_HEAD(&iod->iod_rqlist, rqp, sr_link);
431 SMB_IOD_RQUNLOCK(iod);
433 if (smb_iod_sendrq(iod, rqp) != 0) {
438 * we don't need to lock state field here
440 if (rqp->sr_state != SMBRQ_NOTSENT)
442 tsleep(&iod->iod_flags, 0, "90sndw", hz);
445 smb_iod_removerq(rqp);
446 return rqp->sr_lerror;
449 switch (iod->iod_state) {
450 case SMBIOD_ST_NOTCONN:
453 error = smb_iod_request(vcp->vc_iod, SMBIOD_EV_CONNECT | SMBIOD_EV_SYNC, NULL);
463 if (vcp->vc_maxmux == 0) {
464 SMBERROR("maxmux == 0\n");
467 if (iod->iod_muxcnt < vcp->vc_maxmux)
470 smb_sleep(&iod->iod_muxwant, SMB_IOD_RQINTERLOCK(iod), 0, "90mux", 0);
473 TAILQ_INSERT_TAIL(&iod->iod_rqlist, rqp, sr_link);
474 SMB_IOD_RQUNLOCK(iod);
480 smb_iod_removerq(struct smb_rq *rqp)
482 struct smb_vc *vcp = rqp->sr_vc;
483 struct smbiod *iod = vcp->vc_iod;
486 if (rqp->sr_flags & SMBR_INTERNAL) {
488 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
489 SMB_IOD_RQUNLOCK(iod);
493 while (rqp->sr_flags & SMBR_XLOCK) {
494 rqp->sr_flags |= SMBR_XLOCKWANT;
495 smb_sleep(rqp, SMB_IOD_RQINTERLOCK(iod), 0, "90xrm", 0);
497 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
499 if (iod->iod_muxwant) {
501 wakeup(&iod->iod_muxwant);
503 SMB_IOD_RQUNLOCK(iod);
508 smb_iod_waitrq(struct smb_rq *rqp)
510 struct smbiod *iod = rqp->sr_vc->vc_iod;
514 if (rqp->sr_flags & SMBR_INTERNAL) {
516 smb_iod_sendall(iod);
517 smb_iod_recvall(iod);
518 if (rqp->sr_rpgen != rqp->sr_rplast)
520 tsleep(&iod->iod_flags, 0, "90irq", hz);
522 smb_iod_removerq(rqp);
523 return rqp->sr_lerror;
527 if (rqp->sr_rpgen == rqp->sr_rplast)
528 smb_sleep(&rqp->sr_state, SMBRQ_INTERLOCK(rqp), 0, "90wrq", 0);
531 error = rqp->sr_lerror;
532 if (rqp->sr_flags & SMBR_MULTIPACKET) {
534 * If request should stay in the list, then reinsert it
535 * at the end of queue so other waiters have chance to concur
538 TAILQ_REMOVE(&iod->iod_rqlist, rqp, sr_link);
539 TAILQ_INSERT_TAIL(&iod->iod_rqlist, rqp, sr_link);
540 SMB_IOD_RQUNLOCK(iod);
542 smb_iod_removerq(rqp);
548 smb_iod_sendall(struct smbiod *iod)
550 struct smb_vc *vcp = iod->iod_vc;
552 struct timespec ts, tstimeout;
557 * Loop through the list of requests and send them if possible
560 TAILQ_FOREACH(rqp, &iod->iod_rqlist, sr_link) {
561 switch (rqp->sr_state) {
563 rqp->sr_flags |= SMBR_XLOCK;
564 SMB_IOD_RQUNLOCK(iod);
565 herror = smb_iod_sendrq(iod, rqp);
567 rqp->sr_flags &= ~SMBR_XLOCK;
568 if (rqp->sr_flags & SMBR_XLOCKWANT) {
569 rqp->sr_flags &= ~SMBR_XLOCKWANT;
574 SMB_TRAN_GETPARAM(vcp, SMBTP_TIMEOUT, &tstimeout);
575 timespecadd(&tstimeout, &tstimeout);
577 timespecsub(&ts, &tstimeout);
578 if (timespeccmp(&ts, &rqp->sr_timesent, >)) {
579 smb_iod_rqprocessed(rqp, ETIMEDOUT);
588 SMB_IOD_RQUNLOCK(iod);
589 if (herror == ENOTCONN)
595 * "main" function for smbiod daemon
598 smb_iod_main(struct smbiod *iod)
600 /* struct smb_vc *vcp = iod->iod_vc;*/
601 struct smbiod_event *evp;
602 /* struct timespec tsnow;*/
609 * Check all interesting events
613 evp = STAILQ_FIRST(&iod->iod_evlist);
615 SMB_IOD_EVUNLOCK(iod);
618 STAILQ_REMOVE_HEAD(&iod->iod_evlist, ev_link);
619 evp->ev_type |= SMBIOD_EV_PROCESSING;
620 SMB_IOD_EVUNLOCK(iod);
621 switch (evp->ev_type & SMBIOD_EV_MASK) {
622 case SMBIOD_EV_CONNECT:
623 iod->iod_state = SMBIOD_ST_RECONNECT;
624 evp->ev_error = smb_iod_connect(iod);
626 case SMBIOD_EV_DISCONNECT:
627 evp->ev_error = smb_iod_disconnect(iod);
629 case SMBIOD_EV_TREECONNECT:
630 evp->ev_error = smb_iod_treeconnect(iod, evp->ev_ident);
632 case SMBIOD_EV_SHUTDOWN:
633 iod->iod_flags |= SMBIOD_SHUTDOWN;
635 case SMBIOD_EV_NEWRQ:
638 if (evp->ev_type & SMBIOD_EV_SYNC) {
641 SMB_IOD_EVUNLOCK(iod);
643 kfree(evp, M_SMBIOD);
646 if (iod->iod_state == SMBIOD_ST_VCACTIVE) {
648 timespecsub(&tsnow, &iod->iod_pingtimo);
649 if (timespeccmp(&tsnow, &iod->iod_lastrqsent, >)) {
650 smb_smb_echo(vcp, &iod->iod_scred);
654 smb_iod_sendall(iod);
655 smb_iod_recvall(iod);
659 #define kthread_create_compat smb_kthread_create
660 #define kthread_exit_compat smb_kthread_exit
663 smb_iod_thread(void *arg)
665 struct smbiod *iod = arg;
668 * mplock not held on entry but we aren't mpsafe yet.
672 smb_makescred(&iod->iod_scred, iod->iod_td, NULL);
673 while ((iod->iod_flags & SMBIOD_SHUTDOWN) == 0) {
675 SMBIODEBUG("going to sleep for %d ticks\n", iod->iod_sleeptimo);
676 if (iod->iod_flags & SMBIOD_SHUTDOWN)
678 tsleep(&iod->iod_flags, 0, "90idle", iod->iod_sleeptimo);
680 kthread_exit_compat();
684 smb_iod_create(struct smb_vc *vcp)
687 struct proc *newp = NULL;
690 iod = smb_zmalloc(sizeof(*iod), M_SMBIOD, M_WAITOK);
691 iod->iod_id = smb_iod_next++;
692 iod->iod_state = SMBIOD_ST_NOTCONN;
694 iod->iod_sleeptimo = hz * SMBIOD_SLEEP_TIMO;
695 iod->iod_pingtimo.tv_sec = SMBIOD_PING_TIMO;
696 getnanotime(&iod->iod_lastrqsent);
698 smb_sl_init(&iod->iod_rqlock, "90rql");
699 TAILQ_INIT(&iod->iod_rqlist);
700 smb_sl_init(&iod->iod_evlock, "90evl");
701 STAILQ_INIT(&iod->iod_evlist);
702 error = kthread_create_compat(smb_iod_thread, iod, &newp,
703 RFNOWAIT, "smbiod%d", iod->iod_id);
705 SMBERROR("can't start smbiod: %d", error);
706 kfree(iod, M_SMBIOD);
710 iod->iod_td = ONLY_LWP_IN_PROC(newp)->lwp_thread;
715 smb_iod_destroy(struct smbiod *iod)
717 smb_iod_request(iod, SMBIOD_EV_SHUTDOWN | SMBIOD_EV_SYNC, NULL);
718 smb_sl_destroy(&iod->iod_rqlock);
719 smb_sl_destroy(&iod->iod_evlock);
720 kfree(iod, M_SMBIOD);