Path: blob/main/sys/compat/linuxkpi/common/src/linux_shmemfs.c
103169 views
/*-1* Copyright (c) 2010 Isilon Systems, Inc.2* Copyright (c) 2016 Matthew Macy ([email protected])3* Copyright (c) 2017 Mellanox Technologies, Ltd.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice unmodified, this list of conditions, and the following11* disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/rwlock.h>3132#include <vm/vm.h>33#include <vm/pmap.h>34#include <vm/vm_object.h>35#include <vm/vm_map.h>36#include <vm/vm_page.h>37#include <vm/vm_pager.h>3839#include <linux/fs.h>40#include <linux/mm.h>41#include <linux/shmem_fs.h>4243struct page *44linux_shmem_read_mapping_page_gfp(vm_object_t obj, int pindex, gfp_t gfp)45{46struct page *page;47int rv;4849if ((gfp & GFP_NOWAIT) != 0)50panic("GFP_NOWAIT is unimplemented");5152VM_OBJECT_WLOCK(obj);53rv = vm_page_grab_valid(&page, obj, pindex, VM_ALLOC_NORMAL |54VM_ALLOC_NOBUSY | VM_ALLOC_WIRED);55VM_OBJECT_WUNLOCK(obj);56if (rv != VM_PAGER_OK)57return (ERR_PTR(-EINVAL));58return (page);59}6061struct linux_file *62linux_shmem_file_setup(const char *name, loff_t size, unsigned long flags)63{64struct {65struct linux_file file __aligned(sizeof(void *));66struct vnode vnode __aligned(sizeof(void *));67} *fileobj;68struct linux_file *filp;69struct vnode *vp;70int error;7172fileobj = kzalloc(sizeof(*fileobj), GFP_KERNEL);73if (fileobj == NULL) {74error = -ENOMEM;75goto err_0;76}77filp = &fileobj->file;78vp = &fileobj->vnode;7980filp->f_count = 1;81filp->f_vnode = vp;82filp->f_shmem = vm_pager_allocate(OBJT_SWAP, NULL, size,83VM_PROT_READ | VM_PROT_WRITE, 0, curthread->td_ucred);84if (filp->f_shmem == NULL) {85error = -ENOMEM;86goto err_1;87}88return (filp);89err_1:90kfree(fileobj);91err_0:92return (ERR_PTR(error));93}9495static vm_ooffset_t96linux_invalidate_mapping_pages_sub(vm_object_t obj, vm_pindex_t start,97vm_pindex_t end, int flags)98{99int start_count, end_count;100101VM_OBJECT_WLOCK(obj);102start_count = obj->resident_page_count;103vm_object_page_remove(obj, start, end, flags);104end_count = obj->resident_page_count;105VM_OBJECT_WUNLOCK(obj);106return (start_count - end_count);107}108109unsigned long110linux_invalidate_mapping_pages(vm_object_t obj, pgoff_t start, pgoff_t end)111{112113return (linux_invalidate_mapping_pages_sub(obj, start, end, OBJPR_CLEANONLY));114}115116void117linux_shmem_truncate_range(vm_object_t obj, loff_t lstart, loff_t lend)118{119vm_pindex_t start = OFF_TO_IDX(lstart + PAGE_SIZE - 1);120vm_pindex_t end = OFF_TO_IDX(lend + 1);121122(void) linux_invalidate_mapping_pages_sub(obj, start, end, 0);123}124125126