11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Resizable virtual memory filesystem for Linux. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000 Linus Torvalds. 51da177e4SLinus Torvalds * 2000 Transmeta Corp. 61da177e4SLinus Torvalds * 2000-2001 Christoph Rohland 71da177e4SLinus Torvalds * 2000-2001 SAP AG 81da177e4SLinus Torvalds * 2002 Red Hat Inc. 96922c0c7SHugh Dickins * Copyright (C) 2002-2011 Hugh Dickins. 106922c0c7SHugh Dickins * Copyright (C) 2011 Google Inc. 110edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 121da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Extended attribute support for tmpfs: 151da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 161da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 171da177e4SLinus Torvalds * 18853ac43aSMatt Mackall * tiny-shmem: 19853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 20853ac43aSMatt Mackall * 211da177e4SLinus Torvalds * This file is released under the GPL. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 24853ac43aSMatt Mackall #include <linux/fs.h> 25853ac43aSMatt Mackall #include <linux/init.h> 26853ac43aSMatt Mackall #include <linux/vfs.h> 27853ac43aSMatt Mackall #include <linux/mount.h> 28250297edSAndrew Morton #include <linux/ramfs.h> 29caefba17SHugh Dickins #include <linux/pagemap.h> 30853ac43aSMatt Mackall #include <linux/file.h> 31853ac43aSMatt Mackall #include <linux/mm.h> 32b95f1b31SPaul Gortmaker #include <linux/export.h> 33853ac43aSMatt Mackall #include <linux/swap.h> 34a27bb332SKent Overstreet #include <linux/aio.h> 35853ac43aSMatt Mackall 36853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 37853ac43aSMatt Mackall 38853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 391da177e4SLinus Torvalds /* 401da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 411da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 421da177e4SLinus Torvalds * which makes it a completely usable filesystem. 431da177e4SLinus Torvalds */ 441da177e4SLinus Torvalds 4539f0247dSAndreas Gruenbacher #include <linux/xattr.h> 46a5694255SChristoph Hellwig #include <linux/exportfs.h> 471c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 48feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 491da177e4SLinus Torvalds #include <linux/mman.h> 501da177e4SLinus Torvalds #include <linux/string.h> 511da177e4SLinus Torvalds #include <linux/slab.h> 521da177e4SLinus Torvalds #include <linux/backing-dev.h> 531da177e4SLinus Torvalds #include <linux/shmem_fs.h> 541da177e4SLinus Torvalds #include <linux/writeback.h> 551da177e4SLinus Torvalds #include <linux/blkdev.h> 56bda97eabSHugh Dickins #include <linux/pagevec.h> 5741ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 5883e4fa9cSHugh Dickins #include <linux/falloc.h> 59708e3508SHugh Dickins #include <linux/splice.h> 601da177e4SLinus Torvalds #include <linux/security.h> 611da177e4SLinus Torvalds #include <linux/swapops.h> 621da177e4SLinus Torvalds #include <linux/mempolicy.h> 631da177e4SLinus Torvalds #include <linux/namei.h> 64b00dc3adSHugh Dickins #include <linux/ctype.h> 65304dbdb7SLee Schermerhorn #include <linux/migrate.h> 66c1f60a5aSChristoph Lameter #include <linux/highmem.h> 67680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 6892562927SMimi Zohar #include <linux/magic.h> 69304dbdb7SLee Schermerhorn 701da177e4SLinus Torvalds #include <asm/uaccess.h> 711da177e4SLinus Torvalds #include <asm/pgtable.h> 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) 741da177e4SLinus Torvalds #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 771da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 781da177e4SLinus Torvalds 7969f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 8069f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 8169f07ec9SHugh Dickins 821aac1400SHugh Dickins /* 83f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 84f00cdc6dSHugh Dickins * inode->i_private (with i_mutex making sure that it has only one user at 85f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 861aac1400SHugh Dickins */ 871aac1400SHugh Dickins struct shmem_falloc { 888e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 891aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 901aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 911aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 921aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 931aac1400SHugh Dickins }; 941aac1400SHugh Dickins 95285b2c4fSHugh Dickins /* Flag allocation requirements to shmem_getpage */ 961da177e4SLinus Torvalds enum sgp_type { 971da177e4SLinus Torvalds SGP_READ, /* don't exceed i_size, don't allocate page */ 981da177e4SLinus Torvalds SGP_CACHE, /* don't exceed i_size, may allocate page */ 99a0ee5ec5SHugh Dickins SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ 1001635f6a7SHugh Dickins SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */ 1011635f6a7SHugh Dickins SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */ 1021da177e4SLinus Torvalds }; 1031da177e4SLinus Torvalds 104b76db735SAndrew Morton #ifdef CONFIG_TMPFS 105680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 106680d794bSakpm@linux-foundation.org { 107680d794bSakpm@linux-foundation.org return totalram_pages / 2; 108680d794bSakpm@linux-foundation.org } 109680d794bSakpm@linux-foundation.org 110680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 111680d794bSakpm@linux-foundation.org { 112680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 113680d794bSakpm@linux-foundation.org } 114b76db735SAndrew Morton #endif 115680d794bSakpm@linux-foundation.org 116bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp); 117bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 118bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index); 11968da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 12068da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type); 12168da9f05SHugh Dickins 12268da9f05SHugh Dickins static inline int shmem_getpage(struct inode *inode, pgoff_t index, 12368da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, int *fault_type) 12468da9f05SHugh Dickins { 12568da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 12668da9f05SHugh Dickins mapping_gfp_mask(inode->i_mapping), fault_type); 12768da9f05SHugh Dickins } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds return sb->s_fs_info; 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* 1351da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1361da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1371da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1381da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1391da177e4SLinus Torvalds */ 1401da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1411da177e4SLinus Torvalds { 1420b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 143191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1471da177e4SLinus Torvalds { 1480b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1491da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds /* 1531da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 1541da177e4SLinus Torvalds * pages are allocated, in order to allow huge sparse files. 1551da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1561da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1571da177e4SLinus Torvalds */ 1581da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags) 1591da177e4SLinus Torvalds { 1600b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 161191c5424SAl Viro security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1651da177e4SLinus Torvalds { 1660b0a0806SHugh Dickins if (flags & VM_NORESERVE) 1671da177e4SLinus Torvalds vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 170759b9775SHugh Dickins static const struct super_operations shmem_ops; 171f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 17215ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 17392e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 17492e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 17592e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 176f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 1771da177e4SLinus Torvalds 1786c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info __read_mostly = { 1791da177e4SLinus Torvalds .ra_pages = 0, /* No readahead */ 1804f98a2feSRik van Riel .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, 1811da177e4SLinus Torvalds }; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 184cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 1851da177e4SLinus Torvalds 1865b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 1875b04c689SPavel Emelyanov { 1885b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1895b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 1905b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 1915b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 1925b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1935b04c689SPavel Emelyanov return -ENOSPC; 1945b04c689SPavel Emelyanov } 1955b04c689SPavel Emelyanov sbinfo->free_inodes--; 1965b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 1975b04c689SPavel Emelyanov } 1985b04c689SPavel Emelyanov return 0; 1995b04c689SPavel Emelyanov } 2005b04c689SPavel Emelyanov 2015b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2025b04c689SPavel Emelyanov { 2035b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2045b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2055b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2065b04c689SPavel Emelyanov sbinfo->free_inodes++; 2075b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2085b04c689SPavel Emelyanov } 2095b04c689SPavel Emelyanov } 2105b04c689SPavel Emelyanov 21146711810SRandy Dunlap /** 21241ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2131da177e4SLinus Torvalds * @inode: inode to recalc 2141da177e4SLinus Torvalds * 2151da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2161da177e4SLinus Torvalds * undirtied hole pages behind our back. 2171da177e4SLinus Torvalds * 2181da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2191da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2201da177e4SLinus Torvalds * 2211da177e4SLinus Torvalds * It has to be called with the spinlock held. 2221da177e4SLinus Torvalds */ 2231da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2261da177e4SLinus Torvalds long freed; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2291da177e4SLinus Torvalds if (freed > 0) { 23054af6042SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 23154af6042SHugh Dickins if (sbinfo->max_blocks) 23254af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -freed); 2331da177e4SLinus Torvalds info->alloced -= freed; 23454af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2351da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2397a5d0fbbSHugh Dickins /* 2407a5d0fbbSHugh Dickins * Replace item expected in radix tree by a new item, while holding tree lock. 2417a5d0fbbSHugh Dickins */ 2427a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping, 2437a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 2447a5d0fbbSHugh Dickins { 2457a5d0fbbSHugh Dickins void **pslot; 2466dbaf22cSJohannes Weiner void *item; 2477a5d0fbbSHugh Dickins 2487a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 2496dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 2507a5d0fbbSHugh Dickins pslot = radix_tree_lookup_slot(&mapping->page_tree, index); 2516dbaf22cSJohannes Weiner if (!pslot) 2526dbaf22cSJohannes Weiner return -ENOENT; 2536dbaf22cSJohannes Weiner item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock); 2547a5d0fbbSHugh Dickins if (item != expected) 2557a5d0fbbSHugh Dickins return -ENOENT; 2567a5d0fbbSHugh Dickins radix_tree_replace_slot(pslot, replacement); 2577a5d0fbbSHugh Dickins return 0; 2587a5d0fbbSHugh Dickins } 2597a5d0fbbSHugh Dickins 2607a5d0fbbSHugh Dickins /* 261d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 262d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 263d1899228SHugh Dickins * 264d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 265d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 266d1899228SHugh Dickins */ 267d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 268d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 269d1899228SHugh Dickins { 270d1899228SHugh Dickins void *item; 271d1899228SHugh Dickins 272d1899228SHugh Dickins rcu_read_lock(); 273d1899228SHugh Dickins item = radix_tree_lookup(&mapping->page_tree, index); 274d1899228SHugh Dickins rcu_read_unlock(); 275d1899228SHugh Dickins return item == swp_to_radix_entry(swap); 276d1899228SHugh Dickins } 277d1899228SHugh Dickins 278d1899228SHugh Dickins /* 27946f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 28046f65ec1SHugh Dickins */ 28146f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 28246f65ec1SHugh Dickins struct address_space *mapping, 28346f65ec1SHugh Dickins pgoff_t index, gfp_t gfp, void *expected) 28446f65ec1SHugh Dickins { 285b065b432SHugh Dickins int error; 28646f65ec1SHugh Dickins 287309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 288309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapBacked(page), page); 28946f65ec1SHugh Dickins 29046f65ec1SHugh Dickins page_cache_get(page); 29146f65ec1SHugh Dickins page->mapping = mapping; 29246f65ec1SHugh Dickins page->index = index; 29346f65ec1SHugh Dickins 29446f65ec1SHugh Dickins spin_lock_irq(&mapping->tree_lock); 29546f65ec1SHugh Dickins if (!expected) 296b065b432SHugh Dickins error = radix_tree_insert(&mapping->page_tree, index, page); 29746f65ec1SHugh Dickins else 298b065b432SHugh Dickins error = shmem_radix_tree_replace(mapping, index, expected, 299b065b432SHugh Dickins page); 30046f65ec1SHugh Dickins if (!error) { 30146f65ec1SHugh Dickins mapping->nrpages++; 30246f65ec1SHugh Dickins __inc_zone_page_state(page, NR_FILE_PAGES); 30346f65ec1SHugh Dickins __inc_zone_page_state(page, NR_SHMEM); 30446f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 30546f65ec1SHugh Dickins } else { 30646f65ec1SHugh Dickins page->mapping = NULL; 30746f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 30846f65ec1SHugh Dickins page_cache_release(page); 30946f65ec1SHugh Dickins } 31046f65ec1SHugh Dickins return error; 31146f65ec1SHugh Dickins } 31246f65ec1SHugh Dickins 31346f65ec1SHugh Dickins /* 3146922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 3156922c0c7SHugh Dickins */ 3166922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 3176922c0c7SHugh Dickins { 3186922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 3196922c0c7SHugh Dickins int error; 3206922c0c7SHugh Dickins 3216922c0c7SHugh Dickins spin_lock_irq(&mapping->tree_lock); 3226922c0c7SHugh Dickins error = shmem_radix_tree_replace(mapping, page->index, page, radswap); 3236922c0c7SHugh Dickins page->mapping = NULL; 3246922c0c7SHugh Dickins mapping->nrpages--; 3256922c0c7SHugh Dickins __dec_zone_page_state(page, NR_FILE_PAGES); 3266922c0c7SHugh Dickins __dec_zone_page_state(page, NR_SHMEM); 3276922c0c7SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 3286922c0c7SHugh Dickins page_cache_release(page); 3296922c0c7SHugh Dickins BUG_ON(error); 3306922c0c7SHugh Dickins } 3316922c0c7SHugh Dickins 3326922c0c7SHugh Dickins /* 3337a5d0fbbSHugh Dickins * Remove swap entry from radix tree, free the swap and its page cache. 3347a5d0fbbSHugh Dickins */ 3357a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 3367a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 3377a5d0fbbSHugh Dickins { 3386dbaf22cSJohannes Weiner void *old; 3397a5d0fbbSHugh Dickins 3407a5d0fbbSHugh Dickins spin_lock_irq(&mapping->tree_lock); 3416dbaf22cSJohannes Weiner old = radix_tree_delete_item(&mapping->page_tree, index, radswap); 3427a5d0fbbSHugh Dickins spin_unlock_irq(&mapping->tree_lock); 3436dbaf22cSJohannes Weiner if (old != radswap) 3446dbaf22cSJohannes Weiner return -ENOENT; 3457a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 3466dbaf22cSJohannes Weiner return 0; 3477a5d0fbbSHugh Dickins } 3487a5d0fbbSHugh Dickins 3497a5d0fbbSHugh Dickins /* 35024513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 35124513264SHugh Dickins */ 35224513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 35324513264SHugh Dickins { 35424513264SHugh Dickins struct pagevec pvec; 35524513264SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 35624513264SHugh Dickins pgoff_t index = 0; 35724513264SHugh Dickins 35824513264SHugh Dickins pagevec_init(&pvec, 0); 35924513264SHugh Dickins /* 36024513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 36124513264SHugh Dickins */ 36224513264SHugh Dickins while (!mapping_unevictable(mapping)) { 36324513264SHugh Dickins /* 36424513264SHugh Dickins * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it 36524513264SHugh Dickins * has finished, if it hits a row of PAGEVEC_SIZE swap entries. 36624513264SHugh Dickins */ 3670cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 36824513264SHugh Dickins PAGEVEC_SIZE, pvec.pages, indices); 36924513264SHugh Dickins if (!pvec.nr) 37024513264SHugh Dickins break; 37124513264SHugh Dickins index = indices[pvec.nr - 1] + 1; 3720cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 37324513264SHugh Dickins check_move_unevictable_pages(pvec.pages, pvec.nr); 37424513264SHugh Dickins pagevec_release(&pvec); 37524513264SHugh Dickins cond_resched(); 37624513264SHugh Dickins } 3777a5d0fbbSHugh Dickins } 3787a5d0fbbSHugh Dickins 3797a5d0fbbSHugh Dickins /* 3807a5d0fbbSHugh Dickins * Remove range of pages and swap entries from radix tree, and free them. 3811635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 3827a5d0fbbSHugh Dickins */ 3831635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 3841635f6a7SHugh Dickins bool unfalloc) 3851da177e4SLinus Torvalds { 386285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 3871da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 388285b2c4fSHugh Dickins pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 38983e4fa9cSHugh Dickins pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT; 39083e4fa9cSHugh Dickins unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1); 39183e4fa9cSHugh Dickins unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1); 392bda97eabSHugh Dickins struct pagevec pvec; 3937a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 3947a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 395285b2c4fSHugh Dickins pgoff_t index; 396bda97eabSHugh Dickins int i; 3971da177e4SLinus Torvalds 39883e4fa9cSHugh Dickins if (lend == -1) 39983e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 400bda97eabSHugh Dickins 401bda97eabSHugh Dickins pagevec_init(&pvec, 0); 402bda97eabSHugh Dickins index = start; 40383e4fa9cSHugh Dickins while (index < end) { 4040cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 40583e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 4067a5d0fbbSHugh Dickins pvec.pages, indices); 4077a5d0fbbSHugh Dickins if (!pvec.nr) 4087a5d0fbbSHugh Dickins break; 409bda97eabSHugh Dickins mem_cgroup_uncharge_start(); 410bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 411bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 412bda97eabSHugh Dickins 4137a5d0fbbSHugh Dickins index = indices[i]; 41483e4fa9cSHugh Dickins if (index >= end) 415bda97eabSHugh Dickins break; 416bda97eabSHugh Dickins 4177a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 4181635f6a7SHugh Dickins if (unfalloc) 4191635f6a7SHugh Dickins continue; 4207a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 4217a5d0fbbSHugh Dickins index, page); 4227a5d0fbbSHugh Dickins continue; 4237a5d0fbbSHugh Dickins } 4247a5d0fbbSHugh Dickins 425bda97eabSHugh Dickins if (!trylock_page(page)) 426bda97eabSHugh Dickins continue; 4271635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 4287a5d0fbbSHugh Dickins if (page->mapping == mapping) { 429309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 430bda97eabSHugh Dickins truncate_inode_page(mapping, page); 4317a5d0fbbSHugh Dickins } 4321635f6a7SHugh Dickins } 433bda97eabSHugh Dickins unlock_page(page); 434bda97eabSHugh Dickins } 4350cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 43624513264SHugh Dickins pagevec_release(&pvec); 437bda97eabSHugh Dickins mem_cgroup_uncharge_end(); 438bda97eabSHugh Dickins cond_resched(); 439bda97eabSHugh Dickins index++; 440bda97eabSHugh Dickins } 441bda97eabSHugh Dickins 44283e4fa9cSHugh Dickins if (partial_start) { 443bda97eabSHugh Dickins struct page *page = NULL; 444bda97eabSHugh Dickins shmem_getpage(inode, start - 1, &page, SGP_READ, NULL); 445bda97eabSHugh Dickins if (page) { 44683e4fa9cSHugh Dickins unsigned int top = PAGE_CACHE_SIZE; 44783e4fa9cSHugh Dickins if (start > end) { 44883e4fa9cSHugh Dickins top = partial_end; 44983e4fa9cSHugh Dickins partial_end = 0; 45083e4fa9cSHugh Dickins } 45183e4fa9cSHugh Dickins zero_user_segment(page, partial_start, top); 452bda97eabSHugh Dickins set_page_dirty(page); 453bda97eabSHugh Dickins unlock_page(page); 454bda97eabSHugh Dickins page_cache_release(page); 455bda97eabSHugh Dickins } 456bda97eabSHugh Dickins } 45783e4fa9cSHugh Dickins if (partial_end) { 45883e4fa9cSHugh Dickins struct page *page = NULL; 45983e4fa9cSHugh Dickins shmem_getpage(inode, end, &page, SGP_READ, NULL); 46083e4fa9cSHugh Dickins if (page) { 46183e4fa9cSHugh Dickins zero_user_segment(page, 0, partial_end); 46283e4fa9cSHugh Dickins set_page_dirty(page); 46383e4fa9cSHugh Dickins unlock_page(page); 46483e4fa9cSHugh Dickins page_cache_release(page); 46583e4fa9cSHugh Dickins } 46683e4fa9cSHugh Dickins } 46783e4fa9cSHugh Dickins if (start >= end) 46883e4fa9cSHugh Dickins return; 469bda97eabSHugh Dickins 470bda97eabSHugh Dickins index = start; 471b1a36650SHugh Dickins while (index < end) { 472bda97eabSHugh Dickins cond_resched(); 4730cd6144aSJohannes Weiner 4740cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 47583e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 4767a5d0fbbSHugh Dickins pvec.pages, indices); 4777a5d0fbbSHugh Dickins if (!pvec.nr) { 478b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 479b1a36650SHugh Dickins if (index == start || end != -1) 480bda97eabSHugh Dickins break; 481b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 482bda97eabSHugh Dickins index = start; 483bda97eabSHugh Dickins continue; 484bda97eabSHugh Dickins } 485bda97eabSHugh Dickins mem_cgroup_uncharge_start(); 486bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 487bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 488bda97eabSHugh Dickins 4897a5d0fbbSHugh Dickins index = indices[i]; 49083e4fa9cSHugh Dickins if (index >= end) 491bda97eabSHugh Dickins break; 492bda97eabSHugh Dickins 4937a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 4941635f6a7SHugh Dickins if (unfalloc) 4951635f6a7SHugh Dickins continue; 496b1a36650SHugh Dickins if (shmem_free_swap(mapping, index, page)) { 497b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 498b1a36650SHugh Dickins index--; 499b1a36650SHugh Dickins break; 500b1a36650SHugh Dickins } 501b1a36650SHugh Dickins nr_swaps_freed++; 5027a5d0fbbSHugh Dickins continue; 5037a5d0fbbSHugh Dickins } 5047a5d0fbbSHugh Dickins 505bda97eabSHugh Dickins lock_page(page); 5061635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 5077a5d0fbbSHugh Dickins if (page->mapping == mapping) { 508309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 509bda97eabSHugh Dickins truncate_inode_page(mapping, page); 510b1a36650SHugh Dickins } else { 511b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 512b1a36650SHugh Dickins unlock_page(page); 513b1a36650SHugh Dickins index--; 514b1a36650SHugh Dickins break; 5157a5d0fbbSHugh Dickins } 5161635f6a7SHugh Dickins } 517bda97eabSHugh Dickins unlock_page(page); 518bda97eabSHugh Dickins } 5190cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 52024513264SHugh Dickins pagevec_release(&pvec); 521bda97eabSHugh Dickins mem_cgroup_uncharge_end(); 522bda97eabSHugh Dickins index++; 523bda97eabSHugh Dickins } 52494c1e62dSHugh Dickins 5251da177e4SLinus Torvalds spin_lock(&info->lock); 5267a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 5271da177e4SLinus Torvalds shmem_recalc_inode(inode); 5281da177e4SLinus Torvalds spin_unlock(&info->lock); 5291635f6a7SHugh Dickins } 5301da177e4SLinus Torvalds 5311635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 5321635f6a7SHugh Dickins { 5331635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 534285b2c4fSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 5351da177e4SLinus Torvalds } 53694c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 5371da177e4SLinus Torvalds 53894c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 5411da177e4SLinus Torvalds int error; 5421da177e4SLinus Torvalds 543db78b877SChristoph Hellwig error = inode_change_ok(inode, attr); 544db78b877SChristoph Hellwig if (error) 545db78b877SChristoph Hellwig return error; 546db78b877SChristoph Hellwig 54794c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 54894c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 54994c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 5503889e6e7Snpiggin@suse.de 55194c1e62dSHugh Dickins if (newsize != oldsize) { 55294c1e62dSHugh Dickins i_size_write(inode, newsize); 55394c1e62dSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 55494c1e62dSHugh Dickins } 55594c1e62dSHugh Dickins if (newsize < oldsize) { 55694c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 55794c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 55894c1e62dSHugh Dickins shmem_truncate_range(inode, newsize, (loff_t)-1); 55994c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 56094c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 56194c1e62dSHugh Dickins } 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 5646a1a90adSChristoph Hellwig setattr_copy(inode, attr); 565db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 566feda821eSChristoph Hellwig error = posix_acl_chmod(inode, inode->i_mode); 5671da177e4SLinus Torvalds return error; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5701f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 5711da177e4SLinus Torvalds { 5721da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 5731da177e4SLinus Torvalds 5743889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 5751da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 5761da177e4SLinus Torvalds inode->i_size = 0; 5773889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 5781da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 579cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 5801da177e4SLinus Torvalds list_del_init(&info->swaplist); 581cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 5821da177e4SLinus Torvalds } 58369f07ec9SHugh Dickins } else 58469f07ec9SHugh Dickins kfree(info->symlink); 585b09e0fa4SEric Paris 58638f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 5870f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 5885b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 589dbd5768fSJan Kara clear_inode(inode); 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 59246f65ec1SHugh Dickins /* 59346f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 59446f65ec1SHugh Dickins */ 59541ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 596bde05d1cSHugh Dickins swp_entry_t swap, struct page **pagep) 5971da177e4SLinus Torvalds { 598285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 59946f65ec1SHugh Dickins void *radswap; 60041ffe5d5SHugh Dickins pgoff_t index; 601bde05d1cSHugh Dickins gfp_t gfp; 602bde05d1cSHugh Dickins int error = 0; 6031da177e4SLinus Torvalds 60446f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 605e504f3fdSHugh Dickins index = radix_tree_locate_item(&mapping->page_tree, radswap); 60646f65ec1SHugh Dickins if (index == -1) 6071da177e4SLinus Torvalds return 0; 6082e0e26c7SHugh Dickins 6091b1b32f2SHugh Dickins /* 6101b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 6111f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 6121b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 613285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 6141b1b32f2SHugh Dickins */ 6151b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 6162e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 6172e0e26c7SHugh Dickins 618bde05d1cSHugh Dickins gfp = mapping_gfp_mask(mapping); 619bde05d1cSHugh Dickins if (shmem_should_replace_page(*pagep, gfp)) { 620bde05d1cSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 621bde05d1cSHugh Dickins error = shmem_replace_page(pagep, gfp, info, index); 622bde05d1cSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 623bde05d1cSHugh Dickins /* 624bde05d1cSHugh Dickins * We needed to drop mutex to make that restrictive page 6250142ef6cSHugh Dickins * allocation, but the inode might have been freed while we 6260142ef6cSHugh Dickins * dropped it: although a racing shmem_evict_inode() cannot 6270142ef6cSHugh Dickins * complete without emptying the radix_tree, our page lock 6280142ef6cSHugh Dickins * on this swapcache page is not enough to prevent that - 6290142ef6cSHugh Dickins * free_swap_and_cache() of our swap entry will only 6300142ef6cSHugh Dickins * trylock_page(), removing swap from radix_tree whatever. 6310142ef6cSHugh Dickins * 6320142ef6cSHugh Dickins * We must not proceed to shmem_add_to_page_cache() if the 6330142ef6cSHugh Dickins * inode has been freed, but of course we cannot rely on 6340142ef6cSHugh Dickins * inode or mapping or info to check that. However, we can 6350142ef6cSHugh Dickins * safely check if our swap entry is still in use (and here 6360142ef6cSHugh Dickins * it can't have got reused for another page): if it's still 6370142ef6cSHugh Dickins * in use, then the inode cannot have been freed yet, and we 6380142ef6cSHugh Dickins * can safely proceed (if it's no longer in use, that tells 6390142ef6cSHugh Dickins * nothing about the inode, but we don't need to unuse swap). 640bde05d1cSHugh Dickins */ 641bde05d1cSHugh Dickins if (!page_swapcount(*pagep)) 642bde05d1cSHugh Dickins error = -ENOENT; 643bde05d1cSHugh Dickins } 644bde05d1cSHugh Dickins 645d13d1443SKAMEZAWA Hiroyuki /* 646778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 647778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 648778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 649d13d1443SKAMEZAWA Hiroyuki */ 650bde05d1cSHugh Dickins if (!error) 651bde05d1cSHugh Dickins error = shmem_add_to_page_cache(*pagep, mapping, index, 65246f65ec1SHugh Dickins GFP_NOWAIT, radswap); 65348f170fbSHugh Dickins if (error != -ENOMEM) { 65446f65ec1SHugh Dickins /* 65546f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 65646f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 65746f65ec1SHugh Dickins */ 658bde05d1cSHugh Dickins delete_from_swap_cache(*pagep); 659bde05d1cSHugh Dickins set_page_dirty(*pagep); 66046f65ec1SHugh Dickins if (!error) { 66146f65ec1SHugh Dickins spin_lock(&info->lock); 662285b2c4fSHugh Dickins info->swapped--; 66346f65ec1SHugh Dickins spin_unlock(&info->lock); 66441ffe5d5SHugh Dickins swap_free(swap); 66546f65ec1SHugh Dickins } 6662e0e26c7SHugh Dickins error = 1; /* not an error, but entry was found */ 6671da177e4SLinus Torvalds } 6682e0e26c7SHugh Dickins return error; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds /* 67246f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 6731da177e4SLinus Torvalds */ 67441ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 6751da177e4SLinus Torvalds { 67641ffe5d5SHugh Dickins struct list_head *this, *next; 6771da177e4SLinus Torvalds struct shmem_inode_info *info; 6781da177e4SLinus Torvalds int found = 0; 679bde05d1cSHugh Dickins int error = 0; 680bde05d1cSHugh Dickins 681bde05d1cSHugh Dickins /* 682bde05d1cSHugh Dickins * There's a faint possibility that swap page was replaced before 6830142ef6cSHugh Dickins * caller locked it: caller will come back later with the right page. 684bde05d1cSHugh Dickins */ 6850142ef6cSHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) 686bde05d1cSHugh Dickins goto out; 687778dd893SHugh Dickins 688778dd893SHugh Dickins /* 689778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 690778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 691778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 692778dd893SHugh Dickins */ 693d715ae08SMichal Hocko error = mem_cgroup_charge_file(page, current->mm, GFP_KERNEL); 694778dd893SHugh Dickins if (error) 695778dd893SHugh Dickins goto out; 69646f65ec1SHugh Dickins /* No radix_tree_preload: swap entry keeps a place for page in tree */ 6971da177e4SLinus Torvalds 698cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 69941ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 70041ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 701285b2c4fSHugh Dickins if (info->swapped) 702bde05d1cSHugh Dickins found = shmem_unuse_inode(info, swap, &page); 7036922c0c7SHugh Dickins else 7046922c0c7SHugh Dickins list_del_init(&info->swaplist); 705cb5f7b9aSHugh Dickins cond_resched(); 7062e0e26c7SHugh Dickins if (found) 707778dd893SHugh Dickins break; 7081da177e4SLinus Torvalds } 709cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 710778dd893SHugh Dickins 711778dd893SHugh Dickins if (found < 0) 712778dd893SHugh Dickins error = found; 713778dd893SHugh Dickins out: 714aaa46865SHugh Dickins unlock_page(page); 715aaa46865SHugh Dickins page_cache_release(page); 716778dd893SHugh Dickins return error; 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds /* 7201da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 7211da177e4SLinus Torvalds */ 7221da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 7231da177e4SLinus Torvalds { 7241da177e4SLinus Torvalds struct shmem_inode_info *info; 7251da177e4SLinus Torvalds struct address_space *mapping; 7261da177e4SLinus Torvalds struct inode *inode; 7276922c0c7SHugh Dickins swp_entry_t swap; 7286922c0c7SHugh Dickins pgoff_t index; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 7311da177e4SLinus Torvalds mapping = page->mapping; 7321da177e4SLinus Torvalds index = page->index; 7331da177e4SLinus Torvalds inode = mapping->host; 7341da177e4SLinus Torvalds info = SHMEM_I(inode); 7351da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 7361da177e4SLinus Torvalds goto redirty; 737d9fe526aSHugh Dickins if (!total_swap_pages) 7381da177e4SLinus Torvalds goto redirty; 7391da177e4SLinus Torvalds 740d9fe526aSHugh Dickins /* 741d9fe526aSHugh Dickins * shmem_backing_dev_info's capabilities prevent regular writeback or 742d9fe526aSHugh Dickins * sync from ever calling shmem_writepage; but a stacking filesystem 74348f170fbSHugh Dickins * might use ->writepage of its underlying filesystem, in which case 744d9fe526aSHugh Dickins * tmpfs should write out to swap only in response to memory pressure, 74548f170fbSHugh Dickins * and not for the writeback threads or sync. 746d9fe526aSHugh Dickins */ 74748f170fbSHugh Dickins if (!wbc->for_reclaim) { 74848f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 74948f170fbSHugh Dickins goto redirty; 75048f170fbSHugh Dickins } 7511635f6a7SHugh Dickins 7521635f6a7SHugh Dickins /* 7531635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 7541635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 7551635f6a7SHugh Dickins * fallocated page arriving here is now to initialize it and write it. 7561aac1400SHugh Dickins * 7571aac1400SHugh Dickins * That's okay for a page already fallocated earlier, but if we have 7581aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 7591aac1400SHugh Dickins * of this page in case we have to undo it, and (b) it may not be a 7601aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 7611aac1400SHugh Dickins * reactivate the page, and let shmem_fallocate() quit when too many. 7621635f6a7SHugh Dickins */ 7631635f6a7SHugh Dickins if (!PageUptodate(page)) { 7641aac1400SHugh Dickins if (inode->i_private) { 7651aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 7661aac1400SHugh Dickins spin_lock(&inode->i_lock); 7671aac1400SHugh Dickins shmem_falloc = inode->i_private; 7681aac1400SHugh Dickins if (shmem_falloc && 7698e205f77SHugh Dickins !shmem_falloc->waitq && 7701aac1400SHugh Dickins index >= shmem_falloc->start && 7711aac1400SHugh Dickins index < shmem_falloc->next) 7721aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 7731aac1400SHugh Dickins else 7741aac1400SHugh Dickins shmem_falloc = NULL; 7751aac1400SHugh Dickins spin_unlock(&inode->i_lock); 7761aac1400SHugh Dickins if (shmem_falloc) 7771aac1400SHugh Dickins goto redirty; 7781aac1400SHugh Dickins } 7791635f6a7SHugh Dickins clear_highpage(page); 7801635f6a7SHugh Dickins flush_dcache_page(page); 7811635f6a7SHugh Dickins SetPageUptodate(page); 7821635f6a7SHugh Dickins } 7831635f6a7SHugh Dickins 784d9fe526aSHugh Dickins swap = get_swap_page(); 78548f170fbSHugh Dickins if (!swap.val) 78648f170fbSHugh Dickins goto redirty; 787d9fe526aSHugh Dickins 788b1dea800SHugh Dickins /* 789b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 7906922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 7916922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 792b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 7936922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 7946922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 795b1dea800SHugh Dickins */ 796b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 79705bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 79805bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 799b1dea800SHugh Dickins 80048f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 801aaa46865SHugh Dickins swap_shmem_alloc(swap); 8026922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 8036922c0c7SHugh Dickins 8046922c0c7SHugh Dickins spin_lock(&info->lock); 8056922c0c7SHugh Dickins info->swapped++; 8066922c0c7SHugh Dickins shmem_recalc_inode(inode); 807826267cfSHugh Dickins spin_unlock(&info->lock); 8086922c0c7SHugh Dickins 8096922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 810d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 8119fab5619SHugh Dickins swap_writepage(page, wbc); 8121da177e4SLinus Torvalds return 0; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds 8156922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 816cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, NULL); 8171da177e4SLinus Torvalds redirty: 8181da177e4SLinus Torvalds set_page_dirty(page); 819d9fe526aSHugh Dickins if (wbc->for_reclaim) 820d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 821d9fe526aSHugh Dickins unlock_page(page); 822d9fe526aSHugh Dickins return 0; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds #ifdef CONFIG_NUMA 826680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 82771fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 828680d794bSakpm@linux-foundation.org { 829680d794bSakpm@linux-foundation.org char buffer[64]; 830680d794bSakpm@linux-foundation.org 83171fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 832095f1fc4SLee Schermerhorn return; /* show nothing */ 833095f1fc4SLee Schermerhorn 834a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 835095f1fc4SLee Schermerhorn 836095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 837680d794bSakpm@linux-foundation.org } 83871fe804bSLee Schermerhorn 83971fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 84071fe804bSLee Schermerhorn { 84171fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 84271fe804bSLee Schermerhorn if (sbinfo->mpol) { 84371fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 84471fe804bSLee Schermerhorn mpol = sbinfo->mpol; 84571fe804bSLee Schermerhorn mpol_get(mpol); 84671fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 84771fe804bSLee Schermerhorn } 84871fe804bSLee Schermerhorn return mpol; 84971fe804bSLee Schermerhorn } 850680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 851680d794bSakpm@linux-foundation.org 85241ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 85341ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 8541da177e4SLinus Torvalds { 8551da177e4SLinus Torvalds struct vm_area_struct pvma; 85618a2f371SMel Gorman struct page *page; 8571da177e4SLinus Torvalds 858c4cc6d07SHugh Dickins /* Create a pseudo vma that just contains the policy */ 859c4cc6d07SHugh Dickins pvma.vm_start = 0; 86009c231cbSNathan Zimmer /* Bias interleave by inode number to distribute better across nodes */ 86109c231cbSNathan Zimmer pvma.vm_pgoff = index + info->vfs_inode.i_ino; 862c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 86341ffe5d5SHugh Dickins pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 86452cd3b07SLee Schermerhorn 86518a2f371SMel Gorman page = swapin_readahead(swap, gfp, &pvma, 0); 86618a2f371SMel Gorman 86718a2f371SMel Gorman /* Drop reference taken by mpol_shared_policy_lookup() */ 86818a2f371SMel Gorman mpol_cond_put(pvma.vm_policy); 86918a2f371SMel Gorman 87018a2f371SMel Gorman return page; 87118a2f371SMel Gorman } 87218a2f371SMel Gorman 87318a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp, 87418a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 87518a2f371SMel Gorman { 87618a2f371SMel Gorman struct vm_area_struct pvma; 87718a2f371SMel Gorman struct page *page; 87818a2f371SMel Gorman 87918a2f371SMel Gorman /* Create a pseudo vma that just contains the policy */ 88018a2f371SMel Gorman pvma.vm_start = 0; 88118a2f371SMel Gorman /* Bias interleave by inode number to distribute better across nodes */ 88218a2f371SMel Gorman pvma.vm_pgoff = index + info->vfs_inode.i_ino; 88318a2f371SMel Gorman pvma.vm_ops = NULL; 88418a2f371SMel Gorman pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); 88518a2f371SMel Gorman 88618a2f371SMel Gorman page = alloc_page_vma(gfp, &pvma, 0); 88718a2f371SMel Gorman 88818a2f371SMel Gorman /* Drop reference taken by mpol_shared_policy_lookup() */ 88918a2f371SMel Gorman mpol_cond_put(pvma.vm_policy); 89018a2f371SMel Gorman 89118a2f371SMel Gorman return page; 8921da177e4SLinus Torvalds } 893680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */ 894680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 89541ffe5d5SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 896680d794bSakpm@linux-foundation.org { 897680d794bSakpm@linux-foundation.org } 898680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 899680d794bSakpm@linux-foundation.org 90041ffe5d5SHugh Dickins static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 90141ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 9021da177e4SLinus Torvalds { 90341ffe5d5SHugh Dickins return swapin_readahead(swap, gfp, NULL, 0); 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 90602098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp, 90741ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 9081da177e4SLinus Torvalds { 909e84e2e13SHugh Dickins return alloc_page(gfp); 9101da177e4SLinus Torvalds } 911680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */ 9121da177e4SLinus Torvalds 91371fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) 91471fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 91571fe804bSLee Schermerhorn { 91671fe804bSLee Schermerhorn return NULL; 91771fe804bSLee Schermerhorn } 91871fe804bSLee Schermerhorn #endif 91971fe804bSLee Schermerhorn 9201da177e4SLinus Torvalds /* 921bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 922bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 923bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 924bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 925bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 926bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 927bde05d1cSHugh Dickins * 928bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 929bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 930bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 931bde05d1cSHugh Dickins */ 932bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp) 933bde05d1cSHugh Dickins { 934bde05d1cSHugh Dickins return page_zonenum(page) > gfp_zone(gfp); 935bde05d1cSHugh Dickins } 936bde05d1cSHugh Dickins 937bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 938bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 939bde05d1cSHugh Dickins { 940bde05d1cSHugh Dickins struct page *oldpage, *newpage; 941bde05d1cSHugh Dickins struct address_space *swap_mapping; 942bde05d1cSHugh Dickins pgoff_t swap_index; 943bde05d1cSHugh Dickins int error; 944bde05d1cSHugh Dickins 945bde05d1cSHugh Dickins oldpage = *pagep; 946bde05d1cSHugh Dickins swap_index = page_private(oldpage); 947bde05d1cSHugh Dickins swap_mapping = page_mapping(oldpage); 948bde05d1cSHugh Dickins 949bde05d1cSHugh Dickins /* 950bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 951bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 952bde05d1cSHugh Dickins */ 953bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 954bde05d1cSHugh Dickins newpage = shmem_alloc_page(gfp, info, index); 955bde05d1cSHugh Dickins if (!newpage) 956bde05d1cSHugh Dickins return -ENOMEM; 957bde05d1cSHugh Dickins 958bde05d1cSHugh Dickins page_cache_get(newpage); 959bde05d1cSHugh Dickins copy_highpage(newpage, oldpage); 9600142ef6cSHugh Dickins flush_dcache_page(newpage); 961bde05d1cSHugh Dickins 962bde05d1cSHugh Dickins __set_page_locked(newpage); 963bde05d1cSHugh Dickins SetPageUptodate(newpage); 964bde05d1cSHugh Dickins SetPageSwapBacked(newpage); 965bde05d1cSHugh Dickins set_page_private(newpage, swap_index); 966bde05d1cSHugh Dickins SetPageSwapCache(newpage); 967bde05d1cSHugh Dickins 968bde05d1cSHugh Dickins /* 969bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 970bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 971bde05d1cSHugh Dickins */ 972bde05d1cSHugh Dickins spin_lock_irq(&swap_mapping->tree_lock); 973bde05d1cSHugh Dickins error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage, 974bde05d1cSHugh Dickins newpage); 9750142ef6cSHugh Dickins if (!error) { 976bde05d1cSHugh Dickins __inc_zone_page_state(newpage, NR_FILE_PAGES); 977bde05d1cSHugh Dickins __dec_zone_page_state(oldpage, NR_FILE_PAGES); 9780142ef6cSHugh Dickins } 979bde05d1cSHugh Dickins spin_unlock_irq(&swap_mapping->tree_lock); 980bde05d1cSHugh Dickins 9810142ef6cSHugh Dickins if (unlikely(error)) { 9820142ef6cSHugh Dickins /* 9830142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 9840142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 9850142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 9860142ef6cSHugh Dickins */ 9870142ef6cSHugh Dickins oldpage = newpage; 9880142ef6cSHugh Dickins } else { 989bde05d1cSHugh Dickins mem_cgroup_replace_page_cache(oldpage, newpage); 990bde05d1cSHugh Dickins lru_cache_add_anon(newpage); 9910142ef6cSHugh Dickins *pagep = newpage; 9920142ef6cSHugh Dickins } 993bde05d1cSHugh Dickins 994bde05d1cSHugh Dickins ClearPageSwapCache(oldpage); 995bde05d1cSHugh Dickins set_page_private(oldpage, 0); 996bde05d1cSHugh Dickins 997bde05d1cSHugh Dickins unlock_page(oldpage); 998bde05d1cSHugh Dickins page_cache_release(oldpage); 999bde05d1cSHugh Dickins page_cache_release(oldpage); 10000142ef6cSHugh Dickins return error; 1001bde05d1cSHugh Dickins } 1002bde05d1cSHugh Dickins 1003bde05d1cSHugh Dickins /* 100468da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 10051da177e4SLinus Torvalds * 10061da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 10071da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 10081da177e4SLinus Torvalds * entry since a page cannot live in both the swap and page cache 10091da177e4SLinus Torvalds */ 101041ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 101168da9f05SHugh Dickins struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) 10121da177e4SLinus Torvalds { 10131da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 101454af6042SHugh Dickins struct shmem_inode_info *info; 10151da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 101627ab7006SHugh Dickins struct page *page; 10171da177e4SLinus Torvalds swp_entry_t swap; 10181da177e4SLinus Torvalds int error; 101954af6042SHugh Dickins int once = 0; 10201635f6a7SHugh Dickins int alloced = 0; 10211da177e4SLinus Torvalds 102241ffe5d5SHugh Dickins if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT)) 10231da177e4SLinus Torvalds return -EFBIG; 10241da177e4SLinus Torvalds repeat: 102554af6042SHugh Dickins swap.val = 0; 10260cd6144aSJohannes Weiner page = find_lock_entry(mapping, index); 102754af6042SHugh Dickins if (radix_tree_exceptional_entry(page)) { 102854af6042SHugh Dickins swap = radix_to_swp_entry(page); 102954af6042SHugh Dickins page = NULL; 103054af6042SHugh Dickins } 103154af6042SHugh Dickins 10321635f6a7SHugh Dickins if (sgp != SGP_WRITE && sgp != SGP_FALLOC && 103354af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 103454af6042SHugh Dickins error = -EINVAL; 103554af6042SHugh Dickins goto failed; 103654af6042SHugh Dickins } 103754af6042SHugh Dickins 103866d2f4d2SHugh Dickins if (page && sgp == SGP_WRITE) 103966d2f4d2SHugh Dickins mark_page_accessed(page); 104066d2f4d2SHugh Dickins 10411635f6a7SHugh Dickins /* fallocated page? */ 10421635f6a7SHugh Dickins if (page && !PageUptodate(page)) { 10431635f6a7SHugh Dickins if (sgp != SGP_READ) 10441635f6a7SHugh Dickins goto clear; 10451635f6a7SHugh Dickins unlock_page(page); 10461635f6a7SHugh Dickins page_cache_release(page); 10471635f6a7SHugh Dickins page = NULL; 10481635f6a7SHugh Dickins } 104954af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 105054af6042SHugh Dickins *pagep = page; 105154af6042SHugh Dickins return 0; 105227ab7006SHugh Dickins } 105327ab7006SHugh Dickins 1054b409f9fcSHugh Dickins /* 105554af6042SHugh Dickins * Fast cache lookup did not find it: 105654af6042SHugh Dickins * bring it back from swap or allocate. 1057b409f9fcSHugh Dickins */ 105854af6042SHugh Dickins info = SHMEM_I(inode); 105954af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 106027ab7006SHugh Dickins 10611da177e4SLinus Torvalds if (swap.val) { 10621da177e4SLinus Torvalds /* Look it up and read it in.. */ 106327ab7006SHugh Dickins page = lookup_swap_cache(swap); 106427ab7006SHugh Dickins if (!page) { 1065456f998eSYing Han /* here we actually do the io */ 106668da9f05SHugh Dickins if (fault_type) 106768da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 106841ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 106927ab7006SHugh Dickins if (!page) { 10701da177e4SLinus Torvalds error = -ENOMEM; 107154af6042SHugh Dickins goto failed; 1072285b2c4fSHugh Dickins } 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 107654af6042SHugh Dickins lock_page(page); 10770142ef6cSHugh Dickins if (!PageSwapCache(page) || page_private(page) != swap.val || 1078d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1079bde05d1cSHugh Dickins error = -EEXIST; /* try again */ 1080d1899228SHugh Dickins goto unlock; 1081bde05d1cSHugh Dickins } 108227ab7006SHugh Dickins if (!PageUptodate(page)) { 10831da177e4SLinus Torvalds error = -EIO; 108454af6042SHugh Dickins goto failed; 108554af6042SHugh Dickins } 108654af6042SHugh Dickins wait_on_page_writeback(page); 108754af6042SHugh Dickins 1088bde05d1cSHugh Dickins if (shmem_should_replace_page(page, gfp)) { 1089bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 1090bde05d1cSHugh Dickins if (error) 109154af6042SHugh Dickins goto failed; 10921da177e4SLinus Torvalds } 10931da177e4SLinus Torvalds 1094d715ae08SMichal Hocko error = mem_cgroup_charge_file(page, current->mm, 1095aa3b1895SHugh Dickins gfp & GFP_RECLAIM_MASK); 1096d1899228SHugh Dickins if (!error) { 109754af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 109854af6042SHugh Dickins gfp, swp_to_radix_entry(swap)); 1099215c02bcSHugh Dickins /* 1100215c02bcSHugh Dickins * We already confirmed swap under page lock, and make 1101215c02bcSHugh Dickins * no memory allocation here, so usually no possibility 1102215c02bcSHugh Dickins * of error; but free_swap_and_cache() only trylocks a 1103215c02bcSHugh Dickins * page, so it is just possible that the entry has been 1104215c02bcSHugh Dickins * truncated or holepunched since swap was confirmed. 1105215c02bcSHugh Dickins * shmem_undo_range() will have done some of the 1106215c02bcSHugh Dickins * unaccounting, now delete_from_swap_cache() will do 1107215c02bcSHugh Dickins * the rest (including mem_cgroup_uncharge_swapcache). 1108215c02bcSHugh Dickins * Reset swap.val? No, leave it so "failed" goes back to 1109215c02bcSHugh Dickins * "repeat": reading a hole and writing should succeed. 1110215c02bcSHugh Dickins */ 1111215c02bcSHugh Dickins if (error) 1112215c02bcSHugh Dickins delete_from_swap_cache(page); 1113d1899228SHugh Dickins } 111454af6042SHugh Dickins if (error) 111554af6042SHugh Dickins goto failed; 111654af6042SHugh Dickins 111754af6042SHugh Dickins spin_lock(&info->lock); 111854af6042SHugh Dickins info->swapped--; 111954af6042SHugh Dickins shmem_recalc_inode(inode); 11201da177e4SLinus Torvalds spin_unlock(&info->lock); 112127ab7006SHugh Dickins 112266d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 112366d2f4d2SHugh Dickins mark_page_accessed(page); 112466d2f4d2SHugh Dickins 112527ab7006SHugh Dickins delete_from_swap_cache(page); 112627ab7006SHugh Dickins set_page_dirty(page); 112727ab7006SHugh Dickins swap_free(swap); 112827ab7006SHugh Dickins 112954af6042SHugh Dickins } else { 113054af6042SHugh Dickins if (shmem_acct_block(info->flags)) { 113154af6042SHugh Dickins error = -ENOSPC; 113254af6042SHugh Dickins goto failed; 11331da177e4SLinus Torvalds } 11340edd73b3SHugh Dickins if (sbinfo->max_blocks) { 1135fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 113654af6042SHugh Dickins sbinfo->max_blocks) >= 0) { 113754af6042SHugh Dickins error = -ENOSPC; 113854af6042SHugh Dickins goto unacct; 113954af6042SHugh Dickins } 11407e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 114159a16eadSHugh Dickins } 11421da177e4SLinus Torvalds 114354af6042SHugh Dickins page = shmem_alloc_page(gfp, info, index); 114454af6042SHugh Dickins if (!page) { 114554af6042SHugh Dickins error = -ENOMEM; 114654af6042SHugh Dickins goto decused; 114754af6042SHugh Dickins } 114854af6042SHugh Dickins 114907a42788SMel Gorman __SetPageSwapBacked(page); 115054af6042SHugh Dickins __set_page_locked(page); 115166d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 115266d2f4d2SHugh Dickins init_page_accessed(page); 115366d2f4d2SHugh Dickins 1154d715ae08SMichal Hocko error = mem_cgroup_charge_file(page, current->mm, 1155aa3b1895SHugh Dickins gfp & GFP_RECLAIM_MASK); 115654af6042SHugh Dickins if (error) 115754af6042SHugh Dickins goto decused; 11585e4c0d97SJan Kara error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK); 1159b065b432SHugh Dickins if (!error) { 1160b065b432SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1161b065b432SHugh Dickins gfp, NULL); 1162b065b432SHugh Dickins radix_tree_preload_end(); 1163b065b432SHugh Dickins } 1164b065b432SHugh Dickins if (error) { 1165b065b432SHugh Dickins mem_cgroup_uncharge_cache_page(page); 1166b065b432SHugh Dickins goto decused; 1167b065b432SHugh Dickins } 116854af6042SHugh Dickins lru_cache_add_anon(page); 116954af6042SHugh Dickins 117054af6042SHugh Dickins spin_lock(&info->lock); 11711da177e4SLinus Torvalds info->alloced++; 117254af6042SHugh Dickins inode->i_blocks += BLOCKS_PER_PAGE; 117354af6042SHugh Dickins shmem_recalc_inode(inode); 117459a16eadSHugh Dickins spin_unlock(&info->lock); 11751635f6a7SHugh Dickins alloced = true; 117654af6042SHugh Dickins 1177ec9516fbSHugh Dickins /* 11781635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 11791635f6a7SHugh Dickins */ 11801635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 11811635f6a7SHugh Dickins sgp = SGP_WRITE; 11821635f6a7SHugh Dickins clear: 11831635f6a7SHugh Dickins /* 11841635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 11851635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 11861635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1187ec9516fbSHugh Dickins */ 1188ec9516fbSHugh Dickins if (sgp != SGP_WRITE) { 118927ab7006SHugh Dickins clear_highpage(page); 119027ab7006SHugh Dickins flush_dcache_page(page); 119127ab7006SHugh Dickins SetPageUptodate(page); 1192ec9516fbSHugh Dickins } 1193a0ee5ec5SHugh Dickins if (sgp == SGP_DIRTY) 119427ab7006SHugh Dickins set_page_dirty(page); 11951da177e4SLinus Torvalds } 1196bde05d1cSHugh Dickins 119754af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 11981635f6a7SHugh Dickins if (sgp != SGP_WRITE && sgp != SGP_FALLOC && 119954af6042SHugh Dickins ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 120054af6042SHugh Dickins error = -EINVAL; 12011635f6a7SHugh Dickins if (alloced) 120254af6042SHugh Dickins goto trunc; 12031635f6a7SHugh Dickins else 12041635f6a7SHugh Dickins goto failed; 1205ff36b801SShaohua Li } 120654af6042SHugh Dickins *pagep = page; 120754af6042SHugh Dickins return 0; 1208d00806b1SNick Piggin 1209d0217ac0SNick Piggin /* 121054af6042SHugh Dickins * Error recovery. 12111da177e4SLinus Torvalds */ 121254af6042SHugh Dickins trunc: 12131635f6a7SHugh Dickins info = SHMEM_I(inode); 121454af6042SHugh Dickins ClearPageDirty(page); 121554af6042SHugh Dickins delete_from_page_cache(page); 121654af6042SHugh Dickins spin_lock(&info->lock); 121754af6042SHugh Dickins info->alloced--; 121854af6042SHugh Dickins inode->i_blocks -= BLOCKS_PER_PAGE; 12191da177e4SLinus Torvalds spin_unlock(&info->lock); 122054af6042SHugh Dickins decused: 12211635f6a7SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 122254af6042SHugh Dickins if (sbinfo->max_blocks) 122354af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -1); 122454af6042SHugh Dickins unacct: 122554af6042SHugh Dickins shmem_unacct_blocks(info->flags, 1); 122654af6042SHugh Dickins failed: 1227d1899228SHugh Dickins if (swap.val && error != -EINVAL && 1228d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) 122954af6042SHugh Dickins error = -EEXIST; 1230d1899228SHugh Dickins unlock: 123127ab7006SHugh Dickins if (page) { 123254af6042SHugh Dickins unlock_page(page); 12331da177e4SLinus Torvalds page_cache_release(page); 123454af6042SHugh Dickins } 123554af6042SHugh Dickins if (error == -ENOSPC && !once++) { 123654af6042SHugh Dickins info = SHMEM_I(inode); 123754af6042SHugh Dickins spin_lock(&info->lock); 123854af6042SHugh Dickins shmem_recalc_inode(inode); 123954af6042SHugh Dickins spin_unlock(&info->lock); 12401da177e4SLinus Torvalds goto repeat; 1241d8dc74f2SAdrian Bunk } 1242d1899228SHugh Dickins if (error == -EEXIST) /* from above or from radix_tree_insert */ 124354af6042SHugh Dickins goto repeat; 124454af6042SHugh Dickins return error; 12451da177e4SLinus Torvalds } 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 12481da177e4SLinus Torvalds { 1249496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 12501da177e4SLinus Torvalds int error; 125168da9f05SHugh Dickins int ret = VM_FAULT_LOCKED; 12521da177e4SLinus Torvalds 1253f00cdc6dSHugh Dickins /* 1254f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 1255f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 1256f00cdc6dSHugh Dickins * locks writers out with its hold on i_mutex. So refrain from 12578e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 12588e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 12598e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 12608e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 12618e205f77SHugh Dickins * 12628e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 12638e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 12648e205f77SHugh Dickins * we just need to make racing faults a rare case. 12658e205f77SHugh Dickins * 12668e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 12678e205f77SHugh Dickins * standard mutex or completion: but we cannot take i_mutex in fault, 12688e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 1269f00cdc6dSHugh Dickins */ 1270f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 1271f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 1272f00cdc6dSHugh Dickins 1273f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 1274f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 12758e205f77SHugh Dickins if (shmem_falloc && 12768e205f77SHugh Dickins shmem_falloc->waitq && 12778e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 12788e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 12798e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 12808e205f77SHugh Dickins DEFINE_WAIT(shmem_fault_wait); 12818e205f77SHugh Dickins 12828e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 1283f00cdc6dSHugh Dickins if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) && 1284f00cdc6dSHugh Dickins !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 12858e205f77SHugh Dickins /* It's polite to up mmap_sem if we can */ 1286f00cdc6dSHugh Dickins up_read(&vma->vm_mm->mmap_sem); 12878e205f77SHugh Dickins ret = VM_FAULT_RETRY; 1288f00cdc6dSHugh Dickins } 12898e205f77SHugh Dickins 12908e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 12918e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 12928e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 12938e205f77SHugh Dickins spin_unlock(&inode->i_lock); 12948e205f77SHugh Dickins schedule(); 12958e205f77SHugh Dickins 12968e205f77SHugh Dickins /* 12978e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 12988e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 12998e205f77SHugh Dickins * is usually invalid by the time we reach here, but 13008e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 13018e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 13028e205f77SHugh Dickins */ 13038e205f77SHugh Dickins spin_lock(&inode->i_lock); 13048e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 13058e205f77SHugh Dickins spin_unlock(&inode->i_lock); 13068e205f77SHugh Dickins return ret; 1307f00cdc6dSHugh Dickins } 13088e205f77SHugh Dickins spin_unlock(&inode->i_lock); 1309f00cdc6dSHugh Dickins } 1310f00cdc6dSHugh Dickins 13111da177e4SLinus Torvalds error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); 13121da177e4SLinus Torvalds if (error) 13131da177e4SLinus Torvalds return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 131468da9f05SHugh Dickins 1315456f998eSYing Han if (ret & VM_FAULT_MAJOR) { 1316456f998eSYing Han count_vm_event(PGMAJFAULT); 1317456f998eSYing Han mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 1318456f998eSYing Han } 131968da9f05SHugh Dickins return ret; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds #ifdef CONFIG_NUMA 132341ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 13241da177e4SLinus Torvalds { 1325496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 132641ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds 1329d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 1330d8dc74f2SAdrian Bunk unsigned long addr) 13311da177e4SLinus Torvalds { 1332496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 133341ffe5d5SHugh Dickins pgoff_t index; 13341da177e4SLinus Torvalds 133541ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 133641ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 13371da177e4SLinus Torvalds } 13381da177e4SLinus Torvalds #endif 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 13411da177e4SLinus Torvalds { 1342496ad9aaSAl Viro struct inode *inode = file_inode(file); 13431da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 13441da177e4SLinus Torvalds int retval = -ENOMEM; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds spin_lock(&info->lock); 13471da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 13481da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 13491da177e4SLinus Torvalds goto out_nomem; 13501da177e4SLinus Torvalds info->flags |= VM_LOCKED; 135189e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 13541da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 13551da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 135689e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds retval = 0; 135989e004eaSLee Schermerhorn 13601da177e4SLinus Torvalds out_nomem: 13611da177e4SLinus Torvalds spin_unlock(&info->lock); 13621da177e4SLinus Torvalds return retval; 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 13659b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 13661da177e4SLinus Torvalds { 13671da177e4SLinus Torvalds file_accessed(file); 13681da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 13691da177e4SLinus Torvalds return 0; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 1372454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 137309208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 13741da177e4SLinus Torvalds { 13751da177e4SLinus Torvalds struct inode *inode; 13761da177e4SLinus Torvalds struct shmem_inode_info *info; 13771da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 13781da177e4SLinus Torvalds 13795b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 13801da177e4SLinus Torvalds return NULL; 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds inode = new_inode(sb); 13831da177e4SLinus Torvalds if (inode) { 138485fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 1385454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 13861da177e4SLinus Torvalds inode->i_blocks = 0; 13871da177e4SLinus Torvalds inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; 13881da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 138991828a40SDavid M. Grimes inode->i_generation = get_seconds(); 13901da177e4SLinus Torvalds info = SHMEM_I(inode); 13911da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 13921da177e4SLinus Torvalds spin_lock_init(&info->lock); 13930b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 13941da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 139538f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 139672c04902SAl Viro cache_no_acl(inode); 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds switch (mode & S_IFMT) { 13991da177e4SLinus Torvalds default: 140039f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 14011da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 14021da177e4SLinus Torvalds break; 14031da177e4SLinus Torvalds case S_IFREG: 140414fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 14051da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 14061da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 140771fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 140871fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 14091da177e4SLinus Torvalds break; 14101da177e4SLinus Torvalds case S_IFDIR: 1411d8c76e6fSDave Hansen inc_nlink(inode); 14121da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 14131da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 14141da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 14151da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 14161da177e4SLinus Torvalds break; 14171da177e4SLinus Torvalds case S_IFLNK: 14181da177e4SLinus Torvalds /* 14191da177e4SLinus Torvalds * Must not load anything in the rbtree, 14201da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 14211da177e4SLinus Torvalds */ 142271fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 14231da177e4SLinus Torvalds break; 14241da177e4SLinus Torvalds } 14255b04c689SPavel Emelyanov } else 14265b04c689SPavel Emelyanov shmem_free_inode(sb); 14271da177e4SLinus Torvalds return inode; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 14300cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping) 14310cd6144aSJohannes Weiner { 14320cd6144aSJohannes Weiner return mapping->backing_dev_info == &shmem_backing_dev_info; 14330cd6144aSJohannes Weiner } 14340cd6144aSJohannes Weiner 14351da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 143692e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 143769f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 14381da177e4SLinus Torvalds 14396d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR 14406d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 14416d9d88d0SJarkko Sakkinen #else 14426d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL 14436d9d88d0SJarkko Sakkinen #endif 14446d9d88d0SJarkko Sakkinen 14451da177e4SLinus Torvalds static int 1446800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 1447800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 1448800d15a5SNick Piggin struct page **pagep, void **fsdata) 14491da177e4SLinus Torvalds { 1450800d15a5SNick Piggin struct inode *inode = mapping->host; 1451800d15a5SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 145266d2f4d2SHugh Dickins return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); 1453800d15a5SNick Piggin } 1454800d15a5SNick Piggin 1455800d15a5SNick Piggin static int 1456800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 1457800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 1458800d15a5SNick Piggin struct page *page, void *fsdata) 1459800d15a5SNick Piggin { 1460800d15a5SNick Piggin struct inode *inode = mapping->host; 1461800d15a5SNick Piggin 1462800d15a5SNick Piggin if (pos + copied > inode->i_size) 1463800d15a5SNick Piggin i_size_write(inode, pos + copied); 1464800d15a5SNick Piggin 1465ec9516fbSHugh Dickins if (!PageUptodate(page)) { 1466ec9516fbSHugh Dickins if (copied < PAGE_CACHE_SIZE) { 1467ec9516fbSHugh Dickins unsigned from = pos & (PAGE_CACHE_SIZE - 1); 1468ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 1469ec9516fbSHugh Dickins from + copied, PAGE_CACHE_SIZE); 1470ec9516fbSHugh Dickins } 1471ec9516fbSHugh Dickins SetPageUptodate(page); 1472ec9516fbSHugh Dickins } 1473d3602444SHugh Dickins set_page_dirty(page); 14746746aff7SWu Fengguang unlock_page(page); 1475d3602444SHugh Dickins page_cache_release(page); 1476d3602444SHugh Dickins 1477800d15a5SNick Piggin return copied; 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 14802ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 14811da177e4SLinus Torvalds { 14826e58e79dSAl Viro struct file *file = iocb->ki_filp; 14836e58e79dSAl Viro struct inode *inode = file_inode(file); 14841da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 148541ffe5d5SHugh Dickins pgoff_t index; 148641ffe5d5SHugh Dickins unsigned long offset; 1487a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 1488f7c1d074SGeert Uytterhoeven int error = 0; 1489cb66a7a1SAl Viro ssize_t retval = 0; 14906e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 1491a0ee5ec5SHugh Dickins 1492a0ee5ec5SHugh Dickins /* 1493a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 1494a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 1495a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 1496a0ee5ec5SHugh Dickins */ 1497a0ee5ec5SHugh Dickins if (segment_eq(get_fs(), KERNEL_DS)) 1498a0ee5ec5SHugh Dickins sgp = SGP_DIRTY; 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds index = *ppos >> PAGE_CACHE_SHIFT; 15011da177e4SLinus Torvalds offset = *ppos & ~PAGE_CACHE_MASK; 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds for (;;) { 15041da177e4SLinus Torvalds struct page *page = NULL; 150541ffe5d5SHugh Dickins pgoff_t end_index; 150641ffe5d5SHugh Dickins unsigned long nr, ret; 15071da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 15101da177e4SLinus Torvalds if (index > end_index) 15111da177e4SLinus Torvalds break; 15121da177e4SLinus Torvalds if (index == end_index) { 15131da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 15141da177e4SLinus Torvalds if (nr <= offset) 15151da177e4SLinus Torvalds break; 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds 15186e58e79dSAl Viro error = shmem_getpage(inode, index, &page, sgp, NULL); 15196e58e79dSAl Viro if (error) { 15206e58e79dSAl Viro if (error == -EINVAL) 15216e58e79dSAl Viro error = 0; 15221da177e4SLinus Torvalds break; 15231da177e4SLinus Torvalds } 1524d3602444SHugh Dickins if (page) 1525d3602444SHugh Dickins unlock_page(page); 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds /* 15281da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 15291b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 15301da177e4SLinus Torvalds */ 15311da177e4SLinus Torvalds nr = PAGE_CACHE_SIZE; 15321da177e4SLinus Torvalds i_size = i_size_read(inode); 15331da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 15341da177e4SLinus Torvalds if (index == end_index) { 15351da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 15361da177e4SLinus Torvalds if (nr <= offset) { 15371da177e4SLinus Torvalds if (page) 15381da177e4SLinus Torvalds page_cache_release(page); 15391da177e4SLinus Torvalds break; 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds nr -= offset; 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds if (page) { 15451da177e4SLinus Torvalds /* 15461da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 15471da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 15481da177e4SLinus Torvalds * before reading the page on the kernel side. 15491da177e4SLinus Torvalds */ 15501da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 15511da177e4SLinus Torvalds flush_dcache_page(page); 15521da177e4SLinus Torvalds /* 15531da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 15541da177e4SLinus Torvalds */ 15551da177e4SLinus Torvalds if (!offset) 15561da177e4SLinus Torvalds mark_page_accessed(page); 1557b5810039SNick Piggin } else { 15581da177e4SLinus Torvalds page = ZERO_PAGE(0); 1559b5810039SNick Piggin page_cache_get(page); 1560b5810039SNick Piggin } 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds /* 15631da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 15641da177e4SLinus Torvalds * now we can copy it to user space... 15651da177e4SLinus Torvalds */ 15662ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 15676e58e79dSAl Viro retval += ret; 15681da177e4SLinus Torvalds offset += ret; 15691da177e4SLinus Torvalds index += offset >> PAGE_CACHE_SHIFT; 15701da177e4SLinus Torvalds offset &= ~PAGE_CACHE_MASK; 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds page_cache_release(page); 15732ba5bbedSAl Viro if (!iov_iter_count(to)) 15741da177e4SLinus Torvalds break; 15756e58e79dSAl Viro if (ret < nr) { 15766e58e79dSAl Viro error = -EFAULT; 15776e58e79dSAl Viro break; 15786e58e79dSAl Viro } 15791da177e4SLinus Torvalds cond_resched(); 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; 15836e58e79dSAl Viro file_accessed(file); 15846e58e79dSAl Viro return retval ? retval : error; 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds 1587708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 1588708e3508SHugh Dickins struct pipe_inode_info *pipe, size_t len, 1589708e3508SHugh Dickins unsigned int flags) 1590708e3508SHugh Dickins { 1591708e3508SHugh Dickins struct address_space *mapping = in->f_mapping; 159271f0e07aSHugh Dickins struct inode *inode = mapping->host; 1593708e3508SHugh Dickins unsigned int loff, nr_pages, req_pages; 1594708e3508SHugh Dickins struct page *pages[PIPE_DEF_BUFFERS]; 1595708e3508SHugh Dickins struct partial_page partial[PIPE_DEF_BUFFERS]; 1596708e3508SHugh Dickins struct page *page; 1597708e3508SHugh Dickins pgoff_t index, end_index; 1598708e3508SHugh Dickins loff_t isize, left; 1599708e3508SHugh Dickins int error, page_nr; 1600708e3508SHugh Dickins struct splice_pipe_desc spd = { 1601708e3508SHugh Dickins .pages = pages, 1602708e3508SHugh Dickins .partial = partial, 1603047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 1604708e3508SHugh Dickins .flags = flags, 1605708e3508SHugh Dickins .ops = &page_cache_pipe_buf_ops, 1606708e3508SHugh Dickins .spd_release = spd_release_page, 1607708e3508SHugh Dickins }; 1608708e3508SHugh Dickins 160971f0e07aSHugh Dickins isize = i_size_read(inode); 1610708e3508SHugh Dickins if (unlikely(*ppos >= isize)) 1611708e3508SHugh Dickins return 0; 1612708e3508SHugh Dickins 1613708e3508SHugh Dickins left = isize - *ppos; 1614708e3508SHugh Dickins if (unlikely(left < len)) 1615708e3508SHugh Dickins len = left; 1616708e3508SHugh Dickins 1617708e3508SHugh Dickins if (splice_grow_spd(pipe, &spd)) 1618708e3508SHugh Dickins return -ENOMEM; 1619708e3508SHugh Dickins 1620708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1621708e3508SHugh Dickins loff = *ppos & ~PAGE_CACHE_MASK; 1622708e3508SHugh Dickins req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1623a786c06dSAl Viro nr_pages = min(req_pages, spd.nr_pages_max); 1624708e3508SHugh Dickins 1625708e3508SHugh Dickins spd.nr_pages = find_get_pages_contig(mapping, index, 1626708e3508SHugh Dickins nr_pages, spd.pages); 1627708e3508SHugh Dickins index += spd.nr_pages; 1628708e3508SHugh Dickins error = 0; 162971f0e07aSHugh Dickins 1630708e3508SHugh Dickins while (spd.nr_pages < nr_pages) { 163171f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); 163271f0e07aSHugh Dickins if (error) 1633708e3508SHugh Dickins break; 1634708e3508SHugh Dickins unlock_page(page); 1635708e3508SHugh Dickins spd.pages[spd.nr_pages++] = page; 1636708e3508SHugh Dickins index++; 1637708e3508SHugh Dickins } 1638708e3508SHugh Dickins 1639708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1640708e3508SHugh Dickins nr_pages = spd.nr_pages; 1641708e3508SHugh Dickins spd.nr_pages = 0; 164271f0e07aSHugh Dickins 1643708e3508SHugh Dickins for (page_nr = 0; page_nr < nr_pages; page_nr++) { 1644708e3508SHugh Dickins unsigned int this_len; 1645708e3508SHugh Dickins 1646708e3508SHugh Dickins if (!len) 1647708e3508SHugh Dickins break; 1648708e3508SHugh Dickins 1649708e3508SHugh Dickins this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 1650708e3508SHugh Dickins page = spd.pages[page_nr]; 1651708e3508SHugh Dickins 165271f0e07aSHugh Dickins if (!PageUptodate(page) || page->mapping != mapping) { 165371f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, 165471f0e07aSHugh Dickins SGP_CACHE, NULL); 165571f0e07aSHugh Dickins if (error) 1656708e3508SHugh Dickins break; 165771f0e07aSHugh Dickins unlock_page(page); 1658708e3508SHugh Dickins page_cache_release(spd.pages[page_nr]); 1659708e3508SHugh Dickins spd.pages[page_nr] = page; 1660708e3508SHugh Dickins } 1661708e3508SHugh Dickins 166271f0e07aSHugh Dickins isize = i_size_read(inode); 1663708e3508SHugh Dickins end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 1664708e3508SHugh Dickins if (unlikely(!isize || index > end_index)) 1665708e3508SHugh Dickins break; 1666708e3508SHugh Dickins 1667708e3508SHugh Dickins if (end_index == index) { 1668708e3508SHugh Dickins unsigned int plen; 1669708e3508SHugh Dickins 1670708e3508SHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 1671708e3508SHugh Dickins if (plen <= loff) 1672708e3508SHugh Dickins break; 1673708e3508SHugh Dickins 1674708e3508SHugh Dickins this_len = min(this_len, plen - loff); 1675708e3508SHugh Dickins len = this_len; 1676708e3508SHugh Dickins } 1677708e3508SHugh Dickins 1678708e3508SHugh Dickins spd.partial[page_nr].offset = loff; 1679708e3508SHugh Dickins spd.partial[page_nr].len = this_len; 1680708e3508SHugh Dickins len -= this_len; 1681708e3508SHugh Dickins loff = 0; 1682708e3508SHugh Dickins spd.nr_pages++; 1683708e3508SHugh Dickins index++; 1684708e3508SHugh Dickins } 1685708e3508SHugh Dickins 1686708e3508SHugh Dickins while (page_nr < nr_pages) 1687708e3508SHugh Dickins page_cache_release(spd.pages[page_nr++]); 1688708e3508SHugh Dickins 1689708e3508SHugh Dickins if (spd.nr_pages) 1690708e3508SHugh Dickins error = splice_to_pipe(pipe, &spd); 1691708e3508SHugh Dickins 1692047fe360SEric Dumazet splice_shrink_spd(&spd); 1693708e3508SHugh Dickins 1694708e3508SHugh Dickins if (error > 0) { 1695708e3508SHugh Dickins *ppos += error; 1696708e3508SHugh Dickins file_accessed(in); 1697708e3508SHugh Dickins } 1698708e3508SHugh Dickins return error; 1699708e3508SHugh Dickins } 1700708e3508SHugh Dickins 1701220f2ac9SHugh Dickins /* 1702220f2ac9SHugh Dickins * llseek SEEK_DATA or SEEK_HOLE through the radix_tree. 1703220f2ac9SHugh Dickins */ 1704220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping, 1705965c8e59SAndrew Morton pgoff_t index, pgoff_t end, int whence) 1706220f2ac9SHugh Dickins { 1707220f2ac9SHugh Dickins struct page *page; 1708220f2ac9SHugh Dickins struct pagevec pvec; 1709220f2ac9SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 1710220f2ac9SHugh Dickins bool done = false; 1711220f2ac9SHugh Dickins int i; 1712220f2ac9SHugh Dickins 1713220f2ac9SHugh Dickins pagevec_init(&pvec, 0); 1714220f2ac9SHugh Dickins pvec.nr = 1; /* start small: we may be there already */ 1715220f2ac9SHugh Dickins while (!done) { 17160cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 1717220f2ac9SHugh Dickins pvec.nr, pvec.pages, indices); 1718220f2ac9SHugh Dickins if (!pvec.nr) { 1719965c8e59SAndrew Morton if (whence == SEEK_DATA) 1720220f2ac9SHugh Dickins index = end; 1721220f2ac9SHugh Dickins break; 1722220f2ac9SHugh Dickins } 1723220f2ac9SHugh Dickins for (i = 0; i < pvec.nr; i++, index++) { 1724220f2ac9SHugh Dickins if (index < indices[i]) { 1725965c8e59SAndrew Morton if (whence == SEEK_HOLE) { 1726220f2ac9SHugh Dickins done = true; 1727220f2ac9SHugh Dickins break; 1728220f2ac9SHugh Dickins } 1729220f2ac9SHugh Dickins index = indices[i]; 1730220f2ac9SHugh Dickins } 1731220f2ac9SHugh Dickins page = pvec.pages[i]; 1732220f2ac9SHugh Dickins if (page && !radix_tree_exceptional_entry(page)) { 1733220f2ac9SHugh Dickins if (!PageUptodate(page)) 1734220f2ac9SHugh Dickins page = NULL; 1735220f2ac9SHugh Dickins } 1736220f2ac9SHugh Dickins if (index >= end || 1737965c8e59SAndrew Morton (page && whence == SEEK_DATA) || 1738965c8e59SAndrew Morton (!page && whence == SEEK_HOLE)) { 1739220f2ac9SHugh Dickins done = true; 1740220f2ac9SHugh Dickins break; 1741220f2ac9SHugh Dickins } 1742220f2ac9SHugh Dickins } 17430cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 1744220f2ac9SHugh Dickins pagevec_release(&pvec); 1745220f2ac9SHugh Dickins pvec.nr = PAGEVEC_SIZE; 1746220f2ac9SHugh Dickins cond_resched(); 1747220f2ac9SHugh Dickins } 1748220f2ac9SHugh Dickins return index; 1749220f2ac9SHugh Dickins } 1750220f2ac9SHugh Dickins 1751965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 1752220f2ac9SHugh Dickins { 1753220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 1754220f2ac9SHugh Dickins struct inode *inode = mapping->host; 1755220f2ac9SHugh Dickins pgoff_t start, end; 1756220f2ac9SHugh Dickins loff_t new_offset; 1757220f2ac9SHugh Dickins 1758965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 1759965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 1760220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 1761220f2ac9SHugh Dickins mutex_lock(&inode->i_mutex); 1762220f2ac9SHugh Dickins /* We're holding i_mutex so we can access i_size directly */ 1763220f2ac9SHugh Dickins 1764220f2ac9SHugh Dickins if (offset < 0) 1765220f2ac9SHugh Dickins offset = -EINVAL; 1766220f2ac9SHugh Dickins else if (offset >= inode->i_size) 1767220f2ac9SHugh Dickins offset = -ENXIO; 1768220f2ac9SHugh Dickins else { 1769220f2ac9SHugh Dickins start = offset >> PAGE_CACHE_SHIFT; 1770220f2ac9SHugh Dickins end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1771965c8e59SAndrew Morton new_offset = shmem_seek_hole_data(mapping, start, end, whence); 1772220f2ac9SHugh Dickins new_offset <<= PAGE_CACHE_SHIFT; 1773220f2ac9SHugh Dickins if (new_offset > offset) { 1774220f2ac9SHugh Dickins if (new_offset < inode->i_size) 1775220f2ac9SHugh Dickins offset = new_offset; 1776965c8e59SAndrew Morton else if (whence == SEEK_DATA) 1777220f2ac9SHugh Dickins offset = -ENXIO; 1778220f2ac9SHugh Dickins else 1779220f2ac9SHugh Dickins offset = inode->i_size; 1780220f2ac9SHugh Dickins } 1781220f2ac9SHugh Dickins } 1782220f2ac9SHugh Dickins 1783387aae6fSHugh Dickins if (offset >= 0) 178446a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 1785220f2ac9SHugh Dickins mutex_unlock(&inode->i_mutex); 1786220f2ac9SHugh Dickins return offset; 1787220f2ac9SHugh Dickins } 1788220f2ac9SHugh Dickins 178983e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 179083e4fa9cSHugh Dickins loff_t len) 179183e4fa9cSHugh Dickins { 1792496ad9aaSAl Viro struct inode *inode = file_inode(file); 1793e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 17941aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 1795e2d12e22SHugh Dickins pgoff_t start, index, end; 1796e2d12e22SHugh Dickins int error; 179783e4fa9cSHugh Dickins 179813ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 179913ace4d0SHugh Dickins return -EOPNOTSUPP; 180013ace4d0SHugh Dickins 180183e4fa9cSHugh Dickins mutex_lock(&inode->i_mutex); 180283e4fa9cSHugh Dickins 180383e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 180483e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 180583e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 180683e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 18078e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 180883e4fa9cSHugh Dickins 18098e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 1810f00cdc6dSHugh Dickins shmem_falloc.start = unmap_start >> PAGE_SHIFT; 1811f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 1812f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 1813f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 1814f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 1815f00cdc6dSHugh Dickins 181683e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 181783e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 181883e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 181983e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 182083e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 18218e205f77SHugh Dickins 18228e205f77SHugh Dickins spin_lock(&inode->i_lock); 18238e205f77SHugh Dickins inode->i_private = NULL; 18248e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 18258e205f77SHugh Dickins spin_unlock(&inode->i_lock); 182683e4fa9cSHugh Dickins error = 0; 18278e205f77SHugh Dickins goto out; 182883e4fa9cSHugh Dickins } 182983e4fa9cSHugh Dickins 1830e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 1831e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 1832e2d12e22SHugh Dickins if (error) 1833e2d12e22SHugh Dickins goto out; 1834e2d12e22SHugh Dickins 1835e2d12e22SHugh Dickins start = offset >> PAGE_CACHE_SHIFT; 1836e2d12e22SHugh Dickins end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1837e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 1838e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 1839e2d12e22SHugh Dickins error = -ENOSPC; 1840e2d12e22SHugh Dickins goto out; 1841e2d12e22SHugh Dickins } 1842e2d12e22SHugh Dickins 18438e205f77SHugh Dickins shmem_falloc.waitq = NULL; 18441aac1400SHugh Dickins shmem_falloc.start = start; 18451aac1400SHugh Dickins shmem_falloc.next = start; 18461aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 18471aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 18481aac1400SHugh Dickins spin_lock(&inode->i_lock); 18491aac1400SHugh Dickins inode->i_private = &shmem_falloc; 18501aac1400SHugh Dickins spin_unlock(&inode->i_lock); 18511aac1400SHugh Dickins 1852e2d12e22SHugh Dickins for (index = start; index < end; index++) { 1853e2d12e22SHugh Dickins struct page *page; 1854e2d12e22SHugh Dickins 1855e2d12e22SHugh Dickins /* 1856e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 1857e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 1858e2d12e22SHugh Dickins */ 1859e2d12e22SHugh Dickins if (signal_pending(current)) 1860e2d12e22SHugh Dickins error = -EINTR; 18611aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 18621aac1400SHugh Dickins error = -ENOMEM; 1863e2d12e22SHugh Dickins else 18641635f6a7SHugh Dickins error = shmem_getpage(inode, index, &page, SGP_FALLOC, 1865e2d12e22SHugh Dickins NULL); 1866e2d12e22SHugh Dickins if (error) { 18671635f6a7SHugh Dickins /* Remove the !PageUptodate pages we added */ 18681635f6a7SHugh Dickins shmem_undo_range(inode, 18691635f6a7SHugh Dickins (loff_t)start << PAGE_CACHE_SHIFT, 18701635f6a7SHugh Dickins (loff_t)index << PAGE_CACHE_SHIFT, true); 18711aac1400SHugh Dickins goto undone; 1872e2d12e22SHugh Dickins } 1873e2d12e22SHugh Dickins 1874e2d12e22SHugh Dickins /* 18751aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 18761aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 18771aac1400SHugh Dickins */ 18781aac1400SHugh Dickins shmem_falloc.next++; 18791aac1400SHugh Dickins if (!PageUptodate(page)) 18801aac1400SHugh Dickins shmem_falloc.nr_falloced++; 18811aac1400SHugh Dickins 18821aac1400SHugh Dickins /* 18831635f6a7SHugh Dickins * If !PageUptodate, leave it that way so that freeable pages 18841635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 18851635f6a7SHugh Dickins * But set_page_dirty so that memory pressure will swap rather 1886e2d12e22SHugh Dickins * than free the pages we are allocating (and SGP_CACHE pages 1887e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 1888e2d12e22SHugh Dickins */ 1889e2d12e22SHugh Dickins set_page_dirty(page); 1890e2d12e22SHugh Dickins unlock_page(page); 1891e2d12e22SHugh Dickins page_cache_release(page); 1892e2d12e22SHugh Dickins cond_resched(); 1893e2d12e22SHugh Dickins } 1894e2d12e22SHugh Dickins 1895e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 1896e2d12e22SHugh Dickins i_size_write(inode, offset + len); 1897e2d12e22SHugh Dickins inode->i_ctime = CURRENT_TIME; 18981aac1400SHugh Dickins undone: 18991aac1400SHugh Dickins spin_lock(&inode->i_lock); 19001aac1400SHugh Dickins inode->i_private = NULL; 19011aac1400SHugh Dickins spin_unlock(&inode->i_lock); 1902e2d12e22SHugh Dickins out: 190383e4fa9cSHugh Dickins mutex_unlock(&inode->i_mutex); 190483e4fa9cSHugh Dickins return error; 190583e4fa9cSHugh Dickins } 190683e4fa9cSHugh Dickins 1907726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 19081da177e4SLinus Torvalds { 1909726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 19121da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 19131da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 19140edd73b3SHugh Dickins if (sbinfo->max_blocks) { 19151da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 191641ffe5d5SHugh Dickins buf->f_bavail = 191741ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 191841ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 19190edd73b3SHugh Dickins } 19200edd73b3SHugh Dickins if (sbinfo->max_inodes) { 19211da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 19221da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 19251da177e4SLinus Torvalds return 0; 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds /* 19291da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 19301da177e4SLinus Torvalds */ 19311da177e4SLinus Torvalds static int 19321a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 19331da177e4SLinus Torvalds { 19340b0a0806SHugh Dickins struct inode *inode; 19351da177e4SLinus Torvalds int error = -ENOSPC; 19361da177e4SLinus Torvalds 1937454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 19381da177e4SLinus Torvalds if (inode) { 1939feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 1940feda821eSChristoph Hellwig if (error) 1941feda821eSChristoph Hellwig goto out_iput; 19422a7dba39SEric Paris error = security_inode_init_security(inode, dir, 19439d8f13baSMimi Zohar &dentry->d_name, 19446d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 1945feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 1946feda821eSChristoph Hellwig goto out_iput; 194737ec43cdSMimi Zohar 1948718deb6bSAl Viro error = 0; 19491da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 19501da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 19511da177e4SLinus Torvalds d_instantiate(dentry, inode); 19521da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds return error; 1955feda821eSChristoph Hellwig out_iput: 1956feda821eSChristoph Hellwig iput(inode); 1957feda821eSChristoph Hellwig return error; 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds 196060545d0dSAl Viro static int 196160545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 196260545d0dSAl Viro { 196360545d0dSAl Viro struct inode *inode; 196460545d0dSAl Viro int error = -ENOSPC; 196560545d0dSAl Viro 196660545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 196760545d0dSAl Viro if (inode) { 196860545d0dSAl Viro error = security_inode_init_security(inode, dir, 196960545d0dSAl Viro NULL, 197060545d0dSAl Viro shmem_initxattrs, NULL); 1971feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 1972feda821eSChristoph Hellwig goto out_iput; 1973feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 1974feda821eSChristoph Hellwig if (error) 1975feda821eSChristoph Hellwig goto out_iput; 197660545d0dSAl Viro d_tmpfile(dentry, inode); 197760545d0dSAl Viro } 197860545d0dSAl Viro return error; 1979feda821eSChristoph Hellwig out_iput: 1980feda821eSChristoph Hellwig iput(inode); 1981feda821eSChristoph Hellwig return error; 198260545d0dSAl Viro } 198360545d0dSAl Viro 198418bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 19851da177e4SLinus Torvalds { 19861da177e4SLinus Torvalds int error; 19871da177e4SLinus Torvalds 19881da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 19891da177e4SLinus Torvalds return error; 1990d8c76e6fSDave Hansen inc_nlink(dir); 19911da177e4SLinus Torvalds return 0; 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds 19944acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, 1995ebfc3b49SAl Viro bool excl) 19961da177e4SLinus Torvalds { 19971da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 19981da177e4SLinus Torvalds } 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds /* 20011da177e4SLinus Torvalds * Link a file.. 20021da177e4SLinus Torvalds */ 20031da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 20041da177e4SLinus Torvalds { 20051da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 20065b04c689SPavel Emelyanov int ret; 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds /* 20091da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 20101da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 20111da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 20121da177e4SLinus Torvalds */ 20135b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 20145b04c689SPavel Emelyanov if (ret) 20155b04c689SPavel Emelyanov goto out; 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 20181da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 2019d8c76e6fSDave Hansen inc_nlink(inode); 20207de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 20211da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 20221da177e4SLinus Torvalds d_instantiate(dentry, inode); 20235b04c689SPavel Emelyanov out: 20245b04c689SPavel Emelyanov return ret; 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 20281da177e4SLinus Torvalds { 20291da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20301da177e4SLinus Torvalds 20315b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 20325b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 20351da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 20369a53c3a7SDave Hansen drop_nlink(inode); 20371da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 20381da177e4SLinus Torvalds return 0; 20391da177e4SLinus Torvalds } 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 20421da177e4SLinus Torvalds { 20431da177e4SLinus Torvalds if (!simple_empty(dentry)) 20441da177e4SLinus Torvalds return -ENOTEMPTY; 20451da177e4SLinus Torvalds 20469a53c3a7SDave Hansen drop_nlink(dentry->d_inode); 20479a53c3a7SDave Hansen drop_nlink(dir); 20481da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds /* 20521da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 20531da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 20541da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 20551da177e4SLinus Torvalds * gets overwritten. 20561da177e4SLinus Torvalds */ 2057*3b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) 20581da177e4SLinus Torvalds { 20591da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 20601da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 20611da177e4SLinus Torvalds 2062*3b69ff51SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE)) 2063*3b69ff51SMiklos Szeredi return -EINVAL; 2064*3b69ff51SMiklos Szeredi 20651da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 20661da177e4SLinus Torvalds return -ENOTEMPTY; 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds if (new_dentry->d_inode) { 20691da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 20701da177e4SLinus Torvalds if (they_are_dirs) 20719a53c3a7SDave Hansen drop_nlink(old_dir); 20721da177e4SLinus Torvalds } else if (they_are_dirs) { 20739a53c3a7SDave Hansen drop_nlink(old_dir); 2074d8c76e6fSDave Hansen inc_nlink(new_dir); 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 20781da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 20791da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 20801da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 20811da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 20821da177e4SLinus Torvalds return 0; 20831da177e4SLinus Torvalds } 20841da177e4SLinus Torvalds 20851da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 20861da177e4SLinus Torvalds { 20871da177e4SLinus Torvalds int error; 20881da177e4SLinus Torvalds int len; 20891da177e4SLinus Torvalds struct inode *inode; 20909276aad6SHugh Dickins struct page *page; 20911da177e4SLinus Torvalds char *kaddr; 20921da177e4SLinus Torvalds struct shmem_inode_info *info; 20931da177e4SLinus Torvalds 20941da177e4SLinus Torvalds len = strlen(symname) + 1; 20951da177e4SLinus Torvalds if (len > PAGE_CACHE_SIZE) 20961da177e4SLinus Torvalds return -ENAMETOOLONG; 20971da177e4SLinus Torvalds 2098454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 20991da177e4SLinus Torvalds if (!inode) 21001da177e4SLinus Torvalds return -ENOSPC; 21011da177e4SLinus Torvalds 21029d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 21036d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2104570bc1c2SStephen Smalley if (error) { 2105570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 2106570bc1c2SStephen Smalley iput(inode); 2107570bc1c2SStephen Smalley return error; 2108570bc1c2SStephen Smalley } 2109570bc1c2SStephen Smalley error = 0; 2110570bc1c2SStephen Smalley } 2111570bc1c2SStephen Smalley 21121da177e4SLinus Torvalds info = SHMEM_I(inode); 21131da177e4SLinus Torvalds inode->i_size = len-1; 211469f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 211569f07ec9SHugh Dickins info->symlink = kmemdup(symname, len, GFP_KERNEL); 211669f07ec9SHugh Dickins if (!info->symlink) { 211769f07ec9SHugh Dickins iput(inode); 211869f07ec9SHugh Dickins return -ENOMEM; 211969f07ec9SHugh Dickins } 212069f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 21211da177e4SLinus Torvalds } else { 21221da177e4SLinus Torvalds error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); 21231da177e4SLinus Torvalds if (error) { 21241da177e4SLinus Torvalds iput(inode); 21251da177e4SLinus Torvalds return error; 21261da177e4SLinus Torvalds } 212714fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 21281da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 21299b04c5feSCong Wang kaddr = kmap_atomic(page); 21301da177e4SLinus Torvalds memcpy(kaddr, symname, len); 21319b04c5feSCong Wang kunmap_atomic(kaddr); 2132ec9516fbSHugh Dickins SetPageUptodate(page); 21331da177e4SLinus Torvalds set_page_dirty(page); 21346746aff7SWu Fengguang unlock_page(page); 21351da177e4SLinus Torvalds page_cache_release(page); 21361da177e4SLinus Torvalds } 21371da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 21381da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 21391da177e4SLinus Torvalds d_instantiate(dentry, inode); 21401da177e4SLinus Torvalds dget(dentry); 21411da177e4SLinus Torvalds return 0; 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 214469f07ec9SHugh Dickins static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd) 21451da177e4SLinus Torvalds { 214669f07ec9SHugh Dickins nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink); 2147cc314eefSLinus Torvalds return NULL; 21481da177e4SLinus Torvalds } 21491da177e4SLinus Torvalds 2150cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) 21511da177e4SLinus Torvalds { 21521da177e4SLinus Torvalds struct page *page = NULL; 215341ffe5d5SHugh Dickins int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); 215441ffe5d5SHugh Dickins nd_set_link(nd, error ? ERR_PTR(error) : kmap(page)); 2155d3602444SHugh Dickins if (page) 2156d3602444SHugh Dickins unlock_page(page); 2157cc314eefSLinus Torvalds return page; 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds 2160cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 21611da177e4SLinus Torvalds { 21621da177e4SLinus Torvalds if (!IS_ERR(nd_get_link(nd))) { 2163cc314eefSLinus Torvalds struct page *page = cookie; 21641da177e4SLinus Torvalds kunmap(page); 21651da177e4SLinus Torvalds mark_page_accessed(page); 21661da177e4SLinus Torvalds page_cache_release(page); 21671da177e4SLinus Torvalds } 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds 2170b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2171b09e0fa4SEric Paris /* 2172b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 2173b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 2174b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 2175b09e0fa4SEric Paris * filesystem level, though. 2176b09e0fa4SEric Paris */ 2177b09e0fa4SEric Paris 21786d9d88d0SJarkko Sakkinen /* 21796d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 21806d9d88d0SJarkko Sakkinen */ 21816d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 21826d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 21836d9d88d0SJarkko Sakkinen void *fs_info) 21846d9d88d0SJarkko Sakkinen { 21856d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 21866d9d88d0SJarkko Sakkinen const struct xattr *xattr; 218738f38657SAristeu Rozanski struct simple_xattr *new_xattr; 21886d9d88d0SJarkko Sakkinen size_t len; 21896d9d88d0SJarkko Sakkinen 21906d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 219138f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 21926d9d88d0SJarkko Sakkinen if (!new_xattr) 21936d9d88d0SJarkko Sakkinen return -ENOMEM; 21946d9d88d0SJarkko Sakkinen 21956d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 21966d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 21976d9d88d0SJarkko Sakkinen GFP_KERNEL); 21986d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 21996d9d88d0SJarkko Sakkinen kfree(new_xattr); 22006d9d88d0SJarkko Sakkinen return -ENOMEM; 22016d9d88d0SJarkko Sakkinen } 22026d9d88d0SJarkko Sakkinen 22036d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 22046d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 22056d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 22066d9d88d0SJarkko Sakkinen xattr->name, len); 22076d9d88d0SJarkko Sakkinen 220838f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 22096d9d88d0SJarkko Sakkinen } 22106d9d88d0SJarkko Sakkinen 22116d9d88d0SJarkko Sakkinen return 0; 22126d9d88d0SJarkko Sakkinen } 22136d9d88d0SJarkko Sakkinen 2214b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 2215b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 2216feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 2217feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 2218b09e0fa4SEric Paris #endif 2219b09e0fa4SEric Paris NULL 2220b09e0fa4SEric Paris }; 2221b09e0fa4SEric Paris 2222b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name) 2223b09e0fa4SEric Paris { 2224b09e0fa4SEric Paris struct { const char *prefix; size_t len; } arr[] = { 2225b09e0fa4SEric Paris { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, 2226b09e0fa4SEric Paris { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } 2227b09e0fa4SEric Paris }; 2228b09e0fa4SEric Paris int i; 2229b09e0fa4SEric Paris 2230b09e0fa4SEric Paris for (i = 0; i < ARRAY_SIZE(arr); i++) { 2231b09e0fa4SEric Paris size_t preflen = arr[i].len; 2232b09e0fa4SEric Paris if (strncmp(name, arr[i].prefix, preflen) == 0) { 2233b09e0fa4SEric Paris if (!name[preflen]) 2234b09e0fa4SEric Paris return -EINVAL; 2235b09e0fa4SEric Paris return 0; 2236b09e0fa4SEric Paris } 2237b09e0fa4SEric Paris } 2238b09e0fa4SEric Paris return -EOPNOTSUPP; 2239b09e0fa4SEric Paris } 2240b09e0fa4SEric Paris 2241b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, 2242b09e0fa4SEric Paris void *buffer, size_t size) 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_getxattr(dentry, name, buffer, size); 2254b09e0fa4SEric Paris 2255b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2256b09e0fa4SEric Paris if (err) 2257b09e0fa4SEric Paris return err; 2258b09e0fa4SEric Paris 225938f38657SAristeu Rozanski return simple_xattr_get(&info->xattrs, name, buffer, size); 2260b09e0fa4SEric Paris } 2261b09e0fa4SEric Paris 2262b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name, 2263b09e0fa4SEric Paris const void *value, size_t size, int flags) 2264b09e0fa4SEric Paris { 226538f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 2266b09e0fa4SEric Paris int err; 2267b09e0fa4SEric Paris 2268b09e0fa4SEric Paris /* 2269b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2270b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2271b09e0fa4SEric Paris * for it via sb->s_xattr. 2272b09e0fa4SEric Paris */ 2273b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2274b09e0fa4SEric Paris return generic_setxattr(dentry, name, value, size, flags); 2275b09e0fa4SEric Paris 2276b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2277b09e0fa4SEric Paris if (err) 2278b09e0fa4SEric Paris return err; 2279b09e0fa4SEric Paris 228038f38657SAristeu Rozanski return simple_xattr_set(&info->xattrs, name, value, size, flags); 2281b09e0fa4SEric Paris } 2282b09e0fa4SEric Paris 2283b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name) 2284b09e0fa4SEric Paris { 228538f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 2286b09e0fa4SEric Paris int err; 2287b09e0fa4SEric Paris 2288b09e0fa4SEric Paris /* 2289b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2290b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2291b09e0fa4SEric Paris * for it via sb->s_xattr. 2292b09e0fa4SEric Paris */ 2293b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2294b09e0fa4SEric Paris return generic_removexattr(dentry, name); 2295b09e0fa4SEric Paris 2296b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2297b09e0fa4SEric Paris if (err) 2298b09e0fa4SEric Paris return err; 2299b09e0fa4SEric Paris 230038f38657SAristeu Rozanski return simple_xattr_remove(&info->xattrs, name); 2301b09e0fa4SEric Paris } 2302b09e0fa4SEric Paris 2303b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 2304b09e0fa4SEric Paris { 230538f38657SAristeu Rozanski struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); 230638f38657SAristeu Rozanski return simple_xattr_list(&info->xattrs, buffer, size); 2307b09e0fa4SEric Paris } 2308b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 2309b09e0fa4SEric Paris 231069f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 23111da177e4SLinus Torvalds .readlink = generic_readlink, 231269f07ec9SHugh Dickins .follow_link = shmem_follow_short_symlink, 2313b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2314b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2315b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2316b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2317b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2318b09e0fa4SEric Paris #endif 23191da177e4SLinus Torvalds }; 23201da177e4SLinus Torvalds 232192e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 23221da177e4SLinus Torvalds .readlink = generic_readlink, 23231da177e4SLinus Torvalds .follow_link = shmem_follow_link, 23241da177e4SLinus Torvalds .put_link = shmem_put_link, 2325b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2326b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2327b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2328b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2329b09e0fa4SEric Paris .removexattr = shmem_removexattr, 233039f0247dSAndreas Gruenbacher #endif 2331b09e0fa4SEric Paris }; 233239f0247dSAndreas Gruenbacher 233391828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 233491828a40SDavid M. Grimes { 233591828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 233691828a40SDavid M. Grimes } 233791828a40SDavid M. Grimes 233891828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 233991828a40SDavid M. Grimes { 234091828a40SDavid M. Grimes __u32 *fh = vfh; 234191828a40SDavid M. Grimes __u64 inum = fh[2]; 234291828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 234391828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 234491828a40SDavid M. Grimes } 234591828a40SDavid M. Grimes 2346480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 2347480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 234891828a40SDavid M. Grimes { 234991828a40SDavid M. Grimes struct inode *inode; 2350480b116cSChristoph Hellwig struct dentry *dentry = NULL; 235135c2a7f4SHugh Dickins u64 inum; 235291828a40SDavid M. Grimes 2353480b116cSChristoph Hellwig if (fh_len < 3) 2354480b116cSChristoph Hellwig return NULL; 2355480b116cSChristoph Hellwig 235635c2a7f4SHugh Dickins inum = fid->raw[2]; 235735c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 235835c2a7f4SHugh Dickins 2359480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 2360480b116cSChristoph Hellwig shmem_match, fid->raw); 236191828a40SDavid M. Grimes if (inode) { 2362480b116cSChristoph Hellwig dentry = d_find_alias(inode); 236391828a40SDavid M. Grimes iput(inode); 236491828a40SDavid M. Grimes } 236591828a40SDavid M. Grimes 2366480b116cSChristoph Hellwig return dentry; 236791828a40SDavid M. Grimes } 236891828a40SDavid M. Grimes 2369b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 2370b0b0382bSAl Viro struct inode *parent) 237191828a40SDavid M. Grimes { 23725fe0c237SAneesh Kumar K.V if (*len < 3) { 23735fe0c237SAneesh Kumar K.V *len = 3; 237494e07a75SNamjae Jeon return FILEID_INVALID; 23755fe0c237SAneesh Kumar K.V } 237691828a40SDavid M. Grimes 23771d3382cbSAl Viro if (inode_unhashed(inode)) { 237891828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 237991828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 238091828a40SDavid M. Grimes * time, we need a lock to ensure we only try 238191828a40SDavid M. Grimes * to do it once 238291828a40SDavid M. Grimes */ 238391828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 238491828a40SDavid M. Grimes spin_lock(&lock); 23851d3382cbSAl Viro if (inode_unhashed(inode)) 238691828a40SDavid M. Grimes __insert_inode_hash(inode, 238791828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 238891828a40SDavid M. Grimes spin_unlock(&lock); 238991828a40SDavid M. Grimes } 239091828a40SDavid M. Grimes 239191828a40SDavid M. Grimes fh[0] = inode->i_generation; 239291828a40SDavid M. Grimes fh[1] = inode->i_ino; 239391828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 239491828a40SDavid M. Grimes 239591828a40SDavid M. Grimes *len = 3; 239691828a40SDavid M. Grimes return 1; 239791828a40SDavid M. Grimes } 239891828a40SDavid M. Grimes 239939655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 240091828a40SDavid M. Grimes .get_parent = shmem_get_parent, 240191828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 2402480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 240391828a40SDavid M. Grimes }; 240491828a40SDavid M. Grimes 2405680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 2406680d794bSakpm@linux-foundation.org bool remount) 24071da177e4SLinus Torvalds { 24081da177e4SLinus Torvalds char *this_char, *value, *rest; 240949cd0a5cSGreg Thelen struct mempolicy *mpol = NULL; 24108751e039SEric W. Biederman uid_t uid; 24118751e039SEric W. Biederman gid_t gid; 24121da177e4SLinus Torvalds 2413b00dc3adSHugh Dickins while (options != NULL) { 2414b00dc3adSHugh Dickins this_char = options; 2415b00dc3adSHugh Dickins for (;;) { 2416b00dc3adSHugh Dickins /* 2417b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 2418b00dc3adSHugh Dickins * mount options form a comma-separated list, 2419b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 2420b00dc3adSHugh Dickins */ 2421b00dc3adSHugh Dickins options = strchr(options, ','); 2422b00dc3adSHugh Dickins if (options == NULL) 2423b00dc3adSHugh Dickins break; 2424b00dc3adSHugh Dickins options++; 2425b00dc3adSHugh Dickins if (!isdigit(*options)) { 2426b00dc3adSHugh Dickins options[-1] = '\0'; 2427b00dc3adSHugh Dickins break; 2428b00dc3adSHugh Dickins } 2429b00dc3adSHugh Dickins } 24301da177e4SLinus Torvalds if (!*this_char) 24311da177e4SLinus Torvalds continue; 24321da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 24331da177e4SLinus Torvalds *value++ = 0; 24341da177e4SLinus Torvalds } else { 24351da177e4SLinus Torvalds printk(KERN_ERR 24361da177e4SLinus Torvalds "tmpfs: No value for mount option '%s'\n", 24371da177e4SLinus Torvalds this_char); 243849cd0a5cSGreg Thelen goto error; 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 24421da177e4SLinus Torvalds unsigned long long size; 24431da177e4SLinus Torvalds size = memparse(value,&rest); 24441da177e4SLinus Torvalds if (*rest == '%') { 24451da177e4SLinus Torvalds size <<= PAGE_SHIFT; 24461da177e4SLinus Torvalds size *= totalram_pages; 24471da177e4SLinus Torvalds do_div(size, 100); 24481da177e4SLinus Torvalds rest++; 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds if (*rest) 24511da177e4SLinus Torvalds goto bad_val; 2452680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 2453680d794bSakpm@linux-foundation.org DIV_ROUND_UP(size, PAGE_CACHE_SIZE); 24541da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 2455680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 24561da177e4SLinus Torvalds if (*rest) 24571da177e4SLinus Torvalds goto bad_val; 24581da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 2459680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 24601da177e4SLinus Torvalds if (*rest) 24611da177e4SLinus Torvalds goto bad_val; 24621da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 2463680d794bSakpm@linux-foundation.org if (remount) 24641da177e4SLinus Torvalds continue; 2465680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 24661da177e4SLinus Torvalds if (*rest) 24671da177e4SLinus Torvalds goto bad_val; 24681da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 2469680d794bSakpm@linux-foundation.org if (remount) 24701da177e4SLinus Torvalds continue; 24718751e039SEric W. Biederman uid = simple_strtoul(value, &rest, 0); 24721da177e4SLinus Torvalds if (*rest) 24731da177e4SLinus Torvalds goto bad_val; 24748751e039SEric W. Biederman sbinfo->uid = make_kuid(current_user_ns(), uid); 24758751e039SEric W. Biederman if (!uid_valid(sbinfo->uid)) 24768751e039SEric W. Biederman goto bad_val; 24771da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 2478680d794bSakpm@linux-foundation.org if (remount) 24791da177e4SLinus Torvalds continue; 24808751e039SEric W. Biederman gid = simple_strtoul(value, &rest, 0); 24811da177e4SLinus Torvalds if (*rest) 24821da177e4SLinus Torvalds goto bad_val; 24838751e039SEric W. Biederman sbinfo->gid = make_kgid(current_user_ns(), gid); 24848751e039SEric W. Biederman if (!gid_valid(sbinfo->gid)) 24858751e039SEric W. Biederman goto bad_val; 24867339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 248749cd0a5cSGreg Thelen mpol_put(mpol); 248849cd0a5cSGreg Thelen mpol = NULL; 248949cd0a5cSGreg Thelen if (mpol_parse_str(value, &mpol)) 24907339ff83SRobin Holt goto bad_val; 24911da177e4SLinus Torvalds } else { 24921da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad mount option %s\n", 24931da177e4SLinus Torvalds this_char); 249449cd0a5cSGreg Thelen goto error; 24951da177e4SLinus Torvalds } 24961da177e4SLinus Torvalds } 249749cd0a5cSGreg Thelen sbinfo->mpol = mpol; 24981da177e4SLinus Torvalds return 0; 24991da177e4SLinus Torvalds 25001da177e4SLinus Torvalds bad_val: 25011da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", 25021da177e4SLinus Torvalds value, this_char); 250349cd0a5cSGreg Thelen error: 250449cd0a5cSGreg Thelen mpol_put(mpol); 25051da177e4SLinus Torvalds return 1; 25061da177e4SLinus Torvalds 25071da177e4SLinus Torvalds } 25081da177e4SLinus Torvalds 25091da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 25101da177e4SLinus Torvalds { 25111da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2512680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 25130edd73b3SHugh Dickins unsigned long inodes; 25140edd73b3SHugh Dickins int error = -EINVAL; 25151da177e4SLinus Torvalds 25165f00110fSGreg Thelen config.mpol = NULL; 2517680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 25180edd73b3SHugh Dickins return error; 25190edd73b3SHugh Dickins 25200edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 25210edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 25227e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 25230edd73b3SHugh Dickins goto out; 2524680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 25250edd73b3SHugh Dickins goto out; 25260edd73b3SHugh Dickins /* 252754af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 25280edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 25290edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 25300edd73b3SHugh Dickins */ 2531680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 25320edd73b3SHugh Dickins goto out; 2533680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 25340edd73b3SHugh Dickins goto out; 25350edd73b3SHugh Dickins 25360edd73b3SHugh Dickins error = 0; 2537680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 2538680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 2539680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 254071fe804bSLee Schermerhorn 25415f00110fSGreg Thelen /* 25425f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 25435f00110fSGreg Thelen */ 25445f00110fSGreg Thelen if (config.mpol) { 254571fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 254671fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 25475f00110fSGreg Thelen } 25480edd73b3SHugh Dickins out: 25490edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 25500edd73b3SHugh Dickins return error; 25511da177e4SLinus Torvalds } 2552680d794bSakpm@linux-foundation.org 255334c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 2554680d794bSakpm@linux-foundation.org { 255534c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 2556680d794bSakpm@linux-foundation.org 2557680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 2558680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 2559680d794bSakpm@linux-foundation.org sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); 2560680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 2561680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 2562680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 256309208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 25648751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 25658751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 25668751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 25678751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 25688751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 25698751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 257071fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 2571680d794bSakpm@linux-foundation.org return 0; 2572680d794bSakpm@linux-foundation.org } 2573680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 25761da177e4SLinus Torvalds { 2577602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2578602586a8SHugh Dickins 2579602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 258049cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 2581602586a8SHugh Dickins kfree(sbinfo); 25821da177e4SLinus Torvalds sb->s_fs_info = NULL; 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 25852b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 25861da177e4SLinus Torvalds { 25871da177e4SLinus Torvalds struct inode *inode; 25880edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 2589680d794bSakpm@linux-foundation.org int err = -ENOMEM; 2590680d794bSakpm@linux-foundation.org 2591680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 2592425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 2593680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 2594680d794bSakpm@linux-foundation.org if (!sbinfo) 2595680d794bSakpm@linux-foundation.org return -ENOMEM; 2596680d794bSakpm@linux-foundation.org 2597680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 259876aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 259976aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 2600680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 26011da177e4SLinus Torvalds 26020edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 26031da177e4SLinus Torvalds /* 26041da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 26051da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 26061da177e4SLinus Torvalds * but the internal instance is left unlimited. 26071da177e4SLinus Torvalds */ 2608ca4e0519SAl Viro if (!(sb->s_flags & MS_KERNMOUNT)) { 2609680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 2610680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 2611680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 2612680d794bSakpm@linux-foundation.org err = -EINVAL; 2613680d794bSakpm@linux-foundation.org goto failed; 2614680d794bSakpm@linux-foundation.org } 2615ca4e0519SAl Viro } else { 2616ca4e0519SAl Viro sb->s_flags |= MS_NOUSER; 26171da177e4SLinus Torvalds } 261891828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 26192f6e38f3SHugh Dickins sb->s_flags |= MS_NOSEC; 26200edd73b3SHugh Dickins #else 26210edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 26220edd73b3SHugh Dickins #endif 26231da177e4SLinus Torvalds 26241da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 2625602586a8SHugh Dickins if (percpu_counter_init(&sbinfo->used_blocks, 0)) 2626602586a8SHugh Dickins goto failed; 2627680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 26281da177e4SLinus Torvalds 2629285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 26301da177e4SLinus Torvalds sb->s_blocksize = PAGE_CACHE_SIZE; 26311da177e4SLinus Torvalds sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 26321da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 26331da177e4SLinus Torvalds sb->s_op = &shmem_ops; 2634cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 2635b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 263639f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 2637b09e0fa4SEric Paris #endif 2638b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 263939f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 264039f0247dSAndreas Gruenbacher #endif 26410edd73b3SHugh Dickins 2642454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 26431da177e4SLinus Torvalds if (!inode) 26441da177e4SLinus Torvalds goto failed; 2645680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 2646680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 2647318ceed0SAl Viro sb->s_root = d_make_root(inode); 2648318ceed0SAl Viro if (!sb->s_root) 264948fde701SAl Viro goto failed; 26501da177e4SLinus Torvalds return 0; 26511da177e4SLinus Torvalds 26521da177e4SLinus Torvalds failed: 26531da177e4SLinus Torvalds shmem_put_super(sb); 26541da177e4SLinus Torvalds return err; 26551da177e4SLinus Torvalds } 26561da177e4SLinus Torvalds 2657fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 26581da177e4SLinus Torvalds 26591da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 26601da177e4SLinus Torvalds { 266141ffe5d5SHugh Dickins struct shmem_inode_info *info; 266241ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 266341ffe5d5SHugh Dickins if (!info) 26641da177e4SLinus Torvalds return NULL; 266541ffe5d5SHugh Dickins return &info->vfs_inode; 26661da177e4SLinus Torvalds } 26671da177e4SLinus Torvalds 266841ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 2669fa0d7e3dSNick Piggin { 2670fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 2671fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 2672fa0d7e3dSNick Piggin } 2673fa0d7e3dSNick Piggin 26741da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 26751da177e4SLinus Torvalds { 267609208d15SAl Viro if (S_ISREG(inode->i_mode)) 26771da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 267841ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 26791da177e4SLinus Torvalds } 26801da177e4SLinus Torvalds 268141ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 26821da177e4SLinus Torvalds { 268341ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 268441ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 26851da177e4SLinus Torvalds } 26861da177e4SLinus Torvalds 268741ffe5d5SHugh Dickins static int shmem_init_inodecache(void) 26881da177e4SLinus Torvalds { 26891da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 26901da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 269141ffe5d5SHugh Dickins 0, SLAB_PANIC, shmem_init_inode); 26921da177e4SLinus Torvalds return 0; 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds 269541ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 26961da177e4SLinus Torvalds { 26971a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds 2700f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 27011da177e4SLinus Torvalds .writepage = shmem_writepage, 270276719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 27031da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2704800d15a5SNick Piggin .write_begin = shmem_write_begin, 2705800d15a5SNick Piggin .write_end = shmem_write_end, 27061da177e4SLinus Torvalds #endif 2707304dbdb7SLee Schermerhorn .migratepage = migrate_page, 2708aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 27091da177e4SLinus Torvalds }; 27101da177e4SLinus Torvalds 271115ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 27121da177e4SLinus Torvalds .mmap = shmem_mmap, 27131da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2714220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 27152ba5bbedSAl Viro .read = new_sync_read, 27168174202bSAl Viro .write = new_sync_write, 27172ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 27188174202bSAl Viro .write_iter = generic_file_write_iter, 27191b061d92SChristoph Hellwig .fsync = noop_fsync, 2720708e3508SHugh Dickins .splice_read = shmem_file_splice_read, 2721f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 272283e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 27231da177e4SLinus Torvalds #endif 27241da177e4SLinus Torvalds }; 27251da177e4SLinus Torvalds 272692e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 272794c1e62dSHugh Dickins .setattr = shmem_setattr, 2728b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2729b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2730b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2731b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2732b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2733feda821eSChristoph Hellwig .set_acl = simple_set_acl, 2734b09e0fa4SEric Paris #endif 27351da177e4SLinus Torvalds }; 27361da177e4SLinus Torvalds 273792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 27381da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 27391da177e4SLinus Torvalds .create = shmem_create, 27401da177e4SLinus Torvalds .lookup = simple_lookup, 27411da177e4SLinus Torvalds .link = shmem_link, 27421da177e4SLinus Torvalds .unlink = shmem_unlink, 27431da177e4SLinus Torvalds .symlink = shmem_symlink, 27441da177e4SLinus Torvalds .mkdir = shmem_mkdir, 27451da177e4SLinus Torvalds .rmdir = shmem_rmdir, 27461da177e4SLinus Torvalds .mknod = shmem_mknod, 2747*3b69ff51SMiklos Szeredi .rename2 = shmem_rename2, 274860545d0dSAl Viro .tmpfile = shmem_tmpfile, 27491da177e4SLinus Torvalds #endif 2750b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2751b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2752b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2753b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2754b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2755b09e0fa4SEric Paris #endif 275639f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 275794c1e62dSHugh Dickins .setattr = shmem_setattr, 2758feda821eSChristoph Hellwig .set_acl = simple_set_acl, 275939f0247dSAndreas Gruenbacher #endif 276039f0247dSAndreas Gruenbacher }; 276139f0247dSAndreas Gruenbacher 276292e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 2763b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2764b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2765b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2766b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2767b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2768b09e0fa4SEric Paris #endif 276939f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 277094c1e62dSHugh Dickins .setattr = shmem_setattr, 2771feda821eSChristoph Hellwig .set_acl = simple_set_acl, 277239f0247dSAndreas Gruenbacher #endif 27731da177e4SLinus Torvalds }; 27741da177e4SLinus Torvalds 2775759b9775SHugh Dickins static const struct super_operations shmem_ops = { 27761da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 27771da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 27781da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 27791da177e4SLinus Torvalds .statfs = shmem_statfs, 27801da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 2781680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 27821da177e4SLinus Torvalds #endif 27831f895f75SAl Viro .evict_inode = shmem_evict_inode, 27841da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 27851da177e4SLinus Torvalds .put_super = shmem_put_super, 27861da177e4SLinus Torvalds }; 27871da177e4SLinus Torvalds 2788f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 278954cb8821SNick Piggin .fault = shmem_fault, 2790d7c17551SNing Qu .map_pages = filemap_map_pages, 27911da177e4SLinus Torvalds #ifdef CONFIG_NUMA 27921da177e4SLinus Torvalds .set_policy = shmem_set_policy, 27931da177e4SLinus Torvalds .get_policy = shmem_get_policy, 27941da177e4SLinus Torvalds #endif 27950b173bc4SKonstantin Khlebnikov .remap_pages = generic_file_remap_pages, 27961da177e4SLinus Torvalds }; 27971da177e4SLinus Torvalds 27983c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 27993c26ff6eSAl Viro int flags, const char *dev_name, void *data) 28001da177e4SLinus Torvalds { 28013c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds 280441ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 28051da177e4SLinus Torvalds .owner = THIS_MODULE, 28061da177e4SLinus Torvalds .name = "tmpfs", 28073c26ff6eSAl Viro .mount = shmem_mount, 28081da177e4SLinus Torvalds .kill_sb = kill_litter_super, 28092b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 28101da177e4SLinus Torvalds }; 28111da177e4SLinus Torvalds 281241ffe5d5SHugh Dickins int __init shmem_init(void) 28131da177e4SLinus Torvalds { 28141da177e4SLinus Torvalds int error; 28151da177e4SLinus Torvalds 281616203a7aSRob Landley /* If rootfs called this, don't re-init */ 281716203a7aSRob Landley if (shmem_inode_cachep) 281816203a7aSRob Landley return 0; 281916203a7aSRob Landley 2820e0bf68ddSPeter Zijlstra error = bdi_init(&shmem_backing_dev_info); 2821e0bf68ddSPeter Zijlstra if (error) 2822e0bf68ddSPeter Zijlstra goto out4; 2823e0bf68ddSPeter Zijlstra 282441ffe5d5SHugh Dickins error = shmem_init_inodecache(); 28251da177e4SLinus Torvalds if (error) 28261da177e4SLinus Torvalds goto out3; 28271da177e4SLinus Torvalds 282841ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 28291da177e4SLinus Torvalds if (error) { 28301da177e4SLinus Torvalds printk(KERN_ERR "Could not register tmpfs\n"); 28311da177e4SLinus Torvalds goto out2; 28321da177e4SLinus Torvalds } 283395dc112aSGreg Kroah-Hartman 2834ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 28351da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 28361da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 28371da177e4SLinus Torvalds printk(KERN_ERR "Could not kern_mount tmpfs\n"); 28381da177e4SLinus Torvalds goto out1; 28391da177e4SLinus Torvalds } 28401da177e4SLinus Torvalds return 0; 28411da177e4SLinus Torvalds 28421da177e4SLinus Torvalds out1: 284341ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 28441da177e4SLinus Torvalds out2: 284541ffe5d5SHugh Dickins shmem_destroy_inodecache(); 28461da177e4SLinus Torvalds out3: 2847e0bf68ddSPeter Zijlstra bdi_destroy(&shmem_backing_dev_info); 2848e0bf68ddSPeter Zijlstra out4: 28491da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 28501da177e4SLinus Torvalds return error; 28511da177e4SLinus Torvalds } 2852853ac43aSMatt Mackall 2853853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 2854853ac43aSMatt Mackall 2855853ac43aSMatt Mackall /* 2856853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 2857853ac43aSMatt Mackall * 2858853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 2859853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 2860853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 2861853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 2862853ac43aSMatt Mackall */ 2863853ac43aSMatt Mackall 286441ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 2865853ac43aSMatt Mackall .name = "tmpfs", 28663c26ff6eSAl Viro .mount = ramfs_mount, 2867853ac43aSMatt Mackall .kill_sb = kill_litter_super, 28682b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 2869853ac43aSMatt Mackall }; 2870853ac43aSMatt Mackall 287141ffe5d5SHugh Dickins int __init shmem_init(void) 2872853ac43aSMatt Mackall { 287341ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 2874853ac43aSMatt Mackall 287541ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 2876853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 2877853ac43aSMatt Mackall 2878853ac43aSMatt Mackall return 0; 2879853ac43aSMatt Mackall } 2880853ac43aSMatt Mackall 288141ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 2882853ac43aSMatt Mackall { 2883853ac43aSMatt Mackall return 0; 2884853ac43aSMatt Mackall } 2885853ac43aSMatt Mackall 28863f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 28873f96b79aSHugh Dickins { 28883f96b79aSHugh Dickins return 0; 28893f96b79aSHugh Dickins } 28903f96b79aSHugh Dickins 289124513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 289224513264SHugh Dickins { 289324513264SHugh Dickins } 289424513264SHugh Dickins 289541ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 289694c1e62dSHugh Dickins { 289741ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 289894c1e62dSHugh Dickins } 289994c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 290094c1e62dSHugh Dickins 2901853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 29020b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 2903454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 29040b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 29050b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 2906853ac43aSMatt Mackall 2907853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 2908853ac43aSMatt Mackall 2909853ac43aSMatt Mackall /* common code */ 29101da177e4SLinus Torvalds 29113451538aSAl Viro static struct dentry_operations anon_ops = { 2912118b2302SAl Viro .d_dname = simple_dname 29133451538aSAl Viro }; 29143451538aSAl Viro 2915c7277090SEric Paris static struct file *__shmem_file_setup(const char *name, loff_t size, 2916c7277090SEric Paris unsigned long flags, unsigned int i_flags) 29171da177e4SLinus Torvalds { 29186b4d0b27SAl Viro struct file *res; 29191da177e4SLinus Torvalds struct inode *inode; 29202c48b9c4SAl Viro struct path path; 29213451538aSAl Viro struct super_block *sb; 29221da177e4SLinus Torvalds struct qstr this; 29231da177e4SLinus Torvalds 29241da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 29256b4d0b27SAl Viro return ERR_CAST(shm_mnt); 29261da177e4SLinus Torvalds 2927285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 29281da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 29311da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 29321da177e4SLinus Torvalds 29336b4d0b27SAl Viro res = ERR_PTR(-ENOMEM); 29341da177e4SLinus Torvalds this.name = name; 29351da177e4SLinus Torvalds this.len = strlen(name); 29361da177e4SLinus Torvalds this.hash = 0; /* will go */ 29373451538aSAl Viro sb = shm_mnt->mnt_sb; 29383451538aSAl Viro path.dentry = d_alloc_pseudo(sb, &this); 29392c48b9c4SAl Viro if (!path.dentry) 29401da177e4SLinus Torvalds goto put_memory; 29413451538aSAl Viro d_set_d_op(path.dentry, &anon_ops); 29422c48b9c4SAl Viro path.mnt = mntget(shm_mnt); 29431da177e4SLinus Torvalds 29446b4d0b27SAl Viro res = ERR_PTR(-ENOSPC); 29453451538aSAl Viro inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 29461da177e4SLinus Torvalds if (!inode) 29474b42af81SAl Viro goto put_dentry; 29481da177e4SLinus Torvalds 2949c7277090SEric Paris inode->i_flags |= i_flags; 29502c48b9c4SAl Viro d_instantiate(path.dentry, inode); 29511da177e4SLinus Torvalds inode->i_size = size; 29526d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 295326567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 295426567cdbSAl Viro if (IS_ERR(res)) 29554b42af81SAl Viro goto put_dentry; 29564b42af81SAl Viro 29576b4d0b27SAl Viro res = alloc_file(&path, FMODE_WRITE | FMODE_READ, 29584b42af81SAl Viro &shmem_file_operations); 29596b4d0b27SAl Viro if (IS_ERR(res)) 29604b42af81SAl Viro goto put_dentry; 29614b42af81SAl Viro 29626b4d0b27SAl Viro return res; 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds put_dentry: 29652c48b9c4SAl Viro path_put(&path); 29661da177e4SLinus Torvalds put_memory: 29671da177e4SLinus Torvalds shmem_unacct_size(flags, size); 29686b4d0b27SAl Viro return res; 29691da177e4SLinus Torvalds } 2970c7277090SEric Paris 2971c7277090SEric Paris /** 2972c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 2973c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 2974c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 2975c7277090SEric Paris * higher layer. The one user is the big_key implementation. LSM checks 2976c7277090SEric Paris * are provided at the key level rather than the inode level. 2977c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 2978c7277090SEric Paris * @size: size to be set for the file 2979c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 2980c7277090SEric Paris */ 2981c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 2982c7277090SEric Paris { 2983c7277090SEric Paris return __shmem_file_setup(name, size, flags, S_PRIVATE); 2984c7277090SEric Paris } 2985c7277090SEric Paris 2986c7277090SEric Paris /** 2987c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 2988c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 2989c7277090SEric Paris * @size: size to be set for the file 2990c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 2991c7277090SEric Paris */ 2992c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 2993c7277090SEric Paris { 2994c7277090SEric Paris return __shmem_file_setup(name, size, flags, 0); 2995c7277090SEric Paris } 2996395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 29971da177e4SLinus Torvalds 299846711810SRandy Dunlap /** 29991da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 30001da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 30011da177e4SLinus Torvalds */ 30021da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 30031da177e4SLinus Torvalds { 30041da177e4SLinus Torvalds struct file *file; 30051da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 30061da177e4SLinus Torvalds 30071da177e4SLinus Torvalds file = shmem_file_setup("dev/zero", size, vma->vm_flags); 30081da177e4SLinus Torvalds if (IS_ERR(file)) 30091da177e4SLinus Torvalds return PTR_ERR(file); 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds if (vma->vm_file) 30121da177e4SLinus Torvalds fput(vma->vm_file); 30131da177e4SLinus Torvalds vma->vm_file = file; 30141da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 30151da177e4SLinus Torvalds return 0; 30161da177e4SLinus Torvalds } 3017d9d90e5eSHugh Dickins 3018d9d90e5eSHugh Dickins /** 3019d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 3020d9d90e5eSHugh Dickins * @mapping: the page's address_space 3021d9d90e5eSHugh Dickins * @index: the page index 3022d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 3023d9d90e5eSHugh Dickins * 3024d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 3025d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 3026d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 3027d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 3028d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 3029d9d90e5eSHugh Dickins * 303068da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 303168da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 3032d9d90e5eSHugh Dickins */ 3033d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 3034d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 3035d9d90e5eSHugh Dickins { 303668da9f05SHugh Dickins #ifdef CONFIG_SHMEM 303768da9f05SHugh Dickins struct inode *inode = mapping->host; 30389276aad6SHugh Dickins struct page *page; 303968da9f05SHugh Dickins int error; 304068da9f05SHugh Dickins 304168da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 304268da9f05SHugh Dickins error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL); 304368da9f05SHugh Dickins if (error) 304468da9f05SHugh Dickins page = ERR_PTR(error); 304568da9f05SHugh Dickins else 304668da9f05SHugh Dickins unlock_page(page); 304768da9f05SHugh Dickins return page; 304868da9f05SHugh Dickins #else 304968da9f05SHugh Dickins /* 305068da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 305168da9f05SHugh Dickins */ 3052d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 305368da9f05SHugh Dickins #endif 3054d9d90e5eSHugh Dickins } 3055d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 3056