xref: /freebsd/sys/netsmb/smb_dev.c (revision fb919e4d5a2c1baca52ac70d1064f140fffdda71)
1681a5bbeSBoris Popov /*
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  *
32681a5bbeSBoris Popov  * $FreeBSD$
33681a5bbeSBoris Popov  */
34681a5bbeSBoris Popov #include <sys/param.h>
35681a5bbeSBoris Popov #include <sys/kernel.h>
36681a5bbeSBoris Popov #include <sys/systm.h>
37681a5bbeSBoris Popov #include <sys/conf.h>
38681a5bbeSBoris Popov #include <sys/fcntl.h>
39fb919e4dSMark Murray #include <sys/ioccom.h>
40fb919e4dSMark Murray #include <sys/lock.h>
41fb919e4dSMark Murray #include <sys/malloc.h>
42fb919e4dSMark Murray #include <sys/file.h>		/* Must come after sys/malloc.h */
43fb919e4dSMark Murray #include <sys/mbuf.h>
44681a5bbeSBoris Popov #include <sys/poll.h>
45fb919e4dSMark Murray #include <sys/proc.h>
46fb919e4dSMark Murray #include <sys/select.h>
47fb919e4dSMark Murray #include <sys/socket.h>
48681a5bbeSBoris Popov #include <sys/socketvar.h>
49681a5bbeSBoris Popov #include <sys/sysctl.h>
50fb919e4dSMark Murray #include <sys/uio.h>
51681a5bbeSBoris Popov #include <sys/vnode.h>
52681a5bbeSBoris Popov 
53681a5bbeSBoris Popov #include <net/if.h>
54681a5bbeSBoris Popov 
55681a5bbeSBoris Popov #include <netsmb/smb.h>
56681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
57681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
58681a5bbeSBoris Popov #include <netsmb/smb_dev.h>
59681a5bbeSBoris Popov 
60681a5bbeSBoris Popov #define SMB_GETDEV(dev)		((struct smb_dev*)(dev)->si_drv1)
61681a5bbeSBoris Popov #define	SMB_CHECKMINOR(dev)	do { \
62681a5bbeSBoris Popov 				    sdp = SMB_GETDEV(dev); \
63681a5bbeSBoris Popov 				    if (sdp == NULL) return ENXIO; \
64681a5bbeSBoris Popov 				} while(0)
65681a5bbeSBoris Popov 
66681a5bbeSBoris Popov static d_open_t	 nsmb_dev_open;
67681a5bbeSBoris Popov static d_close_t nsmb_dev_close;
68681a5bbeSBoris Popov static d_read_t	 nsmb_dev_read;
69681a5bbeSBoris Popov static d_write_t nsmb_dev_write;
70681a5bbeSBoris Popov static d_ioctl_t nsmb_dev_ioctl;
71681a5bbeSBoris Popov static d_poll_t	 nsmb_dev_poll;
72681a5bbeSBoris Popov 
73681a5bbeSBoris Popov MODULE_DEPEND(netsmb, libiconv, 1, 1, 1);
74681a5bbeSBoris Popov MODULE_VERSION(netsmb, NSMB_VERSION);
75681a5bbeSBoris Popov 
76681a5bbeSBoris Popov static int smb_version = NSMB_VERSION;
77681a5bbeSBoris Popov 
78681a5bbeSBoris Popov 
79681a5bbeSBoris Popov SYSCTL_DECL(_net_smb);
80681a5bbeSBoris Popov SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
81681a5bbeSBoris Popov 
82681a5bbeSBoris Popov static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
83681a5bbeSBoris Popov 
84681a5bbeSBoris Popov 
85681a5bbeSBoris Popov /*
86681a5bbeSBoris Popov int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
87681a5bbeSBoris Popov */
88681a5bbeSBoris Popov 
89681a5bbeSBoris Popov static struct cdevsw nsmb_cdevsw = {
90681a5bbeSBoris Popov 	/* open */	nsmb_dev_open,
91681a5bbeSBoris Popov 	/* close */	nsmb_dev_close,
92681a5bbeSBoris Popov 	/* read */	nsmb_dev_read,
93681a5bbeSBoris Popov 	/* write */	nsmb_dev_write,
94681a5bbeSBoris Popov 	/* ioctl */ 	nsmb_dev_ioctl,
95681a5bbeSBoris Popov 	/* poll */	nsmb_dev_poll,
96681a5bbeSBoris Popov 	/* mmap */	nommap,
97681a5bbeSBoris Popov 	/* strategy */	nostrategy,
98681a5bbeSBoris Popov 	/* name */	NSMB_NAME,
99681a5bbeSBoris Popov 	/* maj */	NSMB_MAJOR,
100681a5bbeSBoris Popov 	/* dump */	nodump,
101681a5bbeSBoris Popov 	/* psize */	nopsize,
102681a5bbeSBoris Popov 	/* flags */	0,
103681a5bbeSBoris Popov #ifndef FB_CURRENT
104681a5bbeSBoris Popov 	/* bmaj */	-1
105681a5bbeSBoris Popov #endif
106681a5bbeSBoris Popov };
107681a5bbeSBoris Popov 
108681a5bbeSBoris Popov static eventhandler_tag nsmb_dev_tag;
109681a5bbeSBoris Popov 
110681a5bbeSBoris Popov static void
111681a5bbeSBoris Popov nsmb_dev_clone(void *arg, char *name, int namelen, dev_t *dev)
112681a5bbeSBoris Popov {
113681a5bbeSBoris Popov 	int min;
114681a5bbeSBoris Popov 
115681a5bbeSBoris Popov 	if (*dev != NODEV)
116681a5bbeSBoris Popov 		return;
117681a5bbeSBoris Popov 	if (dev_stdclone(name, NULL, NSMB_NAME, &min) != 1)
118681a5bbeSBoris Popov 		return;
119681a5bbeSBoris Popov 	*dev = make_dev(&nsmb_cdevsw, min, 0, 0, 0600, NSMB_NAME"%d", min);
120681a5bbeSBoris Popov }
121681a5bbeSBoris Popov 
122681a5bbeSBoris Popov static int
123681a5bbeSBoris Popov nsmb_dev_open(dev_t dev, int oflags, int devtype, struct proc *p)
124681a5bbeSBoris Popov {
125681a5bbeSBoris Popov 	struct smb_dev *sdp;
126681a5bbeSBoris Popov 	struct ucred *cred = p->p_ucred;
127681a5bbeSBoris Popov 	int s;
128681a5bbeSBoris Popov 
129681a5bbeSBoris Popov 	sdp = SMB_GETDEV(dev);
130681a5bbeSBoris Popov 	if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
131681a5bbeSBoris Popov 		return EBUSY;
132681a5bbeSBoris Popov 	if (sdp == NULL) {
133681a5bbeSBoris Popov 		sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
134681a5bbeSBoris Popov 		dev->si_drv1 = (void*)sdp;
135681a5bbeSBoris Popov 	}
136681a5bbeSBoris Popov 	/*
137681a5bbeSBoris Popov 	 * XXX: this is just crazy - make a device for an already passed device...
138681a5bbeSBoris Popov 	 * someone should take care of it.
139681a5bbeSBoris Popov 	 */
140681a5bbeSBoris Popov 	if ((dev->si_flags & SI_NAMED) == 0)
141681a5bbeSBoris Popov 		make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700,
142681a5bbeSBoris Popov 		    NSMB_NAME"%d", dev2unit(dev));
143681a5bbeSBoris Popov 	bzero(sdp, sizeof(*sdp));
144681a5bbeSBoris Popov /*
145681a5bbeSBoris Popov 	STAILQ_INIT(&sdp->sd_rqlist);
146681a5bbeSBoris Popov 	STAILQ_INIT(&sdp->sd_rplist);
147681a5bbeSBoris Popov 	bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
148681a5bbeSBoris Popov */
149681a5bbeSBoris Popov 	s = splimp();
150681a5bbeSBoris Popov 	sdp->sd_level = -1;
151681a5bbeSBoris Popov 	sdp->sd_flags |= NSMBFL_OPEN;
152681a5bbeSBoris Popov 	splx(s);
153681a5bbeSBoris Popov 	return 0;
154681a5bbeSBoris Popov }
155681a5bbeSBoris Popov 
156681a5bbeSBoris Popov static int
157681a5bbeSBoris Popov nsmb_dev_close(dev_t dev, int flag, int fmt, struct proc *p)
158681a5bbeSBoris Popov {
159681a5bbeSBoris Popov 	struct smb_dev *sdp;
160681a5bbeSBoris Popov 	struct smb_vc *vcp;
161681a5bbeSBoris Popov 	struct smb_share *ssp;
162681a5bbeSBoris Popov 	struct smb_cred scred;
163681a5bbeSBoris Popov 	int s;
164681a5bbeSBoris Popov 
165681a5bbeSBoris Popov 	SMB_CHECKMINOR(dev);
166681a5bbeSBoris Popov 	s = splimp();
167681a5bbeSBoris Popov 	if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
168681a5bbeSBoris Popov 		splx(s);
169681a5bbeSBoris Popov 		return EBADF;
170681a5bbeSBoris Popov 	}
171681a5bbeSBoris Popov 	smb_makescred(&scred, p, NULL);
172681a5bbeSBoris Popov 	ssp = sdp->sd_share;
173681a5bbeSBoris Popov 	if (ssp != NULL)
174681a5bbeSBoris Popov 		smb_share_rele(ssp, &scred);
175681a5bbeSBoris Popov 	vcp = sdp->sd_vc;
176681a5bbeSBoris Popov 	if (vcp != NULL)
177681a5bbeSBoris Popov 		smb_vc_rele(vcp, &scred);
178681a5bbeSBoris Popov /*
179681a5bbeSBoris Popov 	smb_flushq(&sdp->sd_rqlist);
180681a5bbeSBoris Popov 	smb_flushq(&sdp->sd_rplist);
181681a5bbeSBoris Popov */
182681a5bbeSBoris Popov #if __FreeBSD_version > 400001
183681a5bbeSBoris Popov 	dev->si_drv1 = NULL;
184681a5bbeSBoris Popov 	free(sdp, M_NSMBDEV);
185681a5bbeSBoris Popov 	destroy_dev(dev);
186681a5bbeSBoris Popov #else
187681a5bbeSBoris Popov 	sdp->sd_flags &= ~NSMBFL_OPEN;
188681a5bbeSBoris Popov #endif
189681a5bbeSBoris Popov 	splx(s);
190681a5bbeSBoris Popov 	return 0;
191681a5bbeSBoris Popov }
192681a5bbeSBoris Popov 
193681a5bbeSBoris Popov 
194681a5bbeSBoris Popov static int
195681a5bbeSBoris Popov nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
196681a5bbeSBoris Popov {
197681a5bbeSBoris Popov 	struct smb_dev *sdp;
198681a5bbeSBoris Popov 	struct smb_vc *vcp;
199681a5bbeSBoris Popov 	struct smb_share *ssp;
200681a5bbeSBoris Popov 	struct smb_cred scred;
201681a5bbeSBoris Popov 	int error = 0;
202681a5bbeSBoris Popov 
203681a5bbeSBoris Popov 	SMB_CHECKMINOR(dev);
204681a5bbeSBoris Popov 	if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
205681a5bbeSBoris Popov 		return EBADF;
206681a5bbeSBoris Popov 
207681a5bbeSBoris Popov 	smb_makescred(&scred, p, NULL);
208681a5bbeSBoris Popov 	switch (cmd) {
209681a5bbeSBoris Popov 	    case SMBIOC_OPENSESSION:
210681a5bbeSBoris Popov 		if (sdp->sd_vc)
211681a5bbeSBoris Popov 			return EISCONN;
212681a5bbeSBoris Popov 		error = smb_usr_opensession((struct smbioc_ossn*)data,
213681a5bbeSBoris Popov 		    &scred, &vcp);
214681a5bbeSBoris Popov 		if (error)
215681a5bbeSBoris Popov 			break;
216681a5bbeSBoris Popov 		sdp->sd_vc = vcp;
217681a5bbeSBoris Popov 		smb_vc_unlock(vcp, 0, p);
218681a5bbeSBoris Popov 		sdp->sd_level = SMBL_VC;
219681a5bbeSBoris Popov 		break;
220681a5bbeSBoris Popov 	    case SMBIOC_OPENSHARE:
221681a5bbeSBoris Popov 		if (sdp->sd_share)
222681a5bbeSBoris Popov 			return EISCONN;
223681a5bbeSBoris Popov 		if (sdp->sd_vc == NULL)
224681a5bbeSBoris Popov 			return ENOTCONN;
225681a5bbeSBoris Popov 		error = smb_usr_openshare(sdp->sd_vc,
226681a5bbeSBoris Popov 		    (struct smbioc_oshare*)data, &scred, &ssp);
227681a5bbeSBoris Popov 		if (error)
228681a5bbeSBoris Popov 			break;
229681a5bbeSBoris Popov 		sdp->sd_share = ssp;
230681a5bbeSBoris Popov 		smb_share_unlock(ssp, 0, p);
231681a5bbeSBoris Popov 		sdp->sd_level = SMBL_SHARE;
232681a5bbeSBoris Popov 		break;
233681a5bbeSBoris Popov 	    case SMBIOC_REQUEST:
234681a5bbeSBoris Popov 		if (sdp->sd_share == NULL)
235681a5bbeSBoris Popov 			return ENOTCONN;
236681a5bbeSBoris Popov 		error = smb_usr_simplerequest(sdp->sd_share,
237681a5bbeSBoris Popov 		    (struct smbioc_rq*)data, &scred);
238681a5bbeSBoris Popov 		break;
239681a5bbeSBoris Popov 	    case SMBIOC_T2RQ:
240681a5bbeSBoris Popov 		if (sdp->sd_share == NULL)
241681a5bbeSBoris Popov 			return ENOTCONN;
242681a5bbeSBoris Popov 		error = smb_usr_t2request(sdp->sd_share,
243681a5bbeSBoris Popov 		    (struct smbioc_t2rq*)data, &scred);
244681a5bbeSBoris Popov 		break;
245681a5bbeSBoris Popov 	    case SMBIOC_SETFLAGS: {
246681a5bbeSBoris Popov 		struct smbioc_flags *fl = (struct smbioc_flags*)data;
247681a5bbeSBoris Popov 		int on;
248681a5bbeSBoris Popov 
249681a5bbeSBoris Popov 		if (fl->ioc_level == SMBL_VC) {
250681a5bbeSBoris Popov 			if (fl->ioc_mask & SMBV_PERMANENT) {
251681a5bbeSBoris Popov 				on = fl->ioc_flags & SMBV_PERMANENT;
252681a5bbeSBoris Popov 				if ((vcp = sdp->sd_vc) == NULL)
253681a5bbeSBoris Popov 					return ENOTCONN;
254681a5bbeSBoris Popov 				error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
255681a5bbeSBoris Popov 				if (error)
256681a5bbeSBoris Popov 					break;
257681a5bbeSBoris Popov 				if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
258681a5bbeSBoris Popov 					vcp->obj.co_flags |= SMBV_PERMANENT;
259681a5bbeSBoris Popov 					smb_vc_ref(vcp, p);
260681a5bbeSBoris Popov 				} else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
261681a5bbeSBoris Popov 					vcp->obj.co_flags &= ~SMBV_PERMANENT;
262681a5bbeSBoris Popov 					smb_vc_rele(vcp, &scred);
263681a5bbeSBoris Popov 				}
264681a5bbeSBoris Popov 				smb_vc_put(vcp, &scred);
265681a5bbeSBoris Popov 			} else
266681a5bbeSBoris Popov 				error = EINVAL;
267681a5bbeSBoris Popov 		} else if (fl->ioc_level == SMBL_SHARE) {
268681a5bbeSBoris Popov 			if (fl->ioc_mask & SMBS_PERMANENT) {
269681a5bbeSBoris Popov 				on = fl->ioc_flags & SMBS_PERMANENT;
270681a5bbeSBoris Popov 				if ((ssp = sdp->sd_share) == NULL)
271681a5bbeSBoris Popov 					return ENOTCONN;
272681a5bbeSBoris Popov 				error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
273681a5bbeSBoris Popov 				if (error)
274681a5bbeSBoris Popov 					break;
275681a5bbeSBoris Popov 				if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
276681a5bbeSBoris Popov 					ssp->obj.co_flags |= SMBS_PERMANENT;
277681a5bbeSBoris Popov 					smb_share_ref(ssp, p);
278681a5bbeSBoris Popov 				} else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
279681a5bbeSBoris Popov 					ssp->obj.co_flags &= ~SMBS_PERMANENT;
280681a5bbeSBoris Popov 					smb_share_rele(ssp, &scred);
281681a5bbeSBoris Popov 				}
282681a5bbeSBoris Popov 				smb_share_put(ssp, &scred);
283681a5bbeSBoris Popov 			} else
284681a5bbeSBoris Popov 				error = EINVAL;
285681a5bbeSBoris Popov 			break;
286681a5bbeSBoris Popov 		} else
287681a5bbeSBoris Popov 			error = EINVAL;
288681a5bbeSBoris Popov 		break;
289681a5bbeSBoris Popov 	    }
290681a5bbeSBoris Popov 	    case SMBIOC_LOOKUP:
291681a5bbeSBoris Popov 		if (sdp->sd_vc || sdp->sd_share)
292681a5bbeSBoris Popov 			return EISCONN;
293681a5bbeSBoris Popov 		vcp = NULL;
294681a5bbeSBoris Popov 		ssp = NULL;
295681a5bbeSBoris Popov 		error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
296681a5bbeSBoris Popov 		if (error)
297681a5bbeSBoris Popov 			break;
298681a5bbeSBoris Popov 		if (vcp) {
299681a5bbeSBoris Popov 			sdp->sd_vc = vcp;
300681a5bbeSBoris Popov 			smb_vc_unlock(vcp, 0, p);
301681a5bbeSBoris Popov 			sdp->sd_level = SMBL_VC;
302681a5bbeSBoris Popov 		}
303681a5bbeSBoris Popov 		if (ssp) {
304681a5bbeSBoris Popov 			sdp->sd_share = ssp;
305681a5bbeSBoris Popov 			smb_share_unlock(ssp, 0, p);
306681a5bbeSBoris Popov 			sdp->sd_level = SMBL_SHARE;
307681a5bbeSBoris Popov 		}
308681a5bbeSBoris Popov 		break;
309681a5bbeSBoris Popov 	    case SMBIOC_READ: case SMBIOC_WRITE: {
310681a5bbeSBoris Popov 		struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
311681a5bbeSBoris Popov 		struct uio auio;
312681a5bbeSBoris Popov 		struct iovec iov;
313681a5bbeSBoris Popov 
314681a5bbeSBoris Popov 		if ((ssp = sdp->sd_share) == NULL)
315681a5bbeSBoris Popov 			return ENOTCONN;
316681a5bbeSBoris Popov 		iov.iov_base = rwrq->ioc_base;
317681a5bbeSBoris Popov 		iov.iov_len = rwrq->ioc_cnt;
318681a5bbeSBoris Popov 		auio.uio_iov = &iov;
319681a5bbeSBoris Popov 		auio.uio_iovcnt = 1;
320681a5bbeSBoris Popov 		auio.uio_offset = rwrq->ioc_offset;
321681a5bbeSBoris Popov 		auio.uio_resid = rwrq->ioc_cnt;
322681a5bbeSBoris Popov 		auio.uio_segflg = UIO_USERSPACE;
323681a5bbeSBoris Popov 		auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
324681a5bbeSBoris Popov 		auio.uio_procp = p;
325681a5bbeSBoris Popov 		if (cmd == SMBIOC_READ)
326681a5bbeSBoris Popov 			error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
327681a5bbeSBoris Popov 		else
328681a5bbeSBoris Popov 			error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
329681a5bbeSBoris Popov 		rwrq->ioc_cnt -= auio.uio_resid;
330681a5bbeSBoris Popov 		break;
331681a5bbeSBoris Popov 	    }
332681a5bbeSBoris Popov 	    default:
333681a5bbeSBoris Popov 		error = ENODEV;
334681a5bbeSBoris Popov 	}
335681a5bbeSBoris Popov 	return error;
336681a5bbeSBoris Popov }
337681a5bbeSBoris Popov 
338681a5bbeSBoris Popov static int
339681a5bbeSBoris Popov nsmb_dev_read(dev_t dev, struct uio *uio, int flag)
340681a5bbeSBoris Popov {
341681a5bbeSBoris Popov 	return EACCES;
342681a5bbeSBoris Popov }
343681a5bbeSBoris Popov 
344681a5bbeSBoris Popov static int
345681a5bbeSBoris Popov nsmb_dev_write(dev_t dev, struct uio *uio, int flag)
346681a5bbeSBoris Popov {
347681a5bbeSBoris Popov 	return EACCES;
348681a5bbeSBoris Popov }
349681a5bbeSBoris Popov 
350681a5bbeSBoris Popov static int
351681a5bbeSBoris Popov nsmb_dev_poll(dev_t dev, int events, struct proc *p)
352681a5bbeSBoris Popov {
353681a5bbeSBoris Popov 	return ENODEV;
354681a5bbeSBoris Popov }
355681a5bbeSBoris Popov 
356681a5bbeSBoris Popov static int
357681a5bbeSBoris Popov nsmb_dev_load(module_t mod, int cmd, void *arg)
358681a5bbeSBoris Popov {
359681a5bbeSBoris Popov 	int error = 0;
360681a5bbeSBoris Popov 
361681a5bbeSBoris Popov 	switch (cmd) {
362681a5bbeSBoris Popov 	    case MOD_LOAD:
363681a5bbeSBoris Popov 		error = smb_sm_init();
364681a5bbeSBoris Popov 		if (error)
365681a5bbeSBoris Popov 			break;
366681a5bbeSBoris Popov 		error = smb_iod_init();
367681a5bbeSBoris Popov 		if (error) {
368681a5bbeSBoris Popov 			smb_sm_done();
369681a5bbeSBoris Popov 			break;
370681a5bbeSBoris Popov 		}
371681a5bbeSBoris Popov #if __FreeBSD_version > 400001
372681a5bbeSBoris Popov 		cdevsw_add(&nsmb_cdevsw);
373681a5bbeSBoris Popov #endif
374681a5bbeSBoris Popov #if __FreeBSD_version > 500000
375681a5bbeSBoris Popov 		nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000);
376681a5bbeSBoris Popov #endif
377681a5bbeSBoris Popov 		printf("netsmb_dev: loaded\n");
378681a5bbeSBoris Popov 		break;
379681a5bbeSBoris Popov 	    case MOD_UNLOAD:
380681a5bbeSBoris Popov 		smb_iod_done();
381681a5bbeSBoris Popov 		error = smb_sm_done();
382681a5bbeSBoris Popov 		error = 0;
383681a5bbeSBoris Popov #if __FreeBSD_version > 500000
384681a5bbeSBoris Popov 		EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag);
385681a5bbeSBoris Popov #endif
386681a5bbeSBoris Popov #if __FreeBSD_version > 400001
387681a5bbeSBoris Popov 		cdevsw_remove(&nsmb_cdevsw);
388681a5bbeSBoris Popov #endif
389681a5bbeSBoris Popov 		printf("netsmb_dev: unloaded\n");
390681a5bbeSBoris Popov 		break;
391681a5bbeSBoris Popov 	    default:
392681a5bbeSBoris Popov 		error = EINVAL;
393681a5bbeSBoris Popov 		break;
394681a5bbeSBoris Popov 	}
395681a5bbeSBoris Popov 	return error;
396681a5bbeSBoris Popov }
397681a5bbeSBoris Popov 
398681a5bbeSBoris Popov #if __FreeBSD_version > 400000
399681a5bbeSBoris Popov DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
400681a5bbeSBoris Popov #else
401681a5bbeSBoris Popov CDEV_MODULE(dev_netsmb, NSMB_MAJOR, nsmb_cdevsw, nsmb_dev_load, 0);
402681a5bbeSBoris Popov #endif
403681a5bbeSBoris Popov 
404681a5bbeSBoris Popov 
405681a5bbeSBoris Popov /*
406681a5bbeSBoris Popov  * Convert a file descriptor to appropriate smb_share pointer
407681a5bbeSBoris Popov  */
408681a5bbeSBoris Popov static struct file*
409681a5bbeSBoris Popov nsmb_getfp(struct filedesc* fdp, int fd, int flag)
410681a5bbeSBoris Popov {
411681a5bbeSBoris Popov 	struct file* fp;
412681a5bbeSBoris Popov 
413681a5bbeSBoris Popov 	if (((u_int)fd) >= fdp->fd_nfiles ||
414681a5bbeSBoris Popov 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
415681a5bbeSBoris Popov 	    (fp->f_flag & flag) == 0)
416681a5bbeSBoris Popov 		return (NULL);
417681a5bbeSBoris Popov 	return (fp);
418681a5bbeSBoris Popov }
419681a5bbeSBoris Popov 
420681a5bbeSBoris Popov int
421681a5bbeSBoris Popov smb_dev2share(int fd, int mode, struct smb_cred *scred,
422681a5bbeSBoris Popov 	struct smb_share **sspp)
423681a5bbeSBoris Popov {
424681a5bbeSBoris Popov 	struct file *fp;
425681a5bbeSBoris Popov 	struct vnode *vp;
426681a5bbeSBoris Popov 	struct smb_dev *sdp;
427681a5bbeSBoris Popov 	struct smb_share *ssp;
428681a5bbeSBoris Popov 	dev_t dev;
429681a5bbeSBoris Popov 	int error;
430681a5bbeSBoris Popov 
431681a5bbeSBoris Popov 	if ((fp = nsmb_getfp(scred->scr_p->p_fd, fd, FREAD | FWRITE)) == NULL)
432681a5bbeSBoris Popov 		return EBADF;
433681a5bbeSBoris Popov 	vp = (struct vnode*)fp->f_data;
434681a5bbeSBoris Popov 	if (vp == NULL)
435681a5bbeSBoris Popov 		return EBADF;
436681a5bbeSBoris Popov 	dev = vn_todev(vp);
437681a5bbeSBoris Popov 	if (dev == NODEV)
438681a5bbeSBoris Popov 		return EBADF;
439681a5bbeSBoris Popov 	SMB_CHECKMINOR(dev);
440681a5bbeSBoris Popov 	ssp = sdp->sd_share;
441681a5bbeSBoris Popov 	if (ssp == NULL)
442681a5bbeSBoris Popov 		return ENOTCONN;
443681a5bbeSBoris Popov 	error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
444681a5bbeSBoris Popov 	if (error)
445681a5bbeSBoris Popov 		return error;
446681a5bbeSBoris Popov 	*sspp = ssp;
447681a5bbeSBoris Popov 	return 0;
448681a5bbeSBoris Popov }
449681a5bbeSBoris Popov 
450