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. 90edd73b3SHugh Dickins * Copyright (C) 2002-2005 Hugh Dickins. 100edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 111da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Extended attribute support for tmpfs: 141da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 151da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 161da177e4SLinus Torvalds * 17853ac43aSMatt Mackall * tiny-shmem: 18853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 19853ac43aSMatt Mackall * 201da177e4SLinus Torvalds * This file is released under the GPL. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 23853ac43aSMatt Mackall #include <linux/fs.h> 24853ac43aSMatt Mackall #include <linux/init.h> 25853ac43aSMatt Mackall #include <linux/vfs.h> 26853ac43aSMatt Mackall #include <linux/mount.h> 27caefba17SHugh Dickins #include <linux/pagemap.h> 28853ac43aSMatt Mackall #include <linux/file.h> 29853ac43aSMatt Mackall #include <linux/mm.h> 30853ac43aSMatt Mackall #include <linux/module.h> 317e496299STim Chen #include <linux/percpu_counter.h> 32853ac43aSMatt Mackall #include <linux/swap.h> 33853ac43aSMatt Mackall 34853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 35853ac43aSMatt Mackall 36853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 391da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 401da177e4SLinus Torvalds * which makes it a completely usable filesystem. 411da177e4SLinus Torvalds */ 421da177e4SLinus Torvalds 4339f0247dSAndreas Gruenbacher #include <linux/xattr.h> 44a5694255SChristoph Hellwig #include <linux/exportfs.h> 451c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 4639f0247dSAndreas Gruenbacher #include <linux/generic_acl.h> 471da177e4SLinus Torvalds #include <linux/mman.h> 481da177e4SLinus Torvalds #include <linux/string.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 501da177e4SLinus Torvalds #include <linux/backing-dev.h> 511da177e4SLinus Torvalds #include <linux/shmem_fs.h> 521da177e4SLinus Torvalds #include <linux/writeback.h> 531da177e4SLinus Torvalds #include <linux/blkdev.h> 54708e3508SHugh Dickins #include <linux/splice.h> 551da177e4SLinus Torvalds #include <linux/security.h> 561da177e4SLinus Torvalds #include <linux/swapops.h> 571da177e4SLinus Torvalds #include <linux/mempolicy.h> 581da177e4SLinus Torvalds #include <linux/namei.h> 59b00dc3adSHugh Dickins #include <linux/ctype.h> 60304dbdb7SLee Schermerhorn #include <linux/migrate.h> 61c1f60a5aSChristoph Lameter #include <linux/highmem.h> 62680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 6392562927SMimi Zohar #include <linux/magic.h> 64304dbdb7SLee Schermerhorn 651da177e4SLinus Torvalds #include <asm/uaccess.h> 661da177e4SLinus Torvalds #include <asm/div64.h> 671da177e4SLinus Torvalds #include <asm/pgtable.h> 681da177e4SLinus Torvalds 69caefba17SHugh Dickins /* 70caefba17SHugh Dickins * The maximum size of a shmem/tmpfs file is limited by the maximum size of 71caefba17SHugh Dickins * its triple-indirect swap vector - see illustration at shmem_swp_entry(). 72caefba17SHugh Dickins * 73caefba17SHugh Dickins * With 4kB page size, maximum file size is just over 2TB on a 32-bit kernel, 74caefba17SHugh Dickins * but one eighth of that on a 64-bit kernel. With 8kB page size, maximum 75caefba17SHugh Dickins * file size is just over 4TB on a 64-bit kernel, but 16TB on a 32-bit kernel, 76caefba17SHugh Dickins * MAX_LFS_FILESIZE being then more restrictive than swap vector layout. 77caefba17SHugh Dickins * 78caefba17SHugh Dickins * We use / and * instead of shifts in the definitions below, so that the swap 79caefba17SHugh Dickins * vector can be tested with small even values (e.g. 20) for ENTRIES_PER_PAGE. 80caefba17SHugh Dickins */ 811da177e4SLinus Torvalds #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long)) 8261609d01SYuri Tikhonov #define ENTRIES_PER_PAGEPAGE ((unsigned long long)ENTRIES_PER_PAGE*ENTRIES_PER_PAGE) 83caefba17SHugh Dickins 84caefba17SHugh Dickins #define SHMSWP_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1)) 85caefba17SHugh Dickins #define SHMSWP_MAX_BYTES (SHMSWP_MAX_INDEX << PAGE_CACHE_SHIFT) 86caefba17SHugh Dickins 87caefba17SHugh Dickins #define SHMEM_MAX_BYTES min_t(unsigned long long, SHMSWP_MAX_BYTES, MAX_LFS_FILESIZE) 88caefba17SHugh Dickins #define SHMEM_MAX_INDEX ((unsigned long)((SHMEM_MAX_BYTES+1) >> PAGE_CACHE_SHIFT)) 89caefba17SHugh Dickins 901da177e4SLinus Torvalds #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) 911da177e4SLinus Torvalds #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* info->flags needs VM_flags to handle pagein/truncate races efficiently */ 941da177e4SLinus Torvalds #define SHMEM_PAGEIN VM_READ 951da177e4SLinus Torvalds #define SHMEM_TRUNCATE VM_WRITE 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* Definition to limit shmem_truncate's steps between cond_rescheds */ 981da177e4SLinus Torvalds #define LATENCY_LIMIT 64 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 1011da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 1021da177e4SLinus Torvalds 103b09e0fa4SEric Paris struct shmem_xattr { 104b09e0fa4SEric Paris struct list_head list; /* anchored by shmem_inode_info->xattr_list */ 105b09e0fa4SEric Paris char *name; /* xattr name */ 106b09e0fa4SEric Paris size_t size; 107b09e0fa4SEric Paris char value[0]; 108b09e0fa4SEric Paris }; 109b09e0fa4SEric Paris 1101da177e4SLinus Torvalds /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */ 1111da177e4SLinus Torvalds enum sgp_type { 1121da177e4SLinus Torvalds SGP_READ, /* don't exceed i_size, don't allocate page */ 1131da177e4SLinus Torvalds SGP_CACHE, /* don't exceed i_size, may allocate page */ 114a0ee5ec5SHugh Dickins SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ 1151da177e4SLinus Torvalds SGP_WRITE, /* may exceed i_size, may allocate page */ 1161da177e4SLinus Torvalds }; 1171da177e4SLinus Torvalds 118b76db735SAndrew Morton #ifdef CONFIG_TMPFS 119680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 120680d794bSakpm@linux-foundation.org { 121680d794bSakpm@linux-foundation.org return totalram_pages / 2; 122680d794bSakpm@linux-foundation.org } 123680d794bSakpm@linux-foundation.org 124680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 125680d794bSakpm@linux-foundation.org { 126680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 127680d794bSakpm@linux-foundation.org } 128b76db735SAndrew Morton #endif 129680d794bSakpm@linux-foundation.org 1301da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx, 1311da177e4SLinus Torvalds struct page **pagep, enum sgp_type sgp, int *type); 1321da177e4SLinus Torvalds 1336daa0e28SAl Viro static inline struct page *shmem_dir_alloc(gfp_t gfp_mask) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds /* 1361da177e4SLinus Torvalds * The above definition of ENTRIES_PER_PAGE, and the use of 1371da177e4SLinus Torvalds * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE: 1381da177e4SLinus Torvalds * might be reconsidered if it ever diverges from PAGE_SIZE. 139769848c0SMel Gorman * 140e12ba74dSMel Gorman * Mobility flags are masked out as swap vectors cannot move 1411da177e4SLinus Torvalds */ 142e12ba74dSMel Gorman return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO, 143769848c0SMel Gorman PAGE_CACHE_SHIFT-PAGE_SHIFT); 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds static inline void shmem_dir_free(struct page *page) 1471da177e4SLinus Torvalds { 1481da177e4SLinus Torvalds __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT); 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static struct page **shmem_dir_map(struct page *page) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds return (struct page **)kmap_atomic(page, KM_USER0); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds static inline void shmem_dir_unmap(struct page **dir) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds kunmap_atomic(dir, KM_USER0); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds static swp_entry_t *shmem_swp_map(struct page *page) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds return (swp_entry_t *)kmap_atomic(page, KM_USER1); 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds static inline void shmem_swp_balance_unmap(void) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds /* 1691da177e4SLinus Torvalds * When passing a pointer to an i_direct entry, to code which 1701da177e4SLinus Torvalds * also handles indirect entries and so will shmem_swp_unmap, 1711da177e4SLinus Torvalds * we must arrange for the preempt count to remain in balance. 1721da177e4SLinus Torvalds * What kmap_atomic of a lowmem page does depends on config 1731da177e4SLinus Torvalds * and architecture, so pretend to kmap_atomic some lowmem page. 1741da177e4SLinus Torvalds */ 1751da177e4SLinus Torvalds (void) kmap_atomic(ZERO_PAGE(0), KM_USER1); 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds static inline void shmem_swp_unmap(swp_entry_t *entry) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds kunmap_atomic(entry, KM_USER1); 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1841da177e4SLinus Torvalds { 1851da177e4SLinus Torvalds return sb->s_fs_info; 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* 1891da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1901da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1911da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1921da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1931da177e4SLinus Torvalds */ 1941da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1951da177e4SLinus Torvalds { 1960b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 1970b0a0806SHugh Dickins 0 : security_vm_enough_memory_kern(VM_ACCT(size)); 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 2011da177e4SLinus Torvalds { 2020b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 2031da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /* 2071da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 2081da177e4SLinus Torvalds * pages are allocated, in order to allow huge sparse files. 2091da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 2101da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags) 2131da177e4SLinus Torvalds { 2140b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 2150b0a0806SHugh Dickins security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 2191da177e4SLinus Torvalds { 2200b0a0806SHugh Dickins if (flags & VM_NORESERVE) 2211da177e4SLinus Torvalds vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 224759b9775SHugh Dickins static const struct super_operations shmem_ops; 225f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 22615ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 22792e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 22892e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 22992e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 230f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 2311da177e4SLinus Torvalds 2326c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info __read_mostly = { 2331da177e4SLinus Torvalds .ra_pages = 0, /* No readahead */ 2344f98a2feSRik van Riel .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, 2351da177e4SLinus Torvalds }; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 238cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds static void shmem_free_blocks(struct inode *inode, long pages) 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2430edd73b3SHugh Dickins if (sbinfo->max_blocks) { 2447e496299STim Chen percpu_counter_add(&sbinfo->used_blocks, -pages); 2451da177e4SLinus Torvalds inode->i_blocks -= pages*BLOCKS_PER_PAGE; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 2495b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 2505b04c689SPavel Emelyanov { 2515b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2525b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2535b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2545b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 2555b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2565b04c689SPavel Emelyanov return -ENOSPC; 2575b04c689SPavel Emelyanov } 2585b04c689SPavel Emelyanov sbinfo->free_inodes--; 2595b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2605b04c689SPavel Emelyanov } 2615b04c689SPavel Emelyanov return 0; 2625b04c689SPavel Emelyanov } 2635b04c689SPavel Emelyanov 2645b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2655b04c689SPavel Emelyanov { 2665b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2675b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2685b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2695b04c689SPavel Emelyanov sbinfo->free_inodes++; 2705b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2715b04c689SPavel Emelyanov } 2725b04c689SPavel Emelyanov } 2735b04c689SPavel Emelyanov 27446711810SRandy Dunlap /** 2751da177e4SLinus Torvalds * shmem_recalc_inode - recalculate the size of an inode 2761da177e4SLinus Torvalds * @inode: inode to recalc 2771da177e4SLinus Torvalds * 2781da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2791da177e4SLinus Torvalds * undirtied hole pages behind our back. 2801da177e4SLinus Torvalds * 2811da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2821da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2831da177e4SLinus Torvalds * 2841da177e4SLinus Torvalds * It has to be called with the spinlock held. 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2891da177e4SLinus Torvalds long freed; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2921da177e4SLinus Torvalds if (freed > 0) { 2931da177e4SLinus Torvalds info->alloced -= freed; 2941da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2951da177e4SLinus Torvalds shmem_free_blocks(inode, freed); 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 29946711810SRandy Dunlap /** 3001da177e4SLinus Torvalds * shmem_swp_entry - find the swap vector position in the info structure 3011da177e4SLinus Torvalds * @info: info structure for the inode 3021da177e4SLinus Torvalds * @index: index of the page to find 3031da177e4SLinus Torvalds * @page: optional page to add to the structure. Has to be preset to 3041da177e4SLinus Torvalds * all zeros 3051da177e4SLinus Torvalds * 3061da177e4SLinus Torvalds * If there is no space allocated yet it will return NULL when 3071da177e4SLinus Torvalds * page is NULL, else it will use the page for the needed block, 3081da177e4SLinus Torvalds * setting it to NULL on return to indicate that it has been used. 3091da177e4SLinus Torvalds * 3101da177e4SLinus Torvalds * The swap vector is organized the following way: 3111da177e4SLinus Torvalds * 3121da177e4SLinus Torvalds * There are SHMEM_NR_DIRECT entries directly stored in the 3131da177e4SLinus Torvalds * shmem_inode_info structure. So small files do not need an addional 3141da177e4SLinus Torvalds * allocation. 3151da177e4SLinus Torvalds * 3161da177e4SLinus Torvalds * For pages with index > SHMEM_NR_DIRECT there is the pointer 3171da177e4SLinus Torvalds * i_indirect which points to a page which holds in the first half 3181da177e4SLinus Torvalds * doubly indirect blocks, in the second half triple indirect blocks: 3191da177e4SLinus Torvalds * 3201da177e4SLinus Torvalds * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the 3211da177e4SLinus Torvalds * following layout (for SHMEM_NR_DIRECT == 16): 3221da177e4SLinus Torvalds * 3231da177e4SLinus Torvalds * i_indirect -> dir --> 16-19 3241da177e4SLinus Torvalds * | +-> 20-23 3251da177e4SLinus Torvalds * | 3261da177e4SLinus Torvalds * +-->dir2 --> 24-27 3271da177e4SLinus Torvalds * | +-> 28-31 3281da177e4SLinus Torvalds * | +-> 32-35 3291da177e4SLinus Torvalds * | +-> 36-39 3301da177e4SLinus Torvalds * | 3311da177e4SLinus Torvalds * +-->dir3 --> 40-43 3321da177e4SLinus Torvalds * +-> 44-47 3331da177e4SLinus Torvalds * +-> 48-51 3341da177e4SLinus Torvalds * +-> 52-55 3351da177e4SLinus Torvalds */ 3361da177e4SLinus Torvalds static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page) 3371da177e4SLinus Torvalds { 3381da177e4SLinus Torvalds unsigned long offset; 3391da177e4SLinus Torvalds struct page **dir; 3401da177e4SLinus Torvalds struct page *subdir; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds if (index < SHMEM_NR_DIRECT) { 3431da177e4SLinus Torvalds shmem_swp_balance_unmap(); 3441da177e4SLinus Torvalds return info->i_direct+index; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds if (!info->i_indirect) { 3471da177e4SLinus Torvalds if (page) { 3481da177e4SLinus Torvalds info->i_indirect = *page; 3491da177e4SLinus Torvalds *page = NULL; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds return NULL; /* need another page */ 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds index -= SHMEM_NR_DIRECT; 3551da177e4SLinus Torvalds offset = index % ENTRIES_PER_PAGE; 3561da177e4SLinus Torvalds index /= ENTRIES_PER_PAGE; 3571da177e4SLinus Torvalds dir = shmem_dir_map(info->i_indirect); 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds if (index >= ENTRIES_PER_PAGE/2) { 3601da177e4SLinus Torvalds index -= ENTRIES_PER_PAGE/2; 3611da177e4SLinus Torvalds dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE; 3621da177e4SLinus Torvalds index %= ENTRIES_PER_PAGE; 3631da177e4SLinus Torvalds subdir = *dir; 3641da177e4SLinus Torvalds if (!subdir) { 3651da177e4SLinus Torvalds if (page) { 3661da177e4SLinus Torvalds *dir = *page; 3671da177e4SLinus Torvalds *page = NULL; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds shmem_dir_unmap(dir); 3701da177e4SLinus Torvalds return NULL; /* need another page */ 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds shmem_dir_unmap(dir); 3731da177e4SLinus Torvalds dir = shmem_dir_map(subdir); 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds dir += index; 3771da177e4SLinus Torvalds subdir = *dir; 3781da177e4SLinus Torvalds if (!subdir) { 3791da177e4SLinus Torvalds if (!page || !(subdir = *page)) { 3801da177e4SLinus Torvalds shmem_dir_unmap(dir); 3811da177e4SLinus Torvalds return NULL; /* need a page */ 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds *dir = subdir; 3841da177e4SLinus Torvalds *page = NULL; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds shmem_dir_unmap(dir); 3871da177e4SLinus Torvalds return shmem_swp_map(subdir) + offset; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds long incdec = value? 1: -1; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds entry->val = value; 3951da177e4SLinus Torvalds info->swapped += incdec; 3964c21e2f2SHugh Dickins if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) { 3974c21e2f2SHugh Dickins struct page *page = kmap_atomic_to_page(entry); 3984c21e2f2SHugh Dickins set_page_private(page, page_private(page) + incdec); 3994c21e2f2SHugh Dickins } 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 40246711810SRandy Dunlap /** 4031da177e4SLinus Torvalds * shmem_swp_alloc - get the position of the swap entry for the page. 4041da177e4SLinus Torvalds * @info: info structure for the inode 4051da177e4SLinus Torvalds * @index: index of the page to find 4061da177e4SLinus Torvalds * @sgp: check and recheck i_size? skip allocation? 40746711810SRandy Dunlap * 40846711810SRandy Dunlap * If the entry does not exist, allocate it. 4091da177e4SLinus Torvalds */ 4101da177e4SLinus Torvalds static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp) 4111da177e4SLinus Torvalds { 4121da177e4SLinus Torvalds struct inode *inode = &info->vfs_inode; 4131da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 4141da177e4SLinus Torvalds struct page *page = NULL; 4151da177e4SLinus Torvalds swp_entry_t *entry; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds if (sgp != SGP_WRITE && 4181da177e4SLinus Torvalds ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 4191da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds while (!(entry = shmem_swp_entry(info, index, &page))) { 4221da177e4SLinus Torvalds if (sgp == SGP_READ) 4231da177e4SLinus Torvalds return shmem_swp_map(ZERO_PAGE(0)); 4241da177e4SLinus Torvalds /* 4257e496299STim Chen * Test used_blocks against 1 less max_blocks, since we have 1 data 4261da177e4SLinus Torvalds * page (and perhaps indirect index pages) yet to allocate: 4271da177e4SLinus Torvalds * a waste to allocate index if we cannot allocate data. 4281da177e4SLinus Torvalds */ 4290edd73b3SHugh Dickins if (sbinfo->max_blocks) { 430fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 431fc5da22aSHugh Dickins sbinfo->max_blocks - 1) >= 0) 4321da177e4SLinus Torvalds return ERR_PTR(-ENOSPC); 4337e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 4341da177e4SLinus Torvalds inode->i_blocks += BLOCKS_PER_PAGE; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds spin_unlock(&info->lock); 438769848c0SMel Gorman page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping)); 4391da177e4SLinus Torvalds spin_lock(&info->lock); 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds if (!page) { 4421da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 4431da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds if (sgp != SGP_WRITE && 4461da177e4SLinus Torvalds ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 4471da177e4SLinus Torvalds entry = ERR_PTR(-EINVAL); 4481da177e4SLinus Torvalds break; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds if (info->next_index <= index) 4511da177e4SLinus Torvalds info->next_index = index + 1; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds if (page) { 4541da177e4SLinus Torvalds /* another task gave its page, or truncated the file */ 4551da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 4561da177e4SLinus Torvalds shmem_dir_free(page); 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds if (info->next_index <= index && !IS_ERR(entry)) 4591da177e4SLinus Torvalds info->next_index = index + 1; 4601da177e4SLinus Torvalds return entry; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 46346711810SRandy Dunlap /** 4641da177e4SLinus Torvalds * shmem_free_swp - free some swap entries in a directory 4651da177e4SLinus Torvalds * @dir: pointer to the directory 4661da177e4SLinus Torvalds * @edir: pointer after last entry of the directory 4671ae70006SHugh Dickins * @punch_lock: pointer to spinlock when needed for the holepunch case 4681da177e4SLinus Torvalds */ 4691ae70006SHugh Dickins static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir, 4701ae70006SHugh Dickins spinlock_t *punch_lock) 4711da177e4SLinus Torvalds { 4721ae70006SHugh Dickins spinlock_t *punch_unlock = NULL; 4731da177e4SLinus Torvalds swp_entry_t *ptr; 4741da177e4SLinus Torvalds int freed = 0; 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds for (ptr = dir; ptr < edir; ptr++) { 4771da177e4SLinus Torvalds if (ptr->val) { 4781ae70006SHugh Dickins if (unlikely(punch_lock)) { 4791ae70006SHugh Dickins punch_unlock = punch_lock; 4801ae70006SHugh Dickins punch_lock = NULL; 4811ae70006SHugh Dickins spin_lock(punch_unlock); 4821ae70006SHugh Dickins if (!ptr->val) 4831ae70006SHugh Dickins continue; 4841ae70006SHugh Dickins } 4851da177e4SLinus Torvalds free_swap_and_cache(*ptr); 4861da177e4SLinus Torvalds *ptr = (swp_entry_t){0}; 4871da177e4SLinus Torvalds freed++; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds } 4901ae70006SHugh Dickins if (punch_unlock) 4911ae70006SHugh Dickins spin_unlock(punch_unlock); 4921da177e4SLinus Torvalds return freed; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 4951ae70006SHugh Dickins static int shmem_map_and_free_swp(struct page *subdir, int offset, 4961ae70006SHugh Dickins int limit, struct page ***dir, spinlock_t *punch_lock) 4971da177e4SLinus Torvalds { 4981da177e4SLinus Torvalds swp_entry_t *ptr; 4991da177e4SLinus Torvalds int freed = 0; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds ptr = shmem_swp_map(subdir); 5021da177e4SLinus Torvalds for (; offset < limit; offset += LATENCY_LIMIT) { 5031da177e4SLinus Torvalds int size = limit - offset; 5041da177e4SLinus Torvalds if (size > LATENCY_LIMIT) 5051da177e4SLinus Torvalds size = LATENCY_LIMIT; 5061ae70006SHugh Dickins freed += shmem_free_swp(ptr+offset, ptr+offset+size, 5071ae70006SHugh Dickins punch_lock); 5081da177e4SLinus Torvalds if (need_resched()) { 5091da177e4SLinus Torvalds shmem_swp_unmap(ptr); 5101da177e4SLinus Torvalds if (*dir) { 5111da177e4SLinus Torvalds shmem_dir_unmap(*dir); 5121da177e4SLinus Torvalds *dir = NULL; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds cond_resched(); 5151da177e4SLinus Torvalds ptr = shmem_swp_map(subdir); 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds shmem_swp_unmap(ptr); 5191da177e4SLinus Torvalds return freed; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds static void shmem_free_pages(struct list_head *next) 5231da177e4SLinus Torvalds { 5241da177e4SLinus Torvalds struct page *page; 5251da177e4SLinus Torvalds int freed = 0; 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds do { 5281da177e4SLinus Torvalds page = container_of(next, struct page, lru); 5291da177e4SLinus Torvalds next = next->next; 5301da177e4SLinus Torvalds shmem_dir_free(page); 5311da177e4SLinus Torvalds freed++; 5321da177e4SLinus Torvalds if (freed >= LATENCY_LIMIT) { 5331da177e4SLinus Torvalds cond_resched(); 5341da177e4SLinus Torvalds freed = 0; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds } while (next); 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds 53994c1e62dSHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end) 5401da177e4SLinus Torvalds { 5411da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 5421da177e4SLinus Torvalds unsigned long idx; 5431da177e4SLinus Torvalds unsigned long size; 5441da177e4SLinus Torvalds unsigned long limit; 5451da177e4SLinus Torvalds unsigned long stage; 5461da177e4SLinus Torvalds unsigned long diroff; 5471da177e4SLinus Torvalds struct page **dir; 5481da177e4SLinus Torvalds struct page *topdir; 5491da177e4SLinus Torvalds struct page *middir; 5501da177e4SLinus Torvalds struct page *subdir; 5511da177e4SLinus Torvalds swp_entry_t *ptr; 5521da177e4SLinus Torvalds LIST_HEAD(pages_to_free); 5531da177e4SLinus Torvalds long nr_pages_to_free = 0; 5541da177e4SLinus Torvalds long nr_swaps_freed = 0; 5551da177e4SLinus Torvalds int offset; 5561da177e4SLinus Torvalds int freed; 557a2646d1eSHugh Dickins int punch_hole; 5581ae70006SHugh Dickins spinlock_t *needs_lock; 5591ae70006SHugh Dickins spinlock_t *punch_lock; 560a2646d1eSHugh Dickins unsigned long upper_limit; 5611da177e4SLinus Torvalds 56294c1e62dSHugh Dickins truncate_inode_pages_range(inode->i_mapping, start, end); 56394c1e62dSHugh Dickins 5641da177e4SLinus Torvalds inode->i_ctime = inode->i_mtime = CURRENT_TIME; 565f6b3ec23SBadari Pulavarty idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 5661da177e4SLinus Torvalds if (idx >= info->next_index) 5671da177e4SLinus Torvalds return; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds spin_lock(&info->lock); 5701da177e4SLinus Torvalds info->flags |= SHMEM_TRUNCATE; 571f6b3ec23SBadari Pulavarty if (likely(end == (loff_t) -1)) { 5721da177e4SLinus Torvalds limit = info->next_index; 573a2646d1eSHugh Dickins upper_limit = SHMEM_MAX_INDEX; 5741da177e4SLinus Torvalds info->next_index = idx; 5751ae70006SHugh Dickins needs_lock = NULL; 576a2646d1eSHugh Dickins punch_hole = 0; 577f6b3ec23SBadari Pulavarty } else { 578a2646d1eSHugh Dickins if (end + 1 >= inode->i_size) { /* we may free a little more */ 579a2646d1eSHugh Dickins limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >> 580a2646d1eSHugh Dickins PAGE_CACHE_SHIFT; 581a2646d1eSHugh Dickins upper_limit = SHMEM_MAX_INDEX; 582a2646d1eSHugh Dickins } else { 583a2646d1eSHugh Dickins limit = (end + 1) >> PAGE_CACHE_SHIFT; 584a2646d1eSHugh Dickins upper_limit = limit; 585a2646d1eSHugh Dickins } 5861ae70006SHugh Dickins needs_lock = &info->lock; 587f6b3ec23SBadari Pulavarty punch_hole = 1; 588f6b3ec23SBadari Pulavarty } 589f6b3ec23SBadari Pulavarty 5901da177e4SLinus Torvalds topdir = info->i_indirect; 591f6b3ec23SBadari Pulavarty if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) { 5921da177e4SLinus Torvalds info->i_indirect = NULL; 5931da177e4SLinus Torvalds nr_pages_to_free++; 5941da177e4SLinus Torvalds list_add(&topdir->lru, &pages_to_free); 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds spin_unlock(&info->lock); 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds if (info->swapped && idx < SHMEM_NR_DIRECT) { 5991da177e4SLinus Torvalds ptr = info->i_direct; 6001da177e4SLinus Torvalds size = limit; 6011da177e4SLinus Torvalds if (size > SHMEM_NR_DIRECT) 6021da177e4SLinus Torvalds size = SHMEM_NR_DIRECT; 6031ae70006SHugh Dickins nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock); 6041da177e4SLinus Torvalds } 60592a3d03aSBadari Pulavarty 60692a3d03aSBadari Pulavarty /* 60792a3d03aSBadari Pulavarty * If there are no indirect blocks or we are punching a hole 60892a3d03aSBadari Pulavarty * below indirect blocks, nothing to be done. 60992a3d03aSBadari Pulavarty */ 610a2646d1eSHugh Dickins if (!topdir || limit <= SHMEM_NR_DIRECT) 6111da177e4SLinus Torvalds goto done2; 6121da177e4SLinus Torvalds 6131ae70006SHugh Dickins /* 6141ae70006SHugh Dickins * The truncation case has already dropped info->lock, and we're safe 6151ae70006SHugh Dickins * because i_size and next_index have already been lowered, preventing 6161ae70006SHugh Dickins * access beyond. But in the punch_hole case, we still need to take 6171ae70006SHugh Dickins * the lock when updating the swap directory, because there might be 6181ae70006SHugh Dickins * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or 6191ae70006SHugh Dickins * shmem_writepage. However, whenever we find we can remove a whole 6201ae70006SHugh Dickins * directory page (not at the misaligned start or end of the range), 6211ae70006SHugh Dickins * we first NULLify its pointer in the level above, and then have no 6221ae70006SHugh Dickins * need to take the lock when updating its contents: needs_lock and 6231ae70006SHugh Dickins * punch_lock (either pointing to info->lock or NULL) manage this. 6241ae70006SHugh Dickins */ 6251ae70006SHugh Dickins 626a2646d1eSHugh Dickins upper_limit -= SHMEM_NR_DIRECT; 6271da177e4SLinus Torvalds limit -= SHMEM_NR_DIRECT; 6281da177e4SLinus Torvalds idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0; 6291da177e4SLinus Torvalds offset = idx % ENTRIES_PER_PAGE; 6301da177e4SLinus Torvalds idx -= offset; 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds dir = shmem_dir_map(topdir); 6331da177e4SLinus Torvalds stage = ENTRIES_PER_PAGEPAGE/2; 6341da177e4SLinus Torvalds if (idx < ENTRIES_PER_PAGEPAGE/2) { 6351da177e4SLinus Torvalds middir = topdir; 6361da177e4SLinus Torvalds diroff = idx/ENTRIES_PER_PAGE; 6371da177e4SLinus Torvalds } else { 6381da177e4SLinus Torvalds dir += ENTRIES_PER_PAGE/2; 6391da177e4SLinus Torvalds dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE; 6401da177e4SLinus Torvalds while (stage <= idx) 6411da177e4SLinus Torvalds stage += ENTRIES_PER_PAGEPAGE; 6421da177e4SLinus Torvalds middir = *dir; 6431da177e4SLinus Torvalds if (*dir) { 6441da177e4SLinus Torvalds diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) % 6451da177e4SLinus Torvalds ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE; 646a2646d1eSHugh Dickins if (!diroff && !offset && upper_limit >= stage) { 6471ae70006SHugh Dickins if (needs_lock) { 6481ae70006SHugh Dickins spin_lock(needs_lock); 6491ae70006SHugh Dickins *dir = NULL; 6501ae70006SHugh Dickins spin_unlock(needs_lock); 6511ae70006SHugh Dickins needs_lock = NULL; 6521ae70006SHugh Dickins } else 6531da177e4SLinus Torvalds *dir = NULL; 6541da177e4SLinus Torvalds nr_pages_to_free++; 6551da177e4SLinus Torvalds list_add(&middir->lru, &pages_to_free); 6561da177e4SLinus Torvalds } 6571da177e4SLinus Torvalds shmem_dir_unmap(dir); 6581da177e4SLinus Torvalds dir = shmem_dir_map(middir); 6591da177e4SLinus Torvalds } else { 6601da177e4SLinus Torvalds diroff = 0; 6611da177e4SLinus Torvalds offset = 0; 6621da177e4SLinus Torvalds idx = stage; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) { 6671da177e4SLinus Torvalds if (unlikely(idx == stage)) { 6681da177e4SLinus Torvalds shmem_dir_unmap(dir); 6691da177e4SLinus Torvalds dir = shmem_dir_map(topdir) + 6701da177e4SLinus Torvalds ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE; 6711da177e4SLinus Torvalds while (!*dir) { 6721da177e4SLinus Torvalds dir++; 6731da177e4SLinus Torvalds idx += ENTRIES_PER_PAGEPAGE; 6741da177e4SLinus Torvalds if (idx >= limit) 6751da177e4SLinus Torvalds goto done1; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds stage = idx + ENTRIES_PER_PAGEPAGE; 6781da177e4SLinus Torvalds middir = *dir; 6791ae70006SHugh Dickins if (punch_hole) 6801ae70006SHugh Dickins needs_lock = &info->lock; 681a2646d1eSHugh Dickins if (upper_limit >= stage) { 6821ae70006SHugh Dickins if (needs_lock) { 6831ae70006SHugh Dickins spin_lock(needs_lock); 6841ae70006SHugh Dickins *dir = NULL; 6851ae70006SHugh Dickins spin_unlock(needs_lock); 6861ae70006SHugh Dickins needs_lock = NULL; 6871ae70006SHugh Dickins } else 6881da177e4SLinus Torvalds *dir = NULL; 6891da177e4SLinus Torvalds nr_pages_to_free++; 6901da177e4SLinus Torvalds list_add(&middir->lru, &pages_to_free); 691a2646d1eSHugh Dickins } 6921da177e4SLinus Torvalds shmem_dir_unmap(dir); 6931da177e4SLinus Torvalds cond_resched(); 6941da177e4SLinus Torvalds dir = shmem_dir_map(middir); 6951da177e4SLinus Torvalds diroff = 0; 6961da177e4SLinus Torvalds } 6971ae70006SHugh Dickins punch_lock = needs_lock; 6981da177e4SLinus Torvalds subdir = dir[diroff]; 6991ae70006SHugh Dickins if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) { 7001ae70006SHugh Dickins if (needs_lock) { 7011ae70006SHugh Dickins spin_lock(needs_lock); 7021ae70006SHugh Dickins dir[diroff] = NULL; 7031ae70006SHugh Dickins spin_unlock(needs_lock); 7041ae70006SHugh Dickins punch_lock = NULL; 7051ae70006SHugh Dickins } else 7061da177e4SLinus Torvalds dir[diroff] = NULL; 7071da177e4SLinus Torvalds nr_pages_to_free++; 7081da177e4SLinus Torvalds list_add(&subdir->lru, &pages_to_free); 7091da177e4SLinus Torvalds } 7101ae70006SHugh Dickins if (subdir && page_private(subdir) /* has swap entries */) { 7111ae70006SHugh Dickins size = limit - idx; 7121ae70006SHugh Dickins if (size > ENTRIES_PER_PAGE) 7131ae70006SHugh Dickins size = ENTRIES_PER_PAGE; 7141ae70006SHugh Dickins freed = shmem_map_and_free_swp(subdir, 7151ae70006SHugh Dickins offset, size, &dir, punch_lock); 7161ae70006SHugh Dickins if (!dir) 7171ae70006SHugh Dickins dir = shmem_dir_map(middir); 7181ae70006SHugh Dickins nr_swaps_freed += freed; 7191ae70006SHugh Dickins if (offset || punch_lock) { 7201ae70006SHugh Dickins spin_lock(&info->lock); 7211ae70006SHugh Dickins set_page_private(subdir, 7221ae70006SHugh Dickins page_private(subdir) - freed); 7231ae70006SHugh Dickins spin_unlock(&info->lock); 7241ae70006SHugh Dickins } else 7251ae70006SHugh Dickins BUG_ON(page_private(subdir) != freed); 7261ae70006SHugh Dickins } 7271ae70006SHugh Dickins offset = 0; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds done1: 7301da177e4SLinus Torvalds shmem_dir_unmap(dir); 7311da177e4SLinus Torvalds done2: 7321da177e4SLinus Torvalds if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) { 7331da177e4SLinus Torvalds /* 7341da177e4SLinus Torvalds * Call truncate_inode_pages again: racing shmem_unuse_inode 7353889e6e7Snpiggin@suse.de * may have swizzled a page in from swap since 7363889e6e7Snpiggin@suse.de * truncate_pagecache or generic_delete_inode did it, before we 7373889e6e7Snpiggin@suse.de * lowered next_index. Also, though shmem_getpage checks 7383889e6e7Snpiggin@suse.de * i_size before adding to cache, no recheck after: so fix the 7393889e6e7Snpiggin@suse.de * narrow window there too. 7401da177e4SLinus Torvalds */ 741f6b3ec23SBadari Pulavarty truncate_inode_pages_range(inode->i_mapping, start, end); 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds spin_lock(&info->lock); 7451da177e4SLinus Torvalds info->flags &= ~SHMEM_TRUNCATE; 7461da177e4SLinus Torvalds info->swapped -= nr_swaps_freed; 7471da177e4SLinus Torvalds if (nr_pages_to_free) 7481da177e4SLinus Torvalds shmem_free_blocks(inode, nr_pages_to_free); 7491da177e4SLinus Torvalds shmem_recalc_inode(inode); 7501da177e4SLinus Torvalds spin_unlock(&info->lock); 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /* 7531da177e4SLinus Torvalds * Empty swap vector directory pages to be freed? 7541da177e4SLinus Torvalds */ 7551da177e4SLinus Torvalds if (!list_empty(&pages_to_free)) { 7561da177e4SLinus Torvalds pages_to_free.prev->next = NULL; 7571da177e4SLinus Torvalds shmem_free_pages(pages_to_free.next); 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds } 76094c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 7611da177e4SLinus Torvalds 76294c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 7631da177e4SLinus Torvalds { 7641da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 7651da177e4SLinus Torvalds int error; 7661da177e4SLinus Torvalds 767db78b877SChristoph Hellwig error = inode_change_ok(inode, attr); 768db78b877SChristoph Hellwig if (error) 769db78b877SChristoph Hellwig return error; 770db78b877SChristoph Hellwig 77194c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 77294c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 77394c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 7743889e6e7Snpiggin@suse.de struct page *page = NULL; 7753889e6e7Snpiggin@suse.de 77694c1e62dSHugh Dickins if (newsize < oldsize) { 7771da177e4SLinus Torvalds /* 7781da177e4SLinus Torvalds * If truncating down to a partial page, then 7791da177e4SLinus Torvalds * if that page is already allocated, hold it 7801da177e4SLinus Torvalds * in memory until the truncation is over, so 781ae0e47f0SJustin P. Mattock * truncate_partial_page cannot miss it were 7821da177e4SLinus Torvalds * it assigned to swap. 7831da177e4SLinus Torvalds */ 7843889e6e7Snpiggin@suse.de if (newsize & (PAGE_CACHE_SIZE-1)) { 7851da177e4SLinus Torvalds (void) shmem_getpage(inode, 7863889e6e7Snpiggin@suse.de newsize >> PAGE_CACHE_SHIFT, 7871da177e4SLinus Torvalds &page, SGP_READ, NULL); 788d3602444SHugh Dickins if (page) 789d3602444SHugh Dickins unlock_page(page); 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds /* 7921da177e4SLinus Torvalds * Reset SHMEM_PAGEIN flag so that shmem_truncate can 7931da177e4SLinus Torvalds * detect if any pages might have been added to cache 7941da177e4SLinus Torvalds * after truncate_inode_pages. But we needn't bother 7951da177e4SLinus Torvalds * if it's being fully truncated to zero-length: the 7961da177e4SLinus Torvalds * nrpages check is efficient enough in that case. 7971da177e4SLinus Torvalds */ 7983889e6e7Snpiggin@suse.de if (newsize) { 7991da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 8001da177e4SLinus Torvalds spin_lock(&info->lock); 8011da177e4SLinus Torvalds info->flags &= ~SHMEM_PAGEIN; 8021da177e4SLinus Torvalds spin_unlock(&info->lock); 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds } 80594c1e62dSHugh Dickins if (newsize != oldsize) { 80694c1e62dSHugh Dickins i_size_write(inode, newsize); 80794c1e62dSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 80894c1e62dSHugh Dickins } 80994c1e62dSHugh Dickins if (newsize < oldsize) { 81094c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 81194c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 81294c1e62dSHugh Dickins shmem_truncate_range(inode, newsize, (loff_t)-1); 81394c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 81494c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 81594c1e62dSHugh Dickins } 8163889e6e7Snpiggin@suse.de if (page) 8173889e6e7Snpiggin@suse.de page_cache_release(page); 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 8206a1a90adSChristoph Hellwig setattr_copy(inode, attr); 82139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 822db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 8231c7c474cSChristoph Hellwig error = generic_acl_chmod(inode); 82439f0247dSAndreas Gruenbacher #endif 8251da177e4SLinus Torvalds return error; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 8291da177e4SLinus Torvalds { 8301da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 831b09e0fa4SEric Paris struct shmem_xattr *xattr, *nxattr; 8321da177e4SLinus Torvalds 8333889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 8341da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 8351da177e4SLinus Torvalds inode->i_size = 0; 8363889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 8371da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 838cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 8391da177e4SLinus Torvalds list_del_init(&info->swaplist); 840cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds } 843b09e0fa4SEric Paris 844b09e0fa4SEric Paris list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) { 845b09e0fa4SEric Paris kfree(xattr->name); 846b09e0fa4SEric Paris kfree(xattr); 847b09e0fa4SEric Paris } 8481da177e4SLinus Torvalds BUG_ON(inode->i_blocks); 8495b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 8501f895f75SAl Viro end_writeback(inode); 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir) 8541da177e4SLinus Torvalds { 8551da177e4SLinus Torvalds swp_entry_t *ptr; 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds for (ptr = dir; ptr < edir; ptr++) { 8581da177e4SLinus Torvalds if (ptr->val == entry.val) 8591da177e4SLinus Torvalds return ptr - dir; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds return -1; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page) 8651da177e4SLinus Torvalds { 866778dd893SHugh Dickins struct address_space *mapping; 8671da177e4SLinus Torvalds unsigned long idx; 8681da177e4SLinus Torvalds unsigned long size; 8691da177e4SLinus Torvalds unsigned long limit; 8701da177e4SLinus Torvalds unsigned long stage; 8711da177e4SLinus Torvalds struct page **dir; 8721da177e4SLinus Torvalds struct page *subdir; 8731da177e4SLinus Torvalds swp_entry_t *ptr; 8741da177e4SLinus Torvalds int offset; 875d9fe526aSHugh Dickins int error; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds idx = 0; 8781da177e4SLinus Torvalds ptr = info->i_direct; 8791da177e4SLinus Torvalds spin_lock(&info->lock); 8801b1b32f2SHugh Dickins if (!info->swapped) { 8811b1b32f2SHugh Dickins list_del_init(&info->swaplist); 8821b1b32f2SHugh Dickins goto lost2; 8831b1b32f2SHugh Dickins } 8841da177e4SLinus Torvalds limit = info->next_index; 8851da177e4SLinus Torvalds size = limit; 8861da177e4SLinus Torvalds if (size > SHMEM_NR_DIRECT) 8871da177e4SLinus Torvalds size = SHMEM_NR_DIRECT; 8881da177e4SLinus Torvalds offset = shmem_find_swp(entry, ptr, ptr+size); 889778dd893SHugh Dickins if (offset >= 0) { 890778dd893SHugh Dickins shmem_swp_balance_unmap(); 8911da177e4SLinus Torvalds goto found; 892778dd893SHugh Dickins } 8931da177e4SLinus Torvalds if (!info->i_indirect) 8941da177e4SLinus Torvalds goto lost2; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds dir = shmem_dir_map(info->i_indirect); 8971da177e4SLinus Torvalds stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) { 9001da177e4SLinus Torvalds if (unlikely(idx == stage)) { 9011da177e4SLinus Torvalds shmem_dir_unmap(dir-1); 902cb5f7b9aSHugh Dickins if (cond_resched_lock(&info->lock)) { 903cb5f7b9aSHugh Dickins /* check it has not been truncated */ 904cb5f7b9aSHugh Dickins if (limit > info->next_index) { 905cb5f7b9aSHugh Dickins limit = info->next_index; 906cb5f7b9aSHugh Dickins if (idx >= limit) 907cb5f7b9aSHugh Dickins goto lost2; 908cb5f7b9aSHugh Dickins } 909cb5f7b9aSHugh Dickins } 9101da177e4SLinus Torvalds dir = shmem_dir_map(info->i_indirect) + 9111da177e4SLinus Torvalds ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE; 9121da177e4SLinus Torvalds while (!*dir) { 9131da177e4SLinus Torvalds dir++; 9141da177e4SLinus Torvalds idx += ENTRIES_PER_PAGEPAGE; 9151da177e4SLinus Torvalds if (idx >= limit) 9161da177e4SLinus Torvalds goto lost1; 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds stage = idx + ENTRIES_PER_PAGEPAGE; 9191da177e4SLinus Torvalds subdir = *dir; 9201da177e4SLinus Torvalds shmem_dir_unmap(dir); 9211da177e4SLinus Torvalds dir = shmem_dir_map(subdir); 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds subdir = *dir; 9244c21e2f2SHugh Dickins if (subdir && page_private(subdir)) { 9251da177e4SLinus Torvalds ptr = shmem_swp_map(subdir); 9261da177e4SLinus Torvalds size = limit - idx; 9271da177e4SLinus Torvalds if (size > ENTRIES_PER_PAGE) 9281da177e4SLinus Torvalds size = ENTRIES_PER_PAGE; 9291da177e4SLinus Torvalds offset = shmem_find_swp(entry, ptr, ptr+size); 930e6c9366bSHugh Dickins shmem_swp_unmap(ptr); 9311da177e4SLinus Torvalds if (offset >= 0) { 9321da177e4SLinus Torvalds shmem_dir_unmap(dir); 933e6c9366bSHugh Dickins ptr = shmem_swp_map(subdir); 9341da177e4SLinus Torvalds goto found; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds lost1: 9391da177e4SLinus Torvalds shmem_dir_unmap(dir-1); 9401da177e4SLinus Torvalds lost2: 9411da177e4SLinus Torvalds spin_unlock(&info->lock); 9421da177e4SLinus Torvalds return 0; 9431da177e4SLinus Torvalds found: 9441da177e4SLinus Torvalds idx += offset; 945778dd893SHugh Dickins ptr += offset; 9462e0e26c7SHugh Dickins 9471b1b32f2SHugh Dickins /* 9481b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 9491f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 9501b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 9511b1b32f2SHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. We 9521b1b32f2SHugh Dickins * could avoid doing it if inode NULL; or use this minor optimization. 9531b1b32f2SHugh Dickins */ 9541b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 9552e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 9562e0e26c7SHugh Dickins 957d13d1443SKAMEZAWA Hiroyuki /* 958778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 959778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 960778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 961d13d1443SKAMEZAWA Hiroyuki */ 962778dd893SHugh Dickins mapping = info->vfs_inode.i_mapping; 963778dd893SHugh Dickins error = add_to_page_cache_locked(page, mapping, idx, GFP_NOWAIT); 964778dd893SHugh Dickins /* which does mem_cgroup_uncharge_cache_page on error */ 96569029cd5SKAMEZAWA Hiroyuki 966d9fe526aSHugh Dickins if (error == -EEXIST) { 967778dd893SHugh Dickins struct page *filepage = find_get_page(mapping, idx); 9682e0e26c7SHugh Dickins error = 1; 969d9fe526aSHugh Dickins if (filepage) { 970d9fe526aSHugh Dickins /* 971d9fe526aSHugh Dickins * There might be a more uptodate page coming down 972d9fe526aSHugh Dickins * from a stacked writepage: forget our swappage if so. 973d9fe526aSHugh Dickins */ 974d9fe526aSHugh Dickins if (PageUptodate(filepage)) 975d9fe526aSHugh Dickins error = 0; 976d9fe526aSHugh Dickins page_cache_release(filepage); 977d9fe526aSHugh Dickins } 978d9fe526aSHugh Dickins } 979d9fe526aSHugh Dickins if (!error) { 98073b1262fSHugh Dickins delete_from_swap_cache(page); 98173b1262fSHugh Dickins set_page_dirty(page); 9821da177e4SLinus Torvalds info->flags |= SHMEM_PAGEIN; 9832e0e26c7SHugh Dickins shmem_swp_set(info, ptr, 0); 9842e0e26c7SHugh Dickins swap_free(entry); 9852e0e26c7SHugh Dickins error = 1; /* not an error, but entry was found */ 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds shmem_swp_unmap(ptr); 9881da177e4SLinus Torvalds spin_unlock(&info->lock); 9892e0e26c7SHugh Dickins return error; 9901da177e4SLinus Torvalds } 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds /* 9931da177e4SLinus Torvalds * shmem_unuse() search for an eventually swapped out shmem page. 9941da177e4SLinus Torvalds */ 9951da177e4SLinus Torvalds int shmem_unuse(swp_entry_t entry, struct page *page) 9961da177e4SLinus Torvalds { 9971da177e4SLinus Torvalds struct list_head *p, *next; 9981da177e4SLinus Torvalds struct shmem_inode_info *info; 9991da177e4SLinus Torvalds int found = 0; 1000778dd893SHugh Dickins int error; 1001778dd893SHugh Dickins 1002778dd893SHugh Dickins /* 1003778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 1004778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 1005778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 1006778dd893SHugh Dickins * add_to_page_cache() will be called with GFP_NOWAIT. 1007778dd893SHugh Dickins */ 1008778dd893SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL); 1009778dd893SHugh Dickins if (error) 1010778dd893SHugh Dickins goto out; 1011778dd893SHugh Dickins /* 1012778dd893SHugh Dickins * Try to preload while we can wait, to not make a habit of 1013778dd893SHugh Dickins * draining atomic reserves; but don't latch on to this cpu, 1014778dd893SHugh Dickins * it's okay if sometimes we get rescheduled after this. 1015778dd893SHugh Dickins */ 1016778dd893SHugh Dickins error = radix_tree_preload(GFP_KERNEL); 1017778dd893SHugh Dickins if (error) 1018778dd893SHugh Dickins goto uncharge; 1019778dd893SHugh Dickins radix_tree_preload_end(); 10201da177e4SLinus Torvalds 1021cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 10221da177e4SLinus Torvalds list_for_each_safe(p, next, &shmem_swaplist) { 10231da177e4SLinus Torvalds info = list_entry(p, struct shmem_inode_info, swaplist); 10242e0e26c7SHugh Dickins found = shmem_unuse_inode(info, entry, page); 1025cb5f7b9aSHugh Dickins cond_resched(); 10262e0e26c7SHugh Dickins if (found) 1027778dd893SHugh Dickins break; 10281da177e4SLinus Torvalds } 1029cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1030778dd893SHugh Dickins 1031778dd893SHugh Dickins uncharge: 1032778dd893SHugh Dickins if (!found) 1033778dd893SHugh Dickins mem_cgroup_uncharge_cache_page(page); 1034778dd893SHugh Dickins if (found < 0) 1035778dd893SHugh Dickins error = found; 1036778dd893SHugh Dickins out: 1037aaa46865SHugh Dickins unlock_page(page); 1038aaa46865SHugh Dickins page_cache_release(page); 1039778dd893SHugh Dickins return error; 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds /* 10431da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 10441da177e4SLinus Torvalds */ 10451da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 10461da177e4SLinus Torvalds { 10471da177e4SLinus Torvalds struct shmem_inode_info *info; 10481da177e4SLinus Torvalds swp_entry_t *entry, swap; 10491da177e4SLinus Torvalds struct address_space *mapping; 10501da177e4SLinus Torvalds unsigned long index; 10511da177e4SLinus Torvalds struct inode *inode; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 10541da177e4SLinus Torvalds mapping = page->mapping; 10551da177e4SLinus Torvalds index = page->index; 10561da177e4SLinus Torvalds inode = mapping->host; 10571da177e4SLinus Torvalds info = SHMEM_I(inode); 10581da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 10591da177e4SLinus Torvalds goto redirty; 1060d9fe526aSHugh Dickins if (!total_swap_pages) 10611da177e4SLinus Torvalds goto redirty; 10621da177e4SLinus Torvalds 1063d9fe526aSHugh Dickins /* 1064d9fe526aSHugh Dickins * shmem_backing_dev_info's capabilities prevent regular writeback or 1065d9fe526aSHugh Dickins * sync from ever calling shmem_writepage; but a stacking filesystem 1066d9fe526aSHugh Dickins * may use the ->writepage of its underlying filesystem, in which case 1067d9fe526aSHugh Dickins * tmpfs should write out to swap only in response to memory pressure, 10685b0830cbSJens Axboe * and not for the writeback threads or sync. However, in those cases, 10695b0830cbSJens Axboe * we do still want to check if there's a redundant swappage to be 10705b0830cbSJens Axboe * discarded. 1071d9fe526aSHugh Dickins */ 1072d9fe526aSHugh Dickins if (wbc->for_reclaim) 1073d9fe526aSHugh Dickins swap = get_swap_page(); 1074d9fe526aSHugh Dickins else 1075d9fe526aSHugh Dickins swap.val = 0; 1076d9fe526aSHugh Dickins 1077b1dea800SHugh Dickins /* 1078b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 1079b1dea800SHugh Dickins * if it's not already there. Do it now because we cannot take 1080b1dea800SHugh Dickins * mutex while holding spinlock, and must do so before the page 1081b1dea800SHugh Dickins * is moved to swap cache, when its pagelock no longer protects 1082b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 1083b1dea800SHugh Dickins * we've taken the spinlock, because shmem_unuse_inode() will 1084b1dea800SHugh Dickins * prune a !swapped inode from the swaplist under both locks. 1085b1dea800SHugh Dickins */ 108605bf86b4SHugh Dickins if (swap.val) { 1087b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 108805bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 108905bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 1090b1dea800SHugh Dickins } 1091b1dea800SHugh Dickins 10921da177e4SLinus Torvalds spin_lock(&info->lock); 109305bf86b4SHugh Dickins if (swap.val) 1094b1dea800SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1095b1dea800SHugh Dickins 10961da177e4SLinus Torvalds if (index >= info->next_index) { 10971da177e4SLinus Torvalds BUG_ON(!(info->flags & SHMEM_TRUNCATE)); 10981da177e4SLinus Torvalds goto unlock; 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds entry = shmem_swp_entry(info, index, NULL); 1101d9fe526aSHugh Dickins if (entry->val) { 1102d9fe526aSHugh Dickins /* 1103d9fe526aSHugh Dickins * The more uptodate page coming down from a stacked 1104d9fe526aSHugh Dickins * writepage should replace our old swappage. 1105d9fe526aSHugh Dickins */ 1106d9fe526aSHugh Dickins free_swap_and_cache(*entry); 1107d9fe526aSHugh Dickins shmem_swp_set(info, entry, 0); 1108d9fe526aSHugh Dickins } 1109d9fe526aSHugh Dickins shmem_recalc_inode(inode); 11101da177e4SLinus Torvalds 1111d9fe526aSHugh Dickins if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 11124c73b1bcSMinchan Kim delete_from_page_cache(page); 11131da177e4SLinus Torvalds shmem_swp_set(info, entry, swap.val); 11141da177e4SLinus Torvalds shmem_swp_unmap(entry); 1115aaa46865SHugh Dickins swap_shmem_alloc(swap); 1116826267cfSHugh Dickins spin_unlock(&info->lock); 1117d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 11189fab5619SHugh Dickins swap_writepage(page, wbc); 11191da177e4SLinus Torvalds return 0; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds shmem_swp_unmap(entry); 11231da177e4SLinus Torvalds unlock: 11241da177e4SLinus Torvalds spin_unlock(&info->lock); 11252ca4532aSDaisuke Nishimura /* 11262ca4532aSDaisuke Nishimura * add_to_swap_cache() doesn't return -EEXIST, so we can safely 11272ca4532aSDaisuke Nishimura * clear SWAP_HAS_CACHE flag. 11282ca4532aSDaisuke Nishimura */ 1129cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, NULL); 11301da177e4SLinus Torvalds redirty: 11311da177e4SLinus Torvalds set_page_dirty(page); 1132d9fe526aSHugh Dickins if (wbc->for_reclaim) 1133d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 1134d9fe526aSHugh Dickins unlock_page(page); 1135d9fe526aSHugh Dickins return 0; 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1139680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 114071fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1141680d794bSakpm@linux-foundation.org { 1142680d794bSakpm@linux-foundation.org char buffer[64]; 1143680d794bSakpm@linux-foundation.org 114471fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1145095f1fc4SLee Schermerhorn return; /* show nothing */ 1146095f1fc4SLee Schermerhorn 114771fe804bSLee Schermerhorn mpol_to_str(buffer, sizeof(buffer), mpol, 1); 1148095f1fc4SLee Schermerhorn 1149095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1150680d794bSakpm@linux-foundation.org } 115171fe804bSLee Schermerhorn 115271fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 115371fe804bSLee Schermerhorn { 115471fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 115571fe804bSLee Schermerhorn if (sbinfo->mpol) { 115671fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 115771fe804bSLee Schermerhorn mpol = sbinfo->mpol; 115871fe804bSLee Schermerhorn mpol_get(mpol); 115971fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 116071fe804bSLee Schermerhorn } 116171fe804bSLee Schermerhorn return mpol; 116271fe804bSLee Schermerhorn } 1163680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 1164680d794bSakpm@linux-foundation.org 116502098feaSHugh Dickins static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp, 116602098feaSHugh Dickins struct shmem_inode_info *info, unsigned long idx) 11671da177e4SLinus Torvalds { 116852cd3b07SLee Schermerhorn struct mempolicy mpol, *spol; 11691da177e4SLinus Torvalds struct vm_area_struct pvma; 1170c4cc6d07SHugh Dickins struct page *page; 11711da177e4SLinus Torvalds 117252cd3b07SLee Schermerhorn spol = mpol_cond_copy(&mpol, 117352cd3b07SLee Schermerhorn mpol_shared_policy_lookup(&info->policy, idx)); 117452cd3b07SLee Schermerhorn 11751da177e4SLinus Torvalds /* Create a pseudo vma that just contains the policy */ 1176c4cc6d07SHugh Dickins pvma.vm_start = 0; 11771da177e4SLinus Torvalds pvma.vm_pgoff = idx; 1178c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 117952cd3b07SLee Schermerhorn pvma.vm_policy = spol; 118002098feaSHugh Dickins page = swapin_readahead(entry, gfp, &pvma, 0); 11811da177e4SLinus Torvalds return page; 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds 118402098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp, 118502098feaSHugh Dickins struct shmem_inode_info *info, unsigned long idx) 11861da177e4SLinus Torvalds { 11871da177e4SLinus Torvalds struct vm_area_struct pvma; 11881da177e4SLinus Torvalds 1189c4cc6d07SHugh Dickins /* Create a pseudo vma that just contains the policy */ 1190c4cc6d07SHugh Dickins pvma.vm_start = 0; 11911da177e4SLinus Torvalds pvma.vm_pgoff = idx; 1192c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 1193c4cc6d07SHugh Dickins pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx); 119452cd3b07SLee Schermerhorn 119552cd3b07SLee Schermerhorn /* 119652cd3b07SLee Schermerhorn * alloc_page_vma() will drop the shared policy reference 119752cd3b07SLee Schermerhorn */ 119852cd3b07SLee Schermerhorn return alloc_page_vma(gfp, &pvma, 0); 11991da177e4SLinus Torvalds } 1200680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */ 1201680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 120271fe804bSLee Schermerhorn static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p) 1203680d794bSakpm@linux-foundation.org { 1204680d794bSakpm@linux-foundation.org } 1205680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 1206680d794bSakpm@linux-foundation.org 120702098feaSHugh Dickins static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp, 120802098feaSHugh Dickins struct shmem_inode_info *info, unsigned long idx) 12091da177e4SLinus Torvalds { 121002098feaSHugh Dickins return swapin_readahead(entry, gfp, NULL, 0); 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 121302098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp, 121402098feaSHugh Dickins struct shmem_inode_info *info, unsigned long idx) 12151da177e4SLinus Torvalds { 1216e84e2e13SHugh Dickins return alloc_page(gfp); 12171da177e4SLinus Torvalds } 1218680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */ 12191da177e4SLinus Torvalds 122071fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) 122171fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 122271fe804bSLee Schermerhorn { 122371fe804bSLee Schermerhorn return NULL; 122471fe804bSLee Schermerhorn } 122571fe804bSLee Schermerhorn #endif 122671fe804bSLee Schermerhorn 12271da177e4SLinus Torvalds /* 12281da177e4SLinus Torvalds * shmem_getpage - either get the page from swap or allocate a new one 12291da177e4SLinus Torvalds * 12301da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 12311da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 12321da177e4SLinus Torvalds * entry since a page cannot live in both the swap and page cache 12331da177e4SLinus Torvalds */ 12341da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx, 12351da177e4SLinus Torvalds struct page **pagep, enum sgp_type sgp, int *type) 12361da177e4SLinus Torvalds { 12371da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 12381da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 12391da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 12401da177e4SLinus Torvalds struct page *filepage = *pagep; 12411da177e4SLinus Torvalds struct page *swappage; 1242ff36b801SShaohua Li struct page *prealloc_page = NULL; 12431da177e4SLinus Torvalds swp_entry_t *entry; 12441da177e4SLinus Torvalds swp_entry_t swap; 124502098feaSHugh Dickins gfp_t gfp; 12461da177e4SLinus Torvalds int error; 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds if (idx >= SHMEM_MAX_INDEX) 12491da177e4SLinus Torvalds return -EFBIG; 125054cb8821SNick Piggin 125154cb8821SNick Piggin if (type) 125283c54070SNick Piggin *type = 0; 125354cb8821SNick Piggin 12541da177e4SLinus Torvalds /* 12551da177e4SLinus Torvalds * Normally, filepage is NULL on entry, and either found 12561da177e4SLinus Torvalds * uptodate immediately, or allocated and zeroed, or read 12571da177e4SLinus Torvalds * in under swappage, which is then assigned to filepage. 12585402b976SHugh Dickins * But shmem_readpage (required for splice) passes in a locked 1259ae976416SHugh Dickins * filepage, which may be found not uptodate by other callers 1260ae976416SHugh Dickins * too, and may need to be copied from the swappage read in. 12611da177e4SLinus Torvalds */ 12621da177e4SLinus Torvalds repeat: 12631da177e4SLinus Torvalds if (!filepage) 12641da177e4SLinus Torvalds filepage = find_lock_page(mapping, idx); 12651da177e4SLinus Torvalds if (filepage && PageUptodate(filepage)) 12661da177e4SLinus Torvalds goto done; 126702098feaSHugh Dickins gfp = mapping_gfp_mask(mapping); 1268b409f9fcSHugh Dickins if (!filepage) { 1269b409f9fcSHugh Dickins /* 1270b409f9fcSHugh Dickins * Try to preload while we can wait, to not make a habit of 1271b409f9fcSHugh Dickins * draining atomic reserves; but don't latch on to this cpu. 1272b409f9fcSHugh Dickins */ 1273b409f9fcSHugh Dickins error = radix_tree_preload(gfp & ~__GFP_HIGHMEM); 1274b409f9fcSHugh Dickins if (error) 1275b409f9fcSHugh Dickins goto failed; 1276b409f9fcSHugh Dickins radix_tree_preload_end(); 1277ff36b801SShaohua Li if (sgp != SGP_READ && !prealloc_page) { 1278ff36b801SShaohua Li /* We don't care if this fails */ 1279ff36b801SShaohua Li prealloc_page = shmem_alloc_page(gfp, info, idx); 1280ff36b801SShaohua Li if (prealloc_page) { 1281ff36b801SShaohua Li if (mem_cgroup_cache_charge(prealloc_page, 1282ff36b801SShaohua Li current->mm, GFP_KERNEL)) { 1283ff36b801SShaohua Li page_cache_release(prealloc_page); 1284ff36b801SShaohua Li prealloc_page = NULL; 1285b409f9fcSHugh Dickins } 1286ff36b801SShaohua Li } 1287ff36b801SShaohua Li } 1288ff36b801SShaohua Li } 1289ff36b801SShaohua Li error = 0; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds spin_lock(&info->lock); 12921da177e4SLinus Torvalds shmem_recalc_inode(inode); 12931da177e4SLinus Torvalds entry = shmem_swp_alloc(info, idx, sgp); 12941da177e4SLinus Torvalds if (IS_ERR(entry)) { 12951da177e4SLinus Torvalds spin_unlock(&info->lock); 12961da177e4SLinus Torvalds error = PTR_ERR(entry); 12971da177e4SLinus Torvalds goto failed; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds swap = *entry; 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds if (swap.val) { 13021da177e4SLinus Torvalds /* Look it up and read it in.. */ 13031da177e4SLinus Torvalds swappage = lookup_swap_cache(swap); 13041da177e4SLinus Torvalds if (!swappage) { 13051da177e4SLinus Torvalds shmem_swp_unmap(entry); 1306f8891e5eSChristoph Lameter spin_unlock(&info->lock); 1307456f998eSYing Han /* here we actually do the io */ 1308456f998eSYing Han if (type) 1309456f998eSYing Han *type |= VM_FAULT_MAJOR; 131002098feaSHugh Dickins swappage = shmem_swapin(swap, gfp, info, idx); 13111da177e4SLinus Torvalds if (!swappage) { 13121da177e4SLinus Torvalds spin_lock(&info->lock); 13131da177e4SLinus Torvalds entry = shmem_swp_alloc(info, idx, sgp); 13141da177e4SLinus Torvalds if (IS_ERR(entry)) 13151da177e4SLinus Torvalds error = PTR_ERR(entry); 13161da177e4SLinus Torvalds else { 13171da177e4SLinus Torvalds if (entry->val == swap.val) 13181da177e4SLinus Torvalds error = -ENOMEM; 13191da177e4SLinus Torvalds shmem_swp_unmap(entry); 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds spin_unlock(&info->lock); 13221da177e4SLinus Torvalds if (error) 13231da177e4SLinus Torvalds goto failed; 13241da177e4SLinus Torvalds goto repeat; 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds wait_on_page_locked(swappage); 13271da177e4SLinus Torvalds page_cache_release(swappage); 13281da177e4SLinus Torvalds goto repeat; 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 1332529ae9aaSNick Piggin if (!trylock_page(swappage)) { 13331da177e4SLinus Torvalds shmem_swp_unmap(entry); 13341da177e4SLinus Torvalds spin_unlock(&info->lock); 13351da177e4SLinus Torvalds wait_on_page_locked(swappage); 13361da177e4SLinus Torvalds page_cache_release(swappage); 13371da177e4SLinus Torvalds goto repeat; 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds if (PageWriteback(swappage)) { 13401da177e4SLinus Torvalds shmem_swp_unmap(entry); 13411da177e4SLinus Torvalds spin_unlock(&info->lock); 13421da177e4SLinus Torvalds wait_on_page_writeback(swappage); 13431da177e4SLinus Torvalds unlock_page(swappage); 13441da177e4SLinus Torvalds page_cache_release(swappage); 13451da177e4SLinus Torvalds goto repeat; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds if (!PageUptodate(swappage)) { 13481da177e4SLinus Torvalds shmem_swp_unmap(entry); 13491da177e4SLinus Torvalds spin_unlock(&info->lock); 13501da177e4SLinus Torvalds unlock_page(swappage); 13511da177e4SLinus Torvalds page_cache_release(swappage); 13521da177e4SLinus Torvalds error = -EIO; 13531da177e4SLinus Torvalds goto failed; 13541da177e4SLinus Torvalds } 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds if (filepage) { 13571da177e4SLinus Torvalds shmem_swp_set(info, entry, 0); 13581da177e4SLinus Torvalds shmem_swp_unmap(entry); 13591da177e4SLinus Torvalds delete_from_swap_cache(swappage); 13601da177e4SLinus Torvalds spin_unlock(&info->lock); 13611da177e4SLinus Torvalds copy_highpage(filepage, swappage); 13621da177e4SLinus Torvalds unlock_page(swappage); 13631da177e4SLinus Torvalds page_cache_release(swappage); 13641da177e4SLinus Torvalds flush_dcache_page(filepage); 13651da177e4SLinus Torvalds SetPageUptodate(filepage); 13661da177e4SLinus Torvalds set_page_dirty(filepage); 13671da177e4SLinus Torvalds swap_free(swap); 1368e286781dSNick Piggin } else if (!(error = add_to_page_cache_locked(swappage, mapping, 1369e286781dSNick Piggin idx, GFP_NOWAIT))) { 13701da177e4SLinus Torvalds info->flags |= SHMEM_PAGEIN; 13711da177e4SLinus Torvalds shmem_swp_set(info, entry, 0); 13721da177e4SLinus Torvalds shmem_swp_unmap(entry); 137373b1262fSHugh Dickins delete_from_swap_cache(swappage); 13741da177e4SLinus Torvalds spin_unlock(&info->lock); 13751da177e4SLinus Torvalds filepage = swappage; 137673b1262fSHugh Dickins set_page_dirty(filepage); 13771da177e4SLinus Torvalds swap_free(swap); 13781da177e4SLinus Torvalds } else { 13791da177e4SLinus Torvalds shmem_swp_unmap(entry); 13801da177e4SLinus Torvalds spin_unlock(&info->lock); 138182369553SHugh Dickins if (error == -ENOMEM) { 1382ae3abae6SDaisuke Nishimura /* 1383ae3abae6SDaisuke Nishimura * reclaim from proper memory cgroup and 1384ae3abae6SDaisuke Nishimura * call memcg's OOM if needed. 1385ae3abae6SDaisuke Nishimura */ 1386ae3abae6SDaisuke Nishimura error = mem_cgroup_shmem_charge_fallback( 1387ae3abae6SDaisuke Nishimura swappage, 1388b5a84319SKAMEZAWA Hiroyuki current->mm, 1389c9b0ed51SKAMEZAWA Hiroyuki gfp); 1390b5a84319SKAMEZAWA Hiroyuki if (error) { 1391b5a84319SKAMEZAWA Hiroyuki unlock_page(swappage); 1392b5a84319SKAMEZAWA Hiroyuki page_cache_release(swappage); 139382369553SHugh Dickins goto failed; 139482369553SHugh Dickins } 1395b5a84319SKAMEZAWA Hiroyuki } 1396b5a84319SKAMEZAWA Hiroyuki unlock_page(swappage); 1397b5a84319SKAMEZAWA Hiroyuki page_cache_release(swappage); 13981da177e4SLinus Torvalds goto repeat; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds } else if (sgp == SGP_READ && !filepage) { 14011da177e4SLinus Torvalds shmem_swp_unmap(entry); 14021da177e4SLinus Torvalds filepage = find_get_page(mapping, idx); 14031da177e4SLinus Torvalds if (filepage && 1404529ae9aaSNick Piggin (!PageUptodate(filepage) || !trylock_page(filepage))) { 14051da177e4SLinus Torvalds spin_unlock(&info->lock); 14061da177e4SLinus Torvalds wait_on_page_locked(filepage); 14071da177e4SLinus Torvalds page_cache_release(filepage); 14081da177e4SLinus Torvalds filepage = NULL; 14091da177e4SLinus Torvalds goto repeat; 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds spin_unlock(&info->lock); 14121da177e4SLinus Torvalds } else { 14131da177e4SLinus Torvalds shmem_swp_unmap(entry); 14141da177e4SLinus Torvalds sbinfo = SHMEM_SB(inode->i_sb); 14150edd73b3SHugh Dickins if (sbinfo->max_blocks) { 1416fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 1417fc5da22aSHugh Dickins sbinfo->max_blocks) >= 0 || 141859a16eadSHugh Dickins shmem_acct_block(info->flags)) 141959a16eadSHugh Dickins goto nospace; 14207e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 14211da177e4SLinus Torvalds inode->i_blocks += BLOCKS_PER_PAGE; 142259a16eadSHugh Dickins } else if (shmem_acct_block(info->flags)) 142359a16eadSHugh Dickins goto nospace; 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds if (!filepage) { 142669029cd5SKAMEZAWA Hiroyuki int ret; 142769029cd5SKAMEZAWA Hiroyuki 1428ff36b801SShaohua Li if (!prealloc_page) { 14291da177e4SLinus Torvalds spin_unlock(&info->lock); 143002098feaSHugh Dickins filepage = shmem_alloc_page(gfp, info, idx); 14311da177e4SLinus Torvalds if (!filepage) { 1432d515afe8SHugh Dickins spin_lock(&info->lock); 14331da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, 1); 14341da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 1435d515afe8SHugh Dickins spin_unlock(&info->lock); 14361da177e4SLinus Torvalds error = -ENOMEM; 14371da177e4SLinus Torvalds goto failed; 14381da177e4SLinus Torvalds } 1439b2e18538SRik van Riel SetPageSwapBacked(filepage); 14401da177e4SLinus Torvalds 1441ff36b801SShaohua Li /* 1442ff36b801SShaohua Li * Precharge page while we can wait, compensate 1443ff36b801SShaohua Li * after 1444ff36b801SShaohua Li */ 1445ff36b801SShaohua Li error = mem_cgroup_cache_charge(filepage, 1446ff36b801SShaohua Li current->mm, GFP_KERNEL); 144782369553SHugh Dickins if (error) { 144882369553SHugh Dickins page_cache_release(filepage); 1449d515afe8SHugh Dickins spin_lock(&info->lock); 145082369553SHugh Dickins shmem_unacct_blocks(info->flags, 1); 145182369553SHugh Dickins shmem_free_blocks(inode, 1); 1452d515afe8SHugh Dickins spin_unlock(&info->lock); 145382369553SHugh Dickins filepage = NULL; 145482369553SHugh Dickins goto failed; 145582369553SHugh Dickins } 145682369553SHugh Dickins 14571da177e4SLinus Torvalds spin_lock(&info->lock); 1458ff36b801SShaohua Li } else { 1459ff36b801SShaohua Li filepage = prealloc_page; 1460ff36b801SShaohua Li prealloc_page = NULL; 1461ff36b801SShaohua Li SetPageSwapBacked(filepage); 1462ff36b801SShaohua Li } 1463ff36b801SShaohua Li 14641da177e4SLinus Torvalds entry = shmem_swp_alloc(info, idx, sgp); 14651da177e4SLinus Torvalds if (IS_ERR(entry)) 14661da177e4SLinus Torvalds error = PTR_ERR(entry); 14671da177e4SLinus Torvalds else { 14681da177e4SLinus Torvalds swap = *entry; 14691da177e4SLinus Torvalds shmem_swp_unmap(entry); 14701da177e4SLinus Torvalds } 147169029cd5SKAMEZAWA Hiroyuki ret = error || swap.val; 147269029cd5SKAMEZAWA Hiroyuki if (ret) 147369029cd5SKAMEZAWA Hiroyuki mem_cgroup_uncharge_cache_page(filepage); 147469029cd5SKAMEZAWA Hiroyuki else 147569029cd5SKAMEZAWA Hiroyuki ret = add_to_page_cache_lru(filepage, mapping, 147669029cd5SKAMEZAWA Hiroyuki idx, GFP_NOWAIT); 147769029cd5SKAMEZAWA Hiroyuki /* 147869029cd5SKAMEZAWA Hiroyuki * At add_to_page_cache_lru() failure, uncharge will 147969029cd5SKAMEZAWA Hiroyuki * be done automatically. 148069029cd5SKAMEZAWA Hiroyuki */ 148169029cd5SKAMEZAWA Hiroyuki if (ret) { 14821da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, 1); 14831da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 1484d515afe8SHugh Dickins spin_unlock(&info->lock); 1485d515afe8SHugh Dickins page_cache_release(filepage); 14861da177e4SLinus Torvalds filepage = NULL; 14871da177e4SLinus Torvalds if (error) 14881da177e4SLinus Torvalds goto failed; 14891da177e4SLinus Torvalds goto repeat; 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds info->flags |= SHMEM_PAGEIN; 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds info->alloced++; 14951da177e4SLinus Torvalds spin_unlock(&info->lock); 1496e84e2e13SHugh Dickins clear_highpage(filepage); 14971da177e4SLinus Torvalds flush_dcache_page(filepage); 14981da177e4SLinus Torvalds SetPageUptodate(filepage); 1499a0ee5ec5SHugh Dickins if (sgp == SGP_DIRTY) 1500a0ee5ec5SHugh Dickins set_page_dirty(filepage); 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds done: 15031da177e4SLinus Torvalds *pagep = filepage; 1504ff36b801SShaohua Li error = 0; 1505ff36b801SShaohua Li goto out; 15061da177e4SLinus Torvalds 150759a16eadSHugh Dickins nospace: 150859a16eadSHugh Dickins /* 150959a16eadSHugh Dickins * Perhaps the page was brought in from swap between find_lock_page 151059a16eadSHugh Dickins * and taking info->lock? We allow for that at add_to_page_cache_lru, 151159a16eadSHugh Dickins * but must also avoid reporting a spurious ENOSPC while working on a 151259a16eadSHugh Dickins * full tmpfs. (When filepage has been passed in to shmem_getpage, it 151359a16eadSHugh Dickins * is already in page cache, which prevents this race from occurring.) 151459a16eadSHugh Dickins */ 151559a16eadSHugh Dickins if (!filepage) { 151659a16eadSHugh Dickins struct page *page = find_get_page(mapping, idx); 151759a16eadSHugh Dickins if (page) { 151859a16eadSHugh Dickins spin_unlock(&info->lock); 151959a16eadSHugh Dickins page_cache_release(page); 152059a16eadSHugh Dickins goto repeat; 152159a16eadSHugh Dickins } 152259a16eadSHugh Dickins } 152359a16eadSHugh Dickins spin_unlock(&info->lock); 152459a16eadSHugh Dickins error = -ENOSPC; 15251da177e4SLinus Torvalds failed: 15261da177e4SLinus Torvalds if (*pagep != filepage) { 15271da177e4SLinus Torvalds unlock_page(filepage); 15281da177e4SLinus Torvalds page_cache_release(filepage); 15291da177e4SLinus Torvalds } 1530ff36b801SShaohua Li out: 1531ff36b801SShaohua Li if (prealloc_page) { 1532ff36b801SShaohua Li mem_cgroup_uncharge_cache_page(prealloc_page); 1533ff36b801SShaohua Li page_cache_release(prealloc_page); 1534ff36b801SShaohua Li } 15351da177e4SLinus Torvalds return error; 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds 1538d0217ac0SNick Piggin static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 15391da177e4SLinus Torvalds { 1540d3ac7f89SJosef "Jeff" Sipek struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 15411da177e4SLinus Torvalds int error; 1542d0217ac0SNick Piggin int ret; 15431da177e4SLinus Torvalds 1544d0217ac0SNick Piggin if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 1545d0217ac0SNick Piggin return VM_FAULT_SIGBUS; 1546d00806b1SNick Piggin 154727d54b39SHugh Dickins error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); 1548d0217ac0SNick Piggin if (error) 1549d0217ac0SNick Piggin return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 1550456f998eSYing Han if (ret & VM_FAULT_MAJOR) { 1551456f998eSYing Han count_vm_event(PGMAJFAULT); 1552456f998eSYing Han mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 1553456f998eSYing Han } 155483c54070SNick Piggin return ret | VM_FAULT_LOCKED; 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1558d8dc74f2SAdrian Bunk static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new) 15591da177e4SLinus Torvalds { 1560d3ac7f89SJosef "Jeff" Sipek struct inode *i = vma->vm_file->f_path.dentry->d_inode; 15611da177e4SLinus Torvalds return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new); 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 1564d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 1565d8dc74f2SAdrian Bunk unsigned long addr) 15661da177e4SLinus Torvalds { 1567d3ac7f89SJosef "Jeff" Sipek struct inode *i = vma->vm_file->f_path.dentry->d_inode; 15681da177e4SLinus Torvalds unsigned long idx; 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 15711da177e4SLinus Torvalds return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx); 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds #endif 15741da177e4SLinus Torvalds 15751da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 15761da177e4SLinus Torvalds { 1577d3ac7f89SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 15781da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 15791da177e4SLinus Torvalds int retval = -ENOMEM; 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds spin_lock(&info->lock); 15821da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 15831da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 15841da177e4SLinus Torvalds goto out_nomem; 15851da177e4SLinus Torvalds info->flags |= VM_LOCKED; 158689e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 15891da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 15901da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 159189e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 159289e004eaSLee Schermerhorn scan_mapping_unevictable_pages(file->f_mapping); 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds retval = 0; 159589e004eaSLee Schermerhorn 15961da177e4SLinus Torvalds out_nomem: 15971da177e4SLinus Torvalds spin_unlock(&info->lock); 15981da177e4SLinus Torvalds return retval; 15991da177e4SLinus Torvalds } 16001da177e4SLinus Torvalds 16019b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 16021da177e4SLinus Torvalds { 16031da177e4SLinus Torvalds file_accessed(file); 16041da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 1605d0217ac0SNick Piggin vma->vm_flags |= VM_CAN_NONLINEAR; 16061da177e4SLinus Torvalds return 0; 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds 1609454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 1610454abafeSDmitry Monakhov int mode, dev_t dev, unsigned long flags) 16111da177e4SLinus Torvalds { 16121da177e4SLinus Torvalds struct inode *inode; 16131da177e4SLinus Torvalds struct shmem_inode_info *info; 16141da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 16151da177e4SLinus Torvalds 16165b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 16171da177e4SLinus Torvalds return NULL; 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds inode = new_inode(sb); 16201da177e4SLinus Torvalds if (inode) { 162185fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 1622454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 16231da177e4SLinus Torvalds inode->i_blocks = 0; 16241da177e4SLinus Torvalds inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; 16251da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 162691828a40SDavid M. Grimes inode->i_generation = get_seconds(); 16271da177e4SLinus Torvalds info = SHMEM_I(inode); 16281da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 16291da177e4SLinus Torvalds spin_lock_init(&info->lock); 16300b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 16311da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 1632b09e0fa4SEric Paris INIT_LIST_HEAD(&info->xattr_list); 163372c04902SAl Viro cache_no_acl(inode); 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds switch (mode & S_IFMT) { 16361da177e4SLinus Torvalds default: 163739f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 16381da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 16391da177e4SLinus Torvalds break; 16401da177e4SLinus Torvalds case S_IFREG: 164114fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 16421da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 16431da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 164471fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 164571fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 16461da177e4SLinus Torvalds break; 16471da177e4SLinus Torvalds case S_IFDIR: 1648d8c76e6fSDave Hansen inc_nlink(inode); 16491da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 16501da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 16511da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 16521da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 16531da177e4SLinus Torvalds break; 16541da177e4SLinus Torvalds case S_IFLNK: 16551da177e4SLinus Torvalds /* 16561da177e4SLinus Torvalds * Must not load anything in the rbtree, 16571da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 16581da177e4SLinus Torvalds */ 165971fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 16601da177e4SLinus Torvalds break; 16611da177e4SLinus Torvalds } 16625b04c689SPavel Emelyanov } else 16635b04c689SPavel Emelyanov shmem_free_inode(sb); 16641da177e4SLinus Torvalds return inode; 16651da177e4SLinus Torvalds } 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 166892e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 166992e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations; 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds /* 1672800d15a5SNick Piggin * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin; 1673ae976416SHugh Dickins * but providing them allows a tmpfs file to be used for splice, sendfile, and 1674ae976416SHugh Dickins * below the loop driver, in the generic fashion that many filesystems support. 16751da177e4SLinus Torvalds */ 1676ae976416SHugh Dickins static int shmem_readpage(struct file *file, struct page *page) 1677ae976416SHugh Dickins { 1678ae976416SHugh Dickins struct inode *inode = page->mapping->host; 1679ae976416SHugh Dickins int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL); 1680ae976416SHugh Dickins unlock_page(page); 1681ae976416SHugh Dickins return error; 1682ae976416SHugh Dickins } 1683ae976416SHugh Dickins 16841da177e4SLinus Torvalds static int 1685800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 1686800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 1687800d15a5SNick Piggin struct page **pagep, void **fsdata) 16881da177e4SLinus Torvalds { 1689800d15a5SNick Piggin struct inode *inode = mapping->host; 1690800d15a5SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 1691800d15a5SNick Piggin *pagep = NULL; 1692800d15a5SNick Piggin return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); 1693800d15a5SNick Piggin } 1694800d15a5SNick Piggin 1695800d15a5SNick Piggin static int 1696800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 1697800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 1698800d15a5SNick Piggin struct page *page, void *fsdata) 1699800d15a5SNick Piggin { 1700800d15a5SNick Piggin struct inode *inode = mapping->host; 1701800d15a5SNick Piggin 1702800d15a5SNick Piggin if (pos + copied > inode->i_size) 1703800d15a5SNick Piggin i_size_write(inode, pos + copied); 1704800d15a5SNick Piggin 1705d3602444SHugh Dickins set_page_dirty(page); 17066746aff7SWu Fengguang unlock_page(page); 1707d3602444SHugh Dickins page_cache_release(page); 1708d3602444SHugh Dickins 1709800d15a5SNick Piggin return copied; 17101da177e4SLinus Torvalds } 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) 17131da177e4SLinus Torvalds { 1714d3ac7f89SJosef "Jeff" Sipek struct inode *inode = filp->f_path.dentry->d_inode; 17151da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 17161da177e4SLinus Torvalds unsigned long index, offset; 1717a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 1718a0ee5ec5SHugh Dickins 1719a0ee5ec5SHugh Dickins /* 1720a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 1721a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 1722a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 1723a0ee5ec5SHugh Dickins */ 1724a0ee5ec5SHugh Dickins if (segment_eq(get_fs(), KERNEL_DS)) 1725a0ee5ec5SHugh Dickins sgp = SGP_DIRTY; 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds index = *ppos >> PAGE_CACHE_SHIFT; 17281da177e4SLinus Torvalds offset = *ppos & ~PAGE_CACHE_MASK; 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds for (;;) { 17311da177e4SLinus Torvalds struct page *page = NULL; 17321da177e4SLinus Torvalds unsigned long end_index, nr, ret; 17331da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 17361da177e4SLinus Torvalds if (index > end_index) 17371da177e4SLinus Torvalds break; 17381da177e4SLinus Torvalds if (index == end_index) { 17391da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 17401da177e4SLinus Torvalds if (nr <= offset) 17411da177e4SLinus Torvalds break; 17421da177e4SLinus Torvalds } 17431da177e4SLinus Torvalds 1744a0ee5ec5SHugh Dickins desc->error = shmem_getpage(inode, index, &page, sgp, NULL); 17451da177e4SLinus Torvalds if (desc->error) { 17461da177e4SLinus Torvalds if (desc->error == -EINVAL) 17471da177e4SLinus Torvalds desc->error = 0; 17481da177e4SLinus Torvalds break; 17491da177e4SLinus Torvalds } 1750d3602444SHugh Dickins if (page) 1751d3602444SHugh Dickins unlock_page(page); 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds /* 17541da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 17551b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 17561da177e4SLinus Torvalds */ 17571da177e4SLinus Torvalds nr = PAGE_CACHE_SIZE; 17581da177e4SLinus Torvalds i_size = i_size_read(inode); 17591da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 17601da177e4SLinus Torvalds if (index == end_index) { 17611da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 17621da177e4SLinus Torvalds if (nr <= offset) { 17631da177e4SLinus Torvalds if (page) 17641da177e4SLinus Torvalds page_cache_release(page); 17651da177e4SLinus Torvalds break; 17661da177e4SLinus Torvalds } 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds nr -= offset; 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds if (page) { 17711da177e4SLinus Torvalds /* 17721da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 17731da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 17741da177e4SLinus Torvalds * before reading the page on the kernel side. 17751da177e4SLinus Torvalds */ 17761da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 17771da177e4SLinus Torvalds flush_dcache_page(page); 17781da177e4SLinus Torvalds /* 17791da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 17801da177e4SLinus Torvalds */ 17811da177e4SLinus Torvalds if (!offset) 17821da177e4SLinus Torvalds mark_page_accessed(page); 1783b5810039SNick Piggin } else { 17841da177e4SLinus Torvalds page = ZERO_PAGE(0); 1785b5810039SNick Piggin page_cache_get(page); 1786b5810039SNick Piggin } 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds /* 17891da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 17901da177e4SLinus Torvalds * now we can copy it to user space... 17911da177e4SLinus Torvalds * 17921da177e4SLinus Torvalds * The actor routine returns how many bytes were actually used.. 17931da177e4SLinus Torvalds * NOTE! This may not be the same as how much of a user buffer 17941da177e4SLinus Torvalds * we filled up (we may be padding etc), so we can only update 17951da177e4SLinus Torvalds * "pos" here (the actor routine has to update the user buffer 17961da177e4SLinus Torvalds * pointers and the remaining count). 17971da177e4SLinus Torvalds */ 17981da177e4SLinus Torvalds ret = actor(desc, page, offset, nr); 17991da177e4SLinus Torvalds offset += ret; 18001da177e4SLinus Torvalds index += offset >> PAGE_CACHE_SHIFT; 18011da177e4SLinus Torvalds offset &= ~PAGE_CACHE_MASK; 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds page_cache_release(page); 18041da177e4SLinus Torvalds if (ret != nr || !desc->count) 18051da177e4SLinus Torvalds break; 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds cond_resched(); 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; 18111da177e4SLinus Torvalds file_accessed(filp); 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds 1814bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb, 1815bcd78e49SHugh Dickins const struct iovec *iov, unsigned long nr_segs, loff_t pos) 18161da177e4SLinus Torvalds { 1817bcd78e49SHugh Dickins struct file *filp = iocb->ki_filp; 1818bcd78e49SHugh Dickins ssize_t retval; 1819bcd78e49SHugh Dickins unsigned long seg; 1820bcd78e49SHugh Dickins size_t count; 1821bcd78e49SHugh Dickins loff_t *ppos = &iocb->ki_pos; 1822bcd78e49SHugh Dickins 1823bcd78e49SHugh Dickins retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); 1824bcd78e49SHugh Dickins if (retval) 1825bcd78e49SHugh Dickins return retval; 1826bcd78e49SHugh Dickins 1827bcd78e49SHugh Dickins for (seg = 0; seg < nr_segs; seg++) { 18281da177e4SLinus Torvalds read_descriptor_t desc; 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds desc.written = 0; 1831bcd78e49SHugh Dickins desc.arg.buf = iov[seg].iov_base; 1832bcd78e49SHugh Dickins desc.count = iov[seg].iov_len; 1833bcd78e49SHugh Dickins if (desc.count == 0) 1834bcd78e49SHugh Dickins continue; 18351da177e4SLinus Torvalds desc.error = 0; 18361da177e4SLinus Torvalds do_shmem_file_read(filp, ppos, &desc, file_read_actor); 1837bcd78e49SHugh Dickins retval += desc.written; 1838bcd78e49SHugh Dickins if (desc.error) { 1839bcd78e49SHugh Dickins retval = retval ?: desc.error; 1840bcd78e49SHugh Dickins break; 1841bcd78e49SHugh Dickins } 1842bcd78e49SHugh Dickins if (desc.count > 0) 1843bcd78e49SHugh Dickins break; 1844bcd78e49SHugh Dickins } 1845bcd78e49SHugh Dickins return retval; 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds 1848708e3508SHugh Dickins static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 1849708e3508SHugh Dickins struct pipe_inode_info *pipe, size_t len, 1850708e3508SHugh Dickins unsigned int flags) 1851708e3508SHugh Dickins { 1852708e3508SHugh Dickins struct address_space *mapping = in->f_mapping; 1853*71f0e07aSHugh Dickins struct inode *inode = mapping->host; 1854708e3508SHugh Dickins unsigned int loff, nr_pages, req_pages; 1855708e3508SHugh Dickins struct page *pages[PIPE_DEF_BUFFERS]; 1856708e3508SHugh Dickins struct partial_page partial[PIPE_DEF_BUFFERS]; 1857708e3508SHugh Dickins struct page *page; 1858708e3508SHugh Dickins pgoff_t index, end_index; 1859708e3508SHugh Dickins loff_t isize, left; 1860708e3508SHugh Dickins int error, page_nr; 1861708e3508SHugh Dickins struct splice_pipe_desc spd = { 1862708e3508SHugh Dickins .pages = pages, 1863708e3508SHugh Dickins .partial = partial, 1864708e3508SHugh Dickins .flags = flags, 1865708e3508SHugh Dickins .ops = &page_cache_pipe_buf_ops, 1866708e3508SHugh Dickins .spd_release = spd_release_page, 1867708e3508SHugh Dickins }; 1868708e3508SHugh Dickins 1869*71f0e07aSHugh Dickins isize = i_size_read(inode); 1870708e3508SHugh Dickins if (unlikely(*ppos >= isize)) 1871708e3508SHugh Dickins return 0; 1872708e3508SHugh Dickins 1873708e3508SHugh Dickins left = isize - *ppos; 1874708e3508SHugh Dickins if (unlikely(left < len)) 1875708e3508SHugh Dickins len = left; 1876708e3508SHugh Dickins 1877708e3508SHugh Dickins if (splice_grow_spd(pipe, &spd)) 1878708e3508SHugh Dickins return -ENOMEM; 1879708e3508SHugh Dickins 1880708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1881708e3508SHugh Dickins loff = *ppos & ~PAGE_CACHE_MASK; 1882708e3508SHugh Dickins req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1883708e3508SHugh Dickins nr_pages = min(req_pages, pipe->buffers); 1884708e3508SHugh Dickins 1885708e3508SHugh Dickins spd.nr_pages = find_get_pages_contig(mapping, index, 1886708e3508SHugh Dickins nr_pages, spd.pages); 1887708e3508SHugh Dickins index += spd.nr_pages; 1888708e3508SHugh Dickins error = 0; 1889*71f0e07aSHugh Dickins 1890708e3508SHugh Dickins while (spd.nr_pages < nr_pages) { 1891*71f0e07aSHugh Dickins page = NULL; 1892*71f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); 1893*71f0e07aSHugh Dickins if (error) 1894708e3508SHugh Dickins break; 1895708e3508SHugh Dickins unlock_page(page); 1896708e3508SHugh Dickins spd.pages[spd.nr_pages++] = page; 1897708e3508SHugh Dickins index++; 1898708e3508SHugh Dickins } 1899708e3508SHugh Dickins 1900708e3508SHugh Dickins index = *ppos >> PAGE_CACHE_SHIFT; 1901708e3508SHugh Dickins nr_pages = spd.nr_pages; 1902708e3508SHugh Dickins spd.nr_pages = 0; 1903*71f0e07aSHugh Dickins 1904708e3508SHugh Dickins for (page_nr = 0; page_nr < nr_pages; page_nr++) { 1905708e3508SHugh Dickins unsigned int this_len; 1906708e3508SHugh Dickins 1907708e3508SHugh Dickins if (!len) 1908708e3508SHugh Dickins break; 1909708e3508SHugh Dickins 1910708e3508SHugh Dickins this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); 1911708e3508SHugh Dickins page = spd.pages[page_nr]; 1912708e3508SHugh Dickins 1913*71f0e07aSHugh Dickins if (!PageUptodate(page) || page->mapping != mapping) { 1914*71f0e07aSHugh Dickins page = NULL; 1915*71f0e07aSHugh Dickins error = shmem_getpage(inode, index, &page, 1916*71f0e07aSHugh Dickins SGP_CACHE, NULL); 1917*71f0e07aSHugh Dickins if (error) 1918708e3508SHugh Dickins break; 1919*71f0e07aSHugh Dickins unlock_page(page); 1920708e3508SHugh Dickins page_cache_release(spd.pages[page_nr]); 1921708e3508SHugh Dickins spd.pages[page_nr] = page; 1922708e3508SHugh Dickins } 1923708e3508SHugh Dickins 1924*71f0e07aSHugh Dickins isize = i_size_read(inode); 1925708e3508SHugh Dickins end_index = (isize - 1) >> PAGE_CACHE_SHIFT; 1926708e3508SHugh Dickins if (unlikely(!isize || index > end_index)) 1927708e3508SHugh Dickins break; 1928708e3508SHugh Dickins 1929708e3508SHugh Dickins if (end_index == index) { 1930708e3508SHugh Dickins unsigned int plen; 1931708e3508SHugh Dickins 1932708e3508SHugh Dickins plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; 1933708e3508SHugh Dickins if (plen <= loff) 1934708e3508SHugh Dickins break; 1935708e3508SHugh Dickins 1936708e3508SHugh Dickins this_len = min(this_len, plen - loff); 1937708e3508SHugh Dickins len = this_len; 1938708e3508SHugh Dickins } 1939708e3508SHugh Dickins 1940708e3508SHugh Dickins spd.partial[page_nr].offset = loff; 1941708e3508SHugh Dickins spd.partial[page_nr].len = this_len; 1942708e3508SHugh Dickins len -= this_len; 1943708e3508SHugh Dickins loff = 0; 1944708e3508SHugh Dickins spd.nr_pages++; 1945708e3508SHugh Dickins index++; 1946708e3508SHugh Dickins } 1947708e3508SHugh Dickins 1948708e3508SHugh Dickins while (page_nr < nr_pages) 1949708e3508SHugh Dickins page_cache_release(spd.pages[page_nr++]); 1950708e3508SHugh Dickins 1951708e3508SHugh Dickins if (spd.nr_pages) 1952708e3508SHugh Dickins error = splice_to_pipe(pipe, &spd); 1953708e3508SHugh Dickins 1954708e3508SHugh Dickins splice_shrink_spd(pipe, &spd); 1955708e3508SHugh Dickins 1956708e3508SHugh Dickins if (error > 0) { 1957708e3508SHugh Dickins *ppos += error; 1958708e3508SHugh Dickins file_accessed(in); 1959708e3508SHugh Dickins } 1960708e3508SHugh Dickins return error; 1961708e3508SHugh Dickins } 1962708e3508SHugh Dickins 1963726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 19641da177e4SLinus Torvalds { 1965726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 19681da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 19691da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 19700edd73b3SHugh Dickins if (sbinfo->max_blocks) { 19711da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 19727e496299STim Chen buf->f_bavail = buf->f_bfree = 19737e496299STim Chen sbinfo->max_blocks - percpu_counter_sum(&sbinfo->used_blocks); 19740edd73b3SHugh Dickins } 19750edd73b3SHugh Dickins if (sbinfo->max_inodes) { 19761da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 19771da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 19801da177e4SLinus Torvalds return 0; 19811da177e4SLinus Torvalds } 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds /* 19841da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 19851da177e4SLinus Torvalds */ 19861da177e4SLinus Torvalds static int 19871da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 19881da177e4SLinus Torvalds { 19890b0a0806SHugh Dickins struct inode *inode; 19901da177e4SLinus Torvalds int error = -ENOSPC; 19911da177e4SLinus Torvalds 1992454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 19931da177e4SLinus Torvalds if (inode) { 19942a7dba39SEric Paris error = security_inode_init_security(inode, dir, 19952a7dba39SEric Paris &dentry->d_name, NULL, 19962a7dba39SEric Paris NULL, NULL); 1997570bc1c2SStephen Smalley if (error) { 1998570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1999570bc1c2SStephen Smalley iput(inode); 2000570bc1c2SStephen Smalley return error; 2001570bc1c2SStephen Smalley } 200239f0247dSAndreas Gruenbacher } 20031c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL 20041c7c474cSChristoph Hellwig error = generic_acl_init(inode, dir); 200539f0247dSAndreas Gruenbacher if (error) { 200639f0247dSAndreas Gruenbacher iput(inode); 200739f0247dSAndreas Gruenbacher return error; 2008570bc1c2SStephen Smalley } 2009718deb6bSAl Viro #else 2010718deb6bSAl Viro error = 0; 20111c7c474cSChristoph Hellwig #endif 20121da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 20131da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 20141da177e4SLinus Torvalds d_instantiate(dentry, inode); 20151da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds return error; 20181da177e4SLinus Torvalds } 20191da177e4SLinus Torvalds 20201da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode) 20211da177e4SLinus Torvalds { 20221da177e4SLinus Torvalds int error; 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 20251da177e4SLinus Torvalds return error; 2026d8c76e6fSDave Hansen inc_nlink(dir); 20271da177e4SLinus Torvalds return 0; 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode, 20311da177e4SLinus Torvalds struct nameidata *nd) 20321da177e4SLinus Torvalds { 20331da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds /* 20371da177e4SLinus Torvalds * Link a file.. 20381da177e4SLinus Torvalds */ 20391da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 20401da177e4SLinus Torvalds { 20411da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 20425b04c689SPavel Emelyanov int ret; 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds /* 20451da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 20461da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 20471da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 20481da177e4SLinus Torvalds */ 20495b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 20505b04c689SPavel Emelyanov if (ret) 20515b04c689SPavel Emelyanov goto out; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 20541da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 2055d8c76e6fSDave Hansen inc_nlink(inode); 20567de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 20571da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 20581da177e4SLinus Torvalds d_instantiate(dentry, inode); 20595b04c689SPavel Emelyanov out: 20605b04c689SPavel Emelyanov return ret; 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 20641da177e4SLinus Torvalds { 20651da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20661da177e4SLinus Torvalds 20675b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 20685b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 20711da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 20729a53c3a7SDave Hansen drop_nlink(inode); 20731da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 20741da177e4SLinus Torvalds return 0; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 20781da177e4SLinus Torvalds { 20791da177e4SLinus Torvalds if (!simple_empty(dentry)) 20801da177e4SLinus Torvalds return -ENOTEMPTY; 20811da177e4SLinus Torvalds 20829a53c3a7SDave Hansen drop_nlink(dentry->d_inode); 20839a53c3a7SDave Hansen drop_nlink(dir); 20841da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds /* 20881da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 20891da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 20901da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 20911da177e4SLinus Torvalds * gets overwritten. 20921da177e4SLinus Torvalds */ 20931da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 20941da177e4SLinus Torvalds { 20951da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 20961da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 20991da177e4SLinus Torvalds return -ENOTEMPTY; 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds if (new_dentry->d_inode) { 21021da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 21031da177e4SLinus Torvalds if (they_are_dirs) 21049a53c3a7SDave Hansen drop_nlink(old_dir); 21051da177e4SLinus Torvalds } else if (they_are_dirs) { 21069a53c3a7SDave Hansen drop_nlink(old_dir); 2107d8c76e6fSDave Hansen inc_nlink(new_dir); 21081da177e4SLinus Torvalds } 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 21111da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 21121da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 21131da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 21141da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 21151da177e4SLinus Torvalds return 0; 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds 21181da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 21191da177e4SLinus Torvalds { 21201da177e4SLinus Torvalds int error; 21211da177e4SLinus Torvalds int len; 21221da177e4SLinus Torvalds struct inode *inode; 21231da177e4SLinus Torvalds struct page *page = NULL; 21241da177e4SLinus Torvalds char *kaddr; 21251da177e4SLinus Torvalds struct shmem_inode_info *info; 21261da177e4SLinus Torvalds 21271da177e4SLinus Torvalds len = strlen(symname) + 1; 21281da177e4SLinus Torvalds if (len > PAGE_CACHE_SIZE) 21291da177e4SLinus Torvalds return -ENAMETOOLONG; 21301da177e4SLinus Torvalds 2131454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 21321da177e4SLinus Torvalds if (!inode) 21331da177e4SLinus Torvalds return -ENOSPC; 21341da177e4SLinus Torvalds 21352a7dba39SEric Paris error = security_inode_init_security(inode, dir, &dentry->d_name, NULL, 21362a7dba39SEric Paris NULL, NULL); 2137570bc1c2SStephen Smalley if (error) { 2138570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 2139570bc1c2SStephen Smalley iput(inode); 2140570bc1c2SStephen Smalley return error; 2141570bc1c2SStephen Smalley } 2142570bc1c2SStephen Smalley error = 0; 2143570bc1c2SStephen Smalley } 2144570bc1c2SStephen Smalley 21451da177e4SLinus Torvalds info = SHMEM_I(inode); 21461da177e4SLinus Torvalds inode->i_size = len-1; 2147b09e0fa4SEric Paris if (len <= SHMEM_SYMLINK_INLINE_LEN) { 21481da177e4SLinus Torvalds /* do it inline */ 2149b09e0fa4SEric Paris memcpy(info->inline_symlink, symname, len); 21501da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inline_operations; 21511da177e4SLinus Torvalds } else { 21521da177e4SLinus Torvalds error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); 21531da177e4SLinus Torvalds if (error) { 21541da177e4SLinus Torvalds iput(inode); 21551da177e4SLinus Torvalds return error; 21561da177e4SLinus Torvalds } 215714fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 21581da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 21591da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 21601da177e4SLinus Torvalds memcpy(kaddr, symname, len); 21611da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 21621da177e4SLinus Torvalds set_page_dirty(page); 21636746aff7SWu Fengguang unlock_page(page); 21641da177e4SLinus Torvalds page_cache_release(page); 21651da177e4SLinus Torvalds } 21661da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 21671da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 21681da177e4SLinus Torvalds d_instantiate(dentry, inode); 21691da177e4SLinus Torvalds dget(dentry); 21701da177e4SLinus Torvalds return 0; 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds 2173cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd) 21741da177e4SLinus Torvalds { 2175b09e0fa4SEric Paris nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink); 2176cc314eefSLinus Torvalds return NULL; 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds 2179cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) 21801da177e4SLinus Torvalds { 21811da177e4SLinus Torvalds struct page *page = NULL; 21821da177e4SLinus Torvalds int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); 21831da177e4SLinus Torvalds nd_set_link(nd, res ? ERR_PTR(res) : kmap(page)); 2184d3602444SHugh Dickins if (page) 2185d3602444SHugh Dickins unlock_page(page); 2186cc314eefSLinus Torvalds return page; 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds 2189cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 21901da177e4SLinus Torvalds { 21911da177e4SLinus Torvalds if (!IS_ERR(nd_get_link(nd))) { 2192cc314eefSLinus Torvalds struct page *page = cookie; 21931da177e4SLinus Torvalds kunmap(page); 21941da177e4SLinus Torvalds mark_page_accessed(page); 21951da177e4SLinus Torvalds page_cache_release(page); 21961da177e4SLinus Torvalds } 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 2199b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2200b09e0fa4SEric Paris /* 2201b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 2202b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 2203b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 2204b09e0fa4SEric Paris * filesystem level, though. 2205b09e0fa4SEric Paris */ 2206b09e0fa4SEric Paris 2207b09e0fa4SEric Paris static int shmem_xattr_get(struct dentry *dentry, const char *name, 2208b09e0fa4SEric Paris void *buffer, size_t size) 2209b09e0fa4SEric Paris { 2210b09e0fa4SEric Paris struct shmem_inode_info *info; 2211b09e0fa4SEric Paris struct shmem_xattr *xattr; 2212b09e0fa4SEric Paris int ret = -ENODATA; 2213b09e0fa4SEric Paris 2214b09e0fa4SEric Paris info = SHMEM_I(dentry->d_inode); 2215b09e0fa4SEric Paris 2216b09e0fa4SEric Paris spin_lock(&info->lock); 2217b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 2218b09e0fa4SEric Paris if (strcmp(name, xattr->name)) 2219b09e0fa4SEric Paris continue; 2220b09e0fa4SEric Paris 2221b09e0fa4SEric Paris ret = xattr->size; 2222b09e0fa4SEric Paris if (buffer) { 2223b09e0fa4SEric Paris if (size < xattr->size) 2224b09e0fa4SEric Paris ret = -ERANGE; 2225b09e0fa4SEric Paris else 2226b09e0fa4SEric Paris memcpy(buffer, xattr->value, xattr->size); 2227b09e0fa4SEric Paris } 2228b09e0fa4SEric Paris break; 2229b09e0fa4SEric Paris } 2230b09e0fa4SEric Paris spin_unlock(&info->lock); 2231b09e0fa4SEric Paris return ret; 2232b09e0fa4SEric Paris } 2233b09e0fa4SEric Paris 2234b09e0fa4SEric Paris static int shmem_xattr_set(struct dentry *dentry, const char *name, 2235b09e0fa4SEric Paris const void *value, size_t size, int flags) 2236b09e0fa4SEric Paris { 2237b09e0fa4SEric Paris struct inode *inode = dentry->d_inode; 2238b09e0fa4SEric Paris struct shmem_inode_info *info = SHMEM_I(inode); 2239b09e0fa4SEric Paris struct shmem_xattr *xattr; 2240b09e0fa4SEric Paris struct shmem_xattr *new_xattr = NULL; 2241b09e0fa4SEric Paris size_t len; 2242b09e0fa4SEric Paris int err = 0; 2243b09e0fa4SEric Paris 2244b09e0fa4SEric Paris /* value == NULL means remove */ 2245b09e0fa4SEric Paris if (value) { 2246b09e0fa4SEric Paris /* wrap around? */ 2247b09e0fa4SEric Paris len = sizeof(*new_xattr) + size; 2248b09e0fa4SEric Paris if (len <= sizeof(*new_xattr)) 2249b09e0fa4SEric Paris return -ENOMEM; 2250b09e0fa4SEric Paris 2251b09e0fa4SEric Paris new_xattr = kmalloc(len, GFP_KERNEL); 2252b09e0fa4SEric Paris if (!new_xattr) 2253b09e0fa4SEric Paris return -ENOMEM; 2254b09e0fa4SEric Paris 2255b09e0fa4SEric Paris new_xattr->name = kstrdup(name, GFP_KERNEL); 2256b09e0fa4SEric Paris if (!new_xattr->name) { 2257b09e0fa4SEric Paris kfree(new_xattr); 2258b09e0fa4SEric Paris return -ENOMEM; 2259b09e0fa4SEric Paris } 2260b09e0fa4SEric Paris 2261b09e0fa4SEric Paris new_xattr->size = size; 2262b09e0fa4SEric Paris memcpy(new_xattr->value, value, size); 2263b09e0fa4SEric Paris } 2264b09e0fa4SEric Paris 2265b09e0fa4SEric Paris spin_lock(&info->lock); 2266b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 2267b09e0fa4SEric Paris if (!strcmp(name, xattr->name)) { 2268b09e0fa4SEric Paris if (flags & XATTR_CREATE) { 2269b09e0fa4SEric Paris xattr = new_xattr; 2270b09e0fa4SEric Paris err = -EEXIST; 2271b09e0fa4SEric Paris } else if (new_xattr) { 2272b09e0fa4SEric Paris list_replace(&xattr->list, &new_xattr->list); 2273b09e0fa4SEric Paris } else { 2274b09e0fa4SEric Paris list_del(&xattr->list); 2275b09e0fa4SEric Paris } 2276b09e0fa4SEric Paris goto out; 2277b09e0fa4SEric Paris } 2278b09e0fa4SEric Paris } 2279b09e0fa4SEric Paris if (flags & XATTR_REPLACE) { 2280b09e0fa4SEric Paris xattr = new_xattr; 2281b09e0fa4SEric Paris err = -ENODATA; 2282b09e0fa4SEric Paris } else { 2283b09e0fa4SEric Paris list_add(&new_xattr->list, &info->xattr_list); 2284b09e0fa4SEric Paris xattr = NULL; 2285b09e0fa4SEric Paris } 2286b09e0fa4SEric Paris out: 2287b09e0fa4SEric Paris spin_unlock(&info->lock); 2288b09e0fa4SEric Paris if (xattr) 2289b09e0fa4SEric Paris kfree(xattr->name); 2290b09e0fa4SEric Paris kfree(xattr); 2291b09e0fa4SEric Paris return err; 2292b09e0fa4SEric Paris } 2293b09e0fa4SEric Paris 2294b09e0fa4SEric Paris 2295b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 2296b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 2297b09e0fa4SEric Paris &generic_acl_access_handler, 2298b09e0fa4SEric Paris &generic_acl_default_handler, 2299b09e0fa4SEric Paris #endif 2300b09e0fa4SEric Paris NULL 2301b09e0fa4SEric Paris }; 2302b09e0fa4SEric Paris 2303b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name) 2304b09e0fa4SEric Paris { 2305b09e0fa4SEric Paris struct { const char *prefix; size_t len; } arr[] = { 2306b09e0fa4SEric Paris { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, 2307b09e0fa4SEric Paris { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } 2308b09e0fa4SEric Paris }; 2309b09e0fa4SEric Paris int i; 2310b09e0fa4SEric Paris 2311b09e0fa4SEric Paris for (i = 0; i < ARRAY_SIZE(arr); i++) { 2312b09e0fa4SEric Paris size_t preflen = arr[i].len; 2313b09e0fa4SEric Paris if (strncmp(name, arr[i].prefix, preflen) == 0) { 2314b09e0fa4SEric Paris if (!name[preflen]) 2315b09e0fa4SEric Paris return -EINVAL; 2316b09e0fa4SEric Paris return 0; 2317b09e0fa4SEric Paris } 2318b09e0fa4SEric Paris } 2319b09e0fa4SEric Paris return -EOPNOTSUPP; 2320b09e0fa4SEric Paris } 2321b09e0fa4SEric Paris 2322b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, 2323b09e0fa4SEric Paris void *buffer, size_t size) 2324b09e0fa4SEric Paris { 2325b09e0fa4SEric Paris int err; 2326b09e0fa4SEric Paris 2327b09e0fa4SEric Paris /* 2328b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2329b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2330b09e0fa4SEric Paris * for it via sb->s_xattr. 2331b09e0fa4SEric Paris */ 2332b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2333b09e0fa4SEric Paris return generic_getxattr(dentry, name, buffer, size); 2334b09e0fa4SEric Paris 2335b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2336b09e0fa4SEric Paris if (err) 2337b09e0fa4SEric Paris return err; 2338b09e0fa4SEric Paris 2339b09e0fa4SEric Paris return shmem_xattr_get(dentry, name, buffer, size); 2340b09e0fa4SEric Paris } 2341b09e0fa4SEric Paris 2342b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name, 2343b09e0fa4SEric Paris const void *value, size_t size, int flags) 2344b09e0fa4SEric Paris { 2345b09e0fa4SEric Paris int err; 2346b09e0fa4SEric Paris 2347b09e0fa4SEric Paris /* 2348b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2349b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2350b09e0fa4SEric Paris * for it via sb->s_xattr. 2351b09e0fa4SEric Paris */ 2352b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2353b09e0fa4SEric Paris return generic_setxattr(dentry, name, value, size, flags); 2354b09e0fa4SEric Paris 2355b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2356b09e0fa4SEric Paris if (err) 2357b09e0fa4SEric Paris return err; 2358b09e0fa4SEric Paris 2359b09e0fa4SEric Paris if (size == 0) 2360b09e0fa4SEric Paris value = ""; /* empty EA, do not remove */ 2361b09e0fa4SEric Paris 2362b09e0fa4SEric Paris return shmem_xattr_set(dentry, name, value, size, flags); 2363b09e0fa4SEric Paris 2364b09e0fa4SEric Paris } 2365b09e0fa4SEric Paris 2366b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name) 2367b09e0fa4SEric Paris { 2368b09e0fa4SEric Paris int err; 2369b09e0fa4SEric Paris 2370b09e0fa4SEric Paris /* 2371b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2372b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2373b09e0fa4SEric Paris * for it via sb->s_xattr. 2374b09e0fa4SEric Paris */ 2375b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2376b09e0fa4SEric Paris return generic_removexattr(dentry, name); 2377b09e0fa4SEric Paris 2378b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2379b09e0fa4SEric Paris if (err) 2380b09e0fa4SEric Paris return err; 2381b09e0fa4SEric Paris 2382b09e0fa4SEric Paris return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE); 2383b09e0fa4SEric Paris } 2384b09e0fa4SEric Paris 2385b09e0fa4SEric Paris static bool xattr_is_trusted(const char *name) 2386b09e0fa4SEric Paris { 2387b09e0fa4SEric Paris return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN); 2388b09e0fa4SEric Paris } 2389b09e0fa4SEric Paris 2390b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 2391b09e0fa4SEric Paris { 2392b09e0fa4SEric Paris bool trusted = capable(CAP_SYS_ADMIN); 2393b09e0fa4SEric Paris struct shmem_xattr *xattr; 2394b09e0fa4SEric Paris struct shmem_inode_info *info; 2395b09e0fa4SEric Paris size_t used = 0; 2396b09e0fa4SEric Paris 2397b09e0fa4SEric Paris info = SHMEM_I(dentry->d_inode); 2398b09e0fa4SEric Paris 2399b09e0fa4SEric Paris spin_lock(&info->lock); 2400b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 2401b09e0fa4SEric Paris size_t len; 2402b09e0fa4SEric Paris 2403b09e0fa4SEric Paris /* skip "trusted." attributes for unprivileged callers */ 2404b09e0fa4SEric Paris if (!trusted && xattr_is_trusted(xattr->name)) 2405b09e0fa4SEric Paris continue; 2406b09e0fa4SEric Paris 2407b09e0fa4SEric Paris len = strlen(xattr->name) + 1; 2408b09e0fa4SEric Paris used += len; 2409b09e0fa4SEric Paris if (buffer) { 2410b09e0fa4SEric Paris if (size < used) { 2411b09e0fa4SEric Paris used = -ERANGE; 2412b09e0fa4SEric Paris break; 2413b09e0fa4SEric Paris } 2414b09e0fa4SEric Paris memcpy(buffer, xattr->name, len); 2415b09e0fa4SEric Paris buffer += len; 2416b09e0fa4SEric Paris } 2417b09e0fa4SEric Paris } 2418b09e0fa4SEric Paris spin_unlock(&info->lock); 2419b09e0fa4SEric Paris 2420b09e0fa4SEric Paris return used; 2421b09e0fa4SEric Paris } 2422b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 2423b09e0fa4SEric Paris 242492e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations = { 24251da177e4SLinus Torvalds .readlink = generic_readlink, 24261da177e4SLinus Torvalds .follow_link = shmem_follow_link_inline, 2427b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2428b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2429b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2430b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2431b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2432b09e0fa4SEric Paris #endif 24331da177e4SLinus Torvalds }; 24341da177e4SLinus Torvalds 243592e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 24361da177e4SLinus Torvalds .readlink = generic_readlink, 24371da177e4SLinus Torvalds .follow_link = shmem_follow_link, 24381da177e4SLinus Torvalds .put_link = shmem_put_link, 2439b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2440b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2441b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2442b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2443b09e0fa4SEric Paris .removexattr = shmem_removexattr, 244439f0247dSAndreas Gruenbacher #endif 2445b09e0fa4SEric Paris }; 244639f0247dSAndreas Gruenbacher 244791828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 244891828a40SDavid M. Grimes { 244991828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 245091828a40SDavid M. Grimes } 245191828a40SDavid M. Grimes 245291828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 245391828a40SDavid M. Grimes { 245491828a40SDavid M. Grimes __u32 *fh = vfh; 245591828a40SDavid M. Grimes __u64 inum = fh[2]; 245691828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 245791828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 245891828a40SDavid M. Grimes } 245991828a40SDavid M. Grimes 2460480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 2461480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 246291828a40SDavid M. Grimes { 246391828a40SDavid M. Grimes struct inode *inode; 2464480b116cSChristoph Hellwig struct dentry *dentry = NULL; 2465480b116cSChristoph Hellwig u64 inum = fid->raw[2]; 2466480b116cSChristoph Hellwig inum = (inum << 32) | fid->raw[1]; 246791828a40SDavid M. Grimes 2468480b116cSChristoph Hellwig if (fh_len < 3) 2469480b116cSChristoph Hellwig return NULL; 2470480b116cSChristoph Hellwig 2471480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 2472480b116cSChristoph Hellwig shmem_match, fid->raw); 247391828a40SDavid M. Grimes if (inode) { 2474480b116cSChristoph Hellwig dentry = d_find_alias(inode); 247591828a40SDavid M. Grimes iput(inode); 247691828a40SDavid M. Grimes } 247791828a40SDavid M. Grimes 2478480b116cSChristoph Hellwig return dentry; 247991828a40SDavid M. Grimes } 248091828a40SDavid M. Grimes 248191828a40SDavid M. Grimes static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len, 248291828a40SDavid M. Grimes int connectable) 248391828a40SDavid M. Grimes { 248491828a40SDavid M. Grimes struct inode *inode = dentry->d_inode; 248591828a40SDavid M. Grimes 24865fe0c237SAneesh Kumar K.V if (*len < 3) { 24875fe0c237SAneesh Kumar K.V *len = 3; 248891828a40SDavid M. Grimes return 255; 24895fe0c237SAneesh Kumar K.V } 249091828a40SDavid M. Grimes 24911d3382cbSAl Viro if (inode_unhashed(inode)) { 249291828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 249391828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 249491828a40SDavid M. Grimes * time, we need a lock to ensure we only try 249591828a40SDavid M. Grimes * to do it once 249691828a40SDavid M. Grimes */ 249791828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 249891828a40SDavid M. Grimes spin_lock(&lock); 24991d3382cbSAl Viro if (inode_unhashed(inode)) 250091828a40SDavid M. Grimes __insert_inode_hash(inode, 250191828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 250291828a40SDavid M. Grimes spin_unlock(&lock); 250391828a40SDavid M. Grimes } 250491828a40SDavid M. Grimes 250591828a40SDavid M. Grimes fh[0] = inode->i_generation; 250691828a40SDavid M. Grimes fh[1] = inode->i_ino; 250791828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 250891828a40SDavid M. Grimes 250991828a40SDavid M. Grimes *len = 3; 251091828a40SDavid M. Grimes return 1; 251191828a40SDavid M. Grimes } 251291828a40SDavid M. Grimes 251339655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 251491828a40SDavid M. Grimes .get_parent = shmem_get_parent, 251591828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 2516480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 251791828a40SDavid M. Grimes }; 251891828a40SDavid M. Grimes 2519680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 2520680d794bSakpm@linux-foundation.org bool remount) 25211da177e4SLinus Torvalds { 25221da177e4SLinus Torvalds char *this_char, *value, *rest; 25231da177e4SLinus Torvalds 2524b00dc3adSHugh Dickins while (options != NULL) { 2525b00dc3adSHugh Dickins this_char = options; 2526b00dc3adSHugh Dickins for (;;) { 2527b00dc3adSHugh Dickins /* 2528b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 2529b00dc3adSHugh Dickins * mount options form a comma-separated list, 2530b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 2531b00dc3adSHugh Dickins */ 2532b00dc3adSHugh Dickins options = strchr(options, ','); 2533b00dc3adSHugh Dickins if (options == NULL) 2534b00dc3adSHugh Dickins break; 2535b00dc3adSHugh Dickins options++; 2536b00dc3adSHugh Dickins if (!isdigit(*options)) { 2537b00dc3adSHugh Dickins options[-1] = '\0'; 2538b00dc3adSHugh Dickins break; 2539b00dc3adSHugh Dickins } 2540b00dc3adSHugh Dickins } 25411da177e4SLinus Torvalds if (!*this_char) 25421da177e4SLinus Torvalds continue; 25431da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 25441da177e4SLinus Torvalds *value++ = 0; 25451da177e4SLinus Torvalds } else { 25461da177e4SLinus Torvalds printk(KERN_ERR 25471da177e4SLinus Torvalds "tmpfs: No value for mount option '%s'\n", 25481da177e4SLinus Torvalds this_char); 25491da177e4SLinus Torvalds return 1; 25501da177e4SLinus Torvalds } 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 25531da177e4SLinus Torvalds unsigned long long size; 25541da177e4SLinus Torvalds size = memparse(value,&rest); 25551da177e4SLinus Torvalds if (*rest == '%') { 25561da177e4SLinus Torvalds size <<= PAGE_SHIFT; 25571da177e4SLinus Torvalds size *= totalram_pages; 25581da177e4SLinus Torvalds do_div(size, 100); 25591da177e4SLinus Torvalds rest++; 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds if (*rest) 25621da177e4SLinus Torvalds goto bad_val; 2563680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 2564680d794bSakpm@linux-foundation.org DIV_ROUND_UP(size, PAGE_CACHE_SIZE); 25651da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 2566680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 25671da177e4SLinus Torvalds if (*rest) 25681da177e4SLinus Torvalds goto bad_val; 25691da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 2570680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 25711da177e4SLinus Torvalds if (*rest) 25721da177e4SLinus Torvalds goto bad_val; 25731da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 2574680d794bSakpm@linux-foundation.org if (remount) 25751da177e4SLinus Torvalds continue; 2576680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 25771da177e4SLinus Torvalds if (*rest) 25781da177e4SLinus Torvalds goto bad_val; 25791da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 2580680d794bSakpm@linux-foundation.org if (remount) 25811da177e4SLinus Torvalds continue; 2582680d794bSakpm@linux-foundation.org sbinfo->uid = simple_strtoul(value, &rest, 0); 25831da177e4SLinus Torvalds if (*rest) 25841da177e4SLinus Torvalds goto bad_val; 25851da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 2586680d794bSakpm@linux-foundation.org if (remount) 25871da177e4SLinus Torvalds continue; 2588680d794bSakpm@linux-foundation.org sbinfo->gid = simple_strtoul(value, &rest, 0); 25891da177e4SLinus Torvalds if (*rest) 25901da177e4SLinus Torvalds goto bad_val; 25917339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 259271fe804bSLee Schermerhorn if (mpol_parse_str(value, &sbinfo->mpol, 1)) 25937339ff83SRobin Holt goto bad_val; 25941da177e4SLinus Torvalds } else { 25951da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad mount option %s\n", 25961da177e4SLinus Torvalds this_char); 25971da177e4SLinus Torvalds return 1; 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds } 26001da177e4SLinus Torvalds return 0; 26011da177e4SLinus Torvalds 26021da177e4SLinus Torvalds bad_val: 26031da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", 26041da177e4SLinus Torvalds value, this_char); 26051da177e4SLinus Torvalds return 1; 26061da177e4SLinus Torvalds 26071da177e4SLinus Torvalds } 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 26101da177e4SLinus Torvalds { 26111da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2612680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 26130edd73b3SHugh Dickins unsigned long inodes; 26140edd73b3SHugh Dickins int error = -EINVAL; 26151da177e4SLinus Torvalds 2616680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 26170edd73b3SHugh Dickins return error; 26180edd73b3SHugh Dickins 26190edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 26200edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 26217e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 26220edd73b3SHugh Dickins goto out; 2623680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 26240edd73b3SHugh Dickins goto out; 26250edd73b3SHugh Dickins /* 26260edd73b3SHugh Dickins * Those tests also disallow limited->unlimited while any are in 26270edd73b3SHugh Dickins * use, so i_blocks will always be zero when max_blocks is zero; 26280edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 26290edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 26300edd73b3SHugh Dickins */ 2631680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 26320edd73b3SHugh Dickins goto out; 2633680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 26340edd73b3SHugh Dickins goto out; 26350edd73b3SHugh Dickins 26360edd73b3SHugh Dickins error = 0; 2637680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 2638680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 2639680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 264071fe804bSLee Schermerhorn 264171fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 264271fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 26430edd73b3SHugh Dickins out: 26440edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 26450edd73b3SHugh Dickins return error; 26461da177e4SLinus Torvalds } 2647680d794bSakpm@linux-foundation.org 2648680d794bSakpm@linux-foundation.org static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs) 2649680d794bSakpm@linux-foundation.org { 2650680d794bSakpm@linux-foundation.org struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb); 2651680d794bSakpm@linux-foundation.org 2652680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 2653680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 2654680d794bSakpm@linux-foundation.org sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); 2655680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 2656680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 2657680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 2658680d794bSakpm@linux-foundation.org seq_printf(seq, ",mode=%03o", sbinfo->mode); 2659680d794bSakpm@linux-foundation.org if (sbinfo->uid != 0) 2660680d794bSakpm@linux-foundation.org seq_printf(seq, ",uid=%u", sbinfo->uid); 2661680d794bSakpm@linux-foundation.org if (sbinfo->gid != 0) 2662680d794bSakpm@linux-foundation.org seq_printf(seq, ",gid=%u", sbinfo->gid); 266371fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 2664680d794bSakpm@linux-foundation.org return 0; 2665680d794bSakpm@linux-foundation.org } 2666680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 26671da177e4SLinus Torvalds 26681da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 26691da177e4SLinus Torvalds { 2670602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2671602586a8SHugh Dickins 2672602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 2673602586a8SHugh Dickins kfree(sbinfo); 26741da177e4SLinus Torvalds sb->s_fs_info = NULL; 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 26772b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 26781da177e4SLinus Torvalds { 26791da177e4SLinus Torvalds struct inode *inode; 26801da177e4SLinus Torvalds struct dentry *root; 26810edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 2682680d794bSakpm@linux-foundation.org int err = -ENOMEM; 2683680d794bSakpm@linux-foundation.org 2684680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 2685425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 2686680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 2687680d794bSakpm@linux-foundation.org if (!sbinfo) 2688680d794bSakpm@linux-foundation.org return -ENOMEM; 2689680d794bSakpm@linux-foundation.org 2690680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 269176aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 269276aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 2693680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 26941da177e4SLinus Torvalds 26950edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 26961da177e4SLinus Torvalds /* 26971da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 26981da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 26991da177e4SLinus Torvalds * but the internal instance is left unlimited. 27001da177e4SLinus Torvalds */ 27011da177e4SLinus Torvalds if (!(sb->s_flags & MS_NOUSER)) { 2702680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 2703680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 2704680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 2705680d794bSakpm@linux-foundation.org err = -EINVAL; 2706680d794bSakpm@linux-foundation.org goto failed; 2707680d794bSakpm@linux-foundation.org } 27081da177e4SLinus Torvalds } 270991828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 27100edd73b3SHugh Dickins #else 27110edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 27120edd73b3SHugh Dickins #endif 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 2715602586a8SHugh Dickins if (percpu_counter_init(&sbinfo->used_blocks, 0)) 2716602586a8SHugh Dickins goto failed; 2717680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds sb->s_maxbytes = SHMEM_MAX_BYTES; 27201da177e4SLinus Torvalds sb->s_blocksize = PAGE_CACHE_SIZE; 27211da177e4SLinus Torvalds sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 27221da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 27231da177e4SLinus Torvalds sb->s_op = &shmem_ops; 2724cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 2725b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 272639f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 2727b09e0fa4SEric Paris #endif 2728b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 272939f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 273039f0247dSAndreas Gruenbacher #endif 27310edd73b3SHugh Dickins 2732454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 27331da177e4SLinus Torvalds if (!inode) 27341da177e4SLinus Torvalds goto failed; 2735680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 2736680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 27371da177e4SLinus Torvalds root = d_alloc_root(inode); 27381da177e4SLinus Torvalds if (!root) 27391da177e4SLinus Torvalds goto failed_iput; 27401da177e4SLinus Torvalds sb->s_root = root; 27411da177e4SLinus Torvalds return 0; 27421da177e4SLinus Torvalds 27431da177e4SLinus Torvalds failed_iput: 27441da177e4SLinus Torvalds iput(inode); 27451da177e4SLinus Torvalds failed: 27461da177e4SLinus Torvalds shmem_put_super(sb); 27471da177e4SLinus Torvalds return err; 27481da177e4SLinus Torvalds } 27491da177e4SLinus Torvalds 2750fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 27511da177e4SLinus Torvalds 27521da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 27531da177e4SLinus Torvalds { 27541da177e4SLinus Torvalds struct shmem_inode_info *p; 2755e94b1766SChristoph Lameter p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 27561da177e4SLinus Torvalds if (!p) 27571da177e4SLinus Torvalds return NULL; 27581da177e4SLinus Torvalds return &p->vfs_inode; 27591da177e4SLinus Torvalds } 27601da177e4SLinus Torvalds 2761fa0d7e3dSNick Piggin static void shmem_i_callback(struct rcu_head *head) 2762fa0d7e3dSNick Piggin { 2763fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 2764fa0d7e3dSNick Piggin INIT_LIST_HEAD(&inode->i_dentry); 2765fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 2766fa0d7e3dSNick Piggin } 2767fa0d7e3dSNick Piggin 27681da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 27691da177e4SLinus Torvalds { 27701da177e4SLinus Torvalds if ((inode->i_mode & S_IFMT) == S_IFREG) { 27711da177e4SLinus Torvalds /* only struct inode is valid if it's an inline symlink */ 27721da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 27731da177e4SLinus Torvalds } 2774fa0d7e3dSNick Piggin call_rcu(&inode->i_rcu, shmem_i_callback); 27751da177e4SLinus Torvalds } 27761da177e4SLinus Torvalds 277751cc5068SAlexey Dobriyan static void init_once(void *foo) 27781da177e4SLinus Torvalds { 27791da177e4SLinus Torvalds struct shmem_inode_info *p = (struct shmem_inode_info *) foo; 27801da177e4SLinus Torvalds 27811da177e4SLinus Torvalds inode_init_once(&p->vfs_inode); 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds static int init_inodecache(void) 27851da177e4SLinus Torvalds { 27861da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 27871da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 2788040b5c6fSAlexey Dobriyan 0, SLAB_PANIC, init_once); 27891da177e4SLinus Torvalds return 0; 27901da177e4SLinus Torvalds } 27911da177e4SLinus Torvalds 27921da177e4SLinus Torvalds static void destroy_inodecache(void) 27931da177e4SLinus Torvalds { 27941a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 2797f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 27981da177e4SLinus Torvalds .writepage = shmem_writepage, 279976719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 28001da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2801ae976416SHugh Dickins .readpage = shmem_readpage, 2802800d15a5SNick Piggin .write_begin = shmem_write_begin, 2803800d15a5SNick Piggin .write_end = shmem_write_end, 28041da177e4SLinus Torvalds #endif 2805304dbdb7SLee Schermerhorn .migratepage = migrate_page, 2806aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 28071da177e4SLinus Torvalds }; 28081da177e4SLinus Torvalds 280915ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 28101da177e4SLinus Torvalds .mmap = shmem_mmap, 28111da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 28121da177e4SLinus Torvalds .llseek = generic_file_llseek, 2813bcd78e49SHugh Dickins .read = do_sync_read, 28145402b976SHugh Dickins .write = do_sync_write, 2815bcd78e49SHugh Dickins .aio_read = shmem_file_aio_read, 28165402b976SHugh Dickins .aio_write = generic_file_aio_write, 28171b061d92SChristoph Hellwig .fsync = noop_fsync, 2818708e3508SHugh Dickins .splice_read = shmem_file_splice_read, 2819ae976416SHugh Dickins .splice_write = generic_file_splice_write, 28201da177e4SLinus Torvalds #endif 28211da177e4SLinus Torvalds }; 28221da177e4SLinus Torvalds 282392e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 282494c1e62dSHugh Dickins .setattr = shmem_setattr, 2825f6b3ec23SBadari Pulavarty .truncate_range = shmem_truncate_range, 2826b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2827b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2828b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2829b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2830b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2831b09e0fa4SEric Paris #endif 283239f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 28331c7c474cSChristoph Hellwig .check_acl = generic_check_acl, 283439f0247dSAndreas Gruenbacher #endif 283539f0247dSAndreas Gruenbacher 28361da177e4SLinus Torvalds }; 28371da177e4SLinus Torvalds 283892e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 28391da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 28401da177e4SLinus Torvalds .create = shmem_create, 28411da177e4SLinus Torvalds .lookup = simple_lookup, 28421da177e4SLinus Torvalds .link = shmem_link, 28431da177e4SLinus Torvalds .unlink = shmem_unlink, 28441da177e4SLinus Torvalds .symlink = shmem_symlink, 28451da177e4SLinus Torvalds .mkdir = shmem_mkdir, 28461da177e4SLinus Torvalds .rmdir = shmem_rmdir, 28471da177e4SLinus Torvalds .mknod = shmem_mknod, 28481da177e4SLinus Torvalds .rename = shmem_rename, 28491da177e4SLinus Torvalds #endif 2850b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2851b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2852b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2853b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2854b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2855b09e0fa4SEric Paris #endif 285639f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 285794c1e62dSHugh Dickins .setattr = shmem_setattr, 28581c7c474cSChristoph Hellwig .check_acl = generic_check_acl, 285939f0247dSAndreas Gruenbacher #endif 286039f0247dSAndreas Gruenbacher }; 286139f0247dSAndreas Gruenbacher 286292e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 2863b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2864b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2865b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2866b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2867b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2868b09e0fa4SEric Paris #endif 286939f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 287094c1e62dSHugh Dickins .setattr = shmem_setattr, 28711c7c474cSChristoph Hellwig .check_acl = generic_check_acl, 287239f0247dSAndreas Gruenbacher #endif 28731da177e4SLinus Torvalds }; 28741da177e4SLinus Torvalds 2875759b9775SHugh Dickins static const struct super_operations shmem_ops = { 28761da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 28771da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 28781da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 28791da177e4SLinus Torvalds .statfs = shmem_statfs, 28801da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 2881680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 28821da177e4SLinus Torvalds #endif 28831f895f75SAl Viro .evict_inode = shmem_evict_inode, 28841da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 28851da177e4SLinus Torvalds .put_super = shmem_put_super, 28861da177e4SLinus Torvalds }; 28871da177e4SLinus Torvalds 2888f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 288954cb8821SNick Piggin .fault = shmem_fault, 28901da177e4SLinus Torvalds #ifdef CONFIG_NUMA 28911da177e4SLinus Torvalds .set_policy = shmem_set_policy, 28921da177e4SLinus Torvalds .get_policy = shmem_get_policy, 28931da177e4SLinus Torvalds #endif 28941da177e4SLinus Torvalds }; 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds 28973c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 28983c26ff6eSAl Viro int flags, const char *dev_name, void *data) 28991da177e4SLinus Torvalds { 29003c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 29011da177e4SLinus Torvalds } 29021da177e4SLinus Torvalds 29031da177e4SLinus Torvalds static struct file_system_type tmpfs_fs_type = { 29041da177e4SLinus Torvalds .owner = THIS_MODULE, 29051da177e4SLinus Torvalds .name = "tmpfs", 29063c26ff6eSAl Viro .mount = shmem_mount, 29071da177e4SLinus Torvalds .kill_sb = kill_litter_super, 29081da177e4SLinus Torvalds }; 29091da177e4SLinus Torvalds 29102b2af54aSKay Sievers int __init init_tmpfs(void) 29111da177e4SLinus Torvalds { 29121da177e4SLinus Torvalds int error; 29131da177e4SLinus Torvalds 2914e0bf68ddSPeter Zijlstra error = bdi_init(&shmem_backing_dev_info); 2915e0bf68ddSPeter Zijlstra if (error) 2916e0bf68ddSPeter Zijlstra goto out4; 2917e0bf68ddSPeter Zijlstra 29181da177e4SLinus Torvalds error = init_inodecache(); 29191da177e4SLinus Torvalds if (error) 29201da177e4SLinus Torvalds goto out3; 29211da177e4SLinus Torvalds 29221da177e4SLinus Torvalds error = register_filesystem(&tmpfs_fs_type); 29231da177e4SLinus Torvalds if (error) { 29241da177e4SLinus Torvalds printk(KERN_ERR "Could not register tmpfs\n"); 29251da177e4SLinus Torvalds goto out2; 29261da177e4SLinus Torvalds } 292795dc112aSGreg Kroah-Hartman 29281f5ce9e9STrond Myklebust shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER, 29291da177e4SLinus Torvalds tmpfs_fs_type.name, NULL); 29301da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 29311da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 29321da177e4SLinus Torvalds printk(KERN_ERR "Could not kern_mount tmpfs\n"); 29331da177e4SLinus Torvalds goto out1; 29341da177e4SLinus Torvalds } 29351da177e4SLinus Torvalds return 0; 29361da177e4SLinus Torvalds 29371da177e4SLinus Torvalds out1: 29381da177e4SLinus Torvalds unregister_filesystem(&tmpfs_fs_type); 29391da177e4SLinus Torvalds out2: 29401da177e4SLinus Torvalds destroy_inodecache(); 29411da177e4SLinus Torvalds out3: 2942e0bf68ddSPeter Zijlstra bdi_destroy(&shmem_backing_dev_info); 2943e0bf68ddSPeter Zijlstra out4: 29441da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 29451da177e4SLinus Torvalds return error; 29461da177e4SLinus Torvalds } 2947853ac43aSMatt Mackall 294887946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR 294987946a72SDaisuke Nishimura /** 295087946a72SDaisuke Nishimura * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file 295187946a72SDaisuke Nishimura * @inode: the inode to be searched 295287946a72SDaisuke Nishimura * @pgoff: the offset to be searched 295387946a72SDaisuke Nishimura * @pagep: the pointer for the found page to be stored 295487946a72SDaisuke Nishimura * @ent: the pointer for the found swap entry to be stored 295587946a72SDaisuke Nishimura * 295687946a72SDaisuke Nishimura * If a page is found, refcount of it is incremented. Callers should handle 295787946a72SDaisuke Nishimura * these refcount. 295887946a72SDaisuke Nishimura */ 295987946a72SDaisuke Nishimura void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff, 296087946a72SDaisuke Nishimura struct page **pagep, swp_entry_t *ent) 296187946a72SDaisuke Nishimura { 296287946a72SDaisuke Nishimura swp_entry_t entry = { .val = 0 }, *ptr; 296387946a72SDaisuke Nishimura struct page *page = NULL; 296487946a72SDaisuke Nishimura struct shmem_inode_info *info = SHMEM_I(inode); 296587946a72SDaisuke Nishimura 296687946a72SDaisuke Nishimura if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 296787946a72SDaisuke Nishimura goto out; 296887946a72SDaisuke Nishimura 296987946a72SDaisuke Nishimura spin_lock(&info->lock); 297087946a72SDaisuke Nishimura ptr = shmem_swp_entry(info, pgoff, NULL); 297187946a72SDaisuke Nishimura #ifdef CONFIG_SWAP 297287946a72SDaisuke Nishimura if (ptr && ptr->val) { 297387946a72SDaisuke Nishimura entry.val = ptr->val; 297487946a72SDaisuke Nishimura page = find_get_page(&swapper_space, entry.val); 297587946a72SDaisuke Nishimura } else 297687946a72SDaisuke Nishimura #endif 297787946a72SDaisuke Nishimura page = find_get_page(inode->i_mapping, pgoff); 297887946a72SDaisuke Nishimura if (ptr) 297987946a72SDaisuke Nishimura shmem_swp_unmap(ptr); 298087946a72SDaisuke Nishimura spin_unlock(&info->lock); 298187946a72SDaisuke Nishimura out: 298287946a72SDaisuke Nishimura *pagep = page; 298387946a72SDaisuke Nishimura *ent = entry; 298487946a72SDaisuke Nishimura } 298587946a72SDaisuke Nishimura #endif 298687946a72SDaisuke Nishimura 2987853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 2988853ac43aSMatt Mackall 2989853ac43aSMatt Mackall /* 2990853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 2991853ac43aSMatt Mackall * 2992853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 2993853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 2994853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 2995853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 2996853ac43aSMatt Mackall */ 2997853ac43aSMatt Mackall 2998853ac43aSMatt Mackall #include <linux/ramfs.h> 2999853ac43aSMatt Mackall 3000853ac43aSMatt Mackall static struct file_system_type tmpfs_fs_type = { 3001853ac43aSMatt Mackall .name = "tmpfs", 30023c26ff6eSAl Viro .mount = ramfs_mount, 3003853ac43aSMatt Mackall .kill_sb = kill_litter_super, 3004853ac43aSMatt Mackall }; 3005853ac43aSMatt Mackall 30062b2af54aSKay Sievers int __init init_tmpfs(void) 3007853ac43aSMatt Mackall { 3008853ac43aSMatt Mackall BUG_ON(register_filesystem(&tmpfs_fs_type) != 0); 3009853ac43aSMatt Mackall 3010853ac43aSMatt Mackall shm_mnt = kern_mount(&tmpfs_fs_type); 3011853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 3012853ac43aSMatt Mackall 3013853ac43aSMatt Mackall return 0; 3014853ac43aSMatt Mackall } 3015853ac43aSMatt Mackall 3016853ac43aSMatt Mackall int shmem_unuse(swp_entry_t entry, struct page *page) 3017853ac43aSMatt Mackall { 3018853ac43aSMatt Mackall return 0; 3019853ac43aSMatt Mackall } 3020853ac43aSMatt Mackall 30213f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 30223f96b79aSHugh Dickins { 30233f96b79aSHugh Dickins return 0; 30243f96b79aSHugh Dickins } 30253f96b79aSHugh Dickins 302694c1e62dSHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end) 302794c1e62dSHugh Dickins { 302894c1e62dSHugh Dickins truncate_inode_pages_range(inode->i_mapping, start, end); 302994c1e62dSHugh Dickins } 303094c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 303194c1e62dSHugh Dickins 303287946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR 303387946a72SDaisuke Nishimura /** 303487946a72SDaisuke Nishimura * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file 303587946a72SDaisuke Nishimura * @inode: the inode to be searched 303687946a72SDaisuke Nishimura * @pgoff: the offset to be searched 303787946a72SDaisuke Nishimura * @pagep: the pointer for the found page to be stored 303887946a72SDaisuke Nishimura * @ent: the pointer for the found swap entry to be stored 303987946a72SDaisuke Nishimura * 304087946a72SDaisuke Nishimura * If a page is found, refcount of it is incremented. Callers should handle 304187946a72SDaisuke Nishimura * these refcount. 304287946a72SDaisuke Nishimura */ 304387946a72SDaisuke Nishimura void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff, 304487946a72SDaisuke Nishimura struct page **pagep, swp_entry_t *ent) 304587946a72SDaisuke Nishimura { 304687946a72SDaisuke Nishimura struct page *page = NULL; 304787946a72SDaisuke Nishimura 304887946a72SDaisuke Nishimura if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 304987946a72SDaisuke Nishimura goto out; 305087946a72SDaisuke Nishimura page = find_get_page(inode->i_mapping, pgoff); 305187946a72SDaisuke Nishimura out: 305287946a72SDaisuke Nishimura *pagep = page; 305387946a72SDaisuke Nishimura *ent = (swp_entry_t){ .val = 0 }; 305487946a72SDaisuke Nishimura } 305587946a72SDaisuke Nishimura #endif 305687946a72SDaisuke Nishimura 3057853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 30580b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 3059454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 30600b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 30610b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 3062caefba17SHugh Dickins #define SHMEM_MAX_BYTES MAX_LFS_FILESIZE 3063853ac43aSMatt Mackall 3064853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 3065853ac43aSMatt Mackall 3066853ac43aSMatt Mackall /* common code */ 30671da177e4SLinus Torvalds 306846711810SRandy Dunlap /** 30691da177e4SLinus Torvalds * shmem_file_setup - get an unlinked file living in tmpfs 30701da177e4SLinus Torvalds * @name: name for dentry (to be seen in /proc/<pid>/maps 30711da177e4SLinus Torvalds * @size: size to be set for the file 30720b0a0806SHugh Dickins * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 30731da177e4SLinus Torvalds */ 3074168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 30751da177e4SLinus Torvalds { 30761da177e4SLinus Torvalds int error; 30771da177e4SLinus Torvalds struct file *file; 30781da177e4SLinus Torvalds struct inode *inode; 30792c48b9c4SAl Viro struct path path; 30802c48b9c4SAl Viro struct dentry *root; 30811da177e4SLinus Torvalds struct qstr this; 30821da177e4SLinus Torvalds 30831da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 30841da177e4SLinus Torvalds return (void *)shm_mnt; 30851da177e4SLinus Torvalds 30861da177e4SLinus Torvalds if (size < 0 || size > SHMEM_MAX_BYTES) 30871da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 30881da177e4SLinus Torvalds 30891da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 30901da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 30911da177e4SLinus Torvalds 30921da177e4SLinus Torvalds error = -ENOMEM; 30931da177e4SLinus Torvalds this.name = name; 30941da177e4SLinus Torvalds this.len = strlen(name); 30951da177e4SLinus Torvalds this.hash = 0; /* will go */ 30961da177e4SLinus Torvalds root = shm_mnt->mnt_root; 30972c48b9c4SAl Viro path.dentry = d_alloc(root, &this); 30982c48b9c4SAl Viro if (!path.dentry) 30991da177e4SLinus Torvalds goto put_memory; 31002c48b9c4SAl Viro path.mnt = mntget(shm_mnt); 31011da177e4SLinus Torvalds 31021da177e4SLinus Torvalds error = -ENOSPC; 3103454abafeSDmitry Monakhov inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 31041da177e4SLinus Torvalds if (!inode) 31054b42af81SAl Viro goto put_dentry; 31061da177e4SLinus Torvalds 31072c48b9c4SAl Viro d_instantiate(path.dentry, inode); 31081da177e4SLinus Torvalds inode->i_size = size; 31091da177e4SLinus Torvalds inode->i_nlink = 0; /* It is unlinked */ 3110853ac43aSMatt Mackall #ifndef CONFIG_MMU 3111853ac43aSMatt Mackall error = ramfs_nommu_expand_for_mapping(inode, size); 3112853ac43aSMatt Mackall if (error) 31134b42af81SAl Viro goto put_dentry; 3114853ac43aSMatt Mackall #endif 31154b42af81SAl Viro 31164b42af81SAl Viro error = -ENFILE; 31172c48b9c4SAl Viro file = alloc_file(&path, FMODE_WRITE | FMODE_READ, 31184b42af81SAl Viro &shmem_file_operations); 31194b42af81SAl Viro if (!file) 31204b42af81SAl Viro goto put_dentry; 31214b42af81SAl Viro 31221da177e4SLinus Torvalds return file; 31231da177e4SLinus Torvalds 31241da177e4SLinus Torvalds put_dentry: 31252c48b9c4SAl Viro path_put(&path); 31261da177e4SLinus Torvalds put_memory: 31271da177e4SLinus Torvalds shmem_unacct_size(flags, size); 31281da177e4SLinus Torvalds return ERR_PTR(error); 31291da177e4SLinus Torvalds } 3130395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 31311da177e4SLinus Torvalds 313246711810SRandy Dunlap /** 31331da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 31341da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 31351da177e4SLinus Torvalds */ 31361da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 31371da177e4SLinus Torvalds { 31381da177e4SLinus Torvalds struct file *file; 31391da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 31401da177e4SLinus Torvalds 31411da177e4SLinus Torvalds file = shmem_file_setup("dev/zero", size, vma->vm_flags); 31421da177e4SLinus Torvalds if (IS_ERR(file)) 31431da177e4SLinus Torvalds return PTR_ERR(file); 31441da177e4SLinus Torvalds 31451da177e4SLinus Torvalds if (vma->vm_file) 31461da177e4SLinus Torvalds fput(vma->vm_file); 31471da177e4SLinus Torvalds vma->vm_file = file; 31481da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 3149bee4c36aSHugh Dickins vma->vm_flags |= VM_CAN_NONLINEAR; 31501da177e4SLinus Torvalds return 0; 31511da177e4SLinus Torvalds } 3152d9d90e5eSHugh Dickins 3153d9d90e5eSHugh Dickins /** 3154d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 3155d9d90e5eSHugh Dickins * @mapping: the page's address_space 3156d9d90e5eSHugh Dickins * @index: the page index 3157d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 3158d9d90e5eSHugh Dickins * 3159d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 3160d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 3161d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 3162d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 3163d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 3164d9d90e5eSHugh Dickins * 3165d9d90e5eSHugh Dickins * Provide a stub for those callers to start using now, then later 3166d9d90e5eSHugh Dickins * flesh it out to call shmem_getpage() with additional gfp mask, when 3167d9d90e5eSHugh Dickins * shmem_file_splice_read() is added and shmem_readpage() is removed. 3168d9d90e5eSHugh Dickins */ 3169d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 3170d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 3171d9d90e5eSHugh Dickins { 3172d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 3173d9d90e5eSHugh Dickins } 3174d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 3175