2 * Copyright (c) 1999, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 * $FreeBSD: src/sys/nwfs/nwfs_io.c,v 1.6.2.1 2000/10/25 02:11:10 bp Exp $
33 * $DragonFly: src/sys/vfs/nwfs/nwfs_io.c,v 1.2 2003/06/17 04:28:54 dillon Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
39 #include <sys/kernel.h>
42 #include <sys/mount.h>
43 #include <sys/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/dirent.h>
46 #include <sys/signalvar.h>
47 #include <sys/sysctl.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_pager.h>
54 #include <vm/vnode_pager.h>
56 #include <netncp/ncp.h>
57 #include <netncp/ncp_conn.h>
58 #include <netncp/ncp_subr.h>
60 #include <nwfs/nwfs.h>
61 #include <nwfs/nwfs_node.h>
62 #include <nwfs/nwfs_subr.h>
64 static int nwfs_fastlookup = 1;
66 extern struct linker_set sysctl_vfs_nwfs;
68 SYSCTL_DECL(_vfs_nwfs);
69 SYSCTL_INT(_vfs_nwfs, OID_AUTO, fastlookup, CTLFLAG_RW, &nwfs_fastlookup, 0, "");
72 extern int nwfs_pbuf_freecnt;
74 #define DE_SIZE (sizeof(struct dirent))
78 nwfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred) {
79 struct nwmount *nmp = VTONWFS(vp);
82 struct nwnode *np = VTONW(vp);
83 struct nw_entry_info fattr;
85 struct componentname cn;
89 NCPVNDEBUG("dirname='%s'\n",np->n_name);
90 if (uio->uio_resid < DE_SIZE || (uio->uio_offset < 0))
94 i = uio->uio_offset / DE_SIZE; /* offset in directory */
96 error = ncp_initsearch(vp, uio->uio_procp, cred);
98 NCPVNDEBUG("cannot initialize search, error=%d",error);
103 for (; uio->uio_resid >= DE_SIZE; i++) {
104 bzero((char *) &dp, DE_SIZE);
105 dp.d_reclen = DE_SIZE;
109 dp.d_fileno = (i == 0) ? np->n_fid.f_id : np->n_parent.f_id;
110 if (!dp.d_fileno) dp.d_fileno = NWFS_ROOT_INO;
114 dp.d_name[i + 1] = '\0';
118 error = ncp_search_for_file_or_subdir(nmp, &np->n_seq, &fattr, uio->uio_procp, cred);
119 if (error && error < 0x80) break;
120 dp.d_fileno = fattr.dirEntNum;
121 dp.d_type = (fattr.attributes & aDIR) ? DT_DIR : DT_REG;
122 dp.d_namlen = fattr.nameLen;
123 bcopy(fattr.entryName, dp.d_name, dp.d_namlen);
124 dp.d_name[dp.d_namlen] = '\0';
126 if (error && eofflag) {
133 if (nwfs_fastlookup && !error && i > 1) {
134 fid.f_id = fattr.dirEntNum;
135 fid.f_parent = np->n_fid.f_id;
136 error = nwfs_nget(vp->v_mount, fid, &fattr, vp, &newvp);
138 VTONW(newvp)->n_ctime = VTONW(newvp)->n_vattr.va_ctime.tv_sec;
139 cn.cn_nameptr = dp.d_name;
140 cn.cn_namelen = dp.d_namlen;
141 cache_enter(vp, newvp, &cn);
150 if ((error = uiomove((caddr_t)&dp, DE_SIZE, uio)))
154 uio->uio_offset = i * DE_SIZE;
159 nwfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred) {
160 struct nwmount *nmp = VFSTONWFS(vp->v_mount);
161 struct nwnode *np = VTONW(vp);
166 if (vp->v_type != VREG && vp->v_type != VDIR) {
167 printf("%s: vn types other than VREG or VDIR are unsupported !\n",__FUNCTION__);
170 if (uiop->uio_resid == 0) return 0;
171 if (uiop->uio_offset < 0) return EINVAL;
172 /* if (uiop->uio_offset + uiop->uio_resid > nmp->nm_maxfilesize)
175 if (vp->v_type == VDIR) {
176 error = nwfs_readvdir(vp, uiop, cred);
179 biosize = NWFSTOCONN(nmp)->buffer_size;
180 if (np->n_flag & NMODIFIED) {
181 nwfs_attr_cacheremove(vp);
182 error = VOP_GETATTR(vp, &vattr, cred, p);
183 if (error) return (error);
184 np->n_mtime = vattr.va_mtime.tv_sec;
186 error = VOP_GETATTR(vp, &vattr, cred, p);
187 if (error) return (error);
188 if (np->n_mtime != vattr.va_mtime.tv_sec) {
189 error = nwfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
190 if (error) return (error);
191 np->n_mtime = vattr.va_mtime.tv_sec;
194 error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, uiop,cred);
199 nwfs_writevnode(vp, uiop, cred, ioflag)
205 struct nwmount *nmp = VTONWFS(vp);
206 struct nwnode *np = VTONW(vp);
208 /* struct vattr vattr;*/
211 if (vp->v_type != VREG) {
212 printf("%s: vn types other than VREG unsupported !\n",__FUNCTION__);
215 NCPVNDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
216 if (uiop->uio_offset < 0) return EINVAL;
217 /* if (uiop->uio_offset + uiop->uio_resid > nmp->nm_maxfilesize)
220 if (ioflag & (IO_APPEND | IO_SYNC)) {
221 if (np->n_flag & NMODIFIED) {
222 nwfs_attr_cacheremove(vp);
223 error = nwfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
224 if (error) return (error);
226 if (ioflag & IO_APPEND) {
227 /* We can relay only on local information about file size,
228 * because until file is closed NetWare will not return
229 * the correct size. */
231 nwfs_attr_cacheremove(vp);
232 error = VOP_GETATTR(vp, &vattr, cred, p);
233 if (error) return (error);
235 uiop->uio_offset = np->n_size;
238 if (uiop->uio_resid == 0) return 0;
239 if (p && uiop->uio_offset + uiop->uio_resid > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
243 error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, uiop, cred);
244 NCPVNDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
246 if (uiop->uio_offset > np->n_size) {
247 np->n_vattr.va_size = np->n_size = uiop->uio_offset;
248 vnode_pager_setsize(vp, np->n_size);
255 * Do an I/O operation to/from a cache block.
273 nmp = VFSTONWFS(vp->v_mount);
276 uiop->uio_iovcnt = 1;
277 uiop->uio_segflg = UIO_SYSSPACE;
279 if (bp->b_flags & B_READ) {
280 io.iov_len = uiop->uio_resid = bp->b_bcount;
281 io.iov_base = bp->b_data;
282 uiop->uio_rw = UIO_READ;
283 switch (vp->v_type) {
285 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
286 error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, uiop, cr);
289 if (uiop->uio_resid) {
290 int left = uiop->uio_resid;
291 int nread = bp->b_bcount - left;
293 bzero((char *)bp->b_data + nread, left);
297 nfsstats.readdir_bios++;
298 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
299 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
300 error = nfs_readdirplusrpc(vp, uiop, cr);
301 if (error == NFSERR_NOTSUPP)
302 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
304 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
305 error = nfs_readdirrpc(vp, uiop, cr);
306 if (error == 0 && uiop->uio_resid == bp->b_bcount)
307 bp->b_flags |= B_INVAL;
311 printf("nwfs_doio: type %x unexpected\n",vp->v_type);
315 bp->b_flags |= B_ERROR;
319 if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
320 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
322 if (bp->b_dirtyend > bp->b_dirtyoff) {
323 io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
324 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
325 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
326 uiop->uio_rw = UIO_WRITE;
327 bp->b_flags |= B_WRITEINPROG;
328 error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, uiop, cr);
329 bp->b_flags &= ~B_WRITEINPROG;
332 * For an interrupted write, the buffer is still valid
333 * and the write hasn't been pushed to the server yet,
334 * so we can't set B_ERROR and report the interruption
335 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
336 * is not relevant, so the rpc attempt is essentially
337 * a noop. For the case of a V3 write rpc not being
338 * committed to stable storage, the block is still
339 * dirty and requires either a commit rpc or another
340 * write rpc with iomode == NFSV3WRITE_FILESYNC before
341 * the block is reused. This is indicated by setting
342 * the B_DELWRI and B_NEEDCOMMIT flags.
345 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
349 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
350 if ((bp->b_flags & B_ASYNC) == 0)
351 bp->b_flags |= B_EINTR;
352 if ((bp->b_flags & B_PAGING) == 0) {
354 bp->b_flags &= ~B_DONE;
356 if ((bp->b_flags & B_ASYNC) == 0)
357 bp->b_flags |= B_EINTR;
361 bp->b_flags |= B_ERROR;
362 bp->b_error /*= np->n_error */= error;
363 /* np->n_flag |= NWRITEERR;*/
365 bp->b_dirtyoff = bp->b_dirtyend = 0;
373 bp->b_resid = uiop->uio_resid;
379 * Vnode op for VM getpages.
380 * Wish wish .... get rid from multiple IO routines
384 struct vop_getpages_args /* {
389 vm_ooffset_t a_offset;
393 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
396 int i, error, nextoff, size, toff, npages, count;
409 p = curproc; /* XXX */
410 cred = curproc->p_ucred; /* XXX */
412 nmp = VFSTONWFS(vp->v_mount);
416 if (vp->v_object == NULL) {
417 printf("nwfs_getpages: called with non-merged cache vnode??\n");
418 return VM_PAGER_ERROR;
421 bp = getpbuf(&nwfs_pbuf_freecnt);
422 npages = btoc(count);
423 kva = (vm_offset_t) bp->b_data;
424 pmap_qenter(kva, pages, npages);
426 iov.iov_base = (caddr_t) kva;
430 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
431 uio.uio_resid = count;
432 uio.uio_segflg = UIO_SYSSPACE;
433 uio.uio_rw = UIO_READ;
436 error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, &uio,cred);
437 pmap_qremove(kva, npages);
439 relpbuf(bp, &nwfs_pbuf_freecnt);
441 if (error && (uio.uio_resid == count)) {
442 printf("nwfs_getpages: error %d\n",error);
443 for (i = 0; i < npages; i++) {
444 if (ap->a_reqpage != i)
445 vnode_pager_freepage(pages[i]);
447 return VM_PAGER_ERROR;
450 size = count - uio.uio_resid;
452 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
454 nextoff = toff + PAGE_SIZE;
457 m->flags &= ~PG_ZERO;
459 if (nextoff <= size) {
460 m->valid = VM_PAGE_BITS_ALL;
463 int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
464 vm_page_set_validclean(m, 0, nvalid);
467 if (i != ap->a_reqpage) {
469 * Whether or not to leave the page activated is up in
470 * the air, but we should put the page on a page queue
471 * somewhere (it already is in the object). Result:
472 * It appears that emperical results show that
473 * deactivating pages is best.
477 * Just in case someone was asking for this page we
478 * now tell them that it is ok to use.
481 if (m->flags & PG_WANTED)
484 vm_page_deactivate(m);
487 vnode_pager_freepage(m);
492 #endif /* NWFS_RWCACHE */
496 * Vnode op for VM putpages.
497 * possible bug: all IO done in sync mode
498 * Note that vop_close always invalidate pages before close, so it's
499 * not necessary to open vnode.
503 struct vop_putpages_args /* {
509 vm_ooffset_t a_offset;
513 struct vnode *vp = ap->a_vp;
518 p = curproc; /* XXX */
519 cred = p->p_ucred; /* XXX */
520 VOP_OPEN(vp, FWRITE, cred, p);
521 error = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
522 ap->a_sync, ap->a_rtvals);
523 VOP_CLOSE(vp, FWRITE, cred, p);
530 int i, npages, count;
536 p = curproc; /* XXX */
537 cred = p->p_ucred; /* XXX */
538 /* VOP_OPEN(vp, FWRITE, cred, p);*/
540 nmp = VFSTONWFS(vp->v_mount);
543 rtvals = ap->a_rtvals;
544 npages = btoc(count);
546 for (i = 0; i < npages; i++) {
547 rtvals[i] = VM_PAGER_AGAIN;
550 bp = getpbuf(&nwfs_pbuf_freecnt);
551 kva = (vm_offset_t) bp->b_data;
552 pmap_qenter(kva, pages, npages);
554 iov.iov_base = (caddr_t) kva;
558 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
559 uio.uio_resid = count;
560 uio.uio_segflg = UIO_SYSSPACE;
561 uio.uio_rw = UIO_WRITE;
563 NCPVNDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
565 error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, &uio, cred);
566 /* VOP_CLOSE(vp, FWRITE, cred, p);*/
567 NCPVNDEBUG("paged write done: %d\n", error);
569 pmap_qremove(kva, npages);
570 relpbuf(bp, &nwfs_pbuf_freecnt);
573 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
574 for (i = 0; i < nwritten; i++) {
575 rtvals[i] = VM_PAGER_OK;
580 #endif /* NWFS_RWCACHE */
583 * Flush and invalidate all dirty buffers. If another process is already
584 * doing the flush, just wait for completion.
587 nwfs_vinvalbuf(vp, flags, cred, p, intrflg)
594 struct nwnode *np = VTONW(vp);
595 /* struct nwmount *nmp = VTONWFS(vp);*/
596 int error = 0, slpflag, slptimeo;
598 if (vp->v_flag & VXLOCK) {
608 while (np->n_flag & NFLUSHINPROG) {
609 np->n_flag |= NFLUSHWANT;
610 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nwfsvinv", slptimeo);
611 error = ncp_chkintr(NWFSTOCONN(VTONWFS(vp)), p);
612 if (error == EINTR && intrflg)
615 np->n_flag |= NFLUSHINPROG;
616 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
618 if (intrflg && (error == ERESTART || error == EINTR)) {
619 np->n_flag &= ~NFLUSHINPROG;
620 if (np->n_flag & NFLUSHWANT) {
621 np->n_flag &= ~NFLUSHWANT;
622 wakeup((caddr_t)&np->n_flag);
626 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
628 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
629 if (np->n_flag & NFLUSHWANT) {
630 np->n_flag &= ~NFLUSHWANT;
631 wakeup((caddr_t)&np->n_flag);