No empty .Rs/.Re
[netbsd-mini2440.git] / sys / fs / smbfs / smbfs_io.c
blob3fcced9405e9219236cf6e437874e9426d2d94c2
1 /* $NetBSD: smbfs_io.c,v 1.32 2009/03/18 16:00:21 cegger Exp $ */
3 /*
4 * Copyright (c) 2000-2001, Boris Popov
5 * All rights reserved.
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.
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.
34 * FreeBSD: src/sys/fs/smbfs/smbfs_io.c,v 1.7 2001/12/02 08:56:58 bp Exp
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: smbfs_io.c,v 1.32 2009/03/18 16:00:21 cegger Exp $");
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>
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__ */
74 #include <sys/ioccom.h>
76 #include <netsmb/smb.h>
77 #include <netsmb/smb_conn.h>
78 #include <netsmb/smb_subr.h>
80 #include <fs/smbfs/smbfs.h>
81 #include <fs/smbfs/smbfs_node.h>
82 #include <fs/smbfs/smbfs_subr.h>
84 /*#define SMBFS_RWGENERIC*/
86 #define DE_SIZE (sizeof(struct dirent))
88 static int
89 smbfs_readvdir(struct vnode *vp, struct uio *uio, kauth_cred_t cred)
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;
98 KASSERT(vp->v_type == VDIR);
100 if (uio->uio_resid < DE_SIZE || uio->uio_offset < 0)
101 return EINVAL;
103 de = malloc(DE_SIZE, M_SMBFSDATA, M_WAITOK | M_ZERO);
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;
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++;
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++;
139 if (limit == 0)
140 goto out;
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;
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;
156 np->n_dirseq = ctx;
157 } else
158 ctx = np->n_dirseq;
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;
171 for (; limit; limit--, offset++) {
172 error = smbfs_findnext(ctx, limit, &scred);
173 if (error) {
174 if (error == ENOENT)
175 error = 0;
176 break;
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;
192 out:
193 free(de, M_SMBFSDATA);
194 return (error);
198 smbfs_readvnode(struct vnode *vp, struct uio *uiop, kauth_cred_t cred)
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;
207 KASSERT(vp->v_type == VREG || vp->v_type == VDIR);
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;
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;
237 smb_makescred(&scred, l, cred);
238 return smb_read(smp->sm_share, np->n_fid, uiop, &scred);
242 smbfs_writevnode(struct vnode *vp, struct uio *uiop,
243 kauth_cred_t cred, int ioflag)
245 struct smbmount *smp = VTOSMBFS(vp);
246 struct smbnode *np = VTOSMB(vp);
247 struct smb_cred scred;
248 struct lwp *l = curlwp;
249 struct proc *p = l->l_proc;
250 int error = 0;
251 int extended = 0;
252 size_t resid = uiop->uio_resid;
254 /* vn types other than VREG unsupported */
255 KASSERT(vp->v_type == VREG);
257 SMBVDEBUG("ofs=%lld,resid=%zu\n",
258 (long long int) uiop->uio_offset,
259 uiop->uio_resid);
260 if (uiop->uio_offset < 0)
261 return EINVAL;
262 /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
263 return (EFBIG);*/
264 if (ioflag & (IO_APPEND | IO_SYNC)) {
265 if (np->n_flag & NMODIFIED) {
266 smbfs_attr_cacheremove(vp);
267 error = smbfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
268 if (error)
269 return error;
271 if (ioflag & IO_APPEND) {
272 #if notyet
274 * File size can be changed by another client
276 smbfs_attr_cacheremove(vp);
277 error = VOP_GETATTR(vp, &vattr, cred, td);
278 if (error)
279 return (error);
280 #endif
281 uiop->uio_offset = np->n_size;
284 if (uiop->uio_resid == 0)
285 return 0;
286 if (p && uiop->uio_offset + uiop->uio_resid > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
287 mutex_enter(proc_lock);
288 psignal(p, SIGXFSZ);
289 mutex_exit(proc_lock);
290 return EFBIG;
292 smb_makescred(&scred, l, cred);
293 error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
294 SMBVDEBUG("after: ofs=%lld,resid=%zu,err=%d\n",
295 (long long int)uiop->uio_offset, uiop->uio_resid, error);
296 if (!error) {
297 if (uiop->uio_offset > np->n_size) {
298 np->n_size = uiop->uio_offset;
299 uvm_vnp_setsize(vp, np->n_size);
300 extended = 1;
304 if (resid > uiop->uio_resid)
305 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
306 return error;
310 * Do an I/O operation to/from a cache block.
313 smbfs_doio(struct buf *bp, kauth_cred_t cr, struct lwp *l)
315 struct vnode *vp = bp->b_vp;
316 struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
317 struct smbnode *np = VTOSMB(vp);
318 struct uio uio, *uiop = &uio;
319 struct iovec io;
320 struct smb_cred scred;
321 int error = 0;
323 uiop->uio_iov = &io;
324 uiop->uio_iovcnt = 1;
325 UIO_SETUP_SYSSPACE(uiop);
327 smb_makescred(&scred, l, cr);
329 if (bp->b_flags == B_READ) {
330 io.iov_len = uiop->uio_resid = bp->b_bcount;
331 io.iov_base = bp->b_data;
332 uiop->uio_rw = UIO_READ;
333 switch (vp->v_type) {
334 case VREG:
335 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
336 error = smb_read(smp->sm_share, np->n_fid, uiop, &scred);
337 if (error)
338 break;
339 if (uiop->uio_resid) {
340 int left = uiop->uio_resid;
341 int nread = bp->b_bcount - left;
342 if (left > 0)
343 memset((char *)bp->b_data + nread, 0, left);
345 break;
346 default:
347 printf("smbfs_doio: type %x unexpected\n",vp->v_type);
348 break;
350 if (error) {
351 bp->b_error = error;
353 } else { /* write */
354 io.iov_len = uiop->uio_resid = bp->b_bcount;
355 uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
356 io.iov_base = bp->b_data;
357 uiop->uio_rw = UIO_WRITE;
358 bp->b_cflags |= BC_BUSY;
359 error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
360 bp->b_cflags &= ~BC_BUSY;
363 #ifndef __NetBSD__ /* XXX */
365 * For an interrupted write, the buffer is still valid
366 * and the write hasn't been pushed to the server yet,
367 * so we can't set BIO_ERROR and report the interruption
368 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
369 * is not relevant, so the rpc attempt is essentially
370 * a noop. For the case of a V3 write rpc not being
371 * committed to stable storage, the block is still
372 * dirty and requires either a commit rpc or another
373 * write rpc with iomode == NFSV3WRITE_FILESYNC before
374 * the block is reused. This is indicated by setting
375 * the B_DELWRI and B_NEEDCOMMIT flags.
377 if (error == EINTR
378 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
379 int s;
381 s = splbio();
382 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
383 if ((bp->b_flags & B_ASYNC) == 0)
384 bp->b_flags |= B_EINTR;
385 if ((bp->b_flags & B_PAGING) == 0) {
386 bdirty(bp);
387 bp->b_flags &= ~B_DONE;
389 if ((bp->b_flags & B_ASYNC) == 0)
390 bp->b_flags |= B_EINTR;
391 splx(s);
392 } else {
393 if (error) {
394 bp->b_ioflags |= BIO_ERROR;
395 bp->b_error = error;
397 bp->b_dirtyoff = bp->b_dirtyend = 0;
399 #endif /* !__NetBSD__ */
401 bp->b_resid = uiop->uio_resid;
402 biodone(bp);
403 return error;
407 * Flush and invalidate all dirty buffers. If another process is already
408 * doing the flush, just wait for completion.
411 smbfs_vinvalbuf(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l, int intrflg)
413 struct smbnode *np = VTOSMB(vp);
414 int error = 0, slpflag;
416 if (intrflg)
417 slpflag = PCATCH;
418 else
419 slpflag = 0;
421 while (np->n_flag & NFLUSHINPROG) {
422 np->n_flag |= NFLUSHWANT;
423 error = tsleep(&np->n_flag,
424 (PRIBIO + 2) | slpflag, "smfsvinv", 0);
425 if (error)
426 return (error);
428 np->n_flag |= NFLUSHINPROG;
429 for(;;) {
430 if ((error = vinvalbuf(vp, flags, cred, l, slpflag, 0)) == 0)
431 break;
433 if (intrflg && (error == ERESTART || error == EINTR)) {
434 np->n_flag &= ~NFLUSHINPROG;
435 if (np->n_flag & NFLUSHWANT) {
436 np->n_flag &= ~NFLUSHWANT;
437 wakeup(&np->n_flag);
439 return (error);
442 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
443 if (np->n_flag & NFLUSHWANT) {
444 np->n_flag &= ~NFLUSHWANT;
445 wakeup(&np->n_flag);
447 return (error);