2 * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * Author: Umesh Vaishampayan [umeshv@apple.com]
25 * 05-Aug-1999 umeshv Created.
27 * Functions related to Unified Buffer cache.
29 * Caller of UBC functions MUST have a valid reference on the vnode.
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/mount.h>
42 #include <sys/vnode.h>
44 #include <sys/ucred.h>
48 #include <mach/mach_types.h>
49 #include <mach/memory_object_types.h>
51 #include <kern/zalloc.h>
57 #define assert(cond) \
58 ((void) ((cond) ? 0 : panic("%s:%d (%s)", __FILE__, __LINE__, # cond)))
60 #include <kern/assert.h>
61 #endif /* DIAGNOSTIC */
63 struct zone
*ubc_info_zone
;
65 /* lock for changes to struct UBC */
66 static __inline__
void
67 ubc_lock(struct vnode
*vp
)
69 /* For now, just use the v_interlock */
70 simple_lock(&vp
->v_interlock
);
74 static __inline__
void
75 ubc_unlock(struct vnode
*vp
)
77 /* For now, just use the v_interlock */
78 simple_unlock(&vp
->v_interlock
);
82 * Serialize the requests to the VM
85 * 1 - Sucessful in acquiring the lock
86 * 2 - Sucessful in acquiring the lock recursively
87 * do not call ubc_unbusy()
88 * [This is strange, but saves 4 bytes in struct ubc_info]
91 ubc_busy(struct vnode
*vp
)
93 register struct ubc_info
*uip
;
95 if (!UBCINFOEXISTS(vp
))
100 while (ISSET(uip
->ui_flags
, UI_BUSY
)) {
102 if (uip
->ui_owner
== (void *)current_thread())
105 SET(uip
->ui_flags
, UI_WANTED
);
106 (void) tsleep((caddr_t
)&vp
->v_ubcinfo
, PINOD
, "ubcbusy", 0);
108 if (!UBCINFOEXISTS(vp
))
111 uip
->ui_owner
= (void *)current_thread();
113 SET(uip
->ui_flags
, UI_BUSY
);
119 ubc_unbusy(struct vnode
*vp
)
121 register struct ubc_info
*uip
;
123 if (!UBCINFOEXISTS(vp
)) {
124 wakeup((caddr_t
)&vp
->v_ubcinfo
);
128 CLR(uip
->ui_flags
, UI_BUSY
);
129 uip
->ui_owner
= (void *)NULL
;
131 if (ISSET(uip
->ui_flags
, UI_WANTED
)) {
132 CLR(uip
->ui_flags
, UI_WANTED
);
133 wakeup((caddr_t
)&vp
->v_ubcinfo
);
138 * Initialization of the zone for Unified Buffer Cache.
140 __private_extern__
void
145 i
= (vm_size_t
) sizeof (struct ubc_info
);
146 /* XXX the number of elements should be tied in to maxvnodes */
147 ubc_info_zone
= zinit (i
, 10000*i
, 8192, "ubc_info zone");
152 * Initialize a ubc_info structure for a vnode.
155 ubc_info_init(struct vnode
*vp
)
157 register struct ubc_info
*uip
;
160 struct proc
*p
= current_proc();
163 memory_object_control_t control
;
169 if (ISSET(vp
->v_flag
, VUINIT
)) {
171 * other thread is already doing this
174 while (ISSET(vp
->v_flag
, VUINIT
)) {
175 SET(vp
->v_flag
, VUWANT
); /* XXX overloaded! */
177 (void) tsleep((caddr_t
)vp
, PINOD
, "ubcinfo", 0);
183 SET(vp
->v_flag
, VUINIT
);
187 if ((uip
== UBC_INFO_NULL
) || (uip
== UBC_NOINFO
)) {
189 uip
= (struct ubc_info
*) zalloc(ubc_info_zone
);
190 uip
->ui_pager
= MEMORY_OBJECT_NULL
;
191 uip
->ui_control
= MEMORY_OBJECT_CONTROL_NULL
;
192 uip
->ui_flags
= UI_INITED
;
194 uip
->ui_ucred
= NOCRED
;
195 uip
->ui_refcount
= 1;
198 uip
->ui_owner
= (void *)NULL
;
203 Debugger("ubc_info_init: already");
204 #endif /* DIAGNOSTIC */
206 assert(uip
->ui_flags
!= UI_NONE
);
207 assert(uip
->ui_vnode
== vp
);
210 if(ISSET(uip
->ui_flags
, UI_HASPAGER
))
214 /* now set this ubc_info in the vnode */
216 SET(uip
->ui_flags
, UI_HASPAGER
);
218 pager
= (void *)vnode_pager_setup(vp
, uip
->ui_pager
);
220 ubc_setpager(vp
, pager
);
223 * Note: We can not use VOP_GETATTR() to get accurate
224 * value of ui_size. Thanks to NFS.
225 * nfs_getattr() can call vinvalbuf() and in this case
226 * ubc_info is not set up to deal with that.
231 * create a vnode - vm_object association
232 * memory_object_create_named() creates a "named" reference on the
233 * memory object we hold this reference as long as the vnode is
234 * "alive." Since memory_object_create_named() took its own reference
235 * on the vnode pager we passed it, we can drop the reference
236 * vnode_pager_setup() returned here.
238 kret
= memory_object_create_named(pager
,
239 (memory_object_size_t
)uip
->ui_size
, &control
);
240 vnode_pager_deallocate(pager
);
241 if (kret
!= KERN_SUCCESS
)
242 panic("ubc_info_init: memory_object_create_named returned %d", kret
);
245 uip
->ui_control
= control
; /* cache the value of the mo control */
246 SET(uip
->ui_flags
, UI_HASOBJREF
); /* with a named reference */
247 /* create a pager reference on the vnode */
248 error
= vnode_pager_vget(vp
);
250 panic("ubc_info_init: vnode_pager_vget error = %d", error
);
252 /* initialize the size */
253 error
= VOP_GETATTR(vp
, &vattr
, p
->p_ucred
, p
);
256 uip
->ui_size
= (error
? 0: vattr
.va_size
);
259 CLR(vp
->v_flag
, VUINIT
);
260 if (ISSET(vp
->v_flag
, VUWANT
)) {
261 CLR(vp
->v_flag
, VUWANT
);
270 /* Free the ubc_info */
272 ubc_info_free(struct ubc_info
*uip
)
276 credp
= uip
->ui_ucred
;
277 if (credp
!= NOCRED
) {
278 uip
->ui_ucred
= NOCRED
;
282 if (uip
->ui_control
!= MEMORY_OBJECT_CONTROL_NULL
)
283 memory_object_control_deallocate(uip
->ui_control
);
285 zfree(ubc_info_zone
, (vm_offset_t
)uip
);
290 ubc_info_deallocate(struct ubc_info
*uip
)
293 assert(uip
->ui_refcount
> 0);
295 if (uip
->ui_refcount
-- == 1) {
299 if (ISSET(uip
->ui_flags
, UI_WANTED
)) {
300 CLR(uip
->ui_flags
, UI_WANTED
);
301 wakeup((caddr_t
)&vp
->v_ubcinfo
);
309 * Communicate with VM the size change of the file
310 * returns 1 on success, 0 on failure
313 ubc_setsize(struct vnode
*vp
, off_t nsize
)
315 off_t osize
; /* ui_size before change */
316 off_t lastpg
, olastpgend
, lastoff
;
317 struct ubc_info
*uip
;
318 memory_object_control_t control
;
321 assert(nsize
>= (off_t
)0);
326 if (!UBCINFOEXISTS(vp
))
330 osize
= uip
->ui_size
; /* call ubc_getsize() ??? */
331 /* Update the size before flushing the VM */
332 uip
->ui_size
= nsize
;
334 if (nsize
>= osize
) /* Nothing more to do */
335 return (1); /* return success */
338 * When the file shrinks, invalidate the pages beyond the
339 * new size. Also get rid of garbage beyond nsize on the
340 * last page. The ui_size already has the nsize. This
341 * insures that the pageout would not write beyond the new
345 lastpg
= trunc_page_64(nsize
);
346 olastpgend
= round_page_64(osize
);
347 control
= uip
->ui_control
;
349 lastoff
= (nsize
& PAGE_MASK_64
);
352 * If length is multiple of page size, we should not flush
353 * invalidating is sufficient
356 /* invalidate last page and old contents beyond nsize */
357 kret
= memory_object_lock_request(control
,
358 (memory_object_offset_t
)lastpg
,
359 (memory_object_size_t
)(olastpgend
- lastpg
),
360 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
362 if (kret
!= KERN_SUCCESS
)
363 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
365 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
368 /* flush the last page */
369 kret
= memory_object_lock_request(control
,
370 (memory_object_offset_t
)lastpg
,
372 MEMORY_OBJECT_RETURN_DIRTY
, FALSE
,
375 if (kret
== KERN_SUCCESS
) {
376 /* invalidate last page and old contents beyond nsize */
377 kret
= memory_object_lock_request(control
,
378 (memory_object_offset_t
)lastpg
,
379 (memory_object_size_t
)(olastpgend
- lastpg
),
380 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
382 if (kret
!= KERN_SUCCESS
)
383 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
385 printf("ubc_setsize: flush failed (error = %d)\n", kret
);
387 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
391 * Get the size of the file
394 ubc_getsize(struct vnode
*vp
)
396 return (vp
->v_ubcinfo
->ui_size
);
400 * Caller indicate that the object corresponding to the vnode
401 * can not be cached in object cache. Make it so.
402 * returns 1 on success, 0 on failure
405 ubc_uncache(struct vnode
*vp
)
408 struct ubc_info
*uip
;
410 memory_object_control_t control
;
411 memory_object_perf_info_data_t perf
;
413 if (!UBCINFOEXISTS(vp
))
416 if ((recursed
= ubc_busy(vp
)) == 0)
421 assert(uip
!= UBC_INFO_NULL
);
424 * AGE it so that vfree() can make sure that it
425 * would get recycled soon after the last reference is gone
426 * This will insure that .nfs turds would not linger
430 /* set the "do not cache" bit */
431 SET(uip
->ui_flags
, UI_DONTCACHE
);
433 control
= uip
->ui_control
;
436 perf
.cluster_size
= PAGE_SIZE
; /* XXX use real cluster_size. */
437 perf
.may_cache
= FALSE
;
438 kret
= memory_object_change_attributes(control
,
439 MEMORY_OBJECT_PERFORMANCE_INFO
,
440 (memory_object_info_t
) &perf
,
441 MEMORY_OBJECT_PERF_INFO_COUNT
);
443 if (kret
!= KERN_SUCCESS
) {
444 printf("ubc_uncache: memory_object_change_attributes_named "
451 ubc_release_named(vp
);
459 * call ubc_clean() and ubc_uncache() on all the vnodes
460 * for this mount point.
461 * returns 1 on success, 0 on failure
463 __private_extern__
int
464 ubc_umount(struct mount
*mp
)
466 struct proc
*p
= current_proc();
467 struct vnode
*vp
, *nvp
;
471 simple_lock(&mntvnode_slock
);
472 for (vp
= mp
->mnt_vnodelist
.lh_first
; vp
; vp
= nvp
) {
473 if (vp
->v_mount
!= mp
) {
474 simple_unlock(&mntvnode_slock
);
477 nvp
= vp
->v_mntvnodes
.le_next
;
478 simple_unlock(&mntvnode_slock
);
479 if (UBCINFOEXISTS(vp
)) {
482 * Must get a valid reference on the vnode
483 * before callig UBC functions
485 if (vget(vp
, 0, p
)) {
487 simple_lock(&mntvnode_slock
);
488 continue; /* move on to the next vnode */
490 ret
&= ubc_clean(vp
, 0); /* do not invalidate */
491 ret
&= ubc_uncache(vp
);
494 simple_lock(&mntvnode_slock
);
496 simple_unlock(&mntvnode_slock
);
501 * Call ubc_unmount() for all filesystems.
502 * The list is traversed in reverse order
503 * of mounting to avoid dependencies.
505 __private_extern__
void
508 struct mount
*mp
, *nmp
;
511 * Since this only runs when rebooting, it is not interlocked.
513 for (mp
= mountlist
.cqh_last
; mp
!= (void *)&mountlist
; mp
= nmp
) {
514 nmp
= mp
->mnt_list
.cqe_prev
;
515 (void) ubc_umount(mp
);
519 /* Get the credentials */
521 ubc_getcred(struct vnode
*vp
)
523 struct ubc_info
*uip
;
530 return (uip
->ui_ucred
);
534 * Set the credentials
535 * existing credentials are not changed
536 * returns 1 on success and 0 on failure
539 ubc_setcred(struct vnode
*vp
, struct proc
*p
)
541 struct ubc_info
*uip
;
549 credp
= uip
->ui_ucred
;
550 if (credp
== NOCRED
) {
552 uip
->ui_ucred
= p
->p_ucred
;
559 __private_extern__ memory_object_t
560 ubc_getpager(struct vnode
*vp
)
562 struct ubc_info
*uip
;
569 return (uip
->ui_pager
);
573 * Get the memory object associated with this vnode
574 * If the vnode was reactivated, memory object would not exist.
575 * Unless "do not rectivate" was specified, look it up using the pager.
576 * If hold was requested create an object reference of one does not
580 memory_object_control_t
581 ubc_getobject(struct vnode
*vp
, int flags
)
583 struct ubc_info
*uip
;
585 memory_object_control_t control
;
590 if ((recursed
= ubc_busy(vp
)) == 0)
594 control
= uip
->ui_control
;
596 if ((flags
& UBC_HOLDOBJECT
) && (!ISSET(uip
->ui_flags
, UI_HASOBJREF
))) {
599 * Take a temporary reference on the ubc info so that it won't go
600 * away during our recovery attempt.
605 if (memory_object_recover_named(control
, TRUE
) == KERN_SUCCESS
) {
606 SET(uip
->ui_flags
, UI_HASOBJREF
);
608 control
= MEMORY_OBJECT_CONTROL_NULL
;
612 ubc_info_deallocate(uip
);
624 ubc_setpager(struct vnode
*vp
, memory_object_t pager
)
626 struct ubc_info
*uip
;
633 uip
->ui_pager
= pager
;
638 ubc_setflags(struct vnode
* vp
, int flags
)
640 struct ubc_info
*uip
;
647 SET(uip
->ui_flags
, flags
);
653 ubc_clearflags(struct vnode
* vp
, int flags
)
655 struct ubc_info
*uip
;
662 CLR(uip
->ui_flags
, flags
);
669 ubc_issetflags(struct vnode
* vp
, int flags
)
671 struct ubc_info
*uip
;
678 return (ISSET(uip
->ui_flags
, flags
));
682 ubc_blktooff(struct vnode
*vp
, daddr_t blkno
)
690 error
= VOP_BLKTOOFF(vp
, blkno
, &file_offset
);
694 return (file_offset
);
698 ubc_offtoblk(struct vnode
*vp
, off_t offset
)
703 if (UBCINVALID(vp
)) {
704 return ((daddr_t
)-1);
707 error
= VOP_OFFTOBLK(vp
, offset
, &blkno
);
715 * Cause the file data in VM to be pushed out to the storage
716 * it also causes all currently valid pages to be released
717 * returns 1 on success, 0 on failure
720 ubc_clean(struct vnode
*vp
, int invalidate
)
723 struct ubc_info
*uip
;
724 memory_object_control_t control
;
731 if (!UBCINFOEXISTS(vp
))
735 * if invalidate was requested, write dirty data and then discard
739 flags
= (MEMORY_OBJECT_DATA_FLUSH
| MEMORY_OBJECT_DATA_NO_CHANGE
);
742 size
= uip
->ui_size
; /* call ubc_getsize() ??? */
744 control
= uip
->ui_control
;
747 vp
->v_flag
&= ~VHASDIRTY
;
750 /* Write the dirty data in the file and discard cached pages */
751 kret
= memory_object_lock_request(control
,
752 (memory_object_offset_t
)0,
753 (memory_object_size_t
)round_page_64(size
),
754 MEMORY_OBJECT_RETURN_ALL
, flags
,
757 if (kret
!= KERN_SUCCESS
)
758 printf("ubc_clean: clean failed (error = %d)\n", kret
);
760 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
764 * Cause the file data in VM to be pushed out to the storage
765 * currently valid pages are NOT invalidated
766 * returns 1 on success, 0 on failure
769 ubc_pushdirty(struct vnode
*vp
)
772 struct ubc_info
*uip
;
773 memory_object_control_t control
;
779 if (!UBCINFOEXISTS(vp
))
783 size
= uip
->ui_size
; /* call ubc_getsize() ??? */
785 control
= uip
->ui_control
;
788 vp
->v_flag
&= ~VHASDIRTY
;
791 /* Write the dirty data in the file and discard cached pages */
792 kret
= memory_object_lock_request(control
,
793 (memory_object_offset_t
)0,
794 (memory_object_size_t
)round_page_64(size
),
795 MEMORY_OBJECT_RETURN_DIRTY
, FALSE
,
798 if (kret
!= KERN_SUCCESS
)
799 printf("ubc_pushdirty: flush failed (error = %d)\n", kret
);
801 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
805 * Cause the file data in VM to be pushed out to the storage
806 * currently valid pages are NOT invalidated
807 * returns 1 on success, 0 on failure
810 ubc_pushdirty_range(struct vnode
*vp
, off_t offset
, off_t size
)
812 struct ubc_info
*uip
;
813 memory_object_control_t control
;
819 if (!UBCINFOEXISTS(vp
))
824 control
= uip
->ui_control
;
827 /* Write any dirty pages in the requested range of the file: */
828 kret
= memory_object_lock_request(control
,
829 (memory_object_offset_t
)offset
,
830 (memory_object_size_t
)round_page_64(size
),
831 MEMORY_OBJECT_RETURN_DIRTY
, FALSE
,
834 if (kret
!= KERN_SUCCESS
)
835 printf("ubc_pushdirty_range: flush failed (error = %d)\n", kret
);
837 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
841 * Make sure the vm object does not vanish
842 * returns 1 if the hold count was incremented
843 * returns 0 if the hold count was not incremented
844 * This return value should be used to balance
845 * ubc_hold() and ubc_rele().
848 ubc_hold(struct vnode
*vp
)
850 struct ubc_info
*uip
;
852 memory_object_control_t object
;
857 if ((recursed
= ubc_busy(vp
)) == 0) {
858 /* must be invalid or dying vnode */
859 assert(UBCINVALID(vp
) ||
860 ((vp
->v_flag
& VXLOCK
) || (vp
->v_flag
& VTERMINATE
)));
865 assert(uip
->ui_control
!= MEMORY_OBJECT_CONTROL_NULL
);
871 if (!ISSET(uip
->ui_flags
, UI_HASOBJREF
)) {
872 if (memory_object_recover_named(uip
->ui_control
, TRUE
)
876 ubc_info_deallocate(uip
);
879 SET(uip
->ui_flags
, UI_HASOBJREF
);
884 assert(uip
->ui_refcount
> 0);
890 * Drop the holdcount.
891 * release the reference on the vm object if the this is "uncached"
895 ubc_rele(struct vnode
*vp
)
897 struct ubc_info
*uip
;
902 if (!UBCINFOEXISTS(vp
)) {
903 /* nothing more to do for a dying vnode */
904 if ((vp
->v_flag
& VXLOCK
) || (vp
->v_flag
& VTERMINATE
))
906 panic("ubc_rele: can not");
911 if (uip
->ui_refcount
== 1)
912 panic("ubc_rele: ui_refcount");
916 if ((uip
->ui_refcount
== 1)
917 && ISSET(uip
->ui_flags
, UI_DONTCACHE
))
918 (void) ubc_release_named(vp
);
924 * The vnode is mapped explicitly, mark it so.
926 __private_extern__
void
927 ubc_map(struct vnode
*vp
)
929 struct ubc_info
*uip
;
934 if (!UBCINFOEXISTS(vp
))
940 SET(uip
->ui_flags
, UI_WASMAPPED
);
948 * Release the memory object reference on the vnode
949 * only if it is not in use
950 * Return 1 if the reference was released, 0 otherwise.
953 ubc_release_named(struct vnode
*vp
)
955 struct ubc_info
*uip
;
957 memory_object_control_t control
;
958 kern_return_t kret
= KERN_FAILURE
;
963 if ((recursed
= ubc_busy(vp
)) == 0)
967 /* can not release held or mapped vnodes */
968 if (ISSET(uip
->ui_flags
, UI_HASOBJREF
) &&
969 (uip
->ui_refcount
== 1) && !uip
->ui_mapped
) {
970 control
= uip
->ui_control
;
972 CLR(uip
->ui_flags
, UI_HASOBJREF
);
973 kret
= memory_object_release_name(control
,
974 MEMORY_OBJECT_RESPECT_CACHE
);
979 return ((kret
!= KERN_SUCCESS
) ? 0 : 1);
983 * This function used to called by extensions directly. Some may
984 * still exist with this behavior. In those cases, we will do the
985 * release as part of reclaiming or cleaning the vnode. We don't
986 * need anything explicit - so just stub this out until those callers
997 * destroy the named reference for a given vnode
999 __private_extern__
int
1003 memory_object_control_t control
;
1005 struct ubc_info
*uip
;
1009 * We may already have had the object terminated
1010 * and the ubcinfo released as a side effect of
1011 * some earlier processing. If so, pretend we did
1012 * it, because it probably was a result of our
1015 if (!UBCINFOEXISTS(vp
))
1018 uip
= vp
->v_ubcinfo
;
1020 /* can not destroy held vnodes */
1021 if (uip
->ui_refcount
> 1)
1025 * Terminate the memory object.
1026 * memory_object_destroy() will result in
1027 * vnode_pager_no_senders().
1028 * That will release the pager reference
1029 * and the vnode will move to the free list.
1031 control
= ubc_getobject(vp
, UBC_HOLDOBJECT
);
1032 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
1034 if (ISSET(vp
->v_flag
, VTERMINATE
))
1035 panic("ubc_destroy_named: already teminating");
1036 SET(vp
->v_flag
, VTERMINATE
);
1038 kret
= memory_object_destroy(control
, 0);
1039 if (kret
!= KERN_SUCCESS
)
1043 * memory_object_destroy() is asynchronous
1044 * with respect to vnode_pager_no_senders().
1045 * wait for vnode_pager_no_senders() to clear
1048 while (ISSET(vp
->v_flag
, VTERMINATE
)) {
1049 SET(vp
->v_flag
, VTERMWANT
);
1050 (void)tsleep((caddr_t
)&vp
->v_ubcinfo
,
1051 PINOD
, "ubc_destroy_named", 0);
1059 * Invalidate a range in the memory object that backs this
1060 * vnode. The offset is truncated to the page boundary and the
1061 * size is adjusted to include the last page in the range.
1064 ubc_invalidate(struct vnode
*vp
, off_t offset
, size_t size
)
1066 struct ubc_info
*uip
;
1067 memory_object_control_t control
;
1075 if (!UBCINFOEXISTS(vp
))
1078 toff
= trunc_page_64(offset
);
1079 tsize
= (size_t)(round_page_64(offset
+size
) - toff
);
1080 uip
= vp
->v_ubcinfo
;
1081 control
= uip
->ui_control
;
1084 /* invalidate pages in the range requested */
1085 kret
= memory_object_lock_request(control
,
1086 (memory_object_offset_t
)toff
,
1087 (memory_object_size_t
)tsize
,
1088 MEMORY_OBJECT_RETURN_NONE
,
1089 (MEMORY_OBJECT_DATA_NO_CHANGE
| MEMORY_OBJECT_DATA_FLUSH
),
1091 if (kret
!= KERN_SUCCESS
)
1092 printf("ubc_invalidate: invalidate failed (error = %d)\n", kret
);
1094 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
1098 * Find out whether a vnode is in use by UBC
1099 * Returns 1 if file is in use by UBC, 0 if not
1102 ubc_isinuse(struct vnode
*vp
, int tookref
)
1104 int busycount
= tookref
? 2 : 1;
1106 if (!UBCINFOEXISTS(vp
))
1110 printf("ubc_isinuse: called without a valid reference"
1111 ": v_tag = %d\v", vp
->v_tag
);
1112 vprint("ubc_isinuse", vp
);
1116 if (vp
->v_usecount
> busycount
)
1119 if ((vp
->v_usecount
== busycount
)
1120 && (vp
->v_ubcinfo
->ui_mapped
== 1))
1127 * The backdoor routine to clear the ui_mapped.
1128 * MUST only be called by the VM
1130 * Note that this routine is not called under funnel. There are numerous
1131 * things about the calling sequence that make this work on SMP.
1132 * Any code change in those paths can break this.
1135 __private_extern__
void
1136 ubc_unmap(struct vnode
*vp
)
1138 struct ubc_info
*uip
;
1139 boolean_t funnel_state
;
1144 if (!UBCINFOEXISTS(vp
))
1148 uip
= vp
->v_ubcinfo
;
1150 if ((uip
->ui_refcount
> 1) || !ISSET(uip
->ui_flags
, UI_DONTCACHE
)) {
1156 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1157 (void) ubc_release_named(vp
);
1158 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1166 vm_offset_t
*phys_entryp
,
1169 memory_object_control_t control
;
1171 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
1172 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
1173 return KERN_INVALID_ARGUMENT
;
1175 return (memory_object_page_op(control
,
1176 (memory_object_offset_t
)f_offset
,
1188 upl_page_info_t
**plp
,
1191 memory_object_control_t control
;
1196 if (bufsize
& 0xfff)
1197 return KERN_INVALID_ARGUMENT
;
1199 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
1200 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
1201 return KERN_INVALID_ARGUMENT
;
1203 uplflags
|= (UPL_NO_SYNC
|UPL_CLEAN_IN_PLACE
|UPL_SET_INTERNAL
);
1205 kr
= memory_object_upl_request(control
, f_offset
, bufsize
,
1206 uplp
, NULL
, &count
, uplflags
);
1208 *plp
= UPL_GET_INTERNAL_PAGE_LIST(*uplp
);
1216 vm_offset_t
*dst_addr
)
1218 return (vm_upl_map(kernel_map
, upl
, dst_addr
));
1226 return(vm_upl_unmap(kernel_map
, upl
));
1233 upl_page_info_t
*pl
;
1236 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1237 kr
= upl_commit(upl
, pl
, MAX_UPL_TRANSFER
);
1238 upl_deallocate(upl
);
1244 ubc_upl_commit_range(
1250 upl_page_info_t
*pl
;
1254 if (flags
& UPL_COMMIT_FREE_ON_EMPTY
)
1255 flags
|= UPL_COMMIT_NOTIFY_EMPTY
;
1257 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1259 kr
= upl_commit_range(upl
, offset
, size
, flags
,
1260 pl
, MAX_UPL_TRANSFER
, &empty
);
1262 if((flags
& UPL_COMMIT_FREE_ON_EMPTY
) && empty
)
1263 upl_deallocate(upl
);
1269 ubc_upl_abort_range(
1276 boolean_t empty
= FALSE
;
1278 if (abort_flags
& UPL_ABORT_FREE_ON_EMPTY
)
1279 abort_flags
|= UPL_ABORT_NOTIFY_EMPTY
;
1281 kr
= upl_abort_range(upl
, offset
, size
, abort_flags
, &empty
);
1283 if((abort_flags
& UPL_ABORT_FREE_ON_EMPTY
) && empty
)
1284 upl_deallocate(upl
);
1296 kr
= upl_abort(upl
, abort_type
);
1297 upl_deallocate(upl
);
1305 return (UPL_GET_INTERNAL_PAGE_LIST(upl
));