1 /*- 2 * Copyright (c) 2011 The FreeBSD Foundation 3 * Copyright (c) 2014-2020 François Tigeot <ftigeot@wolfpond.org> 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Konstantin Belousov 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 #include <vm/vm.h> 33 #include <vm/vm_page.h> 34 #include <vm/vm_page2.h> 35 #include <vm/vm_pager.h> 36 37 #include <linux/err.h> 38 #include <linux/shmem_fs.h> 39 40 /* 41 * This code is typically called with a normal VM object to access 42 * data from a userspace shared memory mapping. However, handle the 43 * case where it might be called with OBJT_MGTDEVICE anyway. 44 */ 45 struct page * 46 shmem_read_mapping_page(vm_object_t object, vm_pindex_t pindex) 47 { 48 vm_page_t m; 49 int rv; 50 51 VM_OBJECT_LOCK(object); 52 if (object->type == OBJT_MGTDEVICE) { 53 m = NULL; 54 rv = vm_pager_get_page(object, pindex, &m, 1); 55 if (m == NULL) 56 return ERR_PTR(-ENOMEM); 57 if (rv != VM_PAGER_OK) { 58 vm_page_free(m); 59 return ERR_PTR(-ENOMEM); 60 } 61 } else { 62 m = vm_page_grab(object, pindex, 63 VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 64 if (m->valid != VM_PAGE_BITS_ALL) { 65 if (vm_pager_has_page(object, pindex)) { 66 rv = vm_pager_get_page(object, pindex, &m, 1); 67 m = vm_page_lookup(object, pindex); 68 if (m == NULL) 69 return ERR_PTR(-ENOMEM); 70 if (rv != VM_PAGER_OK) { 71 vm_page_free(m); 72 return ERR_PTR(-ENOMEM); 73 } 74 } else { 75 pmap_zero_page(VM_PAGE_TO_PHYS(m)); 76 m->valid = VM_PAGE_BITS_ALL; 77 m->dirty = 0; 78 } 79 } 80 } 81 vm_page_wire(m); /* put_page() undoes this */ 82 vm_page_wakeup(m); 83 VM_OBJECT_UNLOCK(object); 84 85 return (struct page *)m; 86 } 87 88 struct page * 89 shmem_read_mapping_page_gfp(struct vm_object *mapping, 90 pgoff_t index, gfp_t gfp_mask) 91 { 92 return shmem_read_mapping_page(mapping, index); 93 } 94 95 #include <linux/fs.h> 96 97 int 98 pagecache_write_begin(struct vm_object *obj, struct address_space *mapping, 99 loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata) 100 { 101 *pagep = shmem_read_mapping_page(obj, OFF_TO_IDX(pos)); 102 103 return 0; 104 } 105 106 /* This is really shmem_write_end() for the i915 gem code */ 107 int 108 pagecache_write_end(struct vm_object *obj, struct address_space *mapping, 109 loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata) 110 { 111 set_page_dirty(page); 112 put_page(page); 113 114 return copied; 115 } 116 117