11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Resizable virtual memory filesystem for Linux. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000 Linus Torvalds. 51da177e4SLinus Torvalds * 2000 Transmeta Corp. 61da177e4SLinus Torvalds * 2000-2001 Christoph Rohland 71da177e4SLinus Torvalds * 2000-2001 SAP AG 81da177e4SLinus Torvalds * 2002 Red Hat Inc. 96922c0c7SHugh Dickins * Copyright (C) 2002-2011 Hugh Dickins. 106922c0c7SHugh Dickins * Copyright (C) 2011 Google Inc. 110edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 121da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Extended attribute support for tmpfs: 151da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 161da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 171da177e4SLinus Torvalds * 18853ac43aSMatt Mackall * tiny-shmem: 19853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 20853ac43aSMatt Mackall * 211da177e4SLinus Torvalds * This file is released under the GPL. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 24853ac43aSMatt Mackall #include <linux/fs.h> 25853ac43aSMatt Mackall #include <linux/init.h> 26853ac43aSMatt Mackall #include <linux/vfs.h> 27853ac43aSMatt Mackall #include <linux/mount.h> 28250297edSAndrew Morton #include <linux/ramfs.h> 29caefba17SHugh Dickins #include <linux/pagemap.h> 30853ac43aSMatt Mackall #include <linux/file.h> 31853ac43aSMatt Mackall #include <linux/mm.h> 32b95f1b31SPaul Gortmaker #include <linux/export.h> 33853ac43aSMatt Mackall #include <linux/swap.h> 34e2e40f2cSChristoph Hellwig #include <linux/uio.h> 35f3f0e1d2SKirill A. Shutemov #include <linux/khugepaged.h> 36853ac43aSMatt Mackall 37853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 38853ac43aSMatt Mackall 39853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 401da177e4SLinus Torvalds /* 411da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 421da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 431da177e4SLinus Torvalds * which makes it a completely usable filesystem. 441da177e4SLinus Torvalds */ 451da177e4SLinus Torvalds 4639f0247dSAndreas Gruenbacher #include <linux/xattr.h> 47a5694255SChristoph Hellwig #include <linux/exportfs.h> 481c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 49feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 501da177e4SLinus Torvalds #include <linux/mman.h> 511da177e4SLinus Torvalds #include <linux/string.h> 521da177e4SLinus Torvalds #include <linux/slab.h> 531da177e4SLinus Torvalds #include <linux/backing-dev.h> 541da177e4SLinus Torvalds #include <linux/shmem_fs.h> 551da177e4SLinus Torvalds #include <linux/writeback.h> 561da177e4SLinus Torvalds #include <linux/blkdev.h> 57bda97eabSHugh Dickins #include <linux/pagevec.h> 5841ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 5983e4fa9cSHugh Dickins #include <linux/falloc.h> 60708e3508SHugh Dickins #include <linux/splice.h> 611da177e4SLinus Torvalds #include <linux/security.h> 621da177e4SLinus Torvalds #include <linux/swapops.h> 631da177e4SLinus Torvalds #include <linux/mempolicy.h> 641da177e4SLinus Torvalds #include <linux/namei.h> 65b00dc3adSHugh Dickins #include <linux/ctype.h> 66304dbdb7SLee Schermerhorn #include <linux/migrate.h> 67c1f60a5aSChristoph Lameter #include <linux/highmem.h> 68680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 6992562927SMimi Zohar #include <linux/magic.h> 709183df25SDavid Herrmann #include <linux/syscalls.h> 7140e041a2SDavid Herrmann #include <linux/fcntl.h> 729183df25SDavid Herrmann #include <uapi/linux/memfd.h> 73304dbdb7SLee Schermerhorn 741da177e4SLinus Torvalds #include <asm/uaccess.h> 751da177e4SLinus Torvalds #include <asm/pgtable.h> 761da177e4SLinus Torvalds 77dd56b046SMel Gorman #include "internal.h" 78dd56b046SMel Gorman 7909cbfeafSKirill A. Shutemov #define BLOCKS_PER_PAGE (PAGE_SIZE/512) 8009cbfeafSKirill A. Shutemov #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 831da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 841da177e4SLinus Torvalds 8569f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 8669f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 8769f07ec9SHugh Dickins 881aac1400SHugh Dickins /* 89f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 90f00cdc6dSHugh Dickins * inode->i_private (with i_mutex making sure that it has only one user at 91f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 921aac1400SHugh Dickins */ 931aac1400SHugh Dickins struct shmem_falloc { 948e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 951aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 961aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 971aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 981aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 991aac1400SHugh Dickins }; 1001aac1400SHugh Dickins 101b76db735SAndrew Morton #ifdef CONFIG_TMPFS 102680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 103680d794bSakpm@linux-foundation.org { 104680d794bSakpm@linux-foundation.org return totalram_pages / 2; 105680d794bSakpm@linux-foundation.org } 106680d794bSakpm@linux-foundation.org 107680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 108680d794bSakpm@linux-foundation.org { 109680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 110680d794bSakpm@linux-foundation.org } 111b76db735SAndrew Morton #endif 112680d794bSakpm@linux-foundation.org 113bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp); 114bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 115bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index); 11668da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 1179e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, 1189e18eb29SAndres Lagar-Cavilla gfp_t gfp, struct mm_struct *fault_mm, int *fault_type); 11968da9f05SHugh Dickins 120f3f0e1d2SKirill A. Shutemov int shmem_getpage(struct inode *inode, pgoff_t index, 1219e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp) 12268da9f05SHugh Dickins { 12368da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 1249e18eb29SAndres Lagar-Cavilla mapping_gfp_mask(inode->i_mapping), NULL, NULL); 12568da9f05SHugh Dickins } 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds return sb->s_fs_info; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* 1331da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1341da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1351da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1361da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1371da177e4SLinus Torvalds */ 1381da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1391da177e4SLinus Torvalds { 1400b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 141191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1451da177e4SLinus Torvalds { 1460b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1471da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 15077142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 15177142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 15277142517SKonstantin Khlebnikov { 15377142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 15477142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 15577142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 15677142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 15777142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 15877142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 15977142517SKonstantin Khlebnikov } 16077142517SKonstantin Khlebnikov return 0; 16177142517SKonstantin Khlebnikov } 16277142517SKonstantin Khlebnikov 1631da177e4SLinus Torvalds /* 1641da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 16575edd345SHugh Dickins * pages are allocated, in order to allow large sparse files. 1661da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1671da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1681da177e4SLinus Torvalds */ 169800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages) 1701da177e4SLinus Torvalds { 171800d8c63SKirill A. Shutemov if (!(flags & VM_NORESERVE)) 172800d8c63SKirill A. Shutemov return 0; 173800d8c63SKirill A. Shutemov 174800d8c63SKirill A. Shutemov return security_vm_enough_memory_mm(current->mm, 175800d8c63SKirill A. Shutemov pages * VM_ACCT(PAGE_SIZE)); 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1791da177e4SLinus Torvalds { 1800b0a0806SHugh Dickins if (flags & VM_NORESERVE) 18109cbfeafSKirill A. Shutemov vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 184759b9775SHugh Dickins static const struct super_operations shmem_ops; 185f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 18615ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 18792e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 18892e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 18992e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 190f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 191779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 194cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 1951da177e4SLinus Torvalds 1965b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 1975b04c689SPavel Emelyanov { 1985b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 1995b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2005b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2015b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 2025b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2035b04c689SPavel Emelyanov return -ENOSPC; 2045b04c689SPavel Emelyanov } 2055b04c689SPavel Emelyanov sbinfo->free_inodes--; 2065b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2075b04c689SPavel Emelyanov } 2085b04c689SPavel Emelyanov return 0; 2095b04c689SPavel Emelyanov } 2105b04c689SPavel Emelyanov 2115b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2125b04c689SPavel Emelyanov { 2135b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2145b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2155b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2165b04c689SPavel Emelyanov sbinfo->free_inodes++; 2175b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2185b04c689SPavel Emelyanov } 2195b04c689SPavel Emelyanov } 2205b04c689SPavel Emelyanov 22146711810SRandy Dunlap /** 22241ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2231da177e4SLinus Torvalds * @inode: inode to recalc 2241da177e4SLinus Torvalds * 2251da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2261da177e4SLinus Torvalds * undirtied hole pages behind our back. 2271da177e4SLinus Torvalds * 2281da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2291da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2301da177e4SLinus Torvalds * 2311da177e4SLinus Torvalds * It has to be called with the spinlock held. 2321da177e4SLinus Torvalds */ 2331da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2341da177e4SLinus Torvalds { 2351da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2361da177e4SLinus Torvalds long freed; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2391da177e4SLinus Torvalds if (freed > 0) { 24054af6042SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 24154af6042SHugh Dickins if (sbinfo->max_blocks) 24254af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -freed); 2431da177e4SLinus Torvalds info->alloced -= freed; 24454af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2451da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 249800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages) 250800d8c63SKirill A. Shutemov { 251800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 252800d8c63SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2534595ef88SKirill A. Shutemov unsigned long flags; 254800d8c63SKirill A. Shutemov 255800d8c63SKirill A. Shutemov if (shmem_acct_block(info->flags, pages)) 256800d8c63SKirill A. Shutemov return false; 2574595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 258800d8c63SKirill A. Shutemov info->alloced += pages; 259800d8c63SKirill A. Shutemov inode->i_blocks += pages * BLOCKS_PER_PAGE; 260800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 2614595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 262800d8c63SKirill A. Shutemov inode->i_mapping->nrpages += pages; 263800d8c63SKirill A. Shutemov 264800d8c63SKirill A. Shutemov if (!sbinfo->max_blocks) 265800d8c63SKirill A. Shutemov return true; 266800d8c63SKirill A. Shutemov if (percpu_counter_compare(&sbinfo->used_blocks, 267800d8c63SKirill A. Shutemov sbinfo->max_blocks - pages) > 0) { 268800d8c63SKirill A. Shutemov inode->i_mapping->nrpages -= pages; 2694595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 270800d8c63SKirill A. Shutemov info->alloced -= pages; 271800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 2724595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 273800d8c63SKirill A. Shutemov 274800d8c63SKirill A. Shutemov return false; 275800d8c63SKirill A. Shutemov } 276800d8c63SKirill A. Shutemov percpu_counter_add(&sbinfo->used_blocks, pages); 277800d8c63SKirill A. Shutemov return true; 278800d8c63SKirill A. Shutemov } 279800d8c63SKirill A. Shutemov 280800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages) 281800d8c63SKirill A. Shutemov { 282800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 283800d8c63SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2844595ef88SKirill A. Shutemov unsigned long flags; 285800d8c63SKirill A. Shutemov 2864595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 287800d8c63SKirill A. Shutemov info->alloced -= pages; 288800d8c63SKirill A. Shutemov inode->i_blocks -= pages * BLOCKS_PER_PAGE; 289800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 2904595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 291800d8c63SKirill A. Shutemov 292800d8c63SKirill A. Shutemov if (sbinfo->max_blocks) 293800d8c63SKirill A. Shutemov percpu_counter_sub(&sbinfo->used_blocks, pages); 294800d8c63SKirill A. Shutemov } 295800d8c63SKirill A. Shutemov 2967a5d0fbbSHugh Dickins /* 2977a5d0fbbSHugh Dickins * Replace item expected in radix tree by a new item, while holding tree lock. 2987a5d0fbbSHugh Dickins */ 2997a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping, 3007a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 3017a5d0fbbSHugh Dickins { 3027a5d0fbbSHugh Dickins void **pslot; 3036dbaf22cSJohannes Weiner void *item; 3047a5d0fbbSHugh Dickins 3057a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 3066dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 3077a5d0fbbSHugh Dickins pslot = radix_tree_lookup_slot(&mapping->page_tree, index); 3086dbaf22cSJohannes Weiner if (!pslot) 3096dbaf22cSJohannes Weiner return -ENOENT; 3106dbaf22cSJohannes Weiner item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock); 3117a5d0fbbSHugh Dickins if (item != expected) 3127a5d0fbbSHugh Dickins return -ENOENT; 3137a5d0fbbSHugh Dickins radix_tree_replace_slot(pslot, replacement); 3147a5d0fbbSHugh Dickins return 0; 3157a5d0fbbSHugh Dickins } 3167a5d0fbbSHugh Dickins 3177a5d0fbbSHugh Dickins /* 318d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 319d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 320d1899228SHugh Dickins * 321d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 322d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 323d1899228SHugh Dickins */ 324d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 325d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 326d1899228SHugh Dickins { 327d1899228SHugh Dickins void *item; 328d1899228SHugh Dickins 329d1899228SHugh Dickins rcu_read_lock(); 330d1899228SHugh Dickins item = radix_tree_lookup(&mapping->page_tree, index); 331d1899228SHugh Dickins rcu_read_unlock(); 332d1899228SHugh Dickins return item == swp_to_radix_entry(swap); 333d1899228SHugh Dickins } 334d1899228SHugh Dickins 335d1899228SHugh Dickins /* 3365a6e75f8SKirill A. Shutemov * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 3375a6e75f8SKirill A. Shutemov * 3385a6e75f8SKirill A. Shutemov * SHMEM_HUGE_NEVER: 3395a6e75f8SKirill A. Shutemov * disables huge pages for the mount; 3405a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ALWAYS: 3415a6e75f8SKirill A. Shutemov * enables huge pages for the mount; 3425a6e75f8SKirill A. Shutemov * SHMEM_HUGE_WITHIN_SIZE: 3435a6e75f8SKirill A. Shutemov * only allocate huge pages if the page will be fully within i_size, 3445a6e75f8SKirill A. Shutemov * also respect fadvise()/madvise() hints; 3455a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ADVISE: 3465a6e75f8SKirill A. Shutemov * only allocate huge pages if requested with fadvise()/madvise(); 3475a6e75f8SKirill A. Shutemov */ 3485a6e75f8SKirill A. Shutemov 3495a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER 0 3505a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS 1 3515a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE 2 3525a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE 3 3535a6e75f8SKirill A. Shutemov 3545a6e75f8SKirill A. Shutemov /* 3555a6e75f8SKirill A. Shutemov * Special values. 3565a6e75f8SKirill A. Shutemov * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 3575a6e75f8SKirill A. Shutemov * 3585a6e75f8SKirill A. Shutemov * SHMEM_HUGE_DENY: 3595a6e75f8SKirill A. Shutemov * disables huge on shm_mnt and all mounts, for emergency use; 3605a6e75f8SKirill A. Shutemov * SHMEM_HUGE_FORCE: 3615a6e75f8SKirill A. Shutemov * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 3625a6e75f8SKirill A. Shutemov * 3635a6e75f8SKirill A. Shutemov */ 3645a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY (-1) 3655a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE (-2) 3665a6e75f8SKirill A. Shutemov 367e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3685a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */ 3695a6e75f8SKirill A. Shutemov 3705a6e75f8SKirill A. Shutemov int shmem_huge __read_mostly; 3715a6e75f8SKirill A. Shutemov 3725a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str) 3735a6e75f8SKirill A. Shutemov { 3745a6e75f8SKirill A. Shutemov if (!strcmp(str, "never")) 3755a6e75f8SKirill A. Shutemov return SHMEM_HUGE_NEVER; 3765a6e75f8SKirill A. Shutemov if (!strcmp(str, "always")) 3775a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ALWAYS; 3785a6e75f8SKirill A. Shutemov if (!strcmp(str, "within_size")) 3795a6e75f8SKirill A. Shutemov return SHMEM_HUGE_WITHIN_SIZE; 3805a6e75f8SKirill A. Shutemov if (!strcmp(str, "advise")) 3815a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ADVISE; 3825a6e75f8SKirill A. Shutemov if (!strcmp(str, "deny")) 3835a6e75f8SKirill A. Shutemov return SHMEM_HUGE_DENY; 3845a6e75f8SKirill A. Shutemov if (!strcmp(str, "force")) 3855a6e75f8SKirill A. Shutemov return SHMEM_HUGE_FORCE; 3865a6e75f8SKirill A. Shutemov return -EINVAL; 3875a6e75f8SKirill A. Shutemov } 3885a6e75f8SKirill A. Shutemov 3895a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge) 3905a6e75f8SKirill A. Shutemov { 3915a6e75f8SKirill A. Shutemov switch (huge) { 3925a6e75f8SKirill A. Shutemov case SHMEM_HUGE_NEVER: 3935a6e75f8SKirill A. Shutemov return "never"; 3945a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 3955a6e75f8SKirill A. Shutemov return "always"; 3965a6e75f8SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 3975a6e75f8SKirill A. Shutemov return "within_size"; 3985a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 3995a6e75f8SKirill A. Shutemov return "advise"; 4005a6e75f8SKirill A. Shutemov case SHMEM_HUGE_DENY: 4015a6e75f8SKirill A. Shutemov return "deny"; 4025a6e75f8SKirill A. Shutemov case SHMEM_HUGE_FORCE: 4035a6e75f8SKirill A. Shutemov return "force"; 4045a6e75f8SKirill A. Shutemov default: 4055a6e75f8SKirill A. Shutemov VM_BUG_ON(1); 4065a6e75f8SKirill A. Shutemov return "bad_val"; 4075a6e75f8SKirill A. Shutemov } 4085a6e75f8SKirill A. Shutemov } 4095a6e75f8SKirill A. Shutemov 410779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 411779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 412779750d2SKirill A. Shutemov { 413779750d2SKirill A. Shutemov LIST_HEAD(list), *pos, *next; 414779750d2SKirill A. Shutemov struct inode *inode; 415779750d2SKirill A. Shutemov struct shmem_inode_info *info; 416779750d2SKirill A. Shutemov struct page *page; 417779750d2SKirill A. Shutemov unsigned long batch = sc ? sc->nr_to_scan : 128; 418779750d2SKirill A. Shutemov int removed = 0, split = 0; 419779750d2SKirill A. Shutemov 420779750d2SKirill A. Shutemov if (list_empty(&sbinfo->shrinklist)) 421779750d2SKirill A. Shutemov return SHRINK_STOP; 422779750d2SKirill A. Shutemov 423779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 424779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &sbinfo->shrinklist) { 425779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 426779750d2SKirill A. Shutemov 427779750d2SKirill A. Shutemov /* pin the inode */ 428779750d2SKirill A. Shutemov inode = igrab(&info->vfs_inode); 429779750d2SKirill A. Shutemov 430779750d2SKirill A. Shutemov /* inode is about to be evicted */ 431779750d2SKirill A. Shutemov if (!inode) { 432779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 433779750d2SKirill A. Shutemov removed++; 434779750d2SKirill A. Shutemov goto next; 435779750d2SKirill A. Shutemov } 436779750d2SKirill A. Shutemov 437779750d2SKirill A. Shutemov /* Check if there's anything to gain */ 438779750d2SKirill A. Shutemov if (round_up(inode->i_size, PAGE_SIZE) == 439779750d2SKirill A. Shutemov round_up(inode->i_size, HPAGE_PMD_SIZE)) { 440779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 441779750d2SKirill A. Shutemov removed++; 442779750d2SKirill A. Shutemov iput(inode); 443779750d2SKirill A. Shutemov goto next; 444779750d2SKirill A. Shutemov } 445779750d2SKirill A. Shutemov 446779750d2SKirill A. Shutemov list_move(&info->shrinklist, &list); 447779750d2SKirill A. Shutemov next: 448779750d2SKirill A. Shutemov if (!--batch) 449779750d2SKirill A. Shutemov break; 450779750d2SKirill A. Shutemov } 451779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 452779750d2SKirill A. Shutemov 453779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &list) { 454779750d2SKirill A. Shutemov int ret; 455779750d2SKirill A. Shutemov 456779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 457779750d2SKirill A. Shutemov inode = &info->vfs_inode; 458779750d2SKirill A. Shutemov 459779750d2SKirill A. Shutemov if (nr_to_split && split >= nr_to_split) { 460779750d2SKirill A. Shutemov iput(inode); 461779750d2SKirill A. Shutemov continue; 462779750d2SKirill A. Shutemov } 463779750d2SKirill A. Shutemov 464779750d2SKirill A. Shutemov page = find_lock_page(inode->i_mapping, 465779750d2SKirill A. Shutemov (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT); 466779750d2SKirill A. Shutemov if (!page) 467779750d2SKirill A. Shutemov goto drop; 468779750d2SKirill A. Shutemov 469779750d2SKirill A. Shutemov if (!PageTransHuge(page)) { 470779750d2SKirill A. Shutemov unlock_page(page); 471779750d2SKirill A. Shutemov put_page(page); 472779750d2SKirill A. Shutemov goto drop; 473779750d2SKirill A. Shutemov } 474779750d2SKirill A. Shutemov 475779750d2SKirill A. Shutemov ret = split_huge_page(page); 476779750d2SKirill A. Shutemov unlock_page(page); 477779750d2SKirill A. Shutemov put_page(page); 478779750d2SKirill A. Shutemov 479779750d2SKirill A. Shutemov if (ret) { 480779750d2SKirill A. Shutemov /* split failed: leave it on the list */ 481779750d2SKirill A. Shutemov iput(inode); 482779750d2SKirill A. Shutemov continue; 483779750d2SKirill A. Shutemov } 484779750d2SKirill A. Shutemov 485779750d2SKirill A. Shutemov split++; 486779750d2SKirill A. Shutemov drop: 487779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 488779750d2SKirill A. Shutemov removed++; 489779750d2SKirill A. Shutemov iput(inode); 490779750d2SKirill A. Shutemov } 491779750d2SKirill A. Shutemov 492779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 493779750d2SKirill A. Shutemov list_splice_tail(&list, &sbinfo->shrinklist); 494779750d2SKirill A. Shutemov sbinfo->shrinklist_len -= removed; 495779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 496779750d2SKirill A. Shutemov 497779750d2SKirill A. Shutemov return split; 498779750d2SKirill A. Shutemov } 499779750d2SKirill A. Shutemov 500779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb, 501779750d2SKirill A. Shutemov struct shrink_control *sc) 502779750d2SKirill A. Shutemov { 503779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 504779750d2SKirill A. Shutemov 505779750d2SKirill A. Shutemov if (!READ_ONCE(sbinfo->shrinklist_len)) 506779750d2SKirill A. Shutemov return SHRINK_STOP; 507779750d2SKirill A. Shutemov 508779750d2SKirill A. Shutemov return shmem_unused_huge_shrink(sbinfo, sc, 0); 509779750d2SKirill A. Shutemov } 510779750d2SKirill A. Shutemov 511779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb, 512779750d2SKirill A. Shutemov struct shrink_control *sc) 513779750d2SKirill A. Shutemov { 514779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 515779750d2SKirill A. Shutemov return READ_ONCE(sbinfo->shrinklist_len); 516779750d2SKirill A. Shutemov } 517e496cf3dSKirill A. Shutemov #else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 5185a6e75f8SKirill A. Shutemov 5195a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY 5205a6e75f8SKirill A. Shutemov 521779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 522779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 523779750d2SKirill A. Shutemov { 524779750d2SKirill A. Shutemov return 0; 525779750d2SKirill A. Shutemov } 526e496cf3dSKirill A. Shutemov #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 5275a6e75f8SKirill A. Shutemov 5285a6e75f8SKirill A. Shutemov /* 52946f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 53046f65ec1SHugh Dickins */ 53146f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 53246f65ec1SHugh Dickins struct address_space *mapping, 533fed400a1SWang Sheng-Hui pgoff_t index, void *expected) 53446f65ec1SHugh Dickins { 535800d8c63SKirill A. Shutemov int error, nr = hpage_nr_pages(page); 53646f65ec1SHugh Dickins 537800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 538800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(index != round_down(index, nr), page); 539309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 540309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapBacked(page), page); 541800d8c63SKirill A. Shutemov VM_BUG_ON(expected && PageTransHuge(page)); 54246f65ec1SHugh Dickins 543800d8c63SKirill A. Shutemov page_ref_add(page, nr); 54446f65ec1SHugh Dickins page->mapping = mapping; 54546f65ec1SHugh Dickins page->index = index; 54646f65ec1SHugh Dickins 54746f65ec1SHugh Dickins spin_lock_irq(&mapping->tree_lock); 548800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 549800d8c63SKirill A. Shutemov void __rcu **results; 550800d8c63SKirill A. Shutemov pgoff_t idx; 551800d8c63SKirill A. Shutemov int i; 552800d8c63SKirill A. Shutemov 553800d8c63SKirill A. Shutemov error = 0; 554800d8c63SKirill A. Shutemov if (radix_tree_gang_lookup_slot(&mapping->page_tree, 555800d8c63SKirill A. Shutemov &results, &idx, index, 1) && 556800d8c63SKirill A. Shutemov idx < index + HPAGE_PMD_NR) { 557800d8c63SKirill A. Shutemov error = -EEXIST; 558800d8c63SKirill A. Shutemov } 559800d8c63SKirill A. Shutemov 560800d8c63SKirill A. Shutemov if (!error) { 561800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 562800d8c63SKirill A. Shutemov error = radix_tree_insert(&mapping->page_tree, 563800d8c63SKirill A. Shutemov index + i, page + i); 564800d8c63SKirill A. Shutemov VM_BUG_ON(error); 565800d8c63SKirill A. Shutemov } 566800d8c63SKirill A. Shutemov count_vm_event(THP_FILE_ALLOC); 567800d8c63SKirill A. Shutemov } 568800d8c63SKirill A. Shutemov } else if (!expected) { 569b065b432SHugh Dickins error = radix_tree_insert(&mapping->page_tree, index, page); 570800d8c63SKirill A. Shutemov } else { 571b065b432SHugh Dickins error = shmem_radix_tree_replace(mapping, index, expected, 572b065b432SHugh Dickins page); 573800d8c63SKirill A. Shutemov } 574800d8c63SKirill A. Shutemov 57546f65ec1SHugh Dickins if (!error) { 576800d8c63SKirill A. Shutemov mapping->nrpages += nr; 577800d8c63SKirill A. Shutemov if (PageTransHuge(page)) 57811fb9989SMel Gorman __inc_node_page_state(page, NR_SHMEM_THPS); 57911fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr); 58011fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr); 58146f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 58246f65ec1SHugh Dickins } else { 58346f65ec1SHugh Dickins page->mapping = NULL; 58446f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 585800d8c63SKirill A. Shutemov page_ref_sub(page, nr); 58646f65ec1SHugh Dickins } 58746f65ec1SHugh Dickins return error; 58846f65ec1SHugh Dickins } 58946f65ec1SHugh Dickins 59046f65ec1SHugh Dickins /* 5916922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 5926922c0c7SHugh Dickins */ 5936922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 5946922c0c7SHugh Dickins { 5956922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 5966922c0c7SHugh Dickins int error; 5976922c0c7SHugh Dickins 598800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 599800d8c63SKirill A. Shutemov 6006922c0c7SHugh Dickins spin_lock_irq(&mapping->tree_lock); 6016922c0c7SHugh Dickins error = shmem_radix_tree_replace(mapping, page->index, page, radswap); 6026922c0c7SHugh Dickins page->mapping = NULL; 6036922c0c7SHugh Dickins mapping->nrpages--; 60411fb9989SMel Gorman __dec_node_page_state(page, NR_FILE_PAGES); 60511fb9989SMel Gorman __dec_node_page_state(page, NR_SHMEM); 6066922c0c7SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 60709cbfeafSKirill A. Shutemov put_page(page); 6086922c0c7SHugh Dickins BUG_ON(error); 6096922c0c7SHugh Dickins } 6106922c0c7SHugh Dickins 6116922c0c7SHugh Dickins /* 6127a5d0fbbSHugh Dickins * Remove swap entry from radix tree, free the swap and its page cache. 6137a5d0fbbSHugh Dickins */ 6147a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 6157a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 6167a5d0fbbSHugh Dickins { 6176dbaf22cSJohannes Weiner void *old; 6187a5d0fbbSHugh Dickins 6197a5d0fbbSHugh Dickins spin_lock_irq(&mapping->tree_lock); 6206dbaf22cSJohannes Weiner old = radix_tree_delete_item(&mapping->page_tree, index, radswap); 6217a5d0fbbSHugh Dickins spin_unlock_irq(&mapping->tree_lock); 6226dbaf22cSJohannes Weiner if (old != radswap) 6236dbaf22cSJohannes Weiner return -ENOENT; 6247a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 6256dbaf22cSJohannes Weiner return 0; 6267a5d0fbbSHugh Dickins } 6277a5d0fbbSHugh Dickins 6287a5d0fbbSHugh Dickins /* 6296a15a370SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 63048131e03SVlastimil Babka * given offsets are swapped out. 6316a15a370SVlastimil Babka * 6326a15a370SVlastimil Babka * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU, 6336a15a370SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 6346a15a370SVlastimil Babka */ 63548131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping, 63648131e03SVlastimil Babka pgoff_t start, pgoff_t end) 6376a15a370SVlastimil Babka { 6386a15a370SVlastimil Babka struct radix_tree_iter iter; 6396a15a370SVlastimil Babka void **slot; 6406a15a370SVlastimil Babka struct page *page; 64148131e03SVlastimil Babka unsigned long swapped = 0; 6426a15a370SVlastimil Babka 6436a15a370SVlastimil Babka rcu_read_lock(); 6446a15a370SVlastimil Babka 6456a15a370SVlastimil Babka radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { 6466a15a370SVlastimil Babka if (iter.index >= end) 6476a15a370SVlastimil Babka break; 6486a15a370SVlastimil Babka 6496a15a370SVlastimil Babka page = radix_tree_deref_slot(slot); 6506a15a370SVlastimil Babka 6512cf938aaSMatthew Wilcox if (radix_tree_deref_retry(page)) { 6522cf938aaSMatthew Wilcox slot = radix_tree_iter_retry(&iter); 6532cf938aaSMatthew Wilcox continue; 6542cf938aaSMatthew Wilcox } 6556a15a370SVlastimil Babka 6566a15a370SVlastimil Babka if (radix_tree_exceptional_entry(page)) 6576a15a370SVlastimil Babka swapped++; 6586a15a370SVlastimil Babka 6596a15a370SVlastimil Babka if (need_resched()) { 6606a15a370SVlastimil Babka cond_resched_rcu(); 6617165092fSMatthew Wilcox slot = radix_tree_iter_next(&iter); 6626a15a370SVlastimil Babka } 6636a15a370SVlastimil Babka } 6646a15a370SVlastimil Babka 6656a15a370SVlastimil Babka rcu_read_unlock(); 6666a15a370SVlastimil Babka 6676a15a370SVlastimil Babka return swapped << PAGE_SHIFT; 6686a15a370SVlastimil Babka } 6696a15a370SVlastimil Babka 6706a15a370SVlastimil Babka /* 67148131e03SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 67248131e03SVlastimil Babka * given vma is swapped out. 67348131e03SVlastimil Babka * 67448131e03SVlastimil Babka * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU, 67548131e03SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 67648131e03SVlastimil Babka */ 67748131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma) 67848131e03SVlastimil Babka { 67948131e03SVlastimil Babka struct inode *inode = file_inode(vma->vm_file); 68048131e03SVlastimil Babka struct shmem_inode_info *info = SHMEM_I(inode); 68148131e03SVlastimil Babka struct address_space *mapping = inode->i_mapping; 68248131e03SVlastimil Babka unsigned long swapped; 68348131e03SVlastimil Babka 68448131e03SVlastimil Babka /* Be careful as we don't hold info->lock */ 68548131e03SVlastimil Babka swapped = READ_ONCE(info->swapped); 68648131e03SVlastimil Babka 68748131e03SVlastimil Babka /* 68848131e03SVlastimil Babka * The easier cases are when the shmem object has nothing in swap, or 68948131e03SVlastimil Babka * the vma maps it whole. Then we can simply use the stats that we 69048131e03SVlastimil Babka * already track. 69148131e03SVlastimil Babka */ 69248131e03SVlastimil Babka if (!swapped) 69348131e03SVlastimil Babka return 0; 69448131e03SVlastimil Babka 69548131e03SVlastimil Babka if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 69648131e03SVlastimil Babka return swapped << PAGE_SHIFT; 69748131e03SVlastimil Babka 69848131e03SVlastimil Babka /* Here comes the more involved part */ 69948131e03SVlastimil Babka return shmem_partial_swap_usage(mapping, 70048131e03SVlastimil Babka linear_page_index(vma, vma->vm_start), 70148131e03SVlastimil Babka linear_page_index(vma, vma->vm_end)); 70248131e03SVlastimil Babka } 70348131e03SVlastimil Babka 70448131e03SVlastimil Babka /* 70524513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 70624513264SHugh Dickins */ 70724513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 70824513264SHugh Dickins { 70924513264SHugh Dickins struct pagevec pvec; 71024513264SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 71124513264SHugh Dickins pgoff_t index = 0; 71224513264SHugh Dickins 71324513264SHugh Dickins pagevec_init(&pvec, 0); 71424513264SHugh Dickins /* 71524513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 71624513264SHugh Dickins */ 71724513264SHugh Dickins while (!mapping_unevictable(mapping)) { 71824513264SHugh Dickins /* 71924513264SHugh Dickins * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it 72024513264SHugh Dickins * has finished, if it hits a row of PAGEVEC_SIZE swap entries. 72124513264SHugh Dickins */ 7220cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 72324513264SHugh Dickins PAGEVEC_SIZE, pvec.pages, indices); 72424513264SHugh Dickins if (!pvec.nr) 72524513264SHugh Dickins break; 72624513264SHugh Dickins index = indices[pvec.nr - 1] + 1; 7270cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 72824513264SHugh Dickins check_move_unevictable_pages(pvec.pages, pvec.nr); 72924513264SHugh Dickins pagevec_release(&pvec); 73024513264SHugh Dickins cond_resched(); 73124513264SHugh Dickins } 7327a5d0fbbSHugh Dickins } 7337a5d0fbbSHugh Dickins 7347a5d0fbbSHugh Dickins /* 7357a5d0fbbSHugh Dickins * Remove range of pages and swap entries from radix tree, and free them. 7361635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 7377a5d0fbbSHugh Dickins */ 7381635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 7391635f6a7SHugh Dickins bool unfalloc) 7401da177e4SLinus Torvalds { 741285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 7421da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 74309cbfeafSKirill A. Shutemov pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 74409cbfeafSKirill A. Shutemov pgoff_t end = (lend + 1) >> PAGE_SHIFT; 74509cbfeafSKirill A. Shutemov unsigned int partial_start = lstart & (PAGE_SIZE - 1); 74609cbfeafSKirill A. Shutemov unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1); 747bda97eabSHugh Dickins struct pagevec pvec; 7487a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 7497a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 750285b2c4fSHugh Dickins pgoff_t index; 751bda97eabSHugh Dickins int i; 7521da177e4SLinus Torvalds 75383e4fa9cSHugh Dickins if (lend == -1) 75483e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 755bda97eabSHugh Dickins 756bda97eabSHugh Dickins pagevec_init(&pvec, 0); 757bda97eabSHugh Dickins index = start; 75883e4fa9cSHugh Dickins while (index < end) { 7590cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 76083e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 7617a5d0fbbSHugh Dickins pvec.pages, indices); 7627a5d0fbbSHugh Dickins if (!pvec.nr) 7637a5d0fbbSHugh Dickins break; 764bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 765bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 766bda97eabSHugh Dickins 7677a5d0fbbSHugh Dickins index = indices[i]; 76883e4fa9cSHugh Dickins if (index >= end) 769bda97eabSHugh Dickins break; 770bda97eabSHugh Dickins 7717a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 7721635f6a7SHugh Dickins if (unfalloc) 7731635f6a7SHugh Dickins continue; 7747a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 7757a5d0fbbSHugh Dickins index, page); 7767a5d0fbbSHugh Dickins continue; 7777a5d0fbbSHugh Dickins } 7787a5d0fbbSHugh Dickins 779800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page); 780800d8c63SKirill A. Shutemov 781bda97eabSHugh Dickins if (!trylock_page(page)) 782bda97eabSHugh Dickins continue; 783800d8c63SKirill A. Shutemov 784800d8c63SKirill A. Shutemov if (PageTransTail(page)) { 785800d8c63SKirill A. Shutemov /* Middle of THP: zero out the page */ 786800d8c63SKirill A. Shutemov clear_highpage(page); 787800d8c63SKirill A. Shutemov unlock_page(page); 788800d8c63SKirill A. Shutemov continue; 789800d8c63SKirill A. Shutemov } else if (PageTransHuge(page)) { 790800d8c63SKirill A. Shutemov if (index == round_down(end, HPAGE_PMD_NR)) { 791800d8c63SKirill A. Shutemov /* 792800d8c63SKirill A. Shutemov * Range ends in the middle of THP: 793800d8c63SKirill A. Shutemov * zero out the page 794800d8c63SKirill A. Shutemov */ 795800d8c63SKirill A. Shutemov clear_highpage(page); 796800d8c63SKirill A. Shutemov unlock_page(page); 797800d8c63SKirill A. Shutemov continue; 798800d8c63SKirill A. Shutemov } 799800d8c63SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 800800d8c63SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 801800d8c63SKirill A. Shutemov } 802800d8c63SKirill A. Shutemov 8031635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 804800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 805800d8c63SKirill A. Shutemov if (page_mapping(page) == mapping) { 806309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 807bda97eabSHugh Dickins truncate_inode_page(mapping, page); 8087a5d0fbbSHugh Dickins } 8091635f6a7SHugh Dickins } 810bda97eabSHugh Dickins unlock_page(page); 811bda97eabSHugh Dickins } 8120cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 81324513264SHugh Dickins pagevec_release(&pvec); 814bda97eabSHugh Dickins cond_resched(); 815bda97eabSHugh Dickins index++; 816bda97eabSHugh Dickins } 817bda97eabSHugh Dickins 81883e4fa9cSHugh Dickins if (partial_start) { 819bda97eabSHugh Dickins struct page *page = NULL; 8209e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, start - 1, &page, SGP_READ); 821bda97eabSHugh Dickins if (page) { 82209cbfeafSKirill A. Shutemov unsigned int top = PAGE_SIZE; 82383e4fa9cSHugh Dickins if (start > end) { 82483e4fa9cSHugh Dickins top = partial_end; 82583e4fa9cSHugh Dickins partial_end = 0; 82683e4fa9cSHugh Dickins } 82783e4fa9cSHugh Dickins zero_user_segment(page, partial_start, top); 828bda97eabSHugh Dickins set_page_dirty(page); 829bda97eabSHugh Dickins unlock_page(page); 83009cbfeafSKirill A. Shutemov put_page(page); 831bda97eabSHugh Dickins } 832bda97eabSHugh Dickins } 83383e4fa9cSHugh Dickins if (partial_end) { 83483e4fa9cSHugh Dickins struct page *page = NULL; 8359e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, end, &page, SGP_READ); 83683e4fa9cSHugh Dickins if (page) { 83783e4fa9cSHugh Dickins zero_user_segment(page, 0, partial_end); 83883e4fa9cSHugh Dickins set_page_dirty(page); 83983e4fa9cSHugh Dickins unlock_page(page); 84009cbfeafSKirill A. Shutemov put_page(page); 84183e4fa9cSHugh Dickins } 84283e4fa9cSHugh Dickins } 84383e4fa9cSHugh Dickins if (start >= end) 84483e4fa9cSHugh Dickins return; 845bda97eabSHugh Dickins 846bda97eabSHugh Dickins index = start; 847b1a36650SHugh Dickins while (index < end) { 848bda97eabSHugh Dickins cond_resched(); 8490cd6144aSJohannes Weiner 8500cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 85183e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 8527a5d0fbbSHugh Dickins pvec.pages, indices); 8537a5d0fbbSHugh Dickins if (!pvec.nr) { 854b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 855b1a36650SHugh Dickins if (index == start || end != -1) 856bda97eabSHugh Dickins break; 857b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 858bda97eabSHugh Dickins index = start; 859bda97eabSHugh Dickins continue; 860bda97eabSHugh Dickins } 861bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 862bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 863bda97eabSHugh Dickins 8647a5d0fbbSHugh Dickins index = indices[i]; 86583e4fa9cSHugh Dickins if (index >= end) 866bda97eabSHugh Dickins break; 867bda97eabSHugh Dickins 8687a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 8691635f6a7SHugh Dickins if (unfalloc) 8701635f6a7SHugh Dickins continue; 871b1a36650SHugh Dickins if (shmem_free_swap(mapping, index, page)) { 872b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 873b1a36650SHugh Dickins index--; 874b1a36650SHugh Dickins break; 875b1a36650SHugh Dickins } 876b1a36650SHugh Dickins nr_swaps_freed++; 8777a5d0fbbSHugh Dickins continue; 8787a5d0fbbSHugh Dickins } 8797a5d0fbbSHugh Dickins 880bda97eabSHugh Dickins lock_page(page); 881800d8c63SKirill A. Shutemov 882800d8c63SKirill A. Shutemov if (PageTransTail(page)) { 883800d8c63SKirill A. Shutemov /* Middle of THP: zero out the page */ 884800d8c63SKirill A. Shutemov clear_highpage(page); 885800d8c63SKirill A. Shutemov unlock_page(page); 886800d8c63SKirill A. Shutemov /* 887800d8c63SKirill A. Shutemov * Partial thp truncate due 'start' in middle 888800d8c63SKirill A. Shutemov * of THP: don't need to look on these pages 889800d8c63SKirill A. Shutemov * again on !pvec.nr restart. 890800d8c63SKirill A. Shutemov */ 891800d8c63SKirill A. Shutemov if (index != round_down(end, HPAGE_PMD_NR)) 892800d8c63SKirill A. Shutemov start++; 893800d8c63SKirill A. Shutemov continue; 894800d8c63SKirill A. Shutemov } else if (PageTransHuge(page)) { 895800d8c63SKirill A. Shutemov if (index == round_down(end, HPAGE_PMD_NR)) { 896800d8c63SKirill A. Shutemov /* 897800d8c63SKirill A. Shutemov * Range ends in the middle of THP: 898800d8c63SKirill A. Shutemov * zero out the page 899800d8c63SKirill A. Shutemov */ 900800d8c63SKirill A. Shutemov clear_highpage(page); 901800d8c63SKirill A. Shutemov unlock_page(page); 902800d8c63SKirill A. Shutemov continue; 903800d8c63SKirill A. Shutemov } 904800d8c63SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 905800d8c63SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 906800d8c63SKirill A. Shutemov } 907800d8c63SKirill A. Shutemov 9081635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 909800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 910800d8c63SKirill A. Shutemov if (page_mapping(page) == mapping) { 911309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 912bda97eabSHugh Dickins truncate_inode_page(mapping, page); 913b1a36650SHugh Dickins } else { 914b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 915b1a36650SHugh Dickins unlock_page(page); 916b1a36650SHugh Dickins index--; 917b1a36650SHugh Dickins break; 9187a5d0fbbSHugh Dickins } 9191635f6a7SHugh Dickins } 920bda97eabSHugh Dickins unlock_page(page); 921bda97eabSHugh Dickins } 9220cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 92324513264SHugh Dickins pagevec_release(&pvec); 924bda97eabSHugh Dickins index++; 925bda97eabSHugh Dickins } 92694c1e62dSHugh Dickins 9274595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 9287a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 9291da177e4SLinus Torvalds shmem_recalc_inode(inode); 9304595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 9311635f6a7SHugh Dickins } 9321da177e4SLinus Torvalds 9331635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 9341635f6a7SHugh Dickins { 9351635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 936285b2c4fSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 9371da177e4SLinus Torvalds } 93894c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 9391da177e4SLinus Torvalds 94044a30220SYu Zhao static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry, 94144a30220SYu Zhao struct kstat *stat) 94244a30220SYu Zhao { 94344a30220SYu Zhao struct inode *inode = dentry->d_inode; 94444a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 94544a30220SYu Zhao 946d0424c42SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) { 9474595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 94844a30220SYu Zhao shmem_recalc_inode(inode); 9494595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 950d0424c42SHugh Dickins } 95144a30220SYu Zhao generic_fillattr(inode, stat); 95244a30220SYu Zhao return 0; 95344a30220SYu Zhao } 95444a30220SYu Zhao 95594c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 9561da177e4SLinus Torvalds { 95775c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 95840e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 959779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 9601da177e4SLinus Torvalds int error; 9611da177e4SLinus Torvalds 962*31051c85SJan Kara error = setattr_prepare(dentry, attr); 963db78b877SChristoph Hellwig if (error) 964db78b877SChristoph Hellwig return error; 965db78b877SChristoph Hellwig 96694c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 96794c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 96894c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 9693889e6e7Snpiggin@suse.de 97040e041a2SDavid Herrmann /* protected by i_mutex */ 97140e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 97240e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 97340e041a2SDavid Herrmann return -EPERM; 97440e041a2SDavid Herrmann 97594c1e62dSHugh Dickins if (newsize != oldsize) { 97677142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 97777142517SKonstantin Khlebnikov oldsize, newsize); 97877142517SKonstantin Khlebnikov if (error) 97977142517SKonstantin Khlebnikov return error; 98094c1e62dSHugh Dickins i_size_write(inode, newsize); 98194c1e62dSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 98294c1e62dSHugh Dickins } 983afa2db2fSJosef Bacik if (newsize <= oldsize) { 98494c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 985d0424c42SHugh Dickins if (oldsize > holebegin) 986d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 987d0424c42SHugh Dickins holebegin, 0, 1); 988d0424c42SHugh Dickins if (info->alloced) 989d0424c42SHugh Dickins shmem_truncate_range(inode, 990d0424c42SHugh Dickins newsize, (loff_t)-1); 99194c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 992d0424c42SHugh Dickins if (oldsize > holebegin) 993d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 994d0424c42SHugh Dickins holebegin, 0, 1); 995779750d2SKirill A. Shutemov 996779750d2SKirill A. Shutemov /* 997779750d2SKirill A. Shutemov * Part of the huge page can be beyond i_size: subject 998779750d2SKirill A. Shutemov * to shrink under memory pressure. 999779750d2SKirill A. Shutemov */ 1000779750d2SKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 1001779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1002779750d2SKirill A. Shutemov if (list_empty(&info->shrinklist)) { 1003779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1004779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1005779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1006779750d2SKirill A. Shutemov } 1007779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1008779750d2SKirill A. Shutemov } 100994c1e62dSHugh Dickins } 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds 10126a1a90adSChristoph Hellwig setattr_copy(inode, attr); 1013db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 1014feda821eSChristoph Hellwig error = posix_acl_chmod(inode, inode->i_mode); 10151da177e4SLinus Torvalds return error; 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds 10181f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 10191da177e4SLinus Torvalds { 10201da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 1021779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 10221da177e4SLinus Torvalds 10233889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 10241da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 10251da177e4SLinus Torvalds inode->i_size = 0; 10263889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 1027779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1028779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1029779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1030779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 1031779750d2SKirill A. Shutemov sbinfo->shrinklist_len--; 1032779750d2SKirill A. Shutemov } 1033779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1034779750d2SKirill A. Shutemov } 10351da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 1036cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 10371da177e4SLinus Torvalds list_del_init(&info->swaplist); 1038cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 10391da177e4SLinus Torvalds } 10403ed47db3SAl Viro } 1041b09e0fa4SEric Paris 104238f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 10430f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 10445b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 1045dbd5768fSJan Kara clear_inode(inode); 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 104846f65ec1SHugh Dickins /* 104946f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 105046f65ec1SHugh Dickins */ 105141ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 1052bde05d1cSHugh Dickins swp_entry_t swap, struct page **pagep) 10531da177e4SLinus Torvalds { 1054285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 105546f65ec1SHugh Dickins void *radswap; 105641ffe5d5SHugh Dickins pgoff_t index; 1057bde05d1cSHugh Dickins gfp_t gfp; 1058bde05d1cSHugh Dickins int error = 0; 10591da177e4SLinus Torvalds 106046f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 1061e504f3fdSHugh Dickins index = radix_tree_locate_item(&mapping->page_tree, radswap); 106246f65ec1SHugh Dickins if (index == -1) 106300501b53SJohannes Weiner return -EAGAIN; /* tell shmem_unuse we found nothing */ 10642e0e26c7SHugh Dickins 10651b1b32f2SHugh Dickins /* 10661b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 10671f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 10681b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 1069285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 10701b1b32f2SHugh Dickins */ 10711b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 10722e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 10732e0e26c7SHugh Dickins 1074bde05d1cSHugh Dickins gfp = mapping_gfp_mask(mapping); 1075bde05d1cSHugh Dickins if (shmem_should_replace_page(*pagep, gfp)) { 1076bde05d1cSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1077bde05d1cSHugh Dickins error = shmem_replace_page(pagep, gfp, info, index); 1078bde05d1cSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 1079bde05d1cSHugh Dickins /* 1080bde05d1cSHugh Dickins * We needed to drop mutex to make that restrictive page 10810142ef6cSHugh Dickins * allocation, but the inode might have been freed while we 10820142ef6cSHugh Dickins * dropped it: although a racing shmem_evict_inode() cannot 10830142ef6cSHugh Dickins * complete without emptying the radix_tree, our page lock 10840142ef6cSHugh Dickins * on this swapcache page is not enough to prevent that - 10850142ef6cSHugh Dickins * free_swap_and_cache() of our swap entry will only 10860142ef6cSHugh Dickins * trylock_page(), removing swap from radix_tree whatever. 10870142ef6cSHugh Dickins * 10880142ef6cSHugh Dickins * We must not proceed to shmem_add_to_page_cache() if the 10890142ef6cSHugh Dickins * inode has been freed, but of course we cannot rely on 10900142ef6cSHugh Dickins * inode or mapping or info to check that. However, we can 10910142ef6cSHugh Dickins * safely check if our swap entry is still in use (and here 10920142ef6cSHugh Dickins * it can't have got reused for another page): if it's still 10930142ef6cSHugh Dickins * in use, then the inode cannot have been freed yet, and we 10940142ef6cSHugh Dickins * can safely proceed (if it's no longer in use, that tells 10950142ef6cSHugh Dickins * nothing about the inode, but we don't need to unuse swap). 1096bde05d1cSHugh Dickins */ 1097bde05d1cSHugh Dickins if (!page_swapcount(*pagep)) 1098bde05d1cSHugh Dickins error = -ENOENT; 1099bde05d1cSHugh Dickins } 1100bde05d1cSHugh Dickins 1101d13d1443SKAMEZAWA Hiroyuki /* 1102778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 1103778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 1104778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 1105d13d1443SKAMEZAWA Hiroyuki */ 1106bde05d1cSHugh Dickins if (!error) 1107bde05d1cSHugh Dickins error = shmem_add_to_page_cache(*pagep, mapping, index, 1108fed400a1SWang Sheng-Hui radswap); 110948f170fbSHugh Dickins if (error != -ENOMEM) { 111046f65ec1SHugh Dickins /* 111146f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 111246f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 111346f65ec1SHugh Dickins */ 1114bde05d1cSHugh Dickins delete_from_swap_cache(*pagep); 1115bde05d1cSHugh Dickins set_page_dirty(*pagep); 111646f65ec1SHugh Dickins if (!error) { 11174595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1118285b2c4fSHugh Dickins info->swapped--; 11194595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 112041ffe5d5SHugh Dickins swap_free(swap); 112146f65ec1SHugh Dickins } 11221da177e4SLinus Torvalds } 11232e0e26c7SHugh Dickins return error; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds /* 112746f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 11281da177e4SLinus Torvalds */ 112941ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 11301da177e4SLinus Torvalds { 113141ffe5d5SHugh Dickins struct list_head *this, *next; 11321da177e4SLinus Torvalds struct shmem_inode_info *info; 113300501b53SJohannes Weiner struct mem_cgroup *memcg; 1134bde05d1cSHugh Dickins int error = 0; 1135bde05d1cSHugh Dickins 1136bde05d1cSHugh Dickins /* 1137bde05d1cSHugh Dickins * There's a faint possibility that swap page was replaced before 11380142ef6cSHugh Dickins * caller locked it: caller will come back later with the right page. 1139bde05d1cSHugh Dickins */ 11400142ef6cSHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) 1141bde05d1cSHugh Dickins goto out; 1142778dd893SHugh Dickins 1143778dd893SHugh Dickins /* 1144778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 1145778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 1146778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 1147778dd893SHugh Dickins */ 1148f627c2f5SKirill A. Shutemov error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg, 1149f627c2f5SKirill A. Shutemov false); 1150778dd893SHugh Dickins if (error) 1151778dd893SHugh Dickins goto out; 115246f65ec1SHugh Dickins /* No radix_tree_preload: swap entry keeps a place for page in tree */ 115300501b53SJohannes Weiner error = -EAGAIN; 11541da177e4SLinus Torvalds 1155cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 115641ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 115741ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 1158285b2c4fSHugh Dickins if (info->swapped) 115900501b53SJohannes Weiner error = shmem_unuse_inode(info, swap, &page); 11606922c0c7SHugh Dickins else 11616922c0c7SHugh Dickins list_del_init(&info->swaplist); 1162cb5f7b9aSHugh Dickins cond_resched(); 116300501b53SJohannes Weiner if (error != -EAGAIN) 1164778dd893SHugh Dickins break; 116500501b53SJohannes Weiner /* found nothing in this: move on to search the next */ 11661da177e4SLinus Torvalds } 1167cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1168778dd893SHugh Dickins 116900501b53SJohannes Weiner if (error) { 117000501b53SJohannes Weiner if (error != -ENOMEM) 117100501b53SJohannes Weiner error = 0; 1172f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 117300501b53SJohannes Weiner } else 1174f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 1175778dd893SHugh Dickins out: 1176aaa46865SHugh Dickins unlock_page(page); 117709cbfeafSKirill A. Shutemov put_page(page); 1178778dd893SHugh Dickins return error; 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds /* 11821da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 11831da177e4SLinus Torvalds */ 11841da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 11851da177e4SLinus Torvalds { 11861da177e4SLinus Torvalds struct shmem_inode_info *info; 11871da177e4SLinus Torvalds struct address_space *mapping; 11881da177e4SLinus Torvalds struct inode *inode; 11896922c0c7SHugh Dickins swp_entry_t swap; 11906922c0c7SHugh Dickins pgoff_t index; 11911da177e4SLinus Torvalds 1192800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 11931da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 11941da177e4SLinus Torvalds mapping = page->mapping; 11951da177e4SLinus Torvalds index = page->index; 11961da177e4SLinus Torvalds inode = mapping->host; 11971da177e4SLinus Torvalds info = SHMEM_I(inode); 11981da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 11991da177e4SLinus Torvalds goto redirty; 1200d9fe526aSHugh Dickins if (!total_swap_pages) 12011da177e4SLinus Torvalds goto redirty; 12021da177e4SLinus Torvalds 1203d9fe526aSHugh Dickins /* 120497b713baSChristoph Hellwig * Our capabilities prevent regular writeback or sync from ever calling 120597b713baSChristoph Hellwig * shmem_writepage; but a stacking filesystem might use ->writepage of 120697b713baSChristoph Hellwig * its underlying filesystem, in which case tmpfs should write out to 120797b713baSChristoph Hellwig * swap only in response to memory pressure, and not for the writeback 120897b713baSChristoph Hellwig * threads or sync. 1209d9fe526aSHugh Dickins */ 121048f170fbSHugh Dickins if (!wbc->for_reclaim) { 121148f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 121248f170fbSHugh Dickins goto redirty; 121348f170fbSHugh Dickins } 12141635f6a7SHugh Dickins 12151635f6a7SHugh Dickins /* 12161635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 12171635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 12181635f6a7SHugh Dickins * fallocated page arriving here is now to initialize it and write it. 12191aac1400SHugh Dickins * 12201aac1400SHugh Dickins * That's okay for a page already fallocated earlier, but if we have 12211aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 12221aac1400SHugh Dickins * of this page in case we have to undo it, and (b) it may not be a 12231aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 12241aac1400SHugh Dickins * reactivate the page, and let shmem_fallocate() quit when too many. 12251635f6a7SHugh Dickins */ 12261635f6a7SHugh Dickins if (!PageUptodate(page)) { 12271aac1400SHugh Dickins if (inode->i_private) { 12281aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 12291aac1400SHugh Dickins spin_lock(&inode->i_lock); 12301aac1400SHugh Dickins shmem_falloc = inode->i_private; 12311aac1400SHugh Dickins if (shmem_falloc && 12328e205f77SHugh Dickins !shmem_falloc->waitq && 12331aac1400SHugh Dickins index >= shmem_falloc->start && 12341aac1400SHugh Dickins index < shmem_falloc->next) 12351aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 12361aac1400SHugh Dickins else 12371aac1400SHugh Dickins shmem_falloc = NULL; 12381aac1400SHugh Dickins spin_unlock(&inode->i_lock); 12391aac1400SHugh Dickins if (shmem_falloc) 12401aac1400SHugh Dickins goto redirty; 12411aac1400SHugh Dickins } 12421635f6a7SHugh Dickins clear_highpage(page); 12431635f6a7SHugh Dickins flush_dcache_page(page); 12441635f6a7SHugh Dickins SetPageUptodate(page); 12451635f6a7SHugh Dickins } 12461635f6a7SHugh Dickins 1247d9fe526aSHugh Dickins swap = get_swap_page(); 124848f170fbSHugh Dickins if (!swap.val) 124948f170fbSHugh Dickins goto redirty; 1250d9fe526aSHugh Dickins 125137e84351SVladimir Davydov if (mem_cgroup_try_charge_swap(page, swap)) 125237e84351SVladimir Davydov goto free_swap; 125337e84351SVladimir Davydov 1254b1dea800SHugh Dickins /* 1255b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 12566922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 12576922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 1258b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 12596922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 12606922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 1261b1dea800SHugh Dickins */ 1262b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 126305bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 126405bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 1265b1dea800SHugh Dickins 126648f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 12674595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1268267a4c76SHugh Dickins shmem_recalc_inode(inode); 1269267a4c76SHugh Dickins info->swapped++; 12704595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1271267a4c76SHugh Dickins 1272aaa46865SHugh Dickins swap_shmem_alloc(swap); 12736922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 12746922c0c7SHugh Dickins 12756922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1276d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 12779fab5619SHugh Dickins swap_writepage(page, wbc); 12781da177e4SLinus Torvalds return 0; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 12816922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 128237e84351SVladimir Davydov free_swap: 12830a31bc97SJohannes Weiner swapcache_free(swap); 12841da177e4SLinus Torvalds redirty: 12851da177e4SLinus Torvalds set_page_dirty(page); 1286d9fe526aSHugh Dickins if (wbc->for_reclaim) 1287d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 1288d9fe526aSHugh Dickins unlock_page(page); 1289d9fe526aSHugh Dickins return 0; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 129275edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 129371fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1294680d794bSakpm@linux-foundation.org { 1295680d794bSakpm@linux-foundation.org char buffer[64]; 1296680d794bSakpm@linux-foundation.org 129771fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1298095f1fc4SLee Schermerhorn return; /* show nothing */ 1299095f1fc4SLee Schermerhorn 1300a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 1301095f1fc4SLee Schermerhorn 1302095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1303680d794bSakpm@linux-foundation.org } 130471fe804bSLee Schermerhorn 130571fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 130671fe804bSLee Schermerhorn { 130771fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 130871fe804bSLee Schermerhorn if (sbinfo->mpol) { 130971fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 131071fe804bSLee Schermerhorn mpol = sbinfo->mpol; 131171fe804bSLee Schermerhorn mpol_get(mpol); 131271fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 131371fe804bSLee Schermerhorn } 131471fe804bSLee Schermerhorn return mpol; 131571fe804bSLee Schermerhorn } 131675edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 131775edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 131875edd345SHugh Dickins { 131975edd345SHugh Dickins } 132075edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 132175edd345SHugh Dickins { 132275edd345SHugh Dickins return NULL; 132375edd345SHugh Dickins } 132475edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 132575edd345SHugh Dickins #ifndef CONFIG_NUMA 132675edd345SHugh Dickins #define vm_policy vm_private_data 132775edd345SHugh Dickins #endif 1328680d794bSakpm@linux-foundation.org 1329800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma, 1330800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1331800d8c63SKirill A. Shutemov { 1332800d8c63SKirill A. Shutemov /* Create a pseudo vma that just contains the policy */ 1333800d8c63SKirill A. Shutemov vma->vm_start = 0; 1334800d8c63SKirill A. Shutemov /* Bias interleave by inode number to distribute better across nodes */ 1335800d8c63SKirill A. Shutemov vma->vm_pgoff = index + info->vfs_inode.i_ino; 1336800d8c63SKirill A. Shutemov vma->vm_ops = NULL; 1337800d8c63SKirill A. Shutemov vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); 1338800d8c63SKirill A. Shutemov } 1339800d8c63SKirill A. Shutemov 1340800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) 1341800d8c63SKirill A. Shutemov { 1342800d8c63SKirill A. Shutemov /* Drop reference taken by mpol_shared_policy_lookup() */ 1343800d8c63SKirill A. Shutemov mpol_cond_put(vma->vm_policy); 1344800d8c63SKirill A. Shutemov } 1345800d8c63SKirill A. Shutemov 134641ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 134741ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 13481da177e4SLinus Torvalds { 13491da177e4SLinus Torvalds struct vm_area_struct pvma; 135018a2f371SMel Gorman struct page *page; 13511da177e4SLinus Torvalds 1352800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 135318a2f371SMel Gorman page = swapin_readahead(swap, gfp, &pvma, 0); 1354800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 135518a2f371SMel Gorman 1356800d8c63SKirill A. Shutemov return page; 1357800d8c63SKirill A. Shutemov } 135818a2f371SMel Gorman 1359800d8c63SKirill A. Shutemov static struct page *shmem_alloc_hugepage(gfp_t gfp, 1360800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1361800d8c63SKirill A. Shutemov { 1362800d8c63SKirill A. Shutemov struct vm_area_struct pvma; 1363800d8c63SKirill A. Shutemov struct inode *inode = &info->vfs_inode; 1364800d8c63SKirill A. Shutemov struct address_space *mapping = inode->i_mapping; 13654620a06eSGeert Uytterhoeven pgoff_t idx, hindex; 1366800d8c63SKirill A. Shutemov void __rcu **results; 1367800d8c63SKirill A. Shutemov struct page *page; 1368800d8c63SKirill A. Shutemov 1369e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1370800d8c63SKirill A. Shutemov return NULL; 1371800d8c63SKirill A. Shutemov 13724620a06eSGeert Uytterhoeven hindex = round_down(index, HPAGE_PMD_NR); 1373800d8c63SKirill A. Shutemov rcu_read_lock(); 1374800d8c63SKirill A. Shutemov if (radix_tree_gang_lookup_slot(&mapping->page_tree, &results, &idx, 1375800d8c63SKirill A. Shutemov hindex, 1) && idx < hindex + HPAGE_PMD_NR) { 1376800d8c63SKirill A. Shutemov rcu_read_unlock(); 1377800d8c63SKirill A. Shutemov return NULL; 1378800d8c63SKirill A. Shutemov } 1379800d8c63SKirill A. Shutemov rcu_read_unlock(); 1380800d8c63SKirill A. Shutemov 1381800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, hindex); 1382800d8c63SKirill A. Shutemov page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN, 1383800d8c63SKirill A. Shutemov HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true); 1384800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 1385800d8c63SKirill A. Shutemov if (page) 1386800d8c63SKirill A. Shutemov prep_transhuge_page(page); 138718a2f371SMel Gorman return page; 138818a2f371SMel Gorman } 138918a2f371SMel Gorman 139018a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp, 139118a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 139218a2f371SMel Gorman { 139318a2f371SMel Gorman struct vm_area_struct pvma; 139418a2f371SMel Gorman struct page *page; 139518a2f371SMel Gorman 1396800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1397800d8c63SKirill A. Shutemov page = alloc_page_vma(gfp, &pvma, 0); 1398800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 139918a2f371SMel Gorman 1400800d8c63SKirill A. Shutemov return page; 1401800d8c63SKirill A. Shutemov } 1402800d8c63SKirill A. Shutemov 1403800d8c63SKirill A. Shutemov static struct page *shmem_alloc_and_acct_page(gfp_t gfp, 1404800d8c63SKirill A. Shutemov struct shmem_inode_info *info, struct shmem_sb_info *sbinfo, 1405800d8c63SKirill A. Shutemov pgoff_t index, bool huge) 1406800d8c63SKirill A. Shutemov { 1407800d8c63SKirill A. Shutemov struct page *page; 1408800d8c63SKirill A. Shutemov int nr; 1409800d8c63SKirill A. Shutemov int err = -ENOSPC; 1410800d8c63SKirill A. Shutemov 1411e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1412800d8c63SKirill A. Shutemov huge = false; 1413800d8c63SKirill A. Shutemov nr = huge ? HPAGE_PMD_NR : 1; 1414800d8c63SKirill A. Shutemov 1415800d8c63SKirill A. Shutemov if (shmem_acct_block(info->flags, nr)) 1416800d8c63SKirill A. Shutemov goto failed; 1417800d8c63SKirill A. Shutemov if (sbinfo->max_blocks) { 1418800d8c63SKirill A. Shutemov if (percpu_counter_compare(&sbinfo->used_blocks, 1419800d8c63SKirill A. Shutemov sbinfo->max_blocks - nr) > 0) 1420800d8c63SKirill A. Shutemov goto unacct; 1421800d8c63SKirill A. Shutemov percpu_counter_add(&sbinfo->used_blocks, nr); 1422800d8c63SKirill A. Shutemov } 1423800d8c63SKirill A. Shutemov 1424800d8c63SKirill A. Shutemov if (huge) 1425800d8c63SKirill A. Shutemov page = shmem_alloc_hugepage(gfp, info, index); 1426800d8c63SKirill A. Shutemov else 1427800d8c63SKirill A. Shutemov page = shmem_alloc_page(gfp, info, index); 142875edd345SHugh Dickins if (page) { 142975edd345SHugh Dickins __SetPageLocked(page); 143075edd345SHugh Dickins __SetPageSwapBacked(page); 1431800d8c63SKirill A. Shutemov return page; 143275edd345SHugh Dickins } 143318a2f371SMel Gorman 1434800d8c63SKirill A. Shutemov err = -ENOMEM; 1435800d8c63SKirill A. Shutemov if (sbinfo->max_blocks) 1436800d8c63SKirill A. Shutemov percpu_counter_add(&sbinfo->used_blocks, -nr); 1437800d8c63SKirill A. Shutemov unacct: 1438800d8c63SKirill A. Shutemov shmem_unacct_blocks(info->flags, nr); 1439800d8c63SKirill A. Shutemov failed: 1440800d8c63SKirill A. Shutemov return ERR_PTR(err); 14411da177e4SLinus Torvalds } 144271fe804bSLee Schermerhorn 14431da177e4SLinus Torvalds /* 1444bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 1445bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 1446bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 1447bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 1448bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 1449bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 1450bde05d1cSHugh Dickins * 1451bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 1452bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 1453bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 1454bde05d1cSHugh Dickins */ 1455bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp) 1456bde05d1cSHugh Dickins { 1457bde05d1cSHugh Dickins return page_zonenum(page) > gfp_zone(gfp); 1458bde05d1cSHugh Dickins } 1459bde05d1cSHugh Dickins 1460bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 1461bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 1462bde05d1cSHugh Dickins { 1463bde05d1cSHugh Dickins struct page *oldpage, *newpage; 1464bde05d1cSHugh Dickins struct address_space *swap_mapping; 1465bde05d1cSHugh Dickins pgoff_t swap_index; 1466bde05d1cSHugh Dickins int error; 1467bde05d1cSHugh Dickins 1468bde05d1cSHugh Dickins oldpage = *pagep; 1469bde05d1cSHugh Dickins swap_index = page_private(oldpage); 1470bde05d1cSHugh Dickins swap_mapping = page_mapping(oldpage); 1471bde05d1cSHugh Dickins 1472bde05d1cSHugh Dickins /* 1473bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1474bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1475bde05d1cSHugh Dickins */ 1476bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1477bde05d1cSHugh Dickins newpage = shmem_alloc_page(gfp, info, index); 1478bde05d1cSHugh Dickins if (!newpage) 1479bde05d1cSHugh Dickins return -ENOMEM; 1480bde05d1cSHugh Dickins 148109cbfeafSKirill A. Shutemov get_page(newpage); 1482bde05d1cSHugh Dickins copy_highpage(newpage, oldpage); 14830142ef6cSHugh Dickins flush_dcache_page(newpage); 1484bde05d1cSHugh Dickins 1485bde05d1cSHugh Dickins SetPageUptodate(newpage); 1486bde05d1cSHugh Dickins set_page_private(newpage, swap_index); 1487bde05d1cSHugh Dickins SetPageSwapCache(newpage); 1488bde05d1cSHugh Dickins 1489bde05d1cSHugh Dickins /* 1490bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1491bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1492bde05d1cSHugh Dickins */ 1493bde05d1cSHugh Dickins spin_lock_irq(&swap_mapping->tree_lock); 1494bde05d1cSHugh Dickins error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage, 1495bde05d1cSHugh Dickins newpage); 14960142ef6cSHugh Dickins if (!error) { 149711fb9989SMel Gorman __inc_node_page_state(newpage, NR_FILE_PAGES); 149811fb9989SMel Gorman __dec_node_page_state(oldpage, NR_FILE_PAGES); 14990142ef6cSHugh Dickins } 1500bde05d1cSHugh Dickins spin_unlock_irq(&swap_mapping->tree_lock); 1501bde05d1cSHugh Dickins 15020142ef6cSHugh Dickins if (unlikely(error)) { 15030142ef6cSHugh Dickins /* 15040142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 15050142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 15060142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 15070142ef6cSHugh Dickins */ 15080142ef6cSHugh Dickins oldpage = newpage; 15090142ef6cSHugh Dickins } else { 15106a93ca8fSJohannes Weiner mem_cgroup_migrate(oldpage, newpage); 1511bde05d1cSHugh Dickins lru_cache_add_anon(newpage); 15120142ef6cSHugh Dickins *pagep = newpage; 15130142ef6cSHugh Dickins } 1514bde05d1cSHugh Dickins 1515bde05d1cSHugh Dickins ClearPageSwapCache(oldpage); 1516bde05d1cSHugh Dickins set_page_private(oldpage, 0); 1517bde05d1cSHugh Dickins 1518bde05d1cSHugh Dickins unlock_page(oldpage); 151909cbfeafSKirill A. Shutemov put_page(oldpage); 152009cbfeafSKirill A. Shutemov put_page(oldpage); 15210142ef6cSHugh Dickins return error; 1522bde05d1cSHugh Dickins } 1523bde05d1cSHugh Dickins 1524bde05d1cSHugh Dickins /* 152568da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 15261da177e4SLinus Torvalds * 15271da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 15281da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 15299e18eb29SAndres Lagar-Cavilla * entry since a page cannot live in both the swap and page cache. 15309e18eb29SAndres Lagar-Cavilla * 15319e18eb29SAndres Lagar-Cavilla * fault_mm and fault_type are only supplied by shmem_fault: 15329e18eb29SAndres Lagar-Cavilla * otherwise they are NULL. 15331da177e4SLinus Torvalds */ 153441ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 15359e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, gfp_t gfp, 15369e18eb29SAndres Lagar-Cavilla struct mm_struct *fault_mm, int *fault_type) 15371da177e4SLinus Torvalds { 15381da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 153954af6042SHugh Dickins struct shmem_inode_info *info; 15401da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 15419e18eb29SAndres Lagar-Cavilla struct mm_struct *charge_mm; 154200501b53SJohannes Weiner struct mem_cgroup *memcg; 154327ab7006SHugh Dickins struct page *page; 15441da177e4SLinus Torvalds swp_entry_t swap; 1545657e3038SKirill A. Shutemov enum sgp_type sgp_huge = sgp; 1546800d8c63SKirill A. Shutemov pgoff_t hindex = index; 15471da177e4SLinus Torvalds int error; 154854af6042SHugh Dickins int once = 0; 15491635f6a7SHugh Dickins int alloced = 0; 15501da177e4SLinus Torvalds 155109cbfeafSKirill A. Shutemov if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 15521da177e4SLinus Torvalds return -EFBIG; 1553657e3038SKirill A. Shutemov if (sgp == SGP_NOHUGE || sgp == SGP_HUGE) 1554657e3038SKirill A. Shutemov sgp = SGP_CACHE; 15551da177e4SLinus Torvalds repeat: 155654af6042SHugh Dickins swap.val = 0; 15570cd6144aSJohannes Weiner page = find_lock_entry(mapping, index); 155854af6042SHugh Dickins if (radix_tree_exceptional_entry(page)) { 155954af6042SHugh Dickins swap = radix_to_swp_entry(page); 156054af6042SHugh Dickins page = NULL; 156154af6042SHugh Dickins } 156254af6042SHugh Dickins 156375edd345SHugh Dickins if (sgp <= SGP_CACHE && 156409cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 156554af6042SHugh Dickins error = -EINVAL; 1566267a4c76SHugh Dickins goto unlock; 156754af6042SHugh Dickins } 156854af6042SHugh Dickins 156966d2f4d2SHugh Dickins if (page && sgp == SGP_WRITE) 157066d2f4d2SHugh Dickins mark_page_accessed(page); 157166d2f4d2SHugh Dickins 15721635f6a7SHugh Dickins /* fallocated page? */ 15731635f6a7SHugh Dickins if (page && !PageUptodate(page)) { 15741635f6a7SHugh Dickins if (sgp != SGP_READ) 15751635f6a7SHugh Dickins goto clear; 15761635f6a7SHugh Dickins unlock_page(page); 157709cbfeafSKirill A. Shutemov put_page(page); 15781635f6a7SHugh Dickins page = NULL; 15791635f6a7SHugh Dickins } 158054af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 158154af6042SHugh Dickins *pagep = page; 158254af6042SHugh Dickins return 0; 158327ab7006SHugh Dickins } 158427ab7006SHugh Dickins 1585b409f9fcSHugh Dickins /* 158654af6042SHugh Dickins * Fast cache lookup did not find it: 158754af6042SHugh Dickins * bring it back from swap or allocate. 1588b409f9fcSHugh Dickins */ 158954af6042SHugh Dickins info = SHMEM_I(inode); 159054af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 15919e18eb29SAndres Lagar-Cavilla charge_mm = fault_mm ? : current->mm; 159227ab7006SHugh Dickins 15931da177e4SLinus Torvalds if (swap.val) { 15941da177e4SLinus Torvalds /* Look it up and read it in.. */ 159527ab7006SHugh Dickins page = lookup_swap_cache(swap); 159627ab7006SHugh Dickins if (!page) { 15979e18eb29SAndres Lagar-Cavilla /* Or update major stats only when swapin succeeds?? */ 15989e18eb29SAndres Lagar-Cavilla if (fault_type) { 159968da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 16009e18eb29SAndres Lagar-Cavilla count_vm_event(PGMAJFAULT); 16019e18eb29SAndres Lagar-Cavilla mem_cgroup_count_vm_event(fault_mm, PGMAJFAULT); 16029e18eb29SAndres Lagar-Cavilla } 16039e18eb29SAndres Lagar-Cavilla /* Here we actually start the io */ 160441ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 160527ab7006SHugh Dickins if (!page) { 16061da177e4SLinus Torvalds error = -ENOMEM; 160754af6042SHugh Dickins goto failed; 1608285b2c4fSHugh Dickins } 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 161254af6042SHugh Dickins lock_page(page); 16130142ef6cSHugh Dickins if (!PageSwapCache(page) || page_private(page) != swap.val || 1614d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1615bde05d1cSHugh Dickins error = -EEXIST; /* try again */ 1616d1899228SHugh Dickins goto unlock; 1617bde05d1cSHugh Dickins } 161827ab7006SHugh Dickins if (!PageUptodate(page)) { 16191da177e4SLinus Torvalds error = -EIO; 162054af6042SHugh Dickins goto failed; 162154af6042SHugh Dickins } 162254af6042SHugh Dickins wait_on_page_writeback(page); 162354af6042SHugh Dickins 1624bde05d1cSHugh Dickins if (shmem_should_replace_page(page, gfp)) { 1625bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 1626bde05d1cSHugh Dickins if (error) 162754af6042SHugh Dickins goto failed; 16281da177e4SLinus Torvalds } 16291da177e4SLinus Torvalds 16309e18eb29SAndres Lagar-Cavilla error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg, 1631f627c2f5SKirill A. Shutemov false); 1632d1899228SHugh Dickins if (!error) { 163354af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1634fed400a1SWang Sheng-Hui swp_to_radix_entry(swap)); 1635215c02bcSHugh Dickins /* 1636215c02bcSHugh Dickins * We already confirmed swap under page lock, and make 1637215c02bcSHugh Dickins * no memory allocation here, so usually no possibility 1638215c02bcSHugh Dickins * of error; but free_swap_and_cache() only trylocks a 1639215c02bcSHugh Dickins * page, so it is just possible that the entry has been 1640215c02bcSHugh Dickins * truncated or holepunched since swap was confirmed. 1641215c02bcSHugh Dickins * shmem_undo_range() will have done some of the 1642215c02bcSHugh Dickins * unaccounting, now delete_from_swap_cache() will do 164393aa7d95SVladimir Davydov * the rest. 1644215c02bcSHugh Dickins * Reset swap.val? No, leave it so "failed" goes back to 1645215c02bcSHugh Dickins * "repeat": reading a hole and writing should succeed. 1646215c02bcSHugh Dickins */ 164700501b53SJohannes Weiner if (error) { 1648f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1649215c02bcSHugh Dickins delete_from_swap_cache(page); 1650d1899228SHugh Dickins } 165100501b53SJohannes Weiner } 165254af6042SHugh Dickins if (error) 165354af6042SHugh Dickins goto failed; 165454af6042SHugh Dickins 1655f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 165600501b53SJohannes Weiner 16574595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 165854af6042SHugh Dickins info->swapped--; 165954af6042SHugh Dickins shmem_recalc_inode(inode); 16604595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 166127ab7006SHugh Dickins 166266d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 166366d2f4d2SHugh Dickins mark_page_accessed(page); 166466d2f4d2SHugh Dickins 166527ab7006SHugh Dickins delete_from_swap_cache(page); 166627ab7006SHugh Dickins set_page_dirty(page); 166727ab7006SHugh Dickins swap_free(swap); 166827ab7006SHugh Dickins 166954af6042SHugh Dickins } else { 1670800d8c63SKirill A. Shutemov /* shmem_symlink() */ 1671800d8c63SKirill A. Shutemov if (mapping->a_ops != &shmem_aops) 1672800d8c63SKirill A. Shutemov goto alloc_nohuge; 1673657e3038SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE) 1674800d8c63SKirill A. Shutemov goto alloc_nohuge; 1675800d8c63SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 1676800d8c63SKirill A. Shutemov goto alloc_huge; 1677800d8c63SKirill A. Shutemov switch (sbinfo->huge) { 1678800d8c63SKirill A. Shutemov loff_t i_size; 1679800d8c63SKirill A. Shutemov pgoff_t off; 1680800d8c63SKirill A. Shutemov case SHMEM_HUGE_NEVER: 1681800d8c63SKirill A. Shutemov goto alloc_nohuge; 1682800d8c63SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 1683800d8c63SKirill A. Shutemov off = round_up(index, HPAGE_PMD_NR); 1684800d8c63SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 1685800d8c63SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 1686800d8c63SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 1687800d8c63SKirill A. Shutemov goto alloc_huge; 1688800d8c63SKirill A. Shutemov /* fallthrough */ 1689800d8c63SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 1690657e3038SKirill A. Shutemov if (sgp_huge == SGP_HUGE) 1691657e3038SKirill A. Shutemov goto alloc_huge; 1692657e3038SKirill A. Shutemov /* TODO: implement fadvise() hints */ 1693800d8c63SKirill A. Shutemov goto alloc_nohuge; 169459a16eadSHugh Dickins } 16951da177e4SLinus Torvalds 1696800d8c63SKirill A. Shutemov alloc_huge: 1697800d8c63SKirill A. Shutemov page = shmem_alloc_and_acct_page(gfp, info, sbinfo, 1698800d8c63SKirill A. Shutemov index, true); 1699800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 1700800d8c63SKirill A. Shutemov alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, info, sbinfo, 1701800d8c63SKirill A. Shutemov index, false); 170254af6042SHugh Dickins } 1703800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 1704779750d2SKirill A. Shutemov int retry = 5; 1705800d8c63SKirill A. Shutemov error = PTR_ERR(page); 1706800d8c63SKirill A. Shutemov page = NULL; 1707779750d2SKirill A. Shutemov if (error != -ENOSPC) 1708779750d2SKirill A. Shutemov goto failed; 1709779750d2SKirill A. Shutemov /* 1710779750d2SKirill A. Shutemov * Try to reclaim some spece by splitting a huge page 1711779750d2SKirill A. Shutemov * beyond i_size on the filesystem. 1712779750d2SKirill A. Shutemov */ 1713779750d2SKirill A. Shutemov while (retry--) { 1714779750d2SKirill A. Shutemov int ret; 1715779750d2SKirill A. Shutemov ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); 1716779750d2SKirill A. Shutemov if (ret == SHRINK_STOP) 1717779750d2SKirill A. Shutemov break; 1718779750d2SKirill A. Shutemov if (ret) 1719779750d2SKirill A. Shutemov goto alloc_nohuge; 1720779750d2SKirill A. Shutemov } 1721800d8c63SKirill A. Shutemov goto failed; 1722800d8c63SKirill A. Shutemov } 1723800d8c63SKirill A. Shutemov 1724800d8c63SKirill A. Shutemov if (PageTransHuge(page)) 1725800d8c63SKirill A. Shutemov hindex = round_down(index, HPAGE_PMD_NR); 1726800d8c63SKirill A. Shutemov else 1727800d8c63SKirill A. Shutemov hindex = index; 1728800d8c63SKirill A. Shutemov 172966d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1730eb39d618SHugh Dickins __SetPageReferenced(page); 173166d2f4d2SHugh Dickins 17329e18eb29SAndres Lagar-Cavilla error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg, 1733800d8c63SKirill A. Shutemov PageTransHuge(page)); 173454af6042SHugh Dickins if (error) 1735800d8c63SKirill A. Shutemov goto unacct; 1736800d8c63SKirill A. Shutemov error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK, 1737800d8c63SKirill A. Shutemov compound_order(page)); 1738b065b432SHugh Dickins if (!error) { 1739800d8c63SKirill A. Shutemov error = shmem_add_to_page_cache(page, mapping, hindex, 1740fed400a1SWang Sheng-Hui NULL); 1741b065b432SHugh Dickins radix_tree_preload_end(); 1742b065b432SHugh Dickins } 1743b065b432SHugh Dickins if (error) { 1744800d8c63SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, 1745800d8c63SKirill A. Shutemov PageTransHuge(page)); 1746800d8c63SKirill A. Shutemov goto unacct; 1747b065b432SHugh Dickins } 1748800d8c63SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, 1749800d8c63SKirill A. Shutemov PageTransHuge(page)); 175054af6042SHugh Dickins lru_cache_add_anon(page); 175154af6042SHugh Dickins 17524595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1753800d8c63SKirill A. Shutemov info->alloced += 1 << compound_order(page); 1754800d8c63SKirill A. Shutemov inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page); 175554af6042SHugh Dickins shmem_recalc_inode(inode); 17564595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 17571635f6a7SHugh Dickins alloced = true; 175854af6042SHugh Dickins 1759779750d2SKirill A. Shutemov if (PageTransHuge(page) && 1760779750d2SKirill A. Shutemov DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 1761779750d2SKirill A. Shutemov hindex + HPAGE_PMD_NR - 1) { 1762779750d2SKirill A. Shutemov /* 1763779750d2SKirill A. Shutemov * Part of the huge page is beyond i_size: subject 1764779750d2SKirill A. Shutemov * to shrink under memory pressure. 1765779750d2SKirill A. Shutemov */ 1766779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1767779750d2SKirill A. Shutemov if (list_empty(&info->shrinklist)) { 1768779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1769779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1770779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1771779750d2SKirill A. Shutemov } 1772779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1773779750d2SKirill A. Shutemov } 1774779750d2SKirill A. Shutemov 1775ec9516fbSHugh Dickins /* 17761635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 17771635f6a7SHugh Dickins */ 17781635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 17791635f6a7SHugh Dickins sgp = SGP_WRITE; 17801635f6a7SHugh Dickins clear: 17811635f6a7SHugh Dickins /* 17821635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 17831635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 17841635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1785ec9516fbSHugh Dickins */ 1786800d8c63SKirill A. Shutemov if (sgp != SGP_WRITE && !PageUptodate(page)) { 1787800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 1788800d8c63SKirill A. Shutemov int i; 1789800d8c63SKirill A. Shutemov 1790800d8c63SKirill A. Shutemov for (i = 0; i < (1 << compound_order(head)); i++) { 1791800d8c63SKirill A. Shutemov clear_highpage(head + i); 1792800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 1793800d8c63SKirill A. Shutemov } 1794800d8c63SKirill A. Shutemov SetPageUptodate(head); 1795ec9516fbSHugh Dickins } 17961da177e4SLinus Torvalds } 1797bde05d1cSHugh Dickins 179854af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 179975edd345SHugh Dickins if (sgp <= SGP_CACHE && 180009cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1801267a4c76SHugh Dickins if (alloced) { 1802267a4c76SHugh Dickins ClearPageDirty(page); 1803267a4c76SHugh Dickins delete_from_page_cache(page); 18044595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1805267a4c76SHugh Dickins shmem_recalc_inode(inode); 18064595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1807267a4c76SHugh Dickins } 180854af6042SHugh Dickins error = -EINVAL; 1809267a4c76SHugh Dickins goto unlock; 1810ff36b801SShaohua Li } 1811800d8c63SKirill A. Shutemov *pagep = page + index - hindex; 181254af6042SHugh Dickins return 0; 1813d00806b1SNick Piggin 1814d0217ac0SNick Piggin /* 181554af6042SHugh Dickins * Error recovery. 18161da177e4SLinus Torvalds */ 181754af6042SHugh Dickins unacct: 1818800d8c63SKirill A. Shutemov if (sbinfo->max_blocks) 1819800d8c63SKirill A. Shutemov percpu_counter_sub(&sbinfo->used_blocks, 1820800d8c63SKirill A. Shutemov 1 << compound_order(page)); 1821800d8c63SKirill A. Shutemov shmem_unacct_blocks(info->flags, 1 << compound_order(page)); 1822800d8c63SKirill A. Shutemov 1823800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 1824800d8c63SKirill A. Shutemov unlock_page(page); 1825800d8c63SKirill A. Shutemov put_page(page); 1826800d8c63SKirill A. Shutemov goto alloc_nohuge; 1827800d8c63SKirill A. Shutemov } 182854af6042SHugh Dickins failed: 1829267a4c76SHugh Dickins if (swap.val && !shmem_confirm_swap(mapping, index, swap)) 183054af6042SHugh Dickins error = -EEXIST; 1831d1899228SHugh Dickins unlock: 183227ab7006SHugh Dickins if (page) { 183354af6042SHugh Dickins unlock_page(page); 183409cbfeafSKirill A. Shutemov put_page(page); 183554af6042SHugh Dickins } 183654af6042SHugh Dickins if (error == -ENOSPC && !once++) { 183754af6042SHugh Dickins info = SHMEM_I(inode); 18384595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 183954af6042SHugh Dickins shmem_recalc_inode(inode); 18404595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 18411da177e4SLinus Torvalds goto repeat; 1842d8dc74f2SAdrian Bunk } 1843d1899228SHugh Dickins if (error == -EEXIST) /* from above or from radix_tree_insert */ 184454af6042SHugh Dickins goto repeat; 184554af6042SHugh Dickins return error; 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 18491da177e4SLinus Torvalds { 1850496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 18519e18eb29SAndres Lagar-Cavilla gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 1852657e3038SKirill A. Shutemov enum sgp_type sgp; 18531da177e4SLinus Torvalds int error; 185468da9f05SHugh Dickins int ret = VM_FAULT_LOCKED; 18551da177e4SLinus Torvalds 1856f00cdc6dSHugh Dickins /* 1857f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 1858f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 1859f00cdc6dSHugh Dickins * locks writers out with its hold on i_mutex. So refrain from 18608e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 18618e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 18628e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 18638e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 18648e205f77SHugh Dickins * 18658e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 18668e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 18678e205f77SHugh Dickins * we just need to make racing faults a rare case. 18688e205f77SHugh Dickins * 18698e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 18708e205f77SHugh Dickins * standard mutex or completion: but we cannot take i_mutex in fault, 18718e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 1872f00cdc6dSHugh Dickins */ 1873f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 1874f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 1875f00cdc6dSHugh Dickins 1876f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 1877f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 18788e205f77SHugh Dickins if (shmem_falloc && 18798e205f77SHugh Dickins shmem_falloc->waitq && 18808e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 18818e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 18828e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 18838e205f77SHugh Dickins DEFINE_WAIT(shmem_fault_wait); 18848e205f77SHugh Dickins 18858e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 1886f00cdc6dSHugh Dickins if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) && 1887f00cdc6dSHugh Dickins !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 18888e205f77SHugh Dickins /* It's polite to up mmap_sem if we can */ 1889f00cdc6dSHugh Dickins up_read(&vma->vm_mm->mmap_sem); 18908e205f77SHugh Dickins ret = VM_FAULT_RETRY; 1891f00cdc6dSHugh Dickins } 18928e205f77SHugh Dickins 18938e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 18948e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 18958e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 18968e205f77SHugh Dickins spin_unlock(&inode->i_lock); 18978e205f77SHugh Dickins schedule(); 18988e205f77SHugh Dickins 18998e205f77SHugh Dickins /* 19008e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 19018e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 19028e205f77SHugh Dickins * is usually invalid by the time we reach here, but 19038e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 19048e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 19058e205f77SHugh Dickins */ 19068e205f77SHugh Dickins spin_lock(&inode->i_lock); 19078e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 19088e205f77SHugh Dickins spin_unlock(&inode->i_lock); 19098e205f77SHugh Dickins return ret; 1910f00cdc6dSHugh Dickins } 19118e205f77SHugh Dickins spin_unlock(&inode->i_lock); 1912f00cdc6dSHugh Dickins } 1913f00cdc6dSHugh Dickins 1914657e3038SKirill A. Shutemov sgp = SGP_CACHE; 1915657e3038SKirill A. Shutemov if (vma->vm_flags & VM_HUGEPAGE) 1916657e3038SKirill A. Shutemov sgp = SGP_HUGE; 1917657e3038SKirill A. Shutemov else if (vma->vm_flags & VM_NOHUGEPAGE) 1918657e3038SKirill A. Shutemov sgp = SGP_NOHUGE; 1919657e3038SKirill A. Shutemov 1920657e3038SKirill A. Shutemov error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp, 19219e18eb29SAndres Lagar-Cavilla gfp, vma->vm_mm, &ret); 19221da177e4SLinus Torvalds if (error) 19231da177e4SLinus Torvalds return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 192468da9f05SHugh Dickins return ret; 19251da177e4SLinus Torvalds } 19261da177e4SLinus Torvalds 1927c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 1928c01d5b30SHugh Dickins unsigned long uaddr, unsigned long len, 1929c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 1930c01d5b30SHugh Dickins { 1931c01d5b30SHugh Dickins unsigned long (*get_area)(struct file *, 1932c01d5b30SHugh Dickins unsigned long, unsigned long, unsigned long, unsigned long); 1933c01d5b30SHugh Dickins unsigned long addr; 1934c01d5b30SHugh Dickins unsigned long offset; 1935c01d5b30SHugh Dickins unsigned long inflated_len; 1936c01d5b30SHugh Dickins unsigned long inflated_addr; 1937c01d5b30SHugh Dickins unsigned long inflated_offset; 1938c01d5b30SHugh Dickins 1939c01d5b30SHugh Dickins if (len > TASK_SIZE) 1940c01d5b30SHugh Dickins return -ENOMEM; 1941c01d5b30SHugh Dickins 1942c01d5b30SHugh Dickins get_area = current->mm->get_unmapped_area; 1943c01d5b30SHugh Dickins addr = get_area(file, uaddr, len, pgoff, flags); 1944c01d5b30SHugh Dickins 1945e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1946c01d5b30SHugh Dickins return addr; 1947c01d5b30SHugh Dickins if (IS_ERR_VALUE(addr)) 1948c01d5b30SHugh Dickins return addr; 1949c01d5b30SHugh Dickins if (addr & ~PAGE_MASK) 1950c01d5b30SHugh Dickins return addr; 1951c01d5b30SHugh Dickins if (addr > TASK_SIZE - len) 1952c01d5b30SHugh Dickins return addr; 1953c01d5b30SHugh Dickins 1954c01d5b30SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 1955c01d5b30SHugh Dickins return addr; 1956c01d5b30SHugh Dickins if (len < HPAGE_PMD_SIZE) 1957c01d5b30SHugh Dickins return addr; 1958c01d5b30SHugh Dickins if (flags & MAP_FIXED) 1959c01d5b30SHugh Dickins return addr; 1960c01d5b30SHugh Dickins /* 1961c01d5b30SHugh Dickins * Our priority is to support MAP_SHARED mapped hugely; 1962c01d5b30SHugh Dickins * and support MAP_PRIVATE mapped hugely too, until it is COWed. 1963c01d5b30SHugh Dickins * But if caller specified an address hint, respect that as before. 1964c01d5b30SHugh Dickins */ 1965c01d5b30SHugh Dickins if (uaddr) 1966c01d5b30SHugh Dickins return addr; 1967c01d5b30SHugh Dickins 1968c01d5b30SHugh Dickins if (shmem_huge != SHMEM_HUGE_FORCE) { 1969c01d5b30SHugh Dickins struct super_block *sb; 1970c01d5b30SHugh Dickins 1971c01d5b30SHugh Dickins if (file) { 1972c01d5b30SHugh Dickins VM_BUG_ON(file->f_op != &shmem_file_operations); 1973c01d5b30SHugh Dickins sb = file_inode(file)->i_sb; 1974c01d5b30SHugh Dickins } else { 1975c01d5b30SHugh Dickins /* 1976c01d5b30SHugh Dickins * Called directly from mm/mmap.c, or drivers/char/mem.c 1977c01d5b30SHugh Dickins * for "/dev/zero", to create a shared anonymous object. 1978c01d5b30SHugh Dickins */ 1979c01d5b30SHugh Dickins if (IS_ERR(shm_mnt)) 1980c01d5b30SHugh Dickins return addr; 1981c01d5b30SHugh Dickins sb = shm_mnt->mnt_sb; 1982c01d5b30SHugh Dickins } 1983c01d5b30SHugh Dickins if (SHMEM_SB(sb)->huge != SHMEM_HUGE_NEVER) 1984c01d5b30SHugh Dickins return addr; 1985c01d5b30SHugh Dickins } 1986c01d5b30SHugh Dickins 1987c01d5b30SHugh Dickins offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); 1988c01d5b30SHugh Dickins if (offset && offset + len < 2 * HPAGE_PMD_SIZE) 1989c01d5b30SHugh Dickins return addr; 1990c01d5b30SHugh Dickins if ((addr & (HPAGE_PMD_SIZE-1)) == offset) 1991c01d5b30SHugh Dickins return addr; 1992c01d5b30SHugh Dickins 1993c01d5b30SHugh Dickins inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; 1994c01d5b30SHugh Dickins if (inflated_len > TASK_SIZE) 1995c01d5b30SHugh Dickins return addr; 1996c01d5b30SHugh Dickins if (inflated_len < len) 1997c01d5b30SHugh Dickins return addr; 1998c01d5b30SHugh Dickins 1999c01d5b30SHugh Dickins inflated_addr = get_area(NULL, 0, inflated_len, 0, flags); 2000c01d5b30SHugh Dickins if (IS_ERR_VALUE(inflated_addr)) 2001c01d5b30SHugh Dickins return addr; 2002c01d5b30SHugh Dickins if (inflated_addr & ~PAGE_MASK) 2003c01d5b30SHugh Dickins return addr; 2004c01d5b30SHugh Dickins 2005c01d5b30SHugh Dickins inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); 2006c01d5b30SHugh Dickins inflated_addr += offset - inflated_offset; 2007c01d5b30SHugh Dickins if (inflated_offset > offset) 2008c01d5b30SHugh Dickins inflated_addr += HPAGE_PMD_SIZE; 2009c01d5b30SHugh Dickins 2010c01d5b30SHugh Dickins if (inflated_addr > TASK_SIZE - len) 2011c01d5b30SHugh Dickins return addr; 2012c01d5b30SHugh Dickins return inflated_addr; 2013c01d5b30SHugh Dickins } 2014c01d5b30SHugh Dickins 20151da177e4SLinus Torvalds #ifdef CONFIG_NUMA 201641ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 20171da177e4SLinus Torvalds { 2018496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 201941ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds 2022d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2023d8dc74f2SAdrian Bunk unsigned long addr) 20241da177e4SLinus Torvalds { 2025496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 202641ffe5d5SHugh Dickins pgoff_t index; 20271da177e4SLinus Torvalds 202841ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 202941ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 20301da177e4SLinus Torvalds } 20311da177e4SLinus Torvalds #endif 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 20341da177e4SLinus Torvalds { 2035496ad9aaSAl Viro struct inode *inode = file_inode(file); 20361da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 20371da177e4SLinus Torvalds int retval = -ENOMEM; 20381da177e4SLinus Torvalds 20394595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 20401da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 20411da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 20421da177e4SLinus Torvalds goto out_nomem; 20431da177e4SLinus Torvalds info->flags |= VM_LOCKED; 204489e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 20451da177e4SLinus Torvalds } 20461da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 20471da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 20481da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 204989e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 20501da177e4SLinus Torvalds } 20511da177e4SLinus Torvalds retval = 0; 205289e004eaSLee Schermerhorn 20531da177e4SLinus Torvalds out_nomem: 20544595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 20551da177e4SLinus Torvalds return retval; 20561da177e4SLinus Torvalds } 20571da177e4SLinus Torvalds 20589b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 20591da177e4SLinus Torvalds { 20601da177e4SLinus Torvalds file_accessed(file); 20611da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2062e496cf3dSKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 2063f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 2064f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 2065f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 2066f3f0e1d2SKirill A. Shutemov } 20671da177e4SLinus Torvalds return 0; 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 2070454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 207109208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 20721da177e4SLinus Torvalds { 20731da177e4SLinus Torvalds struct inode *inode; 20741da177e4SLinus Torvalds struct shmem_inode_info *info; 20751da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 20761da177e4SLinus Torvalds 20775b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 20781da177e4SLinus Torvalds return NULL; 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds inode = new_inode(sb); 20811da177e4SLinus Torvalds if (inode) { 208285fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 2083454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 20841da177e4SLinus Torvalds inode->i_blocks = 0; 20851da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 208691828a40SDavid M. Grimes inode->i_generation = get_seconds(); 20871da177e4SLinus Torvalds info = SHMEM_I(inode); 20881da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 20891da177e4SLinus Torvalds spin_lock_init(&info->lock); 209040e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 20910b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 2092779750d2SKirill A. Shutemov INIT_LIST_HEAD(&info->shrinklist); 20931da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 209438f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 209572c04902SAl Viro cache_no_acl(inode); 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds switch (mode & S_IFMT) { 20981da177e4SLinus Torvalds default: 209939f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 21001da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 21011da177e4SLinus Torvalds break; 21021da177e4SLinus Torvalds case S_IFREG: 210314fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 21041da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 21051da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 210671fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 210771fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 21081da177e4SLinus Torvalds break; 21091da177e4SLinus Torvalds case S_IFDIR: 2110d8c76e6fSDave Hansen inc_nlink(inode); 21111da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 21121da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 21131da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 21141da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 21151da177e4SLinus Torvalds break; 21161da177e4SLinus Torvalds case S_IFLNK: 21171da177e4SLinus Torvalds /* 21181da177e4SLinus Torvalds * Must not load anything in the rbtree, 21191da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 21201da177e4SLinus Torvalds */ 212171fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 21221da177e4SLinus Torvalds break; 21231da177e4SLinus Torvalds } 21245b04c689SPavel Emelyanov } else 21255b04c689SPavel Emelyanov shmem_free_inode(sb); 21261da177e4SLinus Torvalds return inode; 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds 21290cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping) 21300cd6144aSJohannes Weiner { 2131f0774d88SSasha Levin if (!mapping->host) 2132f0774d88SSasha Levin return false; 2133f0774d88SSasha Levin 213497b713baSChristoph Hellwig return mapping->host->i_sb->s_op == &shmem_ops; 21350cd6144aSJohannes Weiner } 21360cd6144aSJohannes Weiner 21371da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 213892e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 213969f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 21401da177e4SLinus Torvalds 21416d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR 21426d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 21436d9d88d0SJarkko Sakkinen #else 21446d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL 21456d9d88d0SJarkko Sakkinen #endif 21466d9d88d0SJarkko Sakkinen 21471da177e4SLinus Torvalds static int 2148800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 2149800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 2150800d15a5SNick Piggin struct page **pagep, void **fsdata) 21511da177e4SLinus Torvalds { 2152800d15a5SNick Piggin struct inode *inode = mapping->host; 215340e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 215409cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 215540e041a2SDavid Herrmann 215640e041a2SDavid Herrmann /* i_mutex is held by caller */ 215740e041a2SDavid Herrmann if (unlikely(info->seals)) { 215840e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) 215940e041a2SDavid Herrmann return -EPERM; 216040e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 216140e041a2SDavid Herrmann return -EPERM; 216240e041a2SDavid Herrmann } 216340e041a2SDavid Herrmann 21649e18eb29SAndres Lagar-Cavilla return shmem_getpage(inode, index, pagep, SGP_WRITE); 2165800d15a5SNick Piggin } 2166800d15a5SNick Piggin 2167800d15a5SNick Piggin static int 2168800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 2169800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 2170800d15a5SNick Piggin struct page *page, void *fsdata) 2171800d15a5SNick Piggin { 2172800d15a5SNick Piggin struct inode *inode = mapping->host; 2173800d15a5SNick Piggin 2174800d15a5SNick Piggin if (pos + copied > inode->i_size) 2175800d15a5SNick Piggin i_size_write(inode, pos + copied); 2176800d15a5SNick Piggin 2177ec9516fbSHugh Dickins if (!PageUptodate(page)) { 2178800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 2179800d8c63SKirill A. Shutemov if (PageTransCompound(page)) { 2180800d8c63SKirill A. Shutemov int i; 2181800d8c63SKirill A. Shutemov 2182800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 2183800d8c63SKirill A. Shutemov if (head + i == page) 2184800d8c63SKirill A. Shutemov continue; 2185800d8c63SKirill A. Shutemov clear_highpage(head + i); 2186800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 2187800d8c63SKirill A. Shutemov } 2188800d8c63SKirill A. Shutemov } 218909cbfeafSKirill A. Shutemov if (copied < PAGE_SIZE) { 219009cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 2191ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 219209cbfeafSKirill A. Shutemov from + copied, PAGE_SIZE); 2193ec9516fbSHugh Dickins } 2194800d8c63SKirill A. Shutemov SetPageUptodate(head); 2195ec9516fbSHugh Dickins } 2196d3602444SHugh Dickins set_page_dirty(page); 21976746aff7SWu Fengguang unlock_page(page); 219809cbfeafSKirill A. Shutemov put_page(page); 2199d3602444SHugh Dickins 2200800d15a5SNick Piggin return copied; 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds 22032ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 22041da177e4SLinus Torvalds { 22056e58e79dSAl Viro struct file *file = iocb->ki_filp; 22066e58e79dSAl Viro struct inode *inode = file_inode(file); 22071da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 220841ffe5d5SHugh Dickins pgoff_t index; 220941ffe5d5SHugh Dickins unsigned long offset; 2210a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 2211f7c1d074SGeert Uytterhoeven int error = 0; 2212cb66a7a1SAl Viro ssize_t retval = 0; 22136e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 2214a0ee5ec5SHugh Dickins 2215a0ee5ec5SHugh Dickins /* 2216a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 2217a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 2218a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 2219a0ee5ec5SHugh Dickins */ 2220777eda2cSAl Viro if (!iter_is_iovec(to)) 222175edd345SHugh Dickins sgp = SGP_CACHE; 22221da177e4SLinus Torvalds 222309cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 222409cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 22251da177e4SLinus Torvalds 22261da177e4SLinus Torvalds for (;;) { 22271da177e4SLinus Torvalds struct page *page = NULL; 222841ffe5d5SHugh Dickins pgoff_t end_index; 222941ffe5d5SHugh Dickins unsigned long nr, ret; 22301da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 22311da177e4SLinus Torvalds 223209cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 22331da177e4SLinus Torvalds if (index > end_index) 22341da177e4SLinus Torvalds break; 22351da177e4SLinus Torvalds if (index == end_index) { 223609cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 22371da177e4SLinus Torvalds if (nr <= offset) 22381da177e4SLinus Torvalds break; 22391da177e4SLinus Torvalds } 22401da177e4SLinus Torvalds 22419e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, sgp); 22426e58e79dSAl Viro if (error) { 22436e58e79dSAl Viro if (error == -EINVAL) 22446e58e79dSAl Viro error = 0; 22451da177e4SLinus Torvalds break; 22461da177e4SLinus Torvalds } 224775edd345SHugh Dickins if (page) { 224875edd345SHugh Dickins if (sgp == SGP_CACHE) 224975edd345SHugh Dickins set_page_dirty(page); 2250d3602444SHugh Dickins unlock_page(page); 225175edd345SHugh Dickins } 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds /* 22541da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 22551b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 22561da177e4SLinus Torvalds */ 225709cbfeafSKirill A. Shutemov nr = PAGE_SIZE; 22581da177e4SLinus Torvalds i_size = i_size_read(inode); 225909cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 22601da177e4SLinus Torvalds if (index == end_index) { 226109cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 22621da177e4SLinus Torvalds if (nr <= offset) { 22631da177e4SLinus Torvalds if (page) 226409cbfeafSKirill A. Shutemov put_page(page); 22651da177e4SLinus Torvalds break; 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds nr -= offset; 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds if (page) { 22711da177e4SLinus Torvalds /* 22721da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 22731da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 22741da177e4SLinus Torvalds * before reading the page on the kernel side. 22751da177e4SLinus Torvalds */ 22761da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 22771da177e4SLinus Torvalds flush_dcache_page(page); 22781da177e4SLinus Torvalds /* 22791da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 22801da177e4SLinus Torvalds */ 22811da177e4SLinus Torvalds if (!offset) 22821da177e4SLinus Torvalds mark_page_accessed(page); 2283b5810039SNick Piggin } else { 22841da177e4SLinus Torvalds page = ZERO_PAGE(0); 228509cbfeafSKirill A. Shutemov get_page(page); 2286b5810039SNick Piggin } 22871da177e4SLinus Torvalds 22881da177e4SLinus Torvalds /* 22891da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 22901da177e4SLinus Torvalds * now we can copy it to user space... 22911da177e4SLinus Torvalds */ 22922ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 22936e58e79dSAl Viro retval += ret; 22941da177e4SLinus Torvalds offset += ret; 229509cbfeafSKirill A. Shutemov index += offset >> PAGE_SHIFT; 229609cbfeafSKirill A. Shutemov offset &= ~PAGE_MASK; 22971da177e4SLinus Torvalds 229809cbfeafSKirill A. Shutemov put_page(page); 22992ba5bbedSAl Viro if (!iov_iter_count(to)) 23001da177e4SLinus Torvalds break; 23016e58e79dSAl Viro if (ret < nr) { 23026e58e79dSAl Viro error = -EFAULT; 23036e58e79dSAl Viro break; 23046e58e79dSAl Viro } 23051da177e4SLinus Torvalds cond_resched(); 23061da177e4SLinus Torvalds } 23071da177e4SLinus Torvalds 230809cbfeafSKirill A. Shutemov *ppos = ((loff_t) index << PAGE_SHIFT) + offset; 23096e58e79dSAl Viro file_accessed(file); 23106e58e79dSAl Viro return retval ? retval : error; 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds 2313708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 2314708e3508SHugh Dickins struct pipe_inode_info *pipe, size_t len, 2315708e3508SHugh Dickins unsigned int flags) 2316708e3508SHugh Dickins { 2317708e3508SHugh Dickins struct address_space *mapping = in->f_mapping; 231871f0e07aSHugh Dickins struct inode *inode = mapping->host; 2319708e3508SHugh Dickins unsigned int loff, nr_pages, req_pages; 2320708e3508SHugh Dickins struct page *pages[PIPE_DEF_BUFFERS]; 2321708e3508SHugh Dickins struct partial_page partial[PIPE_DEF_BUFFERS]; 2322708e3508SHugh Dickins struct page *page; 2323708e3508SHugh Dickins pgoff_t index, end_index; 2324708e3508SHugh Dickins loff_t isize, left; 2325708e3508SHugh Dickins int error, page_nr; 2326708e3508SHugh Dickins struct splice_pipe_desc spd = { 2327708e3508SHugh Dickins .pages = pages, 2328708e3508SHugh Dickins .partial = partial, 2329047fe360SEric Dumazet .nr_pages_max = PIPE_DEF_BUFFERS, 2330708e3508SHugh Dickins .flags = flags, 2331708e3508SHugh Dickins .ops = &page_cache_pipe_buf_ops, 2332708e3508SHugh Dickins .spd_release = spd_release_page, 2333708e3508SHugh Dickins }; 2334708e3508SHugh Dickins 233571f0e07aSHugh Dickins isize = i_size_read(inode); 2336708e3508SHugh Dickins if (unlikely(*ppos >= isize)) 2337708e3508SHugh Dickins return 0; 2338708e3508SHugh Dickins 2339708e3508SHugh Dickins left = isize - *ppos; 2340708e3508SHugh Dickins if (unlikely(left < len)) 2341708e3508SHugh Dickins len = left; 2342708e3508SHugh Dickins 2343708e3508SHugh Dickins if (splice_grow_spd(pipe, &spd)) 2344708e3508SHugh Dickins return -ENOMEM; 2345708e3508SHugh Dickins 234609cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 234709cbfeafSKirill A. Shutemov loff = *ppos & ~PAGE_MASK; 234809cbfeafSKirill A. Shutemov req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT; 2349a786c06dSAl Viro nr_pages = min(req_pages, spd.nr_pages_max); 2350708e3508SHugh Dickins 2351708e3508SHugh Dickins spd.nr_pages = find_get_pages_contig(mapping, index, 2352708e3508SHugh Dickins nr_pages, spd.pages); 2353708e3508SHugh Dickins index += spd.nr_pages; 2354708e3508SHugh Dickins error = 0; 235571f0e07aSHugh Dickins 2356708e3508SHugh Dickins while (spd.nr_pages < nr_pages) { 23579e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, SGP_CACHE); 235871f0e07aSHugh Dickins if (error) 2359708e3508SHugh Dickins break; 2360708e3508SHugh Dickins unlock_page(page); 2361708e3508SHugh Dickins spd.pages[spd.nr_pages++] = page; 2362708e3508SHugh Dickins index++; 2363708e3508SHugh Dickins } 2364708e3508SHugh Dickins 236509cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 2366708e3508SHugh Dickins nr_pages = spd.nr_pages; 2367708e3508SHugh Dickins spd.nr_pages = 0; 236871f0e07aSHugh Dickins 2369708e3508SHugh Dickins for (page_nr = 0; page_nr < nr_pages; page_nr++) { 2370708e3508SHugh Dickins unsigned int this_len; 2371708e3508SHugh Dickins 2372708e3508SHugh Dickins if (!len) 2373708e3508SHugh Dickins break; 2374708e3508SHugh Dickins 237509cbfeafSKirill A. Shutemov this_len = min_t(unsigned long, len, PAGE_SIZE - loff); 2376708e3508SHugh Dickins page = spd.pages[page_nr]; 2377708e3508SHugh Dickins 237871f0e07aSHugh Dickins if (!PageUptodate(page) || page->mapping != mapping) { 23799e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, SGP_CACHE); 238071f0e07aSHugh Dickins if (error) 2381708e3508SHugh Dickins break; 238271f0e07aSHugh Dickins unlock_page(page); 238309cbfeafSKirill A. Shutemov put_page(spd.pages[page_nr]); 2384708e3508SHugh Dickins spd.pages[page_nr] = page; 2385708e3508SHugh Dickins } 2386708e3508SHugh Dickins 238771f0e07aSHugh Dickins isize = i_size_read(inode); 238809cbfeafSKirill A. Shutemov end_index = (isize - 1) >> PAGE_SHIFT; 2389708e3508SHugh Dickins if (unlikely(!isize || index > end_index)) 2390708e3508SHugh Dickins break; 2391708e3508SHugh Dickins 2392708e3508SHugh Dickins if (end_index == index) { 2393708e3508SHugh Dickins unsigned int plen; 2394708e3508SHugh Dickins 239509cbfeafSKirill A. Shutemov plen = ((isize - 1) & ~PAGE_MASK) + 1; 2396708e3508SHugh Dickins if (plen <= loff) 2397708e3508SHugh Dickins break; 2398708e3508SHugh Dickins 2399708e3508SHugh Dickins this_len = min(this_len, plen - loff); 2400708e3508SHugh Dickins len = this_len; 2401708e3508SHugh Dickins } 2402708e3508SHugh Dickins 2403708e3508SHugh Dickins spd.partial[page_nr].offset = loff; 2404708e3508SHugh Dickins spd.partial[page_nr].len = this_len; 2405708e3508SHugh Dickins len -= this_len; 2406708e3508SHugh Dickins loff = 0; 2407708e3508SHugh Dickins spd.nr_pages++; 2408708e3508SHugh Dickins index++; 2409708e3508SHugh Dickins } 2410708e3508SHugh Dickins 2411708e3508SHugh Dickins while (page_nr < nr_pages) 241209cbfeafSKirill A. Shutemov put_page(spd.pages[page_nr++]); 2413708e3508SHugh Dickins 2414708e3508SHugh Dickins if (spd.nr_pages) 2415708e3508SHugh Dickins error = splice_to_pipe(pipe, &spd); 2416708e3508SHugh Dickins 2417047fe360SEric Dumazet splice_shrink_spd(&spd); 2418708e3508SHugh Dickins 2419708e3508SHugh Dickins if (error > 0) { 2420708e3508SHugh Dickins *ppos += error; 2421708e3508SHugh Dickins file_accessed(in); 2422708e3508SHugh Dickins } 2423708e3508SHugh Dickins return error; 2424708e3508SHugh Dickins } 2425708e3508SHugh Dickins 2426220f2ac9SHugh Dickins /* 2427220f2ac9SHugh Dickins * llseek SEEK_DATA or SEEK_HOLE through the radix_tree. 2428220f2ac9SHugh Dickins */ 2429220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping, 2430965c8e59SAndrew Morton pgoff_t index, pgoff_t end, int whence) 2431220f2ac9SHugh Dickins { 2432220f2ac9SHugh Dickins struct page *page; 2433220f2ac9SHugh Dickins struct pagevec pvec; 2434220f2ac9SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 2435220f2ac9SHugh Dickins bool done = false; 2436220f2ac9SHugh Dickins int i; 2437220f2ac9SHugh Dickins 2438220f2ac9SHugh Dickins pagevec_init(&pvec, 0); 2439220f2ac9SHugh Dickins pvec.nr = 1; /* start small: we may be there already */ 2440220f2ac9SHugh Dickins while (!done) { 24410cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 2442220f2ac9SHugh Dickins pvec.nr, pvec.pages, indices); 2443220f2ac9SHugh Dickins if (!pvec.nr) { 2444965c8e59SAndrew Morton if (whence == SEEK_DATA) 2445220f2ac9SHugh Dickins index = end; 2446220f2ac9SHugh Dickins break; 2447220f2ac9SHugh Dickins } 2448220f2ac9SHugh Dickins for (i = 0; i < pvec.nr; i++, index++) { 2449220f2ac9SHugh Dickins if (index < indices[i]) { 2450965c8e59SAndrew Morton if (whence == SEEK_HOLE) { 2451220f2ac9SHugh Dickins done = true; 2452220f2ac9SHugh Dickins break; 2453220f2ac9SHugh Dickins } 2454220f2ac9SHugh Dickins index = indices[i]; 2455220f2ac9SHugh Dickins } 2456220f2ac9SHugh Dickins page = pvec.pages[i]; 2457220f2ac9SHugh Dickins if (page && !radix_tree_exceptional_entry(page)) { 2458220f2ac9SHugh Dickins if (!PageUptodate(page)) 2459220f2ac9SHugh Dickins page = NULL; 2460220f2ac9SHugh Dickins } 2461220f2ac9SHugh Dickins if (index >= end || 2462965c8e59SAndrew Morton (page && whence == SEEK_DATA) || 2463965c8e59SAndrew Morton (!page && whence == SEEK_HOLE)) { 2464220f2ac9SHugh Dickins done = true; 2465220f2ac9SHugh Dickins break; 2466220f2ac9SHugh Dickins } 2467220f2ac9SHugh Dickins } 24680cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 2469220f2ac9SHugh Dickins pagevec_release(&pvec); 2470220f2ac9SHugh Dickins pvec.nr = PAGEVEC_SIZE; 2471220f2ac9SHugh Dickins cond_resched(); 2472220f2ac9SHugh Dickins } 2473220f2ac9SHugh Dickins return index; 2474220f2ac9SHugh Dickins } 2475220f2ac9SHugh Dickins 2476965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 2477220f2ac9SHugh Dickins { 2478220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 2479220f2ac9SHugh Dickins struct inode *inode = mapping->host; 2480220f2ac9SHugh Dickins pgoff_t start, end; 2481220f2ac9SHugh Dickins loff_t new_offset; 2482220f2ac9SHugh Dickins 2483965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 2484965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 2485220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 24865955102cSAl Viro inode_lock(inode); 2487220f2ac9SHugh Dickins /* We're holding i_mutex so we can access i_size directly */ 2488220f2ac9SHugh Dickins 2489220f2ac9SHugh Dickins if (offset < 0) 2490220f2ac9SHugh Dickins offset = -EINVAL; 2491220f2ac9SHugh Dickins else if (offset >= inode->i_size) 2492220f2ac9SHugh Dickins offset = -ENXIO; 2493220f2ac9SHugh Dickins else { 249409cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 249509cbfeafSKirill A. Shutemov end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; 2496965c8e59SAndrew Morton new_offset = shmem_seek_hole_data(mapping, start, end, whence); 249709cbfeafSKirill A. Shutemov new_offset <<= PAGE_SHIFT; 2498220f2ac9SHugh Dickins if (new_offset > offset) { 2499220f2ac9SHugh Dickins if (new_offset < inode->i_size) 2500220f2ac9SHugh Dickins offset = new_offset; 2501965c8e59SAndrew Morton else if (whence == SEEK_DATA) 2502220f2ac9SHugh Dickins offset = -ENXIO; 2503220f2ac9SHugh Dickins else 2504220f2ac9SHugh Dickins offset = inode->i_size; 2505220f2ac9SHugh Dickins } 2506220f2ac9SHugh Dickins } 2507220f2ac9SHugh Dickins 2508387aae6fSHugh Dickins if (offset >= 0) 250946a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 25105955102cSAl Viro inode_unlock(inode); 2511220f2ac9SHugh Dickins return offset; 2512220f2ac9SHugh Dickins } 2513220f2ac9SHugh Dickins 251405f65b5cSDavid Herrmann /* 251505f65b5cSDavid Herrmann * We need a tag: a new tag would expand every radix_tree_node by 8 bytes, 251605f65b5cSDavid Herrmann * so reuse a tag which we firmly believe is never set or cleared on shmem. 251705f65b5cSDavid Herrmann */ 251805f65b5cSDavid Herrmann #define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE 251905f65b5cSDavid Herrmann #define LAST_SCAN 4 /* about 150ms max */ 252005f65b5cSDavid Herrmann 252105f65b5cSDavid Herrmann static void shmem_tag_pins(struct address_space *mapping) 252205f65b5cSDavid Herrmann { 252305f65b5cSDavid Herrmann struct radix_tree_iter iter; 252405f65b5cSDavid Herrmann void **slot; 252505f65b5cSDavid Herrmann pgoff_t start; 252605f65b5cSDavid Herrmann struct page *page; 252705f65b5cSDavid Herrmann 252805f65b5cSDavid Herrmann lru_add_drain(); 252905f65b5cSDavid Herrmann start = 0; 253005f65b5cSDavid Herrmann rcu_read_lock(); 253105f65b5cSDavid Herrmann 253205f65b5cSDavid Herrmann radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { 253305f65b5cSDavid Herrmann page = radix_tree_deref_slot(slot); 253405f65b5cSDavid Herrmann if (!page || radix_tree_exception(page)) { 25352cf938aaSMatthew Wilcox if (radix_tree_deref_retry(page)) { 25362cf938aaSMatthew Wilcox slot = radix_tree_iter_retry(&iter); 25372cf938aaSMatthew Wilcox continue; 25382cf938aaSMatthew Wilcox } 253905f65b5cSDavid Herrmann } else if (page_count(page) - page_mapcount(page) > 1) { 254005f65b5cSDavid Herrmann spin_lock_irq(&mapping->tree_lock); 254105f65b5cSDavid Herrmann radix_tree_tag_set(&mapping->page_tree, iter.index, 254205f65b5cSDavid Herrmann SHMEM_TAG_PINNED); 254305f65b5cSDavid Herrmann spin_unlock_irq(&mapping->tree_lock); 254405f65b5cSDavid Herrmann } 254505f65b5cSDavid Herrmann 254605f65b5cSDavid Herrmann if (need_resched()) { 254705f65b5cSDavid Herrmann cond_resched_rcu(); 25487165092fSMatthew Wilcox slot = radix_tree_iter_next(&iter); 254905f65b5cSDavid Herrmann } 255005f65b5cSDavid Herrmann } 255105f65b5cSDavid Herrmann rcu_read_unlock(); 255205f65b5cSDavid Herrmann } 255305f65b5cSDavid Herrmann 255405f65b5cSDavid Herrmann /* 255505f65b5cSDavid Herrmann * Setting SEAL_WRITE requires us to verify there's no pending writer. However, 255605f65b5cSDavid Herrmann * via get_user_pages(), drivers might have some pending I/O without any active 255705f65b5cSDavid Herrmann * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages 255805f65b5cSDavid Herrmann * and see whether it has an elevated ref-count. If so, we tag them and wait for 255905f65b5cSDavid Herrmann * them to be dropped. 256005f65b5cSDavid Herrmann * The caller must guarantee that no new user will acquire writable references 256105f65b5cSDavid Herrmann * to those pages to avoid races. 256205f65b5cSDavid Herrmann */ 256340e041a2SDavid Herrmann static int shmem_wait_for_pins(struct address_space *mapping) 256440e041a2SDavid Herrmann { 256505f65b5cSDavid Herrmann struct radix_tree_iter iter; 256605f65b5cSDavid Herrmann void **slot; 256705f65b5cSDavid Herrmann pgoff_t start; 256805f65b5cSDavid Herrmann struct page *page; 256905f65b5cSDavid Herrmann int error, scan; 257005f65b5cSDavid Herrmann 257105f65b5cSDavid Herrmann shmem_tag_pins(mapping); 257205f65b5cSDavid Herrmann 257305f65b5cSDavid Herrmann error = 0; 257405f65b5cSDavid Herrmann for (scan = 0; scan <= LAST_SCAN; scan++) { 257505f65b5cSDavid Herrmann if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED)) 257605f65b5cSDavid Herrmann break; 257705f65b5cSDavid Herrmann 257805f65b5cSDavid Herrmann if (!scan) 257905f65b5cSDavid Herrmann lru_add_drain_all(); 258005f65b5cSDavid Herrmann else if (schedule_timeout_killable((HZ << scan) / 200)) 258105f65b5cSDavid Herrmann scan = LAST_SCAN; 258205f65b5cSDavid Herrmann 258305f65b5cSDavid Herrmann start = 0; 258405f65b5cSDavid Herrmann rcu_read_lock(); 258505f65b5cSDavid Herrmann radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 258605f65b5cSDavid Herrmann start, SHMEM_TAG_PINNED) { 258705f65b5cSDavid Herrmann 258805f65b5cSDavid Herrmann page = radix_tree_deref_slot(slot); 258905f65b5cSDavid Herrmann if (radix_tree_exception(page)) { 25902cf938aaSMatthew Wilcox if (radix_tree_deref_retry(page)) { 25912cf938aaSMatthew Wilcox slot = radix_tree_iter_retry(&iter); 25922cf938aaSMatthew Wilcox continue; 25932cf938aaSMatthew Wilcox } 259405f65b5cSDavid Herrmann 259505f65b5cSDavid Herrmann page = NULL; 259605f65b5cSDavid Herrmann } 259705f65b5cSDavid Herrmann 259805f65b5cSDavid Herrmann if (page && 259905f65b5cSDavid Herrmann page_count(page) - page_mapcount(page) != 1) { 260005f65b5cSDavid Herrmann if (scan < LAST_SCAN) 260105f65b5cSDavid Herrmann goto continue_resched; 260205f65b5cSDavid Herrmann 260305f65b5cSDavid Herrmann /* 260405f65b5cSDavid Herrmann * On the last scan, we clean up all those tags 260505f65b5cSDavid Herrmann * we inserted; but make a note that we still 260605f65b5cSDavid Herrmann * found pages pinned. 260705f65b5cSDavid Herrmann */ 260805f65b5cSDavid Herrmann error = -EBUSY; 260905f65b5cSDavid Herrmann } 261005f65b5cSDavid Herrmann 261105f65b5cSDavid Herrmann spin_lock_irq(&mapping->tree_lock); 261205f65b5cSDavid Herrmann radix_tree_tag_clear(&mapping->page_tree, 261305f65b5cSDavid Herrmann iter.index, SHMEM_TAG_PINNED); 261405f65b5cSDavid Herrmann spin_unlock_irq(&mapping->tree_lock); 261505f65b5cSDavid Herrmann continue_resched: 261605f65b5cSDavid Herrmann if (need_resched()) { 261705f65b5cSDavid Herrmann cond_resched_rcu(); 26187165092fSMatthew Wilcox slot = radix_tree_iter_next(&iter); 261905f65b5cSDavid Herrmann } 262005f65b5cSDavid Herrmann } 262105f65b5cSDavid Herrmann rcu_read_unlock(); 262205f65b5cSDavid Herrmann } 262305f65b5cSDavid Herrmann 262405f65b5cSDavid Herrmann return error; 262540e041a2SDavid Herrmann } 262640e041a2SDavid Herrmann 262740e041a2SDavid Herrmann #define F_ALL_SEALS (F_SEAL_SEAL | \ 262840e041a2SDavid Herrmann F_SEAL_SHRINK | \ 262940e041a2SDavid Herrmann F_SEAL_GROW | \ 263040e041a2SDavid Herrmann F_SEAL_WRITE) 263140e041a2SDavid Herrmann 263240e041a2SDavid Herrmann int shmem_add_seals(struct file *file, unsigned int seals) 263340e041a2SDavid Herrmann { 263440e041a2SDavid Herrmann struct inode *inode = file_inode(file); 263540e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 263640e041a2SDavid Herrmann int error; 263740e041a2SDavid Herrmann 263840e041a2SDavid Herrmann /* 263940e041a2SDavid Herrmann * SEALING 264040e041a2SDavid Herrmann * Sealing allows multiple parties to share a shmem-file but restrict 264140e041a2SDavid Herrmann * access to a specific subset of file operations. Seals can only be 264240e041a2SDavid Herrmann * added, but never removed. This way, mutually untrusted parties can 264340e041a2SDavid Herrmann * share common memory regions with a well-defined policy. A malicious 264440e041a2SDavid Herrmann * peer can thus never perform unwanted operations on a shared object. 264540e041a2SDavid Herrmann * 264640e041a2SDavid Herrmann * Seals are only supported on special shmem-files and always affect 264740e041a2SDavid Herrmann * the whole underlying inode. Once a seal is set, it may prevent some 264840e041a2SDavid Herrmann * kinds of access to the file. Currently, the following seals are 264940e041a2SDavid Herrmann * defined: 265040e041a2SDavid Herrmann * SEAL_SEAL: Prevent further seals from being set on this file 265140e041a2SDavid Herrmann * SEAL_SHRINK: Prevent the file from shrinking 265240e041a2SDavid Herrmann * SEAL_GROW: Prevent the file from growing 265340e041a2SDavid Herrmann * SEAL_WRITE: Prevent write access to the file 265440e041a2SDavid Herrmann * 265540e041a2SDavid Herrmann * As we don't require any trust relationship between two parties, we 265640e041a2SDavid Herrmann * must prevent seals from being removed. Therefore, sealing a file 265740e041a2SDavid Herrmann * only adds a given set of seals to the file, it never touches 265840e041a2SDavid Herrmann * existing seals. Furthermore, the "setting seals"-operation can be 265940e041a2SDavid Herrmann * sealed itself, which basically prevents any further seal from being 266040e041a2SDavid Herrmann * added. 266140e041a2SDavid Herrmann * 266240e041a2SDavid Herrmann * Semantics of sealing are only defined on volatile files. Only 266340e041a2SDavid Herrmann * anonymous shmem files support sealing. More importantly, seals are 266440e041a2SDavid Herrmann * never written to disk. Therefore, there's no plan to support it on 266540e041a2SDavid Herrmann * other file types. 266640e041a2SDavid Herrmann */ 266740e041a2SDavid Herrmann 266840e041a2SDavid Herrmann if (file->f_op != &shmem_file_operations) 266940e041a2SDavid Herrmann return -EINVAL; 267040e041a2SDavid Herrmann if (!(file->f_mode & FMODE_WRITE)) 267140e041a2SDavid Herrmann return -EPERM; 267240e041a2SDavid Herrmann if (seals & ~(unsigned int)F_ALL_SEALS) 267340e041a2SDavid Herrmann return -EINVAL; 267440e041a2SDavid Herrmann 26755955102cSAl Viro inode_lock(inode); 267640e041a2SDavid Herrmann 267740e041a2SDavid Herrmann if (info->seals & F_SEAL_SEAL) { 267840e041a2SDavid Herrmann error = -EPERM; 267940e041a2SDavid Herrmann goto unlock; 268040e041a2SDavid Herrmann } 268140e041a2SDavid Herrmann 268240e041a2SDavid Herrmann if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) { 268340e041a2SDavid Herrmann error = mapping_deny_writable(file->f_mapping); 268440e041a2SDavid Herrmann if (error) 268540e041a2SDavid Herrmann goto unlock; 268640e041a2SDavid Herrmann 268740e041a2SDavid Herrmann error = shmem_wait_for_pins(file->f_mapping); 268840e041a2SDavid Herrmann if (error) { 268940e041a2SDavid Herrmann mapping_allow_writable(file->f_mapping); 269040e041a2SDavid Herrmann goto unlock; 269140e041a2SDavid Herrmann } 269240e041a2SDavid Herrmann } 269340e041a2SDavid Herrmann 269440e041a2SDavid Herrmann info->seals |= seals; 269540e041a2SDavid Herrmann error = 0; 269640e041a2SDavid Herrmann 269740e041a2SDavid Herrmann unlock: 26985955102cSAl Viro inode_unlock(inode); 269940e041a2SDavid Herrmann return error; 270040e041a2SDavid Herrmann } 270140e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_add_seals); 270240e041a2SDavid Herrmann 270340e041a2SDavid Herrmann int shmem_get_seals(struct file *file) 270440e041a2SDavid Herrmann { 270540e041a2SDavid Herrmann if (file->f_op != &shmem_file_operations) 270640e041a2SDavid Herrmann return -EINVAL; 270740e041a2SDavid Herrmann 270840e041a2SDavid Herrmann return SHMEM_I(file_inode(file))->seals; 270940e041a2SDavid Herrmann } 271040e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_get_seals); 271140e041a2SDavid Herrmann 271240e041a2SDavid Herrmann long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg) 271340e041a2SDavid Herrmann { 271440e041a2SDavid Herrmann long error; 271540e041a2SDavid Herrmann 271640e041a2SDavid Herrmann switch (cmd) { 271740e041a2SDavid Herrmann case F_ADD_SEALS: 271840e041a2SDavid Herrmann /* disallow upper 32bit */ 271940e041a2SDavid Herrmann if (arg > UINT_MAX) 272040e041a2SDavid Herrmann return -EINVAL; 272140e041a2SDavid Herrmann 272240e041a2SDavid Herrmann error = shmem_add_seals(file, arg); 272340e041a2SDavid Herrmann break; 272440e041a2SDavid Herrmann case F_GET_SEALS: 272540e041a2SDavid Herrmann error = shmem_get_seals(file); 272640e041a2SDavid Herrmann break; 272740e041a2SDavid Herrmann default: 272840e041a2SDavid Herrmann error = -EINVAL; 272940e041a2SDavid Herrmann break; 273040e041a2SDavid Herrmann } 273140e041a2SDavid Herrmann 273240e041a2SDavid Herrmann return error; 273340e041a2SDavid Herrmann } 273440e041a2SDavid Herrmann 273583e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 273683e4fa9cSHugh Dickins loff_t len) 273783e4fa9cSHugh Dickins { 2738496ad9aaSAl Viro struct inode *inode = file_inode(file); 2739e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 274040e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 27411aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 2742e2d12e22SHugh Dickins pgoff_t start, index, end; 2743e2d12e22SHugh Dickins int error; 274483e4fa9cSHugh Dickins 274513ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 274613ace4d0SHugh Dickins return -EOPNOTSUPP; 274713ace4d0SHugh Dickins 27485955102cSAl Viro inode_lock(inode); 274983e4fa9cSHugh Dickins 275083e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 275183e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 275283e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 275383e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 27548e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 275583e4fa9cSHugh Dickins 275640e041a2SDavid Herrmann /* protected by i_mutex */ 275740e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) { 275840e041a2SDavid Herrmann error = -EPERM; 275940e041a2SDavid Herrmann goto out; 276040e041a2SDavid Herrmann } 276140e041a2SDavid Herrmann 27628e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 2763f00cdc6dSHugh Dickins shmem_falloc.start = unmap_start >> PAGE_SHIFT; 2764f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 2765f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2766f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 2767f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 2768f00cdc6dSHugh Dickins 276983e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 277083e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 277183e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 277283e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 277383e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 27748e205f77SHugh Dickins 27758e205f77SHugh Dickins spin_lock(&inode->i_lock); 27768e205f77SHugh Dickins inode->i_private = NULL; 27778e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 27788e205f77SHugh Dickins spin_unlock(&inode->i_lock); 277983e4fa9cSHugh Dickins error = 0; 27808e205f77SHugh Dickins goto out; 278183e4fa9cSHugh Dickins } 278283e4fa9cSHugh Dickins 2783e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 2784e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 2785e2d12e22SHugh Dickins if (error) 2786e2d12e22SHugh Dickins goto out; 2787e2d12e22SHugh Dickins 278840e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 278940e041a2SDavid Herrmann error = -EPERM; 279040e041a2SDavid Herrmann goto out; 279140e041a2SDavid Herrmann } 279240e041a2SDavid Herrmann 279309cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 279409cbfeafSKirill A. Shutemov end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 2795e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 2796e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 2797e2d12e22SHugh Dickins error = -ENOSPC; 2798e2d12e22SHugh Dickins goto out; 2799e2d12e22SHugh Dickins } 2800e2d12e22SHugh Dickins 28018e205f77SHugh Dickins shmem_falloc.waitq = NULL; 28021aac1400SHugh Dickins shmem_falloc.start = start; 28031aac1400SHugh Dickins shmem_falloc.next = start; 28041aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 28051aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 28061aac1400SHugh Dickins spin_lock(&inode->i_lock); 28071aac1400SHugh Dickins inode->i_private = &shmem_falloc; 28081aac1400SHugh Dickins spin_unlock(&inode->i_lock); 28091aac1400SHugh Dickins 2810e2d12e22SHugh Dickins for (index = start; index < end; index++) { 2811e2d12e22SHugh Dickins struct page *page; 2812e2d12e22SHugh Dickins 2813e2d12e22SHugh Dickins /* 2814e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 2815e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 2816e2d12e22SHugh Dickins */ 2817e2d12e22SHugh Dickins if (signal_pending(current)) 2818e2d12e22SHugh Dickins error = -EINTR; 28191aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 28201aac1400SHugh Dickins error = -ENOMEM; 2821e2d12e22SHugh Dickins else 28229e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, SGP_FALLOC); 2823e2d12e22SHugh Dickins if (error) { 28241635f6a7SHugh Dickins /* Remove the !PageUptodate pages we added */ 28257f556567SHugh Dickins if (index > start) { 28261635f6a7SHugh Dickins shmem_undo_range(inode, 282709cbfeafSKirill A. Shutemov (loff_t)start << PAGE_SHIFT, 2828b9b4bb26SAnthony Romano ((loff_t)index << PAGE_SHIFT) - 1, true); 28297f556567SHugh Dickins } 28301aac1400SHugh Dickins goto undone; 2831e2d12e22SHugh Dickins } 2832e2d12e22SHugh Dickins 2833e2d12e22SHugh Dickins /* 28341aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 28351aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 28361aac1400SHugh Dickins */ 28371aac1400SHugh Dickins shmem_falloc.next++; 28381aac1400SHugh Dickins if (!PageUptodate(page)) 28391aac1400SHugh Dickins shmem_falloc.nr_falloced++; 28401aac1400SHugh Dickins 28411aac1400SHugh Dickins /* 28421635f6a7SHugh Dickins * If !PageUptodate, leave it that way so that freeable pages 28431635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 28441635f6a7SHugh Dickins * But set_page_dirty so that memory pressure will swap rather 2845e2d12e22SHugh Dickins * than free the pages we are allocating (and SGP_CACHE pages 2846e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 2847e2d12e22SHugh Dickins */ 2848e2d12e22SHugh Dickins set_page_dirty(page); 2849e2d12e22SHugh Dickins unlock_page(page); 285009cbfeafSKirill A. Shutemov put_page(page); 2851e2d12e22SHugh Dickins cond_resched(); 2852e2d12e22SHugh Dickins } 2853e2d12e22SHugh Dickins 2854e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 2855e2d12e22SHugh Dickins i_size_write(inode, offset + len); 2856e2d12e22SHugh Dickins inode->i_ctime = CURRENT_TIME; 28571aac1400SHugh Dickins undone: 28581aac1400SHugh Dickins spin_lock(&inode->i_lock); 28591aac1400SHugh Dickins inode->i_private = NULL; 28601aac1400SHugh Dickins spin_unlock(&inode->i_lock); 2861e2d12e22SHugh Dickins out: 28625955102cSAl Viro inode_unlock(inode); 286383e4fa9cSHugh Dickins return error; 286483e4fa9cSHugh Dickins } 286583e4fa9cSHugh Dickins 2866726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 28671da177e4SLinus Torvalds { 2868726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 287109cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 28721da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 28730edd73b3SHugh Dickins if (sbinfo->max_blocks) { 28741da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 287541ffe5d5SHugh Dickins buf->f_bavail = 287641ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 287741ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 28780edd73b3SHugh Dickins } 28790edd73b3SHugh Dickins if (sbinfo->max_inodes) { 28801da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 28811da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 28821da177e4SLinus Torvalds } 28831da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 28841da177e4SLinus Torvalds return 0; 28851da177e4SLinus Torvalds } 28861da177e4SLinus Torvalds 28871da177e4SLinus Torvalds /* 28881da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 28891da177e4SLinus Torvalds */ 28901da177e4SLinus Torvalds static int 28911a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 28921da177e4SLinus Torvalds { 28930b0a0806SHugh Dickins struct inode *inode; 28941da177e4SLinus Torvalds int error = -ENOSPC; 28951da177e4SLinus Torvalds 2896454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 28971da177e4SLinus Torvalds if (inode) { 2898feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2899feda821eSChristoph Hellwig if (error) 2900feda821eSChristoph Hellwig goto out_iput; 29012a7dba39SEric Paris error = security_inode_init_security(inode, dir, 29029d8f13baSMimi Zohar &dentry->d_name, 29036d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2904feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2905feda821eSChristoph Hellwig goto out_iput; 290637ec43cdSMimi Zohar 2907718deb6bSAl Viro error = 0; 29081da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 29091da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 29101da177e4SLinus Torvalds d_instantiate(dentry, inode); 29111da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 29121da177e4SLinus Torvalds } 29131da177e4SLinus Torvalds return error; 2914feda821eSChristoph Hellwig out_iput: 2915feda821eSChristoph Hellwig iput(inode); 2916feda821eSChristoph Hellwig return error; 29171da177e4SLinus Torvalds } 29181da177e4SLinus Torvalds 291960545d0dSAl Viro static int 292060545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 292160545d0dSAl Viro { 292260545d0dSAl Viro struct inode *inode; 292360545d0dSAl Viro int error = -ENOSPC; 292460545d0dSAl Viro 292560545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 292660545d0dSAl Viro if (inode) { 292760545d0dSAl Viro error = security_inode_init_security(inode, dir, 292860545d0dSAl Viro NULL, 292960545d0dSAl Viro shmem_initxattrs, NULL); 2930feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2931feda821eSChristoph Hellwig goto out_iput; 2932feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2933feda821eSChristoph Hellwig if (error) 2934feda821eSChristoph Hellwig goto out_iput; 293560545d0dSAl Viro d_tmpfile(dentry, inode); 293660545d0dSAl Viro } 293760545d0dSAl Viro return error; 2938feda821eSChristoph Hellwig out_iput: 2939feda821eSChristoph Hellwig iput(inode); 2940feda821eSChristoph Hellwig return error; 294160545d0dSAl Viro } 294260545d0dSAl Viro 294318bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 29441da177e4SLinus Torvalds { 29451da177e4SLinus Torvalds int error; 29461da177e4SLinus Torvalds 29471da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 29481da177e4SLinus Torvalds return error; 2949d8c76e6fSDave Hansen inc_nlink(dir); 29501da177e4SLinus Torvalds return 0; 29511da177e4SLinus Torvalds } 29521da177e4SLinus Torvalds 29534acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2954ebfc3b49SAl Viro bool excl) 29551da177e4SLinus Torvalds { 29561da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 29571da177e4SLinus Torvalds } 29581da177e4SLinus Torvalds 29591da177e4SLinus Torvalds /* 29601da177e4SLinus Torvalds * Link a file.. 29611da177e4SLinus Torvalds */ 29621da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 29631da177e4SLinus Torvalds { 296475c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 29655b04c689SPavel Emelyanov int ret; 29661da177e4SLinus Torvalds 29671da177e4SLinus Torvalds /* 29681da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 29691da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 29701da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 29711da177e4SLinus Torvalds */ 29725b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 29735b04c689SPavel Emelyanov if (ret) 29745b04c689SPavel Emelyanov goto out; 29751da177e4SLinus Torvalds 29761da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 29771da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 2978d8c76e6fSDave Hansen inc_nlink(inode); 29797de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 29801da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 29811da177e4SLinus Torvalds d_instantiate(dentry, inode); 29825b04c689SPavel Emelyanov out: 29835b04c689SPavel Emelyanov return ret; 29841da177e4SLinus Torvalds } 29851da177e4SLinus Torvalds 29861da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 29871da177e4SLinus Torvalds { 298875c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 29891da177e4SLinus Torvalds 29905b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 29915b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 29941da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 29959a53c3a7SDave Hansen drop_nlink(inode); 29961da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 29971da177e4SLinus Torvalds return 0; 29981da177e4SLinus Torvalds } 29991da177e4SLinus Torvalds 30001da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 30011da177e4SLinus Torvalds { 30021da177e4SLinus Torvalds if (!simple_empty(dentry)) 30031da177e4SLinus Torvalds return -ENOTEMPTY; 30041da177e4SLinus Torvalds 300575c3cfa8SDavid Howells drop_nlink(d_inode(dentry)); 30069a53c3a7SDave Hansen drop_nlink(dir); 30071da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 30081da177e4SLinus Torvalds } 30091da177e4SLinus Torvalds 301037456771SMiklos Szeredi static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 301137456771SMiklos Szeredi { 3012e36cb0b8SDavid Howells bool old_is_dir = d_is_dir(old_dentry); 3013e36cb0b8SDavid Howells bool new_is_dir = d_is_dir(new_dentry); 301437456771SMiklos Szeredi 301537456771SMiklos Szeredi if (old_dir != new_dir && old_is_dir != new_is_dir) { 301637456771SMiklos Szeredi if (old_is_dir) { 301737456771SMiklos Szeredi drop_nlink(old_dir); 301837456771SMiklos Szeredi inc_nlink(new_dir); 301937456771SMiklos Szeredi } else { 302037456771SMiklos Szeredi drop_nlink(new_dir); 302137456771SMiklos Szeredi inc_nlink(old_dir); 302237456771SMiklos Szeredi } 302337456771SMiklos Szeredi } 302437456771SMiklos Szeredi old_dir->i_ctime = old_dir->i_mtime = 302537456771SMiklos Szeredi new_dir->i_ctime = new_dir->i_mtime = 302675c3cfa8SDavid Howells d_inode(old_dentry)->i_ctime = 302775c3cfa8SDavid Howells d_inode(new_dentry)->i_ctime = CURRENT_TIME; 302837456771SMiklos Szeredi 302937456771SMiklos Szeredi return 0; 303037456771SMiklos Szeredi } 303137456771SMiklos Szeredi 303246fdb794SMiklos Szeredi static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry) 303346fdb794SMiklos Szeredi { 303446fdb794SMiklos Szeredi struct dentry *whiteout; 303546fdb794SMiklos Szeredi int error; 303646fdb794SMiklos Szeredi 303746fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 303846fdb794SMiklos Szeredi if (!whiteout) 303946fdb794SMiklos Szeredi return -ENOMEM; 304046fdb794SMiklos Szeredi 304146fdb794SMiklos Szeredi error = shmem_mknod(old_dir, whiteout, 304246fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 304346fdb794SMiklos Szeredi dput(whiteout); 304446fdb794SMiklos Szeredi if (error) 304546fdb794SMiklos Szeredi return error; 304646fdb794SMiklos Szeredi 304746fdb794SMiklos Szeredi /* 304846fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 304946fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 305046fdb794SMiklos Szeredi * 305146fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 305246fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 305346fdb794SMiklos Szeredi */ 305446fdb794SMiklos Szeredi d_rehash(whiteout); 305546fdb794SMiklos Szeredi return 0; 305646fdb794SMiklos Szeredi } 305746fdb794SMiklos Szeredi 30581da177e4SLinus Torvalds /* 30591da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 30601da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 30611da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 30621da177e4SLinus Torvalds * gets overwritten. 30631da177e4SLinus Torvalds */ 30643b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) 30651da177e4SLinus Torvalds { 306675c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 30671da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 30681da177e4SLinus Torvalds 306946fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 30703b69ff51SMiklos Szeredi return -EINVAL; 30713b69ff51SMiklos Szeredi 307237456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 307337456771SMiklos Szeredi return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry); 307437456771SMiklos Szeredi 30751da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 30761da177e4SLinus Torvalds return -ENOTEMPTY; 30771da177e4SLinus Torvalds 307846fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 307946fdb794SMiklos Szeredi int error; 308046fdb794SMiklos Szeredi 308146fdb794SMiklos Szeredi error = shmem_whiteout(old_dir, old_dentry); 308246fdb794SMiklos Szeredi if (error) 308346fdb794SMiklos Szeredi return error; 308446fdb794SMiklos Szeredi } 308546fdb794SMiklos Szeredi 308675c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 30871da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 3088b928095bSMiklos Szeredi if (they_are_dirs) { 308975c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 30909a53c3a7SDave Hansen drop_nlink(old_dir); 3091b928095bSMiklos Szeredi } 30921da177e4SLinus Torvalds } else if (they_are_dirs) { 30939a53c3a7SDave Hansen drop_nlink(old_dir); 3094d8c76e6fSDave Hansen inc_nlink(new_dir); 30951da177e4SLinus Torvalds } 30961da177e4SLinus Torvalds 30971da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 30981da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 30991da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 31001da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 31011da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 31021da177e4SLinus Torvalds return 0; 31031da177e4SLinus Torvalds } 31041da177e4SLinus Torvalds 31051da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 31061da177e4SLinus Torvalds { 31071da177e4SLinus Torvalds int error; 31081da177e4SLinus Torvalds int len; 31091da177e4SLinus Torvalds struct inode *inode; 31109276aad6SHugh Dickins struct page *page; 31111da177e4SLinus Torvalds struct shmem_inode_info *info; 31121da177e4SLinus Torvalds 31131da177e4SLinus Torvalds len = strlen(symname) + 1; 311409cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 31151da177e4SLinus Torvalds return -ENAMETOOLONG; 31161da177e4SLinus Torvalds 3117454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 31181da177e4SLinus Torvalds if (!inode) 31191da177e4SLinus Torvalds return -ENOSPC; 31201da177e4SLinus Torvalds 31219d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 31226d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 3123570bc1c2SStephen Smalley if (error) { 3124570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 3125570bc1c2SStephen Smalley iput(inode); 3126570bc1c2SStephen Smalley return error; 3127570bc1c2SStephen Smalley } 3128570bc1c2SStephen Smalley error = 0; 3129570bc1c2SStephen Smalley } 3130570bc1c2SStephen Smalley 31311da177e4SLinus Torvalds info = SHMEM_I(inode); 31321da177e4SLinus Torvalds inode->i_size = len-1; 313369f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 31343ed47db3SAl Viro inode->i_link = kmemdup(symname, len, GFP_KERNEL); 31353ed47db3SAl Viro if (!inode->i_link) { 313669f07ec9SHugh Dickins iput(inode); 313769f07ec9SHugh Dickins return -ENOMEM; 313869f07ec9SHugh Dickins } 313969f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 31401da177e4SLinus Torvalds } else { 3141e8ecde25SAl Viro inode_nohighmem(inode); 31429e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_WRITE); 31431da177e4SLinus Torvalds if (error) { 31441da177e4SLinus Torvalds iput(inode); 31451da177e4SLinus Torvalds return error; 31461da177e4SLinus Torvalds } 314714fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 31481da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 314921fc61c7SAl Viro memcpy(page_address(page), symname, len); 3150ec9516fbSHugh Dickins SetPageUptodate(page); 31511da177e4SLinus Torvalds set_page_dirty(page); 31526746aff7SWu Fengguang unlock_page(page); 315309cbfeafSKirill A. Shutemov put_page(page); 31541da177e4SLinus Torvalds } 31551da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 31561da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 31571da177e4SLinus Torvalds d_instantiate(dentry, inode); 31581da177e4SLinus Torvalds dget(dentry); 31591da177e4SLinus Torvalds return 0; 31601da177e4SLinus Torvalds } 31611da177e4SLinus Torvalds 3162fceef393SAl Viro static void shmem_put_link(void *arg) 3163fceef393SAl Viro { 3164fceef393SAl Viro mark_page_accessed(arg); 3165fceef393SAl Viro put_page(arg); 3166fceef393SAl Viro } 3167fceef393SAl Viro 31686b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 3169fceef393SAl Viro struct inode *inode, 3170fceef393SAl Viro struct delayed_call *done) 31711da177e4SLinus Torvalds { 31721da177e4SLinus Torvalds struct page *page = NULL; 31736b255391SAl Viro int error; 31746a6c9904SAl Viro if (!dentry) { 31756a6c9904SAl Viro page = find_get_page(inode->i_mapping, 0); 31766a6c9904SAl Viro if (!page) 31776b255391SAl Viro return ERR_PTR(-ECHILD); 31786a6c9904SAl Viro if (!PageUptodate(page)) { 31796a6c9904SAl Viro put_page(page); 31806a6c9904SAl Viro return ERR_PTR(-ECHILD); 31816a6c9904SAl Viro } 31826a6c9904SAl Viro } else { 31839e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_READ); 3184680baacbSAl Viro if (error) 3185680baacbSAl Viro return ERR_PTR(error); 3186d3602444SHugh Dickins unlock_page(page); 31871da177e4SLinus Torvalds } 3188fceef393SAl Viro set_delayed_call(done, shmem_put_link, page); 318921fc61c7SAl Viro return page_address(page); 31901da177e4SLinus Torvalds } 31911da177e4SLinus Torvalds 3192b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3193b09e0fa4SEric Paris /* 3194b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 3195b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 3196b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 3197b09e0fa4SEric Paris * filesystem level, though. 3198b09e0fa4SEric Paris */ 3199b09e0fa4SEric Paris 32006d9d88d0SJarkko Sakkinen /* 32016d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 32026d9d88d0SJarkko Sakkinen */ 32036d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 32046d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 32056d9d88d0SJarkko Sakkinen void *fs_info) 32066d9d88d0SJarkko Sakkinen { 32076d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 32086d9d88d0SJarkko Sakkinen const struct xattr *xattr; 320938f38657SAristeu Rozanski struct simple_xattr *new_xattr; 32106d9d88d0SJarkko Sakkinen size_t len; 32116d9d88d0SJarkko Sakkinen 32126d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 321338f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 32146d9d88d0SJarkko Sakkinen if (!new_xattr) 32156d9d88d0SJarkko Sakkinen return -ENOMEM; 32166d9d88d0SJarkko Sakkinen 32176d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 32186d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 32196d9d88d0SJarkko Sakkinen GFP_KERNEL); 32206d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 32216d9d88d0SJarkko Sakkinen kfree(new_xattr); 32226d9d88d0SJarkko Sakkinen return -ENOMEM; 32236d9d88d0SJarkko Sakkinen } 32246d9d88d0SJarkko Sakkinen 32256d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 32266d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 32276d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 32286d9d88d0SJarkko Sakkinen xattr->name, len); 32296d9d88d0SJarkko Sakkinen 323038f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 32316d9d88d0SJarkko Sakkinen } 32326d9d88d0SJarkko Sakkinen 32336d9d88d0SJarkko Sakkinen return 0; 32346d9d88d0SJarkko Sakkinen } 32356d9d88d0SJarkko Sakkinen 3236aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 3237b296821aSAl Viro struct dentry *unused, struct inode *inode, 3238b296821aSAl Viro const char *name, void *buffer, size_t size) 3239aa7c5241SAndreas Gruenbacher { 3240b296821aSAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3241aa7c5241SAndreas Gruenbacher 3242aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3243aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 3244aa7c5241SAndreas Gruenbacher } 3245aa7c5241SAndreas Gruenbacher 3246aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 324759301226SAl Viro struct dentry *unused, struct inode *inode, 324859301226SAl Viro const char *name, const void *value, 324959301226SAl Viro size_t size, int flags) 3250aa7c5241SAndreas Gruenbacher { 325159301226SAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3252aa7c5241SAndreas Gruenbacher 3253aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3254aa7c5241SAndreas Gruenbacher return simple_xattr_set(&info->xattrs, name, value, size, flags); 3255aa7c5241SAndreas Gruenbacher } 3256aa7c5241SAndreas Gruenbacher 3257aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 3258aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3259aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3260aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3261aa7c5241SAndreas Gruenbacher }; 3262aa7c5241SAndreas Gruenbacher 3263aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 3264aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3265aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3266aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3267aa7c5241SAndreas Gruenbacher }; 3268aa7c5241SAndreas Gruenbacher 3269b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 3270b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 3271feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 3272feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 3273b09e0fa4SEric Paris #endif 3274aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 3275aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 3276b09e0fa4SEric Paris NULL 3277b09e0fa4SEric Paris }; 3278b09e0fa4SEric Paris 3279b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 3280b09e0fa4SEric Paris { 328175c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3282786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 3283b09e0fa4SEric Paris } 3284b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 3285b09e0fa4SEric Paris 328669f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 32871da177e4SLinus Torvalds .readlink = generic_readlink, 32886b255391SAl Viro .get_link = simple_get_link, 3289b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3290aa7c5241SAndreas Gruenbacher .setxattr = generic_setxattr, 3291aa7c5241SAndreas Gruenbacher .getxattr = generic_getxattr, 3292b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3293aa7c5241SAndreas Gruenbacher .removexattr = generic_removexattr, 3294b09e0fa4SEric Paris #endif 32951da177e4SLinus Torvalds }; 32961da177e4SLinus Torvalds 329792e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 32981da177e4SLinus Torvalds .readlink = generic_readlink, 32996b255391SAl Viro .get_link = shmem_get_link, 3300b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3301aa7c5241SAndreas Gruenbacher .setxattr = generic_setxattr, 3302aa7c5241SAndreas Gruenbacher .getxattr = generic_getxattr, 3303b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3304aa7c5241SAndreas Gruenbacher .removexattr = generic_removexattr, 330539f0247dSAndreas Gruenbacher #endif 3306b09e0fa4SEric Paris }; 330739f0247dSAndreas Gruenbacher 330891828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 330991828a40SDavid M. Grimes { 331091828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 331191828a40SDavid M. Grimes } 331291828a40SDavid M. Grimes 331391828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 331491828a40SDavid M. Grimes { 331591828a40SDavid M. Grimes __u32 *fh = vfh; 331691828a40SDavid M. Grimes __u64 inum = fh[2]; 331791828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 331891828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 331991828a40SDavid M. Grimes } 332091828a40SDavid M. Grimes 3321480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 3322480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 332391828a40SDavid M. Grimes { 332491828a40SDavid M. Grimes struct inode *inode; 3325480b116cSChristoph Hellwig struct dentry *dentry = NULL; 332635c2a7f4SHugh Dickins u64 inum; 332791828a40SDavid M. Grimes 3328480b116cSChristoph Hellwig if (fh_len < 3) 3329480b116cSChristoph Hellwig return NULL; 3330480b116cSChristoph Hellwig 333135c2a7f4SHugh Dickins inum = fid->raw[2]; 333235c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 333335c2a7f4SHugh Dickins 3334480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 3335480b116cSChristoph Hellwig shmem_match, fid->raw); 333691828a40SDavid M. Grimes if (inode) { 3337480b116cSChristoph Hellwig dentry = d_find_alias(inode); 333891828a40SDavid M. Grimes iput(inode); 333991828a40SDavid M. Grimes } 334091828a40SDavid M. Grimes 3341480b116cSChristoph Hellwig return dentry; 334291828a40SDavid M. Grimes } 334391828a40SDavid M. Grimes 3344b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 3345b0b0382bSAl Viro struct inode *parent) 334691828a40SDavid M. Grimes { 33475fe0c237SAneesh Kumar K.V if (*len < 3) { 33485fe0c237SAneesh Kumar K.V *len = 3; 334994e07a75SNamjae Jeon return FILEID_INVALID; 33505fe0c237SAneesh Kumar K.V } 335191828a40SDavid M. Grimes 33521d3382cbSAl Viro if (inode_unhashed(inode)) { 335391828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 335491828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 335591828a40SDavid M. Grimes * time, we need a lock to ensure we only try 335691828a40SDavid M. Grimes * to do it once 335791828a40SDavid M. Grimes */ 335891828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 335991828a40SDavid M. Grimes spin_lock(&lock); 33601d3382cbSAl Viro if (inode_unhashed(inode)) 336191828a40SDavid M. Grimes __insert_inode_hash(inode, 336291828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 336391828a40SDavid M. Grimes spin_unlock(&lock); 336491828a40SDavid M. Grimes } 336591828a40SDavid M. Grimes 336691828a40SDavid M. Grimes fh[0] = inode->i_generation; 336791828a40SDavid M. Grimes fh[1] = inode->i_ino; 336891828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 336991828a40SDavid M. Grimes 337091828a40SDavid M. Grimes *len = 3; 337191828a40SDavid M. Grimes return 1; 337291828a40SDavid M. Grimes } 337391828a40SDavid M. Grimes 337439655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 337591828a40SDavid M. Grimes .get_parent = shmem_get_parent, 337691828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 3377480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 337891828a40SDavid M. Grimes }; 337991828a40SDavid M. Grimes 3380680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 3381680d794bSakpm@linux-foundation.org bool remount) 33821da177e4SLinus Torvalds { 33831da177e4SLinus Torvalds char *this_char, *value, *rest; 338449cd0a5cSGreg Thelen struct mempolicy *mpol = NULL; 33858751e039SEric W. Biederman uid_t uid; 33868751e039SEric W. Biederman gid_t gid; 33871da177e4SLinus Torvalds 3388b00dc3adSHugh Dickins while (options != NULL) { 3389b00dc3adSHugh Dickins this_char = options; 3390b00dc3adSHugh Dickins for (;;) { 3391b00dc3adSHugh Dickins /* 3392b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 3393b00dc3adSHugh Dickins * mount options form a comma-separated list, 3394b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 3395b00dc3adSHugh Dickins */ 3396b00dc3adSHugh Dickins options = strchr(options, ','); 3397b00dc3adSHugh Dickins if (options == NULL) 3398b00dc3adSHugh Dickins break; 3399b00dc3adSHugh Dickins options++; 3400b00dc3adSHugh Dickins if (!isdigit(*options)) { 3401b00dc3adSHugh Dickins options[-1] = '\0'; 3402b00dc3adSHugh Dickins break; 3403b00dc3adSHugh Dickins } 3404b00dc3adSHugh Dickins } 34051da177e4SLinus Torvalds if (!*this_char) 34061da177e4SLinus Torvalds continue; 34071da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 34081da177e4SLinus Torvalds *value++ = 0; 34091da177e4SLinus Torvalds } else { 34101170532bSJoe Perches pr_err("tmpfs: No value for mount option '%s'\n", 34111da177e4SLinus Torvalds this_char); 341249cd0a5cSGreg Thelen goto error; 34131da177e4SLinus Torvalds } 34141da177e4SLinus Torvalds 34151da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 34161da177e4SLinus Torvalds unsigned long long size; 34171da177e4SLinus Torvalds size = memparse(value,&rest); 34181da177e4SLinus Torvalds if (*rest == '%') { 34191da177e4SLinus Torvalds size <<= PAGE_SHIFT; 34201da177e4SLinus Torvalds size *= totalram_pages; 34211da177e4SLinus Torvalds do_div(size, 100); 34221da177e4SLinus Torvalds rest++; 34231da177e4SLinus Torvalds } 34241da177e4SLinus Torvalds if (*rest) 34251da177e4SLinus Torvalds goto bad_val; 3426680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 342709cbfeafSKirill A. Shutemov DIV_ROUND_UP(size, PAGE_SIZE); 34281da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 3429680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 34301da177e4SLinus Torvalds if (*rest) 34311da177e4SLinus Torvalds goto bad_val; 34321da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 3433680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 34341da177e4SLinus Torvalds if (*rest) 34351da177e4SLinus Torvalds goto bad_val; 34361da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 3437680d794bSakpm@linux-foundation.org if (remount) 34381da177e4SLinus Torvalds continue; 3439680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 34401da177e4SLinus Torvalds if (*rest) 34411da177e4SLinus Torvalds goto bad_val; 34421da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 3443680d794bSakpm@linux-foundation.org if (remount) 34441da177e4SLinus Torvalds continue; 34458751e039SEric W. Biederman uid = simple_strtoul(value, &rest, 0); 34461da177e4SLinus Torvalds if (*rest) 34471da177e4SLinus Torvalds goto bad_val; 34488751e039SEric W. Biederman sbinfo->uid = make_kuid(current_user_ns(), uid); 34498751e039SEric W. Biederman if (!uid_valid(sbinfo->uid)) 34508751e039SEric W. Biederman goto bad_val; 34511da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 3452680d794bSakpm@linux-foundation.org if (remount) 34531da177e4SLinus Torvalds continue; 34548751e039SEric W. Biederman gid = simple_strtoul(value, &rest, 0); 34551da177e4SLinus Torvalds if (*rest) 34561da177e4SLinus Torvalds goto bad_val; 34578751e039SEric W. Biederman sbinfo->gid = make_kgid(current_user_ns(), gid); 34588751e039SEric W. Biederman if (!gid_valid(sbinfo->gid)) 34598751e039SEric W. Biederman goto bad_val; 3460e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 34615a6e75f8SKirill A. Shutemov } else if (!strcmp(this_char, "huge")) { 34625a6e75f8SKirill A. Shutemov int huge; 34635a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(value); 34645a6e75f8SKirill A. Shutemov if (huge < 0) 34655a6e75f8SKirill A. Shutemov goto bad_val; 34665a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 34675a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER) 34685a6e75f8SKirill A. Shutemov goto bad_val; 34695a6e75f8SKirill A. Shutemov sbinfo->huge = huge; 34705a6e75f8SKirill A. Shutemov #endif 34715a6e75f8SKirill A. Shutemov #ifdef CONFIG_NUMA 34727339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 347349cd0a5cSGreg Thelen mpol_put(mpol); 347449cd0a5cSGreg Thelen mpol = NULL; 347549cd0a5cSGreg Thelen if (mpol_parse_str(value, &mpol)) 34767339ff83SRobin Holt goto bad_val; 34775a6e75f8SKirill A. Shutemov #endif 34781da177e4SLinus Torvalds } else { 34791170532bSJoe Perches pr_err("tmpfs: Bad mount option %s\n", this_char); 348049cd0a5cSGreg Thelen goto error; 34811da177e4SLinus Torvalds } 34821da177e4SLinus Torvalds } 348349cd0a5cSGreg Thelen sbinfo->mpol = mpol; 34841da177e4SLinus Torvalds return 0; 34851da177e4SLinus Torvalds 34861da177e4SLinus Torvalds bad_val: 34871170532bSJoe Perches pr_err("tmpfs: Bad value '%s' for mount option '%s'\n", 34881da177e4SLinus Torvalds value, this_char); 348949cd0a5cSGreg Thelen error: 349049cd0a5cSGreg Thelen mpol_put(mpol); 34911da177e4SLinus Torvalds return 1; 34921da177e4SLinus Torvalds 34931da177e4SLinus Torvalds } 34941da177e4SLinus Torvalds 34951da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 34961da177e4SLinus Torvalds { 34971da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3498680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 34990edd73b3SHugh Dickins unsigned long inodes; 35000edd73b3SHugh Dickins int error = -EINVAL; 35011da177e4SLinus Torvalds 35025f00110fSGreg Thelen config.mpol = NULL; 3503680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 35040edd73b3SHugh Dickins return error; 35050edd73b3SHugh Dickins 35060edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 35070edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 35087e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 35090edd73b3SHugh Dickins goto out; 3510680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 35110edd73b3SHugh Dickins goto out; 35120edd73b3SHugh Dickins /* 351354af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 35140edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 35150edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 35160edd73b3SHugh Dickins */ 3517680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 35180edd73b3SHugh Dickins goto out; 3519680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 35200edd73b3SHugh Dickins goto out; 35210edd73b3SHugh Dickins 35220edd73b3SHugh Dickins error = 0; 35235a6e75f8SKirill A. Shutemov sbinfo->huge = config.huge; 3524680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 3525680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 3526680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 352771fe804bSLee Schermerhorn 35285f00110fSGreg Thelen /* 35295f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 35305f00110fSGreg Thelen */ 35315f00110fSGreg Thelen if (config.mpol) { 353271fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 353371fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 35345f00110fSGreg Thelen } 35350edd73b3SHugh Dickins out: 35360edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 35370edd73b3SHugh Dickins return error; 35381da177e4SLinus Torvalds } 3539680d794bSakpm@linux-foundation.org 354034c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 3541680d794bSakpm@linux-foundation.org { 354234c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 3543680d794bSakpm@linux-foundation.org 3544680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 3545680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 354609cbfeafSKirill A. Shutemov sbinfo->max_blocks << (PAGE_SHIFT - 10)); 3547680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 3548680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 3549680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 355009208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 35518751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 35528751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 35538751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 35548751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 35558751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 35568751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 3557e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 35585a6e75f8SKirill A. Shutemov /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 35595a6e75f8SKirill A. Shutemov if (sbinfo->huge) 35605a6e75f8SKirill A. Shutemov seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 35615a6e75f8SKirill A. Shutemov #endif 356271fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 3563680d794bSakpm@linux-foundation.org return 0; 3564680d794bSakpm@linux-foundation.org } 35659183df25SDavid Herrmann 35669183df25SDavid Herrmann #define MFD_NAME_PREFIX "memfd:" 35679183df25SDavid Herrmann #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1) 35689183df25SDavid Herrmann #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN) 35699183df25SDavid Herrmann 35709183df25SDavid Herrmann #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING) 35719183df25SDavid Herrmann 35729183df25SDavid Herrmann SYSCALL_DEFINE2(memfd_create, 35739183df25SDavid Herrmann const char __user *, uname, 35749183df25SDavid Herrmann unsigned int, flags) 35759183df25SDavid Herrmann { 35769183df25SDavid Herrmann struct shmem_inode_info *info; 35779183df25SDavid Herrmann struct file *file; 35789183df25SDavid Herrmann int fd, error; 35799183df25SDavid Herrmann char *name; 35809183df25SDavid Herrmann long len; 35819183df25SDavid Herrmann 35829183df25SDavid Herrmann if (flags & ~(unsigned int)MFD_ALL_FLAGS) 35839183df25SDavid Herrmann return -EINVAL; 35849183df25SDavid Herrmann 35859183df25SDavid Herrmann /* length includes terminating zero */ 35869183df25SDavid Herrmann len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); 35879183df25SDavid Herrmann if (len <= 0) 35889183df25SDavid Herrmann return -EFAULT; 35899183df25SDavid Herrmann if (len > MFD_NAME_MAX_LEN + 1) 35909183df25SDavid Herrmann return -EINVAL; 35919183df25SDavid Herrmann 35929183df25SDavid Herrmann name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY); 35939183df25SDavid Herrmann if (!name) 35949183df25SDavid Herrmann return -ENOMEM; 35959183df25SDavid Herrmann 35969183df25SDavid Herrmann strcpy(name, MFD_NAME_PREFIX); 35979183df25SDavid Herrmann if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) { 35989183df25SDavid Herrmann error = -EFAULT; 35999183df25SDavid Herrmann goto err_name; 36009183df25SDavid Herrmann } 36019183df25SDavid Herrmann 36029183df25SDavid Herrmann /* terminating-zero may have changed after strnlen_user() returned */ 36039183df25SDavid Herrmann if (name[len + MFD_NAME_PREFIX_LEN - 1]) { 36049183df25SDavid Herrmann error = -EFAULT; 36059183df25SDavid Herrmann goto err_name; 36069183df25SDavid Herrmann } 36079183df25SDavid Herrmann 36089183df25SDavid Herrmann fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0); 36099183df25SDavid Herrmann if (fd < 0) { 36109183df25SDavid Herrmann error = fd; 36119183df25SDavid Herrmann goto err_name; 36129183df25SDavid Herrmann } 36139183df25SDavid Herrmann 36149183df25SDavid Herrmann file = shmem_file_setup(name, 0, VM_NORESERVE); 36159183df25SDavid Herrmann if (IS_ERR(file)) { 36169183df25SDavid Herrmann error = PTR_ERR(file); 36179183df25SDavid Herrmann goto err_fd; 36189183df25SDavid Herrmann } 36199183df25SDavid Herrmann info = SHMEM_I(file_inode(file)); 36209183df25SDavid Herrmann file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE; 36219183df25SDavid Herrmann file->f_flags |= O_RDWR | O_LARGEFILE; 36229183df25SDavid Herrmann if (flags & MFD_ALLOW_SEALING) 36239183df25SDavid Herrmann info->seals &= ~F_SEAL_SEAL; 36249183df25SDavid Herrmann 36259183df25SDavid Herrmann fd_install(fd, file); 36269183df25SDavid Herrmann kfree(name); 36279183df25SDavid Herrmann return fd; 36289183df25SDavid Herrmann 36299183df25SDavid Herrmann err_fd: 36309183df25SDavid Herrmann put_unused_fd(fd); 36319183df25SDavid Herrmann err_name: 36329183df25SDavid Herrmann kfree(name); 36339183df25SDavid Herrmann return error; 36349183df25SDavid Herrmann } 36359183df25SDavid Herrmann 3636680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 36371da177e4SLinus Torvalds 36381da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 36391da177e4SLinus Torvalds { 3640602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3641602586a8SHugh Dickins 3642602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 364349cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 3644602586a8SHugh Dickins kfree(sbinfo); 36451da177e4SLinus Torvalds sb->s_fs_info = NULL; 36461da177e4SLinus Torvalds } 36471da177e4SLinus Torvalds 36482b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 36491da177e4SLinus Torvalds { 36501da177e4SLinus Torvalds struct inode *inode; 36510edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 3652680d794bSakpm@linux-foundation.org int err = -ENOMEM; 3653680d794bSakpm@linux-foundation.org 3654680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 3655425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 3656680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 3657680d794bSakpm@linux-foundation.org if (!sbinfo) 3658680d794bSakpm@linux-foundation.org return -ENOMEM; 3659680d794bSakpm@linux-foundation.org 3660680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 366176aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 366276aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 3663680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 36641da177e4SLinus Torvalds 36650edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 36661da177e4SLinus Torvalds /* 36671da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 36681da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 36691da177e4SLinus Torvalds * but the internal instance is left unlimited. 36701da177e4SLinus Torvalds */ 3671ca4e0519SAl Viro if (!(sb->s_flags & MS_KERNMOUNT)) { 3672680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 3673680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 3674680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 3675680d794bSakpm@linux-foundation.org err = -EINVAL; 3676680d794bSakpm@linux-foundation.org goto failed; 3677680d794bSakpm@linux-foundation.org } 3678ca4e0519SAl Viro } else { 3679ca4e0519SAl Viro sb->s_flags |= MS_NOUSER; 36801da177e4SLinus Torvalds } 368191828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 36822f6e38f3SHugh Dickins sb->s_flags |= MS_NOSEC; 36830edd73b3SHugh Dickins #else 36840edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 36850edd73b3SHugh Dickins #endif 36861da177e4SLinus Torvalds 36871da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 3688908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 3689602586a8SHugh Dickins goto failed; 3690680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 3691779750d2SKirill A. Shutemov spin_lock_init(&sbinfo->shrinklist_lock); 3692779750d2SKirill A. Shutemov INIT_LIST_HEAD(&sbinfo->shrinklist); 36931da177e4SLinus Torvalds 3694285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 369509cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 369609cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 36971da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 36981da177e4SLinus Torvalds sb->s_op = &shmem_ops; 3699cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 3700b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 370139f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 3702b09e0fa4SEric Paris #endif 3703b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 370439f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 370539f0247dSAndreas Gruenbacher #endif 37060edd73b3SHugh Dickins 3707454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 37081da177e4SLinus Torvalds if (!inode) 37091da177e4SLinus Torvalds goto failed; 3710680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 3711680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 3712318ceed0SAl Viro sb->s_root = d_make_root(inode); 3713318ceed0SAl Viro if (!sb->s_root) 371448fde701SAl Viro goto failed; 37151da177e4SLinus Torvalds return 0; 37161da177e4SLinus Torvalds 37171da177e4SLinus Torvalds failed: 37181da177e4SLinus Torvalds shmem_put_super(sb); 37191da177e4SLinus Torvalds return err; 37201da177e4SLinus Torvalds } 37211da177e4SLinus Torvalds 3722fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 37231da177e4SLinus Torvalds 37241da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 37251da177e4SLinus Torvalds { 372641ffe5d5SHugh Dickins struct shmem_inode_info *info; 372741ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 372841ffe5d5SHugh Dickins if (!info) 37291da177e4SLinus Torvalds return NULL; 373041ffe5d5SHugh Dickins return &info->vfs_inode; 37311da177e4SLinus Torvalds } 37321da177e4SLinus Torvalds 373341ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 3734fa0d7e3dSNick Piggin { 3735fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 373684e710daSAl Viro if (S_ISLNK(inode->i_mode)) 37373ed47db3SAl Viro kfree(inode->i_link); 3738fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 3739fa0d7e3dSNick Piggin } 3740fa0d7e3dSNick Piggin 37411da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 37421da177e4SLinus Torvalds { 374309208d15SAl Viro if (S_ISREG(inode->i_mode)) 37441da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 374541ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 37461da177e4SLinus Torvalds } 37471da177e4SLinus Torvalds 374841ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 37491da177e4SLinus Torvalds { 375041ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 375141ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 37521da177e4SLinus Torvalds } 37531da177e4SLinus Torvalds 375441ffe5d5SHugh Dickins static int shmem_init_inodecache(void) 37551da177e4SLinus Torvalds { 37561da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 37571da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 37585d097056SVladimir Davydov 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 37591da177e4SLinus Torvalds return 0; 37601da177e4SLinus Torvalds } 37611da177e4SLinus Torvalds 376241ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 37631da177e4SLinus Torvalds { 37641a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 37651da177e4SLinus Torvalds } 37661da177e4SLinus Torvalds 3767f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 37681da177e4SLinus Torvalds .writepage = shmem_writepage, 376976719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 37701da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3771800d15a5SNick Piggin .write_begin = shmem_write_begin, 3772800d15a5SNick Piggin .write_end = shmem_write_end, 37731da177e4SLinus Torvalds #endif 37741c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 3775304dbdb7SLee Schermerhorn .migratepage = migrate_page, 37761c93923cSAndrew Morton #endif 3777aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 37781da177e4SLinus Torvalds }; 37791da177e4SLinus Torvalds 378015ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 37811da177e4SLinus Torvalds .mmap = shmem_mmap, 3782c01d5b30SHugh Dickins .get_unmapped_area = shmem_get_unmapped_area, 37831da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3784220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 37852ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 37868174202bSAl Viro .write_iter = generic_file_write_iter, 37871b061d92SChristoph Hellwig .fsync = noop_fsync, 3788708e3508SHugh Dickins .splice_read = shmem_file_splice_read, 3789f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 379083e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 37911da177e4SLinus Torvalds #endif 37921da177e4SLinus Torvalds }; 37931da177e4SLinus Torvalds 379492e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 379544a30220SYu Zhao .getattr = shmem_getattr, 379694c1e62dSHugh Dickins .setattr = shmem_setattr, 3797b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3798aa7c5241SAndreas Gruenbacher .setxattr = generic_setxattr, 3799aa7c5241SAndreas Gruenbacher .getxattr = generic_getxattr, 3800b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3801aa7c5241SAndreas Gruenbacher .removexattr = generic_removexattr, 3802feda821eSChristoph Hellwig .set_acl = simple_set_acl, 3803b09e0fa4SEric Paris #endif 38041da177e4SLinus Torvalds }; 38051da177e4SLinus Torvalds 380692e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 38071da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 38081da177e4SLinus Torvalds .create = shmem_create, 38091da177e4SLinus Torvalds .lookup = simple_lookup, 38101da177e4SLinus Torvalds .link = shmem_link, 38111da177e4SLinus Torvalds .unlink = shmem_unlink, 38121da177e4SLinus Torvalds .symlink = shmem_symlink, 38131da177e4SLinus Torvalds .mkdir = shmem_mkdir, 38141da177e4SLinus Torvalds .rmdir = shmem_rmdir, 38151da177e4SLinus Torvalds .mknod = shmem_mknod, 38163b69ff51SMiklos Szeredi .rename2 = shmem_rename2, 381760545d0dSAl Viro .tmpfile = shmem_tmpfile, 38181da177e4SLinus Torvalds #endif 3819b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3820aa7c5241SAndreas Gruenbacher .setxattr = generic_setxattr, 3821aa7c5241SAndreas Gruenbacher .getxattr = generic_getxattr, 3822b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3823aa7c5241SAndreas Gruenbacher .removexattr = generic_removexattr, 3824b09e0fa4SEric Paris #endif 382539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 382694c1e62dSHugh Dickins .setattr = shmem_setattr, 3827feda821eSChristoph Hellwig .set_acl = simple_set_acl, 382839f0247dSAndreas Gruenbacher #endif 382939f0247dSAndreas Gruenbacher }; 383039f0247dSAndreas Gruenbacher 383192e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 3832b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3833aa7c5241SAndreas Gruenbacher .setxattr = generic_setxattr, 3834aa7c5241SAndreas Gruenbacher .getxattr = generic_getxattr, 3835b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3836aa7c5241SAndreas Gruenbacher .removexattr = generic_removexattr, 3837b09e0fa4SEric Paris #endif 383839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 383994c1e62dSHugh Dickins .setattr = shmem_setattr, 3840feda821eSChristoph Hellwig .set_acl = simple_set_acl, 384139f0247dSAndreas Gruenbacher #endif 38421da177e4SLinus Torvalds }; 38431da177e4SLinus Torvalds 3844759b9775SHugh Dickins static const struct super_operations shmem_ops = { 38451da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 38461da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 38471da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 38481da177e4SLinus Torvalds .statfs = shmem_statfs, 38491da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 3850680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 38511da177e4SLinus Torvalds #endif 38521f895f75SAl Viro .evict_inode = shmem_evict_inode, 38531da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 38541da177e4SLinus Torvalds .put_super = shmem_put_super, 3855779750d2SKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3856779750d2SKirill A. Shutemov .nr_cached_objects = shmem_unused_huge_count, 3857779750d2SKirill A. Shutemov .free_cached_objects = shmem_unused_huge_scan, 3858779750d2SKirill A. Shutemov #endif 38591da177e4SLinus Torvalds }; 38601da177e4SLinus Torvalds 3861f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 386254cb8821SNick Piggin .fault = shmem_fault, 3863d7c17551SNing Qu .map_pages = filemap_map_pages, 38641da177e4SLinus Torvalds #ifdef CONFIG_NUMA 38651da177e4SLinus Torvalds .set_policy = shmem_set_policy, 38661da177e4SLinus Torvalds .get_policy = shmem_get_policy, 38671da177e4SLinus Torvalds #endif 38681da177e4SLinus Torvalds }; 38691da177e4SLinus Torvalds 38703c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 38713c26ff6eSAl Viro int flags, const char *dev_name, void *data) 38721da177e4SLinus Torvalds { 38733c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 38741da177e4SLinus Torvalds } 38751da177e4SLinus Torvalds 387641ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 38771da177e4SLinus Torvalds .owner = THIS_MODULE, 38781da177e4SLinus Torvalds .name = "tmpfs", 38793c26ff6eSAl Viro .mount = shmem_mount, 38801da177e4SLinus Torvalds .kill_sb = kill_litter_super, 38812b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 38821da177e4SLinus Torvalds }; 38831da177e4SLinus Torvalds 388441ffe5d5SHugh Dickins int __init shmem_init(void) 38851da177e4SLinus Torvalds { 38861da177e4SLinus Torvalds int error; 38871da177e4SLinus Torvalds 388816203a7aSRob Landley /* If rootfs called this, don't re-init */ 388916203a7aSRob Landley if (shmem_inode_cachep) 389016203a7aSRob Landley return 0; 389116203a7aSRob Landley 389241ffe5d5SHugh Dickins error = shmem_init_inodecache(); 38931da177e4SLinus Torvalds if (error) 38941da177e4SLinus Torvalds goto out3; 38951da177e4SLinus Torvalds 389641ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 38971da177e4SLinus Torvalds if (error) { 38981170532bSJoe Perches pr_err("Could not register tmpfs\n"); 38991da177e4SLinus Torvalds goto out2; 39001da177e4SLinus Torvalds } 390195dc112aSGreg Kroah-Hartman 3902ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 39031da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 39041da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 39051170532bSJoe Perches pr_err("Could not kern_mount tmpfs\n"); 39061da177e4SLinus Torvalds goto out1; 39071da177e4SLinus Torvalds } 39085a6e75f8SKirill A. Shutemov 3909e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 39105a6e75f8SKirill A. Shutemov if (has_transparent_hugepage() && shmem_huge < SHMEM_HUGE_DENY) 39115a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 39125a6e75f8SKirill A. Shutemov else 39135a6e75f8SKirill A. Shutemov shmem_huge = 0; /* just in case it was patched */ 39145a6e75f8SKirill A. Shutemov #endif 39151da177e4SLinus Torvalds return 0; 39161da177e4SLinus Torvalds 39171da177e4SLinus Torvalds out1: 391841ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 39191da177e4SLinus Torvalds out2: 392041ffe5d5SHugh Dickins shmem_destroy_inodecache(); 39211da177e4SLinus Torvalds out3: 39221da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 39231da177e4SLinus Torvalds return error; 39241da177e4SLinus Torvalds } 3925853ac43aSMatt Mackall 3926e496cf3dSKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS) 39275a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj, 39285a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, char *buf) 39295a6e75f8SKirill A. Shutemov { 39305a6e75f8SKirill A. Shutemov int values[] = { 39315a6e75f8SKirill A. Shutemov SHMEM_HUGE_ALWAYS, 39325a6e75f8SKirill A. Shutemov SHMEM_HUGE_WITHIN_SIZE, 39335a6e75f8SKirill A. Shutemov SHMEM_HUGE_ADVISE, 39345a6e75f8SKirill A. Shutemov SHMEM_HUGE_NEVER, 39355a6e75f8SKirill A. Shutemov SHMEM_HUGE_DENY, 39365a6e75f8SKirill A. Shutemov SHMEM_HUGE_FORCE, 39375a6e75f8SKirill A. Shutemov }; 39385a6e75f8SKirill A. Shutemov int i, count; 39395a6e75f8SKirill A. Shutemov 39405a6e75f8SKirill A. Shutemov for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) { 39415a6e75f8SKirill A. Shutemov const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s "; 39425a6e75f8SKirill A. Shutemov 39435a6e75f8SKirill A. Shutemov count += sprintf(buf + count, fmt, 39445a6e75f8SKirill A. Shutemov shmem_format_huge(values[i])); 39455a6e75f8SKirill A. Shutemov } 39465a6e75f8SKirill A. Shutemov buf[count - 1] = '\n'; 39475a6e75f8SKirill A. Shutemov return count; 39485a6e75f8SKirill A. Shutemov } 39495a6e75f8SKirill A. Shutemov 39505a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj, 39515a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, const char *buf, size_t count) 39525a6e75f8SKirill A. Shutemov { 39535a6e75f8SKirill A. Shutemov char tmp[16]; 39545a6e75f8SKirill A. Shutemov int huge; 39555a6e75f8SKirill A. Shutemov 39565a6e75f8SKirill A. Shutemov if (count + 1 > sizeof(tmp)) 39575a6e75f8SKirill A. Shutemov return -EINVAL; 39585a6e75f8SKirill A. Shutemov memcpy(tmp, buf, count); 39595a6e75f8SKirill A. Shutemov tmp[count] = '\0'; 39605a6e75f8SKirill A. Shutemov if (count && tmp[count - 1] == '\n') 39615a6e75f8SKirill A. Shutemov tmp[count - 1] = '\0'; 39625a6e75f8SKirill A. Shutemov 39635a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(tmp); 39645a6e75f8SKirill A. Shutemov if (huge == -EINVAL) 39655a6e75f8SKirill A. Shutemov return -EINVAL; 39665a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 39675a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 39685a6e75f8SKirill A. Shutemov return -EINVAL; 39695a6e75f8SKirill A. Shutemov 39705a6e75f8SKirill A. Shutemov shmem_huge = huge; 39715a6e75f8SKirill A. Shutemov if (shmem_huge < SHMEM_HUGE_DENY) 39725a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 39735a6e75f8SKirill A. Shutemov return count; 39745a6e75f8SKirill A. Shutemov } 39755a6e75f8SKirill A. Shutemov 39765a6e75f8SKirill A. Shutemov struct kobj_attribute shmem_enabled_attr = 39775a6e75f8SKirill A. Shutemov __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store); 3978f3f0e1d2SKirill A. Shutemov 3979f3f0e1d2SKirill A. Shutemov bool shmem_huge_enabled(struct vm_area_struct *vma) 3980f3f0e1d2SKirill A. Shutemov { 3981f3f0e1d2SKirill A. Shutemov struct inode *inode = file_inode(vma->vm_file); 3982f3f0e1d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 3983f3f0e1d2SKirill A. Shutemov loff_t i_size; 3984f3f0e1d2SKirill A. Shutemov pgoff_t off; 3985f3f0e1d2SKirill A. Shutemov 3986f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 3987f3f0e1d2SKirill A. Shutemov return true; 3988f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY) 3989f3f0e1d2SKirill A. Shutemov return false; 3990f3f0e1d2SKirill A. Shutemov switch (sbinfo->huge) { 3991f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_NEVER: 3992f3f0e1d2SKirill A. Shutemov return false; 3993f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 3994f3f0e1d2SKirill A. Shutemov return true; 3995f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 3996f3f0e1d2SKirill A. Shutemov off = round_up(vma->vm_pgoff, HPAGE_PMD_NR); 3997f3f0e1d2SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 3998f3f0e1d2SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 3999f3f0e1d2SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 4000f3f0e1d2SKirill A. Shutemov return true; 4001f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 4002f3f0e1d2SKirill A. Shutemov /* TODO: implement fadvise() hints */ 4003f3f0e1d2SKirill A. Shutemov return (vma->vm_flags & VM_HUGEPAGE); 4004f3f0e1d2SKirill A. Shutemov default: 4005f3f0e1d2SKirill A. Shutemov VM_BUG_ON(1); 4006f3f0e1d2SKirill A. Shutemov return false; 4007f3f0e1d2SKirill A. Shutemov } 4008f3f0e1d2SKirill A. Shutemov } 4009e496cf3dSKirill A. Shutemov #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */ 40105a6e75f8SKirill A. Shutemov 4011853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 4012853ac43aSMatt Mackall 4013853ac43aSMatt Mackall /* 4014853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 4015853ac43aSMatt Mackall * 4016853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 4017853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 4018853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 4019853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 4020853ac43aSMatt Mackall */ 4021853ac43aSMatt Mackall 402241ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 4023853ac43aSMatt Mackall .name = "tmpfs", 40243c26ff6eSAl Viro .mount = ramfs_mount, 4025853ac43aSMatt Mackall .kill_sb = kill_litter_super, 40262b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 4027853ac43aSMatt Mackall }; 4028853ac43aSMatt Mackall 402941ffe5d5SHugh Dickins int __init shmem_init(void) 4030853ac43aSMatt Mackall { 403141ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 4032853ac43aSMatt Mackall 403341ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 4034853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 4035853ac43aSMatt Mackall 4036853ac43aSMatt Mackall return 0; 4037853ac43aSMatt Mackall } 4038853ac43aSMatt Mackall 403941ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 4040853ac43aSMatt Mackall { 4041853ac43aSMatt Mackall return 0; 4042853ac43aSMatt Mackall } 4043853ac43aSMatt Mackall 40443f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 40453f96b79aSHugh Dickins { 40463f96b79aSHugh Dickins return 0; 40473f96b79aSHugh Dickins } 40483f96b79aSHugh Dickins 404924513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 405024513264SHugh Dickins { 405124513264SHugh Dickins } 405224513264SHugh Dickins 4053c01d5b30SHugh Dickins #ifdef CONFIG_MMU 4054c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 4055c01d5b30SHugh Dickins unsigned long addr, unsigned long len, 4056c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 4057c01d5b30SHugh Dickins { 4058c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); 4059c01d5b30SHugh Dickins } 4060c01d5b30SHugh Dickins #endif 4061c01d5b30SHugh Dickins 406241ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 406394c1e62dSHugh Dickins { 406441ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 406594c1e62dSHugh Dickins } 406694c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 406794c1e62dSHugh Dickins 4068853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 40690b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 4070454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 40710b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 40720b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 4073853ac43aSMatt Mackall 4074853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 4075853ac43aSMatt Mackall 4076853ac43aSMatt Mackall /* common code */ 40771da177e4SLinus Torvalds 40783451538aSAl Viro static struct dentry_operations anon_ops = { 4079118b2302SAl Viro .d_dname = simple_dname 40803451538aSAl Viro }; 40813451538aSAl Viro 4082c7277090SEric Paris static struct file *__shmem_file_setup(const char *name, loff_t size, 4083c7277090SEric Paris unsigned long flags, unsigned int i_flags) 40841da177e4SLinus Torvalds { 40856b4d0b27SAl Viro struct file *res; 40861da177e4SLinus Torvalds struct inode *inode; 40872c48b9c4SAl Viro struct path path; 40883451538aSAl Viro struct super_block *sb; 40891da177e4SLinus Torvalds struct qstr this; 40901da177e4SLinus Torvalds 40911da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 40926b4d0b27SAl Viro return ERR_CAST(shm_mnt); 40931da177e4SLinus Torvalds 4094285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 40951da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 40961da177e4SLinus Torvalds 40971da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 40981da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 40991da177e4SLinus Torvalds 41006b4d0b27SAl Viro res = ERR_PTR(-ENOMEM); 41011da177e4SLinus Torvalds this.name = name; 41021da177e4SLinus Torvalds this.len = strlen(name); 41031da177e4SLinus Torvalds this.hash = 0; /* will go */ 41043451538aSAl Viro sb = shm_mnt->mnt_sb; 410566ee4b88SKonstantin Khlebnikov path.mnt = mntget(shm_mnt); 41063451538aSAl Viro path.dentry = d_alloc_pseudo(sb, &this); 41072c48b9c4SAl Viro if (!path.dentry) 41081da177e4SLinus Torvalds goto put_memory; 41093451538aSAl Viro d_set_d_op(path.dentry, &anon_ops); 41101da177e4SLinus Torvalds 41116b4d0b27SAl Viro res = ERR_PTR(-ENOSPC); 41123451538aSAl Viro inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 41131da177e4SLinus Torvalds if (!inode) 411466ee4b88SKonstantin Khlebnikov goto put_memory; 41151da177e4SLinus Torvalds 4116c7277090SEric Paris inode->i_flags |= i_flags; 41172c48b9c4SAl Viro d_instantiate(path.dentry, inode); 41181da177e4SLinus Torvalds inode->i_size = size; 41196d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 412026567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 412126567cdbSAl Viro if (IS_ERR(res)) 412266ee4b88SKonstantin Khlebnikov goto put_path; 41234b42af81SAl Viro 41246b4d0b27SAl Viro res = alloc_file(&path, FMODE_WRITE | FMODE_READ, 41254b42af81SAl Viro &shmem_file_operations); 41266b4d0b27SAl Viro if (IS_ERR(res)) 412766ee4b88SKonstantin Khlebnikov goto put_path; 41284b42af81SAl Viro 41296b4d0b27SAl Viro return res; 41301da177e4SLinus Torvalds 41311da177e4SLinus Torvalds put_memory: 41321da177e4SLinus Torvalds shmem_unacct_size(flags, size); 413366ee4b88SKonstantin Khlebnikov put_path: 413466ee4b88SKonstantin Khlebnikov path_put(&path); 41356b4d0b27SAl Viro return res; 41361da177e4SLinus Torvalds } 4137c7277090SEric Paris 4138c7277090SEric Paris /** 4139c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 4140c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 4141c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 4142e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 4143e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 4144c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4145c7277090SEric Paris * @size: size to be set for the file 4146c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4147c7277090SEric Paris */ 4148c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 4149c7277090SEric Paris { 4150c7277090SEric Paris return __shmem_file_setup(name, size, flags, S_PRIVATE); 4151c7277090SEric Paris } 4152c7277090SEric Paris 4153c7277090SEric Paris /** 4154c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 4155c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4156c7277090SEric Paris * @size: size to be set for the file 4157c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4158c7277090SEric Paris */ 4159c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 4160c7277090SEric Paris { 4161c7277090SEric Paris return __shmem_file_setup(name, size, flags, 0); 4162c7277090SEric Paris } 4163395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 41641da177e4SLinus Torvalds 416546711810SRandy Dunlap /** 41661da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 41671da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 41681da177e4SLinus Torvalds */ 41691da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 41701da177e4SLinus Torvalds { 41711da177e4SLinus Torvalds struct file *file; 41721da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 41731da177e4SLinus Torvalds 417466fc1303SHugh Dickins /* 417566fc1303SHugh Dickins * Cloning a new file under mmap_sem leads to a lock ordering conflict 417666fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 417766fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 417866fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 417966fc1303SHugh Dickins */ 418066fc1303SHugh Dickins file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE); 41811da177e4SLinus Torvalds if (IS_ERR(file)) 41821da177e4SLinus Torvalds return PTR_ERR(file); 41831da177e4SLinus Torvalds 41841da177e4SLinus Torvalds if (vma->vm_file) 41851da177e4SLinus Torvalds fput(vma->vm_file); 41861da177e4SLinus Torvalds vma->vm_file = file; 41871da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 4188f3f0e1d2SKirill A. Shutemov 4189e496cf3dSKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 4190f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 4191f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 4192f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 4193f3f0e1d2SKirill A. Shutemov } 4194f3f0e1d2SKirill A. Shutemov 41951da177e4SLinus Torvalds return 0; 41961da177e4SLinus Torvalds } 4197d9d90e5eSHugh Dickins 4198d9d90e5eSHugh Dickins /** 4199d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 4200d9d90e5eSHugh Dickins * @mapping: the page's address_space 4201d9d90e5eSHugh Dickins * @index: the page index 4202d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 4203d9d90e5eSHugh Dickins * 4204d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 4205d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 4206d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 4207d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 4208d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 4209d9d90e5eSHugh Dickins * 421068da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 421168da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 4212d9d90e5eSHugh Dickins */ 4213d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 4214d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 4215d9d90e5eSHugh Dickins { 421668da9f05SHugh Dickins #ifdef CONFIG_SHMEM 421768da9f05SHugh Dickins struct inode *inode = mapping->host; 42189276aad6SHugh Dickins struct page *page; 421968da9f05SHugh Dickins int error; 422068da9f05SHugh Dickins 422168da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 42229e18eb29SAndres Lagar-Cavilla error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, 42239e18eb29SAndres Lagar-Cavilla gfp, NULL, NULL); 422468da9f05SHugh Dickins if (error) 422568da9f05SHugh Dickins page = ERR_PTR(error); 422668da9f05SHugh Dickins else 422768da9f05SHugh Dickins unlock_page(page); 422868da9f05SHugh Dickins return page; 422968da9f05SHugh Dickins #else 423068da9f05SHugh Dickins /* 423168da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 423268da9f05SHugh Dickins */ 4233d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 423468da9f05SHugh Dickins #endif 4235d9d90e5eSHugh Dickins } 4236d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 4237