Path: blob/main/sys/compat/linuxkpi/common/src/linux_shmemfs.c
39586 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 fileobj {65struct linux_file file __aligned(sizeof(void *));66struct vnode vnode __aligned(sizeof(void *));67};68struct fileobj *fileobj;69struct linux_file *filp;70struct vnode *vp;71int error;7273fileobj = kzalloc(sizeof(*fileobj), GFP_KERNEL);74if (fileobj == NULL) {75error = -ENOMEM;76goto err_0;77}78filp = &fileobj->file;79vp = &fileobj->vnode;8081filp->f_count = 1;82filp->f_vnode = vp;83filp->f_shmem = vm_pager_allocate(OBJT_SWAP, NULL, size,84VM_PROT_READ | VM_PROT_WRITE, 0, curthread->td_ucred);85if (filp->f_shmem == NULL) {86error = -ENOMEM;87goto err_1;88}89return (filp);90err_1:91kfree(filp);92err_0:93return (ERR_PTR(error));94}9596static vm_ooffset_t97linux_invalidate_mapping_pages_sub(vm_object_t obj, vm_pindex_t start,98vm_pindex_t end, int flags)99{100int start_count, end_count;101102VM_OBJECT_WLOCK(obj);103start_count = obj->resident_page_count;104vm_object_page_remove(obj, start, end, flags);105end_count = obj->resident_page_count;106VM_OBJECT_WUNLOCK(obj);107return (start_count - end_count);108}109110unsigned long111linux_invalidate_mapping_pages(vm_object_t obj, pgoff_t start, pgoff_t end)112{113114return (linux_invalidate_mapping_pages_sub(obj, start, end, OBJPR_CLEANONLY));115}116117void118linux_shmem_truncate_range(vm_object_t obj, loff_t lstart, loff_t lend)119{120vm_pindex_t start = OFF_TO_IDX(lstart + PAGE_SIZE - 1);121vm_pindex_t end = OFF_TO_IDX(lend + 1);122123(void) linux_invalidate_mapping_pages_sub(obj, start, end, 0);124}125126127