]> git.saurik.com Git - apple/xnu.git/blame - bsd/vm/vnode_pager.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / vm / vnode_pager.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
0a7de745 28/*
1c79356b
A
29 * Mach Operating System
30 * Copyright (c) 1987 Carnegie-Mellon University
31 * All rights reserved. The CMU software License Agreement specifies
32 * the terms and conditions for use and redistribution.
33 */
34/*
35 * File: vnode_pager.c
36 *
37 * "Swap" pager that pages to/from vnodes. Also
38 * handles demand paging from files.
39 *
40 */
41
42#include <mach/boolean.h>
43#include <sys/param.h>
44#include <sys/systm.h>
91447636 45#include <sys/user.h>
1c79356b 46#include <sys/proc.h>
91447636 47#include <sys/kauth.h>
1c79356b
A
48#include <sys/buf.h>
49#include <sys/uio.h>
91447636 50#include <sys/vnode_internal.h>
1c79356b 51#include <sys/namei.h>
0a7de745 52#include <sys/mount_internal.h> /* needs internal due to fhandle_t */
91447636 53#include <sys/ubc_internal.h>
1c79356b 54#include <sys/lock.h>
0a7de745 55#include <sys/disk.h> /* For DKIOC calls */
1c79356b
A
56
57#include <mach/mach_types.h>
58#include <mach/memory_object_types.h>
b0d623f7
A
59#include <mach/memory_object_control.h>
60#include <mach/vm_map.h>
61#include <mach/mach_vm.h>
62#include <mach/upl.h>
2d21ac55 63#include <mach/sdt.h>
1c79356b
A
64
65#include <vm/vm_map.h>
66#include <vm/vm_kern.h>
1c79356b
A
67#include <kern/zalloc.h>
68#include <kern/kalloc.h>
69#include <libkern/libkern.h>
70
71#include <vm/vnode_pager.h>
72#include <vm/vm_pageout.h>
73
74#include <kern/assert.h>
9bccf70c 75#include <sys/kdebug.h>
ea3f0419 76#include <nfs/nfs_conf.h>
91447636
A
77#include <nfs/rpcv2.h>
78#include <nfs/nfsproto.h>
79#include <nfs/nfs.h>
80
81#include <vm/vm_protos.h>
1c79356b 82
5ba3f43e 83#include <vfs/vfs_disk_conditioner.h>
b0d623f7 84
6d2010ae
A
85void
86vnode_pager_throttle()
87{
88 struct uthread *ut;
89
90 ut = get_bsdthread_info(current_thread());
91
0a7de745 92 if (ut->uu_lowpri_window) {
39236c6e 93 throttle_lowpri_io(1);
0a7de745 94 }
6d2010ae
A
95}
96
6d2010ae
A
97boolean_t
98vnode_pager_isSSD(vnode_t vp)
99{
5ba3f43e 100 return disk_conditioner_mount_is_ssd(vp->v_mount);
6d2010ae
A
101}
102
fe8ab488
A
103#if CONFIG_IOSCHED
104void
105vnode_pager_issue_reprioritize_io(struct vnode *devvp, uint64_t blkno, uint32_t len, int priority)
106{
107 u_int32_t blocksize = 0;
108 dk_extent_t extent;
0a7de745 109 dk_set_tier_t set_tier;
fe8ab488
A
110 int error = 0;
111
112 error = VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&blocksize, 0, vfs_context_kernel());
0a7de745 113 if (error) {
fe8ab488 114 return;
0a7de745 115 }
fe8ab488
A
116
117 memset(&extent, 0, sizeof(dk_extent_t));
118 memset(&set_tier, 0, sizeof(dk_set_tier_t));
0a7de745 119
fe8ab488
A
120 extent.offset = blkno * (u_int64_t) blocksize;
121 extent.length = len;
122
0a7de745 123 set_tier.extents = &extent;
fe8ab488
A
124 set_tier.extentsCount = 1;
125 set_tier.tier = priority;
0a7de745 126
fe8ab488
A
127 error = VNOP_IOCTL(devvp, DKIOCSETTIER, (caddr_t)&set_tier, 0, vfs_context_kernel());
128 return;
129}
130#endif
6d2010ae 131
d9a64523
A
132void
133vnode_pager_was_dirtied(
0a7de745
A
134 struct vnode *vp,
135 vm_object_offset_t s_offset,
136 vm_object_offset_t e_offset)
d9a64523 137{
0a7de745 138 cluster_update_state(vp, s_offset, e_offset, TRUE);
d9a64523
A
139}
140
b0d623f7
A
141uint32_t
142vnode_pager_isinuse(struct vnode *vp)
143{
0a7de745
A
144 if (vp->v_usecount > vp->v_kusecount) {
145 return 1;
146 }
147 return 0;
b0d623f7
A
148}
149
150uint32_t
39236c6e 151vnode_pager_return_throttle_io_limit(struct vnode *vp, uint32_t *limit)
b0d623f7 152{
0a7de745 153 return cluster_throttle_io_limit(vp, limit);
b0d623f7 154}
1c79356b 155
0b4e3aa0
A
156vm_object_offset_t
157vnode_pager_get_filesize(struct vnode *vp)
158{
0b4e3aa0 159 return (vm_object_offset_t) ubc_getsize(vp);
0b4e3aa0
A
160}
161
15129b1c
A
162extern int safe_getpath(struct vnode *dvp, char *leafname, char *path, int _len, int *truncated_path);
163
0c530ab8 164kern_return_t
15129b1c 165vnode_pager_get_name(
0a7de745
A
166 struct vnode *vp,
167 char *pathname,
168 vm_size_t pathname_len,
169 char *filename,
170 vm_size_t filename_len,
171 boolean_t *truncated_path_p)
0c530ab8 172{
15129b1c
A
173 *truncated_path_p = FALSE;
174 if (pathname != NULL) {
175 /* get the path name */
176 safe_getpath(vp, NULL,
0a7de745
A
177 pathname, (int) pathname_len,
178 truncated_path_p);
15129b1c
A
179 }
180 if ((pathname == NULL || *truncated_path_p) &&
181 filename != NULL) {
182 /* get the file name */
183 const char *name;
184
185 name = vnode_getname_printable(vp);
186 strlcpy(filename, name, (size_t) filename_len);
187 vnode_putname_printable(name);
0c530ab8 188 }
0c530ab8
A
189 return KERN_SUCCESS;
190}
191
192kern_return_t
15129b1c 193vnode_pager_get_mtime(
0a7de745
A
194 struct vnode *vp,
195 struct timespec *current_mtime,
196 struct timespec *cs_mtime)
0c530ab8 197{
15129b1c
A
198 vnode_mtime(vp, current_mtime, vfs_context_current());
199 if (cs_mtime != NULL) {
200 ubc_get_cs_mtime(vp, cs_mtime);
201 }
0c530ab8
A
202 return KERN_SUCCESS;
203}
204
2d21ac55
A
205kern_return_t
206vnode_pager_get_cs_blobs(
0a7de745
A
207 struct vnode *vp,
208 void **blobs)
2d21ac55
A
209{
210 *blobs = ubc_get_cs_blobs(vp);
211 return KERN_SUCCESS;
212}
213
0a7de745 214/*
6d2010ae
A
215 * vnode_trim:
216 * Used to call the DKIOCUNMAP ioctl on the underlying disk device for the specified vnode.
217 * Trims the region at offset bytes into the file, for length bytes.
218 *
219 * Care must be taken to ensure that the vnode is sufficiently reference counted at the time this
220 * function is called; no iocounts or usecounts are taken on the vnode.
221 * This function is non-idempotent in error cases; We cannot un-discard the blocks if only some of them
222 * are successfully discarded.
223 */
0a7de745
A
224u_int32_t
225vnode_trim(
226 struct vnode *vp,
227 off_t offset,
228 size_t length)
6d2010ae 229{
0a7de745
A
230 daddr64_t io_blockno; /* Block number corresponding to the start of the extent */
231 size_t io_bytecount; /* Number of bytes in current extent for the specified range */
6d2010ae 232 size_t trimmed = 0;
0a7de745 233 off_t current_offset = offset;
6d2010ae
A
234 size_t remaining_length = length;
235 int error = 0;
236 u_int32_t blocksize = 0;
237 struct vnode *devvp;
238 dk_extent_t extent;
239 dk_unmap_t unmap;
240
241
242 /* Get the underlying device vnode */
243 devvp = vp->v_mount->mnt_devvp;
244
245 /* Figure out the underlying device block size */
246 error = VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&blocksize, 0, vfs_context_kernel());
247 if (error) {
248 goto trim_exit;
249 }
250
0a7de745 251 /*
6d2010ae
A
252 * We may not get the entire range from offset -> offset+length in a single
253 * extent from the blockmap call. Keep looping/going until we are sure we've hit
254 * the whole range or if we encounter an error.
255 */
256 while (trimmed < length) {
257 /*
258 * VNOP_BLOCKMAP will tell us the logical to physical block number mapping for the
0a7de745 259 * specified offset. It returns blocks in contiguous chunks, so if the logical range is
6d2010ae
A
260 * broken into multiple extents, it must be called multiple times, increasing the offset
261 * in each call to ensure that the entire range is covered.
262 */
0a7de745
A
263 error = VNOP_BLOCKMAP(vp, current_offset, remaining_length,
264 &io_blockno, &io_bytecount, NULL, VNODE_READ | VNODE_BLOCKMAP_NO_TRACK, NULL);
6d2010ae
A
265
266 if (error) {
267 goto trim_exit;
268 }
0a7de745 269 /*
6d2010ae
A
270 * We have a contiguous run. Prepare & issue the ioctl for the device.
271 * the DKIOCUNMAP ioctl takes offset in bytes from the start of the device.
272 */
0a7de745
A
273 memset(&extent, 0, sizeof(dk_extent_t));
274 memset(&unmap, 0, sizeof(dk_unmap_t));
6d2010ae
A
275 extent.offset = (uint64_t) io_blockno * (u_int64_t) blocksize;
276 extent.length = io_bytecount;
277 unmap.extents = &extent;
278 unmap.extentsCount = 1;
279 error = VNOP_IOCTL(devvp, DKIOCUNMAP, (caddr_t)&unmap, 0, vfs_context_kernel());
280
281 if (error) {
282 goto trim_exit;
283 }
284 remaining_length = remaining_length - io_bytecount;
285 trimmed = trimmed + io_bytecount;
286 current_offset = current_offset + io_bytecount;
287 }
288trim_exit:
289
290 return error;
6d2010ae
A
291}
292
1c79356b
A
293pager_return_t
294vnode_pageout(struct vnode *vp,
0a7de745
A
295 upl_t upl,
296 upl_offset_t upl_offset,
297 vm_object_offset_t f_offset,
298 upl_size_t size,
299 int flags,
300 int *errorp)
1c79356b 301{
0a7de745
A
302 int result = PAGER_SUCCESS;
303 int error = 0;
304 int error_ret = 0;
91447636
A
305 daddr64_t blkno;
306 int isize;
1c79356b 307 int pg_index;
91447636 308 int base_index;
b0d623f7 309 upl_offset_t offset;
1c79356b 310 upl_page_info_t *pl;
0a7de745 311 vfs_context_t ctx = vfs_context_current(); /* pager context */
1c79356b 312
1c79356b
A
313 isize = (int)size;
314
9bccf70c 315 if (isize <= 0) {
0a7de745 316 result = PAGER_ERROR;
91447636 317 error_ret = EINVAL;
9bccf70c
A
318 goto out;
319 }
1c79356b 320
2d21ac55 321 if (UBCINFOEXISTS(vp) == 0) {
91447636
A
322 result = PAGER_ERROR;
323 error_ret = EINVAL;
9bccf70c 324
0a7de745
A
325 if (upl && !(flags & UPL_NOCOMMIT)) {
326 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
327 }
1c79356b
A
328 goto out;
329 }
0a7de745 330 if (!(flags & UPL_VNODE_PAGER)) {
1c79356b 331 /*
91447636
A
332 * This is a pageout from the default pager,
333 * just go ahead and call vnop_pageout since
334 * it has already sorted out the dirty ranges
1c79356b 335 */
316670eb 336 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
0a7de745
A
337 (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START,
338 size, 1, 0, 0, 0);
9bccf70c 339
0a7de745
A
340 if ((error_ret = VNOP_PAGEOUT(vp, upl, upl_offset, (off_t)f_offset,
341 (size_t)size, flags, ctx))) {
91447636 342 result = PAGER_ERROR;
0a7de745 343 }
9bccf70c 344
316670eb 345 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
0a7de745
A
346 (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END,
347 size, 1, 0, 0, 0);
9bccf70c 348
1c79356b
A
349 goto out;
350 }
b0d623f7 351 if (upl == NULL) {
0a7de745 352 int request_flags;
b0d623f7
A
353
354 if (vp->v_mount->mnt_vtable->vfc_vfsflags & VFC_VFSVNOP_PAGEOUTV2) {
355 /*
356 * filesystem has requested the new form of VNOP_PAGEOUT for file
357 * backed objects... we will not grab the UPL befofe calling VNOP_PAGEOUT...
358 * it is the fileystem's responsibility to grab the range we're denoting
359 * via 'f_offset' and 'size' into a UPL... this allows the filesystem to first
360 * take any locks it needs, before effectively locking the pages into a UPL...
361 */
0a7de745
A
362 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
363 (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START,
364 size, (int)f_offset, 0, 0, 0);
b0d623f7 365
0a7de745
A
366 if ((error_ret = VNOP_PAGEOUT(vp, NULL, upl_offset, (off_t)f_offset,
367 size, flags, ctx))) {
b0d623f7
A
368 result = PAGER_ERROR;
369 }
316670eb 370 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
0a7de745
A
371 (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END,
372 size, 0, 0, 0, 0);
b0d623f7
A
373
374 goto out;
375 }
0a7de745 376 if (flags & UPL_MSYNC) {
b0d623f7 377 request_flags = UPL_UBC_MSYNC | UPL_RET_ONLY_DIRTY;
0a7de745 378 } else {
b0d623f7 379 request_flags = UPL_UBC_PAGEOUT | UPL_RET_ONLY_DIRTY;
0a7de745
A
380 }
381
382 if (ubc_create_upl_kernel(vp, f_offset, size, &upl, &pl, request_flags, VM_KERN_MEMORY_FILE) != KERN_SUCCESS) {
b0d623f7
A
383 result = PAGER_ERROR;
384 error_ret = EINVAL;
385 goto out;
386 }
387 upl_offset = 0;
0a7de745 388 } else {
b0d623f7 389 pl = ubc_upl_pageinfo(upl);
0a7de745 390 }
b0d623f7 391
fe8ab488
A
392 /*
393 * Ignore any non-present pages at the end of the
0a7de745 394 * UPL so that we aren't looking at a upl that
fe8ab488
A
395 * may already have been freed by the preceeding
396 * aborts/completions.
397 */
398 base_index = upl_offset / PAGE_SIZE;
399
400 for (pg_index = (upl_offset + isize) / PAGE_SIZE; pg_index > base_index;) {
0a7de745
A
401 if (upl_page_present(pl, --pg_index)) {
402 break;
403 }
fe8ab488 404 if (pg_index == base_index) {
0a7de745 405 /*
fe8ab488
A
406 * no pages were returned, so release
407 * our hold on the upl and leave
408 */
0a7de745
A
409 if (!(flags & UPL_NOCOMMIT)) {
410 ubc_upl_abort_range(upl, upl_offset, isize, UPL_ABORT_FREE_ON_EMPTY);
411 }
fe8ab488
A
412
413 goto out;
414 }
415 }
416 isize = ((pg_index + 1) - base_index) * PAGE_SIZE;
417
9bccf70c 418 /*
91447636
A
419 * we come here for pageouts to 'real' files and
420 * for msyncs... the upl may not contain any
421 * dirty pages.. it's our responsibility to sort
422 * through it and find the 'runs' of dirty pages
423 * to call VNOP_PAGEOUT on...
9bccf70c 424 */
fe8ab488 425
fa4905b1 426 if (ubc_getsize(vp) == 0) {
0a7de745 427 /*
91447636
A
428 * if the file has been effectively deleted, then
429 * we need to go through the UPL and invalidate any
430 * buffer headers we might have that reference any
431 * of it's pages
432 */
433 for (offset = upl_offset; isize; isize -= PAGE_SIZE, offset += PAGE_SIZE) {
ea3f0419 434#if CONFIG_NFS_CLIENT
0a7de745 435 if (vp->v_tag == VT_NFS) {
91447636
A
436 /* check with nfs if page is OK to drop */
437 error = nfs_buf_page_inval(vp, (off_t)f_offset);
0a7de745 438 } else
ea3f0419 439#endif /* CONFIG_NFS_CLIENT */
91447636 440 {
0a7de745
A
441 blkno = ubc_offtoblk(vp, (off_t)f_offset);
442 error = buf_invalblkno(vp, blkno, 0);
91447636
A
443 }
444 if (error) {
0a7de745
A
445 if (!(flags & UPL_NOCOMMIT)) {
446 ubc_upl_abort_range(upl, offset, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
447 }
448 if (error_ret == 0) {
449 error_ret = error;
450 }
91447636 451 result = PAGER_ERROR;
0a7de745
A
452 } else if (!(flags & UPL_NOCOMMIT)) {
453 ubc_upl_commit_range(upl, offset, PAGE_SIZE, UPL_COMMIT_FREE_ON_EMPTY);
fa4905b1 454 }
91447636 455 f_offset += PAGE_SIZE;
1c79356b 456 }
1c79356b
A
457 goto out;
458 }
91447636
A
459
460 offset = upl_offset;
461 pg_index = base_index;
1c79356b
A
462
463 while (isize) {
464 int xsize;
465 int num_of_pages;
466
0a7de745
A
467 if (!upl_page_present(pl, pg_index)) {
468 /*
91447636
A
469 * we asked for RET_ONLY_DIRTY, so it's possible
470 * to get back empty slots in the UPL
471 * just skip over them
472 */
0a7de745 473 f_offset += PAGE_SIZE;
2d21ac55
A
474 offset += PAGE_SIZE;
475 isize -= PAGE_SIZE;
1c79356b
A
476 pg_index++;
477
478 continue;
479 }
0a7de745 480 if (!upl_dirty_page(pl, pg_index)) {
1c79356b
A
481 /*
482 * if the page is not dirty and reached here it is
483 * marked precious or it is due to invalidation in
484 * memory_object_lock request as part of truncation
485 * We also get here from vm_object_terminate()
486 * So all you need to do in these
487 * cases is to invalidate incore buffer if it is there
91447636 488 * Note we must not sleep here if the buffer is busy - that is
fa4905b1 489 * a lock inversion which causes deadlock.
1c79356b 490 */
ea3f0419 491#if CONFIG_NFS_CLIENT
0a7de745 492 if (vp->v_tag == VT_NFS) {
55e303ae 493 /* check with nfs if page is OK to drop */
2d21ac55 494 error = nfs_buf_page_inval(vp, (off_t)f_offset);
0a7de745 495 } else
ea3f0419 496#endif /* CONFIG_NFS_CLIENT */
91447636 497 {
0a7de745
A
498 blkno = ubc_offtoblk(vp, (off_t)f_offset);
499 error = buf_invalblkno(vp, blkno, 0);
91447636
A
500 }
501 if (error) {
0a7de745
A
502 if (!(flags & UPL_NOCOMMIT)) {
503 ubc_upl_abort_range(upl, offset, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
504 }
505 if (error_ret == 0) {
506 error_ret = error;
507 }
91447636 508 result = PAGER_ERROR;
0a7de745
A
509 } else if (!(flags & UPL_NOCOMMIT)) {
510 ubc_upl_commit_range(upl, offset, PAGE_SIZE, UPL_COMMIT_FREE_ON_EMPTY);
91447636 511 }
0a7de745 512 f_offset += PAGE_SIZE;
2d21ac55
A
513 offset += PAGE_SIZE;
514 isize -= PAGE_SIZE;
1c79356b
A
515 pg_index++;
516
517 continue;
518 }
1c79356b
A
519 num_of_pages = 1;
520 xsize = isize - PAGE_SIZE;
521
522 while (xsize) {
0a7de745 523 if (!upl_dirty_page(pl, pg_index + num_of_pages)) {
1c79356b 524 break;
0a7de745 525 }
1c79356b
A
526 num_of_pages++;
527 xsize -= PAGE_SIZE;
528 }
529 xsize = num_of_pages * PAGE_SIZE;
530
316670eb 531 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
0a7de745
A
532 (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START,
533 xsize, (int)f_offset, 0, 0, 0);
9bccf70c 534
0a7de745
A
535 if ((error = VNOP_PAGEOUT(vp, upl, offset, (off_t)f_offset,
536 xsize, flags, ctx))) {
537 if (error_ret == 0) {
538 error_ret = error;
539 }
91447636
A
540 result = PAGER_ERROR;
541 }
316670eb 542 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
0a7de745
A
543 (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END,
544 xsize, 0, 0, 0, 0);
9bccf70c 545
0a7de745 546 f_offset += xsize;
2d21ac55
A
547 offset += xsize;
548 isize -= xsize;
1c79356b
A
549 pg_index += num_of_pages;
550 }
551out:
0a7de745 552 if (errorp) {
91447636 553 *errorp = error_ret;
0a7de745 554 }
1c79356b 555
0a7de745 556 return result;
1c79356b
A
557}
558
559
560pager_return_t
561vnode_pagein(
0a7de745
A
562 struct vnode *vp,
563 upl_t upl,
564 upl_offset_t upl_offset,
565 vm_object_offset_t f_offset,
566 upl_size_t size,
567 int flags,
568 int *errorp)
1c79356b 569{
0a7de745
A
570 upl_page_info_t *pl;
571 int result = PAGER_SUCCESS;
572 int error = 0;
573 int pages_in_upl;
574 int start_pg;
575 int last_pg;
9bccf70c 576 int first_pg;
0a7de745
A
577 int xsize;
578 int must_commit = 1;
579 int ignore_valid_page_check = 0;
1c79356b 580
0a7de745
A
581 if (flags & UPL_NOCOMMIT) {
582 must_commit = 0;
583 }
1c79356b 584
0a7de745 585 if (flags & UPL_IGNORE_VALID_PAGE_CHECK) {
39236c6e 586 ignore_valid_page_check = 1;
0a7de745 587 }
39236c6e 588
2d21ac55 589 if (UBCINFOEXISTS(vp) == 0) {
1c79356b
A
590 result = PAGER_ERROR;
591 error = PAGER_ERROR;
2d21ac55 592
0a7de745 593 if (upl && must_commit) {
9bccf70c 594 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
0a7de745 595 }
2d21ac55 596
1c79356b
A
597 goto out;
598 }
9bccf70c 599 if (upl == (upl_t)NULL) {
b0d623f7 600 flags &= ~UPL_NOCOMMIT;
2d21ac55 601
0a7de745
A
602 if (size > MAX_UPL_SIZE_BYTES) {
603 result = PAGER_ERROR;
9bccf70c
A
604 error = PAGER_ERROR;
605 goto out;
606 }
b0d623f7
A
607 if (vp->v_mount->mnt_vtable->vfc_vfsflags & VFC_VFSVNOP_PAGEINV2) {
608 /*
609 * filesystem has requested the new form of VNOP_PAGEIN for file
610 * backed objects... we will not grab the UPL befofe calling VNOP_PAGEIN...
611 * it is the fileystem's responsibility to grab the range we're denoting
612 * via 'f_offset' and 'size' into a UPL... this allows the filesystem to first
613 * take any locks it needs, before effectively locking the pages into a UPL...
614 * so we pass a NULL into the filesystem instead of a UPL pointer... the 'upl_offset'
615 * is used to identify the "must have" page in the extent... the filesystem is free
0a7de745 616 * to clip the extent to better fit the underlying FS blocksize if it desires as
b0d623f7
A
617 * long as it continues to include the "must have" page... 'f_offset' + 'upl_offset'
618 * identifies that page
619 */
0a7de745
A
620 if ((error = VNOP_PAGEIN(vp, NULL, upl_offset, (off_t)f_offset,
621 size, flags, vfs_context_current()))) {
cb323159 622 set_thread_pagein_error(current_thread(), error);
b0d623f7
A
623 result = PAGER_ERROR;
624 error = PAGER_ERROR;
625 }
626 goto out;
627 }
0a7de745 628 ubc_create_upl_kernel(vp, f_offset, size, &upl, &pl, UPL_UBC_PAGEIN | UPL_RET_ONLY_ABSENT, VM_KERN_MEMORY_FILE);
1c79356b 629
9bccf70c 630 if (upl == (upl_t)NULL) {
0a7de745 631 result = PAGER_ABSENT;
9bccf70c
A
632 error = PAGER_ABSENT;
633 goto out;
1c79356b 634 }
316670eb
A
635 ubc_upl_range_needed(upl, upl_offset / PAGE_SIZE, 1);
636
9bccf70c 637 upl_offset = 0;
2d21ac55 638 first_pg = 0;
0a7de745 639
9bccf70c
A
640 /*
641 * if we get here, we've created the upl and
642 * are responsible for commiting/aborting it
643 * regardless of what the caller has passed in
644 */
2d21ac55 645 must_commit = 1;
1c79356b 646 } else {
0a7de745 647 pl = ubc_upl_pageinfo(upl);
2d21ac55 648 first_pg = upl_offset / PAGE_SIZE;
9bccf70c
A
649 }
650 pages_in_upl = size / PAGE_SIZE;
2d21ac55 651 DTRACE_VM2(pgpgin, int, pages_in_upl, (uint64_t *), NULL);
9bccf70c
A
652
653 /*
0a7de745 654 * before we start marching forward, we must make sure we end on
9bccf70c 655 * a present page, otherwise we will be working with a freed
0a7de745 656 * upl
9bccf70c
A
657 */
658 for (last_pg = pages_in_upl - 1; last_pg >= first_pg; last_pg--) {
0a7de745 659 if (upl_page_present(pl, last_pg)) {
9bccf70c 660 break;
0a7de745 661 }
2d21ac55 662 if (last_pg == first_pg) {
0a7de745 663 /*
2d21ac55
A
664 * empty UPL, no pages are present
665 */
0a7de745
A
666 if (must_commit) {
667 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
668 }
2d21ac55
A
669 goto out;
670 }
9bccf70c
A
671 }
672 pages_in_upl = last_pg + 1;
2d21ac55 673 last_pg = first_pg;
9bccf70c 674
2d21ac55 675 while (last_pg < pages_in_upl) {
0a7de745 676 /*
2d21ac55 677 * skip over missing pages...
9bccf70c 678 */
0a7de745
A
679 for (; last_pg < pages_in_upl; last_pg++) {
680 if (upl_page_present(pl, last_pg)) {
681 break;
682 }
9bccf70c 683 }
39236c6e
A
684
685 if (ignore_valid_page_check == 1) {
686 start_pg = last_pg;
687 } else {
0a7de745 688 /*
39236c6e
A
689 * skip over 'valid' pages... we don't want to issue I/O for these
690 */
0a7de745
A
691 for (start_pg = last_pg; last_pg < pages_in_upl; last_pg++) {
692 if (!upl_valid_page(pl, last_pg)) {
693 break;
694 }
39236c6e 695 }
9bccf70c 696 }
39236c6e 697
9bccf70c 698 if (last_pg > start_pg) {
0a7de745 699 /*
9bccf70c
A
700 * we've found a range of valid pages
701 * if we've got COMMIT responsibility
702 * commit this range of pages back to the
703 * cache unchanged
704 */
0a7de745 705 xsize = (last_pg - start_pg) * PAGE_SIZE;
1c79356b 706
0a7de745
A
707 if (must_commit) {
708 ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, xsize, UPL_ABORT_FREE_ON_EMPTY);
709 }
9bccf70c 710 }
0a7de745
A
711 if (last_pg == pages_in_upl) {
712 /*
2d21ac55 713 * we're done... all pages that were present
0a7de745 714 * have either had I/O issued on them or
2d21ac55
A
715 * were aborted unchanged...
716 */
0a7de745
A
717 break;
718 }
9bccf70c 719
2d21ac55 720 if (!upl_page_present(pl, last_pg)) {
0a7de745
A
721 /*
722 * we found a range of valid pages
2d21ac55
A
723 * terminated by a missing page...
724 * bump index to the next page and continue on
9bccf70c 725 */
0a7de745
A
726 last_pg++;
727 continue;
2d21ac55 728 }
9bccf70c
A
729 /*
730 * scan from the found invalid page looking for a valid
731 * or non-present page before the end of the upl is reached, if we
732 * find one, then it will be the last page of the request to
733 * 'cluster_io'
734 */
735 for (start_pg = last_pg; last_pg < pages_in_upl; last_pg++) {
0a7de745
A
736 if ((!ignore_valid_page_check && upl_valid_page(pl, last_pg)) || !upl_page_present(pl, last_pg)) {
737 break;
738 }
9bccf70c
A
739 }
740 if (last_pg > start_pg) {
0a7de745
A
741 int xoff;
742 xsize = (last_pg - start_pg) * PAGE_SIZE;
9bccf70c
A
743 xoff = start_pg * PAGE_SIZE;
744
0a7de745
A
745 if ((error = VNOP_PAGEIN(vp, upl, (upl_offset_t) xoff,
746 (off_t)f_offset + xoff,
747 xsize, flags, vfs_context_current()))) {
748 /*
b0d623f7 749 * Usually this UPL will be aborted/committed by the lower cluster layer.
6d2010ae
A
750 *
751 * a) In the case of decmpfs, however, we may return an error (EAGAIN) to avoid
0a7de745 752 * a deadlock with another thread already inflating the file.
6d2010ae
A
753 *
754 * b) In the case of content protection, EPERM is a valid error and we should respect it.
755 *
756 * In those cases, we must take care of our UPL at this layer itself.
b0d623f7
A
757 */
758 if (must_commit) {
0a7de745
A
759 if (error == EAGAIN) {
760 ubc_upl_abort_range(upl, (upl_offset_t) xoff, xsize, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_RESTART);
b0d623f7 761 }
0a7de745
A
762 if (error == EPERM) {
763 ubc_upl_abort_range(upl, (upl_offset_t) xoff, xsize, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
6d2010ae 764 }
b0d623f7 765 }
cb323159 766 set_thread_pagein_error(current_thread(), error);
0b4e3aa0
A
767 result = PAGER_ERROR;
768 error = PAGER_ERROR;
769 }
1c79356b 770 }
0a7de745 771 }
1c79356b 772out:
0a7de745 773 if (errorp) {
fa4905b1 774 *errorp = result;
0a7de745 775 }
1c79356b 776
0a7de745 777 return error;
1c79356b
A
778}
779
0b4e3aa0 780void *
1c79356b
A
781upl_get_internal_page_list(upl_t upl)
782{
0a7de745 783 return UPL_GET_INTERNAL_PAGE_LIST(upl);
1c79356b 784}