11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Resizable virtual memory filesystem for Linux. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000 Linus Torvalds. 51da177e4SLinus Torvalds * 2000 Transmeta Corp. 61da177e4SLinus Torvalds * 2000-2001 Christoph Rohland 71da177e4SLinus Torvalds * 2000-2001 SAP AG 81da177e4SLinus Torvalds * 2002 Red Hat Inc. 96922c0c7SHugh Dickins * Copyright (C) 2002-2011 Hugh Dickins. 106922c0c7SHugh Dickins * Copyright (C) 2011 Google Inc. 110edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 121da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Extended attribute support for tmpfs: 151da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 161da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 171da177e4SLinus Torvalds * 18853ac43aSMatt Mackall * tiny-shmem: 19853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 20853ac43aSMatt Mackall * 211da177e4SLinus Torvalds * This file is released under the GPL. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 24853ac43aSMatt Mackall #include <linux/fs.h> 25853ac43aSMatt Mackall #include <linux/init.h> 26853ac43aSMatt Mackall #include <linux/vfs.h> 27853ac43aSMatt Mackall #include <linux/mount.h> 28caefba17SHugh Dickins #include <linux/pagemap.h> 29853ac43aSMatt Mackall #include <linux/file.h> 30853ac43aSMatt Mackall #include <linux/mm.h> 31b95f1b31SPaul Gortmaker #include <linux/export.h> 32853ac43aSMatt Mackall #include <linux/swap.h> 33853ac43aSMatt Mackall 34853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 35853ac43aSMatt Mackall 36853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 391da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 401da177e4SLinus Torvalds * which makes it a completely usable filesystem. 411da177e4SLinus Torvalds */ 421da177e4SLinus Torvalds 4339f0247dSAndreas Gruenbacher #include <linux/xattr.h> 44a5694255SChristoph Hellwig #include <linux/exportfs.h> 451c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 4639f0247dSAndreas Gruenbacher #include <linux/generic_acl.h> 471da177e4SLinus Torvalds #include <linux/mman.h> 481da177e4SLinus Torvalds #include <linux/string.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 501da177e4SLinus Torvalds #include <linux/backing-dev.h> 511da177e4SLinus Torvalds #include <linux/shmem_fs.h> 521da177e4SLinus Torvalds #include <linux/writeback.h> 531da177e4SLinus Torvalds #include <linux/blkdev.h> 54bda97eabSHugh Dickins #include <linux/pagevec.h> 5541ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 5683e4fa9cSHugh Dickins #include <linux/falloc.h> 57708e3508SHugh Dickins #include <linux/splice.h> 581da177e4SLinus Torvalds #include <linux/security.h> 591da177e4SLinus Torvalds #include <linux/swapops.h> 601da177e4SLinus Torvalds #include <linux/mempolicy.h> 611da177e4SLinus Torvalds #include <linux/namei.h> 62b00dc3adSHugh Dickins #include <linux/ctype.h> 63304dbdb7SLee Schermerhorn #include <linux/migrate.h> 64c1f60a5aSChristoph Lameter #include <linux/highmem.h> 65680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 6692562927SMimi Zohar #include <linux/magic.h> 67304dbdb7SLee Schermerhorn 681da177e4SLinus Torvalds #include <asm/uaccess.h> 691da177e4SLinus Torvalds #include <asm/pgtable.h> 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) 721da177e4SLinus Torvalds #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 751da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 761da177e4SLinus Torvalds 7769f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 7869f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 7969f07ec9SHugh Dickins 801aac1400SHugh Dickins /* 811aac1400SHugh Dickins * shmem_fallocate and shmem_writepage communicate via inode->i_private 821aac1400SHugh Dickins * (with i_mutex making sure that it has only one user at a time): 831aac1400SHugh Dickins * we would prefer not to enlarge the shmem inode just for that. 841aac1400SHugh Dickins */ 851aac1400SHugh Dickins struct shmem_falloc { 861aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 871aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 881aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 891aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 901aac1400SHugh Dickins }; 911aac1400SHugh Dickins 92285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */ 931da177e4SLinus Torvalds enum sgp_type { 941da177e4SLinus Torvalds SGP_READ, /* don't exceed i_size, don't allocate page */ 951da177e4SLinus Torvalds SGP_CACHE, /* don't exceed i_size, may allocate page */ 96a0ee5ec5SHugh Dickins SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ 971635f6a7SHugh Dickins SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */ 981635f6a7SHugh Dickins SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */ 991da177e4SLinus Torvalds }; 1001da177e4SLinus Torvalds 101b76db735SAndrew Morton #ifdef CONFIG_TMPFS 102680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 103680d794bSakpm@linux-foundation.org { 104680d794bSakpm@linux-foundation.org return totalram_pages / 2; 105680d794bSakpm@linux-foundation.org } 106680d794bSakpm@linux-foundation.org 107680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 108680d794bSakpm@linux-foundation.org { 109680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 110680d794bSakpm@linux-foundation.org } 111b76db735SAndrew Morton #endif 112680d794bSakpm@linux-foundation.org 113bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp); 114bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 115bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index); 11668da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 11768da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type); 11868da9f05SHugh Dickins 11968da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index, 12068da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, int *fault_type) 12168da9f05SHugh Dickins { 12268da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 12368da9f05SHugh Dickins mapping_gfp_mask(inode->i_mapping), fault_type); 12468da9f05SHugh Dickins } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1271da177e4SLinus Torvalds { 1281da177e4SLinus Torvalds return sb->s_fs_info; 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* 1321da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1331da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1341da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1351da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1361da177e4SLinus Torvalds */ 1371da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1381da177e4SLinus Torvalds { 1390b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 140191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1441da177e4SLinus Torvalds { 1450b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1461da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* 1501da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 1511da177e4SLinus Torvalds * pages are allocated, in order to allow huge sparse files. 1521da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1531da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1541da177e4SLinus Torvalds */ 1551da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags) 1561da177e4SLinus Torvalds { 1570b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 158191c5424SAl Viro security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1621da177e4SLinus Torvalds { 1630b0a0806SHugh Dickins if (flags & VM_NORESERVE) 1641da177e4SLinus Torvalds vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 167759b9775SHugh Dickins static const struct super_operations shmem_ops; 168f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 16915ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 17092e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 17192e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 17292e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 173f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 1741da177e4SLinus Torvalds 1756c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info __read_mostly = { 1761da177e4SLinus Torvalds .ra_pages = 0, /* No readahead */ 1774f98a2feSRik van Riel .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, 1781da177e4SLinus Torvalds }; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 181cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 1821da177e4SLinus Torvalds 1835b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 1845b04c689SPavel Emelyanov { 1855b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1865b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 1875b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 1885b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 1895b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1905b04c689SPavel Emelyanov return -ENOSPC; 1915b04c689SPavel Emelyanov } 1925b04c689SPavel Emelyanov sbinfo->free_inodes--; 1935b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1945b04c689SPavel Emelyanov } 1955b04c689SPavel Emelyanov return 0; 1965b04c689SPavel Emelyanov } 1975b04c689SPavel Emelyanov 1985b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 1995b04c689SPavel Emelyanov { 2005b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2015b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2025b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2035b04c689SPavel Emelyanov sbinfo->free_inodes++; 2045b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2055b04c689SPavel Emelyanov } 2065b04c689SPavel Emelyanov } 2075b04c689SPavel Emelyanov 20846711810SRandy Dunlap /** 20941ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2101da177e4SLinus Torvalds * @inode: inode to recalc 2111da177e4SLinus Torvalds * 2121da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2131da177e4SLinus Torvalds * undirtied hole pages behind our back. 2141da177e4SLinus Torvalds * 2151da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2161da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2171da177e4SLinus Torvalds * 2181da177e4SLinus Torvalds * It has to be called with the spinlock held. 2191da177e4SLinus Torvalds */ 2201da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2231da177e4SLinus Torvalds long freed; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2261da177e4SLinus Torvalds if (freed > 0) { 22754af6042SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 22854af6042SHugh Dickins if (sbinfo->max_blocks) 22954af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -freed); 2301da177e4SLinus Torvalds info->alloced -= freed; 23154af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2321da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2367a5d0fbbSHugh Dickins /* 2377a5d0fbbSHugh Dickins * Replace item expected in radix tree by a new item, while holding tree lock. 2387a5d0fbbSHugh Dickins */ 2397a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping, 2407a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 2417a5d0fbbSHugh Dickins { 2427a5d0fbbSHugh Dickins void **pslot; 2437a5d0fbbSHugh Dickins void *item = NULL; 2447a5d0fbbSHugh Dickins 2457a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 2467a5d0fbbSHugh Dickins pslot = radix_tree_lookup_slot(&mapping->page_tree, index); 2477a5d0fbbSHugh Dickins if (pslot) 2487a5d0fbbSHugh Dickins item = radix_tree_deref_slot_protected(pslot, 2497a5d0fbbSHugh Dickins &mapping->tree_lock); 2507a5d0fbbSHugh Dickins if (item != expected) 2517a5d0fbbSHugh Dickins return -ENOENT; 2527a5d0fbbSHugh Dickins if (replacement) 2537a5d0fbbSHugh Dickins radix_tree_replace_slot(pslot, replacement); 2547a5d0fbbSHugh Dickins else 2557a5d0fbbSHugh Dickins radix_tree_delete(&mapping->page_tree, index); 2567a5d0fbbSHugh Dickins return 0; 2577a5d0fbbSHugh Dickins } 2587a5d0fbbSHugh Dickins 2597a5d0fbbSHugh Dickins /* 260d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 261d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 262d1899228SHugh Dickins * 263d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 264d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 265d1899228SHugh Dickins */ 266d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 267d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 268d1899228SHugh Dickins { 269d1899228SHugh Dickins void *item; 270d1899228SHugh Dickins 271d1899228SHugh Dickins rcu_read_lock(); 272d1899228SHugh Dickins item = radix_tree_lookup(&mapping->page_tree, index); 273d1899228SHugh Dickins rcu_read_unlock(); 274d1899228SHugh Dickins return item == swp_to_radix_entry(swap); 275d1899228SHugh Dickins } 276d1899228SHugh Dickins 277d1899228SHugh Dickins /* 27846f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 27946f65ec1SHugh Dickins */ 28046f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 28146f65ec1SHugh Dickins struct address_space *mapping, 28246f65ec1SHugh Dickins pgoff_t index, gfp_t gfp, void *expected) 28346f65ec1SHugh Dickins { 284b065b432SHugh Dickins int error; 28546f65ec1SHugh Dickins 28646f65ec1SHugh Dickins VM_BUG_ON(!PageLocked(page)); 28746f65ec1SHugh Dickins VM_BUG_ON(!PageSwapBacked(page)); 28846f65ec1SHugh Dickins 28946f65ec1SHugh Dickins page_cache_get(page); 29046f65ec1SHugh Dickins page->mapping = mapping; 29146f65ec1SHugh Dickins page->index = index; 29246f65ec1SHugh Dickins 29346f65ec1SHugh Dickins spin_lock_irq(&mapping->tree_lock); 29446f65ec1SHugh Dickins if (!expected) 295b065b432SHugh Dickins error = radix_tree_insert(&mapping->page_tree, index, page); 29646f65ec1SHugh Dickins else 297b065b432SHugh Dickins error = shmem_radix_tree_replace(mapping, index, expected, 298b065b432SHugh Dickins page); 29946f65ec1SHugh Dickins if (!error) { 30046f65ec1SHugh Dickins mapping->nrpages++; 30146f65ec1SHugh Dickins __inc_zone_page_state(page, NR_FILE_PAGES); 30246f65ec1SHugh Dickins __inc_zone_page_state(page, NR_SHMEM); 30346f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 30446f65ec1SHugh Dickins } else { 30546f65ec1SHugh Dickins page->mapping = NULL; 30646f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 30746f65ec1SHugh Dickins page_cache_release(page); 30846f65ec1SHugh Dickins } 30946f65ec1SHugh Dickins return error; 31046f65ec1SHugh Dickins } 31146f65ec1SHugh Dickins 31246f65ec1SHugh Dickins /* 3136922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 3146922c0c7SHugh Dickins */ 3156922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 3166922c0c7SHugh Dickins { 3176922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 3186922c0c7SHugh Dickins int error; 3196922c0c7SHugh Dickins 3206922c0c7SHugh Dickins spin_lock_irq(&mapping->tree_lock); 3216922c0c7SHugh Dickins error = shmem_radix_tree_replace(mapping, page->index, page, radswap); 3226922c0c7SHugh Dickins page->mapping = NULL; 3236922c0c7SHugh Dickins mapping->nrpages--; 3246922c0c7SHugh Dickins __dec_zone_page_state(page, NR_FILE_PAGES); 3256922c0c7SHugh Dickins __dec_zone_page_state(page, NR_SHMEM); 3266922c0c7SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 3276922c0c7SHugh Dickins page_cache_release(page); 3286922c0c7SHugh Dickins BUG_ON(error); 3296922c0c7SHugh Dickins } 3306922c0c7SHugh Dickins 3316922c0c7SHugh Dickins /* 3327a5d0fbbSHugh Dickins * Like find_get_pages, but collecting swap entries as well as pages. 3337a5d0fbbSHugh Dickins */ 3347a5d0fbbSHugh Dickins static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping, 3357a5d0fbbSHugh Dickins pgoff_t start, unsigned int nr_pages, 3367a5d0fbbSHugh Dickins struct page **pages, pgoff_t *indices) 3377a5d0fbbSHugh Dickins { 338*860f2759SJohannes Weiner void **slot; 339*860f2759SJohannes Weiner unsigned int ret = 0; 340*860f2759SJohannes Weiner struct radix_tree_iter iter; 341*860f2759SJohannes Weiner 342*860f2759SJohannes Weiner if (!nr_pages) 343*860f2759SJohannes Weiner return 0; 3447a5d0fbbSHugh Dickins 3457a5d0fbbSHugh Dickins rcu_read_lock(); 3467a5d0fbbSHugh Dickins restart: 347*860f2759SJohannes Weiner radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { 3487a5d0fbbSHugh Dickins struct page *page; 3497a5d0fbbSHugh Dickins repeat: 350*860f2759SJohannes Weiner page = radix_tree_deref_slot(slot); 3517a5d0fbbSHugh Dickins if (unlikely(!page)) 3527a5d0fbbSHugh Dickins continue; 3537a5d0fbbSHugh Dickins if (radix_tree_exception(page)) { 3548079b1c8SHugh Dickins if (radix_tree_deref_retry(page)) 3557a5d0fbbSHugh Dickins goto restart; 3568079b1c8SHugh Dickins /* 3578079b1c8SHugh Dickins * Otherwise, we must be storing a swap entry 3588079b1c8SHugh Dickins * here as an exceptional entry: so return it 3598079b1c8SHugh Dickins * without attempting to raise page count. 3608079b1c8SHugh Dickins */ 3618079b1c8SHugh Dickins goto export; 3627a5d0fbbSHugh Dickins } 3637a5d0fbbSHugh Dickins if (!page_cache_get_speculative(page)) 3647a5d0fbbSHugh Dickins goto repeat; 3657a5d0fbbSHugh Dickins 3667a5d0fbbSHugh Dickins /* Has the page moved? */ 367*860f2759SJohannes Weiner if (unlikely(page != *slot)) { 3687a5d0fbbSHugh Dickins page_cache_release(page); 3697a5d0fbbSHugh Dickins goto repeat; 3707a5d0fbbSHugh Dickins } 3717a5d0fbbSHugh Dickins export: 372*860f2759SJohannes Weiner indices[ret] = iter.index; 3737a5d0fbbSHugh Dickins pages[ret] = page; 374*860f2759SJohannes Weiner if (++ret == nr_pages) 375*860f2759SJohannes Weiner break; 3767a5d0fbbSHugh Dickins } 3777a5d0fbbSHugh Dickins rcu_read_unlock(); 3787a5d0fbbSHugh Dickins return ret; 3797a5d0fbbSHugh Dickins } 3807a5d0fbbSHugh Dickins 3817a5d0fbbSHugh Dickins /* 3827a5d0fbbSHugh Dickins * Remove swap entry from radix tree, free the swap and its page cache. 3837a5d0fbbSHugh Dickins */ 3847a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 3857a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 3867a5d0fbbSHugh Dickins { 3877a5d0fbbSHugh Dickins int error; 3887a5d0fbbSHugh Dickins 3897a5d0fbbSHugh Dickins spin_lock_irq(&mapping->tree_lock); 3907a5d0fbbSHugh Dickins error = shmem_radix_tree_replace(mapping, index, radswap, NULL); 3917a5d0fbbSHugh Dickins spin_unlock_irq(&mapping->tree_lock); 3927a5d0fbbSHugh Dickins if (!error) 3937a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 3947a5d0fbbSHugh Dickins return error; 3957a5d0fbbSHugh Dickins } 3967a5d0fbbSHugh Dickins 3977a5d0fbbSHugh Dickins /* 3987a5d0fbbSHugh Dickins * Pagevec may contain swap entries, so shuffle up pages before releasing. 3997a5d0fbbSHugh Dickins */ 40024513264SHugh Dickins static void shmem_deswap_pagevec(struct pagevec *pvec) 4017a5d0fbbSHugh Dickins { 4027a5d0fbbSHugh Dickins int i, j; 4037a5d0fbbSHugh Dickins 4047a5d0fbbSHugh Dickins for (i = 0, j = 0; i < pagevec_count(pvec); i++) { 4057a5d0fbbSHugh Dickins struct page *page = pvec->pages[i]; 4067a5d0fbbSHugh Dickins if (!radix_tree_exceptional_entry(page)) 4077a5d0fbbSHugh Dickins pvec->pages[j++] = page; 4087a5d0fbbSHugh Dickins } 4097a5d0fbbSHugh Dickins pvec->nr = j; 41024513264SHugh Dickins } 41124513264SHugh Dickins 41224513264SHugh Dickins /* 41324513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 41424513264SHugh Dickins */ 41524513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 41624513264SHugh Dickins { 41724513264SHugh Dickins struct pagevec pvec; 41824513264SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 41924513264SHugh Dickins pgoff_t index = 0; 42024513264SHugh Dickins 42124513264SHugh Dickins pagevec_init(&pvec, 0); 42224513264SHugh Dickins /* 42324513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 42424513264SHugh Dickins */ 42524513264SHugh Dickins while (!mapping_unevictable(mapping)) { 42624513264SHugh Dickins /* 42724513264SHugh Dickins * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it 42824513264SHugh Dickins * has finished, if it hits a row of PAGEVEC_SIZE swap entries. 42924513264SHugh Dickins */ 43024513264SHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 43124513264SHugh Dickins PAGEVEC_SIZE, pvec.pages, indices); 43224513264SHugh Dickins if (!pvec.nr) 43324513264SHugh Dickins break; 43424513264SHugh Dickins index = indices[pvec.nr - 1] + 1; 43524513264SHugh Dickins shmem_deswap_pagevec(&pvec); 43624513264SHugh Dickins check_move_unevictable_pages(pvec.pages, pvec.nr); 43724513264SHugh Dickins pagevec_release(&pvec); 43824513264SHugh Dickins cond_resched(); 43924513264SHugh Dickins } 4407a5d0fbbSHugh Dickins } 4417a5d0fbbSHugh Dickins 4427a5d0fbbSHugh Dickins /* 4437a5d0fbbSHugh Dickins * Remove range of pages and swap entries from radix tree, and free them. 4441635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 4457a5d0fbbSHugh Dickins */ 4461635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 4471635f6a7SHugh Dickins bool unfalloc) 4481da177e4SLinus Torvalds { 449285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 4501da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 451285b2c4fSHugh Dickins pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 45283e4fa9cSHugh Dickins pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT; 45383e4fa9cSHugh Dickins unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1); 45483e4fa9cSHugh Dickins unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1); 455bda97eabSHugh Dickins struct pagevec pvec; 4567a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 4577a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 458285b2c4fSHugh Dickins pgoff_t index; 459bda97eabSHugh Dickins int i; 4601da177e4SLinus Torvalds 46183e4fa9cSHugh Dickins if (lend == -1) 46283e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 463bda97eabSHugh Dickins 464bda97eabSHugh Dickins pagevec_init(&pvec, 0); 465bda97eabSHugh Dickins index = start; 46683e4fa9cSHugh Dickins while (index < end) { 4677a5d0fbbSHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 46883e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 4697a5d0fbbSHugh Dickins pvec.pages, indices); 4707a5d0fbbSHugh Dickins if (!pvec.nr) 4717a5d0fbbSHugh Dickins break; 472bda97eabSHugh Dickins mem_cgroup_uncharge_start(); 473bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 474bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 475bda97eabSHugh Dickins 4767a5d0fbbSHugh Dickins index = indices[i]; 47783e4fa9cSHugh Dickins if (index >= end) 478bda97eabSHugh Dickins break; 479bda97eabSHugh Dickins 4807a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 4811635f6a7SHugh Dickins if (unfalloc) 4821635f6a7SHugh Dickins continue; 4837a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 4847a5d0fbbSHugh Dickins index, page); 4857a5d0fbbSHugh Dickins continue; 4867a5d0fbbSHugh Dickins } 4877a5d0fbbSHugh Dickins 488bda97eabSHugh Dickins if (!trylock_page(page)) 489bda97eabSHugh Dickins continue; 4901635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 4917a5d0fbbSHugh Dickins if (page->mapping == mapping) { 4927a5d0fbbSHugh Dickins VM_BUG_ON(PageWriteback(page)); 493bda97eabSHugh Dickins truncate_inode_page(mapping, page); 4947a5d0fbbSHugh Dickins } 4951635f6a7SHugh Dickins } 496bda97eabSHugh Dickins unlock_page(page); 497bda97eabSHugh Dickins } 49824513264SHugh Dickins shmem_deswap_pagevec(&pvec); 49924513264SHugh Dickins pagevec_release(&pvec); 500bda97eabSHugh Dickins mem_cgroup_uncharge_end(); 501bda97eabSHugh Dickins cond_resched(); 502bda97eabSHugh Dickins index++; 503bda97eabSHugh Dickins } 504bda97eabSHugh Dickins 50583e4fa9cSHugh Dickins if (partial_start) { 506bda97eabSHugh Dickins struct page *page = NULL; 507bda97eabSHugh Dickins shmem_getpage(inode, start - 1, &page, SGP_READ, NULL); 508bda97eabSHugh Dickins if (page) { 50983e4fa9cSHugh Dickins unsigned int top = PAGE_CACHE_SIZE; 51083e4fa9cSHugh Dickins if (start > end) { 51183e4fa9cSHugh Dickins top = partial_end; 51283e4fa9cSHugh Dickins partial_end = 0; 51383e4fa9cSHugh Dickins } 51483e4fa9cSHugh Dickins zero_user_segment(page, partial_start, top); 515bda97eabSHugh Dickins set_page_dirty(page); 516bda97eabSHugh Dickins unlock_page(page); 517bda97eabSHugh Dickins page_cache_release(page); 518bda97eabSHugh Dickins } 519bda97eabSHugh Dickins } 52083e4fa9cSHugh Dickins if (partial_end) { 52183e4fa9cSHugh Dickins struct page *page = NULL; 52283e4fa9cSHugh Dickins shmem_getpage(inode, end, &page, SGP_READ, NULL); 52383e4fa9cSHugh Dickins if (page) { 52483e4fa9cSHugh Dickins zero_user_segment(page, 0, partial_end); 52583e4fa9cSHugh Dickins set_page_dirty(page); 52683e4fa9cSHugh Dickins unlock_page(page); 52783e4fa9cSHugh Dickins page_cache_release(page); 52883e4fa9cSHugh Dickins } 52983e4fa9cSHugh Dickins } 53083e4fa9cSHugh Dickins if (start >= end) 53183e4fa9cSHugh Dickins return; 532bda97eabSHugh Dickins 533bda97eabSHugh Dickins index = start; 534bda97eabSHugh Dickins for ( ; ; ) { 535bda97eabSHugh Dickins cond_resched(); 5367a5d0fbbSHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 53783e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 5387a5d0fbbSHugh Dickins pvec.pages, indices); 5397a5d0fbbSHugh Dickins if (!pvec.nr) { 5401635f6a7SHugh Dickins if (index == start || unfalloc) 541bda97eabSHugh Dickins break; 542bda97eabSHugh Dickins index = start; 543bda97eabSHugh Dickins continue; 544bda97eabSHugh Dickins } 5451635f6a7SHugh Dickins if ((index == start || unfalloc) && indices[0] >= end) { 54624513264SHugh Dickins shmem_deswap_pagevec(&pvec); 54724513264SHugh Dickins pagevec_release(&pvec); 548bda97eabSHugh Dickins break; 549bda97eabSHugh Dickins } 550bda97eabSHugh Dickins mem_cgroup_uncharge_start(); 551bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 552bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 553bda97eabSHugh Dickins 5547a5d0fbbSHugh Dickins index = indices[i]; 55583e4fa9cSHugh Dickins if (index >= end) 556bda97eabSHugh Dickins break; 557bda97eabSHugh Dickins 5587a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 5591635f6a7SHugh Dickins if (unfalloc) 5601635f6a7SHugh Dickins continue; 5617a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 5627a5d0fbbSHugh Dickins index, page); 5637a5d0fbbSHugh Dickins continue; 5647a5d0fbbSHugh Dickins } 5657a5d0fbbSHugh Dickins 566bda97eabSHugh Dickins lock_page(page); 5671635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 5687a5d0fbbSHugh Dickins if (page->mapping == mapping) { 5697a5d0fbbSHugh Dickins VM_BUG_ON(PageWriteback(page)); 570bda97eabSHugh Dickins truncate_inode_page(mapping, page); 5717a5d0fbbSHugh Dickins } 5721635f6a7SHugh Dickins } 573bda97eabSHugh Dickins unlock_page(page); 574bda97eabSHugh Dickins } 57524513264SHugh Dickins shmem_deswap_pagevec(&pvec); 57624513264SHugh Dickins pagevec_release(&pvec); 577bda97eabSHugh Dickins mem_cgroup_uncharge_end(); 578bda97eabSHugh Dickins index++; 579bda97eabSHugh Dickins } 58094c1e62dSHugh Dickins 5811da177e4SLinus Torvalds spin_lock(&info->lock); 5827a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 5831da177e4SLinus Torvalds shmem_recalc_inode(inode); 5841da177e4SLinus Torvalds spin_unlock(&info->lock); 5851635f6a7SHugh Dickins } 5861da177e4SLinus Torvalds 5871635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 5881635f6a7SHugh Dickins { 5891635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 590285b2c4fSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 5911da177e4SLinus Torvalds } 59294c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 5931da177e4SLinus Torvalds 59494c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 5951da177e4SLinus Torvalds { 5961da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 5971da177e4SLinus Torvalds int error; 5981da177e4SLinus Torvalds 599db78b877SChristoph Hellwig error = inode_change_ok(inode, attr); 600db78b877SChristoph Hellwig if (error) 601db78b877SChristoph Hellwig return error; 602db78b877SChristoph Hellwig 60394c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 60494c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 60594c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 6063889e6e7Snpiggin@suse.de 60794c1e62dSHugh Dickins if (newsize != oldsize) { 60894c1e62dSHugh Dickins i_size_write(inode, newsize); 60994c1e62dSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 61094c1e62dSHugh Dickins } 61194c1e62dSHugh Dickins if (newsize < oldsize) { 61294c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 61394c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 61494c1e62dSHugh Dickins shmem_truncate_range(inode, newsize, (loff_t)-1); 61594c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 61694c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 61794c1e62dSHugh Dickins } 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 6206a1a90adSChristoph Hellwig setattr_copy(inode, attr); 62139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 622db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 6231c7c474cSChristoph Hellwig error = generic_acl_chmod(inode); 62439f0247dSAndreas Gruenbacher #endif 6251da177e4SLinus Torvalds return error; 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 6281f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 6311da177e4SLinus Torvalds 6323889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 6331da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 6341da177e4SLinus Torvalds inode->i_size = 0; 6353889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 6361da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 637cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 6381da177e4SLinus Torvalds list_del_init(&info->swaplist); 639cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 6401da177e4SLinus Torvalds } 64169f07ec9SHugh Dickins } else 64269f07ec9SHugh Dickins kfree(info->symlink); 643b09e0fa4SEric Paris 64438f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 6450f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 6465b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 647dbd5768fSJan Kara clear_inode(inode); 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 65046f65ec1SHugh Dickins /* 65146f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 65246f65ec1SHugh Dickins */ 65341ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 654bde05d1cSHugh Dickins swp_entry_t swap, struct page **pagep) 6551da177e4SLinus Torvalds { 656285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 65746f65ec1SHugh Dickins void *radswap; 65841ffe5d5SHugh Dickins pgoff_t index; 659bde05d1cSHugh Dickins gfp_t gfp; 660bde05d1cSHugh Dickins int error = 0; 6611da177e4SLinus Torvalds 66246f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 663e504f3fdSHugh Dickins index = radix_tree_locate_item(&mapping->page_tree, radswap); 66446f65ec1SHugh Dickins if (index == -1) 6651da177e4SLinus Torvalds return 0; 6662e0e26c7SHugh Dickins 6671b1b32f2SHugh Dickins /* 6681b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 6691f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 6701b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 671285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 6721b1b32f2SHugh Dickins */ 6731b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 6742e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 6752e0e26c7SHugh Dickins 676bde05d1cSHugh Dickins gfp = mapping_gfp_mask(mapping); 677bde05d1cSHugh Dickins if (shmem_should_replace_page(*pagep, gfp)) { 678bde05d1cSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 679bde05d1cSHugh Dickins error = shmem_replace_page(pagep, gfp, info, index); 680bde05d1cSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 681bde05d1cSHugh Dickins /* 682bde05d1cSHugh Dickins * We needed to drop mutex to make that restrictive page 6830142ef6cSHugh Dickins * allocation, but the inode might have been freed while we 6840142ef6cSHugh Dickins * dropped it: although a racing shmem_evict_inode() cannot 6850142ef6cSHugh Dickins * complete without emptying the radix_tree, our page lock 6860142ef6cSHugh Dickins * on this swapcache page is not enough to prevent that - 6870142ef6cSHugh Dickins * free_swap_and_cache() of our swap entry will only 6880142ef6cSHugh Dickins * trylock_page(), removing swap from radix_tree whatever. 6890142ef6cSHugh Dickins * 6900142ef6cSHugh Dickins * We must not proceed to shmem_add_to_page_cache() if the 6910142ef6cSHugh Dickins * inode has been freed, but of course we cannot rely on 6920142ef6cSHugh Dickins * inode or mapping or info to check that. However, we can 6930142ef6cSHugh Dickins * safely check if our swap entry is still in use (and here 6940142ef6cSHugh Dickins * it can't have got reused for another page): if it's still 6950142ef6cSHugh Dickins * in use, then the inode cannot have been freed yet, and we 6960142ef6cSHugh Dickins * can safely proceed (if it's no longer in use, that tells 6970142ef6cSHugh Dickins * nothing about the inode, but we don't need to unuse swap). 698bde05d1cSHugh Dickins */ 699bde05d1cSHugh Dickins if (!page_swapcount(*pagep)) 700bde05d1cSHugh Dickins error = -ENOENT; 701bde05d1cSHugh Dickins } 702bde05d1cSHugh Dickins 703d13d1443SKAMEZAWA Hiroyuki /* 704778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 705778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 706778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 707d13d1443SKAMEZAWA Hiroyuki */ 708bde05d1cSHugh Dickins if (!error) 709bde05d1cSHugh Dickins error = shmem_add_to_page_cache(*pagep, mapping, index, 71046f65ec1SHugh Dickins GFP_NOWAIT, radswap); 71148f170fbSHugh Dickins if (error != -ENOMEM) { 71246f65ec1SHugh Dickins /* 71346f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 71446f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 71546f65ec1SHugh Dickins */ 716bde05d1cSHugh Dickins delete_from_swap_cache(*pagep); 717bde05d1cSHugh Dickins set_page_dirty(*pagep); 71846f65ec1SHugh Dickins if (!error) { 71946f65ec1SHugh Dickins spin_lock(&info->lock); 720285b2c4fSHugh Dickins info->swapped--; 72146f65ec1SHugh Dickins spin_unlock(&info->lock); 72241ffe5d5SHugh Dickins swap_free(swap); 72346f65ec1SHugh Dickins } 7242e0e26c7SHugh Dickins error = 1; /* not an error, but entry was found */ 7251da177e4SLinus Torvalds } 7262e0e26c7SHugh Dickins return error; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds /* 73046f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 7311da177e4SLinus Torvalds */ 73241ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 7331da177e4SLinus Torvalds { 73441ffe5d5SHugh Dickins struct list_head *this, *next; 7351da177e4SLinus Torvalds struct shmem_inode_info *info; 7361da177e4SLinus Torvalds int found = 0; 737bde05d1cSHugh Dickins int error = 0; 738bde05d1cSHugh Dickins 739bde05d1cSHugh Dickins /* 740bde05d1cSHugh Dickins * There's a faint possibility that swap page was replaced before 7410142ef6cSHugh Dickins * caller locked it: caller will come back later with the right page. 742bde05d1cSHugh Dickins */ 7430142ef6cSHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) 744bde05d1cSHugh Dickins goto out; 745778dd893SHugh Dickins 746778dd893SHugh Dickins /* 747778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 748778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 749778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 750778dd893SHugh Dickins */ 751778dd893SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL); 752778dd893SHugh Dickins if (error) 753778dd893SHugh Dickins goto out; 75446f65ec1SHugh Dickins /* No radix_tree_preload: swap entry keeps a place for page in tree */ 7551da177e4SLinus Torvalds 756cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 75741ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 75841ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 759285b2c4fSHugh Dickins if (info->swapped) 760bde05d1cSHugh Dickins found = shmem_unuse_inode(info, swap, &page); 7616922c0c7SHugh Dickins else 7626922c0c7SHugh Dickins list_del_init(&info->swaplist); 763cb5f7b9aSHugh Dickins cond_resched(); 7642e0e26c7SHugh Dickins if (found) 765778dd893SHugh Dickins break; 7661da177e4SLinus Torvalds } 767cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 768778dd893SHugh Dickins 769778dd893SHugh Dickins if (found < 0) 770778dd893SHugh Dickins error = found; 771778dd893SHugh Dickins out: 772aaa46865SHugh Dickins unlock_page(page); 773aaa46865SHugh Dickins page_cache_release(page); 774778dd893SHugh Dickins return error; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds /* 7781da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 7791da177e4SLinus Torvalds */ 7801da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 7811da177e4SLinus Torvalds { 7821da177e4SLinus Torvalds struct shmem_inode_info *info; 7831da177e4SLinus Torvalds struct address_space *mapping; 7841da177e4SLinus Torvalds struct inode *inode; 7856922c0c7SHugh Dickins swp_entry_t swap; 7866922c0c7SHugh Dickins pgoff_t index; 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 7891da177e4SLinus Torvalds mapping = page->mapping; 7901da177e4SLinus Torvalds index = page->index; 7911da177e4SLinus Torvalds inode = mapping->host; 7921da177e4SLinus Torvalds info = SHMEM_I(inode); 7931da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 7941da177e4SLinus Torvalds goto redirty; 795d9fe526aSHugh Dickins if (!total_swap_pages) 7961da177e4SLinus Torvalds goto redirty; 7971da177e4SLinus Torvalds 798d9fe526aSHugh Dickins /* 799d9fe526aSHugh Dickins * shmem_backing_dev_info's capabilities prevent regular writeback or 800d9fe526aSHugh Dickins * sync from ever calling shmem_writepage; but a stacking filesystem 80148f170fbSHugh Dickins * might use ->writepage of its underlying filesystem, in which case 802d9fe526aSHugh Dickins * tmpfs should write out to swap only in response to memory pressure, 80348f170fbSHugh Dickins * and not for the writeback threads or sync. 804d9fe526aSHugh Dickins */ 80548f170fbSHugh Dickins if (!wbc->for_reclaim) { 80648f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 80748f170fbSHugh Dickins goto redirty; 80848f170fbSHugh Dickins } 8091635f6a7SHugh Dickins 8101635f6a7SHugh Dickins /* 8111635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 8121635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 8131635f6a7SHugh Dickins * fallocated page arriving here is now to initialize it and write it. 8141aac1400SHugh Dickins * 8151aac1400SHugh Dickins * That's okay for a page already fallocated earlier, but if we have 8161aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 8171aac1400SHugh Dickins * of this page in case we have to undo it, and (b) it may not be a 8181aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 8191aac1400SHugh Dickins * reactivate the page, and let shmem_fallocate() quit when too many. 8201635f6a7SHugh Dickins */ 8211635f6a7SHugh Dickins if (!PageUptodate(page)) { 8221aac1400SHugh Dickins if (inode->i_private) { 8231aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 8241aac1400SHugh Dickins spin_lock(&inode->i_lock); 8251aac1400SHugh Dickins shmem_falloc = inode->i_private; 8261aac1400SHugh Dickins if (shmem_falloc && 8271aac1400SHugh Dickins index >= shmem_falloc->start && 8281aac1400SHugh Dickins index < shmem_falloc->next) 8291aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 8301aac1400SHugh Dickins else 8311aac1400SHugh Dickins shmem_falloc = NULL; 8321aac1400SHugh Dickins spin_unlock(&inode->i_lock); 8331aac1400SHugh Dickins if (shmem_falloc) 8341aac1400SHugh Dickins goto redirty; 8351aac1400SHugh Dickins } 8361635f6a7SHugh Dickins clear_highpage(page); 8371635f6a7SHugh Dickins flush_dcache_page(page); 8381635f6a7SHugh Dickins SetPageUptodate(page); 8391635f6a7SHugh Dickins } 8401635f6a7SHugh Dickins 841d9fe526aSHugh Dickins swap = get_swap_page(); 84248f170fbSHugh Dickins if (!swap.val) 84348f170fbSHugh Dickins goto redirty; 844d9fe526aSHugh Dickins 845b1dea800SHugh Dickins /* 846b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 8476922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 8486922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 849b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 8506922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 8516922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 852b1dea800SHugh Dickins */ 853b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 85405bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 85505bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 856b1dea800SHugh Dickins 85748f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 858aaa46865SHugh Dickins swap_shmem_alloc(swap); 8596922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 8606922c0c7SHugh Dickins 8616922c0c7SHugh Dickins spin_lock(&info->lock); 8626922c0c7SHugh Dickins info->swapped++; 8636922c0c7SHugh Dickins shmem_recalc_inode(inode); 864826267cfSHugh Dickins spin_unlock(&info->lock); 8656922c0c7SHugh Dickins 8666922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 867d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 8689fab5619SHugh Dickins swap_writepage(page, wbc); 8691da177e4SLinus Torvalds return 0; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 8726922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 873cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, NULL); 8741da177e4SLinus Torvalds redirty: 8751da177e4SLinus Torvalds set_page_dirty(page); 876d9fe526aSHugh Dickins if (wbc->for_reclaim) 877d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 878d9fe526aSHugh Dickins unlock_page(page); 879d9fe526aSHugh Dickins return 0; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds #ifdef CONFIG_NUMA 883680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 88471fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 885680d794bSakpm@linux-foundation.org { 886680d794bSakpm@linux-foundation.org char buffer[64]; 887680d794bSakpm@linux-foundation.org 88871fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 889095f1fc4SLee Schermerhorn return; /* show nothing */ 890095f1fc4SLee Schermerhorn 891a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 892095f1fc4SLee Schermerhorn 893095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 894680d794bSakpm@linux-foundation.org } 89571fe804bSLee Schermerhorn 89671fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 89771fe804bSLee Schermerhorn { 89871fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 89971fe804bSLee Schermerhorn if (sbinfo->mpol) { 90071fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 90171fe804bSLee Schermerhorn mpol = sbinfo->mpol; 90271fe804bSLee Schermerhorn mpol_get(mpol); 90371fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 90471fe804bSLee Schermerhorn } 90571fe804bSLee Schermerhorn return mpol; 90671fe804bSLee Schermerhorn } 907680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 908680d794bSakpm@linux-foundation.org 90941ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 91041ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 9111da177e4SLinus Torvalds { 9121da177e4SLinus Torvalds struct vm_area_struct pvma; 91318a2f371SMel Gorman struct page *page; 9141da177e4SLinus Torvalds 915c4cc6d07SHugh Dickins /* Create a pseudo vma that just contains the policy */ 916c4cc6d07SHugh Dickins pvma.vm_start = 0; 91709c231cbSNathan Zimmer /* Bias interleave by inode number to distribute better across nodes */ 91809c231cbSNathan Zimmer pvma.vm_pgoff = index + info->vfs_inode.i_ino; 919c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 92041ffe5d5SHugh Dickins pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 92152cd3b07SLee Schermerhorn 92218a2f371SMel Gorman page = swapin_readahead(swap, gfp, &pvma, 0); 92318a2f371SMel Gorman 92418a2f371SMel Gorman /* Drop reference taken by mpol_shared_policy_lookup() */ 92518a2f371SMel Gorman mpol_cond_put(pvma.vm_policy); 92618a2f371SMel Gorman 92718a2f371SMel Gorman return page; 92818a2f371SMel Gorman } 92918a2f371SMel Gorman 93018a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp, 93118a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 93218a2f371SMel Gorman { 93318a2f371SMel Gorman struct vm_area_struct pvma; 93418a2f371SMel Gorman struct page *page; 93518a2f371SMel Gorman 93618a2f371SMel Gorman /* Create a pseudo vma that just contains the policy */ 93718a2f371SMel Gorman pvma.vm_start = 0; 93818a2f371SMel Gorman /* Bias interleave by inode number to distribute better across nodes */ 93918a2f371SMel Gorman pvma.vm_pgoff = index + info->vfs_inode.i_ino; 94018a2f371SMel Gorman pvma.vm_ops = NULL; 94118a2f371SMel Gorman pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 94218a2f371SMel Gorman 94318a2f371SMel Gorman page = alloc_page_vma(gfp, &pvma, 0); 94418a2f371SMel Gorman 94518a2f371SMel Gorman /* Drop reference taken by mpol_shared_policy_lookup() */ 94618a2f371SMel Gorman mpol_cond_put(pvma.vm_policy); 94718a2f371SMel Gorman 94818a2f371SMel Gorman return page; 9491da177e4SLinus Torvalds } 950680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */ 951680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 95241ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 953680d794bSakpm@linux-foundation.org { 954680d794bSakpm@linux-foundation.org } 955680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 956680d794bSakpm@linux-foundation.org 95741ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 95841ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 9591da177e4SLinus Torvalds { 96041ffe5d5SHugh Dickins return swapin_readahead(swap, gfp, NULL, 0); 9611da177e4SLinus Torvalds } 9621da177e4SLinus Torvalds 96302098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp, 96441ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 9651da177e4SLinus Torvalds { 966e84e2e13SHugh Dickins return alloc_page(gfp); 9671da177e4SLinus Torvalds } 968680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */ 9691da177e4SLinus Torvalds 97071fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) 97171fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 97271fe804bSLee Schermerhorn { 97371fe804bSLee Schermerhorn return NULL; 97471fe804bSLee Schermerhorn } 97571fe804bSLee Schermerhorn #endif 97671fe804bSLee Schermerhorn 9771da177e4SLinus Torvalds /* 978bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 979bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 980bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 981bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 982bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 983bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 984bde05d1cSHugh Dickins * 985bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 986bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 987bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 988bde05d1cSHugh Dickins */ 989bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp) 990bde05d1cSHugh Dickins { 991bde05d1cSHugh Dickins return page_zonenum(page) > gfp_zone(gfp); 992bde05d1cSHugh Dickins } 993bde05d1cSHugh Dickins 994bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 995bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 996bde05d1cSHugh Dickins { 997bde05d1cSHugh Dickins struct page *oldpage, *newpage; 998bde05d1cSHugh Dickins struct address_space *swap_mapping; 999bde05d1cSHugh Dickins pgoff_t swap_index; 1000bde05d1cSHugh Dickins int error; 1001bde05d1cSHugh Dickins 1002bde05d1cSHugh Dickins oldpage = *pagep; 1003bde05d1cSHugh Dickins swap_index = page_private(oldpage); 1004bde05d1cSHugh Dickins swap_mapping = page_mapping(oldpage); 1005bde05d1cSHugh Dickins 1006bde05d1cSHugh Dickins /* 1007bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1008bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1009bde05d1cSHugh Dickins */ 1010bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1011bde05d1cSHugh Dickins newpage = shmem_alloc_page(gfp, info, index); 1012bde05d1cSHugh Dickins if (!newpage) 1013bde05d1cSHugh Dickins return -ENOMEM; 1014bde05d1cSHugh Dickins 1015bde05d1cSHugh Dickins page_cache_get(newpage); 1016bde05d1cSHugh Dickins copy_highpage(newpage, oldpage); 10170142ef6cSHugh Dickins flush_dcache_page(newpage); 1018bde05d1cSHugh Dickins 1019bde05d1cSHugh Dickins __set_page_locked(newpage); 1020bde05d1cSHugh Dickins SetPageUptodate(newpage); 1021bde05d1cSHugh Dickins SetPageSwapBacked(newpage); 1022bde05d1cSHugh Dickins set_page_private(newpage, swap_index); 1023bde05d1cSHugh Dickins SetPageSwapCache(newpage); 1024bde05d1cSHugh Dickins 1025bde05d1cSHugh Dickins /* 1026bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1027bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1028bde05d1cSHugh Dickins */ 1029bde05d1cSHugh Dickins spin_lock_irq(&swap_mapping->tree_lock); 1030bde05d1cSHugh Dickins error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage, 1031bde05d1cSHugh Dickins newpage); 10320142ef6cSHugh Dickins if (!error) { 1033bde05d1cSHugh Dickins __inc_zone_page_state(newpage, NR_FILE_PAGES); 1034bde05d1cSHugh Dickins __dec_zone_page_state(oldpage, NR_FILE_PAGES); 10350142ef6cSHugh Dickins } 1036bde05d1cSHugh Dickins spin_unlock_irq(&swap_mapping->tree_lock); 1037bde05d1cSHugh Dickins 10380142ef6cSHugh Dickins if (unlikely(error)) { 10390142ef6cSHugh Dickins /* 10400142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 10410142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 10420142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 10430142ef6cSHugh Dickins */ 10440142ef6cSHugh Dickins oldpage = newpage; 10450142ef6cSHugh Dickins } else { 1046bde05d1cSHugh Dickins mem_cgroup_replace_page_cache(oldpage, newpage); 1047bde05d1cSHugh Dickins lru_cache_add_anon(newpage); 10480142ef6cSHugh Dickins *pagep = newpage; 10490142ef6cSHugh Dickins } 1050bde05d1cSHugh Dickins 1051bde05d1cSHugh Dickins ClearPageSwapCache(oldpage); 1052bde05d1cSHugh Dickins set_page_private(oldpage, 0); 1053bde05d1cSHugh Dickins 1054bde05d1cSHugh Dickins unlock_page(oldpage); 1055bde05d1cSHugh Dickins page_cache_release(oldpage); 1056bde05d1cSHugh Dickins page_cache_release(oldpage); 10570142ef6cSHugh Dickins return error; 1058bde05d1cSHugh Dickins } 1059bde05d1cSHugh Dickins 1060bde05d1cSHugh Dickins /* 106168da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 10621da177e4SLinus Torvalds * 10631da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 10641da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 10651da177e4SLinus Torvalds * entry since a page cannot live in both the swap and page cache 10661da177e4SLinus Torvalds */ 106741ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 106868da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) 10691da177e4SLinus Torvalds { 10701da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 107154af6042SHugh Dickins struct shmem_inode_info *info; 10721da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 107327ab7006SHugh Dickins struct page *page; 10741da177e4SLinus Torvalds swp_entry_t swap; 10751da177e4SLinus Torvalds int error; 107654af6042SHugh Dickins int once = 0; 10771635f6a7SHugh Dickins int alloced = 0; 10781da177e4SLinus Torvalds 107941ffe5d5SHugh Dickins if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT)) 10801da177e4SLinus Torvalds return -EFBIG; 10811da177e4SLinus Torvalds repeat: 108254af6042SHugh Dickins swap.val = 0; 108341ffe5d5SHugh Dickins page = find_lock_page(mapping, index); 108454af6042SHugh Dickins if (radix_tree_exceptional_entry(page)) { 108554af6042SHugh Dickins swap = radix_to_swp_entry(page); 108654af6042SHugh Dickins page = NULL; 108754af6042SHugh Dickins } 108854af6042SHugh Dickins 10891635f6a7SHugh Dickins if (sgp != SGP_WRITE && sgp != SGP_FALLOC && 109054af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 109154af6042SHugh Dickins error = -EINVAL; 109254af6042SHugh Dickins goto failed; 109354af6042SHugh Dickins } 109454af6042SHugh Dickins 10951635f6a7SHugh Dickins /* fallocated page? */ 10961635f6a7SHugh Dickins if (page && !PageUptodate(page)) { 10971635f6a7SHugh Dickins if (sgp != SGP_READ) 10981635f6a7SHugh Dickins goto clear; 10991635f6a7SHugh Dickins unlock_page(page); 11001635f6a7SHugh Dickins page_cache_release(page); 11011635f6a7SHugh Dickins page = NULL; 11021635f6a7SHugh Dickins } 110354af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 110454af6042SHugh Dickins *pagep = page; 110554af6042SHugh Dickins return 0; 110627ab7006SHugh Dickins } 110727ab7006SHugh Dickins 1108b409f9fcSHugh Dickins /* 110954af6042SHugh Dickins * Fast cache lookup did not find it: 111054af6042SHugh Dickins * bring it back from swap or allocate. 1111b409f9fcSHugh Dickins */ 111254af6042SHugh Dickins info = SHMEM_I(inode); 111354af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 111427ab7006SHugh Dickins 11151da177e4SLinus Torvalds if (swap.val) { 11161da177e4SLinus Torvalds /* Look it up and read it in.. */ 111727ab7006SHugh Dickins page = lookup_swap_cache(swap); 111827ab7006SHugh Dickins if (!page) { 1119456f998eSYing Han /* here we actually do the io */ 112068da9f05SHugh Dickins if (fault_type) 112168da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 112241ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 112327ab7006SHugh Dickins if (!page) { 11241da177e4SLinus Torvalds error = -ENOMEM; 112554af6042SHugh Dickins goto failed; 1126285b2c4fSHugh Dickins } 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 113054af6042SHugh Dickins lock_page(page); 11310142ef6cSHugh Dickins if (!PageSwapCache(page) || page_private(page) != swap.val || 1132d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1133bde05d1cSHugh Dickins error = -EEXIST; /* try again */ 1134d1899228SHugh Dickins goto unlock; 1135bde05d1cSHugh Dickins } 113627ab7006SHugh Dickins if (!PageUptodate(page)) { 11371da177e4SLinus Torvalds error = -EIO; 113854af6042SHugh Dickins goto failed; 113954af6042SHugh Dickins } 114054af6042SHugh Dickins wait_on_page_writeback(page); 114154af6042SHugh Dickins 1142bde05d1cSHugh Dickins if (shmem_should_replace_page(page, gfp)) { 1143bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 1144bde05d1cSHugh Dickins if (error) 114554af6042SHugh Dickins goto failed; 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 1148aa3b1895SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, 1149aa3b1895SHugh Dickins gfp & GFP_RECLAIM_MASK); 1150d1899228SHugh Dickins if (!error) { 115154af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 115254af6042SHugh Dickins gfp, swp_to_radix_entry(swap)); 1153215c02bcSHugh Dickins /* 1154215c02bcSHugh Dickins * We already confirmed swap under page lock, and make 1155215c02bcSHugh Dickins * no memory allocation here, so usually no possibility 1156215c02bcSHugh Dickins * of error; but free_swap_and_cache() only trylocks a 1157215c02bcSHugh Dickins * page, so it is just possible that the entry has been 1158215c02bcSHugh Dickins * truncated or holepunched since swap was confirmed. 1159215c02bcSHugh Dickins * shmem_undo_range() will have done some of the 1160215c02bcSHugh Dickins * unaccounting, now delete_from_swap_cache() will do 1161215c02bcSHugh Dickins * the rest (including mem_cgroup_uncharge_swapcache). 1162215c02bcSHugh Dickins * Reset swap.val? No, leave it so "failed" goes back to 1163215c02bcSHugh Dickins * "repeat": reading a hole and writing should succeed. 1164215c02bcSHugh Dickins */ 1165215c02bcSHugh Dickins if (error) 1166215c02bcSHugh Dickins delete_from_swap_cache(page); 1167d1899228SHugh Dickins } 116854af6042SHugh Dickins if (error) 116954af6042SHugh Dickins goto failed; 117054af6042SHugh Dickins 117154af6042SHugh Dickins spin_lock(&info->lock); 117254af6042SHugh Dickins info->swapped--; 117354af6042SHugh Dickins shmem_recalc_inode(inode); 11741da177e4SLinus Torvalds spin_unlock(&info->lock); 117527ab7006SHugh Dickins 117627ab7006SHugh Dickins delete_from_swap_cache(page); 117727ab7006SHugh Dickins set_page_dirty(page); 117827ab7006SHugh Dickins swap_free(swap); 117927ab7006SHugh Dickins 118054af6042SHugh Dickins } else { 118154af6042SHugh Dickins if (shmem_acct_block(info->flags)) { 118254af6042SHugh Dickins error = -ENOSPC; 118354af6042SHugh Dickins goto failed; 11841da177e4SLinus Torvalds } 11850edd73b3SHugh Dickins if (sbinfo->max_blocks) { 1186fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 118754af6042SHugh Dickins sbinfo->max_blocks) >= 0) { 118854af6042SHugh Dickins error = -ENOSPC; 118954af6042SHugh Dickins goto unacct; 119054af6042SHugh Dickins } 11917e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 119259a16eadSHugh Dickins } 11931da177e4SLinus Torvalds 119454af6042SHugh Dickins page = shmem_alloc_page(gfp, info, index); 119554af6042SHugh Dickins if (!page) { 119654af6042SHugh Dickins error = -ENOMEM; 119754af6042SHugh Dickins goto decused; 119854af6042SHugh Dickins } 119954af6042SHugh Dickins 120054af6042SHugh Dickins SetPageSwapBacked(page); 120154af6042SHugh Dickins __set_page_locked(page); 1202aa3b1895SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, 1203aa3b1895SHugh Dickins gfp & GFP_RECLAIM_MASK); 120454af6042SHugh Dickins if (error) 120554af6042SHugh Dickins goto decused; 1206b065b432SHugh Dickins error = radix_tree_preload(gfp & GFP_RECLAIM_MASK); 1207b065b432SHugh Dickins if (!error) { 1208b065b432SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1209b065b432SHugh Dickins gfp, NULL); 1210b065b432SHugh Dickins radix_tree_preload_end(); 1211b065b432SHugh Dickins } 1212b065b432SHugh Dickins if (error) { 1213b065b432SHugh Dickins mem_cgroup_uncharge_cache_page(page); 1214b065b432SHugh Dickins goto decused; 1215b065b432SHugh Dickins } 121654af6042SHugh Dickins lru_cache_add_anon(page); 121754af6042SHugh Dickins 121854af6042SHugh Dickins spin_lock(&info->lock); 12191da177e4SLinus Torvalds info->alloced++; 122054af6042SHugh Dickins inode->i_blocks += BLOCKS_PER_PAGE; 122154af6042SHugh Dickins shmem_recalc_inode(inode); 122259a16eadSHugh Dickins spin_unlock(&info->lock); 12231635f6a7SHugh Dickins alloced = true; 122454af6042SHugh Dickins 1225ec9516fbSHugh Dickins /* 12261635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 12271635f6a7SHugh Dickins */ 12281635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 12291635f6a7SHugh Dickins sgp = SGP_WRITE; 12301635f6a7SHugh Dickins clear: 12311635f6a7SHugh Dickins /* 12321635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 12331635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 12341635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1235ec9516fbSHugh Dickins */ 1236ec9516fbSHugh Dickins if (sgp != SGP_WRITE) { 123727ab7006SHugh Dickins clear_highpage(page); 123827ab7006SHugh Dickins flush_dcache_page(page); 123927ab7006SHugh Dickins SetPageUptodate(page); 1240ec9516fbSHugh Dickins } 1241a0ee5ec5SHugh Dickins if (sgp == SGP_DIRTY) 124227ab7006SHugh Dickins set_page_dirty(page); 12431da177e4SLinus Torvalds } 1244bde05d1cSHugh Dickins 124554af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 12461635f6a7SHugh Dickins if (sgp != SGP_WRITE && sgp != SGP_FALLOC && 124754af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 124854af6042SHugh Dickins error = -EINVAL; 12491635f6a7SHugh Dickins if (alloced) 125054af6042SHugh Dickins goto trunc; 12511635f6a7SHugh Dickins else 12521635f6a7SHugh Dickins goto failed; 1253ff36b801SShaohua Li } 125454af6042SHugh Dickins *pagep = page; 125554af6042SHugh Dickins return 0; 1256d00806b1SNick Piggin 1257d0217ac0SNick Piggin /* 125854af6042SHugh Dickins * Error recovery. 12591da177e4SLinus Torvalds */ 126054af6042SHugh Dickins trunc: 12611635f6a7SHugh Dickins info = SHMEM_I(inode); 126254af6042SHugh Dickins ClearPageDirty(page); 126354af6042SHugh Dickins delete_from_page_cache(page); 126454af6042SHugh Dickins spin_lock(&info->lock); 126554af6042SHugh Dickins info->alloced--; 126654af6042SHugh Dickins inode->i_blocks -= BLOCKS_PER_PAGE; 12671da177e4SLinus Torvalds spin_unlock(&info->lock); 126854af6042SHugh Dickins decused: 12691635f6a7SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 127054af6042SHugh Dickins if (sbinfo->max_blocks) 127154af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -1); 127254af6042SHugh Dickins unacct: 127354af6042SHugh Dickins shmem_unacct_blocks(info->flags, 1); 127454af6042SHugh Dickins failed: 1275d1899228SHugh Dickins if (swap.val && error != -EINVAL && 1276d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) 127754af6042SHugh Dickins error = -EEXIST; 1278d1899228SHugh Dickins unlock: 127927ab7006SHugh Dickins if (page) { 128054af6042SHugh Dickins unlock_page(page); 12811da177e4SLinus Torvalds page_cache_release(page); 128254af6042SHugh Dickins } 128354af6042SHugh Dickins if (error == -ENOSPC && !once++) { 128454af6042SHugh Dickins info = SHMEM_I(inode); 128554af6042SHugh Dickins spin_lock(&info->lock); 128654af6042SHugh Dickins shmem_recalc_inode(inode); 128754af6042SHugh Dickins spin_unlock(&info->lock); 12881da177e4SLinus Torvalds goto repeat; 1289d8dc74f2SAdrian Bunk } 1290d1899228SHugh Dickins if (error == -EEXIST) /* from above or from radix_tree_insert */ 129154af6042SHugh Dickins goto repeat; 129254af6042SHugh Dickins return error; 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 12961da177e4SLinus Torvalds { 12971da177e4SLinus Torvalds struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 12981da177e4SLinus Torvalds int error; 129968da9f05SHugh Dickins int ret = VM_FAULT_LOCKED; 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); 13021da177e4SLinus Torvalds if (error) 13031da177e4SLinus Torvalds return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 130468da9f05SHugh Dickins 1305456f998eSYing Han if (ret & VM_FAULT_MAJOR) { 1306456f998eSYing Han count_vm_event(PGMAJFAULT); 1307456f998eSYing Han mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 1308456f998eSYing Han } 130968da9f05SHugh Dickins return ret; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds #ifdef CONFIG_NUMA 131341ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 13141da177e4SLinus Torvalds { 131541ffe5d5SHugh Dickins struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 131641ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 1319d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 1320d8dc74f2SAdrian Bunk unsigned long addr) 13211da177e4SLinus Torvalds { 132241ffe5d5SHugh Dickins struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 132341ffe5d5SHugh Dickins pgoff_t index; 13241da177e4SLinus Torvalds 132541ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 132641ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds #endif 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 13311da177e4SLinus Torvalds { 1332d3ac7f89SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 13331da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 13341da177e4SLinus Torvalds int retval = -ENOMEM; 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds spin_lock(&info->lock); 13371da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 13381da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 13391da177e4SLinus Torvalds goto out_nomem; 13401da177e4SLinus Torvalds info->flags |= VM_LOCKED; 134189e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 13441da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 13451da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 134689e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds retval = 0; 134989e004eaSLee Schermerhorn 13501da177e4SLinus Torvalds out_nomem: 13511da177e4SLinus Torvalds spin_unlock(&info->lock); 13521da177e4SLinus Torvalds return retval; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 13559b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 13561da177e4SLinus Torvalds { 13571da177e4SLinus Torvalds file_accessed(file); 13581da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 13591da177e4SLinus Torvalds return 0; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 1362454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 136309208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 13641da177e4SLinus Torvalds { 13651da177e4SLinus Torvalds struct inode *inode; 13661da177e4SLinus Torvalds struct shmem_inode_info *info; 13671da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 13681da177e4SLinus Torvalds 13695b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 13701da177e4SLinus Torvalds return NULL; 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds inode = new_inode(sb); 13731da177e4SLinus Torvalds if (inode) { 137485fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 1375454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 13761da177e4SLinus Torvalds inode->i_blocks = 0; 13771da177e4SLinus Torvalds inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; 13781da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 137991828a40SDavid M. Grimes inode->i_generation = get_seconds(); 13801da177e4SLinus Torvalds info = SHMEM_I(inode); 13811da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 13821da177e4SLinus Torvalds spin_lock_init(&info->lock); 13830b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 13841da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 138538f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 138672c04902SAl Viro cache_no_acl(inode); 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds switch (mode & S_IFMT) { 13891da177e4SLinus Torvalds default: 139039f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 13911da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 13921da177e4SLinus Torvalds break; 13931da177e4SLinus Torvalds case S_IFREG: 139414fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 13951da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 13961da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 139771fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 139871fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 13991da177e4SLinus Torvalds break; 14001da177e4SLinus Torvalds case S_IFDIR: 1401d8c76e6fSDave Hansen inc_nlink(inode); 14021da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 14031da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 14041da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 14051da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 14061da177e4SLinus Torvalds break; 14071da177e4SLinus Torvalds case S_IFLNK: 14081da177e4SLinus Torvalds /* 14091da177e4SLinus Torvalds * Must not load anything in the rbtree, 14101da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 14111da177e4SLinus Torvalds */ 141271fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 14131da177e4SLinus Torvalds break; 14141da177e4SLinus Torvalds } 14155b04c689SPavel Emelyanov } else 14165b04c689SPavel Emelyanov shmem_free_inode(sb); 14171da177e4SLinus Torvalds return inode; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 142192e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 142269f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 14231da177e4SLinus Torvalds 14246d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR 14256d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 14266d9d88d0SJarkko Sakkinen #else 14276d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL 14286d9d88d0SJarkko Sakkinen #endif 14296d9d88d0SJarkko Sakkinen 14301da177e4SLinus Torvalds static int 1431800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 1432800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 1433800d15a5SNick Piggin struct page **pagep, void **fsdata) 14341da177e4SLinus Torvalds { 1435800d15a5SNick Piggin struct inode *inode = mapping->host; 1436800d15a5SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 1437800d15a5SNick Piggin return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); 1438800d15a5SNick Piggin } 1439800d15a5SNick Piggin 1440800d15a5SNick Piggin static int 1441800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 1442800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 1443800d15a5SNick Piggin struct page *page, void *fsdata) 1444800d15a5SNick Piggin { 1445800d15a5SNick Piggin struct inode *inode = mapping->host; 1446800d15a5SNick Piggin 1447800d15a5SNick Piggin if (pos + copied > inode->i_size) 1448800d15a5SNick Piggin i_size_write(inode, pos + copied); 1449800d15a5SNick Piggin 1450ec9516fbSHugh Dickins if (!PageUptodate(page)) { 1451ec9516fbSHugh Dickins if (copied < PAGE_CACHE_SIZE) { 1452ec9516fbSHugh Dickins unsigned from = pos & (PAGE_CACHE_SIZE - 1); 1453ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 1454ec9516fbSHugh Dickins from + copied, PAGE_CACHE_SIZE); 1455ec9516fbSHugh Dickins } 1456ec9516fbSHugh Dickins SetPageUptodate(page); 1457ec9516fbSHugh Dickins } 1458d3602444SHugh Dickins set_page_dirty(page); 14596746aff7SWu Fengguang unlock_page(page); 1460d3602444SHugh Dickins page_cache_release(page); 1461d3602444SHugh Dickins 1462800d15a5SNick Piggin return copied; 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) 14661da177e4SLinus Torvalds { 1467d3ac7f89SJosef "Jeff" Sipek struct inode *inode = filp->f_path.dentry->d_inode; 14681da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 146941ffe5d5SHugh Dickins pgoff_t index; 147041ffe5d5SHugh Dickins unsigned long offset; 1471a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 1472a0ee5ec5SHugh Dickins 1473a0ee5ec5SHugh Dickins /* 1474a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 1475a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 1476a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 1477a0ee5ec5SHugh Dickins */ 1478a0ee5ec5SHugh Dickins if (segment_eq(get_fs(), KERNEL_DS)) 1479a0ee5ec5SHugh Dickins sgp = SGP_DIRTY; 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds index = *ppos >> PAGE_CACHE_SHIFT; 14821da177e4SLinus Torvalds offset = *ppos & ~PAGE_CACHE_MASK; 14831da177e4SLinus Torvalds 14841da177e4SLinus Torvalds for (;;) { 14851da177e4SLinus Torvalds struct page *page = NULL; 148641ffe5d5SHugh Dickins pgoff_t end_index; 148741ffe5d5SHugh Dickins unsigned long nr, ret; 14881da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 14911da177e4SLinus Torvalds if (index > end_index) 14921da177e4SLinus Torvalds break; 14931da177e4SLinus Torvalds if (index == end_index) { 14941da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 14951da177e4SLinus Torvalds if (nr <= offset) 14961da177e4SLinus Torvalds break; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 1499a0ee5ec5SHugh Dickins desc->error = shmem_getpage(inode, index, &page, sgp, NULL); 15001da177e4SLinus Torvalds if (desc->error) { 15011da177e4SLinus Torvalds if (desc->error == -EINVAL) 15021da177e4SLinus Torvalds desc->error = 0; 15031da177e4SLinus Torvalds break; 15041da177e4SLinus Torvalds } 1505d3602444SHugh Dickins if (page) 1506d3602444SHugh Dickins unlock_page(page); 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds /* 15091da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 15101b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 15111da177e4SLinus Torvalds */ 15121da177e4SLinus Torvalds nr = PAGE_CACHE_SIZE; 15131da177e4SLinus Torvalds i_size = i_size_read(inode); 15141da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 15151da177e4SLinus Torvalds if (index == end_index) { 15161da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 15171da177e4SLinus Torvalds if (nr <= offset) { 15181da177e4SLinus Torvalds if (page) 15191da177e4SLinus Torvalds page_cache_release(page); 15201da177e4SLinus Torvalds break; 15211da177e4SLinus Torvalds } 15221da177e4SLinus Torvalds } 15231da177e4SLinus Torvalds nr -= offset; 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds if (page) { 15261da177e4SLinus Torvalds /* 15271da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 15281da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 15291da177e4SLinus Torvalds * before reading the page on the kernel side. 15301da177e4SLinus Torvalds */ 15311da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 15321da177e4SLinus Torvalds flush_dcache_page(page); 15331da177e4SLinus Torvalds /* 15341da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 15351da177e4SLinus Torvalds */ 15361da177e4SLinus Torvalds if (!offset) 15371da177e4SLinus Torvalds mark_page_accessed(page); 1538b5810039SNick Piggin } else { 15391da177e4SLinus Torvalds page = ZERO_PAGE(0); 1540b5810039SNick Piggin page_cache_get(page); 1541b5810039SNick Piggin } 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds /* 15441da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 15451da177e4SLinus Torvalds * now we can copy it to user space... 15461da177e4SLinus Torvalds * 15471da177e4SLinus Torvalds * The actor routine returns how many bytes were actually used.. 15481da177e4SLinus Torvalds * NOTE! This may not be the same as how much of a user buffer 15491da177e4SLinus Torvalds * we filled up (we may be padding etc), so we can only update 15501da177e4SLinus Torvalds * "pos" here (the actor routine has to update the user buffer 15511da177e4SLinus Torvalds * pointers and the remaining count). 15521da177e4SLinus Torvalds */ 15531da177e4SLinus Torvalds ret = actor(desc, page, offset, nr); 15541da177e4SLinus Torvalds offset += ret; 15551da177e4SLinus Torvalds index += offset >> PAGE_CACHE_SHIFT; 15561da177e4SLinus Torvalds offset &= ~PAGE_CACHE_MASK; 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds page_cache_release(page); 15591da177e4SLinus Torvalds if (ret != nr || !desc->count) 15601da177e4SLinus Torvalds break; 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds cond_resched(); 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; 15661da177e4SLinus Torvalds file_accessed(filp); 15671da177e4SLinus Torvalds } 15681da177e4SLinus Torvalds 1569bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb, 1570bcd78e49SHugh Dickins const struct iovec *iov, unsigned long nr_segs, loff_t pos) 15711da177e4SLinus Torvalds { 1572bcd78e49SHugh Dickins struct file *filp = iocb->ki_filp; 1573bcd78e49SHugh Dickins ssize_t retval; 1574bcd78e49SHugh Dickins unsigned long seg; 1575bcd78e49SHugh Dickins size_t count; 1576bcd78e49SHugh Dickins loff_t *ppos = &iocb->ki_pos; 1577bcd78e49SHugh Dickins 1578bcd78e49SHugh Dickins retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); 1579bcd78e49SHugh Dickins if (retval) 1580bcd78e49SHugh Dickins return retval; 1581bcd78e49SHugh Dickins 1582bcd78e49SHugh Dickins for (seg = 0; seg < nr_segs; seg++) { 15831da177e4SLinus Torvalds read_descriptor_t desc; 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds desc.written = 0; 1586bcd78e49SHugh Dickins desc.arg.buf = iov[seg].iov_base; 1587bcd78e49SHugh Dickins desc.count = iov[seg].iov_len; 1588bcd78e49SHugh Dickins if (desc.count == 0) 1589bcd78e49SHugh Dickins continue; 15901da177e4SLinus Torvalds desc.error = 0; 15911da177e4SLinus Torvalds do_shmem_file_read(filp, ppos, &desc, file_read_actor); 1592bcd78e49SHugh Dickins retval += desc.written; 1593bcd78e49SHugh Dickins if (desc.error) { 1594bcd78e49SHugh Dickins retval = retval ?: desc.error; 1595bcd78e49SHugh Dickins break; 1596bcd78e49SHugh Dickins } 1597bcd78e49SHugh Dickins if (desc.count > 0) 1598bcd78e49SHugh Dickins break; 1599bcd78e49SHugh Dickins } 1600bcd78e49SHugh Dickins return retval; 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds 1603708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 1604708e3508SHugh Dickins struct pipe_inode_info *pipe, size_t len, 1605708e3508SHugh Dickins unsigned int flags) 1606708e3508SHugh Dickins { 1607708e3508SHugh Dickins struct address_space *mapping = in->f_mapping; 160871f0e07aSHugh Dickins struct inode *inode = mapping->host; 1609708e3508SHugh Dickins unsigned int loff, nr_pages, req_pages; 1610708e3508SHugh Dickins struct page *pages[PIPE_DEF_BUFFERS]; 1611708e3508SHugh Dickins struct partial_page partial[PIPE_DEF_BUFFERS]; 1612708e3508SHugh Dickins struct page *page; 1613708e3508SHugh Dickins pgoff_t index, end_index; 1614708e3508SHugh Dickins loff_t isize, left; 1615708e3508SHugh Dickins int error, page_nr; 1616708e3508SHugh Dickins struct splice_pipe_desc spd = { 1617708e3508SHugh Dickins .pages = pages, 1618708e3508SHugh Dickins .partial = partial, 1619047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 1620708e3508SHugh Dickins .flags = flags, 1621708e3508SHugh Dickins .ops = &page_cache_pipe_buf_ops, 1622708e3508SHugh Dickins .spd_release = spd_release_page, 1623708e3508SHugh Dickins }; 1624708e3508SHugh Dickins 162571f0e07aSHugh Dickins isize = i_size_read(inode); 1626708e3508SHugh Dickins if (unlikely(*ppos >= isize)) 1627708e3508SHugh Dickins return 0; 1628708e3508SHugh Dickins 1629708e3508SHugh Dickins left = isize - *ppos; 1630708e3508SHugh Dickins if (unlikely(left < len)) 1631708e3508SHugh Dickins len = left; 1632708e3508SHugh Dickins 1633708e3508SHugh Dickins if (splice_grow_spd(pipe, &spd)) 1634708e3508SHugh Dickins return -ENOMEM; 1635708e3508SHugh Dickins 1636708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1637708e3508SHugh Dickins loff = *ppos & ~PAGE_CACHE_MASK; 1638708e3508SHugh Dickins req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1639708e3508SHugh Dickins nr_pages = min(req_pages, pipe->buffers); 1640708e3508SHugh Dickins 1641708e3508SHugh Dickins spd.nr_pages = find_get_pages_contig(mapping, index, 1642708e3508SHugh Dickins nr_pages, spd.pages); 1643708e3508SHugh Dickins index += spd.nr_pages; 1644708e3508SHugh Dickins error = 0; 164571f0e07aSHugh Dickins 1646708e3508SHugh Dickins while (spd.nr_pages < nr_pages) { 164771f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); 164871f0e07aSHugh Dickins if (error) 1649708e3508SHugh Dickins break; 1650708e3508SHugh Dickins unlock_page(page); 1651708e3508SHugh Dickins spd.pages[spd.nr_pages++] = page; 1652708e3508SHugh Dickins index++; 1653708e3508SHugh Dickins } 1654708e3508SHugh Dickins 1655708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1656708e3508SHugh Dickins nr_pages = spd.nr_pages; 1657708e3508SHugh Dickins spd.nr_pages = 0; 165871f0e07aSHugh Dickins 1659708e3508SHugh Dickins for (page_nr = 0; page_nr < nr_pages; page_nr++) { 1660708e3508SHugh Dickins unsigned int this_len; 1661708e3508SHugh Dickins 1662708e3508SHugh Dickins if (!len) 1663708e3508SHugh Dickins break; 1664708e3508SHugh Dickins 1665708e3508SHugh Dickins this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 1666708e3508SHugh Dickins page = spd.pages[page_nr]; 1667708e3508SHugh Dickins 166871f0e07aSHugh Dickins if (!PageUptodate(page) || page->mapping != mapping) { 166971f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, 167071f0e07aSHugh Dickins SGP_CACHE, NULL); 167171f0e07aSHugh Dickins if (error) 1672708e3508SHugh Dickins break; 167371f0e07aSHugh Dickins unlock_page(page); 1674708e3508SHugh Dickins page_cache_release(spd.pages[page_nr]); 1675708e3508SHugh Dickins spd.pages[page_nr] = page; 1676708e3508SHugh Dickins } 1677708e3508SHugh Dickins 167871f0e07aSHugh Dickins isize = i_size_read(inode); 1679708e3508SHugh Dickins end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 1680708e3508SHugh Dickins if (unlikely(!isize || index > end_index)) 1681708e3508SHugh Dickins break; 1682708e3508SHugh Dickins 1683708e3508SHugh Dickins if (end_index == index) { 1684708e3508SHugh Dickins unsigned int plen; 1685708e3508SHugh Dickins 1686708e3508SHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 1687708e3508SHugh Dickins if (plen <= loff) 1688708e3508SHugh Dickins break; 1689708e3508SHugh Dickins 1690708e3508SHugh Dickins this_len = min(this_len, plen - loff); 1691708e3508SHugh Dickins len = this_len; 1692708e3508SHugh Dickins } 1693708e3508SHugh Dickins 1694708e3508SHugh Dickins spd.partial[page_nr].offset = loff; 1695708e3508SHugh Dickins spd.partial[page_nr].len = this_len; 1696708e3508SHugh Dickins len -= this_len; 1697708e3508SHugh Dickins loff = 0; 1698708e3508SHugh Dickins spd.nr_pages++; 1699708e3508SHugh Dickins index++; 1700708e3508SHugh Dickins } 1701708e3508SHugh Dickins 1702708e3508SHugh Dickins while (page_nr < nr_pages) 1703708e3508SHugh Dickins page_cache_release(spd.pages[page_nr++]); 1704708e3508SHugh Dickins 1705708e3508SHugh Dickins if (spd.nr_pages) 1706708e3508SHugh Dickins error = splice_to_pipe(pipe, &spd); 1707708e3508SHugh Dickins 1708047fe360SEric Dumazet splice_shrink_spd(&spd); 1709708e3508SHugh Dickins 1710708e3508SHugh Dickins if (error > 0) { 1711708e3508SHugh Dickins *ppos += error; 1712708e3508SHugh Dickins file_accessed(in); 1713708e3508SHugh Dickins } 1714708e3508SHugh Dickins return error; 1715708e3508SHugh Dickins } 1716708e3508SHugh Dickins 1717220f2ac9SHugh Dickins /* 1718220f2ac9SHugh Dickins * llseek SEEK_DATA or SEEK_HOLE through the radix_tree. 1719220f2ac9SHugh Dickins */ 1720220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping, 1721965c8e59SAndrew Morton pgoff_t index, pgoff_t end, int whence) 1722220f2ac9SHugh Dickins { 1723220f2ac9SHugh Dickins struct page *page; 1724220f2ac9SHugh Dickins struct pagevec pvec; 1725220f2ac9SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 1726220f2ac9SHugh Dickins bool done = false; 1727220f2ac9SHugh Dickins int i; 1728220f2ac9SHugh Dickins 1729220f2ac9SHugh Dickins pagevec_init(&pvec, 0); 1730220f2ac9SHugh Dickins pvec.nr = 1; /* start small: we may be there already */ 1731220f2ac9SHugh Dickins while (!done) { 1732220f2ac9SHugh Dickins pvec.nr = shmem_find_get_pages_and_swap(mapping, index, 1733220f2ac9SHugh Dickins pvec.nr, pvec.pages, indices); 1734220f2ac9SHugh Dickins if (!pvec.nr) { 1735965c8e59SAndrew Morton if (whence == SEEK_DATA) 1736220f2ac9SHugh Dickins index = end; 1737220f2ac9SHugh Dickins break; 1738220f2ac9SHugh Dickins } 1739220f2ac9SHugh Dickins for (i = 0; i < pvec.nr; i++, index++) { 1740220f2ac9SHugh Dickins if (index < indices[i]) { 1741965c8e59SAndrew Morton if (whence == SEEK_HOLE) { 1742220f2ac9SHugh Dickins done = true; 1743220f2ac9SHugh Dickins break; 1744220f2ac9SHugh Dickins } 1745220f2ac9SHugh Dickins index = indices[i]; 1746220f2ac9SHugh Dickins } 1747220f2ac9SHugh Dickins page = pvec.pages[i]; 1748220f2ac9SHugh Dickins if (page && !radix_tree_exceptional_entry(page)) { 1749220f2ac9SHugh Dickins if (!PageUptodate(page)) 1750220f2ac9SHugh Dickins page = NULL; 1751220f2ac9SHugh Dickins } 1752220f2ac9SHugh Dickins if (index >= end || 1753965c8e59SAndrew Morton (page && whence == SEEK_DATA) || 1754965c8e59SAndrew Morton (!page && whence == SEEK_HOLE)) { 1755220f2ac9SHugh Dickins done = true; 1756220f2ac9SHugh Dickins break; 1757220f2ac9SHugh Dickins } 1758220f2ac9SHugh Dickins } 1759220f2ac9SHugh Dickins shmem_deswap_pagevec(&pvec); 1760220f2ac9SHugh Dickins pagevec_release(&pvec); 1761220f2ac9SHugh Dickins pvec.nr = PAGEVEC_SIZE; 1762220f2ac9SHugh Dickins cond_resched(); 1763220f2ac9SHugh Dickins } 1764220f2ac9SHugh Dickins return index; 1765220f2ac9SHugh Dickins } 1766220f2ac9SHugh Dickins 1767965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 1768220f2ac9SHugh Dickins { 1769220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 1770220f2ac9SHugh Dickins struct inode *inode = mapping->host; 1771220f2ac9SHugh Dickins pgoff_t start, end; 1772220f2ac9SHugh Dickins loff_t new_offset; 1773220f2ac9SHugh Dickins 1774965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 1775965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 1776220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 1777220f2ac9SHugh Dickins mutex_lock(&inode->i_mutex); 1778220f2ac9SHugh Dickins /* We're holding i_mutex so we can access i_size directly */ 1779220f2ac9SHugh Dickins 1780220f2ac9SHugh Dickins if (offset < 0) 1781220f2ac9SHugh Dickins offset = -EINVAL; 1782220f2ac9SHugh Dickins else if (offset >= inode->i_size) 1783220f2ac9SHugh Dickins offset = -ENXIO; 1784220f2ac9SHugh Dickins else { 1785220f2ac9SHugh Dickins start = offset >> PAGE_CACHE_SHIFT; 1786220f2ac9SHugh Dickins end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1787965c8e59SAndrew Morton new_offset = shmem_seek_hole_data(mapping, start, end, whence); 1788220f2ac9SHugh Dickins new_offset <<= PAGE_CACHE_SHIFT; 1789220f2ac9SHugh Dickins if (new_offset > offset) { 1790220f2ac9SHugh Dickins if (new_offset < inode->i_size) 1791220f2ac9SHugh Dickins offset = new_offset; 1792965c8e59SAndrew Morton else if (whence == SEEK_DATA) 1793220f2ac9SHugh Dickins offset = -ENXIO; 1794220f2ac9SHugh Dickins else 1795220f2ac9SHugh Dickins offset = inode->i_size; 1796220f2ac9SHugh Dickins } 1797220f2ac9SHugh Dickins } 1798220f2ac9SHugh Dickins 1799220f2ac9SHugh Dickins if (offset >= 0 && offset != file->f_pos) { 1800220f2ac9SHugh Dickins file->f_pos = offset; 1801220f2ac9SHugh Dickins file->f_version = 0; 1802220f2ac9SHugh Dickins } 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 { 181083e4fa9cSHugh Dickins struct inode *inode = file->f_path.dentry->d_inode; 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) { 19402a7dba39SEric Paris error = security_inode_init_security(inode, dir, 19419d8f13baSMimi Zohar &dentry->d_name, 19426d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 1943570bc1c2SStephen Smalley if (error) { 1944570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1945570bc1c2SStephen Smalley iput(inode); 1946570bc1c2SStephen Smalley return error; 1947570bc1c2SStephen Smalley } 194839f0247dSAndreas Gruenbacher } 19491c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL 19501c7c474cSChristoph Hellwig error = generic_acl_init(inode, dir); 195139f0247dSAndreas Gruenbacher if (error) { 195239f0247dSAndreas Gruenbacher iput(inode); 195339f0247dSAndreas Gruenbacher return error; 1954570bc1c2SStephen Smalley } 1955718deb6bSAl Viro #else 1956718deb6bSAl Viro error = 0; 19571c7c474cSChristoph Hellwig #endif 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 196618bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 19671da177e4SLinus Torvalds { 19681da177e4SLinus Torvalds int error; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 19711da177e4SLinus Torvalds return error; 1972d8c76e6fSDave Hansen inc_nlink(dir); 19731da177e4SLinus Torvalds return 0; 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 19764acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, 1977ebfc3b49SAl Viro bool excl) 19781da177e4SLinus Torvalds { 19791da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds /* 19831da177e4SLinus Torvalds * Link a file.. 19841da177e4SLinus Torvalds */ 19851da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 19861da177e4SLinus Torvalds { 19871da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 19885b04c689SPavel Emelyanov int ret; 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds /* 19911da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 19921da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 19931da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 19941da177e4SLinus Torvalds */ 19955b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 19965b04c689SPavel Emelyanov if (ret) 19975b04c689SPavel Emelyanov goto out; 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 20001da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 2001d8c76e6fSDave Hansen inc_nlink(inode); 20027de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 20031da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 20041da177e4SLinus Torvalds d_instantiate(dentry, inode); 20055b04c689SPavel Emelyanov out: 20065b04c689SPavel Emelyanov return ret; 20071da177e4SLinus Torvalds } 20081da177e4SLinus Torvalds 20091da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 20101da177e4SLinus Torvalds { 20111da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20121da177e4SLinus Torvalds 20135b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 20145b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 20171da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 20189a53c3a7SDave Hansen drop_nlink(inode); 20191da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 20201da177e4SLinus Torvalds return 0; 20211da177e4SLinus Torvalds } 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 20241da177e4SLinus Torvalds { 20251da177e4SLinus Torvalds if (!simple_empty(dentry)) 20261da177e4SLinus Torvalds return -ENOTEMPTY; 20271da177e4SLinus Torvalds 20289a53c3a7SDave Hansen drop_nlink(dentry->d_inode); 20299a53c3a7SDave Hansen drop_nlink(dir); 20301da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds /* 20341da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 20351da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 20361da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 20371da177e4SLinus Torvalds * gets overwritten. 20381da177e4SLinus Torvalds */ 20391da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 20401da177e4SLinus Torvalds { 20411da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 20421da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 20451da177e4SLinus Torvalds return -ENOTEMPTY; 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds if (new_dentry->d_inode) { 20481da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 20491da177e4SLinus Torvalds if (they_are_dirs) 20509a53c3a7SDave Hansen drop_nlink(old_dir); 20511da177e4SLinus Torvalds } else if (they_are_dirs) { 20529a53c3a7SDave Hansen drop_nlink(old_dir); 2053d8c76e6fSDave Hansen inc_nlink(new_dir); 20541da177e4SLinus Torvalds } 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 20571da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 20581da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 20591da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 20601da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 20611da177e4SLinus Torvalds return 0; 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 20651da177e4SLinus Torvalds { 20661da177e4SLinus Torvalds int error; 20671da177e4SLinus Torvalds int len; 20681da177e4SLinus Torvalds struct inode *inode; 20699276aad6SHugh Dickins struct page *page; 20701da177e4SLinus Torvalds char *kaddr; 20711da177e4SLinus Torvalds struct shmem_inode_info *info; 20721da177e4SLinus Torvalds 20731da177e4SLinus Torvalds len = strlen(symname) + 1; 20741da177e4SLinus Torvalds if (len > PAGE_CACHE_SIZE) 20751da177e4SLinus Torvalds return -ENAMETOOLONG; 20761da177e4SLinus Torvalds 2077454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 20781da177e4SLinus Torvalds if (!inode) 20791da177e4SLinus Torvalds return -ENOSPC; 20801da177e4SLinus Torvalds 20819d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 20826d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2083570bc1c2SStephen Smalley if (error) { 2084570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 2085570bc1c2SStephen Smalley iput(inode); 2086570bc1c2SStephen Smalley return error; 2087570bc1c2SStephen Smalley } 2088570bc1c2SStephen Smalley error = 0; 2089570bc1c2SStephen Smalley } 2090570bc1c2SStephen Smalley 20911da177e4SLinus Torvalds info = SHMEM_I(inode); 20921da177e4SLinus Torvalds inode->i_size = len-1; 209369f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 209469f07ec9SHugh Dickins info->symlink = kmemdup(symname, len, GFP_KERNEL); 209569f07ec9SHugh Dickins if (!info->symlink) { 209669f07ec9SHugh Dickins iput(inode); 209769f07ec9SHugh Dickins return -ENOMEM; 209869f07ec9SHugh Dickins } 209969f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 21001da177e4SLinus Torvalds } else { 21011da177e4SLinus Torvalds error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); 21021da177e4SLinus Torvalds if (error) { 21031da177e4SLinus Torvalds iput(inode); 21041da177e4SLinus Torvalds return error; 21051da177e4SLinus Torvalds } 210614fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 21071da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 21089b04c5feSCong Wang kaddr = kmap_atomic(page); 21091da177e4SLinus Torvalds memcpy(kaddr, symname, len); 21109b04c5feSCong Wang kunmap_atomic(kaddr); 2111ec9516fbSHugh Dickins SetPageUptodate(page); 21121da177e4SLinus Torvalds set_page_dirty(page); 21136746aff7SWu Fengguang unlock_page(page); 21141da177e4SLinus Torvalds page_cache_release(page); 21151da177e4SLinus Torvalds } 21161da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 21171da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 21181da177e4SLinus Torvalds d_instantiate(dentry, inode); 21191da177e4SLinus Torvalds dget(dentry); 21201da177e4SLinus Torvalds return 0; 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds 212369f07ec9SHugh Dickins static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd) 21241da177e4SLinus Torvalds { 212569f07ec9SHugh Dickins nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink); 2126cc314eefSLinus Torvalds return NULL; 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds 2129cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) 21301da177e4SLinus Torvalds { 21311da177e4SLinus Torvalds struct page *page = NULL; 213241ffe5d5SHugh Dickins int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); 213341ffe5d5SHugh Dickins nd_set_link(nd, error ? ERR_PTR(error) : kmap(page)); 2134d3602444SHugh Dickins if (page) 2135d3602444SHugh Dickins unlock_page(page); 2136cc314eefSLinus Torvalds return page; 21371da177e4SLinus Torvalds } 21381da177e4SLinus Torvalds 2139cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 21401da177e4SLinus Torvalds { 21411da177e4SLinus Torvalds if (!IS_ERR(nd_get_link(nd))) { 2142cc314eefSLinus Torvalds struct page *page = cookie; 21431da177e4SLinus Torvalds kunmap(page); 21441da177e4SLinus Torvalds mark_page_accessed(page); 21451da177e4SLinus Torvalds page_cache_release(page); 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds } 21481da177e4SLinus Torvalds 2149b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2150b09e0fa4SEric Paris /* 2151b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 2152b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 2153b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 2154b09e0fa4SEric Paris * filesystem level, though. 2155b09e0fa4SEric Paris */ 2156b09e0fa4SEric Paris 21576d9d88d0SJarkko Sakkinen /* 21586d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 21596d9d88d0SJarkko Sakkinen */ 21606d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 21616d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 21626d9d88d0SJarkko Sakkinen void *fs_info) 21636d9d88d0SJarkko Sakkinen { 21646d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 21656d9d88d0SJarkko Sakkinen const struct xattr *xattr; 216638f38657SAristeu Rozanski struct simple_xattr *new_xattr; 21676d9d88d0SJarkko Sakkinen size_t len; 21686d9d88d0SJarkko Sakkinen 21696d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 217038f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 21716d9d88d0SJarkko Sakkinen if (!new_xattr) 21726d9d88d0SJarkko Sakkinen return -ENOMEM; 21736d9d88d0SJarkko Sakkinen 21746d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 21756d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 21766d9d88d0SJarkko Sakkinen GFP_KERNEL); 21776d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 21786d9d88d0SJarkko Sakkinen kfree(new_xattr); 21796d9d88d0SJarkko Sakkinen return -ENOMEM; 21806d9d88d0SJarkko Sakkinen } 21816d9d88d0SJarkko Sakkinen 21826d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 21836d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 21846d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 21856d9d88d0SJarkko Sakkinen xattr->name, len); 21866d9d88d0SJarkko Sakkinen 218738f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 21886d9d88d0SJarkko Sakkinen } 21896d9d88d0SJarkko Sakkinen 21906d9d88d0SJarkko Sakkinen return 0; 21916d9d88d0SJarkko Sakkinen } 21926d9d88d0SJarkko Sakkinen 2193b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 2194b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 2195b09e0fa4SEric Paris &generic_acl_access_handler, 2196b09e0fa4SEric Paris &generic_acl_default_handler, 2197b09e0fa4SEric Paris #endif 2198b09e0fa4SEric Paris NULL 2199b09e0fa4SEric Paris }; 2200b09e0fa4SEric Paris 2201b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name) 2202b09e0fa4SEric Paris { 2203b09e0fa4SEric Paris struct { const char *prefix; size_t len; } arr[] = { 2204b09e0fa4SEric Paris { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, 2205b09e0fa4SEric Paris { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } 2206b09e0fa4SEric Paris }; 2207b09e0fa4SEric Paris int i; 2208b09e0fa4SEric Paris 2209b09e0fa4SEric Paris for (i = 0; i < ARRAY_SIZE(arr); i++) { 2210b09e0fa4SEric Paris size_t preflen = arr[i].len; 2211b09e0fa4SEric Paris if (strncmp(name, arr[i].prefix, preflen) == 0) { 2212b09e0fa4SEric Paris if (!name[preflen]) 2213b09e0fa4SEric Paris return -EINVAL; 2214b09e0fa4SEric Paris return 0; 2215b09e0fa4SEric Paris } 2216b09e0fa4SEric Paris } 2217b09e0fa4SEric Paris return -EOPNOTSUPP; 2218b09e0fa4SEric Paris } 2219b09e0fa4SEric Paris 2220b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, 2221b09e0fa4SEric Paris void *buffer, size_t size) 2222b09e0fa4SEric Paris { 222338f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 2224b09e0fa4SEric Paris int err; 2225b09e0fa4SEric Paris 2226b09e0fa4SEric Paris /* 2227b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2228b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2229b09e0fa4SEric Paris * for it via sb->s_xattr. 2230b09e0fa4SEric Paris */ 2231b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2232b09e0fa4SEric Paris return generic_getxattr(dentry, name, buffer, size); 2233b09e0fa4SEric Paris 2234b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2235b09e0fa4SEric Paris if (err) 2236b09e0fa4SEric Paris return err; 2237b09e0fa4SEric Paris 223838f38657SAristeu Rozanski return simple_xattr_get(&info->xattrs, name, buffer, size); 2239b09e0fa4SEric Paris } 2240b09e0fa4SEric Paris 2241b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name, 2242b09e0fa4SEric Paris const void *value, size_t size, int flags) 2243b09e0fa4SEric Paris { 224438f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 2245b09e0fa4SEric Paris int err; 2246b09e0fa4SEric Paris 2247b09e0fa4SEric Paris /* 2248b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2249b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2250b09e0fa4SEric Paris * for it via sb->s_xattr. 2251b09e0fa4SEric Paris */ 2252b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2253b09e0fa4SEric Paris return generic_setxattr(dentry, name, value, size, flags); 2254b09e0fa4SEric Paris 2255b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2256b09e0fa4SEric Paris if (err) 2257b09e0fa4SEric Paris return err; 2258b09e0fa4SEric Paris 225938f38657SAristeu Rozanski return simple_xattr_set(&info->xattrs, name, value, size, flags); 2260b09e0fa4SEric Paris } 2261b09e0fa4SEric Paris 2262b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name) 2263b09e0fa4SEric Paris { 226438f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 2265b09e0fa4SEric Paris int err; 2266b09e0fa4SEric Paris 2267b09e0fa4SEric Paris /* 2268b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2269b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2270b09e0fa4SEric Paris * for it via sb->s_xattr. 2271b09e0fa4SEric Paris */ 2272b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2273b09e0fa4SEric Paris return generic_removexattr(dentry, name); 2274b09e0fa4SEric Paris 2275b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2276b09e0fa4SEric Paris if (err) 2277b09e0fa4SEric Paris return err; 2278b09e0fa4SEric Paris 227938f38657SAristeu Rozanski return simple_xattr_remove(&info->xattrs, name); 2280b09e0fa4SEric Paris } 2281b09e0fa4SEric Paris 2282b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 2283b09e0fa4SEric Paris { 228438f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 228538f38657SAristeu Rozanski return simple_xattr_list(&info->xattrs, buffer, size); 2286b09e0fa4SEric Paris } 2287b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 2288b09e0fa4SEric Paris 228969f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 22901da177e4SLinus Torvalds .readlink = generic_readlink, 229169f07ec9SHugh Dickins .follow_link = shmem_follow_short_symlink, 2292b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2293b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2294b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2295b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2296b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2297b09e0fa4SEric Paris #endif 22981da177e4SLinus Torvalds }; 22991da177e4SLinus Torvalds 230092e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 23011da177e4SLinus Torvalds .readlink = generic_readlink, 23021da177e4SLinus Torvalds .follow_link = shmem_follow_link, 23031da177e4SLinus Torvalds .put_link = shmem_put_link, 2304b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2305b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2306b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2307b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2308b09e0fa4SEric Paris .removexattr = shmem_removexattr, 230939f0247dSAndreas Gruenbacher #endif 2310b09e0fa4SEric Paris }; 231139f0247dSAndreas Gruenbacher 231291828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 231391828a40SDavid M. Grimes { 231491828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 231591828a40SDavid M. Grimes } 231691828a40SDavid M. Grimes 231791828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 231891828a40SDavid M. Grimes { 231991828a40SDavid M. Grimes __u32 *fh = vfh; 232091828a40SDavid M. Grimes __u64 inum = fh[2]; 232191828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 232291828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 232391828a40SDavid M. Grimes } 232491828a40SDavid M. Grimes 2325480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 2326480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 232791828a40SDavid M. Grimes { 232891828a40SDavid M. Grimes struct inode *inode; 2329480b116cSChristoph Hellwig struct dentry *dentry = NULL; 233035c2a7f4SHugh Dickins u64 inum; 233191828a40SDavid M. Grimes 2332480b116cSChristoph Hellwig if (fh_len < 3) 2333480b116cSChristoph Hellwig return NULL; 2334480b116cSChristoph Hellwig 233535c2a7f4SHugh Dickins inum = fid->raw[2]; 233635c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 233735c2a7f4SHugh Dickins 2338480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 2339480b116cSChristoph Hellwig shmem_match, fid->raw); 234091828a40SDavid M. Grimes if (inode) { 2341480b116cSChristoph Hellwig dentry = d_find_alias(inode); 234291828a40SDavid M. Grimes iput(inode); 234391828a40SDavid M. Grimes } 234491828a40SDavid M. Grimes 2345480b116cSChristoph Hellwig return dentry; 234691828a40SDavid M. Grimes } 234791828a40SDavid M. Grimes 2348b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 2349b0b0382bSAl Viro struct inode *parent) 235091828a40SDavid M. Grimes { 23515fe0c237SAneesh Kumar K.V if (*len < 3) { 23525fe0c237SAneesh Kumar K.V *len = 3; 235391828a40SDavid M. Grimes return 255; 23545fe0c237SAneesh Kumar K.V } 235591828a40SDavid M. Grimes 23561d3382cbSAl Viro if (inode_unhashed(inode)) { 235791828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 235891828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 235991828a40SDavid M. Grimes * time, we need a lock to ensure we only try 236091828a40SDavid M. Grimes * to do it once 236191828a40SDavid M. Grimes */ 236291828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 236391828a40SDavid M. Grimes spin_lock(&lock); 23641d3382cbSAl Viro if (inode_unhashed(inode)) 236591828a40SDavid M. Grimes __insert_inode_hash(inode, 236691828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 236791828a40SDavid M. Grimes spin_unlock(&lock); 236891828a40SDavid M. Grimes } 236991828a40SDavid M. Grimes 237091828a40SDavid M. Grimes fh[0] = inode->i_generation; 237191828a40SDavid M. Grimes fh[1] = inode->i_ino; 237291828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 237391828a40SDavid M. Grimes 237491828a40SDavid M. Grimes *len = 3; 237591828a40SDavid M. Grimes return 1; 237691828a40SDavid M. Grimes } 237791828a40SDavid M. Grimes 237839655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 237991828a40SDavid M. Grimes .get_parent = shmem_get_parent, 238091828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 2381480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 238291828a40SDavid M. Grimes }; 238391828a40SDavid M. Grimes 2384680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 2385680d794bSakpm@linux-foundation.org bool remount) 23861da177e4SLinus Torvalds { 23871da177e4SLinus Torvalds char *this_char, *value, *rest; 23888751e039SEric W. Biederman uid_t uid; 23898751e039SEric W. Biederman gid_t gid; 23901da177e4SLinus Torvalds 2391b00dc3adSHugh Dickins while (options != NULL) { 2392b00dc3adSHugh Dickins this_char = options; 2393b00dc3adSHugh Dickins for (;;) { 2394b00dc3adSHugh Dickins /* 2395b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 2396b00dc3adSHugh Dickins * mount options form a comma-separated list, 2397b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 2398b00dc3adSHugh Dickins */ 2399b00dc3adSHugh Dickins options = strchr(options, ','); 2400b00dc3adSHugh Dickins if (options == NULL) 2401b00dc3adSHugh Dickins break; 2402b00dc3adSHugh Dickins options++; 2403b00dc3adSHugh Dickins if (!isdigit(*options)) { 2404b00dc3adSHugh Dickins options[-1] = '\0'; 2405b00dc3adSHugh Dickins break; 2406b00dc3adSHugh Dickins } 2407b00dc3adSHugh Dickins } 24081da177e4SLinus Torvalds if (!*this_char) 24091da177e4SLinus Torvalds continue; 24101da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 24111da177e4SLinus Torvalds *value++ = 0; 24121da177e4SLinus Torvalds } else { 24131da177e4SLinus Torvalds printk(KERN_ERR 24141da177e4SLinus Torvalds "tmpfs: No value for mount option '%s'\n", 24151da177e4SLinus Torvalds this_char); 24161da177e4SLinus Torvalds return 1; 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds 24191da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 24201da177e4SLinus Torvalds unsigned long long size; 24211da177e4SLinus Torvalds size = memparse(value,&rest); 24221da177e4SLinus Torvalds if (*rest == '%') { 24231da177e4SLinus Torvalds size <<= PAGE_SHIFT; 24241da177e4SLinus Torvalds size *= totalram_pages; 24251da177e4SLinus Torvalds do_div(size, 100); 24261da177e4SLinus Torvalds rest++; 24271da177e4SLinus Torvalds } 24281da177e4SLinus Torvalds if (*rest) 24291da177e4SLinus Torvalds goto bad_val; 2430680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 2431680d794bSakpm@linux-foundation.org DIV_ROUND_UP(size, PAGE_CACHE_SIZE); 24321da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 2433680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 24341da177e4SLinus Torvalds if (*rest) 24351da177e4SLinus Torvalds goto bad_val; 24361da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 2437680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 24381da177e4SLinus Torvalds if (*rest) 24391da177e4SLinus Torvalds goto bad_val; 24401da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 2441680d794bSakpm@linux-foundation.org if (remount) 24421da177e4SLinus Torvalds continue; 2443680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 24441da177e4SLinus Torvalds if (*rest) 24451da177e4SLinus Torvalds goto bad_val; 24461da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 2447680d794bSakpm@linux-foundation.org if (remount) 24481da177e4SLinus Torvalds continue; 24498751e039SEric W. Biederman uid = simple_strtoul(value, &rest, 0); 24501da177e4SLinus Torvalds if (*rest) 24511da177e4SLinus Torvalds goto bad_val; 24528751e039SEric W. Biederman sbinfo->uid = make_kuid(current_user_ns(), uid); 24538751e039SEric W. Biederman if (!uid_valid(sbinfo->uid)) 24548751e039SEric W. Biederman goto bad_val; 24551da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 2456680d794bSakpm@linux-foundation.org if (remount) 24571da177e4SLinus Torvalds continue; 24588751e039SEric W. Biederman gid = simple_strtoul(value, &rest, 0); 24591da177e4SLinus Torvalds if (*rest) 24601da177e4SLinus Torvalds goto bad_val; 24618751e039SEric W. Biederman sbinfo->gid = make_kgid(current_user_ns(), gid); 24628751e039SEric W. Biederman if (!gid_valid(sbinfo->gid)) 24638751e039SEric W. Biederman goto bad_val; 24647339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 2465a7a88b23SHugh Dickins if (mpol_parse_str(value, &sbinfo->mpol)) 24667339ff83SRobin Holt goto bad_val; 24671da177e4SLinus Torvalds } else { 24681da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad mount option %s\n", 24691da177e4SLinus Torvalds this_char); 24701da177e4SLinus Torvalds return 1; 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds } 24731da177e4SLinus Torvalds return 0; 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds bad_val: 24761da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", 24771da177e4SLinus Torvalds value, this_char); 24781da177e4SLinus Torvalds return 1; 24791da177e4SLinus Torvalds 24801da177e4SLinus Torvalds } 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 24831da177e4SLinus Torvalds { 24841da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2485680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 24860edd73b3SHugh Dickins unsigned long inodes; 24870edd73b3SHugh Dickins int error = -EINVAL; 24881da177e4SLinus Torvalds 2489680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 24900edd73b3SHugh Dickins return error; 24910edd73b3SHugh Dickins 24920edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 24930edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 24947e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 24950edd73b3SHugh Dickins goto out; 2496680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 24970edd73b3SHugh Dickins goto out; 24980edd73b3SHugh Dickins /* 249954af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 25000edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 25010edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 25020edd73b3SHugh Dickins */ 2503680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 25040edd73b3SHugh Dickins goto out; 2505680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 25060edd73b3SHugh Dickins goto out; 25070edd73b3SHugh Dickins 25080edd73b3SHugh Dickins error = 0; 2509680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 2510680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 2511680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 251271fe804bSLee Schermerhorn 251371fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 251471fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 25150edd73b3SHugh Dickins out: 25160edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 25170edd73b3SHugh Dickins return error; 25181da177e4SLinus Torvalds } 2519680d794bSakpm@linux-foundation.org 252034c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 2521680d794bSakpm@linux-foundation.org { 252234c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 2523680d794bSakpm@linux-foundation.org 2524680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 2525680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 2526680d794bSakpm@linux-foundation.org sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); 2527680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 2528680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 2529680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 253009208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 25318751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 25328751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 25338751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 25348751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 25358751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 25368751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 253771fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 2538680d794bSakpm@linux-foundation.org return 0; 2539680d794bSakpm@linux-foundation.org } 2540680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 25431da177e4SLinus Torvalds { 2544602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2545602586a8SHugh Dickins 2546602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 2547602586a8SHugh Dickins kfree(sbinfo); 25481da177e4SLinus Torvalds sb->s_fs_info = NULL; 25491da177e4SLinus Torvalds } 25501da177e4SLinus Torvalds 25512b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 25521da177e4SLinus Torvalds { 25531da177e4SLinus Torvalds struct inode *inode; 25540edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 2555680d794bSakpm@linux-foundation.org int err = -ENOMEM; 2556680d794bSakpm@linux-foundation.org 2557680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 2558425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 2559680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 2560680d794bSakpm@linux-foundation.org if (!sbinfo) 2561680d794bSakpm@linux-foundation.org return -ENOMEM; 2562680d794bSakpm@linux-foundation.org 2563680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 256476aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 256576aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 2566680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 25671da177e4SLinus Torvalds 25680edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 25691da177e4SLinus Torvalds /* 25701da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 25711da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 25721da177e4SLinus Torvalds * but the internal instance is left unlimited. 25731da177e4SLinus Torvalds */ 25741da177e4SLinus Torvalds if (!(sb->s_flags & MS_NOUSER)) { 2575680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 2576680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 2577680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 2578680d794bSakpm@linux-foundation.org err = -EINVAL; 2579680d794bSakpm@linux-foundation.org goto failed; 2580680d794bSakpm@linux-foundation.org } 25811da177e4SLinus Torvalds } 258291828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 25832f6e38f3SHugh Dickins sb->s_flags |= MS_NOSEC; 25840edd73b3SHugh Dickins #else 25850edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 25860edd73b3SHugh Dickins #endif 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 2589602586a8SHugh Dickins if (percpu_counter_init(&sbinfo->used_blocks, 0)) 2590602586a8SHugh Dickins goto failed; 2591680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 25921da177e4SLinus Torvalds 2593285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 25941da177e4SLinus Torvalds sb->s_blocksize = PAGE_CACHE_SIZE; 25951da177e4SLinus Torvalds sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 25961da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 25971da177e4SLinus Torvalds sb->s_op = &shmem_ops; 2598cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 2599b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 260039f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 2601b09e0fa4SEric Paris #endif 2602b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 260339f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 260439f0247dSAndreas Gruenbacher #endif 26050edd73b3SHugh Dickins 2606454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 26071da177e4SLinus Torvalds if (!inode) 26081da177e4SLinus Torvalds goto failed; 2609680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 2610680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 2611318ceed0SAl Viro sb->s_root = d_make_root(inode); 2612318ceed0SAl Viro if (!sb->s_root) 261348fde701SAl Viro goto failed; 26141da177e4SLinus Torvalds return 0; 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds failed: 26171da177e4SLinus Torvalds shmem_put_super(sb); 26181da177e4SLinus Torvalds return err; 26191da177e4SLinus Torvalds } 26201da177e4SLinus Torvalds 2621fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 26241da177e4SLinus Torvalds { 262541ffe5d5SHugh Dickins struct shmem_inode_info *info; 262641ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 262741ffe5d5SHugh Dickins if (!info) 26281da177e4SLinus Torvalds return NULL; 262941ffe5d5SHugh Dickins return &info->vfs_inode; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 263241ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 2633fa0d7e3dSNick Piggin { 2634fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 2635fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 2636fa0d7e3dSNick Piggin } 2637fa0d7e3dSNick Piggin 26381da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 26391da177e4SLinus Torvalds { 264009208d15SAl Viro if (S_ISREG(inode->i_mode)) 26411da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 264241ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 26431da177e4SLinus Torvalds } 26441da177e4SLinus Torvalds 264541ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 26461da177e4SLinus Torvalds { 264741ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 264841ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 26491da177e4SLinus Torvalds } 26501da177e4SLinus Torvalds 265141ffe5d5SHugh Dickins static int shmem_init_inodecache(void) 26521da177e4SLinus Torvalds { 26531da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 26541da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 265541ffe5d5SHugh Dickins 0, SLAB_PANIC, shmem_init_inode); 26561da177e4SLinus Torvalds return 0; 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds 265941ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 26601da177e4SLinus Torvalds { 26611a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds 2664f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 26651da177e4SLinus Torvalds .writepage = shmem_writepage, 266676719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 26671da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2668800d15a5SNick Piggin .write_begin = shmem_write_begin, 2669800d15a5SNick Piggin .write_end = shmem_write_end, 26701da177e4SLinus Torvalds #endif 2671304dbdb7SLee Schermerhorn .migratepage = migrate_page, 2672aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 26731da177e4SLinus Torvalds }; 26741da177e4SLinus Torvalds 267515ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 26761da177e4SLinus Torvalds .mmap = shmem_mmap, 26771da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2678220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 2679bcd78e49SHugh Dickins .read = do_sync_read, 26805402b976SHugh Dickins .write = do_sync_write, 2681bcd78e49SHugh Dickins .aio_read = shmem_file_aio_read, 26825402b976SHugh Dickins .aio_write = generic_file_aio_write, 26831b061d92SChristoph Hellwig .fsync = noop_fsync, 2684708e3508SHugh Dickins .splice_read = shmem_file_splice_read, 2685ae976416SHugh Dickins .splice_write = generic_file_splice_write, 268683e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 26871da177e4SLinus Torvalds #endif 26881da177e4SLinus Torvalds }; 26891da177e4SLinus Torvalds 269092e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 269194c1e62dSHugh Dickins .setattr = shmem_setattr, 2692b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2693b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2694b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2695b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2696b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2697b09e0fa4SEric Paris #endif 26981da177e4SLinus Torvalds }; 26991da177e4SLinus Torvalds 270092e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 27011da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 27021da177e4SLinus Torvalds .create = shmem_create, 27031da177e4SLinus Torvalds .lookup = simple_lookup, 27041da177e4SLinus Torvalds .link = shmem_link, 27051da177e4SLinus Torvalds .unlink = shmem_unlink, 27061da177e4SLinus Torvalds .symlink = shmem_symlink, 27071da177e4SLinus Torvalds .mkdir = shmem_mkdir, 27081da177e4SLinus Torvalds .rmdir = shmem_rmdir, 27091da177e4SLinus Torvalds .mknod = shmem_mknod, 27101da177e4SLinus Torvalds .rename = shmem_rename, 27111da177e4SLinus Torvalds #endif 2712b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2713b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2714b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2715b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2716b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2717b09e0fa4SEric Paris #endif 271839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 271994c1e62dSHugh Dickins .setattr = shmem_setattr, 272039f0247dSAndreas Gruenbacher #endif 272139f0247dSAndreas Gruenbacher }; 272239f0247dSAndreas Gruenbacher 272392e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 2724b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2725b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2726b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2727b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2728b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2729b09e0fa4SEric Paris #endif 273039f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 273194c1e62dSHugh Dickins .setattr = shmem_setattr, 273239f0247dSAndreas Gruenbacher #endif 27331da177e4SLinus Torvalds }; 27341da177e4SLinus Torvalds 2735759b9775SHugh Dickins static const struct super_operations shmem_ops = { 27361da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 27371da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 27381da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 27391da177e4SLinus Torvalds .statfs = shmem_statfs, 27401da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 2741680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 27421da177e4SLinus Torvalds #endif 27431f895f75SAl Viro .evict_inode = shmem_evict_inode, 27441da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 27451da177e4SLinus Torvalds .put_super = shmem_put_super, 27461da177e4SLinus Torvalds }; 27471da177e4SLinus Torvalds 2748f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 274954cb8821SNick Piggin .fault = shmem_fault, 27501da177e4SLinus Torvalds #ifdef CONFIG_NUMA 27511da177e4SLinus Torvalds .set_policy = shmem_set_policy, 27521da177e4SLinus Torvalds .get_policy = shmem_get_policy, 27531da177e4SLinus Torvalds #endif 27540b173bc4SKonstantin Khlebnikov .remap_pages = generic_file_remap_pages, 27551da177e4SLinus Torvalds }; 27561da177e4SLinus Torvalds 27573c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 27583c26ff6eSAl Viro int flags, const char *dev_name, void *data) 27591da177e4SLinus Torvalds { 27603c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 27611da177e4SLinus Torvalds } 27621da177e4SLinus Torvalds 276341ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 27641da177e4SLinus Torvalds .owner = THIS_MODULE, 27651da177e4SLinus Torvalds .name = "tmpfs", 27663c26ff6eSAl Viro .mount = shmem_mount, 27671da177e4SLinus Torvalds .kill_sb = kill_litter_super, 27681da177e4SLinus Torvalds }; 27691da177e4SLinus Torvalds 277041ffe5d5SHugh Dickins int __init shmem_init(void) 27711da177e4SLinus Torvalds { 27721da177e4SLinus Torvalds int error; 27731da177e4SLinus Torvalds 2774e0bf68ddSPeter Zijlstra error = bdi_init(&shmem_backing_dev_info); 2775e0bf68ddSPeter Zijlstra if (error) 2776e0bf68ddSPeter Zijlstra goto out4; 2777e0bf68ddSPeter Zijlstra 277841ffe5d5SHugh Dickins error = shmem_init_inodecache(); 27791da177e4SLinus Torvalds if (error) 27801da177e4SLinus Torvalds goto out3; 27811da177e4SLinus Torvalds 278241ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 27831da177e4SLinus Torvalds if (error) { 27841da177e4SLinus Torvalds printk(KERN_ERR "Could not register tmpfs\n"); 27851da177e4SLinus Torvalds goto out2; 27861da177e4SLinus Torvalds } 278795dc112aSGreg Kroah-Hartman 278841ffe5d5SHugh Dickins shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER, 278941ffe5d5SHugh Dickins shmem_fs_type.name, NULL); 27901da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 27911da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 27921da177e4SLinus Torvalds printk(KERN_ERR "Could not kern_mount tmpfs\n"); 27931da177e4SLinus Torvalds goto out1; 27941da177e4SLinus Torvalds } 27951da177e4SLinus Torvalds return 0; 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds out1: 279841ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 27991da177e4SLinus Torvalds out2: 280041ffe5d5SHugh Dickins shmem_destroy_inodecache(); 28011da177e4SLinus Torvalds out3: 2802e0bf68ddSPeter Zijlstra bdi_destroy(&shmem_backing_dev_info); 2803e0bf68ddSPeter Zijlstra out4: 28041da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 28051da177e4SLinus Torvalds return error; 28061da177e4SLinus Torvalds } 2807853ac43aSMatt Mackall 2808853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 2809853ac43aSMatt Mackall 2810853ac43aSMatt Mackall /* 2811853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 2812853ac43aSMatt Mackall * 2813853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 2814853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 2815853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 2816853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 2817853ac43aSMatt Mackall */ 2818853ac43aSMatt Mackall 2819853ac43aSMatt Mackall #include <linux/ramfs.h> 2820853ac43aSMatt Mackall 282141ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 2822853ac43aSMatt Mackall .name = "tmpfs", 28233c26ff6eSAl Viro .mount = ramfs_mount, 2824853ac43aSMatt Mackall .kill_sb = kill_litter_super, 2825853ac43aSMatt Mackall }; 2826853ac43aSMatt Mackall 282741ffe5d5SHugh Dickins int __init shmem_init(void) 2828853ac43aSMatt Mackall { 282941ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 2830853ac43aSMatt Mackall 283141ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 2832853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 2833853ac43aSMatt Mackall 2834853ac43aSMatt Mackall return 0; 2835853ac43aSMatt Mackall } 2836853ac43aSMatt Mackall 283741ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 2838853ac43aSMatt Mackall { 2839853ac43aSMatt Mackall return 0; 2840853ac43aSMatt Mackall } 2841853ac43aSMatt Mackall 28423f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 28433f96b79aSHugh Dickins { 28443f96b79aSHugh Dickins return 0; 28453f96b79aSHugh Dickins } 28463f96b79aSHugh Dickins 284724513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 284824513264SHugh Dickins { 284924513264SHugh Dickins } 285024513264SHugh Dickins 285141ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 285294c1e62dSHugh Dickins { 285341ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 285494c1e62dSHugh Dickins } 285594c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 285694c1e62dSHugh Dickins 2857853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 28580b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 2859454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 28600b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 28610b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 2862853ac43aSMatt Mackall 2863853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 2864853ac43aSMatt Mackall 2865853ac43aSMatt Mackall /* common code */ 28661da177e4SLinus Torvalds 286746711810SRandy Dunlap /** 28681da177e4SLinus Torvalds * shmem_file_setup - get an unlinked file living in tmpfs 28691da177e4SLinus Torvalds * @name: name for dentry (to be seen in /proc/<pid>/maps 28701da177e4SLinus Torvalds * @size: size to be set for the file 28710b0a0806SHugh Dickins * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 28721da177e4SLinus Torvalds */ 2873168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 28741da177e4SLinus Torvalds { 28751da177e4SLinus Torvalds int error; 28761da177e4SLinus Torvalds struct file *file; 28771da177e4SLinus Torvalds struct inode *inode; 28782c48b9c4SAl Viro struct path path; 28792c48b9c4SAl Viro struct dentry *root; 28801da177e4SLinus Torvalds struct qstr this; 28811da177e4SLinus Torvalds 28821da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 28831da177e4SLinus Torvalds return (void *)shm_mnt; 28841da177e4SLinus Torvalds 2885285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 28861da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 28871da177e4SLinus Torvalds 28881da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 28891da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 28901da177e4SLinus Torvalds 28911da177e4SLinus Torvalds error = -ENOMEM; 28921da177e4SLinus Torvalds this.name = name; 28931da177e4SLinus Torvalds this.len = strlen(name); 28941da177e4SLinus Torvalds this.hash = 0; /* will go */ 28951da177e4SLinus Torvalds root = shm_mnt->mnt_root; 28962c48b9c4SAl Viro path.dentry = d_alloc(root, &this); 28972c48b9c4SAl Viro if (!path.dentry) 28981da177e4SLinus Torvalds goto put_memory; 28992c48b9c4SAl Viro path.mnt = mntget(shm_mnt); 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds error = -ENOSPC; 2902454abafeSDmitry Monakhov inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 29031da177e4SLinus Torvalds if (!inode) 29044b42af81SAl Viro goto put_dentry; 29051da177e4SLinus Torvalds 29062c48b9c4SAl Viro d_instantiate(path.dentry, inode); 29071da177e4SLinus Torvalds inode->i_size = size; 29086d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 2909853ac43aSMatt Mackall #ifndef CONFIG_MMU 2910853ac43aSMatt Mackall error = ramfs_nommu_expand_for_mapping(inode, size); 2911853ac43aSMatt Mackall if (error) 29124b42af81SAl Viro goto put_dentry; 2913853ac43aSMatt Mackall #endif 29144b42af81SAl Viro 29154b42af81SAl Viro error = -ENFILE; 29162c48b9c4SAl Viro file = alloc_file(&path, FMODE_WRITE | FMODE_READ, 29174b42af81SAl Viro &shmem_file_operations); 29184b42af81SAl Viro if (!file) 29194b42af81SAl Viro goto put_dentry; 29204b42af81SAl Viro 29211da177e4SLinus Torvalds return file; 29221da177e4SLinus Torvalds 29231da177e4SLinus Torvalds put_dentry: 29242c48b9c4SAl Viro path_put(&path); 29251da177e4SLinus Torvalds put_memory: 29261da177e4SLinus Torvalds shmem_unacct_size(flags, size); 29271da177e4SLinus Torvalds return ERR_PTR(error); 29281da177e4SLinus Torvalds } 2929395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 29301da177e4SLinus Torvalds 293146711810SRandy Dunlap /** 29321da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 29331da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 29341da177e4SLinus Torvalds */ 29351da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 29361da177e4SLinus Torvalds { 29371da177e4SLinus Torvalds struct file *file; 29381da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 29391da177e4SLinus Torvalds 29401da177e4SLinus Torvalds file = shmem_file_setup("dev/zero", size, vma->vm_flags); 29411da177e4SLinus Torvalds if (IS_ERR(file)) 29421da177e4SLinus Torvalds return PTR_ERR(file); 29431da177e4SLinus Torvalds 29441da177e4SLinus Torvalds if (vma->vm_file) 29451da177e4SLinus Torvalds fput(vma->vm_file); 29461da177e4SLinus Torvalds vma->vm_file = file; 29471da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 29481da177e4SLinus Torvalds return 0; 29491da177e4SLinus Torvalds } 2950d9d90e5eSHugh Dickins 2951d9d90e5eSHugh Dickins /** 2952d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 2953d9d90e5eSHugh Dickins * @mapping: the page's address_space 2954d9d90e5eSHugh Dickins * @index: the page index 2955d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 2956d9d90e5eSHugh Dickins * 2957d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 2958d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 2959d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 2960d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 2961d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 2962d9d90e5eSHugh Dickins * 296368da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 296468da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 2965d9d90e5eSHugh Dickins */ 2966d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 2967d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 2968d9d90e5eSHugh Dickins { 296968da9f05SHugh Dickins #ifdef CONFIG_SHMEM 297068da9f05SHugh Dickins struct inode *inode = mapping->host; 29719276aad6SHugh Dickins struct page *page; 297268da9f05SHugh Dickins int error; 297368da9f05SHugh Dickins 297468da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 297568da9f05SHugh Dickins error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL); 297668da9f05SHugh Dickins if (error) 297768da9f05SHugh Dickins page = ERR_PTR(error); 297868da9f05SHugh Dickins else 297968da9f05SHugh Dickins unlock_page(page); 298068da9f05SHugh Dickins return page; 298168da9f05SHugh Dickins #else 298268da9f05SHugh Dickins /* 298368da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 298468da9f05SHugh Dickins */ 2985d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 298668da9f05SHugh Dickins #endif 2987d9d90e5eSHugh Dickins } 2988d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 2989