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
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
38 #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>
59 #include <netncp/ncp_ncp.h>
61 #include <fs/nwfs/nwfs.h>
62 #include <fs/nwfs/nwfs_node.h>
63 #include <fs/nwfs/nwfs_subr.h>
65 static int nwfs_fastlookup = 1;
67 SYSCTL_DECL(_vfs_nwfs);
68 SYSCTL_INT(_vfs_nwfs, OID_AUTO, fastlookup, CTLFLAG_RW, &nwfs_fastlookup, 0, "");
71 extern int nwfs_pbuf_freecnt;
73 #define DE_SIZE (sizeof(struct dirent))
77 nwfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred) {
78 struct nwmount *nmp = VTONWFS(vp);
81 struct nwnode *np = VTONW(vp);
82 struct nw_entry_info fattr;
84 struct componentname cn;
88 NCPVNDEBUG("dirname='%s'\n",np->n_name);
89 if (uio->uio_resid < DE_SIZE || (uio->uio_offset < 0))
93 i = uio->uio_offset / DE_SIZE; /* offset in directory */
95 error = ncp_initsearch(vp, uio->uio_td, cred);
97 NCPVNDEBUG("cannot initialize search, error=%d",error);
102 for (; uio->uio_resid >= DE_SIZE; i++) {
103 bzero((char *) &dp, DE_SIZE);
104 dp.d_reclen = DE_SIZE;
108 dp.d_fileno = (i == 0) ? np->n_fid.f_id : np->n_parent.f_id;
109 if (!dp.d_fileno) dp.d_fileno = NWFS_ROOT_INO;
113 dp.d_name[i + 1] = '\0';
117 error = ncp_search_for_file_or_subdir(nmp, &np->n_seq, &fattr, uio->uio_td, cred);
118 if (error && error < 0x80) break;
119 dp.d_fileno = fattr.dirEntNum;
120 dp.d_type = (fattr.attributes & aDIR) ? DT_DIR : DT_REG;
121 dp.d_namlen = fattr.nameLen;
122 bcopy(fattr.entryName, dp.d_name, dp.d_namlen);
123 dp.d_name[dp.d_namlen] = '\0';
125 if (error && eofflag) {
132 if (nwfs_fastlookup && !error && i > 1) {
133 fid.f_id = fattr.dirEntNum;
134 fid.f_parent = np->n_fid.f_id;
135 error = nwfs_nget(vp->v_mount, fid, &fattr, vp, &newvp);
137 VTONW(newvp)->n_ctime = VTONW(newvp)->n_vattr.va_ctime.tv_sec;
138 cn.cn_nameptr = dp.d_name;
139 cn.cn_namelen = dp.d_namlen;
140 cache_enter(vp, newvp, &cn);
149 if ((error = uiomove(&dp, DE_SIZE, uio)))
153 uio->uio_offset = i * DE_SIZE;
158 nwfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred) {
159 struct nwmount *nmp = VFSTONWFS(vp->v_mount);
160 struct nwnode *np = VTONW(vp);
165 if (vp->v_type != VREG && vp->v_type != VDIR) {
166 printf("%s: vn types other than VREG or VDIR are unsupported !\n",__func__);
169 if (uiop->uio_resid == 0) return 0;
170 if (uiop->uio_offset < 0) return EINVAL;
171 /* if (uiop->uio_offset + uiop->uio_resid > nmp->nm_maxfilesize)
174 if (vp->v_type == VDIR) {
175 error = nwfs_readvdir(vp, uiop, cred);
178 biosize = NWFSTOCONN(nmp)->buffer_size;
179 if (np->n_flag & NMODIFIED) {
180 nwfs_attr_cacheremove(vp);
181 error = VOP_GETATTR(vp, &vattr, cred, td);
182 if (error) return (error);
183 np->n_mtime = vattr.va_mtime.tv_sec;
185 error = VOP_GETATTR(vp, &vattr, cred, td);
186 if (error) return (error);
187 if (np->n_mtime != vattr.va_mtime.tv_sec) {
188 error = nwfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
189 if (error) return (error);
190 np->n_mtime = vattr.va_mtime.tv_sec;
193 error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, uiop,cred);
198 nwfs_writevnode(vp, uiop, cred, ioflag)
204 struct nwmount *nmp = VTONWFS(vp);
205 struct nwnode *np = VTONW(vp);
207 /* struct vattr vattr;*/
210 if (vp->v_type != VREG) {
211 printf("%s: vn types other than VREG unsupported !\n",__func__);
214 NCPVNDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
215 if (uiop->uio_offset < 0) return EINVAL;
216 /* if (uiop->uio_offset + uiop->uio_resid > nmp->nm_maxfilesize)
219 if (ioflag & (IO_APPEND | IO_SYNC)) {
220 if (np->n_flag & NMODIFIED) {
221 nwfs_attr_cacheremove(vp);
222 error = nwfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
223 if (error) return (error);
225 if (ioflag & IO_APPEND) {
226 /* We can relay only on local information about file size,
227 * because until file is closed NetWare will not return
228 * the correct size. */
230 nwfs_attr_cacheremove(vp);
231 error = VOP_GETATTR(vp, &vattr, cred, td);
232 if (error) return (error);
234 uiop->uio_offset = np->n_size;
237 if (uiop->uio_resid == 0) return 0;
239 PROC_LOCK(td->td_proc);
240 if (uiop->uio_offset + uiop->uio_resid >
241 lim_cur(td->td_proc, RLIMIT_FSIZE)) {
242 psignal(td->td_proc, SIGXFSZ);
243 PROC_UNLOCK(td->td_proc);
246 PROC_UNLOCK(td->td_proc);
248 error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, uiop, cred);
249 NCPVNDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
251 if (uiop->uio_offset > np->n_size) {
252 np->n_vattr.va_size = np->n_size = uiop->uio_offset;
253 vnode_pager_setsize(vp, np->n_size);
260 * Do an I/O operation to/from a cache block.
263 nwfs_doio(bp, cr, td)
278 nmp = VFSTONWFS(vp->v_mount);
281 uiop->uio_iovcnt = 1;
282 uiop->uio_segflg = UIO_SYSSPACE;
284 if (bp->b_iocmd == BIO_READ) {
285 io.iov_len = uiop->uio_resid = bp->b_bcount;
286 io.iov_base = bp->b_data;
287 uiop->uio_rw = UIO_READ;
288 switch (vp->v_type) {
290 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
291 error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, uiop, cr);
294 if (uiop->uio_resid) {
295 int left = uiop->uio_resid;
296 int nread = bp->b_bcount - left;
298 bzero((char *)bp->b_data + nread, left);
302 nfsstats.readdir_bios++;
303 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
304 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
305 error = nfs_readdirplusrpc(vp, uiop, cr);
306 if (error == NFSERR_NOTSUPP)
307 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
309 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
310 error = nfs_readdirrpc(vp, uiop, cr);
311 if (error == 0 && uiop->uio_resid == bp->b_bcount)
312 bp->b_flags |= B_INVAL;
316 printf("nwfs_doio: type %x unexpected\n",vp->v_type);
320 bp->b_ioflags |= BIO_ERROR;
324 if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
325 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
327 if (bp->b_dirtyend > bp->b_dirtyoff) {
328 io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
329 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
330 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
331 uiop->uio_rw = UIO_WRITE;
332 bp->b_flags |= B_WRITEINPROG;
333 error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, uiop, cr);
334 bp->b_flags &= ~B_WRITEINPROG;
337 * For an interrupted write, the buffer is still valid
338 * and the write hasn't been pushed to the server yet,
339 * so we can't set BIO_ERROR and report the interruption
340 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
341 * is not relevant, so the rpc attempt is essentially
342 * a noop. For the case of a V3 write rpc not being
343 * committed to stable storage, the block is still
344 * dirty and requires either a commit rpc or another
345 * write rpc with iomode == NFSV3WRITE_FILESYNC before
346 * the block is reused. This is indicated by setting
347 * the B_DELWRI and B_NEEDCOMMIT flags.
350 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
354 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
355 if ((bp->b_flags & B_ASYNC) == 0)
356 bp->b_flags |= B_EINTR;
357 if ((bp->b_flags & B_PAGING) == 0) {
359 bp->b_flags &= ~B_DONE;
361 if ((bp->b_flags & B_ASYNC) == 0)
362 bp->b_flags |= B_EINTR;
366 bp->b_ioflags |= BIO_ERROR;
367 bp->b_error /*= np->n_error */= error;
368 /* np->n_flag |= NWRITEERR;*/
370 bp->b_dirtyoff = bp->b_dirtyend = 0;
378 bp->b_resid = uiop->uio_resid;
384 * Vnode op for VM getpages.
385 * Wish wish .... get rid from multiple IO routines
389 struct vop_getpages_args /* {
394 vm_ooffset_t a_offset;
398 return vop_stdgetpages(ap);(ap->a_vp, ap->a_m, ap->a_count,
400 int i, error, nextoff, size, toff, npages, count;
414 td = curthread; /* XXX */
415 cred = td->td_ucred; /* XXX */
417 nmp = VFSTONWFS(vp->v_mount);
421 if ((object = vp->v_object) == NULL) {
422 printf("nwfs_getpages: called with non-merged cache vnode??\n");
423 return VM_PAGER_ERROR;
426 bp = getpbuf(&nwfs_pbuf_freecnt);
427 npages = btoc(count);
428 kva = (vm_offset_t) bp->b_data;
429 pmap_qenter(kva, pages, npages);
431 iov.iov_base = (caddr_t) kva;
435 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
436 uio.uio_resid = count;
437 uio.uio_segflg = UIO_SYSSPACE;
438 uio.uio_rw = UIO_READ;
441 error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, &uio,cred);
442 pmap_qremove(kva, npages);
444 relpbuf(bp, &nwfs_pbuf_freecnt);
446 VM_OBJECT_LOCK(object);
447 if (error && (uio.uio_resid == count)) {
448 printf("nwfs_getpages: error %d\n",error);
449 vm_page_lock_queues();
450 for (i = 0; i < npages; i++) {
451 if (ap->a_reqpage != i)
452 vm_page_free(pages[i]);
454 vm_page_unlock_queues();
455 VM_OBJECT_UNLOCK(object);
456 return VM_PAGER_ERROR;
459 size = count - uio.uio_resid;
461 vm_page_lock_queues();
462 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
464 nextoff = toff + PAGE_SIZE;
467 if (nextoff <= size) {
468 m->valid = VM_PAGE_BITS_ALL;
471 int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
472 vm_page_set_validclean(m, 0, nvalid);
475 if (i != ap->a_reqpage) {
477 * Whether or not to leave the page activated is up in
478 * the air, but we should put the page on a page queue
479 * somewhere (it already is in the object). Result:
480 * It appears that emperical results show that
481 * deactivating pages is best.
485 * Just in case someone was asking for this page we
486 * now tell them that it is ok to use.
489 if (m->flags & PG_WANTED)
492 vm_page_deactivate(m);
499 vm_page_unlock_queues();
500 VM_OBJECT_UNLOCK(object);
502 #endif /* NWFS_RWCACHE */
506 * Vnode op for VM putpages.
507 * possible bug: all IO done in sync mode
508 * Note that vop_close always invalidate pages before close, so it's
509 * not necessary to open vnode.
513 struct vop_putpages_args /* {
519 vm_ooffset_t a_offset;
523 struct vnode *vp = ap->a_vp;
528 td = curthread; /* XXX */
529 cred = td->td_ucred; /* XXX */
530 VOP_OPEN(vp, FWRITE, cred, td, -1);
531 error = vop_stdputpages(ap);
532 VOP_CLOSE(vp, FWRITE, cred, td);
539 int i, npages, count;
545 td = curthread; /* XXX */
546 cred = td->td_ucred; /* XXX */
547 /* VOP_OPEN(vp, FWRITE, cred, td, -1);*/
549 nmp = VFSTONWFS(vp->v_mount);
552 rtvals = ap->a_rtvals;
553 npages = btoc(count);
555 for (i = 0; i < npages; i++) {
556 rtvals[i] = VM_PAGER_AGAIN;
559 bp = getpbuf(&nwfs_pbuf_freecnt);
560 kva = (vm_offset_t) bp->b_data;
561 pmap_qenter(kva, pages, npages);
563 iov.iov_base = (caddr_t) kva;
567 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
568 uio.uio_resid = count;
569 uio.uio_segflg = UIO_SYSSPACE;
570 uio.uio_rw = UIO_WRITE;
572 NCPVNDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
574 error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, &uio, cred);
575 /* VOP_CLOSE(vp, FWRITE, cred, td);*/
576 NCPVNDEBUG("paged write done: %d\n", error);
578 pmap_qremove(kva, npages);
579 relpbuf(bp, &nwfs_pbuf_freecnt);
582 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
583 vm_page_lock_queues();
584 for (i = 0; i < nwritten; i++) {
585 rtvals[i] = VM_PAGER_OK;
586 vm_page_undirty(pages[i]);
588 vm_page_unlock_queues();
591 #endif /* NWFS_RWCACHE */
594 * Flush and invalidate all dirty buffers. If another process is already
595 * doing the flush, just wait for completion.
598 nwfs_vinvalbuf(vp, flags, cred, td, intrflg)
605 struct nwnode *np = VTONW(vp);
606 /* struct nwmount *nmp = VTONWFS(vp);*/
607 int error = 0, slpflag, slptimeo;
609 if (vp->v_iflag & VI_XLOCK)
619 while (np->n_flag & NFLUSHINPROG) {
620 np->n_flag |= NFLUSHWANT;
621 error = tsleep(&np->n_flag, PRIBIO + 2, "nwfsvinv", slptimeo);
622 error = ncp_chkintr(NWFSTOCONN(VTONWFS(vp)), td);
623 if (error == EINTR && intrflg)
626 np->n_flag |= NFLUSHINPROG;
627 error = vinvalbuf(vp, flags, cred, td, slpflag, 0);
629 if (intrflg && (error == ERESTART || error == EINTR)) {
630 np->n_flag &= ~NFLUSHINPROG;
631 if (np->n_flag & NFLUSHWANT) {
632 np->n_flag &= ~NFLUSHWANT;
637 error = vinvalbuf(vp, flags, cred, td, slpflag, 0);
639 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
640 if (np->n_flag & NFLUSHWANT) {
641 np->n_flag &= ~NFLUSHWANT;