tmpfs: demolish old swap vector support
[pandora-kernel.git] / mm / shmem.c
index fcedf54..5574b00 100644 (file)
@@ -51,6 +51,7 @@ static struct vfsmount *shm_mnt;
 #include <linux/shmem_fs.h>
 #include <linux/writeback.h>
 #include <linux/blkdev.h>
+#include <linux/splice.h>
 #include <linux/security.h>
 #include <linux/swapops.h>
 #include <linux/mempolicy.h>
@@ -65,37 +66,9 @@ static struct vfsmount *shm_mnt;
 #include <asm/div64.h>
 #include <asm/pgtable.h>
 
-/*
- * The maximum size of a shmem/tmpfs file is limited by the maximum size of
- * its triple-indirect swap vector - see illustration at shmem_swp_entry().
- *
- * With 4kB page size, maximum file size is just over 2TB on a 32-bit kernel,
- * but one eighth of that on a 64-bit kernel.  With 8kB page size, maximum
- * file size is just over 4TB on a 64-bit kernel, but 16TB on a 32-bit kernel,
- * MAX_LFS_FILESIZE being then more restrictive than swap vector layout.
- *
- * We use / and * instead of shifts in the definitions below, so that the swap
- * vector can be tested with small even values (e.g. 20) for ENTRIES_PER_PAGE.
- */
-#define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
-#define ENTRIES_PER_PAGEPAGE ((unsigned long long)ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
-
-#define SHMSWP_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
-#define SHMSWP_MAX_BYTES (SHMSWP_MAX_INDEX << PAGE_CACHE_SHIFT)
-
-#define SHMEM_MAX_BYTES  min_t(unsigned long long, SHMSWP_MAX_BYTES, MAX_LFS_FILESIZE)
-#define SHMEM_MAX_INDEX  ((unsigned long)((SHMEM_MAX_BYTES+1) >> PAGE_CACHE_SHIFT))
-
 #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
 #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
 
-/* info->flags needs VM_flags to handle pagein/truncate races efficiently */
-#define SHMEM_PAGEIN    VM_READ
-#define SHMEM_TRUNCATE  VM_WRITE
-
-/* Definition to limit shmem_truncate's steps between cond_rescheds */
-#define LATENCY_LIMIT   64
-
 /* Pretend that each entry is of this size in directory's i_size */
 #define BOGO_DIRENT_SIZE 20
 
@@ -106,7 +79,7 @@ struct shmem_xattr {
        char value[0];
 };
 
-/* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
+/* Flag allocation requirements to shmem_getpage */
 enum sgp_type {
        SGP_READ,       /* don't exceed i_size, don't allocate page */
        SGP_CACHE,      /* don't exceed i_size, may allocate page */
@@ -126,57 +99,14 @@ static unsigned long shmem_default_max_inodes(void)
 }
 #endif
 
-static int shmem_getpage(struct inode *inode, unsigned long idx,
-                        struct page **pagep, enum sgp_type sgp, int *type);
-
-static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
-{
-       /*
-        * The above definition of ENTRIES_PER_PAGE, and the use of
-        * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
-        * might be reconsidered if it ever diverges from PAGE_SIZE.
-        *
-        * Mobility flags are masked out as swap vectors cannot move
-        */
-       return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
-                               PAGE_CACHE_SHIFT-PAGE_SHIFT);
-}
-
-static inline void shmem_dir_free(struct page *page)
-{
-       __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
-}
-
-static struct page **shmem_dir_map(struct page *page)
-{
-       return (struct page **)kmap_atomic(page, KM_USER0);
-}
-
-static inline void shmem_dir_unmap(struct page **dir)
-{
-       kunmap_atomic(dir, KM_USER0);
-}
-
-static swp_entry_t *shmem_swp_map(struct page *page)
-{
-       return (swp_entry_t *)kmap_atomic(page, KM_USER1);
-}
-
-static inline void shmem_swp_balance_unmap(void)
-{
-       /*
-        * When passing a pointer to an i_direct entry, to code which
-        * also handles indirect entries and so will shmem_swp_unmap,
-        * we must arrange for the preempt count to remain in balance.
-        * What kmap_atomic of a lowmem page does depends on config
-        * and architecture, so pretend to kmap_atomic some lowmem page.
-        */
-       (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
-}
+static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
+       struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
 
-static inline void shmem_swp_unmap(swp_entry_t *entry)
+static inline int shmem_getpage(struct inode *inode, pgoff_t index,
+       struct page **pagep, enum sgp_type sgp, int *fault_type)
 {
-       kunmap_atomic(entry, KM_USER1);
+       return shmem_getpage_gfp(inode, index, pagep, sgp,
+                       mapping_gfp_mask(inode->i_mapping), fault_type);
 }
 
 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
@@ -241,9 +171,7 @@ static void shmem_free_blocks(struct inode *inode, long pages)
        struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
        if (sbinfo->max_blocks) {
                percpu_counter_add(&sbinfo->used_blocks, -pages);
-               spin_lock(&inode->i_lock);
                inode->i_blocks -= pages*BLOCKS_PER_PAGE;
-               spin_unlock(&inode->i_lock);
        }
 }
 
@@ -297,468 +225,56 @@ static void shmem_recalc_inode(struct inode *inode)
        }
 }
 
-/**
- * shmem_swp_entry - find the swap vector position in the info structure
- * @info:  info structure for the inode
- * @index: index of the page to find
- * @page:  optional page to add to the structure. Has to be preset to
- *         all zeros
- *
- * If there is no space allocated yet it will return NULL when
- * page is NULL, else it will use the page for the needed block,
- * setting it to NULL on return to indicate that it has been used.
- *
- * The swap vector is organized the following way:
- *
- * There are SHMEM_NR_DIRECT entries directly stored in the
- * shmem_inode_info structure. So small files do not need an addional
- * allocation.
- *
- * For pages with index > SHMEM_NR_DIRECT there is the pointer
- * i_indirect which points to a page which holds in the first half
- * doubly indirect blocks, in the second half triple indirect blocks:
- *
- * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
- * following layout (for SHMEM_NR_DIRECT == 16):
- *
- * i_indirect -> dir --> 16-19
- *           |      +-> 20-23
- *           |
- *           +-->dir2 --> 24-27
- *           |        +-> 28-31
- *           |        +-> 32-35
- *           |        +-> 36-39
- *           |
- *           +-->dir3 --> 40-43
- *                    +-> 44-47
- *                    +-> 48-51
- *                    +-> 52-55
- */
-static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
+static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index,
+                          swp_entry_t swap)
 {
-       unsigned long offset;
-       struct page **dir;
-       struct page *subdir;
-
-       if (index < SHMEM_NR_DIRECT) {
-               shmem_swp_balance_unmap();
-               return info->i_direct+index;
-       }
-       if (!info->i_indirect) {
-               if (page) {
-                       info->i_indirect = *page;
-                       *page = NULL;
-               }
-               return NULL;                    /* need another page */
-       }
-
-       index -= SHMEM_NR_DIRECT;
-       offset = index % ENTRIES_PER_PAGE;
-       index /= ENTRIES_PER_PAGE;
-       dir = shmem_dir_map(info->i_indirect);
-
-       if (index >= ENTRIES_PER_PAGE/2) {
-               index -= ENTRIES_PER_PAGE/2;
-               dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
-               index %= ENTRIES_PER_PAGE;
-               subdir = *dir;
-               if (!subdir) {
-                       if (page) {
-                               *dir = *page;
-                               *page = NULL;
-                       }
-                       shmem_dir_unmap(dir);
-                       return NULL;            /* need another page */
-               }
-               shmem_dir_unmap(dir);
-               dir = shmem_dir_map(subdir);
-       }
-
-       dir += index;
-       subdir = *dir;
-       if (!subdir) {
-               if (!page || !(subdir = *page)) {
-                       shmem_dir_unmap(dir);
-                       return NULL;            /* need a page */
-               }
-               *dir = subdir;
-               *page = NULL;
-       }
-       shmem_dir_unmap(dir);
-       return shmem_swp_map(subdir) + offset;
+       if (index < SHMEM_NR_DIRECT)
+               info->i_direct[index] = swap;
 }
 
-static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
+static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index)
 {
-       long incdec = value? 1: -1;
-
-       entry->val = value;
-       info->swapped += incdec;
-       if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
-               struct page *page = kmap_atomic_to_page(entry);
-               set_page_private(page, page_private(page) + incdec);
-       }
+       return (index < SHMEM_NR_DIRECT) ?
+               info->i_direct[index] : (swp_entry_t){0};
 }
 
-/**
- * shmem_swp_alloc - get the position of the swap entry for the page.
- * @info:      info structure for the inode
- * @index:     index of the page to find
- * @sgp:       check and recheck i_size? skip allocation?
- *
- * If the entry does not exist, allocate it.
- */
-static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
-{
-       struct inode *inode = &info->vfs_inode;
-       struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
-       struct page *page = NULL;
-       swp_entry_t *entry;
-
-       if (sgp != SGP_WRITE &&
-           ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
-               return ERR_PTR(-EINVAL);
-
-       while (!(entry = shmem_swp_entry(info, index, &page))) {
-               if (sgp == SGP_READ)
-                       return shmem_swp_map(ZERO_PAGE(0));
-               /*
-                * Test used_blocks against 1 less max_blocks, since we have 1 data
-                * page (and perhaps indirect index pages) yet to allocate:
-                * a waste to allocate index if we cannot allocate data.
-                */
-               if (sbinfo->max_blocks) {
-                       if (percpu_counter_compare(&sbinfo->used_blocks,
-                                               sbinfo->max_blocks - 1) >= 0)
-                               return ERR_PTR(-ENOSPC);
-                       percpu_counter_inc(&sbinfo->used_blocks);
-                       spin_lock(&inode->i_lock);
-                       inode->i_blocks += BLOCKS_PER_PAGE;
-                       spin_unlock(&inode->i_lock);
-               }
-
-               spin_unlock(&info->lock);
-               page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
-               spin_lock(&info->lock);
-
-               if (!page) {
-                       shmem_free_blocks(inode, 1);
-                       return ERR_PTR(-ENOMEM);
-               }
-               if (sgp != SGP_WRITE &&
-                   ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
-                       entry = ERR_PTR(-EINVAL);
-                       break;
-               }
-               if (info->next_index <= index)
-                       info->next_index = index + 1;
-       }
-       if (page) {
-               /* another task gave its page, or truncated the file */
-               shmem_free_blocks(inode, 1);
-               shmem_dir_free(page);
-       }
-       if (info->next_index <= index && !IS_ERR(entry))
-               info->next_index = index + 1;
-       return entry;
-}
-
-/**
- * shmem_free_swp - free some swap entries in a directory
- * @dir:        pointer to the directory
- * @edir:       pointer after last entry of the directory
- * @punch_lock: pointer to spinlock when needed for the holepunch case
- */
-static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
-                                               spinlock_t *punch_lock)
-{
-       spinlock_t *punch_unlock = NULL;
-       swp_entry_t *ptr;
-       int freed = 0;
-
-       for (ptr = dir; ptr < edir; ptr++) {
-               if (ptr->val) {
-                       if (unlikely(punch_lock)) {
-                               punch_unlock = punch_lock;
-                               punch_lock = NULL;
-                               spin_lock(punch_unlock);
-                               if (!ptr->val)
-                                       continue;
-                       }
-                       free_swap_and_cache(*ptr);
-                       *ptr = (swp_entry_t){0};
-                       freed++;
-               }
-       }
-       if (punch_unlock)
-               spin_unlock(punch_unlock);
-       return freed;
-}
-
-static int shmem_map_and_free_swp(struct page *subdir, int offset,
-               int limit, struct page ***dir, spinlock_t *punch_lock)
-{
-       swp_entry_t *ptr;
-       int freed = 0;
-
-       ptr = shmem_swp_map(subdir);
-       for (; offset < limit; offset += LATENCY_LIMIT) {
-               int size = limit - offset;
-               if (size > LATENCY_LIMIT)
-                       size = LATENCY_LIMIT;
-               freed += shmem_free_swp(ptr+offset, ptr+offset+size,
-                                                       punch_lock);
-               if (need_resched()) {
-                       shmem_swp_unmap(ptr);
-                       if (*dir) {
-                               shmem_dir_unmap(*dir);
-                               *dir = NULL;
-                       }
-                       cond_resched();
-                       ptr = shmem_swp_map(subdir);
-               }
-       }
-       shmem_swp_unmap(ptr);
-       return freed;
-}
-
-static void shmem_free_pages(struct list_head *next)
-{
-       struct page *page;
-       int freed = 0;
-
-       do {
-               page = container_of(next, struct page, lru);
-               next = next->next;
-               shmem_dir_free(page);
-               freed++;
-               if (freed >= LATENCY_LIMIT) {
-                       cond_resched();
-                       freed = 0;
-               }
-       } while (next);
-}
-
-void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
+void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
 {
+       struct address_space *mapping = inode->i_mapping;
        struct shmem_inode_info *info = SHMEM_I(inode);
-       unsigned long idx;
-       unsigned long size;
-       unsigned long limit;
-       unsigned long stage;
-       unsigned long diroff;
-       struct page **dir;
-       struct page *topdir;
-       struct page *middir;
-       struct page *subdir;
-       swp_entry_t *ptr;
-       LIST_HEAD(pages_to_free);
-       long nr_pages_to_free = 0;
-       long nr_swaps_freed = 0;
-       int offset;
-       int freed;
-       int punch_hole;
-       spinlock_t *needs_lock;
-       spinlock_t *punch_lock;
-       unsigned long upper_limit;
+       pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
+       pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
+       pgoff_t index;
+       swp_entry_t swap;
 
-       truncate_inode_pages_range(inode->i_mapping, start, end);
+       truncate_inode_pages_range(mapping, lstart, lend);
 
-       inode->i_ctime = inode->i_mtime = CURRENT_TIME;
-       idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
-       if (idx >= info->next_index)
-               return;
+       if (end > SHMEM_NR_DIRECT)
+               end = SHMEM_NR_DIRECT;
 
        spin_lock(&info->lock);
-       info->flags |= SHMEM_TRUNCATE;
-       if (likely(end == (loff_t) -1)) {
-               limit = info->next_index;
-               upper_limit = SHMEM_MAX_INDEX;
-               info->next_index = idx;
-               needs_lock = NULL;
-               punch_hole = 0;
-       } else {
-               if (end + 1 >= inode->i_size) { /* we may free a little more */
-                       limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
-                                                       PAGE_CACHE_SHIFT;
-                       upper_limit = SHMEM_MAX_INDEX;
-               } else {
-                       limit = (end + 1) >> PAGE_CACHE_SHIFT;
-                       upper_limit = limit;
-               }
-               needs_lock = &info->lock;
-               punch_hole = 1;
-       }
-
-       topdir = info->i_indirect;
-       if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
-               info->i_indirect = NULL;
-               nr_pages_to_free++;
-               list_add(&topdir->lru, &pages_to_free);
-       }
-       spin_unlock(&info->lock);
-
-       if (info->swapped && idx < SHMEM_NR_DIRECT) {
-               ptr = info->i_direct;
-               size = limit;
-               if (size > SHMEM_NR_DIRECT)
-                       size = SHMEM_NR_DIRECT;
-               nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
-       }
-
-       /*
-        * If there are no indirect blocks or we are punching a hole
-        * below indirect blocks, nothing to be done.
-        */
-       if (!topdir || limit <= SHMEM_NR_DIRECT)
-               goto done2;
-
-       /*
-        * The truncation case has already dropped info->lock, and we're safe
-        * because i_size and next_index have already been lowered, preventing
-        * access beyond.  But in the punch_hole case, we still need to take
-        * the lock when updating the swap directory, because there might be
-        * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
-        * shmem_writepage.  However, whenever we find we can remove a whole
-        * directory page (not at the misaligned start or end of the range),
-        * we first NULLify its pointer in the level above, and then have no
-        * need to take the lock when updating its contents: needs_lock and
-        * punch_lock (either pointing to info->lock or NULL) manage this.
-        */
-
-       upper_limit -= SHMEM_NR_DIRECT;
-       limit -= SHMEM_NR_DIRECT;
-       idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
-       offset = idx % ENTRIES_PER_PAGE;
-       idx -= offset;
-
-       dir = shmem_dir_map(topdir);
-       stage = ENTRIES_PER_PAGEPAGE/2;
-       if (idx < ENTRIES_PER_PAGEPAGE/2) {
-               middir = topdir;
-               diroff = idx/ENTRIES_PER_PAGE;
-       } else {
-               dir += ENTRIES_PER_PAGE/2;
-               dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
-               while (stage <= idx)
-                       stage += ENTRIES_PER_PAGEPAGE;
-               middir = *dir;
-               if (*dir) {
-                       diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
-                               ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
-                       if (!diroff && !offset && upper_limit >= stage) {
-                               if (needs_lock) {
-                                       spin_lock(needs_lock);
-                                       *dir = NULL;
-                                       spin_unlock(needs_lock);
-                                       needs_lock = NULL;
-                               } else
-                                       *dir = NULL;
-                               nr_pages_to_free++;
-                               list_add(&middir->lru, &pages_to_free);
-                       }
-                       shmem_dir_unmap(dir);
-                       dir = shmem_dir_map(middir);
-               } else {
-                       diroff = 0;
-                       offset = 0;
-                       idx = stage;
+       for (index = start; index < end; index++) {
+               swap = shmem_get_swap(info, index);
+               if (swap.val) {
+                       free_swap_and_cache(swap);
+                       shmem_put_swap(info, index, (swp_entry_t){0});
+                       info->swapped--;
                }
        }
 
-       for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
-               if (unlikely(idx == stage)) {
-                       shmem_dir_unmap(dir);
-                       dir = shmem_dir_map(topdir) +
-                           ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
-                       while (!*dir) {
-                               dir++;
-                               idx += ENTRIES_PER_PAGEPAGE;
-                               if (idx >= limit)
-                                       goto done1;
-                       }
-                       stage = idx + ENTRIES_PER_PAGEPAGE;
-                       middir = *dir;
-                       if (punch_hole)
-                               needs_lock = &info->lock;
-                       if (upper_limit >= stage) {
-                               if (needs_lock) {
-                                       spin_lock(needs_lock);
-                                       *dir = NULL;
-                                       spin_unlock(needs_lock);
-                                       needs_lock = NULL;
-                               } else
-                                       *dir = NULL;
-                               nr_pages_to_free++;
-                               list_add(&middir->lru, &pages_to_free);
-                       }
-                       shmem_dir_unmap(dir);
-                       cond_resched();
-                       dir = shmem_dir_map(middir);
-                       diroff = 0;
-               }
-               punch_lock = needs_lock;
-               subdir = dir[diroff];
-               if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
-                       if (needs_lock) {
-                               spin_lock(needs_lock);
-                               dir[diroff] = NULL;
-                               spin_unlock(needs_lock);
-                               punch_lock = NULL;
-                       } else
-                               dir[diroff] = NULL;
-                       nr_pages_to_free++;
-                       list_add(&subdir->lru, &pages_to_free);
-               }
-               if (subdir && page_private(subdir) /* has swap entries */) {
-                       size = limit - idx;
-                       if (size > ENTRIES_PER_PAGE)
-                               size = ENTRIES_PER_PAGE;
-                       freed = shmem_map_and_free_swp(subdir,
-                                       offset, size, &dir, punch_lock);
-                       if (!dir)
-                               dir = shmem_dir_map(middir);
-                       nr_swaps_freed += freed;
-                       if (offset || punch_lock) {
-                               spin_lock(&info->lock);
-                               set_page_private(subdir,
-                                       page_private(subdir) - freed);
-                               spin_unlock(&info->lock);
-                       } else
-                               BUG_ON(page_private(subdir) != freed);
-               }
-               offset = 0;
-       }
-done1:
-       shmem_dir_unmap(dir);
-done2:
-       if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
+       if (mapping->nrpages) {
+               spin_unlock(&info->lock);
                /*
-                * Call truncate_inode_pages again: racing shmem_unuse_inode
-                * may have swizzled a page in from swap since
-                * truncate_pagecache or generic_delete_inode did it, before we
-                * lowered next_index.  Also, though shmem_getpage checks
-                * i_size before adding to cache, no recheck after: so fix the
-                * narrow window there too.
+                * A page may have meanwhile sneaked in from swap.
                 */
-               truncate_inode_pages_range(inode->i_mapping, start, end);
+               truncate_inode_pages_range(mapping, lstart, lend);
+               spin_lock(&info->lock);
        }
 
-       spin_lock(&info->lock);
-       info->flags &= ~SHMEM_TRUNCATE;
-       info->swapped -= nr_swaps_freed;
-       if (nr_pages_to_free)
-               shmem_free_blocks(inode, nr_pages_to_free);
        shmem_recalc_inode(inode);
        spin_unlock(&info->lock);
 
-       /*
-        * Empty swap vector directory pages to be freed?
-        */
-       if (!list_empty(&pages_to_free)) {
-               pages_to_free.prev->next = NULL;
-               shmem_free_pages(pages_to_free.next);
-       }
+       inode->i_ctime = inode->i_mtime = CURRENT_TIME;
 }
 EXPORT_SYMBOL_GPL(shmem_truncate_range);
 
@@ -791,19 +307,6 @@ static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
                                if (page)
                                        unlock_page(page);
                        }
-                       /*
-                        * Reset SHMEM_PAGEIN flag so that shmem_truncate can
-                        * detect if any pages might have been added to cache
-                        * after truncate_inode_pages.  But we needn't bother
-                        * if it's being fully truncated to zero-length: the
-                        * nrpages check is efficient enough in that case.
-                        */
-                       if (newsize) {
-                               struct shmem_inode_info *info = SHMEM_I(inode);
-                               spin_lock(&info->lock);
-                               info->flags &= ~SHMEM_PAGEIN;
-                               spin_unlock(&info->lock);
-                       }
                }
                if (newsize != oldsize) {
                        i_size_write(inode, newsize);
@@ -853,106 +356,28 @@ static void shmem_evict_inode(struct inode *inode)
        end_writeback(inode);
 }
 
-static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
-{
-       swp_entry_t *ptr;
-
-       for (ptr = dir; ptr < edir; ptr++) {
-               if (ptr->val == entry.val)
-                       return ptr - dir;
-       }
-       return -1;
-}
-
 static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
 {
-       struct address_space *mapping;
+       struct address_space *mapping = info->vfs_inode.i_mapping;
        unsigned long idx;
-       unsigned long size;
-       unsigned long limit;
-       unsigned long stage;
-       struct page **dir;
-       struct page *subdir;
-       swp_entry_t *ptr;
-       int offset;
        int error;
 
-       idx = 0;
-       ptr = info->i_direct;
-       spin_lock(&info->lock);
-       if (!info->swapped) {
-               list_del_init(&info->swaplist);
-               goto lost2;
-       }
-       limit = info->next_index;
-       size = limit;
-       if (size > SHMEM_NR_DIRECT)
-               size = SHMEM_NR_DIRECT;
-       offset = shmem_find_swp(entry, ptr, ptr+size);
-       if (offset >= 0) {
-               shmem_swp_balance_unmap();
-               goto found;
-       }
-       if (!info->i_indirect)
-               goto lost2;
-
-       dir = shmem_dir_map(info->i_indirect);
-       stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
-
-       for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
-               if (unlikely(idx == stage)) {
-                       shmem_dir_unmap(dir-1);
-                       if (cond_resched_lock(&info->lock)) {
-                               /* check it has not been truncated */
-                               if (limit > info->next_index) {
-                                       limit = info->next_index;
-                                       if (idx >= limit)
-                                               goto lost2;
-                               }
-                       }
-                       dir = shmem_dir_map(info->i_indirect) +
-                           ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
-                       while (!*dir) {
-                               dir++;
-                               idx += ENTRIES_PER_PAGEPAGE;
-                               if (idx >= limit)
-                                       goto lost1;
-                       }
-                       stage = idx + ENTRIES_PER_PAGEPAGE;
-                       subdir = *dir;
-                       shmem_dir_unmap(dir);
-                       dir = shmem_dir_map(subdir);
-               }
-               subdir = *dir;
-               if (subdir && page_private(subdir)) {
-                       ptr = shmem_swp_map(subdir);
-                       size = limit - idx;
-                       if (size > ENTRIES_PER_PAGE)
-                               size = ENTRIES_PER_PAGE;
-                       offset = shmem_find_swp(entry, ptr, ptr+size);
-                       shmem_swp_unmap(ptr);
-                       if (offset >= 0) {
-                               shmem_dir_unmap(dir);
-                               ptr = shmem_swp_map(subdir);
-                               goto found;
-                       }
-               }
-       }
-lost1:
-       shmem_dir_unmap(dir-1);
-lost2:
-       spin_unlock(&info->lock);
+       for (idx = 0; idx < SHMEM_NR_DIRECT; idx++)
+               if (shmem_get_swap(info, idx).val == entry.val)
+                       goto found;
        return 0;
 found:
-       idx += offset;
-       ptr += offset;
+       spin_lock(&info->lock);
+       if (shmem_get_swap(info, idx).val != entry.val) {
+               spin_unlock(&info->lock);
+               return 0;
+       }
 
        /*
         * Move _head_ to start search for next from here.
         * But be careful: shmem_evict_inode checks list_empty without taking
         * mutex, and there's an instant in list_move_tail when info->swaplist
-        * would appear empty, if it were the only one on shmem_swaplist.  We
-        * could avoid doing it if inode NULL; or use this minor optimization.
+        * would appear empty, if it were the only one on shmem_swaplist.
         */
        if (shmem_swaplist.next != &info->swaplist)
                list_move_tail(&shmem_swaplist, &info->swaplist);
@@ -962,32 +387,17 @@ found:
         * but also to hold up shmem_evict_inode(): so inode cannot be freed
         * beneath us (pagelock doesn't help until the page is in pagecache).
         */
-       mapping = info->vfs_inode.i_mapping;
        error = add_to_page_cache_locked(page, mapping, idx, GFP_NOWAIT);
        /* which does mem_cgroup_uncharge_cache_page on error */
 
-       if (error == -EEXIST) {
-               struct page *filepage = find_get_page(mapping, idx);
-               error = 1;
-               if (filepage) {
-                       /*
-                        * There might be a more uptodate page coming down
-                        * from a stacked writepage: forget our swappage if so.
-                        */
-                       if (PageUptodate(filepage))
-                               error = 0;
-                       page_cache_release(filepage);
-               }
-       }
-       if (!error) {
+       if (error != -ENOMEM) {
                delete_from_swap_cache(page);
                set_page_dirty(page);
-               info->flags |= SHMEM_PAGEIN;
-               shmem_swp_set(info, ptr, 0);
+               shmem_put_swap(info, idx, (swp_entry_t){0});
+               info->swapped--;
                swap_free(entry);
                error = 1;      /* not an error, but entry was found */
        }
-       shmem_swp_unmap(ptr);
        spin_unlock(&info->lock);
        return error;
 }
@@ -1024,7 +434,14 @@ int shmem_unuse(swp_entry_t entry, struct page *page)
        mutex_lock(&shmem_swaplist_mutex);
        list_for_each_safe(p, next, &shmem_swaplist) {
                info = list_entry(p, struct shmem_inode_info, swaplist);
-               found = shmem_unuse_inode(info, entry, page);
+               if (!info->swapped) {
+                       spin_lock(&info->lock);
+                       if (!info->swapped)
+                               list_del_init(&info->swaplist);
+                       spin_unlock(&info->lock);
+               }
+               if (info->swapped)
+                       found = shmem_unuse_inode(info, entry, page);
                cond_resched();
                if (found)
                        break;
@@ -1048,7 +465,7 @@ out:
 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 {
        struct shmem_inode_info *info;
-       swp_entry_t *entry, swap;
+       swp_entry_t swap, oswap;
        struct address_space *mapping;
        unsigned long index;
        struct inode *inode;
@@ -1066,16 +483,26 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
        /*
         * shmem_backing_dev_info's capabilities prevent regular writeback or
         * sync from ever calling shmem_writepage; but a stacking filesystem
-        * may use the ->writepage of its underlying filesystem, in which case
+        * might use ->writepage of its underlying filesystem, in which case
         * tmpfs should write out to swap only in response to memory pressure,
-        * and not for the writeback threads or sync.  However, in those cases,
-        * we do still want to check if there's a redundant swappage to be
-        * discarded.
+        * and not for the writeback threads or sync.
         */
-       if (wbc->for_reclaim)
-               swap = get_swap_page();
-       else
-               swap.val = 0;
+       if (!wbc->for_reclaim) {
+               WARN_ON_ONCE(1);        /* Still happens? Tell us about it! */
+               goto redirty;
+       }
+
+       /*
+        * Just for this patch, we have a toy implementation,
+        * which can swap out only the first SHMEM_NR_DIRECT pages:
+        * for simple demonstration of where we need to think about swap.
+        */
+       if (index >= SHMEM_NR_DIRECT)
+               goto redirty;
+
+       swap = get_swap_page();
+       if (!swap.val)
+               goto redirty;
 
        /*
         * Add inode to shmem_unuse()'s list of swapped-out inodes,
@@ -1086,35 +513,26 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
         * we've taken the spinlock, because shmem_unuse_inode() will
         * prune a !swapped inode from the swaplist under both locks.
         */
-       if (swap.val) {
-               mutex_lock(&shmem_swaplist_mutex);
-               if (list_empty(&info->swaplist))
-                       list_add_tail(&info->swaplist, &shmem_swaplist);
-       }
+       mutex_lock(&shmem_swaplist_mutex);
+       if (list_empty(&info->swaplist))
+               list_add_tail(&info->swaplist, &shmem_swaplist);
 
        spin_lock(&info->lock);
-       if (swap.val)
-               mutex_unlock(&shmem_swaplist_mutex);
+       mutex_unlock(&shmem_swaplist_mutex);
 
-       if (index >= info->next_index) {
-               BUG_ON(!(info->flags & SHMEM_TRUNCATE));
-               goto unlock;
-       }
-       entry = shmem_swp_entry(info, index, NULL);
-       if (entry->val) {
-               /*
-                * The more uptodate page coming down from a stacked
-                * writepage should replace our old swappage.
-                */
-               free_swap_and_cache(*entry);
-               shmem_swp_set(info, entry, 0);
+       oswap = shmem_get_swap(info, index);
+       if (oswap.val) {
+               WARN_ON_ONCE(1);        /* Still happens? Tell us about it! */
+               free_swap_and_cache(oswap);
+               shmem_put_swap(info, index, (swp_entry_t){0});
+               info->swapped--;
        }
        shmem_recalc_inode(inode);
 
-       if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
+       if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
                delete_from_page_cache(page);
-               shmem_swp_set(info, entry, swap.val);
-               shmem_swp_unmap(entry);
+               shmem_put_swap(info, index, swap);
+               info->swapped++;
                swap_shmem_alloc(swap);
                spin_unlock(&info->lock);
                BUG_ON(page_mapped(page));
@@ -1122,13 +540,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
                return 0;
        }
 
-       shmem_swp_unmap(entry);
-unlock:
        spin_unlock(&info->lock);
-       /*
-        * add_to_swap_cache() doesn't return -EEXIST, so we can safely
-        * clear SWAP_HAS_CACHE flag.
-        */
        swapcache_free(swap, NULL);
 redirty:
        set_page_dirty(page);
@@ -1228,158 +640,108 @@ static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
 #endif
 
 /*
- * shmem_getpage - either get the page from swap or allocate a new one
+ * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  *
  * If we allocate a new one we do not mark it dirty. That's up to the
  * vm. If we swap it in we mark it dirty since we also free the swap
  * entry since a page cannot live in both the swap and page cache
  */
-static int shmem_getpage(struct inode *inode, unsigned long idx,
-                       struct page **pagep, enum sgp_type sgp, int *type)
+static int shmem_getpage_gfp(struct inode *inode, pgoff_t idx,
+       struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
 {
        struct address_space *mapping = inode->i_mapping;
        struct shmem_inode_info *info = SHMEM_I(inode);
        struct shmem_sb_info *sbinfo;
-       struct page *filepage = *pagep;
-       struct page *swappage;
+       struct page *page;
        struct page *prealloc_page = NULL;
-       swp_entry_t *entry;
        swp_entry_t swap;
-       gfp_t gfp;
        int error;
 
-       if (idx >= SHMEM_MAX_INDEX)
+       if (idx > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
                return -EFBIG;
-
-       if (type)
-               *type = 0;
-
-       /*
-        * Normally, filepage is NULL on entry, and either found
-        * uptodate immediately, or allocated and zeroed, or read
-        * in under swappage, which is then assigned to filepage.
-        * But shmem_readpage (required for splice) passes in a locked
-        * filepage, which may be found not uptodate by other callers
-        * too, and may need to be copied from the swappage read in.
-        */
 repeat:
-       if (!filepage)
-               filepage = find_lock_page(mapping, idx);
-       if (filepage && PageUptodate(filepage))
-               goto done;
-       gfp = mapping_gfp_mask(mapping);
-       if (!filepage) {
+       page = find_lock_page(mapping, idx);
+       if (page) {
                /*
-                * Try to preload while we can wait, to not make a habit of
-                * draining atomic reserves; but don't latch on to this cpu.
+                * Once we can get the page lock, it must be uptodate:
+                * if there were an error in reading back from swap,
+                * the page would not be inserted into the filecache.
                 */
-               error = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
-               if (error)
-                       goto failed;
-               radix_tree_preload_end();
-               if (sgp != SGP_READ && !prealloc_page) {
-                       /* We don't care if this fails */
-                       prealloc_page = shmem_alloc_page(gfp, info, idx);
-                       if (prealloc_page) {
-                               if (mem_cgroup_cache_charge(prealloc_page,
-                                               current->mm, GFP_KERNEL)) {
-                                       page_cache_release(prealloc_page);
-                                       prealloc_page = NULL;
-                               }
+               BUG_ON(!PageUptodate(page));
+               goto done;
+       }
+
+       /*
+        * Try to preload while we can wait, to not make a habit of
+        * draining atomic reserves; but don't latch on to this cpu.
+        */
+       error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
+       if (error)
+               goto out;
+       radix_tree_preload_end();
+
+       if (sgp != SGP_READ && !prealloc_page) {
+               prealloc_page = shmem_alloc_page(gfp, info, idx);
+               if (prealloc_page) {
+                       SetPageSwapBacked(prealloc_page);
+                       if (mem_cgroup_cache_charge(prealloc_page,
+                                       current->mm, GFP_KERNEL)) {
+                               page_cache_release(prealloc_page);
+                               prealloc_page = NULL;
                        }
                }
        }
-       error = 0;
 
        spin_lock(&info->lock);
        shmem_recalc_inode(inode);
-       entry = shmem_swp_alloc(info, idx, sgp);
-       if (IS_ERR(entry)) {
-               spin_unlock(&info->lock);
-               error = PTR_ERR(entry);
-               goto failed;
-       }
-       swap = *entry;
-
+       swap = shmem_get_swap(info, idx);
        if (swap.val) {
                /* Look it up and read it in.. */
-               swappage = lookup_swap_cache(swap);
-               if (!swappage) {
-                       shmem_swp_unmap(entry);
+               page = lookup_swap_cache(swap);
+               if (!page) {
                        spin_unlock(&info->lock);
                        /* here we actually do the io */
-                       if (type)
-                               *type |= VM_FAULT_MAJOR;
-                       swappage = shmem_swapin(swap, gfp, info, idx);
-                       if (!swappage) {
-                               spin_lock(&info->lock);
-                               entry = shmem_swp_alloc(info, idx, sgp);
-                               if (IS_ERR(entry))
-                                       error = PTR_ERR(entry);
-                               else {
-                                       if (entry->val == swap.val)
-                                               error = -ENOMEM;
-                                       shmem_swp_unmap(entry);
+                       if (fault_type)
+                               *fault_type |= VM_FAULT_MAJOR;
+                       page = shmem_swapin(swap, gfp, info, idx);
+                       if (!page) {
+                               swp_entry_t nswap = shmem_get_swap(info, idx);
+                               if (nswap.val == swap.val) {
+                                       error = -ENOMEM;
+                                       goto out;
                                }
-                               spin_unlock(&info->lock);
-                               if (error)
-                                       goto failed;
                                goto repeat;
                        }
-                       wait_on_page_locked(swappage);
-                       page_cache_release(swappage);
+                       wait_on_page_locked(page);
+                       page_cache_release(page);
                        goto repeat;
                }
 
                /* We have to do this with page locked to prevent races */
-               if (!trylock_page(swappage)) {
-                       shmem_swp_unmap(entry);
+               if (!trylock_page(page)) {
                        spin_unlock(&info->lock);
-                       wait_on_page_locked(swappage);
-                       page_cache_release(swappage);
+                       wait_on_page_locked(page);
+                       page_cache_release(page);
                        goto repeat;
                }
-               if (PageWriteback(swappage)) {
-                       shmem_swp_unmap(entry);
+               if (PageWriteback(page)) {
                        spin_unlock(&info->lock);
-                       wait_on_page_writeback(swappage);
-                       unlock_page(swappage);
-                       page_cache_release(swappage);
+                       wait_on_page_writeback(page);
+                       unlock_page(page);
+                       page_cache_release(page);
                        goto repeat;
                }
-               if (!PageUptodate(swappage)) {
-                       shmem_swp_unmap(entry);
+               if (!PageUptodate(page)) {
                        spin_unlock(&info->lock);
-                       unlock_page(swappage);
-                       page_cache_release(swappage);
+                       unlock_page(page);
+                       page_cache_release(page);
                        error = -EIO;
-                       goto failed;
+                       goto out;
                }
 
-               if (filepage) {
-                       shmem_swp_set(info, entry, 0);
-                       shmem_swp_unmap(entry);
-                       delete_from_swap_cache(swappage);
-                       spin_unlock(&info->lock);
-                       copy_highpage(filepage, swappage);
-                       unlock_page(swappage);
-                       page_cache_release(swappage);
-                       flush_dcache_page(filepage);
-                       SetPageUptodate(filepage);
-                       set_page_dirty(filepage);
-                       swap_free(swap);
-               } else if (!(error = add_to_page_cache_locked(swappage, mapping,
-                                       idx, GFP_NOWAIT))) {
-                       info->flags |= SHMEM_PAGEIN;
-                       shmem_swp_set(info, entry, 0);
-                       shmem_swp_unmap(entry);
-                       delete_from_swap_cache(swappage);
-                       spin_unlock(&info->lock);
-                       filepage = swappage;
-                       set_page_dirty(filepage);
-                       swap_free(swap);
-               } else {
-                       shmem_swp_unmap(entry);
+               error = add_to_page_cache_locked(page, mapping,
+                                                idx, GFP_NOWAIT);
+               if (error) {
                        spin_unlock(&info->lock);
                        if (error == -ENOMEM) {
                                /*
@@ -1387,33 +749,36 @@ repeat:
                                 * call memcg's OOM if needed.
                                 */
                                error = mem_cgroup_shmem_charge_fallback(
-                                                               swappage,
-                                                               current->mm,
-                                                               gfp);
+                                               page, current->mm, gfp);
                                if (error) {
-                                       unlock_page(swappage);
-                                       page_cache_release(swappage);
-                                       goto failed;
+                                       unlock_page(page);
+                                       page_cache_release(page);
+                                       goto out;
                                }
                        }
-                       unlock_page(swappage);
-                       page_cache_release(swappage);
+                       unlock_page(page);
+                       page_cache_release(page);
                        goto repeat;
                }
-       } else if (sgp == SGP_READ && !filepage) {
-               shmem_swp_unmap(entry);
-               filepage = find_get_page(mapping, idx);
-               if (filepage &&
-                   (!PageUptodate(filepage) || !trylock_page(filepage))) {
+
+               delete_from_swap_cache(page);
+               shmem_put_swap(info, idx, (swp_entry_t){0});
+               info->swapped--;
+               spin_unlock(&info->lock);
+               set_page_dirty(page);
+               swap_free(swap);
+
+       } else if (sgp == SGP_READ) {
+               page = find_get_page(mapping, idx);
+               if (page && !trylock_page(page)) {
                        spin_unlock(&info->lock);
-                       wait_on_page_locked(filepage);
-                       page_cache_release(filepage);
-                       filepage = NULL;
+                       wait_on_page_locked(page);
+                       page_cache_release(page);
                        goto repeat;
                }
                spin_unlock(&info->lock);
-       } else {
-               shmem_swp_unmap(entry);
+
+       } else if (prealloc_page) {
                sbinfo = SHMEM_SB(inode->i_sb);
                if (sbinfo->max_blocks) {
                        if (percpu_counter_compare(&sbinfo->used_blocks,
@@ -1421,126 +786,76 @@ repeat:
                            shmem_acct_block(info->flags))
                                goto nospace;
                        percpu_counter_inc(&sbinfo->used_blocks);
-                       spin_lock(&inode->i_lock);
                        inode->i_blocks += BLOCKS_PER_PAGE;
-                       spin_unlock(&inode->i_lock);
                } else if (shmem_acct_block(info->flags))
                        goto nospace;
 
-               if (!filepage) {
-                       int ret;
-
-                       if (!prealloc_page) {
-                               spin_unlock(&info->lock);
-                               filepage = shmem_alloc_page(gfp, info, idx);
-                               if (!filepage) {
-                                       shmem_unacct_blocks(info->flags, 1);
-                                       shmem_free_blocks(inode, 1);
-                                       error = -ENOMEM;
-                                       goto failed;
-                               }
-                               SetPageSwapBacked(filepage);
+               page = prealloc_page;
+               prealloc_page = NULL;
 
-                               /*
-                                * Precharge page while we can wait, compensate
-                                * after
-                                */
-                               error = mem_cgroup_cache_charge(filepage,
-                                       current->mm, GFP_KERNEL);
-                               if (error) {
-                                       page_cache_release(filepage);
-                                       shmem_unacct_blocks(info->flags, 1);
-                                       shmem_free_blocks(inode, 1);
-                                       filepage = NULL;
-                                       goto failed;
-                               }
-
-                               spin_lock(&info->lock);
-                       } else {
-                               filepage = prealloc_page;
-                               prealloc_page = NULL;
-                               SetPageSwapBacked(filepage);
-                       }
-
-                       entry = shmem_swp_alloc(info, idx, sgp);
-                       if (IS_ERR(entry))
-                               error = PTR_ERR(entry);
-                       else {
-                               swap = *entry;
-                               shmem_swp_unmap(entry);
-                       }
-                       ret = error || swap.val;
-                       if (ret)
-                               mem_cgroup_uncharge_cache_page(filepage);
-                       else
-                               ret = add_to_page_cache_lru(filepage, mapping,
+               swap = shmem_get_swap(info, idx);
+               if (swap.val)
+                       mem_cgroup_uncharge_cache_page(page);
+               else
+                       error = add_to_page_cache_lru(page, mapping,
                                                idx, GFP_NOWAIT);
-                       /*
-                        * At add_to_page_cache_lru() failure, uncharge will
-                        * be done automatically.
-                        */
-                       if (ret) {
-                               spin_unlock(&info->lock);
-                               page_cache_release(filepage);
-                               shmem_unacct_blocks(info->flags, 1);
-                               shmem_free_blocks(inode, 1);
-                               filepage = NULL;
-                               if (error)
-                                       goto failed;
-                               goto repeat;
-                       }
-                       info->flags |= SHMEM_PAGEIN;
+               /*
+                * At add_to_page_cache_lru() failure,
+                * uncharge will be done automatically.
+                */
+               if (swap.val || error) {
+                       shmem_unacct_blocks(info->flags, 1);
+                       shmem_free_blocks(inode, 1);
+                       spin_unlock(&info->lock);
+                       page_cache_release(page);
+                       goto repeat;
                }
 
                info->alloced++;
                spin_unlock(&info->lock);
-               clear_highpage(filepage);
-               flush_dcache_page(filepage);
-               SetPageUptodate(filepage);
+               clear_highpage(page);
+               flush_dcache_page(page);
+               SetPageUptodate(page);
                if (sgp == SGP_DIRTY)
-                       set_page_dirty(filepage);
+                       set_page_dirty(page);
+
+       } else {
+               spin_unlock(&info->lock);
+               error = -ENOMEM;
+               goto out;
        }
 done:
-       *pagep = filepage;
+       *pagep = page;
        error = 0;
-       goto out;
+out:
+       if (prealloc_page) {
+               mem_cgroup_uncharge_cache_page(prealloc_page);
+               page_cache_release(prealloc_page);
+       }
+       return error;
 
 nospace:
        /*
         * Perhaps the page was brought in from swap between find_lock_page
         * and taking info->lock?  We allow for that at add_to_page_cache_lru,
         * but must also avoid reporting a spurious ENOSPC while working on a
-        * full tmpfs.  (When filepage has been passed in to shmem_getpage, it
-        * is already in page cache, which prevents this race from occurring.)
+        * full tmpfs.
         */
-       if (!filepage) {
-               struct page *page = find_get_page(mapping, idx);
-               if (page) {
-                       spin_unlock(&info->lock);
-                       page_cache_release(page);
-                       goto repeat;
-               }
-       }
+       page = find_get_page(mapping, idx);
        spin_unlock(&info->lock);
-       error = -ENOSPC;
-failed:
-       if (*pagep != filepage) {
-               unlock_page(filepage);
-               page_cache_release(filepage);
-       }
-out:
-       if (prealloc_page) {
-               mem_cgroup_uncharge_cache_page(prealloc_page);
-               page_cache_release(prealloc_page);
+       if (page) {
+               page_cache_release(page);
+               goto repeat;
        }
-       return error;
+       error = -ENOSPC;
+       goto out;
 }
 
 static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 {
        struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
        int error;
-       int ret;
+       int ret = VM_FAULT_LOCKED;
 
        if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
                return VM_FAULT_SIGBUS;
@@ -1548,11 +863,12 @@ static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
        error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
        if (error)
                return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
+
        if (ret & VM_FAULT_MAJOR) {
                count_vm_event(PGMAJFAULT);
                mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
        }
-       return ret | VM_FAULT_LOCKED;
+       return ret;
 }
 
 #ifdef CONFIG_NUMA
@@ -1669,19 +985,6 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inode
 static const struct inode_operations shmem_symlink_inode_operations;
 static const struct inode_operations shmem_symlink_inline_operations;
 
-/*
- * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
- * but providing them allows a tmpfs file to be used for splice, sendfile, and
- * below the loop driver, in the generic fashion that many filesystems support.
- */
-static int shmem_readpage(struct file *file, struct page *page)
-{
-       struct inode *inode = page->mapping->host;
-       int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
-       unlock_page(page);
-       return error;
-}
-
 static int
 shmem_write_begin(struct file *file, struct address_space *mapping,
                        loff_t pos, unsigned len, unsigned flags,
@@ -1689,7 +992,6 @@ shmem_write_begin(struct file *file, struct address_space *mapping,
 {
        struct inode *inode = mapping->host;
        pgoff_t index = pos >> PAGE_CACHE_SHIFT;
-       *pagep = NULL;
        return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
 }
 
@@ -1846,6 +1148,119 @@ static ssize_t shmem_file_aio_read(struct kiocb *iocb,
        return retval;
 }
 
+static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
+                               struct pipe_inode_info *pipe, size_t len,
+                               unsigned int flags)
+{
+       struct address_space *mapping = in->f_mapping;
+       struct inode *inode = mapping->host;
+       unsigned int loff, nr_pages, req_pages;
+       struct page *pages[PIPE_DEF_BUFFERS];
+       struct partial_page partial[PIPE_DEF_BUFFERS];
+       struct page *page;
+       pgoff_t index, end_index;
+       loff_t isize, left;
+       int error, page_nr;
+       struct splice_pipe_desc spd = {
+               .pages = pages,
+               .partial = partial,
+               .flags = flags,
+               .ops = &page_cache_pipe_buf_ops,
+               .spd_release = spd_release_page,
+       };
+
+       isize = i_size_read(inode);
+       if (unlikely(*ppos >= isize))
+               return 0;
+
+       left = isize - *ppos;
+       if (unlikely(left < len))
+               len = left;
+
+       if (splice_grow_spd(pipe, &spd))
+               return -ENOMEM;
+
+       index = *ppos >> PAGE_CACHE_SHIFT;
+       loff = *ppos & ~PAGE_CACHE_MASK;
+       req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
+       nr_pages = min(req_pages, pipe->buffers);
+
+       spd.nr_pages = find_get_pages_contig(mapping, index,
+                                               nr_pages, spd.pages);
+       index += spd.nr_pages;
+       error = 0;
+
+       while (spd.nr_pages < nr_pages) {
+               error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
+               if (error)
+                       break;
+               unlock_page(page);
+               spd.pages[spd.nr_pages++] = page;
+               index++;
+       }
+
+       index = *ppos >> PAGE_CACHE_SHIFT;
+       nr_pages = spd.nr_pages;
+       spd.nr_pages = 0;
+
+       for (page_nr = 0; page_nr < nr_pages; page_nr++) {
+               unsigned int this_len;
+
+               if (!len)
+                       break;
+
+               this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
+               page = spd.pages[page_nr];
+
+               if (!PageUptodate(page) || page->mapping != mapping) {
+                       error = shmem_getpage(inode, index, &page,
+                                                       SGP_CACHE, NULL);
+                       if (error)
+                               break;
+                       unlock_page(page);
+                       page_cache_release(spd.pages[page_nr]);
+                       spd.pages[page_nr] = page;
+               }
+
+               isize = i_size_read(inode);
+               end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
+               if (unlikely(!isize || index > end_index))
+                       break;
+
+               if (end_index == index) {
+                       unsigned int plen;
+
+                       plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
+                       if (plen <= loff)
+                               break;
+
+                       this_len = min(this_len, plen - loff);
+                       len = this_len;
+               }
+
+               spd.partial[page_nr].offset = loff;
+               spd.partial[page_nr].len = this_len;
+               len -= this_len;
+               loff = 0;
+               spd.nr_pages++;
+               index++;
+       }
+
+       while (page_nr < nr_pages)
+               page_cache_release(spd.pages[page_nr++]);
+
+       if (spd.nr_pages)
+               error = splice_to_pipe(pipe, &spd);
+
+       splice_shrink_spd(pipe, &spd);
+
+       if (error > 0) {
+               *ppos += error;
+               file_accessed(in);
+       }
+       return error;
+}
+
 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
        struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
@@ -2006,7 +1421,7 @@ static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *s
        int error;
        int len;
        struct inode *inode;
-       struct page *page = NULL;
+       struct page *page;
        char *kaddr;
        struct shmem_inode_info *info;
 
@@ -2602,7 +2017,7 @@ int shmem_fill_super(struct super_block *sb, void *data, int silent)
                goto failed;
        sbinfo->free_inodes = sbinfo->max_inodes;
 
-       sb->s_maxbytes = SHMEM_MAX_BYTES;
+       sb->s_maxbytes = MAX_LFS_FILESIZE;
        sb->s_blocksize = PAGE_CACHE_SIZE;
        sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
        sb->s_magic = TMPFS_MAGIC;
@@ -2684,7 +2099,6 @@ static const struct address_space_operations shmem_aops = {
        .writepage      = shmem_writepage,
        .set_page_dirty = __set_page_dirty_no_writeback,
 #ifdef CONFIG_TMPFS
-       .readpage       = shmem_readpage,
        .write_begin    = shmem_write_begin,
        .write_end      = shmem_write_end,
 #endif
@@ -2701,7 +2115,7 @@ static const struct file_operations shmem_file_operations = {
        .aio_read       = shmem_file_aio_read,
        .aio_write      = generic_file_aio_write,
        .fsync          = noop_fsync,
-       .splice_read    = generic_file_splice_read,
+       .splice_read    = shmem_file_splice_read,
        .splice_write   = generic_file_splice_write,
 #endif
 };
@@ -2715,10 +2129,6 @@ static const struct inode_operations shmem_inode_operations = {
        .listxattr      = shmem_listxattr,
        .removexattr    = shmem_removexattr,
 #endif
-#ifdef CONFIG_TMPFS_POSIX_ACL
-       .check_acl      = generic_check_acl,
-#endif
-
 };
 
 static const struct inode_operations shmem_dir_inode_operations = {
@@ -2741,7 +2151,6 @@ static const struct inode_operations shmem_dir_inode_operations = {
 #endif
 #ifdef CONFIG_TMPFS_POSIX_ACL
        .setattr        = shmem_setattr,
-       .check_acl      = generic_check_acl,
 #endif
 };
 
@@ -2754,7 +2163,6 @@ static const struct inode_operations shmem_special_inode_operations = {
 #endif
 #ifdef CONFIG_TMPFS_POSIX_ACL
        .setattr        = shmem_setattr,
-       .check_acl      = generic_check_acl,
 #endif
 };
 
@@ -2845,7 +2253,7 @@ out4:
 void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
                                        struct page **pagep, swp_entry_t *ent)
 {
-       swp_entry_t entry = { .val = 0 }, *ptr;
+       swp_entry_t entry = { .val = 0 };
        struct page *page = NULL;
        struct shmem_inode_info *info = SHMEM_I(inode);
 
@@ -2853,16 +2261,13 @@ void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
                goto out;
 
        spin_lock(&info->lock);
-       ptr = shmem_swp_entry(info, pgoff, NULL);
 #ifdef CONFIG_SWAP
-       if (ptr && ptr->val) {
-               entry.val = ptr->val;
+       entry = shmem_get_swap(info, pgoff);
+       if (entry.val)
                page = find_get_page(&swapper_space, entry.val);
-       else
+       else
 #endif
                page = find_get_page(inode->i_mapping, pgoff);
-       if (ptr)
-               shmem_swp_unmap(ptr);
        spin_unlock(&info->lock);
 out:
        *pagep = page;
@@ -2945,7 +2350,6 @@ out:
 #define shmem_get_inode(sb, dir, mode, dev, flags)     ramfs_get_inode(sb, dir, mode, dev)
 #define shmem_acct_size(flags, size)           0
 #define shmem_unacct_size(flags, size)         do {} while (0)
-#define SHMEM_MAX_BYTES                                MAX_LFS_FILESIZE
 
 #endif /* CONFIG_SHMEM */
 
@@ -2969,7 +2373,7 @@ struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags
        if (IS_ERR(shm_mnt))
                return (void *)shm_mnt;
 
-       if (size < 0 || size > SHMEM_MAX_BYTES)
+       if (size < 0 || size > MAX_LFS_FILESIZE)
                return ERR_PTR(-EINVAL);
 
        if (shmem_acct_size(flags, size))
@@ -3048,13 +2452,29 @@ int shmem_zero_setup(struct vm_area_struct *vma)
  * suit tmpfs, since it may have pages in swapcache, and needs to find those
  * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  *
- * Provide a stub for those callers to start using now, then later
- * flesh it out to call shmem_getpage() with additional gfp mask, when
- * shmem_file_splice_read() is added and shmem_readpage() is removed.
+ * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
+ * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  */
 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
                                         pgoff_t index, gfp_t gfp)
 {
+#ifdef CONFIG_SHMEM
+       struct inode *inode = mapping->host;
+       struct page *page;
+       int error;
+
+       BUG_ON(mapping->a_ops != &shmem_aops);
+       error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
+       if (error)
+               page = ERR_PTR(error);
+       else
+               unlock_page(page);
+       return page;
+#else
+       /*
+        * The tiny !SHMEM case uses ramfs without swap
+        */
        return read_cache_page_gfp(mapping, index, gfp);
+#endif
 }
 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);