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> 32*46c9a946SArnd Bergmann #include <linux/random.h> 33174cd4b1SIngo Molnar #include <linux/sched/signal.h> 34b95f1b31SPaul Gortmaker #include <linux/export.h> 35853ac43aSMatt Mackall #include <linux/swap.h> 36e2e40f2cSChristoph Hellwig #include <linux/uio.h> 37f3f0e1d2SKirill A. Shutemov #include <linux/khugepaged.h> 38749df87bSMike Kravetz #include <linux/hugetlb.h> 39853ac43aSMatt Mackall 4095cc09d6SAndrea Arcangeli #include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */ 4195cc09d6SAndrea Arcangeli 42853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 43853ac43aSMatt Mackall 44853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 451da177e4SLinus Torvalds /* 461da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 471da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 481da177e4SLinus Torvalds * which makes it a completely usable filesystem. 491da177e4SLinus Torvalds */ 501da177e4SLinus Torvalds 5139f0247dSAndreas Gruenbacher #include <linux/xattr.h> 52a5694255SChristoph Hellwig #include <linux/exportfs.h> 531c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 54feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 551da177e4SLinus Torvalds #include <linux/mman.h> 561da177e4SLinus Torvalds #include <linux/string.h> 571da177e4SLinus Torvalds #include <linux/slab.h> 581da177e4SLinus Torvalds #include <linux/backing-dev.h> 591da177e4SLinus Torvalds #include <linux/shmem_fs.h> 601da177e4SLinus Torvalds #include <linux/writeback.h> 611da177e4SLinus Torvalds #include <linux/blkdev.h> 62bda97eabSHugh Dickins #include <linux/pagevec.h> 6341ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 6483e4fa9cSHugh Dickins #include <linux/falloc.h> 65708e3508SHugh Dickins #include <linux/splice.h> 661da177e4SLinus Torvalds #include <linux/security.h> 671da177e4SLinus Torvalds #include <linux/swapops.h> 681da177e4SLinus Torvalds #include <linux/mempolicy.h> 691da177e4SLinus Torvalds #include <linux/namei.h> 70b00dc3adSHugh Dickins #include <linux/ctype.h> 71304dbdb7SLee Schermerhorn #include <linux/migrate.h> 72c1f60a5aSChristoph Lameter #include <linux/highmem.h> 73680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 7492562927SMimi Zohar #include <linux/magic.h> 759183df25SDavid Herrmann #include <linux/syscalls.h> 7640e041a2SDavid Herrmann #include <linux/fcntl.h> 779183df25SDavid Herrmann #include <uapi/linux/memfd.h> 78cfda0526SMike Rapoport #include <linux/userfaultfd_k.h> 794c27fe4cSMike Rapoport #include <linux/rmap.h> 802b4db796SAmir Goldstein #include <linux/uuid.h> 81304dbdb7SLee Schermerhorn 827c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 831da177e4SLinus Torvalds #include <asm/pgtable.h> 841da177e4SLinus Torvalds 85dd56b046SMel Gorman #include "internal.h" 86dd56b046SMel Gorman 8709cbfeafSKirill A. Shutemov #define BLOCKS_PER_PAGE (PAGE_SIZE/512) 8809cbfeafSKirill A. Shutemov #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 911da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 921da177e4SLinus Torvalds 9369f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 9469f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 9569f07ec9SHugh Dickins 961aac1400SHugh Dickins /* 97f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 98f00cdc6dSHugh Dickins * inode->i_private (with i_mutex making sure that it has only one user at 99f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 1001aac1400SHugh Dickins */ 1011aac1400SHugh Dickins struct shmem_falloc { 1028e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 1031aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 1041aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 1051aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 1061aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 1071aac1400SHugh Dickins }; 1081aac1400SHugh Dickins 109b76db735SAndrew Morton #ifdef CONFIG_TMPFS 110680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 111680d794bSakpm@linux-foundation.org { 112680d794bSakpm@linux-foundation.org return totalram_pages / 2; 113680d794bSakpm@linux-foundation.org } 114680d794bSakpm@linux-foundation.org 115680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 116680d794bSakpm@linux-foundation.org { 117680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 118680d794bSakpm@linux-foundation.org } 119b76db735SAndrew Morton #endif 120680d794bSakpm@linux-foundation.org 121bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp); 122bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 123bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index); 12468da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 1259e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, 126cfda0526SMike Rapoport gfp_t gfp, struct vm_area_struct *vma, 127cfda0526SMike Rapoport struct vm_fault *vmf, int *fault_type); 12868da9f05SHugh Dickins 129f3f0e1d2SKirill A. Shutemov int shmem_getpage(struct inode *inode, pgoff_t index, 1309e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp) 13168da9f05SHugh Dickins { 13268da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 133cfda0526SMike Rapoport mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); 13468da9f05SHugh Dickins } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1371da177e4SLinus Torvalds { 1381da177e4SLinus Torvalds return sb->s_fs_info; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* 1421da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1431da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1441da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1451da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1461da177e4SLinus Torvalds */ 1471da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1481da177e4SLinus Torvalds { 1490b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 150191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1541da177e4SLinus Torvalds { 1550b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1561da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 15977142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 16077142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 16177142517SKonstantin Khlebnikov { 16277142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 16377142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 16477142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 16577142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 16677142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 16777142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 16877142517SKonstantin Khlebnikov } 16977142517SKonstantin Khlebnikov return 0; 17077142517SKonstantin Khlebnikov } 17177142517SKonstantin Khlebnikov 1721da177e4SLinus Torvalds /* 1731da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 17475edd345SHugh Dickins * pages are allocated, in order to allow large sparse files. 1751da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1761da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1771da177e4SLinus Torvalds */ 178800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages) 1791da177e4SLinus Torvalds { 180800d8c63SKirill A. Shutemov if (!(flags & VM_NORESERVE)) 181800d8c63SKirill A. Shutemov return 0; 182800d8c63SKirill A. Shutemov 183800d8c63SKirill A. Shutemov return security_vm_enough_memory_mm(current->mm, 184800d8c63SKirill A. Shutemov pages * VM_ACCT(PAGE_SIZE)); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1881da177e4SLinus Torvalds { 1890b0a0806SHugh Dickins if (flags & VM_NORESERVE) 19009cbfeafSKirill A. Shutemov vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1930f079694SMike Rapoport static inline bool shmem_inode_acct_block(struct inode *inode, long pages) 1940f079694SMike Rapoport { 1950f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 1960f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 1970f079694SMike Rapoport 1980f079694SMike Rapoport if (shmem_acct_block(info->flags, pages)) 1990f079694SMike Rapoport return false; 2000f079694SMike Rapoport 2010f079694SMike Rapoport if (sbinfo->max_blocks) { 2020f079694SMike Rapoport if (percpu_counter_compare(&sbinfo->used_blocks, 2030f079694SMike Rapoport sbinfo->max_blocks - pages) > 0) 2040f079694SMike Rapoport goto unacct; 2050f079694SMike Rapoport percpu_counter_add(&sbinfo->used_blocks, pages); 2060f079694SMike Rapoport } 2070f079694SMike Rapoport 2080f079694SMike Rapoport return true; 2090f079694SMike Rapoport 2100f079694SMike Rapoport unacct: 2110f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2120f079694SMike Rapoport return false; 2130f079694SMike Rapoport } 2140f079694SMike Rapoport 2150f079694SMike Rapoport static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) 2160f079694SMike Rapoport { 2170f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2180f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2190f079694SMike Rapoport 2200f079694SMike Rapoport if (sbinfo->max_blocks) 2210f079694SMike Rapoport percpu_counter_sub(&sbinfo->used_blocks, pages); 2220f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2230f079694SMike Rapoport } 2240f079694SMike Rapoport 225759b9775SHugh Dickins static const struct super_operations shmem_ops; 226f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 22715ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 22892e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 22992e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 23092e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 231f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 232779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type; 2331da177e4SLinus Torvalds 234b0506e48SMike Rapoport bool vma_is_shmem(struct vm_area_struct *vma) 235b0506e48SMike Rapoport { 236b0506e48SMike Rapoport return vma->vm_ops == &shmem_vm_ops; 237b0506e48SMike Rapoport } 238b0506e48SMike Rapoport 2391da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 240cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2411da177e4SLinus Torvalds 2425b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 2435b04c689SPavel Emelyanov { 2445b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2455b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2465b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2475b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 2485b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2495b04c689SPavel Emelyanov return -ENOSPC; 2505b04c689SPavel Emelyanov } 2515b04c689SPavel Emelyanov sbinfo->free_inodes--; 2525b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2535b04c689SPavel Emelyanov } 2545b04c689SPavel Emelyanov return 0; 2555b04c689SPavel Emelyanov } 2565b04c689SPavel Emelyanov 2575b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2585b04c689SPavel Emelyanov { 2595b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2605b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2615b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2625b04c689SPavel Emelyanov sbinfo->free_inodes++; 2635b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2645b04c689SPavel Emelyanov } 2655b04c689SPavel Emelyanov } 2665b04c689SPavel Emelyanov 26746711810SRandy Dunlap /** 26841ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2691da177e4SLinus Torvalds * @inode: inode to recalc 2701da177e4SLinus Torvalds * 2711da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2721da177e4SLinus Torvalds * undirtied hole pages behind our back. 2731da177e4SLinus Torvalds * 2741da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2751da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2761da177e4SLinus Torvalds * 2771da177e4SLinus Torvalds * It has to be called with the spinlock held. 2781da177e4SLinus Torvalds */ 2791da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2801da177e4SLinus Torvalds { 2811da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2821da177e4SLinus Torvalds long freed; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2851da177e4SLinus Torvalds if (freed > 0) { 2861da177e4SLinus Torvalds info->alloced -= freed; 28754af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2880f079694SMike Rapoport shmem_inode_unacct_blocks(inode, freed); 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 292800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages) 293800d8c63SKirill A. Shutemov { 294800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 2954595ef88SKirill A. Shutemov unsigned long flags; 296800d8c63SKirill A. Shutemov 2970f079694SMike Rapoport if (!shmem_inode_acct_block(inode, pages)) 298800d8c63SKirill A. Shutemov return false; 299b1cc94abSMike Rapoport 3004595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 301800d8c63SKirill A. Shutemov info->alloced += pages; 302800d8c63SKirill A. Shutemov inode->i_blocks += pages * BLOCKS_PER_PAGE; 303800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3044595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 305800d8c63SKirill A. Shutemov inode->i_mapping->nrpages += pages; 306800d8c63SKirill A. Shutemov 307800d8c63SKirill A. Shutemov return true; 308800d8c63SKirill A. Shutemov } 309800d8c63SKirill A. Shutemov 310800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages) 311800d8c63SKirill A. Shutemov { 312800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3134595ef88SKirill A. Shutemov unsigned long flags; 314800d8c63SKirill A. Shutemov 3154595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 316800d8c63SKirill A. Shutemov info->alloced -= pages; 317800d8c63SKirill A. Shutemov inode->i_blocks -= pages * BLOCKS_PER_PAGE; 318800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3194595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 320800d8c63SKirill A. Shutemov 3210f079694SMike Rapoport shmem_inode_unacct_blocks(inode, pages); 322800d8c63SKirill A. Shutemov } 323800d8c63SKirill A. Shutemov 3247a5d0fbbSHugh Dickins /* 3257a5d0fbbSHugh Dickins * Replace item expected in radix tree by a new item, while holding tree lock. 3267a5d0fbbSHugh Dickins */ 3277a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping, 3287a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 3297a5d0fbbSHugh Dickins { 330f7942430SJohannes Weiner struct radix_tree_node *node; 3315b9c98f3SMike Kravetz void __rcu **pslot; 3326dbaf22cSJohannes Weiner void *item; 3337a5d0fbbSHugh Dickins 3347a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 3356dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 336b93b0163SMatthew Wilcox item = __radix_tree_lookup(&mapping->i_pages, index, &node, &pslot); 337f7942430SJohannes Weiner if (!item) 3386dbaf22cSJohannes Weiner return -ENOENT; 3397a5d0fbbSHugh Dickins if (item != expected) 3407a5d0fbbSHugh Dickins return -ENOENT; 341b93b0163SMatthew Wilcox __radix_tree_replace(&mapping->i_pages, node, pslot, 342c7df8ad2SMel Gorman replacement, NULL); 3437a5d0fbbSHugh Dickins return 0; 3447a5d0fbbSHugh Dickins } 3457a5d0fbbSHugh Dickins 3467a5d0fbbSHugh Dickins /* 347d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 348d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 349d1899228SHugh Dickins * 350d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 351d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 352d1899228SHugh Dickins */ 353d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 354d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 355d1899228SHugh Dickins { 356d1899228SHugh Dickins void *item; 357d1899228SHugh Dickins 358d1899228SHugh Dickins rcu_read_lock(); 359b93b0163SMatthew Wilcox item = radix_tree_lookup(&mapping->i_pages, index); 360d1899228SHugh Dickins rcu_read_unlock(); 361d1899228SHugh Dickins return item == swp_to_radix_entry(swap); 362d1899228SHugh Dickins } 363d1899228SHugh Dickins 364d1899228SHugh Dickins /* 3655a6e75f8SKirill A. Shutemov * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 3665a6e75f8SKirill A. Shutemov * 3675a6e75f8SKirill A. Shutemov * SHMEM_HUGE_NEVER: 3685a6e75f8SKirill A. Shutemov * disables huge pages for the mount; 3695a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ALWAYS: 3705a6e75f8SKirill A. Shutemov * enables huge pages for the mount; 3715a6e75f8SKirill A. Shutemov * SHMEM_HUGE_WITHIN_SIZE: 3725a6e75f8SKirill A. Shutemov * only allocate huge pages if the page will be fully within i_size, 3735a6e75f8SKirill A. Shutemov * also respect fadvise()/madvise() hints; 3745a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ADVISE: 3755a6e75f8SKirill A. Shutemov * only allocate huge pages if requested with fadvise()/madvise(); 3765a6e75f8SKirill A. Shutemov */ 3775a6e75f8SKirill A. Shutemov 3785a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER 0 3795a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS 1 3805a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE 2 3815a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE 3 3825a6e75f8SKirill A. Shutemov 3835a6e75f8SKirill A. Shutemov /* 3845a6e75f8SKirill A. Shutemov * Special values. 3855a6e75f8SKirill A. Shutemov * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 3865a6e75f8SKirill A. Shutemov * 3875a6e75f8SKirill A. Shutemov * SHMEM_HUGE_DENY: 3885a6e75f8SKirill A. Shutemov * disables huge on shm_mnt and all mounts, for emergency use; 3895a6e75f8SKirill A. Shutemov * SHMEM_HUGE_FORCE: 3905a6e75f8SKirill A. Shutemov * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 3915a6e75f8SKirill A. Shutemov * 3925a6e75f8SKirill A. Shutemov */ 3935a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY (-1) 3945a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE (-2) 3955a6e75f8SKirill A. Shutemov 396e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3975a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */ 3985a6e75f8SKirill A. Shutemov 3995b9c98f3SMike Kravetz static int shmem_huge __read_mostly; 4005a6e75f8SKirill A. Shutemov 401f1f5929cSJérémy Lefaure #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) 4025a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str) 4035a6e75f8SKirill A. Shutemov { 4045a6e75f8SKirill A. Shutemov if (!strcmp(str, "never")) 4055a6e75f8SKirill A. Shutemov return SHMEM_HUGE_NEVER; 4065a6e75f8SKirill A. Shutemov if (!strcmp(str, "always")) 4075a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ALWAYS; 4085a6e75f8SKirill A. Shutemov if (!strcmp(str, "within_size")) 4095a6e75f8SKirill A. Shutemov return SHMEM_HUGE_WITHIN_SIZE; 4105a6e75f8SKirill A. Shutemov if (!strcmp(str, "advise")) 4115a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ADVISE; 4125a6e75f8SKirill A. Shutemov if (!strcmp(str, "deny")) 4135a6e75f8SKirill A. Shutemov return SHMEM_HUGE_DENY; 4145a6e75f8SKirill A. Shutemov if (!strcmp(str, "force")) 4155a6e75f8SKirill A. Shutemov return SHMEM_HUGE_FORCE; 4165a6e75f8SKirill A. Shutemov return -EINVAL; 4175a6e75f8SKirill A. Shutemov } 4185a6e75f8SKirill A. Shutemov 4195a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge) 4205a6e75f8SKirill A. Shutemov { 4215a6e75f8SKirill A. Shutemov switch (huge) { 4225a6e75f8SKirill A. Shutemov case SHMEM_HUGE_NEVER: 4235a6e75f8SKirill A. Shutemov return "never"; 4245a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 4255a6e75f8SKirill A. Shutemov return "always"; 4265a6e75f8SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 4275a6e75f8SKirill A. Shutemov return "within_size"; 4285a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 4295a6e75f8SKirill A. Shutemov return "advise"; 4305a6e75f8SKirill A. Shutemov case SHMEM_HUGE_DENY: 4315a6e75f8SKirill A. Shutemov return "deny"; 4325a6e75f8SKirill A. Shutemov case SHMEM_HUGE_FORCE: 4335a6e75f8SKirill A. Shutemov return "force"; 4345a6e75f8SKirill A. Shutemov default: 4355a6e75f8SKirill A. Shutemov VM_BUG_ON(1); 4365a6e75f8SKirill A. Shutemov return "bad_val"; 4375a6e75f8SKirill A. Shutemov } 4385a6e75f8SKirill A. Shutemov } 439f1f5929cSJérémy Lefaure #endif 4405a6e75f8SKirill A. Shutemov 441779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 442779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 443779750d2SKirill A. Shutemov { 444779750d2SKirill A. Shutemov LIST_HEAD(list), *pos, *next; 445253fd0f0SKirill A. Shutemov LIST_HEAD(to_remove); 446779750d2SKirill A. Shutemov struct inode *inode; 447779750d2SKirill A. Shutemov struct shmem_inode_info *info; 448779750d2SKirill A. Shutemov struct page *page; 449779750d2SKirill A. Shutemov unsigned long batch = sc ? sc->nr_to_scan : 128; 450779750d2SKirill A. Shutemov int removed = 0, split = 0; 451779750d2SKirill A. Shutemov 452779750d2SKirill A. Shutemov if (list_empty(&sbinfo->shrinklist)) 453779750d2SKirill A. Shutemov return SHRINK_STOP; 454779750d2SKirill A. Shutemov 455779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 456779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &sbinfo->shrinklist) { 457779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 458779750d2SKirill A. Shutemov 459779750d2SKirill A. Shutemov /* pin the inode */ 460779750d2SKirill A. Shutemov inode = igrab(&info->vfs_inode); 461779750d2SKirill A. Shutemov 462779750d2SKirill A. Shutemov /* inode is about to be evicted */ 463779750d2SKirill A. Shutemov if (!inode) { 464779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 465779750d2SKirill A. Shutemov removed++; 466779750d2SKirill A. Shutemov goto next; 467779750d2SKirill A. Shutemov } 468779750d2SKirill A. Shutemov 469779750d2SKirill A. Shutemov /* Check if there's anything to gain */ 470779750d2SKirill A. Shutemov if (round_up(inode->i_size, PAGE_SIZE) == 471779750d2SKirill A. Shutemov round_up(inode->i_size, HPAGE_PMD_SIZE)) { 472253fd0f0SKirill A. Shutemov list_move(&info->shrinklist, &to_remove); 473779750d2SKirill A. Shutemov removed++; 474779750d2SKirill A. Shutemov goto next; 475779750d2SKirill A. Shutemov } 476779750d2SKirill A. Shutemov 477779750d2SKirill A. Shutemov list_move(&info->shrinklist, &list); 478779750d2SKirill A. Shutemov next: 479779750d2SKirill A. Shutemov if (!--batch) 480779750d2SKirill A. Shutemov break; 481779750d2SKirill A. Shutemov } 482779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 483779750d2SKirill A. Shutemov 484253fd0f0SKirill A. Shutemov list_for_each_safe(pos, next, &to_remove) { 485253fd0f0SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 486253fd0f0SKirill A. Shutemov inode = &info->vfs_inode; 487253fd0f0SKirill A. Shutemov list_del_init(&info->shrinklist); 488253fd0f0SKirill A. Shutemov iput(inode); 489253fd0f0SKirill A. Shutemov } 490253fd0f0SKirill A. Shutemov 491779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &list) { 492779750d2SKirill A. Shutemov int ret; 493779750d2SKirill A. Shutemov 494779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 495779750d2SKirill A. Shutemov inode = &info->vfs_inode; 496779750d2SKirill A. Shutemov 497b3cd54b2SKirill A. Shutemov if (nr_to_split && split >= nr_to_split) 498b3cd54b2SKirill A. Shutemov goto leave; 499779750d2SKirill A. Shutemov 500b3cd54b2SKirill A. Shutemov page = find_get_page(inode->i_mapping, 501779750d2SKirill A. Shutemov (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT); 502779750d2SKirill A. Shutemov if (!page) 503779750d2SKirill A. Shutemov goto drop; 504779750d2SKirill A. Shutemov 505b3cd54b2SKirill A. Shutemov /* No huge page at the end of the file: nothing to split */ 506779750d2SKirill A. Shutemov if (!PageTransHuge(page)) { 507779750d2SKirill A. Shutemov put_page(page); 508779750d2SKirill A. Shutemov goto drop; 509779750d2SKirill A. Shutemov } 510779750d2SKirill A. Shutemov 511b3cd54b2SKirill A. Shutemov /* 512b3cd54b2SKirill A. Shutemov * Leave the inode on the list if we failed to lock 513b3cd54b2SKirill A. Shutemov * the page at this time. 514b3cd54b2SKirill A. Shutemov * 515b3cd54b2SKirill A. Shutemov * Waiting for the lock may lead to deadlock in the 516b3cd54b2SKirill A. Shutemov * reclaim path. 517b3cd54b2SKirill A. Shutemov */ 518b3cd54b2SKirill A. Shutemov if (!trylock_page(page)) { 519b3cd54b2SKirill A. Shutemov put_page(page); 520b3cd54b2SKirill A. Shutemov goto leave; 521b3cd54b2SKirill A. Shutemov } 522b3cd54b2SKirill A. Shutemov 523779750d2SKirill A. Shutemov ret = split_huge_page(page); 524779750d2SKirill A. Shutemov unlock_page(page); 525779750d2SKirill A. Shutemov put_page(page); 526779750d2SKirill A. Shutemov 527b3cd54b2SKirill A. Shutemov /* If split failed leave the inode on the list */ 528b3cd54b2SKirill A. Shutemov if (ret) 529b3cd54b2SKirill A. Shutemov goto leave; 530779750d2SKirill A. Shutemov 531779750d2SKirill A. Shutemov split++; 532779750d2SKirill A. Shutemov drop: 533779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 534779750d2SKirill A. Shutemov removed++; 535b3cd54b2SKirill A. Shutemov leave: 536779750d2SKirill A. Shutemov iput(inode); 537779750d2SKirill A. Shutemov } 538779750d2SKirill A. Shutemov 539779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 540779750d2SKirill A. Shutemov list_splice_tail(&list, &sbinfo->shrinklist); 541779750d2SKirill A. Shutemov sbinfo->shrinklist_len -= removed; 542779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 543779750d2SKirill A. Shutemov 544779750d2SKirill A. Shutemov return split; 545779750d2SKirill A. Shutemov } 546779750d2SKirill A. Shutemov 547779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb, 548779750d2SKirill A. Shutemov struct shrink_control *sc) 549779750d2SKirill A. Shutemov { 550779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 551779750d2SKirill A. Shutemov 552779750d2SKirill A. Shutemov if (!READ_ONCE(sbinfo->shrinklist_len)) 553779750d2SKirill A. Shutemov return SHRINK_STOP; 554779750d2SKirill A. Shutemov 555779750d2SKirill A. Shutemov return shmem_unused_huge_shrink(sbinfo, sc, 0); 556779750d2SKirill A. Shutemov } 557779750d2SKirill A. Shutemov 558779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb, 559779750d2SKirill A. Shutemov struct shrink_control *sc) 560779750d2SKirill A. Shutemov { 561779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 562779750d2SKirill A. Shutemov return READ_ONCE(sbinfo->shrinklist_len); 563779750d2SKirill A. Shutemov } 564e496cf3dSKirill A. Shutemov #else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 5655a6e75f8SKirill A. Shutemov 5665a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY 5675a6e75f8SKirill A. Shutemov 568779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 569779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 570779750d2SKirill A. Shutemov { 571779750d2SKirill A. Shutemov return 0; 572779750d2SKirill A. Shutemov } 573e496cf3dSKirill A. Shutemov #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 5745a6e75f8SKirill A. Shutemov 57589fdcd26SYang Shi static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo) 57689fdcd26SYang Shi { 57789fdcd26SYang Shi if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 57889fdcd26SYang Shi (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) && 57989fdcd26SYang Shi shmem_huge != SHMEM_HUGE_DENY) 58089fdcd26SYang Shi return true; 58189fdcd26SYang Shi return false; 58289fdcd26SYang Shi } 58389fdcd26SYang Shi 5845a6e75f8SKirill A. Shutemov /* 58546f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 58646f65ec1SHugh Dickins */ 58746f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 58846f65ec1SHugh Dickins struct address_space *mapping, 589fed400a1SWang Sheng-Hui pgoff_t index, void *expected) 59046f65ec1SHugh Dickins { 591800d8c63SKirill A. Shutemov int error, nr = hpage_nr_pages(page); 59246f65ec1SHugh Dickins 593800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 594800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(index != round_down(index, nr), page); 595309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 596309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapBacked(page), page); 597800d8c63SKirill A. Shutemov VM_BUG_ON(expected && PageTransHuge(page)); 59846f65ec1SHugh Dickins 599800d8c63SKirill A. Shutemov page_ref_add(page, nr); 60046f65ec1SHugh Dickins page->mapping = mapping; 60146f65ec1SHugh Dickins page->index = index; 60246f65ec1SHugh Dickins 603b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 604800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 605800d8c63SKirill A. Shutemov void __rcu **results; 606800d8c63SKirill A. Shutemov pgoff_t idx; 607800d8c63SKirill A. Shutemov int i; 608800d8c63SKirill A. Shutemov 609800d8c63SKirill A. Shutemov error = 0; 610b93b0163SMatthew Wilcox if (radix_tree_gang_lookup_slot(&mapping->i_pages, 611800d8c63SKirill A. Shutemov &results, &idx, index, 1) && 612800d8c63SKirill A. Shutemov idx < index + HPAGE_PMD_NR) { 613800d8c63SKirill A. Shutemov error = -EEXIST; 614800d8c63SKirill A. Shutemov } 615800d8c63SKirill A. Shutemov 616800d8c63SKirill A. Shutemov if (!error) { 617800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 618b93b0163SMatthew Wilcox error = radix_tree_insert(&mapping->i_pages, 619800d8c63SKirill A. Shutemov index + i, page + i); 620800d8c63SKirill A. Shutemov VM_BUG_ON(error); 621800d8c63SKirill A. Shutemov } 622800d8c63SKirill A. Shutemov count_vm_event(THP_FILE_ALLOC); 623800d8c63SKirill A. Shutemov } 624800d8c63SKirill A. Shutemov } else if (!expected) { 625b93b0163SMatthew Wilcox error = radix_tree_insert(&mapping->i_pages, index, page); 626800d8c63SKirill A. Shutemov } else { 627b065b432SHugh Dickins error = shmem_radix_tree_replace(mapping, index, expected, 628b065b432SHugh Dickins page); 629800d8c63SKirill A. Shutemov } 630800d8c63SKirill A. Shutemov 63146f65ec1SHugh Dickins if (!error) { 632800d8c63SKirill A. Shutemov mapping->nrpages += nr; 633800d8c63SKirill A. Shutemov if (PageTransHuge(page)) 63411fb9989SMel Gorman __inc_node_page_state(page, NR_SHMEM_THPS); 63511fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr); 63611fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr); 637b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 63846f65ec1SHugh Dickins } else { 63946f65ec1SHugh Dickins page->mapping = NULL; 640b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 641800d8c63SKirill A. Shutemov page_ref_sub(page, nr); 64246f65ec1SHugh Dickins } 64346f65ec1SHugh Dickins return error; 64446f65ec1SHugh Dickins } 64546f65ec1SHugh Dickins 64646f65ec1SHugh Dickins /* 6476922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 6486922c0c7SHugh Dickins */ 6496922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 6506922c0c7SHugh Dickins { 6516922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 6526922c0c7SHugh Dickins int error; 6536922c0c7SHugh Dickins 654800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 655800d8c63SKirill A. Shutemov 656b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 6576922c0c7SHugh Dickins error = shmem_radix_tree_replace(mapping, page->index, page, radswap); 6586922c0c7SHugh Dickins page->mapping = NULL; 6596922c0c7SHugh Dickins mapping->nrpages--; 66011fb9989SMel Gorman __dec_node_page_state(page, NR_FILE_PAGES); 66111fb9989SMel Gorman __dec_node_page_state(page, NR_SHMEM); 662b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 66309cbfeafSKirill A. Shutemov put_page(page); 6646922c0c7SHugh Dickins BUG_ON(error); 6656922c0c7SHugh Dickins } 6666922c0c7SHugh Dickins 6676922c0c7SHugh Dickins /* 6687a5d0fbbSHugh Dickins * Remove swap entry from radix tree, free the swap and its page cache. 6697a5d0fbbSHugh Dickins */ 6707a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 6717a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 6727a5d0fbbSHugh Dickins { 6736dbaf22cSJohannes Weiner void *old; 6747a5d0fbbSHugh Dickins 675b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 676b93b0163SMatthew Wilcox old = radix_tree_delete_item(&mapping->i_pages, index, radswap); 677b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 6786dbaf22cSJohannes Weiner if (old != radswap) 6796dbaf22cSJohannes Weiner return -ENOENT; 6807a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 6816dbaf22cSJohannes Weiner return 0; 6827a5d0fbbSHugh Dickins } 6837a5d0fbbSHugh Dickins 6847a5d0fbbSHugh Dickins /* 6856a15a370SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 68648131e03SVlastimil Babka * given offsets are swapped out. 6876a15a370SVlastimil Babka * 688b93b0163SMatthew Wilcox * This is safe to call without i_mutex or the i_pages lock thanks to RCU, 6896a15a370SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 6906a15a370SVlastimil Babka */ 69148131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping, 69248131e03SVlastimil Babka pgoff_t start, pgoff_t end) 6936a15a370SVlastimil Babka { 6946a15a370SVlastimil Babka struct radix_tree_iter iter; 6955b9c98f3SMike Kravetz void __rcu **slot; 6966a15a370SVlastimil Babka struct page *page; 69748131e03SVlastimil Babka unsigned long swapped = 0; 6986a15a370SVlastimil Babka 6996a15a370SVlastimil Babka rcu_read_lock(); 7006a15a370SVlastimil Babka 701b93b0163SMatthew Wilcox radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) { 7026a15a370SVlastimil Babka if (iter.index >= end) 7036a15a370SVlastimil Babka break; 7046a15a370SVlastimil Babka 7056a15a370SVlastimil Babka page = radix_tree_deref_slot(slot); 7066a15a370SVlastimil Babka 7072cf938aaSMatthew Wilcox if (radix_tree_deref_retry(page)) { 7082cf938aaSMatthew Wilcox slot = radix_tree_iter_retry(&iter); 7092cf938aaSMatthew Wilcox continue; 7102cf938aaSMatthew Wilcox } 7116a15a370SVlastimil Babka 7126a15a370SVlastimil Babka if (radix_tree_exceptional_entry(page)) 7136a15a370SVlastimil Babka swapped++; 7146a15a370SVlastimil Babka 7156a15a370SVlastimil Babka if (need_resched()) { 716148deab2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 7176a15a370SVlastimil Babka cond_resched_rcu(); 7186a15a370SVlastimil Babka } 7196a15a370SVlastimil Babka } 7206a15a370SVlastimil Babka 7216a15a370SVlastimil Babka rcu_read_unlock(); 7226a15a370SVlastimil Babka 7236a15a370SVlastimil Babka return swapped << PAGE_SHIFT; 7246a15a370SVlastimil Babka } 7256a15a370SVlastimil Babka 7266a15a370SVlastimil Babka /* 72748131e03SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 72848131e03SVlastimil Babka * given vma is swapped out. 72948131e03SVlastimil Babka * 730b93b0163SMatthew Wilcox * This is safe to call without i_mutex or the i_pages lock thanks to RCU, 73148131e03SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 73248131e03SVlastimil Babka */ 73348131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma) 73448131e03SVlastimil Babka { 73548131e03SVlastimil Babka struct inode *inode = file_inode(vma->vm_file); 73648131e03SVlastimil Babka struct shmem_inode_info *info = SHMEM_I(inode); 73748131e03SVlastimil Babka struct address_space *mapping = inode->i_mapping; 73848131e03SVlastimil Babka unsigned long swapped; 73948131e03SVlastimil Babka 74048131e03SVlastimil Babka /* Be careful as we don't hold info->lock */ 74148131e03SVlastimil Babka swapped = READ_ONCE(info->swapped); 74248131e03SVlastimil Babka 74348131e03SVlastimil Babka /* 74448131e03SVlastimil Babka * The easier cases are when the shmem object has nothing in swap, or 74548131e03SVlastimil Babka * the vma maps it whole. Then we can simply use the stats that we 74648131e03SVlastimil Babka * already track. 74748131e03SVlastimil Babka */ 74848131e03SVlastimil Babka if (!swapped) 74948131e03SVlastimil Babka return 0; 75048131e03SVlastimil Babka 75148131e03SVlastimil Babka if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 75248131e03SVlastimil Babka return swapped << PAGE_SHIFT; 75348131e03SVlastimil Babka 75448131e03SVlastimil Babka /* Here comes the more involved part */ 75548131e03SVlastimil Babka return shmem_partial_swap_usage(mapping, 75648131e03SVlastimil Babka linear_page_index(vma, vma->vm_start), 75748131e03SVlastimil Babka linear_page_index(vma, vma->vm_end)); 75848131e03SVlastimil Babka } 75948131e03SVlastimil Babka 76048131e03SVlastimil Babka /* 76124513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 76224513264SHugh Dickins */ 76324513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 76424513264SHugh Dickins { 76524513264SHugh Dickins struct pagevec pvec; 76624513264SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 76724513264SHugh Dickins pgoff_t index = 0; 76824513264SHugh Dickins 76986679820SMel Gorman pagevec_init(&pvec); 77024513264SHugh Dickins /* 77124513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 77224513264SHugh Dickins */ 77324513264SHugh Dickins while (!mapping_unevictable(mapping)) { 77424513264SHugh Dickins /* 77524513264SHugh Dickins * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it 77624513264SHugh Dickins * has finished, if it hits a row of PAGEVEC_SIZE swap entries. 77724513264SHugh Dickins */ 7780cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 77924513264SHugh Dickins PAGEVEC_SIZE, pvec.pages, indices); 78024513264SHugh Dickins if (!pvec.nr) 78124513264SHugh Dickins break; 78224513264SHugh Dickins index = indices[pvec.nr - 1] + 1; 7830cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 78424513264SHugh Dickins check_move_unevictable_pages(pvec.pages, pvec.nr); 78524513264SHugh Dickins pagevec_release(&pvec); 78624513264SHugh Dickins cond_resched(); 78724513264SHugh Dickins } 7887a5d0fbbSHugh Dickins } 7897a5d0fbbSHugh Dickins 7907a5d0fbbSHugh Dickins /* 7917a5d0fbbSHugh Dickins * Remove range of pages and swap entries from radix tree, and free them. 7921635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 7937a5d0fbbSHugh Dickins */ 7941635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 7951635f6a7SHugh Dickins bool unfalloc) 7961da177e4SLinus Torvalds { 797285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 7981da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 79909cbfeafSKirill A. Shutemov pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 80009cbfeafSKirill A. Shutemov pgoff_t end = (lend + 1) >> PAGE_SHIFT; 80109cbfeafSKirill A. Shutemov unsigned int partial_start = lstart & (PAGE_SIZE - 1); 80209cbfeafSKirill A. Shutemov unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1); 803bda97eabSHugh Dickins struct pagevec pvec; 8047a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 8057a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 806285b2c4fSHugh Dickins pgoff_t index; 807bda97eabSHugh Dickins int i; 8081da177e4SLinus Torvalds 80983e4fa9cSHugh Dickins if (lend == -1) 81083e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 811bda97eabSHugh Dickins 81286679820SMel Gorman pagevec_init(&pvec); 813bda97eabSHugh Dickins index = start; 81483e4fa9cSHugh Dickins while (index < end) { 8150cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 81683e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 8177a5d0fbbSHugh Dickins pvec.pages, indices); 8187a5d0fbbSHugh Dickins if (!pvec.nr) 8197a5d0fbbSHugh Dickins break; 820bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 821bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 822bda97eabSHugh Dickins 8237a5d0fbbSHugh Dickins index = indices[i]; 82483e4fa9cSHugh Dickins if (index >= end) 825bda97eabSHugh Dickins break; 826bda97eabSHugh Dickins 8277a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 8281635f6a7SHugh Dickins if (unfalloc) 8291635f6a7SHugh Dickins continue; 8307a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 8317a5d0fbbSHugh Dickins index, page); 8327a5d0fbbSHugh Dickins continue; 8337a5d0fbbSHugh Dickins } 8347a5d0fbbSHugh Dickins 835800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page); 836800d8c63SKirill A. Shutemov 837bda97eabSHugh Dickins if (!trylock_page(page)) 838bda97eabSHugh Dickins continue; 839800d8c63SKirill A. Shutemov 840800d8c63SKirill A. Shutemov if (PageTransTail(page)) { 841800d8c63SKirill A. Shutemov /* Middle of THP: zero out the page */ 842800d8c63SKirill A. Shutemov clear_highpage(page); 843800d8c63SKirill A. Shutemov unlock_page(page); 844800d8c63SKirill A. Shutemov continue; 845800d8c63SKirill A. Shutemov } else if (PageTransHuge(page)) { 846800d8c63SKirill A. Shutemov if (index == round_down(end, HPAGE_PMD_NR)) { 847800d8c63SKirill A. Shutemov /* 848800d8c63SKirill A. Shutemov * Range ends in the middle of THP: 849800d8c63SKirill A. Shutemov * zero out the page 850800d8c63SKirill A. Shutemov */ 851800d8c63SKirill A. Shutemov clear_highpage(page); 852800d8c63SKirill A. Shutemov unlock_page(page); 853800d8c63SKirill A. Shutemov continue; 854800d8c63SKirill A. Shutemov } 855800d8c63SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 856800d8c63SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 857800d8c63SKirill A. Shutemov } 858800d8c63SKirill A. Shutemov 8591635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 860800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 861800d8c63SKirill A. Shutemov if (page_mapping(page) == mapping) { 862309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 863bda97eabSHugh Dickins truncate_inode_page(mapping, page); 8647a5d0fbbSHugh Dickins } 8651635f6a7SHugh Dickins } 866bda97eabSHugh Dickins unlock_page(page); 867bda97eabSHugh Dickins } 8680cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 86924513264SHugh Dickins pagevec_release(&pvec); 870bda97eabSHugh Dickins cond_resched(); 871bda97eabSHugh Dickins index++; 872bda97eabSHugh Dickins } 873bda97eabSHugh Dickins 87483e4fa9cSHugh Dickins if (partial_start) { 875bda97eabSHugh Dickins struct page *page = NULL; 8769e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, start - 1, &page, SGP_READ); 877bda97eabSHugh Dickins if (page) { 87809cbfeafSKirill A. Shutemov unsigned int top = PAGE_SIZE; 87983e4fa9cSHugh Dickins if (start > end) { 88083e4fa9cSHugh Dickins top = partial_end; 88183e4fa9cSHugh Dickins partial_end = 0; 88283e4fa9cSHugh Dickins } 88383e4fa9cSHugh Dickins zero_user_segment(page, partial_start, top); 884bda97eabSHugh Dickins set_page_dirty(page); 885bda97eabSHugh Dickins unlock_page(page); 88609cbfeafSKirill A. Shutemov put_page(page); 887bda97eabSHugh Dickins } 888bda97eabSHugh Dickins } 88983e4fa9cSHugh Dickins if (partial_end) { 89083e4fa9cSHugh Dickins struct page *page = NULL; 8919e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, end, &page, SGP_READ); 89283e4fa9cSHugh Dickins if (page) { 89383e4fa9cSHugh Dickins zero_user_segment(page, 0, partial_end); 89483e4fa9cSHugh Dickins set_page_dirty(page); 89583e4fa9cSHugh Dickins unlock_page(page); 89609cbfeafSKirill A. Shutemov put_page(page); 89783e4fa9cSHugh Dickins } 89883e4fa9cSHugh Dickins } 89983e4fa9cSHugh Dickins if (start >= end) 90083e4fa9cSHugh Dickins return; 901bda97eabSHugh Dickins 902bda97eabSHugh Dickins index = start; 903b1a36650SHugh Dickins while (index < end) { 904bda97eabSHugh Dickins cond_resched(); 9050cd6144aSJohannes Weiner 9060cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 90783e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 9087a5d0fbbSHugh Dickins pvec.pages, indices); 9097a5d0fbbSHugh Dickins if (!pvec.nr) { 910b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 911b1a36650SHugh Dickins if (index == start || end != -1) 912bda97eabSHugh Dickins break; 913b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 914bda97eabSHugh Dickins index = start; 915bda97eabSHugh Dickins continue; 916bda97eabSHugh Dickins } 917bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 918bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 919bda97eabSHugh Dickins 9207a5d0fbbSHugh Dickins index = indices[i]; 92183e4fa9cSHugh Dickins if (index >= end) 922bda97eabSHugh Dickins break; 923bda97eabSHugh Dickins 9247a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 9251635f6a7SHugh Dickins if (unfalloc) 9261635f6a7SHugh Dickins continue; 927b1a36650SHugh Dickins if (shmem_free_swap(mapping, index, page)) { 928b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 929b1a36650SHugh Dickins index--; 930b1a36650SHugh Dickins break; 931b1a36650SHugh Dickins } 932b1a36650SHugh Dickins nr_swaps_freed++; 9337a5d0fbbSHugh Dickins continue; 9347a5d0fbbSHugh Dickins } 9357a5d0fbbSHugh Dickins 936bda97eabSHugh Dickins lock_page(page); 937800d8c63SKirill A. Shutemov 938800d8c63SKirill A. Shutemov if (PageTransTail(page)) { 939800d8c63SKirill A. Shutemov /* Middle of THP: zero out the page */ 940800d8c63SKirill A. Shutemov clear_highpage(page); 941800d8c63SKirill A. Shutemov unlock_page(page); 942800d8c63SKirill A. Shutemov /* 943800d8c63SKirill A. Shutemov * Partial thp truncate due 'start' in middle 944800d8c63SKirill A. Shutemov * of THP: don't need to look on these pages 945800d8c63SKirill A. Shutemov * again on !pvec.nr restart. 946800d8c63SKirill A. Shutemov */ 947800d8c63SKirill A. Shutemov if (index != round_down(end, HPAGE_PMD_NR)) 948800d8c63SKirill A. Shutemov start++; 949800d8c63SKirill A. Shutemov continue; 950800d8c63SKirill A. Shutemov } else if (PageTransHuge(page)) { 951800d8c63SKirill A. Shutemov if (index == round_down(end, HPAGE_PMD_NR)) { 952800d8c63SKirill A. Shutemov /* 953800d8c63SKirill A. Shutemov * Range ends in the middle of THP: 954800d8c63SKirill A. Shutemov * zero out the page 955800d8c63SKirill A. Shutemov */ 956800d8c63SKirill A. Shutemov clear_highpage(page); 957800d8c63SKirill A. Shutemov unlock_page(page); 958800d8c63SKirill A. Shutemov continue; 959800d8c63SKirill A. Shutemov } 960800d8c63SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 961800d8c63SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 962800d8c63SKirill A. Shutemov } 963800d8c63SKirill A. Shutemov 9641635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 965800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 966800d8c63SKirill A. Shutemov if (page_mapping(page) == mapping) { 967309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 968bda97eabSHugh Dickins truncate_inode_page(mapping, page); 969b1a36650SHugh Dickins } else { 970b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 971b1a36650SHugh Dickins unlock_page(page); 972b1a36650SHugh Dickins index--; 973b1a36650SHugh Dickins break; 9747a5d0fbbSHugh Dickins } 9751635f6a7SHugh Dickins } 976bda97eabSHugh Dickins unlock_page(page); 977bda97eabSHugh Dickins } 9780cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 97924513264SHugh Dickins pagevec_release(&pvec); 980bda97eabSHugh Dickins index++; 981bda97eabSHugh Dickins } 98294c1e62dSHugh Dickins 9834595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 9847a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 9851da177e4SLinus Torvalds shmem_recalc_inode(inode); 9864595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 9871635f6a7SHugh Dickins } 9881da177e4SLinus Torvalds 9891635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 9901635f6a7SHugh Dickins { 9911635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 992078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 9931da177e4SLinus Torvalds } 99494c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 9951da177e4SLinus Torvalds 996a528d35eSDavid Howells static int shmem_getattr(const struct path *path, struct kstat *stat, 997a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 99844a30220SYu Zhao { 999a528d35eSDavid Howells struct inode *inode = path->dentry->d_inode; 100044a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 100189fdcd26SYang Shi struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb); 100244a30220SYu Zhao 1003d0424c42SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) { 10044595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 100544a30220SYu Zhao shmem_recalc_inode(inode); 10064595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1007d0424c42SHugh Dickins } 100844a30220SYu Zhao generic_fillattr(inode, stat); 100989fdcd26SYang Shi 101089fdcd26SYang Shi if (is_huge_enabled(sb_info)) 101189fdcd26SYang Shi stat->blksize = HPAGE_PMD_SIZE; 101289fdcd26SYang Shi 101344a30220SYu Zhao return 0; 101444a30220SYu Zhao } 101544a30220SYu Zhao 101694c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 10171da177e4SLinus Torvalds { 101875c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 101940e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 1020779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 10211da177e4SLinus Torvalds int error; 10221da177e4SLinus Torvalds 102331051c85SJan Kara error = setattr_prepare(dentry, attr); 1024db78b877SChristoph Hellwig if (error) 1025db78b877SChristoph Hellwig return error; 1026db78b877SChristoph Hellwig 102794c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 102894c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 102994c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 10303889e6e7Snpiggin@suse.de 103140e041a2SDavid Herrmann /* protected by i_mutex */ 103240e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 103340e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 103440e041a2SDavid Herrmann return -EPERM; 103540e041a2SDavid Herrmann 103694c1e62dSHugh Dickins if (newsize != oldsize) { 103777142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 103877142517SKonstantin Khlebnikov oldsize, newsize); 103977142517SKonstantin Khlebnikov if (error) 104077142517SKonstantin Khlebnikov return error; 104194c1e62dSHugh Dickins i_size_write(inode, newsize); 1042078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 104394c1e62dSHugh Dickins } 1044afa2db2fSJosef Bacik if (newsize <= oldsize) { 104594c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 1046d0424c42SHugh Dickins if (oldsize > holebegin) 1047d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1048d0424c42SHugh Dickins holebegin, 0, 1); 1049d0424c42SHugh Dickins if (info->alloced) 1050d0424c42SHugh Dickins shmem_truncate_range(inode, 1051d0424c42SHugh Dickins newsize, (loff_t)-1); 105294c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 1053d0424c42SHugh Dickins if (oldsize > holebegin) 1054d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1055d0424c42SHugh Dickins holebegin, 0, 1); 1056779750d2SKirill A. Shutemov 1057779750d2SKirill A. Shutemov /* 1058779750d2SKirill A. Shutemov * Part of the huge page can be beyond i_size: subject 1059779750d2SKirill A. Shutemov * to shrink under memory pressure. 1060779750d2SKirill A. Shutemov */ 1061779750d2SKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 1062779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1063d041353dSCong Wang /* 1064d041353dSCong Wang * _careful to defend against unlocked access to 1065d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1066d041353dSCong Wang */ 1067d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1068779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1069779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1070779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1071779750d2SKirill A. Shutemov } 1072779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1073779750d2SKirill A. Shutemov } 107494c1e62dSHugh Dickins } 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds 10776a1a90adSChristoph Hellwig setattr_copy(inode, attr); 1078db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 1079feda821eSChristoph Hellwig error = posix_acl_chmod(inode, inode->i_mode); 10801da177e4SLinus Torvalds return error; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 10841da177e4SLinus Torvalds { 10851da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 1086779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 10871da177e4SLinus Torvalds 10883889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 10891da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 10901da177e4SLinus Torvalds inode->i_size = 0; 10913889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 1092779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1093779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1094779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1095779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 1096779750d2SKirill A. Shutemov sbinfo->shrinklist_len--; 1097779750d2SKirill A. Shutemov } 1098779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1099779750d2SKirill A. Shutemov } 11001da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 1101cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 11021da177e4SLinus Torvalds list_del_init(&info->swaplist); 1103cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 11041da177e4SLinus Torvalds } 11053ed47db3SAl Viro } 1106b09e0fa4SEric Paris 110738f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 11080f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 11095b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 1110dbd5768fSJan Kara clear_inode(inode); 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 1113478922e2SMatthew Wilcox static unsigned long find_swap_entry(struct radix_tree_root *root, void *item) 1114478922e2SMatthew Wilcox { 1115478922e2SMatthew Wilcox struct radix_tree_iter iter; 11165b9c98f3SMike Kravetz void __rcu **slot; 1117478922e2SMatthew Wilcox unsigned long found = -1; 1118478922e2SMatthew Wilcox unsigned int checked = 0; 1119478922e2SMatthew Wilcox 1120478922e2SMatthew Wilcox rcu_read_lock(); 1121478922e2SMatthew Wilcox radix_tree_for_each_slot(slot, root, &iter, 0) { 11225b9c98f3SMike Kravetz void *entry = radix_tree_deref_slot(slot); 11235b9c98f3SMike Kravetz 11245b9c98f3SMike Kravetz if (radix_tree_deref_retry(entry)) { 11255b9c98f3SMike Kravetz slot = radix_tree_iter_retry(&iter); 11265b9c98f3SMike Kravetz continue; 11275b9c98f3SMike Kravetz } 11285b9c98f3SMike Kravetz if (entry == item) { 1129478922e2SMatthew Wilcox found = iter.index; 1130478922e2SMatthew Wilcox break; 1131478922e2SMatthew Wilcox } 1132478922e2SMatthew Wilcox checked++; 1133478922e2SMatthew Wilcox if ((checked % 4096) != 0) 1134478922e2SMatthew Wilcox continue; 1135478922e2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 1136478922e2SMatthew Wilcox cond_resched_rcu(); 1137478922e2SMatthew Wilcox } 1138478922e2SMatthew Wilcox 1139478922e2SMatthew Wilcox rcu_read_unlock(); 1140478922e2SMatthew Wilcox return found; 1141478922e2SMatthew Wilcox } 1142478922e2SMatthew Wilcox 114346f65ec1SHugh Dickins /* 114446f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 114546f65ec1SHugh Dickins */ 114641ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 1147bde05d1cSHugh Dickins swp_entry_t swap, struct page **pagep) 11481da177e4SLinus Torvalds { 1149285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 115046f65ec1SHugh Dickins void *radswap; 115141ffe5d5SHugh Dickins pgoff_t index; 1152bde05d1cSHugh Dickins gfp_t gfp; 1153bde05d1cSHugh Dickins int error = 0; 11541da177e4SLinus Torvalds 115546f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 1156b93b0163SMatthew Wilcox index = find_swap_entry(&mapping->i_pages, radswap); 115746f65ec1SHugh Dickins if (index == -1) 115800501b53SJohannes Weiner return -EAGAIN; /* tell shmem_unuse we found nothing */ 11592e0e26c7SHugh Dickins 11601b1b32f2SHugh Dickins /* 11611b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 11621f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 11631b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 1164285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 11651b1b32f2SHugh Dickins */ 11661b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 11672e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 11682e0e26c7SHugh Dickins 1169bde05d1cSHugh Dickins gfp = mapping_gfp_mask(mapping); 1170bde05d1cSHugh Dickins if (shmem_should_replace_page(*pagep, gfp)) { 1171bde05d1cSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1172bde05d1cSHugh Dickins error = shmem_replace_page(pagep, gfp, info, index); 1173bde05d1cSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 1174bde05d1cSHugh Dickins /* 1175bde05d1cSHugh Dickins * We needed to drop mutex to make that restrictive page 11760142ef6cSHugh Dickins * allocation, but the inode might have been freed while we 11770142ef6cSHugh Dickins * dropped it: although a racing shmem_evict_inode() cannot 11780142ef6cSHugh Dickins * complete without emptying the radix_tree, our page lock 11790142ef6cSHugh Dickins * on this swapcache page is not enough to prevent that - 11800142ef6cSHugh Dickins * free_swap_and_cache() of our swap entry will only 11810142ef6cSHugh Dickins * trylock_page(), removing swap from radix_tree whatever. 11820142ef6cSHugh Dickins * 11830142ef6cSHugh Dickins * We must not proceed to shmem_add_to_page_cache() if the 11840142ef6cSHugh Dickins * inode has been freed, but of course we cannot rely on 11850142ef6cSHugh Dickins * inode or mapping or info to check that. However, we can 11860142ef6cSHugh Dickins * safely check if our swap entry is still in use (and here 11870142ef6cSHugh Dickins * it can't have got reused for another page): if it's still 11880142ef6cSHugh Dickins * in use, then the inode cannot have been freed yet, and we 11890142ef6cSHugh Dickins * can safely proceed (if it's no longer in use, that tells 11900142ef6cSHugh Dickins * nothing about the inode, but we don't need to unuse swap). 1191bde05d1cSHugh Dickins */ 1192bde05d1cSHugh Dickins if (!page_swapcount(*pagep)) 1193bde05d1cSHugh Dickins error = -ENOENT; 1194bde05d1cSHugh Dickins } 1195bde05d1cSHugh Dickins 1196d13d1443SKAMEZAWA Hiroyuki /* 1197778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 1198778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 1199778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 1200d13d1443SKAMEZAWA Hiroyuki */ 1201bde05d1cSHugh Dickins if (!error) 1202bde05d1cSHugh Dickins error = shmem_add_to_page_cache(*pagep, mapping, index, 1203fed400a1SWang Sheng-Hui radswap); 120448f170fbSHugh Dickins if (error != -ENOMEM) { 120546f65ec1SHugh Dickins /* 120646f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 120746f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 120846f65ec1SHugh Dickins */ 1209bde05d1cSHugh Dickins delete_from_swap_cache(*pagep); 1210bde05d1cSHugh Dickins set_page_dirty(*pagep); 121146f65ec1SHugh Dickins if (!error) { 12124595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1213285b2c4fSHugh Dickins info->swapped--; 12144595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 121541ffe5d5SHugh Dickins swap_free(swap); 121646f65ec1SHugh Dickins } 12171da177e4SLinus Torvalds } 12182e0e26c7SHugh Dickins return error; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds /* 122246f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 12231da177e4SLinus Torvalds */ 122441ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 12251da177e4SLinus Torvalds { 122641ffe5d5SHugh Dickins struct list_head *this, *next; 12271da177e4SLinus Torvalds struct shmem_inode_info *info; 122800501b53SJohannes Weiner struct mem_cgroup *memcg; 1229bde05d1cSHugh Dickins int error = 0; 1230bde05d1cSHugh Dickins 1231bde05d1cSHugh Dickins /* 1232bde05d1cSHugh Dickins * There's a faint possibility that swap page was replaced before 12330142ef6cSHugh Dickins * caller locked it: caller will come back later with the right page. 1234bde05d1cSHugh Dickins */ 12350142ef6cSHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) 1236bde05d1cSHugh Dickins goto out; 1237778dd893SHugh Dickins 1238778dd893SHugh Dickins /* 1239778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 1240778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 1241778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 1242778dd893SHugh Dickins */ 12432cf85583STejun Heo error = mem_cgroup_try_charge_delay(page, current->mm, GFP_KERNEL, 12442cf85583STejun Heo &memcg, false); 1245778dd893SHugh Dickins if (error) 1246778dd893SHugh Dickins goto out; 124746f65ec1SHugh Dickins /* No radix_tree_preload: swap entry keeps a place for page in tree */ 124800501b53SJohannes Weiner error = -EAGAIN; 12491da177e4SLinus Torvalds 1250cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 125141ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 125241ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 1253285b2c4fSHugh Dickins if (info->swapped) 125400501b53SJohannes Weiner error = shmem_unuse_inode(info, swap, &page); 12556922c0c7SHugh Dickins else 12566922c0c7SHugh Dickins list_del_init(&info->swaplist); 1257cb5f7b9aSHugh Dickins cond_resched(); 125800501b53SJohannes Weiner if (error != -EAGAIN) 1259778dd893SHugh Dickins break; 126000501b53SJohannes Weiner /* found nothing in this: move on to search the next */ 12611da177e4SLinus Torvalds } 1262cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1263778dd893SHugh Dickins 126400501b53SJohannes Weiner if (error) { 126500501b53SJohannes Weiner if (error != -ENOMEM) 126600501b53SJohannes Weiner error = 0; 1267f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 126800501b53SJohannes Weiner } else 1269f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 1270778dd893SHugh Dickins out: 1271aaa46865SHugh Dickins unlock_page(page); 127209cbfeafSKirill A. Shutemov put_page(page); 1273778dd893SHugh Dickins return error; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds /* 12771da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 12781da177e4SLinus Torvalds */ 12791da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 12801da177e4SLinus Torvalds { 12811da177e4SLinus Torvalds struct shmem_inode_info *info; 12821da177e4SLinus Torvalds struct address_space *mapping; 12831da177e4SLinus Torvalds struct inode *inode; 12846922c0c7SHugh Dickins swp_entry_t swap; 12856922c0c7SHugh Dickins pgoff_t index; 12861da177e4SLinus Torvalds 1287800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 12881da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 12891da177e4SLinus Torvalds mapping = page->mapping; 12901da177e4SLinus Torvalds index = page->index; 12911da177e4SLinus Torvalds inode = mapping->host; 12921da177e4SLinus Torvalds info = SHMEM_I(inode); 12931da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 12941da177e4SLinus Torvalds goto redirty; 1295d9fe526aSHugh Dickins if (!total_swap_pages) 12961da177e4SLinus Torvalds goto redirty; 12971da177e4SLinus Torvalds 1298d9fe526aSHugh Dickins /* 129997b713baSChristoph Hellwig * Our capabilities prevent regular writeback or sync from ever calling 130097b713baSChristoph Hellwig * shmem_writepage; but a stacking filesystem might use ->writepage of 130197b713baSChristoph Hellwig * its underlying filesystem, in which case tmpfs should write out to 130297b713baSChristoph Hellwig * swap only in response to memory pressure, and not for the writeback 130397b713baSChristoph Hellwig * threads or sync. 1304d9fe526aSHugh Dickins */ 130548f170fbSHugh Dickins if (!wbc->for_reclaim) { 130648f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 130748f170fbSHugh Dickins goto redirty; 130848f170fbSHugh Dickins } 13091635f6a7SHugh Dickins 13101635f6a7SHugh Dickins /* 13111635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 13121635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 13131635f6a7SHugh Dickins * fallocated page arriving here is now to initialize it and write it. 13141aac1400SHugh Dickins * 13151aac1400SHugh Dickins * That's okay for a page already fallocated earlier, but if we have 13161aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 13171aac1400SHugh Dickins * of this page in case we have to undo it, and (b) it may not be a 13181aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 13191aac1400SHugh Dickins * reactivate the page, and let shmem_fallocate() quit when too many. 13201635f6a7SHugh Dickins */ 13211635f6a7SHugh Dickins if (!PageUptodate(page)) { 13221aac1400SHugh Dickins if (inode->i_private) { 13231aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 13241aac1400SHugh Dickins spin_lock(&inode->i_lock); 13251aac1400SHugh Dickins shmem_falloc = inode->i_private; 13261aac1400SHugh Dickins if (shmem_falloc && 13278e205f77SHugh Dickins !shmem_falloc->waitq && 13281aac1400SHugh Dickins index >= shmem_falloc->start && 13291aac1400SHugh Dickins index < shmem_falloc->next) 13301aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 13311aac1400SHugh Dickins else 13321aac1400SHugh Dickins shmem_falloc = NULL; 13331aac1400SHugh Dickins spin_unlock(&inode->i_lock); 13341aac1400SHugh Dickins if (shmem_falloc) 13351aac1400SHugh Dickins goto redirty; 13361aac1400SHugh Dickins } 13371635f6a7SHugh Dickins clear_highpage(page); 13381635f6a7SHugh Dickins flush_dcache_page(page); 13391635f6a7SHugh Dickins SetPageUptodate(page); 13401635f6a7SHugh Dickins } 13411635f6a7SHugh Dickins 134238d8b4e6SHuang Ying swap = get_swap_page(page); 134348f170fbSHugh Dickins if (!swap.val) 134448f170fbSHugh Dickins goto redirty; 1345d9fe526aSHugh Dickins 1346b1dea800SHugh Dickins /* 1347b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 13486922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 13496922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 1350b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 13516922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 13526922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 1353b1dea800SHugh Dickins */ 1354b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 135505bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 135605bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 1357b1dea800SHugh Dickins 135848f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 13594595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1360267a4c76SHugh Dickins shmem_recalc_inode(inode); 1361267a4c76SHugh Dickins info->swapped++; 13624595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1363267a4c76SHugh Dickins 1364aaa46865SHugh Dickins swap_shmem_alloc(swap); 13656922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 13666922c0c7SHugh Dickins 13676922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1368d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 13699fab5619SHugh Dickins swap_writepage(page, wbc); 13701da177e4SLinus Torvalds return 0; 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds 13736922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 137475f6d6d2SMinchan Kim put_swap_page(page, swap); 13751da177e4SLinus Torvalds redirty: 13761da177e4SLinus Torvalds set_page_dirty(page); 1377d9fe526aSHugh Dickins if (wbc->for_reclaim) 1378d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 1379d9fe526aSHugh Dickins unlock_page(page); 1380d9fe526aSHugh Dickins return 0; 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 138375edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 138471fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1385680d794bSakpm@linux-foundation.org { 1386680d794bSakpm@linux-foundation.org char buffer[64]; 1387680d794bSakpm@linux-foundation.org 138871fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1389095f1fc4SLee Schermerhorn return; /* show nothing */ 1390095f1fc4SLee Schermerhorn 1391a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 1392095f1fc4SLee Schermerhorn 1393095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1394680d794bSakpm@linux-foundation.org } 139571fe804bSLee Schermerhorn 139671fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 139771fe804bSLee Schermerhorn { 139871fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 139971fe804bSLee Schermerhorn if (sbinfo->mpol) { 140071fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 140171fe804bSLee Schermerhorn mpol = sbinfo->mpol; 140271fe804bSLee Schermerhorn mpol_get(mpol); 140371fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 140471fe804bSLee Schermerhorn } 140571fe804bSLee Schermerhorn return mpol; 140671fe804bSLee Schermerhorn } 140775edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 140875edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 140975edd345SHugh Dickins { 141075edd345SHugh Dickins } 141175edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 141275edd345SHugh Dickins { 141375edd345SHugh Dickins return NULL; 141475edd345SHugh Dickins } 141575edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 141675edd345SHugh Dickins #ifndef CONFIG_NUMA 141775edd345SHugh Dickins #define vm_policy vm_private_data 141875edd345SHugh Dickins #endif 1419680d794bSakpm@linux-foundation.org 1420800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma, 1421800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1422800d8c63SKirill A. Shutemov { 1423800d8c63SKirill A. Shutemov /* Create a pseudo vma that just contains the policy */ 1424daa28075SKirill A. Shutemov memset(vma, 0, sizeof(*vma)); 14252c4541e2SKirill A. Shutemov vma_init(vma, NULL); 1426800d8c63SKirill A. Shutemov /* Bias interleave by inode number to distribute better across nodes */ 1427800d8c63SKirill A. Shutemov vma->vm_pgoff = index + info->vfs_inode.i_ino; 1428800d8c63SKirill A. Shutemov vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); 1429800d8c63SKirill A. Shutemov } 1430800d8c63SKirill A. Shutemov 1431800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) 1432800d8c63SKirill A. Shutemov { 1433800d8c63SKirill A. Shutemov /* Drop reference taken by mpol_shared_policy_lookup() */ 1434800d8c63SKirill A. Shutemov mpol_cond_put(vma->vm_policy); 1435800d8c63SKirill A. Shutemov } 1436800d8c63SKirill A. Shutemov 143741ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 143841ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 14391da177e4SLinus Torvalds { 14401da177e4SLinus Torvalds struct vm_area_struct pvma; 144118a2f371SMel Gorman struct page *page; 1442e9e9b7ecSMinchan Kim struct vm_fault vmf; 14431da177e4SLinus Torvalds 1444800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1445e9e9b7ecSMinchan Kim vmf.vma = &pvma; 1446e9e9b7ecSMinchan Kim vmf.address = 0; 1447e9e9b7ecSMinchan Kim page = swap_cluster_readahead(swap, gfp, &vmf); 1448800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 144918a2f371SMel Gorman 1450800d8c63SKirill A. Shutemov return page; 1451800d8c63SKirill A. Shutemov } 145218a2f371SMel Gorman 1453800d8c63SKirill A. Shutemov static struct page *shmem_alloc_hugepage(gfp_t gfp, 1454800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1455800d8c63SKirill A. Shutemov { 1456800d8c63SKirill A. Shutemov struct vm_area_struct pvma; 1457800d8c63SKirill A. Shutemov struct inode *inode = &info->vfs_inode; 1458800d8c63SKirill A. Shutemov struct address_space *mapping = inode->i_mapping; 14594620a06eSGeert Uytterhoeven pgoff_t idx, hindex; 1460800d8c63SKirill A. Shutemov void __rcu **results; 1461800d8c63SKirill A. Shutemov struct page *page; 1462800d8c63SKirill A. Shutemov 1463e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1464800d8c63SKirill A. Shutemov return NULL; 1465800d8c63SKirill A. Shutemov 14664620a06eSGeert Uytterhoeven hindex = round_down(index, HPAGE_PMD_NR); 1467800d8c63SKirill A. Shutemov rcu_read_lock(); 1468b93b0163SMatthew Wilcox if (radix_tree_gang_lookup_slot(&mapping->i_pages, &results, &idx, 1469800d8c63SKirill A. Shutemov hindex, 1) && idx < hindex + HPAGE_PMD_NR) { 1470800d8c63SKirill A. Shutemov rcu_read_unlock(); 1471800d8c63SKirill A. Shutemov return NULL; 1472800d8c63SKirill A. Shutemov } 1473800d8c63SKirill A. Shutemov rcu_read_unlock(); 1474800d8c63SKirill A. Shutemov 1475800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, hindex); 1476800d8c63SKirill A. Shutemov page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN, 1477800d8c63SKirill A. Shutemov HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true); 1478800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 1479800d8c63SKirill A. Shutemov if (page) 1480800d8c63SKirill A. Shutemov prep_transhuge_page(page); 148118a2f371SMel Gorman return page; 148218a2f371SMel Gorman } 148318a2f371SMel Gorman 148418a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp, 148518a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 148618a2f371SMel Gorman { 148718a2f371SMel Gorman struct vm_area_struct pvma; 148818a2f371SMel Gorman struct page *page; 148918a2f371SMel Gorman 1490800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1491800d8c63SKirill A. Shutemov page = alloc_page_vma(gfp, &pvma, 0); 1492800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 149318a2f371SMel Gorman 1494800d8c63SKirill A. Shutemov return page; 1495800d8c63SKirill A. Shutemov } 1496800d8c63SKirill A. Shutemov 1497800d8c63SKirill A. Shutemov static struct page *shmem_alloc_and_acct_page(gfp_t gfp, 14980f079694SMike Rapoport struct inode *inode, 1499800d8c63SKirill A. Shutemov pgoff_t index, bool huge) 1500800d8c63SKirill A. Shutemov { 15010f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 1502800d8c63SKirill A. Shutemov struct page *page; 1503800d8c63SKirill A. Shutemov int nr; 1504800d8c63SKirill A. Shutemov int err = -ENOSPC; 1505800d8c63SKirill A. Shutemov 1506e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1507800d8c63SKirill A. Shutemov huge = false; 1508800d8c63SKirill A. Shutemov nr = huge ? HPAGE_PMD_NR : 1; 1509800d8c63SKirill A. Shutemov 15100f079694SMike Rapoport if (!shmem_inode_acct_block(inode, nr)) 1511800d8c63SKirill A. Shutemov goto failed; 1512800d8c63SKirill A. Shutemov 1513800d8c63SKirill A. Shutemov if (huge) 1514800d8c63SKirill A. Shutemov page = shmem_alloc_hugepage(gfp, info, index); 1515800d8c63SKirill A. Shutemov else 1516800d8c63SKirill A. Shutemov page = shmem_alloc_page(gfp, info, index); 151775edd345SHugh Dickins if (page) { 151875edd345SHugh Dickins __SetPageLocked(page); 151975edd345SHugh Dickins __SetPageSwapBacked(page); 1520800d8c63SKirill A. Shutemov return page; 152175edd345SHugh Dickins } 152218a2f371SMel Gorman 1523800d8c63SKirill A. Shutemov err = -ENOMEM; 15240f079694SMike Rapoport shmem_inode_unacct_blocks(inode, nr); 1525800d8c63SKirill A. Shutemov failed: 1526800d8c63SKirill A. Shutemov return ERR_PTR(err); 15271da177e4SLinus Torvalds } 152871fe804bSLee Schermerhorn 15291da177e4SLinus Torvalds /* 1530bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 1531bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 1532bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 1533bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 1534bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 1535bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 1536bde05d1cSHugh Dickins * 1537bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 1538bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 1539bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 1540bde05d1cSHugh Dickins */ 1541bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp) 1542bde05d1cSHugh Dickins { 1543bde05d1cSHugh Dickins return page_zonenum(page) > gfp_zone(gfp); 1544bde05d1cSHugh Dickins } 1545bde05d1cSHugh Dickins 1546bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 1547bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 1548bde05d1cSHugh Dickins { 1549bde05d1cSHugh Dickins struct page *oldpage, *newpage; 1550bde05d1cSHugh Dickins struct address_space *swap_mapping; 1551bde05d1cSHugh Dickins pgoff_t swap_index; 1552bde05d1cSHugh Dickins int error; 1553bde05d1cSHugh Dickins 1554bde05d1cSHugh Dickins oldpage = *pagep; 1555bde05d1cSHugh Dickins swap_index = page_private(oldpage); 1556bde05d1cSHugh Dickins swap_mapping = page_mapping(oldpage); 1557bde05d1cSHugh Dickins 1558bde05d1cSHugh Dickins /* 1559bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1560bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1561bde05d1cSHugh Dickins */ 1562bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1563bde05d1cSHugh Dickins newpage = shmem_alloc_page(gfp, info, index); 1564bde05d1cSHugh Dickins if (!newpage) 1565bde05d1cSHugh Dickins return -ENOMEM; 1566bde05d1cSHugh Dickins 156709cbfeafSKirill A. Shutemov get_page(newpage); 1568bde05d1cSHugh Dickins copy_highpage(newpage, oldpage); 15690142ef6cSHugh Dickins flush_dcache_page(newpage); 1570bde05d1cSHugh Dickins 15719956edf3SHugh Dickins __SetPageLocked(newpage); 15729956edf3SHugh Dickins __SetPageSwapBacked(newpage); 1573bde05d1cSHugh Dickins SetPageUptodate(newpage); 1574bde05d1cSHugh Dickins set_page_private(newpage, swap_index); 1575bde05d1cSHugh Dickins SetPageSwapCache(newpage); 1576bde05d1cSHugh Dickins 1577bde05d1cSHugh Dickins /* 1578bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1579bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1580bde05d1cSHugh Dickins */ 1581b93b0163SMatthew Wilcox xa_lock_irq(&swap_mapping->i_pages); 1582bde05d1cSHugh Dickins error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage, 1583bde05d1cSHugh Dickins newpage); 15840142ef6cSHugh Dickins if (!error) { 158511fb9989SMel Gorman __inc_node_page_state(newpage, NR_FILE_PAGES); 158611fb9989SMel Gorman __dec_node_page_state(oldpage, NR_FILE_PAGES); 15870142ef6cSHugh Dickins } 1588b93b0163SMatthew Wilcox xa_unlock_irq(&swap_mapping->i_pages); 1589bde05d1cSHugh Dickins 15900142ef6cSHugh Dickins if (unlikely(error)) { 15910142ef6cSHugh Dickins /* 15920142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 15930142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 15940142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 15950142ef6cSHugh Dickins */ 15960142ef6cSHugh Dickins oldpage = newpage; 15970142ef6cSHugh Dickins } else { 15986a93ca8fSJohannes Weiner mem_cgroup_migrate(oldpage, newpage); 1599bde05d1cSHugh Dickins lru_cache_add_anon(newpage); 16000142ef6cSHugh Dickins *pagep = newpage; 16010142ef6cSHugh Dickins } 1602bde05d1cSHugh Dickins 1603bde05d1cSHugh Dickins ClearPageSwapCache(oldpage); 1604bde05d1cSHugh Dickins set_page_private(oldpage, 0); 1605bde05d1cSHugh Dickins 1606bde05d1cSHugh Dickins unlock_page(oldpage); 160709cbfeafSKirill A. Shutemov put_page(oldpage); 160809cbfeafSKirill A. Shutemov put_page(oldpage); 16090142ef6cSHugh Dickins return error; 1610bde05d1cSHugh Dickins } 1611bde05d1cSHugh Dickins 1612bde05d1cSHugh Dickins /* 161368da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 16141da177e4SLinus Torvalds * 16151da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 16161da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 16179e18eb29SAndres Lagar-Cavilla * entry since a page cannot live in both the swap and page cache. 16189e18eb29SAndres Lagar-Cavilla * 16199e18eb29SAndres Lagar-Cavilla * fault_mm and fault_type are only supplied by shmem_fault: 16209e18eb29SAndres Lagar-Cavilla * otherwise they are NULL. 16211da177e4SLinus Torvalds */ 162241ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 16239e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, gfp_t gfp, 1624cfda0526SMike Rapoport struct vm_area_struct *vma, struct vm_fault *vmf, int *fault_type) 16251da177e4SLinus Torvalds { 16261da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 162723f919d4SArnd Bergmann struct shmem_inode_info *info = SHMEM_I(inode); 16281da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 16299e18eb29SAndres Lagar-Cavilla struct mm_struct *charge_mm; 163000501b53SJohannes Weiner struct mem_cgroup *memcg; 163127ab7006SHugh Dickins struct page *page; 16321da177e4SLinus Torvalds swp_entry_t swap; 1633657e3038SKirill A. Shutemov enum sgp_type sgp_huge = sgp; 1634800d8c63SKirill A. Shutemov pgoff_t hindex = index; 16351da177e4SLinus Torvalds int error; 163654af6042SHugh Dickins int once = 0; 16371635f6a7SHugh Dickins int alloced = 0; 16381da177e4SLinus Torvalds 163909cbfeafSKirill A. Shutemov if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 16401da177e4SLinus Torvalds return -EFBIG; 1641657e3038SKirill A. Shutemov if (sgp == SGP_NOHUGE || sgp == SGP_HUGE) 1642657e3038SKirill A. Shutemov sgp = SGP_CACHE; 16431da177e4SLinus Torvalds repeat: 164454af6042SHugh Dickins swap.val = 0; 16450cd6144aSJohannes Weiner page = find_lock_entry(mapping, index); 164654af6042SHugh Dickins if (radix_tree_exceptional_entry(page)) { 164754af6042SHugh Dickins swap = radix_to_swp_entry(page); 164854af6042SHugh Dickins page = NULL; 164954af6042SHugh Dickins } 165054af6042SHugh Dickins 165175edd345SHugh Dickins if (sgp <= SGP_CACHE && 165209cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 165354af6042SHugh Dickins error = -EINVAL; 1654267a4c76SHugh Dickins goto unlock; 165554af6042SHugh Dickins } 165654af6042SHugh Dickins 165766d2f4d2SHugh Dickins if (page && sgp == SGP_WRITE) 165866d2f4d2SHugh Dickins mark_page_accessed(page); 165966d2f4d2SHugh Dickins 16601635f6a7SHugh Dickins /* fallocated page? */ 16611635f6a7SHugh Dickins if (page && !PageUptodate(page)) { 16621635f6a7SHugh Dickins if (sgp != SGP_READ) 16631635f6a7SHugh Dickins goto clear; 16641635f6a7SHugh Dickins unlock_page(page); 166509cbfeafSKirill A. Shutemov put_page(page); 16661635f6a7SHugh Dickins page = NULL; 16671635f6a7SHugh Dickins } 166854af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 166954af6042SHugh Dickins *pagep = page; 167054af6042SHugh Dickins return 0; 167127ab7006SHugh Dickins } 167227ab7006SHugh Dickins 1673b409f9fcSHugh Dickins /* 167454af6042SHugh Dickins * Fast cache lookup did not find it: 167554af6042SHugh Dickins * bring it back from swap or allocate. 1676b409f9fcSHugh Dickins */ 167754af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 1678cfda0526SMike Rapoport charge_mm = vma ? vma->vm_mm : current->mm; 167927ab7006SHugh Dickins 16801da177e4SLinus Torvalds if (swap.val) { 16811da177e4SLinus Torvalds /* Look it up and read it in.. */ 1682ec560175SHuang Ying page = lookup_swap_cache(swap, NULL, 0); 168327ab7006SHugh Dickins if (!page) { 16849e18eb29SAndres Lagar-Cavilla /* Or update major stats only when swapin succeeds?? */ 16859e18eb29SAndres Lagar-Cavilla if (fault_type) { 168668da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 16879e18eb29SAndres Lagar-Cavilla count_vm_event(PGMAJFAULT); 16882262185cSRoman Gushchin count_memcg_event_mm(charge_mm, PGMAJFAULT); 16899e18eb29SAndres Lagar-Cavilla } 16909e18eb29SAndres Lagar-Cavilla /* Here we actually start the io */ 169141ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 169227ab7006SHugh Dickins if (!page) { 16931da177e4SLinus Torvalds error = -ENOMEM; 169454af6042SHugh Dickins goto failed; 1695285b2c4fSHugh Dickins } 16961da177e4SLinus Torvalds } 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 169954af6042SHugh Dickins lock_page(page); 17000142ef6cSHugh Dickins if (!PageSwapCache(page) || page_private(page) != swap.val || 1701d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1702bde05d1cSHugh Dickins error = -EEXIST; /* try again */ 1703d1899228SHugh Dickins goto unlock; 1704bde05d1cSHugh Dickins } 170527ab7006SHugh Dickins if (!PageUptodate(page)) { 17061da177e4SLinus Torvalds error = -EIO; 170754af6042SHugh Dickins goto failed; 170854af6042SHugh Dickins } 170954af6042SHugh Dickins wait_on_page_writeback(page); 171054af6042SHugh Dickins 1711bde05d1cSHugh Dickins if (shmem_should_replace_page(page, gfp)) { 1712bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 1713bde05d1cSHugh Dickins if (error) 171454af6042SHugh Dickins goto failed; 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds 17172cf85583STejun Heo error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg, 1718f627c2f5SKirill A. Shutemov false); 1719d1899228SHugh Dickins if (!error) { 172054af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1721fed400a1SWang Sheng-Hui swp_to_radix_entry(swap)); 1722215c02bcSHugh Dickins /* 1723215c02bcSHugh Dickins * We already confirmed swap under page lock, and make 1724215c02bcSHugh Dickins * no memory allocation here, so usually no possibility 1725215c02bcSHugh Dickins * of error; but free_swap_and_cache() only trylocks a 1726215c02bcSHugh Dickins * page, so it is just possible that the entry has been 1727215c02bcSHugh Dickins * truncated or holepunched since swap was confirmed. 1728215c02bcSHugh Dickins * shmem_undo_range() will have done some of the 1729215c02bcSHugh Dickins * unaccounting, now delete_from_swap_cache() will do 173093aa7d95SVladimir Davydov * the rest. 1731215c02bcSHugh Dickins * Reset swap.val? No, leave it so "failed" goes back to 1732215c02bcSHugh Dickins * "repeat": reading a hole and writing should succeed. 1733215c02bcSHugh Dickins */ 173400501b53SJohannes Weiner if (error) { 1735f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1736215c02bcSHugh Dickins delete_from_swap_cache(page); 1737d1899228SHugh Dickins } 173800501b53SJohannes Weiner } 173954af6042SHugh Dickins if (error) 174054af6042SHugh Dickins goto failed; 174154af6042SHugh Dickins 1742f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 174300501b53SJohannes Weiner 17444595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 174554af6042SHugh Dickins info->swapped--; 174654af6042SHugh Dickins shmem_recalc_inode(inode); 17474595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 174827ab7006SHugh Dickins 174966d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 175066d2f4d2SHugh Dickins mark_page_accessed(page); 175166d2f4d2SHugh Dickins 175227ab7006SHugh Dickins delete_from_swap_cache(page); 175327ab7006SHugh Dickins set_page_dirty(page); 175427ab7006SHugh Dickins swap_free(swap); 175527ab7006SHugh Dickins 175654af6042SHugh Dickins } else { 1757cfda0526SMike Rapoport if (vma && userfaultfd_missing(vma)) { 1758cfda0526SMike Rapoport *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); 1759cfda0526SMike Rapoport return 0; 1760cfda0526SMike Rapoport } 1761cfda0526SMike Rapoport 1762800d8c63SKirill A. Shutemov /* shmem_symlink() */ 1763800d8c63SKirill A. Shutemov if (mapping->a_ops != &shmem_aops) 1764800d8c63SKirill A. Shutemov goto alloc_nohuge; 1765657e3038SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE) 1766800d8c63SKirill A. Shutemov goto alloc_nohuge; 1767800d8c63SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 1768800d8c63SKirill A. Shutemov goto alloc_huge; 1769800d8c63SKirill A. Shutemov switch (sbinfo->huge) { 1770800d8c63SKirill A. Shutemov loff_t i_size; 1771800d8c63SKirill A. Shutemov pgoff_t off; 1772800d8c63SKirill A. Shutemov case SHMEM_HUGE_NEVER: 1773800d8c63SKirill A. Shutemov goto alloc_nohuge; 1774800d8c63SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 1775800d8c63SKirill A. Shutemov off = round_up(index, HPAGE_PMD_NR); 1776800d8c63SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 1777800d8c63SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 1778800d8c63SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 1779800d8c63SKirill A. Shutemov goto alloc_huge; 1780800d8c63SKirill A. Shutemov /* fallthrough */ 1781800d8c63SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 1782657e3038SKirill A. Shutemov if (sgp_huge == SGP_HUGE) 1783657e3038SKirill A. Shutemov goto alloc_huge; 1784657e3038SKirill A. Shutemov /* TODO: implement fadvise() hints */ 1785800d8c63SKirill A. Shutemov goto alloc_nohuge; 178659a16eadSHugh Dickins } 17871da177e4SLinus Torvalds 1788800d8c63SKirill A. Shutemov alloc_huge: 17890f079694SMike Rapoport page = shmem_alloc_and_acct_page(gfp, inode, index, true); 1790800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 17910f079694SMike Rapoport alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, 1792800d8c63SKirill A. Shutemov index, false); 179354af6042SHugh Dickins } 1794800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 1795779750d2SKirill A. Shutemov int retry = 5; 1796800d8c63SKirill A. Shutemov error = PTR_ERR(page); 1797800d8c63SKirill A. Shutemov page = NULL; 1798779750d2SKirill A. Shutemov if (error != -ENOSPC) 1799779750d2SKirill A. Shutemov goto failed; 1800779750d2SKirill A. Shutemov /* 1801779750d2SKirill A. Shutemov * Try to reclaim some spece by splitting a huge page 1802779750d2SKirill A. Shutemov * beyond i_size on the filesystem. 1803779750d2SKirill A. Shutemov */ 1804779750d2SKirill A. Shutemov while (retry--) { 1805779750d2SKirill A. Shutemov int ret; 1806779750d2SKirill A. Shutemov ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); 1807779750d2SKirill A. Shutemov if (ret == SHRINK_STOP) 1808779750d2SKirill A. Shutemov break; 1809779750d2SKirill A. Shutemov if (ret) 1810779750d2SKirill A. Shutemov goto alloc_nohuge; 1811779750d2SKirill A. Shutemov } 1812800d8c63SKirill A. Shutemov goto failed; 1813800d8c63SKirill A. Shutemov } 1814800d8c63SKirill A. Shutemov 1815800d8c63SKirill A. Shutemov if (PageTransHuge(page)) 1816800d8c63SKirill A. Shutemov hindex = round_down(index, HPAGE_PMD_NR); 1817800d8c63SKirill A. Shutemov else 1818800d8c63SKirill A. Shutemov hindex = index; 1819800d8c63SKirill A. Shutemov 182066d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1821eb39d618SHugh Dickins __SetPageReferenced(page); 182266d2f4d2SHugh Dickins 18232cf85583STejun Heo error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg, 1824800d8c63SKirill A. Shutemov PageTransHuge(page)); 182554af6042SHugh Dickins if (error) 1826800d8c63SKirill A. Shutemov goto unacct; 1827800d8c63SKirill A. Shutemov error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK, 1828800d8c63SKirill A. Shutemov compound_order(page)); 1829b065b432SHugh Dickins if (!error) { 1830800d8c63SKirill A. Shutemov error = shmem_add_to_page_cache(page, mapping, hindex, 1831fed400a1SWang Sheng-Hui NULL); 1832b065b432SHugh Dickins radix_tree_preload_end(); 1833b065b432SHugh Dickins } 1834b065b432SHugh Dickins if (error) { 1835800d8c63SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, 1836800d8c63SKirill A. Shutemov PageTransHuge(page)); 1837800d8c63SKirill A. Shutemov goto unacct; 1838b065b432SHugh Dickins } 1839800d8c63SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, 1840800d8c63SKirill A. Shutemov PageTransHuge(page)); 184154af6042SHugh Dickins lru_cache_add_anon(page); 184254af6042SHugh Dickins 18434595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1844800d8c63SKirill A. Shutemov info->alloced += 1 << compound_order(page); 1845800d8c63SKirill A. Shutemov inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page); 184654af6042SHugh Dickins shmem_recalc_inode(inode); 18474595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 18481635f6a7SHugh Dickins alloced = true; 184954af6042SHugh Dickins 1850779750d2SKirill A. Shutemov if (PageTransHuge(page) && 1851779750d2SKirill A. Shutemov DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 1852779750d2SKirill A. Shutemov hindex + HPAGE_PMD_NR - 1) { 1853779750d2SKirill A. Shutemov /* 1854779750d2SKirill A. Shutemov * Part of the huge page is beyond i_size: subject 1855779750d2SKirill A. Shutemov * to shrink under memory pressure. 1856779750d2SKirill A. Shutemov */ 1857779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1858d041353dSCong Wang /* 1859d041353dSCong Wang * _careful to defend against unlocked access to 1860d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1861d041353dSCong Wang */ 1862d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1863779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1864779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1865779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1866779750d2SKirill A. Shutemov } 1867779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1868779750d2SKirill A. Shutemov } 1869779750d2SKirill A. Shutemov 1870ec9516fbSHugh Dickins /* 18711635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 18721635f6a7SHugh Dickins */ 18731635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 18741635f6a7SHugh Dickins sgp = SGP_WRITE; 18751635f6a7SHugh Dickins clear: 18761635f6a7SHugh Dickins /* 18771635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 18781635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 18791635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1880ec9516fbSHugh Dickins */ 1881800d8c63SKirill A. Shutemov if (sgp != SGP_WRITE && !PageUptodate(page)) { 1882800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 1883800d8c63SKirill A. Shutemov int i; 1884800d8c63SKirill A. Shutemov 1885800d8c63SKirill A. Shutemov for (i = 0; i < (1 << compound_order(head)); i++) { 1886800d8c63SKirill A. Shutemov clear_highpage(head + i); 1887800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 1888800d8c63SKirill A. Shutemov } 1889800d8c63SKirill A. Shutemov SetPageUptodate(head); 1890ec9516fbSHugh Dickins } 18911da177e4SLinus Torvalds } 1892bde05d1cSHugh Dickins 189354af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 189475edd345SHugh Dickins if (sgp <= SGP_CACHE && 189509cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1896267a4c76SHugh Dickins if (alloced) { 1897267a4c76SHugh Dickins ClearPageDirty(page); 1898267a4c76SHugh Dickins delete_from_page_cache(page); 18994595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1900267a4c76SHugh Dickins shmem_recalc_inode(inode); 19014595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1902267a4c76SHugh Dickins } 190354af6042SHugh Dickins error = -EINVAL; 1904267a4c76SHugh Dickins goto unlock; 1905ff36b801SShaohua Li } 1906800d8c63SKirill A. Shutemov *pagep = page + index - hindex; 190754af6042SHugh Dickins return 0; 1908d00806b1SNick Piggin 1909d0217ac0SNick Piggin /* 191054af6042SHugh Dickins * Error recovery. 19111da177e4SLinus Torvalds */ 191254af6042SHugh Dickins unacct: 19130f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1 << compound_order(page)); 1914800d8c63SKirill A. Shutemov 1915800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 1916800d8c63SKirill A. Shutemov unlock_page(page); 1917800d8c63SKirill A. Shutemov put_page(page); 1918800d8c63SKirill A. Shutemov goto alloc_nohuge; 1919800d8c63SKirill A. Shutemov } 192054af6042SHugh Dickins failed: 1921267a4c76SHugh Dickins if (swap.val && !shmem_confirm_swap(mapping, index, swap)) 192254af6042SHugh Dickins error = -EEXIST; 1923d1899228SHugh Dickins unlock: 192427ab7006SHugh Dickins if (page) { 192554af6042SHugh Dickins unlock_page(page); 192609cbfeafSKirill A. Shutemov put_page(page); 192754af6042SHugh Dickins } 192854af6042SHugh Dickins if (error == -ENOSPC && !once++) { 19294595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 193054af6042SHugh Dickins shmem_recalc_inode(inode); 19314595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 19321da177e4SLinus Torvalds goto repeat; 1933d8dc74f2SAdrian Bunk } 1934d1899228SHugh Dickins if (error == -EEXIST) /* from above or from radix_tree_insert */ 193554af6042SHugh Dickins goto repeat; 193654af6042SHugh Dickins return error; 19371da177e4SLinus Torvalds } 19381da177e4SLinus Torvalds 193910d20bd2SLinus Torvalds /* 194010d20bd2SLinus Torvalds * This is like autoremove_wake_function, but it removes the wait queue 194110d20bd2SLinus Torvalds * entry unconditionally - even if something else had already woken the 194210d20bd2SLinus Torvalds * target. 194310d20bd2SLinus Torvalds */ 1944ac6424b9SIngo Molnar static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 194510d20bd2SLinus Torvalds { 194610d20bd2SLinus Torvalds int ret = default_wake_function(wait, mode, sync, key); 19472055da97SIngo Molnar list_del_init(&wait->entry); 194810d20bd2SLinus Torvalds return ret; 194910d20bd2SLinus Torvalds } 195010d20bd2SLinus Torvalds 195120acce67SSouptick Joarder static vm_fault_t shmem_fault(struct vm_fault *vmf) 19521da177e4SLinus Torvalds { 195311bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 1954496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 19559e18eb29SAndres Lagar-Cavilla gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 1956657e3038SKirill A. Shutemov enum sgp_type sgp; 195720acce67SSouptick Joarder int err; 195820acce67SSouptick Joarder vm_fault_t ret = VM_FAULT_LOCKED; 19591da177e4SLinus Torvalds 1960f00cdc6dSHugh Dickins /* 1961f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 1962f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 1963f00cdc6dSHugh Dickins * locks writers out with its hold on i_mutex. So refrain from 19648e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 19658e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 19668e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 19678e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 19688e205f77SHugh Dickins * 19698e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 19708e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 19718e205f77SHugh Dickins * we just need to make racing faults a rare case. 19728e205f77SHugh Dickins * 19738e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 19748e205f77SHugh Dickins * standard mutex or completion: but we cannot take i_mutex in fault, 19758e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 1976f00cdc6dSHugh Dickins */ 1977f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 1978f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 1979f00cdc6dSHugh Dickins 1980f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 1981f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 19828e205f77SHugh Dickins if (shmem_falloc && 19838e205f77SHugh Dickins shmem_falloc->waitq && 19848e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 19858e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 19868e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 198710d20bd2SLinus Torvalds DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); 19888e205f77SHugh Dickins 19898e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 1990f00cdc6dSHugh Dickins if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) && 1991f00cdc6dSHugh Dickins !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 19928e205f77SHugh Dickins /* It's polite to up mmap_sem if we can */ 1993f00cdc6dSHugh Dickins up_read(&vma->vm_mm->mmap_sem); 19948e205f77SHugh Dickins ret = VM_FAULT_RETRY; 1995f00cdc6dSHugh Dickins } 19968e205f77SHugh Dickins 19978e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 19988e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 19998e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 20008e205f77SHugh Dickins spin_unlock(&inode->i_lock); 20018e205f77SHugh Dickins schedule(); 20028e205f77SHugh Dickins 20038e205f77SHugh Dickins /* 20048e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 20058e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 20068e205f77SHugh Dickins * is usually invalid by the time we reach here, but 20078e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 20088e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 20098e205f77SHugh Dickins */ 20108e205f77SHugh Dickins spin_lock(&inode->i_lock); 20118e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 20128e205f77SHugh Dickins spin_unlock(&inode->i_lock); 20138e205f77SHugh Dickins return ret; 2014f00cdc6dSHugh Dickins } 20158e205f77SHugh Dickins spin_unlock(&inode->i_lock); 2016f00cdc6dSHugh Dickins } 2017f00cdc6dSHugh Dickins 2018657e3038SKirill A. Shutemov sgp = SGP_CACHE; 201918600332SMichal Hocko 202018600332SMichal Hocko if ((vma->vm_flags & VM_NOHUGEPAGE) || 202118600332SMichal Hocko test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) 2022657e3038SKirill A. Shutemov sgp = SGP_NOHUGE; 202318600332SMichal Hocko else if (vma->vm_flags & VM_HUGEPAGE) 202418600332SMichal Hocko sgp = SGP_HUGE; 2025657e3038SKirill A. Shutemov 202620acce67SSouptick Joarder err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp, 2027cfda0526SMike Rapoport gfp, vma, vmf, &ret); 202820acce67SSouptick Joarder if (err) 202920acce67SSouptick Joarder return vmf_error(err); 203068da9f05SHugh Dickins return ret; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 2033c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 2034c01d5b30SHugh Dickins unsigned long uaddr, unsigned long len, 2035c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 2036c01d5b30SHugh Dickins { 2037c01d5b30SHugh Dickins unsigned long (*get_area)(struct file *, 2038c01d5b30SHugh Dickins unsigned long, unsigned long, unsigned long, unsigned long); 2039c01d5b30SHugh Dickins unsigned long addr; 2040c01d5b30SHugh Dickins unsigned long offset; 2041c01d5b30SHugh Dickins unsigned long inflated_len; 2042c01d5b30SHugh Dickins unsigned long inflated_addr; 2043c01d5b30SHugh Dickins unsigned long inflated_offset; 2044c01d5b30SHugh Dickins 2045c01d5b30SHugh Dickins if (len > TASK_SIZE) 2046c01d5b30SHugh Dickins return -ENOMEM; 2047c01d5b30SHugh Dickins 2048c01d5b30SHugh Dickins get_area = current->mm->get_unmapped_area; 2049c01d5b30SHugh Dickins addr = get_area(file, uaddr, len, pgoff, flags); 2050c01d5b30SHugh Dickins 2051e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 2052c01d5b30SHugh Dickins return addr; 2053c01d5b30SHugh Dickins if (IS_ERR_VALUE(addr)) 2054c01d5b30SHugh Dickins return addr; 2055c01d5b30SHugh Dickins if (addr & ~PAGE_MASK) 2056c01d5b30SHugh Dickins return addr; 2057c01d5b30SHugh Dickins if (addr > TASK_SIZE - len) 2058c01d5b30SHugh Dickins return addr; 2059c01d5b30SHugh Dickins 2060c01d5b30SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 2061c01d5b30SHugh Dickins return addr; 2062c01d5b30SHugh Dickins if (len < HPAGE_PMD_SIZE) 2063c01d5b30SHugh Dickins return addr; 2064c01d5b30SHugh Dickins if (flags & MAP_FIXED) 2065c01d5b30SHugh Dickins return addr; 2066c01d5b30SHugh Dickins /* 2067c01d5b30SHugh Dickins * Our priority is to support MAP_SHARED mapped hugely; 2068c01d5b30SHugh Dickins * and support MAP_PRIVATE mapped hugely too, until it is COWed. 2069c01d5b30SHugh Dickins * But if caller specified an address hint, respect that as before. 2070c01d5b30SHugh Dickins */ 2071c01d5b30SHugh Dickins if (uaddr) 2072c01d5b30SHugh Dickins return addr; 2073c01d5b30SHugh Dickins 2074c01d5b30SHugh Dickins if (shmem_huge != SHMEM_HUGE_FORCE) { 2075c01d5b30SHugh Dickins struct super_block *sb; 2076c01d5b30SHugh Dickins 2077c01d5b30SHugh Dickins if (file) { 2078c01d5b30SHugh Dickins VM_BUG_ON(file->f_op != &shmem_file_operations); 2079c01d5b30SHugh Dickins sb = file_inode(file)->i_sb; 2080c01d5b30SHugh Dickins } else { 2081c01d5b30SHugh Dickins /* 2082c01d5b30SHugh Dickins * Called directly from mm/mmap.c, or drivers/char/mem.c 2083c01d5b30SHugh Dickins * for "/dev/zero", to create a shared anonymous object. 2084c01d5b30SHugh Dickins */ 2085c01d5b30SHugh Dickins if (IS_ERR(shm_mnt)) 2086c01d5b30SHugh Dickins return addr; 2087c01d5b30SHugh Dickins sb = shm_mnt->mnt_sb; 2088c01d5b30SHugh Dickins } 20893089bf61SToshi Kani if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) 2090c01d5b30SHugh Dickins return addr; 2091c01d5b30SHugh Dickins } 2092c01d5b30SHugh Dickins 2093c01d5b30SHugh Dickins offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); 2094c01d5b30SHugh Dickins if (offset && offset + len < 2 * HPAGE_PMD_SIZE) 2095c01d5b30SHugh Dickins return addr; 2096c01d5b30SHugh Dickins if ((addr & (HPAGE_PMD_SIZE-1)) == offset) 2097c01d5b30SHugh Dickins return addr; 2098c01d5b30SHugh Dickins 2099c01d5b30SHugh Dickins inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; 2100c01d5b30SHugh Dickins if (inflated_len > TASK_SIZE) 2101c01d5b30SHugh Dickins return addr; 2102c01d5b30SHugh Dickins if (inflated_len < len) 2103c01d5b30SHugh Dickins return addr; 2104c01d5b30SHugh Dickins 2105c01d5b30SHugh Dickins inflated_addr = get_area(NULL, 0, inflated_len, 0, flags); 2106c01d5b30SHugh Dickins if (IS_ERR_VALUE(inflated_addr)) 2107c01d5b30SHugh Dickins return addr; 2108c01d5b30SHugh Dickins if (inflated_addr & ~PAGE_MASK) 2109c01d5b30SHugh Dickins return addr; 2110c01d5b30SHugh Dickins 2111c01d5b30SHugh Dickins inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); 2112c01d5b30SHugh Dickins inflated_addr += offset - inflated_offset; 2113c01d5b30SHugh Dickins if (inflated_offset > offset) 2114c01d5b30SHugh Dickins inflated_addr += HPAGE_PMD_SIZE; 2115c01d5b30SHugh Dickins 2116c01d5b30SHugh Dickins if (inflated_addr > TASK_SIZE - len) 2117c01d5b30SHugh Dickins return addr; 2118c01d5b30SHugh Dickins return inflated_addr; 2119c01d5b30SHugh Dickins } 2120c01d5b30SHugh Dickins 21211da177e4SLinus Torvalds #ifdef CONFIG_NUMA 212241ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 21231da177e4SLinus Torvalds { 2124496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 212541ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 2128d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2129d8dc74f2SAdrian Bunk unsigned long addr) 21301da177e4SLinus Torvalds { 2131496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 213241ffe5d5SHugh Dickins pgoff_t index; 21331da177e4SLinus Torvalds 213441ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 213541ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 21361da177e4SLinus Torvalds } 21371da177e4SLinus Torvalds #endif 21381da177e4SLinus Torvalds 21391da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 21401da177e4SLinus Torvalds { 2141496ad9aaSAl Viro struct inode *inode = file_inode(file); 21421da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 21431da177e4SLinus Torvalds int retval = -ENOMEM; 21441da177e4SLinus Torvalds 21454595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 21461da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 21471da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 21481da177e4SLinus Torvalds goto out_nomem; 21491da177e4SLinus Torvalds info->flags |= VM_LOCKED; 215089e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 21531da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 21541da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 215589e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 21561da177e4SLinus Torvalds } 21571da177e4SLinus Torvalds retval = 0; 215889e004eaSLee Schermerhorn 21591da177e4SLinus Torvalds out_nomem: 21604595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 21611da177e4SLinus Torvalds return retval; 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds 21649b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 21651da177e4SLinus Torvalds { 21661da177e4SLinus Torvalds file_accessed(file); 21671da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2168e496cf3dSKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 2169f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 2170f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 2171f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 2172f3f0e1d2SKirill A. Shutemov } 21731da177e4SLinus Torvalds return 0; 21741da177e4SLinus Torvalds } 21751da177e4SLinus Torvalds 2176454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 217709208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 21781da177e4SLinus Torvalds { 21791da177e4SLinus Torvalds struct inode *inode; 21801da177e4SLinus Torvalds struct shmem_inode_info *info; 21811da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 21821da177e4SLinus Torvalds 21835b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 21841da177e4SLinus Torvalds return NULL; 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds inode = new_inode(sb); 21871da177e4SLinus Torvalds if (inode) { 218885fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 2189454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 21901da177e4SLinus Torvalds inode->i_blocks = 0; 2191078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 2192*46c9a946SArnd Bergmann inode->i_generation = prandom_u32(); 21931da177e4SLinus Torvalds info = SHMEM_I(inode); 21941da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 21951da177e4SLinus Torvalds spin_lock_init(&info->lock); 219640e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 21970b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 2198779750d2SKirill A. Shutemov INIT_LIST_HEAD(&info->shrinklist); 21991da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 220038f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 220172c04902SAl Viro cache_no_acl(inode); 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds switch (mode & S_IFMT) { 22041da177e4SLinus Torvalds default: 220539f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 22061da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 22071da177e4SLinus Torvalds break; 22081da177e4SLinus Torvalds case S_IFREG: 220914fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 22101da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 22111da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 221271fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 221371fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 22141da177e4SLinus Torvalds break; 22151da177e4SLinus Torvalds case S_IFDIR: 2216d8c76e6fSDave Hansen inc_nlink(inode); 22171da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 22181da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 22191da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 22201da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 22211da177e4SLinus Torvalds break; 22221da177e4SLinus Torvalds case S_IFLNK: 22231da177e4SLinus Torvalds /* 22241da177e4SLinus Torvalds * Must not load anything in the rbtree, 22251da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 22261da177e4SLinus Torvalds */ 222771fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 22281da177e4SLinus Torvalds break; 22291da177e4SLinus Torvalds } 22305b04c689SPavel Emelyanov } else 22315b04c689SPavel Emelyanov shmem_free_inode(sb); 22321da177e4SLinus Torvalds return inode; 22331da177e4SLinus Torvalds } 22341da177e4SLinus Torvalds 22350cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping) 22360cd6144aSJohannes Weiner { 2237f8005451SHugh Dickins return mapping->a_ops == &shmem_aops; 22380cd6144aSJohannes Weiner } 22390cd6144aSJohannes Weiner 22408d103963SMike Rapoport static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, 22414c27fe4cSMike Rapoport pmd_t *dst_pmd, 22424c27fe4cSMike Rapoport struct vm_area_struct *dst_vma, 22434c27fe4cSMike Rapoport unsigned long dst_addr, 22444c27fe4cSMike Rapoport unsigned long src_addr, 22458d103963SMike Rapoport bool zeropage, 22464c27fe4cSMike Rapoport struct page **pagep) 22474c27fe4cSMike Rapoport { 22484c27fe4cSMike Rapoport struct inode *inode = file_inode(dst_vma->vm_file); 22494c27fe4cSMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 22504c27fe4cSMike Rapoport struct address_space *mapping = inode->i_mapping; 22514c27fe4cSMike Rapoport gfp_t gfp = mapping_gfp_mask(mapping); 22524c27fe4cSMike Rapoport pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 22534c27fe4cSMike Rapoport struct mem_cgroup *memcg; 22544c27fe4cSMike Rapoport spinlock_t *ptl; 22554c27fe4cSMike Rapoport void *page_kaddr; 22564c27fe4cSMike Rapoport struct page *page; 22574c27fe4cSMike Rapoport pte_t _dst_pte, *dst_pte; 22584c27fe4cSMike Rapoport int ret; 22594c27fe4cSMike Rapoport 22604c27fe4cSMike Rapoport ret = -ENOMEM; 22610f079694SMike Rapoport if (!shmem_inode_acct_block(inode, 1)) 22624c27fe4cSMike Rapoport goto out; 22634c27fe4cSMike Rapoport 2264cb658a45SAndrea Arcangeli if (!*pagep) { 22654c27fe4cSMike Rapoport page = shmem_alloc_page(gfp, info, pgoff); 22664c27fe4cSMike Rapoport if (!page) 22670f079694SMike Rapoport goto out_unacct_blocks; 22684c27fe4cSMike Rapoport 22698d103963SMike Rapoport if (!zeropage) { /* mcopy_atomic */ 22704c27fe4cSMike Rapoport page_kaddr = kmap_atomic(page); 22718d103963SMike Rapoport ret = copy_from_user(page_kaddr, 22728d103963SMike Rapoport (const void __user *)src_addr, 22734c27fe4cSMike Rapoport PAGE_SIZE); 22744c27fe4cSMike Rapoport kunmap_atomic(page_kaddr); 22754c27fe4cSMike Rapoport 22764c27fe4cSMike Rapoport /* fallback to copy_from_user outside mmap_sem */ 22774c27fe4cSMike Rapoport if (unlikely(ret)) { 22784c27fe4cSMike Rapoport *pagep = page; 22790f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 22804c27fe4cSMike Rapoport /* don't free the page */ 22814c27fe4cSMike Rapoport return -EFAULT; 22824c27fe4cSMike Rapoport } 22838d103963SMike Rapoport } else { /* mfill_zeropage_atomic */ 22848d103963SMike Rapoport clear_highpage(page); 22858d103963SMike Rapoport } 22864c27fe4cSMike Rapoport } else { 22874c27fe4cSMike Rapoport page = *pagep; 22884c27fe4cSMike Rapoport *pagep = NULL; 22894c27fe4cSMike Rapoport } 22904c27fe4cSMike Rapoport 22919cc90c66SAndrea Arcangeli VM_BUG_ON(PageLocked(page) || PageSwapBacked(page)); 22929cc90c66SAndrea Arcangeli __SetPageLocked(page); 22939cc90c66SAndrea Arcangeli __SetPageSwapBacked(page); 2294a425d358SAndrea Arcangeli __SetPageUptodate(page); 22959cc90c66SAndrea Arcangeli 22962cf85583STejun Heo ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false); 22974c27fe4cSMike Rapoport if (ret) 22984c27fe4cSMike Rapoport goto out_release; 22994c27fe4cSMike Rapoport 23004c27fe4cSMike Rapoport ret = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK); 23014c27fe4cSMike Rapoport if (!ret) { 23024c27fe4cSMike Rapoport ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL); 23034c27fe4cSMike Rapoport radix_tree_preload_end(); 23044c27fe4cSMike Rapoport } 23054c27fe4cSMike Rapoport if (ret) 23064c27fe4cSMike Rapoport goto out_release_uncharge; 23074c27fe4cSMike Rapoport 23084c27fe4cSMike Rapoport mem_cgroup_commit_charge(page, memcg, false, false); 23094c27fe4cSMike Rapoport 23104c27fe4cSMike Rapoport _dst_pte = mk_pte(page, dst_vma->vm_page_prot); 23114c27fe4cSMike Rapoport if (dst_vma->vm_flags & VM_WRITE) 23124c27fe4cSMike Rapoport _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte)); 23134c27fe4cSMike Rapoport 23144c27fe4cSMike Rapoport ret = -EEXIST; 23154c27fe4cSMike Rapoport dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); 23164c27fe4cSMike Rapoport if (!pte_none(*dst_pte)) 23174c27fe4cSMike Rapoport goto out_release_uncharge_unlock; 23184c27fe4cSMike Rapoport 23194c27fe4cSMike Rapoport lru_cache_add_anon(page); 23204c27fe4cSMike Rapoport 23214c27fe4cSMike Rapoport spin_lock(&info->lock); 23224c27fe4cSMike Rapoport info->alloced++; 23234c27fe4cSMike Rapoport inode->i_blocks += BLOCKS_PER_PAGE; 23244c27fe4cSMike Rapoport shmem_recalc_inode(inode); 23254c27fe4cSMike Rapoport spin_unlock(&info->lock); 23264c27fe4cSMike Rapoport 23274c27fe4cSMike Rapoport inc_mm_counter(dst_mm, mm_counter_file(page)); 23284c27fe4cSMike Rapoport page_add_file_rmap(page, false); 23294c27fe4cSMike Rapoport set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 23304c27fe4cSMike Rapoport 23314c27fe4cSMike Rapoport /* No need to invalidate - it was non-present before */ 23324c27fe4cSMike Rapoport update_mmu_cache(dst_vma, dst_addr, dst_pte); 23334c27fe4cSMike Rapoport unlock_page(page); 23344c27fe4cSMike Rapoport pte_unmap_unlock(dst_pte, ptl); 23354c27fe4cSMike Rapoport ret = 0; 23364c27fe4cSMike Rapoport out: 23374c27fe4cSMike Rapoport return ret; 23384c27fe4cSMike Rapoport out_release_uncharge_unlock: 23394c27fe4cSMike Rapoport pte_unmap_unlock(dst_pte, ptl); 23404c27fe4cSMike Rapoport out_release_uncharge: 23414c27fe4cSMike Rapoport mem_cgroup_cancel_charge(page, memcg, false); 23424c27fe4cSMike Rapoport out_release: 23439cc90c66SAndrea Arcangeli unlock_page(page); 23444c27fe4cSMike Rapoport put_page(page); 23454c27fe4cSMike Rapoport out_unacct_blocks: 23460f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 23474c27fe4cSMike Rapoport goto out; 23484c27fe4cSMike Rapoport } 23494c27fe4cSMike Rapoport 23508d103963SMike Rapoport int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm, 23518d103963SMike Rapoport pmd_t *dst_pmd, 23528d103963SMike Rapoport struct vm_area_struct *dst_vma, 23538d103963SMike Rapoport unsigned long dst_addr, 23548d103963SMike Rapoport unsigned long src_addr, 23558d103963SMike Rapoport struct page **pagep) 23568d103963SMike Rapoport { 23578d103963SMike Rapoport return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, 23588d103963SMike Rapoport dst_addr, src_addr, false, pagep); 23598d103963SMike Rapoport } 23608d103963SMike Rapoport 23618d103963SMike Rapoport int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm, 23628d103963SMike Rapoport pmd_t *dst_pmd, 23638d103963SMike Rapoport struct vm_area_struct *dst_vma, 23648d103963SMike Rapoport unsigned long dst_addr) 23658d103963SMike Rapoport { 23668d103963SMike Rapoport struct page *page = NULL; 23678d103963SMike Rapoport 23688d103963SMike Rapoport return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, 23698d103963SMike Rapoport dst_addr, 0, true, &page); 23708d103963SMike Rapoport } 23718d103963SMike Rapoport 23721da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 237392e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 237469f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 23751da177e4SLinus Torvalds 23766d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR 23776d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 23786d9d88d0SJarkko Sakkinen #else 23796d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL 23806d9d88d0SJarkko Sakkinen #endif 23816d9d88d0SJarkko Sakkinen 23821da177e4SLinus Torvalds static int 2383800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 2384800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 2385800d15a5SNick Piggin struct page **pagep, void **fsdata) 23861da177e4SLinus Torvalds { 2387800d15a5SNick Piggin struct inode *inode = mapping->host; 238840e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 238909cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 239040e041a2SDavid Herrmann 239140e041a2SDavid Herrmann /* i_mutex is held by caller */ 23923f472cc9SSteven Rostedt (VMware) if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) { 239340e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) 239440e041a2SDavid Herrmann return -EPERM; 239540e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 239640e041a2SDavid Herrmann return -EPERM; 239740e041a2SDavid Herrmann } 239840e041a2SDavid Herrmann 23999e18eb29SAndres Lagar-Cavilla return shmem_getpage(inode, index, pagep, SGP_WRITE); 2400800d15a5SNick Piggin } 2401800d15a5SNick Piggin 2402800d15a5SNick Piggin static int 2403800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 2404800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 2405800d15a5SNick Piggin struct page *page, void *fsdata) 2406800d15a5SNick Piggin { 2407800d15a5SNick Piggin struct inode *inode = mapping->host; 2408800d15a5SNick Piggin 2409800d15a5SNick Piggin if (pos + copied > inode->i_size) 2410800d15a5SNick Piggin i_size_write(inode, pos + copied); 2411800d15a5SNick Piggin 2412ec9516fbSHugh Dickins if (!PageUptodate(page)) { 2413800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 2414800d8c63SKirill A. Shutemov if (PageTransCompound(page)) { 2415800d8c63SKirill A. Shutemov int i; 2416800d8c63SKirill A. Shutemov 2417800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 2418800d8c63SKirill A. Shutemov if (head + i == page) 2419800d8c63SKirill A. Shutemov continue; 2420800d8c63SKirill A. Shutemov clear_highpage(head + i); 2421800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 2422800d8c63SKirill A. Shutemov } 2423800d8c63SKirill A. Shutemov } 242409cbfeafSKirill A. Shutemov if (copied < PAGE_SIZE) { 242509cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 2426ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 242709cbfeafSKirill A. Shutemov from + copied, PAGE_SIZE); 2428ec9516fbSHugh Dickins } 2429800d8c63SKirill A. Shutemov SetPageUptodate(head); 2430ec9516fbSHugh Dickins } 2431d3602444SHugh Dickins set_page_dirty(page); 24326746aff7SWu Fengguang unlock_page(page); 243309cbfeafSKirill A. Shutemov put_page(page); 2434d3602444SHugh Dickins 2435800d15a5SNick Piggin return copied; 24361da177e4SLinus Torvalds } 24371da177e4SLinus Torvalds 24382ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 24391da177e4SLinus Torvalds { 24406e58e79dSAl Viro struct file *file = iocb->ki_filp; 24416e58e79dSAl Viro struct inode *inode = file_inode(file); 24421da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 244341ffe5d5SHugh Dickins pgoff_t index; 244441ffe5d5SHugh Dickins unsigned long offset; 2445a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 2446f7c1d074SGeert Uytterhoeven int error = 0; 2447cb66a7a1SAl Viro ssize_t retval = 0; 24486e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 2449a0ee5ec5SHugh Dickins 2450a0ee5ec5SHugh Dickins /* 2451a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 2452a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 2453a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 2454a0ee5ec5SHugh Dickins */ 2455777eda2cSAl Viro if (!iter_is_iovec(to)) 245675edd345SHugh Dickins sgp = SGP_CACHE; 24571da177e4SLinus Torvalds 245809cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 245909cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 24601da177e4SLinus Torvalds 24611da177e4SLinus Torvalds for (;;) { 24621da177e4SLinus Torvalds struct page *page = NULL; 246341ffe5d5SHugh Dickins pgoff_t end_index; 246441ffe5d5SHugh Dickins unsigned long nr, ret; 24651da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 24661da177e4SLinus Torvalds 246709cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 24681da177e4SLinus Torvalds if (index > end_index) 24691da177e4SLinus Torvalds break; 24701da177e4SLinus Torvalds if (index == end_index) { 247109cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 24721da177e4SLinus Torvalds if (nr <= offset) 24731da177e4SLinus Torvalds break; 24741da177e4SLinus Torvalds } 24751da177e4SLinus Torvalds 24769e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, sgp); 24776e58e79dSAl Viro if (error) { 24786e58e79dSAl Viro if (error == -EINVAL) 24796e58e79dSAl Viro error = 0; 24801da177e4SLinus Torvalds break; 24811da177e4SLinus Torvalds } 248275edd345SHugh Dickins if (page) { 248375edd345SHugh Dickins if (sgp == SGP_CACHE) 248475edd345SHugh Dickins set_page_dirty(page); 2485d3602444SHugh Dickins unlock_page(page); 248675edd345SHugh Dickins } 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds /* 24891da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 24901b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 24911da177e4SLinus Torvalds */ 249209cbfeafSKirill A. Shutemov nr = PAGE_SIZE; 24931da177e4SLinus Torvalds i_size = i_size_read(inode); 249409cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 24951da177e4SLinus Torvalds if (index == end_index) { 249609cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 24971da177e4SLinus Torvalds if (nr <= offset) { 24981da177e4SLinus Torvalds if (page) 249909cbfeafSKirill A. Shutemov put_page(page); 25001da177e4SLinus Torvalds break; 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds } 25031da177e4SLinus Torvalds nr -= offset; 25041da177e4SLinus Torvalds 25051da177e4SLinus Torvalds if (page) { 25061da177e4SLinus Torvalds /* 25071da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 25081da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 25091da177e4SLinus Torvalds * before reading the page on the kernel side. 25101da177e4SLinus Torvalds */ 25111da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 25121da177e4SLinus Torvalds flush_dcache_page(page); 25131da177e4SLinus Torvalds /* 25141da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 25151da177e4SLinus Torvalds */ 25161da177e4SLinus Torvalds if (!offset) 25171da177e4SLinus Torvalds mark_page_accessed(page); 2518b5810039SNick Piggin } else { 25191da177e4SLinus Torvalds page = ZERO_PAGE(0); 252009cbfeafSKirill A. Shutemov get_page(page); 2521b5810039SNick Piggin } 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds /* 25241da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 25251da177e4SLinus Torvalds * now we can copy it to user space... 25261da177e4SLinus Torvalds */ 25272ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 25286e58e79dSAl Viro retval += ret; 25291da177e4SLinus Torvalds offset += ret; 253009cbfeafSKirill A. Shutemov index += offset >> PAGE_SHIFT; 253109cbfeafSKirill A. Shutemov offset &= ~PAGE_MASK; 25321da177e4SLinus Torvalds 253309cbfeafSKirill A. Shutemov put_page(page); 25342ba5bbedSAl Viro if (!iov_iter_count(to)) 25351da177e4SLinus Torvalds break; 25366e58e79dSAl Viro if (ret < nr) { 25376e58e79dSAl Viro error = -EFAULT; 25386e58e79dSAl Viro break; 25396e58e79dSAl Viro } 25401da177e4SLinus Torvalds cond_resched(); 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds 254309cbfeafSKirill A. Shutemov *ppos = ((loff_t) index << PAGE_SHIFT) + offset; 25446e58e79dSAl Viro file_accessed(file); 25456e58e79dSAl Viro return retval ? retval : error; 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 2548220f2ac9SHugh Dickins /* 2549220f2ac9SHugh Dickins * llseek SEEK_DATA or SEEK_HOLE through the radix_tree. 2550220f2ac9SHugh Dickins */ 2551220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping, 2552965c8e59SAndrew Morton pgoff_t index, pgoff_t end, int whence) 2553220f2ac9SHugh Dickins { 2554220f2ac9SHugh Dickins struct page *page; 2555220f2ac9SHugh Dickins struct pagevec pvec; 2556220f2ac9SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 2557220f2ac9SHugh Dickins bool done = false; 2558220f2ac9SHugh Dickins int i; 2559220f2ac9SHugh Dickins 256086679820SMel Gorman pagevec_init(&pvec); 2561220f2ac9SHugh Dickins pvec.nr = 1; /* start small: we may be there already */ 2562220f2ac9SHugh Dickins while (!done) { 25630cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 2564220f2ac9SHugh Dickins pvec.nr, pvec.pages, indices); 2565220f2ac9SHugh Dickins if (!pvec.nr) { 2566965c8e59SAndrew Morton if (whence == SEEK_DATA) 2567220f2ac9SHugh Dickins index = end; 2568220f2ac9SHugh Dickins break; 2569220f2ac9SHugh Dickins } 2570220f2ac9SHugh Dickins for (i = 0; i < pvec.nr; i++, index++) { 2571220f2ac9SHugh Dickins if (index < indices[i]) { 2572965c8e59SAndrew Morton if (whence == SEEK_HOLE) { 2573220f2ac9SHugh Dickins done = true; 2574220f2ac9SHugh Dickins break; 2575220f2ac9SHugh Dickins } 2576220f2ac9SHugh Dickins index = indices[i]; 2577220f2ac9SHugh Dickins } 2578220f2ac9SHugh Dickins page = pvec.pages[i]; 2579220f2ac9SHugh Dickins if (page && !radix_tree_exceptional_entry(page)) { 2580220f2ac9SHugh Dickins if (!PageUptodate(page)) 2581220f2ac9SHugh Dickins page = NULL; 2582220f2ac9SHugh Dickins } 2583220f2ac9SHugh Dickins if (index >= end || 2584965c8e59SAndrew Morton (page && whence == SEEK_DATA) || 2585965c8e59SAndrew Morton (!page && whence == SEEK_HOLE)) { 2586220f2ac9SHugh Dickins done = true; 2587220f2ac9SHugh Dickins break; 2588220f2ac9SHugh Dickins } 2589220f2ac9SHugh Dickins } 25900cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 2591220f2ac9SHugh Dickins pagevec_release(&pvec); 2592220f2ac9SHugh Dickins pvec.nr = PAGEVEC_SIZE; 2593220f2ac9SHugh Dickins cond_resched(); 2594220f2ac9SHugh Dickins } 2595220f2ac9SHugh Dickins return index; 2596220f2ac9SHugh Dickins } 2597220f2ac9SHugh Dickins 2598965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 2599220f2ac9SHugh Dickins { 2600220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 2601220f2ac9SHugh Dickins struct inode *inode = mapping->host; 2602220f2ac9SHugh Dickins pgoff_t start, end; 2603220f2ac9SHugh Dickins loff_t new_offset; 2604220f2ac9SHugh Dickins 2605965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 2606965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 2607220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 26085955102cSAl Viro inode_lock(inode); 2609220f2ac9SHugh Dickins /* We're holding i_mutex so we can access i_size directly */ 2610220f2ac9SHugh Dickins 2611220f2ac9SHugh Dickins if (offset < 0) 2612220f2ac9SHugh Dickins offset = -EINVAL; 2613220f2ac9SHugh Dickins else if (offset >= inode->i_size) 2614220f2ac9SHugh Dickins offset = -ENXIO; 2615220f2ac9SHugh Dickins else { 261609cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 261709cbfeafSKirill A. Shutemov end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; 2618965c8e59SAndrew Morton new_offset = shmem_seek_hole_data(mapping, start, end, whence); 261909cbfeafSKirill A. Shutemov new_offset <<= PAGE_SHIFT; 2620220f2ac9SHugh Dickins if (new_offset > offset) { 2621220f2ac9SHugh Dickins if (new_offset < inode->i_size) 2622220f2ac9SHugh Dickins offset = new_offset; 2623965c8e59SAndrew Morton else if (whence == SEEK_DATA) 2624220f2ac9SHugh Dickins offset = -ENXIO; 2625220f2ac9SHugh Dickins else 2626220f2ac9SHugh Dickins offset = inode->i_size; 2627220f2ac9SHugh Dickins } 2628220f2ac9SHugh Dickins } 2629220f2ac9SHugh Dickins 2630387aae6fSHugh Dickins if (offset >= 0) 263146a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 26325955102cSAl Viro inode_unlock(inode); 2633220f2ac9SHugh Dickins return offset; 2634220f2ac9SHugh Dickins } 2635220f2ac9SHugh Dickins 263683e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 263783e4fa9cSHugh Dickins loff_t len) 263883e4fa9cSHugh Dickins { 2639496ad9aaSAl Viro struct inode *inode = file_inode(file); 2640e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 264140e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 26421aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 2643e2d12e22SHugh Dickins pgoff_t start, index, end; 2644e2d12e22SHugh Dickins int error; 264583e4fa9cSHugh Dickins 264613ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 264713ace4d0SHugh Dickins return -EOPNOTSUPP; 264813ace4d0SHugh Dickins 26495955102cSAl Viro inode_lock(inode); 265083e4fa9cSHugh Dickins 265183e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 265283e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 265383e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 265483e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 26558e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 265683e4fa9cSHugh Dickins 265740e041a2SDavid Herrmann /* protected by i_mutex */ 265840e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) { 265940e041a2SDavid Herrmann error = -EPERM; 266040e041a2SDavid Herrmann goto out; 266140e041a2SDavid Herrmann } 266240e041a2SDavid Herrmann 26638e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 2664f00cdc6dSHugh Dickins shmem_falloc.start = unmap_start >> PAGE_SHIFT; 2665f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 2666f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2667f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 2668f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 2669f00cdc6dSHugh Dickins 267083e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 267183e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 267283e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 267383e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 267483e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 26758e205f77SHugh Dickins 26768e205f77SHugh Dickins spin_lock(&inode->i_lock); 26778e205f77SHugh Dickins inode->i_private = NULL; 26788e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 26792055da97SIngo Molnar WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); 26808e205f77SHugh Dickins spin_unlock(&inode->i_lock); 268183e4fa9cSHugh Dickins error = 0; 26828e205f77SHugh Dickins goto out; 268383e4fa9cSHugh Dickins } 268483e4fa9cSHugh Dickins 2685e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 2686e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 2687e2d12e22SHugh Dickins if (error) 2688e2d12e22SHugh Dickins goto out; 2689e2d12e22SHugh Dickins 269040e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 269140e041a2SDavid Herrmann error = -EPERM; 269240e041a2SDavid Herrmann goto out; 269340e041a2SDavid Herrmann } 269440e041a2SDavid Herrmann 269509cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 269609cbfeafSKirill A. Shutemov end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 2697e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 2698e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 2699e2d12e22SHugh Dickins error = -ENOSPC; 2700e2d12e22SHugh Dickins goto out; 2701e2d12e22SHugh Dickins } 2702e2d12e22SHugh Dickins 27038e205f77SHugh Dickins shmem_falloc.waitq = NULL; 27041aac1400SHugh Dickins shmem_falloc.start = start; 27051aac1400SHugh Dickins shmem_falloc.next = start; 27061aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 27071aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 27081aac1400SHugh Dickins spin_lock(&inode->i_lock); 27091aac1400SHugh Dickins inode->i_private = &shmem_falloc; 27101aac1400SHugh Dickins spin_unlock(&inode->i_lock); 27111aac1400SHugh Dickins 2712e2d12e22SHugh Dickins for (index = start; index < end; index++) { 2713e2d12e22SHugh Dickins struct page *page; 2714e2d12e22SHugh Dickins 2715e2d12e22SHugh Dickins /* 2716e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 2717e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 2718e2d12e22SHugh Dickins */ 2719e2d12e22SHugh Dickins if (signal_pending(current)) 2720e2d12e22SHugh Dickins error = -EINTR; 27211aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 27221aac1400SHugh Dickins error = -ENOMEM; 2723e2d12e22SHugh Dickins else 27249e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, SGP_FALLOC); 2725e2d12e22SHugh Dickins if (error) { 27261635f6a7SHugh Dickins /* Remove the !PageUptodate pages we added */ 27277f556567SHugh Dickins if (index > start) { 27281635f6a7SHugh Dickins shmem_undo_range(inode, 272909cbfeafSKirill A. Shutemov (loff_t)start << PAGE_SHIFT, 2730b9b4bb26SAnthony Romano ((loff_t)index << PAGE_SHIFT) - 1, true); 27317f556567SHugh Dickins } 27321aac1400SHugh Dickins goto undone; 2733e2d12e22SHugh Dickins } 2734e2d12e22SHugh Dickins 2735e2d12e22SHugh Dickins /* 27361aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 27371aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 27381aac1400SHugh Dickins */ 27391aac1400SHugh Dickins shmem_falloc.next++; 27401aac1400SHugh Dickins if (!PageUptodate(page)) 27411aac1400SHugh Dickins shmem_falloc.nr_falloced++; 27421aac1400SHugh Dickins 27431aac1400SHugh Dickins /* 27441635f6a7SHugh Dickins * If !PageUptodate, leave it that way so that freeable pages 27451635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 27461635f6a7SHugh Dickins * But set_page_dirty so that memory pressure will swap rather 2747e2d12e22SHugh Dickins * than free the pages we are allocating (and SGP_CACHE pages 2748e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 2749e2d12e22SHugh Dickins */ 2750e2d12e22SHugh Dickins set_page_dirty(page); 2751e2d12e22SHugh Dickins unlock_page(page); 275209cbfeafSKirill A. Shutemov put_page(page); 2753e2d12e22SHugh Dickins cond_resched(); 2754e2d12e22SHugh Dickins } 2755e2d12e22SHugh Dickins 2756e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 2757e2d12e22SHugh Dickins i_size_write(inode, offset + len); 2758078cd827SDeepa Dinamani inode->i_ctime = current_time(inode); 27591aac1400SHugh Dickins undone: 27601aac1400SHugh Dickins spin_lock(&inode->i_lock); 27611aac1400SHugh Dickins inode->i_private = NULL; 27621aac1400SHugh Dickins spin_unlock(&inode->i_lock); 2763e2d12e22SHugh Dickins out: 27645955102cSAl Viro inode_unlock(inode); 276583e4fa9cSHugh Dickins return error; 276683e4fa9cSHugh Dickins } 276783e4fa9cSHugh Dickins 2768726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 27691da177e4SLinus Torvalds { 2770726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 277309cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 27741da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 27750edd73b3SHugh Dickins if (sbinfo->max_blocks) { 27761da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 277741ffe5d5SHugh Dickins buf->f_bavail = 277841ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 277941ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 27800edd73b3SHugh Dickins } 27810edd73b3SHugh Dickins if (sbinfo->max_inodes) { 27821da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 27831da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 27841da177e4SLinus Torvalds } 27851da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 27861da177e4SLinus Torvalds return 0; 27871da177e4SLinus Torvalds } 27881da177e4SLinus Torvalds 27891da177e4SLinus Torvalds /* 27901da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 27911da177e4SLinus Torvalds */ 27921da177e4SLinus Torvalds static int 27931a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 27941da177e4SLinus Torvalds { 27950b0a0806SHugh Dickins struct inode *inode; 27961da177e4SLinus Torvalds int error = -ENOSPC; 27971da177e4SLinus Torvalds 2798454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 27991da177e4SLinus Torvalds if (inode) { 2800feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2801feda821eSChristoph Hellwig if (error) 2802feda821eSChristoph Hellwig goto out_iput; 28032a7dba39SEric Paris error = security_inode_init_security(inode, dir, 28049d8f13baSMimi Zohar &dentry->d_name, 28056d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2806feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2807feda821eSChristoph Hellwig goto out_iput; 280837ec43cdSMimi Zohar 2809718deb6bSAl Viro error = 0; 28101da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2811078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 28121da177e4SLinus Torvalds d_instantiate(dentry, inode); 28131da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 28141da177e4SLinus Torvalds } 28151da177e4SLinus Torvalds return error; 2816feda821eSChristoph Hellwig out_iput: 2817feda821eSChristoph Hellwig iput(inode); 2818feda821eSChristoph Hellwig return error; 28191da177e4SLinus Torvalds } 28201da177e4SLinus Torvalds 282160545d0dSAl Viro static int 282260545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 282360545d0dSAl Viro { 282460545d0dSAl Viro struct inode *inode; 282560545d0dSAl Viro int error = -ENOSPC; 282660545d0dSAl Viro 282760545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 282860545d0dSAl Viro if (inode) { 282960545d0dSAl Viro error = security_inode_init_security(inode, dir, 283060545d0dSAl Viro NULL, 283160545d0dSAl Viro shmem_initxattrs, NULL); 2832feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2833feda821eSChristoph Hellwig goto out_iput; 2834feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2835feda821eSChristoph Hellwig if (error) 2836feda821eSChristoph Hellwig goto out_iput; 283760545d0dSAl Viro d_tmpfile(dentry, inode); 283860545d0dSAl Viro } 283960545d0dSAl Viro return error; 2840feda821eSChristoph Hellwig out_iput: 2841feda821eSChristoph Hellwig iput(inode); 2842feda821eSChristoph Hellwig return error; 284360545d0dSAl Viro } 284460545d0dSAl Viro 284518bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 28461da177e4SLinus Torvalds { 28471da177e4SLinus Torvalds int error; 28481da177e4SLinus Torvalds 28491da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 28501da177e4SLinus Torvalds return error; 2851d8c76e6fSDave Hansen inc_nlink(dir); 28521da177e4SLinus Torvalds return 0; 28531da177e4SLinus Torvalds } 28541da177e4SLinus Torvalds 28554acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2856ebfc3b49SAl Viro bool excl) 28571da177e4SLinus Torvalds { 28581da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 28591da177e4SLinus Torvalds } 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds /* 28621da177e4SLinus Torvalds * Link a file.. 28631da177e4SLinus Torvalds */ 28641da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 28651da177e4SLinus Torvalds { 286675c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 28675b04c689SPavel Emelyanov int ret; 28681da177e4SLinus Torvalds 28691da177e4SLinus Torvalds /* 28701da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 28711da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 28721da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 28731da177e4SLinus Torvalds */ 28745b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 28755b04c689SPavel Emelyanov if (ret) 28765b04c689SPavel Emelyanov goto out; 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2879078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 2880d8c76e6fSDave Hansen inc_nlink(inode); 28817de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 28821da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 28831da177e4SLinus Torvalds d_instantiate(dentry, inode); 28845b04c689SPavel Emelyanov out: 28855b04c689SPavel Emelyanov return ret; 28861da177e4SLinus Torvalds } 28871da177e4SLinus Torvalds 28881da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 28891da177e4SLinus Torvalds { 289075c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 28911da177e4SLinus Torvalds 28925b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 28935b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 28941da177e4SLinus Torvalds 28951da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 2896078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 28979a53c3a7SDave Hansen drop_nlink(inode); 28981da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 28991da177e4SLinus Torvalds return 0; 29001da177e4SLinus Torvalds } 29011da177e4SLinus Torvalds 29021da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 29031da177e4SLinus Torvalds { 29041da177e4SLinus Torvalds if (!simple_empty(dentry)) 29051da177e4SLinus Torvalds return -ENOTEMPTY; 29061da177e4SLinus Torvalds 290775c3cfa8SDavid Howells drop_nlink(d_inode(dentry)); 29089a53c3a7SDave Hansen drop_nlink(dir); 29091da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 29101da177e4SLinus Torvalds } 29111da177e4SLinus Torvalds 291237456771SMiklos Szeredi static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 291337456771SMiklos Szeredi { 2914e36cb0b8SDavid Howells bool old_is_dir = d_is_dir(old_dentry); 2915e36cb0b8SDavid Howells bool new_is_dir = d_is_dir(new_dentry); 291637456771SMiklos Szeredi 291737456771SMiklos Szeredi if (old_dir != new_dir && old_is_dir != new_is_dir) { 291837456771SMiklos Szeredi if (old_is_dir) { 291937456771SMiklos Szeredi drop_nlink(old_dir); 292037456771SMiklos Szeredi inc_nlink(new_dir); 292137456771SMiklos Szeredi } else { 292237456771SMiklos Szeredi drop_nlink(new_dir); 292337456771SMiklos Szeredi inc_nlink(old_dir); 292437456771SMiklos Szeredi } 292537456771SMiklos Szeredi } 292637456771SMiklos Szeredi old_dir->i_ctime = old_dir->i_mtime = 292737456771SMiklos Szeredi new_dir->i_ctime = new_dir->i_mtime = 292875c3cfa8SDavid Howells d_inode(old_dentry)->i_ctime = 2929078cd827SDeepa Dinamani d_inode(new_dentry)->i_ctime = current_time(old_dir); 293037456771SMiklos Szeredi 293137456771SMiklos Szeredi return 0; 293237456771SMiklos Szeredi } 293337456771SMiklos Szeredi 293446fdb794SMiklos Szeredi static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry) 293546fdb794SMiklos Szeredi { 293646fdb794SMiklos Szeredi struct dentry *whiteout; 293746fdb794SMiklos Szeredi int error; 293846fdb794SMiklos Szeredi 293946fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 294046fdb794SMiklos Szeredi if (!whiteout) 294146fdb794SMiklos Szeredi return -ENOMEM; 294246fdb794SMiklos Szeredi 294346fdb794SMiklos Szeredi error = shmem_mknod(old_dir, whiteout, 294446fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 294546fdb794SMiklos Szeredi dput(whiteout); 294646fdb794SMiklos Szeredi if (error) 294746fdb794SMiklos Szeredi return error; 294846fdb794SMiklos Szeredi 294946fdb794SMiklos Szeredi /* 295046fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 295146fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 295246fdb794SMiklos Szeredi * 295346fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 295446fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 295546fdb794SMiklos Szeredi */ 295646fdb794SMiklos Szeredi d_rehash(whiteout); 295746fdb794SMiklos Szeredi return 0; 295846fdb794SMiklos Szeredi } 295946fdb794SMiklos Szeredi 29601da177e4SLinus Torvalds /* 29611da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 29621da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 29631da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 29641da177e4SLinus Torvalds * gets overwritten. 29651da177e4SLinus Torvalds */ 29663b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) 29671da177e4SLinus Torvalds { 296875c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 29691da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 29701da177e4SLinus Torvalds 297146fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 29723b69ff51SMiklos Szeredi return -EINVAL; 29733b69ff51SMiklos Szeredi 297437456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 297537456771SMiklos Szeredi return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry); 297637456771SMiklos Szeredi 29771da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 29781da177e4SLinus Torvalds return -ENOTEMPTY; 29791da177e4SLinus Torvalds 298046fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 298146fdb794SMiklos Szeredi int error; 298246fdb794SMiklos Szeredi 298346fdb794SMiklos Szeredi error = shmem_whiteout(old_dir, old_dentry); 298446fdb794SMiklos Szeredi if (error) 298546fdb794SMiklos Szeredi return error; 298646fdb794SMiklos Szeredi } 298746fdb794SMiklos Szeredi 298875c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 29891da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 2990b928095bSMiklos Szeredi if (they_are_dirs) { 299175c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 29929a53c3a7SDave Hansen drop_nlink(old_dir); 2993b928095bSMiklos Szeredi } 29941da177e4SLinus Torvalds } else if (they_are_dirs) { 29959a53c3a7SDave Hansen drop_nlink(old_dir); 2996d8c76e6fSDave Hansen inc_nlink(new_dir); 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 30001da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 30011da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 30021da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 3003078cd827SDeepa Dinamani inode->i_ctime = current_time(old_dir); 30041da177e4SLinus Torvalds return 0; 30051da177e4SLinus Torvalds } 30061da177e4SLinus Torvalds 30071da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 30081da177e4SLinus Torvalds { 30091da177e4SLinus Torvalds int error; 30101da177e4SLinus Torvalds int len; 30111da177e4SLinus Torvalds struct inode *inode; 30129276aad6SHugh Dickins struct page *page; 30131da177e4SLinus Torvalds 30141da177e4SLinus Torvalds len = strlen(symname) + 1; 301509cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 30161da177e4SLinus Torvalds return -ENAMETOOLONG; 30171da177e4SLinus Torvalds 30180825a6f9SJoe Perches inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0, 30190825a6f9SJoe Perches VM_NORESERVE); 30201da177e4SLinus Torvalds if (!inode) 30211da177e4SLinus Torvalds return -ENOSPC; 30221da177e4SLinus Torvalds 30239d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 30246d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 3025570bc1c2SStephen Smalley if (error) { 3026570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 3027570bc1c2SStephen Smalley iput(inode); 3028570bc1c2SStephen Smalley return error; 3029570bc1c2SStephen Smalley } 3030570bc1c2SStephen Smalley error = 0; 3031570bc1c2SStephen Smalley } 3032570bc1c2SStephen Smalley 30331da177e4SLinus Torvalds inode->i_size = len-1; 303469f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 30353ed47db3SAl Viro inode->i_link = kmemdup(symname, len, GFP_KERNEL); 30363ed47db3SAl Viro if (!inode->i_link) { 303769f07ec9SHugh Dickins iput(inode); 303869f07ec9SHugh Dickins return -ENOMEM; 303969f07ec9SHugh Dickins } 304069f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 30411da177e4SLinus Torvalds } else { 3042e8ecde25SAl Viro inode_nohighmem(inode); 30439e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_WRITE); 30441da177e4SLinus Torvalds if (error) { 30451da177e4SLinus Torvalds iput(inode); 30461da177e4SLinus Torvalds return error; 30471da177e4SLinus Torvalds } 304814fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 30491da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 305021fc61c7SAl Viro memcpy(page_address(page), symname, len); 3051ec9516fbSHugh Dickins SetPageUptodate(page); 30521da177e4SLinus Torvalds set_page_dirty(page); 30536746aff7SWu Fengguang unlock_page(page); 305409cbfeafSKirill A. Shutemov put_page(page); 30551da177e4SLinus Torvalds } 30561da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3057078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 30581da177e4SLinus Torvalds d_instantiate(dentry, inode); 30591da177e4SLinus Torvalds dget(dentry); 30601da177e4SLinus Torvalds return 0; 30611da177e4SLinus Torvalds } 30621da177e4SLinus Torvalds 3063fceef393SAl Viro static void shmem_put_link(void *arg) 3064fceef393SAl Viro { 3065fceef393SAl Viro mark_page_accessed(arg); 3066fceef393SAl Viro put_page(arg); 3067fceef393SAl Viro } 3068fceef393SAl Viro 30696b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 3070fceef393SAl Viro struct inode *inode, 3071fceef393SAl Viro struct delayed_call *done) 30721da177e4SLinus Torvalds { 30731da177e4SLinus Torvalds struct page *page = NULL; 30746b255391SAl Viro int error; 30756a6c9904SAl Viro if (!dentry) { 30766a6c9904SAl Viro page = find_get_page(inode->i_mapping, 0); 30776a6c9904SAl Viro if (!page) 30786b255391SAl Viro return ERR_PTR(-ECHILD); 30796a6c9904SAl Viro if (!PageUptodate(page)) { 30806a6c9904SAl Viro put_page(page); 30816a6c9904SAl Viro return ERR_PTR(-ECHILD); 30826a6c9904SAl Viro } 30836a6c9904SAl Viro } else { 30849e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_READ); 3085680baacbSAl Viro if (error) 3086680baacbSAl Viro return ERR_PTR(error); 3087d3602444SHugh Dickins unlock_page(page); 30881da177e4SLinus Torvalds } 3089fceef393SAl Viro set_delayed_call(done, shmem_put_link, page); 309021fc61c7SAl Viro return page_address(page); 30911da177e4SLinus Torvalds } 30921da177e4SLinus Torvalds 3093b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3094b09e0fa4SEric Paris /* 3095b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 3096b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 3097b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 3098b09e0fa4SEric Paris * filesystem level, though. 3099b09e0fa4SEric Paris */ 3100b09e0fa4SEric Paris 31016d9d88d0SJarkko Sakkinen /* 31026d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 31036d9d88d0SJarkko Sakkinen */ 31046d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 31056d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 31066d9d88d0SJarkko Sakkinen void *fs_info) 31076d9d88d0SJarkko Sakkinen { 31086d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 31096d9d88d0SJarkko Sakkinen const struct xattr *xattr; 311038f38657SAristeu Rozanski struct simple_xattr *new_xattr; 31116d9d88d0SJarkko Sakkinen size_t len; 31126d9d88d0SJarkko Sakkinen 31136d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 311438f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 31156d9d88d0SJarkko Sakkinen if (!new_xattr) 31166d9d88d0SJarkko Sakkinen return -ENOMEM; 31176d9d88d0SJarkko Sakkinen 31186d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 31196d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 31206d9d88d0SJarkko Sakkinen GFP_KERNEL); 31216d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 31226d9d88d0SJarkko Sakkinen kfree(new_xattr); 31236d9d88d0SJarkko Sakkinen return -ENOMEM; 31246d9d88d0SJarkko Sakkinen } 31256d9d88d0SJarkko Sakkinen 31266d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 31276d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 31286d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 31296d9d88d0SJarkko Sakkinen xattr->name, len); 31306d9d88d0SJarkko Sakkinen 313138f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 31326d9d88d0SJarkko Sakkinen } 31336d9d88d0SJarkko Sakkinen 31346d9d88d0SJarkko Sakkinen return 0; 31356d9d88d0SJarkko Sakkinen } 31366d9d88d0SJarkko Sakkinen 3137aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 3138b296821aSAl Viro struct dentry *unused, struct inode *inode, 3139b296821aSAl Viro const char *name, void *buffer, size_t size) 3140aa7c5241SAndreas Gruenbacher { 3141b296821aSAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3142aa7c5241SAndreas Gruenbacher 3143aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3144aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 3145aa7c5241SAndreas Gruenbacher } 3146aa7c5241SAndreas Gruenbacher 3147aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 314859301226SAl Viro struct dentry *unused, struct inode *inode, 314959301226SAl Viro const char *name, const void *value, 315059301226SAl Viro size_t size, int flags) 3151aa7c5241SAndreas Gruenbacher { 315259301226SAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3153aa7c5241SAndreas Gruenbacher 3154aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3155aa7c5241SAndreas Gruenbacher return simple_xattr_set(&info->xattrs, name, value, size, flags); 3156aa7c5241SAndreas Gruenbacher } 3157aa7c5241SAndreas Gruenbacher 3158aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 3159aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3160aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3161aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3162aa7c5241SAndreas Gruenbacher }; 3163aa7c5241SAndreas Gruenbacher 3164aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 3165aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3166aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3167aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3168aa7c5241SAndreas Gruenbacher }; 3169aa7c5241SAndreas Gruenbacher 3170b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 3171b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 3172feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 3173feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 3174b09e0fa4SEric Paris #endif 3175aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 3176aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 3177b09e0fa4SEric Paris NULL 3178b09e0fa4SEric Paris }; 3179b09e0fa4SEric Paris 3180b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 3181b09e0fa4SEric Paris { 318275c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3183786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 3184b09e0fa4SEric Paris } 3185b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 3186b09e0fa4SEric Paris 318769f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 31886b255391SAl Viro .get_link = simple_get_link, 3189b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3190b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3191b09e0fa4SEric Paris #endif 31921da177e4SLinus Torvalds }; 31931da177e4SLinus Torvalds 319492e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 31956b255391SAl Viro .get_link = shmem_get_link, 3196b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3197b09e0fa4SEric Paris .listxattr = shmem_listxattr, 319839f0247dSAndreas Gruenbacher #endif 3199b09e0fa4SEric Paris }; 320039f0247dSAndreas Gruenbacher 320191828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 320291828a40SDavid M. Grimes { 320391828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 320491828a40SDavid M. Grimes } 320591828a40SDavid M. Grimes 320691828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 320791828a40SDavid M. Grimes { 320891828a40SDavid M. Grimes __u32 *fh = vfh; 320991828a40SDavid M. Grimes __u64 inum = fh[2]; 321091828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 321191828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 321291828a40SDavid M. Grimes } 321391828a40SDavid M. Grimes 321412ba780dSAmir Goldstein /* Find any alias of inode, but prefer a hashed alias */ 321512ba780dSAmir Goldstein static struct dentry *shmem_find_alias(struct inode *inode) 321612ba780dSAmir Goldstein { 321712ba780dSAmir Goldstein struct dentry *alias = d_find_alias(inode); 321812ba780dSAmir Goldstein 321912ba780dSAmir Goldstein return alias ?: d_find_any_alias(inode); 322012ba780dSAmir Goldstein } 322112ba780dSAmir Goldstein 322212ba780dSAmir Goldstein 3223480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 3224480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 322591828a40SDavid M. Grimes { 322691828a40SDavid M. Grimes struct inode *inode; 3227480b116cSChristoph Hellwig struct dentry *dentry = NULL; 322835c2a7f4SHugh Dickins u64 inum; 322991828a40SDavid M. Grimes 3230480b116cSChristoph Hellwig if (fh_len < 3) 3231480b116cSChristoph Hellwig return NULL; 3232480b116cSChristoph Hellwig 323335c2a7f4SHugh Dickins inum = fid->raw[2]; 323435c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 323535c2a7f4SHugh Dickins 3236480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 3237480b116cSChristoph Hellwig shmem_match, fid->raw); 323891828a40SDavid M. Grimes if (inode) { 323912ba780dSAmir Goldstein dentry = shmem_find_alias(inode); 324091828a40SDavid M. Grimes iput(inode); 324191828a40SDavid M. Grimes } 324291828a40SDavid M. Grimes 3243480b116cSChristoph Hellwig return dentry; 324491828a40SDavid M. Grimes } 324591828a40SDavid M. Grimes 3246b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 3247b0b0382bSAl Viro struct inode *parent) 324891828a40SDavid M. Grimes { 32495fe0c237SAneesh Kumar K.V if (*len < 3) { 32505fe0c237SAneesh Kumar K.V *len = 3; 325194e07a75SNamjae Jeon return FILEID_INVALID; 32525fe0c237SAneesh Kumar K.V } 325391828a40SDavid M. Grimes 32541d3382cbSAl Viro if (inode_unhashed(inode)) { 325591828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 325691828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 325791828a40SDavid M. Grimes * time, we need a lock to ensure we only try 325891828a40SDavid M. Grimes * to do it once 325991828a40SDavid M. Grimes */ 326091828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 326191828a40SDavid M. Grimes spin_lock(&lock); 32621d3382cbSAl Viro if (inode_unhashed(inode)) 326391828a40SDavid M. Grimes __insert_inode_hash(inode, 326491828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 326591828a40SDavid M. Grimes spin_unlock(&lock); 326691828a40SDavid M. Grimes } 326791828a40SDavid M. Grimes 326891828a40SDavid M. Grimes fh[0] = inode->i_generation; 326991828a40SDavid M. Grimes fh[1] = inode->i_ino; 327091828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 327191828a40SDavid M. Grimes 327291828a40SDavid M. Grimes *len = 3; 327391828a40SDavid M. Grimes return 1; 327491828a40SDavid M. Grimes } 327591828a40SDavid M. Grimes 327639655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 327791828a40SDavid M. Grimes .get_parent = shmem_get_parent, 327891828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 3279480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 328091828a40SDavid M. Grimes }; 328191828a40SDavid M. Grimes 3282680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 3283680d794bSakpm@linux-foundation.org bool remount) 32841da177e4SLinus Torvalds { 32851da177e4SLinus Torvalds char *this_char, *value, *rest; 328649cd0a5cSGreg Thelen struct mempolicy *mpol = NULL; 32878751e039SEric W. Biederman uid_t uid; 32888751e039SEric W. Biederman gid_t gid; 32891da177e4SLinus Torvalds 3290b00dc3adSHugh Dickins while (options != NULL) { 3291b00dc3adSHugh Dickins this_char = options; 3292b00dc3adSHugh Dickins for (;;) { 3293b00dc3adSHugh Dickins /* 3294b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 3295b00dc3adSHugh Dickins * mount options form a comma-separated list, 3296b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 3297b00dc3adSHugh Dickins */ 3298b00dc3adSHugh Dickins options = strchr(options, ','); 3299b00dc3adSHugh Dickins if (options == NULL) 3300b00dc3adSHugh Dickins break; 3301b00dc3adSHugh Dickins options++; 3302b00dc3adSHugh Dickins if (!isdigit(*options)) { 3303b00dc3adSHugh Dickins options[-1] = '\0'; 3304b00dc3adSHugh Dickins break; 3305b00dc3adSHugh Dickins } 3306b00dc3adSHugh Dickins } 33071da177e4SLinus Torvalds if (!*this_char) 33081da177e4SLinus Torvalds continue; 33091da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 33101da177e4SLinus Torvalds *value++ = 0; 33111da177e4SLinus Torvalds } else { 33121170532bSJoe Perches pr_err("tmpfs: No value for mount option '%s'\n", 33131da177e4SLinus Torvalds this_char); 331449cd0a5cSGreg Thelen goto error; 33151da177e4SLinus Torvalds } 33161da177e4SLinus Torvalds 33171da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 33181da177e4SLinus Torvalds unsigned long long size; 33191da177e4SLinus Torvalds size = memparse(value,&rest); 33201da177e4SLinus Torvalds if (*rest == '%') { 33211da177e4SLinus Torvalds size <<= PAGE_SHIFT; 33221da177e4SLinus Torvalds size *= totalram_pages; 33231da177e4SLinus Torvalds do_div(size, 100); 33241da177e4SLinus Torvalds rest++; 33251da177e4SLinus Torvalds } 33261da177e4SLinus Torvalds if (*rest) 33271da177e4SLinus Torvalds goto bad_val; 3328680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 332909cbfeafSKirill A. Shutemov DIV_ROUND_UP(size, PAGE_SIZE); 33301da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 3331680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 33321da177e4SLinus Torvalds if (*rest) 33331da177e4SLinus Torvalds goto bad_val; 33341da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 3335680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 33361da177e4SLinus Torvalds if (*rest) 33371da177e4SLinus Torvalds goto bad_val; 33381da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 3339680d794bSakpm@linux-foundation.org if (remount) 33401da177e4SLinus Torvalds continue; 3341680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 33421da177e4SLinus Torvalds if (*rest) 33431da177e4SLinus Torvalds goto bad_val; 33441da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 3345680d794bSakpm@linux-foundation.org if (remount) 33461da177e4SLinus Torvalds continue; 33478751e039SEric W. Biederman uid = simple_strtoul(value, &rest, 0); 33481da177e4SLinus Torvalds if (*rest) 33491da177e4SLinus Torvalds goto bad_val; 33508751e039SEric W. Biederman sbinfo->uid = make_kuid(current_user_ns(), uid); 33518751e039SEric W. Biederman if (!uid_valid(sbinfo->uid)) 33528751e039SEric W. Biederman goto bad_val; 33531da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 3354680d794bSakpm@linux-foundation.org if (remount) 33551da177e4SLinus Torvalds continue; 33568751e039SEric W. Biederman gid = simple_strtoul(value, &rest, 0); 33571da177e4SLinus Torvalds if (*rest) 33581da177e4SLinus Torvalds goto bad_val; 33598751e039SEric W. Biederman sbinfo->gid = make_kgid(current_user_ns(), gid); 33608751e039SEric W. Biederman if (!gid_valid(sbinfo->gid)) 33618751e039SEric W. Biederman goto bad_val; 3362e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 33635a6e75f8SKirill A. Shutemov } else if (!strcmp(this_char, "huge")) { 33645a6e75f8SKirill A. Shutemov int huge; 33655a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(value); 33665a6e75f8SKirill A. Shutemov if (huge < 0) 33675a6e75f8SKirill A. Shutemov goto bad_val; 33685a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 33695a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER) 33705a6e75f8SKirill A. Shutemov goto bad_val; 33715a6e75f8SKirill A. Shutemov sbinfo->huge = huge; 33725a6e75f8SKirill A. Shutemov #endif 33735a6e75f8SKirill A. Shutemov #ifdef CONFIG_NUMA 33747339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 337549cd0a5cSGreg Thelen mpol_put(mpol); 337649cd0a5cSGreg Thelen mpol = NULL; 337749cd0a5cSGreg Thelen if (mpol_parse_str(value, &mpol)) 33787339ff83SRobin Holt goto bad_val; 33795a6e75f8SKirill A. Shutemov #endif 33801da177e4SLinus Torvalds } else { 33811170532bSJoe Perches pr_err("tmpfs: Bad mount option %s\n", this_char); 338249cd0a5cSGreg Thelen goto error; 33831da177e4SLinus Torvalds } 33841da177e4SLinus Torvalds } 338549cd0a5cSGreg Thelen sbinfo->mpol = mpol; 33861da177e4SLinus Torvalds return 0; 33871da177e4SLinus Torvalds 33881da177e4SLinus Torvalds bad_val: 33891170532bSJoe Perches pr_err("tmpfs: Bad value '%s' for mount option '%s'\n", 33901da177e4SLinus Torvalds value, this_char); 339149cd0a5cSGreg Thelen error: 339249cd0a5cSGreg Thelen mpol_put(mpol); 33931da177e4SLinus Torvalds return 1; 33941da177e4SLinus Torvalds 33951da177e4SLinus Torvalds } 33961da177e4SLinus Torvalds 33971da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 33981da177e4SLinus Torvalds { 33991da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3400680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 34010edd73b3SHugh Dickins unsigned long inodes; 34020edd73b3SHugh Dickins int error = -EINVAL; 34031da177e4SLinus Torvalds 34045f00110fSGreg Thelen config.mpol = NULL; 3405680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 34060edd73b3SHugh Dickins return error; 34070edd73b3SHugh Dickins 34080edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 34090edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 34107e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 34110edd73b3SHugh Dickins goto out; 3412680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 34130edd73b3SHugh Dickins goto out; 34140edd73b3SHugh Dickins /* 341554af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 34160edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 34170edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 34180edd73b3SHugh Dickins */ 3419680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 34200edd73b3SHugh Dickins goto out; 3421680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 34220edd73b3SHugh Dickins goto out; 34230edd73b3SHugh Dickins 34240edd73b3SHugh Dickins error = 0; 34255a6e75f8SKirill A. Shutemov sbinfo->huge = config.huge; 3426680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 3427680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 3428680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 342971fe804bSLee Schermerhorn 34305f00110fSGreg Thelen /* 34315f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 34325f00110fSGreg Thelen */ 34335f00110fSGreg Thelen if (config.mpol) { 343471fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 343571fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 34365f00110fSGreg Thelen } 34370edd73b3SHugh Dickins out: 34380edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 34390edd73b3SHugh Dickins return error; 34401da177e4SLinus Torvalds } 3441680d794bSakpm@linux-foundation.org 344234c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 3443680d794bSakpm@linux-foundation.org { 344434c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 3445680d794bSakpm@linux-foundation.org 3446680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 3447680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 344809cbfeafSKirill A. Shutemov sbinfo->max_blocks << (PAGE_SHIFT - 10)); 3449680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 3450680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 34510825a6f9SJoe Perches if (sbinfo->mode != (0777 | S_ISVTX)) 345209208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 34538751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 34548751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 34558751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 34568751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 34578751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 34588751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 3459e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 34605a6e75f8SKirill A. Shutemov /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 34615a6e75f8SKirill A. Shutemov if (sbinfo->huge) 34625a6e75f8SKirill A. Shutemov seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 34635a6e75f8SKirill A. Shutemov #endif 346471fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 3465680d794bSakpm@linux-foundation.org return 0; 3466680d794bSakpm@linux-foundation.org } 34679183df25SDavid Herrmann 3468680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 34691da177e4SLinus Torvalds 34701da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 34711da177e4SLinus Torvalds { 3472602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3473602586a8SHugh Dickins 3474602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 347549cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 3476602586a8SHugh Dickins kfree(sbinfo); 34771da177e4SLinus Torvalds sb->s_fs_info = NULL; 34781da177e4SLinus Torvalds } 34791da177e4SLinus Torvalds 34802b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 34811da177e4SLinus Torvalds { 34821da177e4SLinus Torvalds struct inode *inode; 34830edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 3484680d794bSakpm@linux-foundation.org int err = -ENOMEM; 3485680d794bSakpm@linux-foundation.org 3486680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 3487425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 3488680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 3489680d794bSakpm@linux-foundation.org if (!sbinfo) 3490680d794bSakpm@linux-foundation.org return -ENOMEM; 3491680d794bSakpm@linux-foundation.org 34920825a6f9SJoe Perches sbinfo->mode = 0777 | S_ISVTX; 349376aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 349476aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 3495680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 34961da177e4SLinus Torvalds 34970edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 34981da177e4SLinus Torvalds /* 34991da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 35001da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 35011da177e4SLinus Torvalds * but the internal instance is left unlimited. 35021da177e4SLinus Torvalds */ 35031751e8a6SLinus Torvalds if (!(sb->s_flags & SB_KERNMOUNT)) { 3504680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 3505680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 3506680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 3507680d794bSakpm@linux-foundation.org err = -EINVAL; 3508680d794bSakpm@linux-foundation.org goto failed; 3509680d794bSakpm@linux-foundation.org } 3510ca4e0519SAl Viro } else { 35111751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 35121da177e4SLinus Torvalds } 351391828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 35141751e8a6SLinus Torvalds sb->s_flags |= SB_NOSEC; 35150edd73b3SHugh Dickins #else 35161751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 35170edd73b3SHugh Dickins #endif 35181da177e4SLinus Torvalds 35191da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 3520908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 3521602586a8SHugh Dickins goto failed; 3522680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 3523779750d2SKirill A. Shutemov spin_lock_init(&sbinfo->shrinklist_lock); 3524779750d2SKirill A. Shutemov INIT_LIST_HEAD(&sbinfo->shrinklist); 35251da177e4SLinus Torvalds 3526285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 352709cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 352809cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 35291da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 35301da177e4SLinus Torvalds sb->s_op = &shmem_ops; 3531cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 3532b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 353339f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 3534b09e0fa4SEric Paris #endif 3535b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 35361751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 353739f0247dSAndreas Gruenbacher #endif 35382b4db796SAmir Goldstein uuid_gen(&sb->s_uuid); 35390edd73b3SHugh Dickins 3540454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 35411da177e4SLinus Torvalds if (!inode) 35421da177e4SLinus Torvalds goto failed; 3543680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 3544680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 3545318ceed0SAl Viro sb->s_root = d_make_root(inode); 3546318ceed0SAl Viro if (!sb->s_root) 354748fde701SAl Viro goto failed; 35481da177e4SLinus Torvalds return 0; 35491da177e4SLinus Torvalds 35501da177e4SLinus Torvalds failed: 35511da177e4SLinus Torvalds shmem_put_super(sb); 35521da177e4SLinus Torvalds return err; 35531da177e4SLinus Torvalds } 35541da177e4SLinus Torvalds 3555fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 35561da177e4SLinus Torvalds 35571da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 35581da177e4SLinus Torvalds { 355941ffe5d5SHugh Dickins struct shmem_inode_info *info; 356041ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 356141ffe5d5SHugh Dickins if (!info) 35621da177e4SLinus Torvalds return NULL; 356341ffe5d5SHugh Dickins return &info->vfs_inode; 35641da177e4SLinus Torvalds } 35651da177e4SLinus Torvalds 356641ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 3567fa0d7e3dSNick Piggin { 3568fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 356984e710daSAl Viro if (S_ISLNK(inode->i_mode)) 35703ed47db3SAl Viro kfree(inode->i_link); 3571fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 3572fa0d7e3dSNick Piggin } 3573fa0d7e3dSNick Piggin 35741da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 35751da177e4SLinus Torvalds { 357609208d15SAl Viro if (S_ISREG(inode->i_mode)) 35771da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 357841ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 35791da177e4SLinus Torvalds } 35801da177e4SLinus Torvalds 358141ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 35821da177e4SLinus Torvalds { 358341ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 358441ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 35851da177e4SLinus Torvalds } 35861da177e4SLinus Torvalds 35879a8ec03eSweiping zhang static void shmem_init_inodecache(void) 35881da177e4SLinus Torvalds { 35891da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 35901da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 35915d097056SVladimir Davydov 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 35921da177e4SLinus Torvalds } 35931da177e4SLinus Torvalds 359441ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 35951da177e4SLinus Torvalds { 35961a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 35971da177e4SLinus Torvalds } 35981da177e4SLinus Torvalds 3599f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 36001da177e4SLinus Torvalds .writepage = shmem_writepage, 360176719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 36021da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3603800d15a5SNick Piggin .write_begin = shmem_write_begin, 3604800d15a5SNick Piggin .write_end = shmem_write_end, 36051da177e4SLinus Torvalds #endif 36061c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 3607304dbdb7SLee Schermerhorn .migratepage = migrate_page, 36081c93923cSAndrew Morton #endif 3609aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 36101da177e4SLinus Torvalds }; 36111da177e4SLinus Torvalds 361215ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 36131da177e4SLinus Torvalds .mmap = shmem_mmap, 3614c01d5b30SHugh Dickins .get_unmapped_area = shmem_get_unmapped_area, 36151da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3616220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 36172ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 36188174202bSAl Viro .write_iter = generic_file_write_iter, 36191b061d92SChristoph Hellwig .fsync = noop_fsync, 362082c156f8SAl Viro .splice_read = generic_file_splice_read, 3621f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 362283e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 36231da177e4SLinus Torvalds #endif 36241da177e4SLinus Torvalds }; 36251da177e4SLinus Torvalds 362692e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 362744a30220SYu Zhao .getattr = shmem_getattr, 362894c1e62dSHugh Dickins .setattr = shmem_setattr, 3629b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3630b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3631feda821eSChristoph Hellwig .set_acl = simple_set_acl, 3632b09e0fa4SEric Paris #endif 36331da177e4SLinus Torvalds }; 36341da177e4SLinus Torvalds 363592e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 36361da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 36371da177e4SLinus Torvalds .create = shmem_create, 36381da177e4SLinus Torvalds .lookup = simple_lookup, 36391da177e4SLinus Torvalds .link = shmem_link, 36401da177e4SLinus Torvalds .unlink = shmem_unlink, 36411da177e4SLinus Torvalds .symlink = shmem_symlink, 36421da177e4SLinus Torvalds .mkdir = shmem_mkdir, 36431da177e4SLinus Torvalds .rmdir = shmem_rmdir, 36441da177e4SLinus Torvalds .mknod = shmem_mknod, 36452773bf00SMiklos Szeredi .rename = shmem_rename2, 364660545d0dSAl Viro .tmpfile = shmem_tmpfile, 36471da177e4SLinus Torvalds #endif 3648b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3649b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3650b09e0fa4SEric Paris #endif 365139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 365294c1e62dSHugh Dickins .setattr = shmem_setattr, 3653feda821eSChristoph Hellwig .set_acl = simple_set_acl, 365439f0247dSAndreas Gruenbacher #endif 365539f0247dSAndreas Gruenbacher }; 365639f0247dSAndreas Gruenbacher 365792e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 3658b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3659b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3660b09e0fa4SEric Paris #endif 366139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 366294c1e62dSHugh Dickins .setattr = shmem_setattr, 3663feda821eSChristoph Hellwig .set_acl = simple_set_acl, 366439f0247dSAndreas Gruenbacher #endif 36651da177e4SLinus Torvalds }; 36661da177e4SLinus Torvalds 3667759b9775SHugh Dickins static const struct super_operations shmem_ops = { 36681da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 36691da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 36701da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 36711da177e4SLinus Torvalds .statfs = shmem_statfs, 36721da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 3673680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 36741da177e4SLinus Torvalds #endif 36751f895f75SAl Viro .evict_inode = shmem_evict_inode, 36761da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 36771da177e4SLinus Torvalds .put_super = shmem_put_super, 3678779750d2SKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3679779750d2SKirill A. Shutemov .nr_cached_objects = shmem_unused_huge_count, 3680779750d2SKirill A. Shutemov .free_cached_objects = shmem_unused_huge_scan, 3681779750d2SKirill A. Shutemov #endif 36821da177e4SLinus Torvalds }; 36831da177e4SLinus Torvalds 3684f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 368554cb8821SNick Piggin .fault = shmem_fault, 3686d7c17551SNing Qu .map_pages = filemap_map_pages, 36871da177e4SLinus Torvalds #ifdef CONFIG_NUMA 36881da177e4SLinus Torvalds .set_policy = shmem_set_policy, 36891da177e4SLinus Torvalds .get_policy = shmem_get_policy, 36901da177e4SLinus Torvalds #endif 36911da177e4SLinus Torvalds }; 36921da177e4SLinus Torvalds 36933c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 36943c26ff6eSAl Viro int flags, const char *dev_name, void *data) 36951da177e4SLinus Torvalds { 36963c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 36971da177e4SLinus Torvalds } 36981da177e4SLinus Torvalds 369941ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 37001da177e4SLinus Torvalds .owner = THIS_MODULE, 37011da177e4SLinus Torvalds .name = "tmpfs", 37023c26ff6eSAl Viro .mount = shmem_mount, 37031da177e4SLinus Torvalds .kill_sb = kill_litter_super, 37042b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 37051da177e4SLinus Torvalds }; 37061da177e4SLinus Torvalds 370741ffe5d5SHugh Dickins int __init shmem_init(void) 37081da177e4SLinus Torvalds { 37091da177e4SLinus Torvalds int error; 37101da177e4SLinus Torvalds 371116203a7aSRob Landley /* If rootfs called this, don't re-init */ 371216203a7aSRob Landley if (shmem_inode_cachep) 371316203a7aSRob Landley return 0; 371416203a7aSRob Landley 37159a8ec03eSweiping zhang shmem_init_inodecache(); 37161da177e4SLinus Torvalds 371741ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 37181da177e4SLinus Torvalds if (error) { 37191170532bSJoe Perches pr_err("Could not register tmpfs\n"); 37201da177e4SLinus Torvalds goto out2; 37211da177e4SLinus Torvalds } 372295dc112aSGreg Kroah-Hartman 3723ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 37241da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 37251da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 37261170532bSJoe Perches pr_err("Could not kern_mount tmpfs\n"); 37271da177e4SLinus Torvalds goto out1; 37281da177e4SLinus Torvalds } 37295a6e75f8SKirill A. Shutemov 3730e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3731435c0b87SKirill A. Shutemov if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) 37325a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 37335a6e75f8SKirill A. Shutemov else 37345a6e75f8SKirill A. Shutemov shmem_huge = 0; /* just in case it was patched */ 37355a6e75f8SKirill A. Shutemov #endif 37361da177e4SLinus Torvalds return 0; 37371da177e4SLinus Torvalds 37381da177e4SLinus Torvalds out1: 373941ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 37401da177e4SLinus Torvalds out2: 374141ffe5d5SHugh Dickins shmem_destroy_inodecache(); 37421da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 37431da177e4SLinus Torvalds return error; 37441da177e4SLinus Torvalds } 3745853ac43aSMatt Mackall 3746e496cf3dSKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS) 37475a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj, 37485a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, char *buf) 37495a6e75f8SKirill A. Shutemov { 37505a6e75f8SKirill A. Shutemov int values[] = { 37515a6e75f8SKirill A. Shutemov SHMEM_HUGE_ALWAYS, 37525a6e75f8SKirill A. Shutemov SHMEM_HUGE_WITHIN_SIZE, 37535a6e75f8SKirill A. Shutemov SHMEM_HUGE_ADVISE, 37545a6e75f8SKirill A. Shutemov SHMEM_HUGE_NEVER, 37555a6e75f8SKirill A. Shutemov SHMEM_HUGE_DENY, 37565a6e75f8SKirill A. Shutemov SHMEM_HUGE_FORCE, 37575a6e75f8SKirill A. Shutemov }; 37585a6e75f8SKirill A. Shutemov int i, count; 37595a6e75f8SKirill A. Shutemov 37605a6e75f8SKirill A. Shutemov for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) { 37615a6e75f8SKirill A. Shutemov const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s "; 37625a6e75f8SKirill A. Shutemov 37635a6e75f8SKirill A. Shutemov count += sprintf(buf + count, fmt, 37645a6e75f8SKirill A. Shutemov shmem_format_huge(values[i])); 37655a6e75f8SKirill A. Shutemov } 37665a6e75f8SKirill A. Shutemov buf[count - 1] = '\n'; 37675a6e75f8SKirill A. Shutemov return count; 37685a6e75f8SKirill A. Shutemov } 37695a6e75f8SKirill A. Shutemov 37705a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj, 37715a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, const char *buf, size_t count) 37725a6e75f8SKirill A. Shutemov { 37735a6e75f8SKirill A. Shutemov char tmp[16]; 37745a6e75f8SKirill A. Shutemov int huge; 37755a6e75f8SKirill A. Shutemov 37765a6e75f8SKirill A. Shutemov if (count + 1 > sizeof(tmp)) 37775a6e75f8SKirill A. Shutemov return -EINVAL; 37785a6e75f8SKirill A. Shutemov memcpy(tmp, buf, count); 37795a6e75f8SKirill A. Shutemov tmp[count] = '\0'; 37805a6e75f8SKirill A. Shutemov if (count && tmp[count - 1] == '\n') 37815a6e75f8SKirill A. Shutemov tmp[count - 1] = '\0'; 37825a6e75f8SKirill A. Shutemov 37835a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(tmp); 37845a6e75f8SKirill A. Shutemov if (huge == -EINVAL) 37855a6e75f8SKirill A. Shutemov return -EINVAL; 37865a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 37875a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 37885a6e75f8SKirill A. Shutemov return -EINVAL; 37895a6e75f8SKirill A. Shutemov 37905a6e75f8SKirill A. Shutemov shmem_huge = huge; 3791435c0b87SKirill A. Shutemov if (shmem_huge > SHMEM_HUGE_DENY) 37925a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 37935a6e75f8SKirill A. Shutemov return count; 37945a6e75f8SKirill A. Shutemov } 37955a6e75f8SKirill A. Shutemov 37965a6e75f8SKirill A. Shutemov struct kobj_attribute shmem_enabled_attr = 37975a6e75f8SKirill A. Shutemov __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store); 37983b33719cSArnd Bergmann #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */ 3799f3f0e1d2SKirill A. Shutemov 38003b33719cSArnd Bergmann #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3801f3f0e1d2SKirill A. Shutemov bool shmem_huge_enabled(struct vm_area_struct *vma) 3802f3f0e1d2SKirill A. Shutemov { 3803f3f0e1d2SKirill A. Shutemov struct inode *inode = file_inode(vma->vm_file); 3804f3f0e1d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 3805f3f0e1d2SKirill A. Shutemov loff_t i_size; 3806f3f0e1d2SKirill A. Shutemov pgoff_t off; 3807f3f0e1d2SKirill A. Shutemov 3808f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 3809f3f0e1d2SKirill A. Shutemov return true; 3810f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY) 3811f3f0e1d2SKirill A. Shutemov return false; 3812f3f0e1d2SKirill A. Shutemov switch (sbinfo->huge) { 3813f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_NEVER: 3814f3f0e1d2SKirill A. Shutemov return false; 3815f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 3816f3f0e1d2SKirill A. Shutemov return true; 3817f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 3818f3f0e1d2SKirill A. Shutemov off = round_up(vma->vm_pgoff, HPAGE_PMD_NR); 3819f3f0e1d2SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 3820f3f0e1d2SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 3821f3f0e1d2SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 3822f3f0e1d2SKirill A. Shutemov return true; 3823c8402871SGustavo A. R. Silva /* fall through */ 3824f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 3825f3f0e1d2SKirill A. Shutemov /* TODO: implement fadvise() hints */ 3826f3f0e1d2SKirill A. Shutemov return (vma->vm_flags & VM_HUGEPAGE); 3827f3f0e1d2SKirill A. Shutemov default: 3828f3f0e1d2SKirill A. Shutemov VM_BUG_ON(1); 3829f3f0e1d2SKirill A. Shutemov return false; 3830f3f0e1d2SKirill A. Shutemov } 3831f3f0e1d2SKirill A. Shutemov } 38323b33719cSArnd Bergmann #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 38335a6e75f8SKirill A. Shutemov 3834853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 3835853ac43aSMatt Mackall 3836853ac43aSMatt Mackall /* 3837853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 3838853ac43aSMatt Mackall * 3839853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 3840853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 3841853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 3842853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 3843853ac43aSMatt Mackall */ 3844853ac43aSMatt Mackall 384541ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 3846853ac43aSMatt Mackall .name = "tmpfs", 38473c26ff6eSAl Viro .mount = ramfs_mount, 3848853ac43aSMatt Mackall .kill_sb = kill_litter_super, 38492b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 3850853ac43aSMatt Mackall }; 3851853ac43aSMatt Mackall 385241ffe5d5SHugh Dickins int __init shmem_init(void) 3853853ac43aSMatt Mackall { 385441ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 3855853ac43aSMatt Mackall 385641ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 3857853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 3858853ac43aSMatt Mackall 3859853ac43aSMatt Mackall return 0; 3860853ac43aSMatt Mackall } 3861853ac43aSMatt Mackall 386241ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 3863853ac43aSMatt Mackall { 3864853ac43aSMatt Mackall return 0; 3865853ac43aSMatt Mackall } 3866853ac43aSMatt Mackall 38673f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 38683f96b79aSHugh Dickins { 38693f96b79aSHugh Dickins return 0; 38703f96b79aSHugh Dickins } 38713f96b79aSHugh Dickins 387224513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 387324513264SHugh Dickins { 387424513264SHugh Dickins } 387524513264SHugh Dickins 3876c01d5b30SHugh Dickins #ifdef CONFIG_MMU 3877c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 3878c01d5b30SHugh Dickins unsigned long addr, unsigned long len, 3879c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 3880c01d5b30SHugh Dickins { 3881c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); 3882c01d5b30SHugh Dickins } 3883c01d5b30SHugh Dickins #endif 3884c01d5b30SHugh Dickins 388541ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 388694c1e62dSHugh Dickins { 388741ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 388894c1e62dSHugh Dickins } 388994c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 389094c1e62dSHugh Dickins 3891853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 38920b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 3893454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 38940b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 38950b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 3896853ac43aSMatt Mackall 3897853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 3898853ac43aSMatt Mackall 3899853ac43aSMatt Mackall /* common code */ 39001da177e4SLinus Torvalds 3901703321b6SMatthew Auld static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, 3902c7277090SEric Paris unsigned long flags, unsigned int i_flags) 39031da177e4SLinus Torvalds { 39041da177e4SLinus Torvalds struct inode *inode; 390593dec2daSAl Viro struct file *res; 39061da177e4SLinus Torvalds 3907703321b6SMatthew Auld if (IS_ERR(mnt)) 3908703321b6SMatthew Auld return ERR_CAST(mnt); 39091da177e4SLinus Torvalds 3910285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 39111da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 39121da177e4SLinus Torvalds 39131da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 39141da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 39151da177e4SLinus Torvalds 391693dec2daSAl Viro inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0, 391793dec2daSAl Viro flags); 3918dac2d1f6SAl Viro if (unlikely(!inode)) { 3919dac2d1f6SAl Viro shmem_unacct_size(flags, size); 3920dac2d1f6SAl Viro return ERR_PTR(-ENOSPC); 3921dac2d1f6SAl Viro } 3922c7277090SEric Paris inode->i_flags |= i_flags; 39231da177e4SLinus Torvalds inode->i_size = size; 39246d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 392526567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 392693dec2daSAl Viro if (!IS_ERR(res)) 392793dec2daSAl Viro res = alloc_file_pseudo(inode, mnt, name, O_RDWR, 39284b42af81SAl Viro &shmem_file_operations); 39296b4d0b27SAl Viro if (IS_ERR(res)) 393093dec2daSAl Viro iput(inode); 39316b4d0b27SAl Viro return res; 39321da177e4SLinus Torvalds } 3933c7277090SEric Paris 3934c7277090SEric Paris /** 3935c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 3936c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 3937c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 3938e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 3939e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 3940c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 3941c7277090SEric Paris * @size: size to be set for the file 3942c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3943c7277090SEric Paris */ 3944c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 3945c7277090SEric Paris { 3946703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); 3947c7277090SEric Paris } 3948c7277090SEric Paris 3949c7277090SEric Paris /** 3950c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 3951c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 3952c7277090SEric Paris * @size: size to be set for the file 3953c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3954c7277090SEric Paris */ 3955c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 3956c7277090SEric Paris { 3957703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, 0); 3958c7277090SEric Paris } 3959395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 39601da177e4SLinus Torvalds 396146711810SRandy Dunlap /** 3962703321b6SMatthew Auld * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs 3963703321b6SMatthew Auld * @mnt: the tmpfs mount where the file will be created 3964703321b6SMatthew Auld * @name: name for dentry (to be seen in /proc/<pid>/maps 3965703321b6SMatthew Auld * @size: size to be set for the file 3966703321b6SMatthew Auld * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 3967703321b6SMatthew Auld */ 3968703321b6SMatthew Auld struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, 3969703321b6SMatthew Auld loff_t size, unsigned long flags) 3970703321b6SMatthew Auld { 3971703321b6SMatthew Auld return __shmem_file_setup(mnt, name, size, flags, 0); 3972703321b6SMatthew Auld } 3973703321b6SMatthew Auld EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); 3974703321b6SMatthew Auld 3975703321b6SMatthew Auld /** 39761da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 39771da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 39781da177e4SLinus Torvalds */ 39791da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 39801da177e4SLinus Torvalds { 39811da177e4SLinus Torvalds struct file *file; 39821da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 39831da177e4SLinus Torvalds 398466fc1303SHugh Dickins /* 398566fc1303SHugh Dickins * Cloning a new file under mmap_sem leads to a lock ordering conflict 398666fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 398766fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 398866fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 398966fc1303SHugh Dickins */ 3990703321b6SMatthew Auld file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); 39911da177e4SLinus Torvalds if (IS_ERR(file)) 39921da177e4SLinus Torvalds return PTR_ERR(file); 39931da177e4SLinus Torvalds 39941da177e4SLinus Torvalds if (vma->vm_file) 39951da177e4SLinus Torvalds fput(vma->vm_file); 39961da177e4SLinus Torvalds vma->vm_file = file; 39971da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 3998f3f0e1d2SKirill A. Shutemov 3999e496cf3dSKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 4000f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 4001f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 4002f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 4003f3f0e1d2SKirill A. Shutemov } 4004f3f0e1d2SKirill A. Shutemov 40051da177e4SLinus Torvalds return 0; 40061da177e4SLinus Torvalds } 4007d9d90e5eSHugh Dickins 4008d9d90e5eSHugh Dickins /** 4009d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 4010d9d90e5eSHugh Dickins * @mapping: the page's address_space 4011d9d90e5eSHugh Dickins * @index: the page index 4012d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 4013d9d90e5eSHugh Dickins * 4014d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 4015d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 4016d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 4017d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 4018d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 4019d9d90e5eSHugh Dickins * 402068da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 402168da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 4022d9d90e5eSHugh Dickins */ 4023d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 4024d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 4025d9d90e5eSHugh Dickins { 402668da9f05SHugh Dickins #ifdef CONFIG_SHMEM 402768da9f05SHugh Dickins struct inode *inode = mapping->host; 40289276aad6SHugh Dickins struct page *page; 402968da9f05SHugh Dickins int error; 403068da9f05SHugh Dickins 403168da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 40329e18eb29SAndres Lagar-Cavilla error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, 4033cfda0526SMike Rapoport gfp, NULL, NULL, NULL); 403468da9f05SHugh Dickins if (error) 403568da9f05SHugh Dickins page = ERR_PTR(error); 403668da9f05SHugh Dickins else 403768da9f05SHugh Dickins unlock_page(page); 403868da9f05SHugh Dickins return page; 403968da9f05SHugh Dickins #else 404068da9f05SHugh Dickins /* 404168da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 404268da9f05SHugh Dickins */ 4043d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 404468da9f05SHugh Dickins #endif 4045d9d90e5eSHugh Dickins } 4046d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 4047