11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Resizable virtual memory filesystem for Linux. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000 Linus Torvalds. 51da177e4SLinus Torvalds * 2000 Transmeta Corp. 61da177e4SLinus Torvalds * 2000-2001 Christoph Rohland 71da177e4SLinus Torvalds * 2000-2001 SAP AG 81da177e4SLinus Torvalds * 2002 Red Hat Inc. 90edd73b3SHugh Dickins * Copyright (C) 2002-2005 Hugh Dickins. 100edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 111da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Extended attribute support for tmpfs: 141da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 151da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 161da177e4SLinus Torvalds * 17853ac43aSMatt Mackall * tiny-shmem: 18853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 19853ac43aSMatt Mackall * 201da177e4SLinus Torvalds * This file is released under the GPL. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 23853ac43aSMatt Mackall #include <linux/fs.h> 24853ac43aSMatt Mackall #include <linux/init.h> 25853ac43aSMatt Mackall #include <linux/vfs.h> 26853ac43aSMatt Mackall #include <linux/mount.h> 27caefba17SHugh Dickins #include <linux/pagemap.h> 28853ac43aSMatt Mackall #include <linux/file.h> 29853ac43aSMatt Mackall #include <linux/mm.h> 30853ac43aSMatt Mackall #include <linux/module.h> 31853ac43aSMatt Mackall #include <linux/swap.h> 32853ac43aSMatt Mackall 33853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 34853ac43aSMatt Mackall 35853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 361da177e4SLinus Torvalds /* 371da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 381da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 391da177e4SLinus Torvalds * which makes it a completely usable filesystem. 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds 4239f0247dSAndreas Gruenbacher #include <linux/xattr.h> 43a5694255SChristoph Hellwig #include <linux/exportfs.h> 441c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 4539f0247dSAndreas Gruenbacher #include <linux/generic_acl.h> 461da177e4SLinus Torvalds #include <linux/mman.h> 471da177e4SLinus Torvalds #include <linux/string.h> 481da177e4SLinus Torvalds #include <linux/slab.h> 491da177e4SLinus Torvalds #include <linux/backing-dev.h> 501da177e4SLinus Torvalds #include <linux/shmem_fs.h> 511da177e4SLinus Torvalds #include <linux/writeback.h> 521da177e4SLinus Torvalds #include <linux/blkdev.h> 53*41ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 54708e3508SHugh Dickins #include <linux/splice.h> 551da177e4SLinus Torvalds #include <linux/security.h> 561da177e4SLinus Torvalds #include <linux/swapops.h> 571da177e4SLinus Torvalds #include <linux/mempolicy.h> 581da177e4SLinus Torvalds #include <linux/namei.h> 59b00dc3adSHugh Dickins #include <linux/ctype.h> 60304dbdb7SLee Schermerhorn #include <linux/migrate.h> 61c1f60a5aSChristoph Lameter #include <linux/highmem.h> 62680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 6392562927SMimi Zohar #include <linux/magic.h> 64304dbdb7SLee Schermerhorn 651da177e4SLinus Torvalds #include <asm/uaccess.h> 661da177e4SLinus Torvalds #include <asm/pgtable.h> 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) 691da177e4SLinus Torvalds #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 721da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 731da177e4SLinus Torvalds 74b09e0fa4SEric Paris struct shmem_xattr { 75b09e0fa4SEric Paris struct list_head list; /* anchored by shmem_inode_info->xattr_list */ 76b09e0fa4SEric Paris char *name; /* xattr name */ 77b09e0fa4SEric Paris size_t size; 78b09e0fa4SEric Paris char value[0]; 79b09e0fa4SEric Paris }; 80b09e0fa4SEric Paris 81285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */ 821da177e4SLinus Torvalds enum sgp_type { 831da177e4SLinus Torvalds SGP_READ, /* don't exceed i_size, don't allocate page */ 841da177e4SLinus Torvalds SGP_CACHE, /* don't exceed i_size, may allocate page */ 85a0ee5ec5SHugh Dickins SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ 861da177e4SLinus Torvalds SGP_WRITE, /* may exceed i_size, may allocate page */ 871da177e4SLinus Torvalds }; 881da177e4SLinus Torvalds 89b76db735SAndrew Morton #ifdef CONFIG_TMPFS 90680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 91680d794bSakpm@linux-foundation.org { 92680d794bSakpm@linux-foundation.org return totalram_pages / 2; 93680d794bSakpm@linux-foundation.org } 94680d794bSakpm@linux-foundation.org 95680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 96680d794bSakpm@linux-foundation.org { 97680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 98680d794bSakpm@linux-foundation.org } 99b76db735SAndrew Morton #endif 100680d794bSakpm@linux-foundation.org 10168da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 10268da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type); 10368da9f05SHugh Dickins 10468da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index, 10568da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, int *fault_type) 10668da9f05SHugh Dickins { 10768da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 10868da9f05SHugh Dickins mapping_gfp_mask(inode->i_mapping), fault_type); 10968da9f05SHugh Dickins } 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1121da177e4SLinus Torvalds { 1131da177e4SLinus Torvalds return sb->s_fs_info; 1141da177e4SLinus Torvalds } 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* 1171da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1181da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1191da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1201da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1211da177e4SLinus Torvalds */ 1221da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1231da177e4SLinus Torvalds { 1240b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 1250b0a0806SHugh Dickins 0 : security_vm_enough_memory_kern(VM_ACCT(size)); 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1291da177e4SLinus Torvalds { 1300b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1311da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* 1351da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 1361da177e4SLinus Torvalds * pages are allocated, in order to allow huge sparse files. 1371da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1381da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1391da177e4SLinus Torvalds */ 1401da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags) 1411da177e4SLinus Torvalds { 1420b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 1430b0a0806SHugh Dickins security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0; 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1471da177e4SLinus Torvalds { 1480b0a0806SHugh Dickins if (flags & VM_NORESERVE) 1491da177e4SLinus Torvalds vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 152759b9775SHugh Dickins static const struct super_operations shmem_ops; 153f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 15415ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 15592e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 15692e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 15792e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 158f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 1591da177e4SLinus Torvalds 1606c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info __read_mostly = { 1611da177e4SLinus Torvalds .ra_pages = 0, /* No readahead */ 1624f98a2feSRik van Riel .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, 1631da177e4SLinus Torvalds }; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 166cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds static void shmem_free_blocks(struct inode *inode, long pages) 1691da177e4SLinus Torvalds { 1701da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 1710edd73b3SHugh Dickins if (sbinfo->max_blocks) { 1727e496299STim Chen percpu_counter_add(&sbinfo->used_blocks, -pages); 1731da177e4SLinus Torvalds inode->i_blocks -= pages*BLOCKS_PER_PAGE; 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1775b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 1785b04c689SPavel Emelyanov { 1795b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1805b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 1815b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 1825b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 1835b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1845b04c689SPavel Emelyanov return -ENOSPC; 1855b04c689SPavel Emelyanov } 1865b04c689SPavel Emelyanov sbinfo->free_inodes--; 1875b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1885b04c689SPavel Emelyanov } 1895b04c689SPavel Emelyanov return 0; 1905b04c689SPavel Emelyanov } 1915b04c689SPavel Emelyanov 1925b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 1935b04c689SPavel Emelyanov { 1945b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1955b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 1965b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 1975b04c689SPavel Emelyanov sbinfo->free_inodes++; 1985b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1995b04c689SPavel Emelyanov } 2005b04c689SPavel Emelyanov } 2015b04c689SPavel Emelyanov 20246711810SRandy Dunlap /** 203*41ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2041da177e4SLinus Torvalds * @inode: inode to recalc 2051da177e4SLinus Torvalds * 2061da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2071da177e4SLinus Torvalds * undirtied hole pages behind our back. 2081da177e4SLinus Torvalds * 2091da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2101da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2111da177e4SLinus Torvalds * 2121da177e4SLinus Torvalds * It has to be called with the spinlock held. 2131da177e4SLinus Torvalds */ 2141da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2151da177e4SLinus Torvalds { 2161da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2171da177e4SLinus Torvalds long freed; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2201da177e4SLinus Torvalds if (freed > 0) { 2211da177e4SLinus Torvalds info->alloced -= freed; 2221da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2231da177e4SLinus Torvalds shmem_free_blocks(inode, freed); 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 227285b2c4fSHugh Dickins static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index, 228285b2c4fSHugh Dickins swp_entry_t swap) 2291da177e4SLinus Torvalds { 230285b2c4fSHugh Dickins if (index < SHMEM_NR_DIRECT) 231285b2c4fSHugh Dickins info->i_direct[index] = swap; 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds 234285b2c4fSHugh Dickins static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index) 2351da177e4SLinus Torvalds { 236285b2c4fSHugh Dickins return (index < SHMEM_NR_DIRECT) ? 237285b2c4fSHugh Dickins info->i_direct[index] : (swp_entry_t){0}; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 240285b2c4fSHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 2411da177e4SLinus Torvalds { 242285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 2431da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 244285b2c4fSHugh Dickins pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 245285b2c4fSHugh Dickins pgoff_t end = (lend >> PAGE_CACHE_SHIFT); 246285b2c4fSHugh Dickins pgoff_t index; 247285b2c4fSHugh Dickins swp_entry_t swap; 2481da177e4SLinus Torvalds 249285b2c4fSHugh Dickins truncate_inode_pages_range(mapping, lstart, lend); 25094c1e62dSHugh Dickins 251285b2c4fSHugh Dickins if (end > SHMEM_NR_DIRECT) 252285b2c4fSHugh Dickins end = SHMEM_NR_DIRECT; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds spin_lock(&info->lock); 255285b2c4fSHugh Dickins for (index = start; index < end; index++) { 256285b2c4fSHugh Dickins swap = shmem_get_swap(info, index); 257285b2c4fSHugh Dickins if (swap.val) { 258285b2c4fSHugh Dickins free_swap_and_cache(swap); 259285b2c4fSHugh Dickins shmem_put_swap(info, index, (swp_entry_t){0}); 260285b2c4fSHugh Dickins info->swapped--; 261a2646d1eSHugh Dickins } 262f6b3ec23SBadari Pulavarty } 263f6b3ec23SBadari Pulavarty 264285b2c4fSHugh Dickins if (mapping->nrpages) { 2651da177e4SLinus Torvalds spin_unlock(&info->lock); 26692a3d03aSBadari Pulavarty /* 267285b2c4fSHugh Dickins * A page may have meanwhile sneaked in from swap. 26892a3d03aSBadari Pulavarty */ 269285b2c4fSHugh Dickins truncate_inode_pages_range(mapping, lstart, lend); 2701ae70006SHugh Dickins spin_lock(&info->lock); 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds shmem_recalc_inode(inode); 2741da177e4SLinus Torvalds spin_unlock(&info->lock); 2751da177e4SLinus Torvalds 276285b2c4fSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 2771da177e4SLinus Torvalds } 27894c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 2791da177e4SLinus Torvalds 28094c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 2811da177e4SLinus Torvalds { 2821da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 2831da177e4SLinus Torvalds int error; 2841da177e4SLinus Torvalds 285db78b877SChristoph Hellwig error = inode_change_ok(inode, attr); 286db78b877SChristoph Hellwig if (error) 287db78b877SChristoph Hellwig return error; 288db78b877SChristoph Hellwig 28994c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 29094c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 29194c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 2923889e6e7Snpiggin@suse.de struct page *page = NULL; 2933889e6e7Snpiggin@suse.de 29494c1e62dSHugh Dickins if (newsize < oldsize) { 2951da177e4SLinus Torvalds /* 2961da177e4SLinus Torvalds * If truncating down to a partial page, then 2971da177e4SLinus Torvalds * if that page is already allocated, hold it 2981da177e4SLinus Torvalds * in memory until the truncation is over, so 299ae0e47f0SJustin P. Mattock * truncate_partial_page cannot miss it were 3001da177e4SLinus Torvalds * it assigned to swap. 3011da177e4SLinus Torvalds */ 3023889e6e7Snpiggin@suse.de if (newsize & (PAGE_CACHE_SIZE-1)) { 3031da177e4SLinus Torvalds (void) shmem_getpage(inode, 3043889e6e7Snpiggin@suse.de newsize >> PAGE_CACHE_SHIFT, 3051da177e4SLinus Torvalds &page, SGP_READ, NULL); 306d3602444SHugh Dickins if (page) 307d3602444SHugh Dickins unlock_page(page); 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds } 31094c1e62dSHugh Dickins if (newsize != oldsize) { 31194c1e62dSHugh Dickins i_size_write(inode, newsize); 31294c1e62dSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 31394c1e62dSHugh Dickins } 31494c1e62dSHugh Dickins if (newsize < oldsize) { 31594c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 31694c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 31794c1e62dSHugh Dickins shmem_truncate_range(inode, newsize, (loff_t)-1); 31894c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 31994c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 32094c1e62dSHugh Dickins } 3213889e6e7Snpiggin@suse.de if (page) 3223889e6e7Snpiggin@suse.de page_cache_release(page); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3256a1a90adSChristoph Hellwig setattr_copy(inode, attr); 32639f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 327db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 3281c7c474cSChristoph Hellwig error = generic_acl_chmod(inode); 32939f0247dSAndreas Gruenbacher #endif 3301da177e4SLinus Torvalds return error; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3331f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 336b09e0fa4SEric Paris struct shmem_xattr *xattr, *nxattr; 3371da177e4SLinus Torvalds 3383889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 3391da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 3401da177e4SLinus Torvalds inode->i_size = 0; 3413889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 3421da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 343cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 3441da177e4SLinus Torvalds list_del_init(&info->swaplist); 345cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds } 348b09e0fa4SEric Paris 349b09e0fa4SEric Paris list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) { 350b09e0fa4SEric Paris kfree(xattr->name); 351b09e0fa4SEric Paris kfree(xattr); 352b09e0fa4SEric Paris } 3531da177e4SLinus Torvalds BUG_ON(inode->i_blocks); 3545b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 3551f895f75SAl Viro end_writeback(inode); 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 358*41ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 359*41ffe5d5SHugh Dickins swp_entry_t swap, struct page *page) 3601da177e4SLinus Torvalds { 361285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 362*41ffe5d5SHugh Dickins pgoff_t index; 363d9fe526aSHugh Dickins int error; 3641da177e4SLinus Torvalds 365*41ffe5d5SHugh Dickins for (index = 0; index < SHMEM_NR_DIRECT; index++) 366*41ffe5d5SHugh Dickins if (shmem_get_swap(info, index).val == swap.val) 3671da177e4SLinus Torvalds goto found; 3681da177e4SLinus Torvalds return 0; 3691da177e4SLinus Torvalds found: 370285b2c4fSHugh Dickins spin_lock(&info->lock); 371*41ffe5d5SHugh Dickins if (shmem_get_swap(info, index).val != swap.val) { 372285b2c4fSHugh Dickins spin_unlock(&info->lock); 373285b2c4fSHugh Dickins return 0; 374285b2c4fSHugh Dickins } 3752e0e26c7SHugh Dickins 3761b1b32f2SHugh Dickins /* 3771b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 3781f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 3791b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 380285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 3811b1b32f2SHugh Dickins */ 3821b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 3832e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 3842e0e26c7SHugh Dickins 385d13d1443SKAMEZAWA Hiroyuki /* 386778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 387778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 388778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 389d13d1443SKAMEZAWA Hiroyuki */ 390*41ffe5d5SHugh Dickins error = add_to_page_cache_locked(page, mapping, index, GFP_NOWAIT); 391778dd893SHugh Dickins /* which does mem_cgroup_uncharge_cache_page on error */ 39269029cd5SKAMEZAWA Hiroyuki 39348f170fbSHugh Dickins if (error != -ENOMEM) { 39473b1262fSHugh Dickins delete_from_swap_cache(page); 39573b1262fSHugh Dickins set_page_dirty(page); 396*41ffe5d5SHugh Dickins shmem_put_swap(info, index, (swp_entry_t){0}); 397285b2c4fSHugh Dickins info->swapped--; 398*41ffe5d5SHugh Dickins swap_free(swap); 3992e0e26c7SHugh Dickins error = 1; /* not an error, but entry was found */ 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds spin_unlock(&info->lock); 4022e0e26c7SHugh Dickins return error; 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* 4061da177e4SLinus Torvalds * shmem_unuse() search for an eventually swapped out shmem page. 4071da177e4SLinus Torvalds */ 408*41ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 4091da177e4SLinus Torvalds { 410*41ffe5d5SHugh Dickins struct list_head *this, *next; 4111da177e4SLinus Torvalds struct shmem_inode_info *info; 4121da177e4SLinus Torvalds int found = 0; 413778dd893SHugh Dickins int error; 414778dd893SHugh Dickins 415778dd893SHugh Dickins /* 416778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 417778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 418778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 419778dd893SHugh Dickins * add_to_page_cache() will be called with GFP_NOWAIT. 420778dd893SHugh Dickins */ 421778dd893SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL); 422778dd893SHugh Dickins if (error) 423778dd893SHugh Dickins goto out; 424778dd893SHugh Dickins /* 425778dd893SHugh Dickins * Try to preload while we can wait, to not make a habit of 426778dd893SHugh Dickins * draining atomic reserves; but don't latch on to this cpu, 427778dd893SHugh Dickins * it's okay if sometimes we get rescheduled after this. 428778dd893SHugh Dickins */ 429778dd893SHugh Dickins error = radix_tree_preload(GFP_KERNEL); 430778dd893SHugh Dickins if (error) 431778dd893SHugh Dickins goto uncharge; 432778dd893SHugh Dickins radix_tree_preload_end(); 4331da177e4SLinus Torvalds 434cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 435*41ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 436*41ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 437285b2c4fSHugh Dickins if (!info->swapped) { 438285b2c4fSHugh Dickins spin_lock(&info->lock); 439285b2c4fSHugh Dickins if (!info->swapped) 440285b2c4fSHugh Dickins list_del_init(&info->swaplist); 441285b2c4fSHugh Dickins spin_unlock(&info->lock); 442285b2c4fSHugh Dickins } 443285b2c4fSHugh Dickins if (info->swapped) 444*41ffe5d5SHugh Dickins found = shmem_unuse_inode(info, swap, page); 445cb5f7b9aSHugh Dickins cond_resched(); 4462e0e26c7SHugh Dickins if (found) 447778dd893SHugh Dickins break; 4481da177e4SLinus Torvalds } 449cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 450778dd893SHugh Dickins 451778dd893SHugh Dickins uncharge: 452778dd893SHugh Dickins if (!found) 453778dd893SHugh Dickins mem_cgroup_uncharge_cache_page(page); 454778dd893SHugh Dickins if (found < 0) 455778dd893SHugh Dickins error = found; 456778dd893SHugh Dickins out: 457aaa46865SHugh Dickins unlock_page(page); 458aaa46865SHugh Dickins page_cache_release(page); 459778dd893SHugh Dickins return error; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds /* 4631da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 4641da177e4SLinus Torvalds */ 4651da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 4661da177e4SLinus Torvalds { 4671da177e4SLinus Torvalds struct shmem_inode_info *info; 468285b2c4fSHugh Dickins swp_entry_t swap, oswap; 4691da177e4SLinus Torvalds struct address_space *mapping; 470*41ffe5d5SHugh Dickins pgoff_t index; 4711da177e4SLinus Torvalds struct inode *inode; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 4741da177e4SLinus Torvalds mapping = page->mapping; 4751da177e4SLinus Torvalds index = page->index; 4761da177e4SLinus Torvalds inode = mapping->host; 4771da177e4SLinus Torvalds info = SHMEM_I(inode); 4781da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 4791da177e4SLinus Torvalds goto redirty; 480d9fe526aSHugh Dickins if (!total_swap_pages) 4811da177e4SLinus Torvalds goto redirty; 4821da177e4SLinus Torvalds 483d9fe526aSHugh Dickins /* 484d9fe526aSHugh Dickins * shmem_backing_dev_info's capabilities prevent regular writeback or 485d9fe526aSHugh Dickins * sync from ever calling shmem_writepage; but a stacking filesystem 48648f170fbSHugh Dickins * might use ->writepage of its underlying filesystem, in which case 487d9fe526aSHugh Dickins * tmpfs should write out to swap only in response to memory pressure, 48848f170fbSHugh Dickins * and not for the writeback threads or sync. 489d9fe526aSHugh Dickins */ 49048f170fbSHugh Dickins if (!wbc->for_reclaim) { 49148f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 49248f170fbSHugh Dickins goto redirty; 49348f170fbSHugh Dickins } 494285b2c4fSHugh Dickins 495285b2c4fSHugh Dickins /* 496285b2c4fSHugh Dickins * Just for this patch, we have a toy implementation, 497285b2c4fSHugh Dickins * which can swap out only the first SHMEM_NR_DIRECT pages: 498285b2c4fSHugh Dickins * for simple demonstration of where we need to think about swap. 499285b2c4fSHugh Dickins */ 500285b2c4fSHugh Dickins if (index >= SHMEM_NR_DIRECT) 501285b2c4fSHugh Dickins goto redirty; 502285b2c4fSHugh Dickins 503d9fe526aSHugh Dickins swap = get_swap_page(); 50448f170fbSHugh Dickins if (!swap.val) 50548f170fbSHugh Dickins goto redirty; 506d9fe526aSHugh Dickins 507b1dea800SHugh Dickins /* 508b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 509b1dea800SHugh Dickins * if it's not already there. Do it now because we cannot take 510b1dea800SHugh Dickins * mutex while holding spinlock, and must do so before the page 511b1dea800SHugh Dickins * is moved to swap cache, when its pagelock no longer protects 512b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 513b1dea800SHugh Dickins * we've taken the spinlock, because shmem_unuse_inode() will 514b1dea800SHugh Dickins * prune a !swapped inode from the swaplist under both locks. 515b1dea800SHugh Dickins */ 516b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 51705bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 51805bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 519b1dea800SHugh Dickins 5201da177e4SLinus Torvalds spin_lock(&info->lock); 521b1dea800SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 522b1dea800SHugh Dickins 523285b2c4fSHugh Dickins oswap = shmem_get_swap(info, index); 524285b2c4fSHugh Dickins if (oswap.val) { 52548f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 526285b2c4fSHugh Dickins free_swap_and_cache(oswap); 527285b2c4fSHugh Dickins shmem_put_swap(info, index, (swp_entry_t){0}); 528285b2c4fSHugh Dickins info->swapped--; 529d9fe526aSHugh Dickins } 530d9fe526aSHugh Dickins shmem_recalc_inode(inode); 5311da177e4SLinus Torvalds 53248f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 5334c73b1bcSMinchan Kim delete_from_page_cache(page); 534285b2c4fSHugh Dickins shmem_put_swap(info, index, swap); 535285b2c4fSHugh Dickins info->swapped++; 536aaa46865SHugh Dickins swap_shmem_alloc(swap); 537826267cfSHugh Dickins spin_unlock(&info->lock); 538d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 5399fab5619SHugh Dickins swap_writepage(page, wbc); 5401da177e4SLinus Torvalds return 0; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds spin_unlock(&info->lock); 544cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, NULL); 5451da177e4SLinus Torvalds redirty: 5461da177e4SLinus Torvalds set_page_dirty(page); 547d9fe526aSHugh Dickins if (wbc->for_reclaim) 548d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 549d9fe526aSHugh Dickins unlock_page(page); 550d9fe526aSHugh Dickins return 0; 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds #ifdef CONFIG_NUMA 554680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 55571fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 556680d794bSakpm@linux-foundation.org { 557680d794bSakpm@linux-foundation.org char buffer[64]; 558680d794bSakpm@linux-foundation.org 55971fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 560095f1fc4SLee Schermerhorn return; /* show nothing */ 561095f1fc4SLee Schermerhorn 56271fe804bSLee Schermerhorn mpol_to_str(buffer, sizeof(buffer), mpol, 1); 563095f1fc4SLee Schermerhorn 564095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 565680d794bSakpm@linux-foundation.org } 56671fe804bSLee Schermerhorn 56771fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 56871fe804bSLee Schermerhorn { 56971fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 57071fe804bSLee Schermerhorn if (sbinfo->mpol) { 57171fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 57271fe804bSLee Schermerhorn mpol = sbinfo->mpol; 57371fe804bSLee Schermerhorn mpol_get(mpol); 57471fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 57571fe804bSLee Schermerhorn } 57671fe804bSLee Schermerhorn return mpol; 57771fe804bSLee Schermerhorn } 578680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 579680d794bSakpm@linux-foundation.org 580*41ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 581*41ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 5821da177e4SLinus Torvalds { 58352cd3b07SLee Schermerhorn struct mempolicy mpol, *spol; 5841da177e4SLinus Torvalds struct vm_area_struct pvma; 5851da177e4SLinus Torvalds 58652cd3b07SLee Schermerhorn spol = mpol_cond_copy(&mpol, 587*41ffe5d5SHugh Dickins mpol_shared_policy_lookup(&info->policy, index)); 58852cd3b07SLee Schermerhorn 5891da177e4SLinus Torvalds /* Create a pseudo vma that just contains the policy */ 590c4cc6d07SHugh Dickins pvma.vm_start = 0; 591*41ffe5d5SHugh Dickins pvma.vm_pgoff = index; 592c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 59352cd3b07SLee Schermerhorn pvma.vm_policy = spol; 594*41ffe5d5SHugh Dickins return swapin_readahead(swap, gfp, &pvma, 0); 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 59702098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp, 598*41ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 5991da177e4SLinus Torvalds { 6001da177e4SLinus Torvalds struct vm_area_struct pvma; 6011da177e4SLinus Torvalds 602c4cc6d07SHugh Dickins /* Create a pseudo vma that just contains the policy */ 603c4cc6d07SHugh Dickins pvma.vm_start = 0; 604*41ffe5d5SHugh Dickins pvma.vm_pgoff = index; 605c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 606*41ffe5d5SHugh Dickins pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 60752cd3b07SLee Schermerhorn 60852cd3b07SLee Schermerhorn /* 60952cd3b07SLee Schermerhorn * alloc_page_vma() will drop the shared policy reference 61052cd3b07SLee Schermerhorn */ 61152cd3b07SLee Schermerhorn return alloc_page_vma(gfp, &pvma, 0); 6121da177e4SLinus Torvalds } 613680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */ 614680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 615*41ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 616680d794bSakpm@linux-foundation.org { 617680d794bSakpm@linux-foundation.org } 618680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 619680d794bSakpm@linux-foundation.org 620*41ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 621*41ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 6221da177e4SLinus Torvalds { 623*41ffe5d5SHugh Dickins return swapin_readahead(swap, gfp, NULL, 0); 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 62602098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp, 627*41ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 6281da177e4SLinus Torvalds { 629e84e2e13SHugh Dickins return alloc_page(gfp); 6301da177e4SLinus Torvalds } 631680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */ 6321da177e4SLinus Torvalds 63371fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) 63471fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 63571fe804bSLee Schermerhorn { 63671fe804bSLee Schermerhorn return NULL; 63771fe804bSLee Schermerhorn } 63871fe804bSLee Schermerhorn #endif 63971fe804bSLee Schermerhorn 6401da177e4SLinus Torvalds /* 64168da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 6421da177e4SLinus Torvalds * 6431da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 6441da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 6451da177e4SLinus Torvalds * entry since a page cannot live in both the swap and page cache 6461da177e4SLinus Torvalds */ 647*41ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 64868da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) 6491da177e4SLinus Torvalds { 6501da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 6511da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 6521da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 65327ab7006SHugh Dickins struct page *page; 654ff36b801SShaohua Li struct page *prealloc_page = NULL; 6551da177e4SLinus Torvalds swp_entry_t swap; 6561da177e4SLinus Torvalds int error; 6571da177e4SLinus Torvalds 658*41ffe5d5SHugh Dickins if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT)) 6591da177e4SLinus Torvalds return -EFBIG; 6601da177e4SLinus Torvalds repeat: 661*41ffe5d5SHugh Dickins page = find_lock_page(mapping, index); 66227ab7006SHugh Dickins if (page) { 66327ab7006SHugh Dickins /* 66427ab7006SHugh Dickins * Once we can get the page lock, it must be uptodate: 66527ab7006SHugh Dickins * if there were an error in reading back from swap, 66627ab7006SHugh Dickins * the page would not be inserted into the filecache. 66727ab7006SHugh Dickins */ 66827ab7006SHugh Dickins BUG_ON(!PageUptodate(page)); 6691da177e4SLinus Torvalds goto done; 67027ab7006SHugh Dickins } 67127ab7006SHugh Dickins 672b409f9fcSHugh Dickins /* 673b409f9fcSHugh Dickins * Try to preload while we can wait, to not make a habit of 674b409f9fcSHugh Dickins * draining atomic reserves; but don't latch on to this cpu. 675b409f9fcSHugh Dickins */ 67668da9f05SHugh Dickins error = radix_tree_preload(gfp & GFP_RECLAIM_MASK); 677b409f9fcSHugh Dickins if (error) 67827ab7006SHugh Dickins goto out; 679b409f9fcSHugh Dickins radix_tree_preload_end(); 68027ab7006SHugh Dickins 681ff36b801SShaohua Li if (sgp != SGP_READ && !prealloc_page) { 682*41ffe5d5SHugh Dickins prealloc_page = shmem_alloc_page(gfp, info, index); 683ff36b801SShaohua Li if (prealloc_page) { 684e83c32e8SHugh Dickins SetPageSwapBacked(prealloc_page); 685ff36b801SShaohua Li if (mem_cgroup_cache_charge(prealloc_page, 686ff36b801SShaohua Li current->mm, GFP_KERNEL)) { 687ff36b801SShaohua Li page_cache_release(prealloc_page); 688ff36b801SShaohua Li prealloc_page = NULL; 689b409f9fcSHugh Dickins } 690ff36b801SShaohua Li } 691ff36b801SShaohua Li } 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds spin_lock(&info->lock); 6941da177e4SLinus Torvalds shmem_recalc_inode(inode); 695*41ffe5d5SHugh Dickins swap = shmem_get_swap(info, index); 6961da177e4SLinus Torvalds if (swap.val) { 6971da177e4SLinus Torvalds /* Look it up and read it in.. */ 69827ab7006SHugh Dickins page = lookup_swap_cache(swap); 69927ab7006SHugh Dickins if (!page) { 700f8891e5eSChristoph Lameter spin_unlock(&info->lock); 701456f998eSYing Han /* here we actually do the io */ 70268da9f05SHugh Dickins if (fault_type) 70368da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 704*41ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 70527ab7006SHugh Dickins if (!page) { 706*41ffe5d5SHugh Dickins swp_entry_t nswap = shmem_get_swap(info, index); 707285b2c4fSHugh Dickins if (nswap.val == swap.val) { 7081da177e4SLinus Torvalds error = -ENOMEM; 70927ab7006SHugh Dickins goto out; 710285b2c4fSHugh Dickins } 7111da177e4SLinus Torvalds goto repeat; 7121da177e4SLinus Torvalds } 71327ab7006SHugh Dickins wait_on_page_locked(page); 71427ab7006SHugh Dickins page_cache_release(page); 7151da177e4SLinus Torvalds goto repeat; 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 71927ab7006SHugh Dickins if (!trylock_page(page)) { 7201da177e4SLinus Torvalds spin_unlock(&info->lock); 72127ab7006SHugh Dickins wait_on_page_locked(page); 72227ab7006SHugh Dickins page_cache_release(page); 7231da177e4SLinus Torvalds goto repeat; 7241da177e4SLinus Torvalds } 72527ab7006SHugh Dickins if (PageWriteback(page)) { 7261da177e4SLinus Torvalds spin_unlock(&info->lock); 72727ab7006SHugh Dickins wait_on_page_writeback(page); 72827ab7006SHugh Dickins unlock_page(page); 72927ab7006SHugh Dickins page_cache_release(page); 7301da177e4SLinus Torvalds goto repeat; 7311da177e4SLinus Torvalds } 73227ab7006SHugh Dickins if (!PageUptodate(page)) { 7331da177e4SLinus Torvalds spin_unlock(&info->lock); 73427ab7006SHugh Dickins unlock_page(page); 73527ab7006SHugh Dickins page_cache_release(page); 7361da177e4SLinus Torvalds error = -EIO; 73727ab7006SHugh Dickins goto out; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds 74027ab7006SHugh Dickins error = add_to_page_cache_locked(page, mapping, 741*41ffe5d5SHugh Dickins index, GFP_NOWAIT); 74227ab7006SHugh Dickins if (error) { 7431da177e4SLinus Torvalds spin_unlock(&info->lock); 74482369553SHugh Dickins if (error == -ENOMEM) { 745ae3abae6SDaisuke Nishimura /* 746ae3abae6SDaisuke Nishimura * reclaim from proper memory cgroup and 747ae3abae6SDaisuke Nishimura * call memcg's OOM if needed. 748ae3abae6SDaisuke Nishimura */ 749ae3abae6SDaisuke Nishimura error = mem_cgroup_shmem_charge_fallback( 75027ab7006SHugh Dickins page, current->mm, gfp); 751b5a84319SKAMEZAWA Hiroyuki if (error) { 75227ab7006SHugh Dickins unlock_page(page); 75327ab7006SHugh Dickins page_cache_release(page); 75427ab7006SHugh Dickins goto out; 75582369553SHugh Dickins } 756b5a84319SKAMEZAWA Hiroyuki } 75727ab7006SHugh Dickins unlock_page(page); 75827ab7006SHugh Dickins page_cache_release(page); 7591da177e4SLinus Torvalds goto repeat; 7601da177e4SLinus Torvalds } 76127ab7006SHugh Dickins 76227ab7006SHugh Dickins delete_from_swap_cache(page); 763*41ffe5d5SHugh Dickins shmem_put_swap(info, index, (swp_entry_t){0}); 764285b2c4fSHugh Dickins info->swapped--; 7651da177e4SLinus Torvalds spin_unlock(&info->lock); 76627ab7006SHugh Dickins set_page_dirty(page); 76727ab7006SHugh Dickins swap_free(swap); 76827ab7006SHugh Dickins 76927ab7006SHugh Dickins } else if (sgp == SGP_READ) { 770*41ffe5d5SHugh Dickins page = find_get_page(mapping, index); 77127ab7006SHugh Dickins if (page && !trylock_page(page)) { 77227ab7006SHugh Dickins spin_unlock(&info->lock); 77327ab7006SHugh Dickins wait_on_page_locked(page); 77427ab7006SHugh Dickins page_cache_release(page); 7751da177e4SLinus Torvalds goto repeat; 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds spin_unlock(&info->lock); 778e83c32e8SHugh Dickins 779e83c32e8SHugh Dickins } else if (prealloc_page) { 7801da177e4SLinus Torvalds sbinfo = SHMEM_SB(inode->i_sb); 7810edd73b3SHugh Dickins if (sbinfo->max_blocks) { 782fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 783fc5da22aSHugh Dickins sbinfo->max_blocks) >= 0 || 78459a16eadSHugh Dickins shmem_acct_block(info->flags)) 78559a16eadSHugh Dickins goto nospace; 7867e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 7871da177e4SLinus Torvalds inode->i_blocks += BLOCKS_PER_PAGE; 78859a16eadSHugh Dickins } else if (shmem_acct_block(info->flags)) 78959a16eadSHugh Dickins goto nospace; 7901da177e4SLinus Torvalds 79127ab7006SHugh Dickins page = prealloc_page; 792ff36b801SShaohua Li prealloc_page = NULL; 793ff36b801SShaohua Li 794*41ffe5d5SHugh Dickins swap = shmem_get_swap(info, index); 795285b2c4fSHugh Dickins if (swap.val) 79627ab7006SHugh Dickins mem_cgroup_uncharge_cache_page(page); 79769029cd5SKAMEZAWA Hiroyuki else 798285b2c4fSHugh Dickins error = add_to_page_cache_lru(page, mapping, 799*41ffe5d5SHugh Dickins index, GFP_NOWAIT); 80069029cd5SKAMEZAWA Hiroyuki /* 80127ab7006SHugh Dickins * At add_to_page_cache_lru() failure, 80227ab7006SHugh Dickins * uncharge will be done automatically. 80369029cd5SKAMEZAWA Hiroyuki */ 804285b2c4fSHugh Dickins if (swap.val || error) { 8051da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, 1); 8061da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 80759a16eadSHugh Dickins spin_unlock(&info->lock); 80859a16eadSHugh Dickins page_cache_release(page); 80959a16eadSHugh Dickins goto repeat; 81059a16eadSHugh Dickins } 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds info->alloced++; 81359a16eadSHugh Dickins spin_unlock(&info->lock); 81427ab7006SHugh Dickins clear_highpage(page); 81527ab7006SHugh Dickins flush_dcache_page(page); 81627ab7006SHugh Dickins SetPageUptodate(page); 817a0ee5ec5SHugh Dickins if (sgp == SGP_DIRTY) 81827ab7006SHugh Dickins set_page_dirty(page); 81927ab7006SHugh Dickins 820e83c32e8SHugh Dickins } else { 821e83c32e8SHugh Dickins spin_unlock(&info->lock); 822e83c32e8SHugh Dickins error = -ENOMEM; 823e83c32e8SHugh Dickins goto out; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds done: 82627ab7006SHugh Dickins *pagep = page; 8271da177e4SLinus Torvalds error = 0; 828ff36b801SShaohua Li out: 829ff36b801SShaohua Li if (prealloc_page) { 830ff36b801SShaohua Li mem_cgroup_uncharge_cache_page(prealloc_page); 831ff36b801SShaohua Li page_cache_release(prealloc_page); 832ff36b801SShaohua Li } 8331da177e4SLinus Torvalds return error; 834d00806b1SNick Piggin 83527d54b39SHugh Dickins nospace: 836d0217ac0SNick Piggin /* 837d0217ac0SNick Piggin * Perhaps the page was brought in from swap between find_lock_page 8381da177e4SLinus Torvalds * and taking info->lock? We allow for that at add_to_page_cache_lru, 83983c54070SNick Piggin * but must also avoid reporting a spurious ENOSPC while working on a 8409276aad6SHugh Dickins * full tmpfs. 8411da177e4SLinus Torvalds */ 842*41ffe5d5SHugh Dickins page = find_get_page(mapping, index); 8431da177e4SLinus Torvalds spin_unlock(&info->lock); 84427ab7006SHugh Dickins if (page) { 8451da177e4SLinus Torvalds page_cache_release(page); 8461da177e4SLinus Torvalds goto repeat; 847d8dc74f2SAdrian Bunk } 8481da177e4SLinus Torvalds error = -ENOSPC; 849e83c32e8SHugh Dickins goto out; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 8531da177e4SLinus Torvalds { 8541da177e4SLinus Torvalds struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 8551da177e4SLinus Torvalds int error; 85668da9f05SHugh Dickins int ret = VM_FAULT_LOCKED; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 8591da177e4SLinus Torvalds return VM_FAULT_SIGBUS; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); 8621da177e4SLinus Torvalds if (error) 8631da177e4SLinus Torvalds return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 86468da9f05SHugh Dickins 865456f998eSYing Han if (ret & VM_FAULT_MAJOR) { 866456f998eSYing Han count_vm_event(PGMAJFAULT); 867456f998eSYing Han mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 868456f998eSYing Han } 86968da9f05SHugh Dickins return ret; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds #ifdef CONFIG_NUMA 873*41ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 8741da177e4SLinus Torvalds { 875*41ffe5d5SHugh Dickins struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 876*41ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 879d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 880d8dc74f2SAdrian Bunk unsigned long addr) 8811da177e4SLinus Torvalds { 882*41ffe5d5SHugh Dickins struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 883*41ffe5d5SHugh Dickins pgoff_t index; 8841da177e4SLinus Torvalds 885*41ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 886*41ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds #endif 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 8911da177e4SLinus Torvalds { 892d3ac7f89SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 8931da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 8941da177e4SLinus Torvalds int retval = -ENOMEM; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds spin_lock(&info->lock); 8971da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 8981da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 8991da177e4SLinus Torvalds goto out_nomem; 9001da177e4SLinus Torvalds info->flags |= VM_LOCKED; 90189e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 9041da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 9051da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 90689e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 90789e004eaSLee Schermerhorn scan_mapping_unevictable_pages(file->f_mapping); 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds retval = 0; 91089e004eaSLee Schermerhorn 9111da177e4SLinus Torvalds out_nomem: 9121da177e4SLinus Torvalds spin_unlock(&info->lock); 9131da177e4SLinus Torvalds return retval; 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds 9169b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 9171da177e4SLinus Torvalds { 9181da177e4SLinus Torvalds file_accessed(file); 9191da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 920d0217ac0SNick Piggin vma->vm_flags |= VM_CAN_NONLINEAR; 9211da177e4SLinus Torvalds return 0; 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds 924454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 925454abafeSDmitry Monakhov int mode, dev_t dev, unsigned long flags) 9261da177e4SLinus Torvalds { 9271da177e4SLinus Torvalds struct inode *inode; 9281da177e4SLinus Torvalds struct shmem_inode_info *info; 9291da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 9301da177e4SLinus Torvalds 9315b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 9321da177e4SLinus Torvalds return NULL; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds inode = new_inode(sb); 9351da177e4SLinus Torvalds if (inode) { 93685fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 937454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 9381da177e4SLinus Torvalds inode->i_blocks = 0; 9391da177e4SLinus Torvalds inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; 9401da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 94191828a40SDavid M. Grimes inode->i_generation = get_seconds(); 9421da177e4SLinus Torvalds info = SHMEM_I(inode); 9431da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 9441da177e4SLinus Torvalds spin_lock_init(&info->lock); 9450b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 9461da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 947b09e0fa4SEric Paris INIT_LIST_HEAD(&info->xattr_list); 94872c04902SAl Viro cache_no_acl(inode); 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds switch (mode & S_IFMT) { 9511da177e4SLinus Torvalds default: 95239f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 9531da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 9541da177e4SLinus Torvalds break; 9551da177e4SLinus Torvalds case S_IFREG: 95614fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 9571da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 9581da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 95971fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 96071fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 9611da177e4SLinus Torvalds break; 9621da177e4SLinus Torvalds case S_IFDIR: 963d8c76e6fSDave Hansen inc_nlink(inode); 9641da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 9651da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 9661da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 9671da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 9681da177e4SLinus Torvalds break; 9691da177e4SLinus Torvalds case S_IFLNK: 9701da177e4SLinus Torvalds /* 9711da177e4SLinus Torvalds * Must not load anything in the rbtree, 9721da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 9731da177e4SLinus Torvalds */ 97471fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 9751da177e4SLinus Torvalds break; 9761da177e4SLinus Torvalds } 9775b04c689SPavel Emelyanov } else 9785b04c689SPavel Emelyanov shmem_free_inode(sb); 9791da177e4SLinus Torvalds return inode; 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 98392e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 98492e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds static int 987800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 988800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 989800d15a5SNick Piggin struct page **pagep, void **fsdata) 9901da177e4SLinus Torvalds { 991800d15a5SNick Piggin struct inode *inode = mapping->host; 992800d15a5SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 993800d15a5SNick Piggin return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); 994800d15a5SNick Piggin } 995800d15a5SNick Piggin 996800d15a5SNick Piggin static int 997800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 998800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 999800d15a5SNick Piggin struct page *page, void *fsdata) 1000800d15a5SNick Piggin { 1001800d15a5SNick Piggin struct inode *inode = mapping->host; 1002800d15a5SNick Piggin 1003800d15a5SNick Piggin if (pos + copied > inode->i_size) 1004800d15a5SNick Piggin i_size_write(inode, pos + copied); 1005800d15a5SNick Piggin 1006d3602444SHugh Dickins set_page_dirty(page); 10076746aff7SWu Fengguang unlock_page(page); 1008d3602444SHugh Dickins page_cache_release(page); 1009d3602444SHugh Dickins 1010800d15a5SNick Piggin return copied; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) 10141da177e4SLinus Torvalds { 1015d3ac7f89SJosef "Jeff" Sipek struct inode *inode = filp->f_path.dentry->d_inode; 10161da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 1017*41ffe5d5SHugh Dickins pgoff_t index; 1018*41ffe5d5SHugh Dickins unsigned long offset; 1019a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 1020a0ee5ec5SHugh Dickins 1021a0ee5ec5SHugh Dickins /* 1022a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 1023a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 1024a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 1025a0ee5ec5SHugh Dickins */ 1026a0ee5ec5SHugh Dickins if (segment_eq(get_fs(), KERNEL_DS)) 1027a0ee5ec5SHugh Dickins sgp = SGP_DIRTY; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds index = *ppos >> PAGE_CACHE_SHIFT; 10301da177e4SLinus Torvalds offset = *ppos & ~PAGE_CACHE_MASK; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds for (;;) { 10331da177e4SLinus Torvalds struct page *page = NULL; 1034*41ffe5d5SHugh Dickins pgoff_t end_index; 1035*41ffe5d5SHugh Dickins unsigned long nr, ret; 10361da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 10391da177e4SLinus Torvalds if (index > end_index) 10401da177e4SLinus Torvalds break; 10411da177e4SLinus Torvalds if (index == end_index) { 10421da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 10431da177e4SLinus Torvalds if (nr <= offset) 10441da177e4SLinus Torvalds break; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 1047a0ee5ec5SHugh Dickins desc->error = shmem_getpage(inode, index, &page, sgp, NULL); 10481da177e4SLinus Torvalds if (desc->error) { 10491da177e4SLinus Torvalds if (desc->error == -EINVAL) 10501da177e4SLinus Torvalds desc->error = 0; 10511da177e4SLinus Torvalds break; 10521da177e4SLinus Torvalds } 1053d3602444SHugh Dickins if (page) 1054d3602444SHugh Dickins unlock_page(page); 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds /* 10571da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 10581b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 10591da177e4SLinus Torvalds */ 10601da177e4SLinus Torvalds nr = PAGE_CACHE_SIZE; 10611da177e4SLinus Torvalds i_size = i_size_read(inode); 10621da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 10631da177e4SLinus Torvalds if (index == end_index) { 10641da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 10651da177e4SLinus Torvalds if (nr <= offset) { 10661da177e4SLinus Torvalds if (page) 10671da177e4SLinus Torvalds page_cache_release(page); 10681da177e4SLinus Torvalds break; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds nr -= offset; 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds if (page) { 10741da177e4SLinus Torvalds /* 10751da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 10761da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 10771da177e4SLinus Torvalds * before reading the page on the kernel side. 10781da177e4SLinus Torvalds */ 10791da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 10801da177e4SLinus Torvalds flush_dcache_page(page); 10811da177e4SLinus Torvalds /* 10821da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 10831da177e4SLinus Torvalds */ 10841da177e4SLinus Torvalds if (!offset) 10851da177e4SLinus Torvalds mark_page_accessed(page); 1086b5810039SNick Piggin } else { 10871da177e4SLinus Torvalds page = ZERO_PAGE(0); 1088b5810039SNick Piggin page_cache_get(page); 1089b5810039SNick Piggin } 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds /* 10921da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 10931da177e4SLinus Torvalds * now we can copy it to user space... 10941da177e4SLinus Torvalds * 10951da177e4SLinus Torvalds * The actor routine returns how many bytes were actually used.. 10961da177e4SLinus Torvalds * NOTE! This may not be the same as how much of a user buffer 10971da177e4SLinus Torvalds * we filled up (we may be padding etc), so we can only update 10981da177e4SLinus Torvalds * "pos" here (the actor routine has to update the user buffer 10991da177e4SLinus Torvalds * pointers and the remaining count). 11001da177e4SLinus Torvalds */ 11011da177e4SLinus Torvalds ret = actor(desc, page, offset, nr); 11021da177e4SLinus Torvalds offset += ret; 11031da177e4SLinus Torvalds index += offset >> PAGE_CACHE_SHIFT; 11041da177e4SLinus Torvalds offset &= ~PAGE_CACHE_MASK; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds page_cache_release(page); 11071da177e4SLinus Torvalds if (ret != nr || !desc->count) 11081da177e4SLinus Torvalds break; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds cond_resched(); 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; 11141da177e4SLinus Torvalds file_accessed(filp); 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 1117bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb, 1118bcd78e49SHugh Dickins const struct iovec *iov, unsigned long nr_segs, loff_t pos) 11191da177e4SLinus Torvalds { 1120bcd78e49SHugh Dickins struct file *filp = iocb->ki_filp; 1121bcd78e49SHugh Dickins ssize_t retval; 1122bcd78e49SHugh Dickins unsigned long seg; 1123bcd78e49SHugh Dickins size_t count; 1124bcd78e49SHugh Dickins loff_t *ppos = &iocb->ki_pos; 1125bcd78e49SHugh Dickins 1126bcd78e49SHugh Dickins retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); 1127bcd78e49SHugh Dickins if (retval) 1128bcd78e49SHugh Dickins return retval; 1129bcd78e49SHugh Dickins 1130bcd78e49SHugh Dickins for (seg = 0; seg < nr_segs; seg++) { 11311da177e4SLinus Torvalds read_descriptor_t desc; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds desc.written = 0; 1134bcd78e49SHugh Dickins desc.arg.buf = iov[seg].iov_base; 1135bcd78e49SHugh Dickins desc.count = iov[seg].iov_len; 1136bcd78e49SHugh Dickins if (desc.count == 0) 1137bcd78e49SHugh Dickins continue; 11381da177e4SLinus Torvalds desc.error = 0; 11391da177e4SLinus Torvalds do_shmem_file_read(filp, ppos, &desc, file_read_actor); 1140bcd78e49SHugh Dickins retval += desc.written; 1141bcd78e49SHugh Dickins if (desc.error) { 1142bcd78e49SHugh Dickins retval = retval ?: desc.error; 1143bcd78e49SHugh Dickins break; 1144bcd78e49SHugh Dickins } 1145bcd78e49SHugh Dickins if (desc.count > 0) 1146bcd78e49SHugh Dickins break; 1147bcd78e49SHugh Dickins } 1148bcd78e49SHugh Dickins return retval; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 1151708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 1152708e3508SHugh Dickins struct pipe_inode_info *pipe, size_t len, 1153708e3508SHugh Dickins unsigned int flags) 1154708e3508SHugh Dickins { 1155708e3508SHugh Dickins struct address_space *mapping = in->f_mapping; 115671f0e07aSHugh Dickins struct inode *inode = mapping->host; 1157708e3508SHugh Dickins unsigned int loff, nr_pages, req_pages; 1158708e3508SHugh Dickins struct page *pages[PIPE_DEF_BUFFERS]; 1159708e3508SHugh Dickins struct partial_page partial[PIPE_DEF_BUFFERS]; 1160708e3508SHugh Dickins struct page *page; 1161708e3508SHugh Dickins pgoff_t index, end_index; 1162708e3508SHugh Dickins loff_t isize, left; 1163708e3508SHugh Dickins int error, page_nr; 1164708e3508SHugh Dickins struct splice_pipe_desc spd = { 1165708e3508SHugh Dickins .pages = pages, 1166708e3508SHugh Dickins .partial = partial, 1167708e3508SHugh Dickins .flags = flags, 1168708e3508SHugh Dickins .ops = &page_cache_pipe_buf_ops, 1169708e3508SHugh Dickins .spd_release = spd_release_page, 1170708e3508SHugh Dickins }; 1171708e3508SHugh Dickins 117271f0e07aSHugh Dickins isize = i_size_read(inode); 1173708e3508SHugh Dickins if (unlikely(*ppos >= isize)) 1174708e3508SHugh Dickins return 0; 1175708e3508SHugh Dickins 1176708e3508SHugh Dickins left = isize - *ppos; 1177708e3508SHugh Dickins if (unlikely(left < len)) 1178708e3508SHugh Dickins len = left; 1179708e3508SHugh Dickins 1180708e3508SHugh Dickins if (splice_grow_spd(pipe, &spd)) 1181708e3508SHugh Dickins return -ENOMEM; 1182708e3508SHugh Dickins 1183708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1184708e3508SHugh Dickins loff = *ppos & ~PAGE_CACHE_MASK; 1185708e3508SHugh Dickins req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1186708e3508SHugh Dickins nr_pages = min(req_pages, pipe->buffers); 1187708e3508SHugh Dickins 1188708e3508SHugh Dickins spd.nr_pages = find_get_pages_contig(mapping, index, 1189708e3508SHugh Dickins nr_pages, spd.pages); 1190708e3508SHugh Dickins index += spd.nr_pages; 1191708e3508SHugh Dickins error = 0; 119271f0e07aSHugh Dickins 1193708e3508SHugh Dickins while (spd.nr_pages < nr_pages) { 119471f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); 119571f0e07aSHugh Dickins if (error) 1196708e3508SHugh Dickins break; 1197708e3508SHugh Dickins unlock_page(page); 1198708e3508SHugh Dickins spd.pages[spd.nr_pages++] = page; 1199708e3508SHugh Dickins index++; 1200708e3508SHugh Dickins } 1201708e3508SHugh Dickins 1202708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1203708e3508SHugh Dickins nr_pages = spd.nr_pages; 1204708e3508SHugh Dickins spd.nr_pages = 0; 120571f0e07aSHugh Dickins 1206708e3508SHugh Dickins for (page_nr = 0; page_nr < nr_pages; page_nr++) { 1207708e3508SHugh Dickins unsigned int this_len; 1208708e3508SHugh Dickins 1209708e3508SHugh Dickins if (!len) 1210708e3508SHugh Dickins break; 1211708e3508SHugh Dickins 1212708e3508SHugh Dickins this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 1213708e3508SHugh Dickins page = spd.pages[page_nr]; 1214708e3508SHugh Dickins 121571f0e07aSHugh Dickins if (!PageUptodate(page) || page->mapping != mapping) { 121671f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, 121771f0e07aSHugh Dickins SGP_CACHE, NULL); 121871f0e07aSHugh Dickins if (error) 1219708e3508SHugh Dickins break; 122071f0e07aSHugh Dickins unlock_page(page); 1221708e3508SHugh Dickins page_cache_release(spd.pages[page_nr]); 1222708e3508SHugh Dickins spd.pages[page_nr] = page; 1223708e3508SHugh Dickins } 1224708e3508SHugh Dickins 122571f0e07aSHugh Dickins isize = i_size_read(inode); 1226708e3508SHugh Dickins end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 1227708e3508SHugh Dickins if (unlikely(!isize || index > end_index)) 1228708e3508SHugh Dickins break; 1229708e3508SHugh Dickins 1230708e3508SHugh Dickins if (end_index == index) { 1231708e3508SHugh Dickins unsigned int plen; 1232708e3508SHugh Dickins 1233708e3508SHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 1234708e3508SHugh Dickins if (plen <= loff) 1235708e3508SHugh Dickins break; 1236708e3508SHugh Dickins 1237708e3508SHugh Dickins this_len = min(this_len, plen - loff); 1238708e3508SHugh Dickins len = this_len; 1239708e3508SHugh Dickins } 1240708e3508SHugh Dickins 1241708e3508SHugh Dickins spd.partial[page_nr].offset = loff; 1242708e3508SHugh Dickins spd.partial[page_nr].len = this_len; 1243708e3508SHugh Dickins len -= this_len; 1244708e3508SHugh Dickins loff = 0; 1245708e3508SHugh Dickins spd.nr_pages++; 1246708e3508SHugh Dickins index++; 1247708e3508SHugh Dickins } 1248708e3508SHugh Dickins 1249708e3508SHugh Dickins while (page_nr < nr_pages) 1250708e3508SHugh Dickins page_cache_release(spd.pages[page_nr++]); 1251708e3508SHugh Dickins 1252708e3508SHugh Dickins if (spd.nr_pages) 1253708e3508SHugh Dickins error = splice_to_pipe(pipe, &spd); 1254708e3508SHugh Dickins 1255708e3508SHugh Dickins splice_shrink_spd(pipe, &spd); 1256708e3508SHugh Dickins 1257708e3508SHugh Dickins if (error > 0) { 1258708e3508SHugh Dickins *ppos += error; 1259708e3508SHugh Dickins file_accessed(in); 1260708e3508SHugh Dickins } 1261708e3508SHugh Dickins return error; 1262708e3508SHugh Dickins } 1263708e3508SHugh Dickins 1264726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 12651da177e4SLinus Torvalds { 1266726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 12691da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 12701da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 12710edd73b3SHugh Dickins if (sbinfo->max_blocks) { 12721da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 1273*41ffe5d5SHugh Dickins buf->f_bavail = 1274*41ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 1275*41ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 12760edd73b3SHugh Dickins } 12770edd73b3SHugh Dickins if (sbinfo->max_inodes) { 12781da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 12791da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 12821da177e4SLinus Torvalds return 0; 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds /* 12861da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 12871da177e4SLinus Torvalds */ 12881da177e4SLinus Torvalds static int 12891da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 12901da177e4SLinus Torvalds { 12910b0a0806SHugh Dickins struct inode *inode; 12921da177e4SLinus Torvalds int error = -ENOSPC; 12931da177e4SLinus Torvalds 1294454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 12951da177e4SLinus Torvalds if (inode) { 12962a7dba39SEric Paris error = security_inode_init_security(inode, dir, 12972a7dba39SEric Paris &dentry->d_name, NULL, 12982a7dba39SEric Paris NULL, NULL); 1299570bc1c2SStephen Smalley if (error) { 1300570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1301570bc1c2SStephen Smalley iput(inode); 1302570bc1c2SStephen Smalley return error; 1303570bc1c2SStephen Smalley } 130439f0247dSAndreas Gruenbacher } 13051c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL 13061c7c474cSChristoph Hellwig error = generic_acl_init(inode, dir); 130739f0247dSAndreas Gruenbacher if (error) { 130839f0247dSAndreas Gruenbacher iput(inode); 130939f0247dSAndreas Gruenbacher return error; 1310570bc1c2SStephen Smalley } 1311718deb6bSAl Viro #else 1312718deb6bSAl Viro error = 0; 13131c7c474cSChristoph Hellwig #endif 13141da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 13151da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 13161da177e4SLinus Torvalds d_instantiate(dentry, inode); 13171da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds return error; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode) 13231da177e4SLinus Torvalds { 13241da177e4SLinus Torvalds int error; 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 13271da177e4SLinus Torvalds return error; 1328d8c76e6fSDave Hansen inc_nlink(dir); 13291da177e4SLinus Torvalds return 0; 13301da177e4SLinus Torvalds } 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode, 13331da177e4SLinus Torvalds struct nameidata *nd) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds /* 13391da177e4SLinus Torvalds * Link a file.. 13401da177e4SLinus Torvalds */ 13411da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 13421da177e4SLinus Torvalds { 13431da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 13445b04c689SPavel Emelyanov int ret; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds /* 13471da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 13481da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 13491da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 13501da177e4SLinus Torvalds */ 13515b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 13525b04c689SPavel Emelyanov if (ret) 13535b04c689SPavel Emelyanov goto out; 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 13561da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 1357d8c76e6fSDave Hansen inc_nlink(inode); 13587de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 13591da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 13601da177e4SLinus Torvalds d_instantiate(dentry, inode); 13615b04c689SPavel Emelyanov out: 13625b04c689SPavel Emelyanov return ret; 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 13661da177e4SLinus Torvalds { 13671da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13681da177e4SLinus Torvalds 13695b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 13705b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 13731da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 13749a53c3a7SDave Hansen drop_nlink(inode); 13751da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 13761da177e4SLinus Torvalds return 0; 13771da177e4SLinus Torvalds } 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 13801da177e4SLinus Torvalds { 13811da177e4SLinus Torvalds if (!simple_empty(dentry)) 13821da177e4SLinus Torvalds return -ENOTEMPTY; 13831da177e4SLinus Torvalds 13849a53c3a7SDave Hansen drop_nlink(dentry->d_inode); 13859a53c3a7SDave Hansen drop_nlink(dir); 13861da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds /* 13901da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 13911da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 13921da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 13931da177e4SLinus Torvalds * gets overwritten. 13941da177e4SLinus Torvalds */ 13951da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 13961da177e4SLinus Torvalds { 13971da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 13981da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 14011da177e4SLinus Torvalds return -ENOTEMPTY; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds if (new_dentry->d_inode) { 14041da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 14051da177e4SLinus Torvalds if (they_are_dirs) 14069a53c3a7SDave Hansen drop_nlink(old_dir); 14071da177e4SLinus Torvalds } else if (they_are_dirs) { 14089a53c3a7SDave Hansen drop_nlink(old_dir); 1409d8c76e6fSDave Hansen inc_nlink(new_dir); 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 14131da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 14141da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 14151da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 14161da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 14171da177e4SLinus Torvalds return 0; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 14211da177e4SLinus Torvalds { 14221da177e4SLinus Torvalds int error; 14231da177e4SLinus Torvalds int len; 14241da177e4SLinus Torvalds struct inode *inode; 14259276aad6SHugh Dickins struct page *page; 14261da177e4SLinus Torvalds char *kaddr; 14271da177e4SLinus Torvalds struct shmem_inode_info *info; 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds len = strlen(symname) + 1; 14301da177e4SLinus Torvalds if (len > PAGE_CACHE_SIZE) 14311da177e4SLinus Torvalds return -ENAMETOOLONG; 14321da177e4SLinus Torvalds 1433454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 14341da177e4SLinus Torvalds if (!inode) 14351da177e4SLinus Torvalds return -ENOSPC; 14361da177e4SLinus Torvalds 14372a7dba39SEric Paris error = security_inode_init_security(inode, dir, &dentry->d_name, NULL, 14382a7dba39SEric Paris NULL, NULL); 1439570bc1c2SStephen Smalley if (error) { 1440570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1441570bc1c2SStephen Smalley iput(inode); 1442570bc1c2SStephen Smalley return error; 1443570bc1c2SStephen Smalley } 1444570bc1c2SStephen Smalley error = 0; 1445570bc1c2SStephen Smalley } 1446570bc1c2SStephen Smalley 14471da177e4SLinus Torvalds info = SHMEM_I(inode); 14481da177e4SLinus Torvalds inode->i_size = len-1; 1449b09e0fa4SEric Paris if (len <= SHMEM_SYMLINK_INLINE_LEN) { 14501da177e4SLinus Torvalds /* do it inline */ 1451b09e0fa4SEric Paris memcpy(info->inline_symlink, symname, len); 14521da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inline_operations; 14531da177e4SLinus Torvalds } else { 14541da177e4SLinus Torvalds error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); 14551da177e4SLinus Torvalds if (error) { 14561da177e4SLinus Torvalds iput(inode); 14571da177e4SLinus Torvalds return error; 14581da177e4SLinus Torvalds } 145914fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 14601da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 14611da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 14621da177e4SLinus Torvalds memcpy(kaddr, symname, len); 14631da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 14641da177e4SLinus Torvalds set_page_dirty(page); 14656746aff7SWu Fengguang unlock_page(page); 14661da177e4SLinus Torvalds page_cache_release(page); 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 14691da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 14701da177e4SLinus Torvalds d_instantiate(dentry, inode); 14711da177e4SLinus Torvalds dget(dentry); 14721da177e4SLinus Torvalds return 0; 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds 1475cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd) 14761da177e4SLinus Torvalds { 1477b09e0fa4SEric Paris nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink); 1478cc314eefSLinus Torvalds return NULL; 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds 1481cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) 14821da177e4SLinus Torvalds { 14831da177e4SLinus Torvalds struct page *page = NULL; 1484*41ffe5d5SHugh Dickins int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); 1485*41ffe5d5SHugh Dickins nd_set_link(nd, error ? ERR_PTR(error) : kmap(page)); 1486d3602444SHugh Dickins if (page) 1487d3602444SHugh Dickins unlock_page(page); 1488cc314eefSLinus Torvalds return page; 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds 1491cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 14921da177e4SLinus Torvalds { 14931da177e4SLinus Torvalds if (!IS_ERR(nd_get_link(nd))) { 1494cc314eefSLinus Torvalds struct page *page = cookie; 14951da177e4SLinus Torvalds kunmap(page); 14961da177e4SLinus Torvalds mark_page_accessed(page); 14971da177e4SLinus Torvalds page_cache_release(page); 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 1501b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 1502b09e0fa4SEric Paris /* 1503b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 1504b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 1505b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 1506b09e0fa4SEric Paris * filesystem level, though. 1507b09e0fa4SEric Paris */ 1508b09e0fa4SEric Paris 1509b09e0fa4SEric Paris static int shmem_xattr_get(struct dentry *dentry, const char *name, 1510b09e0fa4SEric Paris void *buffer, size_t size) 1511b09e0fa4SEric Paris { 1512b09e0fa4SEric Paris struct shmem_inode_info *info; 1513b09e0fa4SEric Paris struct shmem_xattr *xattr; 1514b09e0fa4SEric Paris int ret = -ENODATA; 1515b09e0fa4SEric Paris 1516b09e0fa4SEric Paris info = SHMEM_I(dentry->d_inode); 1517b09e0fa4SEric Paris 1518b09e0fa4SEric Paris spin_lock(&info->lock); 1519b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 1520b09e0fa4SEric Paris if (strcmp(name, xattr->name)) 1521b09e0fa4SEric Paris continue; 1522b09e0fa4SEric Paris 1523b09e0fa4SEric Paris ret = xattr->size; 1524b09e0fa4SEric Paris if (buffer) { 1525b09e0fa4SEric Paris if (size < xattr->size) 1526b09e0fa4SEric Paris ret = -ERANGE; 1527b09e0fa4SEric Paris else 1528b09e0fa4SEric Paris memcpy(buffer, xattr->value, xattr->size); 1529b09e0fa4SEric Paris } 1530b09e0fa4SEric Paris break; 1531b09e0fa4SEric Paris } 1532b09e0fa4SEric Paris spin_unlock(&info->lock); 1533b09e0fa4SEric Paris return ret; 1534b09e0fa4SEric Paris } 1535b09e0fa4SEric Paris 1536b09e0fa4SEric Paris static int shmem_xattr_set(struct dentry *dentry, const char *name, 1537b09e0fa4SEric Paris const void *value, size_t size, int flags) 1538b09e0fa4SEric Paris { 1539b09e0fa4SEric Paris struct inode *inode = dentry->d_inode; 1540b09e0fa4SEric Paris struct shmem_inode_info *info = SHMEM_I(inode); 1541b09e0fa4SEric Paris struct shmem_xattr *xattr; 1542b09e0fa4SEric Paris struct shmem_xattr *new_xattr = NULL; 1543b09e0fa4SEric Paris size_t len; 1544b09e0fa4SEric Paris int err = 0; 1545b09e0fa4SEric Paris 1546b09e0fa4SEric Paris /* value == NULL means remove */ 1547b09e0fa4SEric Paris if (value) { 1548b09e0fa4SEric Paris /* wrap around? */ 1549b09e0fa4SEric Paris len = sizeof(*new_xattr) + size; 1550b09e0fa4SEric Paris if (len <= sizeof(*new_xattr)) 1551b09e0fa4SEric Paris return -ENOMEM; 1552b09e0fa4SEric Paris 1553b09e0fa4SEric Paris new_xattr = kmalloc(len, GFP_KERNEL); 1554b09e0fa4SEric Paris if (!new_xattr) 1555b09e0fa4SEric Paris return -ENOMEM; 1556b09e0fa4SEric Paris 1557b09e0fa4SEric Paris new_xattr->name = kstrdup(name, GFP_KERNEL); 1558b09e0fa4SEric Paris if (!new_xattr->name) { 1559b09e0fa4SEric Paris kfree(new_xattr); 1560b09e0fa4SEric Paris return -ENOMEM; 1561b09e0fa4SEric Paris } 1562b09e0fa4SEric Paris 1563b09e0fa4SEric Paris new_xattr->size = size; 1564b09e0fa4SEric Paris memcpy(new_xattr->value, value, size); 1565b09e0fa4SEric Paris } 1566b09e0fa4SEric Paris 1567b09e0fa4SEric Paris spin_lock(&info->lock); 1568b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 1569b09e0fa4SEric Paris if (!strcmp(name, xattr->name)) { 1570b09e0fa4SEric Paris if (flags & XATTR_CREATE) { 1571b09e0fa4SEric Paris xattr = new_xattr; 1572b09e0fa4SEric Paris err = -EEXIST; 1573b09e0fa4SEric Paris } else if (new_xattr) { 1574b09e0fa4SEric Paris list_replace(&xattr->list, &new_xattr->list); 1575b09e0fa4SEric Paris } else { 1576b09e0fa4SEric Paris list_del(&xattr->list); 1577b09e0fa4SEric Paris } 1578b09e0fa4SEric Paris goto out; 1579b09e0fa4SEric Paris } 1580b09e0fa4SEric Paris } 1581b09e0fa4SEric Paris if (flags & XATTR_REPLACE) { 1582b09e0fa4SEric Paris xattr = new_xattr; 1583b09e0fa4SEric Paris err = -ENODATA; 1584b09e0fa4SEric Paris } else { 1585b09e0fa4SEric Paris list_add(&new_xattr->list, &info->xattr_list); 1586b09e0fa4SEric Paris xattr = NULL; 1587b09e0fa4SEric Paris } 1588b09e0fa4SEric Paris out: 1589b09e0fa4SEric Paris spin_unlock(&info->lock); 1590b09e0fa4SEric Paris if (xattr) 1591b09e0fa4SEric Paris kfree(xattr->name); 1592b09e0fa4SEric Paris kfree(xattr); 1593b09e0fa4SEric Paris return err; 1594b09e0fa4SEric Paris } 1595b09e0fa4SEric Paris 1596b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 1597b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 1598b09e0fa4SEric Paris &generic_acl_access_handler, 1599b09e0fa4SEric Paris &generic_acl_default_handler, 1600b09e0fa4SEric Paris #endif 1601b09e0fa4SEric Paris NULL 1602b09e0fa4SEric Paris }; 1603b09e0fa4SEric Paris 1604b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name) 1605b09e0fa4SEric Paris { 1606b09e0fa4SEric Paris struct { const char *prefix; size_t len; } arr[] = { 1607b09e0fa4SEric Paris { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, 1608b09e0fa4SEric Paris { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } 1609b09e0fa4SEric Paris }; 1610b09e0fa4SEric Paris int i; 1611b09e0fa4SEric Paris 1612b09e0fa4SEric Paris for (i = 0; i < ARRAY_SIZE(arr); i++) { 1613b09e0fa4SEric Paris size_t preflen = arr[i].len; 1614b09e0fa4SEric Paris if (strncmp(name, arr[i].prefix, preflen) == 0) { 1615b09e0fa4SEric Paris if (!name[preflen]) 1616b09e0fa4SEric Paris return -EINVAL; 1617b09e0fa4SEric Paris return 0; 1618b09e0fa4SEric Paris } 1619b09e0fa4SEric Paris } 1620b09e0fa4SEric Paris return -EOPNOTSUPP; 1621b09e0fa4SEric Paris } 1622b09e0fa4SEric Paris 1623b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, 1624b09e0fa4SEric Paris void *buffer, size_t size) 1625b09e0fa4SEric Paris { 1626b09e0fa4SEric Paris int err; 1627b09e0fa4SEric Paris 1628b09e0fa4SEric Paris /* 1629b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 1630b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 1631b09e0fa4SEric Paris * for it via sb->s_xattr. 1632b09e0fa4SEric Paris */ 1633b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 1634b09e0fa4SEric Paris return generic_getxattr(dentry, name, buffer, size); 1635b09e0fa4SEric Paris 1636b09e0fa4SEric Paris err = shmem_xattr_validate(name); 1637b09e0fa4SEric Paris if (err) 1638b09e0fa4SEric Paris return err; 1639b09e0fa4SEric Paris 1640b09e0fa4SEric Paris return shmem_xattr_get(dentry, name, buffer, size); 1641b09e0fa4SEric Paris } 1642b09e0fa4SEric Paris 1643b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name, 1644b09e0fa4SEric Paris const void *value, size_t size, int flags) 1645b09e0fa4SEric Paris { 1646b09e0fa4SEric Paris int err; 1647b09e0fa4SEric Paris 1648b09e0fa4SEric Paris /* 1649b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 1650b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 1651b09e0fa4SEric Paris * for it via sb->s_xattr. 1652b09e0fa4SEric Paris */ 1653b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 1654b09e0fa4SEric Paris return generic_setxattr(dentry, name, value, size, flags); 1655b09e0fa4SEric Paris 1656b09e0fa4SEric Paris err = shmem_xattr_validate(name); 1657b09e0fa4SEric Paris if (err) 1658b09e0fa4SEric Paris return err; 1659b09e0fa4SEric Paris 1660b09e0fa4SEric Paris if (size == 0) 1661b09e0fa4SEric Paris value = ""; /* empty EA, do not remove */ 1662b09e0fa4SEric Paris 1663b09e0fa4SEric Paris return shmem_xattr_set(dentry, name, value, size, flags); 1664b09e0fa4SEric Paris 1665b09e0fa4SEric Paris } 1666b09e0fa4SEric Paris 1667b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name) 1668b09e0fa4SEric Paris { 1669b09e0fa4SEric Paris int err; 1670b09e0fa4SEric Paris 1671b09e0fa4SEric Paris /* 1672b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 1673b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 1674b09e0fa4SEric Paris * for it via sb->s_xattr. 1675b09e0fa4SEric Paris */ 1676b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 1677b09e0fa4SEric Paris return generic_removexattr(dentry, name); 1678b09e0fa4SEric Paris 1679b09e0fa4SEric Paris err = shmem_xattr_validate(name); 1680b09e0fa4SEric Paris if (err) 1681b09e0fa4SEric Paris return err; 1682b09e0fa4SEric Paris 1683b09e0fa4SEric Paris return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE); 1684b09e0fa4SEric Paris } 1685b09e0fa4SEric Paris 1686b09e0fa4SEric Paris static bool xattr_is_trusted(const char *name) 1687b09e0fa4SEric Paris { 1688b09e0fa4SEric Paris return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN); 1689b09e0fa4SEric Paris } 1690b09e0fa4SEric Paris 1691b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 1692b09e0fa4SEric Paris { 1693b09e0fa4SEric Paris bool trusted = capable(CAP_SYS_ADMIN); 1694b09e0fa4SEric Paris struct shmem_xattr *xattr; 1695b09e0fa4SEric Paris struct shmem_inode_info *info; 1696b09e0fa4SEric Paris size_t used = 0; 1697b09e0fa4SEric Paris 1698b09e0fa4SEric Paris info = SHMEM_I(dentry->d_inode); 1699b09e0fa4SEric Paris 1700b09e0fa4SEric Paris spin_lock(&info->lock); 1701b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 1702b09e0fa4SEric Paris size_t len; 1703b09e0fa4SEric Paris 1704b09e0fa4SEric Paris /* skip "trusted." attributes for unprivileged callers */ 1705b09e0fa4SEric Paris if (!trusted && xattr_is_trusted(xattr->name)) 1706b09e0fa4SEric Paris continue; 1707b09e0fa4SEric Paris 1708b09e0fa4SEric Paris len = strlen(xattr->name) + 1; 1709b09e0fa4SEric Paris used += len; 1710b09e0fa4SEric Paris if (buffer) { 1711b09e0fa4SEric Paris if (size < used) { 1712b09e0fa4SEric Paris used = -ERANGE; 1713b09e0fa4SEric Paris break; 1714b09e0fa4SEric Paris } 1715b09e0fa4SEric Paris memcpy(buffer, xattr->name, len); 1716b09e0fa4SEric Paris buffer += len; 1717b09e0fa4SEric Paris } 1718b09e0fa4SEric Paris } 1719b09e0fa4SEric Paris spin_unlock(&info->lock); 1720b09e0fa4SEric Paris 1721b09e0fa4SEric Paris return used; 1722b09e0fa4SEric Paris } 1723b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 1724b09e0fa4SEric Paris 172592e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations = { 17261da177e4SLinus Torvalds .readlink = generic_readlink, 17271da177e4SLinus Torvalds .follow_link = shmem_follow_link_inline, 1728b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 1729b09e0fa4SEric Paris .setxattr = shmem_setxattr, 1730b09e0fa4SEric Paris .getxattr = shmem_getxattr, 1731b09e0fa4SEric Paris .listxattr = shmem_listxattr, 1732b09e0fa4SEric Paris .removexattr = shmem_removexattr, 1733b09e0fa4SEric Paris #endif 17341da177e4SLinus Torvalds }; 17351da177e4SLinus Torvalds 173692e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 17371da177e4SLinus Torvalds .readlink = generic_readlink, 17381da177e4SLinus Torvalds .follow_link = shmem_follow_link, 17391da177e4SLinus Torvalds .put_link = shmem_put_link, 1740b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 1741b09e0fa4SEric Paris .setxattr = shmem_setxattr, 1742b09e0fa4SEric Paris .getxattr = shmem_getxattr, 1743b09e0fa4SEric Paris .listxattr = shmem_listxattr, 1744b09e0fa4SEric Paris .removexattr = shmem_removexattr, 174539f0247dSAndreas Gruenbacher #endif 1746b09e0fa4SEric Paris }; 174739f0247dSAndreas Gruenbacher 174891828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 174991828a40SDavid M. Grimes { 175091828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 175191828a40SDavid M. Grimes } 175291828a40SDavid M. Grimes 175391828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 175491828a40SDavid M. Grimes { 175591828a40SDavid M. Grimes __u32 *fh = vfh; 175691828a40SDavid M. Grimes __u64 inum = fh[2]; 175791828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 175891828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 175991828a40SDavid M. Grimes } 176091828a40SDavid M. Grimes 1761480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 1762480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 176391828a40SDavid M. Grimes { 176491828a40SDavid M. Grimes struct inode *inode; 1765480b116cSChristoph Hellwig struct dentry *dentry = NULL; 1766480b116cSChristoph Hellwig u64 inum = fid->raw[2]; 1767480b116cSChristoph Hellwig inum = (inum << 32) | fid->raw[1]; 176891828a40SDavid M. Grimes 1769480b116cSChristoph Hellwig if (fh_len < 3) 1770480b116cSChristoph Hellwig return NULL; 1771480b116cSChristoph Hellwig 1772480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 1773480b116cSChristoph Hellwig shmem_match, fid->raw); 177491828a40SDavid M. Grimes if (inode) { 1775480b116cSChristoph Hellwig dentry = d_find_alias(inode); 177691828a40SDavid M. Grimes iput(inode); 177791828a40SDavid M. Grimes } 177891828a40SDavid M. Grimes 1779480b116cSChristoph Hellwig return dentry; 178091828a40SDavid M. Grimes } 178191828a40SDavid M. Grimes 178291828a40SDavid M. Grimes static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len, 178391828a40SDavid M. Grimes int connectable) 178491828a40SDavid M. Grimes { 178591828a40SDavid M. Grimes struct inode *inode = dentry->d_inode; 178691828a40SDavid M. Grimes 17875fe0c237SAneesh Kumar K.V if (*len < 3) { 17885fe0c237SAneesh Kumar K.V *len = 3; 178991828a40SDavid M. Grimes return 255; 17905fe0c237SAneesh Kumar K.V } 179191828a40SDavid M. Grimes 17921d3382cbSAl Viro if (inode_unhashed(inode)) { 179391828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 179491828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 179591828a40SDavid M. Grimes * time, we need a lock to ensure we only try 179691828a40SDavid M. Grimes * to do it once 179791828a40SDavid M. Grimes */ 179891828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 179991828a40SDavid M. Grimes spin_lock(&lock); 18001d3382cbSAl Viro if (inode_unhashed(inode)) 180191828a40SDavid M. Grimes __insert_inode_hash(inode, 180291828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 180391828a40SDavid M. Grimes spin_unlock(&lock); 180491828a40SDavid M. Grimes } 180591828a40SDavid M. Grimes 180691828a40SDavid M. Grimes fh[0] = inode->i_generation; 180791828a40SDavid M. Grimes fh[1] = inode->i_ino; 180891828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 180991828a40SDavid M. Grimes 181091828a40SDavid M. Grimes *len = 3; 181191828a40SDavid M. Grimes return 1; 181291828a40SDavid M. Grimes } 181391828a40SDavid M. Grimes 181439655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 181591828a40SDavid M. Grimes .get_parent = shmem_get_parent, 181691828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 1817480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 181891828a40SDavid M. Grimes }; 181991828a40SDavid M. Grimes 1820680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 1821680d794bSakpm@linux-foundation.org bool remount) 18221da177e4SLinus Torvalds { 18231da177e4SLinus Torvalds char *this_char, *value, *rest; 18241da177e4SLinus Torvalds 1825b00dc3adSHugh Dickins while (options != NULL) { 1826b00dc3adSHugh Dickins this_char = options; 1827b00dc3adSHugh Dickins for (;;) { 1828b00dc3adSHugh Dickins /* 1829b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 1830b00dc3adSHugh Dickins * mount options form a comma-separated list, 1831b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 1832b00dc3adSHugh Dickins */ 1833b00dc3adSHugh Dickins options = strchr(options, ','); 1834b00dc3adSHugh Dickins if (options == NULL) 1835b00dc3adSHugh Dickins break; 1836b00dc3adSHugh Dickins options++; 1837b00dc3adSHugh Dickins if (!isdigit(*options)) { 1838b00dc3adSHugh Dickins options[-1] = '\0'; 1839b00dc3adSHugh Dickins break; 1840b00dc3adSHugh Dickins } 1841b00dc3adSHugh Dickins } 18421da177e4SLinus Torvalds if (!*this_char) 18431da177e4SLinus Torvalds continue; 18441da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 18451da177e4SLinus Torvalds *value++ = 0; 18461da177e4SLinus Torvalds } else { 18471da177e4SLinus Torvalds printk(KERN_ERR 18481da177e4SLinus Torvalds "tmpfs: No value for mount option '%s'\n", 18491da177e4SLinus Torvalds this_char); 18501da177e4SLinus Torvalds return 1; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 18541da177e4SLinus Torvalds unsigned long long size; 18551da177e4SLinus Torvalds size = memparse(value,&rest); 18561da177e4SLinus Torvalds if (*rest == '%') { 18571da177e4SLinus Torvalds size <<= PAGE_SHIFT; 18581da177e4SLinus Torvalds size *= totalram_pages; 18591da177e4SLinus Torvalds do_div(size, 100); 18601da177e4SLinus Torvalds rest++; 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds if (*rest) 18631da177e4SLinus Torvalds goto bad_val; 1864680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 1865680d794bSakpm@linux-foundation.org DIV_ROUND_UP(size, PAGE_CACHE_SIZE); 18661da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 1867680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 18681da177e4SLinus Torvalds if (*rest) 18691da177e4SLinus Torvalds goto bad_val; 18701da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 1871680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 18721da177e4SLinus Torvalds if (*rest) 18731da177e4SLinus Torvalds goto bad_val; 18741da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 1875680d794bSakpm@linux-foundation.org if (remount) 18761da177e4SLinus Torvalds continue; 1877680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 18781da177e4SLinus Torvalds if (*rest) 18791da177e4SLinus Torvalds goto bad_val; 18801da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 1881680d794bSakpm@linux-foundation.org if (remount) 18821da177e4SLinus Torvalds continue; 1883680d794bSakpm@linux-foundation.org sbinfo->uid = simple_strtoul(value, &rest, 0); 18841da177e4SLinus Torvalds if (*rest) 18851da177e4SLinus Torvalds goto bad_val; 18861da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 1887680d794bSakpm@linux-foundation.org if (remount) 18881da177e4SLinus Torvalds continue; 1889680d794bSakpm@linux-foundation.org sbinfo->gid = simple_strtoul(value, &rest, 0); 18901da177e4SLinus Torvalds if (*rest) 18911da177e4SLinus Torvalds goto bad_val; 18927339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 189371fe804bSLee Schermerhorn if (mpol_parse_str(value, &sbinfo->mpol, 1)) 18947339ff83SRobin Holt goto bad_val; 18951da177e4SLinus Torvalds } else { 18961da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad mount option %s\n", 18971da177e4SLinus Torvalds this_char); 18981da177e4SLinus Torvalds return 1; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds return 0; 19021da177e4SLinus Torvalds 19031da177e4SLinus Torvalds bad_val: 19041da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", 19051da177e4SLinus Torvalds value, this_char); 19061da177e4SLinus Torvalds return 1; 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 19111da177e4SLinus Torvalds { 19121da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1913680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 19140edd73b3SHugh Dickins unsigned long inodes; 19150edd73b3SHugh Dickins int error = -EINVAL; 19161da177e4SLinus Torvalds 1917680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 19180edd73b3SHugh Dickins return error; 19190edd73b3SHugh Dickins 19200edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 19210edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 19227e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 19230edd73b3SHugh Dickins goto out; 1924680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 19250edd73b3SHugh Dickins goto out; 19260edd73b3SHugh Dickins /* 19270edd73b3SHugh Dickins * Those tests also disallow limited->unlimited while any are in 19280edd73b3SHugh Dickins * use, so i_blocks will always be zero when max_blocks is zero; 19290edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 19300edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 19310edd73b3SHugh Dickins */ 1932680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 19330edd73b3SHugh Dickins goto out; 1934680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 19350edd73b3SHugh Dickins goto out; 19360edd73b3SHugh Dickins 19370edd73b3SHugh Dickins error = 0; 1938680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 1939680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 1940680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 194171fe804bSLee Schermerhorn 194271fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 194371fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 19440edd73b3SHugh Dickins out: 19450edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 19460edd73b3SHugh Dickins return error; 19471da177e4SLinus Torvalds } 1948680d794bSakpm@linux-foundation.org 1949680d794bSakpm@linux-foundation.org static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs) 1950680d794bSakpm@linux-foundation.org { 1951680d794bSakpm@linux-foundation.org struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb); 1952680d794bSakpm@linux-foundation.org 1953680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 1954680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 1955680d794bSakpm@linux-foundation.org sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); 1956680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 1957680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 1958680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 1959680d794bSakpm@linux-foundation.org seq_printf(seq, ",mode=%03o", sbinfo->mode); 1960680d794bSakpm@linux-foundation.org if (sbinfo->uid != 0) 1961680d794bSakpm@linux-foundation.org seq_printf(seq, ",uid=%u", sbinfo->uid); 1962680d794bSakpm@linux-foundation.org if (sbinfo->gid != 0) 1963680d794bSakpm@linux-foundation.org seq_printf(seq, ",gid=%u", sbinfo->gid); 196471fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 1965680d794bSakpm@linux-foundation.org return 0; 1966680d794bSakpm@linux-foundation.org } 1967680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 19701da177e4SLinus Torvalds { 1971602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1972602586a8SHugh Dickins 1973602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 1974602586a8SHugh Dickins kfree(sbinfo); 19751da177e4SLinus Torvalds sb->s_fs_info = NULL; 19761da177e4SLinus Torvalds } 19771da177e4SLinus Torvalds 19782b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 19791da177e4SLinus Torvalds { 19801da177e4SLinus Torvalds struct inode *inode; 19811da177e4SLinus Torvalds struct dentry *root; 19820edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 1983680d794bSakpm@linux-foundation.org int err = -ENOMEM; 1984680d794bSakpm@linux-foundation.org 1985680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 1986425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 1987680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 1988680d794bSakpm@linux-foundation.org if (!sbinfo) 1989680d794bSakpm@linux-foundation.org return -ENOMEM; 1990680d794bSakpm@linux-foundation.org 1991680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 199276aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 199376aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 1994680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 19951da177e4SLinus Torvalds 19960edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 19971da177e4SLinus Torvalds /* 19981da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 19991da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 20001da177e4SLinus Torvalds * but the internal instance is left unlimited. 20011da177e4SLinus Torvalds */ 20021da177e4SLinus Torvalds if (!(sb->s_flags & MS_NOUSER)) { 2003680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 2004680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 2005680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 2006680d794bSakpm@linux-foundation.org err = -EINVAL; 2007680d794bSakpm@linux-foundation.org goto failed; 2008680d794bSakpm@linux-foundation.org } 20091da177e4SLinus Torvalds } 201091828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 20110edd73b3SHugh Dickins #else 20120edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 20130edd73b3SHugh Dickins #endif 20141da177e4SLinus Torvalds 20151da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 2016602586a8SHugh Dickins if (percpu_counter_init(&sbinfo->used_blocks, 0)) 2017602586a8SHugh Dickins goto failed; 2018680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 20191da177e4SLinus Torvalds 2020285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 20211da177e4SLinus Torvalds sb->s_blocksize = PAGE_CACHE_SIZE; 20221da177e4SLinus Torvalds sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 20231da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 20241da177e4SLinus Torvalds sb->s_op = &shmem_ops; 2025cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 2026b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 202739f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 2028b09e0fa4SEric Paris #endif 2029b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 203039f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 203139f0247dSAndreas Gruenbacher #endif 20320edd73b3SHugh Dickins 2033454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 20341da177e4SLinus Torvalds if (!inode) 20351da177e4SLinus Torvalds goto failed; 2036680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 2037680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 20381da177e4SLinus Torvalds root = d_alloc_root(inode); 20391da177e4SLinus Torvalds if (!root) 20401da177e4SLinus Torvalds goto failed_iput; 20411da177e4SLinus Torvalds sb->s_root = root; 20421da177e4SLinus Torvalds return 0; 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds failed_iput: 20451da177e4SLinus Torvalds iput(inode); 20461da177e4SLinus Torvalds failed: 20471da177e4SLinus Torvalds shmem_put_super(sb); 20481da177e4SLinus Torvalds return err; 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds 2051fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 20541da177e4SLinus Torvalds { 2055*41ffe5d5SHugh Dickins struct shmem_inode_info *info; 2056*41ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 2057*41ffe5d5SHugh Dickins if (!info) 20581da177e4SLinus Torvalds return NULL; 2059*41ffe5d5SHugh Dickins return &info->vfs_inode; 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds 2062*41ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 2063fa0d7e3dSNick Piggin { 2064fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 2065fa0d7e3dSNick Piggin INIT_LIST_HEAD(&inode->i_dentry); 2066fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 2067fa0d7e3dSNick Piggin } 2068fa0d7e3dSNick Piggin 20691da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 20701da177e4SLinus Torvalds { 20711da177e4SLinus Torvalds if ((inode->i_mode & S_IFMT) == S_IFREG) { 20721da177e4SLinus Torvalds /* only struct inode is valid if it's an inline symlink */ 20731da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 20741da177e4SLinus Torvalds } 2075*41ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds 2078*41ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 20791da177e4SLinus Torvalds { 2080*41ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 2081*41ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds 2084*41ffe5d5SHugh Dickins static int shmem_init_inodecache(void) 20851da177e4SLinus Torvalds { 20861da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 20871da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 2088*41ffe5d5SHugh Dickins 0, SLAB_PANIC, shmem_init_inode); 20891da177e4SLinus Torvalds return 0; 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds 2092*41ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 20931da177e4SLinus Torvalds { 20941a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds 2097f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 20981da177e4SLinus Torvalds .writepage = shmem_writepage, 209976719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 21001da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2101800d15a5SNick Piggin .write_begin = shmem_write_begin, 2102800d15a5SNick Piggin .write_end = shmem_write_end, 21031da177e4SLinus Torvalds #endif 2104304dbdb7SLee Schermerhorn .migratepage = migrate_page, 2105aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 21061da177e4SLinus Torvalds }; 21071da177e4SLinus Torvalds 210815ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 21091da177e4SLinus Torvalds .mmap = shmem_mmap, 21101da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 21111da177e4SLinus Torvalds .llseek = generic_file_llseek, 2112bcd78e49SHugh Dickins .read = do_sync_read, 21135402b976SHugh Dickins .write = do_sync_write, 2114bcd78e49SHugh Dickins .aio_read = shmem_file_aio_read, 21155402b976SHugh Dickins .aio_write = generic_file_aio_write, 21161b061d92SChristoph Hellwig .fsync = noop_fsync, 2117708e3508SHugh Dickins .splice_read = shmem_file_splice_read, 2118ae976416SHugh Dickins .splice_write = generic_file_splice_write, 21191da177e4SLinus Torvalds #endif 21201da177e4SLinus Torvalds }; 21211da177e4SLinus Torvalds 212292e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 212394c1e62dSHugh Dickins .setattr = shmem_setattr, 2124f6b3ec23SBadari Pulavarty .truncate_range = shmem_truncate_range, 2125b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2126b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2127b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2128b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2129b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2130b09e0fa4SEric Paris #endif 21311da177e4SLinus Torvalds }; 21321da177e4SLinus Torvalds 213392e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 21341da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 21351da177e4SLinus Torvalds .create = shmem_create, 21361da177e4SLinus Torvalds .lookup = simple_lookup, 21371da177e4SLinus Torvalds .link = shmem_link, 21381da177e4SLinus Torvalds .unlink = shmem_unlink, 21391da177e4SLinus Torvalds .symlink = shmem_symlink, 21401da177e4SLinus Torvalds .mkdir = shmem_mkdir, 21411da177e4SLinus Torvalds .rmdir = shmem_rmdir, 21421da177e4SLinus Torvalds .mknod = shmem_mknod, 21431da177e4SLinus Torvalds .rename = shmem_rename, 21441da177e4SLinus Torvalds #endif 2145b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2146b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2147b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2148b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2149b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2150b09e0fa4SEric Paris #endif 215139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 215294c1e62dSHugh Dickins .setattr = shmem_setattr, 215339f0247dSAndreas Gruenbacher #endif 215439f0247dSAndreas Gruenbacher }; 215539f0247dSAndreas Gruenbacher 215692e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 2157b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2158b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2159b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2160b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2161b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2162b09e0fa4SEric Paris #endif 216339f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 216494c1e62dSHugh Dickins .setattr = shmem_setattr, 216539f0247dSAndreas Gruenbacher #endif 21661da177e4SLinus Torvalds }; 21671da177e4SLinus Torvalds 2168759b9775SHugh Dickins static const struct super_operations shmem_ops = { 21691da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 21701da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 21711da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 21721da177e4SLinus Torvalds .statfs = shmem_statfs, 21731da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 2174680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 21751da177e4SLinus Torvalds #endif 21761f895f75SAl Viro .evict_inode = shmem_evict_inode, 21771da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 21781da177e4SLinus Torvalds .put_super = shmem_put_super, 21791da177e4SLinus Torvalds }; 21801da177e4SLinus Torvalds 2181f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 218254cb8821SNick Piggin .fault = shmem_fault, 21831da177e4SLinus Torvalds #ifdef CONFIG_NUMA 21841da177e4SLinus Torvalds .set_policy = shmem_set_policy, 21851da177e4SLinus Torvalds .get_policy = shmem_get_policy, 21861da177e4SLinus Torvalds #endif 21871da177e4SLinus Torvalds }; 21881da177e4SLinus Torvalds 21893c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 21903c26ff6eSAl Viro int flags, const char *dev_name, void *data) 21911da177e4SLinus Torvalds { 21923c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds 2195*41ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 21961da177e4SLinus Torvalds .owner = THIS_MODULE, 21971da177e4SLinus Torvalds .name = "tmpfs", 21983c26ff6eSAl Viro .mount = shmem_mount, 21991da177e4SLinus Torvalds .kill_sb = kill_litter_super, 22001da177e4SLinus Torvalds }; 22011da177e4SLinus Torvalds 2202*41ffe5d5SHugh Dickins int __init shmem_init(void) 22031da177e4SLinus Torvalds { 22041da177e4SLinus Torvalds int error; 22051da177e4SLinus Torvalds 2206e0bf68ddSPeter Zijlstra error = bdi_init(&shmem_backing_dev_info); 2207e0bf68ddSPeter Zijlstra if (error) 2208e0bf68ddSPeter Zijlstra goto out4; 2209e0bf68ddSPeter Zijlstra 2210*41ffe5d5SHugh Dickins error = shmem_init_inodecache(); 22111da177e4SLinus Torvalds if (error) 22121da177e4SLinus Torvalds goto out3; 22131da177e4SLinus Torvalds 2214*41ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 22151da177e4SLinus Torvalds if (error) { 22161da177e4SLinus Torvalds printk(KERN_ERR "Could not register tmpfs\n"); 22171da177e4SLinus Torvalds goto out2; 22181da177e4SLinus Torvalds } 221995dc112aSGreg Kroah-Hartman 2220*41ffe5d5SHugh Dickins shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER, 2221*41ffe5d5SHugh Dickins shmem_fs_type.name, NULL); 22221da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 22231da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 22241da177e4SLinus Torvalds printk(KERN_ERR "Could not kern_mount tmpfs\n"); 22251da177e4SLinus Torvalds goto out1; 22261da177e4SLinus Torvalds } 22271da177e4SLinus Torvalds return 0; 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds out1: 2230*41ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 22311da177e4SLinus Torvalds out2: 2232*41ffe5d5SHugh Dickins shmem_destroy_inodecache(); 22331da177e4SLinus Torvalds out3: 2234e0bf68ddSPeter Zijlstra bdi_destroy(&shmem_backing_dev_info); 2235e0bf68ddSPeter Zijlstra out4: 22361da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 22371da177e4SLinus Torvalds return error; 22381da177e4SLinus Torvalds } 2239853ac43aSMatt Mackall 224087946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR 224187946a72SDaisuke Nishimura /** 2242*41ffe5d5SHugh Dickins * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file 224387946a72SDaisuke Nishimura * @inode: the inode to be searched 2244*41ffe5d5SHugh Dickins * @index: the page offset to be searched 224587946a72SDaisuke Nishimura * @pagep: the pointer for the found page to be stored 2246*41ffe5d5SHugh Dickins * @swapp: the pointer for the found swap entry to be stored 224787946a72SDaisuke Nishimura * 224887946a72SDaisuke Nishimura * If a page is found, refcount of it is incremented. Callers should handle 224987946a72SDaisuke Nishimura * these refcount. 225087946a72SDaisuke Nishimura */ 2251*41ffe5d5SHugh Dickins void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index, 2252*41ffe5d5SHugh Dickins struct page **pagep, swp_entry_t *swapp) 225387946a72SDaisuke Nishimura { 225487946a72SDaisuke Nishimura struct shmem_inode_info *info = SHMEM_I(inode); 2255*41ffe5d5SHugh Dickins struct page *page = NULL; 2256*41ffe5d5SHugh Dickins swp_entry_t swap = {0}; 225787946a72SDaisuke Nishimura 2258*41ffe5d5SHugh Dickins if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 225987946a72SDaisuke Nishimura goto out; 226087946a72SDaisuke Nishimura 226187946a72SDaisuke Nishimura spin_lock(&info->lock); 226287946a72SDaisuke Nishimura #ifdef CONFIG_SWAP 2263*41ffe5d5SHugh Dickins swap = shmem_get_swap(info, index); 2264*41ffe5d5SHugh Dickins if (swap.val) 2265*41ffe5d5SHugh Dickins page = find_get_page(&swapper_space, swap.val); 2266285b2c4fSHugh Dickins else 226787946a72SDaisuke Nishimura #endif 2268*41ffe5d5SHugh Dickins page = find_get_page(inode->i_mapping, index); 226987946a72SDaisuke Nishimura spin_unlock(&info->lock); 227087946a72SDaisuke Nishimura out: 227187946a72SDaisuke Nishimura *pagep = page; 2272*41ffe5d5SHugh Dickins *swapp = swap; 227387946a72SDaisuke Nishimura } 227487946a72SDaisuke Nishimura #endif 227587946a72SDaisuke Nishimura 2276853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 2277853ac43aSMatt Mackall 2278853ac43aSMatt Mackall /* 2279853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 2280853ac43aSMatt Mackall * 2281853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 2282853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 2283853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 2284853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 2285853ac43aSMatt Mackall */ 2286853ac43aSMatt Mackall 2287853ac43aSMatt Mackall #include <linux/ramfs.h> 2288853ac43aSMatt Mackall 2289*41ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 2290853ac43aSMatt Mackall .name = "tmpfs", 22913c26ff6eSAl Viro .mount = ramfs_mount, 2292853ac43aSMatt Mackall .kill_sb = kill_litter_super, 2293853ac43aSMatt Mackall }; 2294853ac43aSMatt Mackall 2295*41ffe5d5SHugh Dickins int __init shmem_init(void) 2296853ac43aSMatt Mackall { 2297*41ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 2298853ac43aSMatt Mackall 2299*41ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 2300853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 2301853ac43aSMatt Mackall 2302853ac43aSMatt Mackall return 0; 2303853ac43aSMatt Mackall } 2304853ac43aSMatt Mackall 2305*41ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 2306853ac43aSMatt Mackall { 2307853ac43aSMatt Mackall return 0; 2308853ac43aSMatt Mackall } 2309853ac43aSMatt Mackall 23103f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 23113f96b79aSHugh Dickins { 23123f96b79aSHugh Dickins return 0; 23133f96b79aSHugh Dickins } 23143f96b79aSHugh Dickins 2315*41ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 231694c1e62dSHugh Dickins { 2317*41ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 231894c1e62dSHugh Dickins } 231994c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 232094c1e62dSHugh Dickins 232187946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR 232287946a72SDaisuke Nishimura /** 2323*41ffe5d5SHugh Dickins * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file 232487946a72SDaisuke Nishimura * @inode: the inode to be searched 2325*41ffe5d5SHugh Dickins * @index: the page offset to be searched 232687946a72SDaisuke Nishimura * @pagep: the pointer for the found page to be stored 2327*41ffe5d5SHugh Dickins * @swapp: the pointer for the found swap entry to be stored 232887946a72SDaisuke Nishimura * 232987946a72SDaisuke Nishimura * If a page is found, refcount of it is incremented. Callers should handle 233087946a72SDaisuke Nishimura * these refcount. 233187946a72SDaisuke Nishimura */ 2332*41ffe5d5SHugh Dickins void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index, 2333*41ffe5d5SHugh Dickins struct page **pagep, swp_entry_t *swapp) 233487946a72SDaisuke Nishimura { 233587946a72SDaisuke Nishimura struct page *page = NULL; 233687946a72SDaisuke Nishimura 2337*41ffe5d5SHugh Dickins if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 233887946a72SDaisuke Nishimura goto out; 2339*41ffe5d5SHugh Dickins page = find_get_page(inode->i_mapping, index); 234087946a72SDaisuke Nishimura out: 234187946a72SDaisuke Nishimura *pagep = page; 2342*41ffe5d5SHugh Dickins *swapp = (swp_entry_t){0}; 234387946a72SDaisuke Nishimura } 234487946a72SDaisuke Nishimura #endif 234587946a72SDaisuke Nishimura 2346853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 23470b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 2348454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 23490b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 23500b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 2351853ac43aSMatt Mackall 2352853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 2353853ac43aSMatt Mackall 2354853ac43aSMatt Mackall /* common code */ 23551da177e4SLinus Torvalds 235646711810SRandy Dunlap /** 23571da177e4SLinus Torvalds * shmem_file_setup - get an unlinked file living in tmpfs 23581da177e4SLinus Torvalds * @name: name for dentry (to be seen in /proc/<pid>/maps 23591da177e4SLinus Torvalds * @size: size to be set for the file 23600b0a0806SHugh Dickins * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 23611da177e4SLinus Torvalds */ 2362168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 23631da177e4SLinus Torvalds { 23641da177e4SLinus Torvalds int error; 23651da177e4SLinus Torvalds struct file *file; 23661da177e4SLinus Torvalds struct inode *inode; 23672c48b9c4SAl Viro struct path path; 23682c48b9c4SAl Viro struct dentry *root; 23691da177e4SLinus Torvalds struct qstr this; 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 23721da177e4SLinus Torvalds return (void *)shm_mnt; 23731da177e4SLinus Torvalds 2374285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 23751da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 23761da177e4SLinus Torvalds 23771da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 23781da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 23791da177e4SLinus Torvalds 23801da177e4SLinus Torvalds error = -ENOMEM; 23811da177e4SLinus Torvalds this.name = name; 23821da177e4SLinus Torvalds this.len = strlen(name); 23831da177e4SLinus Torvalds this.hash = 0; /* will go */ 23841da177e4SLinus Torvalds root = shm_mnt->mnt_root; 23852c48b9c4SAl Viro path.dentry = d_alloc(root, &this); 23862c48b9c4SAl Viro if (!path.dentry) 23871da177e4SLinus Torvalds goto put_memory; 23882c48b9c4SAl Viro path.mnt = mntget(shm_mnt); 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds error = -ENOSPC; 2391454abafeSDmitry Monakhov inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 23921da177e4SLinus Torvalds if (!inode) 23934b42af81SAl Viro goto put_dentry; 23941da177e4SLinus Torvalds 23952c48b9c4SAl Viro d_instantiate(path.dentry, inode); 23961da177e4SLinus Torvalds inode->i_size = size; 23971da177e4SLinus Torvalds inode->i_nlink = 0; /* It is unlinked */ 2398853ac43aSMatt Mackall #ifndef CONFIG_MMU 2399853ac43aSMatt Mackall error = ramfs_nommu_expand_for_mapping(inode, size); 2400853ac43aSMatt Mackall if (error) 24014b42af81SAl Viro goto put_dentry; 2402853ac43aSMatt Mackall #endif 24034b42af81SAl Viro 24044b42af81SAl Viro error = -ENFILE; 24052c48b9c4SAl Viro file = alloc_file(&path, FMODE_WRITE | FMODE_READ, 24064b42af81SAl Viro &shmem_file_operations); 24074b42af81SAl Viro if (!file) 24084b42af81SAl Viro goto put_dentry; 24094b42af81SAl Viro 24101da177e4SLinus Torvalds return file; 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds put_dentry: 24132c48b9c4SAl Viro path_put(&path); 24141da177e4SLinus Torvalds put_memory: 24151da177e4SLinus Torvalds shmem_unacct_size(flags, size); 24161da177e4SLinus Torvalds return ERR_PTR(error); 24171da177e4SLinus Torvalds } 2418395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 24191da177e4SLinus Torvalds 242046711810SRandy Dunlap /** 24211da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 24221da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 24231da177e4SLinus Torvalds */ 24241da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 24251da177e4SLinus Torvalds { 24261da177e4SLinus Torvalds struct file *file; 24271da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 24281da177e4SLinus Torvalds 24291da177e4SLinus Torvalds file = shmem_file_setup("dev/zero", size, vma->vm_flags); 24301da177e4SLinus Torvalds if (IS_ERR(file)) 24311da177e4SLinus Torvalds return PTR_ERR(file); 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds if (vma->vm_file) 24341da177e4SLinus Torvalds fput(vma->vm_file); 24351da177e4SLinus Torvalds vma->vm_file = file; 24361da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2437bee4c36aSHugh Dickins vma->vm_flags |= VM_CAN_NONLINEAR; 24381da177e4SLinus Torvalds return 0; 24391da177e4SLinus Torvalds } 2440d9d90e5eSHugh Dickins 2441d9d90e5eSHugh Dickins /** 2442d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 2443d9d90e5eSHugh Dickins * @mapping: the page's address_space 2444d9d90e5eSHugh Dickins * @index: the page index 2445d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 2446d9d90e5eSHugh Dickins * 2447d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 2448d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 2449d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 2450d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 2451d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 2452d9d90e5eSHugh Dickins * 245368da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 245468da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 2455d9d90e5eSHugh Dickins */ 2456d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 2457d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 2458d9d90e5eSHugh Dickins { 245968da9f05SHugh Dickins #ifdef CONFIG_SHMEM 246068da9f05SHugh Dickins struct inode *inode = mapping->host; 24619276aad6SHugh Dickins struct page *page; 246268da9f05SHugh Dickins int error; 246368da9f05SHugh Dickins 246468da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 246568da9f05SHugh Dickins error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL); 246668da9f05SHugh Dickins if (error) 246768da9f05SHugh Dickins page = ERR_PTR(error); 246868da9f05SHugh Dickins else 246968da9f05SHugh Dickins unlock_page(page); 247068da9f05SHugh Dickins return page; 247168da9f05SHugh Dickins #else 247268da9f05SHugh Dickins /* 247368da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 247468da9f05SHugh Dickins */ 2475d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 247668da9f05SHugh Dickins #endif 2477d9d90e5eSHugh Dickins } 2478d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 2479