xref: /freebsd/sys/netsmb/smb_dev.c (revision 19caf6c0881cc962d22da9a10bee042c15adbe93)
1c398230bSWarner Losh /*-
2681a5bbeSBoris Popov  * Copyright (c) 2000-2001 Boris Popov
3681a5bbeSBoris Popov  * All rights reserved.
4681a5bbeSBoris Popov  *
5681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
6681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
7681a5bbeSBoris Popov  * are met:
8681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
9681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
10681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
11681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
12681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
13681a5bbeSBoris Popov  * 3. All advertising materials mentioning features or use of this software
14681a5bbeSBoris Popov  *    must display the following acknowledgement:
15681a5bbeSBoris Popov  *    This product includes software developed by Boris Popov.
16681a5bbeSBoris Popov  * 4. Neither the name of the author nor the names of any co-contributors
17681a5bbeSBoris Popov  *    may be used to endorse or promote products derived from this software
18681a5bbeSBoris Popov  *    without specific prior written permission.
19681a5bbeSBoris Popov  *
20681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30681a5bbeSBoris Popov  * SUCH DAMAGE.
31681a5bbeSBoris Popov  */
32ab0de15bSDavid E. O'Brien 
33ab0de15bSDavid E. O'Brien #include <sys/cdefs.h>
34ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$");
35ab0de15bSDavid E. O'Brien 
36681a5bbeSBoris Popov #include <sys/param.h>
37681a5bbeSBoris Popov #include <sys/kernel.h>
385dba30f1SPoul-Henning Kamp #include <sys/module.h>
39681a5bbeSBoris Popov #include <sys/systm.h>
40681a5bbeSBoris Popov #include <sys/conf.h>
41681a5bbeSBoris Popov #include <sys/fcntl.h>
42fb919e4dSMark Murray #include <sys/ioccom.h>
43fb919e4dSMark Murray #include <sys/lock.h>
44fb919e4dSMark Murray #include <sys/malloc.h>
45fb919e4dSMark Murray #include <sys/file.h>		/* Must come after sys/malloc.h */
4613438f68SAlfred Perlstein #include <sys/filedesc.h>
47fb919e4dSMark Murray #include <sys/mbuf.h>
48681a5bbeSBoris Popov #include <sys/poll.h>
49fb919e4dSMark Murray #include <sys/proc.h>
50fb919e4dSMark Murray #include <sys/select.h>
51fb919e4dSMark Murray #include <sys/socket.h>
52681a5bbeSBoris Popov #include <sys/socketvar.h>
53681a5bbeSBoris Popov #include <sys/sysctl.h>
54fb919e4dSMark Murray #include <sys/uio.h>
55681a5bbeSBoris Popov #include <sys/vnode.h>
56681a5bbeSBoris Popov 
57681a5bbeSBoris Popov #include <net/if.h>
58681a5bbeSBoris Popov 
59681a5bbeSBoris Popov #include <netsmb/smb.h>
60681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
61681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
62681a5bbeSBoris Popov #include <netsmb/smb_dev.h>
63681a5bbeSBoris Popov 
64681a5bbeSBoris Popov #define SMB_GETDEV(dev)		((struct smb_dev*)(dev)->si_drv1)
65681a5bbeSBoris Popov #define	SMB_CHECKMINOR(dev)	do { \
66681a5bbeSBoris Popov 				    sdp = SMB_GETDEV(dev); \
67681a5bbeSBoris Popov 				    if (sdp == NULL) return ENXIO; \
68681a5bbeSBoris Popov 				} while(0)
69681a5bbeSBoris Popov 
70681a5bbeSBoris Popov static d_open_t	 nsmb_dev_open;
71681a5bbeSBoris Popov static d_close_t nsmb_dev_close;
72681a5bbeSBoris Popov static d_ioctl_t nsmb_dev_ioctl;
73681a5bbeSBoris Popov 
74c4f02a89SMax Khon MODULE_DEPEND(netsmb, libiconv, 1, 1, 2);
75681a5bbeSBoris Popov MODULE_VERSION(netsmb, NSMB_VERSION);
76681a5bbeSBoris Popov 
77681a5bbeSBoris Popov static int smb_version = NSMB_VERSION;
78681a5bbeSBoris Popov 
79681a5bbeSBoris Popov 
80681a5bbeSBoris Popov SYSCTL_DECL(_net_smb);
81681a5bbeSBoris Popov SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
82681a5bbeSBoris Popov 
83681a5bbeSBoris Popov static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
84681a5bbeSBoris Popov 
85681a5bbeSBoris Popov 
86681a5bbeSBoris Popov /*
87681a5bbeSBoris Popov int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
88681a5bbeSBoris Popov */
89681a5bbeSBoris Popov 
90681a5bbeSBoris Popov static struct cdevsw nsmb_cdevsw = {
91dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
92dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
937ac40f5fSPoul-Henning Kamp 	.d_open =	nsmb_dev_open,
947ac40f5fSPoul-Henning Kamp 	.d_close =	nsmb_dev_close,
957ac40f5fSPoul-Henning Kamp 	.d_ioctl =	nsmb_dev_ioctl,
96fd13f792STim J. Robbins 	.d_name =	NSMB_NAME
97681a5bbeSBoris Popov };
98681a5bbeSBoris Popov 
99681a5bbeSBoris Popov static eventhandler_tag nsmb_dev_tag;
100681a5bbeSBoris Popov 
101681a5bbeSBoris Popov static void
1026a113b3dSRobert Watson nsmb_dev_clone(void *arg, struct ucred *cred, char *name, int namelen,
1036a113b3dSRobert Watson     struct cdev **dev)
104681a5bbeSBoris Popov {
105a8c8a6cbSTim J. Robbins 	int u;
106681a5bbeSBoris Popov 
107f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
108681a5bbeSBoris Popov 		return;
109a8c8a6cbSTim J. Robbins 	if (dev_stdclone(name, NULL, NSMB_NAME, &u) != 1)
110681a5bbeSBoris Popov 		return;
111a8c8a6cbSTim J. Robbins 	*dev = make_dev(&nsmb_cdevsw, unit2minor(u), 0, 0, 0600,
112a8c8a6cbSTim J. Robbins 	    NSMB_NAME"%d", u);
113f4f6abcbSPoul-Henning Kamp 	dev_ref(*dev);
114681a5bbeSBoris Popov }
115681a5bbeSBoris Popov 
116681a5bbeSBoris Popov static int
11789c9c53dSPoul-Henning Kamp nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
118681a5bbeSBoris Popov {
119681a5bbeSBoris Popov 	struct smb_dev *sdp;
120a854ed98SJohn Baldwin 	struct ucred *cred = td->td_ucred;
121681a5bbeSBoris Popov 	int s;
122681a5bbeSBoris Popov 
123681a5bbeSBoris Popov 	sdp = SMB_GETDEV(dev);
124681a5bbeSBoris Popov 	if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
125681a5bbeSBoris Popov 		return EBUSY;
126681a5bbeSBoris Popov 	if (sdp == NULL) {
127a163d034SWarner Losh 		sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
128681a5bbeSBoris Popov 		dev->si_drv1 = (void*)sdp;
129681a5bbeSBoris Popov 	}
130681a5bbeSBoris Popov 	/*
131681a5bbeSBoris Popov 	 * XXX: this is just crazy - make a device for an already passed device...
132681a5bbeSBoris Popov 	 * someone should take care of it.
133681a5bbeSBoris Popov 	 */
134681a5bbeSBoris Popov 	if ((dev->si_flags & SI_NAMED) == 0)
135681a5bbeSBoris Popov 		make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700,
136681a5bbeSBoris Popov 		    NSMB_NAME"%d", dev2unit(dev));
137681a5bbeSBoris Popov 	bzero(sdp, sizeof(*sdp));
138681a5bbeSBoris Popov /*
139681a5bbeSBoris Popov 	STAILQ_INIT(&sdp->sd_rqlist);
140681a5bbeSBoris Popov 	STAILQ_INIT(&sdp->sd_rplist);
141681a5bbeSBoris Popov 	bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
142681a5bbeSBoris Popov */
143681a5bbeSBoris Popov 	s = splimp();
144681a5bbeSBoris Popov 	sdp->sd_level = -1;
145681a5bbeSBoris Popov 	sdp->sd_flags |= NSMBFL_OPEN;
146681a5bbeSBoris Popov 	splx(s);
147681a5bbeSBoris Popov 	return 0;
148681a5bbeSBoris Popov }
149681a5bbeSBoris Popov 
150681a5bbeSBoris Popov static int
15189c9c53dSPoul-Henning Kamp nsmb_dev_close(struct cdev *dev, int flag, int fmt, struct thread *td)
152681a5bbeSBoris Popov {
153681a5bbeSBoris Popov 	struct smb_dev *sdp;
154681a5bbeSBoris Popov 	struct smb_vc *vcp;
155681a5bbeSBoris Popov 	struct smb_share *ssp;
156681a5bbeSBoris Popov 	struct smb_cred scred;
157681a5bbeSBoris Popov 	int s;
158681a5bbeSBoris Popov 
159681a5bbeSBoris Popov 	SMB_CHECKMINOR(dev);
160681a5bbeSBoris Popov 	s = splimp();
161681a5bbeSBoris Popov 	if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
162681a5bbeSBoris Popov 		splx(s);
163681a5bbeSBoris Popov 		return EBADF;
164681a5bbeSBoris Popov 	}
165fce6fbfaSBoris Popov 	smb_makescred(&scred, td, NULL);
166681a5bbeSBoris Popov 	ssp = sdp->sd_share;
167681a5bbeSBoris Popov 	if (ssp != NULL)
168681a5bbeSBoris Popov 		smb_share_rele(ssp, &scred);
169681a5bbeSBoris Popov 	vcp = sdp->sd_vc;
170681a5bbeSBoris Popov 	if (vcp != NULL)
171681a5bbeSBoris Popov 		smb_vc_rele(vcp, &scred);
172681a5bbeSBoris Popov /*
173681a5bbeSBoris Popov 	smb_flushq(&sdp->sd_rqlist);
174681a5bbeSBoris Popov 	smb_flushq(&sdp->sd_rplist);
175681a5bbeSBoris Popov */
176681a5bbeSBoris Popov 	dev->si_drv1 = NULL;
177681a5bbeSBoris Popov 	free(sdp, M_NSMBDEV);
178681a5bbeSBoris Popov 	destroy_dev(dev);
179681a5bbeSBoris Popov 	splx(s);
180681a5bbeSBoris Popov 	return 0;
181681a5bbeSBoris Popov }
182681a5bbeSBoris Popov 
183681a5bbeSBoris Popov 
184681a5bbeSBoris Popov static int
18589c9c53dSPoul-Henning Kamp nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
186681a5bbeSBoris Popov {
187681a5bbeSBoris Popov 	struct smb_dev *sdp;
188681a5bbeSBoris Popov 	struct smb_vc *vcp;
189681a5bbeSBoris Popov 	struct smb_share *ssp;
190681a5bbeSBoris Popov 	struct smb_cred scred;
191681a5bbeSBoris Popov 	int error = 0;
192681a5bbeSBoris Popov 
193681a5bbeSBoris Popov 	SMB_CHECKMINOR(dev);
194681a5bbeSBoris Popov 	if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
195681a5bbeSBoris Popov 		return EBADF;
196681a5bbeSBoris Popov 
197fce6fbfaSBoris Popov 	smb_makescred(&scred, td, NULL);
198681a5bbeSBoris Popov 	switch (cmd) {
199681a5bbeSBoris Popov 	    case SMBIOC_OPENSESSION:
200681a5bbeSBoris Popov 		if (sdp->sd_vc)
201681a5bbeSBoris Popov 			return EISCONN;
202681a5bbeSBoris Popov 		error = smb_usr_opensession((struct smbioc_ossn*)data,
203681a5bbeSBoris Popov 		    &scred, &vcp);
204681a5bbeSBoris Popov 		if (error)
205681a5bbeSBoris Popov 			break;
206681a5bbeSBoris Popov 		sdp->sd_vc = vcp;
207fce6fbfaSBoris Popov 		smb_vc_unlock(vcp, 0, td);
208681a5bbeSBoris Popov 		sdp->sd_level = SMBL_VC;
209681a5bbeSBoris Popov 		break;
210681a5bbeSBoris Popov 	    case SMBIOC_OPENSHARE:
211681a5bbeSBoris Popov 		if (sdp->sd_share)
212681a5bbeSBoris Popov 			return EISCONN;
213681a5bbeSBoris Popov 		if (sdp->sd_vc == NULL)
214681a5bbeSBoris Popov 			return ENOTCONN;
215681a5bbeSBoris Popov 		error = smb_usr_openshare(sdp->sd_vc,
216681a5bbeSBoris Popov 		    (struct smbioc_oshare*)data, &scred, &ssp);
217681a5bbeSBoris Popov 		if (error)
218681a5bbeSBoris Popov 			break;
219681a5bbeSBoris Popov 		sdp->sd_share = ssp;
220fce6fbfaSBoris Popov 		smb_share_unlock(ssp, 0, td);
221681a5bbeSBoris Popov 		sdp->sd_level = SMBL_SHARE;
222681a5bbeSBoris Popov 		break;
223681a5bbeSBoris Popov 	    case SMBIOC_REQUEST:
224681a5bbeSBoris Popov 		if (sdp->sd_share == NULL)
225681a5bbeSBoris Popov 			return ENOTCONN;
226681a5bbeSBoris Popov 		error = smb_usr_simplerequest(sdp->sd_share,
227681a5bbeSBoris Popov 		    (struct smbioc_rq*)data, &scred);
228681a5bbeSBoris Popov 		break;
229681a5bbeSBoris Popov 	    case SMBIOC_T2RQ:
230681a5bbeSBoris Popov 		if (sdp->sd_share == NULL)
231681a5bbeSBoris Popov 			return ENOTCONN;
232681a5bbeSBoris Popov 		error = smb_usr_t2request(sdp->sd_share,
233681a5bbeSBoris Popov 		    (struct smbioc_t2rq*)data, &scred);
234681a5bbeSBoris Popov 		break;
235681a5bbeSBoris Popov 	    case SMBIOC_SETFLAGS: {
236681a5bbeSBoris Popov 		struct smbioc_flags *fl = (struct smbioc_flags*)data;
237681a5bbeSBoris Popov 		int on;
238681a5bbeSBoris Popov 
239681a5bbeSBoris Popov 		if (fl->ioc_level == SMBL_VC) {
240681a5bbeSBoris Popov 			if (fl->ioc_mask & SMBV_PERMANENT) {
241681a5bbeSBoris Popov 				on = fl->ioc_flags & SMBV_PERMANENT;
242681a5bbeSBoris Popov 				if ((vcp = sdp->sd_vc) == NULL)
243681a5bbeSBoris Popov 					return ENOTCONN;
244681a5bbeSBoris Popov 				error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
245681a5bbeSBoris Popov 				if (error)
246681a5bbeSBoris Popov 					break;
247681a5bbeSBoris Popov 				if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
248681a5bbeSBoris Popov 					vcp->obj.co_flags |= SMBV_PERMANENT;
249fce6fbfaSBoris Popov 					smb_vc_ref(vcp);
250681a5bbeSBoris Popov 				} else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
251681a5bbeSBoris Popov 					vcp->obj.co_flags &= ~SMBV_PERMANENT;
252681a5bbeSBoris Popov 					smb_vc_rele(vcp, &scred);
253681a5bbeSBoris Popov 				}
254681a5bbeSBoris Popov 				smb_vc_put(vcp, &scred);
255681a5bbeSBoris Popov 			} else
256681a5bbeSBoris Popov 				error = EINVAL;
257681a5bbeSBoris Popov 		} else if (fl->ioc_level == SMBL_SHARE) {
258681a5bbeSBoris Popov 			if (fl->ioc_mask & SMBS_PERMANENT) {
259681a5bbeSBoris Popov 				on = fl->ioc_flags & SMBS_PERMANENT;
260681a5bbeSBoris Popov 				if ((ssp = sdp->sd_share) == NULL)
261681a5bbeSBoris Popov 					return ENOTCONN;
262681a5bbeSBoris Popov 				error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
263681a5bbeSBoris Popov 				if (error)
264681a5bbeSBoris Popov 					break;
265681a5bbeSBoris Popov 				if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
266681a5bbeSBoris Popov 					ssp->obj.co_flags |= SMBS_PERMANENT;
267fce6fbfaSBoris Popov 					smb_share_ref(ssp);
268681a5bbeSBoris Popov 				} else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
269681a5bbeSBoris Popov 					ssp->obj.co_flags &= ~SMBS_PERMANENT;
270681a5bbeSBoris Popov 					smb_share_rele(ssp, &scred);
271681a5bbeSBoris Popov 				}
272681a5bbeSBoris Popov 				smb_share_put(ssp, &scred);
273681a5bbeSBoris Popov 			} else
274681a5bbeSBoris Popov 				error = EINVAL;
275681a5bbeSBoris Popov 			break;
276681a5bbeSBoris Popov 		} else
277681a5bbeSBoris Popov 			error = EINVAL;
278681a5bbeSBoris Popov 		break;
279681a5bbeSBoris Popov 	    }
280681a5bbeSBoris Popov 	    case SMBIOC_LOOKUP:
281681a5bbeSBoris Popov 		if (sdp->sd_vc || sdp->sd_share)
282681a5bbeSBoris Popov 			return EISCONN;
283681a5bbeSBoris Popov 		vcp = NULL;
284681a5bbeSBoris Popov 		ssp = NULL;
285681a5bbeSBoris Popov 		error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
286681a5bbeSBoris Popov 		if (error)
287681a5bbeSBoris Popov 			break;
288681a5bbeSBoris Popov 		if (vcp) {
289681a5bbeSBoris Popov 			sdp->sd_vc = vcp;
290fce6fbfaSBoris Popov 			smb_vc_unlock(vcp, 0, td);
291681a5bbeSBoris Popov 			sdp->sd_level = SMBL_VC;
292681a5bbeSBoris Popov 		}
293681a5bbeSBoris Popov 		if (ssp) {
294681a5bbeSBoris Popov 			sdp->sd_share = ssp;
295fce6fbfaSBoris Popov 			smb_share_unlock(ssp, 0, td);
296681a5bbeSBoris Popov 			sdp->sd_level = SMBL_SHARE;
297681a5bbeSBoris Popov 		}
298681a5bbeSBoris Popov 		break;
299681a5bbeSBoris Popov 	    case SMBIOC_READ: case SMBIOC_WRITE: {
300681a5bbeSBoris Popov 		struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
301681a5bbeSBoris Popov 		struct uio auio;
302681a5bbeSBoris Popov 		struct iovec iov;
303681a5bbeSBoris Popov 
304681a5bbeSBoris Popov 		if ((ssp = sdp->sd_share) == NULL)
305681a5bbeSBoris Popov 			return ENOTCONN;
306681a5bbeSBoris Popov 		iov.iov_base = rwrq->ioc_base;
307681a5bbeSBoris Popov 		iov.iov_len = rwrq->ioc_cnt;
308681a5bbeSBoris Popov 		auio.uio_iov = &iov;
309681a5bbeSBoris Popov 		auio.uio_iovcnt = 1;
310681a5bbeSBoris Popov 		auio.uio_offset = rwrq->ioc_offset;
311681a5bbeSBoris Popov 		auio.uio_resid = rwrq->ioc_cnt;
312681a5bbeSBoris Popov 		auio.uio_segflg = UIO_USERSPACE;
313681a5bbeSBoris Popov 		auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
314fce6fbfaSBoris Popov 		auio.uio_td = td;
315681a5bbeSBoris Popov 		if (cmd == SMBIOC_READ)
316681a5bbeSBoris Popov 			error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
317681a5bbeSBoris Popov 		else
318681a5bbeSBoris Popov 			error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
319681a5bbeSBoris Popov 		rwrq->ioc_cnt -= auio.uio_resid;
320681a5bbeSBoris Popov 		break;
321681a5bbeSBoris Popov 	    }
322681a5bbeSBoris Popov 	    default:
323681a5bbeSBoris Popov 		error = ENODEV;
324681a5bbeSBoris Popov 	}
325681a5bbeSBoris Popov 	return error;
326681a5bbeSBoris Popov }
327681a5bbeSBoris Popov 
328681a5bbeSBoris Popov static int
329681a5bbeSBoris Popov nsmb_dev_load(module_t mod, int cmd, void *arg)
330681a5bbeSBoris Popov {
331681a5bbeSBoris Popov 	int error = 0;
332681a5bbeSBoris Popov 
333681a5bbeSBoris Popov 	switch (cmd) {
334681a5bbeSBoris Popov 	    case MOD_LOAD:
335681a5bbeSBoris Popov 		error = smb_sm_init();
336681a5bbeSBoris Popov 		if (error)
337681a5bbeSBoris Popov 			break;
338681a5bbeSBoris Popov 		error = smb_iod_init();
339681a5bbeSBoris Popov 		if (error) {
340681a5bbeSBoris Popov 			smb_sm_done();
341681a5bbeSBoris Popov 			break;
342681a5bbeSBoris Popov 		}
343681a5bbeSBoris Popov 		nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000);
344681a5bbeSBoris Popov 		printf("netsmb_dev: loaded\n");
345681a5bbeSBoris Popov 		break;
346681a5bbeSBoris Popov 	    case MOD_UNLOAD:
347681a5bbeSBoris Popov 		smb_iod_done();
348681a5bbeSBoris Popov 		error = smb_sm_done();
34919caf6c0SBoris Popov 		if (error)
35019caf6c0SBoris Popov 			break;
351681a5bbeSBoris Popov 		EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag);
352681a5bbeSBoris Popov 		printf("netsmb_dev: unloaded\n");
353681a5bbeSBoris Popov 		break;
354681a5bbeSBoris Popov 	    default:
355681a5bbeSBoris Popov 		error = EINVAL;
356681a5bbeSBoris Popov 		break;
357681a5bbeSBoris Popov 	}
358681a5bbeSBoris Popov 	return error;
359681a5bbeSBoris Popov }
360681a5bbeSBoris Popov 
361681a5bbeSBoris Popov DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
362681a5bbeSBoris Popov 
363681a5bbeSBoris Popov /*
364681a5bbeSBoris Popov  * Convert a file descriptor to appropriate smb_share pointer
365681a5bbeSBoris Popov  */
366681a5bbeSBoris Popov static struct file*
367681a5bbeSBoris Popov nsmb_getfp(struct filedesc* fdp, int fd, int flag)
368681a5bbeSBoris Popov {
369681a5bbeSBoris Popov 	struct file* fp;
370681a5bbeSBoris Popov 
371426da3bcSAlfred Perlstein 	FILEDESC_LOCK(fdp);
372681a5bbeSBoris Popov 	if (((u_int)fd) >= fdp->fd_nfiles ||
373681a5bbeSBoris Popov 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
374426da3bcSAlfred Perlstein 	    (fp->f_flag & flag) == 0) {
375426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(fdp);
376681a5bbeSBoris Popov 		return (NULL);
377426da3bcSAlfred Perlstein 	}
378426da3bcSAlfred Perlstein 	fhold(fp);
379426da3bcSAlfred Perlstein 	FILEDESC_UNLOCK(fdp);
380681a5bbeSBoris Popov 	return (fp);
381681a5bbeSBoris Popov }
382681a5bbeSBoris Popov 
383681a5bbeSBoris Popov int
384681a5bbeSBoris Popov smb_dev2share(int fd, int mode, struct smb_cred *scred,
385681a5bbeSBoris Popov 	struct smb_share **sspp)
386681a5bbeSBoris Popov {
387681a5bbeSBoris Popov 	struct file *fp;
388681a5bbeSBoris Popov 	struct vnode *vp;
389681a5bbeSBoris Popov 	struct smb_dev *sdp;
390681a5bbeSBoris Popov 	struct smb_share *ssp;
39189c9c53dSPoul-Henning Kamp 	struct cdev *dev;
392681a5bbeSBoris Popov 	int error;
393681a5bbeSBoris Popov 
394fce6fbfaSBoris Popov 	fp = nsmb_getfp(scred->scr_td->td_proc->p_fd, fd, FREAD | FWRITE);
395fce6fbfaSBoris Popov 	if (fp == NULL)
396681a5bbeSBoris Popov 		return EBADF;
3973b6d9652SPoul-Henning Kamp 	vp = fp->f_vnode;
398426da3bcSAlfred Perlstein 	if (vp == NULL) {
399426da3bcSAlfred Perlstein 		fdrop(fp, curthread);
400681a5bbeSBoris Popov 		return EBADF;
401426da3bcSAlfred Perlstein 	}
40238bb5ccdSPoul-Henning Kamp 	if (vp->v_type != VCHR) {
403426da3bcSAlfred Perlstein 		fdrop(fp, curthread);
404681a5bbeSBoris Popov 		return EBADF;
405426da3bcSAlfred Perlstein 	}
40638bb5ccdSPoul-Henning Kamp 	dev = vp->v_rdev;
407681a5bbeSBoris Popov 	SMB_CHECKMINOR(dev);
408681a5bbeSBoris Popov 	ssp = sdp->sd_share;
409426da3bcSAlfred Perlstein 	if (ssp == NULL) {
410426da3bcSAlfred Perlstein 		fdrop(fp, curthread);
411681a5bbeSBoris Popov 		return ENOTCONN;
412426da3bcSAlfred Perlstein 	}
413681a5bbeSBoris Popov 	error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
414426da3bcSAlfred Perlstein 	if (error == 0)
415681a5bbeSBoris Popov 		*sspp = ssp;
416426da3bcSAlfred Perlstein 	fdrop(fp, curthread);
417426da3bcSAlfred Perlstein 	return error;
418681a5bbeSBoris Popov }
419681a5bbeSBoris Popov 
420