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. 90edd73b3SHugh Dickins * Copyright (C) 2002-2005 Hugh Dickins. 100edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 111da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Extended attribute support for tmpfs: 141da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 151da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 161da177e4SLinus Torvalds * 17853ac43aSMatt Mackall * tiny-shmem: 18853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 19853ac43aSMatt Mackall * 201da177e4SLinus Torvalds * This file is released under the GPL. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 23853ac43aSMatt Mackall #include <linux/fs.h> 24853ac43aSMatt Mackall #include <linux/init.h> 25853ac43aSMatt Mackall #include <linux/vfs.h> 26853ac43aSMatt Mackall #include <linux/mount.h> 27caefba17SHugh Dickins #include <linux/pagemap.h> 28853ac43aSMatt Mackall #include <linux/file.h> 29853ac43aSMatt Mackall #include <linux/mm.h> 30853ac43aSMatt Mackall #include <linux/module.h> 31853ac43aSMatt Mackall #include <linux/swap.h> 32853ac43aSMatt Mackall 33853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 34853ac43aSMatt Mackall 35853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 361da177e4SLinus Torvalds /* 371da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 381da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 391da177e4SLinus Torvalds * which makes it a completely usable filesystem. 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds 4239f0247dSAndreas Gruenbacher #include <linux/xattr.h> 43a5694255SChristoph Hellwig #include <linux/exportfs.h> 441c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 4539f0247dSAndreas Gruenbacher #include <linux/generic_acl.h> 461da177e4SLinus Torvalds #include <linux/mman.h> 471da177e4SLinus Torvalds #include <linux/string.h> 481da177e4SLinus Torvalds #include <linux/slab.h> 491da177e4SLinus Torvalds #include <linux/backing-dev.h> 501da177e4SLinus Torvalds #include <linux/shmem_fs.h> 511da177e4SLinus Torvalds #include <linux/writeback.h> 521da177e4SLinus Torvalds #include <linux/blkdev.h> 53bda97eabSHugh Dickins #include <linux/pagevec.h> 5441ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 55708e3508SHugh Dickins #include <linux/splice.h> 561da177e4SLinus Torvalds #include <linux/security.h> 571da177e4SLinus Torvalds #include <linux/swapops.h> 581da177e4SLinus Torvalds #include <linux/mempolicy.h> 591da177e4SLinus Torvalds #include <linux/namei.h> 60b00dc3adSHugh Dickins #include <linux/ctype.h> 61304dbdb7SLee Schermerhorn #include <linux/migrate.h> 62c1f60a5aSChristoph Lameter #include <linux/highmem.h> 63680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 6492562927SMimi Zohar #include <linux/magic.h> 65304dbdb7SLee Schermerhorn 661da177e4SLinus Torvalds #include <asm/uaccess.h> 671da177e4SLinus Torvalds #include <asm/pgtable.h> 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) 701da177e4SLinus Torvalds #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 731da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 741da177e4SLinus Torvalds 75b09e0fa4SEric Paris struct shmem_xattr { 76b09e0fa4SEric Paris struct list_head list; /* anchored by shmem_inode_info->xattr_list */ 77b09e0fa4SEric Paris char *name; /* xattr name */ 78b09e0fa4SEric Paris size_t size; 79b09e0fa4SEric Paris char value[0]; 80b09e0fa4SEric Paris }; 81b09e0fa4SEric Paris 82285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */ 831da177e4SLinus Torvalds enum sgp_type { 841da177e4SLinus Torvalds SGP_READ, /* don't exceed i_size, don't allocate page */ 851da177e4SLinus Torvalds SGP_CACHE, /* don't exceed i_size, may allocate page */ 86a0ee5ec5SHugh Dickins SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ 871da177e4SLinus Torvalds SGP_WRITE, /* may exceed i_size, may allocate page */ 881da177e4SLinus Torvalds }; 891da177e4SLinus Torvalds 90b76db735SAndrew Morton #ifdef CONFIG_TMPFS 91680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 92680d794bSakpm@linux-foundation.org { 93680d794bSakpm@linux-foundation.org return totalram_pages / 2; 94680d794bSakpm@linux-foundation.org } 95680d794bSakpm@linux-foundation.org 96680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 97680d794bSakpm@linux-foundation.org { 98680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 99680d794bSakpm@linux-foundation.org } 100b76db735SAndrew Morton #endif 101680d794bSakpm@linux-foundation.org 10268da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 10368da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type); 10468da9f05SHugh Dickins 10568da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index, 10668da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, int *fault_type) 10768da9f05SHugh Dickins { 10868da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 10968da9f05SHugh Dickins mapping_gfp_mask(inode->i_mapping), fault_type); 11068da9f05SHugh Dickins } 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds return sb->s_fs_info; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds /* 1181da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1191da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1201da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1211da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1221da177e4SLinus Torvalds */ 1231da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1241da177e4SLinus Torvalds { 1250b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 1260b0a0806SHugh Dickins 0 : security_vm_enough_memory_kern(VM_ACCT(size)); 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1301da177e4SLinus Torvalds { 1310b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1321da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* 1361da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 1371da177e4SLinus Torvalds * pages are allocated, in order to allow huge sparse files. 1381da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1391da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1401da177e4SLinus Torvalds */ 1411da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags) 1421da177e4SLinus Torvalds { 1430b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 1440b0a0806SHugh Dickins security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1481da177e4SLinus Torvalds { 1490b0a0806SHugh Dickins if (flags & VM_NORESERVE) 1501da177e4SLinus Torvalds vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 153759b9775SHugh Dickins static const struct super_operations shmem_ops; 154f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 15515ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 15692e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 15792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 15892e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 159f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 1601da177e4SLinus Torvalds 1616c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info __read_mostly = { 1621da177e4SLinus Torvalds .ra_pages = 0, /* No readahead */ 1634f98a2feSRik van Riel .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, 1641da177e4SLinus Torvalds }; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 167cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 1681da177e4SLinus Torvalds 1695b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 1705b04c689SPavel Emelyanov { 1715b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1725b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 1735b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 1745b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 1755b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1765b04c689SPavel Emelyanov return -ENOSPC; 1775b04c689SPavel Emelyanov } 1785b04c689SPavel Emelyanov sbinfo->free_inodes--; 1795b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1805b04c689SPavel Emelyanov } 1815b04c689SPavel Emelyanov return 0; 1825b04c689SPavel Emelyanov } 1835b04c689SPavel Emelyanov 1845b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 1855b04c689SPavel Emelyanov { 1865b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1875b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 1885b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 1895b04c689SPavel Emelyanov sbinfo->free_inodes++; 1905b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1915b04c689SPavel Emelyanov } 1925b04c689SPavel Emelyanov } 1935b04c689SPavel Emelyanov 19446711810SRandy Dunlap /** 19541ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 1961da177e4SLinus Torvalds * @inode: inode to recalc 1971da177e4SLinus Torvalds * 1981da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 1991da177e4SLinus Torvalds * undirtied hole pages behind our back. 2001da177e4SLinus Torvalds * 2011da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2021da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2031da177e4SLinus Torvalds * 2041da177e4SLinus Torvalds * It has to be called with the spinlock held. 2051da177e4SLinus Torvalds */ 2061da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2071da177e4SLinus Torvalds { 2081da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2091da177e4SLinus Torvalds long freed; 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2121da177e4SLinus Torvalds if (freed > 0) { 213*54af6042SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 214*54af6042SHugh Dickins if (sbinfo->max_blocks) 215*54af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -freed); 2161da177e4SLinus Torvalds info->alloced -= freed; 217*54af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2181da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 222285b2c4fSHugh Dickins static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index, 223285b2c4fSHugh Dickins swp_entry_t swap) 2241da177e4SLinus Torvalds { 225285b2c4fSHugh Dickins if (index < SHMEM_NR_DIRECT) 226285b2c4fSHugh Dickins info->i_direct[index] = swap; 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 229285b2c4fSHugh Dickins static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index) 2301da177e4SLinus Torvalds { 231285b2c4fSHugh Dickins return (index < SHMEM_NR_DIRECT) ? 232285b2c4fSHugh Dickins info->i_direct[index] : (swp_entry_t){0}; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2357a5d0fbbSHugh Dickins /* 2367a5d0fbbSHugh Dickins * Replace item expected in radix tree by a new item, while holding tree lock. 2377a5d0fbbSHugh Dickins */ 2387a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping, 2397a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 2407a5d0fbbSHugh Dickins { 2417a5d0fbbSHugh Dickins void **pslot; 2427a5d0fbbSHugh Dickins void *item = NULL; 2437a5d0fbbSHugh Dickins 2447a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 2457a5d0fbbSHugh Dickins pslot = radix_tree_lookup_slot(&mapping->page_tree, index); 2467a5d0fbbSHugh Dickins if (pslot) 2477a5d0fbbSHugh Dickins item = radix_tree_deref_slot_protected(pslot, 2487a5d0fbbSHugh Dickins &mapping->tree_lock); 2497a5d0fbbSHugh Dickins if (item != expected) 2507a5d0fbbSHugh Dickins return -ENOENT; 2517a5d0fbbSHugh Dickins if (replacement) 2527a5d0fbbSHugh Dickins radix_tree_replace_slot(pslot, replacement); 2537a5d0fbbSHugh Dickins else 2547a5d0fbbSHugh Dickins radix_tree_delete(&mapping->page_tree, index); 2557a5d0fbbSHugh Dickins return 0; 2567a5d0fbbSHugh Dickins } 2577a5d0fbbSHugh Dickins 2587a5d0fbbSHugh Dickins /* 25946f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 26046f65ec1SHugh Dickins */ 26146f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 26246f65ec1SHugh Dickins struct address_space *mapping, 26346f65ec1SHugh Dickins pgoff_t index, gfp_t gfp, void *expected) 26446f65ec1SHugh Dickins { 26546f65ec1SHugh Dickins int error; 26646f65ec1SHugh Dickins 26746f65ec1SHugh Dickins VM_BUG_ON(!PageLocked(page)); 26846f65ec1SHugh Dickins VM_BUG_ON(!PageSwapBacked(page)); 26946f65ec1SHugh Dickins 27046f65ec1SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, 27146f65ec1SHugh Dickins gfp & GFP_RECLAIM_MASK); 27246f65ec1SHugh Dickins if (error) 27346f65ec1SHugh Dickins goto out; 27446f65ec1SHugh Dickins if (!expected) 27546f65ec1SHugh Dickins error = radix_tree_preload(gfp & GFP_RECLAIM_MASK); 27646f65ec1SHugh Dickins if (!error) { 27746f65ec1SHugh Dickins page_cache_get(page); 27846f65ec1SHugh Dickins page->mapping = mapping; 27946f65ec1SHugh Dickins page->index = index; 28046f65ec1SHugh Dickins 28146f65ec1SHugh Dickins spin_lock_irq(&mapping->tree_lock); 28246f65ec1SHugh Dickins if (!expected) 28346f65ec1SHugh Dickins error = radix_tree_insert(&mapping->page_tree, 28446f65ec1SHugh Dickins index, page); 28546f65ec1SHugh Dickins else 28646f65ec1SHugh Dickins error = shmem_radix_tree_replace(mapping, index, 28746f65ec1SHugh Dickins expected, page); 28846f65ec1SHugh Dickins if (!error) { 28946f65ec1SHugh Dickins mapping->nrpages++; 29046f65ec1SHugh Dickins __inc_zone_page_state(page, NR_FILE_PAGES); 29146f65ec1SHugh Dickins __inc_zone_page_state(page, NR_SHMEM); 29246f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 29346f65ec1SHugh Dickins } else { 29446f65ec1SHugh Dickins page->mapping = NULL; 29546f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 29646f65ec1SHugh Dickins page_cache_release(page); 29746f65ec1SHugh Dickins } 29846f65ec1SHugh Dickins if (!expected) 29946f65ec1SHugh Dickins radix_tree_preload_end(); 30046f65ec1SHugh Dickins } 30146f65ec1SHugh Dickins if (error) 30246f65ec1SHugh Dickins mem_cgroup_uncharge_cache_page(page); 30346f65ec1SHugh Dickins out: 30446f65ec1SHugh Dickins return error; 30546f65ec1SHugh Dickins } 30646f65ec1SHugh Dickins 30746f65ec1SHugh Dickins /* 3087a5d0fbbSHugh Dickins * Like find_get_pages, but collecting swap entries as well as pages. 3097a5d0fbbSHugh Dickins */ 3107a5d0fbbSHugh Dickins static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping, 3117a5d0fbbSHugh Dickins pgoff_t start, unsigned int nr_pages, 3127a5d0fbbSHugh Dickins struct page **pages, pgoff_t *indices) 3137a5d0fbbSHugh Dickins { 3147a5d0fbbSHugh Dickins unsigned int i; 3157a5d0fbbSHugh Dickins unsigned int ret; 3167a5d0fbbSHugh Dickins unsigned int nr_found; 3177a5d0fbbSHugh Dickins 3187a5d0fbbSHugh Dickins rcu_read_lock(); 3197a5d0fbbSHugh Dickins restart: 3207a5d0fbbSHugh Dickins nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree, 3217a5d0fbbSHugh Dickins (void ***)pages, indices, start, nr_pages); 3227a5d0fbbSHugh Dickins ret = 0; 3237a5d0fbbSHugh Dickins for (i = 0; i < nr_found; i++) { 3247a5d0fbbSHugh Dickins struct page *page; 3257a5d0fbbSHugh Dickins repeat: 3267a5d0fbbSHugh Dickins page = radix_tree_deref_slot((void **)pages[i]); 3277a5d0fbbSHugh Dickins if (unlikely(!page)) 3287a5d0fbbSHugh Dickins continue; 3297a5d0fbbSHugh Dickins if (radix_tree_exception(page)) { 3307a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) 3317a5d0fbbSHugh Dickins goto export; 3327a5d0fbbSHugh Dickins /* radix_tree_deref_retry(page) */ 3337a5d0fbbSHugh Dickins goto restart; 3347a5d0fbbSHugh Dickins } 3357a5d0fbbSHugh Dickins if (!page_cache_get_speculative(page)) 3367a5d0fbbSHugh Dickins goto repeat; 3377a5d0fbbSHugh Dickins 3387a5d0fbbSHugh Dickins /* Has the page moved? */ 3397a5d0fbbSHugh Dickins if (unlikely(page != *((void **)pages[i]))) { 3407a5d0fbbSHugh Dickins page_cache_release(page); 3417a5d0fbbSHugh Dickins goto repeat; 3427a5d0fbbSHugh Dickins } 3437a5d0fbbSHugh Dickins export: 3447a5d0fbbSHugh Dickins indices[ret] = indices[i]; 3457a5d0fbbSHugh Dickins pages[ret] = page; 3467a5d0fbbSHugh Dickins ret++; 3477a5d0fbbSHugh Dickins } 3487a5d0fbbSHugh Dickins if (unlikely(!ret && nr_found)) 3497a5d0fbbSHugh Dickins goto restart; 3507a5d0fbbSHugh Dickins rcu_read_unlock(); 3517a5d0fbbSHugh Dickins return ret; 3527a5d0fbbSHugh Dickins } 3537a5d0fbbSHugh Dickins 3547a5d0fbbSHugh Dickins /* 35546f65ec1SHugh Dickins * Lockless lookup of swap entry in radix tree, avoiding refcount on pages. 35646f65ec1SHugh Dickins */ 35746f65ec1SHugh Dickins static pgoff_t shmem_find_swap(struct address_space *mapping, void *radswap) 35846f65ec1SHugh Dickins { 35946f65ec1SHugh Dickins void **slots[PAGEVEC_SIZE]; 36046f65ec1SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 36146f65ec1SHugh Dickins unsigned int nr_found; 36246f65ec1SHugh Dickins 36346f65ec1SHugh Dickins restart: 36446f65ec1SHugh Dickins nr_found = 1; 36546f65ec1SHugh Dickins indices[0] = -1; 36646f65ec1SHugh Dickins while (nr_found) { 36746f65ec1SHugh Dickins pgoff_t index = indices[nr_found - 1] + 1; 36846f65ec1SHugh Dickins unsigned int i; 36946f65ec1SHugh Dickins 37046f65ec1SHugh Dickins rcu_read_lock(); 37146f65ec1SHugh Dickins nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree, 37246f65ec1SHugh Dickins slots, indices, index, PAGEVEC_SIZE); 37346f65ec1SHugh Dickins for (i = 0; i < nr_found; i++) { 37446f65ec1SHugh Dickins void *item = radix_tree_deref_slot(slots[i]); 37546f65ec1SHugh Dickins if (radix_tree_deref_retry(item)) { 37646f65ec1SHugh Dickins rcu_read_unlock(); 37746f65ec1SHugh Dickins goto restart; 37846f65ec1SHugh Dickins } 37946f65ec1SHugh Dickins if (item == radswap) { 38046f65ec1SHugh Dickins rcu_read_unlock(); 38146f65ec1SHugh Dickins return indices[i]; 38246f65ec1SHugh Dickins } 38346f65ec1SHugh Dickins } 38446f65ec1SHugh Dickins rcu_read_unlock(); 38546f65ec1SHugh Dickins cond_resched(); 38646f65ec1SHugh Dickins } 38746f65ec1SHugh Dickins return -1; 38846f65ec1SHugh Dickins } 38946f65ec1SHugh Dickins 39046f65ec1SHugh Dickins /* 3917a5d0fbbSHugh Dickins * Remove swap entry from radix tree, free the swap and its page cache. 3927a5d0fbbSHugh Dickins */ 3937a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 3947a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 3957a5d0fbbSHugh Dickins { 3967a5d0fbbSHugh Dickins int error; 3977a5d0fbbSHugh Dickins 3987a5d0fbbSHugh Dickins spin_lock_irq(&mapping->tree_lock); 3997a5d0fbbSHugh Dickins error = shmem_radix_tree_replace(mapping, index, radswap, NULL); 4007a5d0fbbSHugh Dickins spin_unlock_irq(&mapping->tree_lock); 4017a5d0fbbSHugh Dickins if (!error) 4027a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 4037a5d0fbbSHugh Dickins return error; 4047a5d0fbbSHugh Dickins } 4057a5d0fbbSHugh Dickins 4067a5d0fbbSHugh Dickins /* 4077a5d0fbbSHugh Dickins * Pagevec may contain swap entries, so shuffle up pages before releasing. 4087a5d0fbbSHugh Dickins */ 4097a5d0fbbSHugh Dickins static void shmem_pagevec_release(struct pagevec *pvec) 4107a5d0fbbSHugh Dickins { 4117a5d0fbbSHugh Dickins int i, j; 4127a5d0fbbSHugh Dickins 4137a5d0fbbSHugh Dickins for (i = 0, j = 0; i < pagevec_count(pvec); i++) { 4147a5d0fbbSHugh Dickins struct page *page = pvec->pages[i]; 4157a5d0fbbSHugh Dickins if (!radix_tree_exceptional_entry(page)) 4167a5d0fbbSHugh Dickins pvec->pages[j++] = page; 4177a5d0fbbSHugh Dickins } 4187a5d0fbbSHugh Dickins pvec->nr = j; 4197a5d0fbbSHugh Dickins pagevec_release(pvec); 4207a5d0fbbSHugh Dickins } 4217a5d0fbbSHugh Dickins 4227a5d0fbbSHugh Dickins /* 4237a5d0fbbSHugh Dickins * Remove range of pages and swap entries from radix tree, and free them. 4247a5d0fbbSHugh Dickins */ 425285b2c4fSHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 4261da177e4SLinus Torvalds { 427285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 4281da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 429285b2c4fSHugh Dickins pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 430bda97eabSHugh Dickins unsigned partial = lstart & (PAGE_CACHE_SIZE - 1); 431285b2c4fSHugh Dickins pgoff_t end = (lend >> PAGE_CACHE_SHIFT); 432bda97eabSHugh Dickins struct pagevec pvec; 4337a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 4347a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 435285b2c4fSHugh Dickins pgoff_t index; 436bda97eabSHugh Dickins int i; 4371da177e4SLinus Torvalds 438bda97eabSHugh Dickins BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1)); 439bda97eabSHugh Dickins 440bda97eabSHugh Dickins pagevec_init(&pvec, 0); 441bda97eabSHugh Dickins index = start; 4427a5d0fbbSHugh Dickins while (index <= end) { 4437a5d0fbbSHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 4447a5d0fbbSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1, 4457a5d0fbbSHugh Dickins pvec.pages, indices); 4467a5d0fbbSHugh Dickins if (!pvec.nr) 4477a5d0fbbSHugh Dickins break; 448bda97eabSHugh Dickins mem_cgroup_uncharge_start(); 449bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 450bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 451bda97eabSHugh Dickins 4527a5d0fbbSHugh Dickins index = indices[i]; 453bda97eabSHugh Dickins if (index > end) 454bda97eabSHugh Dickins break; 455bda97eabSHugh Dickins 4567a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 4577a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 4587a5d0fbbSHugh Dickins index, page); 4597a5d0fbbSHugh Dickins continue; 4607a5d0fbbSHugh Dickins } 4617a5d0fbbSHugh Dickins 462bda97eabSHugh Dickins if (!trylock_page(page)) 463bda97eabSHugh Dickins continue; 4647a5d0fbbSHugh Dickins if (page->mapping == mapping) { 4657a5d0fbbSHugh Dickins VM_BUG_ON(PageWriteback(page)); 466bda97eabSHugh Dickins truncate_inode_page(mapping, page); 4677a5d0fbbSHugh Dickins } 468bda97eabSHugh Dickins unlock_page(page); 469bda97eabSHugh Dickins } 4707a5d0fbbSHugh Dickins shmem_pagevec_release(&pvec); 471bda97eabSHugh Dickins mem_cgroup_uncharge_end(); 472bda97eabSHugh Dickins cond_resched(); 473bda97eabSHugh Dickins index++; 474bda97eabSHugh Dickins } 475bda97eabSHugh Dickins 476bda97eabSHugh Dickins if (partial) { 477bda97eabSHugh Dickins struct page *page = NULL; 478bda97eabSHugh Dickins shmem_getpage(inode, start - 1, &page, SGP_READ, NULL); 479bda97eabSHugh Dickins if (page) { 480bda97eabSHugh Dickins zero_user_segment(page, partial, PAGE_CACHE_SIZE); 481bda97eabSHugh Dickins set_page_dirty(page); 482bda97eabSHugh Dickins unlock_page(page); 483bda97eabSHugh Dickins page_cache_release(page); 484bda97eabSHugh Dickins } 485bda97eabSHugh Dickins } 486bda97eabSHugh Dickins 487bda97eabSHugh Dickins index = start; 488bda97eabSHugh Dickins for ( ; ; ) { 489bda97eabSHugh Dickins cond_resched(); 4907a5d0fbbSHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 4917a5d0fbbSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1, 4927a5d0fbbSHugh Dickins pvec.pages, indices); 4937a5d0fbbSHugh Dickins if (!pvec.nr) { 494bda97eabSHugh Dickins if (index == start) 495bda97eabSHugh Dickins break; 496bda97eabSHugh Dickins index = start; 497bda97eabSHugh Dickins continue; 498bda97eabSHugh Dickins } 4997a5d0fbbSHugh Dickins if (index == start && indices[0] > end) { 5007a5d0fbbSHugh Dickins shmem_pagevec_release(&pvec); 501bda97eabSHugh Dickins break; 502bda97eabSHugh Dickins } 503bda97eabSHugh Dickins mem_cgroup_uncharge_start(); 504bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 505bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 506bda97eabSHugh Dickins 5077a5d0fbbSHugh Dickins index = indices[i]; 508bda97eabSHugh Dickins if (index > end) 509bda97eabSHugh Dickins break; 510bda97eabSHugh Dickins 5117a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 5127a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 5137a5d0fbbSHugh Dickins index, page); 5147a5d0fbbSHugh Dickins continue; 5157a5d0fbbSHugh Dickins } 5167a5d0fbbSHugh Dickins 517bda97eabSHugh Dickins lock_page(page); 5187a5d0fbbSHugh Dickins if (page->mapping == mapping) { 5197a5d0fbbSHugh Dickins VM_BUG_ON(PageWriteback(page)); 520bda97eabSHugh Dickins truncate_inode_page(mapping, page); 5217a5d0fbbSHugh Dickins } 522bda97eabSHugh Dickins unlock_page(page); 523bda97eabSHugh Dickins } 5247a5d0fbbSHugh Dickins shmem_pagevec_release(&pvec); 525bda97eabSHugh Dickins mem_cgroup_uncharge_end(); 526bda97eabSHugh Dickins index++; 527bda97eabSHugh Dickins } 52894c1e62dSHugh Dickins 5291da177e4SLinus Torvalds spin_lock(&info->lock); 5307a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 5311da177e4SLinus Torvalds shmem_recalc_inode(inode); 5321da177e4SLinus Torvalds spin_unlock(&info->lock); 5331da177e4SLinus Torvalds 534285b2c4fSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 5351da177e4SLinus Torvalds } 53694c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 5371da177e4SLinus Torvalds 53894c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 5411da177e4SLinus Torvalds int error; 5421da177e4SLinus Torvalds 543db78b877SChristoph Hellwig error = inode_change_ok(inode, attr); 544db78b877SChristoph Hellwig if (error) 545db78b877SChristoph Hellwig return error; 546db78b877SChristoph Hellwig 54794c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 54894c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 54994c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 5503889e6e7Snpiggin@suse.de 55194c1e62dSHugh Dickins if (newsize != oldsize) { 55294c1e62dSHugh Dickins i_size_write(inode, newsize); 55394c1e62dSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 55494c1e62dSHugh Dickins } 55594c1e62dSHugh Dickins if (newsize < oldsize) { 55694c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 55794c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 55894c1e62dSHugh Dickins shmem_truncate_range(inode, newsize, (loff_t)-1); 55994c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 56094c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 56194c1e62dSHugh Dickins } 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 5646a1a90adSChristoph Hellwig setattr_copy(inode, attr); 56539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 566db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 5671c7c474cSChristoph Hellwig error = generic_acl_chmod(inode); 56839f0247dSAndreas Gruenbacher #endif 5691da177e4SLinus Torvalds return error; 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds 5721f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 5731da177e4SLinus Torvalds { 5741da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 575b09e0fa4SEric Paris struct shmem_xattr *xattr, *nxattr; 5761da177e4SLinus Torvalds 5773889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 5781da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 5791da177e4SLinus Torvalds inode->i_size = 0; 5803889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 5811da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 582cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 5831da177e4SLinus Torvalds list_del_init(&info->swaplist); 584cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds } 587b09e0fa4SEric Paris 588b09e0fa4SEric Paris list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) { 589b09e0fa4SEric Paris kfree(xattr->name); 590b09e0fa4SEric Paris kfree(xattr); 591b09e0fa4SEric Paris } 5921da177e4SLinus Torvalds BUG_ON(inode->i_blocks); 5935b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 5941f895f75SAl Viro end_writeback(inode); 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 59746f65ec1SHugh Dickins /* 59846f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 59946f65ec1SHugh Dickins */ 60041ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 60141ffe5d5SHugh Dickins swp_entry_t swap, struct page *page) 6021da177e4SLinus Torvalds { 603285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 60446f65ec1SHugh Dickins void *radswap; 60541ffe5d5SHugh Dickins pgoff_t index; 606d9fe526aSHugh Dickins int error; 6071da177e4SLinus Torvalds 60846f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 60946f65ec1SHugh Dickins index = shmem_find_swap(mapping, radswap); 61046f65ec1SHugh Dickins if (index == -1) 6111da177e4SLinus Torvalds return 0; 6122e0e26c7SHugh Dickins 6131b1b32f2SHugh Dickins /* 6141b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 6151f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 6161b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 617285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 6181b1b32f2SHugh Dickins */ 6191b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 6202e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 6212e0e26c7SHugh Dickins 622d13d1443SKAMEZAWA Hiroyuki /* 623778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 624778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 625778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 626d13d1443SKAMEZAWA Hiroyuki */ 62746f65ec1SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 62846f65ec1SHugh Dickins GFP_NOWAIT, radswap); 629778dd893SHugh Dickins /* which does mem_cgroup_uncharge_cache_page on error */ 63069029cd5SKAMEZAWA Hiroyuki 63148f170fbSHugh Dickins if (error != -ENOMEM) { 63246f65ec1SHugh Dickins /* 63346f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 63446f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 63546f65ec1SHugh Dickins */ 63673b1262fSHugh Dickins delete_from_swap_cache(page); 63773b1262fSHugh Dickins set_page_dirty(page); 63846f65ec1SHugh Dickins if (!error) { 63946f65ec1SHugh Dickins spin_lock(&info->lock); 640285b2c4fSHugh Dickins info->swapped--; 64146f65ec1SHugh Dickins spin_unlock(&info->lock); 64241ffe5d5SHugh Dickins swap_free(swap); 64346f65ec1SHugh Dickins } 6442e0e26c7SHugh Dickins error = 1; /* not an error, but entry was found */ 6451da177e4SLinus Torvalds } 6462e0e26c7SHugh Dickins return error; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds /* 65046f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 6511da177e4SLinus Torvalds */ 65241ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 6531da177e4SLinus Torvalds { 65441ffe5d5SHugh Dickins struct list_head *this, *next; 6551da177e4SLinus Torvalds struct shmem_inode_info *info; 6561da177e4SLinus Torvalds int found = 0; 657778dd893SHugh Dickins int error; 658778dd893SHugh Dickins 659778dd893SHugh Dickins /* 660778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 661778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 662778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 66346f65ec1SHugh Dickins * shmem_add_to_page_cache() will be called with GFP_NOWAIT. 664778dd893SHugh Dickins */ 665778dd893SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL); 666778dd893SHugh Dickins if (error) 667778dd893SHugh Dickins goto out; 66846f65ec1SHugh Dickins /* No radix_tree_preload: swap entry keeps a place for page in tree */ 6691da177e4SLinus Torvalds 670cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 67141ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 67241ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 673285b2c4fSHugh Dickins if (!info->swapped) { 674285b2c4fSHugh Dickins spin_lock(&info->lock); 675285b2c4fSHugh Dickins if (!info->swapped) 676285b2c4fSHugh Dickins list_del_init(&info->swaplist); 677285b2c4fSHugh Dickins spin_unlock(&info->lock); 678285b2c4fSHugh Dickins } 679285b2c4fSHugh Dickins if (info->swapped) 68041ffe5d5SHugh Dickins found = shmem_unuse_inode(info, swap, page); 681cb5f7b9aSHugh Dickins cond_resched(); 6822e0e26c7SHugh Dickins if (found) 683778dd893SHugh Dickins break; 6841da177e4SLinus Torvalds } 685cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 686778dd893SHugh Dickins 687778dd893SHugh Dickins if (!found) 688778dd893SHugh Dickins mem_cgroup_uncharge_cache_page(page); 689778dd893SHugh Dickins if (found < 0) 690778dd893SHugh Dickins error = found; 691778dd893SHugh Dickins out: 692aaa46865SHugh Dickins unlock_page(page); 693aaa46865SHugh Dickins page_cache_release(page); 694778dd893SHugh Dickins return error; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds /* 6981da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 6991da177e4SLinus Torvalds */ 7001da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 7011da177e4SLinus Torvalds { 7021da177e4SLinus Torvalds struct shmem_inode_info *info; 703285b2c4fSHugh Dickins swp_entry_t swap, oswap; 7041da177e4SLinus Torvalds struct address_space *mapping; 70541ffe5d5SHugh Dickins pgoff_t index; 7061da177e4SLinus Torvalds struct inode *inode; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 7091da177e4SLinus Torvalds mapping = page->mapping; 7101da177e4SLinus Torvalds index = page->index; 7111da177e4SLinus Torvalds inode = mapping->host; 7121da177e4SLinus Torvalds info = SHMEM_I(inode); 7131da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 7141da177e4SLinus Torvalds goto redirty; 715d9fe526aSHugh Dickins if (!total_swap_pages) 7161da177e4SLinus Torvalds goto redirty; 7171da177e4SLinus Torvalds 718d9fe526aSHugh Dickins /* 719d9fe526aSHugh Dickins * shmem_backing_dev_info's capabilities prevent regular writeback or 720d9fe526aSHugh Dickins * sync from ever calling shmem_writepage; but a stacking filesystem 72148f170fbSHugh Dickins * might use ->writepage of its underlying filesystem, in which case 722d9fe526aSHugh Dickins * tmpfs should write out to swap only in response to memory pressure, 72348f170fbSHugh Dickins * and not for the writeback threads or sync. 724d9fe526aSHugh Dickins */ 72548f170fbSHugh Dickins if (!wbc->for_reclaim) { 72648f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 72748f170fbSHugh Dickins goto redirty; 72848f170fbSHugh Dickins } 729285b2c4fSHugh Dickins 730285b2c4fSHugh Dickins /* 7317a5d0fbbSHugh Dickins * Disable even the toy swapping implementation, while we convert 7327a5d0fbbSHugh Dickins * functions one by one to having swap entries in the radix tree. 733285b2c4fSHugh Dickins */ 7347a5d0fbbSHugh Dickins if (index < ULONG_MAX) 735285b2c4fSHugh Dickins goto redirty; 736285b2c4fSHugh Dickins 737d9fe526aSHugh Dickins swap = get_swap_page(); 73848f170fbSHugh Dickins if (!swap.val) 73948f170fbSHugh Dickins goto redirty; 740d9fe526aSHugh Dickins 741b1dea800SHugh Dickins /* 742b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 743b1dea800SHugh Dickins * if it's not already there. Do it now because we cannot take 744b1dea800SHugh Dickins * mutex while holding spinlock, and must do so before the page 745b1dea800SHugh Dickins * is moved to swap cache, when its pagelock no longer protects 746b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 747b1dea800SHugh Dickins * we've taken the spinlock, because shmem_unuse_inode() will 748b1dea800SHugh Dickins * prune a !swapped inode from the swaplist under both locks. 749b1dea800SHugh Dickins */ 750b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 75105bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 75205bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 753b1dea800SHugh Dickins 7541da177e4SLinus Torvalds spin_lock(&info->lock); 755b1dea800SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 756b1dea800SHugh Dickins 757285b2c4fSHugh Dickins oswap = shmem_get_swap(info, index); 758285b2c4fSHugh Dickins if (oswap.val) { 75948f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 760285b2c4fSHugh Dickins free_swap_and_cache(oswap); 761285b2c4fSHugh Dickins shmem_put_swap(info, index, (swp_entry_t){0}); 762285b2c4fSHugh Dickins info->swapped--; 763d9fe526aSHugh Dickins } 764d9fe526aSHugh Dickins shmem_recalc_inode(inode); 7651da177e4SLinus Torvalds 76648f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 7674c73b1bcSMinchan Kim delete_from_page_cache(page); 768285b2c4fSHugh Dickins shmem_put_swap(info, index, swap); 769285b2c4fSHugh Dickins info->swapped++; 770aaa46865SHugh Dickins swap_shmem_alloc(swap); 771826267cfSHugh Dickins spin_unlock(&info->lock); 772d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 7739fab5619SHugh Dickins swap_writepage(page, wbc); 7741da177e4SLinus Torvalds return 0; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds spin_unlock(&info->lock); 778cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, NULL); 7791da177e4SLinus Torvalds redirty: 7801da177e4SLinus Torvalds set_page_dirty(page); 781d9fe526aSHugh Dickins if (wbc->for_reclaim) 782d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 783d9fe526aSHugh Dickins unlock_page(page); 784d9fe526aSHugh Dickins return 0; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds #ifdef CONFIG_NUMA 788680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 78971fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 790680d794bSakpm@linux-foundation.org { 791680d794bSakpm@linux-foundation.org char buffer[64]; 792680d794bSakpm@linux-foundation.org 79371fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 794095f1fc4SLee Schermerhorn return; /* show nothing */ 795095f1fc4SLee Schermerhorn 79671fe804bSLee Schermerhorn mpol_to_str(buffer, sizeof(buffer), mpol, 1); 797095f1fc4SLee Schermerhorn 798095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 799680d794bSakpm@linux-foundation.org } 80071fe804bSLee Schermerhorn 80171fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 80271fe804bSLee Schermerhorn { 80371fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 80471fe804bSLee Schermerhorn if (sbinfo->mpol) { 80571fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 80671fe804bSLee Schermerhorn mpol = sbinfo->mpol; 80771fe804bSLee Schermerhorn mpol_get(mpol); 80871fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 80971fe804bSLee Schermerhorn } 81071fe804bSLee Schermerhorn return mpol; 81171fe804bSLee Schermerhorn } 812680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 813680d794bSakpm@linux-foundation.org 81441ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 81541ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 8161da177e4SLinus Torvalds { 81752cd3b07SLee Schermerhorn struct mempolicy mpol, *spol; 8181da177e4SLinus Torvalds struct vm_area_struct pvma; 8191da177e4SLinus Torvalds 82052cd3b07SLee Schermerhorn spol = mpol_cond_copy(&mpol, 82141ffe5d5SHugh Dickins mpol_shared_policy_lookup(&info->policy, index)); 82252cd3b07SLee Schermerhorn 8231da177e4SLinus Torvalds /* Create a pseudo vma that just contains the policy */ 824c4cc6d07SHugh Dickins pvma.vm_start = 0; 82541ffe5d5SHugh Dickins pvma.vm_pgoff = index; 826c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 82752cd3b07SLee Schermerhorn pvma.vm_policy = spol; 82841ffe5d5SHugh Dickins return swapin_readahead(swap, gfp, &pvma, 0); 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds 83102098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp, 83241ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 8331da177e4SLinus Torvalds { 8341da177e4SLinus Torvalds struct vm_area_struct pvma; 8351da177e4SLinus Torvalds 836c4cc6d07SHugh Dickins /* Create a pseudo vma that just contains the policy */ 837c4cc6d07SHugh Dickins pvma.vm_start = 0; 83841ffe5d5SHugh Dickins pvma.vm_pgoff = index; 839c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 84041ffe5d5SHugh Dickins pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 84152cd3b07SLee Schermerhorn 84252cd3b07SLee Schermerhorn /* 84352cd3b07SLee Schermerhorn * alloc_page_vma() will drop the shared policy reference 84452cd3b07SLee Schermerhorn */ 84552cd3b07SLee Schermerhorn return alloc_page_vma(gfp, &pvma, 0); 8461da177e4SLinus Torvalds } 847680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */ 848680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 84941ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 850680d794bSakpm@linux-foundation.org { 851680d794bSakpm@linux-foundation.org } 852680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 853680d794bSakpm@linux-foundation.org 85441ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 85541ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 8561da177e4SLinus Torvalds { 85741ffe5d5SHugh Dickins return swapin_readahead(swap, gfp, NULL, 0); 8581da177e4SLinus Torvalds } 8591da177e4SLinus Torvalds 86002098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp, 86141ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 8621da177e4SLinus Torvalds { 863e84e2e13SHugh Dickins return alloc_page(gfp); 8641da177e4SLinus Torvalds } 865680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */ 8661da177e4SLinus Torvalds 86771fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) 86871fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 86971fe804bSLee Schermerhorn { 87071fe804bSLee Schermerhorn return NULL; 87171fe804bSLee Schermerhorn } 87271fe804bSLee Schermerhorn #endif 87371fe804bSLee Schermerhorn 8741da177e4SLinus Torvalds /* 87568da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 8761da177e4SLinus Torvalds * 8771da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 8781da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 8791da177e4SLinus Torvalds * entry since a page cannot live in both the swap and page cache 8801da177e4SLinus Torvalds */ 88141ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 88268da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) 8831da177e4SLinus Torvalds { 8841da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 885*54af6042SHugh Dickins struct shmem_inode_info *info; 8861da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 88727ab7006SHugh Dickins struct page *page; 8881da177e4SLinus Torvalds swp_entry_t swap; 8891da177e4SLinus Torvalds int error; 890*54af6042SHugh Dickins int once = 0; 8911da177e4SLinus Torvalds 89241ffe5d5SHugh Dickins if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT)) 8931da177e4SLinus Torvalds return -EFBIG; 8941da177e4SLinus Torvalds repeat: 895*54af6042SHugh Dickins swap.val = 0; 89641ffe5d5SHugh Dickins page = find_lock_page(mapping, index); 897*54af6042SHugh Dickins if (radix_tree_exceptional_entry(page)) { 898*54af6042SHugh Dickins swap = radix_to_swp_entry(page); 899*54af6042SHugh Dickins page = NULL; 900*54af6042SHugh Dickins } 901*54af6042SHugh Dickins 902*54af6042SHugh Dickins if (sgp != SGP_WRITE && 903*54af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 904*54af6042SHugh Dickins error = -EINVAL; 905*54af6042SHugh Dickins goto failed; 906*54af6042SHugh Dickins } 907*54af6042SHugh Dickins 908*54af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 90927ab7006SHugh Dickins /* 91027ab7006SHugh Dickins * Once we can get the page lock, it must be uptodate: 91127ab7006SHugh Dickins * if there were an error in reading back from swap, 91227ab7006SHugh Dickins * the page would not be inserted into the filecache. 91327ab7006SHugh Dickins */ 914*54af6042SHugh Dickins BUG_ON(page && !PageUptodate(page)); 915*54af6042SHugh Dickins *pagep = page; 916*54af6042SHugh Dickins return 0; 91727ab7006SHugh Dickins } 91827ab7006SHugh Dickins 919b409f9fcSHugh Dickins /* 920*54af6042SHugh Dickins * Fast cache lookup did not find it: 921*54af6042SHugh Dickins * bring it back from swap or allocate. 922b409f9fcSHugh Dickins */ 923*54af6042SHugh Dickins info = SHMEM_I(inode); 924*54af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 92527ab7006SHugh Dickins 9261da177e4SLinus Torvalds if (swap.val) { 9271da177e4SLinus Torvalds /* Look it up and read it in.. */ 92827ab7006SHugh Dickins page = lookup_swap_cache(swap); 92927ab7006SHugh Dickins if (!page) { 930456f998eSYing Han /* here we actually do the io */ 93168da9f05SHugh Dickins if (fault_type) 93268da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 93341ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 93427ab7006SHugh Dickins if (!page) { 9351da177e4SLinus Torvalds error = -ENOMEM; 936*54af6042SHugh Dickins goto failed; 937285b2c4fSHugh Dickins } 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 941*54af6042SHugh Dickins lock_page(page); 94227ab7006SHugh Dickins if (!PageUptodate(page)) { 9431da177e4SLinus Torvalds error = -EIO; 944*54af6042SHugh Dickins goto failed; 945*54af6042SHugh Dickins } 946*54af6042SHugh Dickins wait_on_page_writeback(page); 947*54af6042SHugh Dickins 948*54af6042SHugh Dickins /* Someone may have already done it for us */ 949*54af6042SHugh Dickins if (page->mapping) { 950*54af6042SHugh Dickins if (page->mapping == mapping && 951*54af6042SHugh Dickins page->index == index) 952*54af6042SHugh Dickins goto done; 953*54af6042SHugh Dickins error = -EEXIST; 954*54af6042SHugh Dickins goto failed; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 957*54af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 958*54af6042SHugh Dickins gfp, swp_to_radix_entry(swap)); 959*54af6042SHugh Dickins if (error) 960*54af6042SHugh Dickins goto failed; 961*54af6042SHugh Dickins 962*54af6042SHugh Dickins spin_lock(&info->lock); 963*54af6042SHugh Dickins info->swapped--; 964*54af6042SHugh Dickins shmem_recalc_inode(inode); 9651da177e4SLinus Torvalds spin_unlock(&info->lock); 96627ab7006SHugh Dickins 96727ab7006SHugh Dickins delete_from_swap_cache(page); 96827ab7006SHugh Dickins set_page_dirty(page); 96927ab7006SHugh Dickins swap_free(swap); 97027ab7006SHugh Dickins 971*54af6042SHugh Dickins } else { 972*54af6042SHugh Dickins if (shmem_acct_block(info->flags)) { 973*54af6042SHugh Dickins error = -ENOSPC; 974*54af6042SHugh Dickins goto failed; 9751da177e4SLinus Torvalds } 9760edd73b3SHugh Dickins if (sbinfo->max_blocks) { 977fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 978*54af6042SHugh Dickins sbinfo->max_blocks) >= 0) { 979*54af6042SHugh Dickins error = -ENOSPC; 980*54af6042SHugh Dickins goto unacct; 981*54af6042SHugh Dickins } 9827e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 98359a16eadSHugh Dickins } 9841da177e4SLinus Torvalds 985*54af6042SHugh Dickins page = shmem_alloc_page(gfp, info, index); 986*54af6042SHugh Dickins if (!page) { 987*54af6042SHugh Dickins error = -ENOMEM; 988*54af6042SHugh Dickins goto decused; 989*54af6042SHugh Dickins } 990*54af6042SHugh Dickins 991*54af6042SHugh Dickins SetPageSwapBacked(page); 992*54af6042SHugh Dickins __set_page_locked(page); 993*54af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 994*54af6042SHugh Dickins gfp, NULL); 995*54af6042SHugh Dickins if (error) 996*54af6042SHugh Dickins goto decused; 997*54af6042SHugh Dickins lru_cache_add_anon(page); 998*54af6042SHugh Dickins 999*54af6042SHugh Dickins spin_lock(&info->lock); 10001da177e4SLinus Torvalds info->alloced++; 1001*54af6042SHugh Dickins inode->i_blocks += BLOCKS_PER_PAGE; 1002*54af6042SHugh Dickins shmem_recalc_inode(inode); 100359a16eadSHugh Dickins spin_unlock(&info->lock); 1004*54af6042SHugh Dickins 100527ab7006SHugh Dickins clear_highpage(page); 100627ab7006SHugh Dickins flush_dcache_page(page); 100727ab7006SHugh Dickins SetPageUptodate(page); 1008a0ee5ec5SHugh Dickins if (sgp == SGP_DIRTY) 100927ab7006SHugh Dickins set_page_dirty(page); 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds done: 1012*54af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 1013*54af6042SHugh Dickins if (sgp != SGP_WRITE && 1014*54af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 1015*54af6042SHugh Dickins error = -EINVAL; 1016*54af6042SHugh Dickins goto trunc; 1017ff36b801SShaohua Li } 1018*54af6042SHugh Dickins *pagep = page; 1019*54af6042SHugh Dickins return 0; 1020d00806b1SNick Piggin 1021d0217ac0SNick Piggin /* 1022*54af6042SHugh Dickins * Error recovery. 10231da177e4SLinus Torvalds */ 1024*54af6042SHugh Dickins trunc: 1025*54af6042SHugh Dickins ClearPageDirty(page); 1026*54af6042SHugh Dickins delete_from_page_cache(page); 1027*54af6042SHugh Dickins spin_lock(&info->lock); 1028*54af6042SHugh Dickins info->alloced--; 1029*54af6042SHugh Dickins inode->i_blocks -= BLOCKS_PER_PAGE; 10301da177e4SLinus Torvalds spin_unlock(&info->lock); 1031*54af6042SHugh Dickins decused: 1032*54af6042SHugh Dickins if (sbinfo->max_blocks) 1033*54af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -1); 1034*54af6042SHugh Dickins unacct: 1035*54af6042SHugh Dickins shmem_unacct_blocks(info->flags, 1); 1036*54af6042SHugh Dickins failed: 1037*54af6042SHugh Dickins if (swap.val && error != -EINVAL) { 1038*54af6042SHugh Dickins struct page *test = find_get_page(mapping, index); 1039*54af6042SHugh Dickins if (test && !radix_tree_exceptional_entry(test)) 1040*54af6042SHugh Dickins page_cache_release(test); 1041*54af6042SHugh Dickins /* Have another try if the entry has changed */ 1042*54af6042SHugh Dickins if (test != swp_to_radix_entry(swap)) 1043*54af6042SHugh Dickins error = -EEXIST; 1044*54af6042SHugh Dickins } 104527ab7006SHugh Dickins if (page) { 1046*54af6042SHugh Dickins unlock_page(page); 10471da177e4SLinus Torvalds page_cache_release(page); 1048*54af6042SHugh Dickins } 1049*54af6042SHugh Dickins if (error == -ENOSPC && !once++) { 1050*54af6042SHugh Dickins info = SHMEM_I(inode); 1051*54af6042SHugh Dickins spin_lock(&info->lock); 1052*54af6042SHugh Dickins shmem_recalc_inode(inode); 1053*54af6042SHugh Dickins spin_unlock(&info->lock); 10541da177e4SLinus Torvalds goto repeat; 1055d8dc74f2SAdrian Bunk } 1056*54af6042SHugh Dickins if (error == -EEXIST) 1057*54af6042SHugh Dickins goto repeat; 1058*54af6042SHugh Dickins return error; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 10621da177e4SLinus Torvalds { 10631da177e4SLinus Torvalds struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 10641da177e4SLinus Torvalds int error; 106568da9f05SHugh Dickins int ret = VM_FAULT_LOCKED; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); 10681da177e4SLinus Torvalds if (error) 10691da177e4SLinus Torvalds return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 107068da9f05SHugh Dickins 1071456f998eSYing Han if (ret & VM_FAULT_MAJOR) { 1072456f998eSYing Han count_vm_event(PGMAJFAULT); 1073456f998eSYing Han mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 1074456f998eSYing Han } 107568da9f05SHugh Dickins return ret; 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds #ifdef CONFIG_NUMA 107941ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 10801da177e4SLinus Torvalds { 108141ffe5d5SHugh Dickins struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 108241ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 1085d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 1086d8dc74f2SAdrian Bunk unsigned long addr) 10871da177e4SLinus Torvalds { 108841ffe5d5SHugh Dickins struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 108941ffe5d5SHugh Dickins pgoff_t index; 10901da177e4SLinus Torvalds 109141ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 109241ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds #endif 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 10971da177e4SLinus Torvalds { 1098d3ac7f89SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 10991da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 11001da177e4SLinus Torvalds int retval = -ENOMEM; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds spin_lock(&info->lock); 11031da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 11041da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 11051da177e4SLinus Torvalds goto out_nomem; 11061da177e4SLinus Torvalds info->flags |= VM_LOCKED; 110789e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 11101da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 11111da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 111289e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 111389e004eaSLee Schermerhorn scan_mapping_unevictable_pages(file->f_mapping); 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds retval = 0; 111689e004eaSLee Schermerhorn 11171da177e4SLinus Torvalds out_nomem: 11181da177e4SLinus Torvalds spin_unlock(&info->lock); 11191da177e4SLinus Torvalds return retval; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds 11229b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 11231da177e4SLinus Torvalds { 11241da177e4SLinus Torvalds file_accessed(file); 11251da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 1126d0217ac0SNick Piggin vma->vm_flags |= VM_CAN_NONLINEAR; 11271da177e4SLinus Torvalds return 0; 11281da177e4SLinus Torvalds } 11291da177e4SLinus Torvalds 1130454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 1131454abafeSDmitry Monakhov int mode, dev_t dev, unsigned long flags) 11321da177e4SLinus Torvalds { 11331da177e4SLinus Torvalds struct inode *inode; 11341da177e4SLinus Torvalds struct shmem_inode_info *info; 11351da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 11361da177e4SLinus Torvalds 11375b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 11381da177e4SLinus Torvalds return NULL; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds inode = new_inode(sb); 11411da177e4SLinus Torvalds if (inode) { 114285fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 1143454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 11441da177e4SLinus Torvalds inode->i_blocks = 0; 11451da177e4SLinus Torvalds inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; 11461da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 114791828a40SDavid M. Grimes inode->i_generation = get_seconds(); 11481da177e4SLinus Torvalds info = SHMEM_I(inode); 11491da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 11501da177e4SLinus Torvalds spin_lock_init(&info->lock); 11510b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 11521da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 1153b09e0fa4SEric Paris INIT_LIST_HEAD(&info->xattr_list); 115472c04902SAl Viro cache_no_acl(inode); 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds switch (mode & S_IFMT) { 11571da177e4SLinus Torvalds default: 115839f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 11591da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 11601da177e4SLinus Torvalds break; 11611da177e4SLinus Torvalds case S_IFREG: 116214fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 11631da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 11641da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 116571fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 116671fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 11671da177e4SLinus Torvalds break; 11681da177e4SLinus Torvalds case S_IFDIR: 1169d8c76e6fSDave Hansen inc_nlink(inode); 11701da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 11711da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 11721da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 11731da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 11741da177e4SLinus Torvalds break; 11751da177e4SLinus Torvalds case S_IFLNK: 11761da177e4SLinus Torvalds /* 11771da177e4SLinus Torvalds * Must not load anything in the rbtree, 11781da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 11791da177e4SLinus Torvalds */ 118071fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 11811da177e4SLinus Torvalds break; 11821da177e4SLinus Torvalds } 11835b04c689SPavel Emelyanov } else 11845b04c689SPavel Emelyanov shmem_free_inode(sb); 11851da177e4SLinus Torvalds return inode; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 118992e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 119092e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations; 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds static int 1193800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 1194800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 1195800d15a5SNick Piggin struct page **pagep, void **fsdata) 11961da177e4SLinus Torvalds { 1197800d15a5SNick Piggin struct inode *inode = mapping->host; 1198800d15a5SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 1199800d15a5SNick Piggin return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); 1200800d15a5SNick Piggin } 1201800d15a5SNick Piggin 1202800d15a5SNick Piggin static int 1203800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 1204800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 1205800d15a5SNick Piggin struct page *page, void *fsdata) 1206800d15a5SNick Piggin { 1207800d15a5SNick Piggin struct inode *inode = mapping->host; 1208800d15a5SNick Piggin 1209800d15a5SNick Piggin if (pos + copied > inode->i_size) 1210800d15a5SNick Piggin i_size_write(inode, pos + copied); 1211800d15a5SNick Piggin 1212d3602444SHugh Dickins set_page_dirty(page); 12136746aff7SWu Fengguang unlock_page(page); 1214d3602444SHugh Dickins page_cache_release(page); 1215d3602444SHugh Dickins 1216800d15a5SNick Piggin return copied; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) 12201da177e4SLinus Torvalds { 1221d3ac7f89SJosef "Jeff" Sipek struct inode *inode = filp->f_path.dentry->d_inode; 12221da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 122341ffe5d5SHugh Dickins pgoff_t index; 122441ffe5d5SHugh Dickins unsigned long offset; 1225a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 1226a0ee5ec5SHugh Dickins 1227a0ee5ec5SHugh Dickins /* 1228a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 1229a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 1230a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 1231a0ee5ec5SHugh Dickins */ 1232a0ee5ec5SHugh Dickins if (segment_eq(get_fs(), KERNEL_DS)) 1233a0ee5ec5SHugh Dickins sgp = SGP_DIRTY; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds index = *ppos >> PAGE_CACHE_SHIFT; 12361da177e4SLinus Torvalds offset = *ppos & ~PAGE_CACHE_MASK; 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds for (;;) { 12391da177e4SLinus Torvalds struct page *page = NULL; 124041ffe5d5SHugh Dickins pgoff_t end_index; 124141ffe5d5SHugh Dickins unsigned long nr, ret; 12421da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 12451da177e4SLinus Torvalds if (index > end_index) 12461da177e4SLinus Torvalds break; 12471da177e4SLinus Torvalds if (index == end_index) { 12481da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 12491da177e4SLinus Torvalds if (nr <= offset) 12501da177e4SLinus Torvalds break; 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 1253a0ee5ec5SHugh Dickins desc->error = shmem_getpage(inode, index, &page, sgp, NULL); 12541da177e4SLinus Torvalds if (desc->error) { 12551da177e4SLinus Torvalds if (desc->error == -EINVAL) 12561da177e4SLinus Torvalds desc->error = 0; 12571da177e4SLinus Torvalds break; 12581da177e4SLinus Torvalds } 1259d3602444SHugh Dickins if (page) 1260d3602444SHugh Dickins unlock_page(page); 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds /* 12631da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 12641b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 12651da177e4SLinus Torvalds */ 12661da177e4SLinus Torvalds nr = PAGE_CACHE_SIZE; 12671da177e4SLinus Torvalds i_size = i_size_read(inode); 12681da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 12691da177e4SLinus Torvalds if (index == end_index) { 12701da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 12711da177e4SLinus Torvalds if (nr <= offset) { 12721da177e4SLinus Torvalds if (page) 12731da177e4SLinus Torvalds page_cache_release(page); 12741da177e4SLinus Torvalds break; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds nr -= offset; 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds if (page) { 12801da177e4SLinus Torvalds /* 12811da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 12821da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 12831da177e4SLinus Torvalds * before reading the page on the kernel side. 12841da177e4SLinus Torvalds */ 12851da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 12861da177e4SLinus Torvalds flush_dcache_page(page); 12871da177e4SLinus Torvalds /* 12881da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 12891da177e4SLinus Torvalds */ 12901da177e4SLinus Torvalds if (!offset) 12911da177e4SLinus Torvalds mark_page_accessed(page); 1292b5810039SNick Piggin } else { 12931da177e4SLinus Torvalds page = ZERO_PAGE(0); 1294b5810039SNick Piggin page_cache_get(page); 1295b5810039SNick Piggin } 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds /* 12981da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 12991da177e4SLinus Torvalds * now we can copy it to user space... 13001da177e4SLinus Torvalds * 13011da177e4SLinus Torvalds * The actor routine returns how many bytes were actually used.. 13021da177e4SLinus Torvalds * NOTE! This may not be the same as how much of a user buffer 13031da177e4SLinus Torvalds * we filled up (we may be padding etc), so we can only update 13041da177e4SLinus Torvalds * "pos" here (the actor routine has to update the user buffer 13051da177e4SLinus Torvalds * pointers and the remaining count). 13061da177e4SLinus Torvalds */ 13071da177e4SLinus Torvalds ret = actor(desc, page, offset, nr); 13081da177e4SLinus Torvalds offset += ret; 13091da177e4SLinus Torvalds index += offset >> PAGE_CACHE_SHIFT; 13101da177e4SLinus Torvalds offset &= ~PAGE_CACHE_MASK; 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds page_cache_release(page); 13131da177e4SLinus Torvalds if (ret != nr || !desc->count) 13141da177e4SLinus Torvalds break; 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds cond_resched(); 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; 13201da177e4SLinus Torvalds file_accessed(filp); 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 1323bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb, 1324bcd78e49SHugh Dickins const struct iovec *iov, unsigned long nr_segs, loff_t pos) 13251da177e4SLinus Torvalds { 1326bcd78e49SHugh Dickins struct file *filp = iocb->ki_filp; 1327bcd78e49SHugh Dickins ssize_t retval; 1328bcd78e49SHugh Dickins unsigned long seg; 1329bcd78e49SHugh Dickins size_t count; 1330bcd78e49SHugh Dickins loff_t *ppos = &iocb->ki_pos; 1331bcd78e49SHugh Dickins 1332bcd78e49SHugh Dickins retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); 1333bcd78e49SHugh Dickins if (retval) 1334bcd78e49SHugh Dickins return retval; 1335bcd78e49SHugh Dickins 1336bcd78e49SHugh Dickins for (seg = 0; seg < nr_segs; seg++) { 13371da177e4SLinus Torvalds read_descriptor_t desc; 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds desc.written = 0; 1340bcd78e49SHugh Dickins desc.arg.buf = iov[seg].iov_base; 1341bcd78e49SHugh Dickins desc.count = iov[seg].iov_len; 1342bcd78e49SHugh Dickins if (desc.count == 0) 1343bcd78e49SHugh Dickins continue; 13441da177e4SLinus Torvalds desc.error = 0; 13451da177e4SLinus Torvalds do_shmem_file_read(filp, ppos, &desc, file_read_actor); 1346bcd78e49SHugh Dickins retval += desc.written; 1347bcd78e49SHugh Dickins if (desc.error) { 1348bcd78e49SHugh Dickins retval = retval ?: desc.error; 1349bcd78e49SHugh Dickins break; 1350bcd78e49SHugh Dickins } 1351bcd78e49SHugh Dickins if (desc.count > 0) 1352bcd78e49SHugh Dickins break; 1353bcd78e49SHugh Dickins } 1354bcd78e49SHugh Dickins return retval; 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds 1357708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 1358708e3508SHugh Dickins struct pipe_inode_info *pipe, size_t len, 1359708e3508SHugh Dickins unsigned int flags) 1360708e3508SHugh Dickins { 1361708e3508SHugh Dickins struct address_space *mapping = in->f_mapping; 136271f0e07aSHugh Dickins struct inode *inode = mapping->host; 1363708e3508SHugh Dickins unsigned int loff, nr_pages, req_pages; 1364708e3508SHugh Dickins struct page *pages[PIPE_DEF_BUFFERS]; 1365708e3508SHugh Dickins struct partial_page partial[PIPE_DEF_BUFFERS]; 1366708e3508SHugh Dickins struct page *page; 1367708e3508SHugh Dickins pgoff_t index, end_index; 1368708e3508SHugh Dickins loff_t isize, left; 1369708e3508SHugh Dickins int error, page_nr; 1370708e3508SHugh Dickins struct splice_pipe_desc spd = { 1371708e3508SHugh Dickins .pages = pages, 1372708e3508SHugh Dickins .partial = partial, 1373708e3508SHugh Dickins .flags = flags, 1374708e3508SHugh Dickins .ops = &page_cache_pipe_buf_ops, 1375708e3508SHugh Dickins .spd_release = spd_release_page, 1376708e3508SHugh Dickins }; 1377708e3508SHugh Dickins 137871f0e07aSHugh Dickins isize = i_size_read(inode); 1379708e3508SHugh Dickins if (unlikely(*ppos >= isize)) 1380708e3508SHugh Dickins return 0; 1381708e3508SHugh Dickins 1382708e3508SHugh Dickins left = isize - *ppos; 1383708e3508SHugh Dickins if (unlikely(left < len)) 1384708e3508SHugh Dickins len = left; 1385708e3508SHugh Dickins 1386708e3508SHugh Dickins if (splice_grow_spd(pipe, &spd)) 1387708e3508SHugh Dickins return -ENOMEM; 1388708e3508SHugh Dickins 1389708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1390708e3508SHugh Dickins loff = *ppos & ~PAGE_CACHE_MASK; 1391708e3508SHugh Dickins req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1392708e3508SHugh Dickins nr_pages = min(req_pages, pipe->buffers); 1393708e3508SHugh Dickins 1394708e3508SHugh Dickins spd.nr_pages = find_get_pages_contig(mapping, index, 1395708e3508SHugh Dickins nr_pages, spd.pages); 1396708e3508SHugh Dickins index += spd.nr_pages; 1397708e3508SHugh Dickins error = 0; 139871f0e07aSHugh Dickins 1399708e3508SHugh Dickins while (spd.nr_pages < nr_pages) { 140071f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); 140171f0e07aSHugh Dickins if (error) 1402708e3508SHugh Dickins break; 1403708e3508SHugh Dickins unlock_page(page); 1404708e3508SHugh Dickins spd.pages[spd.nr_pages++] = page; 1405708e3508SHugh Dickins index++; 1406708e3508SHugh Dickins } 1407708e3508SHugh Dickins 1408708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1409708e3508SHugh Dickins nr_pages = spd.nr_pages; 1410708e3508SHugh Dickins spd.nr_pages = 0; 141171f0e07aSHugh Dickins 1412708e3508SHugh Dickins for (page_nr = 0; page_nr < nr_pages; page_nr++) { 1413708e3508SHugh Dickins unsigned int this_len; 1414708e3508SHugh Dickins 1415708e3508SHugh Dickins if (!len) 1416708e3508SHugh Dickins break; 1417708e3508SHugh Dickins 1418708e3508SHugh Dickins this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 1419708e3508SHugh Dickins page = spd.pages[page_nr]; 1420708e3508SHugh Dickins 142171f0e07aSHugh Dickins if (!PageUptodate(page) || page->mapping != mapping) { 142271f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, 142371f0e07aSHugh Dickins SGP_CACHE, NULL); 142471f0e07aSHugh Dickins if (error) 1425708e3508SHugh Dickins break; 142671f0e07aSHugh Dickins unlock_page(page); 1427708e3508SHugh Dickins page_cache_release(spd.pages[page_nr]); 1428708e3508SHugh Dickins spd.pages[page_nr] = page; 1429708e3508SHugh Dickins } 1430708e3508SHugh Dickins 143171f0e07aSHugh Dickins isize = i_size_read(inode); 1432708e3508SHugh Dickins end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 1433708e3508SHugh Dickins if (unlikely(!isize || index > end_index)) 1434708e3508SHugh Dickins break; 1435708e3508SHugh Dickins 1436708e3508SHugh Dickins if (end_index == index) { 1437708e3508SHugh Dickins unsigned int plen; 1438708e3508SHugh Dickins 1439708e3508SHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 1440708e3508SHugh Dickins if (plen <= loff) 1441708e3508SHugh Dickins break; 1442708e3508SHugh Dickins 1443708e3508SHugh Dickins this_len = min(this_len, plen - loff); 1444708e3508SHugh Dickins len = this_len; 1445708e3508SHugh Dickins } 1446708e3508SHugh Dickins 1447708e3508SHugh Dickins spd.partial[page_nr].offset = loff; 1448708e3508SHugh Dickins spd.partial[page_nr].len = this_len; 1449708e3508SHugh Dickins len -= this_len; 1450708e3508SHugh Dickins loff = 0; 1451708e3508SHugh Dickins spd.nr_pages++; 1452708e3508SHugh Dickins index++; 1453708e3508SHugh Dickins } 1454708e3508SHugh Dickins 1455708e3508SHugh Dickins while (page_nr < nr_pages) 1456708e3508SHugh Dickins page_cache_release(spd.pages[page_nr++]); 1457708e3508SHugh Dickins 1458708e3508SHugh Dickins if (spd.nr_pages) 1459708e3508SHugh Dickins error = splice_to_pipe(pipe, &spd); 1460708e3508SHugh Dickins 1461708e3508SHugh Dickins splice_shrink_spd(pipe, &spd); 1462708e3508SHugh Dickins 1463708e3508SHugh Dickins if (error > 0) { 1464708e3508SHugh Dickins *ppos += error; 1465708e3508SHugh Dickins file_accessed(in); 1466708e3508SHugh Dickins } 1467708e3508SHugh Dickins return error; 1468708e3508SHugh Dickins } 1469708e3508SHugh Dickins 1470726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 14711da177e4SLinus Torvalds { 1472726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 14751da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 14761da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 14770edd73b3SHugh Dickins if (sbinfo->max_blocks) { 14781da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 147941ffe5d5SHugh Dickins buf->f_bavail = 148041ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 148141ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 14820edd73b3SHugh Dickins } 14830edd73b3SHugh Dickins if (sbinfo->max_inodes) { 14841da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 14851da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 14881da177e4SLinus Torvalds return 0; 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds /* 14921da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 14931da177e4SLinus Torvalds */ 14941da177e4SLinus Torvalds static int 14951da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 14961da177e4SLinus Torvalds { 14970b0a0806SHugh Dickins struct inode *inode; 14981da177e4SLinus Torvalds int error = -ENOSPC; 14991da177e4SLinus Torvalds 1500454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 15011da177e4SLinus Torvalds if (inode) { 15022a7dba39SEric Paris error = security_inode_init_security(inode, dir, 15032a7dba39SEric Paris &dentry->d_name, NULL, 15042a7dba39SEric Paris NULL, NULL); 1505570bc1c2SStephen Smalley if (error) { 1506570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1507570bc1c2SStephen Smalley iput(inode); 1508570bc1c2SStephen Smalley return error; 1509570bc1c2SStephen Smalley } 151039f0247dSAndreas Gruenbacher } 15111c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL 15121c7c474cSChristoph Hellwig error = generic_acl_init(inode, dir); 151339f0247dSAndreas Gruenbacher if (error) { 151439f0247dSAndreas Gruenbacher iput(inode); 151539f0247dSAndreas Gruenbacher return error; 1516570bc1c2SStephen Smalley } 1517718deb6bSAl Viro #else 1518718deb6bSAl Viro error = 0; 15191c7c474cSChristoph Hellwig #endif 15201da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 15211da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 15221da177e4SLinus Torvalds d_instantiate(dentry, inode); 15231da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds return error; 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode) 15291da177e4SLinus Torvalds { 15301da177e4SLinus Torvalds int error; 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 15331da177e4SLinus Torvalds return error; 1534d8c76e6fSDave Hansen inc_nlink(dir); 15351da177e4SLinus Torvalds return 0; 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode, 15391da177e4SLinus Torvalds struct nameidata *nd) 15401da177e4SLinus Torvalds { 15411da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds /* 15451da177e4SLinus Torvalds * Link a file.. 15461da177e4SLinus Torvalds */ 15471da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 15481da177e4SLinus Torvalds { 15491da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 15505b04c689SPavel Emelyanov int ret; 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds /* 15531da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 15541da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 15551da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 15561da177e4SLinus Torvalds */ 15575b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 15585b04c689SPavel Emelyanov if (ret) 15595b04c689SPavel Emelyanov goto out; 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 15621da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 1563d8c76e6fSDave Hansen inc_nlink(inode); 15647de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 15651da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 15661da177e4SLinus Torvalds d_instantiate(dentry, inode); 15675b04c689SPavel Emelyanov out: 15685b04c689SPavel Emelyanov return ret; 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 15721da177e4SLinus Torvalds { 15731da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 15741da177e4SLinus Torvalds 15755b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 15765b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 15791da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 15809a53c3a7SDave Hansen drop_nlink(inode); 15811da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 15821da177e4SLinus Torvalds return 0; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 15861da177e4SLinus Torvalds { 15871da177e4SLinus Torvalds if (!simple_empty(dentry)) 15881da177e4SLinus Torvalds return -ENOTEMPTY; 15891da177e4SLinus Torvalds 15909a53c3a7SDave Hansen drop_nlink(dentry->d_inode); 15919a53c3a7SDave Hansen drop_nlink(dir); 15921da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds /* 15961da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 15971da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 15981da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 15991da177e4SLinus Torvalds * gets overwritten. 16001da177e4SLinus Torvalds */ 16011da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 16021da177e4SLinus Torvalds { 16031da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 16041da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 16051da177e4SLinus Torvalds 16061da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 16071da177e4SLinus Torvalds return -ENOTEMPTY; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds if (new_dentry->d_inode) { 16101da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 16111da177e4SLinus Torvalds if (they_are_dirs) 16129a53c3a7SDave Hansen drop_nlink(old_dir); 16131da177e4SLinus Torvalds } else if (they_are_dirs) { 16149a53c3a7SDave Hansen drop_nlink(old_dir); 1615d8c76e6fSDave Hansen inc_nlink(new_dir); 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 16191da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 16201da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 16211da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 16221da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 16231da177e4SLinus Torvalds return 0; 16241da177e4SLinus Torvalds } 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 16271da177e4SLinus Torvalds { 16281da177e4SLinus Torvalds int error; 16291da177e4SLinus Torvalds int len; 16301da177e4SLinus Torvalds struct inode *inode; 16319276aad6SHugh Dickins struct page *page; 16321da177e4SLinus Torvalds char *kaddr; 16331da177e4SLinus Torvalds struct shmem_inode_info *info; 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds len = strlen(symname) + 1; 16361da177e4SLinus Torvalds if (len > PAGE_CACHE_SIZE) 16371da177e4SLinus Torvalds return -ENAMETOOLONG; 16381da177e4SLinus Torvalds 1639454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 16401da177e4SLinus Torvalds if (!inode) 16411da177e4SLinus Torvalds return -ENOSPC; 16421da177e4SLinus Torvalds 16432a7dba39SEric Paris error = security_inode_init_security(inode, dir, &dentry->d_name, NULL, 16442a7dba39SEric Paris NULL, NULL); 1645570bc1c2SStephen Smalley if (error) { 1646570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1647570bc1c2SStephen Smalley iput(inode); 1648570bc1c2SStephen Smalley return error; 1649570bc1c2SStephen Smalley } 1650570bc1c2SStephen Smalley error = 0; 1651570bc1c2SStephen Smalley } 1652570bc1c2SStephen Smalley 16531da177e4SLinus Torvalds info = SHMEM_I(inode); 16541da177e4SLinus Torvalds inode->i_size = len-1; 1655b09e0fa4SEric Paris if (len <= SHMEM_SYMLINK_INLINE_LEN) { 16561da177e4SLinus Torvalds /* do it inline */ 1657b09e0fa4SEric Paris memcpy(info->inline_symlink, symname, len); 16581da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inline_operations; 16591da177e4SLinus Torvalds } else { 16601da177e4SLinus Torvalds error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); 16611da177e4SLinus Torvalds if (error) { 16621da177e4SLinus Torvalds iput(inode); 16631da177e4SLinus Torvalds return error; 16641da177e4SLinus Torvalds } 166514fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 16661da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 16671da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 16681da177e4SLinus Torvalds memcpy(kaddr, symname, len); 16691da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 16701da177e4SLinus Torvalds set_page_dirty(page); 16716746aff7SWu Fengguang unlock_page(page); 16721da177e4SLinus Torvalds page_cache_release(page); 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 16751da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 16761da177e4SLinus Torvalds d_instantiate(dentry, inode); 16771da177e4SLinus Torvalds dget(dentry); 16781da177e4SLinus Torvalds return 0; 16791da177e4SLinus Torvalds } 16801da177e4SLinus Torvalds 1681cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd) 16821da177e4SLinus Torvalds { 1683b09e0fa4SEric Paris nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink); 1684cc314eefSLinus Torvalds return NULL; 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds 1687cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) 16881da177e4SLinus Torvalds { 16891da177e4SLinus Torvalds struct page *page = NULL; 169041ffe5d5SHugh Dickins int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); 169141ffe5d5SHugh Dickins nd_set_link(nd, error ? ERR_PTR(error) : kmap(page)); 1692d3602444SHugh Dickins if (page) 1693d3602444SHugh Dickins unlock_page(page); 1694cc314eefSLinus Torvalds return page; 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds 1697cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 16981da177e4SLinus Torvalds { 16991da177e4SLinus Torvalds if (!IS_ERR(nd_get_link(nd))) { 1700cc314eefSLinus Torvalds struct page *page = cookie; 17011da177e4SLinus Torvalds kunmap(page); 17021da177e4SLinus Torvalds mark_page_accessed(page); 17031da177e4SLinus Torvalds page_cache_release(page); 17041da177e4SLinus Torvalds } 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 1707b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 1708b09e0fa4SEric Paris /* 1709b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 1710b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 1711b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 1712b09e0fa4SEric Paris * filesystem level, though. 1713b09e0fa4SEric Paris */ 1714b09e0fa4SEric Paris 1715b09e0fa4SEric Paris static int shmem_xattr_get(struct dentry *dentry, const char *name, 1716b09e0fa4SEric Paris void *buffer, size_t size) 1717b09e0fa4SEric Paris { 1718b09e0fa4SEric Paris struct shmem_inode_info *info; 1719b09e0fa4SEric Paris struct shmem_xattr *xattr; 1720b09e0fa4SEric Paris int ret = -ENODATA; 1721b09e0fa4SEric Paris 1722b09e0fa4SEric Paris info = SHMEM_I(dentry->d_inode); 1723b09e0fa4SEric Paris 1724b09e0fa4SEric Paris spin_lock(&info->lock); 1725b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 1726b09e0fa4SEric Paris if (strcmp(name, xattr->name)) 1727b09e0fa4SEric Paris continue; 1728b09e0fa4SEric Paris 1729b09e0fa4SEric Paris ret = xattr->size; 1730b09e0fa4SEric Paris if (buffer) { 1731b09e0fa4SEric Paris if (size < xattr->size) 1732b09e0fa4SEric Paris ret = -ERANGE; 1733b09e0fa4SEric Paris else 1734b09e0fa4SEric Paris memcpy(buffer, xattr->value, xattr->size); 1735b09e0fa4SEric Paris } 1736b09e0fa4SEric Paris break; 1737b09e0fa4SEric Paris } 1738b09e0fa4SEric Paris spin_unlock(&info->lock); 1739b09e0fa4SEric Paris return ret; 1740b09e0fa4SEric Paris } 1741b09e0fa4SEric Paris 1742b09e0fa4SEric Paris static int shmem_xattr_set(struct dentry *dentry, const char *name, 1743b09e0fa4SEric Paris const void *value, size_t size, int flags) 1744b09e0fa4SEric Paris { 1745b09e0fa4SEric Paris struct inode *inode = dentry->d_inode; 1746b09e0fa4SEric Paris struct shmem_inode_info *info = SHMEM_I(inode); 1747b09e0fa4SEric Paris struct shmem_xattr *xattr; 1748b09e0fa4SEric Paris struct shmem_xattr *new_xattr = NULL; 1749b09e0fa4SEric Paris size_t len; 1750b09e0fa4SEric Paris int err = 0; 1751b09e0fa4SEric Paris 1752b09e0fa4SEric Paris /* value == NULL means remove */ 1753b09e0fa4SEric Paris if (value) { 1754b09e0fa4SEric Paris /* wrap around? */ 1755b09e0fa4SEric Paris len = sizeof(*new_xattr) + size; 1756b09e0fa4SEric Paris if (len <= sizeof(*new_xattr)) 1757b09e0fa4SEric Paris return -ENOMEM; 1758b09e0fa4SEric Paris 1759b09e0fa4SEric Paris new_xattr = kmalloc(len, GFP_KERNEL); 1760b09e0fa4SEric Paris if (!new_xattr) 1761b09e0fa4SEric Paris return -ENOMEM; 1762b09e0fa4SEric Paris 1763b09e0fa4SEric Paris new_xattr->name = kstrdup(name, GFP_KERNEL); 1764b09e0fa4SEric Paris if (!new_xattr->name) { 1765b09e0fa4SEric Paris kfree(new_xattr); 1766b09e0fa4SEric Paris return -ENOMEM; 1767b09e0fa4SEric Paris } 1768b09e0fa4SEric Paris 1769b09e0fa4SEric Paris new_xattr->size = size; 1770b09e0fa4SEric Paris memcpy(new_xattr->value, value, size); 1771b09e0fa4SEric Paris } 1772b09e0fa4SEric Paris 1773b09e0fa4SEric Paris spin_lock(&info->lock); 1774b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 1775b09e0fa4SEric Paris if (!strcmp(name, xattr->name)) { 1776b09e0fa4SEric Paris if (flags & XATTR_CREATE) { 1777b09e0fa4SEric Paris xattr = new_xattr; 1778b09e0fa4SEric Paris err = -EEXIST; 1779b09e0fa4SEric Paris } else if (new_xattr) { 1780b09e0fa4SEric Paris list_replace(&xattr->list, &new_xattr->list); 1781b09e0fa4SEric Paris } else { 1782b09e0fa4SEric Paris list_del(&xattr->list); 1783b09e0fa4SEric Paris } 1784b09e0fa4SEric Paris goto out; 1785b09e0fa4SEric Paris } 1786b09e0fa4SEric Paris } 1787b09e0fa4SEric Paris if (flags & XATTR_REPLACE) { 1788b09e0fa4SEric Paris xattr = new_xattr; 1789b09e0fa4SEric Paris err = -ENODATA; 1790b09e0fa4SEric Paris } else { 1791b09e0fa4SEric Paris list_add(&new_xattr->list, &info->xattr_list); 1792b09e0fa4SEric Paris xattr = NULL; 1793b09e0fa4SEric Paris } 1794b09e0fa4SEric Paris out: 1795b09e0fa4SEric Paris spin_unlock(&info->lock); 1796b09e0fa4SEric Paris if (xattr) 1797b09e0fa4SEric Paris kfree(xattr->name); 1798b09e0fa4SEric Paris kfree(xattr); 1799b09e0fa4SEric Paris return err; 1800b09e0fa4SEric Paris } 1801b09e0fa4SEric Paris 1802b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 1803b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 1804b09e0fa4SEric Paris &generic_acl_access_handler, 1805b09e0fa4SEric Paris &generic_acl_default_handler, 1806b09e0fa4SEric Paris #endif 1807b09e0fa4SEric Paris NULL 1808b09e0fa4SEric Paris }; 1809b09e0fa4SEric Paris 1810b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name) 1811b09e0fa4SEric Paris { 1812b09e0fa4SEric Paris struct { const char *prefix; size_t len; } arr[] = { 1813b09e0fa4SEric Paris { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, 1814b09e0fa4SEric Paris { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } 1815b09e0fa4SEric Paris }; 1816b09e0fa4SEric Paris int i; 1817b09e0fa4SEric Paris 1818b09e0fa4SEric Paris for (i = 0; i < ARRAY_SIZE(arr); i++) { 1819b09e0fa4SEric Paris size_t preflen = arr[i].len; 1820b09e0fa4SEric Paris if (strncmp(name, arr[i].prefix, preflen) == 0) { 1821b09e0fa4SEric Paris if (!name[preflen]) 1822b09e0fa4SEric Paris return -EINVAL; 1823b09e0fa4SEric Paris return 0; 1824b09e0fa4SEric Paris } 1825b09e0fa4SEric Paris } 1826b09e0fa4SEric Paris return -EOPNOTSUPP; 1827b09e0fa4SEric Paris } 1828b09e0fa4SEric Paris 1829b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, 1830b09e0fa4SEric Paris void *buffer, size_t size) 1831b09e0fa4SEric Paris { 1832b09e0fa4SEric Paris int err; 1833b09e0fa4SEric Paris 1834b09e0fa4SEric Paris /* 1835b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 1836b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 1837b09e0fa4SEric Paris * for it via sb->s_xattr. 1838b09e0fa4SEric Paris */ 1839b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 1840b09e0fa4SEric Paris return generic_getxattr(dentry, name, buffer, size); 1841b09e0fa4SEric Paris 1842b09e0fa4SEric Paris err = shmem_xattr_validate(name); 1843b09e0fa4SEric Paris if (err) 1844b09e0fa4SEric Paris return err; 1845b09e0fa4SEric Paris 1846b09e0fa4SEric Paris return shmem_xattr_get(dentry, name, buffer, size); 1847b09e0fa4SEric Paris } 1848b09e0fa4SEric Paris 1849b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name, 1850b09e0fa4SEric Paris const void *value, size_t size, int flags) 1851b09e0fa4SEric Paris { 1852b09e0fa4SEric Paris int err; 1853b09e0fa4SEric Paris 1854b09e0fa4SEric Paris /* 1855b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 1856b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 1857b09e0fa4SEric Paris * for it via sb->s_xattr. 1858b09e0fa4SEric Paris */ 1859b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 1860b09e0fa4SEric Paris return generic_setxattr(dentry, name, value, size, flags); 1861b09e0fa4SEric Paris 1862b09e0fa4SEric Paris err = shmem_xattr_validate(name); 1863b09e0fa4SEric Paris if (err) 1864b09e0fa4SEric Paris return err; 1865b09e0fa4SEric Paris 1866b09e0fa4SEric Paris if (size == 0) 1867b09e0fa4SEric Paris value = ""; /* empty EA, do not remove */ 1868b09e0fa4SEric Paris 1869b09e0fa4SEric Paris return shmem_xattr_set(dentry, name, value, size, flags); 1870b09e0fa4SEric Paris 1871b09e0fa4SEric Paris } 1872b09e0fa4SEric Paris 1873b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name) 1874b09e0fa4SEric Paris { 1875b09e0fa4SEric Paris int err; 1876b09e0fa4SEric Paris 1877b09e0fa4SEric Paris /* 1878b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 1879b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 1880b09e0fa4SEric Paris * for it via sb->s_xattr. 1881b09e0fa4SEric Paris */ 1882b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 1883b09e0fa4SEric Paris return generic_removexattr(dentry, name); 1884b09e0fa4SEric Paris 1885b09e0fa4SEric Paris err = shmem_xattr_validate(name); 1886b09e0fa4SEric Paris if (err) 1887b09e0fa4SEric Paris return err; 1888b09e0fa4SEric Paris 1889b09e0fa4SEric Paris return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE); 1890b09e0fa4SEric Paris } 1891b09e0fa4SEric Paris 1892b09e0fa4SEric Paris static bool xattr_is_trusted(const char *name) 1893b09e0fa4SEric Paris { 1894b09e0fa4SEric Paris return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN); 1895b09e0fa4SEric Paris } 1896b09e0fa4SEric Paris 1897b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 1898b09e0fa4SEric Paris { 1899b09e0fa4SEric Paris bool trusted = capable(CAP_SYS_ADMIN); 1900b09e0fa4SEric Paris struct shmem_xattr *xattr; 1901b09e0fa4SEric Paris struct shmem_inode_info *info; 1902b09e0fa4SEric Paris size_t used = 0; 1903b09e0fa4SEric Paris 1904b09e0fa4SEric Paris info = SHMEM_I(dentry->d_inode); 1905b09e0fa4SEric Paris 1906b09e0fa4SEric Paris spin_lock(&info->lock); 1907b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 1908b09e0fa4SEric Paris size_t len; 1909b09e0fa4SEric Paris 1910b09e0fa4SEric Paris /* skip "trusted." attributes for unprivileged callers */ 1911b09e0fa4SEric Paris if (!trusted && xattr_is_trusted(xattr->name)) 1912b09e0fa4SEric Paris continue; 1913b09e0fa4SEric Paris 1914b09e0fa4SEric Paris len = strlen(xattr->name) + 1; 1915b09e0fa4SEric Paris used += len; 1916b09e0fa4SEric Paris if (buffer) { 1917b09e0fa4SEric Paris if (size < used) { 1918b09e0fa4SEric Paris used = -ERANGE; 1919b09e0fa4SEric Paris break; 1920b09e0fa4SEric Paris } 1921b09e0fa4SEric Paris memcpy(buffer, xattr->name, len); 1922b09e0fa4SEric Paris buffer += len; 1923b09e0fa4SEric Paris } 1924b09e0fa4SEric Paris } 1925b09e0fa4SEric Paris spin_unlock(&info->lock); 1926b09e0fa4SEric Paris 1927b09e0fa4SEric Paris return used; 1928b09e0fa4SEric Paris } 1929b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 1930b09e0fa4SEric Paris 193192e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations = { 19321da177e4SLinus Torvalds .readlink = generic_readlink, 19331da177e4SLinus Torvalds .follow_link = shmem_follow_link_inline, 1934b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 1935b09e0fa4SEric Paris .setxattr = shmem_setxattr, 1936b09e0fa4SEric Paris .getxattr = shmem_getxattr, 1937b09e0fa4SEric Paris .listxattr = shmem_listxattr, 1938b09e0fa4SEric Paris .removexattr = shmem_removexattr, 1939b09e0fa4SEric Paris #endif 19401da177e4SLinus Torvalds }; 19411da177e4SLinus Torvalds 194292e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 19431da177e4SLinus Torvalds .readlink = generic_readlink, 19441da177e4SLinus Torvalds .follow_link = shmem_follow_link, 19451da177e4SLinus Torvalds .put_link = shmem_put_link, 1946b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 1947b09e0fa4SEric Paris .setxattr = shmem_setxattr, 1948b09e0fa4SEric Paris .getxattr = shmem_getxattr, 1949b09e0fa4SEric Paris .listxattr = shmem_listxattr, 1950b09e0fa4SEric Paris .removexattr = shmem_removexattr, 195139f0247dSAndreas Gruenbacher #endif 1952b09e0fa4SEric Paris }; 195339f0247dSAndreas Gruenbacher 195491828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 195591828a40SDavid M. Grimes { 195691828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 195791828a40SDavid M. Grimes } 195891828a40SDavid M. Grimes 195991828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 196091828a40SDavid M. Grimes { 196191828a40SDavid M. Grimes __u32 *fh = vfh; 196291828a40SDavid M. Grimes __u64 inum = fh[2]; 196391828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 196491828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 196591828a40SDavid M. Grimes } 196691828a40SDavid M. Grimes 1967480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 1968480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 196991828a40SDavid M. Grimes { 197091828a40SDavid M. Grimes struct inode *inode; 1971480b116cSChristoph Hellwig struct dentry *dentry = NULL; 1972480b116cSChristoph Hellwig u64 inum = fid->raw[2]; 1973480b116cSChristoph Hellwig inum = (inum << 32) | fid->raw[1]; 197491828a40SDavid M. Grimes 1975480b116cSChristoph Hellwig if (fh_len < 3) 1976480b116cSChristoph Hellwig return NULL; 1977480b116cSChristoph Hellwig 1978480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 1979480b116cSChristoph Hellwig shmem_match, fid->raw); 198091828a40SDavid M. Grimes if (inode) { 1981480b116cSChristoph Hellwig dentry = d_find_alias(inode); 198291828a40SDavid M. Grimes iput(inode); 198391828a40SDavid M. Grimes } 198491828a40SDavid M. Grimes 1985480b116cSChristoph Hellwig return dentry; 198691828a40SDavid M. Grimes } 198791828a40SDavid M. Grimes 198891828a40SDavid M. Grimes static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len, 198991828a40SDavid M. Grimes int connectable) 199091828a40SDavid M. Grimes { 199191828a40SDavid M. Grimes struct inode *inode = dentry->d_inode; 199291828a40SDavid M. Grimes 19935fe0c237SAneesh Kumar K.V if (*len < 3) { 19945fe0c237SAneesh Kumar K.V *len = 3; 199591828a40SDavid M. Grimes return 255; 19965fe0c237SAneesh Kumar K.V } 199791828a40SDavid M. Grimes 19981d3382cbSAl Viro if (inode_unhashed(inode)) { 199991828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 200091828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 200191828a40SDavid M. Grimes * time, we need a lock to ensure we only try 200291828a40SDavid M. Grimes * to do it once 200391828a40SDavid M. Grimes */ 200491828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 200591828a40SDavid M. Grimes spin_lock(&lock); 20061d3382cbSAl Viro if (inode_unhashed(inode)) 200791828a40SDavid M. Grimes __insert_inode_hash(inode, 200891828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 200991828a40SDavid M. Grimes spin_unlock(&lock); 201091828a40SDavid M. Grimes } 201191828a40SDavid M. Grimes 201291828a40SDavid M. Grimes fh[0] = inode->i_generation; 201391828a40SDavid M. Grimes fh[1] = inode->i_ino; 201491828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 201591828a40SDavid M. Grimes 201691828a40SDavid M. Grimes *len = 3; 201791828a40SDavid M. Grimes return 1; 201891828a40SDavid M. Grimes } 201991828a40SDavid M. Grimes 202039655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 202191828a40SDavid M. Grimes .get_parent = shmem_get_parent, 202291828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 2023480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 202491828a40SDavid M. Grimes }; 202591828a40SDavid M. Grimes 2026680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 2027680d794bSakpm@linux-foundation.org bool remount) 20281da177e4SLinus Torvalds { 20291da177e4SLinus Torvalds char *this_char, *value, *rest; 20301da177e4SLinus Torvalds 2031b00dc3adSHugh Dickins while (options != NULL) { 2032b00dc3adSHugh Dickins this_char = options; 2033b00dc3adSHugh Dickins for (;;) { 2034b00dc3adSHugh Dickins /* 2035b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 2036b00dc3adSHugh Dickins * mount options form a comma-separated list, 2037b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 2038b00dc3adSHugh Dickins */ 2039b00dc3adSHugh Dickins options = strchr(options, ','); 2040b00dc3adSHugh Dickins if (options == NULL) 2041b00dc3adSHugh Dickins break; 2042b00dc3adSHugh Dickins options++; 2043b00dc3adSHugh Dickins if (!isdigit(*options)) { 2044b00dc3adSHugh Dickins options[-1] = '\0'; 2045b00dc3adSHugh Dickins break; 2046b00dc3adSHugh Dickins } 2047b00dc3adSHugh Dickins } 20481da177e4SLinus Torvalds if (!*this_char) 20491da177e4SLinus Torvalds continue; 20501da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 20511da177e4SLinus Torvalds *value++ = 0; 20521da177e4SLinus Torvalds } else { 20531da177e4SLinus Torvalds printk(KERN_ERR 20541da177e4SLinus Torvalds "tmpfs: No value for mount option '%s'\n", 20551da177e4SLinus Torvalds this_char); 20561da177e4SLinus Torvalds return 1; 20571da177e4SLinus Torvalds } 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 20601da177e4SLinus Torvalds unsigned long long size; 20611da177e4SLinus Torvalds size = memparse(value,&rest); 20621da177e4SLinus Torvalds if (*rest == '%') { 20631da177e4SLinus Torvalds size <<= PAGE_SHIFT; 20641da177e4SLinus Torvalds size *= totalram_pages; 20651da177e4SLinus Torvalds do_div(size, 100); 20661da177e4SLinus Torvalds rest++; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds if (*rest) 20691da177e4SLinus Torvalds goto bad_val; 2070680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 2071680d794bSakpm@linux-foundation.org DIV_ROUND_UP(size, PAGE_CACHE_SIZE); 20721da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 2073680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 20741da177e4SLinus Torvalds if (*rest) 20751da177e4SLinus Torvalds goto bad_val; 20761da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 2077680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 20781da177e4SLinus Torvalds if (*rest) 20791da177e4SLinus Torvalds goto bad_val; 20801da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 2081680d794bSakpm@linux-foundation.org if (remount) 20821da177e4SLinus Torvalds continue; 2083680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 20841da177e4SLinus Torvalds if (*rest) 20851da177e4SLinus Torvalds goto bad_val; 20861da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 2087680d794bSakpm@linux-foundation.org if (remount) 20881da177e4SLinus Torvalds continue; 2089680d794bSakpm@linux-foundation.org sbinfo->uid = simple_strtoul(value, &rest, 0); 20901da177e4SLinus Torvalds if (*rest) 20911da177e4SLinus Torvalds goto bad_val; 20921da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 2093680d794bSakpm@linux-foundation.org if (remount) 20941da177e4SLinus Torvalds continue; 2095680d794bSakpm@linux-foundation.org sbinfo->gid = simple_strtoul(value, &rest, 0); 20961da177e4SLinus Torvalds if (*rest) 20971da177e4SLinus Torvalds goto bad_val; 20987339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 209971fe804bSLee Schermerhorn if (mpol_parse_str(value, &sbinfo->mpol, 1)) 21007339ff83SRobin Holt goto bad_val; 21011da177e4SLinus Torvalds } else { 21021da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad mount option %s\n", 21031da177e4SLinus Torvalds this_char); 21041da177e4SLinus Torvalds return 1; 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds } 21071da177e4SLinus Torvalds return 0; 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds bad_val: 21101da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", 21111da177e4SLinus Torvalds value, this_char); 21121da177e4SLinus Torvalds return 1; 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 21171da177e4SLinus Torvalds { 21181da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2119680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 21200edd73b3SHugh Dickins unsigned long inodes; 21210edd73b3SHugh Dickins int error = -EINVAL; 21221da177e4SLinus Torvalds 2123680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 21240edd73b3SHugh Dickins return error; 21250edd73b3SHugh Dickins 21260edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 21270edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 21287e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 21290edd73b3SHugh Dickins goto out; 2130680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 21310edd73b3SHugh Dickins goto out; 21320edd73b3SHugh Dickins /* 2133*54af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 21340edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 21350edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 21360edd73b3SHugh Dickins */ 2137680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 21380edd73b3SHugh Dickins goto out; 2139680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 21400edd73b3SHugh Dickins goto out; 21410edd73b3SHugh Dickins 21420edd73b3SHugh Dickins error = 0; 2143680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 2144680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 2145680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 214671fe804bSLee Schermerhorn 214771fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 214871fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 21490edd73b3SHugh Dickins out: 21500edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 21510edd73b3SHugh Dickins return error; 21521da177e4SLinus Torvalds } 2153680d794bSakpm@linux-foundation.org 2154680d794bSakpm@linux-foundation.org static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs) 2155680d794bSakpm@linux-foundation.org { 2156680d794bSakpm@linux-foundation.org struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb); 2157680d794bSakpm@linux-foundation.org 2158680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 2159680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 2160680d794bSakpm@linux-foundation.org sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); 2161680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 2162680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 2163680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 2164680d794bSakpm@linux-foundation.org seq_printf(seq, ",mode=%03o", sbinfo->mode); 2165680d794bSakpm@linux-foundation.org if (sbinfo->uid != 0) 2166680d794bSakpm@linux-foundation.org seq_printf(seq, ",uid=%u", sbinfo->uid); 2167680d794bSakpm@linux-foundation.org if (sbinfo->gid != 0) 2168680d794bSakpm@linux-foundation.org seq_printf(seq, ",gid=%u", sbinfo->gid); 216971fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 2170680d794bSakpm@linux-foundation.org return 0; 2171680d794bSakpm@linux-foundation.org } 2172680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 21751da177e4SLinus Torvalds { 2176602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2177602586a8SHugh Dickins 2178602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 2179602586a8SHugh Dickins kfree(sbinfo); 21801da177e4SLinus Torvalds sb->s_fs_info = NULL; 21811da177e4SLinus Torvalds } 21821da177e4SLinus Torvalds 21832b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 21841da177e4SLinus Torvalds { 21851da177e4SLinus Torvalds struct inode *inode; 21861da177e4SLinus Torvalds struct dentry *root; 21870edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 2188680d794bSakpm@linux-foundation.org int err = -ENOMEM; 2189680d794bSakpm@linux-foundation.org 2190680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 2191425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 2192680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 2193680d794bSakpm@linux-foundation.org if (!sbinfo) 2194680d794bSakpm@linux-foundation.org return -ENOMEM; 2195680d794bSakpm@linux-foundation.org 2196680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 219776aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 219876aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 2199680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 22001da177e4SLinus Torvalds 22010edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 22021da177e4SLinus Torvalds /* 22031da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 22041da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 22051da177e4SLinus Torvalds * but the internal instance is left unlimited. 22061da177e4SLinus Torvalds */ 22071da177e4SLinus Torvalds if (!(sb->s_flags & MS_NOUSER)) { 2208680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 2209680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 2210680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 2211680d794bSakpm@linux-foundation.org err = -EINVAL; 2212680d794bSakpm@linux-foundation.org goto failed; 2213680d794bSakpm@linux-foundation.org } 22141da177e4SLinus Torvalds } 221591828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 22160edd73b3SHugh Dickins #else 22170edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 22180edd73b3SHugh Dickins #endif 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 2221602586a8SHugh Dickins if (percpu_counter_init(&sbinfo->used_blocks, 0)) 2222602586a8SHugh Dickins goto failed; 2223680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 22241da177e4SLinus Torvalds 2225285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 22261da177e4SLinus Torvalds sb->s_blocksize = PAGE_CACHE_SIZE; 22271da177e4SLinus Torvalds sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 22281da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 22291da177e4SLinus Torvalds sb->s_op = &shmem_ops; 2230cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 2231b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 223239f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 2233b09e0fa4SEric Paris #endif 2234b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 223539f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 223639f0247dSAndreas Gruenbacher #endif 22370edd73b3SHugh Dickins 2238454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 22391da177e4SLinus Torvalds if (!inode) 22401da177e4SLinus Torvalds goto failed; 2241680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 2242680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 22431da177e4SLinus Torvalds root = d_alloc_root(inode); 22441da177e4SLinus Torvalds if (!root) 22451da177e4SLinus Torvalds goto failed_iput; 22461da177e4SLinus Torvalds sb->s_root = root; 22471da177e4SLinus Torvalds return 0; 22481da177e4SLinus Torvalds 22491da177e4SLinus Torvalds failed_iput: 22501da177e4SLinus Torvalds iput(inode); 22511da177e4SLinus Torvalds failed: 22521da177e4SLinus Torvalds shmem_put_super(sb); 22531da177e4SLinus Torvalds return err; 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds 2256fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 22591da177e4SLinus Torvalds { 226041ffe5d5SHugh Dickins struct shmem_inode_info *info; 226141ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 226241ffe5d5SHugh Dickins if (!info) 22631da177e4SLinus Torvalds return NULL; 226441ffe5d5SHugh Dickins return &info->vfs_inode; 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds 226741ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 2268fa0d7e3dSNick Piggin { 2269fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 2270fa0d7e3dSNick Piggin INIT_LIST_HEAD(&inode->i_dentry); 2271fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 2272fa0d7e3dSNick Piggin } 2273fa0d7e3dSNick Piggin 22741da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 22751da177e4SLinus Torvalds { 22761da177e4SLinus Torvalds if ((inode->i_mode & S_IFMT) == S_IFREG) { 22771da177e4SLinus Torvalds /* only struct inode is valid if it's an inline symlink */ 22781da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 22791da177e4SLinus Torvalds } 228041ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 22811da177e4SLinus Torvalds } 22821da177e4SLinus Torvalds 228341ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 22841da177e4SLinus Torvalds { 228541ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 228641ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 228941ffe5d5SHugh Dickins static int shmem_init_inodecache(void) 22901da177e4SLinus Torvalds { 22911da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 22921da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 229341ffe5d5SHugh Dickins 0, SLAB_PANIC, shmem_init_inode); 22941da177e4SLinus Torvalds return 0; 22951da177e4SLinus Torvalds } 22961da177e4SLinus Torvalds 229741ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 22981da177e4SLinus Torvalds { 22991a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 23001da177e4SLinus Torvalds } 23011da177e4SLinus Torvalds 2302f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 23031da177e4SLinus Torvalds .writepage = shmem_writepage, 230476719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 23051da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2306800d15a5SNick Piggin .write_begin = shmem_write_begin, 2307800d15a5SNick Piggin .write_end = shmem_write_end, 23081da177e4SLinus Torvalds #endif 2309304dbdb7SLee Schermerhorn .migratepage = migrate_page, 2310aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 23111da177e4SLinus Torvalds }; 23121da177e4SLinus Torvalds 231315ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 23141da177e4SLinus Torvalds .mmap = shmem_mmap, 23151da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 23161da177e4SLinus Torvalds .llseek = generic_file_llseek, 2317bcd78e49SHugh Dickins .read = do_sync_read, 23185402b976SHugh Dickins .write = do_sync_write, 2319bcd78e49SHugh Dickins .aio_read = shmem_file_aio_read, 23205402b976SHugh Dickins .aio_write = generic_file_aio_write, 23211b061d92SChristoph Hellwig .fsync = noop_fsync, 2322708e3508SHugh Dickins .splice_read = shmem_file_splice_read, 2323ae976416SHugh Dickins .splice_write = generic_file_splice_write, 23241da177e4SLinus Torvalds #endif 23251da177e4SLinus Torvalds }; 23261da177e4SLinus Torvalds 232792e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 232894c1e62dSHugh Dickins .setattr = shmem_setattr, 2329f6b3ec23SBadari Pulavarty .truncate_range = shmem_truncate_range, 2330b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2331b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2332b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2333b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2334b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2335b09e0fa4SEric Paris #endif 23361da177e4SLinus Torvalds }; 23371da177e4SLinus Torvalds 233892e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 23391da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 23401da177e4SLinus Torvalds .create = shmem_create, 23411da177e4SLinus Torvalds .lookup = simple_lookup, 23421da177e4SLinus Torvalds .link = shmem_link, 23431da177e4SLinus Torvalds .unlink = shmem_unlink, 23441da177e4SLinus Torvalds .symlink = shmem_symlink, 23451da177e4SLinus Torvalds .mkdir = shmem_mkdir, 23461da177e4SLinus Torvalds .rmdir = shmem_rmdir, 23471da177e4SLinus Torvalds .mknod = shmem_mknod, 23481da177e4SLinus Torvalds .rename = shmem_rename, 23491da177e4SLinus Torvalds #endif 2350b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2351b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2352b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2353b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2354b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2355b09e0fa4SEric Paris #endif 235639f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 235794c1e62dSHugh Dickins .setattr = shmem_setattr, 235839f0247dSAndreas Gruenbacher #endif 235939f0247dSAndreas Gruenbacher }; 236039f0247dSAndreas Gruenbacher 236192e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 2362b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2363b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2364b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2365b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2366b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2367b09e0fa4SEric Paris #endif 236839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 236994c1e62dSHugh Dickins .setattr = shmem_setattr, 237039f0247dSAndreas Gruenbacher #endif 23711da177e4SLinus Torvalds }; 23721da177e4SLinus Torvalds 2373759b9775SHugh Dickins static const struct super_operations shmem_ops = { 23741da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 23751da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 23761da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 23771da177e4SLinus Torvalds .statfs = shmem_statfs, 23781da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 2379680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 23801da177e4SLinus Torvalds #endif 23811f895f75SAl Viro .evict_inode = shmem_evict_inode, 23821da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 23831da177e4SLinus Torvalds .put_super = shmem_put_super, 23841da177e4SLinus Torvalds }; 23851da177e4SLinus Torvalds 2386f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 238754cb8821SNick Piggin .fault = shmem_fault, 23881da177e4SLinus Torvalds #ifdef CONFIG_NUMA 23891da177e4SLinus Torvalds .set_policy = shmem_set_policy, 23901da177e4SLinus Torvalds .get_policy = shmem_get_policy, 23911da177e4SLinus Torvalds #endif 23921da177e4SLinus Torvalds }; 23931da177e4SLinus Torvalds 23943c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 23953c26ff6eSAl Viro int flags, const char *dev_name, void *data) 23961da177e4SLinus Torvalds { 23973c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 23981da177e4SLinus Torvalds } 23991da177e4SLinus Torvalds 240041ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 24011da177e4SLinus Torvalds .owner = THIS_MODULE, 24021da177e4SLinus Torvalds .name = "tmpfs", 24033c26ff6eSAl Viro .mount = shmem_mount, 24041da177e4SLinus Torvalds .kill_sb = kill_litter_super, 24051da177e4SLinus Torvalds }; 24061da177e4SLinus Torvalds 240741ffe5d5SHugh Dickins int __init shmem_init(void) 24081da177e4SLinus Torvalds { 24091da177e4SLinus Torvalds int error; 24101da177e4SLinus Torvalds 2411e0bf68ddSPeter Zijlstra error = bdi_init(&shmem_backing_dev_info); 2412e0bf68ddSPeter Zijlstra if (error) 2413e0bf68ddSPeter Zijlstra goto out4; 2414e0bf68ddSPeter Zijlstra 241541ffe5d5SHugh Dickins error = shmem_init_inodecache(); 24161da177e4SLinus Torvalds if (error) 24171da177e4SLinus Torvalds goto out3; 24181da177e4SLinus Torvalds 241941ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 24201da177e4SLinus Torvalds if (error) { 24211da177e4SLinus Torvalds printk(KERN_ERR "Could not register tmpfs\n"); 24221da177e4SLinus Torvalds goto out2; 24231da177e4SLinus Torvalds } 242495dc112aSGreg Kroah-Hartman 242541ffe5d5SHugh Dickins shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER, 242641ffe5d5SHugh Dickins shmem_fs_type.name, NULL); 24271da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 24281da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 24291da177e4SLinus Torvalds printk(KERN_ERR "Could not kern_mount tmpfs\n"); 24301da177e4SLinus Torvalds goto out1; 24311da177e4SLinus Torvalds } 24321da177e4SLinus Torvalds return 0; 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds out1: 243541ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 24361da177e4SLinus Torvalds out2: 243741ffe5d5SHugh Dickins shmem_destroy_inodecache(); 24381da177e4SLinus Torvalds out3: 2439e0bf68ddSPeter Zijlstra bdi_destroy(&shmem_backing_dev_info); 2440e0bf68ddSPeter Zijlstra out4: 24411da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 24421da177e4SLinus Torvalds return error; 24431da177e4SLinus Torvalds } 2444853ac43aSMatt Mackall 244587946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR 244687946a72SDaisuke Nishimura /** 244741ffe5d5SHugh Dickins * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file 244887946a72SDaisuke Nishimura * @inode: the inode to be searched 244941ffe5d5SHugh Dickins * @index: the page offset to be searched 245087946a72SDaisuke Nishimura * @pagep: the pointer for the found page to be stored 245141ffe5d5SHugh Dickins * @swapp: the pointer for the found swap entry to be stored 245287946a72SDaisuke Nishimura * 245387946a72SDaisuke Nishimura * If a page is found, refcount of it is incremented. Callers should handle 245487946a72SDaisuke Nishimura * these refcount. 245587946a72SDaisuke Nishimura */ 245641ffe5d5SHugh Dickins void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index, 245741ffe5d5SHugh Dickins struct page **pagep, swp_entry_t *swapp) 245887946a72SDaisuke Nishimura { 245987946a72SDaisuke Nishimura struct shmem_inode_info *info = SHMEM_I(inode); 246041ffe5d5SHugh Dickins struct page *page = NULL; 246141ffe5d5SHugh Dickins swp_entry_t swap = {0}; 246287946a72SDaisuke Nishimura 246341ffe5d5SHugh Dickins if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 246487946a72SDaisuke Nishimura goto out; 246587946a72SDaisuke Nishimura 246687946a72SDaisuke Nishimura spin_lock(&info->lock); 246787946a72SDaisuke Nishimura #ifdef CONFIG_SWAP 246841ffe5d5SHugh Dickins swap = shmem_get_swap(info, index); 246941ffe5d5SHugh Dickins if (swap.val) 247041ffe5d5SHugh Dickins page = find_get_page(&swapper_space, swap.val); 2471285b2c4fSHugh Dickins else 247287946a72SDaisuke Nishimura #endif 247341ffe5d5SHugh Dickins page = find_get_page(inode->i_mapping, index); 247487946a72SDaisuke Nishimura spin_unlock(&info->lock); 247587946a72SDaisuke Nishimura out: 247687946a72SDaisuke Nishimura *pagep = page; 247741ffe5d5SHugh Dickins *swapp = swap; 247887946a72SDaisuke Nishimura } 247987946a72SDaisuke Nishimura #endif 248087946a72SDaisuke Nishimura 2481853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 2482853ac43aSMatt Mackall 2483853ac43aSMatt Mackall /* 2484853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 2485853ac43aSMatt Mackall * 2486853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 2487853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 2488853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 2489853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 2490853ac43aSMatt Mackall */ 2491853ac43aSMatt Mackall 2492853ac43aSMatt Mackall #include <linux/ramfs.h> 2493853ac43aSMatt Mackall 249441ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 2495853ac43aSMatt Mackall .name = "tmpfs", 24963c26ff6eSAl Viro .mount = ramfs_mount, 2497853ac43aSMatt Mackall .kill_sb = kill_litter_super, 2498853ac43aSMatt Mackall }; 2499853ac43aSMatt Mackall 250041ffe5d5SHugh Dickins int __init shmem_init(void) 2501853ac43aSMatt Mackall { 250241ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 2503853ac43aSMatt Mackall 250441ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 2505853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 2506853ac43aSMatt Mackall 2507853ac43aSMatt Mackall return 0; 2508853ac43aSMatt Mackall } 2509853ac43aSMatt Mackall 251041ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 2511853ac43aSMatt Mackall { 2512853ac43aSMatt Mackall return 0; 2513853ac43aSMatt Mackall } 2514853ac43aSMatt Mackall 25153f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 25163f96b79aSHugh Dickins { 25173f96b79aSHugh Dickins return 0; 25183f96b79aSHugh Dickins } 25193f96b79aSHugh Dickins 252041ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 252194c1e62dSHugh Dickins { 252241ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 252394c1e62dSHugh Dickins } 252494c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 252594c1e62dSHugh Dickins 252687946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR 252787946a72SDaisuke Nishimura /** 252841ffe5d5SHugh Dickins * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file 252987946a72SDaisuke Nishimura * @inode: the inode to be searched 253041ffe5d5SHugh Dickins * @index: the page offset to be searched 253187946a72SDaisuke Nishimura * @pagep: the pointer for the found page to be stored 253241ffe5d5SHugh Dickins * @swapp: the pointer for the found swap entry to be stored 253387946a72SDaisuke Nishimura * 253487946a72SDaisuke Nishimura * If a page is found, refcount of it is incremented. Callers should handle 253587946a72SDaisuke Nishimura * these refcount. 253687946a72SDaisuke Nishimura */ 253741ffe5d5SHugh Dickins void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index, 253841ffe5d5SHugh Dickins struct page **pagep, swp_entry_t *swapp) 253987946a72SDaisuke Nishimura { 254087946a72SDaisuke Nishimura struct page *page = NULL; 254187946a72SDaisuke Nishimura 254241ffe5d5SHugh Dickins if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 254387946a72SDaisuke Nishimura goto out; 254441ffe5d5SHugh Dickins page = find_get_page(inode->i_mapping, index); 254587946a72SDaisuke Nishimura out: 254687946a72SDaisuke Nishimura *pagep = page; 254741ffe5d5SHugh Dickins *swapp = (swp_entry_t){0}; 254887946a72SDaisuke Nishimura } 254987946a72SDaisuke Nishimura #endif 255087946a72SDaisuke Nishimura 2551853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 25520b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 2553454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 25540b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 25550b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 2556853ac43aSMatt Mackall 2557853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 2558853ac43aSMatt Mackall 2559853ac43aSMatt Mackall /* common code */ 25601da177e4SLinus Torvalds 256146711810SRandy Dunlap /** 25621da177e4SLinus Torvalds * shmem_file_setup - get an unlinked file living in tmpfs 25631da177e4SLinus Torvalds * @name: name for dentry (to be seen in /proc/<pid>/maps 25641da177e4SLinus Torvalds * @size: size to be set for the file 25650b0a0806SHugh Dickins * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 25661da177e4SLinus Torvalds */ 2567168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 25681da177e4SLinus Torvalds { 25691da177e4SLinus Torvalds int error; 25701da177e4SLinus Torvalds struct file *file; 25711da177e4SLinus Torvalds struct inode *inode; 25722c48b9c4SAl Viro struct path path; 25732c48b9c4SAl Viro struct dentry *root; 25741da177e4SLinus Torvalds struct qstr this; 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 25771da177e4SLinus Torvalds return (void *)shm_mnt; 25781da177e4SLinus Torvalds 2579285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 25801da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 25811da177e4SLinus Torvalds 25821da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 25831da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds error = -ENOMEM; 25861da177e4SLinus Torvalds this.name = name; 25871da177e4SLinus Torvalds this.len = strlen(name); 25881da177e4SLinus Torvalds this.hash = 0; /* will go */ 25891da177e4SLinus Torvalds root = shm_mnt->mnt_root; 25902c48b9c4SAl Viro path.dentry = d_alloc(root, &this); 25912c48b9c4SAl Viro if (!path.dentry) 25921da177e4SLinus Torvalds goto put_memory; 25932c48b9c4SAl Viro path.mnt = mntget(shm_mnt); 25941da177e4SLinus Torvalds 25951da177e4SLinus Torvalds error = -ENOSPC; 2596454abafeSDmitry Monakhov inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 25971da177e4SLinus Torvalds if (!inode) 25984b42af81SAl Viro goto put_dentry; 25991da177e4SLinus Torvalds 26002c48b9c4SAl Viro d_instantiate(path.dentry, inode); 26011da177e4SLinus Torvalds inode->i_size = size; 26021da177e4SLinus Torvalds inode->i_nlink = 0; /* It is unlinked */ 2603853ac43aSMatt Mackall #ifndef CONFIG_MMU 2604853ac43aSMatt Mackall error = ramfs_nommu_expand_for_mapping(inode, size); 2605853ac43aSMatt Mackall if (error) 26064b42af81SAl Viro goto put_dentry; 2607853ac43aSMatt Mackall #endif 26084b42af81SAl Viro 26094b42af81SAl Viro error = -ENFILE; 26102c48b9c4SAl Viro file = alloc_file(&path, FMODE_WRITE | FMODE_READ, 26114b42af81SAl Viro &shmem_file_operations); 26124b42af81SAl Viro if (!file) 26134b42af81SAl Viro goto put_dentry; 26144b42af81SAl Viro 26151da177e4SLinus Torvalds return file; 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds put_dentry: 26182c48b9c4SAl Viro path_put(&path); 26191da177e4SLinus Torvalds put_memory: 26201da177e4SLinus Torvalds shmem_unacct_size(flags, size); 26211da177e4SLinus Torvalds return ERR_PTR(error); 26221da177e4SLinus Torvalds } 2623395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 26241da177e4SLinus Torvalds 262546711810SRandy Dunlap /** 26261da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 26271da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 26281da177e4SLinus Torvalds */ 26291da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 26301da177e4SLinus Torvalds { 26311da177e4SLinus Torvalds struct file *file; 26321da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 26331da177e4SLinus Torvalds 26341da177e4SLinus Torvalds file = shmem_file_setup("dev/zero", size, vma->vm_flags); 26351da177e4SLinus Torvalds if (IS_ERR(file)) 26361da177e4SLinus Torvalds return PTR_ERR(file); 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds if (vma->vm_file) 26391da177e4SLinus Torvalds fput(vma->vm_file); 26401da177e4SLinus Torvalds vma->vm_file = file; 26411da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2642bee4c36aSHugh Dickins vma->vm_flags |= VM_CAN_NONLINEAR; 26431da177e4SLinus Torvalds return 0; 26441da177e4SLinus Torvalds } 2645d9d90e5eSHugh Dickins 2646d9d90e5eSHugh Dickins /** 2647d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 2648d9d90e5eSHugh Dickins * @mapping: the page's address_space 2649d9d90e5eSHugh Dickins * @index: the page index 2650d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 2651d9d90e5eSHugh Dickins * 2652d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 2653d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 2654d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 2655d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 2656d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 2657d9d90e5eSHugh Dickins * 265868da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 265968da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 2660d9d90e5eSHugh Dickins */ 2661d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 2662d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 2663d9d90e5eSHugh Dickins { 266468da9f05SHugh Dickins #ifdef CONFIG_SHMEM 266568da9f05SHugh Dickins struct inode *inode = mapping->host; 26669276aad6SHugh Dickins struct page *page; 266768da9f05SHugh Dickins int error; 266868da9f05SHugh Dickins 266968da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 267068da9f05SHugh Dickins error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL); 267168da9f05SHugh Dickins if (error) 267268da9f05SHugh Dickins page = ERR_PTR(error); 267368da9f05SHugh Dickins else 267468da9f05SHugh Dickins unlock_page(page); 267568da9f05SHugh Dickins return page; 267668da9f05SHugh Dickins #else 267768da9f05SHugh Dickins /* 267868da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 267968da9f05SHugh Dickins */ 2680d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 268168da9f05SHugh Dickins #endif 2682d9d90e5eSHugh Dickins } 2683d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 2684