11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Resizable virtual memory filesystem for Linux. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000 Linus Torvalds. 51da177e4SLinus Torvalds * 2000 Transmeta Corp. 61da177e4SLinus Torvalds * 2000-2001 Christoph Rohland 71da177e4SLinus Torvalds * 2000-2001 SAP AG 81da177e4SLinus Torvalds * 2002 Red Hat Inc. 96922c0c7SHugh Dickins * Copyright (C) 2002-2011 Hugh Dickins. 106922c0c7SHugh Dickins * Copyright (C) 2011 Google Inc. 110edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 121da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Extended attribute support for tmpfs: 151da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 161da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 171da177e4SLinus Torvalds * 18853ac43aSMatt Mackall * tiny-shmem: 19853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 20853ac43aSMatt Mackall * 211da177e4SLinus Torvalds * This file is released under the GPL. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 24853ac43aSMatt Mackall #include <linux/fs.h> 25853ac43aSMatt Mackall #include <linux/init.h> 26853ac43aSMatt Mackall #include <linux/vfs.h> 27853ac43aSMatt Mackall #include <linux/mount.h> 28caefba17SHugh Dickins #include <linux/pagemap.h> 29853ac43aSMatt Mackall #include <linux/file.h> 30853ac43aSMatt Mackall #include <linux/mm.h> 31*b95f1b31SPaul Gortmaker #include <linux/export.h> 32853ac43aSMatt Mackall #include <linux/swap.h> 33853ac43aSMatt Mackall 34853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 35853ac43aSMatt Mackall 36853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 391da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 401da177e4SLinus Torvalds * which makes it a completely usable filesystem. 411da177e4SLinus Torvalds */ 421da177e4SLinus Torvalds 4339f0247dSAndreas Gruenbacher #include <linux/xattr.h> 44a5694255SChristoph Hellwig #include <linux/exportfs.h> 451c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 4639f0247dSAndreas Gruenbacher #include <linux/generic_acl.h> 471da177e4SLinus Torvalds #include <linux/mman.h> 481da177e4SLinus Torvalds #include <linux/string.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 501da177e4SLinus Torvalds #include <linux/backing-dev.h> 511da177e4SLinus Torvalds #include <linux/shmem_fs.h> 521da177e4SLinus Torvalds #include <linux/writeback.h> 531da177e4SLinus Torvalds #include <linux/blkdev.h> 54bda97eabSHugh Dickins #include <linux/pagevec.h> 5541ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 56708e3508SHugh Dickins #include <linux/splice.h> 571da177e4SLinus Torvalds #include <linux/security.h> 581da177e4SLinus Torvalds #include <linux/swapops.h> 591da177e4SLinus Torvalds #include <linux/mempolicy.h> 601da177e4SLinus Torvalds #include <linux/namei.h> 61b00dc3adSHugh Dickins #include <linux/ctype.h> 62304dbdb7SLee Schermerhorn #include <linux/migrate.h> 63c1f60a5aSChristoph Lameter #include <linux/highmem.h> 64680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 6592562927SMimi Zohar #include <linux/magic.h> 66304dbdb7SLee Schermerhorn 671da177e4SLinus Torvalds #include <asm/uaccess.h> 681da177e4SLinus Torvalds #include <asm/pgtable.h> 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) 711da177e4SLinus Torvalds #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 741da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 751da177e4SLinus Torvalds 7669f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 7769f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 7869f07ec9SHugh Dickins 79b09e0fa4SEric Paris struct shmem_xattr { 80b09e0fa4SEric Paris struct list_head list; /* anchored by shmem_inode_info->xattr_list */ 81b09e0fa4SEric Paris char *name; /* xattr name */ 82b09e0fa4SEric Paris size_t size; 83b09e0fa4SEric Paris char value[0]; 84b09e0fa4SEric Paris }; 85b09e0fa4SEric Paris 86285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */ 871da177e4SLinus Torvalds enum sgp_type { 881da177e4SLinus Torvalds SGP_READ, /* don't exceed i_size, don't allocate page */ 891da177e4SLinus Torvalds SGP_CACHE, /* don't exceed i_size, may allocate page */ 90a0ee5ec5SHugh Dickins SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ 911da177e4SLinus Torvalds SGP_WRITE, /* may exceed i_size, may allocate page */ 921da177e4SLinus Torvalds }; 931da177e4SLinus Torvalds 94b76db735SAndrew Morton #ifdef CONFIG_TMPFS 95680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 96680d794bSakpm@linux-foundation.org { 97680d794bSakpm@linux-foundation.org return totalram_pages / 2; 98680d794bSakpm@linux-foundation.org } 99680d794bSakpm@linux-foundation.org 100680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 101680d794bSakpm@linux-foundation.org { 102680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 103680d794bSakpm@linux-foundation.org } 104b76db735SAndrew Morton #endif 105680d794bSakpm@linux-foundation.org 10668da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 10768da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type); 10868da9f05SHugh Dickins 10968da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index, 11068da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, int *fault_type) 11168da9f05SHugh Dickins { 11268da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 11368da9f05SHugh Dickins mapping_gfp_mask(inode->i_mapping), fault_type); 11468da9f05SHugh Dickins } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds return sb->s_fs_info; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* 1221da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1231da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1241da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1251da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1281da177e4SLinus Torvalds { 1290b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 1300b0a0806SHugh Dickins 0 : security_vm_enough_memory_kern(VM_ACCT(size)); 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1341da177e4SLinus Torvalds { 1350b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1361da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* 1401da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 1411da177e4SLinus Torvalds * pages are allocated, in order to allow huge sparse files. 1421da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1431da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1441da177e4SLinus Torvalds */ 1451da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags) 1461da177e4SLinus Torvalds { 1470b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 1480b0a0806SHugh Dickins security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0; 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1521da177e4SLinus Torvalds { 1530b0a0806SHugh Dickins if (flags & VM_NORESERVE) 1541da177e4SLinus Torvalds vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 157759b9775SHugh Dickins static const struct super_operations shmem_ops; 158f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 15915ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 16092e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 16192e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 16292e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 163f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 1641da177e4SLinus Torvalds 1656c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info __read_mostly = { 1661da177e4SLinus Torvalds .ra_pages = 0, /* No readahead */ 1674f98a2feSRik van Riel .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, 1681da177e4SLinus Torvalds }; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 171cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 1721da177e4SLinus Torvalds 1735b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 1745b04c689SPavel Emelyanov { 1755b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1765b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 1775b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 1785b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 1795b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1805b04c689SPavel Emelyanov return -ENOSPC; 1815b04c689SPavel Emelyanov } 1825b04c689SPavel Emelyanov sbinfo->free_inodes--; 1835b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1845b04c689SPavel Emelyanov } 1855b04c689SPavel Emelyanov return 0; 1865b04c689SPavel Emelyanov } 1875b04c689SPavel Emelyanov 1885b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 1895b04c689SPavel Emelyanov { 1905b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1915b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 1925b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 1935b04c689SPavel Emelyanov sbinfo->free_inodes++; 1945b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1955b04c689SPavel Emelyanov } 1965b04c689SPavel Emelyanov } 1975b04c689SPavel Emelyanov 19846711810SRandy Dunlap /** 19941ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2001da177e4SLinus Torvalds * @inode: inode to recalc 2011da177e4SLinus Torvalds * 2021da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2031da177e4SLinus Torvalds * undirtied hole pages behind our back. 2041da177e4SLinus Torvalds * 2051da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2061da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2071da177e4SLinus Torvalds * 2081da177e4SLinus Torvalds * It has to be called with the spinlock held. 2091da177e4SLinus Torvalds */ 2101da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2131da177e4SLinus Torvalds long freed; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2161da177e4SLinus Torvalds if (freed > 0) { 21754af6042SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 21854af6042SHugh Dickins if (sbinfo->max_blocks) 21954af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -freed); 2201da177e4SLinus Torvalds info->alloced -= freed; 22154af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2221da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2267a5d0fbbSHugh Dickins /* 2277a5d0fbbSHugh Dickins * Replace item expected in radix tree by a new item, while holding tree lock. 2287a5d0fbbSHugh Dickins */ 2297a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping, 2307a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 2317a5d0fbbSHugh Dickins { 2327a5d0fbbSHugh Dickins void **pslot; 2337a5d0fbbSHugh Dickins void *item = NULL; 2347a5d0fbbSHugh Dickins 2357a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 2367a5d0fbbSHugh Dickins pslot = radix_tree_lookup_slot(&mapping->page_tree, index); 2377a5d0fbbSHugh Dickins if (pslot) 2387a5d0fbbSHugh Dickins item = radix_tree_deref_slot_protected(pslot, 2397a5d0fbbSHugh Dickins &mapping->tree_lock); 2407a5d0fbbSHugh Dickins if (item != expected) 2417a5d0fbbSHugh Dickins return -ENOENT; 2427a5d0fbbSHugh Dickins if (replacement) 2437a5d0fbbSHugh Dickins radix_tree_replace_slot(pslot, replacement); 2447a5d0fbbSHugh Dickins else 2457a5d0fbbSHugh Dickins radix_tree_delete(&mapping->page_tree, index); 2467a5d0fbbSHugh Dickins return 0; 2477a5d0fbbSHugh Dickins } 2487a5d0fbbSHugh Dickins 2497a5d0fbbSHugh Dickins /* 25046f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 25146f65ec1SHugh Dickins */ 25246f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 25346f65ec1SHugh Dickins struct address_space *mapping, 25446f65ec1SHugh Dickins pgoff_t index, gfp_t gfp, void *expected) 25546f65ec1SHugh Dickins { 256aa3b1895SHugh Dickins int error = 0; 25746f65ec1SHugh Dickins 25846f65ec1SHugh Dickins VM_BUG_ON(!PageLocked(page)); 25946f65ec1SHugh Dickins VM_BUG_ON(!PageSwapBacked(page)); 26046f65ec1SHugh Dickins 26146f65ec1SHugh Dickins if (!expected) 26246f65ec1SHugh Dickins error = radix_tree_preload(gfp & GFP_RECLAIM_MASK); 26346f65ec1SHugh Dickins if (!error) { 26446f65ec1SHugh Dickins page_cache_get(page); 26546f65ec1SHugh Dickins page->mapping = mapping; 26646f65ec1SHugh Dickins page->index = index; 26746f65ec1SHugh Dickins 26846f65ec1SHugh Dickins spin_lock_irq(&mapping->tree_lock); 26946f65ec1SHugh Dickins if (!expected) 27046f65ec1SHugh Dickins error = radix_tree_insert(&mapping->page_tree, 27146f65ec1SHugh Dickins index, page); 27246f65ec1SHugh Dickins else 27346f65ec1SHugh Dickins error = shmem_radix_tree_replace(mapping, index, 27446f65ec1SHugh Dickins expected, page); 27546f65ec1SHugh Dickins if (!error) { 27646f65ec1SHugh Dickins mapping->nrpages++; 27746f65ec1SHugh Dickins __inc_zone_page_state(page, NR_FILE_PAGES); 27846f65ec1SHugh Dickins __inc_zone_page_state(page, NR_SHMEM); 27946f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 28046f65ec1SHugh Dickins } else { 28146f65ec1SHugh Dickins page->mapping = NULL; 28246f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 28346f65ec1SHugh Dickins page_cache_release(page); 28446f65ec1SHugh Dickins } 28546f65ec1SHugh Dickins if (!expected) 28646f65ec1SHugh Dickins radix_tree_preload_end(); 28746f65ec1SHugh Dickins } 28846f65ec1SHugh Dickins if (error) 28946f65ec1SHugh Dickins mem_cgroup_uncharge_cache_page(page); 29046f65ec1SHugh Dickins return error; 29146f65ec1SHugh Dickins } 29246f65ec1SHugh Dickins 29346f65ec1SHugh Dickins /* 2946922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 2956922c0c7SHugh Dickins */ 2966922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 2976922c0c7SHugh Dickins { 2986922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 2996922c0c7SHugh Dickins int error; 3006922c0c7SHugh Dickins 3016922c0c7SHugh Dickins spin_lock_irq(&mapping->tree_lock); 3026922c0c7SHugh Dickins error = shmem_radix_tree_replace(mapping, page->index, page, radswap); 3036922c0c7SHugh Dickins page->mapping = NULL; 3046922c0c7SHugh Dickins mapping->nrpages--; 3056922c0c7SHugh Dickins __dec_zone_page_state(page, NR_FILE_PAGES); 3066922c0c7SHugh Dickins __dec_zone_page_state(page, NR_SHMEM); 3076922c0c7SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 3086922c0c7SHugh Dickins page_cache_release(page); 3096922c0c7SHugh Dickins BUG_ON(error); 3106922c0c7SHugh Dickins } 3116922c0c7SHugh Dickins 3126922c0c7SHugh Dickins /* 3137a5d0fbbSHugh Dickins * Like find_get_pages, but collecting swap entries as well as pages. 3147a5d0fbbSHugh Dickins */ 3157a5d0fbbSHugh Dickins static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping, 3167a5d0fbbSHugh Dickins pgoff_t start, unsigned int nr_pages, 3177a5d0fbbSHugh Dickins struct page **pages, pgoff_t *indices) 3187a5d0fbbSHugh Dickins { 3197a5d0fbbSHugh Dickins unsigned int i; 3207a5d0fbbSHugh Dickins unsigned int ret; 3217a5d0fbbSHugh Dickins unsigned int nr_found; 3227a5d0fbbSHugh Dickins 3237a5d0fbbSHugh Dickins rcu_read_lock(); 3247a5d0fbbSHugh Dickins restart: 3257a5d0fbbSHugh Dickins nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree, 3267a5d0fbbSHugh Dickins (void ***)pages, indices, start, nr_pages); 3277a5d0fbbSHugh Dickins ret = 0; 3287a5d0fbbSHugh Dickins for (i = 0; i < nr_found; i++) { 3297a5d0fbbSHugh Dickins struct page *page; 3307a5d0fbbSHugh Dickins repeat: 3317a5d0fbbSHugh Dickins page = radix_tree_deref_slot((void **)pages[i]); 3327a5d0fbbSHugh Dickins if (unlikely(!page)) 3337a5d0fbbSHugh Dickins continue; 3347a5d0fbbSHugh Dickins if (radix_tree_exception(page)) { 3358079b1c8SHugh Dickins if (radix_tree_deref_retry(page)) 3367a5d0fbbSHugh Dickins goto restart; 3378079b1c8SHugh Dickins /* 3388079b1c8SHugh Dickins * Otherwise, we must be storing a swap entry 3398079b1c8SHugh Dickins * here as an exceptional entry: so return it 3408079b1c8SHugh Dickins * without attempting to raise page count. 3418079b1c8SHugh Dickins */ 3428079b1c8SHugh Dickins goto export; 3437a5d0fbbSHugh Dickins } 3447a5d0fbbSHugh Dickins if (!page_cache_get_speculative(page)) 3457a5d0fbbSHugh Dickins goto repeat; 3467a5d0fbbSHugh Dickins 3477a5d0fbbSHugh Dickins /* Has the page moved? */ 3487a5d0fbbSHugh Dickins if (unlikely(page != *((void **)pages[i]))) { 3497a5d0fbbSHugh Dickins page_cache_release(page); 3507a5d0fbbSHugh Dickins goto repeat; 3517a5d0fbbSHugh Dickins } 3527a5d0fbbSHugh Dickins export: 3537a5d0fbbSHugh Dickins indices[ret] = indices[i]; 3547a5d0fbbSHugh Dickins pages[ret] = page; 3557a5d0fbbSHugh Dickins ret++; 3567a5d0fbbSHugh Dickins } 3577a5d0fbbSHugh Dickins if (unlikely(!ret && nr_found)) 3587a5d0fbbSHugh Dickins goto restart; 3597a5d0fbbSHugh Dickins rcu_read_unlock(); 3607a5d0fbbSHugh Dickins return ret; 3617a5d0fbbSHugh Dickins } 3627a5d0fbbSHugh Dickins 3637a5d0fbbSHugh Dickins /* 3647a5d0fbbSHugh Dickins * Remove swap entry from radix tree, free the swap and its page cache. 3657a5d0fbbSHugh Dickins */ 3667a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 3677a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 3687a5d0fbbSHugh Dickins { 3697a5d0fbbSHugh Dickins int error; 3707a5d0fbbSHugh Dickins 3717a5d0fbbSHugh Dickins spin_lock_irq(&mapping->tree_lock); 3727a5d0fbbSHugh Dickins error = shmem_radix_tree_replace(mapping, index, radswap, NULL); 3737a5d0fbbSHugh Dickins spin_unlock_irq(&mapping->tree_lock); 3747a5d0fbbSHugh Dickins if (!error) 3757a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 3767a5d0fbbSHugh Dickins return error; 3777a5d0fbbSHugh Dickins } 3787a5d0fbbSHugh Dickins 3797a5d0fbbSHugh Dickins /* 3807a5d0fbbSHugh Dickins * Pagevec may contain swap entries, so shuffle up pages before releasing. 3817a5d0fbbSHugh Dickins */ 3827a5d0fbbSHugh Dickins static void shmem_pagevec_release(struct pagevec *pvec) 3837a5d0fbbSHugh Dickins { 3847a5d0fbbSHugh Dickins int i, j; 3857a5d0fbbSHugh Dickins 3867a5d0fbbSHugh Dickins for (i = 0, j = 0; i < pagevec_count(pvec); i++) { 3877a5d0fbbSHugh Dickins struct page *page = pvec->pages[i]; 3887a5d0fbbSHugh Dickins if (!radix_tree_exceptional_entry(page)) 3897a5d0fbbSHugh Dickins pvec->pages[j++] = page; 3907a5d0fbbSHugh Dickins } 3917a5d0fbbSHugh Dickins pvec->nr = j; 3927a5d0fbbSHugh Dickins pagevec_release(pvec); 3937a5d0fbbSHugh Dickins } 3947a5d0fbbSHugh Dickins 3957a5d0fbbSHugh Dickins /* 3967a5d0fbbSHugh Dickins * Remove range of pages and swap entries from radix tree, and free them. 3977a5d0fbbSHugh Dickins */ 398285b2c4fSHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 3991da177e4SLinus Torvalds { 400285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 4011da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 402285b2c4fSHugh Dickins pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 403bda97eabSHugh Dickins unsigned partial = lstart & (PAGE_CACHE_SIZE - 1); 404285b2c4fSHugh Dickins pgoff_t end = (lend >> PAGE_CACHE_SHIFT); 405bda97eabSHugh Dickins struct pagevec pvec; 4067a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 4077a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 408285b2c4fSHugh Dickins pgoff_t index; 409bda97eabSHugh Dickins int i; 4101da177e4SLinus Torvalds 411bda97eabSHugh Dickins BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1)); 412bda97eabSHugh Dickins 413bda97eabSHugh Dickins pagevec_init(&pvec, 0); 414bda97eabSHugh Dickins index = start; 4157a5d0fbbSHugh Dickins while (index <= end) { 4167a5d0fbbSHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 4177a5d0fbbSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1, 4187a5d0fbbSHugh Dickins pvec.pages, indices); 4197a5d0fbbSHugh Dickins if (!pvec.nr) 4207a5d0fbbSHugh Dickins break; 421bda97eabSHugh Dickins mem_cgroup_uncharge_start(); 422bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 423bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 424bda97eabSHugh Dickins 4257a5d0fbbSHugh Dickins index = indices[i]; 426bda97eabSHugh Dickins if (index > end) 427bda97eabSHugh Dickins break; 428bda97eabSHugh Dickins 4297a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 4307a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 4317a5d0fbbSHugh Dickins index, page); 4327a5d0fbbSHugh Dickins continue; 4337a5d0fbbSHugh Dickins } 4347a5d0fbbSHugh Dickins 435bda97eabSHugh Dickins if (!trylock_page(page)) 436bda97eabSHugh Dickins continue; 4377a5d0fbbSHugh Dickins if (page->mapping == mapping) { 4387a5d0fbbSHugh Dickins VM_BUG_ON(PageWriteback(page)); 439bda97eabSHugh Dickins truncate_inode_page(mapping, page); 4407a5d0fbbSHugh Dickins } 441bda97eabSHugh Dickins unlock_page(page); 442bda97eabSHugh Dickins } 4437a5d0fbbSHugh Dickins shmem_pagevec_release(&pvec); 444bda97eabSHugh Dickins mem_cgroup_uncharge_end(); 445bda97eabSHugh Dickins cond_resched(); 446bda97eabSHugh Dickins index++; 447bda97eabSHugh Dickins } 448bda97eabSHugh Dickins 449bda97eabSHugh Dickins if (partial) { 450bda97eabSHugh Dickins struct page *page = NULL; 451bda97eabSHugh Dickins shmem_getpage(inode, start - 1, &page, SGP_READ, NULL); 452bda97eabSHugh Dickins if (page) { 453bda97eabSHugh Dickins zero_user_segment(page, partial, PAGE_CACHE_SIZE); 454bda97eabSHugh Dickins set_page_dirty(page); 455bda97eabSHugh Dickins unlock_page(page); 456bda97eabSHugh Dickins page_cache_release(page); 457bda97eabSHugh Dickins } 458bda97eabSHugh Dickins } 459bda97eabSHugh Dickins 460bda97eabSHugh Dickins index = start; 461bda97eabSHugh Dickins for ( ; ; ) { 462bda97eabSHugh Dickins cond_resched(); 4637a5d0fbbSHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 4647a5d0fbbSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1, 4657a5d0fbbSHugh Dickins pvec.pages, indices); 4667a5d0fbbSHugh Dickins if (!pvec.nr) { 467bda97eabSHugh Dickins if (index == start) 468bda97eabSHugh Dickins break; 469bda97eabSHugh Dickins index = start; 470bda97eabSHugh Dickins continue; 471bda97eabSHugh Dickins } 4727a5d0fbbSHugh Dickins if (index == start && indices[0] > end) { 4737a5d0fbbSHugh Dickins shmem_pagevec_release(&pvec); 474bda97eabSHugh Dickins break; 475bda97eabSHugh Dickins } 476bda97eabSHugh Dickins mem_cgroup_uncharge_start(); 477bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 478bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 479bda97eabSHugh Dickins 4807a5d0fbbSHugh Dickins index = indices[i]; 481bda97eabSHugh Dickins if (index > end) 482bda97eabSHugh Dickins break; 483bda97eabSHugh Dickins 4847a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 4857a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 4867a5d0fbbSHugh Dickins index, page); 4877a5d0fbbSHugh Dickins continue; 4887a5d0fbbSHugh Dickins } 4897a5d0fbbSHugh Dickins 490bda97eabSHugh Dickins lock_page(page); 4917a5d0fbbSHugh Dickins if (page->mapping == mapping) { 4927a5d0fbbSHugh Dickins VM_BUG_ON(PageWriteback(page)); 493bda97eabSHugh Dickins truncate_inode_page(mapping, page); 4947a5d0fbbSHugh Dickins } 495bda97eabSHugh Dickins unlock_page(page); 496bda97eabSHugh Dickins } 4977a5d0fbbSHugh Dickins shmem_pagevec_release(&pvec); 498bda97eabSHugh Dickins mem_cgroup_uncharge_end(); 499bda97eabSHugh Dickins index++; 500bda97eabSHugh Dickins } 50194c1e62dSHugh Dickins 5021da177e4SLinus Torvalds spin_lock(&info->lock); 5037a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 5041da177e4SLinus Torvalds shmem_recalc_inode(inode); 5051da177e4SLinus Torvalds spin_unlock(&info->lock); 5061da177e4SLinus Torvalds 507285b2c4fSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 5081da177e4SLinus Torvalds } 50994c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 5101da177e4SLinus Torvalds 51194c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 5121da177e4SLinus Torvalds { 5131da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 5141da177e4SLinus Torvalds int error; 5151da177e4SLinus Torvalds 516db78b877SChristoph Hellwig error = inode_change_ok(inode, attr); 517db78b877SChristoph Hellwig if (error) 518db78b877SChristoph Hellwig return error; 519db78b877SChristoph Hellwig 52094c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 52194c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 52294c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 5233889e6e7Snpiggin@suse.de 52494c1e62dSHugh Dickins if (newsize != oldsize) { 52594c1e62dSHugh Dickins i_size_write(inode, newsize); 52694c1e62dSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 52794c1e62dSHugh Dickins } 52894c1e62dSHugh Dickins if (newsize < oldsize) { 52994c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 53094c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 53194c1e62dSHugh Dickins shmem_truncate_range(inode, newsize, (loff_t)-1); 53294c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 53394c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 53494c1e62dSHugh Dickins } 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 5376a1a90adSChristoph Hellwig setattr_copy(inode, attr); 53839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 539db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 5401c7c474cSChristoph Hellwig error = generic_acl_chmod(inode); 54139f0247dSAndreas Gruenbacher #endif 5421da177e4SLinus Torvalds return error; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5451f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 5461da177e4SLinus Torvalds { 5471da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 548b09e0fa4SEric Paris struct shmem_xattr *xattr, *nxattr; 5491da177e4SLinus Torvalds 5503889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 5511da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 5521da177e4SLinus Torvalds inode->i_size = 0; 5533889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 5541da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 555cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 5561da177e4SLinus Torvalds list_del_init(&info->swaplist); 557cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 5581da177e4SLinus Torvalds } 55969f07ec9SHugh Dickins } else 56069f07ec9SHugh Dickins kfree(info->symlink); 561b09e0fa4SEric Paris 562b09e0fa4SEric Paris list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) { 563b09e0fa4SEric Paris kfree(xattr->name); 564b09e0fa4SEric Paris kfree(xattr); 565b09e0fa4SEric Paris } 5661da177e4SLinus Torvalds BUG_ON(inode->i_blocks); 5675b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 5681f895f75SAl Viro end_writeback(inode); 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 57146f65ec1SHugh Dickins /* 57246f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 57346f65ec1SHugh Dickins */ 57441ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 57541ffe5d5SHugh Dickins swp_entry_t swap, struct page *page) 5761da177e4SLinus Torvalds { 577285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 57846f65ec1SHugh Dickins void *radswap; 57941ffe5d5SHugh Dickins pgoff_t index; 580d9fe526aSHugh Dickins int error; 5811da177e4SLinus Torvalds 58246f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 583e504f3fdSHugh Dickins index = radix_tree_locate_item(&mapping->page_tree, radswap); 58446f65ec1SHugh Dickins if (index == -1) 5851da177e4SLinus Torvalds return 0; 5862e0e26c7SHugh Dickins 5871b1b32f2SHugh Dickins /* 5881b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 5891f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 5901b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 591285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 5921b1b32f2SHugh Dickins */ 5931b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 5942e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 5952e0e26c7SHugh Dickins 596d13d1443SKAMEZAWA Hiroyuki /* 597778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 598778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 599778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 600d13d1443SKAMEZAWA Hiroyuki */ 60146f65ec1SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 60246f65ec1SHugh Dickins GFP_NOWAIT, radswap); 603778dd893SHugh Dickins /* which does mem_cgroup_uncharge_cache_page on error */ 60469029cd5SKAMEZAWA Hiroyuki 60548f170fbSHugh Dickins if (error != -ENOMEM) { 60646f65ec1SHugh Dickins /* 60746f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 60846f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 60946f65ec1SHugh Dickins */ 61073b1262fSHugh Dickins delete_from_swap_cache(page); 61173b1262fSHugh Dickins set_page_dirty(page); 61246f65ec1SHugh Dickins if (!error) { 61346f65ec1SHugh Dickins spin_lock(&info->lock); 614285b2c4fSHugh Dickins info->swapped--; 61546f65ec1SHugh Dickins spin_unlock(&info->lock); 61641ffe5d5SHugh Dickins swap_free(swap); 61746f65ec1SHugh Dickins } 6182e0e26c7SHugh Dickins error = 1; /* not an error, but entry was found */ 6191da177e4SLinus Torvalds } 6202e0e26c7SHugh Dickins return error; 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds /* 62446f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 6251da177e4SLinus Torvalds */ 62641ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 6271da177e4SLinus Torvalds { 62841ffe5d5SHugh Dickins struct list_head *this, *next; 6291da177e4SLinus Torvalds struct shmem_inode_info *info; 6301da177e4SLinus Torvalds int found = 0; 631778dd893SHugh Dickins int error; 632778dd893SHugh Dickins 633778dd893SHugh Dickins /* 634778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 635778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 636778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 637778dd893SHugh Dickins */ 638778dd893SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL); 639778dd893SHugh Dickins if (error) 640778dd893SHugh Dickins goto out; 64146f65ec1SHugh Dickins /* No radix_tree_preload: swap entry keeps a place for page in tree */ 6421da177e4SLinus Torvalds 643cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 64441ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 64541ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 646285b2c4fSHugh Dickins if (info->swapped) 64741ffe5d5SHugh Dickins found = shmem_unuse_inode(info, swap, page); 6486922c0c7SHugh Dickins else 6496922c0c7SHugh Dickins list_del_init(&info->swaplist); 650cb5f7b9aSHugh Dickins cond_resched(); 6512e0e26c7SHugh Dickins if (found) 652778dd893SHugh Dickins break; 6531da177e4SLinus Torvalds } 654cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 655778dd893SHugh Dickins 656778dd893SHugh Dickins if (!found) 657778dd893SHugh Dickins mem_cgroup_uncharge_cache_page(page); 658778dd893SHugh Dickins if (found < 0) 659778dd893SHugh Dickins error = found; 660778dd893SHugh Dickins out: 661aaa46865SHugh Dickins unlock_page(page); 662aaa46865SHugh Dickins page_cache_release(page); 663778dd893SHugh Dickins return error; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds /* 6671da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 6681da177e4SLinus Torvalds */ 6691da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 6701da177e4SLinus Torvalds { 6711da177e4SLinus Torvalds struct shmem_inode_info *info; 6721da177e4SLinus Torvalds struct address_space *mapping; 6731da177e4SLinus Torvalds struct inode *inode; 6746922c0c7SHugh Dickins swp_entry_t swap; 6756922c0c7SHugh Dickins pgoff_t index; 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 6781da177e4SLinus Torvalds mapping = page->mapping; 6791da177e4SLinus Torvalds index = page->index; 6801da177e4SLinus Torvalds inode = mapping->host; 6811da177e4SLinus Torvalds info = SHMEM_I(inode); 6821da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 6831da177e4SLinus Torvalds goto redirty; 684d9fe526aSHugh Dickins if (!total_swap_pages) 6851da177e4SLinus Torvalds goto redirty; 6861da177e4SLinus Torvalds 687d9fe526aSHugh Dickins /* 688d9fe526aSHugh Dickins * shmem_backing_dev_info's capabilities prevent regular writeback or 689d9fe526aSHugh Dickins * sync from ever calling shmem_writepage; but a stacking filesystem 69048f170fbSHugh Dickins * might use ->writepage of its underlying filesystem, in which case 691d9fe526aSHugh Dickins * tmpfs should write out to swap only in response to memory pressure, 69248f170fbSHugh Dickins * and not for the writeback threads or sync. 693d9fe526aSHugh Dickins */ 69448f170fbSHugh Dickins if (!wbc->for_reclaim) { 69548f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 69648f170fbSHugh Dickins goto redirty; 69748f170fbSHugh Dickins } 698d9fe526aSHugh Dickins swap = get_swap_page(); 69948f170fbSHugh Dickins if (!swap.val) 70048f170fbSHugh Dickins goto redirty; 701d9fe526aSHugh Dickins 702b1dea800SHugh Dickins /* 703b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 7046922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 7056922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 706b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 7076922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 7086922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 709b1dea800SHugh Dickins */ 710b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 71105bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 71205bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 713b1dea800SHugh Dickins 71448f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 715aaa46865SHugh Dickins swap_shmem_alloc(swap); 7166922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 7176922c0c7SHugh Dickins 7186922c0c7SHugh Dickins spin_lock(&info->lock); 7196922c0c7SHugh Dickins info->swapped++; 7206922c0c7SHugh Dickins shmem_recalc_inode(inode); 721826267cfSHugh Dickins spin_unlock(&info->lock); 7226922c0c7SHugh Dickins 7236922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 724d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 7259fab5619SHugh Dickins swap_writepage(page, wbc); 7261da177e4SLinus Torvalds return 0; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7296922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 730cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, NULL); 7311da177e4SLinus Torvalds redirty: 7321da177e4SLinus Torvalds set_page_dirty(page); 733d9fe526aSHugh Dickins if (wbc->for_reclaim) 734d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 735d9fe526aSHugh Dickins unlock_page(page); 736d9fe526aSHugh Dickins return 0; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds #ifdef CONFIG_NUMA 740680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 74171fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 742680d794bSakpm@linux-foundation.org { 743680d794bSakpm@linux-foundation.org char buffer[64]; 744680d794bSakpm@linux-foundation.org 74571fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 746095f1fc4SLee Schermerhorn return; /* show nothing */ 747095f1fc4SLee Schermerhorn 74871fe804bSLee Schermerhorn mpol_to_str(buffer, sizeof(buffer), mpol, 1); 749095f1fc4SLee Schermerhorn 750095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 751680d794bSakpm@linux-foundation.org } 75271fe804bSLee Schermerhorn 75371fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 75471fe804bSLee Schermerhorn { 75571fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 75671fe804bSLee Schermerhorn if (sbinfo->mpol) { 75771fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 75871fe804bSLee Schermerhorn mpol = sbinfo->mpol; 75971fe804bSLee Schermerhorn mpol_get(mpol); 76071fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 76171fe804bSLee Schermerhorn } 76271fe804bSLee Schermerhorn return mpol; 76371fe804bSLee Schermerhorn } 764680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 765680d794bSakpm@linux-foundation.org 76641ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 76741ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 7681da177e4SLinus Torvalds { 76952cd3b07SLee Schermerhorn struct mempolicy mpol, *spol; 7701da177e4SLinus Torvalds struct vm_area_struct pvma; 7711da177e4SLinus Torvalds 77252cd3b07SLee Schermerhorn spol = mpol_cond_copy(&mpol, 77341ffe5d5SHugh Dickins mpol_shared_policy_lookup(&info->policy, index)); 77452cd3b07SLee Schermerhorn 7751da177e4SLinus Torvalds /* Create a pseudo vma that just contains the policy */ 776c4cc6d07SHugh Dickins pvma.vm_start = 0; 77741ffe5d5SHugh Dickins pvma.vm_pgoff = index; 778c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 77952cd3b07SLee Schermerhorn pvma.vm_policy = spol; 78041ffe5d5SHugh Dickins return swapin_readahead(swap, gfp, &pvma, 0); 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 78302098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp, 78441ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 7851da177e4SLinus Torvalds { 7861da177e4SLinus Torvalds struct vm_area_struct pvma; 7871da177e4SLinus Torvalds 788c4cc6d07SHugh Dickins /* Create a pseudo vma that just contains the policy */ 789c4cc6d07SHugh Dickins pvma.vm_start = 0; 79041ffe5d5SHugh Dickins pvma.vm_pgoff = index; 791c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 79241ffe5d5SHugh Dickins pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 79352cd3b07SLee Schermerhorn 79452cd3b07SLee Schermerhorn /* 79552cd3b07SLee Schermerhorn * alloc_page_vma() will drop the shared policy reference 79652cd3b07SLee Schermerhorn */ 79752cd3b07SLee Schermerhorn return alloc_page_vma(gfp, &pvma, 0); 7981da177e4SLinus Torvalds } 799680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */ 800680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 80141ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 802680d794bSakpm@linux-foundation.org { 803680d794bSakpm@linux-foundation.org } 804680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 805680d794bSakpm@linux-foundation.org 80641ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 80741ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 8081da177e4SLinus Torvalds { 80941ffe5d5SHugh Dickins return swapin_readahead(swap, gfp, NULL, 0); 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 81202098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp, 81341ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 8141da177e4SLinus Torvalds { 815e84e2e13SHugh Dickins return alloc_page(gfp); 8161da177e4SLinus Torvalds } 817680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */ 8181da177e4SLinus Torvalds 81971fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) 82071fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 82171fe804bSLee Schermerhorn { 82271fe804bSLee Schermerhorn return NULL; 82371fe804bSLee Schermerhorn } 82471fe804bSLee Schermerhorn #endif 82571fe804bSLee Schermerhorn 8261da177e4SLinus Torvalds /* 82768da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 8281da177e4SLinus Torvalds * 8291da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 8301da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 8311da177e4SLinus Torvalds * entry since a page cannot live in both the swap and page cache 8321da177e4SLinus Torvalds */ 83341ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 83468da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) 8351da177e4SLinus Torvalds { 8361da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 83754af6042SHugh Dickins struct shmem_inode_info *info; 8381da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 83927ab7006SHugh Dickins struct page *page; 8401da177e4SLinus Torvalds swp_entry_t swap; 8411da177e4SLinus Torvalds int error; 84254af6042SHugh Dickins int once = 0; 8431da177e4SLinus Torvalds 84441ffe5d5SHugh Dickins if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT)) 8451da177e4SLinus Torvalds return -EFBIG; 8461da177e4SLinus Torvalds repeat: 84754af6042SHugh Dickins swap.val = 0; 84841ffe5d5SHugh Dickins page = find_lock_page(mapping, index); 84954af6042SHugh Dickins if (radix_tree_exceptional_entry(page)) { 85054af6042SHugh Dickins swap = radix_to_swp_entry(page); 85154af6042SHugh Dickins page = NULL; 85254af6042SHugh Dickins } 85354af6042SHugh Dickins 85454af6042SHugh Dickins if (sgp != SGP_WRITE && 85554af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 85654af6042SHugh Dickins error = -EINVAL; 85754af6042SHugh Dickins goto failed; 85854af6042SHugh Dickins } 85954af6042SHugh Dickins 86054af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 86127ab7006SHugh Dickins /* 86227ab7006SHugh Dickins * Once we can get the page lock, it must be uptodate: 86327ab7006SHugh Dickins * if there were an error in reading back from swap, 86427ab7006SHugh Dickins * the page would not be inserted into the filecache. 86527ab7006SHugh Dickins */ 86654af6042SHugh Dickins BUG_ON(page && !PageUptodate(page)); 86754af6042SHugh Dickins *pagep = page; 86854af6042SHugh Dickins return 0; 86927ab7006SHugh Dickins } 87027ab7006SHugh Dickins 871b409f9fcSHugh Dickins /* 87254af6042SHugh Dickins * Fast cache lookup did not find it: 87354af6042SHugh Dickins * bring it back from swap or allocate. 874b409f9fcSHugh Dickins */ 87554af6042SHugh Dickins info = SHMEM_I(inode); 87654af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 87727ab7006SHugh Dickins 8781da177e4SLinus Torvalds if (swap.val) { 8791da177e4SLinus Torvalds /* Look it up and read it in.. */ 88027ab7006SHugh Dickins page = lookup_swap_cache(swap); 88127ab7006SHugh Dickins if (!page) { 882456f998eSYing Han /* here we actually do the io */ 88368da9f05SHugh Dickins if (fault_type) 88468da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 88541ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 88627ab7006SHugh Dickins if (!page) { 8871da177e4SLinus Torvalds error = -ENOMEM; 88854af6042SHugh Dickins goto failed; 889285b2c4fSHugh Dickins } 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 89354af6042SHugh Dickins lock_page(page); 89427ab7006SHugh Dickins if (!PageUptodate(page)) { 8951da177e4SLinus Torvalds error = -EIO; 89654af6042SHugh Dickins goto failed; 89754af6042SHugh Dickins } 89854af6042SHugh Dickins wait_on_page_writeback(page); 89954af6042SHugh Dickins 90054af6042SHugh Dickins /* Someone may have already done it for us */ 90154af6042SHugh Dickins if (page->mapping) { 90254af6042SHugh Dickins if (page->mapping == mapping && 90354af6042SHugh Dickins page->index == index) 90454af6042SHugh Dickins goto done; 90554af6042SHugh Dickins error = -EEXIST; 90654af6042SHugh Dickins goto failed; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 909aa3b1895SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, 910aa3b1895SHugh Dickins gfp & GFP_RECLAIM_MASK); 911aa3b1895SHugh Dickins if (!error) 91254af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 91354af6042SHugh Dickins gfp, swp_to_radix_entry(swap)); 91454af6042SHugh Dickins if (error) 91554af6042SHugh Dickins goto failed; 91654af6042SHugh Dickins 91754af6042SHugh Dickins spin_lock(&info->lock); 91854af6042SHugh Dickins info->swapped--; 91954af6042SHugh Dickins shmem_recalc_inode(inode); 9201da177e4SLinus Torvalds spin_unlock(&info->lock); 92127ab7006SHugh Dickins 92227ab7006SHugh Dickins delete_from_swap_cache(page); 92327ab7006SHugh Dickins set_page_dirty(page); 92427ab7006SHugh Dickins swap_free(swap); 92527ab7006SHugh Dickins 92654af6042SHugh Dickins } else { 92754af6042SHugh Dickins if (shmem_acct_block(info->flags)) { 92854af6042SHugh Dickins error = -ENOSPC; 92954af6042SHugh Dickins goto failed; 9301da177e4SLinus Torvalds } 9310edd73b3SHugh Dickins if (sbinfo->max_blocks) { 932fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 93354af6042SHugh Dickins sbinfo->max_blocks) >= 0) { 93454af6042SHugh Dickins error = -ENOSPC; 93554af6042SHugh Dickins goto unacct; 93654af6042SHugh Dickins } 9377e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 93859a16eadSHugh Dickins } 9391da177e4SLinus Torvalds 94054af6042SHugh Dickins page = shmem_alloc_page(gfp, info, index); 94154af6042SHugh Dickins if (!page) { 94254af6042SHugh Dickins error = -ENOMEM; 94354af6042SHugh Dickins goto decused; 94454af6042SHugh Dickins } 94554af6042SHugh Dickins 94654af6042SHugh Dickins SetPageSwapBacked(page); 94754af6042SHugh Dickins __set_page_locked(page); 948aa3b1895SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, 949aa3b1895SHugh Dickins gfp & GFP_RECLAIM_MASK); 950aa3b1895SHugh Dickins if (!error) 95154af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 95254af6042SHugh Dickins gfp, NULL); 95354af6042SHugh Dickins if (error) 95454af6042SHugh Dickins goto decused; 95554af6042SHugh Dickins lru_cache_add_anon(page); 95654af6042SHugh Dickins 95754af6042SHugh Dickins spin_lock(&info->lock); 9581da177e4SLinus Torvalds info->alloced++; 95954af6042SHugh Dickins inode->i_blocks += BLOCKS_PER_PAGE; 96054af6042SHugh Dickins shmem_recalc_inode(inode); 96159a16eadSHugh Dickins spin_unlock(&info->lock); 96254af6042SHugh Dickins 96327ab7006SHugh Dickins clear_highpage(page); 96427ab7006SHugh Dickins flush_dcache_page(page); 96527ab7006SHugh Dickins SetPageUptodate(page); 966a0ee5ec5SHugh Dickins if (sgp == SGP_DIRTY) 96727ab7006SHugh Dickins set_page_dirty(page); 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds done: 97054af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 97154af6042SHugh Dickins if (sgp != SGP_WRITE && 97254af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 97354af6042SHugh Dickins error = -EINVAL; 97454af6042SHugh Dickins goto trunc; 975ff36b801SShaohua Li } 97654af6042SHugh Dickins *pagep = page; 97754af6042SHugh Dickins return 0; 978d00806b1SNick Piggin 979d0217ac0SNick Piggin /* 98054af6042SHugh Dickins * Error recovery. 9811da177e4SLinus Torvalds */ 98254af6042SHugh Dickins trunc: 98354af6042SHugh Dickins ClearPageDirty(page); 98454af6042SHugh Dickins delete_from_page_cache(page); 98554af6042SHugh Dickins spin_lock(&info->lock); 98654af6042SHugh Dickins info->alloced--; 98754af6042SHugh Dickins inode->i_blocks -= BLOCKS_PER_PAGE; 9881da177e4SLinus Torvalds spin_unlock(&info->lock); 98954af6042SHugh Dickins decused: 99054af6042SHugh Dickins if (sbinfo->max_blocks) 99154af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -1); 99254af6042SHugh Dickins unacct: 99354af6042SHugh Dickins shmem_unacct_blocks(info->flags, 1); 99454af6042SHugh Dickins failed: 99554af6042SHugh Dickins if (swap.val && error != -EINVAL) { 99654af6042SHugh Dickins struct page *test = find_get_page(mapping, index); 99754af6042SHugh Dickins if (test && !radix_tree_exceptional_entry(test)) 99854af6042SHugh Dickins page_cache_release(test); 99954af6042SHugh Dickins /* Have another try if the entry has changed */ 100054af6042SHugh Dickins if (test != swp_to_radix_entry(swap)) 100154af6042SHugh Dickins error = -EEXIST; 100254af6042SHugh Dickins } 100327ab7006SHugh Dickins if (page) { 100454af6042SHugh Dickins unlock_page(page); 10051da177e4SLinus Torvalds page_cache_release(page); 100654af6042SHugh Dickins } 100754af6042SHugh Dickins if (error == -ENOSPC && !once++) { 100854af6042SHugh Dickins info = SHMEM_I(inode); 100954af6042SHugh Dickins spin_lock(&info->lock); 101054af6042SHugh Dickins shmem_recalc_inode(inode); 101154af6042SHugh Dickins spin_unlock(&info->lock); 10121da177e4SLinus Torvalds goto repeat; 1013d8dc74f2SAdrian Bunk } 101454af6042SHugh Dickins if (error == -EEXIST) 101554af6042SHugh Dickins goto repeat; 101654af6042SHugh Dickins return error; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 10201da177e4SLinus Torvalds { 10211da177e4SLinus Torvalds struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 10221da177e4SLinus Torvalds int error; 102368da9f05SHugh Dickins int ret = VM_FAULT_LOCKED; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); 10261da177e4SLinus Torvalds if (error) 10271da177e4SLinus Torvalds return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 102868da9f05SHugh Dickins 1029456f998eSYing Han if (ret & VM_FAULT_MAJOR) { 1030456f998eSYing Han count_vm_event(PGMAJFAULT); 1031456f998eSYing Han mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 1032456f998eSYing Han } 103368da9f05SHugh Dickins return ret; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds #ifdef CONFIG_NUMA 103741ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 10381da177e4SLinus Torvalds { 103941ffe5d5SHugh Dickins struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 104041ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds 1043d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 1044d8dc74f2SAdrian Bunk unsigned long addr) 10451da177e4SLinus Torvalds { 104641ffe5d5SHugh Dickins struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 104741ffe5d5SHugh Dickins pgoff_t index; 10481da177e4SLinus Torvalds 104941ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 105041ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds #endif 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 10551da177e4SLinus Torvalds { 1056d3ac7f89SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 10571da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 10581da177e4SLinus Torvalds int retval = -ENOMEM; 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds spin_lock(&info->lock); 10611da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 10621da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 10631da177e4SLinus Torvalds goto out_nomem; 10641da177e4SLinus Torvalds info->flags |= VM_LOCKED; 106589e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 10681da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 10691da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 107089e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 107189e004eaSLee Schermerhorn scan_mapping_unevictable_pages(file->f_mapping); 10721da177e4SLinus Torvalds } 10731da177e4SLinus Torvalds retval = 0; 107489e004eaSLee Schermerhorn 10751da177e4SLinus Torvalds out_nomem: 10761da177e4SLinus Torvalds spin_unlock(&info->lock); 10771da177e4SLinus Torvalds return retval; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 10809b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 10811da177e4SLinus Torvalds { 10821da177e4SLinus Torvalds file_accessed(file); 10831da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 1084d0217ac0SNick Piggin vma->vm_flags |= VM_CAN_NONLINEAR; 10851da177e4SLinus Torvalds return 0; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 1088454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 1089454abafeSDmitry Monakhov int mode, dev_t dev, unsigned long flags) 10901da177e4SLinus Torvalds { 10911da177e4SLinus Torvalds struct inode *inode; 10921da177e4SLinus Torvalds struct shmem_inode_info *info; 10931da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 10941da177e4SLinus Torvalds 10955b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 10961da177e4SLinus Torvalds return NULL; 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds inode = new_inode(sb); 10991da177e4SLinus Torvalds if (inode) { 110085fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 1101454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 11021da177e4SLinus Torvalds inode->i_blocks = 0; 11031da177e4SLinus Torvalds inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; 11041da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 110591828a40SDavid M. Grimes inode->i_generation = get_seconds(); 11061da177e4SLinus Torvalds info = SHMEM_I(inode); 11071da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 11081da177e4SLinus Torvalds spin_lock_init(&info->lock); 11090b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 11101da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 1111b09e0fa4SEric Paris INIT_LIST_HEAD(&info->xattr_list); 111272c04902SAl Viro cache_no_acl(inode); 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds switch (mode & S_IFMT) { 11151da177e4SLinus Torvalds default: 111639f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 11171da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 11181da177e4SLinus Torvalds break; 11191da177e4SLinus Torvalds case S_IFREG: 112014fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 11211da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 11221da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 112371fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 112471fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 11251da177e4SLinus Torvalds break; 11261da177e4SLinus Torvalds case S_IFDIR: 1127d8c76e6fSDave Hansen inc_nlink(inode); 11281da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 11291da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 11301da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 11311da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 11321da177e4SLinus Torvalds break; 11331da177e4SLinus Torvalds case S_IFLNK: 11341da177e4SLinus Torvalds /* 11351da177e4SLinus Torvalds * Must not load anything in the rbtree, 11361da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 11371da177e4SLinus Torvalds */ 113871fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 11391da177e4SLinus Torvalds break; 11401da177e4SLinus Torvalds } 11415b04c689SPavel Emelyanov } else 11425b04c689SPavel Emelyanov shmem_free_inode(sb); 11431da177e4SLinus Torvalds return inode; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 114792e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 114869f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds static int 1151800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 1152800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 1153800d15a5SNick Piggin struct page **pagep, void **fsdata) 11541da177e4SLinus Torvalds { 1155800d15a5SNick Piggin struct inode *inode = mapping->host; 1156800d15a5SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 1157800d15a5SNick Piggin return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); 1158800d15a5SNick Piggin } 1159800d15a5SNick Piggin 1160800d15a5SNick Piggin static int 1161800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 1162800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 1163800d15a5SNick Piggin struct page *page, void *fsdata) 1164800d15a5SNick Piggin { 1165800d15a5SNick Piggin struct inode *inode = mapping->host; 1166800d15a5SNick Piggin 1167800d15a5SNick Piggin if (pos + copied > inode->i_size) 1168800d15a5SNick Piggin i_size_write(inode, pos + copied); 1169800d15a5SNick Piggin 1170d3602444SHugh Dickins set_page_dirty(page); 11716746aff7SWu Fengguang unlock_page(page); 1172d3602444SHugh Dickins page_cache_release(page); 1173d3602444SHugh Dickins 1174800d15a5SNick Piggin return copied; 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) 11781da177e4SLinus Torvalds { 1179d3ac7f89SJosef "Jeff" Sipek struct inode *inode = filp->f_path.dentry->d_inode; 11801da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 118141ffe5d5SHugh Dickins pgoff_t index; 118241ffe5d5SHugh Dickins unsigned long offset; 1183a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 1184a0ee5ec5SHugh Dickins 1185a0ee5ec5SHugh Dickins /* 1186a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 1187a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 1188a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 1189a0ee5ec5SHugh Dickins */ 1190a0ee5ec5SHugh Dickins if (segment_eq(get_fs(), KERNEL_DS)) 1191a0ee5ec5SHugh Dickins sgp = SGP_DIRTY; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds index = *ppos >> PAGE_CACHE_SHIFT; 11941da177e4SLinus Torvalds offset = *ppos & ~PAGE_CACHE_MASK; 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds for (;;) { 11971da177e4SLinus Torvalds struct page *page = NULL; 119841ffe5d5SHugh Dickins pgoff_t end_index; 119941ffe5d5SHugh Dickins unsigned long nr, ret; 12001da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 12031da177e4SLinus Torvalds if (index > end_index) 12041da177e4SLinus Torvalds break; 12051da177e4SLinus Torvalds if (index == end_index) { 12061da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 12071da177e4SLinus Torvalds if (nr <= offset) 12081da177e4SLinus Torvalds break; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds 1211a0ee5ec5SHugh Dickins desc->error = shmem_getpage(inode, index, &page, sgp, NULL); 12121da177e4SLinus Torvalds if (desc->error) { 12131da177e4SLinus Torvalds if (desc->error == -EINVAL) 12141da177e4SLinus Torvalds desc->error = 0; 12151da177e4SLinus Torvalds break; 12161da177e4SLinus Torvalds } 1217d3602444SHugh Dickins if (page) 1218d3602444SHugh Dickins unlock_page(page); 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds /* 12211da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 12221b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 12231da177e4SLinus Torvalds */ 12241da177e4SLinus Torvalds nr = PAGE_CACHE_SIZE; 12251da177e4SLinus Torvalds i_size = i_size_read(inode); 12261da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 12271da177e4SLinus Torvalds if (index == end_index) { 12281da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 12291da177e4SLinus Torvalds if (nr <= offset) { 12301da177e4SLinus Torvalds if (page) 12311da177e4SLinus Torvalds page_cache_release(page); 12321da177e4SLinus Torvalds break; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds nr -= offset; 12361da177e4SLinus Torvalds 12371da177e4SLinus Torvalds if (page) { 12381da177e4SLinus Torvalds /* 12391da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 12401da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 12411da177e4SLinus Torvalds * before reading the page on the kernel side. 12421da177e4SLinus Torvalds */ 12431da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 12441da177e4SLinus Torvalds flush_dcache_page(page); 12451da177e4SLinus Torvalds /* 12461da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 12471da177e4SLinus Torvalds */ 12481da177e4SLinus Torvalds if (!offset) 12491da177e4SLinus Torvalds mark_page_accessed(page); 1250b5810039SNick Piggin } else { 12511da177e4SLinus Torvalds page = ZERO_PAGE(0); 1252b5810039SNick Piggin page_cache_get(page); 1253b5810039SNick Piggin } 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds /* 12561da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 12571da177e4SLinus Torvalds * now we can copy it to user space... 12581da177e4SLinus Torvalds * 12591da177e4SLinus Torvalds * The actor routine returns how many bytes were actually used.. 12601da177e4SLinus Torvalds * NOTE! This may not be the same as how much of a user buffer 12611da177e4SLinus Torvalds * we filled up (we may be padding etc), so we can only update 12621da177e4SLinus Torvalds * "pos" here (the actor routine has to update the user buffer 12631da177e4SLinus Torvalds * pointers and the remaining count). 12641da177e4SLinus Torvalds */ 12651da177e4SLinus Torvalds ret = actor(desc, page, offset, nr); 12661da177e4SLinus Torvalds offset += ret; 12671da177e4SLinus Torvalds index += offset >> PAGE_CACHE_SHIFT; 12681da177e4SLinus Torvalds offset &= ~PAGE_CACHE_MASK; 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds page_cache_release(page); 12711da177e4SLinus Torvalds if (ret != nr || !desc->count) 12721da177e4SLinus Torvalds break; 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds cond_resched(); 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; 12781da177e4SLinus Torvalds file_accessed(filp); 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 1281bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb, 1282bcd78e49SHugh Dickins const struct iovec *iov, unsigned long nr_segs, loff_t pos) 12831da177e4SLinus Torvalds { 1284bcd78e49SHugh Dickins struct file *filp = iocb->ki_filp; 1285bcd78e49SHugh Dickins ssize_t retval; 1286bcd78e49SHugh Dickins unsigned long seg; 1287bcd78e49SHugh Dickins size_t count; 1288bcd78e49SHugh Dickins loff_t *ppos = &iocb->ki_pos; 1289bcd78e49SHugh Dickins 1290bcd78e49SHugh Dickins retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); 1291bcd78e49SHugh Dickins if (retval) 1292bcd78e49SHugh Dickins return retval; 1293bcd78e49SHugh Dickins 1294bcd78e49SHugh Dickins for (seg = 0; seg < nr_segs; seg++) { 12951da177e4SLinus Torvalds read_descriptor_t desc; 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds desc.written = 0; 1298bcd78e49SHugh Dickins desc.arg.buf = iov[seg].iov_base; 1299bcd78e49SHugh Dickins desc.count = iov[seg].iov_len; 1300bcd78e49SHugh Dickins if (desc.count == 0) 1301bcd78e49SHugh Dickins continue; 13021da177e4SLinus Torvalds desc.error = 0; 13031da177e4SLinus Torvalds do_shmem_file_read(filp, ppos, &desc, file_read_actor); 1304bcd78e49SHugh Dickins retval += desc.written; 1305bcd78e49SHugh Dickins if (desc.error) { 1306bcd78e49SHugh Dickins retval = retval ?: desc.error; 1307bcd78e49SHugh Dickins break; 1308bcd78e49SHugh Dickins } 1309bcd78e49SHugh Dickins if (desc.count > 0) 1310bcd78e49SHugh Dickins break; 1311bcd78e49SHugh Dickins } 1312bcd78e49SHugh Dickins return retval; 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds 1315708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 1316708e3508SHugh Dickins struct pipe_inode_info *pipe, size_t len, 1317708e3508SHugh Dickins unsigned int flags) 1318708e3508SHugh Dickins { 1319708e3508SHugh Dickins struct address_space *mapping = in->f_mapping; 132071f0e07aSHugh Dickins struct inode *inode = mapping->host; 1321708e3508SHugh Dickins unsigned int loff, nr_pages, req_pages; 1322708e3508SHugh Dickins struct page *pages[PIPE_DEF_BUFFERS]; 1323708e3508SHugh Dickins struct partial_page partial[PIPE_DEF_BUFFERS]; 1324708e3508SHugh Dickins struct page *page; 1325708e3508SHugh Dickins pgoff_t index, end_index; 1326708e3508SHugh Dickins loff_t isize, left; 1327708e3508SHugh Dickins int error, page_nr; 1328708e3508SHugh Dickins struct splice_pipe_desc spd = { 1329708e3508SHugh Dickins .pages = pages, 1330708e3508SHugh Dickins .partial = partial, 1331708e3508SHugh Dickins .flags = flags, 1332708e3508SHugh Dickins .ops = &page_cache_pipe_buf_ops, 1333708e3508SHugh Dickins .spd_release = spd_release_page, 1334708e3508SHugh Dickins }; 1335708e3508SHugh Dickins 133671f0e07aSHugh Dickins isize = i_size_read(inode); 1337708e3508SHugh Dickins if (unlikely(*ppos >= isize)) 1338708e3508SHugh Dickins return 0; 1339708e3508SHugh Dickins 1340708e3508SHugh Dickins left = isize - *ppos; 1341708e3508SHugh Dickins if (unlikely(left < len)) 1342708e3508SHugh Dickins len = left; 1343708e3508SHugh Dickins 1344708e3508SHugh Dickins if (splice_grow_spd(pipe, &spd)) 1345708e3508SHugh Dickins return -ENOMEM; 1346708e3508SHugh Dickins 1347708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1348708e3508SHugh Dickins loff = *ppos & ~PAGE_CACHE_MASK; 1349708e3508SHugh Dickins req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1350708e3508SHugh Dickins nr_pages = min(req_pages, pipe->buffers); 1351708e3508SHugh Dickins 1352708e3508SHugh Dickins spd.nr_pages = find_get_pages_contig(mapping, index, 1353708e3508SHugh Dickins nr_pages, spd.pages); 1354708e3508SHugh Dickins index += spd.nr_pages; 1355708e3508SHugh Dickins error = 0; 135671f0e07aSHugh Dickins 1357708e3508SHugh Dickins while (spd.nr_pages < nr_pages) { 135871f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); 135971f0e07aSHugh Dickins if (error) 1360708e3508SHugh Dickins break; 1361708e3508SHugh Dickins unlock_page(page); 1362708e3508SHugh Dickins spd.pages[spd.nr_pages++] = page; 1363708e3508SHugh Dickins index++; 1364708e3508SHugh Dickins } 1365708e3508SHugh Dickins 1366708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1367708e3508SHugh Dickins nr_pages = spd.nr_pages; 1368708e3508SHugh Dickins spd.nr_pages = 0; 136971f0e07aSHugh Dickins 1370708e3508SHugh Dickins for (page_nr = 0; page_nr < nr_pages; page_nr++) { 1371708e3508SHugh Dickins unsigned int this_len; 1372708e3508SHugh Dickins 1373708e3508SHugh Dickins if (!len) 1374708e3508SHugh Dickins break; 1375708e3508SHugh Dickins 1376708e3508SHugh Dickins this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 1377708e3508SHugh Dickins page = spd.pages[page_nr]; 1378708e3508SHugh Dickins 137971f0e07aSHugh Dickins if (!PageUptodate(page) || page->mapping != mapping) { 138071f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, 138171f0e07aSHugh Dickins SGP_CACHE, NULL); 138271f0e07aSHugh Dickins if (error) 1383708e3508SHugh Dickins break; 138471f0e07aSHugh Dickins unlock_page(page); 1385708e3508SHugh Dickins page_cache_release(spd.pages[page_nr]); 1386708e3508SHugh Dickins spd.pages[page_nr] = page; 1387708e3508SHugh Dickins } 1388708e3508SHugh Dickins 138971f0e07aSHugh Dickins isize = i_size_read(inode); 1390708e3508SHugh Dickins end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 1391708e3508SHugh Dickins if (unlikely(!isize || index > end_index)) 1392708e3508SHugh Dickins break; 1393708e3508SHugh Dickins 1394708e3508SHugh Dickins if (end_index == index) { 1395708e3508SHugh Dickins unsigned int plen; 1396708e3508SHugh Dickins 1397708e3508SHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 1398708e3508SHugh Dickins if (plen <= loff) 1399708e3508SHugh Dickins break; 1400708e3508SHugh Dickins 1401708e3508SHugh Dickins this_len = min(this_len, plen - loff); 1402708e3508SHugh Dickins len = this_len; 1403708e3508SHugh Dickins } 1404708e3508SHugh Dickins 1405708e3508SHugh Dickins spd.partial[page_nr].offset = loff; 1406708e3508SHugh Dickins spd.partial[page_nr].len = this_len; 1407708e3508SHugh Dickins len -= this_len; 1408708e3508SHugh Dickins loff = 0; 1409708e3508SHugh Dickins spd.nr_pages++; 1410708e3508SHugh Dickins index++; 1411708e3508SHugh Dickins } 1412708e3508SHugh Dickins 1413708e3508SHugh Dickins while (page_nr < nr_pages) 1414708e3508SHugh Dickins page_cache_release(spd.pages[page_nr++]); 1415708e3508SHugh Dickins 1416708e3508SHugh Dickins if (spd.nr_pages) 1417708e3508SHugh Dickins error = splice_to_pipe(pipe, &spd); 1418708e3508SHugh Dickins 1419708e3508SHugh Dickins splice_shrink_spd(pipe, &spd); 1420708e3508SHugh Dickins 1421708e3508SHugh Dickins if (error > 0) { 1422708e3508SHugh Dickins *ppos += error; 1423708e3508SHugh Dickins file_accessed(in); 1424708e3508SHugh Dickins } 1425708e3508SHugh Dickins return error; 1426708e3508SHugh Dickins } 1427708e3508SHugh Dickins 1428726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 14291da177e4SLinus Torvalds { 1430726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 14331da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 14341da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 14350edd73b3SHugh Dickins if (sbinfo->max_blocks) { 14361da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 143741ffe5d5SHugh Dickins buf->f_bavail = 143841ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 143941ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 14400edd73b3SHugh Dickins } 14410edd73b3SHugh Dickins if (sbinfo->max_inodes) { 14421da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 14431da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 14461da177e4SLinus Torvalds return 0; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds /* 14501da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 14511da177e4SLinus Torvalds */ 14521da177e4SLinus Torvalds static int 14531da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 14541da177e4SLinus Torvalds { 14550b0a0806SHugh Dickins struct inode *inode; 14561da177e4SLinus Torvalds int error = -ENOSPC; 14571da177e4SLinus Torvalds 1458454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 14591da177e4SLinus Torvalds if (inode) { 14602a7dba39SEric Paris error = security_inode_init_security(inode, dir, 14619d8f13baSMimi Zohar &dentry->d_name, 14622a7dba39SEric Paris NULL, NULL); 1463570bc1c2SStephen Smalley if (error) { 1464570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1465570bc1c2SStephen Smalley iput(inode); 1466570bc1c2SStephen Smalley return error; 1467570bc1c2SStephen Smalley } 146839f0247dSAndreas Gruenbacher } 14691c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL 14701c7c474cSChristoph Hellwig error = generic_acl_init(inode, dir); 147139f0247dSAndreas Gruenbacher if (error) { 147239f0247dSAndreas Gruenbacher iput(inode); 147339f0247dSAndreas Gruenbacher return error; 1474570bc1c2SStephen Smalley } 1475718deb6bSAl Viro #else 1476718deb6bSAl Viro error = 0; 14771c7c474cSChristoph Hellwig #endif 14781da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 14791da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 14801da177e4SLinus Torvalds d_instantiate(dentry, inode); 14811da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds return error; 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode) 14871da177e4SLinus Torvalds { 14881da177e4SLinus Torvalds int error; 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 14911da177e4SLinus Torvalds return error; 1492d8c76e6fSDave Hansen inc_nlink(dir); 14931da177e4SLinus Torvalds return 0; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode, 14971da177e4SLinus Torvalds struct nameidata *nd) 14981da177e4SLinus Torvalds { 14991da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds /* 15031da177e4SLinus Torvalds * Link a file.. 15041da177e4SLinus Torvalds */ 15051da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 15061da177e4SLinus Torvalds { 15071da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 15085b04c689SPavel Emelyanov int ret; 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds /* 15111da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 15121da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 15131da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 15141da177e4SLinus Torvalds */ 15155b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 15165b04c689SPavel Emelyanov if (ret) 15175b04c689SPavel Emelyanov goto out; 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 15201da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 1521d8c76e6fSDave Hansen inc_nlink(inode); 15227de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 15231da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 15241da177e4SLinus Torvalds d_instantiate(dentry, inode); 15255b04c689SPavel Emelyanov out: 15265b04c689SPavel Emelyanov return ret; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 15301da177e4SLinus Torvalds { 15311da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 15321da177e4SLinus Torvalds 15335b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 15345b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 15371da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 15389a53c3a7SDave Hansen drop_nlink(inode); 15391da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 15401da177e4SLinus Torvalds return 0; 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 15441da177e4SLinus Torvalds { 15451da177e4SLinus Torvalds if (!simple_empty(dentry)) 15461da177e4SLinus Torvalds return -ENOTEMPTY; 15471da177e4SLinus Torvalds 15489a53c3a7SDave Hansen drop_nlink(dentry->d_inode); 15499a53c3a7SDave Hansen drop_nlink(dir); 15501da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds /* 15541da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 15551da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 15561da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 15571da177e4SLinus Torvalds * gets overwritten. 15581da177e4SLinus Torvalds */ 15591da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 15601da177e4SLinus Torvalds { 15611da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 15621da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 15651da177e4SLinus Torvalds return -ENOTEMPTY; 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds if (new_dentry->d_inode) { 15681da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 15691da177e4SLinus Torvalds if (they_are_dirs) 15709a53c3a7SDave Hansen drop_nlink(old_dir); 15711da177e4SLinus Torvalds } else if (they_are_dirs) { 15729a53c3a7SDave Hansen drop_nlink(old_dir); 1573d8c76e6fSDave Hansen inc_nlink(new_dir); 15741da177e4SLinus Torvalds } 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 15771da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 15781da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 15791da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 15801da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 15811da177e4SLinus Torvalds return 0; 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 15851da177e4SLinus Torvalds { 15861da177e4SLinus Torvalds int error; 15871da177e4SLinus Torvalds int len; 15881da177e4SLinus Torvalds struct inode *inode; 15899276aad6SHugh Dickins struct page *page; 15901da177e4SLinus Torvalds char *kaddr; 15911da177e4SLinus Torvalds struct shmem_inode_info *info; 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds len = strlen(symname) + 1; 15941da177e4SLinus Torvalds if (len > PAGE_CACHE_SIZE) 15951da177e4SLinus Torvalds return -ENAMETOOLONG; 15961da177e4SLinus Torvalds 1597454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 15981da177e4SLinus Torvalds if (!inode) 15991da177e4SLinus Torvalds return -ENOSPC; 16001da177e4SLinus Torvalds 16019d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 16022a7dba39SEric Paris NULL, NULL); 1603570bc1c2SStephen Smalley if (error) { 1604570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1605570bc1c2SStephen Smalley iput(inode); 1606570bc1c2SStephen Smalley return error; 1607570bc1c2SStephen Smalley } 1608570bc1c2SStephen Smalley error = 0; 1609570bc1c2SStephen Smalley } 1610570bc1c2SStephen Smalley 16111da177e4SLinus Torvalds info = SHMEM_I(inode); 16121da177e4SLinus Torvalds inode->i_size = len-1; 161369f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 161469f07ec9SHugh Dickins info->symlink = kmemdup(symname, len, GFP_KERNEL); 161569f07ec9SHugh Dickins if (!info->symlink) { 161669f07ec9SHugh Dickins iput(inode); 161769f07ec9SHugh Dickins return -ENOMEM; 161869f07ec9SHugh Dickins } 161969f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 16201da177e4SLinus Torvalds } else { 16211da177e4SLinus Torvalds error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); 16221da177e4SLinus Torvalds if (error) { 16231da177e4SLinus Torvalds iput(inode); 16241da177e4SLinus Torvalds return error; 16251da177e4SLinus Torvalds } 162614fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 16271da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 16281da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 16291da177e4SLinus Torvalds memcpy(kaddr, symname, len); 16301da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 16311da177e4SLinus Torvalds set_page_dirty(page); 16326746aff7SWu Fengguang unlock_page(page); 16331da177e4SLinus Torvalds page_cache_release(page); 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 16361da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 16371da177e4SLinus Torvalds d_instantiate(dentry, inode); 16381da177e4SLinus Torvalds dget(dentry); 16391da177e4SLinus Torvalds return 0; 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds 164269f07ec9SHugh Dickins static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd) 16431da177e4SLinus Torvalds { 164469f07ec9SHugh Dickins nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink); 1645cc314eefSLinus Torvalds return NULL; 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 1648cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) 16491da177e4SLinus Torvalds { 16501da177e4SLinus Torvalds struct page *page = NULL; 165141ffe5d5SHugh Dickins int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); 165241ffe5d5SHugh Dickins nd_set_link(nd, error ? ERR_PTR(error) : kmap(page)); 1653d3602444SHugh Dickins if (page) 1654d3602444SHugh Dickins unlock_page(page); 1655cc314eefSLinus Torvalds return page; 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 1658cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 16591da177e4SLinus Torvalds { 16601da177e4SLinus Torvalds if (!IS_ERR(nd_get_link(nd))) { 1661cc314eefSLinus Torvalds struct page *page = cookie; 16621da177e4SLinus Torvalds kunmap(page); 16631da177e4SLinus Torvalds mark_page_accessed(page); 16641da177e4SLinus Torvalds page_cache_release(page); 16651da177e4SLinus Torvalds } 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 1668b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 1669b09e0fa4SEric Paris /* 1670b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 1671b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 1672b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 1673b09e0fa4SEric Paris * filesystem level, though. 1674b09e0fa4SEric Paris */ 1675b09e0fa4SEric Paris 1676b09e0fa4SEric Paris static int shmem_xattr_get(struct dentry *dentry, const char *name, 1677b09e0fa4SEric Paris void *buffer, size_t size) 1678b09e0fa4SEric Paris { 1679b09e0fa4SEric Paris struct shmem_inode_info *info; 1680b09e0fa4SEric Paris struct shmem_xattr *xattr; 1681b09e0fa4SEric Paris int ret = -ENODATA; 1682b09e0fa4SEric Paris 1683b09e0fa4SEric Paris info = SHMEM_I(dentry->d_inode); 1684b09e0fa4SEric Paris 1685b09e0fa4SEric Paris spin_lock(&info->lock); 1686b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 1687b09e0fa4SEric Paris if (strcmp(name, xattr->name)) 1688b09e0fa4SEric Paris continue; 1689b09e0fa4SEric Paris 1690b09e0fa4SEric Paris ret = xattr->size; 1691b09e0fa4SEric Paris if (buffer) { 1692b09e0fa4SEric Paris if (size < xattr->size) 1693b09e0fa4SEric Paris ret = -ERANGE; 1694b09e0fa4SEric Paris else 1695b09e0fa4SEric Paris memcpy(buffer, xattr->value, xattr->size); 1696b09e0fa4SEric Paris } 1697b09e0fa4SEric Paris break; 1698b09e0fa4SEric Paris } 1699b09e0fa4SEric Paris spin_unlock(&info->lock); 1700b09e0fa4SEric Paris return ret; 1701b09e0fa4SEric Paris } 1702b09e0fa4SEric Paris 1703b09e0fa4SEric Paris static int shmem_xattr_set(struct dentry *dentry, const char *name, 1704b09e0fa4SEric Paris const void *value, size_t size, int flags) 1705b09e0fa4SEric Paris { 1706b09e0fa4SEric Paris struct inode *inode = dentry->d_inode; 1707b09e0fa4SEric Paris struct shmem_inode_info *info = SHMEM_I(inode); 1708b09e0fa4SEric Paris struct shmem_xattr *xattr; 1709b09e0fa4SEric Paris struct shmem_xattr *new_xattr = NULL; 1710b09e0fa4SEric Paris size_t len; 1711b09e0fa4SEric Paris int err = 0; 1712b09e0fa4SEric Paris 1713b09e0fa4SEric Paris /* value == NULL means remove */ 1714b09e0fa4SEric Paris if (value) { 1715b09e0fa4SEric Paris /* wrap around? */ 1716b09e0fa4SEric Paris len = sizeof(*new_xattr) + size; 1717b09e0fa4SEric Paris if (len <= sizeof(*new_xattr)) 1718b09e0fa4SEric Paris return -ENOMEM; 1719b09e0fa4SEric Paris 1720b09e0fa4SEric Paris new_xattr = kmalloc(len, GFP_KERNEL); 1721b09e0fa4SEric Paris if (!new_xattr) 1722b09e0fa4SEric Paris return -ENOMEM; 1723b09e0fa4SEric Paris 1724b09e0fa4SEric Paris new_xattr->name = kstrdup(name, GFP_KERNEL); 1725b09e0fa4SEric Paris if (!new_xattr->name) { 1726b09e0fa4SEric Paris kfree(new_xattr); 1727b09e0fa4SEric Paris return -ENOMEM; 1728b09e0fa4SEric Paris } 1729b09e0fa4SEric Paris 1730b09e0fa4SEric Paris new_xattr->size = size; 1731b09e0fa4SEric Paris memcpy(new_xattr->value, value, size); 1732b09e0fa4SEric Paris } 1733b09e0fa4SEric Paris 1734b09e0fa4SEric Paris spin_lock(&info->lock); 1735b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 1736b09e0fa4SEric Paris if (!strcmp(name, xattr->name)) { 1737b09e0fa4SEric Paris if (flags & XATTR_CREATE) { 1738b09e0fa4SEric Paris xattr = new_xattr; 1739b09e0fa4SEric Paris err = -EEXIST; 1740b09e0fa4SEric Paris } else if (new_xattr) { 1741b09e0fa4SEric Paris list_replace(&xattr->list, &new_xattr->list); 1742b09e0fa4SEric Paris } else { 1743b09e0fa4SEric Paris list_del(&xattr->list); 1744b09e0fa4SEric Paris } 1745b09e0fa4SEric Paris goto out; 1746b09e0fa4SEric Paris } 1747b09e0fa4SEric Paris } 1748b09e0fa4SEric Paris if (flags & XATTR_REPLACE) { 1749b09e0fa4SEric Paris xattr = new_xattr; 1750b09e0fa4SEric Paris err = -ENODATA; 1751b09e0fa4SEric Paris } else { 1752b09e0fa4SEric Paris list_add(&new_xattr->list, &info->xattr_list); 1753b09e0fa4SEric Paris xattr = NULL; 1754b09e0fa4SEric Paris } 1755b09e0fa4SEric Paris out: 1756b09e0fa4SEric Paris spin_unlock(&info->lock); 1757b09e0fa4SEric Paris if (xattr) 1758b09e0fa4SEric Paris kfree(xattr->name); 1759b09e0fa4SEric Paris kfree(xattr); 1760b09e0fa4SEric Paris return err; 1761b09e0fa4SEric Paris } 1762b09e0fa4SEric Paris 1763b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 1764b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 1765b09e0fa4SEric Paris &generic_acl_access_handler, 1766b09e0fa4SEric Paris &generic_acl_default_handler, 1767b09e0fa4SEric Paris #endif 1768b09e0fa4SEric Paris NULL 1769b09e0fa4SEric Paris }; 1770b09e0fa4SEric Paris 1771b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name) 1772b09e0fa4SEric Paris { 1773b09e0fa4SEric Paris struct { const char *prefix; size_t len; } arr[] = { 1774b09e0fa4SEric Paris { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, 1775b09e0fa4SEric Paris { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } 1776b09e0fa4SEric Paris }; 1777b09e0fa4SEric Paris int i; 1778b09e0fa4SEric Paris 1779b09e0fa4SEric Paris for (i = 0; i < ARRAY_SIZE(arr); i++) { 1780b09e0fa4SEric Paris size_t preflen = arr[i].len; 1781b09e0fa4SEric Paris if (strncmp(name, arr[i].prefix, preflen) == 0) { 1782b09e0fa4SEric Paris if (!name[preflen]) 1783b09e0fa4SEric Paris return -EINVAL; 1784b09e0fa4SEric Paris return 0; 1785b09e0fa4SEric Paris } 1786b09e0fa4SEric Paris } 1787b09e0fa4SEric Paris return -EOPNOTSUPP; 1788b09e0fa4SEric Paris } 1789b09e0fa4SEric Paris 1790b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, 1791b09e0fa4SEric Paris void *buffer, size_t size) 1792b09e0fa4SEric Paris { 1793b09e0fa4SEric Paris int err; 1794b09e0fa4SEric Paris 1795b09e0fa4SEric Paris /* 1796b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 1797b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 1798b09e0fa4SEric Paris * for it via sb->s_xattr. 1799b09e0fa4SEric Paris */ 1800b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 1801b09e0fa4SEric Paris return generic_getxattr(dentry, name, buffer, size); 1802b09e0fa4SEric Paris 1803b09e0fa4SEric Paris err = shmem_xattr_validate(name); 1804b09e0fa4SEric Paris if (err) 1805b09e0fa4SEric Paris return err; 1806b09e0fa4SEric Paris 1807b09e0fa4SEric Paris return shmem_xattr_get(dentry, name, buffer, size); 1808b09e0fa4SEric Paris } 1809b09e0fa4SEric Paris 1810b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name, 1811b09e0fa4SEric Paris const void *value, size_t size, int flags) 1812b09e0fa4SEric Paris { 1813b09e0fa4SEric Paris int err; 1814b09e0fa4SEric Paris 1815b09e0fa4SEric Paris /* 1816b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 1817b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 1818b09e0fa4SEric Paris * for it via sb->s_xattr. 1819b09e0fa4SEric Paris */ 1820b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 1821b09e0fa4SEric Paris return generic_setxattr(dentry, name, value, size, flags); 1822b09e0fa4SEric Paris 1823b09e0fa4SEric Paris err = shmem_xattr_validate(name); 1824b09e0fa4SEric Paris if (err) 1825b09e0fa4SEric Paris return err; 1826b09e0fa4SEric Paris 1827b09e0fa4SEric Paris if (size == 0) 1828b09e0fa4SEric Paris value = ""; /* empty EA, do not remove */ 1829b09e0fa4SEric Paris 1830b09e0fa4SEric Paris return shmem_xattr_set(dentry, name, value, size, flags); 1831b09e0fa4SEric Paris 1832b09e0fa4SEric Paris } 1833b09e0fa4SEric Paris 1834b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name) 1835b09e0fa4SEric Paris { 1836b09e0fa4SEric Paris int err; 1837b09e0fa4SEric Paris 1838b09e0fa4SEric Paris /* 1839b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 1840b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 1841b09e0fa4SEric Paris * for it via sb->s_xattr. 1842b09e0fa4SEric Paris */ 1843b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 1844b09e0fa4SEric Paris return generic_removexattr(dentry, name); 1845b09e0fa4SEric Paris 1846b09e0fa4SEric Paris err = shmem_xattr_validate(name); 1847b09e0fa4SEric Paris if (err) 1848b09e0fa4SEric Paris return err; 1849b09e0fa4SEric Paris 1850b09e0fa4SEric Paris return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE); 1851b09e0fa4SEric Paris } 1852b09e0fa4SEric Paris 1853b09e0fa4SEric Paris static bool xattr_is_trusted(const char *name) 1854b09e0fa4SEric Paris { 1855b09e0fa4SEric Paris return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN); 1856b09e0fa4SEric Paris } 1857b09e0fa4SEric Paris 1858b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 1859b09e0fa4SEric Paris { 1860b09e0fa4SEric Paris bool trusted = capable(CAP_SYS_ADMIN); 1861b09e0fa4SEric Paris struct shmem_xattr *xattr; 1862b09e0fa4SEric Paris struct shmem_inode_info *info; 1863b09e0fa4SEric Paris size_t used = 0; 1864b09e0fa4SEric Paris 1865b09e0fa4SEric Paris info = SHMEM_I(dentry->d_inode); 1866b09e0fa4SEric Paris 1867b09e0fa4SEric Paris spin_lock(&info->lock); 1868b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 1869b09e0fa4SEric Paris size_t len; 1870b09e0fa4SEric Paris 1871b09e0fa4SEric Paris /* skip "trusted." attributes for unprivileged callers */ 1872b09e0fa4SEric Paris if (!trusted && xattr_is_trusted(xattr->name)) 1873b09e0fa4SEric Paris continue; 1874b09e0fa4SEric Paris 1875b09e0fa4SEric Paris len = strlen(xattr->name) + 1; 1876b09e0fa4SEric Paris used += len; 1877b09e0fa4SEric Paris if (buffer) { 1878b09e0fa4SEric Paris if (size < used) { 1879b09e0fa4SEric Paris used = -ERANGE; 1880b09e0fa4SEric Paris break; 1881b09e0fa4SEric Paris } 1882b09e0fa4SEric Paris memcpy(buffer, xattr->name, len); 1883b09e0fa4SEric Paris buffer += len; 1884b09e0fa4SEric Paris } 1885b09e0fa4SEric Paris } 1886b09e0fa4SEric Paris spin_unlock(&info->lock); 1887b09e0fa4SEric Paris 1888b09e0fa4SEric Paris return used; 1889b09e0fa4SEric Paris } 1890b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 1891b09e0fa4SEric Paris 189269f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 18931da177e4SLinus Torvalds .readlink = generic_readlink, 189469f07ec9SHugh Dickins .follow_link = shmem_follow_short_symlink, 1895b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 1896b09e0fa4SEric Paris .setxattr = shmem_setxattr, 1897b09e0fa4SEric Paris .getxattr = shmem_getxattr, 1898b09e0fa4SEric Paris .listxattr = shmem_listxattr, 1899b09e0fa4SEric Paris .removexattr = shmem_removexattr, 1900b09e0fa4SEric Paris #endif 19011da177e4SLinus Torvalds }; 19021da177e4SLinus Torvalds 190392e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 19041da177e4SLinus Torvalds .readlink = generic_readlink, 19051da177e4SLinus Torvalds .follow_link = shmem_follow_link, 19061da177e4SLinus Torvalds .put_link = shmem_put_link, 1907b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 1908b09e0fa4SEric Paris .setxattr = shmem_setxattr, 1909b09e0fa4SEric Paris .getxattr = shmem_getxattr, 1910b09e0fa4SEric Paris .listxattr = shmem_listxattr, 1911b09e0fa4SEric Paris .removexattr = shmem_removexattr, 191239f0247dSAndreas Gruenbacher #endif 1913b09e0fa4SEric Paris }; 191439f0247dSAndreas Gruenbacher 191591828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 191691828a40SDavid M. Grimes { 191791828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 191891828a40SDavid M. Grimes } 191991828a40SDavid M. Grimes 192091828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 192191828a40SDavid M. Grimes { 192291828a40SDavid M. Grimes __u32 *fh = vfh; 192391828a40SDavid M. Grimes __u64 inum = fh[2]; 192491828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 192591828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 192691828a40SDavid M. Grimes } 192791828a40SDavid M. Grimes 1928480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 1929480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 193091828a40SDavid M. Grimes { 193191828a40SDavid M. Grimes struct inode *inode; 1932480b116cSChristoph Hellwig struct dentry *dentry = NULL; 1933480b116cSChristoph Hellwig u64 inum = fid->raw[2]; 1934480b116cSChristoph Hellwig inum = (inum << 32) | fid->raw[1]; 193591828a40SDavid M. Grimes 1936480b116cSChristoph Hellwig if (fh_len < 3) 1937480b116cSChristoph Hellwig return NULL; 1938480b116cSChristoph Hellwig 1939480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 1940480b116cSChristoph Hellwig shmem_match, fid->raw); 194191828a40SDavid M. Grimes if (inode) { 1942480b116cSChristoph Hellwig dentry = d_find_alias(inode); 194391828a40SDavid M. Grimes iput(inode); 194491828a40SDavid M. Grimes } 194591828a40SDavid M. Grimes 1946480b116cSChristoph Hellwig return dentry; 194791828a40SDavid M. Grimes } 194891828a40SDavid M. Grimes 194991828a40SDavid M. Grimes static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len, 195091828a40SDavid M. Grimes int connectable) 195191828a40SDavid M. Grimes { 195291828a40SDavid M. Grimes struct inode *inode = dentry->d_inode; 195391828a40SDavid M. Grimes 19545fe0c237SAneesh Kumar K.V if (*len < 3) { 19555fe0c237SAneesh Kumar K.V *len = 3; 195691828a40SDavid M. Grimes return 255; 19575fe0c237SAneesh Kumar K.V } 195891828a40SDavid M. Grimes 19591d3382cbSAl Viro if (inode_unhashed(inode)) { 196091828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 196191828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 196291828a40SDavid M. Grimes * time, we need a lock to ensure we only try 196391828a40SDavid M. Grimes * to do it once 196491828a40SDavid M. Grimes */ 196591828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 196691828a40SDavid M. Grimes spin_lock(&lock); 19671d3382cbSAl Viro if (inode_unhashed(inode)) 196891828a40SDavid M. Grimes __insert_inode_hash(inode, 196991828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 197091828a40SDavid M. Grimes spin_unlock(&lock); 197191828a40SDavid M. Grimes } 197291828a40SDavid M. Grimes 197391828a40SDavid M. Grimes fh[0] = inode->i_generation; 197491828a40SDavid M. Grimes fh[1] = inode->i_ino; 197591828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 197691828a40SDavid M. Grimes 197791828a40SDavid M. Grimes *len = 3; 197891828a40SDavid M. Grimes return 1; 197991828a40SDavid M. Grimes } 198091828a40SDavid M. Grimes 198139655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 198291828a40SDavid M. Grimes .get_parent = shmem_get_parent, 198391828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 1984480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 198591828a40SDavid M. Grimes }; 198691828a40SDavid M. Grimes 1987680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 1988680d794bSakpm@linux-foundation.org bool remount) 19891da177e4SLinus Torvalds { 19901da177e4SLinus Torvalds char *this_char, *value, *rest; 19911da177e4SLinus Torvalds 1992b00dc3adSHugh Dickins while (options != NULL) { 1993b00dc3adSHugh Dickins this_char = options; 1994b00dc3adSHugh Dickins for (;;) { 1995b00dc3adSHugh Dickins /* 1996b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 1997b00dc3adSHugh Dickins * mount options form a comma-separated list, 1998b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 1999b00dc3adSHugh Dickins */ 2000b00dc3adSHugh Dickins options = strchr(options, ','); 2001b00dc3adSHugh Dickins if (options == NULL) 2002b00dc3adSHugh Dickins break; 2003b00dc3adSHugh Dickins options++; 2004b00dc3adSHugh Dickins if (!isdigit(*options)) { 2005b00dc3adSHugh Dickins options[-1] = '\0'; 2006b00dc3adSHugh Dickins break; 2007b00dc3adSHugh Dickins } 2008b00dc3adSHugh Dickins } 20091da177e4SLinus Torvalds if (!*this_char) 20101da177e4SLinus Torvalds continue; 20111da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 20121da177e4SLinus Torvalds *value++ = 0; 20131da177e4SLinus Torvalds } else { 20141da177e4SLinus Torvalds printk(KERN_ERR 20151da177e4SLinus Torvalds "tmpfs: No value for mount option '%s'\n", 20161da177e4SLinus Torvalds this_char); 20171da177e4SLinus Torvalds return 1; 20181da177e4SLinus Torvalds } 20191da177e4SLinus Torvalds 20201da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 20211da177e4SLinus Torvalds unsigned long long size; 20221da177e4SLinus Torvalds size = memparse(value,&rest); 20231da177e4SLinus Torvalds if (*rest == '%') { 20241da177e4SLinus Torvalds size <<= PAGE_SHIFT; 20251da177e4SLinus Torvalds size *= totalram_pages; 20261da177e4SLinus Torvalds do_div(size, 100); 20271da177e4SLinus Torvalds rest++; 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds if (*rest) 20301da177e4SLinus Torvalds goto bad_val; 2031680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 2032680d794bSakpm@linux-foundation.org DIV_ROUND_UP(size, PAGE_CACHE_SIZE); 20331da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 2034680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 20351da177e4SLinus Torvalds if (*rest) 20361da177e4SLinus Torvalds goto bad_val; 20371da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 2038680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 20391da177e4SLinus Torvalds if (*rest) 20401da177e4SLinus Torvalds goto bad_val; 20411da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 2042680d794bSakpm@linux-foundation.org if (remount) 20431da177e4SLinus Torvalds continue; 2044680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 20451da177e4SLinus Torvalds if (*rest) 20461da177e4SLinus Torvalds goto bad_val; 20471da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 2048680d794bSakpm@linux-foundation.org if (remount) 20491da177e4SLinus Torvalds continue; 2050680d794bSakpm@linux-foundation.org sbinfo->uid = simple_strtoul(value, &rest, 0); 20511da177e4SLinus Torvalds if (*rest) 20521da177e4SLinus Torvalds goto bad_val; 20531da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 2054680d794bSakpm@linux-foundation.org if (remount) 20551da177e4SLinus Torvalds continue; 2056680d794bSakpm@linux-foundation.org sbinfo->gid = simple_strtoul(value, &rest, 0); 20571da177e4SLinus Torvalds if (*rest) 20581da177e4SLinus Torvalds goto bad_val; 20597339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 206071fe804bSLee Schermerhorn if (mpol_parse_str(value, &sbinfo->mpol, 1)) 20617339ff83SRobin Holt goto bad_val; 20621da177e4SLinus Torvalds } else { 20631da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad mount option %s\n", 20641da177e4SLinus Torvalds this_char); 20651da177e4SLinus Torvalds return 1; 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds return 0; 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds bad_val: 20711da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", 20721da177e4SLinus Torvalds value, this_char); 20731da177e4SLinus Torvalds return 1; 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 20781da177e4SLinus Torvalds { 20791da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2080680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 20810edd73b3SHugh Dickins unsigned long inodes; 20820edd73b3SHugh Dickins int error = -EINVAL; 20831da177e4SLinus Torvalds 2084680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 20850edd73b3SHugh Dickins return error; 20860edd73b3SHugh Dickins 20870edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 20880edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 20897e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 20900edd73b3SHugh Dickins goto out; 2091680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 20920edd73b3SHugh Dickins goto out; 20930edd73b3SHugh Dickins /* 209454af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 20950edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 20960edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 20970edd73b3SHugh Dickins */ 2098680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 20990edd73b3SHugh Dickins goto out; 2100680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 21010edd73b3SHugh Dickins goto out; 21020edd73b3SHugh Dickins 21030edd73b3SHugh Dickins error = 0; 2104680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 2105680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 2106680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 210771fe804bSLee Schermerhorn 210871fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 210971fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 21100edd73b3SHugh Dickins out: 21110edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 21120edd73b3SHugh Dickins return error; 21131da177e4SLinus Torvalds } 2114680d794bSakpm@linux-foundation.org 2115680d794bSakpm@linux-foundation.org static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs) 2116680d794bSakpm@linux-foundation.org { 2117680d794bSakpm@linux-foundation.org struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb); 2118680d794bSakpm@linux-foundation.org 2119680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 2120680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 2121680d794bSakpm@linux-foundation.org sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); 2122680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 2123680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 2124680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 2125680d794bSakpm@linux-foundation.org seq_printf(seq, ",mode=%03o", sbinfo->mode); 2126680d794bSakpm@linux-foundation.org if (sbinfo->uid != 0) 2127680d794bSakpm@linux-foundation.org seq_printf(seq, ",uid=%u", sbinfo->uid); 2128680d794bSakpm@linux-foundation.org if (sbinfo->gid != 0) 2129680d794bSakpm@linux-foundation.org seq_printf(seq, ",gid=%u", sbinfo->gid); 213071fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 2131680d794bSakpm@linux-foundation.org return 0; 2132680d794bSakpm@linux-foundation.org } 2133680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 21361da177e4SLinus Torvalds { 2137602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2138602586a8SHugh Dickins 2139602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 2140602586a8SHugh Dickins kfree(sbinfo); 21411da177e4SLinus Torvalds sb->s_fs_info = NULL; 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 21442b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 21451da177e4SLinus Torvalds { 21461da177e4SLinus Torvalds struct inode *inode; 21471da177e4SLinus Torvalds struct dentry *root; 21480edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 2149680d794bSakpm@linux-foundation.org int err = -ENOMEM; 2150680d794bSakpm@linux-foundation.org 2151680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 2152425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 2153680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 2154680d794bSakpm@linux-foundation.org if (!sbinfo) 2155680d794bSakpm@linux-foundation.org return -ENOMEM; 2156680d794bSakpm@linux-foundation.org 2157680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 215876aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 215976aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 2160680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 21611da177e4SLinus Torvalds 21620edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 21631da177e4SLinus Torvalds /* 21641da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 21651da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 21661da177e4SLinus Torvalds * but the internal instance is left unlimited. 21671da177e4SLinus Torvalds */ 21681da177e4SLinus Torvalds if (!(sb->s_flags & MS_NOUSER)) { 2169680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 2170680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 2171680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 2172680d794bSakpm@linux-foundation.org err = -EINVAL; 2173680d794bSakpm@linux-foundation.org goto failed; 2174680d794bSakpm@linux-foundation.org } 21751da177e4SLinus Torvalds } 217691828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 21770edd73b3SHugh Dickins #else 21780edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 21790edd73b3SHugh Dickins #endif 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 2182602586a8SHugh Dickins if (percpu_counter_init(&sbinfo->used_blocks, 0)) 2183602586a8SHugh Dickins goto failed; 2184680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 21851da177e4SLinus Torvalds 2186285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 21871da177e4SLinus Torvalds sb->s_blocksize = PAGE_CACHE_SIZE; 21881da177e4SLinus Torvalds sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 21891da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 21901da177e4SLinus Torvalds sb->s_op = &shmem_ops; 2191cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 2192b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 219339f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 2194b09e0fa4SEric Paris #endif 2195b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 219639f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 219739f0247dSAndreas Gruenbacher #endif 21980edd73b3SHugh Dickins 2199454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 22001da177e4SLinus Torvalds if (!inode) 22011da177e4SLinus Torvalds goto failed; 2202680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 2203680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 22041da177e4SLinus Torvalds root = d_alloc_root(inode); 22051da177e4SLinus Torvalds if (!root) 22061da177e4SLinus Torvalds goto failed_iput; 22071da177e4SLinus Torvalds sb->s_root = root; 22081da177e4SLinus Torvalds return 0; 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds failed_iput: 22111da177e4SLinus Torvalds iput(inode); 22121da177e4SLinus Torvalds failed: 22131da177e4SLinus Torvalds shmem_put_super(sb); 22141da177e4SLinus Torvalds return err; 22151da177e4SLinus Torvalds } 22161da177e4SLinus Torvalds 2217fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 22201da177e4SLinus Torvalds { 222141ffe5d5SHugh Dickins struct shmem_inode_info *info; 222241ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 222341ffe5d5SHugh Dickins if (!info) 22241da177e4SLinus Torvalds return NULL; 222541ffe5d5SHugh Dickins return &info->vfs_inode; 22261da177e4SLinus Torvalds } 22271da177e4SLinus Torvalds 222841ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 2229fa0d7e3dSNick Piggin { 2230fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 2231fa0d7e3dSNick Piggin INIT_LIST_HEAD(&inode->i_dentry); 2232fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 2233fa0d7e3dSNick Piggin } 2234fa0d7e3dSNick Piggin 22351da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 22361da177e4SLinus Torvalds { 223769f07ec9SHugh Dickins if ((inode->i_mode & S_IFMT) == S_IFREG) 22381da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 223941ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 224241ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 22431da177e4SLinus Torvalds { 224441ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 224541ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 224841ffe5d5SHugh Dickins static int shmem_init_inodecache(void) 22491da177e4SLinus Torvalds { 22501da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 22511da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 225241ffe5d5SHugh Dickins 0, SLAB_PANIC, shmem_init_inode); 22531da177e4SLinus Torvalds return 0; 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds 225641ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 22571da177e4SLinus Torvalds { 22581a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 2261f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 22621da177e4SLinus Torvalds .writepage = shmem_writepage, 226376719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 22641da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2265800d15a5SNick Piggin .write_begin = shmem_write_begin, 2266800d15a5SNick Piggin .write_end = shmem_write_end, 22671da177e4SLinus Torvalds #endif 2268304dbdb7SLee Schermerhorn .migratepage = migrate_page, 2269aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 22701da177e4SLinus Torvalds }; 22711da177e4SLinus Torvalds 227215ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 22731da177e4SLinus Torvalds .mmap = shmem_mmap, 22741da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 22751da177e4SLinus Torvalds .llseek = generic_file_llseek, 2276bcd78e49SHugh Dickins .read = do_sync_read, 22775402b976SHugh Dickins .write = do_sync_write, 2278bcd78e49SHugh Dickins .aio_read = shmem_file_aio_read, 22795402b976SHugh Dickins .aio_write = generic_file_aio_write, 22801b061d92SChristoph Hellwig .fsync = noop_fsync, 2281708e3508SHugh Dickins .splice_read = shmem_file_splice_read, 2282ae976416SHugh Dickins .splice_write = generic_file_splice_write, 22831da177e4SLinus Torvalds #endif 22841da177e4SLinus Torvalds }; 22851da177e4SLinus Torvalds 228692e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 228794c1e62dSHugh Dickins .setattr = shmem_setattr, 2288f6b3ec23SBadari Pulavarty .truncate_range = shmem_truncate_range, 2289b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2290b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2291b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2292b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2293b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2294b09e0fa4SEric Paris #endif 22951da177e4SLinus Torvalds }; 22961da177e4SLinus Torvalds 229792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 22981da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 22991da177e4SLinus Torvalds .create = shmem_create, 23001da177e4SLinus Torvalds .lookup = simple_lookup, 23011da177e4SLinus Torvalds .link = shmem_link, 23021da177e4SLinus Torvalds .unlink = shmem_unlink, 23031da177e4SLinus Torvalds .symlink = shmem_symlink, 23041da177e4SLinus Torvalds .mkdir = shmem_mkdir, 23051da177e4SLinus Torvalds .rmdir = shmem_rmdir, 23061da177e4SLinus Torvalds .mknod = shmem_mknod, 23071da177e4SLinus Torvalds .rename = shmem_rename, 23081da177e4SLinus Torvalds #endif 2309b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2310b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2311b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2312b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2313b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2314b09e0fa4SEric Paris #endif 231539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 231694c1e62dSHugh Dickins .setattr = shmem_setattr, 231739f0247dSAndreas Gruenbacher #endif 231839f0247dSAndreas Gruenbacher }; 231939f0247dSAndreas Gruenbacher 232092e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 2321b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2322b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2323b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2324b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2325b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2326b09e0fa4SEric Paris #endif 232739f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 232894c1e62dSHugh Dickins .setattr = shmem_setattr, 232939f0247dSAndreas Gruenbacher #endif 23301da177e4SLinus Torvalds }; 23311da177e4SLinus Torvalds 2332759b9775SHugh Dickins static const struct super_operations shmem_ops = { 23331da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 23341da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 23351da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 23361da177e4SLinus Torvalds .statfs = shmem_statfs, 23371da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 2338680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 23391da177e4SLinus Torvalds #endif 23401f895f75SAl Viro .evict_inode = shmem_evict_inode, 23411da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 23421da177e4SLinus Torvalds .put_super = shmem_put_super, 23431da177e4SLinus Torvalds }; 23441da177e4SLinus Torvalds 2345f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 234654cb8821SNick Piggin .fault = shmem_fault, 23471da177e4SLinus Torvalds #ifdef CONFIG_NUMA 23481da177e4SLinus Torvalds .set_policy = shmem_set_policy, 23491da177e4SLinus Torvalds .get_policy = shmem_get_policy, 23501da177e4SLinus Torvalds #endif 23511da177e4SLinus Torvalds }; 23521da177e4SLinus Torvalds 23533c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 23543c26ff6eSAl Viro int flags, const char *dev_name, void *data) 23551da177e4SLinus Torvalds { 23563c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 23571da177e4SLinus Torvalds } 23581da177e4SLinus Torvalds 235941ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 23601da177e4SLinus Torvalds .owner = THIS_MODULE, 23611da177e4SLinus Torvalds .name = "tmpfs", 23623c26ff6eSAl Viro .mount = shmem_mount, 23631da177e4SLinus Torvalds .kill_sb = kill_litter_super, 23641da177e4SLinus Torvalds }; 23651da177e4SLinus Torvalds 236641ffe5d5SHugh Dickins int __init shmem_init(void) 23671da177e4SLinus Torvalds { 23681da177e4SLinus Torvalds int error; 23691da177e4SLinus Torvalds 2370e0bf68ddSPeter Zijlstra error = bdi_init(&shmem_backing_dev_info); 2371e0bf68ddSPeter Zijlstra if (error) 2372e0bf68ddSPeter Zijlstra goto out4; 2373e0bf68ddSPeter Zijlstra 237441ffe5d5SHugh Dickins error = shmem_init_inodecache(); 23751da177e4SLinus Torvalds if (error) 23761da177e4SLinus Torvalds goto out3; 23771da177e4SLinus Torvalds 237841ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 23791da177e4SLinus Torvalds if (error) { 23801da177e4SLinus Torvalds printk(KERN_ERR "Could not register tmpfs\n"); 23811da177e4SLinus Torvalds goto out2; 23821da177e4SLinus Torvalds } 238395dc112aSGreg Kroah-Hartman 238441ffe5d5SHugh Dickins shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER, 238541ffe5d5SHugh Dickins shmem_fs_type.name, NULL); 23861da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 23871da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 23881da177e4SLinus Torvalds printk(KERN_ERR "Could not kern_mount tmpfs\n"); 23891da177e4SLinus Torvalds goto out1; 23901da177e4SLinus Torvalds } 23911da177e4SLinus Torvalds return 0; 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds out1: 239441ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 23951da177e4SLinus Torvalds out2: 239641ffe5d5SHugh Dickins shmem_destroy_inodecache(); 23971da177e4SLinus Torvalds out3: 2398e0bf68ddSPeter Zijlstra bdi_destroy(&shmem_backing_dev_info); 2399e0bf68ddSPeter Zijlstra out4: 24001da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 24011da177e4SLinus Torvalds return error; 24021da177e4SLinus Torvalds } 2403853ac43aSMatt Mackall 2404853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 2405853ac43aSMatt Mackall 2406853ac43aSMatt Mackall /* 2407853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 2408853ac43aSMatt Mackall * 2409853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 2410853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 2411853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 2412853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 2413853ac43aSMatt Mackall */ 2414853ac43aSMatt Mackall 2415853ac43aSMatt Mackall #include <linux/ramfs.h> 2416853ac43aSMatt Mackall 241741ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 2418853ac43aSMatt Mackall .name = "tmpfs", 24193c26ff6eSAl Viro .mount = ramfs_mount, 2420853ac43aSMatt Mackall .kill_sb = kill_litter_super, 2421853ac43aSMatt Mackall }; 2422853ac43aSMatt Mackall 242341ffe5d5SHugh Dickins int __init shmem_init(void) 2424853ac43aSMatt Mackall { 242541ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 2426853ac43aSMatt Mackall 242741ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 2428853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 2429853ac43aSMatt Mackall 2430853ac43aSMatt Mackall return 0; 2431853ac43aSMatt Mackall } 2432853ac43aSMatt Mackall 243341ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 2434853ac43aSMatt Mackall { 2435853ac43aSMatt Mackall return 0; 2436853ac43aSMatt Mackall } 2437853ac43aSMatt Mackall 24383f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 24393f96b79aSHugh Dickins { 24403f96b79aSHugh Dickins return 0; 24413f96b79aSHugh Dickins } 24423f96b79aSHugh Dickins 244341ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 244494c1e62dSHugh Dickins { 244541ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 244694c1e62dSHugh Dickins } 244794c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 244894c1e62dSHugh Dickins 2449853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 24500b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 2451454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 24520b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 24530b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 2454853ac43aSMatt Mackall 2455853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 2456853ac43aSMatt Mackall 2457853ac43aSMatt Mackall /* common code */ 24581da177e4SLinus Torvalds 245946711810SRandy Dunlap /** 24601da177e4SLinus Torvalds * shmem_file_setup - get an unlinked file living in tmpfs 24611da177e4SLinus Torvalds * @name: name for dentry (to be seen in /proc/<pid>/maps 24621da177e4SLinus Torvalds * @size: size to be set for the file 24630b0a0806SHugh Dickins * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 24641da177e4SLinus Torvalds */ 2465168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 24661da177e4SLinus Torvalds { 24671da177e4SLinus Torvalds int error; 24681da177e4SLinus Torvalds struct file *file; 24691da177e4SLinus Torvalds struct inode *inode; 24702c48b9c4SAl Viro struct path path; 24712c48b9c4SAl Viro struct dentry *root; 24721da177e4SLinus Torvalds struct qstr this; 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 24751da177e4SLinus Torvalds return (void *)shm_mnt; 24761da177e4SLinus Torvalds 2477285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 24781da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 24791da177e4SLinus Torvalds 24801da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 24811da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds error = -ENOMEM; 24841da177e4SLinus Torvalds this.name = name; 24851da177e4SLinus Torvalds this.len = strlen(name); 24861da177e4SLinus Torvalds this.hash = 0; /* will go */ 24871da177e4SLinus Torvalds root = shm_mnt->mnt_root; 24882c48b9c4SAl Viro path.dentry = d_alloc(root, &this); 24892c48b9c4SAl Viro if (!path.dentry) 24901da177e4SLinus Torvalds goto put_memory; 24912c48b9c4SAl Viro path.mnt = mntget(shm_mnt); 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds error = -ENOSPC; 2494454abafeSDmitry Monakhov inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 24951da177e4SLinus Torvalds if (!inode) 24964b42af81SAl Viro goto put_dentry; 24971da177e4SLinus Torvalds 24982c48b9c4SAl Viro d_instantiate(path.dentry, inode); 24991da177e4SLinus Torvalds inode->i_size = size; 25001da177e4SLinus Torvalds inode->i_nlink = 0; /* It is unlinked */ 2501853ac43aSMatt Mackall #ifndef CONFIG_MMU 2502853ac43aSMatt Mackall error = ramfs_nommu_expand_for_mapping(inode, size); 2503853ac43aSMatt Mackall if (error) 25044b42af81SAl Viro goto put_dentry; 2505853ac43aSMatt Mackall #endif 25064b42af81SAl Viro 25074b42af81SAl Viro error = -ENFILE; 25082c48b9c4SAl Viro file = alloc_file(&path, FMODE_WRITE | FMODE_READ, 25094b42af81SAl Viro &shmem_file_operations); 25104b42af81SAl Viro if (!file) 25114b42af81SAl Viro goto put_dentry; 25124b42af81SAl Viro 25131da177e4SLinus Torvalds return file; 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds put_dentry: 25162c48b9c4SAl Viro path_put(&path); 25171da177e4SLinus Torvalds put_memory: 25181da177e4SLinus Torvalds shmem_unacct_size(flags, size); 25191da177e4SLinus Torvalds return ERR_PTR(error); 25201da177e4SLinus Torvalds } 2521395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 25221da177e4SLinus Torvalds 252346711810SRandy Dunlap /** 25241da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 25251da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 25261da177e4SLinus Torvalds */ 25271da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 25281da177e4SLinus Torvalds { 25291da177e4SLinus Torvalds struct file *file; 25301da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds file = shmem_file_setup("dev/zero", size, vma->vm_flags); 25331da177e4SLinus Torvalds if (IS_ERR(file)) 25341da177e4SLinus Torvalds return PTR_ERR(file); 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds if (vma->vm_file) 25371da177e4SLinus Torvalds fput(vma->vm_file); 25381da177e4SLinus Torvalds vma->vm_file = file; 25391da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2540bee4c36aSHugh Dickins vma->vm_flags |= VM_CAN_NONLINEAR; 25411da177e4SLinus Torvalds return 0; 25421da177e4SLinus Torvalds } 2543d9d90e5eSHugh Dickins 2544d9d90e5eSHugh Dickins /** 2545d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 2546d9d90e5eSHugh Dickins * @mapping: the page's address_space 2547d9d90e5eSHugh Dickins * @index: the page index 2548d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 2549d9d90e5eSHugh Dickins * 2550d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 2551d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 2552d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 2553d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 2554d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 2555d9d90e5eSHugh Dickins * 255668da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 255768da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 2558d9d90e5eSHugh Dickins */ 2559d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 2560d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 2561d9d90e5eSHugh Dickins { 256268da9f05SHugh Dickins #ifdef CONFIG_SHMEM 256368da9f05SHugh Dickins struct inode *inode = mapping->host; 25649276aad6SHugh Dickins struct page *page; 256568da9f05SHugh Dickins int error; 256668da9f05SHugh Dickins 256768da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 256868da9f05SHugh Dickins error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL); 256968da9f05SHugh Dickins if (error) 257068da9f05SHugh Dickins page = ERR_PTR(error); 257168da9f05SHugh Dickins else 257268da9f05SHugh Dickins unlock_page(page); 257368da9f05SHugh Dickins return page; 257468da9f05SHugh Dickins #else 257568da9f05SHugh Dickins /* 257668da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 257768da9f05SHugh Dickins */ 2578d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 257968da9f05SHugh Dickins #endif 2580d9d90e5eSHugh Dickins } 2581d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 2582