2 * Copyright (c) 1999-2004 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_internal.h>
42 #include <sys/vnode_internal.h>
43 #include <sys/ubc_internal.h>
44 #include <sys/ucred.h>
45 #include <sys/proc_internal.h>
46 #include <sys/kauth.h>
50 #include <mach/mach_types.h>
51 #include <mach/memory_object_types.h>
52 #include <mach/memory_object_control.h>
53 #include <mach/vm_map.h>
56 #include <kern/kern_types.h>
57 #include <kern/zalloc.h>
58 #include <kern/thread.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_protos.h> /* last */
66 #define assert(cond) \
67 ((void) ((cond) ? 0 : panic("%s:%d (%s)", __FILE__, __LINE__, # cond)))
69 #include <kern/assert.h>
70 #endif /* DIAGNOSTIC */
72 int ubc_info_init_internal(struct vnode
*vp
, int withfsize
, off_t filesize
);
73 static int ubc_umcallback(vnode_t
, void *);
74 int ubc_isinuse_locked(vnode_t
, int, int);
75 static int ubc_msync_internal(vnode_t
, off_t
, off_t
, off_t
*, int, int *);
77 struct zone
*ubc_info_zone
;
80 * Initialization of the zone for Unified Buffer Cache.
82 __private_extern__
void
87 i
= (vm_size_t
) sizeof (struct ubc_info
);
88 /* XXX the number of elements should be tied in to maxvnodes */
89 ubc_info_zone
= zinit (i
, 10000*i
, 8192, "ubc_info zone");
94 * Initialize a ubc_info structure for a vnode.
97 ubc_info_init(struct vnode
*vp
)
99 return(ubc_info_init_internal(vp
, 0, 0));
102 ubc_info_init_withsize(struct vnode
*vp
, off_t filesize
)
104 return(ubc_info_init_internal(vp
, 1, filesize
));
108 ubc_info_init_internal(struct vnode
*vp
, int withfsize
, off_t filesize
)
110 register struct ubc_info
*uip
;
112 struct proc
*p
= current_proc();
115 memory_object_control_t control
;
119 if (uip
== UBC_INFO_NULL
) {
121 uip
= (struct ubc_info
*) zalloc(ubc_info_zone
);
122 bzero((char *)uip
, sizeof(struct ubc_info
));
125 uip
->ui_flags
= UI_INITED
;
126 uip
->ui_ucred
= NOCRED
;
130 Debugger("ubc_info_init: already");
131 #endif /* DIAGNOSTIC */
133 assert(uip
->ui_flags
!= UI_NONE
);
134 assert(uip
->ui_vnode
== vp
);
136 /* now set this ubc_info in the vnode */
139 pager
= (void *)vnode_pager_setup(vp
, uip
->ui_pager
);
142 SET(uip
->ui_flags
, UI_HASPAGER
);
143 uip
->ui_pager
= pager
;
146 * Note: We can not use VNOP_GETATTR() to get accurate
147 * value of ui_size. Thanks to NFS.
148 * nfs_getattr() can call vinvalbuf() and in this case
149 * ubc_info is not set up to deal with that.
154 * create a vnode - vm_object association
155 * memory_object_create_named() creates a "named" reference on the
156 * memory object we hold this reference as long as the vnode is
157 * "alive." Since memory_object_create_named() took its own reference
158 * on the vnode pager we passed it, we can drop the reference
159 * vnode_pager_setup() returned here.
161 kret
= memory_object_create_named(pager
,
162 (memory_object_size_t
)uip
->ui_size
, &control
);
163 vnode_pager_deallocate(pager
);
164 if (kret
!= KERN_SUCCESS
)
165 panic("ubc_info_init: memory_object_create_named returned %d", kret
);
168 uip
->ui_control
= control
; /* cache the value of the mo control */
169 SET(uip
->ui_flags
, UI_HASOBJREF
); /* with a named reference */
171 /* create a pager reference on the vnode */
172 error
= vnode_pager_vget(vp
);
174 panic("ubc_info_init: vnode_pager_vget error = %d", error
);
176 if (withfsize
== 0) {
177 struct vfs_context context
;
178 /* initialize the size */
180 context
.vc_ucred
= kauth_cred_get();
181 error
= vnode_size(vp
, &uip
->ui_size
, &context
);
185 uip
->ui_size
= filesize
;
187 vp
->v_lflag
|= VNAMED_UBC
;
192 /* Free the ubc_info */
194 ubc_info_free(struct ubc_info
*uip
)
196 if (IS_VALID_CRED(uip
->ui_ucred
)) {
197 kauth_cred_unref(&uip
->ui_ucred
);
200 if (uip
->ui_control
!= MEMORY_OBJECT_CONTROL_NULL
)
201 memory_object_control_deallocate(uip
->ui_control
);
203 cluster_release(uip
);
205 zfree(ubc_info_zone
, (vm_offset_t
)uip
);
210 ubc_info_deallocate(struct ubc_info
*uip
)
216 * Communicate with VM the size change of the file
217 * returns 1 on success, 0 on failure
220 ubc_setsize(struct vnode
*vp
, off_t nsize
)
222 off_t osize
; /* ui_size before change */
223 off_t lastpg
, olastpgend
, lastoff
;
224 struct ubc_info
*uip
;
225 memory_object_control_t control
;
228 if (nsize
< (off_t
)0)
231 if (!UBCINFOEXISTS(vp
))
235 osize
= uip
->ui_size
; /* call ubc_getsize() ??? */
236 /* Update the size before flushing the VM */
237 uip
->ui_size
= nsize
;
239 if (nsize
>= osize
) /* Nothing more to do */
240 return (1); /* return success */
243 * When the file shrinks, invalidate the pages beyond the
244 * new size. Also get rid of garbage beyond nsize on the
245 * last page. The ui_size already has the nsize. This
246 * insures that the pageout would not write beyond the new
250 lastpg
= trunc_page_64(nsize
);
251 olastpgend
= round_page_64(osize
);
252 control
= uip
->ui_control
;
254 lastoff
= (nsize
& PAGE_MASK_64
);
257 * If length is multiple of page size, we should not flush
258 * invalidating is sufficient
261 /* invalidate last page and old contents beyond nsize */
262 kret
= memory_object_lock_request(control
,
263 (memory_object_offset_t
)lastpg
,
264 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
265 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
267 if (kret
!= KERN_SUCCESS
)
268 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
270 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
273 /* flush the last page */
274 kret
= memory_object_lock_request(control
,
275 (memory_object_offset_t
)lastpg
,
276 PAGE_SIZE_64
, NULL
, NULL
,
277 MEMORY_OBJECT_RETURN_DIRTY
, FALSE
,
280 if (kret
== KERN_SUCCESS
) {
281 /* invalidate last page and old contents beyond nsize */
282 kret
= memory_object_lock_request(control
,
283 (memory_object_offset_t
)lastpg
,
284 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
285 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
287 if (kret
!= KERN_SUCCESS
)
288 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
290 printf("ubc_setsize: flush failed (error = %d)\n", kret
);
292 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
296 * Get the size of the file
299 ubc_getsize(struct vnode
*vp
)
301 /* people depend on the side effect of this working this way
302 * as they call this for directory
304 if (!UBCINFOEXISTS(vp
))
306 return (vp
->v_ubcinfo
->ui_size
);
310 * call ubc_sync_range(vp, 0, EOF, UBC_PUSHALL) on all the vnodes
311 * for this mount point.
312 * returns 1 on success, 0 on failure
315 __private_extern__
int
316 ubc_umount(struct mount
*mp
)
318 vnode_iterate(mp
, 0, ubc_umcallback
, 0);
323 ubc_umcallback(vnode_t vp
, __unused
void * args
)
326 if (UBCINFOEXISTS(vp
)) {
330 (void) ubc_msync(vp
, (off_t
)0, ubc_getsize(vp
), NULL
, UBC_PUSHALL
);
332 return (VNODE_RETURNED
);
337 /* Get the credentials */
339 ubc_getcred(struct vnode
*vp
)
341 if (UBCINFOEXISTS(vp
))
342 return (vp
->v_ubcinfo
->ui_ucred
);
348 ubc_setthreadcred(struct vnode
*vp
, struct proc
*p
, thread_t thread
)
350 struct ubc_info
*uip
;
352 struct uthread
*uthread
= get_bsdthread_info(thread
);
354 if (!UBCINFOEXISTS(vp
))
360 credp
= uip
->ui_ucred
;
362 if (!IS_VALID_CRED(credp
)) {
363 /* use per-thread cred, if assumed identity, else proc cred */
364 if (uthread
== NULL
|| (uthread
->uu_flag
& UT_SETUID
) == 0) {
365 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
367 uip
->ui_ucred
= uthread
->uu_ucred
;
368 kauth_cred_ref(uip
->ui_ucred
);
377 * Set the credentials
378 * existing credentials are not changed
379 * returns 1 on success and 0 on failure
382 ubc_setcred(struct vnode
*vp
, struct proc
*p
)
384 struct ubc_info
*uip
;
387 if ( !UBCINFOEXISTS(vp
))
393 credp
= uip
->ui_ucred
;
395 if (!IS_VALID_CRED(credp
)) {
396 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
404 __private_extern__ memory_object_t
405 ubc_getpager(struct vnode
*vp
)
407 if (UBCINFOEXISTS(vp
))
408 return (vp
->v_ubcinfo
->ui_pager
);
414 * Get the memory object associated with this vnode
415 * If the vnode was reactivated, memory object would not exist.
416 * Unless "do not rectivate" was specified, look it up using the pager.
417 * If hold was requested create an object reference of one does not
421 memory_object_control_t
422 ubc_getobject(struct vnode
*vp
, __unused
int flags
)
424 if (UBCINFOEXISTS(vp
))
425 return((vp
->v_ubcinfo
->ui_control
));
432 ubc_blktooff(vnode_t vp
, daddr64_t blkno
)
440 error
= VNOP_BLKTOOFF(vp
, blkno
, &file_offset
);
444 return (file_offset
);
448 ubc_offtoblk(vnode_t vp
, off_t offset
)
454 return ((daddr64_t
)-1);
456 error
= VNOP_OFFTOBLK(vp
, offset
, &blkno
);
464 ubc_pages_resident(vnode_t vp
)
467 boolean_t has_pages_resident
;
469 if ( !UBCINFOEXISTS(vp
))
472 kret
= memory_object_pages_resident(vp
->v_ubcinfo
->ui_control
, &has_pages_resident
);
474 if (kret
!= KERN_SUCCESS
)
477 if (has_pages_resident
== TRUE
)
486 * This interface will eventually be deprecated
488 * clean and/or invalidate a range in the memory object that backs this
489 * vnode. The start offset is truncated to the page boundary and the
490 * size is adjusted to include the last page in the range.
492 * returns 1 for success, 0 for failure
495 ubc_sync_range(vnode_t vp
, off_t beg_off
, off_t end_off
, int flags
)
497 return (ubc_msync_internal(vp
, beg_off
, end_off
, NULL
, flags
, NULL
));
502 * clean and/or invalidate a range in the memory object that backs this
503 * vnode. The start offset is truncated to the page boundary and the
504 * size is adjusted to include the last page in the range.
508 ubc_msync(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
)
514 *resid_off
= beg_off
;
516 retval
= ubc_msync_internal(vp
, beg_off
, end_off
, resid_off
, flags
, &io_errno
);
518 if (retval
== 0 && io_errno
== 0)
526 * clean and/or invalidate a range in the memory object that backs this
527 * vnode. The start offset is truncated to the page boundary and the
528 * size is adjusted to include the last page in the range.
531 ubc_msync_internal(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
, int *io_errno
)
533 memory_object_size_t tsize
;
535 int request_flags
= 0;
536 int flush_flags
= MEMORY_OBJECT_RETURN_NONE
;
538 if ( !UBCINFOEXISTS(vp
))
540 if (end_off
<= beg_off
)
542 if ((flags
& (UBC_INVALIDATE
| UBC_PUSHDIRTY
| UBC_PUSHALL
)) == 0)
545 if (flags
& UBC_INVALIDATE
)
547 * discard the resident pages
549 request_flags
= (MEMORY_OBJECT_DATA_FLUSH
| MEMORY_OBJECT_DATA_NO_CHANGE
);
551 if (flags
& UBC_SYNC
)
553 * wait for all the I/O to complete before returning
555 request_flags
|= MEMORY_OBJECT_IO_SYNC
;
557 if (flags
& UBC_PUSHDIRTY
)
559 * we only return the dirty pages in the range
561 flush_flags
= MEMORY_OBJECT_RETURN_DIRTY
;
563 if (flags
& UBC_PUSHALL
)
565 * then return all the interesting pages in the range (both dirty and precious)
568 flush_flags
= MEMORY_OBJECT_RETURN_ALL
;
570 beg_off
= trunc_page_64(beg_off
);
571 end_off
= round_page_64(end_off
);
572 tsize
= (memory_object_size_t
)end_off
- beg_off
;
574 /* flush and/or invalidate pages in the range requested */
575 kret
= memory_object_lock_request(vp
->v_ubcinfo
->ui_control
,
576 beg_off
, tsize
, resid_off
, io_errno
,
577 flush_flags
, request_flags
, VM_PROT_NO_CHANGE
);
579 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
584 * The vnode is mapped explicitly, mark it so.
586 __private_extern__
int
587 ubc_map(vnode_t vp
, int flags
)
589 struct ubc_info
*uip
;
592 struct vfs_context context
;
594 if (vnode_getwithref(vp
))
597 if (UBCINFOEXISTS(vp
)) {
598 context
.vc_proc
= current_proc();
599 context
.vc_ucred
= kauth_cred_get();
601 error
= VNOP_MMAP(vp
, flags
, &context
);
611 if ( !ISSET(uip
->ui_flags
, UI_ISMAPPED
))
613 SET(uip
->ui_flags
, (UI_WASMAPPED
| UI_ISMAPPED
));
627 * destroy the named reference for a given vnode
629 __private_extern__
int
630 ubc_destroy_named(struct vnode
*vp
)
632 memory_object_control_t control
;
633 struct ubc_info
*uip
;
637 * We may already have had the object terminated
638 * and the ubcinfo released as a side effect of
639 * some earlier processing. If so, pretend we did
640 * it, because it probably was a result of our
643 if (!UBCINFOEXISTS(vp
))
649 * Terminate the memory object.
650 * memory_object_destroy() will result in
651 * vnode_pager_no_senders().
652 * That will release the pager reference
653 * and the vnode will move to the free list.
655 control
= ubc_getobject(vp
, UBC_HOLDOBJECT
);
656 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
659 * XXXXX - should we hold the vnode lock here?
661 if (ISSET(vp
->v_flag
, VTERMINATE
))
662 panic("ubc_destroy_named: already teminating");
663 SET(vp
->v_flag
, VTERMINATE
);
665 kret
= memory_object_destroy(control
, 0);
666 if (kret
!= KERN_SUCCESS
)
670 * memory_object_destroy() is asynchronous
671 * with respect to vnode_pager_no_senders().
672 * wait for vnode_pager_no_senders() to clear
676 while (ISSET(vp
->v_lflag
, VNAMED_UBC
)) {
677 (void)msleep((caddr_t
)&vp
->v_lflag
, &vp
->v_lock
,
678 PINOD
, "ubc_destroy_named", 0);
687 * Find out whether a vnode is in use by UBC
688 * Returns 1 if file is in use by UBC, 0 if not
691 ubc_isinuse(struct vnode
*vp
, int busycount
)
693 if ( !UBCINFOEXISTS(vp
))
695 return(ubc_isinuse_locked(vp
, busycount
, 0));
700 ubc_isinuse_locked(struct vnode
*vp
, int busycount
, int locked
)
708 if ((vp
->v_usecount
- vp
->v_kusecount
) > busycount
)
718 * MUST only be called by the VM
720 __private_extern__
void
721 ubc_unmap(struct vnode
*vp
)
723 struct vfs_context context
;
724 struct ubc_info
*uip
;
727 if (vnode_getwithref(vp
))
730 if (UBCINFOEXISTS(vp
)) {
734 if (ISSET(uip
->ui_flags
, UI_ISMAPPED
)) {
735 CLR(uip
->ui_flags
, UI_ISMAPPED
);
741 context
.vc_proc
= current_proc();
742 context
.vc_ucred
= kauth_cred_get();
743 (void)VNOP_MNOMAP(vp
, &context
);
749 * the drop of the vnode ref will cleanup
759 ppnum_t
*phys_entryp
,
762 memory_object_control_t control
;
764 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
765 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
766 return KERN_INVALID_ARGUMENT
;
768 return (memory_object_page_op(control
,
769 (memory_object_offset_t
)f_offset
,
775 __private_extern__ kern_return_t
776 ubc_page_op_with_control(
777 memory_object_control_t control
,
780 ppnum_t
*phys_entryp
,
783 return (memory_object_page_op(control
,
784 (memory_object_offset_t
)f_offset
,
798 memory_object_control_t control
;
800 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
801 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
802 return KERN_INVALID_ARGUMENT
;
804 return (memory_object_range_op(control
,
805 (memory_object_offset_t
)f_offset_beg
,
806 (memory_object_offset_t
)f_offset_end
,
817 upl_page_info_t
**plp
,
820 memory_object_control_t control
;
826 return KERN_INVALID_ARGUMENT
;
828 if (uplflags
& UPL_FOR_PAGEOUT
) {
829 uplflags
&= ~UPL_FOR_PAGEOUT
;
830 ubcflags
= UBC_FOR_PAGEOUT
;
832 ubcflags
= UBC_FLAGS_NONE
;
834 control
= ubc_getobject(vp
, ubcflags
);
835 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
836 return KERN_INVALID_ARGUMENT
;
838 if (uplflags
& UPL_WILL_BE_DUMPED
) {
839 uplflags
&= ~UPL_WILL_BE_DUMPED
;
840 uplflags
|= (UPL_NO_SYNC
|UPL_SET_INTERNAL
);
842 uplflags
|= (UPL_NO_SYNC
|UPL_CLEAN_IN_PLACE
|UPL_SET_INTERNAL
);
844 kr
= memory_object_upl_request(control
, f_offset
, bufsize
,
845 uplp
, NULL
, &count
, uplflags
);
847 *plp
= UPL_GET_INTERNAL_PAGE_LIST(*uplp
);
855 vm_offset_t
*dst_addr
)
857 return (vm_upl_map(kernel_map
, upl
, dst_addr
));
865 return(vm_upl_unmap(kernel_map
, upl
));
875 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
876 kr
= upl_commit(upl
, pl
, MAX_UPL_TRANSFER
);
883 ubc_upl_commit_range(
893 if (flags
& UPL_COMMIT_FREE_ON_EMPTY
)
894 flags
|= UPL_COMMIT_NOTIFY_EMPTY
;
896 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
898 kr
= upl_commit_range(upl
, offset
, size
, flags
,
899 pl
, MAX_UPL_TRANSFER
, &empty
);
901 if((flags
& UPL_COMMIT_FREE_ON_EMPTY
) && empty
)
915 boolean_t empty
= FALSE
;
917 if (abort_flags
& UPL_ABORT_FREE_ON_EMPTY
)
918 abort_flags
|= UPL_ABORT_NOTIFY_EMPTY
;
920 kr
= upl_abort_range(upl
, offset
, size
, abort_flags
, &empty
);
922 if((abort_flags
& UPL_ABORT_FREE_ON_EMPTY
) && empty
)
935 kr
= upl_abort(upl
, abort_type
);
944 return (UPL_GET_INTERNAL_PAGE_LIST(upl
));
947 /************* UBC APIS **************/
950 UBCINFOMISSING(struct vnode
* vp
)
952 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
== UBC_INFO_NULL
));
956 UBCINFORECLAIMED(struct vnode
* vp
)
958 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
== UBC_INFO_NULL
));
963 UBCINFOEXISTS(struct vnode
* vp
)
965 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
!= UBC_INFO_NULL
));
968 UBCISVALID(struct vnode
* vp
)
970 return((vp
) && ((vp
)->v_type
== VREG
) && !((vp
)->v_flag
& VSYSTEM
));
973 UBCINVALID(struct vnode
* vp
)
975 return(((vp
) == NULL
) || ((vp
) && ((vp
)->v_type
!= VREG
))
976 || ((vp
) && ((vp
)->v_flag
& VSYSTEM
)));
979 UBCINFOCHECK(const char * fun
, struct vnode
* vp
)
981 if ((vp
) && ((vp
)->v_type
== VREG
) &&
982 ((vp
)->v_ubcinfo
== UBC_INFO_NULL
)) {
983 panic("%s: lost ubc_info", (fun
));