2 * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
32 * Author: Umesh Vaishampayan [umeshv@apple.com]
33 * 05-Aug-1999 umeshv Created.
35 * Functions related to Unified Buffer cache.
37 * Caller of UBC functions MUST have a valid reference on the vnode.
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/mount_internal.h>
50 #include <sys/vnode_internal.h>
51 #include <sys/ubc_internal.h>
52 #include <sys/ucred.h>
53 #include <sys/proc_internal.h>
54 #include <sys/kauth.h>
58 #include <mach/mach_types.h>
59 #include <mach/memory_object_types.h>
60 #include <mach/memory_object_control.h>
61 #include <mach/vm_map.h>
64 #include <kern/kern_types.h>
65 #include <kern/zalloc.h>
66 #include <kern/thread.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_protos.h> /* last */
74 #define assert(cond) \
75 ((void) ((cond) ? 0 : panic("%s:%d (%s)", __FILE__, __LINE__, # cond)))
77 #include <kern/assert.h>
78 #endif /* DIAGNOSTIC */
80 int ubc_info_init_internal(struct vnode
*vp
, int withfsize
, off_t filesize
);
81 int ubc_umcallback(vnode_t
, void *);
82 int ubc_isinuse_locked(vnode_t
, int, int);
83 int ubc_msync_internal(vnode_t
, off_t
, off_t
, off_t
*, int, int *);
85 struct zone
*ubc_info_zone
;
88 * Initialization of the zone for Unified Buffer Cache.
90 __private_extern__
void
95 i
= (vm_size_t
) sizeof (struct ubc_info
);
96 /* XXX the number of elements should be tied in to maxvnodes */
97 ubc_info_zone
= zinit (i
, 10000*i
, 8192, "ubc_info zone");
102 * Initialize a ubc_info structure for a vnode.
105 ubc_info_init(struct vnode
*vp
)
107 return(ubc_info_init_internal(vp
, 0, 0));
110 ubc_info_init_withsize(struct vnode
*vp
, off_t filesize
)
112 return(ubc_info_init_internal(vp
, 1, filesize
));
116 ubc_info_init_internal(struct vnode
*vp
, int withfsize
, off_t filesize
)
118 register struct ubc_info
*uip
;
120 struct proc
*p
= current_proc();
123 memory_object_control_t control
;
127 if (uip
== UBC_INFO_NULL
) {
129 uip
= (struct ubc_info
*) zalloc(ubc_info_zone
);
130 bzero((char *)uip
, sizeof(struct ubc_info
));
133 uip
->ui_flags
= UI_INITED
;
134 uip
->ui_ucred
= NOCRED
;
138 Debugger("ubc_info_init: already");
139 #endif /* DIAGNOSTIC */
141 assert(uip
->ui_flags
!= UI_NONE
);
142 assert(uip
->ui_vnode
== vp
);
144 /* now set this ubc_info in the vnode */
147 pager
= (void *)vnode_pager_setup(vp
, uip
->ui_pager
);
150 SET(uip
->ui_flags
, UI_HASPAGER
);
151 uip
->ui_pager
= pager
;
154 * Note: We can not use VNOP_GETATTR() to get accurate
155 * value of ui_size. Thanks to NFS.
156 * nfs_getattr() can call vinvalbuf() and in this case
157 * ubc_info is not set up to deal with that.
162 * create a vnode - vm_object association
163 * memory_object_create_named() creates a "named" reference on the
164 * memory object we hold this reference as long as the vnode is
165 * "alive." Since memory_object_create_named() took its own reference
166 * on the vnode pager we passed it, we can drop the reference
167 * vnode_pager_setup() returned here.
169 kret
= memory_object_create_named(pager
,
170 (memory_object_size_t
)uip
->ui_size
, &control
);
171 vnode_pager_deallocate(pager
);
172 if (kret
!= KERN_SUCCESS
)
173 panic("ubc_info_init: memory_object_create_named returned %d", kret
);
176 uip
->ui_control
= control
; /* cache the value of the mo control */
177 SET(uip
->ui_flags
, UI_HASOBJREF
); /* with a named reference */
179 /* create a pager reference on the vnode */
180 error
= vnode_pager_vget(vp
);
182 panic("ubc_info_init: vnode_pager_vget error = %d", error
);
184 if (withfsize
== 0) {
185 struct vfs_context context
;
186 /* initialize the size */
188 context
.vc_ucred
= kauth_cred_get();
189 error
= vnode_size(vp
, &uip
->ui_size
, &context
);
193 uip
->ui_size
= filesize
;
195 vp
->v_lflag
|= VNAMED_UBC
;
200 /* Free the ubc_info */
202 ubc_info_free(struct ubc_info
*uip
)
206 credp
= uip
->ui_ucred
;
207 if (credp
!= NOCRED
) {
208 uip
->ui_ucred
= NOCRED
;
209 kauth_cred_rele(credp
);
212 if (uip
->ui_control
!= MEMORY_OBJECT_CONTROL_NULL
)
213 memory_object_control_deallocate(uip
->ui_control
);
215 cluster_release(uip
);
217 zfree(ubc_info_zone
, (vm_offset_t
)uip
);
222 ubc_info_deallocate(struct ubc_info
*uip
)
228 * Communicate with VM the size change of the file
229 * returns 1 on success, 0 on failure
232 ubc_setsize(struct vnode
*vp
, off_t nsize
)
234 off_t osize
; /* ui_size before change */
235 off_t lastpg
, olastpgend
, lastoff
;
236 struct ubc_info
*uip
;
237 memory_object_control_t control
;
240 if (nsize
< (off_t
)0)
243 if (!UBCINFOEXISTS(vp
))
247 osize
= uip
->ui_size
; /* call ubc_getsize() ??? */
248 /* Update the size before flushing the VM */
249 uip
->ui_size
= nsize
;
251 if (nsize
>= osize
) /* Nothing more to do */
252 return (1); /* return success */
255 * When the file shrinks, invalidate the pages beyond the
256 * new size. Also get rid of garbage beyond nsize on the
257 * last page. The ui_size already has the nsize. This
258 * insures that the pageout would not write beyond the new
262 lastpg
= trunc_page_64(nsize
);
263 olastpgend
= round_page_64(osize
);
264 control
= uip
->ui_control
;
266 lastoff
= (nsize
& PAGE_MASK_64
);
269 * If length is multiple of page size, we should not flush
270 * invalidating is sufficient
273 /* invalidate last page and old contents beyond nsize */
274 kret
= memory_object_lock_request(control
,
275 (memory_object_offset_t
)lastpg
,
276 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
277 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
279 if (kret
!= KERN_SUCCESS
)
280 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
282 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
285 /* flush the last page */
286 kret
= memory_object_lock_request(control
,
287 (memory_object_offset_t
)lastpg
,
288 PAGE_SIZE_64
, NULL
, NULL
,
289 MEMORY_OBJECT_RETURN_DIRTY
, FALSE
,
292 if (kret
== KERN_SUCCESS
) {
293 /* invalidate last page and old contents beyond nsize */
294 kret
= memory_object_lock_request(control
,
295 (memory_object_offset_t
)lastpg
,
296 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
297 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
299 if (kret
!= KERN_SUCCESS
)
300 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
302 printf("ubc_setsize: flush failed (error = %d)\n", kret
);
304 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
308 * Get the size of the file
311 ubc_getsize(struct vnode
*vp
)
313 /* people depend on the side effect of this working this way
314 * as they call this for directory
316 if (!UBCINFOEXISTS(vp
))
318 return (vp
->v_ubcinfo
->ui_size
);
322 * call ubc_sync_range(vp, 0, EOF, UBC_PUSHALL) on all the vnodes
323 * for this mount point.
324 * returns 1 on success, 0 on failure
327 __private_extern__
int
328 ubc_umount(struct mount
*mp
)
330 vnode_iterate(mp
, 0, ubc_umcallback
, 0);
335 ubc_umcallback(vnode_t vp
, __unused
void * args
)
338 if (UBCINFOEXISTS(vp
)) {
342 (void) ubc_msync(vp
, (off_t
)0, ubc_getsize(vp
), NULL
, UBC_PUSHALL
);
344 return (VNODE_RETURNED
);
349 /* Get the credentials */
351 ubc_getcred(struct vnode
*vp
)
353 if (UBCINFOEXISTS(vp
))
354 return (vp
->v_ubcinfo
->ui_ucred
);
360 ubc_setthreadcred(struct vnode
*vp
, struct proc
*p
, thread_t thread
)
362 struct ubc_info
*uip
;
364 struct uthread
*uthread
= get_bsdthread_info(thread
);
366 if (!UBCINFOEXISTS(vp
))
372 credp
= uip
->ui_ucred
;
374 if (credp
== NOCRED
) {
375 /* use per-thread cred, if assumed identity, else proc cred */
376 if (uthread
== NULL
|| (uthread
->uu_flag
& UT_SETUID
) == 0) {
377 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
379 uip
->ui_ucred
= uthread
->uu_ucred
;
380 kauth_cred_ref(uip
->ui_ucred
);
389 * Set the credentials
390 * existing credentials are not changed
391 * returns 1 on success and 0 on failure
394 ubc_setcred(struct vnode
*vp
, struct proc
*p
)
396 struct ubc_info
*uip
;
399 if ( !UBCINFOEXISTS(vp
))
405 credp
= uip
->ui_ucred
;
407 if (credp
== NOCRED
) {
408 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
416 __private_extern__ memory_object_t
417 ubc_getpager(struct vnode
*vp
)
419 if (UBCINFOEXISTS(vp
))
420 return (vp
->v_ubcinfo
->ui_pager
);
426 * Get the memory object associated with this vnode
427 * If the vnode was reactivated, memory object would not exist.
428 * Unless "do not rectivate" was specified, look it up using the pager.
429 * If hold was requested create an object reference of one does not
433 memory_object_control_t
434 ubc_getobject(struct vnode
*vp
, __unused
int flags
)
436 if (UBCINFOEXISTS(vp
))
437 return((vp
->v_ubcinfo
->ui_control
));
444 ubc_blktooff(vnode_t vp
, daddr64_t blkno
)
452 error
= VNOP_BLKTOOFF(vp
, blkno
, &file_offset
);
456 return (file_offset
);
460 ubc_offtoblk(vnode_t vp
, off_t offset
)
466 return ((daddr64_t
)-1);
468 error
= VNOP_OFFTOBLK(vp
, offset
, &blkno
);
476 ubc_pages_resident(vnode_t vp
)
479 boolean_t has_pages_resident
;
481 if ( !UBCINFOEXISTS(vp
))
484 kret
= memory_object_pages_resident(vp
->v_ubcinfo
->ui_control
, &has_pages_resident
);
486 if (kret
!= KERN_SUCCESS
)
489 if (has_pages_resident
== TRUE
)
498 * This interface will eventually be deprecated
500 * clean and/or invalidate a range in the memory object that backs this
501 * vnode. The start offset is truncated to the page boundary and the
502 * size is adjusted to include the last page in the range.
504 * returns 1 for success, 0 for failure
507 ubc_sync_range(vnode_t vp
, off_t beg_off
, off_t end_off
, int flags
)
509 return (ubc_msync_internal(vp
, beg_off
, end_off
, NULL
, flags
, NULL
));
514 * clean and/or invalidate a range in the memory object that backs this
515 * vnode. The start offset is truncated to the page boundary and the
516 * size is adjusted to include the last page in the range.
520 ubc_msync(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
)
526 *resid_off
= beg_off
;
528 retval
= ubc_msync_internal(vp
, beg_off
, end_off
, resid_off
, flags
, &io_errno
);
530 if (retval
== 0 && io_errno
== 0)
538 * clean and/or invalidate a range in the memory object that backs this
539 * vnode. The start offset is truncated to the page boundary and the
540 * size is adjusted to include the last page in the range.
543 ubc_msync_internal(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
, int *io_errno
)
545 memory_object_size_t tsize
;
547 int request_flags
= 0;
548 int flush_flags
= MEMORY_OBJECT_RETURN_NONE
;
550 if ( !UBCINFOEXISTS(vp
))
552 if (end_off
<= beg_off
)
554 if ((flags
& (UBC_INVALIDATE
| UBC_PUSHDIRTY
| UBC_PUSHALL
)) == 0)
557 if (flags
& UBC_INVALIDATE
)
559 * discard the resident pages
561 request_flags
= (MEMORY_OBJECT_DATA_FLUSH
| MEMORY_OBJECT_DATA_NO_CHANGE
);
563 if (flags
& UBC_SYNC
)
565 * wait for all the I/O to complete before returning
567 request_flags
|= MEMORY_OBJECT_IO_SYNC
;
569 if (flags
& UBC_PUSHDIRTY
)
571 * we only return the dirty pages in the range
573 flush_flags
= MEMORY_OBJECT_RETURN_DIRTY
;
575 if (flags
& UBC_PUSHALL
)
577 * then return all the interesting pages in the range (both dirty and precious)
580 flush_flags
= MEMORY_OBJECT_RETURN_ALL
;
582 beg_off
= trunc_page_64(beg_off
);
583 end_off
= round_page_64(end_off
);
584 tsize
= (memory_object_size_t
)end_off
- beg_off
;
586 /* flush and/or invalidate pages in the range requested */
587 kret
= memory_object_lock_request(vp
->v_ubcinfo
->ui_control
,
588 beg_off
, tsize
, resid_off
, io_errno
,
589 flush_flags
, request_flags
, VM_PROT_NO_CHANGE
);
591 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
596 * The vnode is mapped explicitly, mark it so.
598 __private_extern__
int
599 ubc_map(vnode_t vp
, int flags
)
601 struct ubc_info
*uip
;
604 struct vfs_context context
;
606 if (vnode_getwithref(vp
))
609 if (UBCINFOEXISTS(vp
)) {
610 context
.vc_proc
= current_proc();
611 context
.vc_ucred
= kauth_cred_get();
613 error
= VNOP_MMAP(vp
, flags
, &context
);
623 if ( !ISSET(uip
->ui_flags
, UI_ISMAPPED
))
625 SET(uip
->ui_flags
, (UI_WASMAPPED
| UI_ISMAPPED
));
639 * destroy the named reference for a given vnode
641 __private_extern__
int
642 ubc_destroy_named(struct vnode
*vp
)
644 memory_object_control_t control
;
645 struct ubc_info
*uip
;
649 * We may already have had the object terminated
650 * and the ubcinfo released as a side effect of
651 * some earlier processing. If so, pretend we did
652 * it, because it probably was a result of our
655 if (!UBCINFOEXISTS(vp
))
661 * Terminate the memory object.
662 * memory_object_destroy() will result in
663 * vnode_pager_no_senders().
664 * That will release the pager reference
665 * and the vnode will move to the free list.
667 control
= ubc_getobject(vp
, UBC_HOLDOBJECT
);
668 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
671 * XXXXX - should we hold the vnode lock here?
673 if (ISSET(vp
->v_flag
, VTERMINATE
))
674 panic("ubc_destroy_named: already teminating");
675 SET(vp
->v_flag
, VTERMINATE
);
677 kret
= memory_object_destroy(control
, 0);
678 if (kret
!= KERN_SUCCESS
)
682 * memory_object_destroy() is asynchronous
683 * with respect to vnode_pager_no_senders().
684 * wait for vnode_pager_no_senders() to clear
688 while (ISSET(vp
->v_lflag
, VNAMED_UBC
)) {
689 (void)msleep((caddr_t
)&vp
->v_lflag
, &vp
->v_lock
,
690 PINOD
, "ubc_destroy_named", 0);
699 * Find out whether a vnode is in use by UBC
700 * Returns 1 if file is in use by UBC, 0 if not
703 ubc_isinuse(struct vnode
*vp
, int busycount
)
705 if ( !UBCINFOEXISTS(vp
))
707 return(ubc_isinuse_locked(vp
, busycount
, 0));
712 ubc_isinuse_locked(struct vnode
*vp
, int busycount
, int locked
)
720 if ((vp
->v_usecount
- vp
->v_kusecount
) > busycount
)
730 * MUST only be called by the VM
732 __private_extern__
void
733 ubc_unmap(struct vnode
*vp
)
735 struct vfs_context context
;
736 struct ubc_info
*uip
;
739 if (vnode_getwithref(vp
))
742 if (UBCINFOEXISTS(vp
)) {
746 if (ISSET(uip
->ui_flags
, UI_ISMAPPED
)) {
747 CLR(uip
->ui_flags
, UI_ISMAPPED
);
753 context
.vc_proc
= current_proc();
754 context
.vc_ucred
= kauth_cred_get();
755 (void)VNOP_MNOMAP(vp
, &context
);
761 * the drop of the vnode ref will cleanup
771 ppnum_t
*phys_entryp
,
774 memory_object_control_t control
;
776 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
777 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
778 return KERN_INVALID_ARGUMENT
;
780 return (memory_object_page_op(control
,
781 (memory_object_offset_t
)f_offset
,
787 __private_extern__ kern_return_t
788 ubc_page_op_with_control(
789 memory_object_control_t control
,
792 ppnum_t
*phys_entryp
,
795 return (memory_object_page_op(control
,
796 (memory_object_offset_t
)f_offset
,
810 memory_object_control_t control
;
812 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
813 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
814 return KERN_INVALID_ARGUMENT
;
816 return (memory_object_range_op(control
,
817 (memory_object_offset_t
)f_offset_beg
,
818 (memory_object_offset_t
)f_offset_end
,
829 upl_page_info_t
**plp
,
832 memory_object_control_t control
;
838 return KERN_INVALID_ARGUMENT
;
840 if (uplflags
& UPL_FOR_PAGEOUT
) {
841 uplflags
&= ~UPL_FOR_PAGEOUT
;
842 ubcflags
= UBC_FOR_PAGEOUT
;
844 ubcflags
= UBC_FLAGS_NONE
;
846 control
= ubc_getobject(vp
, ubcflags
);
847 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
848 return KERN_INVALID_ARGUMENT
;
850 if (uplflags
& UPL_WILL_BE_DUMPED
) {
851 uplflags
&= ~UPL_WILL_BE_DUMPED
;
852 uplflags
|= (UPL_NO_SYNC
|UPL_SET_INTERNAL
);
854 uplflags
|= (UPL_NO_SYNC
|UPL_CLEAN_IN_PLACE
|UPL_SET_INTERNAL
);
856 kr
= memory_object_upl_request(control
, f_offset
, bufsize
,
857 uplp
, NULL
, &count
, uplflags
);
859 *plp
= UPL_GET_INTERNAL_PAGE_LIST(*uplp
);
867 vm_offset_t
*dst_addr
)
869 return (vm_upl_map(kernel_map
, upl
, dst_addr
));
877 return(vm_upl_unmap(kernel_map
, upl
));
887 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
888 kr
= upl_commit(upl
, pl
, MAX_UPL_TRANSFER
);
895 ubc_upl_commit_range(
905 if (flags
& UPL_COMMIT_FREE_ON_EMPTY
)
906 flags
|= UPL_COMMIT_NOTIFY_EMPTY
;
908 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
910 kr
= upl_commit_range(upl
, offset
, size
, flags
,
911 pl
, MAX_UPL_TRANSFER
, &empty
);
913 if((flags
& UPL_COMMIT_FREE_ON_EMPTY
) && empty
)
927 boolean_t empty
= FALSE
;
929 if (abort_flags
& UPL_ABORT_FREE_ON_EMPTY
)
930 abort_flags
|= UPL_ABORT_NOTIFY_EMPTY
;
932 kr
= upl_abort_range(upl
, offset
, size
, abort_flags
, &empty
);
934 if((abort_flags
& UPL_ABORT_FREE_ON_EMPTY
) && empty
)
947 kr
= upl_abort(upl
, abort_type
);
956 return (UPL_GET_INTERNAL_PAGE_LIST(upl
));
959 /************* UBC APIS **************/
962 UBCINFOMISSING(struct vnode
* vp
)
964 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
== UBC_INFO_NULL
));
968 UBCINFORECLAIMED(struct vnode
* vp
)
970 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
== UBC_INFO_NULL
));
975 UBCINFOEXISTS(struct vnode
* vp
)
977 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
!= UBC_INFO_NULL
));
980 UBCISVALID(struct vnode
* vp
)
982 return((vp
) && ((vp
)->v_type
== VREG
) && !((vp
)->v_flag
& VSYSTEM
));
985 UBCINVALID(struct vnode
* vp
)
987 return(((vp
) == NULL
) || ((vp
) && ((vp
)->v_type
!= VREG
))
988 || ((vp
) && ((vp
)->v_flag
& VSYSTEM
)));
991 UBCINFOCHECK(const char * fun
, struct vnode
* vp
)
993 if ((vp
) && ((vp
)->v_type
== VREG
) &&
994 ((vp
)->v_ubcinfo
== UBC_INFO_NULL
)) {
995 panic("%s: lost ubc_info", (fun
));