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
)
198 credp
= uip
->ui_ucred
;
199 if (credp
!= NOCRED
) {
200 uip
->ui_ucred
= NOCRED
;
201 kauth_cred_rele(credp
);
204 if (uip
->ui_control
!= MEMORY_OBJECT_CONTROL_NULL
)
205 memory_object_control_deallocate(uip
->ui_control
);
207 cluster_release(uip
);
209 zfree(ubc_info_zone
, (vm_offset_t
)uip
);
214 ubc_info_deallocate(struct ubc_info
*uip
)
220 * Communicate with VM the size change of the file
221 * returns 1 on success, 0 on failure
224 ubc_setsize(struct vnode
*vp
, off_t nsize
)
226 off_t osize
; /* ui_size before change */
227 off_t lastpg
, olastpgend
, lastoff
;
228 struct ubc_info
*uip
;
229 memory_object_control_t control
;
232 if (nsize
< (off_t
)0)
235 if (!UBCINFOEXISTS(vp
))
239 osize
= uip
->ui_size
; /* call ubc_getsize() ??? */
240 /* Update the size before flushing the VM */
241 uip
->ui_size
= nsize
;
243 if (nsize
>= osize
) /* Nothing more to do */
244 return (1); /* return success */
247 * When the file shrinks, invalidate the pages beyond the
248 * new size. Also get rid of garbage beyond nsize on the
249 * last page. The ui_size already has the nsize. This
250 * insures that the pageout would not write beyond the new
254 lastpg
= trunc_page_64(nsize
);
255 olastpgend
= round_page_64(osize
);
256 control
= uip
->ui_control
;
258 lastoff
= (nsize
& PAGE_MASK_64
);
261 * If length is multiple of page size, we should not flush
262 * invalidating is sufficient
265 /* invalidate last page and old contents beyond nsize */
266 kret
= memory_object_lock_request(control
,
267 (memory_object_offset_t
)lastpg
,
268 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
269 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
271 if (kret
!= KERN_SUCCESS
)
272 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
274 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
277 /* flush the last page */
278 kret
= memory_object_lock_request(control
,
279 (memory_object_offset_t
)lastpg
,
280 PAGE_SIZE_64
, NULL
, NULL
,
281 MEMORY_OBJECT_RETURN_DIRTY
, FALSE
,
284 if (kret
== KERN_SUCCESS
) {
285 /* invalidate last page and old contents beyond nsize */
286 kret
= memory_object_lock_request(control
,
287 (memory_object_offset_t
)lastpg
,
288 (memory_object_size_t
)(olastpgend
- lastpg
), NULL
, NULL
,
289 MEMORY_OBJECT_RETURN_NONE
, MEMORY_OBJECT_DATA_FLUSH
,
291 if (kret
!= KERN_SUCCESS
)
292 printf("ubc_setsize: invalidate failed (error = %d)\n", kret
);
294 printf("ubc_setsize: flush failed (error = %d)\n", kret
);
296 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
300 * Get the size of the file
303 ubc_getsize(struct vnode
*vp
)
305 /* people depend on the side effect of this working this way
306 * as they call this for directory
308 if (!UBCINFOEXISTS(vp
))
310 return (vp
->v_ubcinfo
->ui_size
);
314 * call ubc_sync_range(vp, 0, EOF, UBC_PUSHALL) on all the vnodes
315 * for this mount point.
316 * returns 1 on success, 0 on failure
319 __private_extern__
int
320 ubc_umount(struct mount
*mp
)
322 vnode_iterate(mp
, 0, ubc_umcallback
, 0);
327 ubc_umcallback(vnode_t vp
, __unused
void * args
)
330 if (UBCINFOEXISTS(vp
)) {
334 (void) ubc_msync(vp
, (off_t
)0, ubc_getsize(vp
), NULL
, UBC_PUSHALL
);
336 return (VNODE_RETURNED
);
341 /* Get the credentials */
343 ubc_getcred(struct vnode
*vp
)
345 if (UBCINFOEXISTS(vp
))
346 return (vp
->v_ubcinfo
->ui_ucred
);
352 ubc_setthreadcred(struct vnode
*vp
, struct proc
*p
, thread_t thread
)
354 struct ubc_info
*uip
;
356 struct uthread
*uthread
= get_bsdthread_info(thread
);
358 if (!UBCINFOEXISTS(vp
))
364 credp
= uip
->ui_ucred
;
366 if (credp
== NOCRED
) {
367 /* use per-thread cred, if assumed identity, else proc cred */
368 if (uthread
== NULL
|| (uthread
->uu_flag
& UT_SETUID
) == 0) {
369 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
371 uip
->ui_ucred
= uthread
->uu_ucred
;
372 kauth_cred_ref(uip
->ui_ucred
);
381 * Set the credentials
382 * existing credentials are not changed
383 * returns 1 on success and 0 on failure
386 ubc_setcred(struct vnode
*vp
, struct proc
*p
)
388 struct ubc_info
*uip
;
391 if ( !UBCINFOEXISTS(vp
))
397 credp
= uip
->ui_ucred
;
399 if (credp
== NOCRED
) {
400 uip
->ui_ucred
= kauth_cred_proc_ref(p
);
408 __private_extern__ memory_object_t
409 ubc_getpager(struct vnode
*vp
)
411 if (UBCINFOEXISTS(vp
))
412 return (vp
->v_ubcinfo
->ui_pager
);
418 * Get the memory object associated with this vnode
419 * If the vnode was reactivated, memory object would not exist.
420 * Unless "do not rectivate" was specified, look it up using the pager.
421 * If hold was requested create an object reference of one does not
425 memory_object_control_t
426 ubc_getobject(struct vnode
*vp
, __unused
int flags
)
428 if (UBCINFOEXISTS(vp
))
429 return((vp
->v_ubcinfo
->ui_control
));
436 ubc_blktooff(vnode_t vp
, daddr64_t blkno
)
444 error
= VNOP_BLKTOOFF(vp
, blkno
, &file_offset
);
448 return (file_offset
);
452 ubc_offtoblk(vnode_t vp
, off_t offset
)
458 return ((daddr64_t
)-1);
460 error
= VNOP_OFFTOBLK(vp
, offset
, &blkno
);
468 ubc_pages_resident(vnode_t vp
)
471 boolean_t has_pages_resident
;
473 if ( !UBCINFOEXISTS(vp
))
476 kret
= memory_object_pages_resident(vp
->v_ubcinfo
->ui_control
, &has_pages_resident
);
478 if (kret
!= KERN_SUCCESS
)
481 if (has_pages_resident
== TRUE
)
490 * This interface will eventually be deprecated
492 * clean and/or invalidate a range in the memory object that backs this
493 * vnode. The start offset is truncated to the page boundary and the
494 * size is adjusted to include the last page in the range.
496 * returns 1 for success, 0 for failure
499 ubc_sync_range(vnode_t vp
, off_t beg_off
, off_t end_off
, int flags
)
501 return (ubc_msync_internal(vp
, beg_off
, end_off
, NULL
, flags
, NULL
));
506 * clean and/or invalidate a range in the memory object that backs this
507 * vnode. The start offset is truncated to the page boundary and the
508 * size is adjusted to include the last page in the range.
512 ubc_msync(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
)
518 *resid_off
= beg_off
;
520 retval
= ubc_msync_internal(vp
, beg_off
, end_off
, resid_off
, flags
, &io_errno
);
522 if (retval
== 0 && io_errno
== 0)
530 * clean and/or invalidate a range in the memory object that backs this
531 * vnode. The start offset is truncated to the page boundary and the
532 * size is adjusted to include the last page in the range.
535 ubc_msync_internal(vnode_t vp
, off_t beg_off
, off_t end_off
, off_t
*resid_off
, int flags
, int *io_errno
)
537 memory_object_size_t tsize
;
539 int request_flags
= 0;
540 int flush_flags
= MEMORY_OBJECT_RETURN_NONE
;
542 if ( !UBCINFOEXISTS(vp
))
544 if (end_off
<= beg_off
)
546 if ((flags
& (UBC_INVALIDATE
| UBC_PUSHDIRTY
| UBC_PUSHALL
)) == 0)
549 if (flags
& UBC_INVALIDATE
)
551 * discard the resident pages
553 request_flags
= (MEMORY_OBJECT_DATA_FLUSH
| MEMORY_OBJECT_DATA_NO_CHANGE
);
555 if (flags
& UBC_SYNC
)
557 * wait for all the I/O to complete before returning
559 request_flags
|= MEMORY_OBJECT_IO_SYNC
;
561 if (flags
& UBC_PUSHDIRTY
)
563 * we only return the dirty pages in the range
565 flush_flags
= MEMORY_OBJECT_RETURN_DIRTY
;
567 if (flags
& UBC_PUSHALL
)
569 * then return all the interesting pages in the range (both dirty and precious)
572 flush_flags
= MEMORY_OBJECT_RETURN_ALL
;
574 beg_off
= trunc_page_64(beg_off
);
575 end_off
= round_page_64(end_off
);
576 tsize
= (memory_object_size_t
)end_off
- beg_off
;
578 /* flush and/or invalidate pages in the range requested */
579 kret
= memory_object_lock_request(vp
->v_ubcinfo
->ui_control
,
580 beg_off
, tsize
, resid_off
, io_errno
,
581 flush_flags
, request_flags
, VM_PROT_NO_CHANGE
);
583 return ((kret
== KERN_SUCCESS
) ? 1 : 0);
588 * The vnode is mapped explicitly, mark it so.
590 __private_extern__
int
591 ubc_map(vnode_t vp
, int flags
)
593 struct ubc_info
*uip
;
596 struct vfs_context context
;
598 if (vnode_getwithref(vp
))
601 if (UBCINFOEXISTS(vp
)) {
602 context
.vc_proc
= current_proc();
603 context
.vc_ucred
= kauth_cred_get();
605 error
= VNOP_MMAP(vp
, flags
, &context
);
615 if ( !ISSET(uip
->ui_flags
, UI_ISMAPPED
))
617 SET(uip
->ui_flags
, (UI_WASMAPPED
| UI_ISMAPPED
));
631 * destroy the named reference for a given vnode
633 __private_extern__
int
634 ubc_destroy_named(struct vnode
*vp
)
636 memory_object_control_t control
;
637 struct ubc_info
*uip
;
641 * We may already have had the object terminated
642 * and the ubcinfo released as a side effect of
643 * some earlier processing. If so, pretend we did
644 * it, because it probably was a result of our
647 if (!UBCINFOEXISTS(vp
))
653 * Terminate the memory object.
654 * memory_object_destroy() will result in
655 * vnode_pager_no_senders().
656 * That will release the pager reference
657 * and the vnode will move to the free list.
659 control
= ubc_getobject(vp
, UBC_HOLDOBJECT
);
660 if (control
!= MEMORY_OBJECT_CONTROL_NULL
) {
663 * XXXXX - should we hold the vnode lock here?
665 if (ISSET(vp
->v_flag
, VTERMINATE
))
666 panic("ubc_destroy_named: already teminating");
667 SET(vp
->v_flag
, VTERMINATE
);
669 kret
= memory_object_destroy(control
, 0);
670 if (kret
!= KERN_SUCCESS
)
674 * memory_object_destroy() is asynchronous
675 * with respect to vnode_pager_no_senders().
676 * wait for vnode_pager_no_senders() to clear
680 while (ISSET(vp
->v_lflag
, VNAMED_UBC
)) {
681 (void)msleep((caddr_t
)&vp
->v_lflag
, &vp
->v_lock
,
682 PINOD
, "ubc_destroy_named", 0);
691 * Find out whether a vnode is in use by UBC
692 * Returns 1 if file is in use by UBC, 0 if not
695 ubc_isinuse(struct vnode
*vp
, int busycount
)
697 if ( !UBCINFOEXISTS(vp
))
699 return(ubc_isinuse_locked(vp
, busycount
, 0));
704 ubc_isinuse_locked(struct vnode
*vp
, int busycount
, int locked
)
712 if ((vp
->v_usecount
- vp
->v_kusecount
) > busycount
)
722 * MUST only be called by the VM
724 __private_extern__
void
725 ubc_unmap(struct vnode
*vp
)
727 struct vfs_context context
;
728 struct ubc_info
*uip
;
731 if (vnode_getwithref(vp
))
734 if (UBCINFOEXISTS(vp
)) {
738 if (ISSET(uip
->ui_flags
, UI_ISMAPPED
)) {
739 CLR(uip
->ui_flags
, UI_ISMAPPED
);
745 context
.vc_proc
= current_proc();
746 context
.vc_ucred
= kauth_cred_get();
747 (void)VNOP_MNOMAP(vp
, &context
);
753 * the drop of the vnode ref will cleanup
763 ppnum_t
*phys_entryp
,
766 memory_object_control_t control
;
768 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
769 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
770 return KERN_INVALID_ARGUMENT
;
772 return (memory_object_page_op(control
,
773 (memory_object_offset_t
)f_offset
,
779 __private_extern__ kern_return_t
780 ubc_page_op_with_control(
781 memory_object_control_t control
,
784 ppnum_t
*phys_entryp
,
787 return (memory_object_page_op(control
,
788 (memory_object_offset_t
)f_offset
,
802 memory_object_control_t control
;
804 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
805 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
806 return KERN_INVALID_ARGUMENT
;
808 return (memory_object_range_op(control
,
809 (memory_object_offset_t
)f_offset_beg
,
810 (memory_object_offset_t
)f_offset_end
,
821 upl_page_info_t
**plp
,
824 memory_object_control_t control
;
830 return KERN_INVALID_ARGUMENT
;
832 if (uplflags
& UPL_FOR_PAGEOUT
) {
833 uplflags
&= ~UPL_FOR_PAGEOUT
;
834 ubcflags
= UBC_FOR_PAGEOUT
;
836 ubcflags
= UBC_FLAGS_NONE
;
838 control
= ubc_getobject(vp
, ubcflags
);
839 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
840 return KERN_INVALID_ARGUMENT
;
842 if (uplflags
& UPL_WILL_BE_DUMPED
) {
843 uplflags
&= ~UPL_WILL_BE_DUMPED
;
844 uplflags
|= (UPL_NO_SYNC
|UPL_SET_INTERNAL
);
846 uplflags
|= (UPL_NO_SYNC
|UPL_CLEAN_IN_PLACE
|UPL_SET_INTERNAL
);
848 kr
= memory_object_upl_request(control
, f_offset
, bufsize
,
849 uplp
, NULL
, &count
, uplflags
);
851 *plp
= UPL_GET_INTERNAL_PAGE_LIST(*uplp
);
859 vm_offset_t
*dst_addr
)
861 return (vm_upl_map(kernel_map
, upl
, dst_addr
));
869 return(vm_upl_unmap(kernel_map
, upl
));
879 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
880 kr
= upl_commit(upl
, pl
, MAX_UPL_TRANSFER
);
887 ubc_upl_commit_range(
897 if (flags
& UPL_COMMIT_FREE_ON_EMPTY
)
898 flags
|= UPL_COMMIT_NOTIFY_EMPTY
;
900 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
902 kr
= upl_commit_range(upl
, offset
, size
, flags
,
903 pl
, MAX_UPL_TRANSFER
, &empty
);
905 if((flags
& UPL_COMMIT_FREE_ON_EMPTY
) && empty
)
919 boolean_t empty
= FALSE
;
921 if (abort_flags
& UPL_ABORT_FREE_ON_EMPTY
)
922 abort_flags
|= UPL_ABORT_NOTIFY_EMPTY
;
924 kr
= upl_abort_range(upl
, offset
, size
, abort_flags
, &empty
);
926 if((abort_flags
& UPL_ABORT_FREE_ON_EMPTY
) && empty
)
939 kr
= upl_abort(upl
, abort_type
);
948 return (UPL_GET_INTERNAL_PAGE_LIST(upl
));
951 /************* UBC APIS **************/
954 UBCINFOMISSING(struct vnode
* vp
)
956 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
== UBC_INFO_NULL
));
960 UBCINFORECLAIMED(struct vnode
* vp
)
962 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
== UBC_INFO_NULL
));
967 UBCINFOEXISTS(struct vnode
* vp
)
969 return((vp
) && ((vp
)->v_type
== VREG
) && ((vp
)->v_ubcinfo
!= UBC_INFO_NULL
));
972 UBCISVALID(struct vnode
* vp
)
974 return((vp
) && ((vp
)->v_type
== VREG
) && !((vp
)->v_flag
& VSYSTEM
));
977 UBCINVALID(struct vnode
* vp
)
979 return(((vp
) == NULL
) || ((vp
) && ((vp
)->v_type
!= VREG
))
980 || ((vp
) && ((vp
)->v_flag
& VSYSTEM
)));
983 UBCINFOCHECK(const char * fun
, struct vnode
* vp
)
985 if ((vp
) && ((vp
)->v_type
== VREG
) &&
986 ((vp
)->v_ubcinfo
== UBC_INFO_NULL
)) {
987 panic("%s: lost ubc_info", (fun
));