1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2016 Matthew Macy (mmacy@mattmacy.io) 4 * Copyright (c) 2017 Mellanox Technologies, Ltd. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/rwlock.h> 33 34 #include <vm/vm.h> 35 #include <vm/pmap.h> 36 #include <vm/vm_object.h> 37 #include <vm/vm_map.h> 38 #include <vm/vm_page.h> 39 #include <vm/vm_pager.h> 40 41 #include <linux/fs.h> 42 #include <linux/mm.h> 43 #include <linux/shmem_fs.h> 44 45 struct page * 46 linux_shmem_read_mapping_page_gfp(vm_object_t obj, int pindex, gfp_t gfp) 47 { 48 struct page *page; 49 int rv; 50 51 /* 52 * Historically, GFP_KERNEL was the equivalent of M_WAITOK. But it was 53 * changed to a synonym of M_NOWAIT to allow allocations in 54 * non-sleepable code. 55 * 56 * However, there was an assertion here to make sure that `gfp` was 57 * never set to GFP_NOWAIT/M_NOWAIT. Do we need a specific handling of 58 * M_NOWAIT here? 59 */ 60 61 VM_OBJECT_WLOCK(obj); 62 rv = vm_page_grab_valid(&page, obj, pindex, VM_ALLOC_NORMAL | 63 VM_ALLOC_NOBUSY | VM_ALLOC_WIRED); 64 VM_OBJECT_WUNLOCK(obj); 65 if (rv != VM_PAGER_OK) 66 return (ERR_PTR(-EINVAL)); 67 return (page); 68 } 69 70 struct linux_file * 71 linux_shmem_file_setup(const char *name, loff_t size, unsigned long flags) 72 { 73 struct fileobj { 74 struct linux_file file __aligned(sizeof(void *)); 75 struct vnode vnode __aligned(sizeof(void *)); 76 }; 77 struct fileobj *fileobj; 78 struct linux_file *filp; 79 struct vnode *vp; 80 int error; 81 82 fileobj = kzalloc(sizeof(*fileobj), GFP_KERNEL); 83 if (fileobj == NULL) { 84 error = -ENOMEM; 85 goto err_0; 86 } 87 filp = &fileobj->file; 88 vp = &fileobj->vnode; 89 90 filp->f_count = 1; 91 filp->f_vnode = vp; 92 filp->f_shmem = vm_pager_allocate(OBJT_SWAP, NULL, size, 93 VM_PROT_READ | VM_PROT_WRITE, 0, curthread->td_ucred); 94 if (filp->f_shmem == NULL) { 95 error = -ENOMEM; 96 goto err_1; 97 } 98 return (filp); 99 err_1: 100 kfree(filp); 101 err_0: 102 return (ERR_PTR(error)); 103 } 104 105 static vm_ooffset_t 106 linux_invalidate_mapping_pages_sub(vm_object_t obj, vm_pindex_t start, 107 vm_pindex_t end, int flags) 108 { 109 int start_count, end_count; 110 111 VM_OBJECT_WLOCK(obj); 112 start_count = obj->resident_page_count; 113 vm_object_page_remove(obj, start, end, flags); 114 end_count = obj->resident_page_count; 115 VM_OBJECT_WUNLOCK(obj); 116 return (start_count - end_count); 117 } 118 119 unsigned long 120 linux_invalidate_mapping_pages(vm_object_t obj, pgoff_t start, pgoff_t end) 121 { 122 123 return (linux_invalidate_mapping_pages_sub(obj, start, end, OBJPR_CLEANONLY)); 124 } 125 126 void 127 linux_shmem_truncate_range(vm_object_t obj, loff_t lstart, loff_t lend) 128 { 129 vm_pindex_t start = OFF_TO_IDX(lstart + PAGE_SIZE - 1); 130 vm_pindex_t end = OFF_TO_IDX(lend + 1); 131 132 (void) linux_invalidate_mapping_pages_sub(obj, start, end, 0); 133 } 134