2 * A large chunk of this file appears to be copied directly from
3 * sys/nfsclient/nfs_bio.c, which has the following license:
6 * Copyright (c) 1989, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Rick Macklem at The University of Guelph.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
43 * Pursuant to a statement of U.C. Berkeley dated 1999-07-22, this license
44 * is amended to drop clause (3) above.
47 #include <afsconfig.h>
48 #include <afs/param.h>
51 #include <afs/sysincludes.h> /* Standard vendor system headers */
52 #include <afsincludes.h> /* Afs-based standard headers */
53 #include <afs/afs_stats.h> /* statistics */
54 #include <sys/malloc.h>
55 #include <sys/namei.h>
56 #include <sys/unistd.h>
57 #if __FreeBSD_version >= 1000030
58 #include <sys/rwlock.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_pager.h>
63 #include <vm/vnode_pager.h>
64 extern int afs_pbuf_freecnt;
67 static vop_access_t afs_vop_access;
68 static vop_advlock_t afs_vop_advlock;
69 static vop_close_t afs_vop_close;
70 static vop_create_t afs_vop_create;
71 static vop_fsync_t afs_vop_fsync;
72 static vop_getattr_t afs_vop_getattr;
73 static vop_getpages_t afs_vop_getpages;
74 static vop_inactive_t afs_vop_inactive;
75 static vop_ioctl_t afs_vop_ioctl;
76 static vop_link_t afs_vop_link;
77 static vop_lookup_t afs_vop_lookup;
78 static vop_mkdir_t afs_vop_mkdir;
79 static vop_mknod_t afs_vop_mknod;
80 static vop_open_t afs_vop_open;
81 static vop_pathconf_t afs_vop_pathconf;
82 static vop_poll_t afs_vop_poll;
83 static vop_print_t afs_vop_print;
84 static vop_putpages_t afs_vop_putpages;
85 static vop_read_t afs_vop_read;
86 static vop_readdir_t afs_vop_readdir;
87 static vop_readlink_t afs_vop_readlink;
88 static vop_reclaim_t afs_vop_reclaim;
89 static vop_remove_t afs_vop_remove;
90 static vop_rename_t afs_vop_rename;
91 static vop_rmdir_t afs_vop_rmdir;
92 static vop_setattr_t afs_vop_setattr;
93 static vop_strategy_t afs_vop_strategy;
94 static vop_symlink_t afs_vop_symlink;
95 static vop_write_t afs_vop_write;
96 #if defined(AFS_FBSD70_ENV) && !defined(AFS_FBSD80_ENV)
97 static vop_lock1_t afs_vop_lock;
98 static vop_unlock_t afs_vop_unlock;
99 static vop_islocked_t afs_vop_islocked;
102 struct vop_vector afs_vnodeops = {
103 .vop_default = &default_vnodeops,
104 .vop_access = afs_vop_access,
105 .vop_advlock = afs_vop_advlock,
106 .vop_close = afs_vop_close,
107 .vop_create = afs_vop_create,
108 .vop_fsync = afs_vop_fsync,
109 .vop_getattr = afs_vop_getattr,
110 .vop_getpages = afs_vop_getpages,
111 .vop_inactive = afs_vop_inactive,
112 .vop_ioctl = afs_vop_ioctl,
113 #if !defined(AFS_FBSD80_ENV)
114 /* removed at least temporarily (NFSv4 flux) */
115 .vop_lease = VOP_NULL,
117 .vop_link = afs_vop_link,
118 .vop_lookup = afs_vop_lookup,
119 .vop_mkdir = afs_vop_mkdir,
120 .vop_mknod = afs_vop_mknod,
121 .vop_open = afs_vop_open,
122 .vop_pathconf = afs_vop_pathconf,
123 .vop_poll = afs_vop_poll,
124 .vop_print = afs_vop_print,
125 .vop_putpages = afs_vop_putpages,
126 .vop_read = afs_vop_read,
127 .vop_readdir = afs_vop_readdir,
128 .vop_readlink = afs_vop_readlink,
129 .vop_reclaim = afs_vop_reclaim,
130 .vop_remove = afs_vop_remove,
131 .vop_rename = afs_vop_rename,
132 .vop_rmdir = afs_vop_rmdir,
133 .vop_setattr = afs_vop_setattr,
134 .vop_strategy = afs_vop_strategy,
135 .vop_symlink = afs_vop_symlink,
136 .vop_write = afs_vop_write,
137 #if defined(AFS_FBSD70_ENV) && !defined(AFS_FBSD80_ENV)
138 .vop_lock1 = afs_vop_lock,
139 .vop_unlock = afs_vop_unlock,
140 .vop_islocked = afs_vop_islocked,
144 #else /* AFS_FBSD60_ENV */
146 int afs_vop_lookup(struct vop_lookup_args *);
147 int afs_vop_create(struct vop_create_args *);
148 int afs_vop_mknod(struct vop_mknod_args *);
149 int afs_vop_open(struct vop_open_args *);
150 int afs_vop_close(struct vop_close_args *);
151 int afs_vop_access(struct vop_access_args *);
152 int afs_vop_getattr(struct vop_getattr_args *);
153 int afs_vop_setattr(struct vop_setattr_args *);
154 int afs_vop_read(struct vop_read_args *);
155 int afs_vop_write(struct vop_write_args *);
156 int afs_vop_getpages(struct vop_getpages_args *);
157 int afs_vop_putpages(struct vop_putpages_args *);
158 int afs_vop_ioctl(struct vop_ioctl_args *);
159 static int afs_vop_pathconf(struct vop_pathconf_args *);
160 int afs_vop_poll(struct vop_poll_args *);
161 int afs_vop_fsync(struct vop_fsync_args *);
162 int afs_vop_remove(struct vop_remove_args *);
163 int afs_vop_link(struct vop_link_args *);
164 int afs_vop_rename(struct vop_rename_args *);
165 int afs_vop_mkdir(struct vop_mkdir_args *);
166 int afs_vop_rmdir(struct vop_rmdir_args *);
167 int afs_vop_symlink(struct vop_symlink_args *);
168 int afs_vop_readdir(struct vop_readdir_args *);
169 int afs_vop_readlink(struct vop_readlink_args *);
170 int afs_vop_inactive(struct vop_inactive_args *);
171 int afs_vop_reclaim(struct vop_reclaim_args *);
172 int afs_vop_bmap(struct vop_bmap_args *);
173 int afs_vop_strategy(struct vop_strategy_args *);
174 int afs_vop_print(struct vop_print_args *);
175 int afs_vop_advlock(struct vop_advlock_args *);
179 /* Global vfs data structures for AFS. */
180 vop_t **afs_vnodeop_p;
181 struct vnodeopv_entry_desc afs_vnodeop_entries[] = {
182 {&vop_default_desc, (vop_t *) vop_defaultop},
183 {&vop_access_desc, (vop_t *) afs_vop_access}, /* access */
184 {&vop_advlock_desc, (vop_t *) afs_vop_advlock}, /* advlock */
185 {&vop_bmap_desc, (vop_t *) afs_vop_bmap}, /* bmap */
186 {&vop_close_desc, (vop_t *) afs_vop_close}, /* close */
187 {&vop_createvobject_desc, (vop_t *) vop_stdcreatevobject},
188 {&vop_destroyvobject_desc, (vop_t *) vop_stddestroyvobject},
189 {&vop_create_desc, (vop_t *) afs_vop_create}, /* create */
190 {&vop_fsync_desc, (vop_t *) afs_vop_fsync}, /* fsync */
191 {&vop_getattr_desc, (vop_t *) afs_vop_getattr}, /* getattr */
192 {&vop_getpages_desc, (vop_t *) afs_vop_getpages}, /* read */
193 {&vop_getvobject_desc, (vop_t *) vop_stdgetvobject},
194 {&vop_putpages_desc, (vop_t *) afs_vop_putpages}, /* write */
195 {&vop_inactive_desc, (vop_t *) afs_vop_inactive}, /* inactive */
196 {&vop_lease_desc, (vop_t *) vop_null},
197 {&vop_link_desc, (vop_t *) afs_vop_link}, /* link */
198 {&vop_lookup_desc, (vop_t *) afs_vop_lookup}, /* lookup */
199 {&vop_mkdir_desc, (vop_t *) afs_vop_mkdir}, /* mkdir */
200 {&vop_mknod_desc, (vop_t *) afs_vop_mknod}, /* mknod */
201 {&vop_open_desc, (vop_t *) afs_vop_open}, /* open */
202 {&vop_pathconf_desc, (vop_t *) afs_vop_pathconf}, /* pathconf */
203 {&vop_poll_desc, (vop_t *) afs_vop_poll}, /* select */
204 {&vop_print_desc, (vop_t *) afs_vop_print}, /* print */
205 {&vop_read_desc, (vop_t *) afs_vop_read}, /* read */
206 {&vop_readdir_desc, (vop_t *) afs_vop_readdir}, /* readdir */
207 {&vop_readlink_desc, (vop_t *) afs_vop_readlink}, /* readlink */
208 {&vop_reclaim_desc, (vop_t *) afs_vop_reclaim}, /* reclaim */
209 {&vop_remove_desc, (vop_t *) afs_vop_remove}, /* remove */
210 {&vop_rename_desc, (vop_t *) afs_vop_rename}, /* rename */
211 {&vop_rmdir_desc, (vop_t *) afs_vop_rmdir}, /* rmdir */
212 {&vop_setattr_desc, (vop_t *) afs_vop_setattr}, /* setattr */
213 {&vop_strategy_desc, (vop_t *) afs_vop_strategy}, /* strategy */
214 {&vop_symlink_desc, (vop_t *) afs_vop_symlink}, /* symlink */
215 {&vop_write_desc, (vop_t *) afs_vop_write}, /* write */
216 {&vop_ioctl_desc, (vop_t *) afs_vop_ioctl}, /* XXX ioctl */
217 /*{ &vop_seek_desc, afs_vop_seek }, *//* seek */
218 #if defined(AFS_FBSD70_ENV) && !defined(AFS_FBSD90_ENV)
219 {&vop_lock1_desc, (vop_t *) afs_vop_lock}, /* lock */
220 {&vop_unlock_desc, (vop_t *) afs_vop_unlock}, /* unlock */
221 {&vop_islocked_desc, (vop_t *) afs_vop_islocked}, /* islocked */
225 struct vnodeopv_desc afs_vnodeop_opv_desc =
226 { &afs_vnodeop_p, afs_vnodeop_entries };
227 #endif /* AFS_FBSD60_ENV */
230 struct componentname *cnp = ap->a_cnp; \
232 MALLOC(name, char *, cnp->cn_namelen+1, M_TEMP, M_WAITOK); \
233 memcpy(name, cnp->cn_nameptr, cnp->cn_namelen); \
234 name[cnp->cn_namelen] = '\0'
236 #define DROPNAME() FREE(name, M_TEMP)
239 * Here we define compatibility functions/macros for interfaces that
240 * have changed between different FreeBSD versions.
242 #if defined(AFS_FBSD90_ENV)
243 static __inline void ma_vm_page_lock_queues(void) {};
244 static __inline void ma_vm_page_unlock_queues(void) {};
245 static __inline void ma_vm_page_lock(vm_page_t m) { vm_page_lock(m); };
246 static __inline void ma_vm_page_unlock(vm_page_t m) { vm_page_unlock(m); };
248 static __inline void ma_vm_page_lock_queues(void) { vm_page_lock_queues(); };
249 static __inline void ma_vm_page_unlock_queues(void) { vm_page_unlock_queues(); };
250 static __inline void ma_vm_page_lock(vm_page_t m) {};
251 static __inline void ma_vm_page_unlock(vm_page_t m) {};
254 #if defined(AFS_FBSD80_ENV)
255 #define ma_vn_lock(vp, flags, p) (vn_lock(vp, flags))
256 #define MA_VOP_LOCK(vp, flags, p) (VOP_LOCK(vp, flags))
257 #define MA_VOP_UNLOCK(vp, flags, p) (VOP_UNLOCK(vp, flags))
259 #define ma_vn_lock(vp, flags, p) (vn_lock(vp, flags, p))
260 #define MA_VOP_LOCK(vp, flags, p) (VOP_LOCK(vp, flags, p))
261 #define MA_VOP_UNLOCK(vp, flags, p) (VOP_UNLOCK(vp, flags, p))
264 #if defined(AFS_FBSD70_ENV)
265 #define MA_PCPU_INC(c) PCPU_INC(c)
266 #define MA_PCPU_ADD(c, n) PCPU_ADD(c, n)
268 #define MA_PCPU_INC(c) PCPU_LAZY_INC(c)
269 #define MA_PCPU_ADD(c, n) (c) += (n)
272 #if __FreeBSD_version >= 1000030
273 #define AFS_VM_OBJECT_WLOCK(o) VM_OBJECT_WLOCK(o)
274 #define AFS_VM_OBJECT_WUNLOCK(o) VM_OBJECT_WUNLOCK(o)
276 #define AFS_VM_OBJECT_WLOCK(o) VM_OBJECT_LOCK(o)
277 #define AFS_VM_OBJECT_WUNLOCK(o) VM_OBJECT_UNLOCK(o)
280 #ifdef AFS_FBSD70_ENV
281 #ifndef AFS_FBSD80_ENV
282 /* From kern_lock.c */
283 #define COUNT(td, x) if ((td)) (td)->td_locks += (x)
284 #define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \
285 LK_SHARE_NONZERO | LK_WAIT_NONZERO)
288 sharelock(struct thread *td, struct lock *lkp, int incr) {
289 lkp->lk_flags |= LK_SHARE_NONZERO;
290 lkp->lk_sharecount += incr;
296 * Standard lock, unlock and islocked functions.
300 struct vop_lock1_args /* {
308 struct vnode *vp = ap->a_vp;
309 struct lock *lkp = vp->v_vnlock;
311 #if 0 && defined(AFS_FBSD80_ENV) && !defined(UKERNEL)
312 afs_warn("afs_vop_lock: tid %d pid %d \"%s\"\n", curthread->td_tid,
313 curthread->td_proc->p_pid, curthread->td_name);
317 #ifdef AFS_FBSD80_ENV
318 return (_lockmgr_args(lkp, ap->a_flags, VI_MTX(vp),
319 LK_WMESG_DEFAULT, LK_PRIO_DEFAULT, LK_TIMO_DEFAULT,
320 ap->a_file, ap->a_line));
322 return (_lockmgr(lkp, ap->a_flags, VI_MTX(vp), ap->a_td, ap->a_file, ap->a_line));
329 struct vop_unlock_args /* {
335 struct vnode *vp = ap->a_vp;
336 struct lock *lkp = vp->v_vnlock;
338 #ifdef AFS_FBSD80_ENV
341 op = ((ap->a_flags) | LK_RELEASE) & LK_TYPE_MASK;
342 int glocked = ISAFS_GLOCK();
345 if ((op & (op - 1)) != 0) {
346 afs_warn("afs_vop_unlock: Shit.\n");
349 code = lockmgr(lkp, ap->a_flags | LK_RELEASE, VI_MTX(vp));
355 /* possibly in current code path where this
356 * forces trace, we should have had a (shared? not
357 * necessarily, see _lockmgr in kern_lock.c) lock
358 * and that's the real bug. but.
361 if ((lkp->lk_exclusivecount == 0) &&
362 (!(lkp->lk_flags & LK_SHARE_NONZERO))) {
363 sharelock(ap->a_td, lkp, 1);
366 return (lockmgr(lkp, ap->a_flags | LK_RELEASE, VI_MTX(vp),
374 struct vop_islocked_args /* {
376 struct thread *a_td; (not in 80)
379 #ifdef AFS_FBSD80_ENV
380 return (lockstatus(ap->a_vp->v_vnlock));
382 return (lockstatus(ap->a_vp->v_vnlock, ap->a_td));
388 * Mosty copied from sys/ufs/ufs/ufs_vnops.c:ufs_pathconf().
389 * We should know the correct answers to these questions with
390 * respect to the AFS protocol (which may differ from the UFS
391 * values) but for the moment this will do.
394 afs_vop_pathconf(struct vop_pathconf_args *ap)
399 switch (ap->a_name) {
401 *ap->a_retval = LINK_MAX;
404 *ap->a_retval = NAME_MAX;
407 *ap->a_retval = PATH_MAX;
410 *ap->a_retval = PIPE_BUF;
412 case _PC_CHOWN_RESTRICTED:
418 #ifdef _PC_ACL_EXTENDED
419 case _PC_ACL_EXTENDED:
422 case _PC_ACL_PATH_MAX:
426 #ifdef _PC_MAC_PRESENT
427 case _PC_MAC_PRESENT:
433 /* _PC_ASYNC_IO should have been handled by upper layers. */
434 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
444 #ifdef _PC_ALLOC_SIZE_MIN
445 case _PC_ALLOC_SIZE_MIN:
446 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
449 #ifdef _PC_FILESIZEBITS
450 case _PC_FILESIZEBITS:
451 *ap->a_retval = 32; /* XXX */
454 #ifdef _PC_REC_INCR_XFER_SIZE
455 case _PC_REC_INCR_XFER_SIZE:
456 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
458 case _PC_REC_MAX_XFER_SIZE:
459 *ap->a_retval = -1; /* means ``unlimited'' */
461 case _PC_REC_MIN_XFER_SIZE:
462 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
464 case _PC_REC_XFER_ALIGN:
465 *ap->a_retval = PAGE_SIZE;
468 #ifdef _PC_SYMLINK_MAX
469 case _PC_SYMLINK_MAX:
470 *ap->a_retval = MAXPATHLEN;
482 struct vop_lookup_args /* {
483 * struct vnodeop_desc * a_desc;
484 * struct vnode *a_dvp;
485 * struct vnode **a_vpp;
486 * struct componentname *a_cnp;
491 struct vnode *vp, *dvp;
492 int flags = ap->a_cnp->cn_flags;
493 int lockparent; /* 1 => lockparent flag is set */
494 int wantparent; /* 1 => wantparent or lockparent flag */
495 #ifndef AFS_FBSD80_ENV
496 struct thread *p = ap->a_cnp->cn_thread;
500 if (dvp->v_type != VDIR) {
501 #ifndef AFS_FBSD70_ENV
507 if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT))
512 lockparent = flags & LOCKPARENT;
513 wantparent = flags & (LOCKPARENT | WANTPARENT);
515 #if __FreeBSD_version < 1000021
516 cnp->cn_flags |= MPSAFE; /* steel */
519 if (flags & ISDOTDOT)
520 MA_VOP_UNLOCK(dvp, 0, p);
523 error = afs_lookup(VTOAFS(dvp), name, &vcp, cnp->cn_cred);
527 if (flags & ISDOTDOT)
528 MA_VOP_LOCK(dvp, LK_EXCLUSIVE | LK_RETRY, p);
529 if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)
530 && (flags & ISLASTCN) && error == ENOENT)
532 if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
533 cnp->cn_flags |= SAVENAME;
538 vp = AFSTOV(vcp); /* always get a node if no error */
540 /* The parent directory comes in locked. We unlock it on return
541 * unless the caller wants it left locked.
542 * we also always return the vnode locked. */
544 if (flags & ISDOTDOT) {
545 /* vp before dvp since we go root to leaf, and .. comes first */
546 ma_vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
547 ma_vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
548 /* always return the child locked */
549 if (lockparent && (flags & ISLASTCN)
550 && (error = ma_vn_lock(dvp, LK_EXCLUSIVE, p))) {
555 } else if (vp == dvp) {
556 /* they're the same; afs_lookup() already ref'ed the leaf.
557 * It came in locked, so we don't need to ref OR lock it */
559 if (!lockparent || !(flags & ISLASTCN)) {
560 #ifndef AFS_FBSD70_ENV /* 6 too? */
561 MA_VOP_UNLOCK(dvp, 0, p); /* done with parent. */
564 ma_vn_lock(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, p);
565 /* always return the child locked */
569 if ((cnp->cn_nameiop == RENAME && wantparent && (flags & ISLASTCN))
570 || (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN)))
571 cnp->cn_flags |= SAVENAME;
579 struct vop_create_args /* {
580 * struct vnode *a_dvp;
581 * struct vnode **a_vpp;
582 * struct componentname *a_cnp;
583 * struct vattr *a_vap;
588 struct vnode *dvp = ap->a_dvp;
589 #ifndef AFS_FBSD80_ENV
590 struct thread *p = ap->a_cnp->cn_thread;
596 afs_create(VTOAFS(dvp), name, ap->a_vap,
597 ap->a_vap->va_vaflags & VA_EXCLUSIVE ? EXCL : NONEXCL,
598 ap->a_vap->va_mode, &vcp, cnp->cn_cred);
606 *ap->a_vpp = AFSTOV(vcp);
607 ma_vn_lock(AFSTOV(vcp), LK_EXCLUSIVE | LK_RETRY, p);
617 struct vop_mknod_args /* {
618 * struct vnode *a_dvp;
619 * struct vnode **a_vpp;
620 * struct componentname *a_cnp;
621 * struct vattr *a_vap;
629 validate_vops(struct vnode *vp, int after)
632 struct vnodeopv_entry_desc *this;
633 for (this = afs_vnodeop_entries; this->opve_op; this++) {
634 if (vp->v_op[this->opve_op->vdesc_offset] != this->opve_impl) {
636 printf("v_op %d ", after);
640 printf("For oper %d (%s), func is %p, not %p",
641 this->opve_op->vdesc_offset, this->opve_op->vdesc_name,
642 vp->v_op[this->opve_op->vdesc_offset], this->opve_impl);
650 struct vop_open_args /* {
651 * struct vnode *a_vp;
653 * struct ucred *a_cred;
654 * struct thread *a_td;
659 struct vcache *vc = VTOAFS(ap->a_vp);
662 error = afs_open(&vc, ap->a_mode, ap->a_cred);
664 if (AFSTOV(vc) != ap->a_vp)
665 panic("AFS open changed vnode!");
668 #ifdef AFS_FBSD60_ENV
669 vnode_create_vobject(ap->a_vp, vc->f.m.Length, ap->a_td);
671 osi_FlushPages(vc, ap->a_cred);
677 struct vop_close_args /* {
678 * struct vnode *a_vp;
680 * struct ucred *a_cred;
681 * struct thread *a_td;
685 struct vnode *vp = ap->a_vp;
686 struct vcache *avc = VTOAFS(vp);
688 #if defined(AFS_FBSD80_ENV)
690 iflag = vp->v_iflag & VI_DOOMED;
692 if (iflag & VI_DOOMED) {
693 /* osi_FlushVCache (correctly) calls vgone() on recycled vnodes, we don't
694 * have an afs_close to process, in that case */
696 panic("afs_vop_close: doomed vnode %p has vcache %p with non-zero opens %d\n",
697 vp, avc, avc->opens);
704 code = afs_close(avc, ap->a_fflag, ap->a_cred);
706 code = afs_close(avc, ap->a_fflag, afs_osi_credp);
707 osi_FlushPages(avc, ap->a_cred); /* hold GLOCK, but not basic vnode lock */
714 struct vop_access_args /* {
715 * struct vnode *a_vp;
716 * accmode_t a_accmode;
717 * struct ucred *a_cred;
718 * struct thread *a_td;
723 #if defined(AFS_FBSD80_ENV)
724 code = afs_access(VTOAFS(ap->a_vp), ap->a_accmode, ap->a_cred);
726 code = afs_access(VTOAFS(ap->a_vp), ap->a_mode, ap->a_cred);
734 struct vop_getattr_args /* {
735 * struct vnode *a_vp;
736 * struct vattr *a_vap;
737 * struct ucred *a_cred;
743 code = afs_getattr(VTOAFS(ap->a_vp), ap->a_vap, ap->a_cred);
751 struct vop_setattr_args /* {
752 * struct vnode *a_vp;
753 * struct vattr *a_vap;
754 * struct ucred *a_cred;
759 code = afs_setattr(VTOAFS(ap->a_vp), ap->a_vap, ap->a_cred);
766 struct vop_read_args /* {
767 * struct vnode *a_vp;
770 * struct ucred *a_cred;
775 struct vcache *avc = VTOAFS(ap->a_vp);
777 osi_FlushPages(avc, ap->a_cred); /* hold GLOCK, but not basic vnode lock */
778 code = afs_read(avc, ap->a_uio, ap->a_cred, 0);
783 /* struct vop_getpages_args {
784 * struct vnode *a_vp;
788 * vm_oofset_t a_offset;
792 afs_vop_getpages(struct vop_getpages_args *ap)
795 int i, nextoff, size, toff, npages;
804 memset(&uio, 0, sizeof(uio));
805 memset(&iov, 0, sizeof(iov));
809 if ((object = vp->v_object) == NULL) {
810 printf("afs_getpages: called with non-merged cache vnode??\n");
811 return VM_PAGER_ERROR;
813 npages = btoc(ap->a_count);
815 * If the requested page is partially valid, just return it and
816 * allow the pager to zero-out the blanks. Partially valid pages
817 * can only occur at the file EOF.
821 vm_page_t m = ap->a_m[ap->a_reqpage];
823 AFS_VM_OBJECT_WLOCK(object);
824 ma_vm_page_lock_queues();
826 /* handled by vm_fault now */
827 /* vm_page_zero_invalid(m, TRUE); */
828 for (i = 0; i < npages; ++i) {
829 if (i != ap->a_reqpage) {
830 ma_vm_page_lock(ap->a_m[i]);
831 vm_page_free(ap->a_m[i]);
832 ma_vm_page_unlock(ap->a_m[i]);
835 ma_vm_page_unlock_queues();
836 AFS_VM_OBJECT_WUNLOCK(object);
839 ma_vm_page_unlock_queues();
840 AFS_VM_OBJECT_WUNLOCK(object);
842 bp = getpbuf(&afs_pbuf_freecnt);
844 kva = (vm_offset_t) bp->b_data;
845 pmap_qenter(kva, ap->a_m, npages);
846 MA_PCPU_INC(cnt.v_vnodein);
847 MA_PCPU_ADD(cnt.v_vnodepgsin, npages);
849 iov.iov_base = (caddr_t) kva;
850 iov.iov_len = ap->a_count;
853 uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex);
854 uio.uio_resid = ap->a_count;
855 uio.uio_segflg = UIO_SYSSPACE;
856 uio.uio_rw = UIO_READ;
857 uio.uio_td = curthread;
860 osi_FlushPages(avc, osi_curcred()); /* hold GLOCK, but not basic vnode lock */
861 code = afs_read(avc, &uio, osi_curcred(), 0);
863 pmap_qremove(kva, npages);
865 relpbuf(bp, &afs_pbuf_freecnt);
867 if (code && (uio.uio_resid == ap->a_count)) {
868 AFS_VM_OBJECT_WLOCK(object);
869 ma_vm_page_lock_queues();
870 for (i = 0; i < npages; ++i) {
871 if (i != ap->a_reqpage)
872 vm_page_free(ap->a_m[i]);
874 ma_vm_page_unlock_queues();
875 AFS_VM_OBJECT_WUNLOCK(object);
876 return VM_PAGER_ERROR;
879 size = ap->a_count - uio.uio_resid;
880 AFS_VM_OBJECT_WLOCK(object);
881 ma_vm_page_lock_queues();
882 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
884 nextoff = toff + PAGE_SIZE;
887 /* XXX not in nfsclient? */
888 m->flags &= ~PG_ZERO;
890 if (nextoff <= size) {
892 * Read operation filled an entire page
894 m->valid = VM_PAGE_BITS_ALL;
895 #ifndef AFS_FBSD80_ENV
898 KASSERT(m->dirty == 0, ("afs_getpages: page %p is dirty", m));
900 } else if (size > toff) {
902 * Read operation filled a partial page.
905 vm_page_set_validclean(m, 0, size - toff);
906 KASSERT(m->dirty == 0, ("afs_getpages: page %p is dirty", m));
909 if (i != ap->a_reqpage) {
910 #if __FreeBSD_version >= 1000042
911 vm_page_readahead_finish(m);
914 * Whether or not to leave the page activated is up in
915 * the air, but we should put the page on a page queue
916 * somewhere (it already is in the object). Result:
917 * It appears that emperical results show that
918 * deactivating pages is best.
922 * Just in case someone was asking for this page we
923 * now tell them that it is ok to use.
926 #if defined(AFS_FBSD70_ENV)
927 if (m->oflags & VPO_WANTED) {
929 if (m->flags & PG_WANTED) {
933 ma_vm_page_unlock(m);
937 vm_page_deactivate(m);
938 ma_vm_page_unlock(m);
944 ma_vm_page_unlock(m);
946 #endif /* __FreeBSD_version 1000042 */
949 ma_vm_page_unlock_queues();
950 AFS_VM_OBJECT_WUNLOCK(object);
956 struct vop_write_args /* {
957 * struct vnode *a_vp;
960 * struct ucred *a_cred;
964 struct vcache *avc = VTOAFS(ap->a_vp);
966 osi_FlushPages(avc, ap->a_cred); /* hold GLOCK, but not basic vnode lock */
968 afs_write(VTOAFS(ap->a_vp), ap->a_uio, ap->a_ioflag, ap->a_cred, 0);
974 * struct vop_putpages_args {
975 * struct vnode *a_vp;
980 * vm_oofset_t a_offset;
984 * All of the pages passed to us in ap->a_m[] are already marked as busy,
985 * so there is no additional locking required to set their flags. -GAW
988 afs_vop_putpages(struct vop_putpages_args *ap)
991 int i, size, npages, sync;
999 memset(&uio, 0, sizeof(uio));
1000 memset(&iov, 0, sizeof(iov));
1004 /* Perhaps these two checks should just be KASSERTs instead... */
1005 if (vp->v_object == NULL) {
1006 printf("afs_putpages: called with non-merged cache vnode??\n");
1007 return VM_PAGER_ERROR; /* XXX I think this is insufficient */
1009 if (vType(avc) != VREG) {
1010 printf("afs_putpages: not VREG");
1011 return VM_PAGER_ERROR; /* XXX I think this is insufficient */
1013 npages = btoc(ap->a_count);
1014 for (i = 0; i < npages; i++)
1015 ap->a_rtvals[i] = VM_PAGER_AGAIN;
1016 bp = getpbuf(&afs_pbuf_freecnt);
1018 kva = (vm_offset_t) bp->b_data;
1019 pmap_qenter(kva, ap->a_m, npages);
1020 MA_PCPU_INC(cnt.v_vnodeout);
1021 MA_PCPU_ADD(cnt.v_vnodepgsout, ap->a_count);
1023 iov.iov_base = (caddr_t) kva;
1024 iov.iov_len = ap->a_count;
1027 uio.uio_offset = IDX_TO_OFF(ap->a_m[0]->pindex);
1028 uio.uio_resid = ap->a_count;
1029 uio.uio_segflg = UIO_SYSSPACE;
1030 uio.uio_rw = UIO_WRITE;
1031 uio.uio_td = curthread;
1033 if (ap->a_sync & VM_PAGER_PUT_SYNC)
1035 /*if (ap->a_sync & VM_PAGER_PUT_INVAL)
1036 * sync |= IO_INVAL; */
1039 code = afs_write(avc, &uio, sync, osi_curcred(), 0);
1042 pmap_qremove(kva, npages);
1043 relpbuf(bp, &afs_pbuf_freecnt);
1046 size = ap->a_count - uio.uio_resid;
1047 for (i = 0; i < round_page(size) / PAGE_SIZE; i++) {
1048 ap->a_rtvals[i] = VM_PAGER_OK;
1049 vm_page_undirty(ap->a_m[i]);
1052 return ap->a_rtvals[0];
1057 struct vop_ioctl_args /* {
1058 * struct vnode *a_vp;
1062 * struct ucred *a_cred;
1063 * struct thread *a_td;
1066 struct vcache *tvc = VTOAFS(ap->a_vp);
1069 /* in case we ever get in here... */
1071 AFS_STATCNT(afs_ioctl);
1072 if (((ap->a_command >> 8) & 0xff) == 'V') {
1073 /* This is a VICEIOCTL call */
1075 error = HandleIoctl(tvc, ap->a_command, ap->a_data);
1079 /* No-op call; just return. */
1087 struct vop_poll_args /* {
1088 * struct vnode *a_vp;
1090 * struct ucred *a_cred;
1091 * struct thread *td;
1095 * We should really check to see if I/O is possible.
1102 struct vop_fsync_args /* {
1103 * struct vnode *a_vp;
1105 * struct thread *td;
1109 struct vnode *vp = ap->a_vp;
1112 /*vflushbuf(vp, wait); */
1113 #ifdef AFS_FBSD60_ENV
1114 error = afs_fsync(VTOAFS(vp), ap->a_td->td_ucred);
1117 error = afs_fsync(VTOAFS(vp), ap->a_cred);
1119 error = afs_fsync(VTOAFS(vp), afs_osi_credp);
1127 struct vop_remove_args /* {
1128 * struct vnode *a_dvp;
1129 * struct vnode *a_vp;
1130 * struct componentname *a_cnp;
1134 struct vnode *vp = ap->a_vp;
1135 struct vnode *dvp = ap->a_dvp;
1139 error = afs_remove(VTOAFS(dvp), name, cnp->cn_cred);
1148 struct vop_link_args /* {
1149 * struct vnode *a_vp;
1150 * struct vnode *a_tdvp;
1151 * struct componentname *a_cnp;
1155 struct vnode *dvp = ap->a_tdvp;
1156 struct vnode *vp = ap->a_vp;
1157 #ifndef AFS_FBSD80_ENV
1158 struct thread *p = ap->a_cnp->cn_thread;
1162 if (dvp->v_mount != vp->v_mount) {
1166 if (vp->v_type == VDIR) {
1170 if ((error = ma_vn_lock(vp, LK_CANRECURSE | LK_EXCLUSIVE, p)) != 0) {
1174 error = afs_link(VTOAFS(vp), VTOAFS(dvp), name, cnp->cn_cred);
1177 MA_VOP_UNLOCK(vp, 0, p);
1185 struct vop_rename_args /* {
1186 * struct vnode *a_fdvp;
1187 * struct vnode *a_fvp;
1188 * struct componentname *a_fcnp;
1189 * struct vnode *a_tdvp;
1190 * struct vnode *a_tvp;
1191 * struct componentname *a_tcnp;
1195 struct componentname *fcnp = ap->a_fcnp;
1197 struct componentname *tcnp = ap->a_tcnp;
1199 struct vnode *tvp = ap->a_tvp;
1200 struct vnode *tdvp = ap->a_tdvp;
1201 struct vnode *fvp = ap->a_fvp;
1202 struct vnode *fdvp = ap->a_fdvp;
1203 #ifndef AFS_FBSD80_ENV
1204 struct thread *p = fcnp->cn_thread;
1208 * Check for cross-device rename.
1210 if ((fvp->v_mount != tdvp->v_mount)
1211 || (tvp && (fvp->v_mount != tvp->v_mount))) {
1225 * if fvp == tvp, we're just removing one name of a pair of
1226 * directory entries for the same element. convert call into rename.
1227 ( (pinched from FreeBSD 4.4's ufs_rename())
1231 if (fvp->v_type == VDIR) {
1236 /* Release destination completely. */
1240 /* Delete source. */
1243 fcnp->cn_flags &= ~MODMASK;
1244 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1245 if ((fcnp->cn_flags & SAVESTART) == 0)
1246 panic("afs_rename: lost from startdir");
1247 fcnp->cn_nameiop = DELETE;
1249 error = relookup(fdvp, &fvp, fcnp);
1256 error = VOP_REMOVE(fdvp, fvp, fcnp);
1264 if ((error = ma_vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
1267 MALLOC(fname, char *, fcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
1268 memcpy(fname, fcnp->cn_nameptr, fcnp->cn_namelen);
1269 fname[fcnp->cn_namelen] = '\0';
1270 MALLOC(tname, char *, tcnp->cn_namelen + 1, M_TEMP, M_WAITOK);
1271 memcpy(tname, tcnp->cn_nameptr, tcnp->cn_namelen);
1272 tname[tcnp->cn_namelen] = '\0';
1276 /* XXX use "from" or "to" creds? NFS uses "to" creds */
1278 afs_rename(VTOAFS(fdvp), fname, VTOAFS(tdvp), tname, tcnp->cn_cred);
1281 FREE(fname, M_TEMP);
1282 FREE(tname, M_TEMP);
1296 struct vop_mkdir_args /* {
1297 * struct vnode *a_dvp;
1298 * struct vnode **a_vpp;
1299 * struct componentname *a_cnp;
1300 * struct vattr *a_vap;
1303 struct vnode *dvp = ap->a_dvp;
1304 struct vattr *vap = ap->a_vap;
1307 #ifndef AFS_FBSD80_ENV
1308 struct thread *p = ap->a_cnp->cn_thread;
1313 if ((cnp->cn_flags & HASBUF) == 0)
1314 panic("afs_vop_mkdir: no name");
1317 error = afs_mkdir(VTOAFS(dvp), name, vap, &vcp, cnp->cn_cred);
1324 *ap->a_vpp = AFSTOV(vcp);
1325 ma_vn_lock(AFSTOV(vcp), LK_EXCLUSIVE | LK_RETRY, p);
1334 struct vop_rmdir_args /* {
1335 * struct vnode *a_dvp;
1336 * struct vnode *a_vp;
1337 * struct componentname *a_cnp;
1341 struct vnode *dvp = ap->a_dvp;
1345 error = afs_rmdir(VTOAFS(dvp), name, cnp->cn_cred);
1351 /* struct vop_symlink_args {
1352 * struct vnode *a_dvp;
1353 * struct vnode **a_vpp;
1354 * struct componentname *a_cnp;
1355 * struct vattr *a_vap;
1360 afs_vop_symlink(struct vop_symlink_args *ap)
1363 struct vnode *newvp;
1374 afs_symlink(VTOAFS(dvp), name, ap->a_vap, ap->a_target, NULL,
1377 error = afs_lookup(VTOAFS(dvp), name, &vcp, cnp->cn_cred);
1379 newvp = AFSTOV(vcp);
1380 ma_vn_lock(newvp, LK_EXCLUSIVE | LK_RETRY, cnp->cn_thread);
1385 *(ap->a_vpp) = newvp;
1391 struct vop_readdir_args /* {
1392 * struct vnode *a_vp;
1393 * struct uio *a_uio;
1394 * struct ucred *a_cred;
1396 * u_long *a_cookies;
1402 /* printf("readdir %x cookies %x ncookies %d\n", ap->a_vp, ap->a_cookies,
1404 off = ap->a_uio->uio_offset;
1407 afs_readdir(VTOAFS(ap->a_vp), ap->a_uio, ap->a_cred, ap->a_eofflag);
1409 if (!error && ap->a_ncookies != NULL) {
1410 struct uio *uio = ap->a_uio;
1411 const struct dirent *dp, *dp_start, *dp_end;
1413 u_long *cookies, *cookiep;
1415 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1416 panic("afs_readdir: burned cookies");
1417 dp = (const struct dirent *)
1418 ((const char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
1420 dp_end = (const struct dirent *)uio->uio_iov->iov_base;
1421 for (dp_start = dp, ncookies = 0; dp < dp_end;
1422 dp = (const struct dirent *)((const char *)dp + dp->d_reclen))
1425 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1427 for (dp = dp_start, cookiep = cookies; dp < dp_end;
1428 dp = (const struct dirent *)((const char *)dp + dp->d_reclen)) {
1429 off += dp->d_reclen;
1432 *ap->a_cookies = cookies;
1433 *ap->a_ncookies = ncookies;
1440 afs_vop_readlink(ap)
1441 struct vop_readlink_args /* {
1442 * struct vnode *a_vp;
1443 * struct uio *a_uio;
1444 * struct ucred *a_cred;
1448 /* printf("readlink %x\n", ap->a_vp);*/
1450 error = afs_readlink(VTOAFS(ap->a_vp), ap->a_uio, ap->a_cred);
1456 afs_vop_inactive(ap)
1457 struct vop_inactive_args /* {
1458 * struct vnode *a_vp;
1459 * struct thread *td;
1462 struct vnode *vp = ap->a_vp;
1465 afs_InactiveVCache(VTOAFS(vp), 0); /* decrs ref counts */
1467 #ifndef AFS_FBSD60_ENV
1468 MA_VOP_UNLOCK(vp, 0, ap->a_td);
1474 * struct vop_reclaim_args {
1475 * struct vnode *a_vp;
1479 afs_vop_reclaim(struct vop_reclaim_args *ap)
1481 /* copied from ../OBSD/osi_vnodeops.c:afs_nbsd_reclaim() */
1483 struct vnode *vp = ap->a_vp;
1484 struct vcache *avc = VTOAFS(vp);
1485 int haveGlock = ISAFS_GLOCK();
1486 int haveVlock = CheckLock(&afs_xvcache);
1491 ObtainWriteLock(&afs_xvcache, 901);
1492 /* reclaim the vnode and the in-memory vcache, but keep the on-disk vcache */
1493 code = afs_FlushVCache(avc, &slept);
1495 if (avc->f.states & CVInit) {
1496 avc->f.states &= ~CVInit;
1497 afs_osi_Wakeup(&avc->f.states);
1501 ReleaseWriteLock(&afs_xvcache);
1506 afs_warn("afs_vop_reclaim: afs_FlushVCache failed code %d vnode\n", code);
1510 /* basically, it must not fail */
1511 vnode_destroy_vobject(vp);
1517 #ifndef AFS_FBSD60_ENV
1520 struct vop_bmap_args /* {
1521 * struct vnode *a_vp;
1523 * struct vnode **a_vpp;
1530 *ap->a_bnp = ap->a_bn * (PAGE_SIZE / DEV_BSIZE);
1533 *ap->a_vpp = ap->a_vp;
1535 if (ap->a_runp != NULL)
1537 if (ap->a_runb != NULL)
1545 afs_vop_strategy(ap)
1546 struct vop_strategy_args /* {
1552 error = afs_ustrategy(ap->a_bp, osi_curcred());
1559 struct vop_print_args /* {
1560 * struct vnode *a_vp;
1563 struct vnode *vp = ap->a_vp;
1564 struct vcache *vc = VTOAFS(ap->a_vp);
1565 int s = vc->f.states;
1567 printf("vc %p vp %p tag %s, fid: %d.%d.%d.%d, opens %d, writers %d", vc, vp, vp->v_tag,
1568 (int)vc->f.fid.Cell, (u_int) vc->f.fid.Fid.Volume,
1569 (u_int) vc->f.fid.Fid.Vnode, (u_int) vc->f.fid.Fid.Unique, vc->opens,
1570 vc->execsOrWriters);
1571 printf("\n states%s%s%s%s%s", (s & CStatd) ? " statd" : "",
1572 (s & CRO) ? " readonly" : "", (s & CDirty) ? " dirty" : "",
1573 (s & CMAPPED) ? " mapped" : "",
1574 (s & CVFlushed) ? " flush in progress" : "");
1580 * Advisory record locking support (fcntl() POSIX style)
1584 struct vop_advlock_args /* {
1585 * struct vnode *a_vp;
1588 * struct flock *a_fl;
1593 struct ucred cr = *osi_curcred();
1597 afs_lockctl(VTOAFS(ap->a_vp),
1600 (int)(intptr_t)ap->a_id); /* XXX: no longer unique! */