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