Deleted Added
full compact
smb_dev.c (82042) smb_dev.c (87192)
1/*
2 * Copyright (c) 2000-2001 Boris Popov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 *
1/*
2 * Copyright (c) 2000-2001 Boris Popov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: head/sys/netsmb/smb_dev.c 82042 2001-08-21 08:58:02Z bp $
32 * $FreeBSD: head/sys/netsmb/smb_dev.c 87192 2001-12-02 08:47:29Z bp $
33 */
34#include <sys/param.h>
35#include <sys/kernel.h>
36#include <sys/systm.h>
37#include <sys/conf.h>
38#include <sys/fcntl.h>
39#include <sys/ioccom.h>
40#include <sys/lock.h>
41#include <sys/malloc.h>
42#include <sys/file.h> /* Must come after sys/malloc.h */
43#include <sys/mbuf.h>
44#include <sys/poll.h>
45#include <sys/proc.h>
46#include <sys/select.h>
47#include <sys/socket.h>
48#include <sys/socketvar.h>
49#include <sys/sysctl.h>
50#include <sys/uio.h>
51#include <sys/vnode.h>
52
53#include <net/if.h>
54
55#include <netsmb/smb.h>
56#include <netsmb/smb_conn.h>
57#include <netsmb/smb_subr.h>
58#include <netsmb/smb_dev.h>
59
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; \
64 } while(0)
65
66static d_open_t nsmb_dev_open;
67static d_close_t nsmb_dev_close;
68static d_read_t nsmb_dev_read;
69static d_write_t nsmb_dev_write;
70static d_ioctl_t nsmb_dev_ioctl;
71static d_poll_t nsmb_dev_poll;
72
73MODULE_DEPEND(netsmb, libiconv, 1, 1, 1);
74MODULE_VERSION(netsmb, NSMB_VERSION);
75
76static int smb_version = NSMB_VERSION;
77
78
79SYSCTL_DECL(_net_smb);
80SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
81
82static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
83
84
85/*
86int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
87*/
88
89static struct cdevsw nsmb_cdevsw = {
90 /* open */ nsmb_dev_open,
91 /* close */ nsmb_dev_close,
92 /* read */ nsmb_dev_read,
93 /* write */ nsmb_dev_write,
94 /* ioctl */ nsmb_dev_ioctl,
95 /* poll */ nsmb_dev_poll,
96 /* mmap */ nommap,
97 /* strategy */ nostrategy,
98 /* name */ NSMB_NAME,
99 /* maj */ NSMB_MAJOR,
100 /* dump */ nodump,
101 /* psize */ nopsize,
102 /* flags */ 0,
103#ifndef FB_CURRENT
104 /* bmaj */ -1
105#endif
106};
107
108static eventhandler_tag nsmb_dev_tag;
109
110static void
111nsmb_dev_clone(void *arg, char *name, int namelen, dev_t *dev)
112{
113 int min;
114
115 if (*dev != NODEV)
116 return;
117 if (dev_stdclone(name, NULL, NSMB_NAME, &min) != 1)
118 return;
119 *dev = make_dev(&nsmb_cdevsw, min, 0, 0, 0600, NSMB_NAME"%d", min);
120}
121
122static int
33 */
34#include <sys/param.h>
35#include <sys/kernel.h>
36#include <sys/systm.h>
37#include <sys/conf.h>
38#include <sys/fcntl.h>
39#include <sys/ioccom.h>
40#include <sys/lock.h>
41#include <sys/malloc.h>
42#include <sys/file.h> /* Must come after sys/malloc.h */
43#include <sys/mbuf.h>
44#include <sys/poll.h>
45#include <sys/proc.h>
46#include <sys/select.h>
47#include <sys/socket.h>
48#include <sys/socketvar.h>
49#include <sys/sysctl.h>
50#include <sys/uio.h>
51#include <sys/vnode.h>
52
53#include <net/if.h>
54
55#include <netsmb/smb.h>
56#include <netsmb/smb_conn.h>
57#include <netsmb/smb_subr.h>
58#include <netsmb/smb_dev.h>
59
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; \
64 } while(0)
65
66static d_open_t nsmb_dev_open;
67static d_close_t nsmb_dev_close;
68static d_read_t nsmb_dev_read;
69static d_write_t nsmb_dev_write;
70static d_ioctl_t nsmb_dev_ioctl;
71static d_poll_t nsmb_dev_poll;
72
73MODULE_DEPEND(netsmb, libiconv, 1, 1, 1);
74MODULE_VERSION(netsmb, NSMB_VERSION);
75
76static int smb_version = NSMB_VERSION;
77
78
79SYSCTL_DECL(_net_smb);
80SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
81
82static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
83
84
85/*
86int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
87*/
88
89static struct cdevsw nsmb_cdevsw = {
90 /* open */ nsmb_dev_open,
91 /* close */ nsmb_dev_close,
92 /* read */ nsmb_dev_read,
93 /* write */ nsmb_dev_write,
94 /* ioctl */ nsmb_dev_ioctl,
95 /* poll */ nsmb_dev_poll,
96 /* mmap */ nommap,
97 /* strategy */ nostrategy,
98 /* name */ NSMB_NAME,
99 /* maj */ NSMB_MAJOR,
100 /* dump */ nodump,
101 /* psize */ nopsize,
102 /* flags */ 0,
103#ifndef FB_CURRENT
104 /* bmaj */ -1
105#endif
106};
107
108static eventhandler_tag nsmb_dev_tag;
109
110static void
111nsmb_dev_clone(void *arg, char *name, int namelen, dev_t *dev)
112{
113 int min;
114
115 if (*dev != NODEV)
116 return;
117 if (dev_stdclone(name, NULL, NSMB_NAME, &min) != 1)
118 return;
119 *dev = make_dev(&nsmb_cdevsw, min, 0, 0, 0600, NSMB_NAME"%d", min);
120}
121
122static int
123nsmb_dev_open(dev_t dev, int oflags, int devtype, struct proc *p)
123nsmb_dev_open(dev_t dev, int oflags, int devtype, struct thread *td)
124{
125 struct smb_dev *sdp;
124{
125 struct smb_dev *sdp;
126 struct proc *p = td->td_proc;
126 struct ucred *cred = p->p_ucred;
127 int s;
128
129 sdp = SMB_GETDEV(dev);
130 if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
131 return EBUSY;
132 if (sdp == NULL) {
133 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
134 dev->si_drv1 = (void*)sdp;
135 }
136 /*
137 * XXX: this is just crazy - make a device for an already passed device...
138 * someone should take care of it.
139 */
140 if ((dev->si_flags & SI_NAMED) == 0)
141 make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700,
142 NSMB_NAME"%d", dev2unit(dev));
143 bzero(sdp, sizeof(*sdp));
144/*
145 STAILQ_INIT(&sdp->sd_rqlist);
146 STAILQ_INIT(&sdp->sd_rplist);
147 bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
148*/
149 s = splimp();
150 sdp->sd_level = -1;
151 sdp->sd_flags |= NSMBFL_OPEN;
152 splx(s);
153 return 0;
154}
155
156static int
127 struct ucred *cred = p->p_ucred;
128 int s;
129
130 sdp = SMB_GETDEV(dev);
131 if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
132 return EBUSY;
133 if (sdp == NULL) {
134 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
135 dev->si_drv1 = (void*)sdp;
136 }
137 /*
138 * XXX: this is just crazy - make a device for an already passed device...
139 * someone should take care of it.
140 */
141 if ((dev->si_flags & SI_NAMED) == 0)
142 make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700,
143 NSMB_NAME"%d", dev2unit(dev));
144 bzero(sdp, sizeof(*sdp));
145/*
146 STAILQ_INIT(&sdp->sd_rqlist);
147 STAILQ_INIT(&sdp->sd_rplist);
148 bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
149*/
150 s = splimp();
151 sdp->sd_level = -1;
152 sdp->sd_flags |= NSMBFL_OPEN;
153 splx(s);
154 return 0;
155}
156
157static int
157nsmb_dev_close(dev_t dev, int flag, int fmt, struct proc *p)
158nsmb_dev_close(dev_t dev, int flag, int fmt, struct thread *td)
158{
159 struct smb_dev *sdp;
160 struct smb_vc *vcp;
161 struct smb_share *ssp;
162 struct smb_cred scred;
163 int s;
164
165 SMB_CHECKMINOR(dev);
166 s = splimp();
167 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
168 splx(s);
169 return EBADF;
170 }
159{
160 struct smb_dev *sdp;
161 struct smb_vc *vcp;
162 struct smb_share *ssp;
163 struct smb_cred scred;
164 int s;
165
166 SMB_CHECKMINOR(dev);
167 s = splimp();
168 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
169 splx(s);
170 return EBADF;
171 }
171 smb_makescred(&scred, p, NULL);
172 smb_makescred(&scred, td, NULL);
172 ssp = sdp->sd_share;
173 if (ssp != NULL)
174 smb_share_rele(ssp, &scred);
175 vcp = sdp->sd_vc;
176 if (vcp != NULL)
177 smb_vc_rele(vcp, &scred);
178/*
179 smb_flushq(&sdp->sd_rqlist);
180 smb_flushq(&sdp->sd_rplist);
181*/
182 dev->si_drv1 = NULL;
183 free(sdp, M_NSMBDEV);
184 destroy_dev(dev);
185 splx(s);
186 return 0;
187}
188
189
190static int
173 ssp = sdp->sd_share;
174 if (ssp != NULL)
175 smb_share_rele(ssp, &scred);
176 vcp = sdp->sd_vc;
177 if (vcp != NULL)
178 smb_vc_rele(vcp, &scred);
179/*
180 smb_flushq(&sdp->sd_rqlist);
181 smb_flushq(&sdp->sd_rplist);
182*/
183 dev->si_drv1 = NULL;
184 free(sdp, M_NSMBDEV);
185 destroy_dev(dev);
186 splx(s);
187 return 0;
188}
189
190
191static int
191nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
192nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
192{
193 struct smb_dev *sdp;
194 struct smb_vc *vcp;
195 struct smb_share *ssp;
196 struct smb_cred scred;
197 int error = 0;
198
199 SMB_CHECKMINOR(dev);
200 if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
201 return EBADF;
202
193{
194 struct smb_dev *sdp;
195 struct smb_vc *vcp;
196 struct smb_share *ssp;
197 struct smb_cred scred;
198 int error = 0;
199
200 SMB_CHECKMINOR(dev);
201 if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
202 return EBADF;
203
203 smb_makescred(&scred, p, NULL);
204 smb_makescred(&scred, td, NULL);
204 switch (cmd) {
205 case SMBIOC_OPENSESSION:
206 if (sdp->sd_vc)
207 return EISCONN;
208 error = smb_usr_opensession((struct smbioc_ossn*)data,
209 &scred, &vcp);
210 if (error)
211 break;
212 sdp->sd_vc = vcp;
205 switch (cmd) {
206 case SMBIOC_OPENSESSION:
207 if (sdp->sd_vc)
208 return EISCONN;
209 error = smb_usr_opensession((struct smbioc_ossn*)data,
210 &scred, &vcp);
211 if (error)
212 break;
213 sdp->sd_vc = vcp;
213 smb_vc_unlock(vcp, 0, p);
214 smb_vc_unlock(vcp, 0, td);
214 sdp->sd_level = SMBL_VC;
215 break;
216 case SMBIOC_OPENSHARE:
217 if (sdp->sd_share)
218 return EISCONN;
219 if (sdp->sd_vc == NULL)
220 return ENOTCONN;
221 error = smb_usr_openshare(sdp->sd_vc,
222 (struct smbioc_oshare*)data, &scred, &ssp);
223 if (error)
224 break;
225 sdp->sd_share = ssp;
215 sdp->sd_level = SMBL_VC;
216 break;
217 case SMBIOC_OPENSHARE:
218 if (sdp->sd_share)
219 return EISCONN;
220 if (sdp->sd_vc == NULL)
221 return ENOTCONN;
222 error = smb_usr_openshare(sdp->sd_vc,
223 (struct smbioc_oshare*)data, &scred, &ssp);
224 if (error)
225 break;
226 sdp->sd_share = ssp;
226 smb_share_unlock(ssp, 0, p);
227 smb_share_unlock(ssp, 0, td);
227 sdp->sd_level = SMBL_SHARE;
228 break;
229 case SMBIOC_REQUEST:
230 if (sdp->sd_share == NULL)
231 return ENOTCONN;
232 error = smb_usr_simplerequest(sdp->sd_share,
233 (struct smbioc_rq*)data, &scred);
234 break;
235 case SMBIOC_T2RQ:
236 if (sdp->sd_share == NULL)
237 return ENOTCONN;
238 error = smb_usr_t2request(sdp->sd_share,
239 (struct smbioc_t2rq*)data, &scred);
240 break;
241 case SMBIOC_SETFLAGS: {
242 struct smbioc_flags *fl = (struct smbioc_flags*)data;
243 int on;
244
245 if (fl->ioc_level == SMBL_VC) {
246 if (fl->ioc_mask & SMBV_PERMANENT) {
247 on = fl->ioc_flags & SMBV_PERMANENT;
248 if ((vcp = sdp->sd_vc) == NULL)
249 return ENOTCONN;
250 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
251 if (error)
252 break;
253 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
254 vcp->obj.co_flags |= SMBV_PERMANENT;
228 sdp->sd_level = SMBL_SHARE;
229 break;
230 case SMBIOC_REQUEST:
231 if (sdp->sd_share == NULL)
232 return ENOTCONN;
233 error = smb_usr_simplerequest(sdp->sd_share,
234 (struct smbioc_rq*)data, &scred);
235 break;
236 case SMBIOC_T2RQ:
237 if (sdp->sd_share == NULL)
238 return ENOTCONN;
239 error = smb_usr_t2request(sdp->sd_share,
240 (struct smbioc_t2rq*)data, &scred);
241 break;
242 case SMBIOC_SETFLAGS: {
243 struct smbioc_flags *fl = (struct smbioc_flags*)data;
244 int on;
245
246 if (fl->ioc_level == SMBL_VC) {
247 if (fl->ioc_mask & SMBV_PERMANENT) {
248 on = fl->ioc_flags & SMBV_PERMANENT;
249 if ((vcp = sdp->sd_vc) == NULL)
250 return ENOTCONN;
251 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
252 if (error)
253 break;
254 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
255 vcp->obj.co_flags |= SMBV_PERMANENT;
255 smb_vc_ref(vcp, p);
256 smb_vc_ref(vcp);
256 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
257 vcp->obj.co_flags &= ~SMBV_PERMANENT;
258 smb_vc_rele(vcp, &scred);
259 }
260 smb_vc_put(vcp, &scred);
261 } else
262 error = EINVAL;
263 } else if (fl->ioc_level == SMBL_SHARE) {
264 if (fl->ioc_mask & SMBS_PERMANENT) {
265 on = fl->ioc_flags & SMBS_PERMANENT;
266 if ((ssp = sdp->sd_share) == NULL)
267 return ENOTCONN;
268 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
269 if (error)
270 break;
271 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
272 ssp->obj.co_flags |= SMBS_PERMANENT;
257 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
258 vcp->obj.co_flags &= ~SMBV_PERMANENT;
259 smb_vc_rele(vcp, &scred);
260 }
261 smb_vc_put(vcp, &scred);
262 } else
263 error = EINVAL;
264 } else if (fl->ioc_level == SMBL_SHARE) {
265 if (fl->ioc_mask & SMBS_PERMANENT) {
266 on = fl->ioc_flags & SMBS_PERMANENT;
267 if ((ssp = sdp->sd_share) == NULL)
268 return ENOTCONN;
269 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
270 if (error)
271 break;
272 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
273 ssp->obj.co_flags |= SMBS_PERMANENT;
273 smb_share_ref(ssp, p);
274 smb_share_ref(ssp);
274 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
275 ssp->obj.co_flags &= ~SMBS_PERMANENT;
276 smb_share_rele(ssp, &scred);
277 }
278 smb_share_put(ssp, &scred);
279 } else
280 error = EINVAL;
281 break;
282 } else
283 error = EINVAL;
284 break;
285 }
286 case SMBIOC_LOOKUP:
287 if (sdp->sd_vc || sdp->sd_share)
288 return EISCONN;
289 vcp = NULL;
290 ssp = NULL;
291 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
292 if (error)
293 break;
294 if (vcp) {
295 sdp->sd_vc = vcp;
275 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
276 ssp->obj.co_flags &= ~SMBS_PERMANENT;
277 smb_share_rele(ssp, &scred);
278 }
279 smb_share_put(ssp, &scred);
280 } else
281 error = EINVAL;
282 break;
283 } else
284 error = EINVAL;
285 break;
286 }
287 case SMBIOC_LOOKUP:
288 if (sdp->sd_vc || sdp->sd_share)
289 return EISCONN;
290 vcp = NULL;
291 ssp = NULL;
292 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
293 if (error)
294 break;
295 if (vcp) {
296 sdp->sd_vc = vcp;
296 smb_vc_unlock(vcp, 0, p);
297 smb_vc_unlock(vcp, 0, td);
297 sdp->sd_level = SMBL_VC;
298 }
299 if (ssp) {
300 sdp->sd_share = ssp;
298 sdp->sd_level = SMBL_VC;
299 }
300 if (ssp) {
301 sdp->sd_share = ssp;
301 smb_share_unlock(ssp, 0, p);
302 smb_share_unlock(ssp, 0, td);
302 sdp->sd_level = SMBL_SHARE;
303 }
304 break;
305 case SMBIOC_READ: case SMBIOC_WRITE: {
306 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
307 struct uio auio;
308 struct iovec iov;
309
310 if ((ssp = sdp->sd_share) == NULL)
311 return ENOTCONN;
312 iov.iov_base = rwrq->ioc_base;
313 iov.iov_len = rwrq->ioc_cnt;
314 auio.uio_iov = &iov;
315 auio.uio_iovcnt = 1;
316 auio.uio_offset = rwrq->ioc_offset;
317 auio.uio_resid = rwrq->ioc_cnt;
318 auio.uio_segflg = UIO_USERSPACE;
319 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
303 sdp->sd_level = SMBL_SHARE;
304 }
305 break;
306 case SMBIOC_READ: case SMBIOC_WRITE: {
307 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
308 struct uio auio;
309 struct iovec iov;
310
311 if ((ssp = sdp->sd_share) == NULL)
312 return ENOTCONN;
313 iov.iov_base = rwrq->ioc_base;
314 iov.iov_len = rwrq->ioc_cnt;
315 auio.uio_iov = &iov;
316 auio.uio_iovcnt = 1;
317 auio.uio_offset = rwrq->ioc_offset;
318 auio.uio_resid = rwrq->ioc_cnt;
319 auio.uio_segflg = UIO_USERSPACE;
320 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
320 auio.uio_procp = p;
321 auio.uio_td = td;
321 if (cmd == SMBIOC_READ)
322 error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
323 else
324 error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
325 rwrq->ioc_cnt -= auio.uio_resid;
326 break;
327 }
328 default:
329 error = ENODEV;
330 }
331 return error;
332}
333
334static int
335nsmb_dev_read(dev_t dev, struct uio *uio, int flag)
336{
337 return EACCES;
338}
339
340static int
341nsmb_dev_write(dev_t dev, struct uio *uio, int flag)
342{
343 return EACCES;
344}
345
346static int
322 if (cmd == SMBIOC_READ)
323 error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
324 else
325 error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
326 rwrq->ioc_cnt -= auio.uio_resid;
327 break;
328 }
329 default:
330 error = ENODEV;
331 }
332 return error;
333}
334
335static int
336nsmb_dev_read(dev_t dev, struct uio *uio, int flag)
337{
338 return EACCES;
339}
340
341static int
342nsmb_dev_write(dev_t dev, struct uio *uio, int flag)
343{
344 return EACCES;
345}
346
347static int
347nsmb_dev_poll(dev_t dev, int events, struct proc *p)
348nsmb_dev_poll(dev_t dev, int events, struct thread *td)
348{
349 return ENODEV;
350}
351
352static int
353nsmb_dev_load(module_t mod, int cmd, void *arg)
354{
355 int error = 0;
356
357 switch (cmd) {
358 case MOD_LOAD:
359 error = smb_sm_init();
360 if (error)
361 break;
362 error = smb_iod_init();
363 if (error) {
364 smb_sm_done();
365 break;
366 }
367 cdevsw_add(&nsmb_cdevsw);
368 nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000);
369 printf("netsmb_dev: loaded\n");
370 break;
371 case MOD_UNLOAD:
372 smb_iod_done();
373 error = smb_sm_done();
374 error = 0;
375 EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag);
376 cdevsw_remove(&nsmb_cdevsw);
377 printf("netsmb_dev: unloaded\n");
378 break;
379 default:
380 error = EINVAL;
381 break;
382 }
383 return error;
384}
385
386DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
387
388/*
389 * Convert a file descriptor to appropriate smb_share pointer
390 */
391static struct file*
392nsmb_getfp(struct filedesc* fdp, int fd, int flag)
393{
394 struct file* fp;
395
396 if (((u_int)fd) >= fdp->fd_nfiles ||
397 (fp = fdp->fd_ofiles[fd]) == NULL ||
398 (fp->f_flag & flag) == 0)
399 return (NULL);
400 return (fp);
401}
402
403int
404smb_dev2share(int fd, int mode, struct smb_cred *scred,
405 struct smb_share **sspp)
406{
407 struct file *fp;
408 struct vnode *vp;
409 struct smb_dev *sdp;
410 struct smb_share *ssp;
411 dev_t dev;
412 int error;
413
349{
350 return ENODEV;
351}
352
353static int
354nsmb_dev_load(module_t mod, int cmd, void *arg)
355{
356 int error = 0;
357
358 switch (cmd) {
359 case MOD_LOAD:
360 error = smb_sm_init();
361 if (error)
362 break;
363 error = smb_iod_init();
364 if (error) {
365 smb_sm_done();
366 break;
367 }
368 cdevsw_add(&nsmb_cdevsw);
369 nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000);
370 printf("netsmb_dev: loaded\n");
371 break;
372 case MOD_UNLOAD:
373 smb_iod_done();
374 error = smb_sm_done();
375 error = 0;
376 EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag);
377 cdevsw_remove(&nsmb_cdevsw);
378 printf("netsmb_dev: unloaded\n");
379 break;
380 default:
381 error = EINVAL;
382 break;
383 }
384 return error;
385}
386
387DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
388
389/*
390 * Convert a file descriptor to appropriate smb_share pointer
391 */
392static struct file*
393nsmb_getfp(struct filedesc* fdp, int fd, int flag)
394{
395 struct file* fp;
396
397 if (((u_int)fd) >= fdp->fd_nfiles ||
398 (fp = fdp->fd_ofiles[fd]) == NULL ||
399 (fp->f_flag & flag) == 0)
400 return (NULL);
401 return (fp);
402}
403
404int
405smb_dev2share(int fd, int mode, struct smb_cred *scred,
406 struct smb_share **sspp)
407{
408 struct file *fp;
409 struct vnode *vp;
410 struct smb_dev *sdp;
411 struct smb_share *ssp;
412 dev_t dev;
413 int error;
414
414 if ((fp = nsmb_getfp(scred->scr_p->p_fd, fd, FREAD | FWRITE)) == NULL)
415 fp = nsmb_getfp(scred->scr_td->td_proc->p_fd, fd, FREAD | FWRITE);
416 if (fp == NULL)
415 return EBADF;
416 vp = (struct vnode*)fp->f_data;
417 if (vp == NULL)
418 return EBADF;
419 dev = vn_todev(vp);
420 if (dev == NODEV)
421 return EBADF;
422 SMB_CHECKMINOR(dev);
423 ssp = sdp->sd_share;
424 if (ssp == NULL)
425 return ENOTCONN;
426 error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
427 if (error)
428 return error;
429 *sspp = ssp;
430 return 0;
431}
432
417 return EBADF;
418 vp = (struct vnode*)fp->f_data;
419 if (vp == NULL)
420 return EBADF;
421 dev = vn_todev(vp);
422 if (dev == NODEV)
423 return EBADF;
424 SMB_CHECKMINOR(dev);
425 ssp = sdp->sd_share;
426 if (ssp == NULL)
427 return ENOTCONN;
428 error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
429 if (error)
430 return error;
431 *sspp = ssp;
432 return 0;
433}
434