2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * vnodeops structure and Digital Unix specific ops and support routines.
14 #include <afsconfig.h>
15 #include "afs/param.h"
19 #include "afs/sysincludes.h" /* Standard vendor system headers */
20 #include "afsincludes.h" /* Afs-based standard headers */
21 #include "afs/afs_stats.h" /* statistics */
23 #include <vm/vnode_pager.h>
24 #include <vm/vm_map.h>
25 /* #include <vm/vm_ubc.h> */
26 #include "afs/afs_cbqueue.h"
27 #include "afs/nfsclient.h"
28 #include "afs/afs_osidnlc.h"
31 extern int afs_lookup(), afs_create(), afs_noop(), afs_open(), afs_close();
32 extern int afs_access(), afs_getattr(), afs_setattr(), afs_badop();
33 extern int afs_fsync(), afs_seek(), afs_remove(), afs_link(), afs_rename();
34 extern int afs_mkdir(), afs_rmdir(), afs_symlink(), afs_readdir();
35 extern int afs_readlink(), afs_lockctl();
36 extern int vn_pathconf_default(), seltrue();
38 int mp_afs_lookup(), mp_afs_create(), mp_afs_open();
39 int mp_afs_access(), mp_afs_getattr(), mp_afs_setattr(), mp_afs_ubcrdwr();
40 int mp_afs_ubcrdwr(), mp_afs_mmap();
41 int mp_afs_fsync(), mp_afs_seek(), mp_afs_remove(), mp_afs_link();
42 int mp_afs_rename(), mp_afs_mkdir(), mp_afs_rmdir(), mp_afs_symlink();
43 int mp_afs_readdir(), mp_afs_readlink(), mp_afs_abortop(), mp_afs_inactive();
44 int mp_afs_reclaim(), mp_afs_bmap(), mp_afs_strategy(), mp_afs_print();
45 int mp_afs_page_read(), mp_afs_page_write(), mp_afs_swap(), mp_afs_bread();
46 int mp_afs_brelse(), mp_afs_lockctl(), mp_afs_syncdata(), mp_afs_close();
51 struct vnodeops Afs_vnodeops = {
54 afs_noop, /* vn_mknod */
62 afs_badop, /* vn_ioctl */
63 seltrue, /* vn_select */
89 afs_noop, /* unLock */
90 afs_noop, /* get ext attrs */
91 afs_noop, /* set ext attrs */
92 afs_noop, /* del ext attrs */
95 struct vnodeops *afs_ops = &Afs_vnodeops;
98 /* vnode file operations, and our own */
100 extern int vn_write();
101 extern int vn_ioctl();
102 extern int vn_select();
103 extern int afs_closex();
105 struct fileops afs_fileops = {
114 mp_afs_lookup(adp, ndp)
116 struct nameidata *ndp;
120 code = afs_lookup(adp, ndp);
125 mp_afs_create(ndp, attrs)
126 struct nameidata *ndp;
131 code = afs_create(ndp, attrs);
136 mp_afs_open(avcp, aflags, acred)
137 struct vcache **avcp;
143 code = afs_open(avcp, aflags, acred);
148 mp_afs_access(avc, amode, acred)
155 code = afs_access(avc, amode, acred);
160 mp_afs_close(avc, flags, cred)
167 code = afs_close(avc, flags, cred);
172 mp_afs_getattr(avc, attrs, acred)
179 code = afs_getattr(avc, attrs, acred);
184 mp_afs_setattr(avc, attrs, acred)
191 code = afs_setattr(avc, attrs, acred);
196 mp_afs_fsync(avc, fflags, acred, waitfor)
204 code = afs_fsync(avc, fflags, acred, waitfor);
210 struct nameidata *ndp;
214 code = afs_remove(ndp);
219 mp_afs_link(avc, ndp)
221 struct nameidata *ndp;
225 code = afs_link(avc, ndp);
230 mp_afs_rename(fndp, tndp)
231 struct nameidata *fndp, *tndp;
235 code = afs_rename(fndp, tndp);
240 mp_afs_mkdir(ndp, attrs)
241 struct nameidata *ndp;
246 code = afs_mkdir(ndp, attrs);
252 struct nameidata *ndp;
256 code = afs_rmdir(ndp);
261 mp_afs_symlink(ndp, attrs, atargetName)
262 struct nameidata *ndp;
264 register char *atargetName;
268 code = afs_symlink(ndp, attrs, atargetName);
273 mp_afs_readdir(avc, auio, acred, eofp)
281 code = afs_readdir(avc, auio, acred, eofp);
286 mp_afs_readlink(avc, auio, acred)
293 code = afs_readlink(avc, auio, acred);
298 mp_afs_lockctl(avc, af, flag, acred, clid, offset)
308 code = afs_lockctl(avc, af, flag, acred, clid, offset);
318 code = afs_closex(afd);
323 mp_afs_seek(avc, oldoff, newoff, cred)
325 off_t oldoff, newoff;
335 struct nameidata *ndp;
340 mp_afs_inactive(avc, acred)
341 register struct vcache *avc;
345 afs_InactiveVCache(avc, acred);
362 mp_afs_page_read(avc, uio, acred)
368 struct vrequest treq;
371 error = afs_rdwr(avc, uio, UIO_READ, 0, acred);
372 afs_Trace3(afs_iclSetp, CM_TRACE_PAGE_READ, ICL_TYPE_POINTER, avc,
373 ICL_TYPE_INT32, error, ICL_TYPE_INT32, avc->f.states);
376 } else if ((avc->f.states) == 0) {
377 afs_InitReq(&treq, acred);
378 ObtainWriteLock(&avc->lock, 161);
379 afs_Wire(avc, &treq);
380 ReleaseWriteLock(&avc->lock);
387 mp_afs_page_write(avc, uio, acred, pager, offset)
391 memory_object_t pager;
397 error = afs_rdwr(avc, uio, UIO_WRITE, 0, acred);
398 afs_Trace3(afs_iclSetp, CM_TRACE_PAGE_WRITE, ICL_TYPE_POINTER, avc,
399 ICL_TYPE_INT32, error, ICL_TYPE_INT32, avc->f.states);
409 mp_afs_ubcrdwr(avc, uio, ioflag, cred)
415 register afs_int32 code;
417 afs_int32 fileBase, size, cnt = 0;
419 register afs_int32 tsize;
420 register afs_int32 pageOffset;
422 struct vrequest treq;
423 int rw = uio->uio_rw;
427 afs_int32 save_resid;
433 afs_InitReq(&treq, cred);
434 if (AFS_NFSXLATORREQ(cred) && rw == UIO_READ) {
436 (avc, PRSFS_READ, &treq,
437 CHECK_MODE_BITS | CMB_ALLOW_EXEC_AS_READ)) {
442 afs_Trace4(afs_iclSetp, CM_TRACE_VMRW, ICL_TYPE_POINTER, avc,
443 ICL_TYPE_INT32, (rw == UIO_WRITE ? 1 : 0), ICL_TYPE_LONG,
444 uio->uio_offset, ICL_TYPE_LONG, uio->uio_resid);
445 code = afs_VerifyVCache(avc, &treq);
447 code = afs_CheckCode(code, &treq, 35);
451 if (vType(avc) != VREG) {
453 return EISDIR; /* can't read or write other things */
455 osi_FlushPages(avc); /* hold bozon lock, but not basic vnode lock */
456 ObtainWriteLock(&avc->lock, 162);
457 /* adjust parameters when appending files */
458 if ((ioflag & IO_APPEND) && uio->uio_rw == UIO_WRITE)
459 uio->uio_offset = avc->f.m.Length; /* write at EOF position */
460 if (uio->uio_rw == UIO_WRITE) {
461 avc->f.states |= CDirty;
465 * before starting any I/O, we must ensure that the file is big enough
466 * to hold the results (since afs_putpage will be called to force
469 size = uio->afsio_resid + uio->afsio_offset; /* new file size */
470 if (size > avc->f.m.Length)
471 avc->f.m.Length = size; /* file grew */
472 avc->f.m.Date = osi_Time(); /* Set file date (for ranlib) */
473 if (uio->afsio_resid > PAGE_SIZE)
474 cnt = uio->afsio_resid / PAGE_SIZE;
475 save_resid = uio->afsio_resid;
480 * compute the amount of data to move into this block,
481 * based on uio->afsio_resid.
483 size = uio->afsio_resid; /* transfer size */
484 fileBase = uio->afsio_offset; /* start file position */
485 pageBase = fileBase & ~(PAGE_SIZE - 1); /* file position of the page */
486 pageOffset = fileBase & (PAGE_SIZE - 1); /* start offset within page */
487 tsize = PAGE_SIZE - pageOffset; /* amount left in this page */
489 * we'll read tsize bytes,
490 * but first must make sure tsize isn't too big
493 tsize = size; /* don't read past end of request */
494 eof = 0; /* flag telling us if we hit the EOF on the read */
495 if (uio->uio_rw == UIO_READ) { /* we're doing a read operation */
496 /* don't read past EOF */
497 if (tsize + fileBase > avc->f.m.Length) {
498 tsize = avc->f.m.Length - fileBase;
499 eof = 1; /* we did hit the EOF */
501 tsize = 0; /* better safe than sorry */
505 break; /* nothing to transfer, we're done */
507 /* Purge dirty chunks of file if there are too many dirty chunks.
508 * Inside the write loop, we only do this at a chunk boundary.
509 * Clean up partial chunk if necessary at end of loop.
511 if (uio->uio_rw == UIO_WRITE && counter > 0
512 && AFS_CHUNKOFFSET(fileBase) == 0) {
513 code = afs_DoPartialWrite(avc, &treq);
514 avc->f.states |= CDirty;
522 ReleaseWriteLock(&avc->lock);
525 ubc_lookup(((struct vnode *)avc)->v_object, pageBase, PAGE_SIZE,
526 PAGE_SIZE, &page, &flags);
528 ObtainWriteLock(&avc->lock, 163);
533 if (flags & B_NOCACHE) {
535 * No page found. We should not read the page in if
536 * 1. the write starts on a page edge (ie, pageoffset == 0)
538 * 1. we will fill the page (ie, size == PAGESIZE), or
539 * 2. we are writing past eof
541 if ((uio->uio_rw == UIO_WRITE)
544 && (size == PAGE_SIZE || fileBase >= avc->f.m.Length)))) {
545 struct vnode *vp = (struct vnode *)avc;
546 /* we're doing a write operation past eof; no need to read it */
549 ubc_page_zero(page, 0, PAGE_SIZE);
550 ubc_page_release(page, B_DONE);
553 /* page wasn't cached, read it in. */
557 bp = ubc_bufalloc(page, 1, PAGE_SIZE, 1, B_READ);
560 bp->b_vp = (struct vnode *)avc;
561 bp->b_blkno = btodb(pageBase);
562 ReleaseWriteLock(&avc->lock);
563 code = afs_ustrategy(bp, cred); /* do the I/O */
564 ObtainWriteLock(&avc->lock, 164);
570 ubc_page_release(page, 0);
578 data = (char *)page->pg_addr; /* DUX 4.0D */
580 data = (char *)PHYS_TO_KSEG(page->pg_phys_addr); /* DUX 4.0E */
582 ReleaseWriteLock(&avc->lock); /* uiomove may page fault */
584 code = uiomove(data + pageOffset, tsize, uio);
585 ubc_unload(page, pageOffset, page_size);
586 if (uio->uio_rw == UIO_WRITE) {
589 /* Mark the page dirty and release it to avoid a deadlock
590 * in ubc_dirty_kluster when more than one process writes
591 * this page at the same time. */
592 toffset = page->pg_offset;
594 ubc_page_release(page, flags);
601 /* We released the page, so we can get a null page
602 * list if another thread calls the strategy routine.
604 pl = ubc_dirty_kluster(((struct vnode *)avc)->v_object, NULL,
605 toffset, 0, B_WANTED, FALSE, &kpcnt);
607 bp = ubc_bufalloc(pl, 1, PAGE_SIZE, 1, B_WRITE);
609 bp->b_vp = (struct vnode *)avc;
610 bp->b_blkno = btodb(pageBase);
612 code = afs_ustrategy(bp, cred); /* do the I/O */
617 ObtainWriteLock(&avc->lock, 415);
623 ubc_page_release(page, flags);
626 ObtainWriteLock(&avc->lock, 165);
628 * If reading at a chunk boundary, start prefetch of next chunk.
630 if (uio->uio_rw == UIO_READ
631 && (counter == 0 || AFS_CHUNKOFFSET(fileBase) == 0)) {
632 tdc = afs_FindDCache(avc, fileBase);
634 if (!(tdc->mflags & DFNextStarted))
635 afs_PrefetchChunk(avc, tdc, cred, &treq);
644 afs_FakeClose(avc, cred);
645 if (uio->uio_rw == UIO_WRITE && code == 0 && (avc->f.states & CDirty)) {
646 code = afs_DoPartialWrite(avc, &treq);
648 ReleaseWriteLock(&avc->lock);
649 if (DO_FLUSH || (!newpage && (cnt < 10))) {
651 ubc_flush_dirty(((struct vnode *)avc)->v_object, flags);
655 ObtainSharedLock(&avc->lock, 409);
658 code = avc->vc_error;
661 /* This is required since we may still have dirty pages after the write.
662 * I could just let close do the right thing, but stat's before the close
663 * return the wrong length.
665 if (code == EDQUOT || code == ENOSPC) {
666 uio->uio_resid = save_resid;
667 UpgradeSToWLock(&avc->lock, 410);
668 osi_ReleaseVM(avc, cred);
669 ConvertWToSLock(&avc->lock);
671 ReleaseSharedLock(&avc->lock);
673 if (!code && (ioflag & IO_SYNC) && (uio->uio_rw == UIO_WRITE)
674 && !AFS_NFSXLATORREQ(cred)) {
675 code = afs_fsync(avc, 0, cred, 0);
678 code = afs_CheckCode(code, &treq, 36);
685 * Now for some bad news. Since we artificially hold on to vnodes by doing
686 * and extra VNHOLD in afs_NewVCache(), there is no way for us to know
687 * when we need to flush the pages when a program exits. Particularly
688 * if it closes the file after mapping it R/W.
692 mp_afs_mmap(avc, offset, map, addrp, len, prot, maxprot, flags, cred)
693 register struct vcache *avc;
703 struct vp_mmap_args args;
704 register struct vp_mmap_args *ap = &args;
705 struct vnode *vp = (struct vnode *)avc;
707 struct vrequest treq;
709 extern kern_return_t u_vp_create();
713 afs_InitReq(&treq, cred);
714 code = afs_VerifyVCache(avc, &treq);
716 code = afs_CheckCode(code, &treq, 37);
720 osi_FlushPages(avc); /* ensure old pages are gone */
721 ObtainWriteLock(&avc->lock, 166);
722 avc->f.states |= CMAPPED;
723 ReleaseWriteLock(&avc->lock);
724 ap->a_offset = offset;
727 ap->a_prot = prot, ap->a_maxprot = maxprot;
730 code = u_vp_create(map, vp->v_object, (vm_offset_t) ap);
732 code = afs_CheckCode(code, &treq, 38);
739 mp_afs_getpage(vop, offset, len, protp, pl, plsz, mape, addr, rw, cred)
751 register afs_int32 code;
752 struct vrequest treq;
754 int i, pages = (len + PAGE_SIZE - 1) >> page_shift;
758 struct vcache *avc = VTOAFS(vop->vu_vp);
760 /* first, obtain the proper lock for the VM system */
763 afs_InitReq(&treq, cred);
764 code = afs_VerifyVCache(avc, &treq);
767 code = afs_CheckCode(code, &treq, 39); /* failed to get it */
772 /* clean all dirty pages for this vnode */
774 ubc_flush_dirty(vop, 0);
777 ObtainWriteLock(&avc->lock, 167);
778 afs_Trace4(afs_iclSetp, CM_TRACE_PAGEIN, ICL_TYPE_POINTER, avc,
779 ICL_TYPE_LONG, offset, ICL_TYPE_LONG, len, ICL_TYPE_INT32,
781 for (i = 0; i < pages; i++) {
783 off = offset + PAGE_SIZE * i;
787 ReleaseWriteLock(&avc->lock);
790 ubc_lookup(((struct vnode *)avc)->v_object, off, PAGE_SIZE,
791 PAGE_SIZE, pagep, &flags);
793 ObtainWriteLock(&avc->lock, 168);
797 if (flags & B_NOCACHE) { /* if (page) */
798 if ((rw & B_WRITE) && (offset + len >= avc->f.m.Length)) {
799 struct vnode *vp = (struct vnode *)avc;
800 /* we're doing a write operation past eof; no need to read it */
802 ubc_page_zero(*pagep, 0, PAGE_SIZE);
803 ubc_page_release(*pagep, B_DONE);
806 /* page wasn't cached, read it in. */
810 bp = ubc_bufalloc(*pagep, 1, PAGE_SIZE, 1, B_READ);
813 bp->b_vp = (struct vnode *)avc;
814 bp->b_blkno = btodb(off);
815 ReleaseWriteLock(&avc->lock);
816 code = afs_ustrategy(bp, cred); /* do the I/O */
817 ObtainWriteLock(&avc->lock, 169);
823 ubc_page_release(pl[i], 0);
829 if ((rw & B_READ) == 0) {
831 ubc_page_dirty(pl[i]);
834 if (protp && (flags & B_DIRTY) == 0) {
835 protp[i] = VM_PROT_WRITE;
840 pl[i] = VM_PAGE_NULL;
841 ReleaseWriteLock(&avc->lock);
842 afs_Trace3(afs_iclSetp, CM_TRACE_PAGEINDONE, ICL_TYPE_INT32, code,
843 ICL_TYPE_POINTER, *pagep, ICL_TYPE_INT32, flags);
844 code = afs_CheckCode(code, &treq, 40);
851 mp_afs_putpage(vop, pl, pcnt, flags, cred)
858 register afs_int32 code = 0;
859 struct vcache *avc = VTOAFS(vop->vu_vp);
860 struct vnode *vp = (struct vnode *)avc;
864 afs_Trace4(afs_iclSetp, CM_TRACE_PAGEOUT, ICL_TYPE_POINTER, avc,
865 ICL_TYPE_INT32, pcnt, ICL_TYPE_INT32, vp->v_flag,
866 ICL_TYPE_INT32, flags);
870 if (vp->v_flag & VXLOCK) {
872 for (i = 0; i < pcnt; i++) {
873 ubc_page_release(pl[i], B_DONE | B_DIRTY);
874 pl[i] = VM_PAGE_NULL;
883 /* first, obtain the proper lock for the VM system */
884 ObtainWriteLock(&avc->lock, 170);
885 for (i = 0; i < pcnt; i++) {
886 vm_page_t page = pl[i];
891 bp = ubc_bufalloc(page, 1, PAGE_SIZE, 1, B_WRITE);
894 bp->b_vp = (struct vnode *)avc;
895 bp->b_blkno = btodb(page->pg_offset);
896 ReleaseWriteLock(&avc->lock);
897 code = afs_ustrategy(bp, cred); /* do the I/O */
898 ObtainWriteLock(&avc->lock, 171);
905 pl[i] = VM_PAGE_NULL;
909 ReleaseWriteLock(&avc->lock);
910 afs_Trace2(afs_iclSetp, CM_TRACE_PAGEOUTDONE, ICL_TYPE_INT32, code,
911 ICL_TYPE_INT32, avc->f.m.Length);
918 mp_afs_swap(avc, swapop, argp)
927 mp_afs_syncdata(avc, flag, offset, length, cred)
934 /* NFS V3 makes this call, ignore it. We'll sync the data in afs_fsync. */
935 if (AFS_NFSXLATORREQ(cred))
941 /* a freelist of one */
942 struct buf *afs_bread_freebp = 0;
945 * Only rfs_read calls this, and it only looks at bp->b_un.b_addr.
946 * Thus we can use fake bufs (ie not from the real buffer pool).
948 mp_afs_bread(vp, lbn, bpp, cred)
954 int offset, fsbsize, error;
960 AFS_STATCNT(afs_bread);
961 fsbsize = vp->v_vfsp->vfs_bsize;
962 offset = lbn * fsbsize;
963 if (afs_bread_freebp) {
964 bp = afs_bread_freebp;
965 afs_bread_freebp = 0;
967 bp = (struct buf *)AFS_KALLOC(sizeof(*bp));
968 bp->b_un.b_addr = (caddr_t) AFS_KALLOC(fsbsize);
971 iov.iov_base = bp->b_un.b_addr;
972 iov.iov_len = fsbsize;
973 uio.afsio_iov = &iov;
974 uio.afsio_iovcnt = 1;
975 uio.afsio_seg = AFS_UIOSYS;
976 uio.afsio_offset = offset;
977 uio.afsio_resid = fsbsize;
979 error = afs_read(VTOAFS(vp), &uio, cred, lbn, bpp, 0);
981 afs_bread_freebp = bp;
986 afs_bread_freebp = bp;
988 *(struct buf **)&bp->b_vp = bp; /* mark as fake */
996 mp_afs_brelse(vp, bp)
1001 AFS_STATCNT(afs_brelse);
1002 if ((struct buf *)bp->b_vp != bp) { /* not fake */
1004 } else if (afs_bread_freebp) {
1005 AFS_KFREE(bp->b_un.b_addr, vp->v_vfsp->vfs_bsize);
1006 AFS_KFREE(bp, sizeof(*bp));
1008 afs_bread_freebp = bp;
1014 mp_afs_bmap(avc, abn, anvp, anbn)
1015 register struct vcache *avc;
1016 afs_int32 abn, *anbn;
1017 struct vcache **anvp;
1020 AFS_STATCNT(afs_bmap);
1024 *anbn = abn * (8192 / DEV_BSIZE); /* in 512 byte units */
1031 mp_afs_strategy(abp)
1032 register struct buf *abp;
1034 register afs_int32 code;
1037 AFS_STATCNT(afs_strategy);
1038 code = afs_osi_MapStrategy(afs_ustrategy, abp);
1044 mp_afs_refer(vm_ubc_object_t vop)
1050 mp_afs_release(vm_ubc_object_t vop)
1056 mp_afs_write_check(vm_ubc_object_t vop, vm_page_t pp)
1063 struct vfs_ubcops afs_ubcops = {
1064 mp_afs_refer, /* refer vnode */
1065 mp_afs_release, /* release vnode */
1066 mp_afs_getpage, /* get page */
1067 mp_afs_putpage, /* put page */
1068 mp_afs_write_check, /* check writablity */
1073 * Cover function for lookup name using OSF equivalent, namei()
1075 * Note, the result vnode (ni_vp) in the namei data structure is remains
1076 * locked after return.
1078 lookupname(namep, seg, follow, dvpp, cvpp)
1079 char *namep; /* path name */
1080 int seg; /* address space containing name */
1081 int follow; /* follow symbolic links */
1082 struct vnode **dvpp; /* result, containing parent vnode */
1083 struct vnode **cvpp; /* result, containing final component vnode */
1085 /* Should I use free-bee in u-area? */
1086 struct nameidata *ndp = &u.u_nd;
1089 ndp->ni_nameiop = ((follow) ? (LOOKUP | FOLLOW) : (LOOKUP));
1090 ndp->ni_segflg = seg;
1091 ndp->ni_dirp = namep;
1094 *dvpp = ndp->ni_dvp;