Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/*
 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
/*
 * Mach Operating System
 * Copyright (c) 1987 Carnegie-Mellon University
 * All rights reserved.  The CMU software License Agreement specifies
 * the terms and conditions for use and redistribution.
 */
/*
 *	File:	vnode_pager.c
 *
 *	"Swap" pager that pages to/from vnodes.  Also
 *	handles demand paging from files.
 *
 */

#include <mach/boolean.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/kauth.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/vnode_internal.h>
#include <sys/namei.h>
#include <sys/mount_internal.h> /* needs internal due to fhandle_t */
#include <sys/ubc_internal.h>
#include <sys/lock.h>
#include <sys/disk.h>           /* For DKIOC calls */

#include <mach/mach_types.h>
#include <mach/memory_object_types.h>
#include <mach/vm_map.h>
#include <mach/mach_vm.h>
#include <mach/upl.h>
#include <mach/sdt.h>

#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include <kern/zalloc.h>
#include <libkern/libkern.h>
#include <kern/telemetry.h>

#include <vm/vnode_pager.h>
#include <vm/vm_pageout.h>
#include <vm/vm_ubc.h>

#include <kern/assert.h>
#include <sys/kdebug.h>
#include <nfs/nfs.h>

#include <vm/vm_protos_internal.h>

#include <sys/kdebug.h>
#include <sys/kdebug_triage.h>
#include <vfs/vfs_disk_conditioner.h>

void
vnode_pager_throttle(void)
{
	if (current_uthread()->uu_lowpri_window) {
		throttle_lowpri_io(1);
	}
}

boolean_t
vnode_pager_isSSD(vnode_t vp)
{
	return disk_conditioner_mount_is_ssd(vp->v_mount);
}

#if FBDP_DEBUG_OBJECT_NO_PAGER
bool
vnode_pager_forced_unmount(vnode_t vp)
{
	mount_t mnt;
	mnt = vnode_mount(vp);
	if (!mnt) {
		return false;
	}
	return vfs_isforce(mnt);
}
#endif /* FBDP_DEBUG_OBJECT_NO_PAGER */

#if CONFIG_IOSCHED
void
vnode_pager_issue_reprioritize_io(struct vnode *devvp, uint64_t blkno, uint32_t len, int priority)
{
	u_int32_t blocksize = 0;
	dk_extent_t extent;
	dk_set_tier_t set_tier;
	int error = 0;

	error = VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&blocksize, 0, vfs_context_kernel());
	if (error) {
		return;
	}

	memset(&extent, 0, sizeof(dk_extent_t));
	memset(&set_tier, 0, sizeof(dk_set_tier_t));

	extent.offset = blkno * (u_int64_t) blocksize;
	extent.length = len;

	set_tier.extents = &extent;
	set_tier.extentsCount = 1;
	set_tier.tier = (uint8_t)priority;

	error = VNOP_IOCTL(devvp, DKIOCSETTIER, (caddr_t)&set_tier, 0, vfs_context_kernel());
	return;
}
#endif

void
vnode_pager_was_dirtied(
	struct vnode            *vp,
	vm_object_offset_t      s_offset,
	vm_object_offset_t      e_offset)
{
	cluster_update_state(vp, s_offset, e_offset, TRUE);
}

uint32_t
vnode_pager_isinuse(struct vnode *vp)
{
	if (vp->v_usecount > vp->v_kusecount) {
		return 1;
	}
	return 0;
}

uint32_t
vnode_pager_return_throttle_io_limit(struct vnode *vp, uint32_t *limit)
{
	return cluster_throttle_io_limit(vp, limit);
}

vm_object_offset_t
vnode_pager_get_filesize(struct vnode *vp)
{
	return (vm_object_offset_t) ubc_getsize(vp);
}

extern int safe_getpath(struct vnode *dvp, char *leafname, char *path, int _len, int *truncated_path);

kern_return_t
vnode_pager_get_name(
	struct vnode    *vp,
	char            *pathname,
	vm_size_t       pathname_len,
	char            *filename,
	vm_size_t       filename_len,
	boolean_t       *truncated_path_p)
{
	*truncated_path_p = FALSE;
	if (pathname != NULL) {
		/* get the path name */
		safe_getpath(vp, NULL,
		    pathname, (int) pathname_len,
		    truncated_path_p);
	}
	if ((pathname == NULL || *truncated_path_p) &&
	    filename != NULL) {
		/* get the file name */
		const char *name;

		name = vnode_getname_printable(vp);
		strlcpy(filename, name, (size_t) filename_len);
		vnode_putname_printable(name);
	}
	return KERN_SUCCESS;
}

kern_return_t
vnode_pager_get_mtime(
	struct vnode    *vp,
	struct timespec *current_mtime,
	struct timespec *cs_mtime)
{
	vnode_mtime(vp, current_mtime, vfs_context_current());
	if (cs_mtime != NULL) {
		ubc_get_cs_mtime(vp, cs_mtime);
	}
	return KERN_SUCCESS;
}

kern_return_t
vnode_pager_get_cs_blobs(
	struct vnode    *vp,
	void            **blobs)
{
	*blobs = ubc_get_cs_blobs(vp);
	return KERN_SUCCESS;
}

/*
 * vnode_trim:
 * Used to call the DKIOCUNMAP ioctl on the underlying disk device for the specified vnode.
 * Trims the region at offset bytes into the file, for length bytes.
 *
 * Care must be taken to ensure that the vnode is sufficiently reference counted at the time this
 * function is called; no iocounts or usecounts are taken on the vnode.
 * This function is non-idempotent in error cases;  We cannot un-discard the blocks if only some of them
 * are successfully discarded.
 */
u_int32_t
vnode_trim(
	struct vnode *vp,
	off_t offset,
	size_t length)
{
	daddr64_t io_blockno;    /* Block number corresponding to the start of the extent */
	size_t io_bytecount;    /* Number of bytes in current extent for the specified range */
	size_t trimmed = 0;
	off_t current_offset = offset;
	size_t remaining_length = length;
	int error = 0;
	u_int32_t blocksize = 0;
	struct vnode *devvp;
	dk_extent_t extent;
	dk_unmap_t unmap;


	/* Get the underlying device vnode */
	devvp = vp->v_mount->mnt_devvp;

	/* Figure out the underlying device block size */
	error  = VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&blocksize, 0, vfs_context_kernel());
	if (error) {
		goto trim_exit;
	}

	/*
	 * We may not get the entire range from offset -> offset+length in a single
	 * extent from the blockmap call.  Keep looping/going until we are sure we've hit
	 * the whole range or if we encounter an error.
	 */
	while (trimmed < length) {
		/*
		 * VNOP_BLOCKMAP will tell us the logical to physical block number mapping for the
		 * specified offset.  It returns blocks in contiguous chunks, so if the logical range is
		 * broken into multiple extents, it must be called multiple times, increasing the offset
		 * in each call to ensure that the entire range is covered.
		 */
		error = VNOP_BLOCKMAP(vp, current_offset, remaining_length,
		    &io_blockno, &io_bytecount, NULL, VNODE_READ | VNODE_BLOCKMAP_NO_TRACK, NULL);

		if (error) {
			goto trim_exit;
		}
		/*
		 * We have a contiguous run.  Prepare & issue the ioctl for the device.
		 * the DKIOCUNMAP ioctl takes offset in bytes from the start of the device.
		 */
		memset(&extent, 0, sizeof(dk_extent_t));
		memset(&unmap, 0, sizeof(dk_unmap_t));
		extent.offset = (uint64_t) io_blockno * (u_int64_t) blocksize;
		extent.length = io_bytecount;
		unmap.extents = &extent;
		unmap.extentsCount = 1;
		error = VNOP_IOCTL(devvp, DKIOCUNMAP, (caddr_t)&unmap, 0, vfs_context_kernel());

		if (error) {
			goto trim_exit;
		}
		remaining_length = remaining_length - io_bytecount;
		trimmed = trimmed + io_bytecount;
		current_offset = current_offset + io_bytecount;
	}
trim_exit:

	return error;
}

pager_return_t
vnode_pageout(struct vnode *vp,
    upl_t                   upl,
    upl_offset_t            upl_offset,
    vm_object_offset_t      f_offset,
    upl_size_t              size,
    int                     flags,
    int                     *errorp)
{
	int             result = PAGER_SUCCESS;
	int             error = 0;
	int             error_ret = 0;
	daddr64_t blkno;
	int isize;
	int pg_index;
	int base_index;
	upl_offset_t offset;
	upl_page_info_t *pl;
	vfs_context_t ctx = vfs_context_current();      /* pager context */

	isize = (int)size;

	/*
	 * This call is non-blocking and does not ever fail but it can
	 * only be made when there is other explicit synchronization
	 * with reclaiming of the vnode which, in this path, is provided
	 * by the paging in progress counter.
	 *
	 * In addition, this may also be entered via explicit ubc_msync
	 * calls or vm_swapfile_io where the existing iocount provides
	 * the necessary synchronization. Ideally we would not take an
	 * additional iocount here in the cases where an explcit iocount
	 * has already been taken but this call doesn't cause a deadlock
	 * as other forms of vnode_get* might if this thread has already
	 * taken an iocount.
	 */
	error = vnode_getalways_from_pager(vp);
	if (error != 0) {
		/* This can't happen */
		panic("vnode_getalways returned %d for vp %p", error, vp);
	}

	if (isize <= 0) {
		result    = PAGER_ERROR;
		error_ret = EINVAL;
		goto out;
	}

	if (UBCINFOEXISTS(vp) == 0) {
		result    = PAGER_ERROR;
		error_ret = EINVAL;

		if (upl && !(flags & UPL_NOCOMMIT)) {
			ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
		}
		goto out;
	}
	if (!(flags & UPL_VNODE_PAGER)) {
		/*
		 * This is a pageout from the default pager,
		 * just go ahead and call vnop_pageout since
		 * it has already sorted out the dirty ranges
		 */
		KDBG_RELEASE(
			VMDBG_CODE(DBG_VM_VNODE_PAGEOUT) | DBG_FUNC_START,
			size, 1);

		if ((error_ret = VNOP_PAGEOUT(vp, upl, upl_offset, (off_t)f_offset,
		    (size_t)size, flags, ctx))) {
			result = PAGER_ERROR;
		}

		KDBG_RELEASE(
			VMDBG_CODE(DBG_VM_VNODE_PAGEOUT) | DBG_FUNC_END,
			size, 1);

		goto out;
	}
	if (upl == NULL) {
		int                     request_flags;

		if (vp->v_mount->mnt_vtable->vfc_vfsflags & VFC_VFSVNOP_PAGEOUTV2) {
			/*
			 * filesystem has requested the new form of VNOP_PAGEOUT for file
			 * backed objects... we will not grab the UPL befofe calling VNOP_PAGEOUT...
			 * it is the fileystem's responsibility to grab the range we're denoting
			 * via 'f_offset' and 'size' into a UPL... this allows the filesystem to first
			 * take any locks it needs, before effectively locking the pages into a UPL...
			 */
			KDBG_RELEASE(VMDBG_CODE(DBG_VM_VNODE_PAGEOUT) | DBG_FUNC_START,
			    size, (int)f_offset);

			if ((error_ret = VNOP_PAGEOUT(vp, NULL, upl_offset, (off_t)f_offset,
			    size, flags, ctx))) {
				result = PAGER_ERROR;
			}
			KDBG_RELEASE(VMDBG_CODE(DBG_VM_VNODE_PAGEOUT) | DBG_FUNC_END,
			    size);

			goto out;
		}
		if (flags & UPL_MSYNC) {
			request_flags = UPL_UBC_MSYNC | UPL_RET_ONLY_DIRTY;
		} else {
			request_flags = UPL_UBC_PAGEOUT | UPL_RET_ONLY_DIRTY;
		}

		if (ubc_create_upl_kernel(vp, f_offset, size, &upl, &pl, request_flags, VM_KERN_MEMORY_FILE) != KERN_SUCCESS) {
			result    = PAGER_ERROR;
			error_ret = EINVAL;
			goto out;
		}
		upl_offset = 0;
	} else {
		pl = ubc_upl_pageinfo(upl);
	}

	/*
	 * Ignore any non-present pages at the end of the
	 * UPL so that we aren't looking at a upl that
	 * may already have been freed by the preceeding
	 * aborts/completions.
	 */
	base_index = upl_offset / PAGE_SIZE;

	for (pg_index = (upl_offset + isize) / PAGE_SIZE; pg_index > base_index;) {
		if (upl_page_present(pl, --pg_index)) {
			break;
		}
		if (pg_index == base_index) {
			/*
			 * no pages were returned, so release
			 * our hold on the upl and leave
			 */
			if (!(flags & UPL_NOCOMMIT)) {
				ubc_upl_abort_range(upl, upl_offset, isize, UPL_ABORT_FREE_ON_EMPTY);
			}

			goto out;
		}
	}
	isize = ((pg_index + 1) - base_index) * PAGE_SIZE;

	/*
	 * we come here for pageouts to 'real' files and
	 * for msyncs...  the upl may not contain any
	 * dirty pages.. it's our responsibility to sort
	 * through it and find the 'runs' of dirty pages
	 * to call VNOP_PAGEOUT on...
	 */

	if (ubc_getsize(vp) == 0) {
		/*
		 * if the file has been effectively deleted, then
		 * we need to go through the UPL and invalidate any
		 * buffer headers we might have that reference any
		 * of it's pages
		 */
		for (offset = upl_offset; isize; isize -= PAGE_SIZE, offset += PAGE_SIZE) {
			if (vp->v_tag == VT_NFS) {
				/* check with nfs if page is OK to drop */
				error = nfs_buf_page_inval(vp, (off_t)f_offset);
			} else {
				blkno = ubc_offtoblk(vp, (off_t)f_offset);
				error = buf_invalblkno(vp, blkno, 0);
			}
			if (error) {
				if (!(flags & UPL_NOCOMMIT)) {
					ubc_upl_abort_range(upl, offset, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
				}
				if (error_ret == 0) {
					error_ret = error;
				}
				result = PAGER_ERROR;
			} else if (!(flags & UPL_NOCOMMIT)) {
				ubc_upl_commit_range(upl, offset, PAGE_SIZE, UPL_COMMIT_FREE_ON_EMPTY);
			}
			f_offset += PAGE_SIZE;
		}
		goto out;
	}

	offset = upl_offset;
	pg_index = base_index;

	while (isize) {
		int  xsize;
		int  num_of_pages;

		if (!upl_page_present(pl, pg_index)) {
			/*
			 * we asked for RET_ONLY_DIRTY, so it's possible
			 * to get back empty slots in the UPL
			 * just skip over them
			 */
			f_offset += PAGE_SIZE;
			offset   += PAGE_SIZE;
			isize    -= PAGE_SIZE;
			pg_index++;

			continue;
		}
		if (!upl_dirty_page(pl, pg_index)) {
			/*
			 * if the page is not dirty and reached here it is
			 * marked precious or it is due to invalidation in
			 * memory_object_lock request as part of truncation
			 * We also get here from vm_object_terminate()
			 * So all you need to do in these
			 * cases is to invalidate incore buffer if it is there
			 * Note we must not sleep here if the buffer is busy - that is
			 * a lock inversion which causes deadlock.
			 */
			if (vp->v_tag == VT_NFS) {
				/* check with nfs if page is OK to drop */
				error = nfs_buf_page_inval(vp, (off_t)f_offset);
			} else {
				blkno = ubc_offtoblk(vp, (off_t)f_offset);
				error = buf_invalblkno(vp, blkno, 0);
			}
			if (error) {
				if (!(flags & UPL_NOCOMMIT)) {
					ubc_upl_abort_range(upl, offset, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
				}
				if (error_ret == 0) {
					error_ret = error;
				}
				result = PAGER_ERROR;
			} else if (!(flags & UPL_NOCOMMIT)) {
				ubc_upl_commit_range(upl, offset, PAGE_SIZE, UPL_COMMIT_FREE_ON_EMPTY);
			}
			f_offset += PAGE_SIZE;
			offset   += PAGE_SIZE;
			isize    -= PAGE_SIZE;
			pg_index++;

			continue;
		}
		num_of_pages = 1;
		xsize = isize - PAGE_SIZE;

		while (xsize) {
			if (!upl_dirty_page(pl, pg_index + num_of_pages)) {
				break;
			}
			num_of_pages++;
			xsize -= PAGE_SIZE;
		}
		xsize = num_of_pages * PAGE_SIZE;

		KDBG_RELEASE(VMDBG_CODE(DBG_VM_VNODE_PAGEOUT) | DBG_FUNC_START,
		    xsize, (int)f_offset);

		if ((error = VNOP_PAGEOUT(vp, upl, offset, (off_t)f_offset,
		    xsize, flags, ctx))) {
			if (error_ret == 0) {
				error_ret = error;
			}
			result = PAGER_ERROR;
		}
		KDBG_RELEASE(VMDBG_CODE(DBG_VM_VNODE_PAGEOUT) | DBG_FUNC_END,
		    xsize, error);

		f_offset += xsize;
		offset   += xsize;
		isize    -= xsize;
		pg_index += num_of_pages;
	}
out:
	vnode_put_from_pager(vp);

	if (errorp) {
		*errorp = error_ret;
	}

	return result;
}

static uint64_t
ktriage_encode_v_tag_and_error(vnode_t vp, int error)
{
	uint64_t tag = vp->v_tag;
	uint32_t ktriage_error = (uint32_t)error;
	return tag << 32 | ktriage_error;
}

int
vnode_get_ids(struct vnode *vp, uint64_t *fsid_out, uint64_t *fsobj_id_out);
int
vnode_get_ids(struct vnode *vp, uint64_t *fsid_out, uint64_t *fsobj_id_out)
{
	struct vnode_attr *attr = kalloc_type(struct vnode_attr, Z_WAITOK | Z_ZERO);
	if (!attr) {
		return ENOMEM;
	}

	VATTR_INIT(attr);
	VATTR_WANTED(attr, va_fsid64);
	VATTR_WANTED(attr, va_fsid);
	VATTR_WANTED(attr, va_fileid);
	int error = vnode_getattr(vp, attr, vfs_context_current());
	if (error == 0) {
		*fsid_out = vnode_get_va_fsid(attr);
		*fsobj_id_out = attr->va_fileid;
	}
	kfree_type(struct vnode_attr, attr);
	return error;
}


pager_return_t
vnode_pagein(
	struct vnode            *vp,
	upl_t                   upl,
	upl_offset_t            upl_offset,
	vm_object_offset_t      f_offset,
	upl_size_t              size,
	int                     flags,
	int                     *errorp)
{
	upl_page_info_t *pl;
	int             result = PAGER_SUCCESS;
	int             error = 0;
	int             pages_in_upl;
	int             start_pg;
	int             last_pg;
	int             first_pg;
	bool            must_commit = !(flags & UPL_NOCOMMIT);
	bool const      ignore_valid_page_check = flags & UPL_IGNORE_VALID_PAGE_CHECK;

	/*
	 * This call is non-blocking and does not ever fail but it can
	 * only be made when there is other explicit synchronization
	 * with reclaiming of the vnode which, in this path, is provided
	 * by the paging in progress counter.
	 *
	 * In addition, this may also be entered via vm_swapfile_io
	 * where the existing iocount provides the necessary synchronization.
	 * Ideally we would not take an additional iocount here in the cases
	 * where an explcit iocount has already been taken but this call
	 * doesn't cause a deadlock as other forms of vnode_get* might if
	 * this thread has already taken an iocount.
	 */
	error = vnode_getalways_from_pager(vp);
	if (error != 0) {
		/* This can't happen */
		panic("vnode_getalways returned %d for vp %p", error, vp);
	}

	if (UBCINFOEXISTS(vp) == 0) {
		result = PAGER_ERROR;
		error  = PAGER_ERROR;

		if (upl && must_commit) {
			ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
		}

		ktriage_record(thread_tid(current_thread()),
		    KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_VNODEPAGEIN_NO_UBCINFO),
		    ktriage_encode_v_tag_and_error(vp, 0));
		goto out;
	}
	if (upl == (upl_t)NULL) {
		flags &= ~UPL_NOCOMMIT;

		if (size > MAX_UPL_SIZE_BYTES) {
			result = PAGER_ERROR;
			error  = PAGER_ERROR;
			goto out;
		}
		if (vp->v_mount->mnt_vtable->vfc_vfsflags & VFC_VFSVNOP_PAGEINV2) {
			/*
			 * filesystem has requested the new form of VNOP_PAGEIN for file
			 * backed objects... we will not grab the UPL befofe calling VNOP_PAGEIN...
			 * it is the fileystem's responsibility to grab the range we're denoting
			 * via 'f_offset' and 'size' into a UPL... this allows the filesystem to first
			 * take any locks it needs, before effectively locking the pages into a UPL...
			 * so we pass a NULL into the filesystem instead of a UPL pointer... the 'upl_offset'
			 * is used to identify the "must have" page in the extent... the filesystem is free
			 * to clip the extent to better fit the underlying FS blocksize if it desires as
			 * long as it continues to include the "must have" page... 'f_offset' + 'upl_offset'
			 * identifies that page
			 */
			if ((error = VNOP_PAGEIN(vp, NULL, upl_offset, (off_t)f_offset,
			    size, flags, vfs_context_current()))) {
				set_thread_pagein_error(current_thread(), error);
				ktriage_record(thread_tid(current_thread()),
				    KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_VNODEPAGEIN_FSPAGEIN_FAIL),
				    ktriage_encode_v_tag_and_error(vp, error));
				result = PAGER_ERROR;
				error  = PAGER_ERROR;
			}
			goto out;
		}
		ubc_create_upl_kernel(vp, f_offset, size, &upl, &pl, UPL_UBC_PAGEIN | UPL_RET_ONLY_ABSENT, VM_KERN_MEMORY_FILE);

		if (upl == (upl_t)NULL) {
			result =  PAGER_ABSENT;
			error = PAGER_ABSENT;
			ktriage_record(thread_tid(current_thread()),
			    KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_VNODEPAGEIN_NO_UPL),
			    ktriage_encode_v_tag_and_error(vp, 0));
			goto out;
		}
		ubc_upl_range_needed(upl, upl_offset / PAGE_SIZE, 1);

		upl_offset = 0;
		first_pg = 0;

		/*
		 * if we get here, we've created the upl and
		 * are responsible for commiting/aborting it
		 * regardless of what the caller has passed in
		 */
		must_commit = true;
	} else {
		pl = ubc_upl_pageinfo(upl);
		first_pg = upl_offset / PAGE_SIZE;
	}
	pages_in_upl = size / PAGE_SIZE;
	DTRACE_VM2(pgpgin, int, pages_in_upl, (uint64_t *), NULL);

	/*
	 * before we start marching forward, we must make sure we end on
	 * a present page, otherwise we will be working with a freed
	 * upl
	 */
	for (last_pg = pages_in_upl - 1; last_pg >= first_pg; last_pg--) {
		if (upl_page_present(pl, last_pg)) {
			break;
		}
		if (last_pg == first_pg) {
			/*
			 * empty UPL, no pages are present
			 */
			if (must_commit) {
				ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
			}
			goto out;
		}
	}
	pages_in_upl = last_pg + 1;
	last_pg = first_pg;

	while (last_pg < pages_in_upl) {
		/*
		 * skip over missing pages...
		 */
		for (; last_pg < pages_in_upl; last_pg++) {
			if (upl_page_present(pl, last_pg)) {
				break;
			}
		}

		if (ignore_valid_page_check) {
			start_pg = last_pg;
		} else {
			/*
			 * skip over 'valid' pages... we don't want to issue I/O for these
			 */
			for (start_pg = last_pg; last_pg < pages_in_upl; last_pg++) {
				if (!upl_valid_page(pl, last_pg)) {
					break;
				}
			}
		}

		if (last_pg > start_pg) {
			/*
			 * we've found a range of valid pages
			 * if we've got COMMIT responsibility
			 * commit this range of pages back to the
			 * cache unchanged
			 */
			int xsize = (last_pg - start_pg) * PAGE_SIZE;

			if (must_commit) {
				ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, xsize, UPL_ABORT_FREE_ON_EMPTY);
			}
		}
		if (last_pg == pages_in_upl) {
			/*
			 * we're done... all pages that were present
			 * have either had I/O issued on them or
			 * were aborted unchanged...
			 */
			break;
		}

		if (!upl_page_present(pl, last_pg)) {
			/*
			 * we found a range of valid pages
			 * terminated by a missing page...
			 * bump index to the next page and continue on
			 */
			last_pg++;
			continue;
		}
		/*
		 * scan from the found invalid page looking for a valid
		 * or non-present page before the end of the upl is reached, if we
		 * find one, then it will be the last page of the request to
		 * 'cluster_io'
		 */
		for (start_pg = last_pg; last_pg < pages_in_upl; last_pg++) {
			if ((!ignore_valid_page_check && upl_valid_page(pl, last_pg)) || !upl_page_present(pl, last_pg)) {
				break;
			}
		}
		if (last_pg > start_pg) {
			int xoff;
			int xsize = (last_pg - start_pg) * PAGE_SIZE;
			xoff  = start_pg * PAGE_SIZE;

			if ((error = VNOP_PAGEIN(vp, upl, (upl_offset_t) xoff,
			    (off_t)f_offset + xoff,
			    xsize, flags, vfs_context_current()))) {
				/*
				 * Usually this UPL will be aborted/committed by the lower cluster layer.
				 *
				 * a)	In the case of decmpfs, however, we may return an error (EAGAIN) to avoid
				 *	a deadlock with another thread already inflating the file.
				 *
				 * b)	In the case of content protection, EPERM is a valid error and we should respect it.
				 *
				 * In those cases, we must take care of our UPL at this layer itself.
				 */
				if (must_commit) {
					if (error == EAGAIN) {
						ubc_upl_abort_range(upl, (upl_offset_t) xoff, xsize, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_RESTART);
					}
					if (error == EPERM) {
						ubc_upl_abort_range(upl, (upl_offset_t) xoff, xsize, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
					}
				}
				set_thread_pagein_error(current_thread(), error);
				ktriage_record(thread_tid(current_thread()),
				    KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_VNODEPAGEIN_FSPAGEIN_FAIL),
				    ktriage_encode_v_tag_and_error(vp, error));
				result = PAGER_ERROR;
				error  = PAGER_ERROR;
			}
		}
	}
out:
	vnode_put_from_pager(vp);

	if (errorp) {
		*errorp = result;
	}

	return error;
}