]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2006 Apple Computer, 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 <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> | |
75 | #include <sys/kdebug.h> | |
76 | #include <nfs/nfs_conf.h> | |
77 | #include <nfs/rpcv2.h> | |
78 | #include <nfs/nfsproto.h> | |
79 | #include <nfs/nfs.h> | |
80 | ||
81 | #include <vm/vm_protos.h> | |
82 | ||
83 | #include <vfs/vfs_disk_conditioner.h> | |
84 | ||
85 | void | |
86 | vnode_pager_throttle() | |
87 | { | |
88 | struct uthread *ut; | |
89 | ||
90 | ut = get_bsdthread_info(current_thread()); | |
91 | ||
92 | if (ut->uu_lowpri_window) { | |
93 | throttle_lowpri_io(1); | |
94 | } | |
95 | } | |
96 | ||
97 | boolean_t | |
98 | vnode_pager_isSSD(vnode_t vp) | |
99 | { | |
100 | return disk_conditioner_mount_is_ssd(vp->v_mount); | |
101 | } | |
102 | ||
103 | #if CONFIG_IOSCHED | |
104 | void | |
105 | vnode_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; | |
109 | dk_set_tier_t set_tier; | |
110 | int error = 0; | |
111 | ||
112 | error = VNOP_IOCTL(devvp, DKIOCGETBLOCKSIZE, (caddr_t)&blocksize, 0, vfs_context_kernel()); | |
113 | if (error) { | |
114 | return; | |
115 | } | |
116 | ||
117 | memset(&extent, 0, sizeof(dk_extent_t)); | |
118 | memset(&set_tier, 0, sizeof(dk_set_tier_t)); | |
119 | ||
120 | extent.offset = blkno * (u_int64_t) blocksize; | |
121 | extent.length = len; | |
122 | ||
123 | set_tier.extents = &extent; | |
124 | set_tier.extentsCount = 1; | |
125 | set_tier.tier = priority; | |
126 | ||
127 | error = VNOP_IOCTL(devvp, DKIOCSETTIER, (caddr_t)&set_tier, 0, vfs_context_kernel()); | |
128 | return; | |
129 | } | |
130 | #endif | |
131 | ||
132 | void | |
133 | vnode_pager_was_dirtied( | |
134 | struct vnode *vp, | |
135 | vm_object_offset_t s_offset, | |
136 | vm_object_offset_t e_offset) | |
137 | { | |
138 | cluster_update_state(vp, s_offset, e_offset, TRUE); | |
139 | } | |
140 | ||
141 | uint32_t | |
142 | vnode_pager_isinuse(struct vnode *vp) | |
143 | { | |
144 | if (vp->v_usecount > vp->v_kusecount) { | |
145 | return 1; | |
146 | } | |
147 | return 0; | |
148 | } | |
149 | ||
150 | uint32_t | |
151 | vnode_pager_return_throttle_io_limit(struct vnode *vp, uint32_t *limit) | |
152 | { | |
153 | return cluster_throttle_io_limit(vp, limit); | |
154 | } | |
155 | ||
156 | vm_object_offset_t | |
157 | vnode_pager_get_filesize(struct vnode *vp) | |
158 | { | |
159 | return (vm_object_offset_t) ubc_getsize(vp); | |
160 | } | |
161 | ||
162 | extern int safe_getpath(struct vnode *dvp, char *leafname, char *path, int _len, int *truncated_path); | |
163 | ||
164 | kern_return_t | |
165 | vnode_pager_get_name( | |
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) | |
172 | { | |
173 | *truncated_path_p = FALSE; | |
174 | if (pathname != NULL) { | |
175 | /* get the path name */ | |
176 | safe_getpath(vp, NULL, | |
177 | pathname, (int) pathname_len, | |
178 | truncated_path_p); | |
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); | |
188 | } | |
189 | return KERN_SUCCESS; | |
190 | } | |
191 | ||
192 | kern_return_t | |
193 | vnode_pager_get_mtime( | |
194 | struct vnode *vp, | |
195 | struct timespec *current_mtime, | |
196 | struct timespec *cs_mtime) | |
197 | { | |
198 | vnode_mtime(vp, current_mtime, vfs_context_current()); | |
199 | if (cs_mtime != NULL) { | |
200 | ubc_get_cs_mtime(vp, cs_mtime); | |
201 | } | |
202 | return KERN_SUCCESS; | |
203 | } | |
204 | ||
205 | kern_return_t | |
206 | vnode_pager_get_cs_blobs( | |
207 | struct vnode *vp, | |
208 | void **blobs) | |
209 | { | |
210 | *blobs = ubc_get_cs_blobs(vp); | |
211 | return KERN_SUCCESS; | |
212 | } | |
213 | ||
214 | /* | |
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 | */ | |
224 | u_int32_t | |
225 | vnode_trim( | |
226 | struct vnode *vp, | |
227 | off_t offset, | |
228 | size_t length) | |
229 | { | |
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 */ | |
232 | size_t trimmed = 0; | |
233 | off_t current_offset = offset; | |
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 | ||
251 | /* | |
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 | |
259 | * specified offset. It returns blocks in contiguous chunks, so if the logical range is | |
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 | */ | |
263 | error = VNOP_BLOCKMAP(vp, current_offset, remaining_length, | |
264 | &io_blockno, &io_bytecount, NULL, VNODE_READ | VNODE_BLOCKMAP_NO_TRACK, NULL); | |
265 | ||
266 | if (error) { | |
267 | goto trim_exit; | |
268 | } | |
269 | /* | |
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 | */ | |
273 | memset(&extent, 0, sizeof(dk_extent_t)); | |
274 | memset(&unmap, 0, sizeof(dk_unmap_t)); | |
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 | } | |
288 | trim_exit: | |
289 | ||
290 | return error; | |
291 | } | |
292 | ||
293 | pager_return_t | |
294 | vnode_pageout(struct vnode *vp, | |
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) | |
301 | { | |
302 | int result = PAGER_SUCCESS; | |
303 | int error = 0; | |
304 | int error_ret = 0; | |
305 | daddr64_t blkno; | |
306 | int isize; | |
307 | int pg_index; | |
308 | int base_index; | |
309 | upl_offset_t offset; | |
310 | upl_page_info_t *pl; | |
311 | vfs_context_t ctx = vfs_context_current(); /* pager context */ | |
312 | ||
313 | isize = (int)size; | |
314 | ||
315 | if (isize <= 0) { | |
316 | result = PAGER_ERROR; | |
317 | error_ret = EINVAL; | |
318 | goto out; | |
319 | } | |
320 | ||
321 | if (UBCINFOEXISTS(vp) == 0) { | |
322 | result = PAGER_ERROR; | |
323 | error_ret = EINVAL; | |
324 | ||
325 | if (upl && !(flags & UPL_NOCOMMIT)) { | |
326 | ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY); | |
327 | } | |
328 | goto out; | |
329 | } | |
330 | if (!(flags & UPL_VNODE_PAGER)) { | |
331 | /* | |
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 | |
335 | */ | |
336 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, | |
337 | (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START, | |
338 | size, 1, 0, 0, 0); | |
339 | ||
340 | if ((error_ret = VNOP_PAGEOUT(vp, upl, upl_offset, (off_t)f_offset, | |
341 | (size_t)size, flags, ctx))) { | |
342 | result = PAGER_ERROR; | |
343 | } | |
344 | ||
345 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, | |
346 | (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END, | |
347 | size, 1, 0, 0, 0); | |
348 | ||
349 | goto out; | |
350 | } | |
351 | if (upl == NULL) { | |
352 | int request_flags; | |
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 | */ | |
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); | |
365 | ||
366 | if ((error_ret = VNOP_PAGEOUT(vp, NULL, upl_offset, (off_t)f_offset, | |
367 | size, flags, ctx))) { | |
368 | result = PAGER_ERROR; | |
369 | } | |
370 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, | |
371 | (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END, | |
372 | size, 0, 0, 0, 0); | |
373 | ||
374 | goto out; | |
375 | } | |
376 | if (flags & UPL_MSYNC) { | |
377 | request_flags = UPL_UBC_MSYNC | UPL_RET_ONLY_DIRTY; | |
378 | } else { | |
379 | request_flags = UPL_UBC_PAGEOUT | UPL_RET_ONLY_DIRTY; | |
380 | } | |
381 | ||
382 | if (ubc_create_upl_kernel(vp, f_offset, size, &upl, &pl, request_flags, VM_KERN_MEMORY_FILE) != KERN_SUCCESS) { | |
383 | result = PAGER_ERROR; | |
384 | error_ret = EINVAL; | |
385 | goto out; | |
386 | } | |
387 | upl_offset = 0; | |
388 | } else { | |
389 | pl = ubc_upl_pageinfo(upl); | |
390 | } | |
391 | ||
392 | /* | |
393 | * Ignore any non-present pages at the end of the | |
394 | * UPL so that we aren't looking at a upl that | |
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;) { | |
401 | if (upl_page_present(pl, --pg_index)) { | |
402 | break; | |
403 | } | |
404 | if (pg_index == base_index) { | |
405 | /* | |
406 | * no pages were returned, so release | |
407 | * our hold on the upl and leave | |
408 | */ | |
409 | if (!(flags & UPL_NOCOMMIT)) { | |
410 | ubc_upl_abort_range(upl, upl_offset, isize, UPL_ABORT_FREE_ON_EMPTY); | |
411 | } | |
412 | ||
413 | goto out; | |
414 | } | |
415 | } | |
416 | isize = ((pg_index + 1) - base_index) * PAGE_SIZE; | |
417 | ||
418 | /* | |
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... | |
424 | */ | |
425 | ||
426 | if (ubc_getsize(vp) == 0) { | |
427 | /* | |
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) { | |
434 | #if CONFIG_NFS_CLIENT | |
435 | if (vp->v_tag == VT_NFS) { | |
436 | /* check with nfs if page is OK to drop */ | |
437 | error = nfs_buf_page_inval(vp, (off_t)f_offset); | |
438 | } else | |
439 | #endif /* CONFIG_NFS_CLIENT */ | |
440 | { | |
441 | blkno = ubc_offtoblk(vp, (off_t)f_offset); | |
442 | error = buf_invalblkno(vp, blkno, 0); | |
443 | } | |
444 | if (error) { | |
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 | } | |
451 | result = PAGER_ERROR; | |
452 | } else if (!(flags & UPL_NOCOMMIT)) { | |
453 | ubc_upl_commit_range(upl, offset, PAGE_SIZE, UPL_COMMIT_FREE_ON_EMPTY); | |
454 | } | |
455 | f_offset += PAGE_SIZE; | |
456 | } | |
457 | goto out; | |
458 | } | |
459 | ||
460 | offset = upl_offset; | |
461 | pg_index = base_index; | |
462 | ||
463 | while (isize) { | |
464 | int xsize; | |
465 | int num_of_pages; | |
466 | ||
467 | if (!upl_page_present(pl, pg_index)) { | |
468 | /* | |
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 | */ | |
473 | f_offset += PAGE_SIZE; | |
474 | offset += PAGE_SIZE; | |
475 | isize -= PAGE_SIZE; | |
476 | pg_index++; | |
477 | ||
478 | continue; | |
479 | } | |
480 | if (!upl_dirty_page(pl, pg_index)) { | |
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 | |
488 | * Note we must not sleep here if the buffer is busy - that is | |
489 | * a lock inversion which causes deadlock. | |
490 | */ | |
491 | #if CONFIG_NFS_CLIENT | |
492 | if (vp->v_tag == VT_NFS) { | |
493 | /* check with nfs if page is OK to drop */ | |
494 | error = nfs_buf_page_inval(vp, (off_t)f_offset); | |
495 | } else | |
496 | #endif /* CONFIG_NFS_CLIENT */ | |
497 | { | |
498 | blkno = ubc_offtoblk(vp, (off_t)f_offset); | |
499 | error = buf_invalblkno(vp, blkno, 0); | |
500 | } | |
501 | if (error) { | |
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 | } | |
508 | result = PAGER_ERROR; | |
509 | } else if (!(flags & UPL_NOCOMMIT)) { | |
510 | ubc_upl_commit_range(upl, offset, PAGE_SIZE, UPL_COMMIT_FREE_ON_EMPTY); | |
511 | } | |
512 | f_offset += PAGE_SIZE; | |
513 | offset += PAGE_SIZE; | |
514 | isize -= PAGE_SIZE; | |
515 | pg_index++; | |
516 | ||
517 | continue; | |
518 | } | |
519 | num_of_pages = 1; | |
520 | xsize = isize - PAGE_SIZE; | |
521 | ||
522 | while (xsize) { | |
523 | if (!upl_dirty_page(pl, pg_index + num_of_pages)) { | |
524 | break; | |
525 | } | |
526 | num_of_pages++; | |
527 | xsize -= PAGE_SIZE; | |
528 | } | |
529 | xsize = num_of_pages * PAGE_SIZE; | |
530 | ||
531 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, | |
532 | (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START, | |
533 | xsize, (int)f_offset, 0, 0, 0); | |
534 | ||
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 | } | |
540 | result = PAGER_ERROR; | |
541 | } | |
542 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, | |
543 | (MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END, | |
544 | xsize, 0, 0, 0, 0); | |
545 | ||
546 | f_offset += xsize; | |
547 | offset += xsize; | |
548 | isize -= xsize; | |
549 | pg_index += num_of_pages; | |
550 | } | |
551 | out: | |
552 | if (errorp) { | |
553 | *errorp = error_ret; | |
554 | } | |
555 | ||
556 | return result; | |
557 | } | |
558 | ||
559 | ||
560 | pager_return_t | |
561 | vnode_pagein( | |
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) | |
569 | { | |
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; | |
576 | int first_pg; | |
577 | int xsize; | |
578 | int must_commit = 1; | |
579 | int ignore_valid_page_check = 0; | |
580 | ||
581 | if (flags & UPL_NOCOMMIT) { | |
582 | must_commit = 0; | |
583 | } | |
584 | ||
585 | if (flags & UPL_IGNORE_VALID_PAGE_CHECK) { | |
586 | ignore_valid_page_check = 1; | |
587 | } | |
588 | ||
589 | if (UBCINFOEXISTS(vp) == 0) { | |
590 | result = PAGER_ERROR; | |
591 | error = PAGER_ERROR; | |
592 | ||
593 | if (upl && must_commit) { | |
594 | ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR); | |
595 | } | |
596 | ||
597 | goto out; | |
598 | } | |
599 | if (upl == (upl_t)NULL) { | |
600 | flags &= ~UPL_NOCOMMIT; | |
601 | ||
602 | if (size > MAX_UPL_SIZE_BYTES) { | |
603 | result = PAGER_ERROR; | |
604 | error = PAGER_ERROR; | |
605 | goto out; | |
606 | } | |
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 | |
616 | * to clip the extent to better fit the underlying FS blocksize if it desires as | |
617 | * long as it continues to include the "must have" page... 'f_offset' + 'upl_offset' | |
618 | * identifies that page | |
619 | */ | |
620 | if ((error = VNOP_PAGEIN(vp, NULL, upl_offset, (off_t)f_offset, | |
621 | size, flags, vfs_context_current()))) { | |
622 | set_thread_pagein_error(current_thread(), error); | |
623 | result = PAGER_ERROR; | |
624 | error = PAGER_ERROR; | |
625 | } | |
626 | goto out; | |
627 | } | |
628 | ubc_create_upl_kernel(vp, f_offset, size, &upl, &pl, UPL_UBC_PAGEIN | UPL_RET_ONLY_ABSENT, VM_KERN_MEMORY_FILE); | |
629 | ||
630 | if (upl == (upl_t)NULL) { | |
631 | result = PAGER_ABSENT; | |
632 | error = PAGER_ABSENT; | |
633 | goto out; | |
634 | } | |
635 | ubc_upl_range_needed(upl, upl_offset / PAGE_SIZE, 1); | |
636 | ||
637 | upl_offset = 0; | |
638 | first_pg = 0; | |
639 | ||
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 | */ | |
645 | must_commit = 1; | |
646 | } else { | |
647 | pl = ubc_upl_pageinfo(upl); | |
648 | first_pg = upl_offset / PAGE_SIZE; | |
649 | } | |
650 | pages_in_upl = size / PAGE_SIZE; | |
651 | DTRACE_VM2(pgpgin, int, pages_in_upl, (uint64_t *), NULL); | |
652 | ||
653 | /* | |
654 | * before we start marching forward, we must make sure we end on | |
655 | * a present page, otherwise we will be working with a freed | |
656 | * upl | |
657 | */ | |
658 | for (last_pg = pages_in_upl - 1; last_pg >= first_pg; last_pg--) { | |
659 | if (upl_page_present(pl, last_pg)) { | |
660 | break; | |
661 | } | |
662 | if (last_pg == first_pg) { | |
663 | /* | |
664 | * empty UPL, no pages are present | |
665 | */ | |
666 | if (must_commit) { | |
667 | ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY); | |
668 | } | |
669 | goto out; | |
670 | } | |
671 | } | |
672 | pages_in_upl = last_pg + 1; | |
673 | last_pg = first_pg; | |
674 | ||
675 | while (last_pg < pages_in_upl) { | |
676 | /* | |
677 | * skip over missing pages... | |
678 | */ | |
679 | for (; last_pg < pages_in_upl; last_pg++) { | |
680 | if (upl_page_present(pl, last_pg)) { | |
681 | break; | |
682 | } | |
683 | } | |
684 | ||
685 | if (ignore_valid_page_check == 1) { | |
686 | start_pg = last_pg; | |
687 | } else { | |
688 | /* | |
689 | * skip over 'valid' pages... we don't want to issue I/O for these | |
690 | */ | |
691 | for (start_pg = last_pg; last_pg < pages_in_upl; last_pg++) { | |
692 | if (!upl_valid_page(pl, last_pg)) { | |
693 | break; | |
694 | } | |
695 | } | |
696 | } | |
697 | ||
698 | if (last_pg > start_pg) { | |
699 | /* | |
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 | */ | |
705 | xsize = (last_pg - start_pg) * PAGE_SIZE; | |
706 | ||
707 | if (must_commit) { | |
708 | ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, xsize, UPL_ABORT_FREE_ON_EMPTY); | |
709 | } | |
710 | } | |
711 | if (last_pg == pages_in_upl) { | |
712 | /* | |
713 | * we're done... all pages that were present | |
714 | * have either had I/O issued on them or | |
715 | * were aborted unchanged... | |
716 | */ | |
717 | break; | |
718 | } | |
719 | ||
720 | if (!upl_page_present(pl, last_pg)) { | |
721 | /* | |
722 | * we found a range of valid pages | |
723 | * terminated by a missing page... | |
724 | * bump index to the next page and continue on | |
725 | */ | |
726 | last_pg++; | |
727 | continue; | |
728 | } | |
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++) { | |
736 | if ((!ignore_valid_page_check && upl_valid_page(pl, last_pg)) || !upl_page_present(pl, last_pg)) { | |
737 | break; | |
738 | } | |
739 | } | |
740 | if (last_pg > start_pg) { | |
741 | int xoff; | |
742 | xsize = (last_pg - start_pg) * PAGE_SIZE; | |
743 | xoff = start_pg * PAGE_SIZE; | |
744 | ||
745 | if ((error = VNOP_PAGEIN(vp, upl, (upl_offset_t) xoff, | |
746 | (off_t)f_offset + xoff, | |
747 | xsize, flags, vfs_context_current()))) { | |
748 | /* | |
749 | * Usually this UPL will be aborted/committed by the lower cluster layer. | |
750 | * | |
751 | * a) In the case of decmpfs, however, we may return an error (EAGAIN) to avoid | |
752 | * a deadlock with another thread already inflating the file. | |
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. | |
757 | */ | |
758 | if (must_commit) { | |
759 | if (error == EAGAIN) { | |
760 | ubc_upl_abort_range(upl, (upl_offset_t) xoff, xsize, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_RESTART); | |
761 | } | |
762 | if (error == EPERM) { | |
763 | ubc_upl_abort_range(upl, (upl_offset_t) xoff, xsize, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR); | |
764 | } | |
765 | } | |
766 | set_thread_pagein_error(current_thread(), error); | |
767 | result = PAGER_ERROR; | |
768 | error = PAGER_ERROR; | |
769 | } | |
770 | } | |
771 | } | |
772 | out: | |
773 | if (errorp) { | |
774 | *errorp = result; | |
775 | } | |
776 | ||
777 | return error; | |
778 | } | |
779 | ||
780 | void * | |
781 | upl_get_internal_page_list(upl_t upl) | |
782 | { | |
783 | return UPL_GET_INTERNAL_PAGE_LIST(upl); | |
784 | } |