Whamcloud - gitweb
LU-6142 lustre: convert IFTODT to S_DT
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-mem.h
index fa4ba3d..69d027f 100644 (file)
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * GPL HEADER START
  *
- *  Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
  *
- * Basic library routines.
+ * Copyright (c) 2011, 2014, Intel Corporation.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * libcfs/include/libcfs/linux/linux-mem.h
  *
+ * Basic library routines.
  */
 
 #ifndef __LIBCFS_LINUX_CFS_MEM_H__
 #define __LIBCFS_LINUX_CFS_MEM_H__
 
-#ifndef __LIBCFS_LIBCFS_H__
-#error Do not #include this file directly. #include <libcfs/libcfs.h> instead
+#include <linux/mm.h>
+#include <linux/vmalloc.h>
+#include <linux/pagemap.h>
+#include <linux/slab.h>
+#ifdef HAVE_MM_INLINE
+# include <linux/mm_inline.h>
+#endif
+#include <linux/sched.h>
+#ifdef HAVE_SCHED_HEADERS
+#include <linux/sched/mm.h>
 #endif
 
-#ifdef __KERNEL__
-# include <linux/mm.h>
-# include <linux/vmalloc.h>
-# include <linux/pagemap.h>
-# include <linux/slab.h>
-# ifdef HAVE_MM_INLINE
-#  include <linux/mm_inline.h>
-# endif
-
-typedef struct page                     cfs_page_t;
-#define CFS_PAGE_SIZE                   PAGE_CACHE_SIZE
-#define CFS_PAGE_SHIFT                  PAGE_CACHE_SHIFT
-#define CFS_PAGE_MASK                   (~((__u64)CFS_PAGE_SIZE-1))
-
-static inline void *cfs_page_address(cfs_page_t *page)
+#ifdef HAVE_TOTALRAM_PAGES_AS_FUNC
+ #ifndef cfs_totalram_pages
+  #define cfs_totalram_pages() totalram_pages()
+ #endif
+#else
+ #ifndef cfs_totalram_pages
+  #define cfs_totalram_pages() totalram_pages
+ #endif
+#endif
+
+#ifndef HAVE_MEMALLOC_RECLAIM
+static inline unsigned int memalloc_noreclaim_save(void)
 {
-        /*
-         * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
-         * from here: this will lead to infinite recursion.
-         */
-        return page_address(page);
+       unsigned int flags = current->flags & PF_MEMALLOC;
+
+       current->flags |= PF_MEMALLOC;
+       return flags;
 }
 
-static inline void *cfs_kmap(cfs_page_t *page)
+static inline void memalloc_noreclaim_restore(unsigned int flags)
 {
-        return kmap(page);
+       current->flags = (current->flags & ~PF_MEMALLOC) | flags;
 }
+#endif /* !HAVE_MEMALLOC_RECLAIM */
 
-static inline void cfs_kunmap(cfs_page_t *page)
+#ifndef HAVE_BITMAP_ALLOC
+static inline unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags)
 {
-        kunmap(page);
+       return kmalloc_array(BITS_TO_LONGS(nbits), sizeof(unsigned long),
+                            flags);
 }
 
-static inline void cfs_get_page(cfs_page_t *page)
+static inline unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags)
 {
-        get_page(page);
+       return bitmap_alloc(nbits, flags | __GFP_ZERO);
 }
 
-static inline int cfs_page_count(cfs_page_t *page)
+static inline void bitmap_free(const unsigned long *bitmap)
 {
-        return page_count(page);
+       kfree(bitmap);
 }
-
-#define cfs_page_index(p)       ((p)->index)
+#endif /* !HAVE_BITMAP_ALLOC */
 
 /*
- * Memory allocator
- * XXX Liang: move these declare to public file
+ * Shrinker
  */
-extern void *cfs_alloc(size_t nr_bytes, u_int32_t flags);
-extern void  cfs_free(void *addr);
-
-extern void *cfs_alloc_large(size_t nr_bytes);
-extern void  cfs_free_large(void *addr);
+#ifndef SHRINK_STOP
+# define SHRINK_STOP (~0UL)
+#endif
 
-extern cfs_page_t *cfs_alloc_pages(unsigned int flags, unsigned int order);
-extern void __cfs_free_pages(cfs_page_t *page, unsigned int order);
+#ifndef HAVE_MMAP_LOCK
+static inline void mmap_write_lock(struct mm_struct *mm)
+{
+       down_write(&mm->mmap_sem);
+}
 
-#define cfs_alloc_page(flags)  cfs_alloc_pages(flags, 0)
-#define __cfs_free_page(page)  __cfs_free_pages(page, 0)
-#define cfs_free_page(p)       __free_pages(p, 0)
+static inline bool mmap_write_trylock(struct mm_struct *mm)
+{
+       return down_write_trylock(&mm->mmap_sem) != 0;
+}
 
-/*
- * In Linux there is no way to determine whether current execution context is
- * blockable.
- */
-#define CFS_ALLOC_ATOMIC_TRY   CFS_ALLOC_ATOMIC
+static inline void mmap_write_unlock(struct mm_struct *mm)
+{
+       up_write(&mm->mmap_sem);
+}
 
-/*
- * SLAB allocator
- * XXX Liang: move these declare to public file
- */
-#ifdef HAVE_KMEM_CACHE
-typedef struct kmem_cache cfs_mem_cache_t;
-#else
-typedef kmem_cache_t cfs_mem_cache_t;
-#endif
-extern cfs_mem_cache_t * cfs_mem_cache_create (const char *, size_t, size_t, unsigned long);
-extern int cfs_mem_cache_destroy ( cfs_mem_cache_t * );
-extern void *cfs_mem_cache_alloc ( cfs_mem_cache_t *, int);
-extern void cfs_mem_cache_free ( cfs_mem_cache_t *, void *);
+static inline void mmap_read_lock(struct mm_struct *mm)
+{
+       down_read(&mm->mmap_sem);
+}
 
-/*
- */
-#define CFS_DECL_MMSPACE                mm_segment_t __oldfs
-#define CFS_MMSPACE_OPEN                do { __oldfs = get_fs(); set_fs(get_ds());} while(0)
-#define CFS_MMSPACE_CLOSE               set_fs(__oldfs)
+static inline bool mmap_read_trylock(struct mm_struct *mm)
+{
+       return down_read_trylock(&mm->mmap_sem) != 0;
+}
 
-#else   /* !__KERNEL__ */
-#ifdef HAVE_ASM_PAGE_H
-#include <asm/page.h>           /* needed for PAGE_SIZE - rread */
+static inline void mmap_read_unlock(struct mm_struct *mm)
+{
+       up_read(&mm->mmap_sem);
+}
 #endif
 
-#include <libcfs/user-prim.h>
-/* __KERNEL__ */
+#ifdef HAVE_VMALLOC_2ARGS
+#define __ll_vmalloc(size, flags) __vmalloc(size, flags)
+#else
+#define __ll_vmalloc(size, flags) __vmalloc(size, flags, PAGE_KERNEL)
 #endif
 
 #endif /* __LINUX_CFS_MEM_H__ */