xref: /freebsd/sys/compat/linuxkpi/common/src/linux_shmemfs.c (revision c5405d1c850765d04f74067ebb71f57e9a26b8ea)
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/param.h>
30 #include <sys/systm.h>
31 #include <sys/rwlock.h>
32 
33 #include <vm/vm.h>
34 #include <vm/pmap.h>
35 #include <vm/vm_object.h>
36 #include <vm/vm_map.h>
37 #include <vm/vm_page.h>
38 #include <vm/vm_pager.h>
39 
40 #include <linux/fs.h>
41 #include <linux/mm.h>
42 #include <linux/shmem_fs.h>
43 
44 struct page *
45 linux_shmem_read_mapping_page_gfp(vm_object_t obj, int pindex, gfp_t gfp)
46 {
47 	struct page *page;
48 	int rv;
49 
50 	/*
51 	 * Historically, GFP_KERNEL was the equivalent of M_WAITOK. But it was
52 	 * changed to a synonym of M_NOWAIT to allow allocations in
53 	 * non-sleepable code.
54 	 *
55 	 * However, there was an assertion here to make sure that `gfp` was
56 	 * never set to GFP_NOWAIT/M_NOWAIT. Do we need a specific handling of
57 	 * M_NOWAIT here?
58 	 */
59 
60 	VM_OBJECT_WLOCK(obj);
61 	rv = vm_page_grab_valid(&page, obj, pindex, VM_ALLOC_NORMAL |
62 	    VM_ALLOC_NOBUSY | VM_ALLOC_WIRED);
63 	VM_OBJECT_WUNLOCK(obj);
64 	if (rv != VM_PAGER_OK)
65 		return (ERR_PTR(-EINVAL));
66 	return (page);
67 }
68 
69 struct linux_file *
70 linux_shmem_file_setup(const char *name, loff_t size, unsigned long flags)
71 {
72 	struct fileobj {
73 		struct linux_file file __aligned(sizeof(void *));
74 		struct vnode vnode __aligned(sizeof(void *));
75 	};
76 	struct fileobj *fileobj;
77 	struct linux_file *filp;
78 	struct vnode *vp;
79 	int error;
80 
81 	fileobj = kzalloc(sizeof(*fileobj), GFP_KERNEL);
82 	if (fileobj == NULL) {
83 		error = -ENOMEM;
84 		goto err_0;
85 	}
86 	filp = &fileobj->file;
87 	vp = &fileobj->vnode;
88 
89 	filp->f_count = 1;
90 	filp->f_vnode = vp;
91 	filp->f_shmem = vm_pager_allocate(OBJT_SWAP, NULL, size,
92 	    VM_PROT_READ | VM_PROT_WRITE, 0, curthread->td_ucred);
93 	if (filp->f_shmem == NULL) {
94 		error = -ENOMEM;
95 		goto err_1;
96 	}
97 	return (filp);
98 err_1:
99 	kfree(filp);
100 err_0:
101 	return (ERR_PTR(error));
102 }
103 
104 static vm_ooffset_t
105 linux_invalidate_mapping_pages_sub(vm_object_t obj, vm_pindex_t start,
106     vm_pindex_t end, int flags)
107 {
108 	int start_count, end_count;
109 
110 	VM_OBJECT_WLOCK(obj);
111 	start_count = obj->resident_page_count;
112 	vm_object_page_remove(obj, start, end, flags);
113 	end_count = obj->resident_page_count;
114 	VM_OBJECT_WUNLOCK(obj);
115 	return (start_count - end_count);
116 }
117 
118 unsigned long
119 linux_invalidate_mapping_pages(vm_object_t obj, pgoff_t start, pgoff_t end)
120 {
121 
122 	return (linux_invalidate_mapping_pages_sub(obj, start, end, OBJPR_CLEANONLY));
123 }
124 
125 void
126 linux_shmem_truncate_range(vm_object_t obj, loff_t lstart, loff_t lend)
127 {
128 	vm_pindex_t start = OFF_TO_IDX(lstart + PAGE_SIZE - 1);
129 	vm_pindex_t end = OFF_TO_IDX(lend + 1);
130 
131 	(void) linux_invalidate_mapping_pages_sub(obj, start, end, 0);
132 }
133