]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2000-2020 Apple 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 | 67 | #include <kern/zalloc.h> |
1c79356b A |
68 | #include <libkern/libkern.h> |
69 | ||
70 | #include <vm/vnode_pager.h> | |
71 | #include <vm/vm_pageout.h> | |
72 | ||
73 | #include <kern/assert.h> | |
9bccf70c | 74 | #include <sys/kdebug.h> |
ea3f0419 | 75 | #include <nfs/nfs_conf.h> |
91447636 A |
76 | #include <nfs/rpcv2.h> |
77 | #include <nfs/nfsproto.h> | |
78 | #include <nfs/nfs.h> | |
79 | ||
80 | #include <vm/vm_protos.h> | |
1c79356b | 81 | |
5ba3f43e | 82 | #include <vfs/vfs_disk_conditioner.h> |
b0d623f7 | 83 | |
6d2010ae | 84 | void |
f427ee49 | 85 | vnode_pager_throttle(void) |
6d2010ae A |
86 | { |
87 | struct uthread *ut; | |
88 | ||
89 | ut = get_bsdthread_info(current_thread()); | |
90 | ||
0a7de745 | 91 | if (ut->uu_lowpri_window) { |
39236c6e | 92 | throttle_lowpri_io(1); |
0a7de745 | 93 | } |
6d2010ae A |
94 | } |
95 | ||
6d2010ae A |
96 | boolean_t |
97 | vnode_pager_isSSD(vnode_t vp) | |
98 | { | |
5ba3f43e | 99 | return disk_conditioner_mount_is_ssd(vp->v_mount); |
6d2010ae A |
100 | } |
101 | ||
fe8ab488 A |
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; | |
0a7de745 | 108 | dk_set_tier_t set_tier; |
fe8ab488 A |
109 | int error = 0; |
110 | ||
111 | error = VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&blocksize, 0, vfs_context_kernel()); | |
0a7de745 | 112 | if (error) { |
fe8ab488 | 113 | return; |
0a7de745 | 114 | } |
fe8ab488 A |
115 | |
116 | memset(&extent, 0, sizeof(dk_extent_t)); | |
117 | memset(&set_tier, 0, sizeof(dk_set_tier_t)); | |
0a7de745 | 118 | |
fe8ab488 A |
119 | extent.offset = blkno * (u_int64_t) blocksize; |
120 | extent.length = len; | |
121 | ||
0a7de745 | 122 | set_tier.extents = &extent; |
fe8ab488 | 123 | set_tier.extentsCount = 1; |
f427ee49 | 124 | set_tier.tier = (uint8_t)priority; |
0a7de745 | 125 | |
fe8ab488 A |
126 | error = VNOP_IOCTL(devvp, DKIOCSETTIER, (caddr_t)&set_tier, 0, vfs_context_kernel()); |
127 | return; | |
128 | } | |
129 | #endif | |
6d2010ae | 130 | |
d9a64523 A |
131 | void |
132 | vnode_pager_was_dirtied( | |
0a7de745 A |
133 | struct vnode *vp, |
134 | vm_object_offset_t s_offset, | |
135 | vm_object_offset_t e_offset) | |
d9a64523 | 136 | { |
0a7de745 | 137 | cluster_update_state(vp, s_offset, e_offset, TRUE); |
d9a64523 A |
138 | } |
139 | ||
b0d623f7 A |
140 | uint32_t |
141 | vnode_pager_isinuse(struct vnode *vp) | |
142 | { | |
0a7de745 A |
143 | if (vp->v_usecount > vp->v_kusecount) { |
144 | return 1; | |
145 | } | |
146 | return 0; | |
b0d623f7 A |
147 | } |
148 | ||
149 | uint32_t | |
39236c6e | 150 | vnode_pager_return_throttle_io_limit(struct vnode *vp, uint32_t *limit) |
b0d623f7 | 151 | { |
0a7de745 | 152 | return cluster_throttle_io_limit(vp, limit); |
b0d623f7 | 153 | } |
1c79356b | 154 | |
0b4e3aa0 A |
155 | vm_object_offset_t |
156 | vnode_pager_get_filesize(struct vnode *vp) | |
157 | { | |
0b4e3aa0 | 158 | return (vm_object_offset_t) ubc_getsize(vp); |
0b4e3aa0 A |
159 | } |
160 | ||
15129b1c A |
161 | extern int safe_getpath(struct vnode *dvp, char *leafname, char *path, int _len, int *truncated_path); |
162 | ||
0c530ab8 | 163 | kern_return_t |
15129b1c | 164 | vnode_pager_get_name( |
0a7de745 A |
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) | |
0c530ab8 | 171 | { |
15129b1c A |
172 | *truncated_path_p = FALSE; |
173 | if (pathname != NULL) { | |
174 | /* get the path name */ | |
175 | safe_getpath(vp, NULL, | |
0a7de745 A |
176 | pathname, (int) pathname_len, |
177 | truncated_path_p); | |
15129b1c A |
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); | |
0c530ab8 | 187 | } |
0c530ab8 A |
188 | return KERN_SUCCESS; |
189 | } | |
190 | ||
191 | kern_return_t | |
15129b1c | 192 | vnode_pager_get_mtime( |
0a7de745 A |
193 | struct vnode *vp, |
194 | struct timespec *current_mtime, | |
195 | struct timespec *cs_mtime) | |
0c530ab8 | 196 | { |
15129b1c A |
197 | vnode_mtime(vp, current_mtime, vfs_context_current()); |
198 | if (cs_mtime != NULL) { | |
199 | ubc_get_cs_mtime(vp, cs_mtime); | |
200 | } | |
0c530ab8 A |
201 | return KERN_SUCCESS; |
202 | } | |
203 | ||
2d21ac55 A |
204 | kern_return_t |
205 | vnode_pager_get_cs_blobs( | |
0a7de745 A |
206 | struct vnode *vp, |
207 | void **blobs) | |
2d21ac55 A |
208 | { |
209 | *blobs = ubc_get_cs_blobs(vp); | |
210 | return KERN_SUCCESS; | |
211 | } | |
212 | ||
0a7de745 | 213 | /* |
6d2010ae A |
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 | */ | |
0a7de745 A |
223 | u_int32_t |
224 | vnode_trim( | |
225 | struct vnode *vp, | |
226 | off_t offset, | |
227 | size_t length) | |
6d2010ae | 228 | { |
0a7de745 A |
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 */ | |
6d2010ae | 231 | size_t trimmed = 0; |
0a7de745 | 232 | off_t current_offset = offset; |
6d2010ae A |
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 | ||
0a7de745 | 250 | /* |
6d2010ae A |
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 | |
0a7de745 | 258 | * specified offset. It returns blocks in contiguous chunks, so if the logical range is |
6d2010ae A |
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 | */ | |
0a7de745 A |
262 | error = VNOP_BLOCKMAP(vp, current_offset, remaining_length, |
263 | &io_blockno, &io_bytecount, NULL, VNODE_READ | VNODE_BLOCKMAP_NO_TRACK, NULL); | |
6d2010ae A |
264 | |
265 | if (error) { | |
266 | goto trim_exit; | |
267 | } | |
0a7de745 | 268 | /* |
6d2010ae A |
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 | */ | |
0a7de745 A |
272 | memset(&extent, 0, sizeof(dk_extent_t)); |
273 | memset(&unmap, 0, sizeof(dk_unmap_t)); | |
6d2010ae A |
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; | |
6d2010ae A |
290 | } |
291 | ||
1c79356b A |
292 | pager_return_t |
293 | vnode_pageout(struct vnode *vp, | |
0a7de745 A |
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) | |
1c79356b | 300 | { |
0a7de745 A |
301 | int result = PAGER_SUCCESS; |
302 | int error = 0; | |
303 | int error_ret = 0; | |
91447636 A |
304 | daddr64_t blkno; |
305 | int isize; | |
1c79356b | 306 | int pg_index; |
91447636 | 307 | int base_index; |
b0d623f7 | 308 | upl_offset_t offset; |
1c79356b | 309 | upl_page_info_t *pl; |
0a7de745 | 310 | vfs_context_t ctx = vfs_context_current(); /* pager context */ |
1c79356b | 311 | |
1c79356b A |
312 | isize = (int)size; |
313 | ||
f427ee49 A |
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 | ||
9bccf70c | 334 | if (isize <= 0) { |
0a7de745 | 335 | result = PAGER_ERROR; |
91447636 | 336 | error_ret = EINVAL; |
9bccf70c A |
337 | goto out; |
338 | } | |
1c79356b | 339 | |
2d21ac55 | 340 | if (UBCINFOEXISTS(vp) == 0) { |
91447636 A |
341 | result = PAGER_ERROR; |
342 | error_ret = EINVAL; | |
9bccf70c | 343 | |
0a7de745 A |
344 | if (upl && !(flags & UPL_NOCOMMIT)) { |
345 | ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY); | |
346 | } | |
1c79356b A |
347 | goto out; |
348 | } | |
0a7de745 | 349 | if (!(flags & UPL_VNODE_PAGER)) { |
1c79356b | 350 | /* |
91447636 A |
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 | |
1c79356b | 354 | */ |
316670eb | 355 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
0a7de745 A |
356 | (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START, |
357 | size, 1, 0, 0, 0); | |
9bccf70c | 358 | |
0a7de745 A |
359 | if ((error_ret = VNOP_PAGEOUT(vp, upl, upl_offset, (off_t)f_offset, |
360 | (size_t)size, flags, ctx))) { | |
91447636 | 361 | result = PAGER_ERROR; |
0a7de745 | 362 | } |
9bccf70c | 363 | |
316670eb | 364 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
0a7de745 A |
365 | (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END, |
366 | size, 1, 0, 0, 0); | |
9bccf70c | 367 | |
1c79356b A |
368 | goto out; |
369 | } | |
b0d623f7 | 370 | if (upl == NULL) { |
0a7de745 | 371 | int request_flags; |
b0d623f7 A |
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 | */ | |
0a7de745 A |
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); | |
b0d623f7 | 384 | |
0a7de745 A |
385 | if ((error_ret = VNOP_PAGEOUT(vp, NULL, upl_offset, (off_t)f_offset, |
386 | size, flags, ctx))) { | |
b0d623f7 A |
387 | result = PAGER_ERROR; |
388 | } | |
316670eb | 389 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
0a7de745 A |
390 | (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END, |
391 | size, 0, 0, 0, 0); | |
b0d623f7 A |
392 | |
393 | goto out; | |
394 | } | |
0a7de745 | 395 | if (flags & UPL_MSYNC) { |
b0d623f7 | 396 | request_flags = UPL_UBC_MSYNC | UPL_RET_ONLY_DIRTY; |
0a7de745 | 397 | } else { |
b0d623f7 | 398 | request_flags = UPL_UBC_PAGEOUT | UPL_RET_ONLY_DIRTY; |
0a7de745 A |
399 | } |
400 | ||
401 | if (ubc_create_upl_kernel(vp, f_offset, size, &upl, &pl, request_flags, VM_KERN_MEMORY_FILE) != KERN_SUCCESS) { | |
b0d623f7 A |
402 | result = PAGER_ERROR; |
403 | error_ret = EINVAL; | |
404 | goto out; | |
405 | } | |
406 | upl_offset = 0; | |
0a7de745 | 407 | } else { |
b0d623f7 | 408 | pl = ubc_upl_pageinfo(upl); |
0a7de745 | 409 | } |
b0d623f7 | 410 | |
fe8ab488 A |
411 | /* |
412 | * Ignore any non-present pages at the end of the | |
0a7de745 | 413 | * UPL so that we aren't looking at a upl that |
fe8ab488 A |
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;) { | |
0a7de745 A |
420 | if (upl_page_present(pl, --pg_index)) { |
421 | break; | |
422 | } | |
fe8ab488 | 423 | if (pg_index == base_index) { |
0a7de745 | 424 | /* |
fe8ab488 A |
425 | * no pages were returned, so release |
426 | * our hold on the upl and leave | |
427 | */ | |
0a7de745 A |
428 | if (!(flags & UPL_NOCOMMIT)) { |
429 | ubc_upl_abort_range(upl, upl_offset, isize, UPL_ABORT_FREE_ON_EMPTY); | |
430 | } | |
fe8ab488 A |
431 | |
432 | goto out; | |
433 | } | |
434 | } | |
435 | isize = ((pg_index + 1) - base_index) * PAGE_SIZE; | |
436 | ||
9bccf70c | 437 | /* |
91447636 A |
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... | |
9bccf70c | 443 | */ |
fe8ab488 | 444 | |
fa4905b1 | 445 | if (ubc_getsize(vp) == 0) { |
0a7de745 | 446 | /* |
91447636 A |
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) { | |
ea3f0419 | 453 | #if CONFIG_NFS_CLIENT |
0a7de745 | 454 | if (vp->v_tag == VT_NFS) { |
91447636 A |
455 | /* check with nfs if page is OK to drop */ |
456 | error = nfs_buf_page_inval(vp, (off_t)f_offset); | |
0a7de745 | 457 | } else |
ea3f0419 | 458 | #endif /* CONFIG_NFS_CLIENT */ |
91447636 | 459 | { |
0a7de745 A |
460 | blkno = ubc_offtoblk(vp, (off_t)f_offset); |
461 | error = buf_invalblkno(vp, blkno, 0); | |
91447636 A |
462 | } |
463 | if (error) { | |
0a7de745 A |
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 | } | |
91447636 | 470 | result = PAGER_ERROR; |
0a7de745 A |
471 | } else if (!(flags & UPL_NOCOMMIT)) { |
472 | ubc_upl_commit_range(upl, offset, PAGE_SIZE, UPL_COMMIT_FREE_ON_EMPTY); | |
fa4905b1 | 473 | } |
91447636 | 474 | f_offset += PAGE_SIZE; |
1c79356b | 475 | } |
1c79356b A |
476 | goto out; |
477 | } | |
91447636 A |
478 | |
479 | offset = upl_offset; | |
480 | pg_index = base_index; | |
1c79356b A |
481 | |
482 | while (isize) { | |
483 | int xsize; | |
484 | int num_of_pages; | |
485 | ||
0a7de745 A |
486 | if (!upl_page_present(pl, pg_index)) { |
487 | /* | |
91447636 A |
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 | */ | |
0a7de745 | 492 | f_offset += PAGE_SIZE; |
2d21ac55 A |
493 | offset += PAGE_SIZE; |
494 | isize -= PAGE_SIZE; | |
1c79356b A |
495 | pg_index++; |
496 | ||
497 | continue; | |
498 | } | |
0a7de745 | 499 | if (!upl_dirty_page(pl, pg_index)) { |
1c79356b A |
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 | |
91447636 | 507 | * Note we must not sleep here if the buffer is busy - that is |
fa4905b1 | 508 | * a lock inversion which causes deadlock. |
1c79356b | 509 | */ |
ea3f0419 | 510 | #if CONFIG_NFS_CLIENT |
0a7de745 | 511 | if (vp->v_tag == VT_NFS) { |
55e303ae | 512 | /* check with nfs if page is OK to drop */ |
2d21ac55 | 513 | error = nfs_buf_page_inval(vp, (off_t)f_offset); |
0a7de745 | 514 | } else |
ea3f0419 | 515 | #endif /* CONFIG_NFS_CLIENT */ |
91447636 | 516 | { |
0a7de745 A |
517 | blkno = ubc_offtoblk(vp, (off_t)f_offset); |
518 | error = buf_invalblkno(vp, blkno, 0); | |
91447636 A |
519 | } |
520 | if (error) { | |
0a7de745 A |
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 | } | |
91447636 | 527 | result = PAGER_ERROR; |
0a7de745 A |
528 | } else if (!(flags & UPL_NOCOMMIT)) { |
529 | ubc_upl_commit_range(upl, offset, PAGE_SIZE, UPL_COMMIT_FREE_ON_EMPTY); | |
91447636 | 530 | } |
0a7de745 | 531 | f_offset += PAGE_SIZE; |
2d21ac55 A |
532 | offset += PAGE_SIZE; |
533 | isize -= PAGE_SIZE; | |
1c79356b A |
534 | pg_index++; |
535 | ||
536 | continue; | |
537 | } | |
1c79356b A |
538 | num_of_pages = 1; |
539 | xsize = isize - PAGE_SIZE; | |
540 | ||
541 | while (xsize) { | |
0a7de745 | 542 | if (!upl_dirty_page(pl, pg_index + num_of_pages)) { |
1c79356b | 543 | break; |
0a7de745 | 544 | } |
1c79356b A |
545 | num_of_pages++; |
546 | xsize -= PAGE_SIZE; | |
547 | } | |
548 | xsize = num_of_pages * PAGE_SIZE; | |
549 | ||
316670eb | 550 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
0a7de745 A |
551 | (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START, |
552 | xsize, (int)f_offset, 0, 0, 0); | |
9bccf70c | 553 | |
0a7de745 A |
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 | } | |
91447636 A |
559 | result = PAGER_ERROR; |
560 | } | |
316670eb | 561 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, |
0a7de745 A |
562 | (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END, |
563 | xsize, 0, 0, 0, 0); | |
9bccf70c | 564 | |
0a7de745 | 565 | f_offset += xsize; |
2d21ac55 A |
566 | offset += xsize; |
567 | isize -= xsize; | |
1c79356b A |
568 | pg_index += num_of_pages; |
569 | } | |
570 | out: | |
f427ee49 A |
571 | vnode_put_from_pager(vp); |
572 | ||
0a7de745 | 573 | if (errorp) { |
91447636 | 574 | *errorp = error_ret; |
0a7de745 | 575 | } |
1c79356b | 576 | |
0a7de745 | 577 | return result; |
1c79356b A |
578 | } |
579 | ||
580 | ||
581 | pager_return_t | |
582 | vnode_pagein( | |
0a7de745 A |
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) | |
1c79356b | 590 | { |
0a7de745 A |
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; | |
9bccf70c | 597 | int first_pg; |
0a7de745 A |
598 | int xsize; |
599 | int must_commit = 1; | |
600 | int ignore_valid_page_check = 0; | |
1c79356b | 601 | |
0a7de745 A |
602 | if (flags & UPL_NOCOMMIT) { |
603 | must_commit = 0; | |
604 | } | |
1c79356b | 605 | |
0a7de745 | 606 | if (flags & UPL_IGNORE_VALID_PAGE_CHECK) { |
39236c6e | 607 | ignore_valid_page_check = 1; |
0a7de745 | 608 | } |
39236c6e | 609 | |
f427ee49 A |
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 | ||
2d21ac55 | 629 | if (UBCINFOEXISTS(vp) == 0) { |
1c79356b A |
630 | result = PAGER_ERROR; |
631 | error = PAGER_ERROR; | |
2d21ac55 | 632 | |
0a7de745 | 633 | if (upl && must_commit) { |
9bccf70c | 634 | ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR); |
0a7de745 | 635 | } |
2d21ac55 | 636 | |
1c79356b A |
637 | goto out; |
638 | } | |
9bccf70c | 639 | if (upl == (upl_t)NULL) { |
b0d623f7 | 640 | flags &= ~UPL_NOCOMMIT; |
2d21ac55 | 641 | |
0a7de745 A |
642 | if (size > MAX_UPL_SIZE_BYTES) { |
643 | result = PAGER_ERROR; | |
9bccf70c A |
644 | error = PAGER_ERROR; |
645 | goto out; | |
646 | } | |
b0d623f7 A |
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 | |
0a7de745 | 656 | * to clip the extent to better fit the underlying FS blocksize if it desires as |
b0d623f7 A |
657 | * long as it continues to include the "must have" page... 'f_offset' + 'upl_offset' |
658 | * identifies that page | |
659 | */ | |
0a7de745 A |
660 | if ((error = VNOP_PAGEIN(vp, NULL, upl_offset, (off_t)f_offset, |
661 | size, flags, vfs_context_current()))) { | |
cb323159 | 662 | set_thread_pagein_error(current_thread(), error); |
b0d623f7 A |
663 | result = PAGER_ERROR; |
664 | error = PAGER_ERROR; | |
665 | } | |
666 | goto out; | |
667 | } | |
0a7de745 | 668 | ubc_create_upl_kernel(vp, f_offset, size, &upl, &pl, UPL_UBC_PAGEIN | UPL_RET_ONLY_ABSENT, VM_KERN_MEMORY_FILE); |
1c79356b | 669 | |
9bccf70c | 670 | if (upl == (upl_t)NULL) { |
0a7de745 | 671 | result = PAGER_ABSENT; |
9bccf70c A |
672 | error = PAGER_ABSENT; |
673 | goto out; | |
1c79356b | 674 | } |
316670eb A |
675 | ubc_upl_range_needed(upl, upl_offset / PAGE_SIZE, 1); |
676 | ||
9bccf70c | 677 | upl_offset = 0; |
2d21ac55 | 678 | first_pg = 0; |
0a7de745 | 679 | |
9bccf70c A |
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 | */ | |
2d21ac55 | 685 | must_commit = 1; |
1c79356b | 686 | } else { |
0a7de745 | 687 | pl = ubc_upl_pageinfo(upl); |
2d21ac55 | 688 | first_pg = upl_offset / PAGE_SIZE; |
9bccf70c A |
689 | } |
690 | pages_in_upl = size / PAGE_SIZE; | |
2d21ac55 | 691 | DTRACE_VM2(pgpgin, int, pages_in_upl, (uint64_t *), NULL); |
9bccf70c A |
692 | |
693 | /* | |
0a7de745 | 694 | * before we start marching forward, we must make sure we end on |
9bccf70c | 695 | * a present page, otherwise we will be working with a freed |
0a7de745 | 696 | * upl |
9bccf70c A |
697 | */ |
698 | for (last_pg = pages_in_upl - 1; last_pg >= first_pg; last_pg--) { | |
0a7de745 | 699 | if (upl_page_present(pl, last_pg)) { |
9bccf70c | 700 | break; |
0a7de745 | 701 | } |
2d21ac55 | 702 | if (last_pg == first_pg) { |
0a7de745 | 703 | /* |
2d21ac55 A |
704 | * empty UPL, no pages are present |
705 | */ | |
0a7de745 A |
706 | if (must_commit) { |
707 | ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY); | |
708 | } | |
2d21ac55 A |
709 | goto out; |
710 | } | |
9bccf70c A |
711 | } |
712 | pages_in_upl = last_pg + 1; | |
2d21ac55 | 713 | last_pg = first_pg; |
9bccf70c | 714 | |
2d21ac55 | 715 | while (last_pg < pages_in_upl) { |
0a7de745 | 716 | /* |
2d21ac55 | 717 | * skip over missing pages... |
9bccf70c | 718 | */ |
0a7de745 A |
719 | for (; last_pg < pages_in_upl; last_pg++) { |
720 | if (upl_page_present(pl, last_pg)) { | |
721 | break; | |
722 | } | |
9bccf70c | 723 | } |
39236c6e A |
724 | |
725 | if (ignore_valid_page_check == 1) { | |
726 | start_pg = last_pg; | |
727 | } else { | |
0a7de745 | 728 | /* |
39236c6e A |
729 | * skip over 'valid' pages... we don't want to issue I/O for these |
730 | */ | |
0a7de745 A |
731 | for (start_pg = last_pg; last_pg < pages_in_upl; last_pg++) { |
732 | if (!upl_valid_page(pl, last_pg)) { | |
733 | break; | |
734 | } | |
39236c6e | 735 | } |
9bccf70c | 736 | } |
39236c6e | 737 | |
9bccf70c | 738 | if (last_pg > start_pg) { |
0a7de745 | 739 | /* |
9bccf70c A |
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 | */ | |
0a7de745 | 745 | xsize = (last_pg - start_pg) * PAGE_SIZE; |
1c79356b | 746 | |
0a7de745 A |
747 | if (must_commit) { |
748 | ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, xsize, UPL_ABORT_FREE_ON_EMPTY); | |
749 | } | |
9bccf70c | 750 | } |
0a7de745 A |
751 | if (last_pg == pages_in_upl) { |
752 | /* | |
2d21ac55 | 753 | * we're done... all pages that were present |
0a7de745 | 754 | * have either had I/O issued on them or |
2d21ac55 A |
755 | * were aborted unchanged... |
756 | */ | |
0a7de745 A |
757 | break; |
758 | } | |
9bccf70c | 759 | |
2d21ac55 | 760 | if (!upl_page_present(pl, last_pg)) { |
0a7de745 A |
761 | /* |
762 | * we found a range of valid pages | |
2d21ac55 A |
763 | * terminated by a missing page... |
764 | * bump index to the next page and continue on | |
9bccf70c | 765 | */ |
0a7de745 A |
766 | last_pg++; |
767 | continue; | |
2d21ac55 | 768 | } |
9bccf70c A |
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++) { | |
0a7de745 A |
776 | if ((!ignore_valid_page_check && upl_valid_page(pl, last_pg)) || !upl_page_present(pl, last_pg)) { |
777 | break; | |
778 | } | |
9bccf70c A |
779 | } |
780 | if (last_pg > start_pg) { | |
0a7de745 A |
781 | int xoff; |
782 | xsize = (last_pg - start_pg) * PAGE_SIZE; | |
9bccf70c A |
783 | xoff = start_pg * PAGE_SIZE; |
784 | ||
0a7de745 A |
785 | if ((error = VNOP_PAGEIN(vp, upl, (upl_offset_t) xoff, |
786 | (off_t)f_offset + xoff, | |
787 | xsize, flags, vfs_context_current()))) { | |
788 | /* | |
b0d623f7 | 789 | * Usually this UPL will be aborted/committed by the lower cluster layer. |
6d2010ae A |
790 | * |
791 | * a) In the case of decmpfs, however, we may return an error (EAGAIN) to avoid | |
0a7de745 | 792 | * a deadlock with another thread already inflating the file. |
6d2010ae A |
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. | |
b0d623f7 A |
797 | */ |
798 | if (must_commit) { | |
0a7de745 A |
799 | if (error == EAGAIN) { |
800 | ubc_upl_abort_range(upl, (upl_offset_t) xoff, xsize, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_RESTART); | |
b0d623f7 | 801 | } |
0a7de745 A |
802 | if (error == EPERM) { |
803 | ubc_upl_abort_range(upl, (upl_offset_t) xoff, xsize, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR); | |
6d2010ae | 804 | } |
b0d623f7 | 805 | } |
cb323159 | 806 | set_thread_pagein_error(current_thread(), error); |
0b4e3aa0 A |
807 | result = PAGER_ERROR; |
808 | error = PAGER_ERROR; | |
809 | } | |
1c79356b | 810 | } |
0a7de745 | 811 | } |
1c79356b | 812 | out: |
f427ee49 A |
813 | vnode_put_from_pager(vp); |
814 | ||
0a7de745 | 815 | if (errorp) { |
fa4905b1 | 816 | *errorp = result; |
0a7de745 | 817 | } |
1c79356b | 818 | |
0a7de745 | 819 | return error; |
1c79356b A |
820 | } |
821 | ||
0b4e3aa0 | 822 | void * |
1c79356b A |
823 | upl_get_internal_page_list(upl_t upl) |
824 | { | |
0a7de745 | 825 | return UPL_GET_INTERNAL_PAGE_LIST(upl); |
1c79356b | 826 | } |