| 1 | /* $NetBSD: smbfs_io.c,v 1.34 2010/04/23 15:38:47 pooka Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2000-2001, Boris Popov |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. All advertising materials mentioning features or use of this software |
| 16 | * must display the following acknowledgement: |
| 17 | * This product includes software developed by Boris Popov. |
| 18 | * 4. Neither the name of the author nor the names of any co-contributors |
| 19 | * may be used to endorse or promote products derived from this software |
| 20 | * without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 | * SUCH DAMAGE. |
| 33 | * |
| 34 | * FreeBSD: src/sys/fs/smbfs/smbfs_io.c,v 1.7 2001/12/02 08:56:58 bp Exp |
| 35 | * |
| 36 | */ |
| 37 | |
| 38 | #include <sys/cdefs.h> |
| 39 | __KERNEL_RCSID(0, "$NetBSD: smbfs_io.c,v 1.34 2010/04/23 15:38:47 pooka Exp $" ); |
| 40 | |
| 41 | #include <sys/param.h> |
| 42 | #include <sys/systm.h> |
| 43 | #include <sys/resourcevar.h> /* defines plimit structure in proc struct */ |
| 44 | #include <sys/kernel.h> |
| 45 | #include <sys/proc.h> |
| 46 | #include <sys/fcntl.h> |
| 47 | #include <sys/buf.h> |
| 48 | #include <sys/mount.h> |
| 49 | #include <sys/malloc.h> |
| 50 | #include <sys/namei.h> |
| 51 | #include <sys/vnode.h> |
| 52 | #include <sys/dirent.h> |
| 53 | #include <sys/signalvar.h> |
| 54 | #include <sys/sysctl.h> |
| 55 | #include <sys/vmmeter.h> |
| 56 | #include <sys/kauth.h> |
| 57 | |
| 58 | #ifndef __NetBSD__ |
| 59 | #include <vm/vm.h> |
| 60 | #if __FreeBSD_version < 400000 |
| 61 | #include <vm/vm_prot.h> |
| 62 | #endif |
| 63 | #include <vm/vm_page.h> |
| 64 | #include <vm/vm_extern.h> |
| 65 | #include <vm/vm_object.h> |
| 66 | #include <vm/vm_pager.h> |
| 67 | #include <vm/vnode_pager.h> |
| 68 | #else /* __NetBSD__ */ |
| 69 | #include <uvm/uvm.h> |
| 70 | #include <uvm/uvm_extern.h> |
| 71 | #endif /* __NetBSD__ */ |
| 72 | |
| 73 | /* |
| 74 | #include <sys/ioccom.h> |
| 75 | */ |
| 76 | #include <netsmb/smb.h> |
| 77 | #include <netsmb/smb_conn.h> |
| 78 | #include <netsmb/smb_subr.h> |
| 79 | |
| 80 | #include <fs/smbfs/smbfs.h> |
| 81 | #include <fs/smbfs/smbfs_node.h> |
| 82 | #include <fs/smbfs/smbfs_subr.h> |
| 83 | |
| 84 | /*#define SMBFS_RWGENERIC*/ |
| 85 | |
| 86 | #define DE_SIZE (sizeof(struct dirent)) |
| 87 | |
| 88 | static int |
| 89 | smbfs_readvdir(struct vnode *vp, struct uio *uio, kauth_cred_t cred) |
| 90 | { |
| 91 | struct dirent *de; |
| 92 | struct smb_cred scred; |
| 93 | struct smbfs_fctx *ctx; |
| 94 | struct smbnode *np = VTOSMB(vp); |
| 95 | int error = 0/*, *eofflag = ap->a_eofflag*/; |
| 96 | long offset, limit; |
| 97 | |
| 98 | KASSERT(vp->v_type == VDIR); |
| 99 | |
| 100 | if (uio->uio_resid < DE_SIZE || uio->uio_offset < 0) |
| 101 | return EINVAL; |
| 102 | |
| 103 | de = malloc(DE_SIZE, M_SMBFSDATA, M_WAITOK | M_ZERO); |
| 104 | |
| 105 | SMBVDEBUG("dirname='%.*s'\n" , (int) np->n_nmlen, np->n_name); |
| 106 | smb_makescred(&scred, curlwp, cred); |
| 107 | offset = uio->uio_offset / DE_SIZE; /* offset in the directory */ |
| 108 | limit = uio->uio_resid / DE_SIZE; |
| 109 | |
| 110 | /* Simulate . */ |
| 111 | if (offset < 1) { |
| 112 | memset(de, 0, DE_SIZE); |
| 113 | de->d_fileno = np->n_ino; |
| 114 | de->d_reclen = DE_SIZE; |
| 115 | de->d_type = DT_DIR; |
| 116 | de->d_namlen = 1; |
| 117 | strncpy(de->d_name, "." , 2); |
| 118 | error = uiomove(de, DE_SIZE, uio); |
| 119 | if (error) |
| 120 | goto out; |
| 121 | limit--; |
| 122 | offset++; |
| 123 | } |
| 124 | /* Simulate .. */ |
| 125 | if (limit > 0 && offset < 2) { |
| 126 | memset(de, 0, DE_SIZE); |
| 127 | de->d_fileno = (np->n_parent ? VTOSMB(np->n_parent)->n_ino : 2); |
| 128 | de->d_reclen = DE_SIZE; |
| 129 | de->d_type = DT_DIR; |
| 130 | de->d_namlen = 2; |
| 131 | strncpy(de->d_name, ".." , 3); |
| 132 | error = uiomove(de, DE_SIZE, uio); |
| 133 | if (error) |
| 134 | goto out; |
| 135 | limit--; |
| 136 | offset++; |
| 137 | } |
| 138 | |
| 139 | if (limit == 0) |
| 140 | goto out; |
| 141 | |
| 142 | if (offset != np->n_dirofs || np->n_dirseq == NULL) { |
| 143 | SMBVDEBUG("Reopening search %ld:%ld\n" , offset, np->n_dirofs); |
| 144 | if (np->n_dirseq) { |
| 145 | smbfs_findclose(np->n_dirseq, &scred); |
| 146 | np->n_dirseq = NULL; |
| 147 | } |
| 148 | np->n_dirofs = 2; |
| 149 | error = smbfs_findopen(np, "*" , 1, |
| 150 | SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR, |
| 151 | &scred, &ctx); |
| 152 | if (error) { |
| 153 | SMBVDEBUG("can not open search, error = %d" , error); |
| 154 | goto out; |
| 155 | } |
| 156 | np->n_dirseq = ctx; |
| 157 | } else |
| 158 | ctx = np->n_dirseq; |
| 159 | |
| 160 | /* skip entries before offset */ |
| 161 | while (np->n_dirofs < offset) { |
| 162 | error = smbfs_findnext(ctx, offset - np->n_dirofs++, &scred); |
| 163 | if (error) { |
| 164 | smbfs_findclose(np->n_dirseq, &scred); |
| 165 | np->n_dirseq = NULL; |
| 166 | error = (error == ENOENT) ? 0 : error; |
| 167 | goto out; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | for (; limit; limit--, offset++) { |
| 172 | error = smbfs_findnext(ctx, limit, &scred); |
| 173 | if (error) { |
| 174 | if (error == ENOENT) |
| 175 | error = 0; |
| 176 | break; |
| 177 | } |
| 178 | np->n_dirofs++; |
| 179 | memset(de, 0, DE_SIZE); |
| 180 | de->d_reclen = DE_SIZE; |
| 181 | de->d_fileno = ctx->f_attr.fa_ino; |
| 182 | de->d_type = (ctx->f_attr.fa_attr & SMB_FA_DIR) ? |
| 183 | DT_DIR : DT_REG; |
| 184 | de->d_namlen = ctx->f_nmlen; |
| 185 | memcpy(de->d_name, ctx->f_name, de->d_namlen); |
| 186 | de->d_name[de->d_namlen] = '\0'; |
| 187 | error = uiomove(de, DE_SIZE, uio); |
| 188 | if (error) |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | out: |
| 193 | free(de, M_SMBFSDATA); |
| 194 | return (error); |
| 195 | } |
| 196 | |
| 197 | int |
| 198 | smbfs_readvnode(struct vnode *vp, struct uio *uiop, kauth_cred_t cred) |
| 199 | { |
| 200 | struct smbmount *smp = VFSTOSMBFS(vp->v_mount); |
| 201 | struct smbnode *np = VTOSMB(vp); |
| 202 | struct lwp *l = curlwp; |
| 203 | struct vattr vattr; |
| 204 | struct smb_cred scred; |
| 205 | int error; |
| 206 | |
| 207 | KASSERT(vp->v_type == VREG || vp->v_type == VDIR); |
| 208 | |
| 209 | if (uiop->uio_resid == 0) |
| 210 | return 0; |
| 211 | if (uiop->uio_offset < 0) |
| 212 | return EINVAL; |
| 213 | /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize) |
| 214 | return EFBIG;*/ |
| 215 | if (vp->v_type == VDIR) { |
| 216 | error = smbfs_readvdir(vp, uiop, cred); |
| 217 | return error; |
| 218 | } |
| 219 | |
| 220 | if (np->n_flag & NMODIFIED) { |
| 221 | smbfs_attr_cacheremove(vp); |
| 222 | error = VOP_GETATTR(vp, &vattr, cred); |
| 223 | if (error) |
| 224 | return error; |
| 225 | np->n_mtime.tv_sec = vattr.va_mtime.tv_sec; |
| 226 | } else { |
| 227 | error = VOP_GETATTR(vp, &vattr, cred); |
| 228 | if (error) |
| 229 | return error; |
| 230 | if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) { |
| 231 | error = smbfs_vinvalbuf(vp, V_SAVE, cred, l, 1); |
| 232 | if (error) |
| 233 | return error; |
| 234 | np->n_mtime.tv_sec = vattr.va_mtime.tv_sec; |
| 235 | } |
| 236 | } |
| 237 | smb_makescred(&scred, l, cred); |
| 238 | return smb_read(smp->sm_share, np->n_fid, uiop, &scred); |
| 239 | } |
| 240 | |
| 241 | int |
| 242 | smbfs_writevnode(struct vnode *vp, struct uio *uiop, |
| 243 | kauth_cred_t cred, int ioflag) |
| 244 | { |
| 245 | struct smbmount *smp = VTOSMBFS(vp); |
| 246 | struct smbnode *np = VTOSMB(vp); |
| 247 | struct lwp *l = curlwp; |
| 248 | struct smb_cred scred; |
| 249 | int error = 0; |
| 250 | int extended = 0; |
| 251 | size_t resid = uiop->uio_resid; |
| 252 | |
| 253 | /* vn types other than VREG unsupported */ |
| 254 | KASSERT(vp->v_type == VREG); |
| 255 | |
| 256 | SMBVDEBUG("ofs=%lld,resid=%zu\n" , |
| 257 | (long long int) uiop->uio_offset, |
| 258 | uiop->uio_resid); |
| 259 | if (uiop->uio_offset < 0) |
| 260 | return EINVAL; |
| 261 | /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize) |
| 262 | return (EFBIG);*/ |
| 263 | if (ioflag & (IO_APPEND | IO_SYNC)) { |
| 264 | if (np->n_flag & NMODIFIED) { |
| 265 | smbfs_attr_cacheremove(vp); |
| 266 | error = smbfs_vinvalbuf(vp, V_SAVE, cred, l, 1); |
| 267 | if (error) |
| 268 | return error; |
| 269 | } |
| 270 | if (ioflag & IO_APPEND) { |
| 271 | #if notyet |
| 272 | struct proc *p = curproc; |
| 273 | |
| 274 | /* |
| 275 | * File size can be changed by another client |
| 276 | */ |
| 277 | smbfs_attr_cacheremove(vp); |
| 278 | error = VOP_GETATTR(vp, &vattr, cred, td); |
| 279 | if (error) |
| 280 | return (error); |
| 281 | if (np->n_size + uiop->uio_resid > |
| 282 | p->p_rlimit[RLIMIT_FSIZE].rlim_cur) { |
| 283 | mutex_enter(proc_lock); |
| 284 | psignal(p, SIGXFSZ); |
| 285 | mutex_exit(proc_lock); |
| 286 | return EFBIG; |
| 287 | } |
| 288 | #endif |
| 289 | uiop->uio_offset = np->n_size; |
| 290 | } |
| 291 | } |
| 292 | if (uiop->uio_resid == 0) |
| 293 | return 0; |
| 294 | smb_makescred(&scred, l, cred); |
| 295 | error = smb_write(smp->sm_share, np->n_fid, uiop, &scred); |
| 296 | SMBVDEBUG("after: ofs=%lld,resid=%zu,err=%d\n" , |
| 297 | (long long int)uiop->uio_offset, uiop->uio_resid, error); |
| 298 | if (!error) { |
| 299 | if (uiop->uio_offset > np->n_size) { |
| 300 | np->n_size = uiop->uio_offset; |
| 301 | uvm_vnp_setsize(vp, np->n_size); |
| 302 | extended = 1; |
| 303 | } |
| 304 | |
| 305 | } |
| 306 | if (resid > uiop->uio_resid) |
| 307 | VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0)); |
| 308 | return error; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Do an I/O operation to/from a cache block. |
| 313 | */ |
| 314 | int |
| 315 | smbfs_doio(struct buf *bp, kauth_cred_t cr, struct lwp *l) |
| 316 | { |
| 317 | struct vnode *vp = bp->b_vp; |
| 318 | struct smbmount *smp = VFSTOSMBFS(vp->v_mount); |
| 319 | struct smbnode *np = VTOSMB(vp); |
| 320 | struct uio uio, *uiop = &uio; |
| 321 | struct iovec io; |
| 322 | struct smb_cred scred; |
| 323 | int error = 0; |
| 324 | |
| 325 | uiop->uio_iov = &io; |
| 326 | uiop->uio_iovcnt = 1; |
| 327 | UIO_SETUP_SYSSPACE(uiop); |
| 328 | |
| 329 | smb_makescred(&scred, l, cr); |
| 330 | |
| 331 | if (bp->b_flags == B_READ) { |
| 332 | io.iov_len = uiop->uio_resid = bp->b_bcount; |
| 333 | io.iov_base = bp->b_data; |
| 334 | uiop->uio_rw = UIO_READ; |
| 335 | switch (vp->v_type) { |
| 336 | case VREG: |
| 337 | uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; |
| 338 | error = smb_read(smp->sm_share, np->n_fid, uiop, &scred); |
| 339 | if (error) |
| 340 | break; |
| 341 | if (uiop->uio_resid) { |
| 342 | int left = uiop->uio_resid; |
| 343 | int nread = bp->b_bcount - left; |
| 344 | if (left > 0) |
| 345 | memset((char *)bp->b_data + nread, 0, left); |
| 346 | } |
| 347 | break; |
| 348 | default: |
| 349 | printf("smbfs_doio: type %x unexpected\n" ,vp->v_type); |
| 350 | break; |
| 351 | }; |
| 352 | if (error) { |
| 353 | bp->b_error = error; |
| 354 | } |
| 355 | } else { /* write */ |
| 356 | io.iov_len = uiop->uio_resid = bp->b_bcount; |
| 357 | uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT; |
| 358 | io.iov_base = bp->b_data; |
| 359 | uiop->uio_rw = UIO_WRITE; |
| 360 | bp->b_cflags |= BC_BUSY; |
| 361 | error = smb_write(smp->sm_share, np->n_fid, uiop, &scred); |
| 362 | bp->b_cflags &= ~BC_BUSY; |
| 363 | |
| 364 | |
| 365 | #ifndef __NetBSD__ /* XXX */ |
| 366 | /* |
| 367 | * For an interrupted write, the buffer is still valid |
| 368 | * and the write hasn't been pushed to the server yet, |
| 369 | * so we can't set BIO_ERROR and report the interruption |
| 370 | * by setting B_EINTR. For the B_ASYNC case, B_EINTR |
| 371 | * is not relevant, so the rpc attempt is essentially |
| 372 | * a noop. For the case of a V3 write rpc not being |
| 373 | * committed to stable storage, the block is still |
| 374 | * dirty and requires either a commit rpc or another |
| 375 | * write rpc with iomode == NFSV3WRITE_FILESYNC before |
| 376 | * the block is reused. This is indicated by setting |
| 377 | * the B_DELWRI and B_NEEDCOMMIT flags. |
| 378 | */ |
| 379 | if (error == EINTR |
| 380 | || (!error && (bp->b_flags & B_NEEDCOMMIT))) { |
| 381 | int s; |
| 382 | |
| 383 | s = splbio(); |
| 384 | bp->b_flags &= ~(B_INVAL|B_NOCACHE); |
| 385 | if ((bp->b_flags & B_ASYNC) == 0) |
| 386 | bp->b_flags |= B_EINTR; |
| 387 | if ((bp->b_flags & B_PAGING) == 0) { |
| 388 | bdirty(bp); |
| 389 | bp->b_flags &= ~B_DONE; |
| 390 | } |
| 391 | if ((bp->b_flags & B_ASYNC) == 0) |
| 392 | bp->b_flags |= B_EINTR; |
| 393 | splx(s); |
| 394 | } else { |
| 395 | if (error) { |
| 396 | bp->b_ioflags |= BIO_ERROR; |
| 397 | bp->b_error = error; |
| 398 | } |
| 399 | bp->b_dirtyoff = bp->b_dirtyend = 0; |
| 400 | } |
| 401 | #endif /* !__NetBSD__ */ |
| 402 | } |
| 403 | bp->b_resid = uiop->uio_resid; |
| 404 | biodone(bp); |
| 405 | return error; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Flush and invalidate all dirty buffers. If another process is already |
| 410 | * doing the flush, just wait for completion. |
| 411 | */ |
| 412 | int |
| 413 | smbfs_vinvalbuf(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l, int intrflg) |
| 414 | { |
| 415 | struct smbnode *np = VTOSMB(vp); |
| 416 | int error = 0, slpflag; |
| 417 | |
| 418 | if (intrflg) |
| 419 | slpflag = PCATCH; |
| 420 | else |
| 421 | slpflag = 0; |
| 422 | |
| 423 | while (np->n_flag & NFLUSHINPROG) { |
| 424 | np->n_flag |= NFLUSHWANT; |
| 425 | error = tsleep(&np->n_flag, |
| 426 | (PRIBIO + 2) | slpflag, "smfsvinv" , 0); |
| 427 | if (error) |
| 428 | return (error); |
| 429 | } |
| 430 | np->n_flag |= NFLUSHINPROG; |
| 431 | for(;;) { |
| 432 | if ((error = vinvalbuf(vp, flags, cred, l, slpflag, 0)) == 0) |
| 433 | break; |
| 434 | |
| 435 | if (intrflg && (error == ERESTART || error == EINTR)) { |
| 436 | np->n_flag &= ~NFLUSHINPROG; |
| 437 | if (np->n_flag & NFLUSHWANT) { |
| 438 | np->n_flag &= ~NFLUSHWANT; |
| 439 | wakeup(&np->n_flag); |
| 440 | } |
| 441 | return (error); |
| 442 | } |
| 443 | } |
| 444 | np->n_flag &= ~(NMODIFIED | NFLUSHINPROG); |
| 445 | if (np->n_flag & NFLUSHWANT) { |
| 446 | np->n_flag &= ~NFLUSHWANT; |
| 447 | wakeup(&np->n_flag); |
| 448 | } |
| 449 | return (error); |
| 450 | } |
| 451 | |