11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Resizable virtual memory filesystem for Linux. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000 Linus Torvalds. 51da177e4SLinus Torvalds * 2000 Transmeta Corp. 61da177e4SLinus Torvalds * 2000-2001 Christoph Rohland 71da177e4SLinus Torvalds * 2000-2001 SAP AG 81da177e4SLinus Torvalds * 2002 Red Hat Inc. 96922c0c7SHugh Dickins * Copyright (C) 2002-2011 Hugh Dickins. 106922c0c7SHugh Dickins * Copyright (C) 2011 Google Inc. 110edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 121da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Extended attribute support for tmpfs: 151da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 161da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 171da177e4SLinus Torvalds * 18853ac43aSMatt Mackall * tiny-shmem: 19853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 20853ac43aSMatt Mackall * 211da177e4SLinus Torvalds * This file is released under the GPL. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 24853ac43aSMatt Mackall #include <linux/fs.h> 25853ac43aSMatt Mackall #include <linux/init.h> 26853ac43aSMatt Mackall #include <linux/vfs.h> 27853ac43aSMatt Mackall #include <linux/mount.h> 28250297edSAndrew Morton #include <linux/ramfs.h> 29caefba17SHugh Dickins #include <linux/pagemap.h> 30853ac43aSMatt Mackall #include <linux/file.h> 31853ac43aSMatt Mackall #include <linux/mm.h> 32b95f1b31SPaul Gortmaker #include <linux/export.h> 33853ac43aSMatt Mackall #include <linux/swap.h> 34a27bb332SKent Overstreet #include <linux/aio.h> 35853ac43aSMatt Mackall 36853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 37853ac43aSMatt Mackall 38853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 391da177e4SLinus Torvalds /* 401da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 411da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 421da177e4SLinus Torvalds * which makes it a completely usable filesystem. 431da177e4SLinus Torvalds */ 441da177e4SLinus Torvalds 4539f0247dSAndreas Gruenbacher #include <linux/xattr.h> 46a5694255SChristoph Hellwig #include <linux/exportfs.h> 471c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 4839f0247dSAndreas Gruenbacher #include <linux/generic_acl.h> 491da177e4SLinus Torvalds #include <linux/mman.h> 501da177e4SLinus Torvalds #include <linux/string.h> 511da177e4SLinus Torvalds #include <linux/slab.h> 521da177e4SLinus Torvalds #include <linux/backing-dev.h> 531da177e4SLinus Torvalds #include <linux/shmem_fs.h> 541da177e4SLinus Torvalds #include <linux/writeback.h> 551da177e4SLinus Torvalds #include <linux/blkdev.h> 56bda97eabSHugh Dickins #include <linux/pagevec.h> 5741ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 5883e4fa9cSHugh Dickins #include <linux/falloc.h> 59708e3508SHugh Dickins #include <linux/splice.h> 601da177e4SLinus Torvalds #include <linux/security.h> 611da177e4SLinus Torvalds #include <linux/swapops.h> 621da177e4SLinus Torvalds #include <linux/mempolicy.h> 631da177e4SLinus Torvalds #include <linux/namei.h> 64b00dc3adSHugh Dickins #include <linux/ctype.h> 65304dbdb7SLee Schermerhorn #include <linux/migrate.h> 66c1f60a5aSChristoph Lameter #include <linux/highmem.h> 67680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 6892562927SMimi Zohar #include <linux/magic.h> 69304dbdb7SLee Schermerhorn 701da177e4SLinus Torvalds #include <asm/uaccess.h> 711da177e4SLinus Torvalds #include <asm/pgtable.h> 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) 741da177e4SLinus Torvalds #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 771da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 781da177e4SLinus Torvalds 7969f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 8069f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 8169f07ec9SHugh Dickins 821aac1400SHugh Dickins /* 831aac1400SHugh Dickins * shmem_fallocate and shmem_writepage communicate via inode->i_private 841aac1400SHugh Dickins * (with i_mutex making sure that it has only one user at a time): 851aac1400SHugh Dickins * we would prefer not to enlarge the shmem inode just for that. 861aac1400SHugh Dickins */ 871aac1400SHugh Dickins struct shmem_falloc { 881aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 891aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 901aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 911aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 921aac1400SHugh Dickins }; 931aac1400SHugh Dickins 94285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */ 951da177e4SLinus Torvalds enum sgp_type { 961da177e4SLinus Torvalds SGP_READ, /* don't exceed i_size, don't allocate page */ 971da177e4SLinus Torvalds SGP_CACHE, /* don't exceed i_size, may allocate page */ 98a0ee5ec5SHugh Dickins SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ 991635f6a7SHugh Dickins SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */ 1001635f6a7SHugh Dickins SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */ 1011da177e4SLinus Torvalds }; 1021da177e4SLinus Torvalds 103b76db735SAndrew Morton #ifdef CONFIG_TMPFS 104680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 105680d794bSakpm@linux-foundation.org { 106680d794bSakpm@linux-foundation.org return totalram_pages / 2; 107680d794bSakpm@linux-foundation.org } 108680d794bSakpm@linux-foundation.org 109680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 110680d794bSakpm@linux-foundation.org { 111680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 112680d794bSakpm@linux-foundation.org } 113b76db735SAndrew Morton #endif 114680d794bSakpm@linux-foundation.org 115bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp); 116bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 117bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index); 11868da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 11968da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type); 12068da9f05SHugh Dickins 12168da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index, 12268da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, int *fault_type) 12368da9f05SHugh Dickins { 12468da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 12568da9f05SHugh Dickins mapping_gfp_mask(inode->i_mapping), fault_type); 12668da9f05SHugh Dickins } 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1291da177e4SLinus Torvalds { 1301da177e4SLinus Torvalds return sb->s_fs_info; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /* 1341da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1351da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1361da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1371da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1381da177e4SLinus Torvalds */ 1391da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1401da177e4SLinus Torvalds { 1410b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 142191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1461da177e4SLinus Torvalds { 1470b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1481da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds /* 1521da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 1531da177e4SLinus Torvalds * pages are allocated, in order to allow huge sparse files. 1541da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1551da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1561da177e4SLinus Torvalds */ 1571da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags) 1581da177e4SLinus Torvalds { 1590b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 160191c5424SAl Viro security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1641da177e4SLinus Torvalds { 1650b0a0806SHugh Dickins if (flags & VM_NORESERVE) 1661da177e4SLinus Torvalds vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 169759b9775SHugh Dickins static const struct super_operations shmem_ops; 170f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 17115ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 17292e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 17392e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 17492e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 175f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 1761da177e4SLinus Torvalds 1776c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info __read_mostly = { 1781da177e4SLinus Torvalds .ra_pages = 0, /* No readahead */ 1794f98a2feSRik van Riel .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, 1801da177e4SLinus Torvalds }; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 183cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 1841da177e4SLinus Torvalds 1855b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 1865b04c689SPavel Emelyanov { 1875b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1885b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 1895b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 1905b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 1915b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1925b04c689SPavel Emelyanov return -ENOSPC; 1935b04c689SPavel Emelyanov } 1945b04c689SPavel Emelyanov sbinfo->free_inodes--; 1955b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1965b04c689SPavel Emelyanov } 1975b04c689SPavel Emelyanov return 0; 1985b04c689SPavel Emelyanov } 1995b04c689SPavel Emelyanov 2005b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2015b04c689SPavel Emelyanov { 2025b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2035b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2045b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2055b04c689SPavel Emelyanov sbinfo->free_inodes++; 2065b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2075b04c689SPavel Emelyanov } 2085b04c689SPavel Emelyanov } 2095b04c689SPavel Emelyanov 21046711810SRandy Dunlap /** 21141ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2121da177e4SLinus Torvalds * @inode: inode to recalc 2131da177e4SLinus Torvalds * 2141da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2151da177e4SLinus Torvalds * undirtied hole pages behind our back. 2161da177e4SLinus Torvalds * 2171da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2181da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2191da177e4SLinus Torvalds * 2201da177e4SLinus Torvalds * It has to be called with the spinlock held. 2211da177e4SLinus Torvalds */ 2221da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2231da177e4SLinus Torvalds { 2241da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2251da177e4SLinus Torvalds long freed; 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2281da177e4SLinus Torvalds if (freed > 0) { 22954af6042SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 23054af6042SHugh Dickins if (sbinfo->max_blocks) 23154af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -freed); 2321da177e4SLinus Torvalds info->alloced -= freed; 23354af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2341da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2387a5d0fbbSHugh Dickins /* 2397a5d0fbbSHugh Dickins * Replace item expected in radix tree by a new item, while holding tree lock. 2407a5d0fbbSHugh Dickins */ 2417a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping, 2427a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 2437a5d0fbbSHugh Dickins { 2447a5d0fbbSHugh Dickins void **pslot; 2457a5d0fbbSHugh Dickins void *item = NULL; 2467a5d0fbbSHugh Dickins 2477a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 2487a5d0fbbSHugh Dickins pslot = radix_tree_lookup_slot(&mapping->page_tree, index); 2497a5d0fbbSHugh Dickins if (pslot) 2507a5d0fbbSHugh Dickins item = radix_tree_deref_slot_protected(pslot, 2517a5d0fbbSHugh Dickins &mapping->tree_lock); 2527a5d0fbbSHugh Dickins if (item != expected) 2537a5d0fbbSHugh Dickins return -ENOENT; 2547a5d0fbbSHugh Dickins if (replacement) 2557a5d0fbbSHugh Dickins radix_tree_replace_slot(pslot, replacement); 2567a5d0fbbSHugh Dickins else 2577a5d0fbbSHugh Dickins radix_tree_delete(&mapping->page_tree, index); 2587a5d0fbbSHugh Dickins return 0; 2597a5d0fbbSHugh Dickins } 2607a5d0fbbSHugh Dickins 2617a5d0fbbSHugh Dickins /* 262d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 263d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 264d1899228SHugh Dickins * 265d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 266d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 267d1899228SHugh Dickins */ 268d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 269d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 270d1899228SHugh Dickins { 271d1899228SHugh Dickins void *item; 272d1899228SHugh Dickins 273d1899228SHugh Dickins rcu_read_lock(); 274d1899228SHugh Dickins item = radix_tree_lookup(&mapping->page_tree, index); 275d1899228SHugh Dickins rcu_read_unlock(); 276d1899228SHugh Dickins return item == swp_to_radix_entry(swap); 277d1899228SHugh Dickins } 278d1899228SHugh Dickins 279d1899228SHugh Dickins /* 28046f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 28146f65ec1SHugh Dickins */ 28246f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 28346f65ec1SHugh Dickins struct address_space *mapping, 28446f65ec1SHugh Dickins pgoff_t index, gfp_t gfp, void *expected) 28546f65ec1SHugh Dickins { 286b065b432SHugh Dickins int error; 28746f65ec1SHugh Dickins 28846f65ec1SHugh Dickins VM_BUG_ON(!PageLocked(page)); 28946f65ec1SHugh Dickins VM_BUG_ON(!PageSwapBacked(page)); 29046f65ec1SHugh Dickins 29146f65ec1SHugh Dickins page_cache_get(page); 29246f65ec1SHugh Dickins page->mapping = mapping; 29346f65ec1SHugh Dickins page->index = index; 29446f65ec1SHugh Dickins 29546f65ec1SHugh Dickins spin_lock_irq(&mapping->tree_lock); 29646f65ec1SHugh Dickins if (!expected) 297b065b432SHugh Dickins error = radix_tree_insert(&mapping->page_tree, index, page); 29846f65ec1SHugh Dickins else 299b065b432SHugh Dickins error = shmem_radix_tree_replace(mapping, index, expected, 300b065b432SHugh Dickins page); 30146f65ec1SHugh Dickins if (!error) { 30246f65ec1SHugh Dickins mapping->nrpages++; 30346f65ec1SHugh Dickins __inc_zone_page_state(page, NR_FILE_PAGES); 30446f65ec1SHugh Dickins __inc_zone_page_state(page, NR_SHMEM); 30546f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 30646f65ec1SHugh Dickins } else { 30746f65ec1SHugh Dickins page->mapping = NULL; 30846f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 30946f65ec1SHugh Dickins page_cache_release(page); 31046f65ec1SHugh Dickins } 31146f65ec1SHugh Dickins return error; 31246f65ec1SHugh Dickins } 31346f65ec1SHugh Dickins 31446f65ec1SHugh Dickins /* 3156922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 3166922c0c7SHugh Dickins */ 3176922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 3186922c0c7SHugh Dickins { 3196922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 3206922c0c7SHugh Dickins int error; 3216922c0c7SHugh Dickins 3226922c0c7SHugh Dickins spin_lock_irq(&mapping->tree_lock); 3236922c0c7SHugh Dickins error = shmem_radix_tree_replace(mapping, page->index, page, radswap); 3246922c0c7SHugh Dickins page->mapping = NULL; 3256922c0c7SHugh Dickins mapping->nrpages--; 3266922c0c7SHugh Dickins __dec_zone_page_state(page, NR_FILE_PAGES); 3276922c0c7SHugh Dickins __dec_zone_page_state(page, NR_SHMEM); 3286922c0c7SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 3296922c0c7SHugh Dickins page_cache_release(page); 3306922c0c7SHugh Dickins BUG_ON(error); 3316922c0c7SHugh Dickins } 3326922c0c7SHugh Dickins 3336922c0c7SHugh Dickins /* 3347a5d0fbbSHugh Dickins * Like find_get_pages, but collecting swap entries as well as pages. 3357a5d0fbbSHugh Dickins */ 3367a5d0fbbSHugh Dickins static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping, 3377a5d0fbbSHugh Dickins pgoff_t start, unsigned int nr_pages, 3387a5d0fbbSHugh Dickins struct page **pages, pgoff_t *indices) 3397a5d0fbbSHugh Dickins { 340860f2759SJohannes Weiner void **slot; 341860f2759SJohannes Weiner unsigned int ret = 0; 342860f2759SJohannes Weiner struct radix_tree_iter iter; 343860f2759SJohannes Weiner 344860f2759SJohannes Weiner if (!nr_pages) 345860f2759SJohannes Weiner return 0; 3467a5d0fbbSHugh Dickins 3477a5d0fbbSHugh Dickins rcu_read_lock(); 3487a5d0fbbSHugh Dickins restart: 349860f2759SJohannes Weiner radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { 3507a5d0fbbSHugh Dickins struct page *page; 3517a5d0fbbSHugh Dickins repeat: 352860f2759SJohannes Weiner page = radix_tree_deref_slot(slot); 3537a5d0fbbSHugh Dickins if (unlikely(!page)) 3547a5d0fbbSHugh Dickins continue; 3557a5d0fbbSHugh Dickins if (radix_tree_exception(page)) { 3568079b1c8SHugh Dickins if (radix_tree_deref_retry(page)) 3577a5d0fbbSHugh Dickins goto restart; 3588079b1c8SHugh Dickins /* 3598079b1c8SHugh Dickins * Otherwise, we must be storing a swap entry 3608079b1c8SHugh Dickins * here as an exceptional entry: so return it 3618079b1c8SHugh Dickins * without attempting to raise page count. 3628079b1c8SHugh Dickins */ 3638079b1c8SHugh Dickins goto export; 3647a5d0fbbSHugh Dickins } 3657a5d0fbbSHugh Dickins if (!page_cache_get_speculative(page)) 3667a5d0fbbSHugh Dickins goto repeat; 3677a5d0fbbSHugh Dickins 3687a5d0fbbSHugh Dickins /* Has the page moved? */ 369860f2759SJohannes Weiner if (unlikely(page != *slot)) { 3707a5d0fbbSHugh Dickins page_cache_release(page); 3717a5d0fbbSHugh Dickins goto repeat; 3727a5d0fbbSHugh Dickins } 3737a5d0fbbSHugh Dickins export: 374860f2759SJohannes Weiner indices[ret] = iter.index; 3757a5d0fbbSHugh Dickins pages[ret] = page; 376860f2759SJohannes Weiner if (++ret == nr_pages) 377860f2759SJohannes Weiner break; 3787a5d0fbbSHugh Dickins } 3797a5d0fbbSHugh Dickins rcu_read_unlock(); 3807a5d0fbbSHugh Dickins return ret; 3817a5d0fbbSHugh Dickins } 3827a5d0fbbSHugh Dickins 3837a5d0fbbSHugh Dickins /* 3847a5d0fbbSHugh Dickins * Remove swap entry from radix tree, free the swap and its page cache. 3857a5d0fbbSHugh Dickins */ 3867a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 3877a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 3887a5d0fbbSHugh Dickins { 3897a5d0fbbSHugh Dickins int error; 3907a5d0fbbSHugh Dickins 3917a5d0fbbSHugh Dickins spin_lock_irq(&mapping->tree_lock); 3927a5d0fbbSHugh Dickins error = shmem_radix_tree_replace(mapping, index, radswap, NULL); 3937a5d0fbbSHugh Dickins spin_unlock_irq(&mapping->tree_lock); 3947a5d0fbbSHugh Dickins if (!error) 3957a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 3967a5d0fbbSHugh Dickins return error; 3977a5d0fbbSHugh Dickins } 3987a5d0fbbSHugh Dickins 3997a5d0fbbSHugh Dickins /* 4007a5d0fbbSHugh Dickins * Pagevec may contain swap entries, so shuffle up pages before releasing. 4017a5d0fbbSHugh Dickins */ 40224513264SHugh Dickins static void shmem_deswap_pagevec(struct pagevec *pvec) 4037a5d0fbbSHugh Dickins { 4047a5d0fbbSHugh Dickins int i, j; 4057a5d0fbbSHugh Dickins 4067a5d0fbbSHugh Dickins for (i = 0, j = 0; i < pagevec_count(pvec); i++) { 4077a5d0fbbSHugh Dickins struct page *page = pvec->pages[i]; 4087a5d0fbbSHugh Dickins if (!radix_tree_exceptional_entry(page)) 4097a5d0fbbSHugh Dickins pvec->pages[j++] = page; 4107a5d0fbbSHugh Dickins } 4117a5d0fbbSHugh Dickins pvec->nr = j; 41224513264SHugh Dickins } 41324513264SHugh Dickins 41424513264SHugh Dickins /* 41524513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 41624513264SHugh Dickins */ 41724513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 41824513264SHugh Dickins { 41924513264SHugh Dickins struct pagevec pvec; 42024513264SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 42124513264SHugh Dickins pgoff_t index = 0; 42224513264SHugh Dickins 42324513264SHugh Dickins pagevec_init(&pvec, 0); 42424513264SHugh Dickins /* 42524513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 42624513264SHugh Dickins */ 42724513264SHugh Dickins while (!mapping_unevictable(mapping)) { 42824513264SHugh Dickins /* 42924513264SHugh Dickins * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it 43024513264SHugh Dickins * has finished, if it hits a row of PAGEVEC_SIZE swap entries. 43124513264SHugh Dickins */ 43224513264SHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 43324513264SHugh Dickins PAGEVEC_SIZE, pvec.pages, indices); 43424513264SHugh Dickins if (!pvec.nr) 43524513264SHugh Dickins break; 43624513264SHugh Dickins index = indices[pvec.nr - 1] + 1; 43724513264SHugh Dickins shmem_deswap_pagevec(&pvec); 43824513264SHugh Dickins check_move_unevictable_pages(pvec.pages, pvec.nr); 43924513264SHugh Dickins pagevec_release(&pvec); 44024513264SHugh Dickins cond_resched(); 44124513264SHugh Dickins } 4427a5d0fbbSHugh Dickins } 4437a5d0fbbSHugh Dickins 4447a5d0fbbSHugh Dickins /* 4457a5d0fbbSHugh Dickins * Remove range of pages and swap entries from radix tree, and free them. 4461635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 4477a5d0fbbSHugh Dickins */ 4481635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 4491635f6a7SHugh Dickins bool unfalloc) 4501da177e4SLinus Torvalds { 451285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 4521da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 453285b2c4fSHugh Dickins pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 45483e4fa9cSHugh Dickins pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT; 45583e4fa9cSHugh Dickins unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1); 45683e4fa9cSHugh Dickins unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1); 457bda97eabSHugh Dickins struct pagevec pvec; 4587a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 4597a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 460285b2c4fSHugh Dickins pgoff_t index; 461bda97eabSHugh Dickins int i; 4621da177e4SLinus Torvalds 46383e4fa9cSHugh Dickins if (lend == -1) 46483e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 465bda97eabSHugh Dickins 466bda97eabSHugh Dickins pagevec_init(&pvec, 0); 467bda97eabSHugh Dickins index = start; 46883e4fa9cSHugh Dickins while (index < end) { 4697a5d0fbbSHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 47083e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 4717a5d0fbbSHugh Dickins pvec.pages, indices); 4727a5d0fbbSHugh Dickins if (!pvec.nr) 4737a5d0fbbSHugh Dickins break; 474bda97eabSHugh Dickins mem_cgroup_uncharge_start(); 475bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 476bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 477bda97eabSHugh Dickins 4787a5d0fbbSHugh Dickins index = indices[i]; 47983e4fa9cSHugh Dickins if (index >= end) 480bda97eabSHugh Dickins break; 481bda97eabSHugh Dickins 4827a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 4831635f6a7SHugh Dickins if (unfalloc) 4841635f6a7SHugh Dickins continue; 4857a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 4867a5d0fbbSHugh Dickins index, page); 4877a5d0fbbSHugh Dickins continue; 4887a5d0fbbSHugh Dickins } 4897a5d0fbbSHugh Dickins 490bda97eabSHugh Dickins if (!trylock_page(page)) 491bda97eabSHugh Dickins continue; 4921635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 4937a5d0fbbSHugh Dickins if (page->mapping == mapping) { 4947a5d0fbbSHugh Dickins VM_BUG_ON(PageWriteback(page)); 495bda97eabSHugh Dickins truncate_inode_page(mapping, page); 4967a5d0fbbSHugh Dickins } 4971635f6a7SHugh Dickins } 498bda97eabSHugh Dickins unlock_page(page); 499bda97eabSHugh Dickins } 50024513264SHugh Dickins shmem_deswap_pagevec(&pvec); 50124513264SHugh Dickins pagevec_release(&pvec); 502bda97eabSHugh Dickins mem_cgroup_uncharge_end(); 503bda97eabSHugh Dickins cond_resched(); 504bda97eabSHugh Dickins index++; 505bda97eabSHugh Dickins } 506bda97eabSHugh Dickins 50783e4fa9cSHugh Dickins if (partial_start) { 508bda97eabSHugh Dickins struct page *page = NULL; 509bda97eabSHugh Dickins shmem_getpage(inode, start - 1, &page, SGP_READ, NULL); 510bda97eabSHugh Dickins if (page) { 51183e4fa9cSHugh Dickins unsigned int top = PAGE_CACHE_SIZE; 51283e4fa9cSHugh Dickins if (start > end) { 51383e4fa9cSHugh Dickins top = partial_end; 51483e4fa9cSHugh Dickins partial_end = 0; 51583e4fa9cSHugh Dickins } 51683e4fa9cSHugh Dickins zero_user_segment(page, partial_start, top); 517bda97eabSHugh Dickins set_page_dirty(page); 518bda97eabSHugh Dickins unlock_page(page); 519bda97eabSHugh Dickins page_cache_release(page); 520bda97eabSHugh Dickins } 521bda97eabSHugh Dickins } 52283e4fa9cSHugh Dickins if (partial_end) { 52383e4fa9cSHugh Dickins struct page *page = NULL; 52483e4fa9cSHugh Dickins shmem_getpage(inode, end, &page, SGP_READ, NULL); 52583e4fa9cSHugh Dickins if (page) { 52683e4fa9cSHugh Dickins zero_user_segment(page, 0, partial_end); 52783e4fa9cSHugh Dickins set_page_dirty(page); 52883e4fa9cSHugh Dickins unlock_page(page); 52983e4fa9cSHugh Dickins page_cache_release(page); 53083e4fa9cSHugh Dickins } 53183e4fa9cSHugh Dickins } 53283e4fa9cSHugh Dickins if (start >= end) 53383e4fa9cSHugh Dickins return; 534bda97eabSHugh Dickins 535bda97eabSHugh Dickins index = start; 536bda97eabSHugh Dickins for ( ; ; ) { 537bda97eabSHugh Dickins cond_resched(); 5387a5d0fbbSHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 53983e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 5407a5d0fbbSHugh Dickins pvec.pages, indices); 5417a5d0fbbSHugh Dickins if (!pvec.nr) { 5421635f6a7SHugh Dickins if (index == start || unfalloc) 543bda97eabSHugh Dickins break; 544bda97eabSHugh Dickins index = start; 545bda97eabSHugh Dickins continue; 546bda97eabSHugh Dickins } 5471635f6a7SHugh Dickins if ((index == start || unfalloc) && indices[0] >= end) { 54824513264SHugh Dickins shmem_deswap_pagevec(&pvec); 54924513264SHugh Dickins pagevec_release(&pvec); 550bda97eabSHugh Dickins break; 551bda97eabSHugh Dickins } 552bda97eabSHugh Dickins mem_cgroup_uncharge_start(); 553bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 554bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 555bda97eabSHugh Dickins 5567a5d0fbbSHugh Dickins index = indices[i]; 55783e4fa9cSHugh Dickins if (index >= end) 558bda97eabSHugh Dickins break; 559bda97eabSHugh Dickins 5607a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 5611635f6a7SHugh Dickins if (unfalloc) 5621635f6a7SHugh Dickins continue; 5637a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 5647a5d0fbbSHugh Dickins index, page); 5657a5d0fbbSHugh Dickins continue; 5667a5d0fbbSHugh Dickins } 5677a5d0fbbSHugh Dickins 568bda97eabSHugh Dickins lock_page(page); 5691635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 5707a5d0fbbSHugh Dickins if (page->mapping == mapping) { 5717a5d0fbbSHugh Dickins VM_BUG_ON(PageWriteback(page)); 572bda97eabSHugh Dickins truncate_inode_page(mapping, page); 5737a5d0fbbSHugh Dickins } 5741635f6a7SHugh Dickins } 575bda97eabSHugh Dickins unlock_page(page); 576bda97eabSHugh Dickins } 57724513264SHugh Dickins shmem_deswap_pagevec(&pvec); 57824513264SHugh Dickins pagevec_release(&pvec); 579bda97eabSHugh Dickins mem_cgroup_uncharge_end(); 580bda97eabSHugh Dickins index++; 581bda97eabSHugh Dickins } 58294c1e62dSHugh Dickins 5831da177e4SLinus Torvalds spin_lock(&info->lock); 5847a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 5851da177e4SLinus Torvalds shmem_recalc_inode(inode); 5861da177e4SLinus Torvalds spin_unlock(&info->lock); 5871635f6a7SHugh Dickins } 5881da177e4SLinus Torvalds 5891635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 5901635f6a7SHugh Dickins { 5911635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 592285b2c4fSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 5931da177e4SLinus Torvalds } 59494c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 5951da177e4SLinus Torvalds 59694c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 5971da177e4SLinus Torvalds { 5981da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 5991da177e4SLinus Torvalds int error; 6001da177e4SLinus Torvalds 601db78b877SChristoph Hellwig error = inode_change_ok(inode, attr); 602db78b877SChristoph Hellwig if (error) 603db78b877SChristoph Hellwig return error; 604db78b877SChristoph Hellwig 60594c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 60694c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 60794c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 6083889e6e7Snpiggin@suse.de 60994c1e62dSHugh Dickins if (newsize != oldsize) { 61094c1e62dSHugh Dickins i_size_write(inode, newsize); 61194c1e62dSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 61294c1e62dSHugh Dickins } 61394c1e62dSHugh Dickins if (newsize < oldsize) { 61494c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 61594c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 61694c1e62dSHugh Dickins shmem_truncate_range(inode, newsize, (loff_t)-1); 61794c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 61894c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 61994c1e62dSHugh Dickins } 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6226a1a90adSChristoph Hellwig setattr_copy(inode, attr); 62339f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 624db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 6251c7c474cSChristoph Hellwig error = generic_acl_chmod(inode); 62639f0247dSAndreas Gruenbacher #endif 6271da177e4SLinus Torvalds return error; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 6331da177e4SLinus Torvalds 6343889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 6351da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 6361da177e4SLinus Torvalds inode->i_size = 0; 6373889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 6381da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 639cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 6401da177e4SLinus Torvalds list_del_init(&info->swaplist); 641cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 6421da177e4SLinus Torvalds } 64369f07ec9SHugh Dickins } else 64469f07ec9SHugh Dickins kfree(info->symlink); 645b09e0fa4SEric Paris 64638f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 6470f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 6485b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 649dbd5768fSJan Kara clear_inode(inode); 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 65246f65ec1SHugh Dickins /* 65346f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 65446f65ec1SHugh Dickins */ 65541ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 656bde05d1cSHugh Dickins swp_entry_t swap, struct page **pagep) 6571da177e4SLinus Torvalds { 658285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 65946f65ec1SHugh Dickins void *radswap; 66041ffe5d5SHugh Dickins pgoff_t index; 661bde05d1cSHugh Dickins gfp_t gfp; 662bde05d1cSHugh Dickins int error = 0; 6631da177e4SLinus Torvalds 66446f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 665e504f3fdSHugh Dickins index = radix_tree_locate_item(&mapping->page_tree, radswap); 66646f65ec1SHugh Dickins if (index == -1) 6671da177e4SLinus Torvalds return 0; 6682e0e26c7SHugh Dickins 6691b1b32f2SHugh Dickins /* 6701b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 6711f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 6721b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 673285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 6741b1b32f2SHugh Dickins */ 6751b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 6762e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 6772e0e26c7SHugh Dickins 678bde05d1cSHugh Dickins gfp = mapping_gfp_mask(mapping); 679bde05d1cSHugh Dickins if (shmem_should_replace_page(*pagep, gfp)) { 680bde05d1cSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 681bde05d1cSHugh Dickins error = shmem_replace_page(pagep, gfp, info, index); 682bde05d1cSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 683bde05d1cSHugh Dickins /* 684bde05d1cSHugh Dickins * We needed to drop mutex to make that restrictive page 6850142ef6cSHugh Dickins * allocation, but the inode might have been freed while we 6860142ef6cSHugh Dickins * dropped it: although a racing shmem_evict_inode() cannot 6870142ef6cSHugh Dickins * complete without emptying the radix_tree, our page lock 6880142ef6cSHugh Dickins * on this swapcache page is not enough to prevent that - 6890142ef6cSHugh Dickins * free_swap_and_cache() of our swap entry will only 6900142ef6cSHugh Dickins * trylock_page(), removing swap from radix_tree whatever. 6910142ef6cSHugh Dickins * 6920142ef6cSHugh Dickins * We must not proceed to shmem_add_to_page_cache() if the 6930142ef6cSHugh Dickins * inode has been freed, but of course we cannot rely on 6940142ef6cSHugh Dickins * inode or mapping or info to check that. However, we can 6950142ef6cSHugh Dickins * safely check if our swap entry is still in use (and here 6960142ef6cSHugh Dickins * it can't have got reused for another page): if it's still 6970142ef6cSHugh Dickins * in use, then the inode cannot have been freed yet, and we 6980142ef6cSHugh Dickins * can safely proceed (if it's no longer in use, that tells 6990142ef6cSHugh Dickins * nothing about the inode, but we don't need to unuse swap). 700bde05d1cSHugh Dickins */ 701bde05d1cSHugh Dickins if (!page_swapcount(*pagep)) 702bde05d1cSHugh Dickins error = -ENOENT; 703bde05d1cSHugh Dickins } 704bde05d1cSHugh Dickins 705d13d1443SKAMEZAWA Hiroyuki /* 706778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 707778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 708778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 709d13d1443SKAMEZAWA Hiroyuki */ 710bde05d1cSHugh Dickins if (!error) 711bde05d1cSHugh Dickins error = shmem_add_to_page_cache(*pagep, mapping, index, 71246f65ec1SHugh Dickins GFP_NOWAIT, radswap); 71348f170fbSHugh Dickins if (error != -ENOMEM) { 71446f65ec1SHugh Dickins /* 71546f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 71646f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 71746f65ec1SHugh Dickins */ 718bde05d1cSHugh Dickins delete_from_swap_cache(*pagep); 719bde05d1cSHugh Dickins set_page_dirty(*pagep); 72046f65ec1SHugh Dickins if (!error) { 72146f65ec1SHugh Dickins spin_lock(&info->lock); 722285b2c4fSHugh Dickins info->swapped--; 72346f65ec1SHugh Dickins spin_unlock(&info->lock); 72441ffe5d5SHugh Dickins swap_free(swap); 72546f65ec1SHugh Dickins } 7262e0e26c7SHugh Dickins error = 1; /* not an error, but entry was found */ 7271da177e4SLinus Torvalds } 7282e0e26c7SHugh Dickins return error; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds /* 73246f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 7331da177e4SLinus Torvalds */ 73441ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 7351da177e4SLinus Torvalds { 73641ffe5d5SHugh Dickins struct list_head *this, *next; 7371da177e4SLinus Torvalds struct shmem_inode_info *info; 7381da177e4SLinus Torvalds int found = 0; 739bde05d1cSHugh Dickins int error = 0; 740bde05d1cSHugh Dickins 741bde05d1cSHugh Dickins /* 742bde05d1cSHugh Dickins * There's a faint possibility that swap page was replaced before 7430142ef6cSHugh Dickins * caller locked it: caller will come back later with the right page. 744bde05d1cSHugh Dickins */ 7450142ef6cSHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) 746bde05d1cSHugh Dickins goto out; 747778dd893SHugh Dickins 748778dd893SHugh Dickins /* 749778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 750778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 751778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 752778dd893SHugh Dickins */ 753778dd893SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL); 754778dd893SHugh Dickins if (error) 755778dd893SHugh Dickins goto out; 75646f65ec1SHugh Dickins /* No radix_tree_preload: swap entry keeps a place for page in tree */ 7571da177e4SLinus Torvalds 758cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 75941ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 76041ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 761285b2c4fSHugh Dickins if (info->swapped) 762bde05d1cSHugh Dickins found = shmem_unuse_inode(info, swap, &page); 7636922c0c7SHugh Dickins else 7646922c0c7SHugh Dickins list_del_init(&info->swaplist); 765cb5f7b9aSHugh Dickins cond_resched(); 7662e0e26c7SHugh Dickins if (found) 767778dd893SHugh Dickins break; 7681da177e4SLinus Torvalds } 769cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 770778dd893SHugh Dickins 771778dd893SHugh Dickins if (found < 0) 772778dd893SHugh Dickins error = found; 773778dd893SHugh Dickins out: 774aaa46865SHugh Dickins unlock_page(page); 775aaa46865SHugh Dickins page_cache_release(page); 776778dd893SHugh Dickins return error; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds /* 7801da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 7811da177e4SLinus Torvalds */ 7821da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 7831da177e4SLinus Torvalds { 7841da177e4SLinus Torvalds struct shmem_inode_info *info; 7851da177e4SLinus Torvalds struct address_space *mapping; 7861da177e4SLinus Torvalds struct inode *inode; 7876922c0c7SHugh Dickins swp_entry_t swap; 7886922c0c7SHugh Dickins pgoff_t index; 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 7911da177e4SLinus Torvalds mapping = page->mapping; 7921da177e4SLinus Torvalds index = page->index; 7931da177e4SLinus Torvalds inode = mapping->host; 7941da177e4SLinus Torvalds info = SHMEM_I(inode); 7951da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 7961da177e4SLinus Torvalds goto redirty; 797d9fe526aSHugh Dickins if (!total_swap_pages) 7981da177e4SLinus Torvalds goto redirty; 7991da177e4SLinus Torvalds 800d9fe526aSHugh Dickins /* 801d9fe526aSHugh Dickins * shmem_backing_dev_info's capabilities prevent regular writeback or 802d9fe526aSHugh Dickins * sync from ever calling shmem_writepage; but a stacking filesystem 80348f170fbSHugh Dickins * might use ->writepage of its underlying filesystem, in which case 804d9fe526aSHugh Dickins * tmpfs should write out to swap only in response to memory pressure, 80548f170fbSHugh Dickins * and not for the writeback threads or sync. 806d9fe526aSHugh Dickins */ 80748f170fbSHugh Dickins if (!wbc->for_reclaim) { 80848f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 80948f170fbSHugh Dickins goto redirty; 81048f170fbSHugh Dickins } 8111635f6a7SHugh Dickins 8121635f6a7SHugh Dickins /* 8131635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 8141635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 8151635f6a7SHugh Dickins * fallocated page arriving here is now to initialize it and write it. 8161aac1400SHugh Dickins * 8171aac1400SHugh Dickins * That's okay for a page already fallocated earlier, but if we have 8181aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 8191aac1400SHugh Dickins * of this page in case we have to undo it, and (b) it may not be a 8201aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 8211aac1400SHugh Dickins * reactivate the page, and let shmem_fallocate() quit when too many. 8221635f6a7SHugh Dickins */ 8231635f6a7SHugh Dickins if (!PageUptodate(page)) { 8241aac1400SHugh Dickins if (inode->i_private) { 8251aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 8261aac1400SHugh Dickins spin_lock(&inode->i_lock); 8271aac1400SHugh Dickins shmem_falloc = inode->i_private; 8281aac1400SHugh Dickins if (shmem_falloc && 8291aac1400SHugh Dickins index >= shmem_falloc->start && 8301aac1400SHugh Dickins index < shmem_falloc->next) 8311aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 8321aac1400SHugh Dickins else 8331aac1400SHugh Dickins shmem_falloc = NULL; 8341aac1400SHugh Dickins spin_unlock(&inode->i_lock); 8351aac1400SHugh Dickins if (shmem_falloc) 8361aac1400SHugh Dickins goto redirty; 8371aac1400SHugh Dickins } 8381635f6a7SHugh Dickins clear_highpage(page); 8391635f6a7SHugh Dickins flush_dcache_page(page); 8401635f6a7SHugh Dickins SetPageUptodate(page); 8411635f6a7SHugh Dickins } 8421635f6a7SHugh Dickins 843d9fe526aSHugh Dickins swap = get_swap_page(); 84448f170fbSHugh Dickins if (!swap.val) 84548f170fbSHugh Dickins goto redirty; 846d9fe526aSHugh Dickins 847b1dea800SHugh Dickins /* 848b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 8496922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 8506922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 851b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 8526922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 8536922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 854b1dea800SHugh Dickins */ 855b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 85605bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 85705bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 858b1dea800SHugh Dickins 85948f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 860aaa46865SHugh Dickins swap_shmem_alloc(swap); 8616922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 8626922c0c7SHugh Dickins 8636922c0c7SHugh Dickins spin_lock(&info->lock); 8646922c0c7SHugh Dickins info->swapped++; 8656922c0c7SHugh Dickins shmem_recalc_inode(inode); 866826267cfSHugh Dickins spin_unlock(&info->lock); 8676922c0c7SHugh Dickins 8686922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 869d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 8709fab5619SHugh Dickins swap_writepage(page, wbc); 8711da177e4SLinus Torvalds return 0; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds 8746922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 875cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, NULL); 8761da177e4SLinus Torvalds redirty: 8771da177e4SLinus Torvalds set_page_dirty(page); 878d9fe526aSHugh Dickins if (wbc->for_reclaim) 879d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 880d9fe526aSHugh Dickins unlock_page(page); 881d9fe526aSHugh Dickins return 0; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds #ifdef CONFIG_NUMA 885680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 88671fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 887680d794bSakpm@linux-foundation.org { 888680d794bSakpm@linux-foundation.org char buffer[64]; 889680d794bSakpm@linux-foundation.org 89071fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 891095f1fc4SLee Schermerhorn return; /* show nothing */ 892095f1fc4SLee Schermerhorn 893a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 894095f1fc4SLee Schermerhorn 895095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 896680d794bSakpm@linux-foundation.org } 89771fe804bSLee Schermerhorn 89871fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 89971fe804bSLee Schermerhorn { 90071fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 90171fe804bSLee Schermerhorn if (sbinfo->mpol) { 90271fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 90371fe804bSLee Schermerhorn mpol = sbinfo->mpol; 90471fe804bSLee Schermerhorn mpol_get(mpol); 90571fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 90671fe804bSLee Schermerhorn } 90771fe804bSLee Schermerhorn return mpol; 90871fe804bSLee Schermerhorn } 909680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 910680d794bSakpm@linux-foundation.org 91141ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 91241ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 9131da177e4SLinus Torvalds { 9141da177e4SLinus Torvalds struct vm_area_struct pvma; 91518a2f371SMel Gorman struct page *page; 9161da177e4SLinus Torvalds 917c4cc6d07SHugh Dickins /* Create a pseudo vma that just contains the policy */ 918c4cc6d07SHugh Dickins pvma.vm_start = 0; 91909c231cbSNathan Zimmer /* Bias interleave by inode number to distribute better across nodes */ 92009c231cbSNathan Zimmer pvma.vm_pgoff = index + info->vfs_inode.i_ino; 921c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 92241ffe5d5SHugh Dickins pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 92352cd3b07SLee Schermerhorn 92418a2f371SMel Gorman page = swapin_readahead(swap, gfp, &pvma, 0); 92518a2f371SMel Gorman 92618a2f371SMel Gorman /* Drop reference taken by mpol_shared_policy_lookup() */ 92718a2f371SMel Gorman mpol_cond_put(pvma.vm_policy); 92818a2f371SMel Gorman 92918a2f371SMel Gorman return page; 93018a2f371SMel Gorman } 93118a2f371SMel Gorman 93218a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp, 93318a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 93418a2f371SMel Gorman { 93518a2f371SMel Gorman struct vm_area_struct pvma; 93618a2f371SMel Gorman struct page *page; 93718a2f371SMel Gorman 93818a2f371SMel Gorman /* Create a pseudo vma that just contains the policy */ 93918a2f371SMel Gorman pvma.vm_start = 0; 94018a2f371SMel Gorman /* Bias interleave by inode number to distribute better across nodes */ 94118a2f371SMel Gorman pvma.vm_pgoff = index + info->vfs_inode.i_ino; 94218a2f371SMel Gorman pvma.vm_ops = NULL; 94318a2f371SMel Gorman pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 94418a2f371SMel Gorman 94518a2f371SMel Gorman page = alloc_page_vma(gfp, &pvma, 0); 94618a2f371SMel Gorman 94718a2f371SMel Gorman /* Drop reference taken by mpol_shared_policy_lookup() */ 94818a2f371SMel Gorman mpol_cond_put(pvma.vm_policy); 94918a2f371SMel Gorman 95018a2f371SMel Gorman return page; 9511da177e4SLinus Torvalds } 952680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */ 953680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 95441ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 955680d794bSakpm@linux-foundation.org { 956680d794bSakpm@linux-foundation.org } 957680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 958680d794bSakpm@linux-foundation.org 95941ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 96041ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 9611da177e4SLinus Torvalds { 96241ffe5d5SHugh Dickins return swapin_readahead(swap, gfp, NULL, 0); 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds 96502098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp, 96641ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 9671da177e4SLinus Torvalds { 968e84e2e13SHugh Dickins return alloc_page(gfp); 9691da177e4SLinus Torvalds } 970680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */ 9711da177e4SLinus Torvalds 97271fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) 97371fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 97471fe804bSLee Schermerhorn { 97571fe804bSLee Schermerhorn return NULL; 97671fe804bSLee Schermerhorn } 97771fe804bSLee Schermerhorn #endif 97871fe804bSLee Schermerhorn 9791da177e4SLinus Torvalds /* 980bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 981bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 982bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 983bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 984bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 985bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 986bde05d1cSHugh Dickins * 987bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 988bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 989bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 990bde05d1cSHugh Dickins */ 991bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp) 992bde05d1cSHugh Dickins { 993bde05d1cSHugh Dickins return page_zonenum(page) > gfp_zone(gfp); 994bde05d1cSHugh Dickins } 995bde05d1cSHugh Dickins 996bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 997bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 998bde05d1cSHugh Dickins { 999bde05d1cSHugh Dickins struct page *oldpage, *newpage; 1000bde05d1cSHugh Dickins struct address_space *swap_mapping; 1001bde05d1cSHugh Dickins pgoff_t swap_index; 1002bde05d1cSHugh Dickins int error; 1003bde05d1cSHugh Dickins 1004bde05d1cSHugh Dickins oldpage = *pagep; 1005bde05d1cSHugh Dickins swap_index = page_private(oldpage); 1006bde05d1cSHugh Dickins swap_mapping = page_mapping(oldpage); 1007bde05d1cSHugh Dickins 1008bde05d1cSHugh Dickins /* 1009bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1010bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1011bde05d1cSHugh Dickins */ 1012bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1013bde05d1cSHugh Dickins newpage = shmem_alloc_page(gfp, info, index); 1014bde05d1cSHugh Dickins if (!newpage) 1015bde05d1cSHugh Dickins return -ENOMEM; 1016bde05d1cSHugh Dickins 1017bde05d1cSHugh Dickins page_cache_get(newpage); 1018bde05d1cSHugh Dickins copy_highpage(newpage, oldpage); 10190142ef6cSHugh Dickins flush_dcache_page(newpage); 1020bde05d1cSHugh Dickins 1021bde05d1cSHugh Dickins __set_page_locked(newpage); 1022bde05d1cSHugh Dickins SetPageUptodate(newpage); 1023bde05d1cSHugh Dickins SetPageSwapBacked(newpage); 1024bde05d1cSHugh Dickins set_page_private(newpage, swap_index); 1025bde05d1cSHugh Dickins SetPageSwapCache(newpage); 1026bde05d1cSHugh Dickins 1027bde05d1cSHugh Dickins /* 1028bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1029bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1030bde05d1cSHugh Dickins */ 1031bde05d1cSHugh Dickins spin_lock_irq(&swap_mapping->tree_lock); 1032bde05d1cSHugh Dickins error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage, 1033bde05d1cSHugh Dickins newpage); 10340142ef6cSHugh Dickins if (!error) { 1035bde05d1cSHugh Dickins __inc_zone_page_state(newpage, NR_FILE_PAGES); 1036bde05d1cSHugh Dickins __dec_zone_page_state(oldpage, NR_FILE_PAGES); 10370142ef6cSHugh Dickins } 1038bde05d1cSHugh Dickins spin_unlock_irq(&swap_mapping->tree_lock); 1039bde05d1cSHugh Dickins 10400142ef6cSHugh Dickins if (unlikely(error)) { 10410142ef6cSHugh Dickins /* 10420142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 10430142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 10440142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 10450142ef6cSHugh Dickins */ 10460142ef6cSHugh Dickins oldpage = newpage; 10470142ef6cSHugh Dickins } else { 1048bde05d1cSHugh Dickins mem_cgroup_replace_page_cache(oldpage, newpage); 1049bde05d1cSHugh Dickins lru_cache_add_anon(newpage); 10500142ef6cSHugh Dickins *pagep = newpage; 10510142ef6cSHugh Dickins } 1052bde05d1cSHugh Dickins 1053bde05d1cSHugh Dickins ClearPageSwapCache(oldpage); 1054bde05d1cSHugh Dickins set_page_private(oldpage, 0); 1055bde05d1cSHugh Dickins 1056bde05d1cSHugh Dickins unlock_page(oldpage); 1057bde05d1cSHugh Dickins page_cache_release(oldpage); 1058bde05d1cSHugh Dickins page_cache_release(oldpage); 10590142ef6cSHugh Dickins return error; 1060bde05d1cSHugh Dickins } 1061bde05d1cSHugh Dickins 1062bde05d1cSHugh Dickins /* 106368da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 10641da177e4SLinus Torvalds * 10651da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 10661da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 10671da177e4SLinus Torvalds * entry since a page cannot live in both the swap and page cache 10681da177e4SLinus Torvalds */ 106941ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 107068da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) 10711da177e4SLinus Torvalds { 10721da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 107354af6042SHugh Dickins struct shmem_inode_info *info; 10741da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 107527ab7006SHugh Dickins struct page *page; 10761da177e4SLinus Torvalds swp_entry_t swap; 10771da177e4SLinus Torvalds int error; 107854af6042SHugh Dickins int once = 0; 10791635f6a7SHugh Dickins int alloced = 0; 10801da177e4SLinus Torvalds 108141ffe5d5SHugh Dickins if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT)) 10821da177e4SLinus Torvalds return -EFBIG; 10831da177e4SLinus Torvalds repeat: 108454af6042SHugh Dickins swap.val = 0; 108541ffe5d5SHugh Dickins page = find_lock_page(mapping, index); 108654af6042SHugh Dickins if (radix_tree_exceptional_entry(page)) { 108754af6042SHugh Dickins swap = radix_to_swp_entry(page); 108854af6042SHugh Dickins page = NULL; 108954af6042SHugh Dickins } 109054af6042SHugh Dickins 10911635f6a7SHugh Dickins if (sgp != SGP_WRITE && sgp != SGP_FALLOC && 109254af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 109354af6042SHugh Dickins error = -EINVAL; 109454af6042SHugh Dickins goto failed; 109554af6042SHugh Dickins } 109654af6042SHugh Dickins 10971635f6a7SHugh Dickins /* fallocated page? */ 10981635f6a7SHugh Dickins if (page && !PageUptodate(page)) { 10991635f6a7SHugh Dickins if (sgp != SGP_READ) 11001635f6a7SHugh Dickins goto clear; 11011635f6a7SHugh Dickins unlock_page(page); 11021635f6a7SHugh Dickins page_cache_release(page); 11031635f6a7SHugh Dickins page = NULL; 11041635f6a7SHugh Dickins } 110554af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 110654af6042SHugh Dickins *pagep = page; 110754af6042SHugh Dickins return 0; 110827ab7006SHugh Dickins } 110927ab7006SHugh Dickins 1110b409f9fcSHugh Dickins /* 111154af6042SHugh Dickins * Fast cache lookup did not find it: 111254af6042SHugh Dickins * bring it back from swap or allocate. 1113b409f9fcSHugh Dickins */ 111454af6042SHugh Dickins info = SHMEM_I(inode); 111554af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 111627ab7006SHugh Dickins 11171da177e4SLinus Torvalds if (swap.val) { 11181da177e4SLinus Torvalds /* Look it up and read it in.. */ 111927ab7006SHugh Dickins page = lookup_swap_cache(swap); 112027ab7006SHugh Dickins if (!page) { 1121456f998eSYing Han /* here we actually do the io */ 112268da9f05SHugh Dickins if (fault_type) 112368da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 112441ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 112527ab7006SHugh Dickins if (!page) { 11261da177e4SLinus Torvalds error = -ENOMEM; 112754af6042SHugh Dickins goto failed; 1128285b2c4fSHugh Dickins } 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 113254af6042SHugh Dickins lock_page(page); 11330142ef6cSHugh Dickins if (!PageSwapCache(page) || page_private(page) != swap.val || 1134d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1135bde05d1cSHugh Dickins error = -EEXIST; /* try again */ 1136d1899228SHugh Dickins goto unlock; 1137bde05d1cSHugh Dickins } 113827ab7006SHugh Dickins if (!PageUptodate(page)) { 11391da177e4SLinus Torvalds error = -EIO; 114054af6042SHugh Dickins goto failed; 114154af6042SHugh Dickins } 114254af6042SHugh Dickins wait_on_page_writeback(page); 114354af6042SHugh Dickins 1144bde05d1cSHugh Dickins if (shmem_should_replace_page(page, gfp)) { 1145bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 1146bde05d1cSHugh Dickins if (error) 114754af6042SHugh Dickins goto failed; 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds 1150aa3b1895SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, 1151aa3b1895SHugh Dickins gfp & GFP_RECLAIM_MASK); 1152d1899228SHugh Dickins if (!error) { 115354af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 115454af6042SHugh Dickins gfp, swp_to_radix_entry(swap)); 1155215c02bcSHugh Dickins /* 1156215c02bcSHugh Dickins * We already confirmed swap under page lock, and make 1157215c02bcSHugh Dickins * no memory allocation here, so usually no possibility 1158215c02bcSHugh Dickins * of error; but free_swap_and_cache() only trylocks a 1159215c02bcSHugh Dickins * page, so it is just possible that the entry has been 1160215c02bcSHugh Dickins * truncated or holepunched since swap was confirmed. 1161215c02bcSHugh Dickins * shmem_undo_range() will have done some of the 1162215c02bcSHugh Dickins * unaccounting, now delete_from_swap_cache() will do 1163215c02bcSHugh Dickins * the rest (including mem_cgroup_uncharge_swapcache). 1164215c02bcSHugh Dickins * Reset swap.val? No, leave it so "failed" goes back to 1165215c02bcSHugh Dickins * "repeat": reading a hole and writing should succeed. 1166215c02bcSHugh Dickins */ 1167215c02bcSHugh Dickins if (error) 1168215c02bcSHugh Dickins delete_from_swap_cache(page); 1169d1899228SHugh Dickins } 117054af6042SHugh Dickins if (error) 117154af6042SHugh Dickins goto failed; 117254af6042SHugh Dickins 117354af6042SHugh Dickins spin_lock(&info->lock); 117454af6042SHugh Dickins info->swapped--; 117554af6042SHugh Dickins shmem_recalc_inode(inode); 11761da177e4SLinus Torvalds spin_unlock(&info->lock); 117727ab7006SHugh Dickins 117827ab7006SHugh Dickins delete_from_swap_cache(page); 117927ab7006SHugh Dickins set_page_dirty(page); 118027ab7006SHugh Dickins swap_free(swap); 118127ab7006SHugh Dickins 118254af6042SHugh Dickins } else { 118354af6042SHugh Dickins if (shmem_acct_block(info->flags)) { 118454af6042SHugh Dickins error = -ENOSPC; 118554af6042SHugh Dickins goto failed; 11861da177e4SLinus Torvalds } 11870edd73b3SHugh Dickins if (sbinfo->max_blocks) { 1188fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 118954af6042SHugh Dickins sbinfo->max_blocks) >= 0) { 119054af6042SHugh Dickins error = -ENOSPC; 119154af6042SHugh Dickins goto unacct; 119254af6042SHugh Dickins } 11937e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 119459a16eadSHugh Dickins } 11951da177e4SLinus Torvalds 119654af6042SHugh Dickins page = shmem_alloc_page(gfp, info, index); 119754af6042SHugh Dickins if (!page) { 119854af6042SHugh Dickins error = -ENOMEM; 119954af6042SHugh Dickins goto decused; 120054af6042SHugh Dickins } 120154af6042SHugh Dickins 120254af6042SHugh Dickins SetPageSwapBacked(page); 120354af6042SHugh Dickins __set_page_locked(page); 1204aa3b1895SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, 1205aa3b1895SHugh Dickins gfp & GFP_RECLAIM_MASK); 120654af6042SHugh Dickins if (error) 120754af6042SHugh Dickins goto decused; 1208b065b432SHugh Dickins error = radix_tree_preload(gfp & GFP_RECLAIM_MASK); 1209b065b432SHugh Dickins if (!error) { 1210b065b432SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1211b065b432SHugh Dickins gfp, NULL); 1212b065b432SHugh Dickins radix_tree_preload_end(); 1213b065b432SHugh Dickins } 1214b065b432SHugh Dickins if (error) { 1215b065b432SHugh Dickins mem_cgroup_uncharge_cache_page(page); 1216b065b432SHugh Dickins goto decused; 1217b065b432SHugh Dickins } 121854af6042SHugh Dickins lru_cache_add_anon(page); 121954af6042SHugh Dickins 122054af6042SHugh Dickins spin_lock(&info->lock); 12211da177e4SLinus Torvalds info->alloced++; 122254af6042SHugh Dickins inode->i_blocks += BLOCKS_PER_PAGE; 122354af6042SHugh Dickins shmem_recalc_inode(inode); 122459a16eadSHugh Dickins spin_unlock(&info->lock); 12251635f6a7SHugh Dickins alloced = true; 122654af6042SHugh Dickins 1227ec9516fbSHugh Dickins /* 12281635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 12291635f6a7SHugh Dickins */ 12301635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 12311635f6a7SHugh Dickins sgp = SGP_WRITE; 12321635f6a7SHugh Dickins clear: 12331635f6a7SHugh Dickins /* 12341635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 12351635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 12361635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1237ec9516fbSHugh Dickins */ 1238ec9516fbSHugh Dickins if (sgp != SGP_WRITE) { 123927ab7006SHugh Dickins clear_highpage(page); 124027ab7006SHugh Dickins flush_dcache_page(page); 124127ab7006SHugh Dickins SetPageUptodate(page); 1242ec9516fbSHugh Dickins } 1243a0ee5ec5SHugh Dickins if (sgp == SGP_DIRTY) 124427ab7006SHugh Dickins set_page_dirty(page); 12451da177e4SLinus Torvalds } 1246bde05d1cSHugh Dickins 124754af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 12481635f6a7SHugh Dickins if (sgp != SGP_WRITE && sgp != SGP_FALLOC && 124954af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 125054af6042SHugh Dickins error = -EINVAL; 12511635f6a7SHugh Dickins if (alloced) 125254af6042SHugh Dickins goto trunc; 12531635f6a7SHugh Dickins else 12541635f6a7SHugh Dickins goto failed; 1255ff36b801SShaohua Li } 125654af6042SHugh Dickins *pagep = page; 125754af6042SHugh Dickins return 0; 1258d00806b1SNick Piggin 1259d0217ac0SNick Piggin /* 126054af6042SHugh Dickins * Error recovery. 12611da177e4SLinus Torvalds */ 126254af6042SHugh Dickins trunc: 12631635f6a7SHugh Dickins info = SHMEM_I(inode); 126454af6042SHugh Dickins ClearPageDirty(page); 126554af6042SHugh Dickins delete_from_page_cache(page); 126654af6042SHugh Dickins spin_lock(&info->lock); 126754af6042SHugh Dickins info->alloced--; 126854af6042SHugh Dickins inode->i_blocks -= BLOCKS_PER_PAGE; 12691da177e4SLinus Torvalds spin_unlock(&info->lock); 127054af6042SHugh Dickins decused: 12711635f6a7SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 127254af6042SHugh Dickins if (sbinfo->max_blocks) 127354af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -1); 127454af6042SHugh Dickins unacct: 127554af6042SHugh Dickins shmem_unacct_blocks(info->flags, 1); 127654af6042SHugh Dickins failed: 1277d1899228SHugh Dickins if (swap.val && error != -EINVAL && 1278d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) 127954af6042SHugh Dickins error = -EEXIST; 1280d1899228SHugh Dickins unlock: 128127ab7006SHugh Dickins if (page) { 128254af6042SHugh Dickins unlock_page(page); 12831da177e4SLinus Torvalds page_cache_release(page); 128454af6042SHugh Dickins } 128554af6042SHugh Dickins if (error == -ENOSPC && !once++) { 128654af6042SHugh Dickins info = SHMEM_I(inode); 128754af6042SHugh Dickins spin_lock(&info->lock); 128854af6042SHugh Dickins shmem_recalc_inode(inode); 128954af6042SHugh Dickins spin_unlock(&info->lock); 12901da177e4SLinus Torvalds goto repeat; 1291d8dc74f2SAdrian Bunk } 1292d1899228SHugh Dickins if (error == -EEXIST) /* from above or from radix_tree_insert */ 129354af6042SHugh Dickins goto repeat; 129454af6042SHugh Dickins return error; 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 12981da177e4SLinus Torvalds { 1299496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 13001da177e4SLinus Torvalds int error; 130168da9f05SHugh Dickins int ret = VM_FAULT_LOCKED; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); 13041da177e4SLinus Torvalds if (error) 13051da177e4SLinus Torvalds return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 130668da9f05SHugh Dickins 1307456f998eSYing Han if (ret & VM_FAULT_MAJOR) { 1308456f998eSYing Han count_vm_event(PGMAJFAULT); 1309456f998eSYing Han mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 1310456f998eSYing Han } 131168da9f05SHugh Dickins return ret; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds #ifdef CONFIG_NUMA 131541ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 13161da177e4SLinus Torvalds { 1317496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 131841ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 1321d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 1322d8dc74f2SAdrian Bunk unsigned long addr) 13231da177e4SLinus Torvalds { 1324496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 132541ffe5d5SHugh Dickins pgoff_t index; 13261da177e4SLinus Torvalds 132741ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 132841ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds #endif 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 13331da177e4SLinus Torvalds { 1334496ad9aaSAl Viro struct inode *inode = file_inode(file); 13351da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 13361da177e4SLinus Torvalds int retval = -ENOMEM; 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds spin_lock(&info->lock); 13391da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 13401da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 13411da177e4SLinus Torvalds goto out_nomem; 13421da177e4SLinus Torvalds info->flags |= VM_LOCKED; 134389e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 13461da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 13471da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 134889e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds retval = 0; 135189e004eaSLee Schermerhorn 13521da177e4SLinus Torvalds out_nomem: 13531da177e4SLinus Torvalds spin_unlock(&info->lock); 13541da177e4SLinus Torvalds return retval; 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds 13579b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 13581da177e4SLinus Torvalds { 13591da177e4SLinus Torvalds file_accessed(file); 13601da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 13611da177e4SLinus Torvalds return 0; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds 1364454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 136509208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 13661da177e4SLinus Torvalds { 13671da177e4SLinus Torvalds struct inode *inode; 13681da177e4SLinus Torvalds struct shmem_inode_info *info; 13691da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 13701da177e4SLinus Torvalds 13715b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 13721da177e4SLinus Torvalds return NULL; 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds inode = new_inode(sb); 13751da177e4SLinus Torvalds if (inode) { 137685fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 1377454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 13781da177e4SLinus Torvalds inode->i_blocks = 0; 13791da177e4SLinus Torvalds inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; 13801da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 138191828a40SDavid M. Grimes inode->i_generation = get_seconds(); 13821da177e4SLinus Torvalds info = SHMEM_I(inode); 13831da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 13841da177e4SLinus Torvalds spin_lock_init(&info->lock); 13850b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 13861da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 138738f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 138872c04902SAl Viro cache_no_acl(inode); 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds switch (mode & S_IFMT) { 13911da177e4SLinus Torvalds default: 139239f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 13931da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 13941da177e4SLinus Torvalds break; 13951da177e4SLinus Torvalds case S_IFREG: 139614fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 13971da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 13981da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 139971fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 140071fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 14011da177e4SLinus Torvalds break; 14021da177e4SLinus Torvalds case S_IFDIR: 1403d8c76e6fSDave Hansen inc_nlink(inode); 14041da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 14051da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 14061da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 14071da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 14081da177e4SLinus Torvalds break; 14091da177e4SLinus Torvalds case S_IFLNK: 14101da177e4SLinus Torvalds /* 14111da177e4SLinus Torvalds * Must not load anything in the rbtree, 14121da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 14131da177e4SLinus Torvalds */ 141471fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 14151da177e4SLinus Torvalds break; 14161da177e4SLinus Torvalds } 14175b04c689SPavel Emelyanov } else 14185b04c689SPavel Emelyanov shmem_free_inode(sb); 14191da177e4SLinus Torvalds return inode; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 142392e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 142469f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 14251da177e4SLinus Torvalds 14266d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR 14276d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 14286d9d88d0SJarkko Sakkinen #else 14296d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL 14306d9d88d0SJarkko Sakkinen #endif 14316d9d88d0SJarkko Sakkinen 14321da177e4SLinus Torvalds static int 1433800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 1434800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 1435800d15a5SNick Piggin struct page **pagep, void **fsdata) 14361da177e4SLinus Torvalds { 1437800d15a5SNick Piggin struct inode *inode = mapping->host; 1438800d15a5SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 1439800d15a5SNick Piggin return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); 1440800d15a5SNick Piggin } 1441800d15a5SNick Piggin 1442800d15a5SNick Piggin static int 1443800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 1444800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 1445800d15a5SNick Piggin struct page *page, void *fsdata) 1446800d15a5SNick Piggin { 1447800d15a5SNick Piggin struct inode *inode = mapping->host; 1448800d15a5SNick Piggin 1449800d15a5SNick Piggin if (pos + copied > inode->i_size) 1450800d15a5SNick Piggin i_size_write(inode, pos + copied); 1451800d15a5SNick Piggin 1452ec9516fbSHugh Dickins if (!PageUptodate(page)) { 1453ec9516fbSHugh Dickins if (copied < PAGE_CACHE_SIZE) { 1454ec9516fbSHugh Dickins unsigned from = pos & (PAGE_CACHE_SIZE - 1); 1455ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 1456ec9516fbSHugh Dickins from + copied, PAGE_CACHE_SIZE); 1457ec9516fbSHugh Dickins } 1458ec9516fbSHugh Dickins SetPageUptodate(page); 1459ec9516fbSHugh Dickins } 1460d3602444SHugh Dickins set_page_dirty(page); 14616746aff7SWu Fengguang unlock_page(page); 1462d3602444SHugh Dickins page_cache_release(page); 1463d3602444SHugh Dickins 1464800d15a5SNick Piggin return copied; 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) 14681da177e4SLinus Torvalds { 1469496ad9aaSAl Viro struct inode *inode = file_inode(filp); 14701da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 147141ffe5d5SHugh Dickins pgoff_t index; 147241ffe5d5SHugh Dickins unsigned long offset; 1473a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 1474a0ee5ec5SHugh Dickins 1475a0ee5ec5SHugh Dickins /* 1476a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 1477a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 1478a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 1479a0ee5ec5SHugh Dickins */ 1480a0ee5ec5SHugh Dickins if (segment_eq(get_fs(), KERNEL_DS)) 1481a0ee5ec5SHugh Dickins sgp = SGP_DIRTY; 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds index = *ppos >> PAGE_CACHE_SHIFT; 14841da177e4SLinus Torvalds offset = *ppos & ~PAGE_CACHE_MASK; 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds for (;;) { 14871da177e4SLinus Torvalds struct page *page = NULL; 148841ffe5d5SHugh Dickins pgoff_t end_index; 148941ffe5d5SHugh Dickins unsigned long nr, ret; 14901da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 14931da177e4SLinus Torvalds if (index > end_index) 14941da177e4SLinus Torvalds break; 14951da177e4SLinus Torvalds if (index == end_index) { 14961da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 14971da177e4SLinus Torvalds if (nr <= offset) 14981da177e4SLinus Torvalds break; 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 1501a0ee5ec5SHugh Dickins desc->error = shmem_getpage(inode, index, &page, sgp, NULL); 15021da177e4SLinus Torvalds if (desc->error) { 15031da177e4SLinus Torvalds if (desc->error == -EINVAL) 15041da177e4SLinus Torvalds desc->error = 0; 15051da177e4SLinus Torvalds break; 15061da177e4SLinus Torvalds } 1507d3602444SHugh Dickins if (page) 1508d3602444SHugh Dickins unlock_page(page); 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds /* 15111da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 15121b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 15131da177e4SLinus Torvalds */ 15141da177e4SLinus Torvalds nr = PAGE_CACHE_SIZE; 15151da177e4SLinus Torvalds i_size = i_size_read(inode); 15161da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 15171da177e4SLinus Torvalds if (index == end_index) { 15181da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 15191da177e4SLinus Torvalds if (nr <= offset) { 15201da177e4SLinus Torvalds if (page) 15211da177e4SLinus Torvalds page_cache_release(page); 15221da177e4SLinus Torvalds break; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds nr -= offset; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds if (page) { 15281da177e4SLinus Torvalds /* 15291da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 15301da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 15311da177e4SLinus Torvalds * before reading the page on the kernel side. 15321da177e4SLinus Torvalds */ 15331da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 15341da177e4SLinus Torvalds flush_dcache_page(page); 15351da177e4SLinus Torvalds /* 15361da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 15371da177e4SLinus Torvalds */ 15381da177e4SLinus Torvalds if (!offset) 15391da177e4SLinus Torvalds mark_page_accessed(page); 1540b5810039SNick Piggin } else { 15411da177e4SLinus Torvalds page = ZERO_PAGE(0); 1542b5810039SNick Piggin page_cache_get(page); 1543b5810039SNick Piggin } 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds /* 15461da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 15471da177e4SLinus Torvalds * now we can copy it to user space... 15481da177e4SLinus Torvalds * 15491da177e4SLinus Torvalds * The actor routine returns how many bytes were actually used.. 15501da177e4SLinus Torvalds * NOTE! This may not be the same as how much of a user buffer 15511da177e4SLinus Torvalds * we filled up (we may be padding etc), so we can only update 15521da177e4SLinus Torvalds * "pos" here (the actor routine has to update the user buffer 15531da177e4SLinus Torvalds * pointers and the remaining count). 15541da177e4SLinus Torvalds */ 15551da177e4SLinus Torvalds ret = actor(desc, page, offset, nr); 15561da177e4SLinus Torvalds offset += ret; 15571da177e4SLinus Torvalds index += offset >> PAGE_CACHE_SHIFT; 15581da177e4SLinus Torvalds offset &= ~PAGE_CACHE_MASK; 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds page_cache_release(page); 15611da177e4SLinus Torvalds if (ret != nr || !desc->count) 15621da177e4SLinus Torvalds break; 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds cond_resched(); 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; 15681da177e4SLinus Torvalds file_accessed(filp); 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 1571bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb, 1572bcd78e49SHugh Dickins const struct iovec *iov, unsigned long nr_segs, loff_t pos) 15731da177e4SLinus Torvalds { 1574bcd78e49SHugh Dickins struct file *filp = iocb->ki_filp; 1575bcd78e49SHugh Dickins ssize_t retval; 1576bcd78e49SHugh Dickins unsigned long seg; 1577bcd78e49SHugh Dickins size_t count; 1578bcd78e49SHugh Dickins loff_t *ppos = &iocb->ki_pos; 1579bcd78e49SHugh Dickins 1580bcd78e49SHugh Dickins retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); 1581bcd78e49SHugh Dickins if (retval) 1582bcd78e49SHugh Dickins return retval; 1583bcd78e49SHugh Dickins 1584bcd78e49SHugh Dickins for (seg = 0; seg < nr_segs; seg++) { 15851da177e4SLinus Torvalds read_descriptor_t desc; 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds desc.written = 0; 1588bcd78e49SHugh Dickins desc.arg.buf = iov[seg].iov_base; 1589bcd78e49SHugh Dickins desc.count = iov[seg].iov_len; 1590bcd78e49SHugh Dickins if (desc.count == 0) 1591bcd78e49SHugh Dickins continue; 15921da177e4SLinus Torvalds desc.error = 0; 15931da177e4SLinus Torvalds do_shmem_file_read(filp, ppos, &desc, file_read_actor); 1594bcd78e49SHugh Dickins retval += desc.written; 1595bcd78e49SHugh Dickins if (desc.error) { 1596bcd78e49SHugh Dickins retval = retval ?: desc.error; 1597bcd78e49SHugh Dickins break; 1598bcd78e49SHugh Dickins } 1599bcd78e49SHugh Dickins if (desc.count > 0) 1600bcd78e49SHugh Dickins break; 1601bcd78e49SHugh Dickins } 1602bcd78e49SHugh Dickins return retval; 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds 1605708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 1606708e3508SHugh Dickins struct pipe_inode_info *pipe, size_t len, 1607708e3508SHugh Dickins unsigned int flags) 1608708e3508SHugh Dickins { 1609708e3508SHugh Dickins struct address_space *mapping = in->f_mapping; 161071f0e07aSHugh Dickins struct inode *inode = mapping->host; 1611708e3508SHugh Dickins unsigned int loff, nr_pages, req_pages; 1612708e3508SHugh Dickins struct page *pages[PIPE_DEF_BUFFERS]; 1613708e3508SHugh Dickins struct partial_page partial[PIPE_DEF_BUFFERS]; 1614708e3508SHugh Dickins struct page *page; 1615708e3508SHugh Dickins pgoff_t index, end_index; 1616708e3508SHugh Dickins loff_t isize, left; 1617708e3508SHugh Dickins int error, page_nr; 1618708e3508SHugh Dickins struct splice_pipe_desc spd = { 1619708e3508SHugh Dickins .pages = pages, 1620708e3508SHugh Dickins .partial = partial, 1621047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 1622708e3508SHugh Dickins .flags = flags, 1623708e3508SHugh Dickins .ops = &page_cache_pipe_buf_ops, 1624708e3508SHugh Dickins .spd_release = spd_release_page, 1625708e3508SHugh Dickins }; 1626708e3508SHugh Dickins 162771f0e07aSHugh Dickins isize = i_size_read(inode); 1628708e3508SHugh Dickins if (unlikely(*ppos >= isize)) 1629708e3508SHugh Dickins return 0; 1630708e3508SHugh Dickins 1631708e3508SHugh Dickins left = isize - *ppos; 1632708e3508SHugh Dickins if (unlikely(left < len)) 1633708e3508SHugh Dickins len = left; 1634708e3508SHugh Dickins 1635708e3508SHugh Dickins if (splice_grow_spd(pipe, &spd)) 1636708e3508SHugh Dickins return -ENOMEM; 1637708e3508SHugh Dickins 1638708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1639708e3508SHugh Dickins loff = *ppos & ~PAGE_CACHE_MASK; 1640708e3508SHugh Dickins req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1641708e3508SHugh Dickins nr_pages = min(req_pages, pipe->buffers); 1642708e3508SHugh Dickins 1643708e3508SHugh Dickins spd.nr_pages = find_get_pages_contig(mapping, index, 1644708e3508SHugh Dickins nr_pages, spd.pages); 1645708e3508SHugh Dickins index += spd.nr_pages; 1646708e3508SHugh Dickins error = 0; 164771f0e07aSHugh Dickins 1648708e3508SHugh Dickins while (spd.nr_pages < nr_pages) { 164971f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); 165071f0e07aSHugh Dickins if (error) 1651708e3508SHugh Dickins break; 1652708e3508SHugh Dickins unlock_page(page); 1653708e3508SHugh Dickins spd.pages[spd.nr_pages++] = page; 1654708e3508SHugh Dickins index++; 1655708e3508SHugh Dickins } 1656708e3508SHugh Dickins 1657708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1658708e3508SHugh Dickins nr_pages = spd.nr_pages; 1659708e3508SHugh Dickins spd.nr_pages = 0; 166071f0e07aSHugh Dickins 1661708e3508SHugh Dickins for (page_nr = 0; page_nr < nr_pages; page_nr++) { 1662708e3508SHugh Dickins unsigned int this_len; 1663708e3508SHugh Dickins 1664708e3508SHugh Dickins if (!len) 1665708e3508SHugh Dickins break; 1666708e3508SHugh Dickins 1667708e3508SHugh Dickins this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 1668708e3508SHugh Dickins page = spd.pages[page_nr]; 1669708e3508SHugh Dickins 167071f0e07aSHugh Dickins if (!PageUptodate(page) || page->mapping != mapping) { 167171f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, 167271f0e07aSHugh Dickins SGP_CACHE, NULL); 167371f0e07aSHugh Dickins if (error) 1674708e3508SHugh Dickins break; 167571f0e07aSHugh Dickins unlock_page(page); 1676708e3508SHugh Dickins page_cache_release(spd.pages[page_nr]); 1677708e3508SHugh Dickins spd.pages[page_nr] = page; 1678708e3508SHugh Dickins } 1679708e3508SHugh Dickins 168071f0e07aSHugh Dickins isize = i_size_read(inode); 1681708e3508SHugh Dickins end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 1682708e3508SHugh Dickins if (unlikely(!isize || index > end_index)) 1683708e3508SHugh Dickins break; 1684708e3508SHugh Dickins 1685708e3508SHugh Dickins if (end_index == index) { 1686708e3508SHugh Dickins unsigned int plen; 1687708e3508SHugh Dickins 1688708e3508SHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 1689708e3508SHugh Dickins if (plen <= loff) 1690708e3508SHugh Dickins break; 1691708e3508SHugh Dickins 1692708e3508SHugh Dickins this_len = min(this_len, plen - loff); 1693708e3508SHugh Dickins len = this_len; 1694708e3508SHugh Dickins } 1695708e3508SHugh Dickins 1696708e3508SHugh Dickins spd.partial[page_nr].offset = loff; 1697708e3508SHugh Dickins spd.partial[page_nr].len = this_len; 1698708e3508SHugh Dickins len -= this_len; 1699708e3508SHugh Dickins loff = 0; 1700708e3508SHugh Dickins spd.nr_pages++; 1701708e3508SHugh Dickins index++; 1702708e3508SHugh Dickins } 1703708e3508SHugh Dickins 1704708e3508SHugh Dickins while (page_nr < nr_pages) 1705708e3508SHugh Dickins page_cache_release(spd.pages[page_nr++]); 1706708e3508SHugh Dickins 1707708e3508SHugh Dickins if (spd.nr_pages) 1708708e3508SHugh Dickins error = splice_to_pipe(pipe, &spd); 1709708e3508SHugh Dickins 1710047fe360SEric Dumazet splice_shrink_spd(&spd); 1711708e3508SHugh Dickins 1712708e3508SHugh Dickins if (error > 0) { 1713708e3508SHugh Dickins *ppos += error; 1714708e3508SHugh Dickins file_accessed(in); 1715708e3508SHugh Dickins } 1716708e3508SHugh Dickins return error; 1717708e3508SHugh Dickins } 1718708e3508SHugh Dickins 1719220f2ac9SHugh Dickins /* 1720220f2ac9SHugh Dickins * llseek SEEK_DATA or SEEK_HOLE through the radix_tree. 1721220f2ac9SHugh Dickins */ 1722220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping, 1723965c8e59SAndrew Morton pgoff_t index, pgoff_t end, int whence) 1724220f2ac9SHugh Dickins { 1725220f2ac9SHugh Dickins struct page *page; 1726220f2ac9SHugh Dickins struct pagevec pvec; 1727220f2ac9SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 1728220f2ac9SHugh Dickins bool done = false; 1729220f2ac9SHugh Dickins int i; 1730220f2ac9SHugh Dickins 1731220f2ac9SHugh Dickins pagevec_init(&pvec, 0); 1732220f2ac9SHugh Dickins pvec.nr = 1; /* start small: we may be there already */ 1733220f2ac9SHugh Dickins while (!done) { 1734220f2ac9SHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 1735220f2ac9SHugh Dickins pvec.nr, pvec.pages, indices); 1736220f2ac9SHugh Dickins if (!pvec.nr) { 1737965c8e59SAndrew Morton if (whence == SEEK_DATA) 1738220f2ac9SHugh Dickins index = end; 1739220f2ac9SHugh Dickins break; 1740220f2ac9SHugh Dickins } 1741220f2ac9SHugh Dickins for (i = 0; i < pvec.nr; i++, index++) { 1742220f2ac9SHugh Dickins if (index < indices[i]) { 1743965c8e59SAndrew Morton if (whence == SEEK_HOLE) { 1744220f2ac9SHugh Dickins done = true; 1745220f2ac9SHugh Dickins break; 1746220f2ac9SHugh Dickins } 1747220f2ac9SHugh Dickins index = indices[i]; 1748220f2ac9SHugh Dickins } 1749220f2ac9SHugh Dickins page = pvec.pages[i]; 1750220f2ac9SHugh Dickins if (page && !radix_tree_exceptional_entry(page)) { 1751220f2ac9SHugh Dickins if (!PageUptodate(page)) 1752220f2ac9SHugh Dickins page = NULL; 1753220f2ac9SHugh Dickins } 1754220f2ac9SHugh Dickins if (index >= end || 1755965c8e59SAndrew Morton (page && whence == SEEK_DATA) || 1756965c8e59SAndrew Morton (!page && whence == SEEK_HOLE)) { 1757220f2ac9SHugh Dickins done = true; 1758220f2ac9SHugh Dickins break; 1759220f2ac9SHugh Dickins } 1760220f2ac9SHugh Dickins } 1761220f2ac9SHugh Dickins shmem_deswap_pagevec(&pvec); 1762220f2ac9SHugh Dickins pagevec_release(&pvec); 1763220f2ac9SHugh Dickins pvec.nr = PAGEVEC_SIZE; 1764220f2ac9SHugh Dickins cond_resched(); 1765220f2ac9SHugh Dickins } 1766220f2ac9SHugh Dickins return index; 1767220f2ac9SHugh Dickins } 1768220f2ac9SHugh Dickins 1769965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 1770220f2ac9SHugh Dickins { 1771220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 1772220f2ac9SHugh Dickins struct inode *inode = mapping->host; 1773220f2ac9SHugh Dickins pgoff_t start, end; 1774220f2ac9SHugh Dickins loff_t new_offset; 1775220f2ac9SHugh Dickins 1776965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 1777965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 1778220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 1779220f2ac9SHugh Dickins mutex_lock(&inode->i_mutex); 1780220f2ac9SHugh Dickins /* We're holding i_mutex so we can access i_size directly */ 1781220f2ac9SHugh Dickins 1782220f2ac9SHugh Dickins if (offset < 0) 1783220f2ac9SHugh Dickins offset = -EINVAL; 1784220f2ac9SHugh Dickins else if (offset >= inode->i_size) 1785220f2ac9SHugh Dickins offset = -ENXIO; 1786220f2ac9SHugh Dickins else { 1787220f2ac9SHugh Dickins start = offset >> PAGE_CACHE_SHIFT; 1788220f2ac9SHugh Dickins end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1789965c8e59SAndrew Morton new_offset = shmem_seek_hole_data(mapping, start, end, whence); 1790220f2ac9SHugh Dickins new_offset <<= PAGE_CACHE_SHIFT; 1791220f2ac9SHugh Dickins if (new_offset > offset) { 1792220f2ac9SHugh Dickins if (new_offset < inode->i_size) 1793220f2ac9SHugh Dickins offset = new_offset; 1794965c8e59SAndrew Morton else if (whence == SEEK_DATA) 1795220f2ac9SHugh Dickins offset = -ENXIO; 1796220f2ac9SHugh Dickins else 1797220f2ac9SHugh Dickins offset = inode->i_size; 1798220f2ac9SHugh Dickins } 1799220f2ac9SHugh Dickins } 1800220f2ac9SHugh Dickins 1801*387aae6fSHugh Dickins if (offset >= 0) 180246a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 1803220f2ac9SHugh Dickins mutex_unlock(&inode->i_mutex); 1804220f2ac9SHugh Dickins return offset; 1805220f2ac9SHugh Dickins } 1806220f2ac9SHugh Dickins 180783e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 180883e4fa9cSHugh Dickins loff_t len) 180983e4fa9cSHugh Dickins { 1810496ad9aaSAl Viro struct inode *inode = file_inode(file); 1811e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 18121aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 1813e2d12e22SHugh Dickins pgoff_t start, index, end; 1814e2d12e22SHugh Dickins int error; 181583e4fa9cSHugh Dickins 181683e4fa9cSHugh Dickins mutex_lock(&inode->i_mutex); 181783e4fa9cSHugh Dickins 181883e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 181983e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 182083e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 182183e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 182283e4fa9cSHugh Dickins 182383e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 182483e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 182583e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 182683e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 182783e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 182883e4fa9cSHugh Dickins error = 0; 1829e2d12e22SHugh Dickins goto out; 183083e4fa9cSHugh Dickins } 183183e4fa9cSHugh Dickins 1832e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 1833e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 1834e2d12e22SHugh Dickins if (error) 1835e2d12e22SHugh Dickins goto out; 1836e2d12e22SHugh Dickins 1837e2d12e22SHugh Dickins start = offset >> PAGE_CACHE_SHIFT; 1838e2d12e22SHugh Dickins end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1839e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 1840e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 1841e2d12e22SHugh Dickins error = -ENOSPC; 1842e2d12e22SHugh Dickins goto out; 1843e2d12e22SHugh Dickins } 1844e2d12e22SHugh Dickins 18451aac1400SHugh Dickins shmem_falloc.start = start; 18461aac1400SHugh Dickins shmem_falloc.next = start; 18471aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 18481aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 18491aac1400SHugh Dickins spin_lock(&inode->i_lock); 18501aac1400SHugh Dickins inode->i_private = &shmem_falloc; 18511aac1400SHugh Dickins spin_unlock(&inode->i_lock); 18521aac1400SHugh Dickins 1853e2d12e22SHugh Dickins for (index = start; index < end; index++) { 1854e2d12e22SHugh Dickins struct page *page; 1855e2d12e22SHugh Dickins 1856e2d12e22SHugh Dickins /* 1857e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 1858e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 1859e2d12e22SHugh Dickins */ 1860e2d12e22SHugh Dickins if (signal_pending(current)) 1861e2d12e22SHugh Dickins error = -EINTR; 18621aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 18631aac1400SHugh Dickins error = -ENOMEM; 1864e2d12e22SHugh Dickins else 18651635f6a7SHugh Dickins error = shmem_getpage(inode, index, &page, SGP_FALLOC, 1866e2d12e22SHugh Dickins NULL); 1867e2d12e22SHugh Dickins if (error) { 18681635f6a7SHugh Dickins /* Remove the !PageUptodate pages we added */ 18691635f6a7SHugh Dickins shmem_undo_range(inode, 18701635f6a7SHugh Dickins (loff_t)start << PAGE_CACHE_SHIFT, 18711635f6a7SHugh Dickins (loff_t)index << PAGE_CACHE_SHIFT, true); 18721aac1400SHugh Dickins goto undone; 1873e2d12e22SHugh Dickins } 1874e2d12e22SHugh Dickins 1875e2d12e22SHugh Dickins /* 18761aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 18771aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 18781aac1400SHugh Dickins */ 18791aac1400SHugh Dickins shmem_falloc.next++; 18801aac1400SHugh Dickins if (!PageUptodate(page)) 18811aac1400SHugh Dickins shmem_falloc.nr_falloced++; 18821aac1400SHugh Dickins 18831aac1400SHugh Dickins /* 18841635f6a7SHugh Dickins * If !PageUptodate, leave it that way so that freeable pages 18851635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 18861635f6a7SHugh Dickins * But set_page_dirty so that memory pressure will swap rather 1887e2d12e22SHugh Dickins * than free the pages we are allocating (and SGP_CACHE pages 1888e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 1889e2d12e22SHugh Dickins */ 1890e2d12e22SHugh Dickins set_page_dirty(page); 1891e2d12e22SHugh Dickins unlock_page(page); 1892e2d12e22SHugh Dickins page_cache_release(page); 1893e2d12e22SHugh Dickins cond_resched(); 1894e2d12e22SHugh Dickins } 1895e2d12e22SHugh Dickins 1896e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 1897e2d12e22SHugh Dickins i_size_write(inode, offset + len); 1898e2d12e22SHugh Dickins inode->i_ctime = CURRENT_TIME; 18991aac1400SHugh Dickins undone: 19001aac1400SHugh Dickins spin_lock(&inode->i_lock); 19011aac1400SHugh Dickins inode->i_private = NULL; 19021aac1400SHugh Dickins spin_unlock(&inode->i_lock); 1903e2d12e22SHugh Dickins out: 190483e4fa9cSHugh Dickins mutex_unlock(&inode->i_mutex); 190583e4fa9cSHugh Dickins return error; 190683e4fa9cSHugh Dickins } 190783e4fa9cSHugh Dickins 1908726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 19091da177e4SLinus Torvalds { 1910726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 19111da177e4SLinus Torvalds 19121da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 19131da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 19141da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 19150edd73b3SHugh Dickins if (sbinfo->max_blocks) { 19161da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 191741ffe5d5SHugh Dickins buf->f_bavail = 191841ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 191941ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 19200edd73b3SHugh Dickins } 19210edd73b3SHugh Dickins if (sbinfo->max_inodes) { 19221da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 19231da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 19261da177e4SLinus Torvalds return 0; 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds /* 19301da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 19311da177e4SLinus Torvalds */ 19321da177e4SLinus Torvalds static int 19331a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 19341da177e4SLinus Torvalds { 19350b0a0806SHugh Dickins struct inode *inode; 19361da177e4SLinus Torvalds int error = -ENOSPC; 19371da177e4SLinus Torvalds 1938454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 19391da177e4SLinus Torvalds if (inode) { 194037ec43cdSMimi Zohar #ifdef CONFIG_TMPFS_POSIX_ACL 194137ec43cdSMimi Zohar error = generic_acl_init(inode, dir); 194237ec43cdSMimi Zohar if (error) { 194337ec43cdSMimi Zohar iput(inode); 194437ec43cdSMimi Zohar return error; 194537ec43cdSMimi Zohar } 194637ec43cdSMimi Zohar #endif 19472a7dba39SEric Paris error = security_inode_init_security(inode, dir, 19489d8f13baSMimi Zohar &dentry->d_name, 19496d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 1950570bc1c2SStephen Smalley if (error) { 1951570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1952570bc1c2SStephen Smalley iput(inode); 1953570bc1c2SStephen Smalley return error; 1954570bc1c2SStephen Smalley } 195539f0247dSAndreas Gruenbacher } 195637ec43cdSMimi Zohar 1957718deb6bSAl Viro error = 0; 19581da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 19591da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 19601da177e4SLinus Torvalds d_instantiate(dentry, inode); 19611da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds return error; 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds 196660545d0dSAl Viro static int 196760545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 196860545d0dSAl Viro { 196960545d0dSAl Viro struct inode *inode; 197060545d0dSAl Viro int error = -ENOSPC; 197160545d0dSAl Viro 197260545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 197360545d0dSAl Viro if (inode) { 197460545d0dSAl Viro error = security_inode_init_security(inode, dir, 197560545d0dSAl Viro NULL, 197660545d0dSAl Viro shmem_initxattrs, NULL); 197760545d0dSAl Viro if (error) { 197860545d0dSAl Viro if (error != -EOPNOTSUPP) { 197960545d0dSAl Viro iput(inode); 198060545d0dSAl Viro return error; 198160545d0dSAl Viro } 198260545d0dSAl Viro } 198360545d0dSAl Viro #ifdef CONFIG_TMPFS_POSIX_ACL 198460545d0dSAl Viro error = generic_acl_init(inode, dir); 198560545d0dSAl Viro if (error) { 198660545d0dSAl Viro iput(inode); 198760545d0dSAl Viro return error; 198860545d0dSAl Viro } 198960545d0dSAl Viro #else 199060545d0dSAl Viro error = 0; 199160545d0dSAl Viro #endif 199260545d0dSAl Viro d_tmpfile(dentry, inode); 199360545d0dSAl Viro } 199460545d0dSAl Viro return error; 199560545d0dSAl Viro } 199660545d0dSAl Viro 199718bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 19981da177e4SLinus Torvalds { 19991da177e4SLinus Torvalds int error; 20001da177e4SLinus Torvalds 20011da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 20021da177e4SLinus Torvalds return error; 2003d8c76e6fSDave Hansen inc_nlink(dir); 20041da177e4SLinus Torvalds return 0; 20051da177e4SLinus Torvalds } 20061da177e4SLinus Torvalds 20074acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2008ebfc3b49SAl Viro bool excl) 20091da177e4SLinus Torvalds { 20101da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds 20131da177e4SLinus Torvalds /* 20141da177e4SLinus Torvalds * Link a file.. 20151da177e4SLinus Torvalds */ 20161da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 20171da177e4SLinus Torvalds { 20181da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 20195b04c689SPavel Emelyanov int ret; 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds /* 20221da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 20231da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 20241da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 20251da177e4SLinus Torvalds */ 20265b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 20275b04c689SPavel Emelyanov if (ret) 20285b04c689SPavel Emelyanov goto out; 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 20311da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 2032d8c76e6fSDave Hansen inc_nlink(inode); 20337de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 20341da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 20351da177e4SLinus Torvalds d_instantiate(dentry, inode); 20365b04c689SPavel Emelyanov out: 20375b04c689SPavel Emelyanov return ret; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 20411da177e4SLinus Torvalds { 20421da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20431da177e4SLinus Torvalds 20445b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 20455b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 20481da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 20499a53c3a7SDave Hansen drop_nlink(inode); 20501da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 20511da177e4SLinus Torvalds return 0; 20521da177e4SLinus Torvalds } 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 20551da177e4SLinus Torvalds { 20561da177e4SLinus Torvalds if (!simple_empty(dentry)) 20571da177e4SLinus Torvalds return -ENOTEMPTY; 20581da177e4SLinus Torvalds 20599a53c3a7SDave Hansen drop_nlink(dentry->d_inode); 20609a53c3a7SDave Hansen drop_nlink(dir); 20611da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds /* 20651da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 20661da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 20671da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 20681da177e4SLinus Torvalds * gets overwritten. 20691da177e4SLinus Torvalds */ 20701da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 20711da177e4SLinus Torvalds { 20721da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 20731da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 20761da177e4SLinus Torvalds return -ENOTEMPTY; 20771da177e4SLinus Torvalds 20781da177e4SLinus Torvalds if (new_dentry->d_inode) { 20791da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 20801da177e4SLinus Torvalds if (they_are_dirs) 20819a53c3a7SDave Hansen drop_nlink(old_dir); 20821da177e4SLinus Torvalds } else if (they_are_dirs) { 20839a53c3a7SDave Hansen drop_nlink(old_dir); 2084d8c76e6fSDave Hansen inc_nlink(new_dir); 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 20881da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 20891da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 20901da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 20911da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 20921da177e4SLinus Torvalds return 0; 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 20961da177e4SLinus Torvalds { 20971da177e4SLinus Torvalds int error; 20981da177e4SLinus Torvalds int len; 20991da177e4SLinus Torvalds struct inode *inode; 21009276aad6SHugh Dickins struct page *page; 21011da177e4SLinus Torvalds char *kaddr; 21021da177e4SLinus Torvalds struct shmem_inode_info *info; 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds len = strlen(symname) + 1; 21051da177e4SLinus Torvalds if (len > PAGE_CACHE_SIZE) 21061da177e4SLinus Torvalds return -ENAMETOOLONG; 21071da177e4SLinus Torvalds 2108454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 21091da177e4SLinus Torvalds if (!inode) 21101da177e4SLinus Torvalds return -ENOSPC; 21111da177e4SLinus Torvalds 21129d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 21136d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2114570bc1c2SStephen Smalley if (error) { 2115570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 2116570bc1c2SStephen Smalley iput(inode); 2117570bc1c2SStephen Smalley return error; 2118570bc1c2SStephen Smalley } 2119570bc1c2SStephen Smalley error = 0; 2120570bc1c2SStephen Smalley } 2121570bc1c2SStephen Smalley 21221da177e4SLinus Torvalds info = SHMEM_I(inode); 21231da177e4SLinus Torvalds inode->i_size = len-1; 212469f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 212569f07ec9SHugh Dickins info->symlink = kmemdup(symname, len, GFP_KERNEL); 212669f07ec9SHugh Dickins if (!info->symlink) { 212769f07ec9SHugh Dickins iput(inode); 212869f07ec9SHugh Dickins return -ENOMEM; 212969f07ec9SHugh Dickins } 213069f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 21311da177e4SLinus Torvalds } else { 21321da177e4SLinus Torvalds error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); 21331da177e4SLinus Torvalds if (error) { 21341da177e4SLinus Torvalds iput(inode); 21351da177e4SLinus Torvalds return error; 21361da177e4SLinus Torvalds } 213714fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 21381da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 21399b04c5feSCong Wang kaddr = kmap_atomic(page); 21401da177e4SLinus Torvalds memcpy(kaddr, symname, len); 21419b04c5feSCong Wang kunmap_atomic(kaddr); 2142ec9516fbSHugh Dickins SetPageUptodate(page); 21431da177e4SLinus Torvalds set_page_dirty(page); 21446746aff7SWu Fengguang unlock_page(page); 21451da177e4SLinus Torvalds page_cache_release(page); 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 21481da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 21491da177e4SLinus Torvalds d_instantiate(dentry, inode); 21501da177e4SLinus Torvalds dget(dentry); 21511da177e4SLinus Torvalds return 0; 21521da177e4SLinus Torvalds } 21531da177e4SLinus Torvalds 215469f07ec9SHugh Dickins static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd) 21551da177e4SLinus Torvalds { 215669f07ec9SHugh Dickins nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink); 2157cc314eefSLinus Torvalds return NULL; 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds 2160cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) 21611da177e4SLinus Torvalds { 21621da177e4SLinus Torvalds struct page *page = NULL; 216341ffe5d5SHugh Dickins int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); 216441ffe5d5SHugh Dickins nd_set_link(nd, error ? ERR_PTR(error) : kmap(page)); 2165d3602444SHugh Dickins if (page) 2166d3602444SHugh Dickins unlock_page(page); 2167cc314eefSLinus Torvalds return page; 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds 2170cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 21711da177e4SLinus Torvalds { 21721da177e4SLinus Torvalds if (!IS_ERR(nd_get_link(nd))) { 2173cc314eefSLinus Torvalds struct page *page = cookie; 21741da177e4SLinus Torvalds kunmap(page); 21751da177e4SLinus Torvalds mark_page_accessed(page); 21761da177e4SLinus Torvalds page_cache_release(page); 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds } 21791da177e4SLinus Torvalds 2180b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2181b09e0fa4SEric Paris /* 2182b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 2183b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 2184b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 2185b09e0fa4SEric Paris * filesystem level, though. 2186b09e0fa4SEric Paris */ 2187b09e0fa4SEric Paris 21886d9d88d0SJarkko Sakkinen /* 21896d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 21906d9d88d0SJarkko Sakkinen */ 21916d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 21926d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 21936d9d88d0SJarkko Sakkinen void *fs_info) 21946d9d88d0SJarkko Sakkinen { 21956d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 21966d9d88d0SJarkko Sakkinen const struct xattr *xattr; 219738f38657SAristeu Rozanski struct simple_xattr *new_xattr; 21986d9d88d0SJarkko Sakkinen size_t len; 21996d9d88d0SJarkko Sakkinen 22006d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 220138f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 22026d9d88d0SJarkko Sakkinen if (!new_xattr) 22036d9d88d0SJarkko Sakkinen return -ENOMEM; 22046d9d88d0SJarkko Sakkinen 22056d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 22066d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 22076d9d88d0SJarkko Sakkinen GFP_KERNEL); 22086d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 22096d9d88d0SJarkko Sakkinen kfree(new_xattr); 22106d9d88d0SJarkko Sakkinen return -ENOMEM; 22116d9d88d0SJarkko Sakkinen } 22126d9d88d0SJarkko Sakkinen 22136d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 22146d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 22156d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 22166d9d88d0SJarkko Sakkinen xattr->name, len); 22176d9d88d0SJarkko Sakkinen 221838f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 22196d9d88d0SJarkko Sakkinen } 22206d9d88d0SJarkko Sakkinen 22216d9d88d0SJarkko Sakkinen return 0; 22226d9d88d0SJarkko Sakkinen } 22236d9d88d0SJarkko Sakkinen 2224b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 2225b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 2226b09e0fa4SEric Paris &generic_acl_access_handler, 2227b09e0fa4SEric Paris &generic_acl_default_handler, 2228b09e0fa4SEric Paris #endif 2229b09e0fa4SEric Paris NULL 2230b09e0fa4SEric Paris }; 2231b09e0fa4SEric Paris 2232b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name) 2233b09e0fa4SEric Paris { 2234b09e0fa4SEric Paris struct { const char *prefix; size_t len; } arr[] = { 2235b09e0fa4SEric Paris { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, 2236b09e0fa4SEric Paris { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } 2237b09e0fa4SEric Paris }; 2238b09e0fa4SEric Paris int i; 2239b09e0fa4SEric Paris 2240b09e0fa4SEric Paris for (i = 0; i < ARRAY_SIZE(arr); i++) { 2241b09e0fa4SEric Paris size_t preflen = arr[i].len; 2242b09e0fa4SEric Paris if (strncmp(name, arr[i].prefix, preflen) == 0) { 2243b09e0fa4SEric Paris if (!name[preflen]) 2244b09e0fa4SEric Paris return -EINVAL; 2245b09e0fa4SEric Paris return 0; 2246b09e0fa4SEric Paris } 2247b09e0fa4SEric Paris } 2248b09e0fa4SEric Paris return -EOPNOTSUPP; 2249b09e0fa4SEric Paris } 2250b09e0fa4SEric Paris 2251b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, 2252b09e0fa4SEric Paris void *buffer, size_t size) 2253b09e0fa4SEric Paris { 225438f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 2255b09e0fa4SEric Paris int err; 2256b09e0fa4SEric Paris 2257b09e0fa4SEric Paris /* 2258b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2259b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2260b09e0fa4SEric Paris * for it via sb->s_xattr. 2261b09e0fa4SEric Paris */ 2262b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2263b09e0fa4SEric Paris return generic_getxattr(dentry, name, buffer, size); 2264b09e0fa4SEric Paris 2265b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2266b09e0fa4SEric Paris if (err) 2267b09e0fa4SEric Paris return err; 2268b09e0fa4SEric Paris 226938f38657SAristeu Rozanski return simple_xattr_get(&info->xattrs, name, buffer, size); 2270b09e0fa4SEric Paris } 2271b09e0fa4SEric Paris 2272b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name, 2273b09e0fa4SEric Paris const void *value, size_t size, int flags) 2274b09e0fa4SEric Paris { 227538f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 2276b09e0fa4SEric Paris int err; 2277b09e0fa4SEric Paris 2278b09e0fa4SEric Paris /* 2279b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2280b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2281b09e0fa4SEric Paris * for it via sb->s_xattr. 2282b09e0fa4SEric Paris */ 2283b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2284b09e0fa4SEric Paris return generic_setxattr(dentry, name, value, size, flags); 2285b09e0fa4SEric Paris 2286b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2287b09e0fa4SEric Paris if (err) 2288b09e0fa4SEric Paris return err; 2289b09e0fa4SEric Paris 229038f38657SAristeu Rozanski return simple_xattr_set(&info->xattrs, name, value, size, flags); 2291b09e0fa4SEric Paris } 2292b09e0fa4SEric Paris 2293b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name) 2294b09e0fa4SEric Paris { 229538f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 2296b09e0fa4SEric Paris int err; 2297b09e0fa4SEric Paris 2298b09e0fa4SEric Paris /* 2299b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2300b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2301b09e0fa4SEric Paris * for it via sb->s_xattr. 2302b09e0fa4SEric Paris */ 2303b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2304b09e0fa4SEric Paris return generic_removexattr(dentry, name); 2305b09e0fa4SEric Paris 2306b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2307b09e0fa4SEric Paris if (err) 2308b09e0fa4SEric Paris return err; 2309b09e0fa4SEric Paris 231038f38657SAristeu Rozanski return simple_xattr_remove(&info->xattrs, name); 2311b09e0fa4SEric Paris } 2312b09e0fa4SEric Paris 2313b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 2314b09e0fa4SEric Paris { 231538f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 231638f38657SAristeu Rozanski return simple_xattr_list(&info->xattrs, buffer, size); 2317b09e0fa4SEric Paris } 2318b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 2319b09e0fa4SEric Paris 232069f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 23211da177e4SLinus Torvalds .readlink = generic_readlink, 232269f07ec9SHugh Dickins .follow_link = shmem_follow_short_symlink, 2323b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2324b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2325b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2326b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2327b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2328b09e0fa4SEric Paris #endif 23291da177e4SLinus Torvalds }; 23301da177e4SLinus Torvalds 233192e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 23321da177e4SLinus Torvalds .readlink = generic_readlink, 23331da177e4SLinus Torvalds .follow_link = shmem_follow_link, 23341da177e4SLinus Torvalds .put_link = shmem_put_link, 2335b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2336b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2337b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2338b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2339b09e0fa4SEric Paris .removexattr = shmem_removexattr, 234039f0247dSAndreas Gruenbacher #endif 2341b09e0fa4SEric Paris }; 234239f0247dSAndreas Gruenbacher 234391828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 234491828a40SDavid M. Grimes { 234591828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 234691828a40SDavid M. Grimes } 234791828a40SDavid M. Grimes 234891828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 234991828a40SDavid M. Grimes { 235091828a40SDavid M. Grimes __u32 *fh = vfh; 235191828a40SDavid M. Grimes __u64 inum = fh[2]; 235291828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 235391828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 235491828a40SDavid M. Grimes } 235591828a40SDavid M. Grimes 2356480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 2357480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 235891828a40SDavid M. Grimes { 235991828a40SDavid M. Grimes struct inode *inode; 2360480b116cSChristoph Hellwig struct dentry *dentry = NULL; 236135c2a7f4SHugh Dickins u64 inum; 236291828a40SDavid M. Grimes 2363480b116cSChristoph Hellwig if (fh_len < 3) 2364480b116cSChristoph Hellwig return NULL; 2365480b116cSChristoph Hellwig 236635c2a7f4SHugh Dickins inum = fid->raw[2]; 236735c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 236835c2a7f4SHugh Dickins 2369480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 2370480b116cSChristoph Hellwig shmem_match, fid->raw); 237191828a40SDavid M. Grimes if (inode) { 2372480b116cSChristoph Hellwig dentry = d_find_alias(inode); 237391828a40SDavid M. Grimes iput(inode); 237491828a40SDavid M. Grimes } 237591828a40SDavid M. Grimes 2376480b116cSChristoph Hellwig return dentry; 237791828a40SDavid M. Grimes } 237891828a40SDavid M. Grimes 2379b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 2380b0b0382bSAl Viro struct inode *parent) 238191828a40SDavid M. Grimes { 23825fe0c237SAneesh Kumar K.V if (*len < 3) { 23835fe0c237SAneesh Kumar K.V *len = 3; 238494e07a75SNamjae Jeon return FILEID_INVALID; 23855fe0c237SAneesh Kumar K.V } 238691828a40SDavid M. Grimes 23871d3382cbSAl Viro if (inode_unhashed(inode)) { 238891828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 238991828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 239091828a40SDavid M. Grimes * time, we need a lock to ensure we only try 239191828a40SDavid M. Grimes * to do it once 239291828a40SDavid M. Grimes */ 239391828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 239491828a40SDavid M. Grimes spin_lock(&lock); 23951d3382cbSAl Viro if (inode_unhashed(inode)) 239691828a40SDavid M. Grimes __insert_inode_hash(inode, 239791828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 239891828a40SDavid M. Grimes spin_unlock(&lock); 239991828a40SDavid M. Grimes } 240091828a40SDavid M. Grimes 240191828a40SDavid M. Grimes fh[0] = inode->i_generation; 240291828a40SDavid M. Grimes fh[1] = inode->i_ino; 240391828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 240491828a40SDavid M. Grimes 240591828a40SDavid M. Grimes *len = 3; 240691828a40SDavid M. Grimes return 1; 240791828a40SDavid M. Grimes } 240891828a40SDavid M. Grimes 240939655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 241091828a40SDavid M. Grimes .get_parent = shmem_get_parent, 241191828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 2412480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 241391828a40SDavid M. Grimes }; 241491828a40SDavid M. Grimes 2415680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 2416680d794bSakpm@linux-foundation.org bool remount) 24171da177e4SLinus Torvalds { 24181da177e4SLinus Torvalds char *this_char, *value, *rest; 241949cd0a5cSGreg Thelen struct mempolicy *mpol = NULL; 24208751e039SEric W. Biederman uid_t uid; 24218751e039SEric W. Biederman gid_t gid; 24221da177e4SLinus Torvalds 2423b00dc3adSHugh Dickins while (options != NULL) { 2424b00dc3adSHugh Dickins this_char = options; 2425b00dc3adSHugh Dickins for (;;) { 2426b00dc3adSHugh Dickins /* 2427b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 2428b00dc3adSHugh Dickins * mount options form a comma-separated list, 2429b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 2430b00dc3adSHugh Dickins */ 2431b00dc3adSHugh Dickins options = strchr(options, ','); 2432b00dc3adSHugh Dickins if (options == NULL) 2433b00dc3adSHugh Dickins break; 2434b00dc3adSHugh Dickins options++; 2435b00dc3adSHugh Dickins if (!isdigit(*options)) { 2436b00dc3adSHugh Dickins options[-1] = '\0'; 2437b00dc3adSHugh Dickins break; 2438b00dc3adSHugh Dickins } 2439b00dc3adSHugh Dickins } 24401da177e4SLinus Torvalds if (!*this_char) 24411da177e4SLinus Torvalds continue; 24421da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 24431da177e4SLinus Torvalds *value++ = 0; 24441da177e4SLinus Torvalds } else { 24451da177e4SLinus Torvalds printk(KERN_ERR 24461da177e4SLinus Torvalds "tmpfs: No value for mount option '%s'\n", 24471da177e4SLinus Torvalds this_char); 244849cd0a5cSGreg Thelen goto error; 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 24521da177e4SLinus Torvalds unsigned long long size; 24531da177e4SLinus Torvalds size = memparse(value,&rest); 24541da177e4SLinus Torvalds if (*rest == '%') { 24551da177e4SLinus Torvalds size <<= PAGE_SHIFT; 24561da177e4SLinus Torvalds size *= totalram_pages; 24571da177e4SLinus Torvalds do_div(size, 100); 24581da177e4SLinus Torvalds rest++; 24591da177e4SLinus Torvalds } 24601da177e4SLinus Torvalds if (*rest) 24611da177e4SLinus Torvalds goto bad_val; 2462680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 2463680d794bSakpm@linux-foundation.org DIV_ROUND_UP(size, PAGE_CACHE_SIZE); 24641da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 2465680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 24661da177e4SLinus Torvalds if (*rest) 24671da177e4SLinus Torvalds goto bad_val; 24681da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 2469680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 24701da177e4SLinus Torvalds if (*rest) 24711da177e4SLinus Torvalds goto bad_val; 24721da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 2473680d794bSakpm@linux-foundation.org if (remount) 24741da177e4SLinus Torvalds continue; 2475680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 24761da177e4SLinus Torvalds if (*rest) 24771da177e4SLinus Torvalds goto bad_val; 24781da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 2479680d794bSakpm@linux-foundation.org if (remount) 24801da177e4SLinus Torvalds continue; 24818751e039SEric W. Biederman uid = simple_strtoul(value, &rest, 0); 24821da177e4SLinus Torvalds if (*rest) 24831da177e4SLinus Torvalds goto bad_val; 24848751e039SEric W. Biederman sbinfo->uid = make_kuid(current_user_ns(), uid); 24858751e039SEric W. Biederman if (!uid_valid(sbinfo->uid)) 24868751e039SEric W. Biederman goto bad_val; 24871da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 2488680d794bSakpm@linux-foundation.org if (remount) 24891da177e4SLinus Torvalds continue; 24908751e039SEric W. Biederman gid = simple_strtoul(value, &rest, 0); 24911da177e4SLinus Torvalds if (*rest) 24921da177e4SLinus Torvalds goto bad_val; 24938751e039SEric W. Biederman sbinfo->gid = make_kgid(current_user_ns(), gid); 24948751e039SEric W. Biederman if (!gid_valid(sbinfo->gid)) 24958751e039SEric W. Biederman goto bad_val; 24967339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 249749cd0a5cSGreg Thelen mpol_put(mpol); 249849cd0a5cSGreg Thelen mpol = NULL; 249949cd0a5cSGreg Thelen if (mpol_parse_str(value, &mpol)) 25007339ff83SRobin Holt goto bad_val; 25011da177e4SLinus Torvalds } else { 25021da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad mount option %s\n", 25031da177e4SLinus Torvalds this_char); 250449cd0a5cSGreg Thelen goto error; 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds } 250749cd0a5cSGreg Thelen sbinfo->mpol = mpol; 25081da177e4SLinus Torvalds return 0; 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds bad_val: 25111da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", 25121da177e4SLinus Torvalds value, this_char); 251349cd0a5cSGreg Thelen error: 251449cd0a5cSGreg Thelen mpol_put(mpol); 25151da177e4SLinus Torvalds return 1; 25161da177e4SLinus Torvalds 25171da177e4SLinus Torvalds } 25181da177e4SLinus Torvalds 25191da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 25201da177e4SLinus Torvalds { 25211da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2522680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 25230edd73b3SHugh Dickins unsigned long inodes; 25240edd73b3SHugh Dickins int error = -EINVAL; 25251da177e4SLinus Torvalds 25265f00110fSGreg Thelen config.mpol = NULL; 2527680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 25280edd73b3SHugh Dickins return error; 25290edd73b3SHugh Dickins 25300edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 25310edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 25327e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 25330edd73b3SHugh Dickins goto out; 2534680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 25350edd73b3SHugh Dickins goto out; 25360edd73b3SHugh Dickins /* 253754af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 25380edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 25390edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 25400edd73b3SHugh Dickins */ 2541680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 25420edd73b3SHugh Dickins goto out; 2543680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 25440edd73b3SHugh Dickins goto out; 25450edd73b3SHugh Dickins 25460edd73b3SHugh Dickins error = 0; 2547680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 2548680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 2549680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 255071fe804bSLee Schermerhorn 25515f00110fSGreg Thelen /* 25525f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 25535f00110fSGreg Thelen */ 25545f00110fSGreg Thelen if (config.mpol) { 255571fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 255671fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 25575f00110fSGreg Thelen } 25580edd73b3SHugh Dickins out: 25590edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 25600edd73b3SHugh Dickins return error; 25611da177e4SLinus Torvalds } 2562680d794bSakpm@linux-foundation.org 256334c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 2564680d794bSakpm@linux-foundation.org { 256534c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 2566680d794bSakpm@linux-foundation.org 2567680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 2568680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 2569680d794bSakpm@linux-foundation.org sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); 2570680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 2571680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 2572680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 257309208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 25748751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 25758751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 25768751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 25778751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 25788751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 25798751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 258071fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 2581680d794bSakpm@linux-foundation.org return 0; 2582680d794bSakpm@linux-foundation.org } 2583680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 25861da177e4SLinus Torvalds { 2587602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2588602586a8SHugh Dickins 2589602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 259049cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 2591602586a8SHugh Dickins kfree(sbinfo); 25921da177e4SLinus Torvalds sb->s_fs_info = NULL; 25931da177e4SLinus Torvalds } 25941da177e4SLinus Torvalds 25952b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 25961da177e4SLinus Torvalds { 25971da177e4SLinus Torvalds struct inode *inode; 25980edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 2599680d794bSakpm@linux-foundation.org int err = -ENOMEM; 2600680d794bSakpm@linux-foundation.org 2601680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 2602425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 2603680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 2604680d794bSakpm@linux-foundation.org if (!sbinfo) 2605680d794bSakpm@linux-foundation.org return -ENOMEM; 2606680d794bSakpm@linux-foundation.org 2607680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 260876aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 260976aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 2610680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 26111da177e4SLinus Torvalds 26120edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 26131da177e4SLinus Torvalds /* 26141da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 26151da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 26161da177e4SLinus Torvalds * but the internal instance is left unlimited. 26171da177e4SLinus Torvalds */ 26181da177e4SLinus Torvalds if (!(sb->s_flags & MS_NOUSER)) { 2619680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 2620680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 2621680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 2622680d794bSakpm@linux-foundation.org err = -EINVAL; 2623680d794bSakpm@linux-foundation.org goto failed; 2624680d794bSakpm@linux-foundation.org } 26251da177e4SLinus Torvalds } 262691828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 26272f6e38f3SHugh Dickins sb->s_flags |= MS_NOSEC; 26280edd73b3SHugh Dickins #else 26290edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 26300edd73b3SHugh Dickins #endif 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 2633602586a8SHugh Dickins if (percpu_counter_init(&sbinfo->used_blocks, 0)) 2634602586a8SHugh Dickins goto failed; 2635680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 26361da177e4SLinus Torvalds 2637285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 26381da177e4SLinus Torvalds sb->s_blocksize = PAGE_CACHE_SIZE; 26391da177e4SLinus Torvalds sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 26401da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 26411da177e4SLinus Torvalds sb->s_op = &shmem_ops; 2642cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 2643b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 264439f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 2645b09e0fa4SEric Paris #endif 2646b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 264739f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 264839f0247dSAndreas Gruenbacher #endif 26490edd73b3SHugh Dickins 2650454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 26511da177e4SLinus Torvalds if (!inode) 26521da177e4SLinus Torvalds goto failed; 2653680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 2654680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 2655318ceed0SAl Viro sb->s_root = d_make_root(inode); 2656318ceed0SAl Viro if (!sb->s_root) 265748fde701SAl Viro goto failed; 26581da177e4SLinus Torvalds return 0; 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds failed: 26611da177e4SLinus Torvalds shmem_put_super(sb); 26621da177e4SLinus Torvalds return err; 26631da177e4SLinus Torvalds } 26641da177e4SLinus Torvalds 2665fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 26681da177e4SLinus Torvalds { 266941ffe5d5SHugh Dickins struct shmem_inode_info *info; 267041ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 267141ffe5d5SHugh Dickins if (!info) 26721da177e4SLinus Torvalds return NULL; 267341ffe5d5SHugh Dickins return &info->vfs_inode; 26741da177e4SLinus Torvalds } 26751da177e4SLinus Torvalds 267641ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 2677fa0d7e3dSNick Piggin { 2678fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 2679fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 2680fa0d7e3dSNick Piggin } 2681fa0d7e3dSNick Piggin 26821da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 26831da177e4SLinus Torvalds { 268409208d15SAl Viro if (S_ISREG(inode->i_mode)) 26851da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 268641ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds 268941ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 26901da177e4SLinus Torvalds { 269141ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 269241ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds 269541ffe5d5SHugh Dickins static int shmem_init_inodecache(void) 26961da177e4SLinus Torvalds { 26971da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 26981da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 269941ffe5d5SHugh Dickins 0, SLAB_PANIC, shmem_init_inode); 27001da177e4SLinus Torvalds return 0; 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds 270341ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 27041da177e4SLinus Torvalds { 27051a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 27061da177e4SLinus Torvalds } 27071da177e4SLinus Torvalds 2708f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 27091da177e4SLinus Torvalds .writepage = shmem_writepage, 271076719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 27111da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2712800d15a5SNick Piggin .write_begin = shmem_write_begin, 2713800d15a5SNick Piggin .write_end = shmem_write_end, 27141da177e4SLinus Torvalds #endif 2715304dbdb7SLee Schermerhorn .migratepage = migrate_page, 2716aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 27171da177e4SLinus Torvalds }; 27181da177e4SLinus Torvalds 271915ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 27201da177e4SLinus Torvalds .mmap = shmem_mmap, 27211da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2722220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 2723bcd78e49SHugh Dickins .read = do_sync_read, 27245402b976SHugh Dickins .write = do_sync_write, 2725bcd78e49SHugh Dickins .aio_read = shmem_file_aio_read, 27265402b976SHugh Dickins .aio_write = generic_file_aio_write, 27271b061d92SChristoph Hellwig .fsync = noop_fsync, 2728708e3508SHugh Dickins .splice_read = shmem_file_splice_read, 2729ae976416SHugh Dickins .splice_write = generic_file_splice_write, 273083e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 27311da177e4SLinus Torvalds #endif 27321da177e4SLinus Torvalds }; 27331da177e4SLinus Torvalds 273492e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 273594c1e62dSHugh Dickins .setattr = shmem_setattr, 2736b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2737b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2738b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2739b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2740b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2741b09e0fa4SEric Paris #endif 27421da177e4SLinus Torvalds }; 27431da177e4SLinus Torvalds 274492e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 27451da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 27461da177e4SLinus Torvalds .create = shmem_create, 27471da177e4SLinus Torvalds .lookup = simple_lookup, 27481da177e4SLinus Torvalds .link = shmem_link, 27491da177e4SLinus Torvalds .unlink = shmem_unlink, 27501da177e4SLinus Torvalds .symlink = shmem_symlink, 27511da177e4SLinus Torvalds .mkdir = shmem_mkdir, 27521da177e4SLinus Torvalds .rmdir = shmem_rmdir, 27531da177e4SLinus Torvalds .mknod = shmem_mknod, 27541da177e4SLinus Torvalds .rename = shmem_rename, 275560545d0dSAl Viro .tmpfile = shmem_tmpfile, 27561da177e4SLinus Torvalds #endif 2757b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2758b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2759b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2760b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2761b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2762b09e0fa4SEric Paris #endif 276339f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 276494c1e62dSHugh Dickins .setattr = shmem_setattr, 276539f0247dSAndreas Gruenbacher #endif 276639f0247dSAndreas Gruenbacher }; 276739f0247dSAndreas Gruenbacher 276892e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 2769b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2770b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2771b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2772b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2773b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2774b09e0fa4SEric Paris #endif 277539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 277694c1e62dSHugh Dickins .setattr = shmem_setattr, 277739f0247dSAndreas Gruenbacher #endif 27781da177e4SLinus Torvalds }; 27791da177e4SLinus Torvalds 2780759b9775SHugh Dickins static const struct super_operations shmem_ops = { 27811da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 27821da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 27831da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 27841da177e4SLinus Torvalds .statfs = shmem_statfs, 27851da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 2786680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 27871da177e4SLinus Torvalds #endif 27881f895f75SAl Viro .evict_inode = shmem_evict_inode, 27891da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 27901da177e4SLinus Torvalds .put_super = shmem_put_super, 27911da177e4SLinus Torvalds }; 27921da177e4SLinus Torvalds 2793f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 279454cb8821SNick Piggin .fault = shmem_fault, 27951da177e4SLinus Torvalds #ifdef CONFIG_NUMA 27961da177e4SLinus Torvalds .set_policy = shmem_set_policy, 27971da177e4SLinus Torvalds .get_policy = shmem_get_policy, 27981da177e4SLinus Torvalds #endif 27990b173bc4SKonstantin Khlebnikov .remap_pages = generic_file_remap_pages, 28001da177e4SLinus Torvalds }; 28011da177e4SLinus Torvalds 28023c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 28033c26ff6eSAl Viro int flags, const char *dev_name, void *data) 28041da177e4SLinus Torvalds { 28053c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 28061da177e4SLinus Torvalds } 28071da177e4SLinus Torvalds 280841ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 28091da177e4SLinus Torvalds .owner = THIS_MODULE, 28101da177e4SLinus Torvalds .name = "tmpfs", 28113c26ff6eSAl Viro .mount = shmem_mount, 28121da177e4SLinus Torvalds .kill_sb = kill_litter_super, 28132b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 28141da177e4SLinus Torvalds }; 28151da177e4SLinus Torvalds 281641ffe5d5SHugh Dickins int __init shmem_init(void) 28171da177e4SLinus Torvalds { 28181da177e4SLinus Torvalds int error; 28191da177e4SLinus Torvalds 2820e0bf68ddSPeter Zijlstra error = bdi_init(&shmem_backing_dev_info); 2821e0bf68ddSPeter Zijlstra if (error) 2822e0bf68ddSPeter Zijlstra goto out4; 2823e0bf68ddSPeter Zijlstra 282441ffe5d5SHugh Dickins error = shmem_init_inodecache(); 28251da177e4SLinus Torvalds if (error) 28261da177e4SLinus Torvalds goto out3; 28271da177e4SLinus Torvalds 282841ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 28291da177e4SLinus Torvalds if (error) { 28301da177e4SLinus Torvalds printk(KERN_ERR "Could not register tmpfs\n"); 28311da177e4SLinus Torvalds goto out2; 28321da177e4SLinus Torvalds } 283395dc112aSGreg Kroah-Hartman 283441ffe5d5SHugh Dickins shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER, 283541ffe5d5SHugh Dickins shmem_fs_type.name, NULL); 28361da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 28371da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 28381da177e4SLinus Torvalds printk(KERN_ERR "Could not kern_mount tmpfs\n"); 28391da177e4SLinus Torvalds goto out1; 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds return 0; 28421da177e4SLinus Torvalds 28431da177e4SLinus Torvalds out1: 284441ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 28451da177e4SLinus Torvalds out2: 284641ffe5d5SHugh Dickins shmem_destroy_inodecache(); 28471da177e4SLinus Torvalds out3: 2848e0bf68ddSPeter Zijlstra bdi_destroy(&shmem_backing_dev_info); 2849e0bf68ddSPeter Zijlstra out4: 28501da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 28511da177e4SLinus Torvalds return error; 28521da177e4SLinus Torvalds } 2853853ac43aSMatt Mackall 2854853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 2855853ac43aSMatt Mackall 2856853ac43aSMatt Mackall /* 2857853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 2858853ac43aSMatt Mackall * 2859853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 2860853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 2861853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 2862853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 2863853ac43aSMatt Mackall */ 2864853ac43aSMatt Mackall 286541ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 2866853ac43aSMatt Mackall .name = "tmpfs", 28673c26ff6eSAl Viro .mount = ramfs_mount, 2868853ac43aSMatt Mackall .kill_sb = kill_litter_super, 28692b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 2870853ac43aSMatt Mackall }; 2871853ac43aSMatt Mackall 287241ffe5d5SHugh Dickins int __init shmem_init(void) 2873853ac43aSMatt Mackall { 287441ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 2875853ac43aSMatt Mackall 287641ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 2877853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 2878853ac43aSMatt Mackall 2879853ac43aSMatt Mackall return 0; 2880853ac43aSMatt Mackall } 2881853ac43aSMatt Mackall 288241ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 2883853ac43aSMatt Mackall { 2884853ac43aSMatt Mackall return 0; 2885853ac43aSMatt Mackall } 2886853ac43aSMatt Mackall 28873f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 28883f96b79aSHugh Dickins { 28893f96b79aSHugh Dickins return 0; 28903f96b79aSHugh Dickins } 28913f96b79aSHugh Dickins 289224513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 289324513264SHugh Dickins { 289424513264SHugh Dickins } 289524513264SHugh Dickins 289641ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 289794c1e62dSHugh Dickins { 289841ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 289994c1e62dSHugh Dickins } 290094c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 290194c1e62dSHugh Dickins 2902853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 29030b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 2904454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 29050b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 29060b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 2907853ac43aSMatt Mackall 2908853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 2909853ac43aSMatt Mackall 2910853ac43aSMatt Mackall /* common code */ 29111da177e4SLinus Torvalds 29123451538aSAl Viro static char *shmem_dname(struct dentry *dentry, char *buffer, int buflen) 29133451538aSAl Viro { 29143451538aSAl Viro return dynamic_dname(dentry, buffer, buflen, "/%s (deleted)", 29153451538aSAl Viro dentry->d_name.name); 29163451538aSAl Viro } 29173451538aSAl Viro 29183451538aSAl Viro static struct dentry_operations anon_ops = { 29193451538aSAl Viro .d_dname = shmem_dname 29203451538aSAl Viro }; 29213451538aSAl Viro 292246711810SRandy Dunlap /** 29231da177e4SLinus Torvalds * shmem_file_setup - get an unlinked file living in tmpfs 29241da177e4SLinus Torvalds * @name: name for dentry (to be seen in /proc/<pid>/maps 29251da177e4SLinus Torvalds * @size: size to be set for the file 29260b0a0806SHugh Dickins * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 29271da177e4SLinus Torvalds */ 2928168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 29291da177e4SLinus Torvalds { 29306b4d0b27SAl Viro struct file *res; 29311da177e4SLinus Torvalds struct inode *inode; 29322c48b9c4SAl Viro struct path path; 29333451538aSAl Viro struct super_block *sb; 29341da177e4SLinus Torvalds struct qstr this; 29351da177e4SLinus Torvalds 29361da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 29376b4d0b27SAl Viro return ERR_CAST(shm_mnt); 29381da177e4SLinus Torvalds 2939285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 29401da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 29411da177e4SLinus Torvalds 29421da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 29431da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 29441da177e4SLinus Torvalds 29456b4d0b27SAl Viro res = ERR_PTR(-ENOMEM); 29461da177e4SLinus Torvalds this.name = name; 29471da177e4SLinus Torvalds this.len = strlen(name); 29481da177e4SLinus Torvalds this.hash = 0; /* will go */ 29493451538aSAl Viro sb = shm_mnt->mnt_sb; 29503451538aSAl Viro path.dentry = d_alloc_pseudo(sb, &this); 29512c48b9c4SAl Viro if (!path.dentry) 29521da177e4SLinus Torvalds goto put_memory; 29533451538aSAl Viro d_set_d_op(path.dentry, &anon_ops); 29542c48b9c4SAl Viro path.mnt = mntget(shm_mnt); 29551da177e4SLinus Torvalds 29566b4d0b27SAl Viro res = ERR_PTR(-ENOSPC); 29573451538aSAl Viro inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 29581da177e4SLinus Torvalds if (!inode) 29594b42af81SAl Viro goto put_dentry; 29601da177e4SLinus Torvalds 29612c48b9c4SAl Viro d_instantiate(path.dentry, inode); 29621da177e4SLinus Torvalds inode->i_size = size; 29636d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 296426567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 296526567cdbSAl Viro if (IS_ERR(res)) 29664b42af81SAl Viro goto put_dentry; 29674b42af81SAl Viro 29686b4d0b27SAl Viro res = alloc_file(&path, FMODE_WRITE | FMODE_READ, 29694b42af81SAl Viro &shmem_file_operations); 29706b4d0b27SAl Viro if (IS_ERR(res)) 29714b42af81SAl Viro goto put_dentry; 29724b42af81SAl Viro 29736b4d0b27SAl Viro return res; 29741da177e4SLinus Torvalds 29751da177e4SLinus Torvalds put_dentry: 29762c48b9c4SAl Viro path_put(&path); 29771da177e4SLinus Torvalds put_memory: 29781da177e4SLinus Torvalds shmem_unacct_size(flags, size); 29796b4d0b27SAl Viro return res; 29801da177e4SLinus Torvalds } 2981395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 29821da177e4SLinus Torvalds 298346711810SRandy Dunlap /** 29841da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 29851da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 29861da177e4SLinus Torvalds */ 29871da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 29881da177e4SLinus Torvalds { 29891da177e4SLinus Torvalds struct file *file; 29901da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 29911da177e4SLinus Torvalds 29921da177e4SLinus Torvalds file = shmem_file_setup("dev/zero", size, vma->vm_flags); 29931da177e4SLinus Torvalds if (IS_ERR(file)) 29941da177e4SLinus Torvalds return PTR_ERR(file); 29951da177e4SLinus Torvalds 29961da177e4SLinus Torvalds if (vma->vm_file) 29971da177e4SLinus Torvalds fput(vma->vm_file); 29981da177e4SLinus Torvalds vma->vm_file = file; 29991da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 30001da177e4SLinus Torvalds return 0; 30011da177e4SLinus Torvalds } 3002d9d90e5eSHugh Dickins 3003d9d90e5eSHugh Dickins /** 3004d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 3005d9d90e5eSHugh Dickins * @mapping: the page's address_space 3006d9d90e5eSHugh Dickins * @index: the page index 3007d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 3008d9d90e5eSHugh Dickins * 3009d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 3010d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 3011d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 3012d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 3013d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 3014d9d90e5eSHugh Dickins * 301568da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 301668da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 3017d9d90e5eSHugh Dickins */ 3018d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 3019d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 3020d9d90e5eSHugh Dickins { 302168da9f05SHugh Dickins #ifdef CONFIG_SHMEM 302268da9f05SHugh Dickins struct inode *inode = mapping->host; 30239276aad6SHugh Dickins struct page *page; 302468da9f05SHugh Dickins int error; 302568da9f05SHugh Dickins 302668da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 302768da9f05SHugh Dickins error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL); 302868da9f05SHugh Dickins if (error) 302968da9f05SHugh Dickins page = ERR_PTR(error); 303068da9f05SHugh Dickins else 303168da9f05SHugh Dickins unlock_page(page); 303268da9f05SHugh Dickins return page; 303368da9f05SHugh Dickins #else 303468da9f05SHugh Dickins /* 303568da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 303668da9f05SHugh Dickins */ 3037d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 303868da9f05SHugh Dickins #endif 3039d9d90e5eSHugh Dickins } 3040d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 3041