2 * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Author: Umesh Vaishampayan [umeshv@apple.com]
31 * 05-Aug-1999 umeshv Created.
33 * Functions related to Unified Buffer cache.
35 * Caller of UBC functions MUST have a valid reference on the vnode.
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/mount_internal.h>
48 #include <sys/vnode_internal.h>
49 #include <sys/ubc_internal.h>
50 #include <sys/ucred.h>
51 #include <sys/proc_internal.h>
52 #include <sys/kauth.h>
56 #include <mach/mach_types.h>
57 #include <mach/memory_object_types.h>
58 #include <mach/memory_object_control.h>
59 #include <mach/vm_map.h>
62 #include <kern/kern_types.h>
63 #include <kern/zalloc.h>
64 #include <kern/thread.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_protos.h> /* last */
72 #define assert(cond) \
73 ((void) ((cond) ? 0 : panic("%s:%d (%s)", __FILE__, __LINE__, # cond)))
75 #include <kern/assert.h>
76 #endif /* DIAGNOSTIC */
78 int ubc_info_init_internal(struct vnode
*vp
, int withfsize
, off_t filesize
);
79 static int ubc_umcallback(vnode_t
, void *);
80 int ubc_isinuse_locked(vnode_t
, int, int);
81 static int ubc_msync_internal(vnode_t
, off_t
, off_t
, off_t
*, int, int *);
83 struct zone
*ubc_info_zone
;
86 * Initialization of the zone for Unified Buffer Cache.
88 __private_extern__
void
93 i
= (vm_size_t
) sizeof (struct ubc_info
);
94 /* XXX the number of elements should be tied in to maxvnodes */
95 ubc_info_zone
= zinit (i
, 10000*i
, 8192, "ubc_info zone");
100 * Initialize a ubc_info structure for a vnode.
103 ubc_info_init(struct vnode
*vp
)
105 return(ubc_info_init_internal(vp
, 0, 0));
108 ubc_info_init_withsize(struct vnode
*vp
, off_t filesize
)
110 return(ubc_info_init_internal(vp
, 1, filesize
));
114 ubc_info_init_internal(struct vnode
*vp
, int withfsize
, off_t filesize
)
116 register struct ubc_info
*uip
;
118 struct proc
*p
= current_proc();
121 memory_object_control_t control
;
125 if (uip
== UBC_INFO_NULL
) {
127 uip
= (struct ubc_info
*) zalloc(ubc_info_zone
);
128 bzero((char *)uip
, sizeof(struct ubc_info
));
131 uip
->ui_flags
= UI_INITED
;
132 uip
->ui_ucred
= NOCRED
;
136 Debugger("ubc_info_init: already");
137 #endif /* DIAGNOSTIC */
139 assert(uip
->ui_flags
!= UI_NONE
);
140 assert(uip
->ui_vnode
== vp
);
142 /* now set this ubc_info in the vnode */
145 pager
= (void *)vnode_pager_setup(vp
, uip
->ui_pager
);
148 SET(uip
->ui_flags
, UI_HASPAGER
);
149 uip
->ui_pager
= pager
;
152 * Note: We can not use VNOP_GETATTR() to get accurate
153 * value of ui_size. Thanks to NFS.
154 * nfs_getattr() can call vinvalbuf() and in this case
155 * ubc_info is not set up to deal with that.
160 * create a vnode - vm_object association
161 * memory_object_create_named() creates a "named" reference on the
162 * memory object we hold this reference as long as the vnode is
163 * "alive." Since memory_object_create_named() took its own reference
164 * on the vnode pager we passed it, we can drop the reference
165 * vnode_pager_setup() returned here.
167 kret
= memory_object_create_named(pager
,
168 (memory_object_size_t
)uip
->ui_size
, &control
);
169 vnode_pager_deallocate(pager
);
170 if (kret
!= KERN_SUCCESS
)
171 panic("ubc_info_init: memory_object_create_named returned %d", kret
);
174 uip
->ui_control
= control
; /* cache the value of the mo control */
175 SET(uip
->ui_flags
, UI_HASOBJREF
); /* with a named reference */
177 /* create a pager reference on the vnode */
178 error
= vnode_pager_vget(vp
);
180 panic("ubc_info_init: vnode_pager_vget error = %d", error
);
182 if (withfsize
== 0) {
183 struct vfs_context context
;
184 /* initialize the size */
186 context
.vc_ucred
= kauth_cred_get();
187 error
= vnode_size(vp
, &uip
->ui_size
, &context
);
191 uip
->ui_size
= filesize
;
193 vp
->v_lflag
|= VNAMED_UBC
;
198 /* Free the ubc_info */
200 ubc_info_free(struct ubc_info
*uip
)
202 if (IS_VALID_CRED(uip
->ui_ucred
)) {
203 kauth_cred_unref(&uip
->ui_ucred
);
206 if (uip
->ui_control
!= MEMORY_OBJECT_CONTROL_NULL
)
207 memory_object_control_deallocate(uip
->ui_control
);
209 cluster_release(uip
);
211 zfree(ubc_info_zone
, (vm_offset_t
)uip
);
216 ubc_info_deallocate(struct ubc_info
*uip
)
222 * Communicate with VM the size change of the file
223 * returns 1 on success, 0 on failure
226 ubc_setsize(struct vnode
*vp
, off_t nsize
)
228 off_t osize
; /* ui_size before change */
229 off_t lastpg
, olastpgend
, lastoff
;
230 struct ubc_info
*uip
;
231 memory_object_control_t control
;
234 if (nsize
< (off_t
)0)
237 if (!UBCINFOEXISTS(vp
))
241 osize
= uip
->ui_size
; /* call ubc_getsize() ??? */
242 /* Update the size before flushing the VM */
243 uip
->ui_size
= nsize
;
245 if (nsize
>= osize
) /* Nothing more to do */
246 return (1); /* return success */
249 * When the file shrinks, invalidate the pages beyond the
250 * new size. Also get rid of garbage beyond nsize on the
251 * last page. The ui_size already has the nsize. This
252 * insures that the pageout would not write beyond the new
256 lastpg
= trunc_page_64(nsize
);
257 olastpgend
= round_page_64(osize
);
258 control
= uip
->ui_control
;
260 lastoff
= (nsize
& PAGE_MASK_64
);
263 * If length is multiple of page size, we should not flush
264 * invalidating is sufficient
267 /* invalidate last page and old contents beyond nsize */
268 kret
= memory_object_lock_request(control
,
269 (memory_object_offset_t
)lastpg
,
270 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
271 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
273 if (kret
!= KERN_SUCCESS
)
274 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
276 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
279 /* flush the last page */
280 kret
= memory_object_lock_request(control
,
281 (memory_object_offset_t
)lastpg
,
282 PAGE_SIZE_64
, NULL
, NULL
,
283 MEMORY_OBJECT_RETURN_DIRTY
, FALSE
,
286 if (kret
== KERN_SUCCESS
) {
287 /* invalidate last page and old contents beyond nsize */
288 kret
= memory_object_lock_request(control
,
289 (memory_object_offset_t
)lastpg
,
290 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
291 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
293 if (kret
!= KERN_SUCCESS
)
294 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
296 printf("ubc_setsize: flush failed (error = %d)\n", kret
);
298 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
302 * Get the size of the file
305 ubc_getsize(struct vnode
*vp
)
307 /* people depend on the side effect of this working this way
308 * as they call this for directory
310 if (!UBCINFOEXISTS(vp
))
312 return (vp
->v_ubcinfo
->ui_size
);
316 * call ubc_sync_range(vp, 0, EOF, UBC_PUSHALL) on all the vnodes
317 * for this mount point.
318 * returns 1 on success, 0 on failure
321 __private_extern__
int
322 ubc_umount(struct mount
*mp
)
324 vnode_iterate(mp
, 0, ubc_umcallback
, 0);
329 ubc_umcallback(vnode_t vp
, __unused
void * args
)
332 if (UBCINFOEXISTS(vp
)) {
336 (void) ubc_msync(vp
, (off_t
)0, ubc_getsize(vp
), NULL
, UBC_PUSHALL
);
338 return (VNODE_RETURNED
);
343 /* Get the credentials */
345 ubc_getcred(struct vnode
*vp
)
347 if (UBCINFOEXISTS(vp
))
348 return (vp
->v_ubcinfo
->ui_ucred
);
354 ubc_setthreadcred(struct vnode
*vp
, struct proc
*p
, thread_t thread
)
356 struct ubc_info
*uip
;
358 struct uthread
*uthread
= get_bsdthread_info(thread
);
360 if (!UBCINFOEXISTS(vp
))
366 credp
= uip
->ui_ucred
;
368 if (!IS_VALID_CRED(credp
)) {
369 /* use per-thread cred, if assumed identity, else proc cred */
370 if (uthread
== NULL
|| (uthread
->uu_flag
& UT_SETUID
) == 0) {
371 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
373 uip
->ui_ucred
= uthread
->uu_ucred
;
374 kauth_cred_ref(uip
->ui_ucred
);
383 * Set the credentials
384 * existing credentials are not changed
385 * returns 1 on success and 0 on failure
388 ubc_setcred(struct vnode
*vp
, struct proc
*p
)
390 struct ubc_info
*uip
;
393 if ( !UBCINFOEXISTS(vp
))
399 credp
= uip
->ui_ucred
;
401 if (!IS_VALID_CRED(credp
)) {
402 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
410 __private_extern__ memory_object_t
411 ubc_getpager(struct vnode
*vp
)
413 if (UBCINFOEXISTS(vp
))
414 return (vp
->v_ubcinfo
->ui_pager
);
420 * Get the memory object associated with this vnode
421 * If the vnode was reactivated, memory object would not exist.
422 * Unless "do not rectivate" was specified, look it up using the pager.
423 * If hold was requested create an object reference of one does not
427 memory_object_control_t
428 ubc_getobject(struct vnode
*vp
, __unused
int flags
)
430 if (UBCINFOEXISTS(vp
))
431 return((vp
->v_ubcinfo
->ui_control
));
438 ubc_blktooff(vnode_t vp
, daddr64_t blkno
)
446 error
= VNOP_BLKTOOFF(vp
, blkno
, &file_offset
);
450 return (file_offset
);
454 ubc_offtoblk(vnode_t vp
, off_t offset
)
460 return ((daddr64_t
)-1);
462 error
= VNOP_OFFTOBLK(vp
, offset
, &blkno
);
470 ubc_pages_resident(vnode_t vp
)
473 boolean_t has_pages_resident
;
475 if ( !UBCINFOEXISTS(vp
))
478 kret
= memory_object_pages_resident(vp
->v_ubcinfo
->ui_control
, &has_pages_resident
);
480 if (kret
!= KERN_SUCCESS
)
483 if (has_pages_resident
== TRUE
)
492 * This interface will eventually be deprecated
494 * clean and/or invalidate a range in the memory object that backs this
495 * vnode. The start offset is truncated to the page boundary and the
496 * size is adjusted to include the last page in the range.
498 * returns 1 for success, 0 for failure
501 ubc_sync_range(vnode_t vp
, off_t beg_off
, off_t end_off
, int flags
)
503 return (ubc_msync_internal(vp
, beg_off
, end_off
, NULL
, flags
, NULL
));
508 * clean and/or invalidate a range in the memory object that backs this
509 * vnode. The start offset is truncated to the page boundary and the
510 * size is adjusted to include the last page in the range.
514 ubc_msync(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
)
520 *resid_off
= beg_off
;
522 retval
= ubc_msync_internal(vp
, beg_off
, end_off
, resid_off
, flags
, &io_errno
);
524 if (retval
== 0 && io_errno
== 0)
532 * clean and/or invalidate a range in the memory object that backs this
533 * vnode. The start offset is truncated to the page boundary and the
534 * size is adjusted to include the last page in the range.
537 ubc_msync_internal(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
, int *io_errno
)
539 memory_object_size_t tsize
;
541 int request_flags
= 0;
542 int flush_flags
= MEMORY_OBJECT_RETURN_NONE
;
544 if ( !UBCINFOEXISTS(vp
))
546 if (end_off
<= beg_off
)
548 if ((flags
& (UBC_INVALIDATE
| UBC_PUSHDIRTY
| UBC_PUSHALL
)) == 0)
551 if (flags
& UBC_INVALIDATE
)
553 * discard the resident pages
555 request_flags
= (MEMORY_OBJECT_DATA_FLUSH
| MEMORY_OBJECT_DATA_NO_CHANGE
);
557 if (flags
& UBC_SYNC
)
559 * wait for all the I/O to complete before returning
561 request_flags
|= MEMORY_OBJECT_IO_SYNC
;
563 if (flags
& UBC_PUSHDIRTY
)
565 * we only return the dirty pages in the range
567 flush_flags
= MEMORY_OBJECT_RETURN_DIRTY
;
569 if (flags
& UBC_PUSHALL
)
571 * then return all the interesting pages in the range (both dirty and precious)
574 flush_flags
= MEMORY_OBJECT_RETURN_ALL
;
576 beg_off
= trunc_page_64(beg_off
);
577 end_off
= round_page_64(end_off
);
578 tsize
= (memory_object_size_t
)end_off
- beg_off
;
580 /* flush and/or invalidate pages in the range requested */
581 kret
= memory_object_lock_request(vp
->v_ubcinfo
->ui_control
,
582 beg_off
, tsize
, resid_off
, io_errno
,
583 flush_flags
, request_flags
, VM_PROT_NO_CHANGE
);
585 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
590 * The vnode is mapped explicitly, mark it so.
592 __private_extern__
int
593 ubc_map(vnode_t vp
, int flags
)
595 struct ubc_info
*uip
;
598 struct vfs_context context
;
600 if (vnode_getwithref(vp
))
603 if (UBCINFOEXISTS(vp
)) {
604 context
.vc_proc
= current_proc();
605 context
.vc_ucred
= kauth_cred_get();
607 error
= VNOP_MMAP(vp
, flags
, &context
);
617 if ( !ISSET(uip
->ui_flags
, UI_ISMAPPED
))
619 SET(uip
->ui_flags
, (UI_WASMAPPED
| UI_ISMAPPED
));
633 * destroy the named reference for a given vnode
635 __private_extern__
int
636 ubc_destroy_named(struct vnode
*vp
)
638 memory_object_control_t control
;
639 struct ubc_info
*uip
;
643 * We may already have had the object terminated
644 * and the ubcinfo released as a side effect of
645 * some earlier processing. If so, pretend we did
646 * it, because it probably was a result of our
649 if (!UBCINFOEXISTS(vp
))
655 * Terminate the memory object.
656 * memory_object_destroy() will result in
657 * vnode_pager_no_senders().
658 * That will release the pager reference
659 * and the vnode will move to the free list.
661 control
= ubc_getobject(vp
, UBC_HOLDOBJECT
);
662 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
665 * XXXXX - should we hold the vnode lock here?
667 if (ISSET(vp
->v_flag
, VTERMINATE
))
668 panic("ubc_destroy_named: already teminating");
669 SET(vp
->v_flag
, VTERMINATE
);
671 kret
= memory_object_destroy(control
, 0);
672 if (kret
!= KERN_SUCCESS
)
676 * memory_object_destroy() is asynchronous
677 * with respect to vnode_pager_no_senders().
678 * wait for vnode_pager_no_senders() to clear
682 while (ISSET(vp
->v_lflag
, VNAMED_UBC
)) {
683 (void)msleep((caddr_t
)&vp
->v_lflag
, &vp
->v_lock
,
684 PINOD
, "ubc_destroy_named", 0);
693 * Find out whether a vnode is in use by UBC
694 * Returns 1 if file is in use by UBC, 0 if not
697 ubc_isinuse(struct vnode
*vp
, int busycount
)
699 if ( !UBCINFOEXISTS(vp
))
701 return(ubc_isinuse_locked(vp
, busycount
, 0));
706 ubc_isinuse_locked(struct vnode
*vp
, int busycount
, int locked
)
714 if ((vp
->v_usecount
- vp
->v_kusecount
) > busycount
)
724 * MUST only be called by the VM
726 __private_extern__
void
727 ubc_unmap(struct vnode
*vp
)
729 struct vfs_context context
;
730 struct ubc_info
*uip
;
733 if (vnode_getwithref(vp
))
736 if (UBCINFOEXISTS(vp
)) {
740 if (ISSET(uip
->ui_flags
, UI_ISMAPPED
)) {
741 CLR(uip
->ui_flags
, UI_ISMAPPED
);
747 context
.vc_proc
= current_proc();
748 context
.vc_ucred
= kauth_cred_get();
749 (void)VNOP_MNOMAP(vp
, &context
);
755 * the drop of the vnode ref will cleanup
765 ppnum_t
*phys_entryp
,
768 memory_object_control_t control
;
770 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
771 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
772 return KERN_INVALID_ARGUMENT
;
774 return (memory_object_page_op(control
,
775 (memory_object_offset_t
)f_offset
,
781 __private_extern__ kern_return_t
782 ubc_page_op_with_control(
783 memory_object_control_t control
,
786 ppnum_t
*phys_entryp
,
789 return (memory_object_page_op(control
,
790 (memory_object_offset_t
)f_offset
,
804 memory_object_control_t control
;
806 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
807 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
808 return KERN_INVALID_ARGUMENT
;
810 return (memory_object_range_op(control
,
811 (memory_object_offset_t
)f_offset_beg
,
812 (memory_object_offset_t
)f_offset_end
,
823 upl_page_info_t
**plp
,
826 memory_object_control_t control
;
832 return KERN_INVALID_ARGUMENT
;
834 if (uplflags
& UPL_FOR_PAGEOUT
) {
835 uplflags
&= ~UPL_FOR_PAGEOUT
;
836 ubcflags
= UBC_FOR_PAGEOUT
;
838 ubcflags
= UBC_FLAGS_NONE
;
840 control
= ubc_getobject(vp
, ubcflags
);
841 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
842 return KERN_INVALID_ARGUMENT
;
844 if (uplflags
& UPL_WILL_BE_DUMPED
) {
845 uplflags
&= ~UPL_WILL_BE_DUMPED
;
846 uplflags
|= (UPL_NO_SYNC
|UPL_SET_INTERNAL
);
848 uplflags
|= (UPL_NO_SYNC
|UPL_CLEAN_IN_PLACE
|UPL_SET_INTERNAL
);
850 kr
= memory_object_upl_request(control
, f_offset
, bufsize
,
851 uplp
, NULL
, &count
, uplflags
);
853 *plp
= UPL_GET_INTERNAL_PAGE_LIST(*uplp
);
861 vm_offset_t
*dst_addr
)
863 return (vm_upl_map(kernel_map
, upl
, dst_addr
));
871 return(vm_upl_unmap(kernel_map
, upl
));
881 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
882 kr
= upl_commit(upl
, pl
, MAX_UPL_TRANSFER
);
889 ubc_upl_commit_range(
899 if (flags
& UPL_COMMIT_FREE_ON_EMPTY
)
900 flags
|= UPL_COMMIT_NOTIFY_EMPTY
;
902 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
904 kr
= upl_commit_range(upl
, offset
, size
, flags
,
905 pl
, MAX_UPL_TRANSFER
, &empty
);
907 if((flags
& UPL_COMMIT_FREE_ON_EMPTY
) && empty
)
921 boolean_t empty
= FALSE
;
923 if (abort_flags
& UPL_ABORT_FREE_ON_EMPTY
)
924 abort_flags
|= UPL_ABORT_NOTIFY_EMPTY
;
926 kr
= upl_abort_range(upl
, offset
, size
, abort_flags
, &empty
);
928 if((abort_flags
& UPL_ABORT_FREE_ON_EMPTY
) && empty
)
941 kr
= upl_abort(upl
, abort_type
);
950 return (UPL_GET_INTERNAL_PAGE_LIST(upl
));
953 /************* UBC APIS **************/
956 UBCINFOMISSING(struct vnode
* vp
)
958 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
== UBC_INFO_NULL
));
962 UBCINFORECLAIMED(struct vnode
* vp
)
964 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
== UBC_INFO_NULL
));
969 UBCINFOEXISTS(struct vnode
* vp
)
971 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
!= UBC_INFO_NULL
));
974 UBCISVALID(struct vnode
* vp
)
976 return((vp
) && ((vp
)->v_type
== VREG
) && !((vp
)->v_flag
& VSYSTEM
));
979 UBCINVALID(struct vnode
* vp
)
981 return(((vp
) == NULL
) || ((vp
) && ((vp
)->v_type
!= VREG
))
982 || ((vp
) && ((vp
)->v_flag
& VSYSTEM
)));
985 UBCINFOCHECK(const char * fun
, struct vnode
* vp
)
987 if ((vp
) && ((vp
)->v_type
== VREG
) &&
988 ((vp
)->v_ubcinfo
== UBC_INFO_NULL
)) {
989 panic("%s: lost ubc_info", (fun
));