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