11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Resizable virtual memory filesystem for Linux. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000 Linus Torvalds. 51da177e4SLinus Torvalds * 2000 Transmeta Corp. 61da177e4SLinus Torvalds * 2000-2001 Christoph Rohland 71da177e4SLinus Torvalds * 2000-2001 SAP AG 81da177e4SLinus Torvalds * 2002 Red Hat Inc. 96922c0c7SHugh Dickins * Copyright (C) 2002-2011 Hugh Dickins. 106922c0c7SHugh Dickins * Copyright (C) 2011 Google Inc. 110edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 121da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Extended attribute support for tmpfs: 151da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 161da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 171da177e4SLinus Torvalds * 18853ac43aSMatt Mackall * tiny-shmem: 19853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 20853ac43aSMatt Mackall * 211da177e4SLinus Torvalds * This file is released under the GPL. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 24853ac43aSMatt Mackall #include <linux/fs.h> 25853ac43aSMatt Mackall #include <linux/init.h> 26853ac43aSMatt Mackall #include <linux/vfs.h> 27853ac43aSMatt Mackall #include <linux/mount.h> 28250297edSAndrew Morton #include <linux/ramfs.h> 29caefba17SHugh Dickins #include <linux/pagemap.h> 30853ac43aSMatt Mackall #include <linux/file.h> 31853ac43aSMatt Mackall #include <linux/mm.h> 32b95f1b31SPaul Gortmaker #include <linux/export.h> 33853ac43aSMatt Mackall #include <linux/swap.h> 34e2e40f2cSChristoph Hellwig #include <linux/uio.h> 35853ac43aSMatt Mackall 36853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 37853ac43aSMatt Mackall 38853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 391da177e4SLinus Torvalds /* 401da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 411da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 421da177e4SLinus Torvalds * which makes it a completely usable filesystem. 431da177e4SLinus Torvalds */ 441da177e4SLinus Torvalds 4539f0247dSAndreas Gruenbacher #include <linux/xattr.h> 46a5694255SChristoph Hellwig #include <linux/exportfs.h> 471c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 48feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 491da177e4SLinus Torvalds #include <linux/mman.h> 501da177e4SLinus Torvalds #include <linux/string.h> 511da177e4SLinus Torvalds #include <linux/slab.h> 521da177e4SLinus Torvalds #include <linux/backing-dev.h> 531da177e4SLinus Torvalds #include <linux/shmem_fs.h> 541da177e4SLinus Torvalds #include <linux/writeback.h> 551da177e4SLinus Torvalds #include <linux/blkdev.h> 56bda97eabSHugh Dickins #include <linux/pagevec.h> 5741ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 5883e4fa9cSHugh Dickins #include <linux/falloc.h> 59708e3508SHugh Dickins #include <linux/splice.h> 601da177e4SLinus Torvalds #include <linux/security.h> 611da177e4SLinus Torvalds #include <linux/swapops.h> 621da177e4SLinus Torvalds #include <linux/mempolicy.h> 631da177e4SLinus Torvalds #include <linux/namei.h> 64b00dc3adSHugh Dickins #include <linux/ctype.h> 65304dbdb7SLee Schermerhorn #include <linux/migrate.h> 66c1f60a5aSChristoph Lameter #include <linux/highmem.h> 67680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 6892562927SMimi Zohar #include <linux/magic.h> 699183df25SDavid Herrmann #include <linux/syscalls.h> 7040e041a2SDavid Herrmann #include <linux/fcntl.h> 719183df25SDavid Herrmann #include <uapi/linux/memfd.h> 72304dbdb7SLee Schermerhorn 731da177e4SLinus Torvalds #include <asm/uaccess.h> 741da177e4SLinus Torvalds #include <asm/pgtable.h> 751da177e4SLinus Torvalds 76dd56b046SMel Gorman #include "internal.h" 77dd56b046SMel Gorman 781da177e4SLinus Torvalds #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) 791da177e4SLinus Torvalds #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 821da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 831da177e4SLinus Torvalds 8469f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 8569f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 8669f07ec9SHugh Dickins 871aac1400SHugh Dickins /* 88f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 89f00cdc6dSHugh Dickins * inode->i_private (with i_mutex making sure that it has only one user at 90f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 911aac1400SHugh Dickins */ 921aac1400SHugh Dickins struct shmem_falloc { 938e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 941aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 951aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 961aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 971aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 981aac1400SHugh Dickins }; 991aac1400SHugh Dickins 100285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */ 1011da177e4SLinus Torvalds enum sgp_type { 1021da177e4SLinus Torvalds SGP_READ, /* don't exceed i_size, don't allocate page */ 1031da177e4SLinus Torvalds SGP_CACHE, /* don't exceed i_size, may allocate page */ 104a0ee5ec5SHugh Dickins SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ 1051635f6a7SHugh Dickins SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */ 1061635f6a7SHugh Dickins SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */ 1071da177e4SLinus Torvalds }; 1081da177e4SLinus Torvalds 109b76db735SAndrew Morton #ifdef CONFIG_TMPFS 110680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 111680d794bSakpm@linux-foundation.org { 112680d794bSakpm@linux-foundation.org return totalram_pages / 2; 113680d794bSakpm@linux-foundation.org } 114680d794bSakpm@linux-foundation.org 115680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 116680d794bSakpm@linux-foundation.org { 117680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 118680d794bSakpm@linux-foundation.org } 119b76db735SAndrew Morton #endif 120680d794bSakpm@linux-foundation.org 121bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp); 122bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 123bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index); 12468da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 12568da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type); 12668da9f05SHugh Dickins 12768da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index, 12868da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, int *fault_type) 12968da9f05SHugh Dickins { 13068da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 13168da9f05SHugh Dickins mapping_gfp_mask(inode->i_mapping), fault_type); 13268da9f05SHugh Dickins } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1351da177e4SLinus Torvalds { 1361da177e4SLinus Torvalds return sb->s_fs_info; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* 1401da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1411da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1421da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1431da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1441da177e4SLinus Torvalds */ 1451da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1461da177e4SLinus Torvalds { 1470b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 148191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1521da177e4SLinus Torvalds { 1530b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1541da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 15777142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 15877142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 15977142517SKonstantin Khlebnikov { 16077142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 16177142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 16277142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 16377142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 16477142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 16577142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 16677142517SKonstantin Khlebnikov } 16777142517SKonstantin Khlebnikov return 0; 16877142517SKonstantin Khlebnikov } 16977142517SKonstantin Khlebnikov 1701da177e4SLinus Torvalds /* 1711da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 1721da177e4SLinus Torvalds * pages are allocated, in order to allow huge sparse files. 1731da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1741da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1751da177e4SLinus Torvalds */ 1761da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags) 1771da177e4SLinus Torvalds { 1780b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 179191c5424SAl Viro security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1831da177e4SLinus Torvalds { 1840b0a0806SHugh Dickins if (flags & VM_NORESERVE) 1851da177e4SLinus Torvalds vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 188759b9775SHugh Dickins static const struct super_operations shmem_ops; 189f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 19015ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 19192e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 19292e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 19392e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 194f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 197cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 1981da177e4SLinus Torvalds 1995b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 2005b04c689SPavel Emelyanov { 2015b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2025b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2035b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2045b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 2055b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2065b04c689SPavel Emelyanov return -ENOSPC; 2075b04c689SPavel Emelyanov } 2085b04c689SPavel Emelyanov sbinfo->free_inodes--; 2095b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2105b04c689SPavel Emelyanov } 2115b04c689SPavel Emelyanov return 0; 2125b04c689SPavel Emelyanov } 2135b04c689SPavel Emelyanov 2145b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2155b04c689SPavel Emelyanov { 2165b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2175b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2185b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2195b04c689SPavel Emelyanov sbinfo->free_inodes++; 2205b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2215b04c689SPavel Emelyanov } 2225b04c689SPavel Emelyanov } 2235b04c689SPavel Emelyanov 22446711810SRandy Dunlap /** 22541ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2261da177e4SLinus Torvalds * @inode: inode to recalc 2271da177e4SLinus Torvalds * 2281da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2291da177e4SLinus Torvalds * undirtied hole pages behind our back. 2301da177e4SLinus Torvalds * 2311da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2321da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2331da177e4SLinus Torvalds * 2341da177e4SLinus Torvalds * It has to be called with the spinlock held. 2351da177e4SLinus Torvalds */ 2361da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2371da177e4SLinus Torvalds { 2381da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2391da177e4SLinus Torvalds long freed; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2421da177e4SLinus Torvalds if (freed > 0) { 24354af6042SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 24454af6042SHugh Dickins if (sbinfo->max_blocks) 24554af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -freed); 2461da177e4SLinus Torvalds info->alloced -= freed; 24754af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2481da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2527a5d0fbbSHugh Dickins /* 2537a5d0fbbSHugh Dickins * Replace item expected in radix tree by a new item, while holding tree lock. 2547a5d0fbbSHugh Dickins */ 2557a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping, 2567a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 2577a5d0fbbSHugh Dickins { 2587a5d0fbbSHugh Dickins void **pslot; 2596dbaf22cSJohannes Weiner void *item; 2607a5d0fbbSHugh Dickins 2617a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 2626dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 2637a5d0fbbSHugh Dickins pslot = radix_tree_lookup_slot(&mapping->page_tree, index); 2646dbaf22cSJohannes Weiner if (!pslot) 2656dbaf22cSJohannes Weiner return -ENOENT; 2666dbaf22cSJohannes Weiner item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock); 2677a5d0fbbSHugh Dickins if (item != expected) 2687a5d0fbbSHugh Dickins return -ENOENT; 2697a5d0fbbSHugh Dickins radix_tree_replace_slot(pslot, replacement); 2707a5d0fbbSHugh Dickins return 0; 2717a5d0fbbSHugh Dickins } 2727a5d0fbbSHugh Dickins 2737a5d0fbbSHugh Dickins /* 274d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 275d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 276d1899228SHugh Dickins * 277d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 278d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 279d1899228SHugh Dickins */ 280d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 281d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 282d1899228SHugh Dickins { 283d1899228SHugh Dickins void *item; 284d1899228SHugh Dickins 285d1899228SHugh Dickins rcu_read_lock(); 286d1899228SHugh Dickins item = radix_tree_lookup(&mapping->page_tree, index); 287d1899228SHugh Dickins rcu_read_unlock(); 288d1899228SHugh Dickins return item == swp_to_radix_entry(swap); 289d1899228SHugh Dickins } 290d1899228SHugh Dickins 291d1899228SHugh Dickins /* 29246f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 29346f65ec1SHugh Dickins */ 29446f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 29546f65ec1SHugh Dickins struct address_space *mapping, 296fed400a1SWang Sheng-Hui pgoff_t index, void *expected) 29746f65ec1SHugh Dickins { 298b065b432SHugh Dickins int error; 29946f65ec1SHugh Dickins 300309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 301309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapBacked(page), page); 30246f65ec1SHugh Dickins 30346f65ec1SHugh Dickins page_cache_get(page); 30446f65ec1SHugh Dickins page->mapping = mapping; 30546f65ec1SHugh Dickins page->index = index; 30646f65ec1SHugh Dickins 30746f65ec1SHugh Dickins spin_lock_irq(&mapping->tree_lock); 30846f65ec1SHugh Dickins if (!expected) 309b065b432SHugh Dickins error = radix_tree_insert(&mapping->page_tree, index, page); 31046f65ec1SHugh Dickins else 311b065b432SHugh Dickins error = shmem_radix_tree_replace(mapping, index, expected, 312b065b432SHugh Dickins page); 31346f65ec1SHugh Dickins if (!error) { 31446f65ec1SHugh Dickins mapping->nrpages++; 31546f65ec1SHugh Dickins __inc_zone_page_state(page, NR_FILE_PAGES); 31646f65ec1SHugh Dickins __inc_zone_page_state(page, NR_SHMEM); 31746f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 31846f65ec1SHugh Dickins } else { 31946f65ec1SHugh Dickins page->mapping = NULL; 32046f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 32146f65ec1SHugh Dickins page_cache_release(page); 32246f65ec1SHugh Dickins } 32346f65ec1SHugh Dickins return error; 32446f65ec1SHugh Dickins } 32546f65ec1SHugh Dickins 32646f65ec1SHugh Dickins /* 3276922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 3286922c0c7SHugh Dickins */ 3296922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 3306922c0c7SHugh Dickins { 3316922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 3326922c0c7SHugh Dickins int error; 3336922c0c7SHugh Dickins 3346922c0c7SHugh Dickins spin_lock_irq(&mapping->tree_lock); 3356922c0c7SHugh Dickins error = shmem_radix_tree_replace(mapping, page->index, page, radswap); 3366922c0c7SHugh Dickins page->mapping = NULL; 3376922c0c7SHugh Dickins mapping->nrpages--; 3386922c0c7SHugh Dickins __dec_zone_page_state(page, NR_FILE_PAGES); 3396922c0c7SHugh Dickins __dec_zone_page_state(page, NR_SHMEM); 3406922c0c7SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 3416922c0c7SHugh Dickins page_cache_release(page); 3426922c0c7SHugh Dickins BUG_ON(error); 3436922c0c7SHugh Dickins } 3446922c0c7SHugh Dickins 3456922c0c7SHugh Dickins /* 3467a5d0fbbSHugh Dickins * Remove swap entry from radix tree, free the swap and its page cache. 3477a5d0fbbSHugh Dickins */ 3487a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 3497a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 3507a5d0fbbSHugh Dickins { 3516dbaf22cSJohannes Weiner void *old; 3527a5d0fbbSHugh Dickins 3537a5d0fbbSHugh Dickins spin_lock_irq(&mapping->tree_lock); 3546dbaf22cSJohannes Weiner old = radix_tree_delete_item(&mapping->page_tree, index, radswap); 3557a5d0fbbSHugh Dickins spin_unlock_irq(&mapping->tree_lock); 3566dbaf22cSJohannes Weiner if (old != radswap) 3576dbaf22cSJohannes Weiner return -ENOENT; 3587a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 3596dbaf22cSJohannes Weiner return 0; 3607a5d0fbbSHugh Dickins } 3617a5d0fbbSHugh Dickins 3627a5d0fbbSHugh Dickins /* 36324513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 36424513264SHugh Dickins */ 36524513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 36624513264SHugh Dickins { 36724513264SHugh Dickins struct pagevec pvec; 36824513264SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 36924513264SHugh Dickins pgoff_t index = 0; 37024513264SHugh Dickins 37124513264SHugh Dickins pagevec_init(&pvec, 0); 37224513264SHugh Dickins /* 37324513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 37424513264SHugh Dickins */ 37524513264SHugh Dickins while (!mapping_unevictable(mapping)) { 37624513264SHugh Dickins /* 37724513264SHugh Dickins * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it 37824513264SHugh Dickins * has finished, if it hits a row of PAGEVEC_SIZE swap entries. 37924513264SHugh Dickins */ 3800cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 38124513264SHugh Dickins PAGEVEC_SIZE, pvec.pages, indices); 38224513264SHugh Dickins if (!pvec.nr) 38324513264SHugh Dickins break; 38424513264SHugh Dickins index = indices[pvec.nr - 1] + 1; 3850cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 38624513264SHugh Dickins check_move_unevictable_pages(pvec.pages, pvec.nr); 38724513264SHugh Dickins pagevec_release(&pvec); 38824513264SHugh Dickins cond_resched(); 38924513264SHugh Dickins } 3907a5d0fbbSHugh Dickins } 3917a5d0fbbSHugh Dickins 3927a5d0fbbSHugh Dickins /* 3937a5d0fbbSHugh Dickins * Remove range of pages and swap entries from radix tree, and free them. 3941635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 3957a5d0fbbSHugh Dickins */ 3961635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 3971635f6a7SHugh Dickins bool unfalloc) 3981da177e4SLinus Torvalds { 399285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 4001da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 401285b2c4fSHugh Dickins pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 40283e4fa9cSHugh Dickins pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT; 40383e4fa9cSHugh Dickins unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1); 40483e4fa9cSHugh Dickins unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1); 405bda97eabSHugh Dickins struct pagevec pvec; 4067a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 4077a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 408285b2c4fSHugh Dickins pgoff_t index; 409bda97eabSHugh Dickins int i; 4101da177e4SLinus Torvalds 41183e4fa9cSHugh Dickins if (lend == -1) 41283e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 413bda97eabSHugh Dickins 414bda97eabSHugh Dickins pagevec_init(&pvec, 0); 415bda97eabSHugh Dickins index = start; 41683e4fa9cSHugh Dickins while (index < end) { 4170cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 41883e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 4197a5d0fbbSHugh Dickins pvec.pages, indices); 4207a5d0fbbSHugh Dickins if (!pvec.nr) 4217a5d0fbbSHugh Dickins break; 422bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 423bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 424bda97eabSHugh Dickins 4257a5d0fbbSHugh Dickins index = indices[i]; 42683e4fa9cSHugh Dickins if (index >= end) 427bda97eabSHugh Dickins break; 428bda97eabSHugh Dickins 4297a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 4301635f6a7SHugh Dickins if (unfalloc) 4311635f6a7SHugh Dickins continue; 4327a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 4337a5d0fbbSHugh Dickins index, page); 4347a5d0fbbSHugh Dickins continue; 4357a5d0fbbSHugh Dickins } 4367a5d0fbbSHugh Dickins 437bda97eabSHugh Dickins if (!trylock_page(page)) 438bda97eabSHugh Dickins continue; 4391635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 4407a5d0fbbSHugh Dickins if (page->mapping == mapping) { 441309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 442bda97eabSHugh Dickins truncate_inode_page(mapping, page); 4437a5d0fbbSHugh Dickins } 4441635f6a7SHugh Dickins } 445bda97eabSHugh Dickins unlock_page(page); 446bda97eabSHugh Dickins } 4470cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 44824513264SHugh Dickins pagevec_release(&pvec); 449bda97eabSHugh Dickins cond_resched(); 450bda97eabSHugh Dickins index++; 451bda97eabSHugh Dickins } 452bda97eabSHugh Dickins 45383e4fa9cSHugh Dickins if (partial_start) { 454bda97eabSHugh Dickins struct page *page = NULL; 455bda97eabSHugh Dickins shmem_getpage(inode, start - 1, &page, SGP_READ, NULL); 456bda97eabSHugh Dickins if (page) { 45783e4fa9cSHugh Dickins unsigned int top = PAGE_CACHE_SIZE; 45883e4fa9cSHugh Dickins if (start > end) { 45983e4fa9cSHugh Dickins top = partial_end; 46083e4fa9cSHugh Dickins partial_end = 0; 46183e4fa9cSHugh Dickins } 46283e4fa9cSHugh Dickins zero_user_segment(page, partial_start, top); 463bda97eabSHugh Dickins set_page_dirty(page); 464bda97eabSHugh Dickins unlock_page(page); 465bda97eabSHugh Dickins page_cache_release(page); 466bda97eabSHugh Dickins } 467bda97eabSHugh Dickins } 46883e4fa9cSHugh Dickins if (partial_end) { 46983e4fa9cSHugh Dickins struct page *page = NULL; 47083e4fa9cSHugh Dickins shmem_getpage(inode, end, &page, SGP_READ, NULL); 47183e4fa9cSHugh Dickins if (page) { 47283e4fa9cSHugh Dickins zero_user_segment(page, 0, partial_end); 47383e4fa9cSHugh Dickins set_page_dirty(page); 47483e4fa9cSHugh Dickins unlock_page(page); 47583e4fa9cSHugh Dickins page_cache_release(page); 47683e4fa9cSHugh Dickins } 47783e4fa9cSHugh Dickins } 47883e4fa9cSHugh Dickins if (start >= end) 47983e4fa9cSHugh Dickins return; 480bda97eabSHugh Dickins 481bda97eabSHugh Dickins index = start; 482b1a36650SHugh Dickins while (index < end) { 483bda97eabSHugh Dickins cond_resched(); 4840cd6144aSJohannes Weiner 4850cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 48683e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 4877a5d0fbbSHugh Dickins pvec.pages, indices); 4887a5d0fbbSHugh Dickins if (!pvec.nr) { 489b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 490b1a36650SHugh Dickins if (index == start || end != -1) 491bda97eabSHugh Dickins break; 492b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 493bda97eabSHugh Dickins index = start; 494bda97eabSHugh Dickins continue; 495bda97eabSHugh Dickins } 496bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 497bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 498bda97eabSHugh Dickins 4997a5d0fbbSHugh Dickins index = indices[i]; 50083e4fa9cSHugh Dickins if (index >= end) 501bda97eabSHugh Dickins break; 502bda97eabSHugh Dickins 5037a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 5041635f6a7SHugh Dickins if (unfalloc) 5051635f6a7SHugh Dickins continue; 506b1a36650SHugh Dickins if (shmem_free_swap(mapping, index, page)) { 507b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 508b1a36650SHugh Dickins index--; 509b1a36650SHugh Dickins break; 510b1a36650SHugh Dickins } 511b1a36650SHugh Dickins nr_swaps_freed++; 5127a5d0fbbSHugh Dickins continue; 5137a5d0fbbSHugh Dickins } 5147a5d0fbbSHugh Dickins 515bda97eabSHugh Dickins lock_page(page); 5161635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 5177a5d0fbbSHugh Dickins if (page->mapping == mapping) { 518309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 519bda97eabSHugh Dickins truncate_inode_page(mapping, page); 520b1a36650SHugh Dickins } else { 521b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 522b1a36650SHugh Dickins unlock_page(page); 523b1a36650SHugh Dickins index--; 524b1a36650SHugh Dickins break; 5257a5d0fbbSHugh Dickins } 5261635f6a7SHugh Dickins } 527bda97eabSHugh Dickins unlock_page(page); 528bda97eabSHugh Dickins } 5290cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 53024513264SHugh Dickins pagevec_release(&pvec); 531bda97eabSHugh Dickins index++; 532bda97eabSHugh Dickins } 53394c1e62dSHugh Dickins 5341da177e4SLinus Torvalds spin_lock(&info->lock); 5357a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 5361da177e4SLinus Torvalds shmem_recalc_inode(inode); 5371da177e4SLinus Torvalds spin_unlock(&info->lock); 5381635f6a7SHugh Dickins } 5391da177e4SLinus Torvalds 5401635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 5411635f6a7SHugh Dickins { 5421635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 543285b2c4fSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 5441da177e4SLinus Torvalds } 54594c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 5461da177e4SLinus Torvalds 54744a30220SYu Zhao static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry, 54844a30220SYu Zhao struct kstat *stat) 54944a30220SYu Zhao { 55044a30220SYu Zhao struct inode *inode = dentry->d_inode; 55144a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 55244a30220SYu Zhao 553d0424c42SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) { 55444a30220SYu Zhao spin_lock(&info->lock); 55544a30220SYu Zhao shmem_recalc_inode(inode); 55644a30220SYu Zhao spin_unlock(&info->lock); 557d0424c42SHugh Dickins } 55844a30220SYu Zhao generic_fillattr(inode, stat); 55944a30220SYu Zhao return 0; 56044a30220SYu Zhao } 56144a30220SYu Zhao 56294c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 5631da177e4SLinus Torvalds { 56475c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 56540e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 5661da177e4SLinus Torvalds int error; 5671da177e4SLinus Torvalds 568db78b877SChristoph Hellwig error = inode_change_ok(inode, attr); 569db78b877SChristoph Hellwig if (error) 570db78b877SChristoph Hellwig return error; 571db78b877SChristoph Hellwig 57294c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 57394c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 57494c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 5753889e6e7Snpiggin@suse.de 57640e041a2SDavid Herrmann /* protected by i_mutex */ 57740e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 57840e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 57940e041a2SDavid Herrmann return -EPERM; 58040e041a2SDavid Herrmann 58194c1e62dSHugh Dickins if (newsize != oldsize) { 58277142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 58377142517SKonstantin Khlebnikov oldsize, newsize); 58477142517SKonstantin Khlebnikov if (error) 58577142517SKonstantin Khlebnikov return error; 58694c1e62dSHugh Dickins i_size_write(inode, newsize); 58794c1e62dSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 58894c1e62dSHugh Dickins } 589afa2db2fSJosef Bacik if (newsize <= oldsize) { 59094c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 591d0424c42SHugh Dickins if (oldsize > holebegin) 592d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 593d0424c42SHugh Dickins holebegin, 0, 1); 594d0424c42SHugh Dickins if (info->alloced) 595d0424c42SHugh Dickins shmem_truncate_range(inode, 596d0424c42SHugh Dickins newsize, (loff_t)-1); 59794c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 598d0424c42SHugh Dickins if (oldsize > holebegin) 599d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 600d0424c42SHugh Dickins holebegin, 0, 1); 60194c1e62dSHugh Dickins } 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds 6046a1a90adSChristoph Hellwig setattr_copy(inode, attr); 605db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 606feda821eSChristoph Hellwig error = posix_acl_chmod(inode, inode->i_mode); 6071da177e4SLinus Torvalds return error; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 6101f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 6111da177e4SLinus Torvalds { 6121da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 6131da177e4SLinus Torvalds 6143889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 6151da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 6161da177e4SLinus Torvalds inode->i_size = 0; 6173889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 6181da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 619cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 6201da177e4SLinus Torvalds list_del_init(&info->swaplist); 621cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 6221da177e4SLinus Torvalds } 62369f07ec9SHugh Dickins } else 62469f07ec9SHugh Dickins kfree(info->symlink); 625b09e0fa4SEric Paris 62638f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 6270f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 6285b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 629dbd5768fSJan Kara clear_inode(inode); 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 63246f65ec1SHugh Dickins /* 63346f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 63446f65ec1SHugh Dickins */ 63541ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 636bde05d1cSHugh Dickins swp_entry_t swap, struct page **pagep) 6371da177e4SLinus Torvalds { 638285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 63946f65ec1SHugh Dickins void *radswap; 64041ffe5d5SHugh Dickins pgoff_t index; 641bde05d1cSHugh Dickins gfp_t gfp; 642bde05d1cSHugh Dickins int error = 0; 6431da177e4SLinus Torvalds 64446f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 645e504f3fdSHugh Dickins index = radix_tree_locate_item(&mapping->page_tree, radswap); 64646f65ec1SHugh Dickins if (index == -1) 64700501b53SJohannes Weiner return -EAGAIN; /* tell shmem_unuse we found nothing */ 6482e0e26c7SHugh Dickins 6491b1b32f2SHugh Dickins /* 6501b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 6511f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 6521b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 653285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 6541b1b32f2SHugh Dickins */ 6551b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 6562e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 6572e0e26c7SHugh Dickins 658bde05d1cSHugh Dickins gfp = mapping_gfp_mask(mapping); 659bde05d1cSHugh Dickins if (shmem_should_replace_page(*pagep, gfp)) { 660bde05d1cSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 661bde05d1cSHugh Dickins error = shmem_replace_page(pagep, gfp, info, index); 662bde05d1cSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 663bde05d1cSHugh Dickins /* 664bde05d1cSHugh Dickins * We needed to drop mutex to make that restrictive page 6650142ef6cSHugh Dickins * allocation, but the inode might have been freed while we 6660142ef6cSHugh Dickins * dropped it: although a racing shmem_evict_inode() cannot 6670142ef6cSHugh Dickins * complete without emptying the radix_tree, our page lock 6680142ef6cSHugh Dickins * on this swapcache page is not enough to prevent that - 6690142ef6cSHugh Dickins * free_swap_and_cache() of our swap entry will only 6700142ef6cSHugh Dickins * trylock_page(), removing swap from radix_tree whatever. 6710142ef6cSHugh Dickins * 6720142ef6cSHugh Dickins * We must not proceed to shmem_add_to_page_cache() if the 6730142ef6cSHugh Dickins * inode has been freed, but of course we cannot rely on 6740142ef6cSHugh Dickins * inode or mapping or info to check that. However, we can 6750142ef6cSHugh Dickins * safely check if our swap entry is still in use (and here 6760142ef6cSHugh Dickins * it can't have got reused for another page): if it's still 6770142ef6cSHugh Dickins * in use, then the inode cannot have been freed yet, and we 6780142ef6cSHugh Dickins * can safely proceed (if it's no longer in use, that tells 6790142ef6cSHugh Dickins * nothing about the inode, but we don't need to unuse swap). 680bde05d1cSHugh Dickins */ 681bde05d1cSHugh Dickins if (!page_swapcount(*pagep)) 682bde05d1cSHugh Dickins error = -ENOENT; 683bde05d1cSHugh Dickins } 684bde05d1cSHugh Dickins 685d13d1443SKAMEZAWA Hiroyuki /* 686778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 687778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 688778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 689d13d1443SKAMEZAWA Hiroyuki */ 690bde05d1cSHugh Dickins if (!error) 691bde05d1cSHugh Dickins error = shmem_add_to_page_cache(*pagep, mapping, index, 692fed400a1SWang Sheng-Hui radswap); 69348f170fbSHugh Dickins if (error != -ENOMEM) { 69446f65ec1SHugh Dickins /* 69546f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 69646f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 69746f65ec1SHugh Dickins */ 698bde05d1cSHugh Dickins delete_from_swap_cache(*pagep); 699bde05d1cSHugh Dickins set_page_dirty(*pagep); 70046f65ec1SHugh Dickins if (!error) { 70146f65ec1SHugh Dickins spin_lock(&info->lock); 702285b2c4fSHugh Dickins info->swapped--; 70346f65ec1SHugh Dickins spin_unlock(&info->lock); 70441ffe5d5SHugh Dickins swap_free(swap); 70546f65ec1SHugh Dickins } 7061da177e4SLinus Torvalds } 7072e0e26c7SHugh Dickins return error; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds /* 71146f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 7121da177e4SLinus Torvalds */ 71341ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 7141da177e4SLinus Torvalds { 71541ffe5d5SHugh Dickins struct list_head *this, *next; 7161da177e4SLinus Torvalds struct shmem_inode_info *info; 71700501b53SJohannes Weiner struct mem_cgroup *memcg; 718bde05d1cSHugh Dickins int error = 0; 719bde05d1cSHugh Dickins 720bde05d1cSHugh Dickins /* 721bde05d1cSHugh Dickins * There's a faint possibility that swap page was replaced before 7220142ef6cSHugh Dickins * caller locked it: caller will come back later with the right page. 723bde05d1cSHugh Dickins */ 7240142ef6cSHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) 725bde05d1cSHugh Dickins goto out; 726778dd893SHugh Dickins 727778dd893SHugh Dickins /* 728778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 729778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 730778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 731778dd893SHugh Dickins */ 73200501b53SJohannes Weiner error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg); 733778dd893SHugh Dickins if (error) 734778dd893SHugh Dickins goto out; 73546f65ec1SHugh Dickins /* No radix_tree_preload: swap entry keeps a place for page in tree */ 73600501b53SJohannes Weiner error = -EAGAIN; 7371da177e4SLinus Torvalds 738cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 73941ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 74041ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 741285b2c4fSHugh Dickins if (info->swapped) 74200501b53SJohannes Weiner error = shmem_unuse_inode(info, swap, &page); 7436922c0c7SHugh Dickins else 7446922c0c7SHugh Dickins list_del_init(&info->swaplist); 745cb5f7b9aSHugh Dickins cond_resched(); 74600501b53SJohannes Weiner if (error != -EAGAIN) 747778dd893SHugh Dickins break; 74800501b53SJohannes Weiner /* found nothing in this: move on to search the next */ 7491da177e4SLinus Torvalds } 750cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 751778dd893SHugh Dickins 75200501b53SJohannes Weiner if (error) { 75300501b53SJohannes Weiner if (error != -ENOMEM) 75400501b53SJohannes Weiner error = 0; 75500501b53SJohannes Weiner mem_cgroup_cancel_charge(page, memcg); 75600501b53SJohannes Weiner } else 75700501b53SJohannes Weiner mem_cgroup_commit_charge(page, memcg, true); 758778dd893SHugh Dickins out: 759aaa46865SHugh Dickins unlock_page(page); 760aaa46865SHugh Dickins page_cache_release(page); 761778dd893SHugh Dickins return error; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds /* 7651da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 7661da177e4SLinus Torvalds */ 7671da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 7681da177e4SLinus Torvalds { 7691da177e4SLinus Torvalds struct shmem_inode_info *info; 7701da177e4SLinus Torvalds struct address_space *mapping; 7711da177e4SLinus Torvalds struct inode *inode; 7726922c0c7SHugh Dickins swp_entry_t swap; 7736922c0c7SHugh Dickins pgoff_t index; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 7761da177e4SLinus Torvalds mapping = page->mapping; 7771da177e4SLinus Torvalds index = page->index; 7781da177e4SLinus Torvalds inode = mapping->host; 7791da177e4SLinus Torvalds info = SHMEM_I(inode); 7801da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 7811da177e4SLinus Torvalds goto redirty; 782d9fe526aSHugh Dickins if (!total_swap_pages) 7831da177e4SLinus Torvalds goto redirty; 7841da177e4SLinus Torvalds 785d9fe526aSHugh Dickins /* 78697b713baSChristoph Hellwig * Our capabilities prevent regular writeback or sync from ever calling 78797b713baSChristoph Hellwig * shmem_writepage; but a stacking filesystem might use ->writepage of 78897b713baSChristoph Hellwig * its underlying filesystem, in which case tmpfs should write out to 78997b713baSChristoph Hellwig * swap only in response to memory pressure, and not for the writeback 79097b713baSChristoph Hellwig * threads or sync. 791d9fe526aSHugh Dickins */ 79248f170fbSHugh Dickins if (!wbc->for_reclaim) { 79348f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 79448f170fbSHugh Dickins goto redirty; 79548f170fbSHugh Dickins } 7961635f6a7SHugh Dickins 7971635f6a7SHugh Dickins /* 7981635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 7991635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 8001635f6a7SHugh Dickins * fallocated page arriving here is now to initialize it and write it. 8011aac1400SHugh Dickins * 8021aac1400SHugh Dickins * That's okay for a page already fallocated earlier, but if we have 8031aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 8041aac1400SHugh Dickins * of this page in case we have to undo it, and (b) it may not be a 8051aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 8061aac1400SHugh Dickins * reactivate the page, and let shmem_fallocate() quit when too many. 8071635f6a7SHugh Dickins */ 8081635f6a7SHugh Dickins if (!PageUptodate(page)) { 8091aac1400SHugh Dickins if (inode->i_private) { 8101aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 8111aac1400SHugh Dickins spin_lock(&inode->i_lock); 8121aac1400SHugh Dickins shmem_falloc = inode->i_private; 8131aac1400SHugh Dickins if (shmem_falloc && 8148e205f77SHugh Dickins !shmem_falloc->waitq && 8151aac1400SHugh Dickins index >= shmem_falloc->start && 8161aac1400SHugh Dickins index < shmem_falloc->next) 8171aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 8181aac1400SHugh Dickins else 8191aac1400SHugh Dickins shmem_falloc = NULL; 8201aac1400SHugh Dickins spin_unlock(&inode->i_lock); 8211aac1400SHugh Dickins if (shmem_falloc) 8221aac1400SHugh Dickins goto redirty; 8231aac1400SHugh Dickins } 8241635f6a7SHugh Dickins clear_highpage(page); 8251635f6a7SHugh Dickins flush_dcache_page(page); 8261635f6a7SHugh Dickins SetPageUptodate(page); 8271635f6a7SHugh Dickins } 8281635f6a7SHugh Dickins 829d9fe526aSHugh Dickins swap = get_swap_page(); 83048f170fbSHugh Dickins if (!swap.val) 83148f170fbSHugh Dickins goto redirty; 832d9fe526aSHugh Dickins 833b1dea800SHugh Dickins /* 834b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 8356922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 8366922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 837b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 8386922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 8396922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 840b1dea800SHugh Dickins */ 841b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 84205bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 84305bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 844b1dea800SHugh Dickins 84548f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 846267a4c76SHugh Dickins spin_lock(&info->lock); 847267a4c76SHugh Dickins shmem_recalc_inode(inode); 848267a4c76SHugh Dickins info->swapped++; 849267a4c76SHugh Dickins spin_unlock(&info->lock); 850267a4c76SHugh Dickins 851aaa46865SHugh Dickins swap_shmem_alloc(swap); 8526922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 8536922c0c7SHugh Dickins 8546922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 855d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 8569fab5619SHugh Dickins swap_writepage(page, wbc); 8571da177e4SLinus Torvalds return 0; 8581da177e4SLinus Torvalds } 8591da177e4SLinus Torvalds 8606922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 8610a31bc97SJohannes Weiner swapcache_free(swap); 8621da177e4SLinus Torvalds redirty: 8631da177e4SLinus Torvalds set_page_dirty(page); 864d9fe526aSHugh Dickins if (wbc->for_reclaim) 865d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 866d9fe526aSHugh Dickins unlock_page(page); 867d9fe526aSHugh Dickins return 0; 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds #ifdef CONFIG_NUMA 871680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 87271fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 873680d794bSakpm@linux-foundation.org { 874680d794bSakpm@linux-foundation.org char buffer[64]; 875680d794bSakpm@linux-foundation.org 87671fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 877095f1fc4SLee Schermerhorn return; /* show nothing */ 878095f1fc4SLee Schermerhorn 879a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 880095f1fc4SLee Schermerhorn 881095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 882680d794bSakpm@linux-foundation.org } 88371fe804bSLee Schermerhorn 88471fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 88571fe804bSLee Schermerhorn { 88671fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 88771fe804bSLee Schermerhorn if (sbinfo->mpol) { 88871fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 88971fe804bSLee Schermerhorn mpol = sbinfo->mpol; 89071fe804bSLee Schermerhorn mpol_get(mpol); 89171fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 89271fe804bSLee Schermerhorn } 89371fe804bSLee Schermerhorn return mpol; 89471fe804bSLee Schermerhorn } 895680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 896680d794bSakpm@linux-foundation.org 89741ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 89841ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 8991da177e4SLinus Torvalds { 9001da177e4SLinus Torvalds struct vm_area_struct pvma; 90118a2f371SMel Gorman struct page *page; 9021da177e4SLinus Torvalds 903c4cc6d07SHugh Dickins /* Create a pseudo vma that just contains the policy */ 904c4cc6d07SHugh Dickins pvma.vm_start = 0; 90509c231cbSNathan Zimmer /* Bias interleave by inode number to distribute better across nodes */ 90609c231cbSNathan Zimmer pvma.vm_pgoff = index + info->vfs_inode.i_ino; 907c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 90841ffe5d5SHugh Dickins pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 90952cd3b07SLee Schermerhorn 91018a2f371SMel Gorman page = swapin_readahead(swap, gfp, &pvma, 0); 91118a2f371SMel Gorman 91218a2f371SMel Gorman /* Drop reference taken by mpol_shared_policy_lookup() */ 91318a2f371SMel Gorman mpol_cond_put(pvma.vm_policy); 91418a2f371SMel Gorman 91518a2f371SMel Gorman return page; 91618a2f371SMel Gorman } 91718a2f371SMel Gorman 91818a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp, 91918a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 92018a2f371SMel Gorman { 92118a2f371SMel Gorman struct vm_area_struct pvma; 92218a2f371SMel Gorman struct page *page; 92318a2f371SMel Gorman 92418a2f371SMel Gorman /* Create a pseudo vma that just contains the policy */ 92518a2f371SMel Gorman pvma.vm_start = 0; 92618a2f371SMel Gorman /* Bias interleave by inode number to distribute better across nodes */ 92718a2f371SMel Gorman pvma.vm_pgoff = index + info->vfs_inode.i_ino; 92818a2f371SMel Gorman pvma.vm_ops = NULL; 92918a2f371SMel Gorman pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 93018a2f371SMel Gorman 93118a2f371SMel Gorman page = alloc_page_vma(gfp, &pvma, 0); 93218a2f371SMel Gorman 93318a2f371SMel Gorman /* Drop reference taken by mpol_shared_policy_lookup() */ 93418a2f371SMel Gorman mpol_cond_put(pvma.vm_policy); 93518a2f371SMel Gorman 93618a2f371SMel Gorman return page; 9371da177e4SLinus Torvalds } 938680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */ 939680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 94041ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 941680d794bSakpm@linux-foundation.org { 942680d794bSakpm@linux-foundation.org } 943680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 944680d794bSakpm@linux-foundation.org 94541ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 94641ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 9471da177e4SLinus Torvalds { 94841ffe5d5SHugh Dickins return swapin_readahead(swap, gfp, NULL, 0); 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 95102098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp, 95241ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 9531da177e4SLinus Torvalds { 954e84e2e13SHugh Dickins return alloc_page(gfp); 9551da177e4SLinus Torvalds } 956680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */ 9571da177e4SLinus Torvalds 95871fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) 95971fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 96071fe804bSLee Schermerhorn { 96171fe804bSLee Schermerhorn return NULL; 96271fe804bSLee Schermerhorn } 96371fe804bSLee Schermerhorn #endif 96471fe804bSLee Schermerhorn 9651da177e4SLinus Torvalds /* 966bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 967bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 968bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 969bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 970bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 971bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 972bde05d1cSHugh Dickins * 973bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 974bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 975bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 976bde05d1cSHugh Dickins */ 977bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp) 978bde05d1cSHugh Dickins { 979bde05d1cSHugh Dickins return page_zonenum(page) > gfp_zone(gfp); 980bde05d1cSHugh Dickins } 981bde05d1cSHugh Dickins 982bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 983bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 984bde05d1cSHugh Dickins { 985bde05d1cSHugh Dickins struct page *oldpage, *newpage; 986bde05d1cSHugh Dickins struct address_space *swap_mapping; 987bde05d1cSHugh Dickins pgoff_t swap_index; 988bde05d1cSHugh Dickins int error; 989bde05d1cSHugh Dickins 990bde05d1cSHugh Dickins oldpage = *pagep; 991bde05d1cSHugh Dickins swap_index = page_private(oldpage); 992bde05d1cSHugh Dickins swap_mapping = page_mapping(oldpage); 993bde05d1cSHugh Dickins 994bde05d1cSHugh Dickins /* 995bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 996bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 997bde05d1cSHugh Dickins */ 998bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 999bde05d1cSHugh Dickins newpage = shmem_alloc_page(gfp, info, index); 1000bde05d1cSHugh Dickins if (!newpage) 1001bde05d1cSHugh Dickins return -ENOMEM; 1002bde05d1cSHugh Dickins 1003bde05d1cSHugh Dickins page_cache_get(newpage); 1004bde05d1cSHugh Dickins copy_highpage(newpage, oldpage); 10050142ef6cSHugh Dickins flush_dcache_page(newpage); 1006bde05d1cSHugh Dickins 1007bde05d1cSHugh Dickins __set_page_locked(newpage); 1008bde05d1cSHugh Dickins SetPageUptodate(newpage); 1009bde05d1cSHugh Dickins SetPageSwapBacked(newpage); 1010bde05d1cSHugh Dickins set_page_private(newpage, swap_index); 1011bde05d1cSHugh Dickins SetPageSwapCache(newpage); 1012bde05d1cSHugh Dickins 1013bde05d1cSHugh Dickins /* 1014bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1015bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1016bde05d1cSHugh Dickins */ 1017bde05d1cSHugh Dickins spin_lock_irq(&swap_mapping->tree_lock); 1018bde05d1cSHugh Dickins error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage, 1019bde05d1cSHugh Dickins newpage); 10200142ef6cSHugh Dickins if (!error) { 1021bde05d1cSHugh Dickins __inc_zone_page_state(newpage, NR_FILE_PAGES); 1022bde05d1cSHugh Dickins __dec_zone_page_state(oldpage, NR_FILE_PAGES); 10230142ef6cSHugh Dickins } 1024bde05d1cSHugh Dickins spin_unlock_irq(&swap_mapping->tree_lock); 1025bde05d1cSHugh Dickins 10260142ef6cSHugh Dickins if (unlikely(error)) { 10270142ef6cSHugh Dickins /* 10280142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 10290142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 10300142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 10310142ef6cSHugh Dickins */ 10320142ef6cSHugh Dickins oldpage = newpage; 10330142ef6cSHugh Dickins } else { 103445637babSHugh Dickins mem_cgroup_replace_page(oldpage, newpage); 1035bde05d1cSHugh Dickins lru_cache_add_anon(newpage); 10360142ef6cSHugh Dickins *pagep = newpage; 10370142ef6cSHugh Dickins } 1038bde05d1cSHugh Dickins 1039bde05d1cSHugh Dickins ClearPageSwapCache(oldpage); 1040bde05d1cSHugh Dickins set_page_private(oldpage, 0); 1041bde05d1cSHugh Dickins 1042bde05d1cSHugh Dickins unlock_page(oldpage); 1043bde05d1cSHugh Dickins page_cache_release(oldpage); 1044bde05d1cSHugh Dickins page_cache_release(oldpage); 10450142ef6cSHugh Dickins return error; 1046bde05d1cSHugh Dickins } 1047bde05d1cSHugh Dickins 1048bde05d1cSHugh Dickins /* 104968da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 10501da177e4SLinus Torvalds * 10511da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 10521da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 10531da177e4SLinus Torvalds * entry since a page cannot live in both the swap and page cache 10541da177e4SLinus Torvalds */ 105541ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 105668da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) 10571da177e4SLinus Torvalds { 10581da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 105954af6042SHugh Dickins struct shmem_inode_info *info; 10601da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 106100501b53SJohannes Weiner struct mem_cgroup *memcg; 106227ab7006SHugh Dickins struct page *page; 10631da177e4SLinus Torvalds swp_entry_t swap; 10641da177e4SLinus Torvalds int error; 106554af6042SHugh Dickins int once = 0; 10661635f6a7SHugh Dickins int alloced = 0; 10671da177e4SLinus Torvalds 106841ffe5d5SHugh Dickins if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT)) 10691da177e4SLinus Torvalds return -EFBIG; 10701da177e4SLinus Torvalds repeat: 107154af6042SHugh Dickins swap.val = 0; 10720cd6144aSJohannes Weiner page = find_lock_entry(mapping, index); 107354af6042SHugh Dickins if (radix_tree_exceptional_entry(page)) { 107454af6042SHugh Dickins swap = radix_to_swp_entry(page); 107554af6042SHugh Dickins page = NULL; 107654af6042SHugh Dickins } 107754af6042SHugh Dickins 10781635f6a7SHugh Dickins if (sgp != SGP_WRITE && sgp != SGP_FALLOC && 107954af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 108054af6042SHugh Dickins error = -EINVAL; 1081267a4c76SHugh Dickins goto unlock; 108254af6042SHugh Dickins } 108354af6042SHugh Dickins 108466d2f4d2SHugh Dickins if (page && sgp == SGP_WRITE) 108566d2f4d2SHugh Dickins mark_page_accessed(page); 108666d2f4d2SHugh Dickins 10871635f6a7SHugh Dickins /* fallocated page? */ 10881635f6a7SHugh Dickins if (page && !PageUptodate(page)) { 10891635f6a7SHugh Dickins if (sgp != SGP_READ) 10901635f6a7SHugh Dickins goto clear; 10911635f6a7SHugh Dickins unlock_page(page); 10921635f6a7SHugh Dickins page_cache_release(page); 10931635f6a7SHugh Dickins page = NULL; 10941635f6a7SHugh Dickins } 109554af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 109654af6042SHugh Dickins *pagep = page; 109754af6042SHugh Dickins return 0; 109827ab7006SHugh Dickins } 109927ab7006SHugh Dickins 1100b409f9fcSHugh Dickins /* 110154af6042SHugh Dickins * Fast cache lookup did not find it: 110254af6042SHugh Dickins * bring it back from swap or allocate. 1103b409f9fcSHugh Dickins */ 110454af6042SHugh Dickins info = SHMEM_I(inode); 110554af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 110627ab7006SHugh Dickins 11071da177e4SLinus Torvalds if (swap.val) { 11081da177e4SLinus Torvalds /* Look it up and read it in.. */ 110927ab7006SHugh Dickins page = lookup_swap_cache(swap); 111027ab7006SHugh Dickins if (!page) { 1111456f998eSYing Han /* here we actually do the io */ 111268da9f05SHugh Dickins if (fault_type) 111368da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 111441ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 111527ab7006SHugh Dickins if (!page) { 11161da177e4SLinus Torvalds error = -ENOMEM; 111754af6042SHugh Dickins goto failed; 1118285b2c4fSHugh Dickins } 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 112254af6042SHugh Dickins lock_page(page); 11230142ef6cSHugh Dickins if (!PageSwapCache(page) || page_private(page) != swap.val || 1124d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1125bde05d1cSHugh Dickins error = -EEXIST; /* try again */ 1126d1899228SHugh Dickins goto unlock; 1127bde05d1cSHugh Dickins } 112827ab7006SHugh Dickins if (!PageUptodate(page)) { 11291da177e4SLinus Torvalds error = -EIO; 113054af6042SHugh Dickins goto failed; 113154af6042SHugh Dickins } 113254af6042SHugh Dickins wait_on_page_writeback(page); 113354af6042SHugh Dickins 1134bde05d1cSHugh Dickins if (shmem_should_replace_page(page, gfp)) { 1135bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 1136bde05d1cSHugh Dickins if (error) 113754af6042SHugh Dickins goto failed; 11381da177e4SLinus Torvalds } 11391da177e4SLinus Torvalds 114000501b53SJohannes Weiner error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg); 1141d1899228SHugh Dickins if (!error) { 114254af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1143fed400a1SWang Sheng-Hui swp_to_radix_entry(swap)); 1144215c02bcSHugh Dickins /* 1145215c02bcSHugh Dickins * We already confirmed swap under page lock, and make 1146215c02bcSHugh Dickins * no memory allocation here, so usually no possibility 1147215c02bcSHugh Dickins * of error; but free_swap_and_cache() only trylocks a 1148215c02bcSHugh Dickins * page, so it is just possible that the entry has been 1149215c02bcSHugh Dickins * truncated or holepunched since swap was confirmed. 1150215c02bcSHugh Dickins * shmem_undo_range() will have done some of the 1151215c02bcSHugh Dickins * unaccounting, now delete_from_swap_cache() will do 115293aa7d95SVladimir Davydov * the rest. 1153215c02bcSHugh Dickins * Reset swap.val? No, leave it so "failed" goes back to 1154215c02bcSHugh Dickins * "repeat": reading a hole and writing should succeed. 1155215c02bcSHugh Dickins */ 115600501b53SJohannes Weiner if (error) { 115700501b53SJohannes Weiner mem_cgroup_cancel_charge(page, memcg); 1158215c02bcSHugh Dickins delete_from_swap_cache(page); 1159d1899228SHugh Dickins } 116000501b53SJohannes Weiner } 116154af6042SHugh Dickins if (error) 116254af6042SHugh Dickins goto failed; 116354af6042SHugh Dickins 116400501b53SJohannes Weiner mem_cgroup_commit_charge(page, memcg, true); 116500501b53SJohannes Weiner 116654af6042SHugh Dickins spin_lock(&info->lock); 116754af6042SHugh Dickins info->swapped--; 116854af6042SHugh Dickins shmem_recalc_inode(inode); 11691da177e4SLinus Torvalds spin_unlock(&info->lock); 117027ab7006SHugh Dickins 117166d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 117266d2f4d2SHugh Dickins mark_page_accessed(page); 117366d2f4d2SHugh Dickins 117427ab7006SHugh Dickins delete_from_swap_cache(page); 117527ab7006SHugh Dickins set_page_dirty(page); 117627ab7006SHugh Dickins swap_free(swap); 117727ab7006SHugh Dickins 117854af6042SHugh Dickins } else { 117954af6042SHugh Dickins if (shmem_acct_block(info->flags)) { 118054af6042SHugh Dickins error = -ENOSPC; 118154af6042SHugh Dickins goto failed; 11821da177e4SLinus Torvalds } 11830edd73b3SHugh Dickins if (sbinfo->max_blocks) { 1184fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 118554af6042SHugh Dickins sbinfo->max_blocks) >= 0) { 118654af6042SHugh Dickins error = -ENOSPC; 118754af6042SHugh Dickins goto unacct; 118854af6042SHugh Dickins } 11897e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 119059a16eadSHugh Dickins } 11911da177e4SLinus Torvalds 119254af6042SHugh Dickins page = shmem_alloc_page(gfp, info, index); 119354af6042SHugh Dickins if (!page) { 119454af6042SHugh Dickins error = -ENOMEM; 119554af6042SHugh Dickins goto decused; 119654af6042SHugh Dickins } 119754af6042SHugh Dickins 119807a42788SMel Gorman __SetPageSwapBacked(page); 119954af6042SHugh Dickins __set_page_locked(page); 120066d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1201eb39d618SHugh Dickins __SetPageReferenced(page); 120266d2f4d2SHugh Dickins 120300501b53SJohannes Weiner error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg); 120454af6042SHugh Dickins if (error) 120554af6042SHugh Dickins goto decused; 12065e4c0d97SJan Kara error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK); 1207b065b432SHugh Dickins if (!error) { 1208b065b432SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1209fed400a1SWang Sheng-Hui NULL); 1210b065b432SHugh Dickins radix_tree_preload_end(); 1211b065b432SHugh Dickins } 1212b065b432SHugh Dickins if (error) { 121300501b53SJohannes Weiner mem_cgroup_cancel_charge(page, memcg); 1214b065b432SHugh Dickins goto decused; 1215b065b432SHugh Dickins } 121600501b53SJohannes Weiner mem_cgroup_commit_charge(page, memcg, false); 121754af6042SHugh Dickins lru_cache_add_anon(page); 121854af6042SHugh Dickins 121954af6042SHugh Dickins spin_lock(&info->lock); 12201da177e4SLinus Torvalds info->alloced++; 122154af6042SHugh Dickins inode->i_blocks += BLOCKS_PER_PAGE; 122254af6042SHugh Dickins shmem_recalc_inode(inode); 122359a16eadSHugh Dickins spin_unlock(&info->lock); 12241635f6a7SHugh Dickins alloced = true; 122554af6042SHugh Dickins 1226ec9516fbSHugh Dickins /* 12271635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 12281635f6a7SHugh Dickins */ 12291635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 12301635f6a7SHugh Dickins sgp = SGP_WRITE; 12311635f6a7SHugh Dickins clear: 12321635f6a7SHugh Dickins /* 12331635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 12341635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 12351635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1236ec9516fbSHugh Dickins */ 1237ec9516fbSHugh Dickins if (sgp != SGP_WRITE) { 123827ab7006SHugh Dickins clear_highpage(page); 123927ab7006SHugh Dickins flush_dcache_page(page); 124027ab7006SHugh Dickins SetPageUptodate(page); 1241ec9516fbSHugh Dickins } 1242a0ee5ec5SHugh Dickins if (sgp == SGP_DIRTY) 124327ab7006SHugh Dickins set_page_dirty(page); 12441da177e4SLinus Torvalds } 1245bde05d1cSHugh Dickins 124654af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 12471635f6a7SHugh Dickins if (sgp != SGP_WRITE && sgp != SGP_FALLOC && 124854af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 1249267a4c76SHugh Dickins if (alloced) { 1250267a4c76SHugh Dickins ClearPageDirty(page); 1251267a4c76SHugh Dickins delete_from_page_cache(page); 1252267a4c76SHugh Dickins spin_lock(&info->lock); 1253267a4c76SHugh Dickins shmem_recalc_inode(inode); 1254267a4c76SHugh Dickins spin_unlock(&info->lock); 1255267a4c76SHugh Dickins } 125654af6042SHugh Dickins error = -EINVAL; 1257267a4c76SHugh Dickins goto unlock; 1258ff36b801SShaohua Li } 125954af6042SHugh Dickins *pagep = page; 126054af6042SHugh Dickins return 0; 1261d00806b1SNick Piggin 1262d0217ac0SNick Piggin /* 126354af6042SHugh Dickins * Error recovery. 12641da177e4SLinus Torvalds */ 126554af6042SHugh Dickins decused: 126654af6042SHugh Dickins if (sbinfo->max_blocks) 126754af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -1); 126854af6042SHugh Dickins unacct: 126954af6042SHugh Dickins shmem_unacct_blocks(info->flags, 1); 127054af6042SHugh Dickins failed: 1271267a4c76SHugh Dickins if (swap.val && !shmem_confirm_swap(mapping, index, swap)) 127254af6042SHugh Dickins error = -EEXIST; 1273d1899228SHugh Dickins unlock: 127427ab7006SHugh Dickins if (page) { 127554af6042SHugh Dickins unlock_page(page); 12761da177e4SLinus Torvalds page_cache_release(page); 127754af6042SHugh Dickins } 127854af6042SHugh Dickins if (error == -ENOSPC && !once++) { 127954af6042SHugh Dickins info = SHMEM_I(inode); 128054af6042SHugh Dickins spin_lock(&info->lock); 128154af6042SHugh Dickins shmem_recalc_inode(inode); 128254af6042SHugh Dickins spin_unlock(&info->lock); 12831da177e4SLinus Torvalds goto repeat; 1284d8dc74f2SAdrian Bunk } 1285d1899228SHugh Dickins if (error == -EEXIST) /* from above or from radix_tree_insert */ 128654af6042SHugh Dickins goto repeat; 128754af6042SHugh Dickins return error; 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 12911da177e4SLinus Torvalds { 1292496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 12931da177e4SLinus Torvalds int error; 129468da9f05SHugh Dickins int ret = VM_FAULT_LOCKED; 12951da177e4SLinus Torvalds 1296f00cdc6dSHugh Dickins /* 1297f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 1298f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 1299f00cdc6dSHugh Dickins * locks writers out with its hold on i_mutex. So refrain from 13008e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 13018e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 13028e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 13038e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 13048e205f77SHugh Dickins * 13058e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 13068e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 13078e205f77SHugh Dickins * we just need to make racing faults a rare case. 13088e205f77SHugh Dickins * 13098e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 13108e205f77SHugh Dickins * standard mutex or completion: but we cannot take i_mutex in fault, 13118e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 1312f00cdc6dSHugh Dickins */ 1313f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 1314f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 1315f00cdc6dSHugh Dickins 1316f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 1317f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 13188e205f77SHugh Dickins if (shmem_falloc && 13198e205f77SHugh Dickins shmem_falloc->waitq && 13208e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 13218e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 13228e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 13238e205f77SHugh Dickins DEFINE_WAIT(shmem_fault_wait); 13248e205f77SHugh Dickins 13258e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 1326f00cdc6dSHugh Dickins if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) && 1327f00cdc6dSHugh Dickins !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 13288e205f77SHugh Dickins /* It's polite to up mmap_sem if we can */ 1329f00cdc6dSHugh Dickins up_read(&vma->vm_mm->mmap_sem); 13308e205f77SHugh Dickins ret = VM_FAULT_RETRY; 1331f00cdc6dSHugh Dickins } 13328e205f77SHugh Dickins 13338e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 13348e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 13358e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 13368e205f77SHugh Dickins spin_unlock(&inode->i_lock); 13378e205f77SHugh Dickins schedule(); 13388e205f77SHugh Dickins 13398e205f77SHugh Dickins /* 13408e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 13418e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 13428e205f77SHugh Dickins * is usually invalid by the time we reach here, but 13438e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 13448e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 13458e205f77SHugh Dickins */ 13468e205f77SHugh Dickins spin_lock(&inode->i_lock); 13478e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 13488e205f77SHugh Dickins spin_unlock(&inode->i_lock); 13498e205f77SHugh Dickins return ret; 1350f00cdc6dSHugh Dickins } 13518e205f77SHugh Dickins spin_unlock(&inode->i_lock); 1352f00cdc6dSHugh Dickins } 1353f00cdc6dSHugh Dickins 13541da177e4SLinus Torvalds error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); 13551da177e4SLinus Torvalds if (error) 13561da177e4SLinus Torvalds return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 135768da9f05SHugh Dickins 1358456f998eSYing Han if (ret & VM_FAULT_MAJOR) { 1359456f998eSYing Han count_vm_event(PGMAJFAULT); 1360456f998eSYing Han mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 1361456f998eSYing Han } 136268da9f05SHugh Dickins return ret; 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds #ifdef CONFIG_NUMA 136641ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 13671da177e4SLinus Torvalds { 1368496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 136941ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 1372d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 1373d8dc74f2SAdrian Bunk unsigned long addr) 13741da177e4SLinus Torvalds { 1375496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 137641ffe5d5SHugh Dickins pgoff_t index; 13771da177e4SLinus Torvalds 137841ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 137941ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds #endif 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 13841da177e4SLinus Torvalds { 1385496ad9aaSAl Viro struct inode *inode = file_inode(file); 13861da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 13871da177e4SLinus Torvalds int retval = -ENOMEM; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds spin_lock(&info->lock); 13901da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 13911da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 13921da177e4SLinus Torvalds goto out_nomem; 13931da177e4SLinus Torvalds info->flags |= VM_LOCKED; 139489e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 13971da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 13981da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 139989e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds retval = 0; 140289e004eaSLee Schermerhorn 14031da177e4SLinus Torvalds out_nomem: 14041da177e4SLinus Torvalds spin_unlock(&info->lock); 14051da177e4SLinus Torvalds return retval; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 14089b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 14091da177e4SLinus Torvalds { 14101da177e4SLinus Torvalds file_accessed(file); 14111da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 14121da177e4SLinus Torvalds return 0; 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds 1415454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 141609208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 14171da177e4SLinus Torvalds { 14181da177e4SLinus Torvalds struct inode *inode; 14191da177e4SLinus Torvalds struct shmem_inode_info *info; 14201da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 14211da177e4SLinus Torvalds 14225b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 14231da177e4SLinus Torvalds return NULL; 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds inode = new_inode(sb); 14261da177e4SLinus Torvalds if (inode) { 142785fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 1428454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 14291da177e4SLinus Torvalds inode->i_blocks = 0; 14301da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 143191828a40SDavid M. Grimes inode->i_generation = get_seconds(); 14321da177e4SLinus Torvalds info = SHMEM_I(inode); 14331da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 14341da177e4SLinus Torvalds spin_lock_init(&info->lock); 143540e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 14360b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 14371da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 143838f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 143972c04902SAl Viro cache_no_acl(inode); 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds switch (mode & S_IFMT) { 14421da177e4SLinus Torvalds default: 144339f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 14441da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 14451da177e4SLinus Torvalds break; 14461da177e4SLinus Torvalds case S_IFREG: 144714fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 14481da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 14491da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 145071fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 145171fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 14521da177e4SLinus Torvalds break; 14531da177e4SLinus Torvalds case S_IFDIR: 1454d8c76e6fSDave Hansen inc_nlink(inode); 14551da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 14561da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 14571da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 14581da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 14591da177e4SLinus Torvalds break; 14601da177e4SLinus Torvalds case S_IFLNK: 14611da177e4SLinus Torvalds /* 14621da177e4SLinus Torvalds * Must not load anything in the rbtree, 14631da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 14641da177e4SLinus Torvalds */ 146571fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 14661da177e4SLinus Torvalds break; 14671da177e4SLinus Torvalds } 14685b04c689SPavel Emelyanov } else 14695b04c689SPavel Emelyanov shmem_free_inode(sb); 14701da177e4SLinus Torvalds return inode; 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds 14730cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping) 14740cd6144aSJohannes Weiner { 1475f0774d88SSasha Levin if (!mapping->host) 1476f0774d88SSasha Levin return false; 1477f0774d88SSasha Levin 147897b713baSChristoph Hellwig return mapping->host->i_sb->s_op == &shmem_ops; 14790cd6144aSJohannes Weiner } 14800cd6144aSJohannes Weiner 14811da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 148292e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 148369f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 14841da177e4SLinus Torvalds 14856d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR 14866d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 14876d9d88d0SJarkko Sakkinen #else 14886d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL 14896d9d88d0SJarkko Sakkinen #endif 14906d9d88d0SJarkko Sakkinen 14911da177e4SLinus Torvalds static int 1492800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 1493800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 1494800d15a5SNick Piggin struct page **pagep, void **fsdata) 14951da177e4SLinus Torvalds { 1496800d15a5SNick Piggin struct inode *inode = mapping->host; 149740e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 1498800d15a5SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 149940e041a2SDavid Herrmann 150040e041a2SDavid Herrmann /* i_mutex is held by caller */ 150140e041a2SDavid Herrmann if (unlikely(info->seals)) { 150240e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) 150340e041a2SDavid Herrmann return -EPERM; 150440e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 150540e041a2SDavid Herrmann return -EPERM; 150640e041a2SDavid Herrmann } 150740e041a2SDavid Herrmann 150866d2f4d2SHugh Dickins return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); 1509800d15a5SNick Piggin } 1510800d15a5SNick Piggin 1511800d15a5SNick Piggin static int 1512800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 1513800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 1514800d15a5SNick Piggin struct page *page, void *fsdata) 1515800d15a5SNick Piggin { 1516800d15a5SNick Piggin struct inode *inode = mapping->host; 1517800d15a5SNick Piggin 1518800d15a5SNick Piggin if (pos + copied > inode->i_size) 1519800d15a5SNick Piggin i_size_write(inode, pos + copied); 1520800d15a5SNick Piggin 1521ec9516fbSHugh Dickins if (!PageUptodate(page)) { 1522ec9516fbSHugh Dickins if (copied < PAGE_CACHE_SIZE) { 1523ec9516fbSHugh Dickins unsigned from = pos & (PAGE_CACHE_SIZE - 1); 1524ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 1525ec9516fbSHugh Dickins from + copied, PAGE_CACHE_SIZE); 1526ec9516fbSHugh Dickins } 1527ec9516fbSHugh Dickins SetPageUptodate(page); 1528ec9516fbSHugh Dickins } 1529d3602444SHugh Dickins set_page_dirty(page); 15306746aff7SWu Fengguang unlock_page(page); 1531d3602444SHugh Dickins page_cache_release(page); 1532d3602444SHugh Dickins 1533800d15a5SNick Piggin return copied; 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds 15362ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 15371da177e4SLinus Torvalds { 15386e58e79dSAl Viro struct file *file = iocb->ki_filp; 15396e58e79dSAl Viro struct inode *inode = file_inode(file); 15401da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 154141ffe5d5SHugh Dickins pgoff_t index; 154241ffe5d5SHugh Dickins unsigned long offset; 1543a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 1544f7c1d074SGeert Uytterhoeven int error = 0; 1545cb66a7a1SAl Viro ssize_t retval = 0; 15466e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 1547a0ee5ec5SHugh Dickins 1548a0ee5ec5SHugh Dickins /* 1549a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 1550a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 1551a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 1552a0ee5ec5SHugh Dickins */ 1553777eda2cSAl Viro if (!iter_is_iovec(to)) 1554a0ee5ec5SHugh Dickins sgp = SGP_DIRTY; 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds index = *ppos >> PAGE_CACHE_SHIFT; 15571da177e4SLinus Torvalds offset = *ppos & ~PAGE_CACHE_MASK; 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds for (;;) { 15601da177e4SLinus Torvalds struct page *page = NULL; 156141ffe5d5SHugh Dickins pgoff_t end_index; 156241ffe5d5SHugh Dickins unsigned long nr, ret; 15631da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 15661da177e4SLinus Torvalds if (index > end_index) 15671da177e4SLinus Torvalds break; 15681da177e4SLinus Torvalds if (index == end_index) { 15691da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 15701da177e4SLinus Torvalds if (nr <= offset) 15711da177e4SLinus Torvalds break; 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds 15746e58e79dSAl Viro error = shmem_getpage(inode, index, &page, sgp, NULL); 15756e58e79dSAl Viro if (error) { 15766e58e79dSAl Viro if (error == -EINVAL) 15776e58e79dSAl Viro error = 0; 15781da177e4SLinus Torvalds break; 15791da177e4SLinus Torvalds } 1580d3602444SHugh Dickins if (page) 1581d3602444SHugh Dickins unlock_page(page); 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds /* 15841da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 15851b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 15861da177e4SLinus Torvalds */ 15871da177e4SLinus Torvalds nr = PAGE_CACHE_SIZE; 15881da177e4SLinus Torvalds i_size = i_size_read(inode); 15891da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 15901da177e4SLinus Torvalds if (index == end_index) { 15911da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 15921da177e4SLinus Torvalds if (nr <= offset) { 15931da177e4SLinus Torvalds if (page) 15941da177e4SLinus Torvalds page_cache_release(page); 15951da177e4SLinus Torvalds break; 15961da177e4SLinus Torvalds } 15971da177e4SLinus Torvalds } 15981da177e4SLinus Torvalds nr -= offset; 15991da177e4SLinus Torvalds 16001da177e4SLinus Torvalds if (page) { 16011da177e4SLinus Torvalds /* 16021da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 16031da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 16041da177e4SLinus Torvalds * before reading the page on the kernel side. 16051da177e4SLinus Torvalds */ 16061da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 16071da177e4SLinus Torvalds flush_dcache_page(page); 16081da177e4SLinus Torvalds /* 16091da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 16101da177e4SLinus Torvalds */ 16111da177e4SLinus Torvalds if (!offset) 16121da177e4SLinus Torvalds mark_page_accessed(page); 1613b5810039SNick Piggin } else { 16141da177e4SLinus Torvalds page = ZERO_PAGE(0); 1615b5810039SNick Piggin page_cache_get(page); 1616b5810039SNick Piggin } 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds /* 16191da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 16201da177e4SLinus Torvalds * now we can copy it to user space... 16211da177e4SLinus Torvalds */ 16222ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 16236e58e79dSAl Viro retval += ret; 16241da177e4SLinus Torvalds offset += ret; 16251da177e4SLinus Torvalds index += offset >> PAGE_CACHE_SHIFT; 16261da177e4SLinus Torvalds offset &= ~PAGE_CACHE_MASK; 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds page_cache_release(page); 16292ba5bbedSAl Viro if (!iov_iter_count(to)) 16301da177e4SLinus Torvalds break; 16316e58e79dSAl Viro if (ret < nr) { 16326e58e79dSAl Viro error = -EFAULT; 16336e58e79dSAl Viro break; 16346e58e79dSAl Viro } 16351da177e4SLinus Torvalds cond_resched(); 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; 16396e58e79dSAl Viro file_accessed(file); 16406e58e79dSAl Viro return retval ? retval : error; 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds 1643708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 1644708e3508SHugh Dickins struct pipe_inode_info *pipe, size_t len, 1645708e3508SHugh Dickins unsigned int flags) 1646708e3508SHugh Dickins { 1647708e3508SHugh Dickins struct address_space *mapping = in->f_mapping; 164871f0e07aSHugh Dickins struct inode *inode = mapping->host; 1649708e3508SHugh Dickins unsigned int loff, nr_pages, req_pages; 1650708e3508SHugh Dickins struct page *pages[PIPE_DEF_BUFFERS]; 1651708e3508SHugh Dickins struct partial_page partial[PIPE_DEF_BUFFERS]; 1652708e3508SHugh Dickins struct page *page; 1653708e3508SHugh Dickins pgoff_t index, end_index; 1654708e3508SHugh Dickins loff_t isize, left; 1655708e3508SHugh Dickins int error, page_nr; 1656708e3508SHugh Dickins struct splice_pipe_desc spd = { 1657708e3508SHugh Dickins .pages = pages, 1658708e3508SHugh Dickins .partial = partial, 1659047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 1660708e3508SHugh Dickins .flags = flags, 1661708e3508SHugh Dickins .ops = &page_cache_pipe_buf_ops, 1662708e3508SHugh Dickins .spd_release = spd_release_page, 1663708e3508SHugh Dickins }; 1664708e3508SHugh Dickins 166571f0e07aSHugh Dickins isize = i_size_read(inode); 1666708e3508SHugh Dickins if (unlikely(*ppos >= isize)) 1667708e3508SHugh Dickins return 0; 1668708e3508SHugh Dickins 1669708e3508SHugh Dickins left = isize - *ppos; 1670708e3508SHugh Dickins if (unlikely(left < len)) 1671708e3508SHugh Dickins len = left; 1672708e3508SHugh Dickins 1673708e3508SHugh Dickins if (splice_grow_spd(pipe, &spd)) 1674708e3508SHugh Dickins return -ENOMEM; 1675708e3508SHugh Dickins 1676708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1677708e3508SHugh Dickins loff = *ppos & ~PAGE_CACHE_MASK; 1678708e3508SHugh Dickins req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1679a786c06dSAl Viro nr_pages = min(req_pages, spd.nr_pages_max); 1680708e3508SHugh Dickins 1681708e3508SHugh Dickins spd.nr_pages = find_get_pages_contig(mapping, index, 1682708e3508SHugh Dickins nr_pages, spd.pages); 1683708e3508SHugh Dickins index += spd.nr_pages; 1684708e3508SHugh Dickins error = 0; 168571f0e07aSHugh Dickins 1686708e3508SHugh Dickins while (spd.nr_pages < nr_pages) { 168771f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); 168871f0e07aSHugh Dickins if (error) 1689708e3508SHugh Dickins break; 1690708e3508SHugh Dickins unlock_page(page); 1691708e3508SHugh Dickins spd.pages[spd.nr_pages++] = page; 1692708e3508SHugh Dickins index++; 1693708e3508SHugh Dickins } 1694708e3508SHugh Dickins 1695708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1696708e3508SHugh Dickins nr_pages = spd.nr_pages; 1697708e3508SHugh Dickins spd.nr_pages = 0; 169871f0e07aSHugh Dickins 1699708e3508SHugh Dickins for (page_nr = 0; page_nr < nr_pages; page_nr++) { 1700708e3508SHugh Dickins unsigned int this_len; 1701708e3508SHugh Dickins 1702708e3508SHugh Dickins if (!len) 1703708e3508SHugh Dickins break; 1704708e3508SHugh Dickins 1705708e3508SHugh Dickins this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 1706708e3508SHugh Dickins page = spd.pages[page_nr]; 1707708e3508SHugh Dickins 170871f0e07aSHugh Dickins if (!PageUptodate(page) || page->mapping != mapping) { 170971f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, 171071f0e07aSHugh Dickins SGP_CACHE, NULL); 171171f0e07aSHugh Dickins if (error) 1712708e3508SHugh Dickins break; 171371f0e07aSHugh Dickins unlock_page(page); 1714708e3508SHugh Dickins page_cache_release(spd.pages[page_nr]); 1715708e3508SHugh Dickins spd.pages[page_nr] = page; 1716708e3508SHugh Dickins } 1717708e3508SHugh Dickins 171871f0e07aSHugh Dickins isize = i_size_read(inode); 1719708e3508SHugh Dickins end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 1720708e3508SHugh Dickins if (unlikely(!isize || index > end_index)) 1721708e3508SHugh Dickins break; 1722708e3508SHugh Dickins 1723708e3508SHugh Dickins if (end_index == index) { 1724708e3508SHugh Dickins unsigned int plen; 1725708e3508SHugh Dickins 1726708e3508SHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 1727708e3508SHugh Dickins if (plen <= loff) 1728708e3508SHugh Dickins break; 1729708e3508SHugh Dickins 1730708e3508SHugh Dickins this_len = min(this_len, plen - loff); 1731708e3508SHugh Dickins len = this_len; 1732708e3508SHugh Dickins } 1733708e3508SHugh Dickins 1734708e3508SHugh Dickins spd.partial[page_nr].offset = loff; 1735708e3508SHugh Dickins spd.partial[page_nr].len = this_len; 1736708e3508SHugh Dickins len -= this_len; 1737708e3508SHugh Dickins loff = 0; 1738708e3508SHugh Dickins spd.nr_pages++; 1739708e3508SHugh Dickins index++; 1740708e3508SHugh Dickins } 1741708e3508SHugh Dickins 1742708e3508SHugh Dickins while (page_nr < nr_pages) 1743708e3508SHugh Dickins page_cache_release(spd.pages[page_nr++]); 1744708e3508SHugh Dickins 1745708e3508SHugh Dickins if (spd.nr_pages) 1746708e3508SHugh Dickins error = splice_to_pipe(pipe, &spd); 1747708e3508SHugh Dickins 1748047fe360SEric Dumazet splice_shrink_spd(&spd); 1749708e3508SHugh Dickins 1750708e3508SHugh Dickins if (error > 0) { 1751708e3508SHugh Dickins *ppos += error; 1752708e3508SHugh Dickins file_accessed(in); 1753708e3508SHugh Dickins } 1754708e3508SHugh Dickins return error; 1755708e3508SHugh Dickins } 1756708e3508SHugh Dickins 1757220f2ac9SHugh Dickins /* 1758220f2ac9SHugh Dickins * llseek SEEK_DATA or SEEK_HOLE through the radix_tree. 1759220f2ac9SHugh Dickins */ 1760220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping, 1761965c8e59SAndrew Morton pgoff_t index, pgoff_t end, int whence) 1762220f2ac9SHugh Dickins { 1763220f2ac9SHugh Dickins struct page *page; 1764220f2ac9SHugh Dickins struct pagevec pvec; 1765220f2ac9SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 1766220f2ac9SHugh Dickins bool done = false; 1767220f2ac9SHugh Dickins int i; 1768220f2ac9SHugh Dickins 1769220f2ac9SHugh Dickins pagevec_init(&pvec, 0); 1770220f2ac9SHugh Dickins pvec.nr = 1; /* start small: we may be there already */ 1771220f2ac9SHugh Dickins while (!done) { 17720cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 1773220f2ac9SHugh Dickins pvec.nr, pvec.pages, indices); 1774220f2ac9SHugh Dickins if (!pvec.nr) { 1775965c8e59SAndrew Morton if (whence == SEEK_DATA) 1776220f2ac9SHugh Dickins index = end; 1777220f2ac9SHugh Dickins break; 1778220f2ac9SHugh Dickins } 1779220f2ac9SHugh Dickins for (i = 0; i < pvec.nr; i++, index++) { 1780220f2ac9SHugh Dickins if (index < indices[i]) { 1781965c8e59SAndrew Morton if (whence == SEEK_HOLE) { 1782220f2ac9SHugh Dickins done = true; 1783220f2ac9SHugh Dickins break; 1784220f2ac9SHugh Dickins } 1785220f2ac9SHugh Dickins index = indices[i]; 1786220f2ac9SHugh Dickins } 1787220f2ac9SHugh Dickins page = pvec.pages[i]; 1788220f2ac9SHugh Dickins if (page && !radix_tree_exceptional_entry(page)) { 1789220f2ac9SHugh Dickins if (!PageUptodate(page)) 1790220f2ac9SHugh Dickins page = NULL; 1791220f2ac9SHugh Dickins } 1792220f2ac9SHugh Dickins if (index >= end || 1793965c8e59SAndrew Morton (page && whence == SEEK_DATA) || 1794965c8e59SAndrew Morton (!page && whence == SEEK_HOLE)) { 1795220f2ac9SHugh Dickins done = true; 1796220f2ac9SHugh Dickins break; 1797220f2ac9SHugh Dickins } 1798220f2ac9SHugh Dickins } 17990cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 1800220f2ac9SHugh Dickins pagevec_release(&pvec); 1801220f2ac9SHugh Dickins pvec.nr = PAGEVEC_SIZE; 1802220f2ac9SHugh Dickins cond_resched(); 1803220f2ac9SHugh Dickins } 1804220f2ac9SHugh Dickins return index; 1805220f2ac9SHugh Dickins } 1806220f2ac9SHugh Dickins 1807965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 1808220f2ac9SHugh Dickins { 1809220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 1810220f2ac9SHugh Dickins struct inode *inode = mapping->host; 1811220f2ac9SHugh Dickins pgoff_t start, end; 1812220f2ac9SHugh Dickins loff_t new_offset; 1813220f2ac9SHugh Dickins 1814965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 1815965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 1816220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 1817220f2ac9SHugh Dickins mutex_lock(&inode->i_mutex); 1818220f2ac9SHugh Dickins /* We're holding i_mutex so we can access i_size directly */ 1819220f2ac9SHugh Dickins 1820220f2ac9SHugh Dickins if (offset < 0) 1821220f2ac9SHugh Dickins offset = -EINVAL; 1822220f2ac9SHugh Dickins else if (offset >= inode->i_size) 1823220f2ac9SHugh Dickins offset = -ENXIO; 1824220f2ac9SHugh Dickins else { 1825220f2ac9SHugh Dickins start = offset >> PAGE_CACHE_SHIFT; 1826220f2ac9SHugh Dickins end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1827965c8e59SAndrew Morton new_offset = shmem_seek_hole_data(mapping, start, end, whence); 1828220f2ac9SHugh Dickins new_offset <<= PAGE_CACHE_SHIFT; 1829220f2ac9SHugh Dickins if (new_offset > offset) { 1830220f2ac9SHugh Dickins if (new_offset < inode->i_size) 1831220f2ac9SHugh Dickins offset = new_offset; 1832965c8e59SAndrew Morton else if (whence == SEEK_DATA) 1833220f2ac9SHugh Dickins offset = -ENXIO; 1834220f2ac9SHugh Dickins else 1835220f2ac9SHugh Dickins offset = inode->i_size; 1836220f2ac9SHugh Dickins } 1837220f2ac9SHugh Dickins } 1838220f2ac9SHugh Dickins 1839387aae6fSHugh Dickins if (offset >= 0) 184046a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 1841220f2ac9SHugh Dickins mutex_unlock(&inode->i_mutex); 1842220f2ac9SHugh Dickins return offset; 1843220f2ac9SHugh Dickins } 1844220f2ac9SHugh Dickins 184505f65b5cSDavid Herrmann /* 184605f65b5cSDavid Herrmann * We need a tag: a new tag would expand every radix_tree_node by 8 bytes, 184705f65b5cSDavid Herrmann * so reuse a tag which we firmly believe is never set or cleared on shmem. 184805f65b5cSDavid Herrmann */ 184905f65b5cSDavid Herrmann #define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE 185005f65b5cSDavid Herrmann #define LAST_SCAN 4 /* about 150ms max */ 185105f65b5cSDavid Herrmann 185205f65b5cSDavid Herrmann static void shmem_tag_pins(struct address_space *mapping) 185305f65b5cSDavid Herrmann { 185405f65b5cSDavid Herrmann struct radix_tree_iter iter; 185505f65b5cSDavid Herrmann void **slot; 185605f65b5cSDavid Herrmann pgoff_t start; 185705f65b5cSDavid Herrmann struct page *page; 185805f65b5cSDavid Herrmann 185905f65b5cSDavid Herrmann lru_add_drain(); 186005f65b5cSDavid Herrmann start = 0; 186105f65b5cSDavid Herrmann rcu_read_lock(); 186205f65b5cSDavid Herrmann 186305f65b5cSDavid Herrmann restart: 186405f65b5cSDavid Herrmann radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { 186505f65b5cSDavid Herrmann page = radix_tree_deref_slot(slot); 186605f65b5cSDavid Herrmann if (!page || radix_tree_exception(page)) { 186705f65b5cSDavid Herrmann if (radix_tree_deref_retry(page)) 186805f65b5cSDavid Herrmann goto restart; 186905f65b5cSDavid Herrmann } else if (page_count(page) - page_mapcount(page) > 1) { 187005f65b5cSDavid Herrmann spin_lock_irq(&mapping->tree_lock); 187105f65b5cSDavid Herrmann radix_tree_tag_set(&mapping->page_tree, iter.index, 187205f65b5cSDavid Herrmann SHMEM_TAG_PINNED); 187305f65b5cSDavid Herrmann spin_unlock_irq(&mapping->tree_lock); 187405f65b5cSDavid Herrmann } 187505f65b5cSDavid Herrmann 187605f65b5cSDavid Herrmann if (need_resched()) { 187705f65b5cSDavid Herrmann cond_resched_rcu(); 187805f65b5cSDavid Herrmann start = iter.index + 1; 187905f65b5cSDavid Herrmann goto restart; 188005f65b5cSDavid Herrmann } 188105f65b5cSDavid Herrmann } 188205f65b5cSDavid Herrmann rcu_read_unlock(); 188305f65b5cSDavid Herrmann } 188405f65b5cSDavid Herrmann 188505f65b5cSDavid Herrmann /* 188605f65b5cSDavid Herrmann * Setting SEAL_WRITE requires us to verify there's no pending writer. However, 188705f65b5cSDavid Herrmann * via get_user_pages(), drivers might have some pending I/O without any active 188805f65b5cSDavid Herrmann * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages 188905f65b5cSDavid Herrmann * and see whether it has an elevated ref-count. If so, we tag them and wait for 189005f65b5cSDavid Herrmann * them to be dropped. 189105f65b5cSDavid Herrmann * The caller must guarantee that no new user will acquire writable references 189205f65b5cSDavid Herrmann * to those pages to avoid races. 189305f65b5cSDavid Herrmann */ 189440e041a2SDavid Herrmann static int shmem_wait_for_pins(struct address_space *mapping) 189540e041a2SDavid Herrmann { 189605f65b5cSDavid Herrmann struct radix_tree_iter iter; 189705f65b5cSDavid Herrmann void **slot; 189805f65b5cSDavid Herrmann pgoff_t start; 189905f65b5cSDavid Herrmann struct page *page; 190005f65b5cSDavid Herrmann int error, scan; 190105f65b5cSDavid Herrmann 190205f65b5cSDavid Herrmann shmem_tag_pins(mapping); 190305f65b5cSDavid Herrmann 190405f65b5cSDavid Herrmann error = 0; 190505f65b5cSDavid Herrmann for (scan = 0; scan <= LAST_SCAN; scan++) { 190605f65b5cSDavid Herrmann if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED)) 190705f65b5cSDavid Herrmann break; 190805f65b5cSDavid Herrmann 190905f65b5cSDavid Herrmann if (!scan) 191005f65b5cSDavid Herrmann lru_add_drain_all(); 191105f65b5cSDavid Herrmann else if (schedule_timeout_killable((HZ << scan) / 200)) 191205f65b5cSDavid Herrmann scan = LAST_SCAN; 191305f65b5cSDavid Herrmann 191405f65b5cSDavid Herrmann start = 0; 191505f65b5cSDavid Herrmann rcu_read_lock(); 191605f65b5cSDavid Herrmann restart: 191705f65b5cSDavid Herrmann radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 191805f65b5cSDavid Herrmann start, SHMEM_TAG_PINNED) { 191905f65b5cSDavid Herrmann 192005f65b5cSDavid Herrmann page = radix_tree_deref_slot(slot); 192105f65b5cSDavid Herrmann if (radix_tree_exception(page)) { 192205f65b5cSDavid Herrmann if (radix_tree_deref_retry(page)) 192305f65b5cSDavid Herrmann goto restart; 192405f65b5cSDavid Herrmann 192505f65b5cSDavid Herrmann page = NULL; 192605f65b5cSDavid Herrmann } 192705f65b5cSDavid Herrmann 192805f65b5cSDavid Herrmann if (page && 192905f65b5cSDavid Herrmann page_count(page) - page_mapcount(page) != 1) { 193005f65b5cSDavid Herrmann if (scan < LAST_SCAN) 193105f65b5cSDavid Herrmann goto continue_resched; 193205f65b5cSDavid Herrmann 193305f65b5cSDavid Herrmann /* 193405f65b5cSDavid Herrmann * On the last scan, we clean up all those tags 193505f65b5cSDavid Herrmann * we inserted; but make a note that we still 193605f65b5cSDavid Herrmann * found pages pinned. 193705f65b5cSDavid Herrmann */ 193805f65b5cSDavid Herrmann error = -EBUSY; 193905f65b5cSDavid Herrmann } 194005f65b5cSDavid Herrmann 194105f65b5cSDavid Herrmann spin_lock_irq(&mapping->tree_lock); 194205f65b5cSDavid Herrmann radix_tree_tag_clear(&mapping->page_tree, 194305f65b5cSDavid Herrmann iter.index, SHMEM_TAG_PINNED); 194405f65b5cSDavid Herrmann spin_unlock_irq(&mapping->tree_lock); 194505f65b5cSDavid Herrmann continue_resched: 194605f65b5cSDavid Herrmann if (need_resched()) { 194705f65b5cSDavid Herrmann cond_resched_rcu(); 194805f65b5cSDavid Herrmann start = iter.index + 1; 194905f65b5cSDavid Herrmann goto restart; 195005f65b5cSDavid Herrmann } 195105f65b5cSDavid Herrmann } 195205f65b5cSDavid Herrmann rcu_read_unlock(); 195305f65b5cSDavid Herrmann } 195405f65b5cSDavid Herrmann 195505f65b5cSDavid Herrmann return error; 195640e041a2SDavid Herrmann } 195740e041a2SDavid Herrmann 195840e041a2SDavid Herrmann #define F_ALL_SEALS (F_SEAL_SEAL | \ 195940e041a2SDavid Herrmann F_SEAL_SHRINK | \ 196040e041a2SDavid Herrmann F_SEAL_GROW | \ 196140e041a2SDavid Herrmann F_SEAL_WRITE) 196240e041a2SDavid Herrmann 196340e041a2SDavid Herrmann int shmem_add_seals(struct file *file, unsigned int seals) 196440e041a2SDavid Herrmann { 196540e041a2SDavid Herrmann struct inode *inode = file_inode(file); 196640e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 196740e041a2SDavid Herrmann int error; 196840e041a2SDavid Herrmann 196940e041a2SDavid Herrmann /* 197040e041a2SDavid Herrmann * SEALING 197140e041a2SDavid Herrmann * Sealing allows multiple parties to share a shmem-file but restrict 197240e041a2SDavid Herrmann * access to a specific subset of file operations. Seals can only be 197340e041a2SDavid Herrmann * added, but never removed. This way, mutually untrusted parties can 197440e041a2SDavid Herrmann * share common memory regions with a well-defined policy. A malicious 197540e041a2SDavid Herrmann * peer can thus never perform unwanted operations on a shared object. 197640e041a2SDavid Herrmann * 197740e041a2SDavid Herrmann * Seals are only supported on special shmem-files and always affect 197840e041a2SDavid Herrmann * the whole underlying inode. Once a seal is set, it may prevent some 197940e041a2SDavid Herrmann * kinds of access to the file. Currently, the following seals are 198040e041a2SDavid Herrmann * defined: 198140e041a2SDavid Herrmann * SEAL_SEAL: Prevent further seals from being set on this file 198240e041a2SDavid Herrmann * SEAL_SHRINK: Prevent the file from shrinking 198340e041a2SDavid Herrmann * SEAL_GROW: Prevent the file from growing 198440e041a2SDavid Herrmann * SEAL_WRITE: Prevent write access to the file 198540e041a2SDavid Herrmann * 198640e041a2SDavid Herrmann * As we don't require any trust relationship between two parties, we 198740e041a2SDavid Herrmann * must prevent seals from being removed. Therefore, sealing a file 198840e041a2SDavid Herrmann * only adds a given set of seals to the file, it never touches 198940e041a2SDavid Herrmann * existing seals. Furthermore, the "setting seals"-operation can be 199040e041a2SDavid Herrmann * sealed itself, which basically prevents any further seal from being 199140e041a2SDavid Herrmann * added. 199240e041a2SDavid Herrmann * 199340e041a2SDavid Herrmann * Semantics of sealing are only defined on volatile files. Only 199440e041a2SDavid Herrmann * anonymous shmem files support sealing. More importantly, seals are 199540e041a2SDavid Herrmann * never written to disk. Therefore, there's no plan to support it on 199640e041a2SDavid Herrmann * other file types. 199740e041a2SDavid Herrmann */ 199840e041a2SDavid Herrmann 199940e041a2SDavid Herrmann if (file->f_op != &shmem_file_operations) 200040e041a2SDavid Herrmann return -EINVAL; 200140e041a2SDavid Herrmann if (!(file->f_mode & FMODE_WRITE)) 200240e041a2SDavid Herrmann return -EPERM; 200340e041a2SDavid Herrmann if (seals & ~(unsigned int)F_ALL_SEALS) 200440e041a2SDavid Herrmann return -EINVAL; 200540e041a2SDavid Herrmann 200640e041a2SDavid Herrmann mutex_lock(&inode->i_mutex); 200740e041a2SDavid Herrmann 200840e041a2SDavid Herrmann if (info->seals & F_SEAL_SEAL) { 200940e041a2SDavid Herrmann error = -EPERM; 201040e041a2SDavid Herrmann goto unlock; 201140e041a2SDavid Herrmann } 201240e041a2SDavid Herrmann 201340e041a2SDavid Herrmann if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) { 201440e041a2SDavid Herrmann error = mapping_deny_writable(file->f_mapping); 201540e041a2SDavid Herrmann if (error) 201640e041a2SDavid Herrmann goto unlock; 201740e041a2SDavid Herrmann 201840e041a2SDavid Herrmann error = shmem_wait_for_pins(file->f_mapping); 201940e041a2SDavid Herrmann if (error) { 202040e041a2SDavid Herrmann mapping_allow_writable(file->f_mapping); 202140e041a2SDavid Herrmann goto unlock; 202240e041a2SDavid Herrmann } 202340e041a2SDavid Herrmann } 202440e041a2SDavid Herrmann 202540e041a2SDavid Herrmann info->seals |= seals; 202640e041a2SDavid Herrmann error = 0; 202740e041a2SDavid Herrmann 202840e041a2SDavid Herrmann unlock: 202940e041a2SDavid Herrmann mutex_unlock(&inode->i_mutex); 203040e041a2SDavid Herrmann return error; 203140e041a2SDavid Herrmann } 203240e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_add_seals); 203340e041a2SDavid Herrmann 203440e041a2SDavid Herrmann int shmem_get_seals(struct file *file) 203540e041a2SDavid Herrmann { 203640e041a2SDavid Herrmann if (file->f_op != &shmem_file_operations) 203740e041a2SDavid Herrmann return -EINVAL; 203840e041a2SDavid Herrmann 203940e041a2SDavid Herrmann return SHMEM_I(file_inode(file))->seals; 204040e041a2SDavid Herrmann } 204140e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_get_seals); 204240e041a2SDavid Herrmann 204340e041a2SDavid Herrmann long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg) 204440e041a2SDavid Herrmann { 204540e041a2SDavid Herrmann long error; 204640e041a2SDavid Herrmann 204740e041a2SDavid Herrmann switch (cmd) { 204840e041a2SDavid Herrmann case F_ADD_SEALS: 204940e041a2SDavid Herrmann /* disallow upper 32bit */ 205040e041a2SDavid Herrmann if (arg > UINT_MAX) 205140e041a2SDavid Herrmann return -EINVAL; 205240e041a2SDavid Herrmann 205340e041a2SDavid Herrmann error = shmem_add_seals(file, arg); 205440e041a2SDavid Herrmann break; 205540e041a2SDavid Herrmann case F_GET_SEALS: 205640e041a2SDavid Herrmann error = shmem_get_seals(file); 205740e041a2SDavid Herrmann break; 205840e041a2SDavid Herrmann default: 205940e041a2SDavid Herrmann error = -EINVAL; 206040e041a2SDavid Herrmann break; 206140e041a2SDavid Herrmann } 206240e041a2SDavid Herrmann 206340e041a2SDavid Herrmann return error; 206440e041a2SDavid Herrmann } 206540e041a2SDavid Herrmann 206683e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 206783e4fa9cSHugh Dickins loff_t len) 206883e4fa9cSHugh Dickins { 2069496ad9aaSAl Viro struct inode *inode = file_inode(file); 2070e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 207140e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 20721aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 2073e2d12e22SHugh Dickins pgoff_t start, index, end; 2074e2d12e22SHugh Dickins int error; 207583e4fa9cSHugh Dickins 207613ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 207713ace4d0SHugh Dickins return -EOPNOTSUPP; 207813ace4d0SHugh Dickins 207983e4fa9cSHugh Dickins mutex_lock(&inode->i_mutex); 208083e4fa9cSHugh Dickins 208183e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 208283e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 208383e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 208483e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 20858e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 208683e4fa9cSHugh Dickins 208740e041a2SDavid Herrmann /* protected by i_mutex */ 208840e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) { 208940e041a2SDavid Herrmann error = -EPERM; 209040e041a2SDavid Herrmann goto out; 209140e041a2SDavid Herrmann } 209240e041a2SDavid Herrmann 20938e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 2094f00cdc6dSHugh Dickins shmem_falloc.start = unmap_start >> PAGE_SHIFT; 2095f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 2096f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2097f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 2098f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 2099f00cdc6dSHugh Dickins 210083e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 210183e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 210283e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 210383e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 210483e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 21058e205f77SHugh Dickins 21068e205f77SHugh Dickins spin_lock(&inode->i_lock); 21078e205f77SHugh Dickins inode->i_private = NULL; 21088e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 21098e205f77SHugh Dickins spin_unlock(&inode->i_lock); 211083e4fa9cSHugh Dickins error = 0; 21118e205f77SHugh Dickins goto out; 211283e4fa9cSHugh Dickins } 211383e4fa9cSHugh Dickins 2114e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 2115e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 2116e2d12e22SHugh Dickins if (error) 2117e2d12e22SHugh Dickins goto out; 2118e2d12e22SHugh Dickins 211940e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 212040e041a2SDavid Herrmann error = -EPERM; 212140e041a2SDavid Herrmann goto out; 212240e041a2SDavid Herrmann } 212340e041a2SDavid Herrmann 2124e2d12e22SHugh Dickins start = offset >> PAGE_CACHE_SHIFT; 2125e2d12e22SHugh Dickins end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 2126e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 2127e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 2128e2d12e22SHugh Dickins error = -ENOSPC; 2129e2d12e22SHugh Dickins goto out; 2130e2d12e22SHugh Dickins } 2131e2d12e22SHugh Dickins 21328e205f77SHugh Dickins shmem_falloc.waitq = NULL; 21331aac1400SHugh Dickins shmem_falloc.start = start; 21341aac1400SHugh Dickins shmem_falloc.next = start; 21351aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 21361aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 21371aac1400SHugh Dickins spin_lock(&inode->i_lock); 21381aac1400SHugh Dickins inode->i_private = &shmem_falloc; 21391aac1400SHugh Dickins spin_unlock(&inode->i_lock); 21401aac1400SHugh Dickins 2141e2d12e22SHugh Dickins for (index = start; index < end; index++) { 2142e2d12e22SHugh Dickins struct page *page; 2143e2d12e22SHugh Dickins 2144e2d12e22SHugh Dickins /* 2145e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 2146e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 2147e2d12e22SHugh Dickins */ 2148e2d12e22SHugh Dickins if (signal_pending(current)) 2149e2d12e22SHugh Dickins error = -EINTR; 21501aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 21511aac1400SHugh Dickins error = -ENOMEM; 2152e2d12e22SHugh Dickins else 21531635f6a7SHugh Dickins error = shmem_getpage(inode, index, &page, SGP_FALLOC, 2154e2d12e22SHugh Dickins NULL); 2155e2d12e22SHugh Dickins if (error) { 21561635f6a7SHugh Dickins /* Remove the !PageUptodate pages we added */ 21571635f6a7SHugh Dickins shmem_undo_range(inode, 21581635f6a7SHugh Dickins (loff_t)start << PAGE_CACHE_SHIFT, 21591635f6a7SHugh Dickins (loff_t)index << PAGE_CACHE_SHIFT, true); 21601aac1400SHugh Dickins goto undone; 2161e2d12e22SHugh Dickins } 2162e2d12e22SHugh Dickins 2163e2d12e22SHugh Dickins /* 21641aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 21651aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 21661aac1400SHugh Dickins */ 21671aac1400SHugh Dickins shmem_falloc.next++; 21681aac1400SHugh Dickins if (!PageUptodate(page)) 21691aac1400SHugh Dickins shmem_falloc.nr_falloced++; 21701aac1400SHugh Dickins 21711aac1400SHugh Dickins /* 21721635f6a7SHugh Dickins * If !PageUptodate, leave it that way so that freeable pages 21731635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 21741635f6a7SHugh Dickins * But set_page_dirty so that memory pressure will swap rather 2175e2d12e22SHugh Dickins * than free the pages we are allocating (and SGP_CACHE pages 2176e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 2177e2d12e22SHugh Dickins */ 2178e2d12e22SHugh Dickins set_page_dirty(page); 2179e2d12e22SHugh Dickins unlock_page(page); 2180e2d12e22SHugh Dickins page_cache_release(page); 2181e2d12e22SHugh Dickins cond_resched(); 2182e2d12e22SHugh Dickins } 2183e2d12e22SHugh Dickins 2184e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 2185e2d12e22SHugh Dickins i_size_write(inode, offset + len); 2186e2d12e22SHugh Dickins inode->i_ctime = CURRENT_TIME; 21871aac1400SHugh Dickins undone: 21881aac1400SHugh Dickins spin_lock(&inode->i_lock); 21891aac1400SHugh Dickins inode->i_private = NULL; 21901aac1400SHugh Dickins spin_unlock(&inode->i_lock); 2191e2d12e22SHugh Dickins out: 219283e4fa9cSHugh Dickins mutex_unlock(&inode->i_mutex); 219383e4fa9cSHugh Dickins return error; 219483e4fa9cSHugh Dickins } 219583e4fa9cSHugh Dickins 2196726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 21971da177e4SLinus Torvalds { 2198726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 22011da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 22021da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 22030edd73b3SHugh Dickins if (sbinfo->max_blocks) { 22041da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 220541ffe5d5SHugh Dickins buf->f_bavail = 220641ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 220741ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 22080edd73b3SHugh Dickins } 22090edd73b3SHugh Dickins if (sbinfo->max_inodes) { 22101da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 22111da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 22141da177e4SLinus Torvalds return 0; 22151da177e4SLinus Torvalds } 22161da177e4SLinus Torvalds 22171da177e4SLinus Torvalds /* 22181da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 22191da177e4SLinus Torvalds */ 22201da177e4SLinus Torvalds static int 22211a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 22221da177e4SLinus Torvalds { 22230b0a0806SHugh Dickins struct inode *inode; 22241da177e4SLinus Torvalds int error = -ENOSPC; 22251da177e4SLinus Torvalds 2226454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 22271da177e4SLinus Torvalds if (inode) { 2228feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2229feda821eSChristoph Hellwig if (error) 2230feda821eSChristoph Hellwig goto out_iput; 22312a7dba39SEric Paris error = security_inode_init_security(inode, dir, 22329d8f13baSMimi Zohar &dentry->d_name, 22336d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2234feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2235feda821eSChristoph Hellwig goto out_iput; 223637ec43cdSMimi Zohar 2237718deb6bSAl Viro error = 0; 22381da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 22391da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 22401da177e4SLinus Torvalds d_instantiate(dentry, inode); 22411da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds return error; 2244feda821eSChristoph Hellwig out_iput: 2245feda821eSChristoph Hellwig iput(inode); 2246feda821eSChristoph Hellwig return error; 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds 224960545d0dSAl Viro static int 225060545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 225160545d0dSAl Viro { 225260545d0dSAl Viro struct inode *inode; 225360545d0dSAl Viro int error = -ENOSPC; 225460545d0dSAl Viro 225560545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 225660545d0dSAl Viro if (inode) { 225760545d0dSAl Viro error = security_inode_init_security(inode, dir, 225860545d0dSAl Viro NULL, 225960545d0dSAl Viro shmem_initxattrs, NULL); 2260feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2261feda821eSChristoph Hellwig goto out_iput; 2262feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2263feda821eSChristoph Hellwig if (error) 2264feda821eSChristoph Hellwig goto out_iput; 226560545d0dSAl Viro d_tmpfile(dentry, inode); 226660545d0dSAl Viro } 226760545d0dSAl Viro return error; 2268feda821eSChristoph Hellwig out_iput: 2269feda821eSChristoph Hellwig iput(inode); 2270feda821eSChristoph Hellwig return error; 227160545d0dSAl Viro } 227260545d0dSAl Viro 227318bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 22741da177e4SLinus Torvalds { 22751da177e4SLinus Torvalds int error; 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 22781da177e4SLinus Torvalds return error; 2279d8c76e6fSDave Hansen inc_nlink(dir); 22801da177e4SLinus Torvalds return 0; 22811da177e4SLinus Torvalds } 22821da177e4SLinus Torvalds 22834acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2284ebfc3b49SAl Viro bool excl) 22851da177e4SLinus Torvalds { 22861da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds /* 22901da177e4SLinus Torvalds * Link a file.. 22911da177e4SLinus Torvalds */ 22921da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 22931da177e4SLinus Torvalds { 229475c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 22955b04c689SPavel Emelyanov int ret; 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds /* 22981da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 22991da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 23001da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 23011da177e4SLinus Torvalds */ 23025b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 23035b04c689SPavel Emelyanov if (ret) 23045b04c689SPavel Emelyanov goto out; 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 23071da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 2308d8c76e6fSDave Hansen inc_nlink(inode); 23097de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 23101da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 23111da177e4SLinus Torvalds d_instantiate(dentry, inode); 23125b04c689SPavel Emelyanov out: 23135b04c689SPavel Emelyanov return ret; 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 23171da177e4SLinus Torvalds { 231875c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 23191da177e4SLinus Torvalds 23205b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 23215b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 23221da177e4SLinus Torvalds 23231da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 23241da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 23259a53c3a7SDave Hansen drop_nlink(inode); 23261da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 23271da177e4SLinus Torvalds return 0; 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 23311da177e4SLinus Torvalds { 23321da177e4SLinus Torvalds if (!simple_empty(dentry)) 23331da177e4SLinus Torvalds return -ENOTEMPTY; 23341da177e4SLinus Torvalds 233575c3cfa8SDavid Howells drop_nlink(d_inode(dentry)); 23369a53c3a7SDave Hansen drop_nlink(dir); 23371da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds 234037456771SMiklos Szeredi static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 234137456771SMiklos Szeredi { 2342e36cb0b8SDavid Howells bool old_is_dir = d_is_dir(old_dentry); 2343e36cb0b8SDavid Howells bool new_is_dir = d_is_dir(new_dentry); 234437456771SMiklos Szeredi 234537456771SMiklos Szeredi if (old_dir != new_dir && old_is_dir != new_is_dir) { 234637456771SMiklos Szeredi if (old_is_dir) { 234737456771SMiklos Szeredi drop_nlink(old_dir); 234837456771SMiklos Szeredi inc_nlink(new_dir); 234937456771SMiklos Szeredi } else { 235037456771SMiklos Szeredi drop_nlink(new_dir); 235137456771SMiklos Szeredi inc_nlink(old_dir); 235237456771SMiklos Szeredi } 235337456771SMiklos Szeredi } 235437456771SMiklos Szeredi old_dir->i_ctime = old_dir->i_mtime = 235537456771SMiklos Szeredi new_dir->i_ctime = new_dir->i_mtime = 235675c3cfa8SDavid Howells d_inode(old_dentry)->i_ctime = 235775c3cfa8SDavid Howells d_inode(new_dentry)->i_ctime = CURRENT_TIME; 235837456771SMiklos Szeredi 235937456771SMiklos Szeredi return 0; 236037456771SMiklos Szeredi } 236137456771SMiklos Szeredi 236246fdb794SMiklos Szeredi static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry) 236346fdb794SMiklos Szeredi { 236446fdb794SMiklos Szeredi struct dentry *whiteout; 236546fdb794SMiklos Szeredi int error; 236646fdb794SMiklos Szeredi 236746fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 236846fdb794SMiklos Szeredi if (!whiteout) 236946fdb794SMiklos Szeredi return -ENOMEM; 237046fdb794SMiklos Szeredi 237146fdb794SMiklos Szeredi error = shmem_mknod(old_dir, whiteout, 237246fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 237346fdb794SMiklos Szeredi dput(whiteout); 237446fdb794SMiklos Szeredi if (error) 237546fdb794SMiklos Szeredi return error; 237646fdb794SMiklos Szeredi 237746fdb794SMiklos Szeredi /* 237846fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 237946fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 238046fdb794SMiklos Szeredi * 238146fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 238246fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 238346fdb794SMiklos Szeredi */ 238446fdb794SMiklos Szeredi d_rehash(whiteout); 238546fdb794SMiklos Szeredi return 0; 238646fdb794SMiklos Szeredi } 238746fdb794SMiklos Szeredi 23881da177e4SLinus Torvalds /* 23891da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 23901da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 23911da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 23921da177e4SLinus Torvalds * gets overwritten. 23931da177e4SLinus Torvalds */ 23943b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) 23951da177e4SLinus Torvalds { 239675c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 23971da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 23981da177e4SLinus Torvalds 239946fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 24003b69ff51SMiklos Szeredi return -EINVAL; 24013b69ff51SMiklos Szeredi 240237456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 240337456771SMiklos Szeredi return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry); 240437456771SMiklos Szeredi 24051da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 24061da177e4SLinus Torvalds return -ENOTEMPTY; 24071da177e4SLinus Torvalds 240846fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 240946fdb794SMiklos Szeredi int error; 241046fdb794SMiklos Szeredi 241146fdb794SMiklos Szeredi error = shmem_whiteout(old_dir, old_dentry); 241246fdb794SMiklos Szeredi if (error) 241346fdb794SMiklos Szeredi return error; 241446fdb794SMiklos Szeredi } 241546fdb794SMiklos Szeredi 241675c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 24171da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 2418b928095bSMiklos Szeredi if (they_are_dirs) { 241975c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 24209a53c3a7SDave Hansen drop_nlink(old_dir); 2421b928095bSMiklos Szeredi } 24221da177e4SLinus Torvalds } else if (they_are_dirs) { 24239a53c3a7SDave Hansen drop_nlink(old_dir); 2424d8c76e6fSDave Hansen inc_nlink(new_dir); 24251da177e4SLinus Torvalds } 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 24281da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 24291da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 24301da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 24311da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 24321da177e4SLinus Torvalds return 0; 24331da177e4SLinus Torvalds } 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 24361da177e4SLinus Torvalds { 24371da177e4SLinus Torvalds int error; 24381da177e4SLinus Torvalds int len; 24391da177e4SLinus Torvalds struct inode *inode; 24409276aad6SHugh Dickins struct page *page; 24411da177e4SLinus Torvalds struct shmem_inode_info *info; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds len = strlen(symname) + 1; 24441da177e4SLinus Torvalds if (len > PAGE_CACHE_SIZE) 24451da177e4SLinus Torvalds return -ENAMETOOLONG; 24461da177e4SLinus Torvalds 2447454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 24481da177e4SLinus Torvalds if (!inode) 24491da177e4SLinus Torvalds return -ENOSPC; 24501da177e4SLinus Torvalds 24519d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 24526d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2453570bc1c2SStephen Smalley if (error) { 2454570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 2455570bc1c2SStephen Smalley iput(inode); 2456570bc1c2SStephen Smalley return error; 2457570bc1c2SStephen Smalley } 2458570bc1c2SStephen Smalley error = 0; 2459570bc1c2SStephen Smalley } 2460570bc1c2SStephen Smalley 24611da177e4SLinus Torvalds info = SHMEM_I(inode); 24621da177e4SLinus Torvalds inode->i_size = len-1; 246369f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 246469f07ec9SHugh Dickins info->symlink = kmemdup(symname, len, GFP_KERNEL); 246569f07ec9SHugh Dickins if (!info->symlink) { 246669f07ec9SHugh Dickins iput(inode); 246769f07ec9SHugh Dickins return -ENOMEM; 246869f07ec9SHugh Dickins } 246969f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 247060380f19SAl Viro inode->i_link = info->symlink; 24711da177e4SLinus Torvalds } else { 2472*e8ecde25SAl Viro inode_nohighmem(inode); 24731da177e4SLinus Torvalds error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); 24741da177e4SLinus Torvalds if (error) { 24751da177e4SLinus Torvalds iput(inode); 24761da177e4SLinus Torvalds return error; 24771da177e4SLinus Torvalds } 247814fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 24791da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 248021fc61c7SAl Viro memcpy(page_address(page), symname, len); 2481ec9516fbSHugh Dickins SetPageUptodate(page); 24821da177e4SLinus Torvalds set_page_dirty(page); 24836746aff7SWu Fengguang unlock_page(page); 24841da177e4SLinus Torvalds page_cache_release(page); 24851da177e4SLinus Torvalds } 24861da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 24871da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 24881da177e4SLinus Torvalds d_instantiate(dentry, inode); 24891da177e4SLinus Torvalds dget(dentry); 24901da177e4SLinus Torvalds return 0; 24911da177e4SLinus Torvalds } 24921da177e4SLinus Torvalds 2493fceef393SAl Viro static void shmem_put_link(void *arg) 2494fceef393SAl Viro { 2495fceef393SAl Viro mark_page_accessed(arg); 2496fceef393SAl Viro put_page(arg); 2497fceef393SAl Viro } 2498fceef393SAl Viro 24996b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 2500fceef393SAl Viro struct inode *inode, 2501fceef393SAl Viro struct delayed_call *done) 25021da177e4SLinus Torvalds { 25031da177e4SLinus Torvalds struct page *page = NULL; 25046b255391SAl Viro int error; 25056a6c9904SAl Viro if (!dentry) { 25066a6c9904SAl Viro page = find_get_page(inode->i_mapping, 0); 25076a6c9904SAl Viro if (!page) 25086b255391SAl Viro return ERR_PTR(-ECHILD); 25096a6c9904SAl Viro if (!PageUptodate(page)) { 25106a6c9904SAl Viro put_page(page); 25116a6c9904SAl Viro return ERR_PTR(-ECHILD); 25126a6c9904SAl Viro } 25136a6c9904SAl Viro } else { 25146b255391SAl Viro error = shmem_getpage(inode, 0, &page, SGP_READ, NULL); 2515680baacbSAl Viro if (error) 2516680baacbSAl Viro return ERR_PTR(error); 2517d3602444SHugh Dickins unlock_page(page); 25181da177e4SLinus Torvalds } 2519fceef393SAl Viro set_delayed_call(done, shmem_put_link, page); 252021fc61c7SAl Viro return page_address(page); 25211da177e4SLinus Torvalds } 25221da177e4SLinus Torvalds 2523b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2524b09e0fa4SEric Paris /* 2525b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 2526b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 2527b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 2528b09e0fa4SEric Paris * filesystem level, though. 2529b09e0fa4SEric Paris */ 2530b09e0fa4SEric Paris 25316d9d88d0SJarkko Sakkinen /* 25326d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 25336d9d88d0SJarkko Sakkinen */ 25346d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 25356d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 25366d9d88d0SJarkko Sakkinen void *fs_info) 25376d9d88d0SJarkko Sakkinen { 25386d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 25396d9d88d0SJarkko Sakkinen const struct xattr *xattr; 254038f38657SAristeu Rozanski struct simple_xattr *new_xattr; 25416d9d88d0SJarkko Sakkinen size_t len; 25426d9d88d0SJarkko Sakkinen 25436d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 254438f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 25456d9d88d0SJarkko Sakkinen if (!new_xattr) 25466d9d88d0SJarkko Sakkinen return -ENOMEM; 25476d9d88d0SJarkko Sakkinen 25486d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 25496d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 25506d9d88d0SJarkko Sakkinen GFP_KERNEL); 25516d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 25526d9d88d0SJarkko Sakkinen kfree(new_xattr); 25536d9d88d0SJarkko Sakkinen return -ENOMEM; 25546d9d88d0SJarkko Sakkinen } 25556d9d88d0SJarkko Sakkinen 25566d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 25576d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 25586d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 25596d9d88d0SJarkko Sakkinen xattr->name, len); 25606d9d88d0SJarkko Sakkinen 256138f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 25626d9d88d0SJarkko Sakkinen } 25636d9d88d0SJarkko Sakkinen 25646d9d88d0SJarkko Sakkinen return 0; 25656d9d88d0SJarkko Sakkinen } 25666d9d88d0SJarkko Sakkinen 2567aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 2568aa7c5241SAndreas Gruenbacher struct dentry *dentry, const char *name, 2569aa7c5241SAndreas Gruenbacher void *buffer, size_t size) 2570aa7c5241SAndreas Gruenbacher { 2571aa7c5241SAndreas Gruenbacher struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 2572aa7c5241SAndreas Gruenbacher 2573aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 2574aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 2575aa7c5241SAndreas Gruenbacher } 2576aa7c5241SAndreas Gruenbacher 2577aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 2578aa7c5241SAndreas Gruenbacher struct dentry *dentry, const char *name, 2579aa7c5241SAndreas Gruenbacher const void *value, size_t size, int flags) 2580aa7c5241SAndreas Gruenbacher { 2581aa7c5241SAndreas Gruenbacher struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 2582aa7c5241SAndreas Gruenbacher 2583aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 2584aa7c5241SAndreas Gruenbacher return simple_xattr_set(&info->xattrs, name, value, size, flags); 2585aa7c5241SAndreas Gruenbacher } 2586aa7c5241SAndreas Gruenbacher 2587aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 2588aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 2589aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 2590aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 2591aa7c5241SAndreas Gruenbacher }; 2592aa7c5241SAndreas Gruenbacher 2593aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 2594aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 2595aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 2596aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 2597aa7c5241SAndreas Gruenbacher }; 2598aa7c5241SAndreas Gruenbacher 2599b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 2600b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 2601feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 2602feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 2603b09e0fa4SEric Paris #endif 2604aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 2605aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 2606b09e0fa4SEric Paris NULL 2607b09e0fa4SEric Paris }; 2608b09e0fa4SEric Paris 2609b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 2610b09e0fa4SEric Paris { 261175c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 2612786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 2613b09e0fa4SEric Paris } 2614b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 2615b09e0fa4SEric Paris 261669f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 26171da177e4SLinus Torvalds .readlink = generic_readlink, 26186b255391SAl Viro .get_link = simple_get_link, 2619b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2620aa7c5241SAndreas Gruenbacher .setxattr = generic_setxattr, 2621aa7c5241SAndreas Gruenbacher .getxattr = generic_getxattr, 2622b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2623aa7c5241SAndreas Gruenbacher .removexattr = generic_removexattr, 2624b09e0fa4SEric Paris #endif 26251da177e4SLinus Torvalds }; 26261da177e4SLinus Torvalds 262792e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 26281da177e4SLinus Torvalds .readlink = generic_readlink, 26296b255391SAl Viro .get_link = shmem_get_link, 2630b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2631aa7c5241SAndreas Gruenbacher .setxattr = generic_setxattr, 2632aa7c5241SAndreas Gruenbacher .getxattr = generic_getxattr, 2633b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2634aa7c5241SAndreas Gruenbacher .removexattr = generic_removexattr, 263539f0247dSAndreas Gruenbacher #endif 2636b09e0fa4SEric Paris }; 263739f0247dSAndreas Gruenbacher 263891828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 263991828a40SDavid M. Grimes { 264091828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 264191828a40SDavid M. Grimes } 264291828a40SDavid M. Grimes 264391828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 264491828a40SDavid M. Grimes { 264591828a40SDavid M. Grimes __u32 *fh = vfh; 264691828a40SDavid M. Grimes __u64 inum = fh[2]; 264791828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 264891828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 264991828a40SDavid M. Grimes } 265091828a40SDavid M. Grimes 2651480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 2652480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 265391828a40SDavid M. Grimes { 265491828a40SDavid M. Grimes struct inode *inode; 2655480b116cSChristoph Hellwig struct dentry *dentry = NULL; 265635c2a7f4SHugh Dickins u64 inum; 265791828a40SDavid M. Grimes 2658480b116cSChristoph Hellwig if (fh_len < 3) 2659480b116cSChristoph Hellwig return NULL; 2660480b116cSChristoph Hellwig 266135c2a7f4SHugh Dickins inum = fid->raw[2]; 266235c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 266335c2a7f4SHugh Dickins 2664480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 2665480b116cSChristoph Hellwig shmem_match, fid->raw); 266691828a40SDavid M. Grimes if (inode) { 2667480b116cSChristoph Hellwig dentry = d_find_alias(inode); 266891828a40SDavid M. Grimes iput(inode); 266991828a40SDavid M. Grimes } 267091828a40SDavid M. Grimes 2671480b116cSChristoph Hellwig return dentry; 267291828a40SDavid M. Grimes } 267391828a40SDavid M. Grimes 2674b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 2675b0b0382bSAl Viro struct inode *parent) 267691828a40SDavid M. Grimes { 26775fe0c237SAneesh Kumar K.V if (*len < 3) { 26785fe0c237SAneesh Kumar K.V *len = 3; 267994e07a75SNamjae Jeon return FILEID_INVALID; 26805fe0c237SAneesh Kumar K.V } 268191828a40SDavid M. Grimes 26821d3382cbSAl Viro if (inode_unhashed(inode)) { 268391828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 268491828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 268591828a40SDavid M. Grimes * time, we need a lock to ensure we only try 268691828a40SDavid M. Grimes * to do it once 268791828a40SDavid M. Grimes */ 268891828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 268991828a40SDavid M. Grimes spin_lock(&lock); 26901d3382cbSAl Viro if (inode_unhashed(inode)) 269191828a40SDavid M. Grimes __insert_inode_hash(inode, 269291828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 269391828a40SDavid M. Grimes spin_unlock(&lock); 269491828a40SDavid M. Grimes } 269591828a40SDavid M. Grimes 269691828a40SDavid M. Grimes fh[0] = inode->i_generation; 269791828a40SDavid M. Grimes fh[1] = inode->i_ino; 269891828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 269991828a40SDavid M. Grimes 270091828a40SDavid M. Grimes *len = 3; 270191828a40SDavid M. Grimes return 1; 270291828a40SDavid M. Grimes } 270391828a40SDavid M. Grimes 270439655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 270591828a40SDavid M. Grimes .get_parent = shmem_get_parent, 270691828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 2707480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 270891828a40SDavid M. Grimes }; 270991828a40SDavid M. Grimes 2710680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 2711680d794bSakpm@linux-foundation.org bool remount) 27121da177e4SLinus Torvalds { 27131da177e4SLinus Torvalds char *this_char, *value, *rest; 271449cd0a5cSGreg Thelen struct mempolicy *mpol = NULL; 27158751e039SEric W. Biederman uid_t uid; 27168751e039SEric W. Biederman gid_t gid; 27171da177e4SLinus Torvalds 2718b00dc3adSHugh Dickins while (options != NULL) { 2719b00dc3adSHugh Dickins this_char = options; 2720b00dc3adSHugh Dickins for (;;) { 2721b00dc3adSHugh Dickins /* 2722b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 2723b00dc3adSHugh Dickins * mount options form a comma-separated list, 2724b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 2725b00dc3adSHugh Dickins */ 2726b00dc3adSHugh Dickins options = strchr(options, ','); 2727b00dc3adSHugh Dickins if (options == NULL) 2728b00dc3adSHugh Dickins break; 2729b00dc3adSHugh Dickins options++; 2730b00dc3adSHugh Dickins if (!isdigit(*options)) { 2731b00dc3adSHugh Dickins options[-1] = '\0'; 2732b00dc3adSHugh Dickins break; 2733b00dc3adSHugh Dickins } 2734b00dc3adSHugh Dickins } 27351da177e4SLinus Torvalds if (!*this_char) 27361da177e4SLinus Torvalds continue; 27371da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 27381da177e4SLinus Torvalds *value++ = 0; 27391da177e4SLinus Torvalds } else { 27401da177e4SLinus Torvalds printk(KERN_ERR 27411da177e4SLinus Torvalds "tmpfs: No value for mount option '%s'\n", 27421da177e4SLinus Torvalds this_char); 274349cd0a5cSGreg Thelen goto error; 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds 27461da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 27471da177e4SLinus Torvalds unsigned long long size; 27481da177e4SLinus Torvalds size = memparse(value,&rest); 27491da177e4SLinus Torvalds if (*rest == '%') { 27501da177e4SLinus Torvalds size <<= PAGE_SHIFT; 27511da177e4SLinus Torvalds size *= totalram_pages; 27521da177e4SLinus Torvalds do_div(size, 100); 27531da177e4SLinus Torvalds rest++; 27541da177e4SLinus Torvalds } 27551da177e4SLinus Torvalds if (*rest) 27561da177e4SLinus Torvalds goto bad_val; 2757680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 2758680d794bSakpm@linux-foundation.org DIV_ROUND_UP(size, PAGE_CACHE_SIZE); 27591da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 2760680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 27611da177e4SLinus Torvalds if (*rest) 27621da177e4SLinus Torvalds goto bad_val; 27631da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 2764680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 27651da177e4SLinus Torvalds if (*rest) 27661da177e4SLinus Torvalds goto bad_val; 27671da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 2768680d794bSakpm@linux-foundation.org if (remount) 27691da177e4SLinus Torvalds continue; 2770680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 27711da177e4SLinus Torvalds if (*rest) 27721da177e4SLinus Torvalds goto bad_val; 27731da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 2774680d794bSakpm@linux-foundation.org if (remount) 27751da177e4SLinus Torvalds continue; 27768751e039SEric W. Biederman uid = simple_strtoul(value, &rest, 0); 27771da177e4SLinus Torvalds if (*rest) 27781da177e4SLinus Torvalds goto bad_val; 27798751e039SEric W. Biederman sbinfo->uid = make_kuid(current_user_ns(), uid); 27808751e039SEric W. Biederman if (!uid_valid(sbinfo->uid)) 27818751e039SEric W. Biederman goto bad_val; 27821da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 2783680d794bSakpm@linux-foundation.org if (remount) 27841da177e4SLinus Torvalds continue; 27858751e039SEric W. Biederman gid = simple_strtoul(value, &rest, 0); 27861da177e4SLinus Torvalds if (*rest) 27871da177e4SLinus Torvalds goto bad_val; 27888751e039SEric W. Biederman sbinfo->gid = make_kgid(current_user_ns(), gid); 27898751e039SEric W. Biederman if (!gid_valid(sbinfo->gid)) 27908751e039SEric W. Biederman goto bad_val; 27917339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 279249cd0a5cSGreg Thelen mpol_put(mpol); 279349cd0a5cSGreg Thelen mpol = NULL; 279449cd0a5cSGreg Thelen if (mpol_parse_str(value, &mpol)) 27957339ff83SRobin Holt goto bad_val; 27961da177e4SLinus Torvalds } else { 27971da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad mount option %s\n", 27981da177e4SLinus Torvalds this_char); 279949cd0a5cSGreg Thelen goto error; 28001da177e4SLinus Torvalds } 28011da177e4SLinus Torvalds } 280249cd0a5cSGreg Thelen sbinfo->mpol = mpol; 28031da177e4SLinus Torvalds return 0; 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds bad_val: 28061da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", 28071da177e4SLinus Torvalds value, this_char); 280849cd0a5cSGreg Thelen error: 280949cd0a5cSGreg Thelen mpol_put(mpol); 28101da177e4SLinus Torvalds return 1; 28111da177e4SLinus Torvalds 28121da177e4SLinus Torvalds } 28131da177e4SLinus Torvalds 28141da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 28151da177e4SLinus Torvalds { 28161da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2817680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 28180edd73b3SHugh Dickins unsigned long inodes; 28190edd73b3SHugh Dickins int error = -EINVAL; 28201da177e4SLinus Torvalds 28215f00110fSGreg Thelen config.mpol = NULL; 2822680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 28230edd73b3SHugh Dickins return error; 28240edd73b3SHugh Dickins 28250edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 28260edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 28277e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 28280edd73b3SHugh Dickins goto out; 2829680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 28300edd73b3SHugh Dickins goto out; 28310edd73b3SHugh Dickins /* 283254af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 28330edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 28340edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 28350edd73b3SHugh Dickins */ 2836680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 28370edd73b3SHugh Dickins goto out; 2838680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 28390edd73b3SHugh Dickins goto out; 28400edd73b3SHugh Dickins 28410edd73b3SHugh Dickins error = 0; 2842680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 2843680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 2844680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 284571fe804bSLee Schermerhorn 28465f00110fSGreg Thelen /* 28475f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 28485f00110fSGreg Thelen */ 28495f00110fSGreg Thelen if (config.mpol) { 285071fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 285171fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 28525f00110fSGreg Thelen } 28530edd73b3SHugh Dickins out: 28540edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 28550edd73b3SHugh Dickins return error; 28561da177e4SLinus Torvalds } 2857680d794bSakpm@linux-foundation.org 285834c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 2859680d794bSakpm@linux-foundation.org { 286034c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 2861680d794bSakpm@linux-foundation.org 2862680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 2863680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 2864680d794bSakpm@linux-foundation.org sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); 2865680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 2866680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 2867680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 286809208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 28698751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 28708751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 28718751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 28728751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 28738751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 28748751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 287571fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 2876680d794bSakpm@linux-foundation.org return 0; 2877680d794bSakpm@linux-foundation.org } 28789183df25SDavid Herrmann 28799183df25SDavid Herrmann #define MFD_NAME_PREFIX "memfd:" 28809183df25SDavid Herrmann #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1) 28819183df25SDavid Herrmann #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN) 28829183df25SDavid Herrmann 28839183df25SDavid Herrmann #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING) 28849183df25SDavid Herrmann 28859183df25SDavid Herrmann SYSCALL_DEFINE2(memfd_create, 28869183df25SDavid Herrmann const char __user *, uname, 28879183df25SDavid Herrmann unsigned int, flags) 28889183df25SDavid Herrmann { 28899183df25SDavid Herrmann struct shmem_inode_info *info; 28909183df25SDavid Herrmann struct file *file; 28919183df25SDavid Herrmann int fd, error; 28929183df25SDavid Herrmann char *name; 28939183df25SDavid Herrmann long len; 28949183df25SDavid Herrmann 28959183df25SDavid Herrmann if (flags & ~(unsigned int)MFD_ALL_FLAGS) 28969183df25SDavid Herrmann return -EINVAL; 28979183df25SDavid Herrmann 28989183df25SDavid Herrmann /* length includes terminating zero */ 28999183df25SDavid Herrmann len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); 29009183df25SDavid Herrmann if (len <= 0) 29019183df25SDavid Herrmann return -EFAULT; 29029183df25SDavid Herrmann if (len > MFD_NAME_MAX_LEN + 1) 29039183df25SDavid Herrmann return -EINVAL; 29049183df25SDavid Herrmann 29059183df25SDavid Herrmann name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY); 29069183df25SDavid Herrmann if (!name) 29079183df25SDavid Herrmann return -ENOMEM; 29089183df25SDavid Herrmann 29099183df25SDavid Herrmann strcpy(name, MFD_NAME_PREFIX); 29109183df25SDavid Herrmann if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) { 29119183df25SDavid Herrmann error = -EFAULT; 29129183df25SDavid Herrmann goto err_name; 29139183df25SDavid Herrmann } 29149183df25SDavid Herrmann 29159183df25SDavid Herrmann /* terminating-zero may have changed after strnlen_user() returned */ 29169183df25SDavid Herrmann if (name[len + MFD_NAME_PREFIX_LEN - 1]) { 29179183df25SDavid Herrmann error = -EFAULT; 29189183df25SDavid Herrmann goto err_name; 29199183df25SDavid Herrmann } 29209183df25SDavid Herrmann 29219183df25SDavid Herrmann fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0); 29229183df25SDavid Herrmann if (fd < 0) { 29239183df25SDavid Herrmann error = fd; 29249183df25SDavid Herrmann goto err_name; 29259183df25SDavid Herrmann } 29269183df25SDavid Herrmann 29279183df25SDavid Herrmann file = shmem_file_setup(name, 0, VM_NORESERVE); 29289183df25SDavid Herrmann if (IS_ERR(file)) { 29299183df25SDavid Herrmann error = PTR_ERR(file); 29309183df25SDavid Herrmann goto err_fd; 29319183df25SDavid Herrmann } 29329183df25SDavid Herrmann info = SHMEM_I(file_inode(file)); 29339183df25SDavid Herrmann file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE; 29349183df25SDavid Herrmann file->f_flags |= O_RDWR | O_LARGEFILE; 29359183df25SDavid Herrmann if (flags & MFD_ALLOW_SEALING) 29369183df25SDavid Herrmann info->seals &= ~F_SEAL_SEAL; 29379183df25SDavid Herrmann 29389183df25SDavid Herrmann fd_install(fd, file); 29399183df25SDavid Herrmann kfree(name); 29409183df25SDavid Herrmann return fd; 29419183df25SDavid Herrmann 29429183df25SDavid Herrmann err_fd: 29439183df25SDavid Herrmann put_unused_fd(fd); 29449183df25SDavid Herrmann err_name: 29459183df25SDavid Herrmann kfree(name); 29469183df25SDavid Herrmann return error; 29479183df25SDavid Herrmann } 29489183df25SDavid Herrmann 2949680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 29501da177e4SLinus Torvalds 29511da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 29521da177e4SLinus Torvalds { 2953602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2954602586a8SHugh Dickins 2955602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 295649cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 2957602586a8SHugh Dickins kfree(sbinfo); 29581da177e4SLinus Torvalds sb->s_fs_info = NULL; 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds 29612b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 29621da177e4SLinus Torvalds { 29631da177e4SLinus Torvalds struct inode *inode; 29640edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 2965680d794bSakpm@linux-foundation.org int err = -ENOMEM; 2966680d794bSakpm@linux-foundation.org 2967680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 2968425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 2969680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 2970680d794bSakpm@linux-foundation.org if (!sbinfo) 2971680d794bSakpm@linux-foundation.org return -ENOMEM; 2972680d794bSakpm@linux-foundation.org 2973680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 297476aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 297576aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 2976680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 29771da177e4SLinus Torvalds 29780edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 29791da177e4SLinus Torvalds /* 29801da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 29811da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 29821da177e4SLinus Torvalds * but the internal instance is left unlimited. 29831da177e4SLinus Torvalds */ 2984ca4e0519SAl Viro if (!(sb->s_flags & MS_KERNMOUNT)) { 2985680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 2986680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 2987680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 2988680d794bSakpm@linux-foundation.org err = -EINVAL; 2989680d794bSakpm@linux-foundation.org goto failed; 2990680d794bSakpm@linux-foundation.org } 2991ca4e0519SAl Viro } else { 2992ca4e0519SAl Viro sb->s_flags |= MS_NOUSER; 29931da177e4SLinus Torvalds } 299491828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 29952f6e38f3SHugh Dickins sb->s_flags |= MS_NOSEC; 29960edd73b3SHugh Dickins #else 29970edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 29980edd73b3SHugh Dickins #endif 29991da177e4SLinus Torvalds 30001da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 3001908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 3002602586a8SHugh Dickins goto failed; 3003680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 30041da177e4SLinus Torvalds 3005285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 30061da177e4SLinus Torvalds sb->s_blocksize = PAGE_CACHE_SIZE; 30071da177e4SLinus Torvalds sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 30081da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 30091da177e4SLinus Torvalds sb->s_op = &shmem_ops; 3010cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 3011b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 301239f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 3013b09e0fa4SEric Paris #endif 3014b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 301539f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 301639f0247dSAndreas Gruenbacher #endif 30170edd73b3SHugh Dickins 3018454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 30191da177e4SLinus Torvalds if (!inode) 30201da177e4SLinus Torvalds goto failed; 3021680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 3022680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 3023318ceed0SAl Viro sb->s_root = d_make_root(inode); 3024318ceed0SAl Viro if (!sb->s_root) 302548fde701SAl Viro goto failed; 30261da177e4SLinus Torvalds return 0; 30271da177e4SLinus Torvalds 30281da177e4SLinus Torvalds failed: 30291da177e4SLinus Torvalds shmem_put_super(sb); 30301da177e4SLinus Torvalds return err; 30311da177e4SLinus Torvalds } 30321da177e4SLinus Torvalds 3033fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 30341da177e4SLinus Torvalds 30351da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 30361da177e4SLinus Torvalds { 303741ffe5d5SHugh Dickins struct shmem_inode_info *info; 303841ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 303941ffe5d5SHugh Dickins if (!info) 30401da177e4SLinus Torvalds return NULL; 304141ffe5d5SHugh Dickins return &info->vfs_inode; 30421da177e4SLinus Torvalds } 30431da177e4SLinus Torvalds 304441ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 3045fa0d7e3dSNick Piggin { 3046fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 3047fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 3048fa0d7e3dSNick Piggin } 3049fa0d7e3dSNick Piggin 30501da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 30511da177e4SLinus Torvalds { 305209208d15SAl Viro if (S_ISREG(inode->i_mode)) 30531da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 305441ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 30551da177e4SLinus Torvalds } 30561da177e4SLinus Torvalds 305741ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 30581da177e4SLinus Torvalds { 305941ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 306041ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 30611da177e4SLinus Torvalds } 30621da177e4SLinus Torvalds 306341ffe5d5SHugh Dickins static int shmem_init_inodecache(void) 30641da177e4SLinus Torvalds { 30651da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 30661da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 306741ffe5d5SHugh Dickins 0, SLAB_PANIC, shmem_init_inode); 30681da177e4SLinus Torvalds return 0; 30691da177e4SLinus Torvalds } 30701da177e4SLinus Torvalds 307141ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 30721da177e4SLinus Torvalds { 30731a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 30741da177e4SLinus Torvalds } 30751da177e4SLinus Torvalds 3076f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 30771da177e4SLinus Torvalds .writepage = shmem_writepage, 307876719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 30791da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3080800d15a5SNick Piggin .write_begin = shmem_write_begin, 3081800d15a5SNick Piggin .write_end = shmem_write_end, 30821da177e4SLinus Torvalds #endif 30831c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 3084304dbdb7SLee Schermerhorn .migratepage = migrate_page, 30851c93923cSAndrew Morton #endif 3086aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 30871da177e4SLinus Torvalds }; 30881da177e4SLinus Torvalds 308915ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 30901da177e4SLinus Torvalds .mmap = shmem_mmap, 30911da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3092220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 30932ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 30948174202bSAl Viro .write_iter = generic_file_write_iter, 30951b061d92SChristoph Hellwig .fsync = noop_fsync, 3096708e3508SHugh Dickins .splice_read = shmem_file_splice_read, 3097f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 309883e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 30991da177e4SLinus Torvalds #endif 31001da177e4SLinus Torvalds }; 31011da177e4SLinus Torvalds 310292e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 310344a30220SYu Zhao .getattr = shmem_getattr, 310494c1e62dSHugh Dickins .setattr = shmem_setattr, 3105b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3106aa7c5241SAndreas Gruenbacher .setxattr = generic_setxattr, 3107aa7c5241SAndreas Gruenbacher .getxattr = generic_getxattr, 3108b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3109aa7c5241SAndreas Gruenbacher .removexattr = generic_removexattr, 3110feda821eSChristoph Hellwig .set_acl = simple_set_acl, 3111b09e0fa4SEric Paris #endif 31121da177e4SLinus Torvalds }; 31131da177e4SLinus Torvalds 311492e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 31151da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 31161da177e4SLinus Torvalds .create = shmem_create, 31171da177e4SLinus Torvalds .lookup = simple_lookup, 31181da177e4SLinus Torvalds .link = shmem_link, 31191da177e4SLinus Torvalds .unlink = shmem_unlink, 31201da177e4SLinus Torvalds .symlink = shmem_symlink, 31211da177e4SLinus Torvalds .mkdir = shmem_mkdir, 31221da177e4SLinus Torvalds .rmdir = shmem_rmdir, 31231da177e4SLinus Torvalds .mknod = shmem_mknod, 31243b69ff51SMiklos Szeredi .rename2 = shmem_rename2, 312560545d0dSAl Viro .tmpfile = shmem_tmpfile, 31261da177e4SLinus Torvalds #endif 3127b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3128aa7c5241SAndreas Gruenbacher .setxattr = generic_setxattr, 3129aa7c5241SAndreas Gruenbacher .getxattr = generic_getxattr, 3130b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3131aa7c5241SAndreas Gruenbacher .removexattr = generic_removexattr, 3132b09e0fa4SEric Paris #endif 313339f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 313494c1e62dSHugh Dickins .setattr = shmem_setattr, 3135feda821eSChristoph Hellwig .set_acl = simple_set_acl, 313639f0247dSAndreas Gruenbacher #endif 313739f0247dSAndreas Gruenbacher }; 313839f0247dSAndreas Gruenbacher 313992e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 3140b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3141aa7c5241SAndreas Gruenbacher .setxattr = generic_setxattr, 3142aa7c5241SAndreas Gruenbacher .getxattr = generic_getxattr, 3143b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3144aa7c5241SAndreas Gruenbacher .removexattr = generic_removexattr, 3145b09e0fa4SEric Paris #endif 314639f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 314794c1e62dSHugh Dickins .setattr = shmem_setattr, 3148feda821eSChristoph Hellwig .set_acl = simple_set_acl, 314939f0247dSAndreas Gruenbacher #endif 31501da177e4SLinus Torvalds }; 31511da177e4SLinus Torvalds 3152759b9775SHugh Dickins static const struct super_operations shmem_ops = { 31531da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 31541da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 31551da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 31561da177e4SLinus Torvalds .statfs = shmem_statfs, 31571da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 3158680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 31591da177e4SLinus Torvalds #endif 31601f895f75SAl Viro .evict_inode = shmem_evict_inode, 31611da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 31621da177e4SLinus Torvalds .put_super = shmem_put_super, 31631da177e4SLinus Torvalds }; 31641da177e4SLinus Torvalds 3165f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 316654cb8821SNick Piggin .fault = shmem_fault, 3167d7c17551SNing Qu .map_pages = filemap_map_pages, 31681da177e4SLinus Torvalds #ifdef CONFIG_NUMA 31691da177e4SLinus Torvalds .set_policy = shmem_set_policy, 31701da177e4SLinus Torvalds .get_policy = shmem_get_policy, 31711da177e4SLinus Torvalds #endif 31721da177e4SLinus Torvalds }; 31731da177e4SLinus Torvalds 31743c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 31753c26ff6eSAl Viro int flags, const char *dev_name, void *data) 31761da177e4SLinus Torvalds { 31773c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 31781da177e4SLinus Torvalds } 31791da177e4SLinus Torvalds 318041ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 31811da177e4SLinus Torvalds .owner = THIS_MODULE, 31821da177e4SLinus Torvalds .name = "tmpfs", 31833c26ff6eSAl Viro .mount = shmem_mount, 31841da177e4SLinus Torvalds .kill_sb = kill_litter_super, 31852b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 31861da177e4SLinus Torvalds }; 31871da177e4SLinus Torvalds 318841ffe5d5SHugh Dickins int __init shmem_init(void) 31891da177e4SLinus Torvalds { 31901da177e4SLinus Torvalds int error; 31911da177e4SLinus Torvalds 319216203a7aSRob Landley /* If rootfs called this, don't re-init */ 319316203a7aSRob Landley if (shmem_inode_cachep) 319416203a7aSRob Landley return 0; 319516203a7aSRob Landley 319641ffe5d5SHugh Dickins error = shmem_init_inodecache(); 31971da177e4SLinus Torvalds if (error) 31981da177e4SLinus Torvalds goto out3; 31991da177e4SLinus Torvalds 320041ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 32011da177e4SLinus Torvalds if (error) { 32021da177e4SLinus Torvalds printk(KERN_ERR "Could not register tmpfs\n"); 32031da177e4SLinus Torvalds goto out2; 32041da177e4SLinus Torvalds } 320595dc112aSGreg Kroah-Hartman 3206ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 32071da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 32081da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 32091da177e4SLinus Torvalds printk(KERN_ERR "Could not kern_mount tmpfs\n"); 32101da177e4SLinus Torvalds goto out1; 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds return 0; 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds out1: 321541ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 32161da177e4SLinus Torvalds out2: 321741ffe5d5SHugh Dickins shmem_destroy_inodecache(); 32181da177e4SLinus Torvalds out3: 32191da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 32201da177e4SLinus Torvalds return error; 32211da177e4SLinus Torvalds } 3222853ac43aSMatt Mackall 3223853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 3224853ac43aSMatt Mackall 3225853ac43aSMatt Mackall /* 3226853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 3227853ac43aSMatt Mackall * 3228853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 3229853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 3230853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 3231853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 3232853ac43aSMatt Mackall */ 3233853ac43aSMatt Mackall 323441ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 3235853ac43aSMatt Mackall .name = "tmpfs", 32363c26ff6eSAl Viro .mount = ramfs_mount, 3237853ac43aSMatt Mackall .kill_sb = kill_litter_super, 32382b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 3239853ac43aSMatt Mackall }; 3240853ac43aSMatt Mackall 324141ffe5d5SHugh Dickins int __init shmem_init(void) 3242853ac43aSMatt Mackall { 324341ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 3244853ac43aSMatt Mackall 324541ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 3246853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 3247853ac43aSMatt Mackall 3248853ac43aSMatt Mackall return 0; 3249853ac43aSMatt Mackall } 3250853ac43aSMatt Mackall 325141ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 3252853ac43aSMatt Mackall { 3253853ac43aSMatt Mackall return 0; 3254853ac43aSMatt Mackall } 3255853ac43aSMatt Mackall 32563f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 32573f96b79aSHugh Dickins { 32583f96b79aSHugh Dickins return 0; 32593f96b79aSHugh Dickins } 32603f96b79aSHugh Dickins 326124513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 326224513264SHugh Dickins { 326324513264SHugh Dickins } 326424513264SHugh Dickins 326541ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 326694c1e62dSHugh Dickins { 326741ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 326894c1e62dSHugh Dickins } 326994c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 327094c1e62dSHugh Dickins 3271853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 32720b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 3273454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 32740b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 32750b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 3276853ac43aSMatt Mackall 3277853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 3278853ac43aSMatt Mackall 3279853ac43aSMatt Mackall /* common code */ 32801da177e4SLinus Torvalds 32813451538aSAl Viro static struct dentry_operations anon_ops = { 3282118b2302SAl Viro .d_dname = simple_dname 32833451538aSAl Viro }; 32843451538aSAl Viro 3285c7277090SEric Paris static struct file *__shmem_file_setup(const char *name, loff_t size, 3286c7277090SEric Paris unsigned long flags, unsigned int i_flags) 32871da177e4SLinus Torvalds { 32886b4d0b27SAl Viro struct file *res; 32891da177e4SLinus Torvalds struct inode *inode; 32902c48b9c4SAl Viro struct path path; 32913451538aSAl Viro struct super_block *sb; 32921da177e4SLinus Torvalds struct qstr this; 32931da177e4SLinus Torvalds 32941da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 32956b4d0b27SAl Viro return ERR_CAST(shm_mnt); 32961da177e4SLinus Torvalds 3297285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 32981da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 32991da177e4SLinus Torvalds 33001da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 33011da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 33021da177e4SLinus Torvalds 33036b4d0b27SAl Viro res = ERR_PTR(-ENOMEM); 33041da177e4SLinus Torvalds this.name = name; 33051da177e4SLinus Torvalds this.len = strlen(name); 33061da177e4SLinus Torvalds this.hash = 0; /* will go */ 33073451538aSAl Viro sb = shm_mnt->mnt_sb; 330866ee4b88SKonstantin Khlebnikov path.mnt = mntget(shm_mnt); 33093451538aSAl Viro path.dentry = d_alloc_pseudo(sb, &this); 33102c48b9c4SAl Viro if (!path.dentry) 33111da177e4SLinus Torvalds goto put_memory; 33123451538aSAl Viro d_set_d_op(path.dentry, &anon_ops); 33131da177e4SLinus Torvalds 33146b4d0b27SAl Viro res = ERR_PTR(-ENOSPC); 33153451538aSAl Viro inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 33161da177e4SLinus Torvalds if (!inode) 331766ee4b88SKonstantin Khlebnikov goto put_memory; 33181da177e4SLinus Torvalds 3319c7277090SEric Paris inode->i_flags |= i_flags; 33202c48b9c4SAl Viro d_instantiate(path.dentry, inode); 33211da177e4SLinus Torvalds inode->i_size = size; 33226d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 332326567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 332426567cdbSAl Viro if (IS_ERR(res)) 332566ee4b88SKonstantin Khlebnikov goto put_path; 33264b42af81SAl Viro 33276b4d0b27SAl Viro res = alloc_file(&path, FMODE_WRITE | FMODE_READ, 33284b42af81SAl Viro &shmem_file_operations); 33296b4d0b27SAl Viro if (IS_ERR(res)) 333066ee4b88SKonstantin Khlebnikov goto put_path; 33314b42af81SAl Viro 33326b4d0b27SAl Viro return res; 33331da177e4SLinus Torvalds 33341da177e4SLinus Torvalds put_memory: 33351da177e4SLinus Torvalds shmem_unacct_size(flags, size); 333666ee4b88SKonstantin Khlebnikov put_path: 333766ee4b88SKonstantin Khlebnikov path_put(&path); 33386b4d0b27SAl Viro return res; 33391da177e4SLinus Torvalds } 3340c7277090SEric Paris 3341c7277090SEric Paris /** 3342c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 3343c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 3344c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 3345e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 3346e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 3347c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 3348c7277090SEric Paris * @size: size to be set for the file 3349c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3350c7277090SEric Paris */ 3351c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 3352c7277090SEric Paris { 3353c7277090SEric Paris return __shmem_file_setup(name, size, flags, S_PRIVATE); 3354c7277090SEric Paris } 3355c7277090SEric Paris 3356c7277090SEric Paris /** 3357c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 3358c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 3359c7277090SEric Paris * @size: size to be set for the file 3360c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3361c7277090SEric Paris */ 3362c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 3363c7277090SEric Paris { 3364c7277090SEric Paris return __shmem_file_setup(name, size, flags, 0); 3365c7277090SEric Paris } 3366395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 33671da177e4SLinus Torvalds 336846711810SRandy Dunlap /** 33691da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 33701da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 33711da177e4SLinus Torvalds */ 33721da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 33731da177e4SLinus Torvalds { 33741da177e4SLinus Torvalds struct file *file; 33751da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 33761da177e4SLinus Torvalds 337766fc1303SHugh Dickins /* 337866fc1303SHugh Dickins * Cloning a new file under mmap_sem leads to a lock ordering conflict 337966fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 338066fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 338166fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 338266fc1303SHugh Dickins */ 338366fc1303SHugh Dickins file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE); 33841da177e4SLinus Torvalds if (IS_ERR(file)) 33851da177e4SLinus Torvalds return PTR_ERR(file); 33861da177e4SLinus Torvalds 33871da177e4SLinus Torvalds if (vma->vm_file) 33881da177e4SLinus Torvalds fput(vma->vm_file); 33891da177e4SLinus Torvalds vma->vm_file = file; 33901da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 33911da177e4SLinus Torvalds return 0; 33921da177e4SLinus Torvalds } 3393d9d90e5eSHugh Dickins 3394d9d90e5eSHugh Dickins /** 3395d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 3396d9d90e5eSHugh Dickins * @mapping: the page's address_space 3397d9d90e5eSHugh Dickins * @index: the page index 3398d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 3399d9d90e5eSHugh Dickins * 3400d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 3401d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 3402d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 3403d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 3404d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 3405d9d90e5eSHugh Dickins * 340668da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 340768da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 3408d9d90e5eSHugh Dickins */ 3409d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 3410d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 3411d9d90e5eSHugh Dickins { 341268da9f05SHugh Dickins #ifdef CONFIG_SHMEM 341368da9f05SHugh Dickins struct inode *inode = mapping->host; 34149276aad6SHugh Dickins struct page *page; 341568da9f05SHugh Dickins int error; 341668da9f05SHugh Dickins 341768da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 341868da9f05SHugh Dickins error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL); 341968da9f05SHugh Dickins if (error) 342068da9f05SHugh Dickins page = ERR_PTR(error); 342168da9f05SHugh Dickins else 342268da9f05SHugh Dickins unlock_page(page); 342368da9f05SHugh Dickins return page; 342468da9f05SHugh Dickins #else 342568da9f05SHugh Dickins /* 342668da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 342768da9f05SHugh Dickins */ 3428d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 342968da9f05SHugh Dickins #endif 3430d9d90e5eSHugh Dickins } 3431d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 3432