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> 541da177e4SLinus Torvalds #include <linux/security.h> 551da177e4SLinus Torvalds #include <linux/swapops.h> 561da177e4SLinus Torvalds #include <linux/mempolicy.h> 571da177e4SLinus Torvalds #include <linux/namei.h> 58b00dc3adSHugh Dickins #include <linux/ctype.h> 59304dbdb7SLee Schermerhorn #include <linux/migrate.h> 60c1f60a5aSChristoph Lameter #include <linux/highmem.h> 61680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 6292562927SMimi Zohar #include <linux/magic.h> 63304dbdb7SLee Schermerhorn 641da177e4SLinus Torvalds #include <asm/uaccess.h> 651da177e4SLinus Torvalds #include <asm/div64.h> 661da177e4SLinus Torvalds #include <asm/pgtable.h> 671da177e4SLinus Torvalds 68caefba17SHugh Dickins /* 69caefba17SHugh Dickins * The maximum size of a shmem/tmpfs file is limited by the maximum size of 70caefba17SHugh Dickins * its triple-indirect swap vector - see illustration at shmem_swp_entry(). 71caefba17SHugh Dickins * 72caefba17SHugh Dickins * With 4kB page size, maximum file size is just over 2TB on a 32-bit kernel, 73caefba17SHugh Dickins * but one eighth of that on a 64-bit kernel. With 8kB page size, maximum 74caefba17SHugh Dickins * file size is just over 4TB on a 64-bit kernel, but 16TB on a 32-bit kernel, 75caefba17SHugh Dickins * MAX_LFS_FILESIZE being then more restrictive than swap vector layout. 76caefba17SHugh Dickins * 77caefba17SHugh Dickins * We use / and * instead of shifts in the definitions below, so that the swap 78caefba17SHugh Dickins * vector can be tested with small even values (e.g. 20) for ENTRIES_PER_PAGE. 79caefba17SHugh Dickins */ 801da177e4SLinus Torvalds #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long)) 8161609d01SYuri Tikhonov #define ENTRIES_PER_PAGEPAGE ((unsigned long long)ENTRIES_PER_PAGE*ENTRIES_PER_PAGE) 82caefba17SHugh Dickins 83caefba17SHugh Dickins #define SHMSWP_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1)) 84caefba17SHugh Dickins #define SHMSWP_MAX_BYTES (SHMSWP_MAX_INDEX << PAGE_CACHE_SHIFT) 85caefba17SHugh Dickins 86caefba17SHugh Dickins #define SHMEM_MAX_BYTES min_t(unsigned long long, SHMSWP_MAX_BYTES, MAX_LFS_FILESIZE) 87caefba17SHugh Dickins #define SHMEM_MAX_INDEX ((unsigned long)((SHMEM_MAX_BYTES+1) >> PAGE_CACHE_SHIFT)) 88caefba17SHugh Dickins 891da177e4SLinus Torvalds #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) 901da177e4SLinus Torvalds #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /* info->flags needs VM_flags to handle pagein/truncate races efficiently */ 931da177e4SLinus Torvalds #define SHMEM_PAGEIN VM_READ 941da177e4SLinus Torvalds #define SHMEM_TRUNCATE VM_WRITE 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* Definition to limit shmem_truncate's steps between cond_rescheds */ 971da177e4SLinus Torvalds #define LATENCY_LIMIT 64 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 1001da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 1011da177e4SLinus Torvalds 102b09e0fa4SEric Paris struct shmem_xattr { 103b09e0fa4SEric Paris struct list_head list; /* anchored by shmem_inode_info->xattr_list */ 104b09e0fa4SEric Paris char *name; /* xattr name */ 105b09e0fa4SEric Paris size_t size; 106b09e0fa4SEric Paris char value[0]; 107b09e0fa4SEric Paris }; 108b09e0fa4SEric Paris 1091da177e4SLinus Torvalds /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */ 1101da177e4SLinus Torvalds enum sgp_type { 1111da177e4SLinus Torvalds SGP_READ, /* don't exceed i_size, don't allocate page */ 1121da177e4SLinus Torvalds SGP_CACHE, /* don't exceed i_size, may allocate page */ 113a0ee5ec5SHugh Dickins SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ 1141da177e4SLinus Torvalds SGP_WRITE, /* may exceed i_size, may allocate page */ 1151da177e4SLinus Torvalds }; 1161da177e4SLinus Torvalds 117b76db735SAndrew Morton #ifdef CONFIG_TMPFS 118680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 119680d794bSakpm@linux-foundation.org { 120680d794bSakpm@linux-foundation.org return totalram_pages / 2; 121680d794bSakpm@linux-foundation.org } 122680d794bSakpm@linux-foundation.org 123680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 124680d794bSakpm@linux-foundation.org { 125680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 126680d794bSakpm@linux-foundation.org } 127b76db735SAndrew Morton #endif 128680d794bSakpm@linux-foundation.org 1291da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx, 1301da177e4SLinus Torvalds struct page **pagep, enum sgp_type sgp, int *type); 1311da177e4SLinus Torvalds 1326daa0e28SAl Viro static inline struct page *shmem_dir_alloc(gfp_t gfp_mask) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds /* 1351da177e4SLinus Torvalds * The above definition of ENTRIES_PER_PAGE, and the use of 1361da177e4SLinus Torvalds * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE: 1371da177e4SLinus Torvalds * might be reconsidered if it ever diverges from PAGE_SIZE. 138769848c0SMel Gorman * 139e12ba74dSMel Gorman * Mobility flags are masked out as swap vectors cannot move 1401da177e4SLinus Torvalds */ 141e12ba74dSMel Gorman return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO, 142769848c0SMel Gorman PAGE_CACHE_SHIFT-PAGE_SHIFT); 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds static inline void shmem_dir_free(struct page *page) 1461da177e4SLinus Torvalds { 1471da177e4SLinus Torvalds __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds static struct page **shmem_dir_map(struct page *page) 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds return (struct page **)kmap_atomic(page, KM_USER0); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds static inline void shmem_dir_unmap(struct page **dir) 1561da177e4SLinus Torvalds { 1571da177e4SLinus Torvalds kunmap_atomic(dir, KM_USER0); 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds static swp_entry_t *shmem_swp_map(struct page *page) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds return (swp_entry_t *)kmap_atomic(page, KM_USER1); 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds static inline void shmem_swp_balance_unmap(void) 1661da177e4SLinus Torvalds { 1671da177e4SLinus Torvalds /* 1681da177e4SLinus Torvalds * When passing a pointer to an i_direct entry, to code which 1691da177e4SLinus Torvalds * also handles indirect entries and so will shmem_swp_unmap, 1701da177e4SLinus Torvalds * we must arrange for the preempt count to remain in balance. 1711da177e4SLinus Torvalds * What kmap_atomic of a lowmem page does depends on config 1721da177e4SLinus Torvalds * and architecture, so pretend to kmap_atomic some lowmem page. 1731da177e4SLinus Torvalds */ 1741da177e4SLinus Torvalds (void) kmap_atomic(ZERO_PAGE(0), KM_USER1); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds static inline void shmem_swp_unmap(swp_entry_t *entry) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds kunmap_atomic(entry, KM_USER1); 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds return sb->s_fs_info; 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* 1881da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1891da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1901da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1911da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1921da177e4SLinus Torvalds */ 1931da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1941da177e4SLinus Torvalds { 1950b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 1960b0a0806SHugh Dickins 0 : security_vm_enough_memory_kern(VM_ACCT(size)); 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 2001da177e4SLinus Torvalds { 2010b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 2021da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds /* 2061da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 2071da177e4SLinus Torvalds * pages are allocated, in order to allow huge sparse files. 2081da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 2091da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 2101da177e4SLinus Torvalds */ 2111da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags) 2121da177e4SLinus Torvalds { 2130b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 2140b0a0806SHugh Dickins security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 2181da177e4SLinus Torvalds { 2190b0a0806SHugh Dickins if (flags & VM_NORESERVE) 2201da177e4SLinus Torvalds vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 223759b9775SHugh Dickins static const struct super_operations shmem_ops; 224f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 22515ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 22692e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 22792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 22892e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 229f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 2301da177e4SLinus Torvalds 2316c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info __read_mostly = { 2321da177e4SLinus Torvalds .ra_pages = 0, /* No readahead */ 2334f98a2feSRik van Riel .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, 2341da177e4SLinus Torvalds }; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 237cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds static void shmem_free_blocks(struct inode *inode, long pages) 2401da177e4SLinus Torvalds { 2411da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2420edd73b3SHugh Dickins if (sbinfo->max_blocks) { 2437e496299STim Chen percpu_counter_add(&sbinfo->used_blocks, -pages); 2447e496299STim Chen spin_lock(&inode->i_lock); 2451da177e4SLinus Torvalds inode->i_blocks -= pages*BLOCKS_PER_PAGE; 2467e496299STim Chen spin_unlock(&inode->i_lock); 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2505b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 2515b04c689SPavel Emelyanov { 2525b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2535b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2545b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2555b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 2565b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2575b04c689SPavel Emelyanov return -ENOSPC; 2585b04c689SPavel Emelyanov } 2595b04c689SPavel Emelyanov sbinfo->free_inodes--; 2605b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2615b04c689SPavel Emelyanov } 2625b04c689SPavel Emelyanov return 0; 2635b04c689SPavel Emelyanov } 2645b04c689SPavel Emelyanov 2655b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2665b04c689SPavel Emelyanov { 2675b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2685b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2695b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2705b04c689SPavel Emelyanov sbinfo->free_inodes++; 2715b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2725b04c689SPavel Emelyanov } 2735b04c689SPavel Emelyanov } 2745b04c689SPavel Emelyanov 27546711810SRandy Dunlap /** 2761da177e4SLinus Torvalds * shmem_recalc_inode - recalculate the size of an inode 2771da177e4SLinus Torvalds * @inode: inode to recalc 2781da177e4SLinus Torvalds * 2791da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2801da177e4SLinus Torvalds * undirtied hole pages behind our back. 2811da177e4SLinus Torvalds * 2821da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2831da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2841da177e4SLinus Torvalds * 2851da177e4SLinus Torvalds * It has to be called with the spinlock held. 2861da177e4SLinus Torvalds */ 2871da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2881da177e4SLinus Torvalds { 2891da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2901da177e4SLinus Torvalds long freed; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2931da177e4SLinus Torvalds if (freed > 0) { 2941da177e4SLinus Torvalds info->alloced -= freed; 2951da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2961da177e4SLinus Torvalds shmem_free_blocks(inode, freed); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 30046711810SRandy Dunlap /** 3011da177e4SLinus Torvalds * shmem_swp_entry - find the swap vector position in the info structure 3021da177e4SLinus Torvalds * @info: info structure for the inode 3031da177e4SLinus Torvalds * @index: index of the page to find 3041da177e4SLinus Torvalds * @page: optional page to add to the structure. Has to be preset to 3051da177e4SLinus Torvalds * all zeros 3061da177e4SLinus Torvalds * 3071da177e4SLinus Torvalds * If there is no space allocated yet it will return NULL when 3081da177e4SLinus Torvalds * page is NULL, else it will use the page for the needed block, 3091da177e4SLinus Torvalds * setting it to NULL on return to indicate that it has been used. 3101da177e4SLinus Torvalds * 3111da177e4SLinus Torvalds * The swap vector is organized the following way: 3121da177e4SLinus Torvalds * 3131da177e4SLinus Torvalds * There are SHMEM_NR_DIRECT entries directly stored in the 3141da177e4SLinus Torvalds * shmem_inode_info structure. So small files do not need an addional 3151da177e4SLinus Torvalds * allocation. 3161da177e4SLinus Torvalds * 3171da177e4SLinus Torvalds * For pages with index > SHMEM_NR_DIRECT there is the pointer 3181da177e4SLinus Torvalds * i_indirect which points to a page which holds in the first half 3191da177e4SLinus Torvalds * doubly indirect blocks, in the second half triple indirect blocks: 3201da177e4SLinus Torvalds * 3211da177e4SLinus Torvalds * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the 3221da177e4SLinus Torvalds * following layout (for SHMEM_NR_DIRECT == 16): 3231da177e4SLinus Torvalds * 3241da177e4SLinus Torvalds * i_indirect -> dir --> 16-19 3251da177e4SLinus Torvalds * | +-> 20-23 3261da177e4SLinus Torvalds * | 3271da177e4SLinus Torvalds * +-->dir2 --> 24-27 3281da177e4SLinus Torvalds * | +-> 28-31 3291da177e4SLinus Torvalds * | +-> 32-35 3301da177e4SLinus Torvalds * | +-> 36-39 3311da177e4SLinus Torvalds * | 3321da177e4SLinus Torvalds * +-->dir3 --> 40-43 3331da177e4SLinus Torvalds * +-> 44-47 3341da177e4SLinus Torvalds * +-> 48-51 3351da177e4SLinus Torvalds * +-> 52-55 3361da177e4SLinus Torvalds */ 3371da177e4SLinus Torvalds static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds unsigned long offset; 3401da177e4SLinus Torvalds struct page **dir; 3411da177e4SLinus Torvalds struct page *subdir; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds if (index < SHMEM_NR_DIRECT) { 3441da177e4SLinus Torvalds shmem_swp_balance_unmap(); 3451da177e4SLinus Torvalds return info->i_direct+index; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds if (!info->i_indirect) { 3481da177e4SLinus Torvalds if (page) { 3491da177e4SLinus Torvalds info->i_indirect = *page; 3501da177e4SLinus Torvalds *page = NULL; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds return NULL; /* need another page */ 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds index -= SHMEM_NR_DIRECT; 3561da177e4SLinus Torvalds offset = index % ENTRIES_PER_PAGE; 3571da177e4SLinus Torvalds index /= ENTRIES_PER_PAGE; 3581da177e4SLinus Torvalds dir = shmem_dir_map(info->i_indirect); 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds if (index >= ENTRIES_PER_PAGE/2) { 3611da177e4SLinus Torvalds index -= ENTRIES_PER_PAGE/2; 3621da177e4SLinus Torvalds dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE; 3631da177e4SLinus Torvalds index %= ENTRIES_PER_PAGE; 3641da177e4SLinus Torvalds subdir = *dir; 3651da177e4SLinus Torvalds if (!subdir) { 3661da177e4SLinus Torvalds if (page) { 3671da177e4SLinus Torvalds *dir = *page; 3681da177e4SLinus Torvalds *page = NULL; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds shmem_dir_unmap(dir); 3711da177e4SLinus Torvalds return NULL; /* need another page */ 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds shmem_dir_unmap(dir); 3741da177e4SLinus Torvalds dir = shmem_dir_map(subdir); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds dir += index; 3781da177e4SLinus Torvalds subdir = *dir; 3791da177e4SLinus Torvalds if (!subdir) { 3801da177e4SLinus Torvalds if (!page || !(subdir = *page)) { 3811da177e4SLinus Torvalds shmem_dir_unmap(dir); 3821da177e4SLinus Torvalds return NULL; /* need a page */ 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds *dir = subdir; 3851da177e4SLinus Torvalds *page = NULL; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds shmem_dir_unmap(dir); 3881da177e4SLinus Torvalds return shmem_swp_map(subdir) + offset; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value) 3921da177e4SLinus Torvalds { 3931da177e4SLinus Torvalds long incdec = value? 1: -1; 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds entry->val = value; 3961da177e4SLinus Torvalds info->swapped += incdec; 3974c21e2f2SHugh Dickins if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) { 3984c21e2f2SHugh Dickins struct page *page = kmap_atomic_to_page(entry); 3994c21e2f2SHugh Dickins set_page_private(page, page_private(page) + incdec); 4004c21e2f2SHugh Dickins } 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 40346711810SRandy Dunlap /** 4041da177e4SLinus Torvalds * shmem_swp_alloc - get the position of the swap entry for the page. 4051da177e4SLinus Torvalds * @info: info structure for the inode 4061da177e4SLinus Torvalds * @index: index of the page to find 4071da177e4SLinus Torvalds * @sgp: check and recheck i_size? skip allocation? 40846711810SRandy Dunlap * 40946711810SRandy Dunlap * If the entry does not exist, allocate it. 4101da177e4SLinus Torvalds */ 4111da177e4SLinus Torvalds static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp) 4121da177e4SLinus Torvalds { 4131da177e4SLinus Torvalds struct inode *inode = &info->vfs_inode; 4141da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 4151da177e4SLinus Torvalds struct page *page = NULL; 4161da177e4SLinus Torvalds swp_entry_t *entry; 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds if (sgp != SGP_WRITE && 4191da177e4SLinus Torvalds ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 4201da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds while (!(entry = shmem_swp_entry(info, index, &page))) { 4231da177e4SLinus Torvalds if (sgp == SGP_READ) 4241da177e4SLinus Torvalds return shmem_swp_map(ZERO_PAGE(0)); 4251da177e4SLinus Torvalds /* 4267e496299STim Chen * Test used_blocks against 1 less max_blocks, since we have 1 data 4271da177e4SLinus Torvalds * page (and perhaps indirect index pages) yet to allocate: 4281da177e4SLinus Torvalds * a waste to allocate index if we cannot allocate data. 4291da177e4SLinus Torvalds */ 4300edd73b3SHugh Dickins if (sbinfo->max_blocks) { 431fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 432fc5da22aSHugh Dickins sbinfo->max_blocks - 1) >= 0) 4331da177e4SLinus Torvalds return ERR_PTR(-ENOSPC); 4347e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 4357e496299STim Chen spin_lock(&inode->i_lock); 4361da177e4SLinus Torvalds inode->i_blocks += BLOCKS_PER_PAGE; 4377e496299STim Chen spin_unlock(&inode->i_lock); 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds spin_unlock(&info->lock); 441769848c0SMel Gorman page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping)); 4421da177e4SLinus Torvalds spin_lock(&info->lock); 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds if (!page) { 4451da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 4461da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds if (sgp != SGP_WRITE && 4491da177e4SLinus Torvalds ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 4501da177e4SLinus Torvalds entry = ERR_PTR(-EINVAL); 4511da177e4SLinus Torvalds break; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds if (info->next_index <= index) 4541da177e4SLinus Torvalds info->next_index = index + 1; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds if (page) { 4571da177e4SLinus Torvalds /* another task gave its page, or truncated the file */ 4581da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 4591da177e4SLinus Torvalds shmem_dir_free(page); 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds if (info->next_index <= index && !IS_ERR(entry)) 4621da177e4SLinus Torvalds info->next_index = index + 1; 4631da177e4SLinus Torvalds return entry; 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds 46646711810SRandy Dunlap /** 4671da177e4SLinus Torvalds * shmem_free_swp - free some swap entries in a directory 4681da177e4SLinus Torvalds * @dir: pointer to the directory 4691da177e4SLinus Torvalds * @edir: pointer after last entry of the directory 4701ae70006SHugh Dickins * @punch_lock: pointer to spinlock when needed for the holepunch case 4711da177e4SLinus Torvalds */ 4721ae70006SHugh Dickins static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir, 4731ae70006SHugh Dickins spinlock_t *punch_lock) 4741da177e4SLinus Torvalds { 4751ae70006SHugh Dickins spinlock_t *punch_unlock = NULL; 4761da177e4SLinus Torvalds swp_entry_t *ptr; 4771da177e4SLinus Torvalds int freed = 0; 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds for (ptr = dir; ptr < edir; ptr++) { 4801da177e4SLinus Torvalds if (ptr->val) { 4811ae70006SHugh Dickins if (unlikely(punch_lock)) { 4821ae70006SHugh Dickins punch_unlock = punch_lock; 4831ae70006SHugh Dickins punch_lock = NULL; 4841ae70006SHugh Dickins spin_lock(punch_unlock); 4851ae70006SHugh Dickins if (!ptr->val) 4861ae70006SHugh Dickins continue; 4871ae70006SHugh Dickins } 4881da177e4SLinus Torvalds free_swap_and_cache(*ptr); 4891da177e4SLinus Torvalds *ptr = (swp_entry_t){0}; 4901da177e4SLinus Torvalds freed++; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds } 4931ae70006SHugh Dickins if (punch_unlock) 4941ae70006SHugh Dickins spin_unlock(punch_unlock); 4951da177e4SLinus Torvalds return freed; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981ae70006SHugh Dickins static int shmem_map_and_free_swp(struct page *subdir, int offset, 4991ae70006SHugh Dickins int limit, struct page ***dir, spinlock_t *punch_lock) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds swp_entry_t *ptr; 5021da177e4SLinus Torvalds int freed = 0; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds ptr = shmem_swp_map(subdir); 5051da177e4SLinus Torvalds for (; offset < limit; offset += LATENCY_LIMIT) { 5061da177e4SLinus Torvalds int size = limit - offset; 5071da177e4SLinus Torvalds if (size > LATENCY_LIMIT) 5081da177e4SLinus Torvalds size = LATENCY_LIMIT; 5091ae70006SHugh Dickins freed += shmem_free_swp(ptr+offset, ptr+offset+size, 5101ae70006SHugh Dickins punch_lock); 5111da177e4SLinus Torvalds if (need_resched()) { 5121da177e4SLinus Torvalds shmem_swp_unmap(ptr); 5131da177e4SLinus Torvalds if (*dir) { 5141da177e4SLinus Torvalds shmem_dir_unmap(*dir); 5151da177e4SLinus Torvalds *dir = NULL; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds cond_resched(); 5181da177e4SLinus Torvalds ptr = shmem_swp_map(subdir); 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds shmem_swp_unmap(ptr); 5221da177e4SLinus Torvalds return freed; 5231da177e4SLinus Torvalds } 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds static void shmem_free_pages(struct list_head *next) 5261da177e4SLinus Torvalds { 5271da177e4SLinus Torvalds struct page *page; 5281da177e4SLinus Torvalds int freed = 0; 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds do { 5311da177e4SLinus Torvalds page = container_of(next, struct page, lru); 5321da177e4SLinus Torvalds next = next->next; 5331da177e4SLinus Torvalds shmem_dir_free(page); 5341da177e4SLinus Torvalds freed++; 5351da177e4SLinus Torvalds if (freed >= LATENCY_LIMIT) { 5361da177e4SLinus Torvalds cond_resched(); 5371da177e4SLinus Torvalds freed = 0; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds } while (next); 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 54294c1e62dSHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end) 5431da177e4SLinus Torvalds { 5441da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 5451da177e4SLinus Torvalds unsigned long idx; 5461da177e4SLinus Torvalds unsigned long size; 5471da177e4SLinus Torvalds unsigned long limit; 5481da177e4SLinus Torvalds unsigned long stage; 5491da177e4SLinus Torvalds unsigned long diroff; 5501da177e4SLinus Torvalds struct page **dir; 5511da177e4SLinus Torvalds struct page *topdir; 5521da177e4SLinus Torvalds struct page *middir; 5531da177e4SLinus Torvalds struct page *subdir; 5541da177e4SLinus Torvalds swp_entry_t *ptr; 5551da177e4SLinus Torvalds LIST_HEAD(pages_to_free); 5561da177e4SLinus Torvalds long nr_pages_to_free = 0; 5571da177e4SLinus Torvalds long nr_swaps_freed = 0; 5581da177e4SLinus Torvalds int offset; 5591da177e4SLinus Torvalds int freed; 560a2646d1eSHugh Dickins int punch_hole; 5611ae70006SHugh Dickins spinlock_t *needs_lock; 5621ae70006SHugh Dickins spinlock_t *punch_lock; 563a2646d1eSHugh Dickins unsigned long upper_limit; 5641da177e4SLinus Torvalds 56594c1e62dSHugh Dickins truncate_inode_pages_range(inode->i_mapping, start, end); 56694c1e62dSHugh Dickins 5671da177e4SLinus Torvalds inode->i_ctime = inode->i_mtime = CURRENT_TIME; 568f6b3ec23SBadari Pulavarty idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 5691da177e4SLinus Torvalds if (idx >= info->next_index) 5701da177e4SLinus Torvalds return; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds spin_lock(&info->lock); 5731da177e4SLinus Torvalds info->flags |= SHMEM_TRUNCATE; 574f6b3ec23SBadari Pulavarty if (likely(end == (loff_t) -1)) { 5751da177e4SLinus Torvalds limit = info->next_index; 576a2646d1eSHugh Dickins upper_limit = SHMEM_MAX_INDEX; 5771da177e4SLinus Torvalds info->next_index = idx; 5781ae70006SHugh Dickins needs_lock = NULL; 579a2646d1eSHugh Dickins punch_hole = 0; 580f6b3ec23SBadari Pulavarty } else { 581a2646d1eSHugh Dickins if (end + 1 >= inode->i_size) { /* we may free a little more */ 582a2646d1eSHugh Dickins limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >> 583a2646d1eSHugh Dickins PAGE_CACHE_SHIFT; 584a2646d1eSHugh Dickins upper_limit = SHMEM_MAX_INDEX; 585a2646d1eSHugh Dickins } else { 586a2646d1eSHugh Dickins limit = (end + 1) >> PAGE_CACHE_SHIFT; 587a2646d1eSHugh Dickins upper_limit = limit; 588a2646d1eSHugh Dickins } 5891ae70006SHugh Dickins needs_lock = &info->lock; 590f6b3ec23SBadari Pulavarty punch_hole = 1; 591f6b3ec23SBadari Pulavarty } 592f6b3ec23SBadari Pulavarty 5931da177e4SLinus Torvalds topdir = info->i_indirect; 594f6b3ec23SBadari Pulavarty if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) { 5951da177e4SLinus Torvalds info->i_indirect = NULL; 5961da177e4SLinus Torvalds nr_pages_to_free++; 5971da177e4SLinus Torvalds list_add(&topdir->lru, &pages_to_free); 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds spin_unlock(&info->lock); 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds if (info->swapped && idx < SHMEM_NR_DIRECT) { 6021da177e4SLinus Torvalds ptr = info->i_direct; 6031da177e4SLinus Torvalds size = limit; 6041da177e4SLinus Torvalds if (size > SHMEM_NR_DIRECT) 6051da177e4SLinus Torvalds size = SHMEM_NR_DIRECT; 6061ae70006SHugh Dickins nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock); 6071da177e4SLinus Torvalds } 60892a3d03aSBadari Pulavarty 60992a3d03aSBadari Pulavarty /* 61092a3d03aSBadari Pulavarty * If there are no indirect blocks or we are punching a hole 61192a3d03aSBadari Pulavarty * below indirect blocks, nothing to be done. 61292a3d03aSBadari Pulavarty */ 613a2646d1eSHugh Dickins if (!topdir || limit <= SHMEM_NR_DIRECT) 6141da177e4SLinus Torvalds goto done2; 6151da177e4SLinus Torvalds 6161ae70006SHugh Dickins /* 6171ae70006SHugh Dickins * The truncation case has already dropped info->lock, and we're safe 6181ae70006SHugh Dickins * because i_size and next_index have already been lowered, preventing 6191ae70006SHugh Dickins * access beyond. But in the punch_hole case, we still need to take 6201ae70006SHugh Dickins * the lock when updating the swap directory, because there might be 6211ae70006SHugh Dickins * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or 6221ae70006SHugh Dickins * shmem_writepage. However, whenever we find we can remove a whole 6231ae70006SHugh Dickins * directory page (not at the misaligned start or end of the range), 6241ae70006SHugh Dickins * we first NULLify its pointer in the level above, and then have no 6251ae70006SHugh Dickins * need to take the lock when updating its contents: needs_lock and 6261ae70006SHugh Dickins * punch_lock (either pointing to info->lock or NULL) manage this. 6271ae70006SHugh Dickins */ 6281ae70006SHugh Dickins 629a2646d1eSHugh Dickins upper_limit -= SHMEM_NR_DIRECT; 6301da177e4SLinus Torvalds limit -= SHMEM_NR_DIRECT; 6311da177e4SLinus Torvalds idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0; 6321da177e4SLinus Torvalds offset = idx % ENTRIES_PER_PAGE; 6331da177e4SLinus Torvalds idx -= offset; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds dir = shmem_dir_map(topdir); 6361da177e4SLinus Torvalds stage = ENTRIES_PER_PAGEPAGE/2; 6371da177e4SLinus Torvalds if (idx < ENTRIES_PER_PAGEPAGE/2) { 6381da177e4SLinus Torvalds middir = topdir; 6391da177e4SLinus Torvalds diroff = idx/ENTRIES_PER_PAGE; 6401da177e4SLinus Torvalds } else { 6411da177e4SLinus Torvalds dir += ENTRIES_PER_PAGE/2; 6421da177e4SLinus Torvalds dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE; 6431da177e4SLinus Torvalds while (stage <= idx) 6441da177e4SLinus Torvalds stage += ENTRIES_PER_PAGEPAGE; 6451da177e4SLinus Torvalds middir = *dir; 6461da177e4SLinus Torvalds if (*dir) { 6471da177e4SLinus Torvalds diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) % 6481da177e4SLinus Torvalds ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE; 649a2646d1eSHugh Dickins if (!diroff && !offset && upper_limit >= stage) { 6501ae70006SHugh Dickins if (needs_lock) { 6511ae70006SHugh Dickins spin_lock(needs_lock); 6521ae70006SHugh Dickins *dir = NULL; 6531ae70006SHugh Dickins spin_unlock(needs_lock); 6541ae70006SHugh Dickins needs_lock = NULL; 6551ae70006SHugh Dickins } else 6561da177e4SLinus Torvalds *dir = NULL; 6571da177e4SLinus Torvalds nr_pages_to_free++; 6581da177e4SLinus Torvalds list_add(&middir->lru, &pages_to_free); 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds shmem_dir_unmap(dir); 6611da177e4SLinus Torvalds dir = shmem_dir_map(middir); 6621da177e4SLinus Torvalds } else { 6631da177e4SLinus Torvalds diroff = 0; 6641da177e4SLinus Torvalds offset = 0; 6651da177e4SLinus Torvalds idx = stage; 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) { 6701da177e4SLinus Torvalds if (unlikely(idx == stage)) { 6711da177e4SLinus Torvalds shmem_dir_unmap(dir); 6721da177e4SLinus Torvalds dir = shmem_dir_map(topdir) + 6731da177e4SLinus Torvalds ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE; 6741da177e4SLinus Torvalds while (!*dir) { 6751da177e4SLinus Torvalds dir++; 6761da177e4SLinus Torvalds idx += ENTRIES_PER_PAGEPAGE; 6771da177e4SLinus Torvalds if (idx >= limit) 6781da177e4SLinus Torvalds goto done1; 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds stage = idx + ENTRIES_PER_PAGEPAGE; 6811da177e4SLinus Torvalds middir = *dir; 6821ae70006SHugh Dickins if (punch_hole) 6831ae70006SHugh Dickins needs_lock = &info->lock; 684a2646d1eSHugh Dickins if (upper_limit >= stage) { 6851ae70006SHugh Dickins if (needs_lock) { 6861ae70006SHugh Dickins spin_lock(needs_lock); 6871ae70006SHugh Dickins *dir = NULL; 6881ae70006SHugh Dickins spin_unlock(needs_lock); 6891ae70006SHugh Dickins needs_lock = NULL; 6901ae70006SHugh Dickins } else 6911da177e4SLinus Torvalds *dir = NULL; 6921da177e4SLinus Torvalds nr_pages_to_free++; 6931da177e4SLinus Torvalds list_add(&middir->lru, &pages_to_free); 694a2646d1eSHugh Dickins } 6951da177e4SLinus Torvalds shmem_dir_unmap(dir); 6961da177e4SLinus Torvalds cond_resched(); 6971da177e4SLinus Torvalds dir = shmem_dir_map(middir); 6981da177e4SLinus Torvalds diroff = 0; 6991da177e4SLinus Torvalds } 7001ae70006SHugh Dickins punch_lock = needs_lock; 7011da177e4SLinus Torvalds subdir = dir[diroff]; 7021ae70006SHugh Dickins if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) { 7031ae70006SHugh Dickins if (needs_lock) { 7041ae70006SHugh Dickins spin_lock(needs_lock); 7051ae70006SHugh Dickins dir[diroff] = NULL; 7061ae70006SHugh Dickins spin_unlock(needs_lock); 7071ae70006SHugh Dickins punch_lock = NULL; 7081ae70006SHugh Dickins } else 7091da177e4SLinus Torvalds dir[diroff] = NULL; 7101da177e4SLinus Torvalds nr_pages_to_free++; 7111da177e4SLinus Torvalds list_add(&subdir->lru, &pages_to_free); 7121da177e4SLinus Torvalds } 7131ae70006SHugh Dickins if (subdir && page_private(subdir) /* has swap entries */) { 7141ae70006SHugh Dickins size = limit - idx; 7151ae70006SHugh Dickins if (size > ENTRIES_PER_PAGE) 7161ae70006SHugh Dickins size = ENTRIES_PER_PAGE; 7171ae70006SHugh Dickins freed = shmem_map_and_free_swp(subdir, 7181ae70006SHugh Dickins offset, size, &dir, punch_lock); 7191ae70006SHugh Dickins if (!dir) 7201ae70006SHugh Dickins dir = shmem_dir_map(middir); 7211ae70006SHugh Dickins nr_swaps_freed += freed; 7221ae70006SHugh Dickins if (offset || punch_lock) { 7231ae70006SHugh Dickins spin_lock(&info->lock); 7241ae70006SHugh Dickins set_page_private(subdir, 7251ae70006SHugh Dickins page_private(subdir) - freed); 7261ae70006SHugh Dickins spin_unlock(&info->lock); 7271ae70006SHugh Dickins } else 7281ae70006SHugh Dickins BUG_ON(page_private(subdir) != freed); 7291ae70006SHugh Dickins } 7301ae70006SHugh Dickins offset = 0; 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds done1: 7331da177e4SLinus Torvalds shmem_dir_unmap(dir); 7341da177e4SLinus Torvalds done2: 7351da177e4SLinus Torvalds if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) { 7361da177e4SLinus Torvalds /* 7371da177e4SLinus Torvalds * Call truncate_inode_pages again: racing shmem_unuse_inode 7383889e6e7Snpiggin@suse.de * may have swizzled a page in from swap since 7393889e6e7Snpiggin@suse.de * truncate_pagecache or generic_delete_inode did it, before we 7403889e6e7Snpiggin@suse.de * lowered next_index. Also, though shmem_getpage checks 7413889e6e7Snpiggin@suse.de * i_size before adding to cache, no recheck after: so fix the 7423889e6e7Snpiggin@suse.de * narrow window there too. 7431da177e4SLinus Torvalds */ 744f6b3ec23SBadari Pulavarty truncate_inode_pages_range(inode->i_mapping, start, end); 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds spin_lock(&info->lock); 7481da177e4SLinus Torvalds info->flags &= ~SHMEM_TRUNCATE; 7491da177e4SLinus Torvalds info->swapped -= nr_swaps_freed; 7501da177e4SLinus Torvalds if (nr_pages_to_free) 7511da177e4SLinus Torvalds shmem_free_blocks(inode, nr_pages_to_free); 7521da177e4SLinus Torvalds shmem_recalc_inode(inode); 7531da177e4SLinus Torvalds spin_unlock(&info->lock); 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds /* 7561da177e4SLinus Torvalds * Empty swap vector directory pages to be freed? 7571da177e4SLinus Torvalds */ 7581da177e4SLinus Torvalds if (!list_empty(&pages_to_free)) { 7591da177e4SLinus Torvalds pages_to_free.prev->next = NULL; 7601da177e4SLinus Torvalds shmem_free_pages(pages_to_free.next); 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds } 76394c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 7641da177e4SLinus Torvalds 76594c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 7661da177e4SLinus Torvalds { 7671da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 7681da177e4SLinus Torvalds int error; 7691da177e4SLinus Torvalds 770db78b877SChristoph Hellwig error = inode_change_ok(inode, attr); 771db78b877SChristoph Hellwig if (error) 772db78b877SChristoph Hellwig return error; 773db78b877SChristoph Hellwig 77494c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 77594c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 77694c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 7773889e6e7Snpiggin@suse.de struct page *page = NULL; 7783889e6e7Snpiggin@suse.de 77994c1e62dSHugh Dickins if (newsize < oldsize) { 7801da177e4SLinus Torvalds /* 7811da177e4SLinus Torvalds * If truncating down to a partial page, then 7821da177e4SLinus Torvalds * if that page is already allocated, hold it 7831da177e4SLinus Torvalds * in memory until the truncation is over, so 784ae0e47f0SJustin P. Mattock * truncate_partial_page cannot miss it were 7851da177e4SLinus Torvalds * it assigned to swap. 7861da177e4SLinus Torvalds */ 7873889e6e7Snpiggin@suse.de if (newsize & (PAGE_CACHE_SIZE-1)) { 7881da177e4SLinus Torvalds (void) shmem_getpage(inode, 7893889e6e7Snpiggin@suse.de newsize >> PAGE_CACHE_SHIFT, 7901da177e4SLinus Torvalds &page, SGP_READ, NULL); 791d3602444SHugh Dickins if (page) 792d3602444SHugh Dickins unlock_page(page); 7931da177e4SLinus Torvalds } 7941da177e4SLinus Torvalds /* 7951da177e4SLinus Torvalds * Reset SHMEM_PAGEIN flag so that shmem_truncate can 7961da177e4SLinus Torvalds * detect if any pages might have been added to cache 7971da177e4SLinus Torvalds * after truncate_inode_pages. But we needn't bother 7981da177e4SLinus Torvalds * if it's being fully truncated to zero-length: the 7991da177e4SLinus Torvalds * nrpages check is efficient enough in that case. 8001da177e4SLinus Torvalds */ 8013889e6e7Snpiggin@suse.de if (newsize) { 8021da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 8031da177e4SLinus Torvalds spin_lock(&info->lock); 8041da177e4SLinus Torvalds info->flags &= ~SHMEM_PAGEIN; 8051da177e4SLinus Torvalds spin_unlock(&info->lock); 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds } 80894c1e62dSHugh Dickins if (newsize != oldsize) { 80994c1e62dSHugh Dickins i_size_write(inode, newsize); 81094c1e62dSHugh Dickins inode->i_ctime = inode->i_mtime = CURRENT_TIME; 81194c1e62dSHugh Dickins } 81294c1e62dSHugh Dickins if (newsize < oldsize) { 81394c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 81494c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 81594c1e62dSHugh Dickins shmem_truncate_range(inode, newsize, (loff_t)-1); 81694c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 81794c1e62dSHugh Dickins unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); 81894c1e62dSHugh Dickins } 8193889e6e7Snpiggin@suse.de if (page) 8203889e6e7Snpiggin@suse.de page_cache_release(page); 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8236a1a90adSChristoph Hellwig setattr_copy(inode, attr); 82439f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 825db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 8261c7c474cSChristoph Hellwig error = generic_acl_chmod(inode); 82739f0247dSAndreas Gruenbacher #endif 8281da177e4SLinus Torvalds return error; 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds 8311f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 8321da177e4SLinus Torvalds { 8331da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 834b09e0fa4SEric Paris struct shmem_xattr *xattr, *nxattr; 8351da177e4SLinus Torvalds 8363889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 8371da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 8381da177e4SLinus Torvalds inode->i_size = 0; 8393889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 8401da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 841cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 8421da177e4SLinus Torvalds list_del_init(&info->swaplist); 843cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds } 846b09e0fa4SEric Paris 847b09e0fa4SEric Paris list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) { 848b09e0fa4SEric Paris kfree(xattr->name); 849b09e0fa4SEric Paris kfree(xattr); 850b09e0fa4SEric Paris } 8511da177e4SLinus Torvalds BUG_ON(inode->i_blocks); 8525b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 8531f895f75SAl Viro end_writeback(inode); 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir) 8571da177e4SLinus Torvalds { 8581da177e4SLinus Torvalds swp_entry_t *ptr; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds for (ptr = dir; ptr < edir; ptr++) { 8611da177e4SLinus Torvalds if (ptr->val == entry.val) 8621da177e4SLinus Torvalds return ptr - dir; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds return -1; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page) 8681da177e4SLinus Torvalds { 869778dd893SHugh Dickins struct address_space *mapping; 8701da177e4SLinus Torvalds unsigned long idx; 8711da177e4SLinus Torvalds unsigned long size; 8721da177e4SLinus Torvalds unsigned long limit; 8731da177e4SLinus Torvalds unsigned long stage; 8741da177e4SLinus Torvalds struct page **dir; 8751da177e4SLinus Torvalds struct page *subdir; 8761da177e4SLinus Torvalds swp_entry_t *ptr; 8771da177e4SLinus Torvalds int offset; 878d9fe526aSHugh Dickins int error; 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds idx = 0; 8811da177e4SLinus Torvalds ptr = info->i_direct; 8821da177e4SLinus Torvalds spin_lock(&info->lock); 8831b1b32f2SHugh Dickins if (!info->swapped) { 8841b1b32f2SHugh Dickins list_del_init(&info->swaplist); 8851b1b32f2SHugh Dickins goto lost2; 8861b1b32f2SHugh Dickins } 8871da177e4SLinus Torvalds limit = info->next_index; 8881da177e4SLinus Torvalds size = limit; 8891da177e4SLinus Torvalds if (size > SHMEM_NR_DIRECT) 8901da177e4SLinus Torvalds size = SHMEM_NR_DIRECT; 8911da177e4SLinus Torvalds offset = shmem_find_swp(entry, ptr, ptr+size); 892778dd893SHugh Dickins if (offset >= 0) { 893778dd893SHugh Dickins shmem_swp_balance_unmap(); 8941da177e4SLinus Torvalds goto found; 895778dd893SHugh Dickins } 8961da177e4SLinus Torvalds if (!info->i_indirect) 8971da177e4SLinus Torvalds goto lost2; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds dir = shmem_dir_map(info->i_indirect); 9001da177e4SLinus Torvalds stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2; 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) { 9031da177e4SLinus Torvalds if (unlikely(idx == stage)) { 9041da177e4SLinus Torvalds shmem_dir_unmap(dir-1); 905cb5f7b9aSHugh Dickins if (cond_resched_lock(&info->lock)) { 906cb5f7b9aSHugh Dickins /* check it has not been truncated */ 907cb5f7b9aSHugh Dickins if (limit > info->next_index) { 908cb5f7b9aSHugh Dickins limit = info->next_index; 909cb5f7b9aSHugh Dickins if (idx >= limit) 910cb5f7b9aSHugh Dickins goto lost2; 911cb5f7b9aSHugh Dickins } 912cb5f7b9aSHugh Dickins } 9131da177e4SLinus Torvalds dir = shmem_dir_map(info->i_indirect) + 9141da177e4SLinus Torvalds ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE; 9151da177e4SLinus Torvalds while (!*dir) { 9161da177e4SLinus Torvalds dir++; 9171da177e4SLinus Torvalds idx += ENTRIES_PER_PAGEPAGE; 9181da177e4SLinus Torvalds if (idx >= limit) 9191da177e4SLinus Torvalds goto lost1; 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds stage = idx + ENTRIES_PER_PAGEPAGE; 9221da177e4SLinus Torvalds subdir = *dir; 9231da177e4SLinus Torvalds shmem_dir_unmap(dir); 9241da177e4SLinus Torvalds dir = shmem_dir_map(subdir); 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds subdir = *dir; 9274c21e2f2SHugh Dickins if (subdir && page_private(subdir)) { 9281da177e4SLinus Torvalds ptr = shmem_swp_map(subdir); 9291da177e4SLinus Torvalds size = limit - idx; 9301da177e4SLinus Torvalds if (size > ENTRIES_PER_PAGE) 9311da177e4SLinus Torvalds size = ENTRIES_PER_PAGE; 9321da177e4SLinus Torvalds offset = shmem_find_swp(entry, ptr, ptr+size); 933e6c9366bSHugh Dickins shmem_swp_unmap(ptr); 9341da177e4SLinus Torvalds if (offset >= 0) { 9351da177e4SLinus Torvalds shmem_dir_unmap(dir); 936e6c9366bSHugh Dickins ptr = shmem_swp_map(subdir); 9371da177e4SLinus Torvalds goto found; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds lost1: 9421da177e4SLinus Torvalds shmem_dir_unmap(dir-1); 9431da177e4SLinus Torvalds lost2: 9441da177e4SLinus Torvalds spin_unlock(&info->lock); 9451da177e4SLinus Torvalds return 0; 9461da177e4SLinus Torvalds found: 9471da177e4SLinus Torvalds idx += offset; 948778dd893SHugh Dickins ptr += offset; 9492e0e26c7SHugh Dickins 9501b1b32f2SHugh Dickins /* 9511b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 9521f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 9531b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 9541b1b32f2SHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. We 9551b1b32f2SHugh Dickins * could avoid doing it if inode NULL; or use this minor optimization. 9561b1b32f2SHugh Dickins */ 9571b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 9582e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 9592e0e26c7SHugh Dickins 960d13d1443SKAMEZAWA Hiroyuki /* 961778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 962778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 963778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 964d13d1443SKAMEZAWA Hiroyuki */ 965778dd893SHugh Dickins mapping = info->vfs_inode.i_mapping; 966778dd893SHugh Dickins error = add_to_page_cache_locked(page, mapping, idx, GFP_NOWAIT); 967778dd893SHugh Dickins /* which does mem_cgroup_uncharge_cache_page on error */ 96869029cd5SKAMEZAWA Hiroyuki 969d9fe526aSHugh Dickins if (error == -EEXIST) { 970778dd893SHugh Dickins struct page *filepage = find_get_page(mapping, idx); 9712e0e26c7SHugh Dickins error = 1; 972d9fe526aSHugh Dickins if (filepage) { 973d9fe526aSHugh Dickins /* 974d9fe526aSHugh Dickins * There might be a more uptodate page coming down 975d9fe526aSHugh Dickins * from a stacked writepage: forget our swappage if so. 976d9fe526aSHugh Dickins */ 977d9fe526aSHugh Dickins if (PageUptodate(filepage)) 978d9fe526aSHugh Dickins error = 0; 979d9fe526aSHugh Dickins page_cache_release(filepage); 980d9fe526aSHugh Dickins } 981d9fe526aSHugh Dickins } 982d9fe526aSHugh Dickins if (!error) { 98373b1262fSHugh Dickins delete_from_swap_cache(page); 98473b1262fSHugh Dickins set_page_dirty(page); 9851da177e4SLinus Torvalds info->flags |= SHMEM_PAGEIN; 9862e0e26c7SHugh Dickins shmem_swp_set(info, ptr, 0); 9872e0e26c7SHugh Dickins swap_free(entry); 9882e0e26c7SHugh Dickins error = 1; /* not an error, but entry was found */ 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds shmem_swp_unmap(ptr); 9911da177e4SLinus Torvalds spin_unlock(&info->lock); 9922e0e26c7SHugh Dickins return error; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds /* 9961da177e4SLinus Torvalds * shmem_unuse() search for an eventually swapped out shmem page. 9971da177e4SLinus Torvalds */ 9981da177e4SLinus Torvalds int shmem_unuse(swp_entry_t entry, struct page *page) 9991da177e4SLinus Torvalds { 10001da177e4SLinus Torvalds struct list_head *p, *next; 10011da177e4SLinus Torvalds struct shmem_inode_info *info; 10021da177e4SLinus Torvalds int found = 0; 1003778dd893SHugh Dickins int error; 1004778dd893SHugh Dickins 1005778dd893SHugh Dickins /* 1006778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 1007778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 1008778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 1009778dd893SHugh Dickins * add_to_page_cache() will be called with GFP_NOWAIT. 1010778dd893SHugh Dickins */ 1011778dd893SHugh Dickins error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL); 1012778dd893SHugh Dickins if (error) 1013778dd893SHugh Dickins goto out; 1014778dd893SHugh Dickins /* 1015778dd893SHugh Dickins * Try to preload while we can wait, to not make a habit of 1016778dd893SHugh Dickins * draining atomic reserves; but don't latch on to this cpu, 1017778dd893SHugh Dickins * it's okay if sometimes we get rescheduled after this. 1018778dd893SHugh Dickins */ 1019778dd893SHugh Dickins error = radix_tree_preload(GFP_KERNEL); 1020778dd893SHugh Dickins if (error) 1021778dd893SHugh Dickins goto uncharge; 1022778dd893SHugh Dickins radix_tree_preload_end(); 10231da177e4SLinus Torvalds 1024cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 10251da177e4SLinus Torvalds list_for_each_safe(p, next, &shmem_swaplist) { 10261da177e4SLinus Torvalds info = list_entry(p, struct shmem_inode_info, swaplist); 10272e0e26c7SHugh Dickins found = shmem_unuse_inode(info, entry, page); 1028cb5f7b9aSHugh Dickins cond_resched(); 10292e0e26c7SHugh Dickins if (found) 1030778dd893SHugh Dickins break; 10311da177e4SLinus Torvalds } 1032cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1033778dd893SHugh Dickins 1034778dd893SHugh Dickins uncharge: 1035778dd893SHugh Dickins if (!found) 1036778dd893SHugh Dickins mem_cgroup_uncharge_cache_page(page); 1037778dd893SHugh Dickins if (found < 0) 1038778dd893SHugh Dickins error = found; 1039778dd893SHugh Dickins out: 1040aaa46865SHugh Dickins unlock_page(page); 1041aaa46865SHugh Dickins page_cache_release(page); 1042778dd893SHugh Dickins return error; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds /* 10461da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 10471da177e4SLinus Torvalds */ 10481da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 10491da177e4SLinus Torvalds { 10501da177e4SLinus Torvalds struct shmem_inode_info *info; 10511da177e4SLinus Torvalds swp_entry_t *entry, swap; 10521da177e4SLinus Torvalds struct address_space *mapping; 10531da177e4SLinus Torvalds unsigned long index; 10541da177e4SLinus Torvalds struct inode *inode; 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 10571da177e4SLinus Torvalds mapping = page->mapping; 10581da177e4SLinus Torvalds index = page->index; 10591da177e4SLinus Torvalds inode = mapping->host; 10601da177e4SLinus Torvalds info = SHMEM_I(inode); 10611da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 10621da177e4SLinus Torvalds goto redirty; 1063d9fe526aSHugh Dickins if (!total_swap_pages) 10641da177e4SLinus Torvalds goto redirty; 10651da177e4SLinus Torvalds 1066d9fe526aSHugh Dickins /* 1067d9fe526aSHugh Dickins * shmem_backing_dev_info's capabilities prevent regular writeback or 1068d9fe526aSHugh Dickins * sync from ever calling shmem_writepage; but a stacking filesystem 1069d9fe526aSHugh Dickins * may use the ->writepage of its underlying filesystem, in which case 1070d9fe526aSHugh Dickins * tmpfs should write out to swap only in response to memory pressure, 10715b0830cbSJens Axboe * and not for the writeback threads or sync. However, in those cases, 10725b0830cbSJens Axboe * we do still want to check if there's a redundant swappage to be 10735b0830cbSJens Axboe * discarded. 1074d9fe526aSHugh Dickins */ 1075d9fe526aSHugh Dickins if (wbc->for_reclaim) 1076d9fe526aSHugh Dickins swap = get_swap_page(); 1077d9fe526aSHugh Dickins else 1078d9fe526aSHugh Dickins swap.val = 0; 1079d9fe526aSHugh Dickins 1080b1dea800SHugh Dickins /* 1081b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 1082b1dea800SHugh Dickins * if it's not already there. Do it now because we cannot take 1083b1dea800SHugh Dickins * mutex while holding spinlock, and must do so before the page 1084b1dea800SHugh Dickins * is moved to swap cache, when its pagelock no longer protects 1085b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 1086b1dea800SHugh Dickins * we've taken the spinlock, because shmem_unuse_inode() will 1087b1dea800SHugh Dickins * prune a !swapped inode from the swaplist under both locks. 1088b1dea800SHugh Dickins */ 108905bf86b4SHugh Dickins if (swap.val) { 1090b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 109105bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 109205bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 1093b1dea800SHugh Dickins } 1094b1dea800SHugh Dickins 10951da177e4SLinus Torvalds spin_lock(&info->lock); 109605bf86b4SHugh Dickins if (swap.val) 1097b1dea800SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1098b1dea800SHugh Dickins 10991da177e4SLinus Torvalds if (index >= info->next_index) { 11001da177e4SLinus Torvalds BUG_ON(!(info->flags & SHMEM_TRUNCATE)); 11011da177e4SLinus Torvalds goto unlock; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds entry = shmem_swp_entry(info, index, NULL); 1104d9fe526aSHugh Dickins if (entry->val) { 1105d9fe526aSHugh Dickins /* 1106d9fe526aSHugh Dickins * The more uptodate page coming down from a stacked 1107d9fe526aSHugh Dickins * writepage should replace our old swappage. 1108d9fe526aSHugh Dickins */ 1109d9fe526aSHugh Dickins free_swap_and_cache(*entry); 1110d9fe526aSHugh Dickins shmem_swp_set(info, entry, 0); 1111d9fe526aSHugh Dickins } 1112d9fe526aSHugh Dickins shmem_recalc_inode(inode); 11131da177e4SLinus Torvalds 1114d9fe526aSHugh Dickins if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 11154c73b1bcSMinchan Kim delete_from_page_cache(page); 11161da177e4SLinus Torvalds shmem_swp_set(info, entry, swap.val); 11171da177e4SLinus Torvalds shmem_swp_unmap(entry); 1118aaa46865SHugh Dickins swap_shmem_alloc(swap); 1119826267cfSHugh Dickins spin_unlock(&info->lock); 1120d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 11219fab5619SHugh Dickins swap_writepage(page, wbc); 11221da177e4SLinus Torvalds return 0; 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds shmem_swp_unmap(entry); 11261da177e4SLinus Torvalds unlock: 11271da177e4SLinus Torvalds spin_unlock(&info->lock); 11282ca4532aSDaisuke Nishimura /* 11292ca4532aSDaisuke Nishimura * add_to_swap_cache() doesn't return -EEXIST, so we can safely 11302ca4532aSDaisuke Nishimura * clear SWAP_HAS_CACHE flag. 11312ca4532aSDaisuke Nishimura */ 1132cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, NULL); 11331da177e4SLinus Torvalds redirty: 11341da177e4SLinus Torvalds set_page_dirty(page); 1135d9fe526aSHugh Dickins if (wbc->for_reclaim) 1136d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 1137d9fe526aSHugh Dickins unlock_page(page); 1138d9fe526aSHugh Dickins return 0; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1142680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 114371fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1144680d794bSakpm@linux-foundation.org { 1145680d794bSakpm@linux-foundation.org char buffer[64]; 1146680d794bSakpm@linux-foundation.org 114771fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1148095f1fc4SLee Schermerhorn return; /* show nothing */ 1149095f1fc4SLee Schermerhorn 115071fe804bSLee Schermerhorn mpol_to_str(buffer, sizeof(buffer), mpol, 1); 1151095f1fc4SLee Schermerhorn 1152095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1153680d794bSakpm@linux-foundation.org } 115471fe804bSLee Schermerhorn 115571fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 115671fe804bSLee Schermerhorn { 115771fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 115871fe804bSLee Schermerhorn if (sbinfo->mpol) { 115971fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 116071fe804bSLee Schermerhorn mpol = sbinfo->mpol; 116171fe804bSLee Schermerhorn mpol_get(mpol); 116271fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 116371fe804bSLee Schermerhorn } 116471fe804bSLee Schermerhorn return mpol; 116571fe804bSLee Schermerhorn } 1166680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 1167680d794bSakpm@linux-foundation.org 116802098feaSHugh Dickins static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp, 116902098feaSHugh Dickins struct shmem_inode_info *info, unsigned long idx) 11701da177e4SLinus Torvalds { 117152cd3b07SLee Schermerhorn struct mempolicy mpol, *spol; 11721da177e4SLinus Torvalds struct vm_area_struct pvma; 1173c4cc6d07SHugh Dickins struct page *page; 11741da177e4SLinus Torvalds 117552cd3b07SLee Schermerhorn spol = mpol_cond_copy(&mpol, 117652cd3b07SLee Schermerhorn mpol_shared_policy_lookup(&info->policy, idx)); 117752cd3b07SLee Schermerhorn 11781da177e4SLinus Torvalds /* Create a pseudo vma that just contains the policy */ 1179c4cc6d07SHugh Dickins pvma.vm_start = 0; 11801da177e4SLinus Torvalds pvma.vm_pgoff = idx; 1181c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 118252cd3b07SLee Schermerhorn pvma.vm_policy = spol; 118302098feaSHugh Dickins page = swapin_readahead(entry, gfp, &pvma, 0); 11841da177e4SLinus Torvalds return page; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 118702098feaSHugh Dickins static struct page *shmem_alloc_page(gfp_t gfp, 118802098feaSHugh Dickins struct shmem_inode_info *info, unsigned long idx) 11891da177e4SLinus Torvalds { 11901da177e4SLinus Torvalds struct vm_area_struct pvma; 11911da177e4SLinus Torvalds 1192c4cc6d07SHugh Dickins /* Create a pseudo vma that just contains the policy */ 1193c4cc6d07SHugh Dickins pvma.vm_start = 0; 11941da177e4SLinus Torvalds pvma.vm_pgoff = idx; 1195c4cc6d07SHugh Dickins pvma.vm_ops = NULL; 1196c4cc6d07SHugh Dickins pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx); 119752cd3b07SLee Schermerhorn 119852cd3b07SLee Schermerhorn /* 119952cd3b07SLee Schermerhorn * alloc_page_vma() will drop the shared policy reference 120052cd3b07SLee Schermerhorn */ 120152cd3b07SLee Schermerhorn return alloc_page_vma(gfp, &pvma, 0); 12021da177e4SLinus Torvalds } 1203680d794bSakpm@linux-foundation.org #else /* !CONFIG_NUMA */ 1204680d794bSakpm@linux-foundation.org #ifdef CONFIG_TMPFS 120571fe804bSLee Schermerhorn static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p) 1206680d794bSakpm@linux-foundation.org { 1207680d794bSakpm@linux-foundation.org } 1208680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 1209680d794bSakpm@linux-foundation.org 121002098feaSHugh Dickins static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp, 121102098feaSHugh Dickins struct shmem_inode_info *info, unsigned long idx) 12121da177e4SLinus Torvalds { 121302098feaSHugh Dickins return swapin_readahead(entry, gfp, NULL, 0); 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 121602098feaSHugh Dickins static inline struct page *shmem_alloc_page(gfp_t gfp, 121702098feaSHugh Dickins struct shmem_inode_info *info, unsigned long idx) 12181da177e4SLinus Torvalds { 1219e84e2e13SHugh Dickins return alloc_page(gfp); 12201da177e4SLinus Torvalds } 1221680d794bSakpm@linux-foundation.org #endif /* CONFIG_NUMA */ 12221da177e4SLinus Torvalds 122371fe804bSLee Schermerhorn #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) 122471fe804bSLee Schermerhorn static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 122571fe804bSLee Schermerhorn { 122671fe804bSLee Schermerhorn return NULL; 122771fe804bSLee Schermerhorn } 122871fe804bSLee Schermerhorn #endif 122971fe804bSLee Schermerhorn 12301da177e4SLinus Torvalds /* 12311da177e4SLinus Torvalds * shmem_getpage - either get the page from swap or allocate a new one 12321da177e4SLinus Torvalds * 12331da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 12341da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 12351da177e4SLinus Torvalds * entry since a page cannot live in both the swap and page cache 12361da177e4SLinus Torvalds */ 12371da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx, 12381da177e4SLinus Torvalds struct page **pagep, enum sgp_type sgp, int *type) 12391da177e4SLinus Torvalds { 12401da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 12411da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 12421da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 12431da177e4SLinus Torvalds struct page *filepage = *pagep; 12441da177e4SLinus Torvalds struct page *swappage; 1245ff36b801SShaohua Li struct page *prealloc_page = NULL; 12461da177e4SLinus Torvalds swp_entry_t *entry; 12471da177e4SLinus Torvalds swp_entry_t swap; 124802098feaSHugh Dickins gfp_t gfp; 12491da177e4SLinus Torvalds int error; 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds if (idx >= SHMEM_MAX_INDEX) 12521da177e4SLinus Torvalds return -EFBIG; 125354cb8821SNick Piggin 125454cb8821SNick Piggin if (type) 125583c54070SNick Piggin *type = 0; 125654cb8821SNick Piggin 12571da177e4SLinus Torvalds /* 12581da177e4SLinus Torvalds * Normally, filepage is NULL on entry, and either found 12591da177e4SLinus Torvalds * uptodate immediately, or allocated and zeroed, or read 12601da177e4SLinus Torvalds * in under swappage, which is then assigned to filepage. 12615402b976SHugh Dickins * But shmem_readpage (required for splice) passes in a locked 1262ae976416SHugh Dickins * filepage, which may be found not uptodate by other callers 1263ae976416SHugh Dickins * too, and may need to be copied from the swappage read in. 12641da177e4SLinus Torvalds */ 12651da177e4SLinus Torvalds repeat: 12661da177e4SLinus Torvalds if (!filepage) 12671da177e4SLinus Torvalds filepage = find_lock_page(mapping, idx); 12681da177e4SLinus Torvalds if (filepage && PageUptodate(filepage)) 12691da177e4SLinus Torvalds goto done; 127002098feaSHugh Dickins gfp = mapping_gfp_mask(mapping); 1271b409f9fcSHugh Dickins if (!filepage) { 1272b409f9fcSHugh Dickins /* 1273b409f9fcSHugh Dickins * Try to preload while we can wait, to not make a habit of 1274b409f9fcSHugh Dickins * draining atomic reserves; but don't latch on to this cpu. 1275b409f9fcSHugh Dickins */ 1276b409f9fcSHugh Dickins error = radix_tree_preload(gfp & ~__GFP_HIGHMEM); 1277b409f9fcSHugh Dickins if (error) 1278b409f9fcSHugh Dickins goto failed; 1279b409f9fcSHugh Dickins radix_tree_preload_end(); 1280ff36b801SShaohua Li if (sgp != SGP_READ && !prealloc_page) { 1281ff36b801SShaohua Li /* We don't care if this fails */ 1282ff36b801SShaohua Li prealloc_page = shmem_alloc_page(gfp, info, idx); 1283ff36b801SShaohua Li if (prealloc_page) { 1284ff36b801SShaohua Li if (mem_cgroup_cache_charge(prealloc_page, 1285ff36b801SShaohua Li current->mm, GFP_KERNEL)) { 1286ff36b801SShaohua Li page_cache_release(prealloc_page); 1287ff36b801SShaohua Li prealloc_page = NULL; 1288b409f9fcSHugh Dickins } 1289ff36b801SShaohua Li } 1290ff36b801SShaohua Li } 1291ff36b801SShaohua Li } 1292ff36b801SShaohua Li error = 0; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds spin_lock(&info->lock); 12951da177e4SLinus Torvalds shmem_recalc_inode(inode); 12961da177e4SLinus Torvalds entry = shmem_swp_alloc(info, idx, sgp); 12971da177e4SLinus Torvalds if (IS_ERR(entry)) { 12981da177e4SLinus Torvalds spin_unlock(&info->lock); 12991da177e4SLinus Torvalds error = PTR_ERR(entry); 13001da177e4SLinus Torvalds goto failed; 13011da177e4SLinus Torvalds } 13021da177e4SLinus Torvalds swap = *entry; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds if (swap.val) { 13051da177e4SLinus Torvalds /* Look it up and read it in.. */ 13061da177e4SLinus Torvalds swappage = lookup_swap_cache(swap); 13071da177e4SLinus Torvalds if (!swappage) { 13081da177e4SLinus Torvalds shmem_swp_unmap(entry); 1309f8891e5eSChristoph Lameter spin_unlock(&info->lock); 1310456f998eSYing Han /* here we actually do the io */ 1311456f998eSYing Han if (type) 1312456f998eSYing Han *type |= VM_FAULT_MAJOR; 131302098feaSHugh Dickins swappage = shmem_swapin(swap, gfp, info, idx); 13141da177e4SLinus Torvalds if (!swappage) { 13151da177e4SLinus Torvalds spin_lock(&info->lock); 13161da177e4SLinus Torvalds entry = shmem_swp_alloc(info, idx, sgp); 13171da177e4SLinus Torvalds if (IS_ERR(entry)) 13181da177e4SLinus Torvalds error = PTR_ERR(entry); 13191da177e4SLinus Torvalds else { 13201da177e4SLinus Torvalds if (entry->val == swap.val) 13211da177e4SLinus Torvalds error = -ENOMEM; 13221da177e4SLinus Torvalds shmem_swp_unmap(entry); 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds spin_unlock(&info->lock); 13251da177e4SLinus Torvalds if (error) 13261da177e4SLinus Torvalds goto failed; 13271da177e4SLinus Torvalds goto repeat; 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds wait_on_page_locked(swappage); 13301da177e4SLinus Torvalds page_cache_release(swappage); 13311da177e4SLinus Torvalds goto repeat; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 1335529ae9aaSNick Piggin if (!trylock_page(swappage)) { 13361da177e4SLinus Torvalds shmem_swp_unmap(entry); 13371da177e4SLinus Torvalds spin_unlock(&info->lock); 13381da177e4SLinus Torvalds wait_on_page_locked(swappage); 13391da177e4SLinus Torvalds page_cache_release(swappage); 13401da177e4SLinus Torvalds goto repeat; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds if (PageWriteback(swappage)) { 13431da177e4SLinus Torvalds shmem_swp_unmap(entry); 13441da177e4SLinus Torvalds spin_unlock(&info->lock); 13451da177e4SLinus Torvalds wait_on_page_writeback(swappage); 13461da177e4SLinus Torvalds unlock_page(swappage); 13471da177e4SLinus Torvalds page_cache_release(swappage); 13481da177e4SLinus Torvalds goto repeat; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds if (!PageUptodate(swappage)) { 13511da177e4SLinus Torvalds shmem_swp_unmap(entry); 13521da177e4SLinus Torvalds spin_unlock(&info->lock); 13531da177e4SLinus Torvalds unlock_page(swappage); 13541da177e4SLinus Torvalds page_cache_release(swappage); 13551da177e4SLinus Torvalds error = -EIO; 13561da177e4SLinus Torvalds goto failed; 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds if (filepage) { 13601da177e4SLinus Torvalds shmem_swp_set(info, entry, 0); 13611da177e4SLinus Torvalds shmem_swp_unmap(entry); 13621da177e4SLinus Torvalds delete_from_swap_cache(swappage); 13631da177e4SLinus Torvalds spin_unlock(&info->lock); 13641da177e4SLinus Torvalds copy_highpage(filepage, swappage); 13651da177e4SLinus Torvalds unlock_page(swappage); 13661da177e4SLinus Torvalds page_cache_release(swappage); 13671da177e4SLinus Torvalds flush_dcache_page(filepage); 13681da177e4SLinus Torvalds SetPageUptodate(filepage); 13691da177e4SLinus Torvalds set_page_dirty(filepage); 13701da177e4SLinus Torvalds swap_free(swap); 1371e286781dSNick Piggin } else if (!(error = add_to_page_cache_locked(swappage, mapping, 1372e286781dSNick Piggin idx, GFP_NOWAIT))) { 13731da177e4SLinus Torvalds info->flags |= SHMEM_PAGEIN; 13741da177e4SLinus Torvalds shmem_swp_set(info, entry, 0); 13751da177e4SLinus Torvalds shmem_swp_unmap(entry); 137673b1262fSHugh Dickins delete_from_swap_cache(swappage); 13771da177e4SLinus Torvalds spin_unlock(&info->lock); 13781da177e4SLinus Torvalds filepage = swappage; 137973b1262fSHugh Dickins set_page_dirty(filepage); 13801da177e4SLinus Torvalds swap_free(swap); 13811da177e4SLinus Torvalds } else { 13821da177e4SLinus Torvalds shmem_swp_unmap(entry); 13831da177e4SLinus Torvalds spin_unlock(&info->lock); 138482369553SHugh Dickins if (error == -ENOMEM) { 1385ae3abae6SDaisuke Nishimura /* 1386ae3abae6SDaisuke Nishimura * reclaim from proper memory cgroup and 1387ae3abae6SDaisuke Nishimura * call memcg's OOM if needed. 1388ae3abae6SDaisuke Nishimura */ 1389ae3abae6SDaisuke Nishimura error = mem_cgroup_shmem_charge_fallback( 1390ae3abae6SDaisuke Nishimura swappage, 1391b5a84319SKAMEZAWA Hiroyuki current->mm, 1392c9b0ed51SKAMEZAWA Hiroyuki gfp); 1393b5a84319SKAMEZAWA Hiroyuki if (error) { 1394b5a84319SKAMEZAWA Hiroyuki unlock_page(swappage); 1395b5a84319SKAMEZAWA Hiroyuki page_cache_release(swappage); 139682369553SHugh Dickins goto failed; 139782369553SHugh Dickins } 1398b5a84319SKAMEZAWA Hiroyuki } 1399b5a84319SKAMEZAWA Hiroyuki unlock_page(swappage); 1400b5a84319SKAMEZAWA Hiroyuki page_cache_release(swappage); 14011da177e4SLinus Torvalds goto repeat; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds } else if (sgp == SGP_READ && !filepage) { 14041da177e4SLinus Torvalds shmem_swp_unmap(entry); 14051da177e4SLinus Torvalds filepage = find_get_page(mapping, idx); 14061da177e4SLinus Torvalds if (filepage && 1407529ae9aaSNick Piggin (!PageUptodate(filepage) || !trylock_page(filepage))) { 14081da177e4SLinus Torvalds spin_unlock(&info->lock); 14091da177e4SLinus Torvalds wait_on_page_locked(filepage); 14101da177e4SLinus Torvalds page_cache_release(filepage); 14111da177e4SLinus Torvalds filepage = NULL; 14121da177e4SLinus Torvalds goto repeat; 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds spin_unlock(&info->lock); 14151da177e4SLinus Torvalds } else { 14161da177e4SLinus Torvalds shmem_swp_unmap(entry); 14171da177e4SLinus Torvalds sbinfo = SHMEM_SB(inode->i_sb); 14180edd73b3SHugh Dickins if (sbinfo->max_blocks) { 1419fc5da22aSHugh Dickins if (percpu_counter_compare(&sbinfo->used_blocks, 1420fc5da22aSHugh Dickins sbinfo->max_blocks) >= 0 || 142159a16eadSHugh Dickins shmem_acct_block(info->flags)) 142259a16eadSHugh Dickins goto nospace; 14237e496299STim Chen percpu_counter_inc(&sbinfo->used_blocks); 14247e496299STim Chen spin_lock(&inode->i_lock); 14251da177e4SLinus Torvalds inode->i_blocks += BLOCKS_PER_PAGE; 14267e496299STim Chen spin_unlock(&inode->i_lock); 142759a16eadSHugh Dickins } else if (shmem_acct_block(info->flags)) 142859a16eadSHugh Dickins goto nospace; 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds if (!filepage) { 143169029cd5SKAMEZAWA Hiroyuki int ret; 143269029cd5SKAMEZAWA Hiroyuki 1433ff36b801SShaohua Li if (!prealloc_page) { 14341da177e4SLinus Torvalds spin_unlock(&info->lock); 143502098feaSHugh Dickins filepage = shmem_alloc_page(gfp, info, idx); 14361da177e4SLinus Torvalds if (!filepage) { 14371da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, 1); 14381da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 14391da177e4SLinus Torvalds error = -ENOMEM; 14401da177e4SLinus Torvalds goto failed; 14411da177e4SLinus Torvalds } 1442b2e18538SRik van Riel SetPageSwapBacked(filepage); 14431da177e4SLinus Torvalds 1444ff36b801SShaohua Li /* 1445ff36b801SShaohua Li * Precharge page while we can wait, compensate 1446ff36b801SShaohua Li * after 1447ff36b801SShaohua Li */ 1448ff36b801SShaohua Li error = mem_cgroup_cache_charge(filepage, 1449ff36b801SShaohua Li current->mm, GFP_KERNEL); 145082369553SHugh Dickins if (error) { 145182369553SHugh Dickins page_cache_release(filepage); 145282369553SHugh Dickins shmem_unacct_blocks(info->flags, 1); 145382369553SHugh Dickins shmem_free_blocks(inode, 1); 145482369553SHugh Dickins filepage = NULL; 145582369553SHugh Dickins goto failed; 145682369553SHugh Dickins } 145782369553SHugh Dickins 14581da177e4SLinus Torvalds spin_lock(&info->lock); 1459ff36b801SShaohua Li } else { 1460ff36b801SShaohua Li filepage = prealloc_page; 1461ff36b801SShaohua Li prealloc_page = NULL; 1462ff36b801SShaohua Li SetPageSwapBacked(filepage); 1463ff36b801SShaohua Li } 1464ff36b801SShaohua Li 14651da177e4SLinus Torvalds entry = shmem_swp_alloc(info, idx, sgp); 14661da177e4SLinus Torvalds if (IS_ERR(entry)) 14671da177e4SLinus Torvalds error = PTR_ERR(entry); 14681da177e4SLinus Torvalds else { 14691da177e4SLinus Torvalds swap = *entry; 14701da177e4SLinus Torvalds shmem_swp_unmap(entry); 14711da177e4SLinus Torvalds } 147269029cd5SKAMEZAWA Hiroyuki ret = error || swap.val; 147369029cd5SKAMEZAWA Hiroyuki if (ret) 147469029cd5SKAMEZAWA Hiroyuki mem_cgroup_uncharge_cache_page(filepage); 147569029cd5SKAMEZAWA Hiroyuki else 147669029cd5SKAMEZAWA Hiroyuki ret = add_to_page_cache_lru(filepage, mapping, 147769029cd5SKAMEZAWA Hiroyuki idx, GFP_NOWAIT); 147869029cd5SKAMEZAWA Hiroyuki /* 147969029cd5SKAMEZAWA Hiroyuki * At add_to_page_cache_lru() failure, uncharge will 148069029cd5SKAMEZAWA Hiroyuki * be done automatically. 148169029cd5SKAMEZAWA Hiroyuki */ 148269029cd5SKAMEZAWA Hiroyuki if (ret) { 14831da177e4SLinus Torvalds spin_unlock(&info->lock); 14841da177e4SLinus Torvalds page_cache_release(filepage); 14851da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, 1); 14861da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 14871da177e4SLinus Torvalds filepage = NULL; 14881da177e4SLinus Torvalds if (error) 14891da177e4SLinus Torvalds goto failed; 14901da177e4SLinus Torvalds goto repeat; 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds info->flags |= SHMEM_PAGEIN; 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds info->alloced++; 14961da177e4SLinus Torvalds spin_unlock(&info->lock); 1497e84e2e13SHugh Dickins clear_highpage(filepage); 14981da177e4SLinus Torvalds flush_dcache_page(filepage); 14991da177e4SLinus Torvalds SetPageUptodate(filepage); 1500a0ee5ec5SHugh Dickins if (sgp == SGP_DIRTY) 1501a0ee5ec5SHugh Dickins set_page_dirty(filepage); 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds done: 15041da177e4SLinus Torvalds *pagep = filepage; 1505ff36b801SShaohua Li error = 0; 1506ff36b801SShaohua Li goto out; 15071da177e4SLinus Torvalds 150859a16eadSHugh Dickins nospace: 150959a16eadSHugh Dickins /* 151059a16eadSHugh Dickins * Perhaps the page was brought in from swap between find_lock_page 151159a16eadSHugh Dickins * and taking info->lock? We allow for that at add_to_page_cache_lru, 151259a16eadSHugh Dickins * but must also avoid reporting a spurious ENOSPC while working on a 151359a16eadSHugh Dickins * full tmpfs. (When filepage has been passed in to shmem_getpage, it 151459a16eadSHugh Dickins * is already in page cache, which prevents this race from occurring.) 151559a16eadSHugh Dickins */ 151659a16eadSHugh Dickins if (!filepage) { 151759a16eadSHugh Dickins struct page *page = find_get_page(mapping, idx); 151859a16eadSHugh Dickins if (page) { 151959a16eadSHugh Dickins spin_unlock(&info->lock); 152059a16eadSHugh Dickins page_cache_release(page); 152159a16eadSHugh Dickins goto repeat; 152259a16eadSHugh Dickins } 152359a16eadSHugh Dickins } 152459a16eadSHugh Dickins spin_unlock(&info->lock); 152559a16eadSHugh Dickins error = -ENOSPC; 15261da177e4SLinus Torvalds failed: 15271da177e4SLinus Torvalds if (*pagep != filepage) { 15281da177e4SLinus Torvalds unlock_page(filepage); 15291da177e4SLinus Torvalds page_cache_release(filepage); 15301da177e4SLinus Torvalds } 1531ff36b801SShaohua Li out: 1532ff36b801SShaohua Li if (prealloc_page) { 1533ff36b801SShaohua Li mem_cgroup_uncharge_cache_page(prealloc_page); 1534ff36b801SShaohua Li page_cache_release(prealloc_page); 1535ff36b801SShaohua Li } 15361da177e4SLinus Torvalds return error; 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds 1539d0217ac0SNick Piggin static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 15401da177e4SLinus Torvalds { 1541d3ac7f89SJosef "Jeff" Sipek struct inode *inode = vma->vm_file->f_path.dentry->d_inode; 15421da177e4SLinus Torvalds int error; 1543d0217ac0SNick Piggin int ret; 15441da177e4SLinus Torvalds 1545d0217ac0SNick Piggin if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 1546d0217ac0SNick Piggin return VM_FAULT_SIGBUS; 1547d00806b1SNick Piggin 154827d54b39SHugh Dickins error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); 1549d0217ac0SNick Piggin if (error) 1550d0217ac0SNick Piggin return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 1551456f998eSYing Han if (ret & VM_FAULT_MAJOR) { 1552456f998eSYing Han count_vm_event(PGMAJFAULT); 1553456f998eSYing Han mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); 1554456f998eSYing Han } 155583c54070SNick Piggin return ret | VM_FAULT_LOCKED; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds #ifdef CONFIG_NUMA 1559d8dc74f2SAdrian Bunk static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new) 15601da177e4SLinus Torvalds { 1561d3ac7f89SJosef "Jeff" Sipek struct inode *i = vma->vm_file->f_path.dentry->d_inode; 15621da177e4SLinus Torvalds return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new); 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 1565d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 1566d8dc74f2SAdrian Bunk unsigned long addr) 15671da177e4SLinus Torvalds { 1568d3ac7f89SJosef "Jeff" Sipek struct inode *i = vma->vm_file->f_path.dentry->d_inode; 15691da177e4SLinus Torvalds unsigned long idx; 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 15721da177e4SLinus Torvalds return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx); 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds #endif 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 15771da177e4SLinus Torvalds { 1578d3ac7f89SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 15791da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 15801da177e4SLinus Torvalds int retval = -ENOMEM; 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds spin_lock(&info->lock); 15831da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 15841da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 15851da177e4SLinus Torvalds goto out_nomem; 15861da177e4SLinus Torvalds info->flags |= VM_LOCKED; 158789e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 15901da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 15911da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 159289e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 159389e004eaSLee Schermerhorn scan_mapping_unevictable_pages(file->f_mapping); 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds retval = 0; 159689e004eaSLee Schermerhorn 15971da177e4SLinus Torvalds out_nomem: 15981da177e4SLinus Torvalds spin_unlock(&info->lock); 15991da177e4SLinus Torvalds return retval; 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds 16029b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 16031da177e4SLinus Torvalds { 16041da177e4SLinus Torvalds file_accessed(file); 16051da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 1606d0217ac0SNick Piggin vma->vm_flags |= VM_CAN_NONLINEAR; 16071da177e4SLinus Torvalds return 0; 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 1610454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 1611454abafeSDmitry Monakhov int mode, dev_t dev, unsigned long flags) 16121da177e4SLinus Torvalds { 16131da177e4SLinus Torvalds struct inode *inode; 16141da177e4SLinus Torvalds struct shmem_inode_info *info; 16151da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 16161da177e4SLinus Torvalds 16175b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 16181da177e4SLinus Torvalds return NULL; 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds inode = new_inode(sb); 16211da177e4SLinus Torvalds if (inode) { 162285fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 1623454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 16241da177e4SLinus Torvalds inode->i_blocks = 0; 16251da177e4SLinus Torvalds inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; 16261da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 162791828a40SDavid M. Grimes inode->i_generation = get_seconds(); 16281da177e4SLinus Torvalds info = SHMEM_I(inode); 16291da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 16301da177e4SLinus Torvalds spin_lock_init(&info->lock); 16310b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 16321da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 1633b09e0fa4SEric Paris INIT_LIST_HEAD(&info->xattr_list); 163472c04902SAl Viro cache_no_acl(inode); 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds switch (mode & S_IFMT) { 16371da177e4SLinus Torvalds default: 163839f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 16391da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 16401da177e4SLinus Torvalds break; 16411da177e4SLinus Torvalds case S_IFREG: 164214fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 16431da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 16441da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 164571fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 164671fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 16471da177e4SLinus Torvalds break; 16481da177e4SLinus Torvalds case S_IFDIR: 1649d8c76e6fSDave Hansen inc_nlink(inode); 16501da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 16511da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 16521da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 16531da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 16541da177e4SLinus Torvalds break; 16551da177e4SLinus Torvalds case S_IFLNK: 16561da177e4SLinus Torvalds /* 16571da177e4SLinus Torvalds * Must not load anything in the rbtree, 16581da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 16591da177e4SLinus Torvalds */ 166071fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 16611da177e4SLinus Torvalds break; 16621da177e4SLinus Torvalds } 16635b04c689SPavel Emelyanov } else 16645b04c689SPavel Emelyanov shmem_free_inode(sb); 16651da177e4SLinus Torvalds return inode; 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 166992e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 167092e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations; 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds /* 1673800d15a5SNick Piggin * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin; 1674ae976416SHugh Dickins * but providing them allows a tmpfs file to be used for splice, sendfile, and 1675ae976416SHugh Dickins * below the loop driver, in the generic fashion that many filesystems support. 16761da177e4SLinus Torvalds */ 1677ae976416SHugh Dickins static int shmem_readpage(struct file *file, struct page *page) 1678ae976416SHugh Dickins { 1679ae976416SHugh Dickins struct inode *inode = page->mapping->host; 1680ae976416SHugh Dickins int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL); 1681ae976416SHugh Dickins unlock_page(page); 1682ae976416SHugh Dickins return error; 1683ae976416SHugh Dickins } 1684ae976416SHugh Dickins 16851da177e4SLinus Torvalds static int 1686800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 1687800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 1688800d15a5SNick Piggin struct page **pagep, void **fsdata) 16891da177e4SLinus Torvalds { 1690800d15a5SNick Piggin struct inode *inode = mapping->host; 1691800d15a5SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 1692800d15a5SNick Piggin *pagep = NULL; 1693800d15a5SNick Piggin return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); 1694800d15a5SNick Piggin } 1695800d15a5SNick Piggin 1696800d15a5SNick Piggin static int 1697800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 1698800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 1699800d15a5SNick Piggin struct page *page, void *fsdata) 1700800d15a5SNick Piggin { 1701800d15a5SNick Piggin struct inode *inode = mapping->host; 1702800d15a5SNick Piggin 1703800d15a5SNick Piggin if (pos + copied > inode->i_size) 1704800d15a5SNick Piggin i_size_write(inode, pos + copied); 1705800d15a5SNick Piggin 1706d3602444SHugh Dickins set_page_dirty(page); 17076746aff7SWu Fengguang unlock_page(page); 1708d3602444SHugh Dickins page_cache_release(page); 1709d3602444SHugh Dickins 1710800d15a5SNick Piggin return copied; 17111da177e4SLinus Torvalds } 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) 17141da177e4SLinus Torvalds { 1715d3ac7f89SJosef "Jeff" Sipek struct inode *inode = filp->f_path.dentry->d_inode; 17161da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 17171da177e4SLinus Torvalds unsigned long index, offset; 1718a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 1719a0ee5ec5SHugh Dickins 1720a0ee5ec5SHugh Dickins /* 1721a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 1722a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 1723a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 1724a0ee5ec5SHugh Dickins */ 1725a0ee5ec5SHugh Dickins if (segment_eq(get_fs(), KERNEL_DS)) 1726a0ee5ec5SHugh Dickins sgp = SGP_DIRTY; 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds index = *ppos >> PAGE_CACHE_SHIFT; 17291da177e4SLinus Torvalds offset = *ppos & ~PAGE_CACHE_MASK; 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds for (;;) { 17321da177e4SLinus Torvalds struct page *page = NULL; 17331da177e4SLinus Torvalds unsigned long end_index, nr, ret; 17341da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 17371da177e4SLinus Torvalds if (index > end_index) 17381da177e4SLinus Torvalds break; 17391da177e4SLinus Torvalds if (index == end_index) { 17401da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 17411da177e4SLinus Torvalds if (nr <= offset) 17421da177e4SLinus Torvalds break; 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 1745a0ee5ec5SHugh Dickins desc->error = shmem_getpage(inode, index, &page, sgp, NULL); 17461da177e4SLinus Torvalds if (desc->error) { 17471da177e4SLinus Torvalds if (desc->error == -EINVAL) 17481da177e4SLinus Torvalds desc->error = 0; 17491da177e4SLinus Torvalds break; 17501da177e4SLinus Torvalds } 1751d3602444SHugh Dickins if (page) 1752d3602444SHugh Dickins unlock_page(page); 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /* 17551da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 17561b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 17571da177e4SLinus Torvalds */ 17581da177e4SLinus Torvalds nr = PAGE_CACHE_SIZE; 17591da177e4SLinus Torvalds i_size = i_size_read(inode); 17601da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 17611da177e4SLinus Torvalds if (index == end_index) { 17621da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 17631da177e4SLinus Torvalds if (nr <= offset) { 17641da177e4SLinus Torvalds if (page) 17651da177e4SLinus Torvalds page_cache_release(page); 17661da177e4SLinus Torvalds break; 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds nr -= offset; 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds if (page) { 17721da177e4SLinus Torvalds /* 17731da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 17741da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 17751da177e4SLinus Torvalds * before reading the page on the kernel side. 17761da177e4SLinus Torvalds */ 17771da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 17781da177e4SLinus Torvalds flush_dcache_page(page); 17791da177e4SLinus Torvalds /* 17801da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 17811da177e4SLinus Torvalds */ 17821da177e4SLinus Torvalds if (!offset) 17831da177e4SLinus Torvalds mark_page_accessed(page); 1784b5810039SNick Piggin } else { 17851da177e4SLinus Torvalds page = ZERO_PAGE(0); 1786b5810039SNick Piggin page_cache_get(page); 1787b5810039SNick Piggin } 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds /* 17901da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 17911da177e4SLinus Torvalds * now we can copy it to user space... 17921da177e4SLinus Torvalds * 17931da177e4SLinus Torvalds * The actor routine returns how many bytes were actually used.. 17941da177e4SLinus Torvalds * NOTE! This may not be the same as how much of a user buffer 17951da177e4SLinus Torvalds * we filled up (we may be padding etc), so we can only update 17961da177e4SLinus Torvalds * "pos" here (the actor routine has to update the user buffer 17971da177e4SLinus Torvalds * pointers and the remaining count). 17981da177e4SLinus Torvalds */ 17991da177e4SLinus Torvalds ret = actor(desc, page, offset, nr); 18001da177e4SLinus Torvalds offset += ret; 18011da177e4SLinus Torvalds index += offset >> PAGE_CACHE_SHIFT; 18021da177e4SLinus Torvalds offset &= ~PAGE_CACHE_MASK; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds page_cache_release(page); 18051da177e4SLinus Torvalds if (ret != nr || !desc->count) 18061da177e4SLinus Torvalds break; 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds cond_resched(); 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; 18121da177e4SLinus Torvalds file_accessed(filp); 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds 1815bcd78e49SHugh Dickins static ssize_t shmem_file_aio_read(struct kiocb *iocb, 1816bcd78e49SHugh Dickins const struct iovec *iov, unsigned long nr_segs, loff_t pos) 18171da177e4SLinus Torvalds { 1818bcd78e49SHugh Dickins struct file *filp = iocb->ki_filp; 1819bcd78e49SHugh Dickins ssize_t retval; 1820bcd78e49SHugh Dickins unsigned long seg; 1821bcd78e49SHugh Dickins size_t count; 1822bcd78e49SHugh Dickins loff_t *ppos = &iocb->ki_pos; 1823bcd78e49SHugh Dickins 1824bcd78e49SHugh Dickins retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); 1825bcd78e49SHugh Dickins if (retval) 1826bcd78e49SHugh Dickins return retval; 1827bcd78e49SHugh Dickins 1828bcd78e49SHugh Dickins for (seg = 0; seg < nr_segs; seg++) { 18291da177e4SLinus Torvalds read_descriptor_t desc; 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds desc.written = 0; 1832bcd78e49SHugh Dickins desc.arg.buf = iov[seg].iov_base; 1833bcd78e49SHugh Dickins desc.count = iov[seg].iov_len; 1834bcd78e49SHugh Dickins if (desc.count == 0) 1835bcd78e49SHugh Dickins continue; 18361da177e4SLinus Torvalds desc.error = 0; 18371da177e4SLinus Torvalds do_shmem_file_read(filp, ppos, &desc, file_read_actor); 1838bcd78e49SHugh Dickins retval += desc.written; 1839bcd78e49SHugh Dickins if (desc.error) { 1840bcd78e49SHugh Dickins retval = retval ?: desc.error; 1841bcd78e49SHugh Dickins break; 1842bcd78e49SHugh Dickins } 1843bcd78e49SHugh Dickins if (desc.count > 0) 1844bcd78e49SHugh Dickins break; 1845bcd78e49SHugh Dickins } 1846bcd78e49SHugh Dickins return retval; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 1849726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 18501da177e4SLinus Torvalds { 1851726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 18541da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 18551da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 18560edd73b3SHugh Dickins if (sbinfo->max_blocks) { 18571da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 18587e496299STim Chen buf->f_bavail = buf->f_bfree = 18597e496299STim Chen sbinfo->max_blocks - percpu_counter_sum(&sbinfo->used_blocks); 18600edd73b3SHugh Dickins } 18610edd73b3SHugh Dickins if (sbinfo->max_inodes) { 18621da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 18631da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 18641da177e4SLinus Torvalds } 18651da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 18661da177e4SLinus Torvalds return 0; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds /* 18701da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 18711da177e4SLinus Torvalds */ 18721da177e4SLinus Torvalds static int 18731da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 18741da177e4SLinus Torvalds { 18750b0a0806SHugh Dickins struct inode *inode; 18761da177e4SLinus Torvalds int error = -ENOSPC; 18771da177e4SLinus Torvalds 1878454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 18791da177e4SLinus Torvalds if (inode) { 18802a7dba39SEric Paris error = security_inode_init_security(inode, dir, 1881*9d8f13baSMimi Zohar &dentry->d_name, 18822a7dba39SEric Paris NULL, NULL); 1883570bc1c2SStephen Smalley if (error) { 1884570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1885570bc1c2SStephen Smalley iput(inode); 1886570bc1c2SStephen Smalley return error; 1887570bc1c2SStephen Smalley } 188839f0247dSAndreas Gruenbacher } 18891c7c474cSChristoph Hellwig #ifdef CONFIG_TMPFS_POSIX_ACL 18901c7c474cSChristoph Hellwig error = generic_acl_init(inode, dir); 189139f0247dSAndreas Gruenbacher if (error) { 189239f0247dSAndreas Gruenbacher iput(inode); 189339f0247dSAndreas Gruenbacher return error; 1894570bc1c2SStephen Smalley } 1895718deb6bSAl Viro #else 1896718deb6bSAl Viro error = 0; 18971c7c474cSChristoph Hellwig #endif 18981da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 18991da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 19001da177e4SLinus Torvalds d_instantiate(dentry, inode); 19011da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds return error; 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode) 19071da177e4SLinus Torvalds { 19081da177e4SLinus Torvalds int error; 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 19111da177e4SLinus Torvalds return error; 1912d8c76e6fSDave Hansen inc_nlink(dir); 19131da177e4SLinus Torvalds return 0; 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode, 19171da177e4SLinus Torvalds struct nameidata *nd) 19181da177e4SLinus Torvalds { 19191da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds /* 19231da177e4SLinus Torvalds * Link a file.. 19241da177e4SLinus Torvalds */ 19251da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 19261da177e4SLinus Torvalds { 19271da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 19285b04c689SPavel Emelyanov int ret; 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds /* 19311da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 19321da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 19331da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 19341da177e4SLinus Torvalds */ 19355b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 19365b04c689SPavel Emelyanov if (ret) 19375b04c689SPavel Emelyanov goto out; 19381da177e4SLinus Torvalds 19391da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 19401da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 1941d8c76e6fSDave Hansen inc_nlink(inode); 19427de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 19431da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 19441da177e4SLinus Torvalds d_instantiate(dentry, inode); 19455b04c689SPavel Emelyanov out: 19465b04c689SPavel Emelyanov return ret; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 19501da177e4SLinus Torvalds { 19511da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 19521da177e4SLinus Torvalds 19535b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 19545b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 19571da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 19589a53c3a7SDave Hansen drop_nlink(inode); 19591da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 19601da177e4SLinus Torvalds return 0; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 19641da177e4SLinus Torvalds { 19651da177e4SLinus Torvalds if (!simple_empty(dentry)) 19661da177e4SLinus Torvalds return -ENOTEMPTY; 19671da177e4SLinus Torvalds 19689a53c3a7SDave Hansen drop_nlink(dentry->d_inode); 19699a53c3a7SDave Hansen drop_nlink(dir); 19701da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 19711da177e4SLinus Torvalds } 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds /* 19741da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 19751da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 19761da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 19771da177e4SLinus Torvalds * gets overwritten. 19781da177e4SLinus Torvalds */ 19791da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 19801da177e4SLinus Torvalds { 19811da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 19821da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 19851da177e4SLinus Torvalds return -ENOTEMPTY; 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds if (new_dentry->d_inode) { 19881da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 19891da177e4SLinus Torvalds if (they_are_dirs) 19909a53c3a7SDave Hansen drop_nlink(old_dir); 19911da177e4SLinus Torvalds } else if (they_are_dirs) { 19929a53c3a7SDave Hansen drop_nlink(old_dir); 1993d8c76e6fSDave Hansen inc_nlink(new_dir); 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 19971da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 19981da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 19991da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 20001da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 20011da177e4SLinus Torvalds return 0; 20021da177e4SLinus Torvalds } 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 20051da177e4SLinus Torvalds { 20061da177e4SLinus Torvalds int error; 20071da177e4SLinus Torvalds int len; 20081da177e4SLinus Torvalds struct inode *inode; 20091da177e4SLinus Torvalds struct page *page = NULL; 20101da177e4SLinus Torvalds char *kaddr; 20111da177e4SLinus Torvalds struct shmem_inode_info *info; 20121da177e4SLinus Torvalds 20131da177e4SLinus Torvalds len = strlen(symname) + 1; 20141da177e4SLinus Torvalds if (len > PAGE_CACHE_SIZE) 20151da177e4SLinus Torvalds return -ENAMETOOLONG; 20161da177e4SLinus Torvalds 2017454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 20181da177e4SLinus Torvalds if (!inode) 20191da177e4SLinus Torvalds return -ENOSPC; 20201da177e4SLinus Torvalds 2021*9d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 20222a7dba39SEric Paris NULL, NULL); 2023570bc1c2SStephen Smalley if (error) { 2024570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 2025570bc1c2SStephen Smalley iput(inode); 2026570bc1c2SStephen Smalley return error; 2027570bc1c2SStephen Smalley } 2028570bc1c2SStephen Smalley error = 0; 2029570bc1c2SStephen Smalley } 2030570bc1c2SStephen Smalley 20311da177e4SLinus Torvalds info = SHMEM_I(inode); 20321da177e4SLinus Torvalds inode->i_size = len-1; 2033b09e0fa4SEric Paris if (len <= SHMEM_SYMLINK_INLINE_LEN) { 20341da177e4SLinus Torvalds /* do it inline */ 2035b09e0fa4SEric Paris memcpy(info->inline_symlink, symname, len); 20361da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inline_operations; 20371da177e4SLinus Torvalds } else { 20381da177e4SLinus Torvalds error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); 20391da177e4SLinus Torvalds if (error) { 20401da177e4SLinus Torvalds iput(inode); 20411da177e4SLinus Torvalds return error; 20421da177e4SLinus Torvalds } 204314fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 20441da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 20451da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 20461da177e4SLinus Torvalds memcpy(kaddr, symname, len); 20471da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 20481da177e4SLinus Torvalds set_page_dirty(page); 20496746aff7SWu Fengguang unlock_page(page); 20501da177e4SLinus Torvalds page_cache_release(page); 20511da177e4SLinus Torvalds } 20521da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 20531da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 20541da177e4SLinus Torvalds d_instantiate(dentry, inode); 20551da177e4SLinus Torvalds dget(dentry); 20561da177e4SLinus Torvalds return 0; 20571da177e4SLinus Torvalds } 20581da177e4SLinus Torvalds 2059cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd) 20601da177e4SLinus Torvalds { 2061b09e0fa4SEric Paris nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink); 2062cc314eefSLinus Torvalds return NULL; 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 2065cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) 20661da177e4SLinus Torvalds { 20671da177e4SLinus Torvalds struct page *page = NULL; 20681da177e4SLinus Torvalds int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); 20691da177e4SLinus Torvalds nd_set_link(nd, res ? ERR_PTR(res) : kmap(page)); 2070d3602444SHugh Dickins if (page) 2071d3602444SHugh Dickins unlock_page(page); 2072cc314eefSLinus Torvalds return page; 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 2075cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 20761da177e4SLinus Torvalds { 20771da177e4SLinus Torvalds if (!IS_ERR(nd_get_link(nd))) { 2078cc314eefSLinus Torvalds struct page *page = cookie; 20791da177e4SLinus Torvalds kunmap(page); 20801da177e4SLinus Torvalds mark_page_accessed(page); 20811da177e4SLinus Torvalds page_cache_release(page); 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds } 20841da177e4SLinus Torvalds 2085b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2086b09e0fa4SEric Paris /* 2087b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 2088b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 2089b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 2090b09e0fa4SEric Paris * filesystem level, though. 2091b09e0fa4SEric Paris */ 2092b09e0fa4SEric Paris 2093b09e0fa4SEric Paris static int shmem_xattr_get(struct dentry *dentry, const char *name, 2094b09e0fa4SEric Paris void *buffer, size_t size) 2095b09e0fa4SEric Paris { 2096b09e0fa4SEric Paris struct shmem_inode_info *info; 2097b09e0fa4SEric Paris struct shmem_xattr *xattr; 2098b09e0fa4SEric Paris int ret = -ENODATA; 2099b09e0fa4SEric Paris 2100b09e0fa4SEric Paris info = SHMEM_I(dentry->d_inode); 2101b09e0fa4SEric Paris 2102b09e0fa4SEric Paris spin_lock(&info->lock); 2103b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 2104b09e0fa4SEric Paris if (strcmp(name, xattr->name)) 2105b09e0fa4SEric Paris continue; 2106b09e0fa4SEric Paris 2107b09e0fa4SEric Paris ret = xattr->size; 2108b09e0fa4SEric Paris if (buffer) { 2109b09e0fa4SEric Paris if (size < xattr->size) 2110b09e0fa4SEric Paris ret = -ERANGE; 2111b09e0fa4SEric Paris else 2112b09e0fa4SEric Paris memcpy(buffer, xattr->value, xattr->size); 2113b09e0fa4SEric Paris } 2114b09e0fa4SEric Paris break; 2115b09e0fa4SEric Paris } 2116b09e0fa4SEric Paris spin_unlock(&info->lock); 2117b09e0fa4SEric Paris return ret; 2118b09e0fa4SEric Paris } 2119b09e0fa4SEric Paris 2120b09e0fa4SEric Paris static int shmem_xattr_set(struct dentry *dentry, const char *name, 2121b09e0fa4SEric Paris const void *value, size_t size, int flags) 2122b09e0fa4SEric Paris { 2123b09e0fa4SEric Paris struct inode *inode = dentry->d_inode; 2124b09e0fa4SEric Paris struct shmem_inode_info *info = SHMEM_I(inode); 2125b09e0fa4SEric Paris struct shmem_xattr *xattr; 2126b09e0fa4SEric Paris struct shmem_xattr *new_xattr = NULL; 2127b09e0fa4SEric Paris size_t len; 2128b09e0fa4SEric Paris int err = 0; 2129b09e0fa4SEric Paris 2130b09e0fa4SEric Paris /* value == NULL means remove */ 2131b09e0fa4SEric Paris if (value) { 2132b09e0fa4SEric Paris /* wrap around? */ 2133b09e0fa4SEric Paris len = sizeof(*new_xattr) + size; 2134b09e0fa4SEric Paris if (len <= sizeof(*new_xattr)) 2135b09e0fa4SEric Paris return -ENOMEM; 2136b09e0fa4SEric Paris 2137b09e0fa4SEric Paris new_xattr = kmalloc(len, GFP_KERNEL); 2138b09e0fa4SEric Paris if (!new_xattr) 2139b09e0fa4SEric Paris return -ENOMEM; 2140b09e0fa4SEric Paris 2141b09e0fa4SEric Paris new_xattr->name = kstrdup(name, GFP_KERNEL); 2142b09e0fa4SEric Paris if (!new_xattr->name) { 2143b09e0fa4SEric Paris kfree(new_xattr); 2144b09e0fa4SEric Paris return -ENOMEM; 2145b09e0fa4SEric Paris } 2146b09e0fa4SEric Paris 2147b09e0fa4SEric Paris new_xattr->size = size; 2148b09e0fa4SEric Paris memcpy(new_xattr->value, value, size); 2149b09e0fa4SEric Paris } 2150b09e0fa4SEric Paris 2151b09e0fa4SEric Paris spin_lock(&info->lock); 2152b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 2153b09e0fa4SEric Paris if (!strcmp(name, xattr->name)) { 2154b09e0fa4SEric Paris if (flags & XATTR_CREATE) { 2155b09e0fa4SEric Paris xattr = new_xattr; 2156b09e0fa4SEric Paris err = -EEXIST; 2157b09e0fa4SEric Paris } else if (new_xattr) { 2158b09e0fa4SEric Paris list_replace(&xattr->list, &new_xattr->list); 2159b09e0fa4SEric Paris } else { 2160b09e0fa4SEric Paris list_del(&xattr->list); 2161b09e0fa4SEric Paris } 2162b09e0fa4SEric Paris goto out; 2163b09e0fa4SEric Paris } 2164b09e0fa4SEric Paris } 2165b09e0fa4SEric Paris if (flags & XATTR_REPLACE) { 2166b09e0fa4SEric Paris xattr = new_xattr; 2167b09e0fa4SEric Paris err = -ENODATA; 2168b09e0fa4SEric Paris } else { 2169b09e0fa4SEric Paris list_add(&new_xattr->list, &info->xattr_list); 2170b09e0fa4SEric Paris xattr = NULL; 2171b09e0fa4SEric Paris } 2172b09e0fa4SEric Paris out: 2173b09e0fa4SEric Paris spin_unlock(&info->lock); 2174b09e0fa4SEric Paris if (xattr) 2175b09e0fa4SEric Paris kfree(xattr->name); 2176b09e0fa4SEric Paris kfree(xattr); 2177b09e0fa4SEric Paris return err; 2178b09e0fa4SEric Paris } 2179b09e0fa4SEric Paris 2180b09e0fa4SEric Paris 2181b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 2182b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 2183b09e0fa4SEric Paris &generic_acl_access_handler, 2184b09e0fa4SEric Paris &generic_acl_default_handler, 2185b09e0fa4SEric Paris #endif 2186b09e0fa4SEric Paris NULL 2187b09e0fa4SEric Paris }; 2188b09e0fa4SEric Paris 2189b09e0fa4SEric Paris static int shmem_xattr_validate(const char *name) 2190b09e0fa4SEric Paris { 2191b09e0fa4SEric Paris struct { const char *prefix; size_t len; } arr[] = { 2192b09e0fa4SEric Paris { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, 2193b09e0fa4SEric Paris { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } 2194b09e0fa4SEric Paris }; 2195b09e0fa4SEric Paris int i; 2196b09e0fa4SEric Paris 2197b09e0fa4SEric Paris for (i = 0; i < ARRAY_SIZE(arr); i++) { 2198b09e0fa4SEric Paris size_t preflen = arr[i].len; 2199b09e0fa4SEric Paris if (strncmp(name, arr[i].prefix, preflen) == 0) { 2200b09e0fa4SEric Paris if (!name[preflen]) 2201b09e0fa4SEric Paris return -EINVAL; 2202b09e0fa4SEric Paris return 0; 2203b09e0fa4SEric Paris } 2204b09e0fa4SEric Paris } 2205b09e0fa4SEric Paris return -EOPNOTSUPP; 2206b09e0fa4SEric Paris } 2207b09e0fa4SEric Paris 2208b09e0fa4SEric Paris static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, 2209b09e0fa4SEric Paris void *buffer, size_t size) 2210b09e0fa4SEric Paris { 2211b09e0fa4SEric Paris int err; 2212b09e0fa4SEric Paris 2213b09e0fa4SEric Paris /* 2214b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2215b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2216b09e0fa4SEric Paris * for it via sb->s_xattr. 2217b09e0fa4SEric Paris */ 2218b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2219b09e0fa4SEric Paris return generic_getxattr(dentry, name, buffer, size); 2220b09e0fa4SEric Paris 2221b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2222b09e0fa4SEric Paris if (err) 2223b09e0fa4SEric Paris return err; 2224b09e0fa4SEric Paris 2225b09e0fa4SEric Paris return shmem_xattr_get(dentry, name, buffer, size); 2226b09e0fa4SEric Paris } 2227b09e0fa4SEric Paris 2228b09e0fa4SEric Paris static int shmem_setxattr(struct dentry *dentry, const char *name, 2229b09e0fa4SEric Paris const void *value, size_t size, int flags) 2230b09e0fa4SEric Paris { 2231b09e0fa4SEric Paris int err; 2232b09e0fa4SEric Paris 2233b09e0fa4SEric Paris /* 2234b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2235b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2236b09e0fa4SEric Paris * for it via sb->s_xattr. 2237b09e0fa4SEric Paris */ 2238b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2239b09e0fa4SEric Paris return generic_setxattr(dentry, name, value, size, flags); 2240b09e0fa4SEric Paris 2241b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2242b09e0fa4SEric Paris if (err) 2243b09e0fa4SEric Paris return err; 2244b09e0fa4SEric Paris 2245b09e0fa4SEric Paris if (size == 0) 2246b09e0fa4SEric Paris value = ""; /* empty EA, do not remove */ 2247b09e0fa4SEric Paris 2248b09e0fa4SEric Paris return shmem_xattr_set(dentry, name, value, size, flags); 2249b09e0fa4SEric Paris 2250b09e0fa4SEric Paris } 2251b09e0fa4SEric Paris 2252b09e0fa4SEric Paris static int shmem_removexattr(struct dentry *dentry, const char *name) 2253b09e0fa4SEric Paris { 2254b09e0fa4SEric Paris int err; 2255b09e0fa4SEric Paris 2256b09e0fa4SEric Paris /* 2257b09e0fa4SEric Paris * If this is a request for a synthetic attribute in the system.* 2258b09e0fa4SEric Paris * namespace use the generic infrastructure to resolve a handler 2259b09e0fa4SEric Paris * for it via sb->s_xattr. 2260b09e0fa4SEric Paris */ 2261b09e0fa4SEric Paris if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) 2262b09e0fa4SEric Paris return generic_removexattr(dentry, name); 2263b09e0fa4SEric Paris 2264b09e0fa4SEric Paris err = shmem_xattr_validate(name); 2265b09e0fa4SEric Paris if (err) 2266b09e0fa4SEric Paris return err; 2267b09e0fa4SEric Paris 2268b09e0fa4SEric Paris return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE); 2269b09e0fa4SEric Paris } 2270b09e0fa4SEric Paris 2271b09e0fa4SEric Paris static bool xattr_is_trusted(const char *name) 2272b09e0fa4SEric Paris { 2273b09e0fa4SEric Paris return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN); 2274b09e0fa4SEric Paris } 2275b09e0fa4SEric Paris 2276b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 2277b09e0fa4SEric Paris { 2278b09e0fa4SEric Paris bool trusted = capable(CAP_SYS_ADMIN); 2279b09e0fa4SEric Paris struct shmem_xattr *xattr; 2280b09e0fa4SEric Paris struct shmem_inode_info *info; 2281b09e0fa4SEric Paris size_t used = 0; 2282b09e0fa4SEric Paris 2283b09e0fa4SEric Paris info = SHMEM_I(dentry->d_inode); 2284b09e0fa4SEric Paris 2285b09e0fa4SEric Paris spin_lock(&info->lock); 2286b09e0fa4SEric Paris list_for_each_entry(xattr, &info->xattr_list, list) { 2287b09e0fa4SEric Paris size_t len; 2288b09e0fa4SEric Paris 2289b09e0fa4SEric Paris /* skip "trusted." attributes for unprivileged callers */ 2290b09e0fa4SEric Paris if (!trusted && xattr_is_trusted(xattr->name)) 2291b09e0fa4SEric Paris continue; 2292b09e0fa4SEric Paris 2293b09e0fa4SEric Paris len = strlen(xattr->name) + 1; 2294b09e0fa4SEric Paris used += len; 2295b09e0fa4SEric Paris if (buffer) { 2296b09e0fa4SEric Paris if (size < used) { 2297b09e0fa4SEric Paris used = -ERANGE; 2298b09e0fa4SEric Paris break; 2299b09e0fa4SEric Paris } 2300b09e0fa4SEric Paris memcpy(buffer, xattr->name, len); 2301b09e0fa4SEric Paris buffer += len; 2302b09e0fa4SEric Paris } 2303b09e0fa4SEric Paris } 2304b09e0fa4SEric Paris spin_unlock(&info->lock); 2305b09e0fa4SEric Paris 2306b09e0fa4SEric Paris return used; 2307b09e0fa4SEric Paris } 2308b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 2309b09e0fa4SEric Paris 231092e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inline_operations = { 23111da177e4SLinus Torvalds .readlink = generic_readlink, 23121da177e4SLinus Torvalds .follow_link = shmem_follow_link_inline, 2313b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2314b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2315b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2316b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2317b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2318b09e0fa4SEric Paris #endif 23191da177e4SLinus Torvalds }; 23201da177e4SLinus Torvalds 232192e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 23221da177e4SLinus Torvalds .readlink = generic_readlink, 23231da177e4SLinus Torvalds .follow_link = shmem_follow_link, 23241da177e4SLinus Torvalds .put_link = shmem_put_link, 2325b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2326b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2327b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2328b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2329b09e0fa4SEric Paris .removexattr = shmem_removexattr, 233039f0247dSAndreas Gruenbacher #endif 2331b09e0fa4SEric Paris }; 233239f0247dSAndreas Gruenbacher 233391828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 233491828a40SDavid M. Grimes { 233591828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 233691828a40SDavid M. Grimes } 233791828a40SDavid M. Grimes 233891828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 233991828a40SDavid M. Grimes { 234091828a40SDavid M. Grimes __u32 *fh = vfh; 234191828a40SDavid M. Grimes __u64 inum = fh[2]; 234291828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 234391828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 234491828a40SDavid M. Grimes } 234591828a40SDavid M. Grimes 2346480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 2347480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 234891828a40SDavid M. Grimes { 234991828a40SDavid M. Grimes struct inode *inode; 2350480b116cSChristoph Hellwig struct dentry *dentry = NULL; 2351480b116cSChristoph Hellwig u64 inum = fid->raw[2]; 2352480b116cSChristoph Hellwig inum = (inum << 32) | fid->raw[1]; 235391828a40SDavid M. Grimes 2354480b116cSChristoph Hellwig if (fh_len < 3) 2355480b116cSChristoph Hellwig return NULL; 2356480b116cSChristoph Hellwig 2357480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 2358480b116cSChristoph Hellwig shmem_match, fid->raw); 235991828a40SDavid M. Grimes if (inode) { 2360480b116cSChristoph Hellwig dentry = d_find_alias(inode); 236191828a40SDavid M. Grimes iput(inode); 236291828a40SDavid M. Grimes } 236391828a40SDavid M. Grimes 2364480b116cSChristoph Hellwig return dentry; 236591828a40SDavid M. Grimes } 236691828a40SDavid M. Grimes 236791828a40SDavid M. Grimes static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len, 236891828a40SDavid M. Grimes int connectable) 236991828a40SDavid M. Grimes { 237091828a40SDavid M. Grimes struct inode *inode = dentry->d_inode; 237191828a40SDavid M. Grimes 23725fe0c237SAneesh Kumar K.V if (*len < 3) { 23735fe0c237SAneesh Kumar K.V *len = 3; 237491828a40SDavid M. Grimes return 255; 23755fe0c237SAneesh Kumar K.V } 237691828a40SDavid M. Grimes 23771d3382cbSAl Viro if (inode_unhashed(inode)) { 237891828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 237991828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 238091828a40SDavid M. Grimes * time, we need a lock to ensure we only try 238191828a40SDavid M. Grimes * to do it once 238291828a40SDavid M. Grimes */ 238391828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 238491828a40SDavid M. Grimes spin_lock(&lock); 23851d3382cbSAl Viro if (inode_unhashed(inode)) 238691828a40SDavid M. Grimes __insert_inode_hash(inode, 238791828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 238891828a40SDavid M. Grimes spin_unlock(&lock); 238991828a40SDavid M. Grimes } 239091828a40SDavid M. Grimes 239191828a40SDavid M. Grimes fh[0] = inode->i_generation; 239291828a40SDavid M. Grimes fh[1] = inode->i_ino; 239391828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 239491828a40SDavid M. Grimes 239591828a40SDavid M. Grimes *len = 3; 239691828a40SDavid M. Grimes return 1; 239791828a40SDavid M. Grimes } 239891828a40SDavid M. Grimes 239939655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 240091828a40SDavid M. Grimes .get_parent = shmem_get_parent, 240191828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 2402480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 240391828a40SDavid M. Grimes }; 240491828a40SDavid M. Grimes 2405680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 2406680d794bSakpm@linux-foundation.org bool remount) 24071da177e4SLinus Torvalds { 24081da177e4SLinus Torvalds char *this_char, *value, *rest; 24091da177e4SLinus Torvalds 2410b00dc3adSHugh Dickins while (options != NULL) { 2411b00dc3adSHugh Dickins this_char = options; 2412b00dc3adSHugh Dickins for (;;) { 2413b00dc3adSHugh Dickins /* 2414b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 2415b00dc3adSHugh Dickins * mount options form a comma-separated list, 2416b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 2417b00dc3adSHugh Dickins */ 2418b00dc3adSHugh Dickins options = strchr(options, ','); 2419b00dc3adSHugh Dickins if (options == NULL) 2420b00dc3adSHugh Dickins break; 2421b00dc3adSHugh Dickins options++; 2422b00dc3adSHugh Dickins if (!isdigit(*options)) { 2423b00dc3adSHugh Dickins options[-1] = '\0'; 2424b00dc3adSHugh Dickins break; 2425b00dc3adSHugh Dickins } 2426b00dc3adSHugh Dickins } 24271da177e4SLinus Torvalds if (!*this_char) 24281da177e4SLinus Torvalds continue; 24291da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 24301da177e4SLinus Torvalds *value++ = 0; 24311da177e4SLinus Torvalds } else { 24321da177e4SLinus Torvalds printk(KERN_ERR 24331da177e4SLinus Torvalds "tmpfs: No value for mount option '%s'\n", 24341da177e4SLinus Torvalds this_char); 24351da177e4SLinus Torvalds return 1; 24361da177e4SLinus Torvalds } 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 24391da177e4SLinus Torvalds unsigned long long size; 24401da177e4SLinus Torvalds size = memparse(value,&rest); 24411da177e4SLinus Torvalds if (*rest == '%') { 24421da177e4SLinus Torvalds size <<= PAGE_SHIFT; 24431da177e4SLinus Torvalds size *= totalram_pages; 24441da177e4SLinus Torvalds do_div(size, 100); 24451da177e4SLinus Torvalds rest++; 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds if (*rest) 24481da177e4SLinus Torvalds goto bad_val; 2449680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 2450680d794bSakpm@linux-foundation.org DIV_ROUND_UP(size, PAGE_CACHE_SIZE); 24511da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 2452680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 24531da177e4SLinus Torvalds if (*rest) 24541da177e4SLinus Torvalds goto bad_val; 24551da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 2456680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 24571da177e4SLinus Torvalds if (*rest) 24581da177e4SLinus Torvalds goto bad_val; 24591da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 2460680d794bSakpm@linux-foundation.org if (remount) 24611da177e4SLinus Torvalds continue; 2462680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 24631da177e4SLinus Torvalds if (*rest) 24641da177e4SLinus Torvalds goto bad_val; 24651da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 2466680d794bSakpm@linux-foundation.org if (remount) 24671da177e4SLinus Torvalds continue; 2468680d794bSakpm@linux-foundation.org sbinfo->uid = simple_strtoul(value, &rest, 0); 24691da177e4SLinus Torvalds if (*rest) 24701da177e4SLinus Torvalds goto bad_val; 24711da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 2472680d794bSakpm@linux-foundation.org if (remount) 24731da177e4SLinus Torvalds continue; 2474680d794bSakpm@linux-foundation.org sbinfo->gid = simple_strtoul(value, &rest, 0); 24751da177e4SLinus Torvalds if (*rest) 24761da177e4SLinus Torvalds goto bad_val; 24777339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 247871fe804bSLee Schermerhorn if (mpol_parse_str(value, &sbinfo->mpol, 1)) 24797339ff83SRobin Holt goto bad_val; 24801da177e4SLinus Torvalds } else { 24811da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad mount option %s\n", 24821da177e4SLinus Torvalds this_char); 24831da177e4SLinus Torvalds return 1; 24841da177e4SLinus Torvalds } 24851da177e4SLinus Torvalds } 24861da177e4SLinus Torvalds return 0; 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds bad_val: 24891da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", 24901da177e4SLinus Torvalds value, this_char); 24911da177e4SLinus Torvalds return 1; 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds } 24941da177e4SLinus Torvalds 24951da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 24961da177e4SLinus Torvalds { 24971da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2498680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 24990edd73b3SHugh Dickins unsigned long inodes; 25000edd73b3SHugh Dickins int error = -EINVAL; 25011da177e4SLinus Torvalds 2502680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 25030edd73b3SHugh Dickins return error; 25040edd73b3SHugh Dickins 25050edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 25060edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 25077e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 25080edd73b3SHugh Dickins goto out; 2509680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 25100edd73b3SHugh Dickins goto out; 25110edd73b3SHugh Dickins /* 25120edd73b3SHugh Dickins * Those tests also disallow limited->unlimited while any are in 25130edd73b3SHugh Dickins * use, so i_blocks will always be zero when max_blocks is zero; 25140edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 25150edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 25160edd73b3SHugh Dickins */ 2517680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 25180edd73b3SHugh Dickins goto out; 2519680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 25200edd73b3SHugh Dickins goto out; 25210edd73b3SHugh Dickins 25220edd73b3SHugh Dickins error = 0; 2523680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 2524680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 2525680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 252671fe804bSLee Schermerhorn 252771fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 252871fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 25290edd73b3SHugh Dickins out: 25300edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 25310edd73b3SHugh Dickins return error; 25321da177e4SLinus Torvalds } 2533680d794bSakpm@linux-foundation.org 2534680d794bSakpm@linux-foundation.org static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs) 2535680d794bSakpm@linux-foundation.org { 2536680d794bSakpm@linux-foundation.org struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb); 2537680d794bSakpm@linux-foundation.org 2538680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 2539680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 2540680d794bSakpm@linux-foundation.org sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); 2541680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 2542680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 2543680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 2544680d794bSakpm@linux-foundation.org seq_printf(seq, ",mode=%03o", sbinfo->mode); 2545680d794bSakpm@linux-foundation.org if (sbinfo->uid != 0) 2546680d794bSakpm@linux-foundation.org seq_printf(seq, ",uid=%u", sbinfo->uid); 2547680d794bSakpm@linux-foundation.org if (sbinfo->gid != 0) 2548680d794bSakpm@linux-foundation.org seq_printf(seq, ",gid=%u", sbinfo->gid); 254971fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 2550680d794bSakpm@linux-foundation.org return 0; 2551680d794bSakpm@linux-foundation.org } 2552680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 25531da177e4SLinus Torvalds 25541da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 25551da177e4SLinus Torvalds { 2556602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2557602586a8SHugh Dickins 2558602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 2559602586a8SHugh Dickins kfree(sbinfo); 25601da177e4SLinus Torvalds sb->s_fs_info = NULL; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25632b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 25641da177e4SLinus Torvalds { 25651da177e4SLinus Torvalds struct inode *inode; 25661da177e4SLinus Torvalds struct dentry *root; 25670edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 2568680d794bSakpm@linux-foundation.org int err = -ENOMEM; 2569680d794bSakpm@linux-foundation.org 2570680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 2571425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 2572680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 2573680d794bSakpm@linux-foundation.org if (!sbinfo) 2574680d794bSakpm@linux-foundation.org return -ENOMEM; 2575680d794bSakpm@linux-foundation.org 2576680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 257776aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 257876aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 2579680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 25801da177e4SLinus Torvalds 25810edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 25821da177e4SLinus Torvalds /* 25831da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 25841da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 25851da177e4SLinus Torvalds * but the internal instance is left unlimited. 25861da177e4SLinus Torvalds */ 25871da177e4SLinus Torvalds if (!(sb->s_flags & MS_NOUSER)) { 2588680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 2589680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 2590680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 2591680d794bSakpm@linux-foundation.org err = -EINVAL; 2592680d794bSakpm@linux-foundation.org goto failed; 2593680d794bSakpm@linux-foundation.org } 25941da177e4SLinus Torvalds } 259591828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 25960edd73b3SHugh Dickins #else 25970edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 25980edd73b3SHugh Dickins #endif 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 2601602586a8SHugh Dickins if (percpu_counter_init(&sbinfo->used_blocks, 0)) 2602602586a8SHugh Dickins goto failed; 2603680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds sb->s_maxbytes = SHMEM_MAX_BYTES; 26061da177e4SLinus Torvalds sb->s_blocksize = PAGE_CACHE_SIZE; 26071da177e4SLinus Torvalds sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 26081da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 26091da177e4SLinus Torvalds sb->s_op = &shmem_ops; 2610cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 2611b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 261239f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 2613b09e0fa4SEric Paris #endif 2614b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 261539f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 261639f0247dSAndreas Gruenbacher #endif 26170edd73b3SHugh Dickins 2618454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 26191da177e4SLinus Torvalds if (!inode) 26201da177e4SLinus Torvalds goto failed; 2621680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 2622680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 26231da177e4SLinus Torvalds root = d_alloc_root(inode); 26241da177e4SLinus Torvalds if (!root) 26251da177e4SLinus Torvalds goto failed_iput; 26261da177e4SLinus Torvalds sb->s_root = root; 26271da177e4SLinus Torvalds return 0; 26281da177e4SLinus Torvalds 26291da177e4SLinus Torvalds failed_iput: 26301da177e4SLinus Torvalds iput(inode); 26311da177e4SLinus Torvalds failed: 26321da177e4SLinus Torvalds shmem_put_super(sb); 26331da177e4SLinus Torvalds return err; 26341da177e4SLinus Torvalds } 26351da177e4SLinus Torvalds 2636fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 26391da177e4SLinus Torvalds { 26401da177e4SLinus Torvalds struct shmem_inode_info *p; 2641e94b1766SChristoph Lameter p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 26421da177e4SLinus Torvalds if (!p) 26431da177e4SLinus Torvalds return NULL; 26441da177e4SLinus Torvalds return &p->vfs_inode; 26451da177e4SLinus Torvalds } 26461da177e4SLinus Torvalds 2647fa0d7e3dSNick Piggin static void shmem_i_callback(struct rcu_head *head) 2648fa0d7e3dSNick Piggin { 2649fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 2650fa0d7e3dSNick Piggin INIT_LIST_HEAD(&inode->i_dentry); 2651fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 2652fa0d7e3dSNick Piggin } 2653fa0d7e3dSNick Piggin 26541da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 26551da177e4SLinus Torvalds { 26561da177e4SLinus Torvalds if ((inode->i_mode & S_IFMT) == S_IFREG) { 26571da177e4SLinus Torvalds /* only struct inode is valid if it's an inline symlink */ 26581da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 26591da177e4SLinus Torvalds } 2660fa0d7e3dSNick Piggin call_rcu(&inode->i_rcu, shmem_i_callback); 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds 266351cc5068SAlexey Dobriyan static void init_once(void *foo) 26641da177e4SLinus Torvalds { 26651da177e4SLinus Torvalds struct shmem_inode_info *p = (struct shmem_inode_info *) foo; 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds inode_init_once(&p->vfs_inode); 26681da177e4SLinus Torvalds } 26691da177e4SLinus Torvalds 26701da177e4SLinus Torvalds static int init_inodecache(void) 26711da177e4SLinus Torvalds { 26721da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 26731da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 2674040b5c6fSAlexey Dobriyan 0, SLAB_PANIC, init_once); 26751da177e4SLinus Torvalds return 0; 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds static void destroy_inodecache(void) 26791da177e4SLinus Torvalds { 26801a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 26811da177e4SLinus Torvalds } 26821da177e4SLinus Torvalds 2683f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 26841da177e4SLinus Torvalds .writepage = shmem_writepage, 268576719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 26861da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 2687ae976416SHugh Dickins .readpage = shmem_readpage, 2688800d15a5SNick Piggin .write_begin = shmem_write_begin, 2689800d15a5SNick Piggin .write_end = shmem_write_end, 26901da177e4SLinus Torvalds #endif 2691304dbdb7SLee Schermerhorn .migratepage = migrate_page, 2692aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 26931da177e4SLinus Torvalds }; 26941da177e4SLinus Torvalds 269515ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 26961da177e4SLinus Torvalds .mmap = shmem_mmap, 26971da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 26981da177e4SLinus Torvalds .llseek = generic_file_llseek, 2699bcd78e49SHugh Dickins .read = do_sync_read, 27005402b976SHugh Dickins .write = do_sync_write, 2701bcd78e49SHugh Dickins .aio_read = shmem_file_aio_read, 27025402b976SHugh Dickins .aio_write = generic_file_aio_write, 27031b061d92SChristoph Hellwig .fsync = noop_fsync, 2704ae976416SHugh Dickins .splice_read = generic_file_splice_read, 2705ae976416SHugh Dickins .splice_write = generic_file_splice_write, 27061da177e4SLinus Torvalds #endif 27071da177e4SLinus Torvalds }; 27081da177e4SLinus Torvalds 270992e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 271094c1e62dSHugh Dickins .setattr = shmem_setattr, 2711f6b3ec23SBadari Pulavarty .truncate_range = shmem_truncate_range, 2712b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2713b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2714b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2715b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2716b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2717b09e0fa4SEric Paris #endif 271839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 27191c7c474cSChristoph Hellwig .check_acl = generic_check_acl, 272039f0247dSAndreas Gruenbacher #endif 272139f0247dSAndreas Gruenbacher 27221da177e4SLinus Torvalds }; 27231da177e4SLinus Torvalds 272492e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 27251da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 27261da177e4SLinus Torvalds .create = shmem_create, 27271da177e4SLinus Torvalds .lookup = simple_lookup, 27281da177e4SLinus Torvalds .link = shmem_link, 27291da177e4SLinus Torvalds .unlink = shmem_unlink, 27301da177e4SLinus Torvalds .symlink = shmem_symlink, 27311da177e4SLinus Torvalds .mkdir = shmem_mkdir, 27321da177e4SLinus Torvalds .rmdir = shmem_rmdir, 27331da177e4SLinus Torvalds .mknod = shmem_mknod, 27341da177e4SLinus Torvalds .rename = shmem_rename, 27351da177e4SLinus Torvalds #endif 2736b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2737b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2738b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2739b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2740b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2741b09e0fa4SEric Paris #endif 274239f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 274394c1e62dSHugh Dickins .setattr = shmem_setattr, 27441c7c474cSChristoph Hellwig .check_acl = generic_check_acl, 274539f0247dSAndreas Gruenbacher #endif 274639f0247dSAndreas Gruenbacher }; 274739f0247dSAndreas Gruenbacher 274892e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 2749b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 2750b09e0fa4SEric Paris .setxattr = shmem_setxattr, 2751b09e0fa4SEric Paris .getxattr = shmem_getxattr, 2752b09e0fa4SEric Paris .listxattr = shmem_listxattr, 2753b09e0fa4SEric Paris .removexattr = shmem_removexattr, 2754b09e0fa4SEric Paris #endif 275539f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 275694c1e62dSHugh Dickins .setattr = shmem_setattr, 27571c7c474cSChristoph Hellwig .check_acl = generic_check_acl, 275839f0247dSAndreas Gruenbacher #endif 27591da177e4SLinus Torvalds }; 27601da177e4SLinus Torvalds 2761759b9775SHugh Dickins static const struct super_operations shmem_ops = { 27621da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 27631da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 27641da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 27651da177e4SLinus Torvalds .statfs = shmem_statfs, 27661da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 2767680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 27681da177e4SLinus Torvalds #endif 27691f895f75SAl Viro .evict_inode = shmem_evict_inode, 27701da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 27711da177e4SLinus Torvalds .put_super = shmem_put_super, 27721da177e4SLinus Torvalds }; 27731da177e4SLinus Torvalds 2774f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 277554cb8821SNick Piggin .fault = shmem_fault, 27761da177e4SLinus Torvalds #ifdef CONFIG_NUMA 27771da177e4SLinus Torvalds .set_policy = shmem_set_policy, 27781da177e4SLinus Torvalds .get_policy = shmem_get_policy, 27791da177e4SLinus Torvalds #endif 27801da177e4SLinus Torvalds }; 27811da177e4SLinus Torvalds 27821da177e4SLinus Torvalds 27833c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 27843c26ff6eSAl Viro int flags, const char *dev_name, void *data) 27851da177e4SLinus Torvalds { 27863c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 27871da177e4SLinus Torvalds } 27881da177e4SLinus Torvalds 27891da177e4SLinus Torvalds static struct file_system_type tmpfs_fs_type = { 27901da177e4SLinus Torvalds .owner = THIS_MODULE, 27911da177e4SLinus Torvalds .name = "tmpfs", 27923c26ff6eSAl Viro .mount = shmem_mount, 27931da177e4SLinus Torvalds .kill_sb = kill_litter_super, 27941da177e4SLinus Torvalds }; 27951da177e4SLinus Torvalds 27962b2af54aSKay Sievers int __init init_tmpfs(void) 27971da177e4SLinus Torvalds { 27981da177e4SLinus Torvalds int error; 27991da177e4SLinus Torvalds 2800e0bf68ddSPeter Zijlstra error = bdi_init(&shmem_backing_dev_info); 2801e0bf68ddSPeter Zijlstra if (error) 2802e0bf68ddSPeter Zijlstra goto out4; 2803e0bf68ddSPeter Zijlstra 28041da177e4SLinus Torvalds error = init_inodecache(); 28051da177e4SLinus Torvalds if (error) 28061da177e4SLinus Torvalds goto out3; 28071da177e4SLinus Torvalds 28081da177e4SLinus Torvalds error = register_filesystem(&tmpfs_fs_type); 28091da177e4SLinus Torvalds if (error) { 28101da177e4SLinus Torvalds printk(KERN_ERR "Could not register tmpfs\n"); 28111da177e4SLinus Torvalds goto out2; 28121da177e4SLinus Torvalds } 281395dc112aSGreg Kroah-Hartman 28141f5ce9e9STrond Myklebust shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER, 28151da177e4SLinus Torvalds tmpfs_fs_type.name, NULL); 28161da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 28171da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 28181da177e4SLinus Torvalds printk(KERN_ERR "Could not kern_mount tmpfs\n"); 28191da177e4SLinus Torvalds goto out1; 28201da177e4SLinus Torvalds } 28211da177e4SLinus Torvalds return 0; 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds out1: 28241da177e4SLinus Torvalds unregister_filesystem(&tmpfs_fs_type); 28251da177e4SLinus Torvalds out2: 28261da177e4SLinus Torvalds destroy_inodecache(); 28271da177e4SLinus Torvalds out3: 2828e0bf68ddSPeter Zijlstra bdi_destroy(&shmem_backing_dev_info); 2829e0bf68ddSPeter Zijlstra out4: 28301da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 28311da177e4SLinus Torvalds return error; 28321da177e4SLinus Torvalds } 2833853ac43aSMatt Mackall 283487946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR 283587946a72SDaisuke Nishimura /** 283687946a72SDaisuke Nishimura * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file 283787946a72SDaisuke Nishimura * @inode: the inode to be searched 283887946a72SDaisuke Nishimura * @pgoff: the offset to be searched 283987946a72SDaisuke Nishimura * @pagep: the pointer for the found page to be stored 284087946a72SDaisuke Nishimura * @ent: the pointer for the found swap entry to be stored 284187946a72SDaisuke Nishimura * 284287946a72SDaisuke Nishimura * If a page is found, refcount of it is incremented. Callers should handle 284387946a72SDaisuke Nishimura * these refcount. 284487946a72SDaisuke Nishimura */ 284587946a72SDaisuke Nishimura void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff, 284687946a72SDaisuke Nishimura struct page **pagep, swp_entry_t *ent) 284787946a72SDaisuke Nishimura { 284887946a72SDaisuke Nishimura swp_entry_t entry = { .val = 0 }, *ptr; 284987946a72SDaisuke Nishimura struct page *page = NULL; 285087946a72SDaisuke Nishimura struct shmem_inode_info *info = SHMEM_I(inode); 285187946a72SDaisuke Nishimura 285287946a72SDaisuke Nishimura if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 285387946a72SDaisuke Nishimura goto out; 285487946a72SDaisuke Nishimura 285587946a72SDaisuke Nishimura spin_lock(&info->lock); 285687946a72SDaisuke Nishimura ptr = shmem_swp_entry(info, pgoff, NULL); 285787946a72SDaisuke Nishimura #ifdef CONFIG_SWAP 285887946a72SDaisuke Nishimura if (ptr && ptr->val) { 285987946a72SDaisuke Nishimura entry.val = ptr->val; 286087946a72SDaisuke Nishimura page = find_get_page(&swapper_space, entry.val); 286187946a72SDaisuke Nishimura } else 286287946a72SDaisuke Nishimura #endif 286387946a72SDaisuke Nishimura page = find_get_page(inode->i_mapping, pgoff); 286487946a72SDaisuke Nishimura if (ptr) 286587946a72SDaisuke Nishimura shmem_swp_unmap(ptr); 286687946a72SDaisuke Nishimura spin_unlock(&info->lock); 286787946a72SDaisuke Nishimura out: 286887946a72SDaisuke Nishimura *pagep = page; 286987946a72SDaisuke Nishimura *ent = entry; 287087946a72SDaisuke Nishimura } 287187946a72SDaisuke Nishimura #endif 287287946a72SDaisuke Nishimura 2873853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 2874853ac43aSMatt Mackall 2875853ac43aSMatt Mackall /* 2876853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 2877853ac43aSMatt Mackall * 2878853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 2879853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 2880853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 2881853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 2882853ac43aSMatt Mackall */ 2883853ac43aSMatt Mackall 2884853ac43aSMatt Mackall #include <linux/ramfs.h> 2885853ac43aSMatt Mackall 2886853ac43aSMatt Mackall static struct file_system_type tmpfs_fs_type = { 2887853ac43aSMatt Mackall .name = "tmpfs", 28883c26ff6eSAl Viro .mount = ramfs_mount, 2889853ac43aSMatt Mackall .kill_sb = kill_litter_super, 2890853ac43aSMatt Mackall }; 2891853ac43aSMatt Mackall 28922b2af54aSKay Sievers int __init init_tmpfs(void) 2893853ac43aSMatt Mackall { 2894853ac43aSMatt Mackall BUG_ON(register_filesystem(&tmpfs_fs_type) != 0); 2895853ac43aSMatt Mackall 2896853ac43aSMatt Mackall shm_mnt = kern_mount(&tmpfs_fs_type); 2897853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 2898853ac43aSMatt Mackall 2899853ac43aSMatt Mackall return 0; 2900853ac43aSMatt Mackall } 2901853ac43aSMatt Mackall 2902853ac43aSMatt Mackall int shmem_unuse(swp_entry_t entry, struct page *page) 2903853ac43aSMatt Mackall { 2904853ac43aSMatt Mackall return 0; 2905853ac43aSMatt Mackall } 2906853ac43aSMatt Mackall 29073f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 29083f96b79aSHugh Dickins { 29093f96b79aSHugh Dickins return 0; 29103f96b79aSHugh Dickins } 29113f96b79aSHugh Dickins 291294c1e62dSHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end) 291394c1e62dSHugh Dickins { 291494c1e62dSHugh Dickins truncate_inode_pages_range(inode->i_mapping, start, end); 291594c1e62dSHugh Dickins } 291694c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 291794c1e62dSHugh Dickins 291887946a72SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR 291987946a72SDaisuke Nishimura /** 292087946a72SDaisuke Nishimura * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file 292187946a72SDaisuke Nishimura * @inode: the inode to be searched 292287946a72SDaisuke Nishimura * @pgoff: the offset to be searched 292387946a72SDaisuke Nishimura * @pagep: the pointer for the found page to be stored 292487946a72SDaisuke Nishimura * @ent: the pointer for the found swap entry to be stored 292587946a72SDaisuke Nishimura * 292687946a72SDaisuke Nishimura * If a page is found, refcount of it is incremented. Callers should handle 292787946a72SDaisuke Nishimura * these refcount. 292887946a72SDaisuke Nishimura */ 292987946a72SDaisuke Nishimura void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff, 293087946a72SDaisuke Nishimura struct page **pagep, swp_entry_t *ent) 293187946a72SDaisuke Nishimura { 293287946a72SDaisuke Nishimura struct page *page = NULL; 293387946a72SDaisuke Nishimura 293487946a72SDaisuke Nishimura if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 293587946a72SDaisuke Nishimura goto out; 293687946a72SDaisuke Nishimura page = find_get_page(inode->i_mapping, pgoff); 293787946a72SDaisuke Nishimura out: 293887946a72SDaisuke Nishimura *pagep = page; 293987946a72SDaisuke Nishimura *ent = (swp_entry_t){ .val = 0 }; 294087946a72SDaisuke Nishimura } 294187946a72SDaisuke Nishimura #endif 294287946a72SDaisuke Nishimura 2943853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 29440b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 2945454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 29460b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 29470b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 2948caefba17SHugh Dickins #define SHMEM_MAX_BYTES MAX_LFS_FILESIZE 2949853ac43aSMatt Mackall 2950853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 2951853ac43aSMatt Mackall 2952853ac43aSMatt Mackall /* common code */ 29531da177e4SLinus Torvalds 295446711810SRandy Dunlap /** 29551da177e4SLinus Torvalds * shmem_file_setup - get an unlinked file living in tmpfs 29561da177e4SLinus Torvalds * @name: name for dentry (to be seen in /proc/<pid>/maps 29571da177e4SLinus Torvalds * @size: size to be set for the file 29580b0a0806SHugh Dickins * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 29591da177e4SLinus Torvalds */ 2960168f5ac6SSergei Trofimovich struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 29611da177e4SLinus Torvalds { 29621da177e4SLinus Torvalds int error; 29631da177e4SLinus Torvalds struct file *file; 29641da177e4SLinus Torvalds struct inode *inode; 29652c48b9c4SAl Viro struct path path; 29662c48b9c4SAl Viro struct dentry *root; 29671da177e4SLinus Torvalds struct qstr this; 29681da177e4SLinus Torvalds 29691da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 29701da177e4SLinus Torvalds return (void *)shm_mnt; 29711da177e4SLinus Torvalds 29721da177e4SLinus Torvalds if (size < 0 || size > SHMEM_MAX_BYTES) 29731da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 29741da177e4SLinus Torvalds 29751da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 29761da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds error = -ENOMEM; 29791da177e4SLinus Torvalds this.name = name; 29801da177e4SLinus Torvalds this.len = strlen(name); 29811da177e4SLinus Torvalds this.hash = 0; /* will go */ 29821da177e4SLinus Torvalds root = shm_mnt->mnt_root; 29832c48b9c4SAl Viro path.dentry = d_alloc(root, &this); 29842c48b9c4SAl Viro if (!path.dentry) 29851da177e4SLinus Torvalds goto put_memory; 29862c48b9c4SAl Viro path.mnt = mntget(shm_mnt); 29871da177e4SLinus Torvalds 29881da177e4SLinus Torvalds error = -ENOSPC; 2989454abafeSDmitry Monakhov inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 29901da177e4SLinus Torvalds if (!inode) 29914b42af81SAl Viro goto put_dentry; 29921da177e4SLinus Torvalds 29932c48b9c4SAl Viro d_instantiate(path.dentry, inode); 29941da177e4SLinus Torvalds inode->i_size = size; 29951da177e4SLinus Torvalds inode->i_nlink = 0; /* It is unlinked */ 2996853ac43aSMatt Mackall #ifndef CONFIG_MMU 2997853ac43aSMatt Mackall error = ramfs_nommu_expand_for_mapping(inode, size); 2998853ac43aSMatt Mackall if (error) 29994b42af81SAl Viro goto put_dentry; 3000853ac43aSMatt Mackall #endif 30014b42af81SAl Viro 30024b42af81SAl Viro error = -ENFILE; 30032c48b9c4SAl Viro file = alloc_file(&path, FMODE_WRITE | FMODE_READ, 30044b42af81SAl Viro &shmem_file_operations); 30054b42af81SAl Viro if (!file) 30064b42af81SAl Viro goto put_dentry; 30074b42af81SAl Viro 30081da177e4SLinus Torvalds return file; 30091da177e4SLinus Torvalds 30101da177e4SLinus Torvalds put_dentry: 30112c48b9c4SAl Viro path_put(&path); 30121da177e4SLinus Torvalds put_memory: 30131da177e4SLinus Torvalds shmem_unacct_size(flags, size); 30141da177e4SLinus Torvalds return ERR_PTR(error); 30151da177e4SLinus Torvalds } 3016395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 30171da177e4SLinus Torvalds 301846711810SRandy Dunlap /** 30191da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 30201da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 30211da177e4SLinus Torvalds */ 30221da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 30231da177e4SLinus Torvalds { 30241da177e4SLinus Torvalds struct file *file; 30251da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 30261da177e4SLinus Torvalds 30271da177e4SLinus Torvalds file = shmem_file_setup("dev/zero", size, vma->vm_flags); 30281da177e4SLinus Torvalds if (IS_ERR(file)) 30291da177e4SLinus Torvalds return PTR_ERR(file); 30301da177e4SLinus Torvalds 30311da177e4SLinus Torvalds if (vma->vm_file) 30321da177e4SLinus Torvalds fput(vma->vm_file); 30331da177e4SLinus Torvalds vma->vm_file = file; 30341da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 3035bee4c36aSHugh Dickins vma->vm_flags |= VM_CAN_NONLINEAR; 30361da177e4SLinus Torvalds return 0; 30371da177e4SLinus Torvalds } 3038d9d90e5eSHugh Dickins 3039d9d90e5eSHugh Dickins /** 3040d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 3041d9d90e5eSHugh Dickins * @mapping: the page's address_space 3042d9d90e5eSHugh Dickins * @index: the page index 3043d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 3044d9d90e5eSHugh Dickins * 3045d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 3046d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 3047d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 3048d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 3049d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 3050d9d90e5eSHugh Dickins * 3051d9d90e5eSHugh Dickins * Provide a stub for those callers to start using now, then later 3052d9d90e5eSHugh Dickins * flesh it out to call shmem_getpage() with additional gfp mask, when 3053d9d90e5eSHugh Dickins * shmem_file_splice_read() is added and shmem_readpage() is removed. 3054d9d90e5eSHugh Dickins */ 3055d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 3056d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 3057d9d90e5eSHugh Dickins { 3058d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 3059d9d90e5eSHugh Dickins } 3060d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 3061