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 * 171da177e4SLinus Torvalds * This file is released under the GPL. 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds /* 211da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 221da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 231da177e4SLinus Torvalds * which makes it a completely usable filesystem. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include <linux/config.h> 271da177e4SLinus Torvalds #include <linux/module.h> 281da177e4SLinus Torvalds #include <linux/init.h> 291da177e4SLinus Torvalds #include <linux/devfs_fs_kernel.h> 301da177e4SLinus Torvalds #include <linux/fs.h> 311da177e4SLinus Torvalds #include <linux/mm.h> 321da177e4SLinus Torvalds #include <linux/mman.h> 331da177e4SLinus Torvalds #include <linux/file.h> 341da177e4SLinus Torvalds #include <linux/swap.h> 351da177e4SLinus Torvalds #include <linux/pagemap.h> 361da177e4SLinus Torvalds #include <linux/string.h> 371da177e4SLinus Torvalds #include <linux/slab.h> 381da177e4SLinus Torvalds #include <linux/backing-dev.h> 391da177e4SLinus Torvalds #include <linux/shmem_fs.h> 401da177e4SLinus Torvalds #include <linux/mount.h> 411da177e4SLinus Torvalds #include <linux/writeback.h> 421da177e4SLinus Torvalds #include <linux/vfs.h> 431da177e4SLinus Torvalds #include <linux/blkdev.h> 441da177e4SLinus Torvalds #include <linux/security.h> 451da177e4SLinus Torvalds #include <linux/swapops.h> 461da177e4SLinus Torvalds #include <linux/mempolicy.h> 471da177e4SLinus Torvalds #include <linux/namei.h> 48b00dc3adSHugh Dickins #include <linux/ctype.h> 49*304dbdb7SLee Schermerhorn #include <linux/migrate.h> 50*304dbdb7SLee Schermerhorn 511da177e4SLinus Torvalds #include <asm/uaccess.h> 521da177e4SLinus Torvalds #include <asm/div64.h> 531da177e4SLinus Torvalds #include <asm/pgtable.h> 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /* This magic number is used in glibc for posix shared memory */ 561da177e4SLinus Torvalds #define TMPFS_MAGIC 0x01021994 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long)) 591da177e4SLinus Torvalds #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE) 601da177e4SLinus Torvalds #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds #define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1)) 631da177e4SLinus Torvalds #define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT) 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* info->flags needs VM_flags to handle pagein/truncate races efficiently */ 681da177e4SLinus Torvalds #define SHMEM_PAGEIN VM_READ 691da177e4SLinus Torvalds #define SHMEM_TRUNCATE VM_WRITE 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* Definition to limit shmem_truncate's steps between cond_rescheds */ 721da177e4SLinus Torvalds #define LATENCY_LIMIT 64 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 751da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */ 781da177e4SLinus Torvalds enum sgp_type { 791da177e4SLinus Torvalds SGP_QUICK, /* don't try more than file page cache lookup */ 801da177e4SLinus Torvalds SGP_READ, /* don't exceed i_size, don't allocate page */ 811da177e4SLinus Torvalds SGP_CACHE, /* don't exceed i_size, may allocate page */ 821da177e4SLinus Torvalds SGP_WRITE, /* may exceed i_size, may allocate page */ 831da177e4SLinus Torvalds }; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx, 861da177e4SLinus Torvalds struct page **pagep, enum sgp_type sgp, int *type); 871da177e4SLinus Torvalds 886daa0e28SAl Viro static inline struct page *shmem_dir_alloc(gfp_t gfp_mask) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds /* 911da177e4SLinus Torvalds * The above definition of ENTRIES_PER_PAGE, and the use of 921da177e4SLinus Torvalds * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE: 931da177e4SLinus Torvalds * might be reconsidered if it ever diverges from PAGE_SIZE. 941da177e4SLinus Torvalds */ 951da177e4SLinus Torvalds return alloc_pages(gfp_mask, PAGE_CACHE_SHIFT-PAGE_SHIFT); 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds static inline void shmem_dir_free(struct page *page) 991da177e4SLinus Torvalds { 1001da177e4SLinus Torvalds __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT); 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds static struct page **shmem_dir_map(struct page *page) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds return (struct page **)kmap_atomic(page, KM_USER0); 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds static inline void shmem_dir_unmap(struct page **dir) 1091da177e4SLinus Torvalds { 1101da177e4SLinus Torvalds kunmap_atomic(dir, KM_USER0); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds static swp_entry_t *shmem_swp_map(struct page *page) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds return (swp_entry_t *)kmap_atomic(page, KM_USER1); 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds static inline void shmem_swp_balance_unmap(void) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds /* 1211da177e4SLinus Torvalds * When passing a pointer to an i_direct entry, to code which 1221da177e4SLinus Torvalds * also handles indirect entries and so will shmem_swp_unmap, 1231da177e4SLinus Torvalds * we must arrange for the preempt count to remain in balance. 1241da177e4SLinus Torvalds * What kmap_atomic of a lowmem page does depends on config 1251da177e4SLinus Torvalds * and architecture, so pretend to kmap_atomic some lowmem page. 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds (void) kmap_atomic(ZERO_PAGE(0), KM_USER1); 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds static inline void shmem_swp_unmap(swp_entry_t *entry) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds kunmap_atomic(entry, KM_USER1); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds return sb->s_fs_info; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds /* 1411da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1421da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1431da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1441da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1451da177e4SLinus Torvalds */ 1461da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1471da177e4SLinus Torvalds { 1481da177e4SLinus Torvalds return (flags & VM_ACCOUNT)? 1491da177e4SLinus Torvalds security_vm_enough_memory(VM_ACCT(size)): 0; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds if (flags & VM_ACCOUNT) 1551da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* 1591da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 1601da177e4SLinus Torvalds * pages are allocated, in order to allow huge sparse files. 1611da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1621da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1631da177e4SLinus Torvalds */ 1641da177e4SLinus Torvalds static inline int shmem_acct_block(unsigned long flags) 1651da177e4SLinus Torvalds { 1661da177e4SLinus Torvalds return (flags & VM_ACCOUNT)? 1671da177e4SLinus Torvalds 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE)); 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds if (!(flags & VM_ACCOUNT)) 1731da177e4SLinus Torvalds vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds static struct super_operations shmem_ops; 1771da177e4SLinus Torvalds static struct address_space_operations shmem_aops; 1781da177e4SLinus Torvalds static struct file_operations shmem_file_operations; 1791da177e4SLinus Torvalds static struct inode_operations shmem_inode_operations; 1801da177e4SLinus Torvalds static struct inode_operations shmem_dir_inode_operations; 1811da177e4SLinus Torvalds static struct vm_operations_struct shmem_vm_ops; 1821da177e4SLinus Torvalds 1836c231b7bSRavikiran G Thirumalai static struct backing_dev_info shmem_backing_dev_info __read_mostly = { 1841da177e4SLinus Torvalds .ra_pages = 0, /* No readahead */ 1851da177e4SLinus Torvalds .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK, 1861da177e4SLinus Torvalds .unplug_io_fn = default_unplug_io_fn, 1871da177e4SLinus Torvalds }; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 1901da177e4SLinus Torvalds static DEFINE_SPINLOCK(shmem_swaplist_lock); 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds static void shmem_free_blocks(struct inode *inode, long pages) 1931da177e4SLinus Torvalds { 1941da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 1950edd73b3SHugh Dickins if (sbinfo->max_blocks) { 1961da177e4SLinus Torvalds spin_lock(&sbinfo->stat_lock); 1971da177e4SLinus Torvalds sbinfo->free_blocks += pages; 1981da177e4SLinus Torvalds inode->i_blocks -= pages*BLOCKS_PER_PAGE; 1991da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* 2041da177e4SLinus Torvalds * shmem_recalc_inode - recalculate the size of an inode 2051da177e4SLinus Torvalds * 2061da177e4SLinus Torvalds * @inode: inode to recalc 2071da177e4SLinus Torvalds * 2081da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2091da177e4SLinus Torvalds * undirtied hole pages behind our back. 2101da177e4SLinus Torvalds * 2111da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2121da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2131da177e4SLinus Torvalds * 2141da177e4SLinus Torvalds * It has to be called with the spinlock held. 2151da177e4SLinus Torvalds */ 2161da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2171da177e4SLinus Torvalds { 2181da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2191da177e4SLinus Torvalds long freed; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2221da177e4SLinus Torvalds if (freed > 0) { 2231da177e4SLinus Torvalds info->alloced -= freed; 2241da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2251da177e4SLinus Torvalds shmem_free_blocks(inode, freed); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds /* 2301da177e4SLinus Torvalds * shmem_swp_entry - find the swap vector position in the info structure 2311da177e4SLinus Torvalds * 2321da177e4SLinus Torvalds * @info: info structure for the inode 2331da177e4SLinus Torvalds * @index: index of the page to find 2341da177e4SLinus Torvalds * @page: optional page to add to the structure. Has to be preset to 2351da177e4SLinus Torvalds * all zeros 2361da177e4SLinus Torvalds * 2371da177e4SLinus Torvalds * If there is no space allocated yet it will return NULL when 2381da177e4SLinus Torvalds * page is NULL, else it will use the page for the needed block, 2391da177e4SLinus Torvalds * setting it to NULL on return to indicate that it has been used. 2401da177e4SLinus Torvalds * 2411da177e4SLinus Torvalds * The swap vector is organized the following way: 2421da177e4SLinus Torvalds * 2431da177e4SLinus Torvalds * There are SHMEM_NR_DIRECT entries directly stored in the 2441da177e4SLinus Torvalds * shmem_inode_info structure. So small files do not need an addional 2451da177e4SLinus Torvalds * allocation. 2461da177e4SLinus Torvalds * 2471da177e4SLinus Torvalds * For pages with index > SHMEM_NR_DIRECT there is the pointer 2481da177e4SLinus Torvalds * i_indirect which points to a page which holds in the first half 2491da177e4SLinus Torvalds * doubly indirect blocks, in the second half triple indirect blocks: 2501da177e4SLinus Torvalds * 2511da177e4SLinus Torvalds * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the 2521da177e4SLinus Torvalds * following layout (for SHMEM_NR_DIRECT == 16): 2531da177e4SLinus Torvalds * 2541da177e4SLinus Torvalds * i_indirect -> dir --> 16-19 2551da177e4SLinus Torvalds * | +-> 20-23 2561da177e4SLinus Torvalds * | 2571da177e4SLinus Torvalds * +-->dir2 --> 24-27 2581da177e4SLinus Torvalds * | +-> 28-31 2591da177e4SLinus Torvalds * | +-> 32-35 2601da177e4SLinus Torvalds * | +-> 36-39 2611da177e4SLinus Torvalds * | 2621da177e4SLinus Torvalds * +-->dir3 --> 40-43 2631da177e4SLinus Torvalds * +-> 44-47 2641da177e4SLinus Torvalds * +-> 48-51 2651da177e4SLinus Torvalds * +-> 52-55 2661da177e4SLinus Torvalds */ 2671da177e4SLinus Torvalds static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds unsigned long offset; 2701da177e4SLinus Torvalds struct page **dir; 2711da177e4SLinus Torvalds struct page *subdir; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds if (index < SHMEM_NR_DIRECT) { 2741da177e4SLinus Torvalds shmem_swp_balance_unmap(); 2751da177e4SLinus Torvalds return info->i_direct+index; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds if (!info->i_indirect) { 2781da177e4SLinus Torvalds if (page) { 2791da177e4SLinus Torvalds info->i_indirect = *page; 2801da177e4SLinus Torvalds *page = NULL; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds return NULL; /* need another page */ 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds index -= SHMEM_NR_DIRECT; 2861da177e4SLinus Torvalds offset = index % ENTRIES_PER_PAGE; 2871da177e4SLinus Torvalds index /= ENTRIES_PER_PAGE; 2881da177e4SLinus Torvalds dir = shmem_dir_map(info->i_indirect); 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds if (index >= ENTRIES_PER_PAGE/2) { 2911da177e4SLinus Torvalds index -= ENTRIES_PER_PAGE/2; 2921da177e4SLinus Torvalds dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE; 2931da177e4SLinus Torvalds index %= ENTRIES_PER_PAGE; 2941da177e4SLinus Torvalds subdir = *dir; 2951da177e4SLinus Torvalds if (!subdir) { 2961da177e4SLinus Torvalds if (page) { 2971da177e4SLinus Torvalds *dir = *page; 2981da177e4SLinus Torvalds *page = NULL; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds shmem_dir_unmap(dir); 3011da177e4SLinus Torvalds return NULL; /* need another page */ 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds shmem_dir_unmap(dir); 3041da177e4SLinus Torvalds dir = shmem_dir_map(subdir); 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds dir += index; 3081da177e4SLinus Torvalds subdir = *dir; 3091da177e4SLinus Torvalds if (!subdir) { 3101da177e4SLinus Torvalds if (!page || !(subdir = *page)) { 3111da177e4SLinus Torvalds shmem_dir_unmap(dir); 3121da177e4SLinus Torvalds return NULL; /* need a page */ 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds *dir = subdir; 3151da177e4SLinus Torvalds *page = NULL; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds shmem_dir_unmap(dir); 3181da177e4SLinus Torvalds return shmem_swp_map(subdir) + offset; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds long incdec = value? 1: -1; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds entry->val = value; 3261da177e4SLinus Torvalds info->swapped += incdec; 3274c21e2f2SHugh Dickins if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) { 3284c21e2f2SHugh Dickins struct page *page = kmap_atomic_to_page(entry); 3294c21e2f2SHugh Dickins set_page_private(page, page_private(page) + incdec); 3304c21e2f2SHugh Dickins } 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds /* 3341da177e4SLinus Torvalds * shmem_swp_alloc - get the position of the swap entry for the page. 3351da177e4SLinus Torvalds * If it does not exist allocate the entry. 3361da177e4SLinus Torvalds * 3371da177e4SLinus Torvalds * @info: info structure for the inode 3381da177e4SLinus Torvalds * @index: index of the page to find 3391da177e4SLinus Torvalds * @sgp: check and recheck i_size? skip allocation? 3401da177e4SLinus Torvalds */ 3411da177e4SLinus Torvalds static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp) 3421da177e4SLinus Torvalds { 3431da177e4SLinus Torvalds struct inode *inode = &info->vfs_inode; 3441da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 3451da177e4SLinus Torvalds struct page *page = NULL; 3461da177e4SLinus Torvalds swp_entry_t *entry; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds if (sgp != SGP_WRITE && 3491da177e4SLinus Torvalds ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 3501da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds while (!(entry = shmem_swp_entry(info, index, &page))) { 3531da177e4SLinus Torvalds if (sgp == SGP_READ) 3541da177e4SLinus Torvalds return shmem_swp_map(ZERO_PAGE(0)); 3551da177e4SLinus Torvalds /* 3561da177e4SLinus Torvalds * Test free_blocks against 1 not 0, since we have 1 data 3571da177e4SLinus Torvalds * page (and perhaps indirect index pages) yet to allocate: 3581da177e4SLinus Torvalds * a waste to allocate index if we cannot allocate data. 3591da177e4SLinus Torvalds */ 3600edd73b3SHugh Dickins if (sbinfo->max_blocks) { 3611da177e4SLinus Torvalds spin_lock(&sbinfo->stat_lock); 3621da177e4SLinus Torvalds if (sbinfo->free_blocks <= 1) { 3631da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 3641da177e4SLinus Torvalds return ERR_PTR(-ENOSPC); 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds sbinfo->free_blocks--; 3671da177e4SLinus Torvalds inode->i_blocks += BLOCKS_PER_PAGE; 3681da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds spin_unlock(&info->lock); 3721da177e4SLinus Torvalds page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping) | __GFP_ZERO); 3734c21e2f2SHugh Dickins if (page) 3744c21e2f2SHugh Dickins set_page_private(page, 0); 3751da177e4SLinus Torvalds spin_lock(&info->lock); 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds if (!page) { 3781da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 3791da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds if (sgp != SGP_WRITE && 3821da177e4SLinus Torvalds ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { 3831da177e4SLinus Torvalds entry = ERR_PTR(-EINVAL); 3841da177e4SLinus Torvalds break; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds if (info->next_index <= index) 3871da177e4SLinus Torvalds info->next_index = index + 1; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds if (page) { 3901da177e4SLinus Torvalds /* another task gave its page, or truncated the file */ 3911da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 3921da177e4SLinus Torvalds shmem_dir_free(page); 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds if (info->next_index <= index && !IS_ERR(entry)) 3951da177e4SLinus Torvalds info->next_index = index + 1; 3961da177e4SLinus Torvalds return entry; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* 4001da177e4SLinus Torvalds * shmem_free_swp - free some swap entries in a directory 4011da177e4SLinus Torvalds * 4021da177e4SLinus Torvalds * @dir: pointer to the directory 4031da177e4SLinus Torvalds * @edir: pointer after last entry of the directory 4041da177e4SLinus Torvalds */ 4051da177e4SLinus Torvalds static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds swp_entry_t *ptr; 4081da177e4SLinus Torvalds int freed = 0; 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds for (ptr = dir; ptr < edir; ptr++) { 4111da177e4SLinus Torvalds if (ptr->val) { 4121da177e4SLinus Torvalds free_swap_and_cache(*ptr); 4131da177e4SLinus Torvalds *ptr = (swp_entry_t){0}; 4141da177e4SLinus Torvalds freed++; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds return freed; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds static int shmem_map_and_free_swp(struct page *subdir, 4211da177e4SLinus Torvalds int offset, int limit, struct page ***dir) 4221da177e4SLinus Torvalds { 4231da177e4SLinus Torvalds swp_entry_t *ptr; 4241da177e4SLinus Torvalds int freed = 0; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds ptr = shmem_swp_map(subdir); 4271da177e4SLinus Torvalds for (; offset < limit; offset += LATENCY_LIMIT) { 4281da177e4SLinus Torvalds int size = limit - offset; 4291da177e4SLinus Torvalds if (size > LATENCY_LIMIT) 4301da177e4SLinus Torvalds size = LATENCY_LIMIT; 4311da177e4SLinus Torvalds freed += shmem_free_swp(ptr+offset, ptr+offset+size); 4321da177e4SLinus Torvalds if (need_resched()) { 4331da177e4SLinus Torvalds shmem_swp_unmap(ptr); 4341da177e4SLinus Torvalds if (*dir) { 4351da177e4SLinus Torvalds shmem_dir_unmap(*dir); 4361da177e4SLinus Torvalds *dir = NULL; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds cond_resched(); 4391da177e4SLinus Torvalds ptr = shmem_swp_map(subdir); 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds shmem_swp_unmap(ptr); 4431da177e4SLinus Torvalds return freed; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds static void shmem_free_pages(struct list_head *next) 4471da177e4SLinus Torvalds { 4481da177e4SLinus Torvalds struct page *page; 4491da177e4SLinus Torvalds int freed = 0; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds do { 4521da177e4SLinus Torvalds page = container_of(next, struct page, lru); 4531da177e4SLinus Torvalds next = next->next; 4541da177e4SLinus Torvalds shmem_dir_free(page); 4551da177e4SLinus Torvalds freed++; 4561da177e4SLinus Torvalds if (freed >= LATENCY_LIMIT) { 4571da177e4SLinus Torvalds cond_resched(); 4581da177e4SLinus Torvalds freed = 0; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds } while (next); 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 463f6b3ec23SBadari Pulavarty static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end) 4641da177e4SLinus Torvalds { 4651da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 4661da177e4SLinus Torvalds unsigned long idx; 4671da177e4SLinus Torvalds unsigned long size; 4681da177e4SLinus Torvalds unsigned long limit; 4691da177e4SLinus Torvalds unsigned long stage; 4701da177e4SLinus Torvalds unsigned long diroff; 4711da177e4SLinus Torvalds struct page **dir; 4721da177e4SLinus Torvalds struct page *topdir; 4731da177e4SLinus Torvalds struct page *middir; 4741da177e4SLinus Torvalds struct page *subdir; 4751da177e4SLinus Torvalds swp_entry_t *ptr; 4761da177e4SLinus Torvalds LIST_HEAD(pages_to_free); 4771da177e4SLinus Torvalds long nr_pages_to_free = 0; 4781da177e4SLinus Torvalds long nr_swaps_freed = 0; 4791da177e4SLinus Torvalds int offset; 4801da177e4SLinus Torvalds int freed; 481f6b3ec23SBadari Pulavarty int punch_hole = 0; 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds inode->i_ctime = inode->i_mtime = CURRENT_TIME; 484f6b3ec23SBadari Pulavarty idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 4851da177e4SLinus Torvalds if (idx >= info->next_index) 4861da177e4SLinus Torvalds return; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds spin_lock(&info->lock); 4891da177e4SLinus Torvalds info->flags |= SHMEM_TRUNCATE; 490f6b3ec23SBadari Pulavarty if (likely(end == (loff_t) -1)) { 4911da177e4SLinus Torvalds limit = info->next_index; 4921da177e4SLinus Torvalds info->next_index = idx; 493f6b3ec23SBadari Pulavarty } else { 494f6b3ec23SBadari Pulavarty limit = (end + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 495f6b3ec23SBadari Pulavarty if (limit > info->next_index) 496f6b3ec23SBadari Pulavarty limit = info->next_index; 497f6b3ec23SBadari Pulavarty punch_hole = 1; 498f6b3ec23SBadari Pulavarty } 499f6b3ec23SBadari Pulavarty 5001da177e4SLinus Torvalds topdir = info->i_indirect; 501f6b3ec23SBadari Pulavarty if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) { 5021da177e4SLinus Torvalds info->i_indirect = NULL; 5031da177e4SLinus Torvalds nr_pages_to_free++; 5041da177e4SLinus Torvalds list_add(&topdir->lru, &pages_to_free); 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds spin_unlock(&info->lock); 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds if (info->swapped && idx < SHMEM_NR_DIRECT) { 5091da177e4SLinus Torvalds ptr = info->i_direct; 5101da177e4SLinus Torvalds size = limit; 5111da177e4SLinus Torvalds if (size > SHMEM_NR_DIRECT) 5121da177e4SLinus Torvalds size = SHMEM_NR_DIRECT; 5131da177e4SLinus Torvalds nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size); 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds if (!topdir) 5161da177e4SLinus Torvalds goto done2; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds BUG_ON(limit <= SHMEM_NR_DIRECT); 5191da177e4SLinus Torvalds limit -= SHMEM_NR_DIRECT; 5201da177e4SLinus Torvalds idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0; 5211da177e4SLinus Torvalds offset = idx % ENTRIES_PER_PAGE; 5221da177e4SLinus Torvalds idx -= offset; 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds dir = shmem_dir_map(topdir); 5251da177e4SLinus Torvalds stage = ENTRIES_PER_PAGEPAGE/2; 5261da177e4SLinus Torvalds if (idx < ENTRIES_PER_PAGEPAGE/2) { 5271da177e4SLinus Torvalds middir = topdir; 5281da177e4SLinus Torvalds diroff = idx/ENTRIES_PER_PAGE; 5291da177e4SLinus Torvalds } else { 5301da177e4SLinus Torvalds dir += ENTRIES_PER_PAGE/2; 5311da177e4SLinus Torvalds dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE; 5321da177e4SLinus Torvalds while (stage <= idx) 5331da177e4SLinus Torvalds stage += ENTRIES_PER_PAGEPAGE; 5341da177e4SLinus Torvalds middir = *dir; 5351da177e4SLinus Torvalds if (*dir) { 5361da177e4SLinus Torvalds diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) % 5371da177e4SLinus Torvalds ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE; 5381da177e4SLinus Torvalds if (!diroff && !offset) { 5391da177e4SLinus Torvalds *dir = NULL; 5401da177e4SLinus Torvalds nr_pages_to_free++; 5411da177e4SLinus Torvalds list_add(&middir->lru, &pages_to_free); 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds shmem_dir_unmap(dir); 5441da177e4SLinus Torvalds dir = shmem_dir_map(middir); 5451da177e4SLinus Torvalds } else { 5461da177e4SLinus Torvalds diroff = 0; 5471da177e4SLinus Torvalds offset = 0; 5481da177e4SLinus Torvalds idx = stage; 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) { 5531da177e4SLinus Torvalds if (unlikely(idx == stage)) { 5541da177e4SLinus Torvalds shmem_dir_unmap(dir); 5551da177e4SLinus Torvalds dir = shmem_dir_map(topdir) + 5561da177e4SLinus Torvalds ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE; 5571da177e4SLinus Torvalds while (!*dir) { 5581da177e4SLinus Torvalds dir++; 5591da177e4SLinus Torvalds idx += ENTRIES_PER_PAGEPAGE; 5601da177e4SLinus Torvalds if (idx >= limit) 5611da177e4SLinus Torvalds goto done1; 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds stage = idx + ENTRIES_PER_PAGEPAGE; 5641da177e4SLinus Torvalds middir = *dir; 5651da177e4SLinus Torvalds *dir = NULL; 5661da177e4SLinus Torvalds nr_pages_to_free++; 5671da177e4SLinus Torvalds list_add(&middir->lru, &pages_to_free); 5681da177e4SLinus Torvalds shmem_dir_unmap(dir); 5691da177e4SLinus Torvalds cond_resched(); 5701da177e4SLinus Torvalds dir = shmem_dir_map(middir); 5711da177e4SLinus Torvalds diroff = 0; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds subdir = dir[diroff]; 5744c21e2f2SHugh Dickins if (subdir && page_private(subdir)) { 5751da177e4SLinus Torvalds size = limit - idx; 5761da177e4SLinus Torvalds if (size > ENTRIES_PER_PAGE) 5771da177e4SLinus Torvalds size = ENTRIES_PER_PAGE; 5781da177e4SLinus Torvalds freed = shmem_map_and_free_swp(subdir, 5791da177e4SLinus Torvalds offset, size, &dir); 5801da177e4SLinus Torvalds if (!dir) 5811da177e4SLinus Torvalds dir = shmem_dir_map(middir); 5821da177e4SLinus Torvalds nr_swaps_freed += freed; 5831da177e4SLinus Torvalds if (offset) 5841da177e4SLinus Torvalds spin_lock(&info->lock); 5854c21e2f2SHugh Dickins set_page_private(subdir, page_private(subdir) - freed); 5861da177e4SLinus Torvalds if (offset) 5871da177e4SLinus Torvalds spin_unlock(&info->lock); 588f6b3ec23SBadari Pulavarty if (!punch_hole) 5894c21e2f2SHugh Dickins BUG_ON(page_private(subdir) > offset); 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds if (offset) 5921da177e4SLinus Torvalds offset = 0; 593f6b3ec23SBadari Pulavarty else if (subdir && !page_private(subdir)) { 5941da177e4SLinus Torvalds dir[diroff] = NULL; 5951da177e4SLinus Torvalds nr_pages_to_free++; 5961da177e4SLinus Torvalds list_add(&subdir->lru, &pages_to_free); 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds done1: 6001da177e4SLinus Torvalds shmem_dir_unmap(dir); 6011da177e4SLinus Torvalds done2: 6021da177e4SLinus Torvalds if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) { 6031da177e4SLinus Torvalds /* 6041da177e4SLinus Torvalds * Call truncate_inode_pages again: racing shmem_unuse_inode 6051da177e4SLinus Torvalds * may have swizzled a page in from swap since vmtruncate or 6061da177e4SLinus Torvalds * generic_delete_inode did it, before we lowered next_index. 6071da177e4SLinus Torvalds * Also, though shmem_getpage checks i_size before adding to 6081da177e4SLinus Torvalds * cache, no recheck after: so fix the narrow window there too. 6091da177e4SLinus Torvalds */ 610f6b3ec23SBadari Pulavarty truncate_inode_pages_range(inode->i_mapping, start, end); 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds spin_lock(&info->lock); 6141da177e4SLinus Torvalds info->flags &= ~SHMEM_TRUNCATE; 6151da177e4SLinus Torvalds info->swapped -= nr_swaps_freed; 6161da177e4SLinus Torvalds if (nr_pages_to_free) 6171da177e4SLinus Torvalds shmem_free_blocks(inode, nr_pages_to_free); 6181da177e4SLinus Torvalds shmem_recalc_inode(inode); 6191da177e4SLinus Torvalds spin_unlock(&info->lock); 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /* 6221da177e4SLinus Torvalds * Empty swap vector directory pages to be freed? 6231da177e4SLinus Torvalds */ 6241da177e4SLinus Torvalds if (!list_empty(&pages_to_free)) { 6251da177e4SLinus Torvalds pages_to_free.prev->next = NULL; 6261da177e4SLinus Torvalds shmem_free_pages(pages_to_free.next); 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 630f6b3ec23SBadari Pulavarty static void shmem_truncate(struct inode *inode) 631f6b3ec23SBadari Pulavarty { 632f6b3ec23SBadari Pulavarty shmem_truncate_range(inode, inode->i_size, (loff_t)-1); 633f6b3ec23SBadari Pulavarty } 634f6b3ec23SBadari Pulavarty 6351da177e4SLinus Torvalds static int shmem_notify_change(struct dentry *dentry, struct iattr *attr) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 6381da177e4SLinus Torvalds struct page *page = NULL; 6391da177e4SLinus Torvalds int error; 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds if (attr->ia_valid & ATTR_SIZE) { 6421da177e4SLinus Torvalds if (attr->ia_size < inode->i_size) { 6431da177e4SLinus Torvalds /* 6441da177e4SLinus Torvalds * If truncating down to a partial page, then 6451da177e4SLinus Torvalds * if that page is already allocated, hold it 6461da177e4SLinus Torvalds * in memory until the truncation is over, so 6471da177e4SLinus Torvalds * truncate_partial_page cannnot miss it were 6481da177e4SLinus Torvalds * it assigned to swap. 6491da177e4SLinus Torvalds */ 6501da177e4SLinus Torvalds if (attr->ia_size & (PAGE_CACHE_SIZE-1)) { 6511da177e4SLinus Torvalds (void) shmem_getpage(inode, 6521da177e4SLinus Torvalds attr->ia_size>>PAGE_CACHE_SHIFT, 6531da177e4SLinus Torvalds &page, SGP_READ, NULL); 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds /* 6561da177e4SLinus Torvalds * Reset SHMEM_PAGEIN flag so that shmem_truncate can 6571da177e4SLinus Torvalds * detect if any pages might have been added to cache 6581da177e4SLinus Torvalds * after truncate_inode_pages. But we needn't bother 6591da177e4SLinus Torvalds * if it's being fully truncated to zero-length: the 6601da177e4SLinus Torvalds * nrpages check is efficient enough in that case. 6611da177e4SLinus Torvalds */ 6621da177e4SLinus Torvalds if (attr->ia_size) { 6631da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 6641da177e4SLinus Torvalds spin_lock(&info->lock); 6651da177e4SLinus Torvalds info->flags &= ~SHMEM_PAGEIN; 6661da177e4SLinus Torvalds spin_unlock(&info->lock); 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds error = inode_change_ok(inode, attr); 6721da177e4SLinus Torvalds if (!error) 6731da177e4SLinus Torvalds error = inode_setattr(inode, attr); 6741da177e4SLinus Torvalds if (page) 6751da177e4SLinus Torvalds page_cache_release(page); 6761da177e4SLinus Torvalds return error; 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds static void shmem_delete_inode(struct inode *inode) 6801da177e4SLinus Torvalds { 6811da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 6821da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds if (inode->i_op->truncate == shmem_truncate) { 685fef26658SMark Fasheh truncate_inode_pages(inode->i_mapping, 0); 6861da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 6871da177e4SLinus Torvalds inode->i_size = 0; 6881da177e4SLinus Torvalds shmem_truncate(inode); 6891da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 6901da177e4SLinus Torvalds spin_lock(&shmem_swaplist_lock); 6911da177e4SLinus Torvalds list_del_init(&info->swaplist); 6921da177e4SLinus Torvalds spin_unlock(&shmem_swaplist_lock); 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds BUG_ON(inode->i_blocks); 6960edd73b3SHugh Dickins if (sbinfo->max_inodes) { 6971da177e4SLinus Torvalds spin_lock(&sbinfo->stat_lock); 6981da177e4SLinus Torvalds sbinfo->free_inodes++; 6991da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds clear_inode(inode); 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir) 7051da177e4SLinus Torvalds { 7061da177e4SLinus Torvalds swp_entry_t *ptr; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds for (ptr = dir; ptr < edir; ptr++) { 7091da177e4SLinus Torvalds if (ptr->val == entry.val) 7101da177e4SLinus Torvalds return ptr - dir; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds return -1; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page) 7161da177e4SLinus Torvalds { 7171da177e4SLinus Torvalds struct inode *inode; 7181da177e4SLinus Torvalds unsigned long idx; 7191da177e4SLinus Torvalds unsigned long size; 7201da177e4SLinus Torvalds unsigned long limit; 7211da177e4SLinus Torvalds unsigned long stage; 7221da177e4SLinus Torvalds struct page **dir; 7231da177e4SLinus Torvalds struct page *subdir; 7241da177e4SLinus Torvalds swp_entry_t *ptr; 7251da177e4SLinus Torvalds int offset; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds idx = 0; 7281da177e4SLinus Torvalds ptr = info->i_direct; 7291da177e4SLinus Torvalds spin_lock(&info->lock); 7301da177e4SLinus Torvalds limit = info->next_index; 7311da177e4SLinus Torvalds size = limit; 7321da177e4SLinus Torvalds if (size > SHMEM_NR_DIRECT) 7331da177e4SLinus Torvalds size = SHMEM_NR_DIRECT; 7341da177e4SLinus Torvalds offset = shmem_find_swp(entry, ptr, ptr+size); 7351da177e4SLinus Torvalds if (offset >= 0) { 7361da177e4SLinus Torvalds shmem_swp_balance_unmap(); 7371da177e4SLinus Torvalds goto found; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds if (!info->i_indirect) 7401da177e4SLinus Torvalds goto lost2; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds dir = shmem_dir_map(info->i_indirect); 7431da177e4SLinus Torvalds stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2; 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) { 7461da177e4SLinus Torvalds if (unlikely(idx == stage)) { 7471da177e4SLinus Torvalds shmem_dir_unmap(dir-1); 7481da177e4SLinus Torvalds dir = shmem_dir_map(info->i_indirect) + 7491da177e4SLinus Torvalds ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE; 7501da177e4SLinus Torvalds while (!*dir) { 7511da177e4SLinus Torvalds dir++; 7521da177e4SLinus Torvalds idx += ENTRIES_PER_PAGEPAGE; 7531da177e4SLinus Torvalds if (idx >= limit) 7541da177e4SLinus Torvalds goto lost1; 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds stage = idx + ENTRIES_PER_PAGEPAGE; 7571da177e4SLinus Torvalds subdir = *dir; 7581da177e4SLinus Torvalds shmem_dir_unmap(dir); 7591da177e4SLinus Torvalds dir = shmem_dir_map(subdir); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds subdir = *dir; 7624c21e2f2SHugh Dickins if (subdir && page_private(subdir)) { 7631da177e4SLinus Torvalds ptr = shmem_swp_map(subdir); 7641da177e4SLinus Torvalds size = limit - idx; 7651da177e4SLinus Torvalds if (size > ENTRIES_PER_PAGE) 7661da177e4SLinus Torvalds size = ENTRIES_PER_PAGE; 7671da177e4SLinus Torvalds offset = shmem_find_swp(entry, ptr, ptr+size); 7681da177e4SLinus Torvalds if (offset >= 0) { 7691da177e4SLinus Torvalds shmem_dir_unmap(dir); 7701da177e4SLinus Torvalds goto found; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds shmem_swp_unmap(ptr); 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds lost1: 7761da177e4SLinus Torvalds shmem_dir_unmap(dir-1); 7771da177e4SLinus Torvalds lost2: 7781da177e4SLinus Torvalds spin_unlock(&info->lock); 7791da177e4SLinus Torvalds return 0; 7801da177e4SLinus Torvalds found: 7811da177e4SLinus Torvalds idx += offset; 7821da177e4SLinus Torvalds inode = &info->vfs_inode; 7831da177e4SLinus Torvalds if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) { 7841da177e4SLinus Torvalds info->flags |= SHMEM_PAGEIN; 7851da177e4SLinus Torvalds shmem_swp_set(info, ptr + offset, 0); 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds shmem_swp_unmap(ptr); 7881da177e4SLinus Torvalds spin_unlock(&info->lock); 7891da177e4SLinus Torvalds /* 7901da177e4SLinus Torvalds * Decrement swap count even when the entry is left behind: 7911da177e4SLinus Torvalds * try_to_unuse will skip over mms, then reincrement count. 7921da177e4SLinus Torvalds */ 7931da177e4SLinus Torvalds swap_free(entry); 7941da177e4SLinus Torvalds return 1; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /* 7981da177e4SLinus Torvalds * shmem_unuse() search for an eventually swapped out shmem page. 7991da177e4SLinus Torvalds */ 8001da177e4SLinus Torvalds int shmem_unuse(swp_entry_t entry, struct page *page) 8011da177e4SLinus Torvalds { 8021da177e4SLinus Torvalds struct list_head *p, *next; 8031da177e4SLinus Torvalds struct shmem_inode_info *info; 8041da177e4SLinus Torvalds int found = 0; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds spin_lock(&shmem_swaplist_lock); 8071da177e4SLinus Torvalds list_for_each_safe(p, next, &shmem_swaplist) { 8081da177e4SLinus Torvalds info = list_entry(p, struct shmem_inode_info, swaplist); 8091da177e4SLinus Torvalds if (!info->swapped) 8101da177e4SLinus Torvalds list_del_init(&info->swaplist); 8111da177e4SLinus Torvalds else if (shmem_unuse_inode(info, entry, page)) { 8121da177e4SLinus Torvalds /* move head to start search for next from here */ 8131da177e4SLinus Torvalds list_move_tail(&shmem_swaplist, &info->swaplist); 8141da177e4SLinus Torvalds found = 1; 8151da177e4SLinus Torvalds break; 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds spin_unlock(&shmem_swaplist_lock); 8191da177e4SLinus Torvalds return found; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds /* 8231da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 8241da177e4SLinus Torvalds */ 8251da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 8261da177e4SLinus Torvalds { 8271da177e4SLinus Torvalds struct shmem_inode_info *info; 8281da177e4SLinus Torvalds swp_entry_t *entry, swap; 8291da177e4SLinus Torvalds struct address_space *mapping; 8301da177e4SLinus Torvalds unsigned long index; 8311da177e4SLinus Torvalds struct inode *inode; 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 8341da177e4SLinus Torvalds BUG_ON(page_mapped(page)); 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds mapping = page->mapping; 8371da177e4SLinus Torvalds index = page->index; 8381da177e4SLinus Torvalds inode = mapping->host; 8391da177e4SLinus Torvalds info = SHMEM_I(inode); 8401da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 8411da177e4SLinus Torvalds goto redirty; 8421da177e4SLinus Torvalds swap = get_swap_page(); 8431da177e4SLinus Torvalds if (!swap.val) 8441da177e4SLinus Torvalds goto redirty; 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds spin_lock(&info->lock); 8471da177e4SLinus Torvalds shmem_recalc_inode(inode); 8481da177e4SLinus Torvalds if (index >= info->next_index) { 8491da177e4SLinus Torvalds BUG_ON(!(info->flags & SHMEM_TRUNCATE)); 8501da177e4SLinus Torvalds goto unlock; 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds entry = shmem_swp_entry(info, index, NULL); 8531da177e4SLinus Torvalds BUG_ON(!entry); 8541da177e4SLinus Torvalds BUG_ON(entry->val); 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds if (move_to_swap_cache(page, swap) == 0) { 8571da177e4SLinus Torvalds shmem_swp_set(info, entry, swap.val); 8581da177e4SLinus Torvalds shmem_swp_unmap(entry); 8591da177e4SLinus Torvalds spin_unlock(&info->lock); 8601da177e4SLinus Torvalds if (list_empty(&info->swaplist)) { 8611da177e4SLinus Torvalds spin_lock(&shmem_swaplist_lock); 8621da177e4SLinus Torvalds /* move instead of add in case we're racing */ 8631da177e4SLinus Torvalds list_move_tail(&info->swaplist, &shmem_swaplist); 8641da177e4SLinus Torvalds spin_unlock(&shmem_swaplist_lock); 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds unlock_page(page); 8671da177e4SLinus Torvalds return 0; 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds shmem_swp_unmap(entry); 8711da177e4SLinus Torvalds unlock: 8721da177e4SLinus Torvalds spin_unlock(&info->lock); 8731da177e4SLinus Torvalds swap_free(swap); 8741da177e4SLinus Torvalds redirty: 8751da177e4SLinus Torvalds set_page_dirty(page); 876994fc28cSZach Brown return AOP_WRITEPAGE_ACTIVATE; /* Return with the page locked */ 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds #ifdef CONFIG_NUMA 880d15c023bSHugh Dickins static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes) 881b00dc3adSHugh Dickins { 882b00dc3adSHugh Dickins char *nodelist = strchr(value, ':'); 883b00dc3adSHugh Dickins int err = 1; 884b00dc3adSHugh Dickins 885b00dc3adSHugh Dickins if (nodelist) { 886b00dc3adSHugh Dickins /* NUL-terminate policy string */ 887b00dc3adSHugh Dickins *nodelist++ = '\0'; 888b00dc3adSHugh Dickins if (nodelist_parse(nodelist, *policy_nodes)) 889b00dc3adSHugh Dickins goto out; 890b00dc3adSHugh Dickins } 891b00dc3adSHugh Dickins if (!strcmp(value, "default")) { 892b00dc3adSHugh Dickins *policy = MPOL_DEFAULT; 893b00dc3adSHugh Dickins /* Don't allow a nodelist */ 894b00dc3adSHugh Dickins if (!nodelist) 895b00dc3adSHugh Dickins err = 0; 896b00dc3adSHugh Dickins } else if (!strcmp(value, "prefer")) { 897b00dc3adSHugh Dickins *policy = MPOL_PREFERRED; 898b00dc3adSHugh Dickins /* Insist on a nodelist of one node only */ 899b00dc3adSHugh Dickins if (nodelist) { 900b00dc3adSHugh Dickins char *rest = nodelist; 901b00dc3adSHugh Dickins while (isdigit(*rest)) 902b00dc3adSHugh Dickins rest++; 903b00dc3adSHugh Dickins if (!*rest) 904b00dc3adSHugh Dickins err = 0; 905b00dc3adSHugh Dickins } 906b00dc3adSHugh Dickins } else if (!strcmp(value, "bind")) { 907b00dc3adSHugh Dickins *policy = MPOL_BIND; 908b00dc3adSHugh Dickins /* Insist on a nodelist */ 909b00dc3adSHugh Dickins if (nodelist) 910b00dc3adSHugh Dickins err = 0; 911b00dc3adSHugh Dickins } else if (!strcmp(value, "interleave")) { 912b00dc3adSHugh Dickins *policy = MPOL_INTERLEAVE; 913b00dc3adSHugh Dickins /* Default to nodes online if no nodelist */ 914b00dc3adSHugh Dickins if (!nodelist) 915b00dc3adSHugh Dickins *policy_nodes = node_online_map; 916b00dc3adSHugh Dickins err = 0; 917b00dc3adSHugh Dickins } 918b00dc3adSHugh Dickins out: 919b00dc3adSHugh Dickins /* Restore string for error message */ 920b00dc3adSHugh Dickins if (nodelist) 921b00dc3adSHugh Dickins *--nodelist = ':'; 922b00dc3adSHugh Dickins return err; 923b00dc3adSHugh Dickins } 924b00dc3adSHugh Dickins 9251da177e4SLinus Torvalds static struct page *shmem_swapin_async(struct shared_policy *p, 9261da177e4SLinus Torvalds swp_entry_t entry, unsigned long idx) 9271da177e4SLinus Torvalds { 9281da177e4SLinus Torvalds struct page *page; 9291da177e4SLinus Torvalds struct vm_area_struct pvma; 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds /* Create a pseudo vma that just contains the policy */ 9321da177e4SLinus Torvalds memset(&pvma, 0, sizeof(struct vm_area_struct)); 9331da177e4SLinus Torvalds pvma.vm_end = PAGE_SIZE; 9341da177e4SLinus Torvalds pvma.vm_pgoff = idx; 9351da177e4SLinus Torvalds pvma.vm_policy = mpol_shared_policy_lookup(p, idx); 9361da177e4SLinus Torvalds page = read_swap_cache_async(entry, &pvma, 0); 9371da177e4SLinus Torvalds mpol_free(pvma.vm_policy); 9381da177e4SLinus Torvalds return page; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds struct page *shmem_swapin(struct shmem_inode_info *info, swp_entry_t entry, 9421da177e4SLinus Torvalds unsigned long idx) 9431da177e4SLinus Torvalds { 9441da177e4SLinus Torvalds struct shared_policy *p = &info->policy; 9451da177e4SLinus Torvalds int i, num; 9461da177e4SLinus Torvalds struct page *page; 9471da177e4SLinus Torvalds unsigned long offset; 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds num = valid_swaphandles(entry, &offset); 9501da177e4SLinus Torvalds for (i = 0; i < num; offset++, i++) { 9511da177e4SLinus Torvalds page = shmem_swapin_async(p, 9521da177e4SLinus Torvalds swp_entry(swp_type(entry), offset), idx); 9531da177e4SLinus Torvalds if (!page) 9541da177e4SLinus Torvalds break; 9551da177e4SLinus Torvalds page_cache_release(page); 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds lru_add_drain(); /* Push any new pages onto the LRU now */ 9581da177e4SLinus Torvalds return shmem_swapin_async(p, entry, idx); 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds static struct page * 9626daa0e28SAl Viro shmem_alloc_page(gfp_t gfp, struct shmem_inode_info *info, 9631da177e4SLinus Torvalds unsigned long idx) 9641da177e4SLinus Torvalds { 9651da177e4SLinus Torvalds struct vm_area_struct pvma; 9661da177e4SLinus Torvalds struct page *page; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds memset(&pvma, 0, sizeof(struct vm_area_struct)); 9691da177e4SLinus Torvalds pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx); 9701da177e4SLinus Torvalds pvma.vm_pgoff = idx; 9711da177e4SLinus Torvalds pvma.vm_end = PAGE_SIZE; 9721da177e4SLinus Torvalds page = alloc_page_vma(gfp | __GFP_ZERO, &pvma, 0); 9731da177e4SLinus Torvalds mpol_free(pvma.vm_policy); 9741da177e4SLinus Torvalds return page; 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds #else 977b00dc3adSHugh Dickins static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes) 978b00dc3adSHugh Dickins { 979b00dc3adSHugh Dickins return 1; 980b00dc3adSHugh Dickins } 981b00dc3adSHugh Dickins 9821da177e4SLinus Torvalds static inline struct page * 9831da177e4SLinus Torvalds shmem_swapin(struct shmem_inode_info *info,swp_entry_t entry,unsigned long idx) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds swapin_readahead(entry, 0, NULL); 9861da177e4SLinus Torvalds return read_swap_cache_async(entry, NULL, 0); 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds static inline struct page * 990dd0fc66fSAl Viro shmem_alloc_page(gfp_t gfp,struct shmem_inode_info *info, unsigned long idx) 9911da177e4SLinus Torvalds { 9921da177e4SLinus Torvalds return alloc_page(gfp | __GFP_ZERO); 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds #endif 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds /* 9971da177e4SLinus Torvalds * shmem_getpage - either get the page from swap or allocate a new one 9981da177e4SLinus Torvalds * 9991da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 10001da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 10011da177e4SLinus Torvalds * entry since a page cannot live in both the swap and page cache 10021da177e4SLinus Torvalds */ 10031da177e4SLinus Torvalds static int shmem_getpage(struct inode *inode, unsigned long idx, 10041da177e4SLinus Torvalds struct page **pagep, enum sgp_type sgp, int *type) 10051da177e4SLinus Torvalds { 10061da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 10071da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 10081da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 10091da177e4SLinus Torvalds struct page *filepage = *pagep; 10101da177e4SLinus Torvalds struct page *swappage; 10111da177e4SLinus Torvalds swp_entry_t *entry; 10121da177e4SLinus Torvalds swp_entry_t swap; 10131da177e4SLinus Torvalds int error; 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds if (idx >= SHMEM_MAX_INDEX) 10161da177e4SLinus Torvalds return -EFBIG; 10171da177e4SLinus Torvalds /* 10181da177e4SLinus Torvalds * Normally, filepage is NULL on entry, and either found 10191da177e4SLinus Torvalds * uptodate immediately, or allocated and zeroed, or read 10201da177e4SLinus Torvalds * in under swappage, which is then assigned to filepage. 10211da177e4SLinus Torvalds * But shmem_prepare_write passes in a locked filepage, 10221da177e4SLinus Torvalds * which may be found not uptodate by other callers too, 10231da177e4SLinus Torvalds * and may need to be copied from the swappage read in. 10241da177e4SLinus Torvalds */ 10251da177e4SLinus Torvalds repeat: 10261da177e4SLinus Torvalds if (!filepage) 10271da177e4SLinus Torvalds filepage = find_lock_page(mapping, idx); 10281da177e4SLinus Torvalds if (filepage && PageUptodate(filepage)) 10291da177e4SLinus Torvalds goto done; 10301da177e4SLinus Torvalds error = 0; 10311da177e4SLinus Torvalds if (sgp == SGP_QUICK) 10321da177e4SLinus Torvalds goto failed; 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds spin_lock(&info->lock); 10351da177e4SLinus Torvalds shmem_recalc_inode(inode); 10361da177e4SLinus Torvalds entry = shmem_swp_alloc(info, idx, sgp); 10371da177e4SLinus Torvalds if (IS_ERR(entry)) { 10381da177e4SLinus Torvalds spin_unlock(&info->lock); 10391da177e4SLinus Torvalds error = PTR_ERR(entry); 10401da177e4SLinus Torvalds goto failed; 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds swap = *entry; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds if (swap.val) { 10451da177e4SLinus Torvalds /* Look it up and read it in.. */ 10461da177e4SLinus Torvalds swappage = lookup_swap_cache(swap); 10471da177e4SLinus Torvalds if (!swappage) { 10481da177e4SLinus Torvalds shmem_swp_unmap(entry); 10491da177e4SLinus Torvalds spin_unlock(&info->lock); 10501da177e4SLinus Torvalds /* here we actually do the io */ 10511da177e4SLinus Torvalds if (type && *type == VM_FAULT_MINOR) { 10521da177e4SLinus Torvalds inc_page_state(pgmajfault); 10531da177e4SLinus Torvalds *type = VM_FAULT_MAJOR; 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds swappage = shmem_swapin(info, swap, idx); 10561da177e4SLinus Torvalds if (!swappage) { 10571da177e4SLinus Torvalds spin_lock(&info->lock); 10581da177e4SLinus Torvalds entry = shmem_swp_alloc(info, idx, sgp); 10591da177e4SLinus Torvalds if (IS_ERR(entry)) 10601da177e4SLinus Torvalds error = PTR_ERR(entry); 10611da177e4SLinus Torvalds else { 10621da177e4SLinus Torvalds if (entry->val == swap.val) 10631da177e4SLinus Torvalds error = -ENOMEM; 10641da177e4SLinus Torvalds shmem_swp_unmap(entry); 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds spin_unlock(&info->lock); 10671da177e4SLinus Torvalds if (error) 10681da177e4SLinus Torvalds goto failed; 10691da177e4SLinus Torvalds goto repeat; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds wait_on_page_locked(swappage); 10721da177e4SLinus Torvalds page_cache_release(swappage); 10731da177e4SLinus Torvalds goto repeat; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 10771da177e4SLinus Torvalds if (TestSetPageLocked(swappage)) { 10781da177e4SLinus Torvalds shmem_swp_unmap(entry); 10791da177e4SLinus Torvalds spin_unlock(&info->lock); 10801da177e4SLinus Torvalds wait_on_page_locked(swappage); 10811da177e4SLinus Torvalds page_cache_release(swappage); 10821da177e4SLinus Torvalds goto repeat; 10831da177e4SLinus Torvalds } 1084b16664e4SChristoph Lameter if (!PageSwapCache(swappage)) { 1085b16664e4SChristoph Lameter /* Page migration has occured */ 1086b16664e4SChristoph Lameter shmem_swp_unmap(entry); 1087b16664e4SChristoph Lameter spin_unlock(&info->lock); 1088b16664e4SChristoph Lameter unlock_page(swappage); 1089b16664e4SChristoph Lameter page_cache_release(swappage); 1090b16664e4SChristoph Lameter goto repeat; 1091b16664e4SChristoph Lameter } 10921da177e4SLinus Torvalds if (PageWriteback(swappage)) { 10931da177e4SLinus Torvalds shmem_swp_unmap(entry); 10941da177e4SLinus Torvalds spin_unlock(&info->lock); 10951da177e4SLinus Torvalds wait_on_page_writeback(swappage); 10961da177e4SLinus Torvalds unlock_page(swappage); 10971da177e4SLinus Torvalds page_cache_release(swappage); 10981da177e4SLinus Torvalds goto repeat; 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds if (!PageUptodate(swappage)) { 11011da177e4SLinus Torvalds shmem_swp_unmap(entry); 11021da177e4SLinus Torvalds spin_unlock(&info->lock); 11031da177e4SLinus Torvalds unlock_page(swappage); 11041da177e4SLinus Torvalds page_cache_release(swappage); 11051da177e4SLinus Torvalds error = -EIO; 11061da177e4SLinus Torvalds goto failed; 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds if (filepage) { 11101da177e4SLinus Torvalds shmem_swp_set(info, entry, 0); 11111da177e4SLinus Torvalds shmem_swp_unmap(entry); 11121da177e4SLinus Torvalds delete_from_swap_cache(swappage); 11131da177e4SLinus Torvalds spin_unlock(&info->lock); 11141da177e4SLinus Torvalds copy_highpage(filepage, swappage); 11151da177e4SLinus Torvalds unlock_page(swappage); 11161da177e4SLinus Torvalds page_cache_release(swappage); 11171da177e4SLinus Torvalds flush_dcache_page(filepage); 11181da177e4SLinus Torvalds SetPageUptodate(filepage); 11191da177e4SLinus Torvalds set_page_dirty(filepage); 11201da177e4SLinus Torvalds swap_free(swap); 11211da177e4SLinus Torvalds } else if (!(error = move_from_swap_cache( 11221da177e4SLinus Torvalds swappage, idx, mapping))) { 11231da177e4SLinus Torvalds info->flags |= SHMEM_PAGEIN; 11241da177e4SLinus Torvalds shmem_swp_set(info, entry, 0); 11251da177e4SLinus Torvalds shmem_swp_unmap(entry); 11261da177e4SLinus Torvalds spin_unlock(&info->lock); 11271da177e4SLinus Torvalds filepage = swappage; 11281da177e4SLinus Torvalds swap_free(swap); 11291da177e4SLinus Torvalds } else { 11301da177e4SLinus Torvalds shmem_swp_unmap(entry); 11311da177e4SLinus Torvalds spin_unlock(&info->lock); 11321da177e4SLinus Torvalds unlock_page(swappage); 11331da177e4SLinus Torvalds page_cache_release(swappage); 11341da177e4SLinus Torvalds if (error == -ENOMEM) { 11351da177e4SLinus Torvalds /* let kswapd refresh zone for GFP_ATOMICs */ 11361da177e4SLinus Torvalds blk_congestion_wait(WRITE, HZ/50); 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds goto repeat; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds } else if (sgp == SGP_READ && !filepage) { 11411da177e4SLinus Torvalds shmem_swp_unmap(entry); 11421da177e4SLinus Torvalds filepage = find_get_page(mapping, idx); 11431da177e4SLinus Torvalds if (filepage && 11441da177e4SLinus Torvalds (!PageUptodate(filepage) || TestSetPageLocked(filepage))) { 11451da177e4SLinus Torvalds spin_unlock(&info->lock); 11461da177e4SLinus Torvalds wait_on_page_locked(filepage); 11471da177e4SLinus Torvalds page_cache_release(filepage); 11481da177e4SLinus Torvalds filepage = NULL; 11491da177e4SLinus Torvalds goto repeat; 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds spin_unlock(&info->lock); 11521da177e4SLinus Torvalds } else { 11531da177e4SLinus Torvalds shmem_swp_unmap(entry); 11541da177e4SLinus Torvalds sbinfo = SHMEM_SB(inode->i_sb); 11550edd73b3SHugh Dickins if (sbinfo->max_blocks) { 11561da177e4SLinus Torvalds spin_lock(&sbinfo->stat_lock); 11571da177e4SLinus Torvalds if (sbinfo->free_blocks == 0 || 11581da177e4SLinus Torvalds shmem_acct_block(info->flags)) { 11591da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 11601da177e4SLinus Torvalds spin_unlock(&info->lock); 11611da177e4SLinus Torvalds error = -ENOSPC; 11621da177e4SLinus Torvalds goto failed; 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds sbinfo->free_blocks--; 11651da177e4SLinus Torvalds inode->i_blocks += BLOCKS_PER_PAGE; 11661da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 11671da177e4SLinus Torvalds } else if (shmem_acct_block(info->flags)) { 11681da177e4SLinus Torvalds spin_unlock(&info->lock); 11691da177e4SLinus Torvalds error = -ENOSPC; 11701da177e4SLinus Torvalds goto failed; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds if (!filepage) { 11741da177e4SLinus Torvalds spin_unlock(&info->lock); 11751da177e4SLinus Torvalds filepage = shmem_alloc_page(mapping_gfp_mask(mapping), 11761da177e4SLinus Torvalds info, 11771da177e4SLinus Torvalds idx); 11781da177e4SLinus Torvalds if (!filepage) { 11791da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, 1); 11801da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 11811da177e4SLinus Torvalds error = -ENOMEM; 11821da177e4SLinus Torvalds goto failed; 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds spin_lock(&info->lock); 11861da177e4SLinus Torvalds entry = shmem_swp_alloc(info, idx, sgp); 11871da177e4SLinus Torvalds if (IS_ERR(entry)) 11881da177e4SLinus Torvalds error = PTR_ERR(entry); 11891da177e4SLinus Torvalds else { 11901da177e4SLinus Torvalds swap = *entry; 11911da177e4SLinus Torvalds shmem_swp_unmap(entry); 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds if (error || swap.val || 0 != add_to_page_cache_lru( 11941da177e4SLinus Torvalds filepage, mapping, idx, GFP_ATOMIC)) { 11951da177e4SLinus Torvalds spin_unlock(&info->lock); 11961da177e4SLinus Torvalds page_cache_release(filepage); 11971da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, 1); 11981da177e4SLinus Torvalds shmem_free_blocks(inode, 1); 11991da177e4SLinus Torvalds filepage = NULL; 12001da177e4SLinus Torvalds if (error) 12011da177e4SLinus Torvalds goto failed; 12021da177e4SLinus Torvalds goto repeat; 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds info->flags |= SHMEM_PAGEIN; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds info->alloced++; 12081da177e4SLinus Torvalds spin_unlock(&info->lock); 12091da177e4SLinus Torvalds flush_dcache_page(filepage); 12101da177e4SLinus Torvalds SetPageUptodate(filepage); 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds done: 12131da177e4SLinus Torvalds if (*pagep != filepage) { 12141da177e4SLinus Torvalds unlock_page(filepage); 12151da177e4SLinus Torvalds *pagep = filepage; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds return 0; 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds failed: 12201da177e4SLinus Torvalds if (*pagep != filepage) { 12211da177e4SLinus Torvalds unlock_page(filepage); 12221da177e4SLinus Torvalds page_cache_release(filepage); 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds return error; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds struct page *shmem_nopage(struct vm_area_struct *vma, unsigned long address, int *type) 12281da177e4SLinus Torvalds { 12291da177e4SLinus Torvalds struct inode *inode = vma->vm_file->f_dentry->d_inode; 12301da177e4SLinus Torvalds struct page *page = NULL; 12311da177e4SLinus Torvalds unsigned long idx; 12321da177e4SLinus Torvalds int error; 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds idx = (address - vma->vm_start) >> PAGE_SHIFT; 12351da177e4SLinus Torvalds idx += vma->vm_pgoff; 12361da177e4SLinus Torvalds idx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT; 12371da177e4SLinus Torvalds if (((loff_t) idx << PAGE_CACHE_SHIFT) >= i_size_read(inode)) 12381da177e4SLinus Torvalds return NOPAGE_SIGBUS; 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds error = shmem_getpage(inode, idx, &page, SGP_CACHE, type); 12411da177e4SLinus Torvalds if (error) 12421da177e4SLinus Torvalds return (error == -ENOMEM)? NOPAGE_OOM: NOPAGE_SIGBUS; 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds mark_page_accessed(page); 12451da177e4SLinus Torvalds return page; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds static int shmem_populate(struct vm_area_struct *vma, 12491da177e4SLinus Torvalds unsigned long addr, unsigned long len, 12501da177e4SLinus Torvalds pgprot_t prot, unsigned long pgoff, int nonblock) 12511da177e4SLinus Torvalds { 12521da177e4SLinus Torvalds struct inode *inode = vma->vm_file->f_dentry->d_inode; 12531da177e4SLinus Torvalds struct mm_struct *mm = vma->vm_mm; 12541da177e4SLinus Torvalds enum sgp_type sgp = nonblock? SGP_QUICK: SGP_CACHE; 12551da177e4SLinus Torvalds unsigned long size; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; 12581da177e4SLinus Torvalds if (pgoff >= size || pgoff + (len >> PAGE_SHIFT) > size) 12591da177e4SLinus Torvalds return -EINVAL; 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds while ((long) len > 0) { 12621da177e4SLinus Torvalds struct page *page = NULL; 12631da177e4SLinus Torvalds int err; 12641da177e4SLinus Torvalds /* 12651da177e4SLinus Torvalds * Will need changing if PAGE_CACHE_SIZE != PAGE_SIZE 12661da177e4SLinus Torvalds */ 12671da177e4SLinus Torvalds err = shmem_getpage(inode, pgoff, &page, sgp, NULL); 12681da177e4SLinus Torvalds if (err) 12691da177e4SLinus Torvalds return err; 1270d44ed4f8SPaolo 'Blaisorblade' Giarrusso /* Page may still be null, but only if nonblock was set. */ 12711da177e4SLinus Torvalds if (page) { 12721da177e4SLinus Torvalds mark_page_accessed(page); 12731da177e4SLinus Torvalds err = install_page(mm, vma, addr, page, prot); 12741da177e4SLinus Torvalds if (err) { 12751da177e4SLinus Torvalds page_cache_release(page); 12761da177e4SLinus Torvalds return err; 12771da177e4SLinus Torvalds } 127865500d23SHugh Dickins } else if (vma->vm_flags & VM_NONLINEAR) { 1279d44ed4f8SPaolo 'Blaisorblade' Giarrusso /* No page was found just because we can't read it in 1280d44ed4f8SPaolo 'Blaisorblade' Giarrusso * now (being here implies nonblock != 0), but the page 1281d44ed4f8SPaolo 'Blaisorblade' Giarrusso * may exist, so set the PTE to fault it in later. */ 12821da177e4SLinus Torvalds err = install_file_pte(mm, vma, addr, pgoff, prot); 12831da177e4SLinus Torvalds if (err) 12841da177e4SLinus Torvalds return err; 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds len -= PAGE_SIZE; 12881da177e4SLinus Torvalds addr += PAGE_SIZE; 12891da177e4SLinus Torvalds pgoff++; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds return 0; 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds #ifdef CONFIG_NUMA 12951da177e4SLinus Torvalds int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new) 12961da177e4SLinus Torvalds { 12971da177e4SLinus Torvalds struct inode *i = vma->vm_file->f_dentry->d_inode; 12981da177e4SLinus Torvalds return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new); 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds struct mempolicy * 13021da177e4SLinus Torvalds shmem_get_policy(struct vm_area_struct *vma, unsigned long addr) 13031da177e4SLinus Torvalds { 13041da177e4SLinus Torvalds struct inode *i = vma->vm_file->f_dentry->d_inode; 13051da177e4SLinus Torvalds unsigned long idx; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 13081da177e4SLinus Torvalds return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx); 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds #endif 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 13131da177e4SLinus Torvalds { 13141da177e4SLinus Torvalds struct inode *inode = file->f_dentry->d_inode; 13151da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 13161da177e4SLinus Torvalds int retval = -ENOMEM; 13171da177e4SLinus Torvalds 13181da177e4SLinus Torvalds spin_lock(&info->lock); 13191da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 13201da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 13211da177e4SLinus Torvalds goto out_nomem; 13221da177e4SLinus Torvalds info->flags |= VM_LOCKED; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 13251da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 13261da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds retval = 0; 13291da177e4SLinus Torvalds out_nomem: 13301da177e4SLinus Torvalds spin_unlock(&info->lock); 13311da177e4SLinus Torvalds return retval; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 1334b0e15190SDavid Howells int shmem_mmap(struct file *file, struct vm_area_struct *vma) 13351da177e4SLinus Torvalds { 13361da177e4SLinus Torvalds file_accessed(file); 13371da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 13381da177e4SLinus Torvalds return 0; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds static struct inode * 13421da177e4SLinus Torvalds shmem_get_inode(struct super_block *sb, int mode, dev_t dev) 13431da177e4SLinus Torvalds { 13441da177e4SLinus Torvalds struct inode *inode; 13451da177e4SLinus Torvalds struct shmem_inode_info *info; 13461da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 13471da177e4SLinus Torvalds 13480edd73b3SHugh Dickins if (sbinfo->max_inodes) { 13491da177e4SLinus Torvalds spin_lock(&sbinfo->stat_lock); 13501da177e4SLinus Torvalds if (!sbinfo->free_inodes) { 13511da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 13521da177e4SLinus Torvalds return NULL; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds sbinfo->free_inodes--; 13551da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds inode = new_inode(sb); 13591da177e4SLinus Torvalds if (inode) { 13601da177e4SLinus Torvalds inode->i_mode = mode; 13611da177e4SLinus Torvalds inode->i_uid = current->fsuid; 13621da177e4SLinus Torvalds inode->i_gid = current->fsgid; 13631da177e4SLinus Torvalds inode->i_blksize = PAGE_CACHE_SIZE; 13641da177e4SLinus Torvalds inode->i_blocks = 0; 13651da177e4SLinus Torvalds inode->i_mapping->a_ops = &shmem_aops; 13661da177e4SLinus Torvalds inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; 13671da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 13681da177e4SLinus Torvalds info = SHMEM_I(inode); 13691da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 13701da177e4SLinus Torvalds spin_lock_init(&info->lock); 13711da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds switch (mode & S_IFMT) { 13741da177e4SLinus Torvalds default: 13751da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 13761da177e4SLinus Torvalds break; 13771da177e4SLinus Torvalds case S_IFREG: 13781da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 13791da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 13807339ff83SRobin Holt mpol_shared_policy_init(&info->policy, sbinfo->policy, 13817339ff83SRobin Holt &sbinfo->policy_nodes); 13821da177e4SLinus Torvalds break; 13831da177e4SLinus Torvalds case S_IFDIR: 13841da177e4SLinus Torvalds inode->i_nlink++; 13851da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 13861da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 13871da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 13881da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 13891da177e4SLinus Torvalds break; 13901da177e4SLinus Torvalds case S_IFLNK: 13911da177e4SLinus Torvalds /* 13921da177e4SLinus Torvalds * Must not load anything in the rbtree, 13931da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 13941da177e4SLinus Torvalds */ 13957339ff83SRobin Holt mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, 13967339ff83SRobin Holt NULL); 13971da177e4SLinus Torvalds break; 13981da177e4SLinus Torvalds } 13990edd73b3SHugh Dickins } else if (sbinfo->max_inodes) { 14001da177e4SLinus Torvalds spin_lock(&sbinfo->stat_lock); 14011da177e4SLinus Torvalds sbinfo->free_inodes++; 14021da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds return inode; 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 14081da177e4SLinus Torvalds static struct inode_operations shmem_symlink_inode_operations; 14091da177e4SLinus Torvalds static struct inode_operations shmem_symlink_inline_operations; 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds /* 14121da177e4SLinus Torvalds * Normally tmpfs makes no use of shmem_prepare_write, but it 14131da177e4SLinus Torvalds * lets a tmpfs file be used read-write below the loop driver. 14141da177e4SLinus Torvalds */ 14151da177e4SLinus Torvalds static int 14161da177e4SLinus Torvalds shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to) 14171da177e4SLinus Torvalds { 14181da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 14191da177e4SLinus Torvalds return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL); 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds static ssize_t 14231da177e4SLinus Torvalds shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) 14241da177e4SLinus Torvalds { 14251da177e4SLinus Torvalds struct inode *inode = file->f_dentry->d_inode; 14261da177e4SLinus Torvalds loff_t pos; 14271da177e4SLinus Torvalds unsigned long written; 14281da177e4SLinus Torvalds ssize_t err; 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds if ((ssize_t) count < 0) 14311da177e4SLinus Torvalds return -EINVAL; 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds if (!access_ok(VERIFY_READ, buf, count)) 14341da177e4SLinus Torvalds return -EFAULT; 14351da177e4SLinus Torvalds 14361b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds pos = *ppos; 14391da177e4SLinus Torvalds written = 0; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds err = generic_write_checks(file, &pos, &count, 0); 14421da177e4SLinus Torvalds if (err || !count) 14431da177e4SLinus Torvalds goto out; 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds err = remove_suid(file->f_dentry); 14461da177e4SLinus Torvalds if (err) 14471da177e4SLinus Torvalds goto out; 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds inode->i_ctime = inode->i_mtime = CURRENT_TIME; 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds do { 14521da177e4SLinus Torvalds struct page *page = NULL; 14531da177e4SLinus Torvalds unsigned long bytes, index, offset; 14541da177e4SLinus Torvalds char *kaddr; 14551da177e4SLinus Torvalds int left; 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */ 14581da177e4SLinus Torvalds index = pos >> PAGE_CACHE_SHIFT; 14591da177e4SLinus Torvalds bytes = PAGE_CACHE_SIZE - offset; 14601da177e4SLinus Torvalds if (bytes > count) 14611da177e4SLinus Torvalds bytes = count; 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds /* 14641da177e4SLinus Torvalds * We don't hold page lock across copy from user - 14651da177e4SLinus Torvalds * what would it guard against? - so no deadlock here. 14661da177e4SLinus Torvalds * But it still may be a good idea to prefault below. 14671da177e4SLinus Torvalds */ 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL); 14701da177e4SLinus Torvalds if (err) 14711da177e4SLinus Torvalds break; 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds left = bytes; 14741da177e4SLinus Torvalds if (PageHighMem(page)) { 14751da177e4SLinus Torvalds volatile unsigned char dummy; 14761da177e4SLinus Torvalds __get_user(dummy, buf); 14771da177e4SLinus Torvalds __get_user(dummy, buf + bytes - 1); 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 14801da177e4SLinus Torvalds left = __copy_from_user_inatomic(kaddr + offset, 14811da177e4SLinus Torvalds buf, bytes); 14821da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds if (left) { 14851da177e4SLinus Torvalds kaddr = kmap(page); 14861da177e4SLinus Torvalds left = __copy_from_user(kaddr + offset, buf, bytes); 14871da177e4SLinus Torvalds kunmap(page); 14881da177e4SLinus Torvalds } 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds written += bytes; 14911da177e4SLinus Torvalds count -= bytes; 14921da177e4SLinus Torvalds pos += bytes; 14931da177e4SLinus Torvalds buf += bytes; 14941da177e4SLinus Torvalds if (pos > inode->i_size) 14951da177e4SLinus Torvalds i_size_write(inode, pos); 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds flush_dcache_page(page); 14981da177e4SLinus Torvalds set_page_dirty(page); 14991da177e4SLinus Torvalds mark_page_accessed(page); 15001da177e4SLinus Torvalds page_cache_release(page); 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds if (left) { 15031da177e4SLinus Torvalds pos -= left; 15041da177e4SLinus Torvalds written -= left; 15051da177e4SLinus Torvalds err = -EFAULT; 15061da177e4SLinus Torvalds break; 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds /* 15101da177e4SLinus Torvalds * Our dirty pages are not counted in nr_dirty, 15111da177e4SLinus Torvalds * and we do not attempt to balance dirty pages. 15121da177e4SLinus Torvalds */ 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds cond_resched(); 15151da177e4SLinus Torvalds } while (count); 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds *ppos = pos; 15181da177e4SLinus Torvalds if (written) 15191da177e4SLinus Torvalds err = written; 15201da177e4SLinus Torvalds out: 15211b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 15221da177e4SLinus Torvalds return err; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) 15261da177e4SLinus Torvalds { 15271da177e4SLinus Torvalds struct inode *inode = filp->f_dentry->d_inode; 15281da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 15291da177e4SLinus Torvalds unsigned long index, offset; 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds index = *ppos >> PAGE_CACHE_SHIFT; 15321da177e4SLinus Torvalds offset = *ppos & ~PAGE_CACHE_MASK; 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds for (;;) { 15351da177e4SLinus Torvalds struct page *page = NULL; 15361da177e4SLinus Torvalds unsigned long end_index, nr, ret; 15371da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 15401da177e4SLinus Torvalds if (index > end_index) 15411da177e4SLinus Torvalds break; 15421da177e4SLinus Torvalds if (index == end_index) { 15431da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 15441da177e4SLinus Torvalds if (nr <= offset) 15451da177e4SLinus Torvalds break; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL); 15491da177e4SLinus Torvalds if (desc->error) { 15501da177e4SLinus Torvalds if (desc->error == -EINVAL) 15511da177e4SLinus Torvalds desc->error = 0; 15521da177e4SLinus Torvalds break; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds /* 15561da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 15571b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 15581da177e4SLinus Torvalds */ 15591da177e4SLinus Torvalds nr = PAGE_CACHE_SIZE; 15601da177e4SLinus Torvalds i_size = i_size_read(inode); 15611da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 15621da177e4SLinus Torvalds if (index == end_index) { 15631da177e4SLinus Torvalds nr = i_size & ~PAGE_CACHE_MASK; 15641da177e4SLinus Torvalds if (nr <= offset) { 15651da177e4SLinus Torvalds if (page) 15661da177e4SLinus Torvalds page_cache_release(page); 15671da177e4SLinus Torvalds break; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds nr -= offset; 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds if (page) { 15731da177e4SLinus Torvalds /* 15741da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 15751da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 15761da177e4SLinus Torvalds * before reading the page on the kernel side. 15771da177e4SLinus Torvalds */ 15781da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 15791da177e4SLinus Torvalds flush_dcache_page(page); 15801da177e4SLinus Torvalds /* 15811da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 15821da177e4SLinus Torvalds */ 15831da177e4SLinus Torvalds if (!offset) 15841da177e4SLinus Torvalds mark_page_accessed(page); 1585b5810039SNick Piggin } else { 15861da177e4SLinus Torvalds page = ZERO_PAGE(0); 1587b5810039SNick Piggin page_cache_get(page); 1588b5810039SNick Piggin } 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds /* 15911da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 15921da177e4SLinus Torvalds * now we can copy it to user space... 15931da177e4SLinus Torvalds * 15941da177e4SLinus Torvalds * The actor routine returns how many bytes were actually used.. 15951da177e4SLinus Torvalds * NOTE! This may not be the same as how much of a user buffer 15961da177e4SLinus Torvalds * we filled up (we may be padding etc), so we can only update 15971da177e4SLinus Torvalds * "pos" here (the actor routine has to update the user buffer 15981da177e4SLinus Torvalds * pointers and the remaining count). 15991da177e4SLinus Torvalds */ 16001da177e4SLinus Torvalds ret = actor(desc, page, offset, nr); 16011da177e4SLinus Torvalds offset += ret; 16021da177e4SLinus Torvalds index += offset >> PAGE_CACHE_SHIFT; 16031da177e4SLinus Torvalds offset &= ~PAGE_CACHE_MASK; 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds page_cache_release(page); 16061da177e4SLinus Torvalds if (ret != nr || !desc->count) 16071da177e4SLinus Torvalds break; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds cond_resched(); 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; 16131da177e4SLinus Torvalds file_accessed(filp); 16141da177e4SLinus Torvalds } 16151da177e4SLinus Torvalds 16161da177e4SLinus Torvalds static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) 16171da177e4SLinus Torvalds { 16181da177e4SLinus Torvalds read_descriptor_t desc; 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds if ((ssize_t) count < 0) 16211da177e4SLinus Torvalds return -EINVAL; 16221da177e4SLinus Torvalds if (!access_ok(VERIFY_WRITE, buf, count)) 16231da177e4SLinus Torvalds return -EFAULT; 16241da177e4SLinus Torvalds if (!count) 16251da177e4SLinus Torvalds return 0; 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds desc.written = 0; 16281da177e4SLinus Torvalds desc.count = count; 16291da177e4SLinus Torvalds desc.arg.buf = buf; 16301da177e4SLinus Torvalds desc.error = 0; 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds do_shmem_file_read(filp, ppos, &desc, file_read_actor); 16331da177e4SLinus Torvalds if (desc.written) 16341da177e4SLinus Torvalds return desc.written; 16351da177e4SLinus Torvalds return desc.error; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds static ssize_t shmem_file_sendfile(struct file *in_file, loff_t *ppos, 16391da177e4SLinus Torvalds size_t count, read_actor_t actor, void *target) 16401da177e4SLinus Torvalds { 16411da177e4SLinus Torvalds read_descriptor_t desc; 16421da177e4SLinus Torvalds 16431da177e4SLinus Torvalds if (!count) 16441da177e4SLinus Torvalds return 0; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds desc.written = 0; 16471da177e4SLinus Torvalds desc.count = count; 16481da177e4SLinus Torvalds desc.arg.data = target; 16491da177e4SLinus Torvalds desc.error = 0; 16501da177e4SLinus Torvalds 16511da177e4SLinus Torvalds do_shmem_file_read(in_file, ppos, &desc, actor); 16521da177e4SLinus Torvalds if (desc.written) 16531da177e4SLinus Torvalds return desc.written; 16541da177e4SLinus Torvalds return desc.error; 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds static int shmem_statfs(struct super_block *sb, struct kstatfs *buf) 16581da177e4SLinus Torvalds { 16591da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 16621da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 16631da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 16641da177e4SLinus Torvalds spin_lock(&sbinfo->stat_lock); 16650edd73b3SHugh Dickins if (sbinfo->max_blocks) { 16661da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 16671da177e4SLinus Torvalds buf->f_bavail = buf->f_bfree = sbinfo->free_blocks; 16680edd73b3SHugh Dickins } 16690edd73b3SHugh Dickins if (sbinfo->max_inodes) { 16701da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 16711da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 16721da177e4SLinus Torvalds } 16731da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 16740edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 16751da177e4SLinus Torvalds return 0; 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds /* 16791da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 16801da177e4SLinus Torvalds */ 16811da177e4SLinus Torvalds static int 16821da177e4SLinus Torvalds shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 16831da177e4SLinus Torvalds { 16841da177e4SLinus Torvalds struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev); 16851da177e4SLinus Torvalds int error = -ENOSPC; 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds if (inode) { 1688570bc1c2SStephen Smalley error = security_inode_init_security(inode, dir, NULL, NULL, 1689570bc1c2SStephen Smalley NULL); 1690570bc1c2SStephen Smalley if (error) { 1691570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1692570bc1c2SStephen Smalley iput(inode); 1693570bc1c2SStephen Smalley return error; 1694570bc1c2SStephen Smalley } 1695570bc1c2SStephen Smalley error = 0; 1696570bc1c2SStephen Smalley } 16971da177e4SLinus Torvalds if (dir->i_mode & S_ISGID) { 16981da177e4SLinus Torvalds inode->i_gid = dir->i_gid; 16991da177e4SLinus Torvalds if (S_ISDIR(mode)) 17001da177e4SLinus Torvalds inode->i_mode |= S_ISGID; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 17031da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 17041da177e4SLinus Torvalds d_instantiate(dentry, inode); 17051da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds return error; 17081da177e4SLinus Torvalds } 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode) 17111da177e4SLinus Torvalds { 17121da177e4SLinus Torvalds int error; 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 17151da177e4SLinus Torvalds return error; 17161da177e4SLinus Torvalds dir->i_nlink++; 17171da177e4SLinus Torvalds return 0; 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds static int shmem_create(struct inode *dir, struct dentry *dentry, int mode, 17211da177e4SLinus Torvalds struct nameidata *nd) 17221da177e4SLinus Torvalds { 17231da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds /* 17271da177e4SLinus Torvalds * Link a file.. 17281da177e4SLinus Torvalds */ 17291da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 17301da177e4SLinus Torvalds { 17311da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 17321da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds /* 17351da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 17361da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 17371da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 17381da177e4SLinus Torvalds */ 17390edd73b3SHugh Dickins if (sbinfo->max_inodes) { 17401da177e4SLinus Torvalds spin_lock(&sbinfo->stat_lock); 17411da177e4SLinus Torvalds if (!sbinfo->free_inodes) { 17421da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 17431da177e4SLinus Torvalds return -ENOSPC; 17441da177e4SLinus Torvalds } 17451da177e4SLinus Torvalds sbinfo->free_inodes--; 17461da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 17501da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 17511da177e4SLinus Torvalds inode->i_nlink++; 17521da177e4SLinus Torvalds atomic_inc(&inode->i_count); /* New dentry reference */ 17531da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 17541da177e4SLinus Torvalds d_instantiate(dentry, inode); 17551da177e4SLinus Torvalds return 0; 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds 17581da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 17591da177e4SLinus Torvalds { 17601da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) { 17631da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 17640edd73b3SHugh Dickins if (sbinfo->max_inodes) { 17651da177e4SLinus Torvalds spin_lock(&sbinfo->stat_lock); 17661da177e4SLinus Torvalds sbinfo->free_inodes++; 17671da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 17721da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 17731da177e4SLinus Torvalds inode->i_nlink--; 17741da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 17751da177e4SLinus Torvalds return 0; 17761da177e4SLinus Torvalds } 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 17791da177e4SLinus Torvalds { 17801da177e4SLinus Torvalds if (!simple_empty(dentry)) 17811da177e4SLinus Torvalds return -ENOTEMPTY; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds dir->i_nlink--; 17841da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 17851da177e4SLinus Torvalds } 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds /* 17881da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 17891da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 17901da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 17911da177e4SLinus Torvalds * gets overwritten. 17921da177e4SLinus Torvalds */ 17931da177e4SLinus Torvalds static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 17941da177e4SLinus Torvalds { 17951da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 17961da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 17991da177e4SLinus Torvalds return -ENOTEMPTY; 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds if (new_dentry->d_inode) { 18021da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 18031da177e4SLinus Torvalds if (they_are_dirs) 18041da177e4SLinus Torvalds old_dir->i_nlink--; 18051da177e4SLinus Torvalds } else if (they_are_dirs) { 18061da177e4SLinus Torvalds old_dir->i_nlink--; 18071da177e4SLinus Torvalds new_dir->i_nlink++; 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 18111da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 18121da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 18131da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 18141da177e4SLinus Torvalds inode->i_ctime = CURRENT_TIME; 18151da177e4SLinus Torvalds return 0; 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 18191da177e4SLinus Torvalds { 18201da177e4SLinus Torvalds int error; 18211da177e4SLinus Torvalds int len; 18221da177e4SLinus Torvalds struct inode *inode; 18231da177e4SLinus Torvalds struct page *page = NULL; 18241da177e4SLinus Torvalds char *kaddr; 18251da177e4SLinus Torvalds struct shmem_inode_info *info; 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds len = strlen(symname) + 1; 18281da177e4SLinus Torvalds if (len > PAGE_CACHE_SIZE) 18291da177e4SLinus Torvalds return -ENAMETOOLONG; 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0); 18321da177e4SLinus Torvalds if (!inode) 18331da177e4SLinus Torvalds return -ENOSPC; 18341da177e4SLinus Torvalds 1835570bc1c2SStephen Smalley error = security_inode_init_security(inode, dir, NULL, NULL, 1836570bc1c2SStephen Smalley NULL); 1837570bc1c2SStephen Smalley if (error) { 1838570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 1839570bc1c2SStephen Smalley iput(inode); 1840570bc1c2SStephen Smalley return error; 1841570bc1c2SStephen Smalley } 1842570bc1c2SStephen Smalley error = 0; 1843570bc1c2SStephen Smalley } 1844570bc1c2SStephen Smalley 18451da177e4SLinus Torvalds info = SHMEM_I(inode); 18461da177e4SLinus Torvalds inode->i_size = len-1; 18471da177e4SLinus Torvalds if (len <= (char *)inode - (char *)info) { 18481da177e4SLinus Torvalds /* do it inline */ 18491da177e4SLinus Torvalds memcpy(info, symname, len); 18501da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inline_operations; 18511da177e4SLinus Torvalds } else { 18521da177e4SLinus Torvalds error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); 18531da177e4SLinus Torvalds if (error) { 18541da177e4SLinus Torvalds iput(inode); 18551da177e4SLinus Torvalds return error; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 18581da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 18591da177e4SLinus Torvalds memcpy(kaddr, symname, len); 18601da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 18611da177e4SLinus Torvalds set_page_dirty(page); 18621da177e4SLinus Torvalds page_cache_release(page); 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds if (dir->i_mode & S_ISGID) 18651da177e4SLinus Torvalds inode->i_gid = dir->i_gid; 18661da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 18671da177e4SLinus Torvalds dir->i_ctime = dir->i_mtime = CURRENT_TIME; 18681da177e4SLinus Torvalds d_instantiate(dentry, inode); 18691da177e4SLinus Torvalds dget(dentry); 18701da177e4SLinus Torvalds return 0; 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds 1873cc314eefSLinus Torvalds static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd) 18741da177e4SLinus Torvalds { 18751da177e4SLinus Torvalds nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode)); 1876cc314eefSLinus Torvalds return NULL; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 1879cc314eefSLinus Torvalds static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) 18801da177e4SLinus Torvalds { 18811da177e4SLinus Torvalds struct page *page = NULL; 18821da177e4SLinus Torvalds int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); 18831da177e4SLinus Torvalds nd_set_link(nd, res ? ERR_PTR(res) : kmap(page)); 1884cc314eefSLinus Torvalds return page; 18851da177e4SLinus Torvalds } 18861da177e4SLinus Torvalds 1887cc314eefSLinus Torvalds static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 18881da177e4SLinus Torvalds { 18891da177e4SLinus Torvalds if (!IS_ERR(nd_get_link(nd))) { 1890cc314eefSLinus Torvalds struct page *page = cookie; 18911da177e4SLinus Torvalds kunmap(page); 18921da177e4SLinus Torvalds mark_page_accessed(page); 18931da177e4SLinus Torvalds page_cache_release(page); 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds static struct inode_operations shmem_symlink_inline_operations = { 18981da177e4SLinus Torvalds .readlink = generic_readlink, 18991da177e4SLinus Torvalds .follow_link = shmem_follow_link_inline, 19001da177e4SLinus Torvalds }; 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds static struct inode_operations shmem_symlink_inode_operations = { 19031da177e4SLinus Torvalds .truncate = shmem_truncate, 19041da177e4SLinus Torvalds .readlink = generic_readlink, 19051da177e4SLinus Torvalds .follow_link = shmem_follow_link, 19061da177e4SLinus Torvalds .put_link = shmem_put_link, 19071da177e4SLinus Torvalds }; 19081da177e4SLinus Torvalds 19097339ff83SRobin Holt static int shmem_parse_options(char *options, int *mode, uid_t *uid, 19107339ff83SRobin Holt gid_t *gid, unsigned long *blocks, unsigned long *inodes, 19117339ff83SRobin Holt int *policy, nodemask_t *policy_nodes) 19121da177e4SLinus Torvalds { 19131da177e4SLinus Torvalds char *this_char, *value, *rest; 19141da177e4SLinus Torvalds 1915b00dc3adSHugh Dickins while (options != NULL) { 1916b00dc3adSHugh Dickins this_char = options; 1917b00dc3adSHugh Dickins for (;;) { 1918b00dc3adSHugh Dickins /* 1919b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 1920b00dc3adSHugh Dickins * mount options form a comma-separated list, 1921b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 1922b00dc3adSHugh Dickins */ 1923b00dc3adSHugh Dickins options = strchr(options, ','); 1924b00dc3adSHugh Dickins if (options == NULL) 1925b00dc3adSHugh Dickins break; 1926b00dc3adSHugh Dickins options++; 1927b00dc3adSHugh Dickins if (!isdigit(*options)) { 1928b00dc3adSHugh Dickins options[-1] = '\0'; 1929b00dc3adSHugh Dickins break; 1930b00dc3adSHugh Dickins } 1931b00dc3adSHugh Dickins } 19321da177e4SLinus Torvalds if (!*this_char) 19331da177e4SLinus Torvalds continue; 19341da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 19351da177e4SLinus Torvalds *value++ = 0; 19361da177e4SLinus Torvalds } else { 19371da177e4SLinus Torvalds printk(KERN_ERR 19381da177e4SLinus Torvalds "tmpfs: No value for mount option '%s'\n", 19391da177e4SLinus Torvalds this_char); 19401da177e4SLinus Torvalds return 1; 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 19441da177e4SLinus Torvalds unsigned long long size; 19451da177e4SLinus Torvalds size = memparse(value,&rest); 19461da177e4SLinus Torvalds if (*rest == '%') { 19471da177e4SLinus Torvalds size <<= PAGE_SHIFT; 19481da177e4SLinus Torvalds size *= totalram_pages; 19491da177e4SLinus Torvalds do_div(size, 100); 19501da177e4SLinus Torvalds rest++; 19511da177e4SLinus Torvalds } 19521da177e4SLinus Torvalds if (*rest) 19531da177e4SLinus Torvalds goto bad_val; 19541da177e4SLinus Torvalds *blocks = size >> PAGE_CACHE_SHIFT; 19551da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 19561da177e4SLinus Torvalds *blocks = memparse(value,&rest); 19571da177e4SLinus Torvalds if (*rest) 19581da177e4SLinus Torvalds goto bad_val; 19591da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 19601da177e4SLinus Torvalds *inodes = memparse(value,&rest); 19611da177e4SLinus Torvalds if (*rest) 19621da177e4SLinus Torvalds goto bad_val; 19631da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 19641da177e4SLinus Torvalds if (!mode) 19651da177e4SLinus Torvalds continue; 19661da177e4SLinus Torvalds *mode = simple_strtoul(value,&rest,8); 19671da177e4SLinus Torvalds if (*rest) 19681da177e4SLinus Torvalds goto bad_val; 19691da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 19701da177e4SLinus Torvalds if (!uid) 19711da177e4SLinus Torvalds continue; 19721da177e4SLinus Torvalds *uid = simple_strtoul(value,&rest,0); 19731da177e4SLinus Torvalds if (*rest) 19741da177e4SLinus Torvalds goto bad_val; 19751da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 19761da177e4SLinus Torvalds if (!gid) 19771da177e4SLinus Torvalds continue; 19781da177e4SLinus Torvalds *gid = simple_strtoul(value,&rest,0); 19791da177e4SLinus Torvalds if (*rest) 19801da177e4SLinus Torvalds goto bad_val; 19817339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 1982b00dc3adSHugh Dickins if (shmem_parse_mpol(value,policy,policy_nodes)) 19837339ff83SRobin Holt goto bad_val; 19841da177e4SLinus Torvalds } else { 19851da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad mount option %s\n", 19861da177e4SLinus Torvalds this_char); 19871da177e4SLinus Torvalds return 1; 19881da177e4SLinus Torvalds } 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds return 0; 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds bad_val: 19931da177e4SLinus Torvalds printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", 19941da177e4SLinus Torvalds value, this_char); 19951da177e4SLinus Torvalds return 1; 19961da177e4SLinus Torvalds 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 20001da177e4SLinus Torvalds { 20011da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 20020edd73b3SHugh Dickins unsigned long max_blocks = sbinfo->max_blocks; 20030edd73b3SHugh Dickins unsigned long max_inodes = sbinfo->max_inodes; 20047339ff83SRobin Holt int policy = sbinfo->policy; 20057339ff83SRobin Holt nodemask_t policy_nodes = sbinfo->policy_nodes; 20060edd73b3SHugh Dickins unsigned long blocks; 20070edd73b3SHugh Dickins unsigned long inodes; 20080edd73b3SHugh Dickins int error = -EINVAL; 20091da177e4SLinus Torvalds 20107339ff83SRobin Holt if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks, 20117339ff83SRobin Holt &max_inodes, &policy, &policy_nodes)) 20120edd73b3SHugh Dickins return error; 20130edd73b3SHugh Dickins 20140edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 20150edd73b3SHugh Dickins blocks = sbinfo->max_blocks - sbinfo->free_blocks; 20160edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 20170edd73b3SHugh Dickins if (max_blocks < blocks) 20180edd73b3SHugh Dickins goto out; 20190edd73b3SHugh Dickins if (max_inodes < inodes) 20200edd73b3SHugh Dickins goto out; 20210edd73b3SHugh Dickins /* 20220edd73b3SHugh Dickins * Those tests also disallow limited->unlimited while any are in 20230edd73b3SHugh Dickins * use, so i_blocks will always be zero when max_blocks is zero; 20240edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 20250edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 20260edd73b3SHugh Dickins */ 20270edd73b3SHugh Dickins if (max_blocks && !sbinfo->max_blocks) 20280edd73b3SHugh Dickins goto out; 20290edd73b3SHugh Dickins if (max_inodes && !sbinfo->max_inodes) 20300edd73b3SHugh Dickins goto out; 20310edd73b3SHugh Dickins 20320edd73b3SHugh Dickins error = 0; 20330edd73b3SHugh Dickins sbinfo->max_blocks = max_blocks; 20340edd73b3SHugh Dickins sbinfo->free_blocks = max_blocks - blocks; 20350edd73b3SHugh Dickins sbinfo->max_inodes = max_inodes; 20360edd73b3SHugh Dickins sbinfo->free_inodes = max_inodes - inodes; 20377339ff83SRobin Holt sbinfo->policy = policy; 20387339ff83SRobin Holt sbinfo->policy_nodes = policy_nodes; 20390edd73b3SHugh Dickins out: 20400edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 20410edd73b3SHugh Dickins return error; 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds #endif 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 20461da177e4SLinus Torvalds { 20471da177e4SLinus Torvalds kfree(sb->s_fs_info); 20481da177e4SLinus Torvalds sb->s_fs_info = NULL; 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds static int shmem_fill_super(struct super_block *sb, 20521da177e4SLinus Torvalds void *data, int silent) 20531da177e4SLinus Torvalds { 20541da177e4SLinus Torvalds struct inode *inode; 20551da177e4SLinus Torvalds struct dentry *root; 20561da177e4SLinus Torvalds int mode = S_IRWXUGO | S_ISVTX; 20571da177e4SLinus Torvalds uid_t uid = current->fsuid; 20581da177e4SLinus Torvalds gid_t gid = current->fsgid; 20591da177e4SLinus Torvalds int err = -ENOMEM; 20600edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 20611da177e4SLinus Torvalds unsigned long blocks = 0; 20621da177e4SLinus Torvalds unsigned long inodes = 0; 20637339ff83SRobin Holt int policy = MPOL_DEFAULT; 20647339ff83SRobin Holt nodemask_t policy_nodes = node_online_map; 20651da177e4SLinus Torvalds 20660edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 20671da177e4SLinus Torvalds /* 20681da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 20691da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 20701da177e4SLinus Torvalds * but the internal instance is left unlimited. 20711da177e4SLinus Torvalds */ 20721da177e4SLinus Torvalds if (!(sb->s_flags & MS_NOUSER)) { 20731da177e4SLinus Torvalds blocks = totalram_pages / 2; 20741da177e4SLinus Torvalds inodes = totalram_pages - totalhigh_pages; 20751da177e4SLinus Torvalds if (inodes > blocks) 20761da177e4SLinus Torvalds inodes = blocks; 20777339ff83SRobin Holt if (shmem_parse_options(data, &mode, &uid, &gid, &blocks, 20787339ff83SRobin Holt &inodes, &policy, &policy_nodes)) 20791da177e4SLinus Torvalds return -EINVAL; 20801da177e4SLinus Torvalds } 20810edd73b3SHugh Dickins #else 20820edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 20830edd73b3SHugh Dickins #endif 20841da177e4SLinus Torvalds 20850edd73b3SHugh Dickins /* Round up to L1_CACHE_BYTES to resist false sharing */ 20860edd73b3SHugh Dickins sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info), 20870edd73b3SHugh Dickins L1_CACHE_BYTES), GFP_KERNEL); 20881da177e4SLinus Torvalds if (!sbinfo) 20891da177e4SLinus Torvalds return -ENOMEM; 20900edd73b3SHugh Dickins 20911da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 20921da177e4SLinus Torvalds sbinfo->max_blocks = blocks; 20931da177e4SLinus Torvalds sbinfo->free_blocks = blocks; 20941da177e4SLinus Torvalds sbinfo->max_inodes = inodes; 20951da177e4SLinus Torvalds sbinfo->free_inodes = inodes; 20967339ff83SRobin Holt sbinfo->policy = policy; 20977339ff83SRobin Holt sbinfo->policy_nodes = policy_nodes; 20981da177e4SLinus Torvalds 20990edd73b3SHugh Dickins sb->s_fs_info = sbinfo; 21001da177e4SLinus Torvalds sb->s_maxbytes = SHMEM_MAX_BYTES; 21011da177e4SLinus Torvalds sb->s_blocksize = PAGE_CACHE_SIZE; 21021da177e4SLinus Torvalds sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 21031da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 21041da177e4SLinus Torvalds sb->s_op = &shmem_ops; 21050edd73b3SHugh Dickins 21061da177e4SLinus Torvalds inode = shmem_get_inode(sb, S_IFDIR | mode, 0); 21071da177e4SLinus Torvalds if (!inode) 21081da177e4SLinus Torvalds goto failed; 21091da177e4SLinus Torvalds inode->i_uid = uid; 21101da177e4SLinus Torvalds inode->i_gid = gid; 21111da177e4SLinus Torvalds root = d_alloc_root(inode); 21121da177e4SLinus Torvalds if (!root) 21131da177e4SLinus Torvalds goto failed_iput; 21141da177e4SLinus Torvalds sb->s_root = root; 21151da177e4SLinus Torvalds return 0; 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds failed_iput: 21181da177e4SLinus Torvalds iput(inode); 21191da177e4SLinus Torvalds failed: 21201da177e4SLinus Torvalds shmem_put_super(sb); 21211da177e4SLinus Torvalds return err; 21221da177e4SLinus Torvalds } 21231da177e4SLinus Torvalds 2124fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 21271da177e4SLinus Torvalds { 21281da177e4SLinus Torvalds struct shmem_inode_info *p; 21291da177e4SLinus Torvalds p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, SLAB_KERNEL); 21301da177e4SLinus Torvalds if (!p) 21311da177e4SLinus Torvalds return NULL; 21321da177e4SLinus Torvalds return &p->vfs_inode; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 21361da177e4SLinus Torvalds { 21371da177e4SLinus Torvalds if ((inode->i_mode & S_IFMT) == S_IFREG) { 21381da177e4SLinus Torvalds /* only struct inode is valid if it's an inline symlink */ 21391da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 2144fcc234f8SPekka Enberg static void init_once(void *foo, struct kmem_cache *cachep, 2145fcc234f8SPekka Enberg unsigned long flags) 21461da177e4SLinus Torvalds { 21471da177e4SLinus Torvalds struct shmem_inode_info *p = (struct shmem_inode_info *) foo; 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == 21501da177e4SLinus Torvalds SLAB_CTOR_CONSTRUCTOR) { 21511da177e4SLinus Torvalds inode_init_once(&p->vfs_inode); 21521da177e4SLinus Torvalds } 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds static int init_inodecache(void) 21561da177e4SLinus Torvalds { 21571da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 21581da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 21591da177e4SLinus Torvalds 0, 0, init_once, NULL); 21601da177e4SLinus Torvalds if (shmem_inode_cachep == NULL) 21611da177e4SLinus Torvalds return -ENOMEM; 21621da177e4SLinus Torvalds return 0; 21631da177e4SLinus Torvalds } 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds static void destroy_inodecache(void) 21661da177e4SLinus Torvalds { 21671da177e4SLinus Torvalds if (kmem_cache_destroy(shmem_inode_cachep)) 21681da177e4SLinus Torvalds printk(KERN_INFO "shmem_inode_cache: not all structures were freed\n"); 21691da177e4SLinus Torvalds } 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds static struct address_space_operations shmem_aops = { 21721da177e4SLinus Torvalds .writepage = shmem_writepage, 21731da177e4SLinus Torvalds .set_page_dirty = __set_page_dirty_nobuffers, 21741da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 21751da177e4SLinus Torvalds .prepare_write = shmem_prepare_write, 21761da177e4SLinus Torvalds .commit_write = simple_commit_write, 21771da177e4SLinus Torvalds #endif 2178*304dbdb7SLee Schermerhorn .migratepage = migrate_page, 21791da177e4SLinus Torvalds }; 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds static struct file_operations shmem_file_operations = { 21821da177e4SLinus Torvalds .mmap = shmem_mmap, 21831da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 21841da177e4SLinus Torvalds .llseek = generic_file_llseek, 21851da177e4SLinus Torvalds .read = shmem_file_read, 21861da177e4SLinus Torvalds .write = shmem_file_write, 21871da177e4SLinus Torvalds .fsync = simple_sync_file, 21881da177e4SLinus Torvalds .sendfile = shmem_file_sendfile, 21891da177e4SLinus Torvalds #endif 21901da177e4SLinus Torvalds }; 21911da177e4SLinus Torvalds 21921da177e4SLinus Torvalds static struct inode_operations shmem_inode_operations = { 21931da177e4SLinus Torvalds .truncate = shmem_truncate, 21941da177e4SLinus Torvalds .setattr = shmem_notify_change, 2195f6b3ec23SBadari Pulavarty .truncate_range = shmem_truncate_range, 21961da177e4SLinus Torvalds }; 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds static struct inode_operations shmem_dir_inode_operations = { 21991da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 22001da177e4SLinus Torvalds .create = shmem_create, 22011da177e4SLinus Torvalds .lookup = simple_lookup, 22021da177e4SLinus Torvalds .link = shmem_link, 22031da177e4SLinus Torvalds .unlink = shmem_unlink, 22041da177e4SLinus Torvalds .symlink = shmem_symlink, 22051da177e4SLinus Torvalds .mkdir = shmem_mkdir, 22061da177e4SLinus Torvalds .rmdir = shmem_rmdir, 22071da177e4SLinus Torvalds .mknod = shmem_mknod, 22081da177e4SLinus Torvalds .rename = shmem_rename, 22091da177e4SLinus Torvalds #endif 22101da177e4SLinus Torvalds }; 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds static struct super_operations shmem_ops = { 22131da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 22141da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 22151da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 22161da177e4SLinus Torvalds .statfs = shmem_statfs, 22171da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 22181da177e4SLinus Torvalds #endif 22191da177e4SLinus Torvalds .delete_inode = shmem_delete_inode, 22201da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 22211da177e4SLinus Torvalds .put_super = shmem_put_super, 22221da177e4SLinus Torvalds }; 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds static struct vm_operations_struct shmem_vm_ops = { 22251da177e4SLinus Torvalds .nopage = shmem_nopage, 22261da177e4SLinus Torvalds .populate = shmem_populate, 22271da177e4SLinus Torvalds #ifdef CONFIG_NUMA 22281da177e4SLinus Torvalds .set_policy = shmem_set_policy, 22291da177e4SLinus Torvalds .get_policy = shmem_get_policy, 22301da177e4SLinus Torvalds #endif 22311da177e4SLinus Torvalds }; 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds 22341da177e4SLinus Torvalds static struct super_block *shmem_get_sb(struct file_system_type *fs_type, 22351da177e4SLinus Torvalds int flags, const char *dev_name, void *data) 22361da177e4SLinus Torvalds { 22371da177e4SLinus Torvalds return get_sb_nodev(fs_type, flags, data, shmem_fill_super); 22381da177e4SLinus Torvalds } 22391da177e4SLinus Torvalds 22401da177e4SLinus Torvalds static struct file_system_type tmpfs_fs_type = { 22411da177e4SLinus Torvalds .owner = THIS_MODULE, 22421da177e4SLinus Torvalds .name = "tmpfs", 22431da177e4SLinus Torvalds .get_sb = shmem_get_sb, 22441da177e4SLinus Torvalds .kill_sb = kill_litter_super, 22451da177e4SLinus Torvalds }; 22461da177e4SLinus Torvalds static struct vfsmount *shm_mnt; 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds static int __init init_tmpfs(void) 22491da177e4SLinus Torvalds { 22501da177e4SLinus Torvalds int error; 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds error = init_inodecache(); 22531da177e4SLinus Torvalds if (error) 22541da177e4SLinus Torvalds goto out3; 22551da177e4SLinus Torvalds 22561da177e4SLinus Torvalds error = register_filesystem(&tmpfs_fs_type); 22571da177e4SLinus Torvalds if (error) { 22581da177e4SLinus Torvalds printk(KERN_ERR "Could not register tmpfs\n"); 22591da177e4SLinus Torvalds goto out2; 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 22621da177e4SLinus Torvalds devfs_mk_dir("shm"); 22631da177e4SLinus Torvalds #endif 22641da177e4SLinus Torvalds shm_mnt = do_kern_mount(tmpfs_fs_type.name, MS_NOUSER, 22651da177e4SLinus Torvalds tmpfs_fs_type.name, NULL); 22661da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 22671da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 22681da177e4SLinus Torvalds printk(KERN_ERR "Could not kern_mount tmpfs\n"); 22691da177e4SLinus Torvalds goto out1; 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds return 0; 22721da177e4SLinus Torvalds 22731da177e4SLinus Torvalds out1: 22741da177e4SLinus Torvalds unregister_filesystem(&tmpfs_fs_type); 22751da177e4SLinus Torvalds out2: 22761da177e4SLinus Torvalds destroy_inodecache(); 22771da177e4SLinus Torvalds out3: 22781da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 22791da177e4SLinus Torvalds return error; 22801da177e4SLinus Torvalds } 22811da177e4SLinus Torvalds module_init(init_tmpfs) 22821da177e4SLinus Torvalds 22831da177e4SLinus Torvalds /* 22841da177e4SLinus Torvalds * shmem_file_setup - get an unlinked file living in tmpfs 22851da177e4SLinus Torvalds * 22861da177e4SLinus Torvalds * @name: name for dentry (to be seen in /proc/<pid>/maps 22871da177e4SLinus Torvalds * @size: size to be set for the file 22881da177e4SLinus Torvalds * 22891da177e4SLinus Torvalds */ 22901da177e4SLinus Torvalds struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags) 22911da177e4SLinus Torvalds { 22921da177e4SLinus Torvalds int error; 22931da177e4SLinus Torvalds struct file *file; 22941da177e4SLinus Torvalds struct inode *inode; 22951da177e4SLinus Torvalds struct dentry *dentry, *root; 22961da177e4SLinus Torvalds struct qstr this; 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 22991da177e4SLinus Torvalds return (void *)shm_mnt; 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds if (size < 0 || size > SHMEM_MAX_BYTES) 23021da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 23031da177e4SLinus Torvalds 23041da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 23051da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 23061da177e4SLinus Torvalds 23071da177e4SLinus Torvalds error = -ENOMEM; 23081da177e4SLinus Torvalds this.name = name; 23091da177e4SLinus Torvalds this.len = strlen(name); 23101da177e4SLinus Torvalds this.hash = 0; /* will go */ 23111da177e4SLinus Torvalds root = shm_mnt->mnt_root; 23121da177e4SLinus Torvalds dentry = d_alloc(root, &this); 23131da177e4SLinus Torvalds if (!dentry) 23141da177e4SLinus Torvalds goto put_memory; 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds error = -ENFILE; 23171da177e4SLinus Torvalds file = get_empty_filp(); 23181da177e4SLinus Torvalds if (!file) 23191da177e4SLinus Torvalds goto put_dentry; 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds error = -ENOSPC; 23221da177e4SLinus Torvalds inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0); 23231da177e4SLinus Torvalds if (!inode) 23241da177e4SLinus Torvalds goto close_file; 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds SHMEM_I(inode)->flags = flags & VM_ACCOUNT; 23271da177e4SLinus Torvalds d_instantiate(dentry, inode); 23281da177e4SLinus Torvalds inode->i_size = size; 23291da177e4SLinus Torvalds inode->i_nlink = 0; /* It is unlinked */ 23301da177e4SLinus Torvalds file->f_vfsmnt = mntget(shm_mnt); 23311da177e4SLinus Torvalds file->f_dentry = dentry; 23321da177e4SLinus Torvalds file->f_mapping = inode->i_mapping; 23331da177e4SLinus Torvalds file->f_op = &shmem_file_operations; 23341da177e4SLinus Torvalds file->f_mode = FMODE_WRITE | FMODE_READ; 23351da177e4SLinus Torvalds return file; 23361da177e4SLinus Torvalds 23371da177e4SLinus Torvalds close_file: 23381da177e4SLinus Torvalds put_filp(file); 23391da177e4SLinus Torvalds put_dentry: 23401da177e4SLinus Torvalds dput(dentry); 23411da177e4SLinus Torvalds put_memory: 23421da177e4SLinus Torvalds shmem_unacct_size(flags, size); 23431da177e4SLinus Torvalds return ERR_PTR(error); 23441da177e4SLinus Torvalds } 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds /* 23471da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 23481da177e4SLinus Torvalds * 23491da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 23501da177e4SLinus Torvalds */ 23511da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 23521da177e4SLinus Torvalds { 23531da177e4SLinus Torvalds struct file *file; 23541da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds file = shmem_file_setup("dev/zero", size, vma->vm_flags); 23571da177e4SLinus Torvalds if (IS_ERR(file)) 23581da177e4SLinus Torvalds return PTR_ERR(file); 23591da177e4SLinus Torvalds 23601da177e4SLinus Torvalds if (vma->vm_file) 23611da177e4SLinus Torvalds fput(vma->vm_file); 23621da177e4SLinus Torvalds vma->vm_file = file; 23631da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 23641da177e4SLinus Torvalds return 0; 23651da177e4SLinus Torvalds } 2366