Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Resizable virtual memory filesystem for Linux. |
| 3 | * |
| 4 | * Copyright (C) 2000 Linus Torvalds. |
| 5 | * 2000 Transmeta Corp. |
| 6 | * 2000-2001 Christoph Rohland |
| 7 | * 2000-2001 SAP AG |
| 8 | * 2002 Red Hat Inc. |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 9 | * Copyright (C) 2002-2005 Hugh Dickins. |
| 10 | * Copyright (C) 2002-2005 VERITAS Software Corporation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Copyright (C) 2004 Andi Kleen, SuSE Labs |
| 12 | * |
| 13 | * Extended attribute support for tmpfs: |
| 14 | * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> |
| 15 | * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 16 | * |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 17 | * tiny-shmem: |
| 18 | * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> |
| 19 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * This file is released under the GPL. |
| 21 | */ |
| 22 | |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 23 | #include <linux/fs.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/vfs.h> |
| 26 | #include <linux/mount.h> |
Hugh Dickins | caefba1 | 2009-04-13 14:40:12 -0700 | [diff] [blame] | 27 | #include <linux/pagemap.h> |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 28 | #include <linux/file.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/swap.h> |
| 32 | |
| 33 | static struct vfsmount *shm_mnt; |
| 34 | |
| 35 | #ifdef CONFIG_SHMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* |
| 37 | * This virtual memory filesystem is heavily based on the ramfs. It |
| 38 | * extends ramfs by the ability to use swap and honor resource limits |
| 39 | * which makes it a completely usable filesystem. |
| 40 | */ |
| 41 | |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 42 | #include <linux/xattr.h> |
Christoph Hellwig | a569425 | 2007-07-17 04:04:28 -0700 | [diff] [blame] | 43 | #include <linux/exportfs.h> |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 44 | #include <linux/posix_acl.h> |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 45 | #include <linux/generic_acl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/string.h> |
| 48 | #include <linux/slab.h> |
| 49 | #include <linux/backing-dev.h> |
| 50 | #include <linux/shmem_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/blkdev.h> |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 53 | #include <linux/percpu_counter.h> |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 54 | #include <linux/splice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/security.h> |
| 56 | #include <linux/swapops.h> |
| 57 | #include <linux/mempolicy.h> |
| 58 | #include <linux/namei.h> |
Hugh Dickins | b00dc3a | 2006-02-21 23:49:47 +0000 | [diff] [blame] | 59 | #include <linux/ctype.h> |
Lee Schermerhorn | 304dbdb | 2006-04-22 02:35:48 -0700 | [diff] [blame] | 60 | #include <linux/migrate.h> |
Christoph Lameter | c1f60a5 | 2006-09-25 23:31:11 -0700 | [diff] [blame] | 61 | #include <linux/highmem.h> |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 62 | #include <linux/seq_file.h> |
Mimi Zohar | 9256292 | 2008-10-07 14:00:12 -0400 | [diff] [blame] | 63 | #include <linux/magic.h> |
Lee Schermerhorn | 304dbdb | 2006-04-22 02:35:48 -0700 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #include <asm/pgtable.h> |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* Pretend that each entry is of this size in directory's i_size */ |
| 72 | #define BOGO_DIRENT_SIZE 20 |
| 73 | |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 74 | struct shmem_xattr { |
| 75 | struct list_head list; /* anchored by shmem_inode_info->xattr_list */ |
| 76 | char *name; /* xattr name */ |
| 77 | size_t size; |
| 78 | char value[0]; |
| 79 | }; |
| 80 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 81 | /* Flag allocation requirements to shmem_getpage */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | enum sgp_type { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | SGP_READ, /* don't exceed i_size, don't allocate page */ |
| 84 | SGP_CACHE, /* don't exceed i_size, may allocate page */ |
Hugh Dickins | a0ee5ec | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 85 | SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | SGP_WRITE, /* may exceed i_size, may allocate page */ |
| 87 | }; |
| 88 | |
Andrew Morton | b76db735 | 2008-02-08 04:21:49 -0800 | [diff] [blame] | 89 | #ifdef CONFIG_TMPFS |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 90 | static unsigned long shmem_default_max_blocks(void) |
| 91 | { |
| 92 | return totalram_pages / 2; |
| 93 | } |
| 94 | |
| 95 | static unsigned long shmem_default_max_inodes(void) |
| 96 | { |
| 97 | return min(totalram_pages - totalhigh_pages, totalram_pages / 2); |
| 98 | } |
Andrew Morton | b76db735 | 2008-02-08 04:21:49 -0800 | [diff] [blame] | 99 | #endif |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 100 | |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 101 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
| 102 | struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type); |
| 103 | |
| 104 | static inline int shmem_getpage(struct inode *inode, pgoff_t index, |
| 105 | struct page **pagep, enum sgp_type sgp, int *fault_type) |
| 106 | { |
| 107 | return shmem_getpage_gfp(inode, index, pagep, sgp, |
| 108 | mapping_gfp_mask(inode->i_mapping), fault_type); |
| 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) |
| 112 | { |
| 113 | return sb->s_fs_info; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * shmem_file_setup pre-accounts the whole fixed size of a VM object, |
| 118 | * for shared memory and for shared anonymous (/dev/zero) mappings |
| 119 | * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), |
| 120 | * consistent with the pre-accounting of private mappings ... |
| 121 | */ |
| 122 | static inline int shmem_acct_size(unsigned long flags, loff_t size) |
| 123 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 124 | return (flags & VM_NORESERVE) ? |
| 125 | 0 : security_vm_enough_memory_kern(VM_ACCT(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static inline void shmem_unacct_size(unsigned long flags, loff_t size) |
| 129 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 130 | if (!(flags & VM_NORESERVE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | vm_unacct_memory(VM_ACCT(size)); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * ... whereas tmpfs objects are accounted incrementally as |
| 136 | * pages are allocated, in order to allow huge sparse files. |
| 137 | * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, |
| 138 | * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. |
| 139 | */ |
| 140 | static inline int shmem_acct_block(unsigned long flags) |
| 141 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 142 | return (flags & VM_NORESERVE) ? |
| 143 | security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static inline void shmem_unacct_blocks(unsigned long flags, long pages) |
| 147 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 148 | if (flags & VM_NORESERVE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); |
| 150 | } |
| 151 | |
Hugh Dickins | 759b977 | 2007-03-05 00:30:28 -0800 | [diff] [blame] | 152 | static const struct super_operations shmem_ops; |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 153 | static const struct address_space_operations shmem_aops; |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 154 | static const struct file_operations shmem_file_operations; |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 155 | static const struct inode_operations shmem_inode_operations; |
| 156 | static const struct inode_operations shmem_dir_inode_operations; |
| 157 | static const struct inode_operations shmem_special_inode_operations; |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 158 | static const struct vm_operations_struct shmem_vm_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Ravikiran G Thirumalai | 6c231b7 | 2005-09-06 15:17:45 -0700 | [diff] [blame] | 160 | static struct backing_dev_info shmem_backing_dev_info __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | .ra_pages = 0, /* No readahead */ |
Rik van Riel | 4f98a2f | 2008-10-18 20:26:32 -0700 | [diff] [blame] | 162 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static LIST_HEAD(shmem_swaplist); |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 166 | static DEFINE_MUTEX(shmem_swaplist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | static void shmem_free_blocks(struct inode *inode, long pages) |
| 169 | { |
| 170 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 171 | if (sbinfo->max_blocks) { |
Tim Chen | 7e49629 | 2010-08-09 17:19:05 -0700 | [diff] [blame] | 172 | percpu_counter_add(&sbinfo->used_blocks, -pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | inode->i_blocks -= pages*BLOCKS_PER_PAGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 177 | static int shmem_reserve_inode(struct super_block *sb) |
| 178 | { |
| 179 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 180 | if (sbinfo->max_inodes) { |
| 181 | spin_lock(&sbinfo->stat_lock); |
| 182 | if (!sbinfo->free_inodes) { |
| 183 | spin_unlock(&sbinfo->stat_lock); |
| 184 | return -ENOSPC; |
| 185 | } |
| 186 | sbinfo->free_inodes--; |
| 187 | spin_unlock(&sbinfo->stat_lock); |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static void shmem_free_inode(struct super_block *sb) |
| 193 | { |
| 194 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 195 | if (sbinfo->max_inodes) { |
| 196 | spin_lock(&sbinfo->stat_lock); |
| 197 | sbinfo->free_inodes++; |
| 198 | spin_unlock(&sbinfo->stat_lock); |
| 199 | } |
| 200 | } |
| 201 | |
Randy Dunlap | 4671181 | 2008-03-19 17:00:41 -0700 | [diff] [blame] | 202 | /** |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 203 | * shmem_recalc_inode - recalculate the block usage of an inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * @inode: inode to recalc |
| 205 | * |
| 206 | * We have to calculate the free blocks since the mm can drop |
| 207 | * undirtied hole pages behind our back. |
| 208 | * |
| 209 | * But normally info->alloced == inode->i_mapping->nrpages + info->swapped |
| 210 | * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) |
| 211 | * |
| 212 | * It has to be called with the spinlock held. |
| 213 | */ |
| 214 | static void shmem_recalc_inode(struct inode *inode) |
| 215 | { |
| 216 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 217 | long freed; |
| 218 | |
| 219 | freed = info->alloced - info->swapped - inode->i_mapping->nrpages; |
| 220 | if (freed > 0) { |
| 221 | info->alloced -= freed; |
| 222 | shmem_unacct_blocks(info->flags, freed); |
| 223 | shmem_free_blocks(inode, freed); |
| 224 | } |
| 225 | } |
| 226 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 227 | static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index, |
| 228 | swp_entry_t swap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 230 | if (index < SHMEM_NR_DIRECT) |
| 231 | info->i_direct[index] = swap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 234 | static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 236 | return (index < SHMEM_NR_DIRECT) ? |
| 237 | info->i_direct[index] : (swp_entry_t){0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 240 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 242 | struct address_space *mapping = inode->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | struct shmem_inode_info *info = SHMEM_I(inode); |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 244 | pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 245 | pgoff_t end = (lend >> PAGE_CACHE_SHIFT); |
| 246 | pgoff_t index; |
| 247 | swp_entry_t swap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 249 | truncate_inode_pages_range(mapping, lstart, lend); |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 250 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 251 | if (end > SHMEM_NR_DIRECT) |
| 252 | end = SHMEM_NR_DIRECT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | spin_lock(&info->lock); |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 255 | for (index = start; index < end; index++) { |
| 256 | swap = shmem_get_swap(info, index); |
| 257 | if (swap.val) { |
| 258 | free_swap_and_cache(swap); |
| 259 | shmem_put_swap(info, index, (swp_entry_t){0}); |
| 260 | info->swapped--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 264 | if (mapping->nrpages) { |
| 265 | spin_unlock(&info->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | /* |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 267 | * A page may have meanwhile sneaked in from swap. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | */ |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 269 | truncate_inode_pages_range(mapping, lstart, lend); |
| 270 | spin_lock(&info->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | shmem_recalc_inode(inode); |
| 274 | spin_unlock(&info->lock); |
| 275 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 276 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 278 | EXPORT_SYMBOL_GPL(shmem_truncate_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 280 | static int shmem_setattr(struct dentry *dentry, struct iattr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
| 282 | struct inode *inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | int error; |
| 284 | |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 285 | error = inode_change_ok(inode, attr); |
| 286 | if (error) |
| 287 | return error; |
| 288 | |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 289 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
| 290 | loff_t oldsize = inode->i_size; |
| 291 | loff_t newsize = attr->ia_size; |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 292 | struct page *page = NULL; |
| 293 | |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 294 | if (newsize < oldsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* |
| 296 | * If truncating down to a partial page, then |
| 297 | * if that page is already allocated, hold it |
| 298 | * in memory until the truncation is over, so |
Justin P. Mattock | ae0e47f | 2011-03-01 15:06:02 +0100 | [diff] [blame] | 299 | * truncate_partial_page cannot miss it were |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | * it assigned to swap. |
| 301 | */ |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 302 | if (newsize & (PAGE_CACHE_SIZE-1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | (void) shmem_getpage(inode, |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 304 | newsize >> PAGE_CACHE_SHIFT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | &page, SGP_READ, NULL); |
Hugh Dickins | d360244 | 2008-02-04 22:28:44 -0800 | [diff] [blame] | 306 | if (page) |
| 307 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 310 | if (newsize != oldsize) { |
| 311 | i_size_write(inode, newsize); |
| 312 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 313 | } |
| 314 | if (newsize < oldsize) { |
| 315 | loff_t holebegin = round_up(newsize, PAGE_SIZE); |
| 316 | unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); |
| 317 | shmem_truncate_range(inode, newsize, (loff_t)-1); |
| 318 | /* unmap again to remove racily COWed private pages */ |
| 319 | unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); |
| 320 | } |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 321 | if (page) |
| 322 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 325 | setattr_copy(inode, attr); |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 326 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 327 | if (attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 328 | error = generic_acl_chmod(inode); |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 329 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return error; |
| 331 | } |
| 332 | |
Al Viro | 1f895f7 | 2010-06-05 19:10:41 -0400 | [diff] [blame] | 333 | static void shmem_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | struct shmem_inode_info *info = SHMEM_I(inode); |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 336 | struct shmem_xattr *xattr, *nxattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 338 | if (inode->i_mapping->a_ops == &shmem_aops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | shmem_unacct_size(info->flags, inode->i_size); |
| 340 | inode->i_size = 0; |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 341 | shmem_truncate_range(inode, 0, (loff_t)-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | if (!list_empty(&info->swaplist)) { |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 343 | mutex_lock(&shmem_swaplist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | list_del_init(&info->swaplist); |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 345 | mutex_unlock(&shmem_swaplist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 348 | |
| 349 | list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) { |
| 350 | kfree(xattr->name); |
| 351 | kfree(xattr); |
| 352 | } |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 353 | BUG_ON(inode->i_blocks); |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 354 | shmem_free_inode(inode->i_sb); |
Al Viro | 1f895f7 | 2010-06-05 19:10:41 -0400 | [diff] [blame] | 355 | end_writeback(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 358 | static int shmem_unuse_inode(struct shmem_inode_info *info, |
| 359 | swp_entry_t swap, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 361 | struct address_space *mapping = info->vfs_inode.i_mapping; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 362 | pgoff_t index; |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 363 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 365 | for (index = 0; index < SHMEM_NR_DIRECT; index++) |
| 366 | if (shmem_get_swap(info, index).val == swap.val) |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 367 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return 0; |
| 369 | found: |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 370 | spin_lock(&info->lock); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 371 | if (shmem_get_swap(info, index).val != swap.val) { |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 372 | spin_unlock(&info->lock); |
| 373 | return 0; |
| 374 | } |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 375 | |
Hugh Dickins | 1b1b32f | 2008-02-04 22:28:55 -0800 | [diff] [blame] | 376 | /* |
| 377 | * Move _head_ to start search for next from here. |
Al Viro | 1f895f7 | 2010-06-05 19:10:41 -0400 | [diff] [blame] | 378 | * But be careful: shmem_evict_inode checks list_empty without taking |
Hugh Dickins | 1b1b32f | 2008-02-04 22:28:55 -0800 | [diff] [blame] | 379 | * mutex, and there's an instant in list_move_tail when info->swaplist |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 380 | * would appear empty, if it were the only one on shmem_swaplist. |
Hugh Dickins | 1b1b32f | 2008-02-04 22:28:55 -0800 | [diff] [blame] | 381 | */ |
| 382 | if (shmem_swaplist.next != &info->swaplist) |
| 383 | list_move_tail(&shmem_swaplist, &info->swaplist); |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 384 | |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 385 | /* |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 386 | * We rely on shmem_swaplist_mutex, not only to protect the swaplist, |
| 387 | * but also to hold up shmem_evict_inode(): so inode cannot be freed |
| 388 | * beneath us (pagelock doesn't help until the page is in pagecache). |
KAMEZAWA Hiroyuki | d13d144 | 2009-01-07 18:07:56 -0800 | [diff] [blame] | 389 | */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 390 | error = add_to_page_cache_locked(page, mapping, index, GFP_NOWAIT); |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 391 | /* which does mem_cgroup_uncharge_cache_page on error */ |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 392 | |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 393 | if (error != -ENOMEM) { |
Hugh Dickins | 73b1262 | 2008-02-04 22:28:50 -0800 | [diff] [blame] | 394 | delete_from_swap_cache(page); |
| 395 | set_page_dirty(page); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 396 | shmem_put_swap(info, index, (swp_entry_t){0}); |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 397 | info->swapped--; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 398 | swap_free(swap); |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 399 | error = 1; /* not an error, but entry was found */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | spin_unlock(&info->lock); |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 402 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* |
| 406 | * shmem_unuse() search for an eventually swapped out shmem page. |
| 407 | */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 408 | int shmem_unuse(swp_entry_t swap, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 410 | struct list_head *this, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | struct shmem_inode_info *info; |
| 412 | int found = 0; |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 413 | int error; |
| 414 | |
| 415 | /* |
| 416 | * Charge page using GFP_KERNEL while we can wait, before taking |
| 417 | * the shmem_swaplist_mutex which might hold up shmem_writepage(). |
| 418 | * Charged back to the user (not to caller) when swap account is used. |
| 419 | * add_to_page_cache() will be called with GFP_NOWAIT. |
| 420 | */ |
| 421 | error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL); |
| 422 | if (error) |
| 423 | goto out; |
| 424 | /* |
| 425 | * Try to preload while we can wait, to not make a habit of |
| 426 | * draining atomic reserves; but don't latch on to this cpu, |
| 427 | * it's okay if sometimes we get rescheduled after this. |
| 428 | */ |
| 429 | error = radix_tree_preload(GFP_KERNEL); |
| 430 | if (error) |
| 431 | goto uncharge; |
| 432 | radix_tree_preload_end(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 434 | mutex_lock(&shmem_swaplist_mutex); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 435 | list_for_each_safe(this, next, &shmem_swaplist) { |
| 436 | info = list_entry(this, struct shmem_inode_info, swaplist); |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 437 | if (!info->swapped) { |
| 438 | spin_lock(&info->lock); |
| 439 | if (!info->swapped) |
| 440 | list_del_init(&info->swaplist); |
| 441 | spin_unlock(&info->lock); |
| 442 | } |
| 443 | if (info->swapped) |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 444 | found = shmem_unuse_inode(info, swap, page); |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 445 | cond_resched(); |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 446 | if (found) |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 447 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 449 | mutex_unlock(&shmem_swaplist_mutex); |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 450 | |
| 451 | uncharge: |
| 452 | if (!found) |
| 453 | mem_cgroup_uncharge_cache_page(page); |
| 454 | if (found < 0) |
| 455 | error = found; |
| 456 | out: |
Hugh Dickins | aaa4686 | 2009-12-14 17:58:47 -0800 | [diff] [blame] | 457 | unlock_page(page); |
| 458 | page_cache_release(page); |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 459 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Move the page from the page cache to the swap cache. |
| 464 | */ |
| 465 | static int shmem_writepage(struct page *page, struct writeback_control *wbc) |
| 466 | { |
| 467 | struct shmem_inode_info *info; |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 468 | swp_entry_t swap, oswap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | struct address_space *mapping; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 470 | pgoff_t index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | struct inode *inode; |
| 472 | |
| 473 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | mapping = page->mapping; |
| 475 | index = page->index; |
| 476 | inode = mapping->host; |
| 477 | info = SHMEM_I(inode); |
| 478 | if (info->flags & VM_LOCKED) |
| 479 | goto redirty; |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 480 | if (!total_swap_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | goto redirty; |
| 482 | |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 483 | /* |
| 484 | * shmem_backing_dev_info's capabilities prevent regular writeback or |
| 485 | * sync from ever calling shmem_writepage; but a stacking filesystem |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 486 | * might use ->writepage of its underlying filesystem, in which case |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 487 | * tmpfs should write out to swap only in response to memory pressure, |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 488 | * and not for the writeback threads or sync. |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 489 | */ |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 490 | if (!wbc->for_reclaim) { |
| 491 | WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ |
| 492 | goto redirty; |
| 493 | } |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 494 | |
| 495 | /* |
| 496 | * Just for this patch, we have a toy implementation, |
| 497 | * which can swap out only the first SHMEM_NR_DIRECT pages: |
| 498 | * for simple demonstration of where we need to think about swap. |
| 499 | */ |
| 500 | if (index >= SHMEM_NR_DIRECT) |
| 501 | goto redirty; |
| 502 | |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 503 | swap = get_swap_page(); |
| 504 | if (!swap.val) |
| 505 | goto redirty; |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 506 | |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 507 | /* |
| 508 | * Add inode to shmem_unuse()'s list of swapped-out inodes, |
| 509 | * if it's not already there. Do it now because we cannot take |
| 510 | * mutex while holding spinlock, and must do so before the page |
| 511 | * is moved to swap cache, when its pagelock no longer protects |
| 512 | * the inode from eviction. But don't unlock the mutex until |
| 513 | * we've taken the spinlock, because shmem_unuse_inode() will |
| 514 | * prune a !swapped inode from the swaplist under both locks. |
| 515 | */ |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 516 | mutex_lock(&shmem_swaplist_mutex); |
| 517 | if (list_empty(&info->swaplist)) |
| 518 | list_add_tail(&info->swaplist, &shmem_swaplist); |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | spin_lock(&info->lock); |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 521 | mutex_unlock(&shmem_swaplist_mutex); |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 522 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 523 | oswap = shmem_get_swap(info, index); |
| 524 | if (oswap.val) { |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 525 | WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 526 | free_swap_and_cache(oswap); |
| 527 | shmem_put_swap(info, index, (swp_entry_t){0}); |
| 528 | info->swapped--; |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 529 | } |
| 530 | shmem_recalc_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 532 | if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { |
Minchan Kim | 4c73b1b | 2011-03-22 16:32:40 -0700 | [diff] [blame] | 533 | delete_from_page_cache(page); |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 534 | shmem_put_swap(info, index, swap); |
| 535 | info->swapped++; |
Hugh Dickins | aaa4686 | 2009-12-14 17:58:47 -0800 | [diff] [blame] | 536 | swap_shmem_alloc(swap); |
Hugh Dickins | 826267c | 2011-05-28 13:14:09 -0700 | [diff] [blame] | 537 | spin_unlock(&info->lock); |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 538 | BUG_ON(page_mapped(page)); |
Hugh Dickins | 9fab561 | 2009-03-31 15:23:33 -0700 | [diff] [blame] | 539 | swap_writepage(page, wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | spin_unlock(&info->lock); |
KAMEZAWA Hiroyuki | cb4b86b | 2009-06-16 15:32:52 -0700 | [diff] [blame] | 544 | swapcache_free(swap, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | redirty: |
| 546 | set_page_dirty(page); |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 547 | if (wbc->for_reclaim) |
| 548 | return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ |
| 549 | unlock_page(page); |
| 550 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | #ifdef CONFIG_NUMA |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 554 | #ifdef CONFIG_TMPFS |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 555 | static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 556 | { |
Lee Schermerhorn | 095f1fc | 2008-04-28 02:13:23 -0700 | [diff] [blame] | 557 | char buffer[64]; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 558 | |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 559 | if (!mpol || mpol->mode == MPOL_DEFAULT) |
Lee Schermerhorn | 095f1fc | 2008-04-28 02:13:23 -0700 | [diff] [blame] | 560 | return; /* show nothing */ |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 561 | |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 562 | mpol_to_str(buffer, sizeof(buffer), mpol, 1); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 563 | |
Lee Schermerhorn | 095f1fc | 2008-04-28 02:13:23 -0700 | [diff] [blame] | 564 | seq_printf(seq, ",mpol=%s", buffer); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 565 | } |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 566 | |
| 567 | static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) |
| 568 | { |
| 569 | struct mempolicy *mpol = NULL; |
| 570 | if (sbinfo->mpol) { |
| 571 | spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ |
| 572 | mpol = sbinfo->mpol; |
| 573 | mpol_get(mpol); |
| 574 | spin_unlock(&sbinfo->stat_lock); |
| 575 | } |
| 576 | return mpol; |
| 577 | } |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 578 | #endif /* CONFIG_TMPFS */ |
| 579 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 580 | static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, |
| 581 | struct shmem_inode_info *info, pgoff_t index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | { |
Lee Schermerhorn | 52cd3b0 | 2008-04-28 02:13:16 -0700 | [diff] [blame] | 583 | struct mempolicy mpol, *spol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | struct vm_area_struct pvma; |
| 585 | |
Lee Schermerhorn | 52cd3b0 | 2008-04-28 02:13:16 -0700 | [diff] [blame] | 586 | spol = mpol_cond_copy(&mpol, |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 587 | mpol_shared_policy_lookup(&info->policy, index)); |
Lee Schermerhorn | 52cd3b0 | 2008-04-28 02:13:16 -0700 | [diff] [blame] | 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | /* Create a pseudo vma that just contains the policy */ |
Hugh Dickins | c4cc6d0 | 2008-02-04 22:28:40 -0800 | [diff] [blame] | 590 | pvma.vm_start = 0; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 591 | pvma.vm_pgoff = index; |
Hugh Dickins | c4cc6d0 | 2008-02-04 22:28:40 -0800 | [diff] [blame] | 592 | pvma.vm_ops = NULL; |
Lee Schermerhorn | 52cd3b0 | 2008-04-28 02:13:16 -0700 | [diff] [blame] | 593 | pvma.vm_policy = spol; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 594 | return swapin_readahead(swap, gfp, &pvma, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Hugh Dickins | 02098fe | 2008-02-04 22:28:42 -0800 | [diff] [blame] | 597 | static struct page *shmem_alloc_page(gfp_t gfp, |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 598 | struct shmem_inode_info *info, pgoff_t index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | { |
| 600 | struct vm_area_struct pvma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
Hugh Dickins | c4cc6d0 | 2008-02-04 22:28:40 -0800 | [diff] [blame] | 602 | /* Create a pseudo vma that just contains the policy */ |
| 603 | pvma.vm_start = 0; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 604 | pvma.vm_pgoff = index; |
Hugh Dickins | c4cc6d0 | 2008-02-04 22:28:40 -0800 | [diff] [blame] | 605 | pvma.vm_ops = NULL; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 606 | pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); |
Lee Schermerhorn | 52cd3b0 | 2008-04-28 02:13:16 -0700 | [diff] [blame] | 607 | |
| 608 | /* |
| 609 | * alloc_page_vma() will drop the shared policy reference |
| 610 | */ |
| 611 | return alloc_page_vma(gfp, &pvma, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 613 | #else /* !CONFIG_NUMA */ |
| 614 | #ifdef CONFIG_TMPFS |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 615 | static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 616 | { |
| 617 | } |
| 618 | #endif /* CONFIG_TMPFS */ |
| 619 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 620 | static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, |
| 621 | struct shmem_inode_info *info, pgoff_t index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 623 | return swapin_readahead(swap, gfp, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Hugh Dickins | 02098fe | 2008-02-04 22:28:42 -0800 | [diff] [blame] | 626 | static inline struct page *shmem_alloc_page(gfp_t gfp, |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 627 | struct shmem_inode_info *info, pgoff_t index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
Hugh Dickins | e84e2e1 | 2007-11-28 18:55:10 +0000 | [diff] [blame] | 629 | return alloc_page(gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 631 | #endif /* CONFIG_NUMA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 633 | #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) |
| 634 | static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) |
| 635 | { |
| 636 | return NULL; |
| 637 | } |
| 638 | #endif |
| 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | /* |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 641 | * shmem_getpage_gfp - find page in cache, or get from swap, or allocate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | * |
| 643 | * If we allocate a new one we do not mark it dirty. That's up to the |
| 644 | * vm. If we swap it in we mark it dirty since we also free the swap |
| 645 | * entry since a page cannot live in both the swap and page cache |
| 646 | */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 647 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 648 | struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | { |
| 650 | struct address_space *mapping = inode->i_mapping; |
| 651 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 652 | struct shmem_sb_info *sbinfo; |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 653 | struct page *page; |
Shaohua Li | ff36b801 | 2010-08-09 17:19:06 -0700 | [diff] [blame] | 654 | struct page *prealloc_page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | swp_entry_t swap; |
| 656 | int error; |
| 657 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 658 | if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | return -EFBIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | repeat: |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 661 | page = find_lock_page(mapping, index); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 662 | if (page) { |
Hugh Dickins | b409f9f | 2008-02-04 22:28:54 -0800 | [diff] [blame] | 663 | /* |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 664 | * Once we can get the page lock, it must be uptodate: |
| 665 | * if there were an error in reading back from swap, |
| 666 | * the page would not be inserted into the filecache. |
Hugh Dickins | b409f9f | 2008-02-04 22:28:54 -0800 | [diff] [blame] | 667 | */ |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 668 | BUG_ON(!PageUptodate(page)); |
| 669 | goto done; |
| 670 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | /* |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 673 | * Try to preload while we can wait, to not make a habit of |
| 674 | * draining atomic reserves; but don't latch on to this cpu. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | */ |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 676 | error = radix_tree_preload(gfp & GFP_RECLAIM_MASK); |
| 677 | if (error) |
| 678 | goto out; |
| 679 | radix_tree_preload_end(); |
| 680 | |
| 681 | if (sgp != SGP_READ && !prealloc_page) { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 682 | prealloc_page = shmem_alloc_page(gfp, info, index); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 683 | if (prealloc_page) { |
| 684 | SetPageSwapBacked(prealloc_page); |
| 685 | if (mem_cgroup_cache_charge(prealloc_page, |
| 686 | current->mm, GFP_KERNEL)) { |
| 687 | page_cache_release(prealloc_page); |
| 688 | prealloc_page = NULL; |
Shaohua Li | ff36b801 | 2010-08-09 17:19:06 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
Hugh Dickins | b409f9f | 2008-02-04 22:28:54 -0800 | [diff] [blame] | 691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
| 693 | spin_lock(&info->lock); |
| 694 | shmem_recalc_inode(inode); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 695 | swap = shmem_get_swap(info, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | if (swap.val) { |
| 697 | /* Look it up and read it in.. */ |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 698 | page = lookup_swap_cache(swap); |
| 699 | if (!page) { |
Christoph Lameter | f8891e5 | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 700 | spin_unlock(&info->lock); |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 701 | /* here we actually do the io */ |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 702 | if (fault_type) |
| 703 | *fault_type |= VM_FAULT_MAJOR; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 704 | page = shmem_swapin(swap, gfp, info, index); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 705 | if (!page) { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 706 | swp_entry_t nswap = shmem_get_swap(info, index); |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 707 | if (nswap.val == swap.val) { |
| 708 | error = -ENOMEM; |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 709 | goto out; |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | goto repeat; |
| 712 | } |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 713 | wait_on_page_locked(page); |
| 714 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | goto repeat; |
| 716 | } |
| 717 | |
| 718 | /* We have to do this with page locked to prevent races */ |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 719 | if (!trylock_page(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | spin_unlock(&info->lock); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 721 | wait_on_page_locked(page); |
| 722 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | goto repeat; |
| 724 | } |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 725 | if (PageWriteback(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | spin_unlock(&info->lock); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 727 | wait_on_page_writeback(page); |
| 728 | unlock_page(page); |
| 729 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | goto repeat; |
| 731 | } |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 732 | if (!PageUptodate(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | spin_unlock(&info->lock); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 734 | unlock_page(page); |
| 735 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | error = -EIO; |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 737 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 740 | error = add_to_page_cache_locked(page, mapping, |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 741 | index, GFP_NOWAIT); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 742 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | spin_unlock(&info->lock); |
Hugh Dickins | 8236955 | 2008-02-07 00:14:22 -0800 | [diff] [blame] | 744 | if (error == -ENOMEM) { |
Daisuke Nishimura | ae3abae | 2009-04-30 15:08:19 -0700 | [diff] [blame] | 745 | /* |
| 746 | * reclaim from proper memory cgroup and |
| 747 | * call memcg's OOM if needed. |
| 748 | */ |
| 749 | error = mem_cgroup_shmem_charge_fallback( |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 750 | page, current->mm, gfp); |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 751 | if (error) { |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 752 | unlock_page(page); |
| 753 | page_cache_release(page); |
| 754 | goto out; |
KAMEZAWA Hiroyuki | b5a8431 | 2009-01-07 18:08:35 -0800 | [diff] [blame] | 755 | } |
Hugh Dickins | 8236955 | 2008-02-07 00:14:22 -0800 | [diff] [blame] | 756 | } |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 757 | unlock_page(page); |
| 758 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | goto repeat; |
| 760 | } |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 761 | |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 762 | delete_from_swap_cache(page); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 763 | shmem_put_swap(info, index, (swp_entry_t){0}); |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 764 | info->swapped--; |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 765 | spin_unlock(&info->lock); |
| 766 | set_page_dirty(page); |
| 767 | swap_free(swap); |
| 768 | |
| 769 | } else if (sgp == SGP_READ) { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 770 | page = find_get_page(mapping, index); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 771 | if (page && !trylock_page(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | spin_unlock(&info->lock); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 773 | wait_on_page_locked(page); |
| 774 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | goto repeat; |
| 776 | } |
| 777 | spin_unlock(&info->lock); |
Hugh Dickins | e83c32e | 2011-07-25 17:12:35 -0700 | [diff] [blame] | 778 | |
| 779 | } else if (prealloc_page) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | sbinfo = SHMEM_SB(inode->i_sb); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 781 | if (sbinfo->max_blocks) { |
Hugh Dickins | fc5da22 | 2011-04-14 15:22:07 -0700 | [diff] [blame] | 782 | if (percpu_counter_compare(&sbinfo->used_blocks, |
| 783 | sbinfo->max_blocks) >= 0 || |
Hugh Dickins | 59a16ea | 2011-05-11 15:13:38 -0700 | [diff] [blame] | 784 | shmem_acct_block(info->flags)) |
| 785 | goto nospace; |
Tim Chen | 7e49629 | 2010-08-09 17:19:05 -0700 | [diff] [blame] | 786 | percpu_counter_inc(&sbinfo->used_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | inode->i_blocks += BLOCKS_PER_PAGE; |
Hugh Dickins | 59a16ea | 2011-05-11 15:13:38 -0700 | [diff] [blame] | 788 | } else if (shmem_acct_block(info->flags)) |
| 789 | goto nospace; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 791 | page = prealloc_page; |
| 792 | prealloc_page = NULL; |
KAMEZAWA Hiroyuki | 69029cd | 2008-07-25 01:47:14 -0700 | [diff] [blame] | 793 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 794 | swap = shmem_get_swap(info, index); |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 795 | if (swap.val) |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 796 | mem_cgroup_uncharge_cache_page(page); |
| 797 | else |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 798 | error = add_to_page_cache_lru(page, mapping, |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 799 | index, GFP_NOWAIT); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 800 | /* |
| 801 | * At add_to_page_cache_lru() failure, |
| 802 | * uncharge will be done automatically. |
| 803 | */ |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 804 | if (swap.val || error) { |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 805 | shmem_unacct_blocks(info->flags, 1); |
| 806 | shmem_free_blocks(inode, 1); |
Hugh Dickins | 59a16ea | 2011-05-11 15:13:38 -0700 | [diff] [blame] | 807 | spin_unlock(&info->lock); |
| 808 | page_cache_release(page); |
| 809 | goto repeat; |
| 810 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
| 812 | info->alloced++; |
| 813 | spin_unlock(&info->lock); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 814 | clear_highpage(page); |
| 815 | flush_dcache_page(page); |
| 816 | SetPageUptodate(page); |
Hugh Dickins | a0ee5ec | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 817 | if (sgp == SGP_DIRTY) |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 818 | set_page_dirty(page); |
| 819 | |
Hugh Dickins | e83c32e | 2011-07-25 17:12:35 -0700 | [diff] [blame] | 820 | } else { |
| 821 | spin_unlock(&info->lock); |
| 822 | error = -ENOMEM; |
| 823 | goto out; |
Hugh Dickins | 59a16ea | 2011-05-11 15:13:38 -0700 | [diff] [blame] | 824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | done: |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 826 | *pagep = page; |
Shaohua Li | ff36b801 | 2010-08-09 17:19:06 -0700 | [diff] [blame] | 827 | error = 0; |
Shaohua Li | ff36b801 | 2010-08-09 17:19:06 -0700 | [diff] [blame] | 828 | out: |
| 829 | if (prealloc_page) { |
| 830 | mem_cgroup_uncharge_cache_page(prealloc_page); |
| 831 | page_cache_release(prealloc_page); |
| 832 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | return error; |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 834 | |
Hugh Dickins | 27d54b3 | 2008-02-04 22:28:43 -0800 | [diff] [blame] | 835 | nospace: |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 836 | /* |
| 837 | * Perhaps the page was brought in from swap between find_lock_page |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | * and taking info->lock? We allow for that at add_to_page_cache_lru, |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 839 | * but must also avoid reporting a spurious ENOSPC while working on a |
Hugh Dickins | 9276aad | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 840 | * full tmpfs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 842 | page = find_get_page(mapping, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | spin_unlock(&info->lock); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 844 | if (page) { |
| 845 | page_cache_release(page); |
| 846 | goto repeat; |
Josef "Jeff" Sipek | d3ac7f8 | 2006-12-08 02:36:44 -0800 | [diff] [blame] | 847 | } |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 848 | error = -ENOSPC; |
Hugh Dickins | e83c32e | 2011-07-25 17:12:35 -0700 | [diff] [blame] | 849 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 853 | { |
| 854 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
| 855 | int error; |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 856 | int ret = VM_FAULT_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
| 858 | if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode)) |
| 859 | return VM_FAULT_SIGBUS; |
| 860 | |
| 861 | error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); |
| 862 | if (error) |
| 863 | return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 864 | |
Ying Han | 456f998 | 2011-05-26 16:25:38 -0700 | [diff] [blame] | 865 | if (ret & VM_FAULT_MAJOR) { |
| 866 | count_vm_event(PGMAJFAULT); |
| 867 | mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); |
| 868 | } |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 869 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | #ifdef CONFIG_NUMA |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 873 | static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 875 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
| 876 | return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Adrian Bunk | d8dc74f | 2007-10-16 01:26:26 -0700 | [diff] [blame] | 879 | static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, |
| 880 | unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 882 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
| 883 | pgoff_t index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 885 | index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
| 886 | return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
| 888 | #endif |
| 889 | |
| 890 | int shmem_lock(struct file *file, int lock, struct user_struct *user) |
| 891 | { |
Josef "Jeff" Sipek | d3ac7f8 | 2006-12-08 02:36:44 -0800 | [diff] [blame] | 892 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 894 | int retval = -ENOMEM; |
| 895 | |
| 896 | spin_lock(&info->lock); |
| 897 | if (lock && !(info->flags & VM_LOCKED)) { |
| 898 | if (!user_shm_lock(inode->i_size, user)) |
| 899 | goto out_nomem; |
| 900 | info->flags |= VM_LOCKED; |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 901 | mapping_set_unevictable(file->f_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | } |
| 903 | if (!lock && (info->flags & VM_LOCKED) && user) { |
| 904 | user_shm_unlock(inode->i_size, user); |
| 905 | info->flags &= ~VM_LOCKED; |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 906 | mapping_clear_unevictable(file->f_mapping); |
| 907 | scan_mapping_unevictable_pages(file->f_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | } |
| 909 | retval = 0; |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | out_nomem: |
| 912 | spin_unlock(&info->lock); |
| 913 | return retval; |
| 914 | } |
| 915 | |
Adrian Bunk | 9b83a6a | 2007-02-28 20:11:03 -0800 | [diff] [blame] | 916 | static int shmem_mmap(struct file *file, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | { |
| 918 | file_accessed(file); |
| 919 | vma->vm_ops = &shmem_vm_ops; |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 920 | vma->vm_flags |= VM_CAN_NONLINEAR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | return 0; |
| 922 | } |
| 923 | |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 924 | static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, |
| 925 | int mode, dev_t dev, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | { |
| 927 | struct inode *inode; |
| 928 | struct shmem_inode_info *info; |
| 929 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 930 | |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 931 | if (shmem_reserve_inode(sb)) |
| 932 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | |
| 934 | inode = new_inode(sb); |
| 935 | if (inode) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 936 | inode->i_ino = get_next_ino(); |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 937 | inode_init_owner(inode, dir, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | inode->i_blocks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; |
| 940 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 941 | inode->i_generation = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | info = SHMEM_I(inode); |
| 943 | memset(info, 0, (char *)inode - (char *)info); |
| 944 | spin_lock_init(&info->lock); |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 945 | info->flags = flags & VM_NORESERVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | INIT_LIST_HEAD(&info->swaplist); |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 947 | INIT_LIST_HEAD(&info->xattr_list); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 948 | cache_no_acl(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
| 950 | switch (mode & S_IFMT) { |
| 951 | default: |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 952 | inode->i_op = &shmem_special_inode_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | init_special_inode(inode, mode, dev); |
| 954 | break; |
| 955 | case S_IFREG: |
Hugh Dickins | 14fcc23 | 2008-07-28 15:46:19 -0700 | [diff] [blame] | 956 | inode->i_mapping->a_ops = &shmem_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | inode->i_op = &shmem_inode_operations; |
| 958 | inode->i_fop = &shmem_file_operations; |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 959 | mpol_shared_policy_init(&info->policy, |
| 960 | shmem_get_sbmpol(sbinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | break; |
| 962 | case S_IFDIR: |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 963 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | /* Some things misbehave if size == 0 on a directory */ |
| 965 | inode->i_size = 2 * BOGO_DIRENT_SIZE; |
| 966 | inode->i_op = &shmem_dir_inode_operations; |
| 967 | inode->i_fop = &simple_dir_operations; |
| 968 | break; |
| 969 | case S_IFLNK: |
| 970 | /* |
| 971 | * Must not load anything in the rbtree, |
| 972 | * mpol_free_shared_policy will not be called. |
| 973 | */ |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 974 | mpol_shared_policy_init(&info->policy, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | break; |
| 976 | } |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 977 | } else |
| 978 | shmem_free_inode(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | return inode; |
| 980 | } |
| 981 | |
| 982 | #ifdef CONFIG_TMPFS |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 983 | static const struct inode_operations shmem_symlink_inode_operations; |
| 984 | static const struct inode_operations shmem_symlink_inline_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | static int |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 987 | shmem_write_begin(struct file *file, struct address_space *mapping, |
| 988 | loff_t pos, unsigned len, unsigned flags, |
| 989 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | { |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 991 | struct inode *inode = mapping->host; |
| 992 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 993 | return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); |
| 994 | } |
| 995 | |
| 996 | static int |
| 997 | shmem_write_end(struct file *file, struct address_space *mapping, |
| 998 | loff_t pos, unsigned len, unsigned copied, |
| 999 | struct page *page, void *fsdata) |
| 1000 | { |
| 1001 | struct inode *inode = mapping->host; |
| 1002 | |
Hugh Dickins | d360244 | 2008-02-04 22:28:44 -0800 | [diff] [blame] | 1003 | if (pos + copied > inode->i_size) |
| 1004 | i_size_write(inode, pos + copied); |
| 1005 | |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 1006 | set_page_dirty(page); |
Wu Fengguang | 6746aff | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 1007 | unlock_page(page); |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 1008 | page_cache_release(page); |
| 1009 | |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 1010 | return copied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) |
| 1014 | { |
Josef "Jeff" Sipek | d3ac7f8 | 2006-12-08 02:36:44 -0800 | [diff] [blame] | 1015 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | struct address_space *mapping = inode->i_mapping; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 1017 | pgoff_t index; |
| 1018 | unsigned long offset; |
Hugh Dickins | a0ee5ec | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1019 | enum sgp_type sgp = SGP_READ; |
| 1020 | |
| 1021 | /* |
| 1022 | * Might this read be for a stacking filesystem? Then when reading |
| 1023 | * holes of a sparse file, we actually need to allocate those pages, |
| 1024 | * and even mark them dirty, so it cannot exceed the max_blocks limit. |
| 1025 | */ |
| 1026 | if (segment_eq(get_fs(), KERNEL_DS)) |
| 1027 | sgp = SGP_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
| 1029 | index = *ppos >> PAGE_CACHE_SHIFT; |
| 1030 | offset = *ppos & ~PAGE_CACHE_MASK; |
| 1031 | |
| 1032 | for (;;) { |
| 1033 | struct page *page = NULL; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 1034 | pgoff_t end_index; |
| 1035 | unsigned long nr, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | loff_t i_size = i_size_read(inode); |
| 1037 | |
| 1038 | end_index = i_size >> PAGE_CACHE_SHIFT; |
| 1039 | if (index > end_index) |
| 1040 | break; |
| 1041 | if (index == end_index) { |
| 1042 | nr = i_size & ~PAGE_CACHE_MASK; |
| 1043 | if (nr <= offset) |
| 1044 | break; |
| 1045 | } |
| 1046 | |
Hugh Dickins | a0ee5ec | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1047 | desc->error = shmem_getpage(inode, index, &page, sgp, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | if (desc->error) { |
| 1049 | if (desc->error == -EINVAL) |
| 1050 | desc->error = 0; |
| 1051 | break; |
| 1052 | } |
Hugh Dickins | d360244 | 2008-02-04 22:28:44 -0800 | [diff] [blame] | 1053 | if (page) |
| 1054 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
| 1056 | /* |
| 1057 | * We must evaluate after, since reads (unlike writes) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1058 | * are called without i_mutex protection against truncate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | */ |
| 1060 | nr = PAGE_CACHE_SIZE; |
| 1061 | i_size = i_size_read(inode); |
| 1062 | end_index = i_size >> PAGE_CACHE_SHIFT; |
| 1063 | if (index == end_index) { |
| 1064 | nr = i_size & ~PAGE_CACHE_MASK; |
| 1065 | if (nr <= offset) { |
| 1066 | if (page) |
| 1067 | page_cache_release(page); |
| 1068 | break; |
| 1069 | } |
| 1070 | } |
| 1071 | nr -= offset; |
| 1072 | |
| 1073 | if (page) { |
| 1074 | /* |
| 1075 | * If users can be writing to this page using arbitrary |
| 1076 | * virtual addresses, take care about potential aliasing |
| 1077 | * before reading the page on the kernel side. |
| 1078 | */ |
| 1079 | if (mapping_writably_mapped(mapping)) |
| 1080 | flush_dcache_page(page); |
| 1081 | /* |
| 1082 | * Mark the page accessed if we read the beginning. |
| 1083 | */ |
| 1084 | if (!offset) |
| 1085 | mark_page_accessed(page); |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 1086 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | page = ZERO_PAGE(0); |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 1088 | page_cache_get(page); |
| 1089 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
| 1091 | /* |
| 1092 | * Ok, we have the page, and it's up-to-date, so |
| 1093 | * now we can copy it to user space... |
| 1094 | * |
| 1095 | * The actor routine returns how many bytes were actually used.. |
| 1096 | * NOTE! This may not be the same as how much of a user buffer |
| 1097 | * we filled up (we may be padding etc), so we can only update |
| 1098 | * "pos" here (the actor routine has to update the user buffer |
| 1099 | * pointers and the remaining count). |
| 1100 | */ |
| 1101 | ret = actor(desc, page, offset, nr); |
| 1102 | offset += ret; |
| 1103 | index += offset >> PAGE_CACHE_SHIFT; |
| 1104 | offset &= ~PAGE_CACHE_MASK; |
| 1105 | |
| 1106 | page_cache_release(page); |
| 1107 | if (ret != nr || !desc->count) |
| 1108 | break; |
| 1109 | |
| 1110 | cond_resched(); |
| 1111 | } |
| 1112 | |
| 1113 | *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; |
| 1114 | file_accessed(filp); |
| 1115 | } |
| 1116 | |
Hugh Dickins | bcd78e4 | 2008-07-23 21:27:35 -0700 | [diff] [blame] | 1117 | static ssize_t shmem_file_aio_read(struct kiocb *iocb, |
| 1118 | const struct iovec *iov, unsigned long nr_segs, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | { |
Hugh Dickins | bcd78e4 | 2008-07-23 21:27:35 -0700 | [diff] [blame] | 1120 | struct file *filp = iocb->ki_filp; |
| 1121 | ssize_t retval; |
| 1122 | unsigned long seg; |
| 1123 | size_t count; |
| 1124 | loff_t *ppos = &iocb->ki_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | |
Hugh Dickins | bcd78e4 | 2008-07-23 21:27:35 -0700 | [diff] [blame] | 1126 | retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); |
| 1127 | if (retval) |
| 1128 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | |
Hugh Dickins | bcd78e4 | 2008-07-23 21:27:35 -0700 | [diff] [blame] | 1130 | for (seg = 0; seg < nr_segs; seg++) { |
| 1131 | read_descriptor_t desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | |
Hugh Dickins | bcd78e4 | 2008-07-23 21:27:35 -0700 | [diff] [blame] | 1133 | desc.written = 0; |
| 1134 | desc.arg.buf = iov[seg].iov_base; |
| 1135 | desc.count = iov[seg].iov_len; |
| 1136 | if (desc.count == 0) |
| 1137 | continue; |
| 1138 | desc.error = 0; |
| 1139 | do_shmem_file_read(filp, ppos, &desc, file_read_actor); |
| 1140 | retval += desc.written; |
| 1141 | if (desc.error) { |
| 1142 | retval = retval ?: desc.error; |
| 1143 | break; |
| 1144 | } |
| 1145 | if (desc.count > 0) |
| 1146 | break; |
| 1147 | } |
| 1148 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1151 | static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, |
| 1152 | struct pipe_inode_info *pipe, size_t len, |
| 1153 | unsigned int flags) |
| 1154 | { |
| 1155 | struct address_space *mapping = in->f_mapping; |
Hugh Dickins | 71f0e07 | 2011-07-25 17:12:33 -0700 | [diff] [blame] | 1156 | struct inode *inode = mapping->host; |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1157 | unsigned int loff, nr_pages, req_pages; |
| 1158 | struct page *pages[PIPE_DEF_BUFFERS]; |
| 1159 | struct partial_page partial[PIPE_DEF_BUFFERS]; |
| 1160 | struct page *page; |
| 1161 | pgoff_t index, end_index; |
| 1162 | loff_t isize, left; |
| 1163 | int error, page_nr; |
| 1164 | struct splice_pipe_desc spd = { |
| 1165 | .pages = pages, |
| 1166 | .partial = partial, |
| 1167 | .flags = flags, |
| 1168 | .ops = &page_cache_pipe_buf_ops, |
| 1169 | .spd_release = spd_release_page, |
| 1170 | }; |
| 1171 | |
Hugh Dickins | 71f0e07 | 2011-07-25 17:12:33 -0700 | [diff] [blame] | 1172 | isize = i_size_read(inode); |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1173 | if (unlikely(*ppos >= isize)) |
| 1174 | return 0; |
| 1175 | |
| 1176 | left = isize - *ppos; |
| 1177 | if (unlikely(left < len)) |
| 1178 | len = left; |
| 1179 | |
| 1180 | if (splice_grow_spd(pipe, &spd)) |
| 1181 | return -ENOMEM; |
| 1182 | |
| 1183 | index = *ppos >> PAGE_CACHE_SHIFT; |
| 1184 | loff = *ppos & ~PAGE_CACHE_MASK; |
| 1185 | req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 1186 | nr_pages = min(req_pages, pipe->buffers); |
| 1187 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1188 | spd.nr_pages = find_get_pages_contig(mapping, index, |
| 1189 | nr_pages, spd.pages); |
| 1190 | index += spd.nr_pages; |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1191 | error = 0; |
Hugh Dickins | 71f0e07 | 2011-07-25 17:12:33 -0700 | [diff] [blame] | 1192 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1193 | while (spd.nr_pages < nr_pages) { |
Hugh Dickins | 71f0e07 | 2011-07-25 17:12:33 -0700 | [diff] [blame] | 1194 | error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); |
| 1195 | if (error) |
| 1196 | break; |
| 1197 | unlock_page(page); |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1198 | spd.pages[spd.nr_pages++] = page; |
| 1199 | index++; |
| 1200 | } |
| 1201 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1202 | index = *ppos >> PAGE_CACHE_SHIFT; |
| 1203 | nr_pages = spd.nr_pages; |
| 1204 | spd.nr_pages = 0; |
Hugh Dickins | 71f0e07 | 2011-07-25 17:12:33 -0700 | [diff] [blame] | 1205 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1206 | for (page_nr = 0; page_nr < nr_pages; page_nr++) { |
| 1207 | unsigned int this_len; |
| 1208 | |
| 1209 | if (!len) |
| 1210 | break; |
| 1211 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1212 | this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); |
| 1213 | page = spd.pages[page_nr]; |
| 1214 | |
Hugh Dickins | 71f0e07 | 2011-07-25 17:12:33 -0700 | [diff] [blame] | 1215 | if (!PageUptodate(page) || page->mapping != mapping) { |
Hugh Dickins | 71f0e07 | 2011-07-25 17:12:33 -0700 | [diff] [blame] | 1216 | error = shmem_getpage(inode, index, &page, |
| 1217 | SGP_CACHE, NULL); |
| 1218 | if (error) |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1219 | break; |
Hugh Dickins | 71f0e07 | 2011-07-25 17:12:33 -0700 | [diff] [blame] | 1220 | unlock_page(page); |
| 1221 | page_cache_release(spd.pages[page_nr]); |
| 1222 | spd.pages[page_nr] = page; |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1223 | } |
Hugh Dickins | 71f0e07 | 2011-07-25 17:12:33 -0700 | [diff] [blame] | 1224 | |
| 1225 | isize = i_size_read(inode); |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1226 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
| 1227 | if (unlikely(!isize || index > end_index)) |
| 1228 | break; |
| 1229 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1230 | if (end_index == index) { |
| 1231 | unsigned int plen; |
| 1232 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1233 | plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; |
| 1234 | if (plen <= loff) |
| 1235 | break; |
| 1236 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1237 | this_len = min(this_len, plen - loff); |
| 1238 | len = this_len; |
| 1239 | } |
| 1240 | |
| 1241 | spd.partial[page_nr].offset = loff; |
| 1242 | spd.partial[page_nr].len = this_len; |
| 1243 | len -= this_len; |
| 1244 | loff = 0; |
| 1245 | spd.nr_pages++; |
| 1246 | index++; |
| 1247 | } |
| 1248 | |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1249 | while (page_nr < nr_pages) |
| 1250 | page_cache_release(spd.pages[page_nr++]); |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 1251 | |
| 1252 | if (spd.nr_pages) |
| 1253 | error = splice_to_pipe(pipe, &spd); |
| 1254 | |
| 1255 | splice_shrink_spd(pipe, &spd); |
| 1256 | |
| 1257 | if (error > 0) { |
| 1258 | *ppos += error; |
| 1259 | file_accessed(in); |
| 1260 | } |
| 1261 | return error; |
| 1262 | } |
| 1263 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 1264 | static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | { |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 1266 | struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
| 1268 | buf->f_type = TMPFS_MAGIC; |
| 1269 | buf->f_bsize = PAGE_CACHE_SIZE; |
| 1270 | buf->f_namelen = NAME_MAX; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1271 | if (sbinfo->max_blocks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | buf->f_blocks = sbinfo->max_blocks; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 1273 | buf->f_bavail = |
| 1274 | buf->f_bfree = sbinfo->max_blocks - |
| 1275 | percpu_counter_sum(&sbinfo->used_blocks); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1276 | } |
| 1277 | if (sbinfo->max_inodes) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | buf->f_files = sbinfo->max_inodes; |
| 1279 | buf->f_ffree = sbinfo->free_inodes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | } |
| 1281 | /* else leave those fields 0 like simple_statfs */ |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * File creation. Allocate an inode, and we're done.. |
| 1287 | */ |
| 1288 | static int |
| 1289 | shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) |
| 1290 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 1291 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | int error = -ENOSPC; |
| 1293 | |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 1294 | inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | if (inode) { |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 1296 | error = security_inode_init_security(inode, dir, |
| 1297 | &dentry->d_name, NULL, |
| 1298 | NULL, NULL); |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 1299 | if (error) { |
| 1300 | if (error != -EOPNOTSUPP) { |
| 1301 | iput(inode); |
| 1302 | return error; |
| 1303 | } |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 1304 | } |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 1305 | #ifdef CONFIG_TMPFS_POSIX_ACL |
| 1306 | error = generic_acl_init(inode, dir); |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 1307 | if (error) { |
| 1308 | iput(inode); |
| 1309 | return error; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 1310 | } |
Al Viro | 718deb6 | 2009-12-16 19:35:36 -0500 | [diff] [blame] | 1311 | #else |
| 1312 | error = 0; |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 1313 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | dir->i_size += BOGO_DIRENT_SIZE; |
| 1315 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 1316 | d_instantiate(dentry, inode); |
| 1317 | dget(dentry); /* Extra count - pin the dentry in core */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | } |
| 1319 | return error; |
| 1320 | } |
| 1321 | |
| 1322 | static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1323 | { |
| 1324 | int error; |
| 1325 | |
| 1326 | if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) |
| 1327 | return error; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1328 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | static int shmem_create(struct inode *dir, struct dentry *dentry, int mode, |
| 1333 | struct nameidata *nd) |
| 1334 | { |
| 1335 | return shmem_mknod(dir, dentry, mode | S_IFREG, 0); |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * Link a file.. |
| 1340 | */ |
| 1341 | static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
| 1342 | { |
| 1343 | struct inode *inode = old_dentry->d_inode; |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 1344 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
| 1346 | /* |
| 1347 | * No ordinary (disk based) filesystem counts links as inodes; |
| 1348 | * but each new link needs a new dentry, pinning lowmem, and |
| 1349 | * tmpfs dentries cannot be pruned until they are unlinked. |
| 1350 | */ |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 1351 | ret = shmem_reserve_inode(inode->i_sb); |
| 1352 | if (ret) |
| 1353 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | |
| 1355 | dir->i_size += BOGO_DIRENT_SIZE; |
| 1356 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1357 | inc_nlink(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 1358 | ihold(inode); /* New dentry reference */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | dget(dentry); /* Extra pinning count for the created dentry */ |
| 1360 | d_instantiate(dentry, inode); |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 1361 | out: |
| 1362 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | static int shmem_unlink(struct inode *dir, struct dentry *dentry) |
| 1366 | { |
| 1367 | struct inode *inode = dentry->d_inode; |
| 1368 | |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 1369 | if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) |
| 1370 | shmem_free_inode(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
| 1372 | dir->i_size -= BOGO_DIRENT_SIZE; |
| 1373 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 1374 | drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | dput(dentry); /* Undo the count from "create" - this does all the work */ |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | static int shmem_rmdir(struct inode *dir, struct dentry *dentry) |
| 1380 | { |
| 1381 | if (!simple_empty(dentry)) |
| 1382 | return -ENOTEMPTY; |
| 1383 | |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 1384 | drop_nlink(dentry->d_inode); |
| 1385 | drop_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | return shmem_unlink(dir, dentry); |
| 1387 | } |
| 1388 | |
| 1389 | /* |
| 1390 | * The VFS layer already does all the dentry stuff for rename, |
| 1391 | * we just have to decrement the usage count for the target if |
| 1392 | * it exists so that the VFS layer correctly free's it when it |
| 1393 | * gets overwritten. |
| 1394 | */ |
| 1395 | static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) |
| 1396 | { |
| 1397 | struct inode *inode = old_dentry->d_inode; |
| 1398 | int they_are_dirs = S_ISDIR(inode->i_mode); |
| 1399 | |
| 1400 | if (!simple_empty(new_dentry)) |
| 1401 | return -ENOTEMPTY; |
| 1402 | |
| 1403 | if (new_dentry->d_inode) { |
| 1404 | (void) shmem_unlink(new_dir, new_dentry); |
| 1405 | if (they_are_dirs) |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 1406 | drop_nlink(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | } else if (they_are_dirs) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 1408 | drop_nlink(old_dir); |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1409 | inc_nlink(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | old_dir->i_size -= BOGO_DIRENT_SIZE; |
| 1413 | new_dir->i_size += BOGO_DIRENT_SIZE; |
| 1414 | old_dir->i_ctime = old_dir->i_mtime = |
| 1415 | new_dir->i_ctime = new_dir->i_mtime = |
| 1416 | inode->i_ctime = CURRENT_TIME; |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
| 1420 | static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) |
| 1421 | { |
| 1422 | int error; |
| 1423 | int len; |
| 1424 | struct inode *inode; |
Hugh Dickins | 9276aad | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 1425 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | char *kaddr; |
| 1427 | struct shmem_inode_info *info; |
| 1428 | |
| 1429 | len = strlen(symname) + 1; |
| 1430 | if (len > PAGE_CACHE_SIZE) |
| 1431 | return -ENAMETOOLONG; |
| 1432 | |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 1433 | inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | if (!inode) |
| 1435 | return -ENOSPC; |
| 1436 | |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 1437 | error = security_inode_init_security(inode, dir, &dentry->d_name, NULL, |
| 1438 | NULL, NULL); |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 1439 | if (error) { |
| 1440 | if (error != -EOPNOTSUPP) { |
| 1441 | iput(inode); |
| 1442 | return error; |
| 1443 | } |
| 1444 | error = 0; |
| 1445 | } |
| 1446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | info = SHMEM_I(inode); |
| 1448 | inode->i_size = len-1; |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 1449 | if (len <= SHMEM_SYMLINK_INLINE_LEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | /* do it inline */ |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 1451 | memcpy(info->inline_symlink, symname, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | inode->i_op = &shmem_symlink_inline_operations; |
| 1453 | } else { |
| 1454 | error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); |
| 1455 | if (error) { |
| 1456 | iput(inode); |
| 1457 | return error; |
| 1458 | } |
Hugh Dickins | 14fcc23 | 2008-07-28 15:46:19 -0700 | [diff] [blame] | 1459 | inode->i_mapping->a_ops = &shmem_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | inode->i_op = &shmem_symlink_inode_operations; |
| 1461 | kaddr = kmap_atomic(page, KM_USER0); |
| 1462 | memcpy(kaddr, symname, len); |
| 1463 | kunmap_atomic(kaddr, KM_USER0); |
| 1464 | set_page_dirty(page); |
Wu Fengguang | 6746aff | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 1465 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | page_cache_release(page); |
| 1467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | dir->i_size += BOGO_DIRENT_SIZE; |
| 1469 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 1470 | d_instantiate(dentry, inode); |
| 1471 | dget(dentry); |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 1475 | static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | { |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 1477 | nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink); |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 1478 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 1481 | static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | { |
| 1483 | struct page *page = NULL; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 1484 | int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); |
| 1485 | nd_set_link(nd, error ? ERR_PTR(error) : kmap(page)); |
Hugh Dickins | d360244 | 2008-02-04 22:28:44 -0800 | [diff] [blame] | 1486 | if (page) |
| 1487 | unlock_page(page); |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 1488 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 1491 | static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | { |
| 1493 | if (!IS_ERR(nd_get_link(nd))) { |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 1494 | struct page *page = cookie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | kunmap(page); |
| 1496 | mark_page_accessed(page); |
| 1497 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 1501 | #ifdef CONFIG_TMPFS_XATTR |
| 1502 | /* |
| 1503 | * Superblocks without xattr inode operations may get some security.* xattr |
| 1504 | * support from the LSM "for free". As soon as we have any other xattrs |
| 1505 | * like ACLs, we also need to implement the security.* handlers at |
| 1506 | * filesystem level, though. |
| 1507 | */ |
| 1508 | |
| 1509 | static int shmem_xattr_get(struct dentry *dentry, const char *name, |
| 1510 | void *buffer, size_t size) |
| 1511 | { |
| 1512 | struct shmem_inode_info *info; |
| 1513 | struct shmem_xattr *xattr; |
| 1514 | int ret = -ENODATA; |
| 1515 | |
| 1516 | info = SHMEM_I(dentry->d_inode); |
| 1517 | |
| 1518 | spin_lock(&info->lock); |
| 1519 | list_for_each_entry(xattr, &info->xattr_list, list) { |
| 1520 | if (strcmp(name, xattr->name)) |
| 1521 | continue; |
| 1522 | |
| 1523 | ret = xattr->size; |
| 1524 | if (buffer) { |
| 1525 | if (size < xattr->size) |
| 1526 | ret = -ERANGE; |
| 1527 | else |
| 1528 | memcpy(buffer, xattr->value, xattr->size); |
| 1529 | } |
| 1530 | break; |
| 1531 | } |
| 1532 | spin_unlock(&info->lock); |
| 1533 | return ret; |
| 1534 | } |
| 1535 | |
| 1536 | static int shmem_xattr_set(struct dentry *dentry, const char *name, |
| 1537 | const void *value, size_t size, int flags) |
| 1538 | { |
| 1539 | struct inode *inode = dentry->d_inode; |
| 1540 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 1541 | struct shmem_xattr *xattr; |
| 1542 | struct shmem_xattr *new_xattr = NULL; |
| 1543 | size_t len; |
| 1544 | int err = 0; |
| 1545 | |
| 1546 | /* value == NULL means remove */ |
| 1547 | if (value) { |
| 1548 | /* wrap around? */ |
| 1549 | len = sizeof(*new_xattr) + size; |
| 1550 | if (len <= sizeof(*new_xattr)) |
| 1551 | return -ENOMEM; |
| 1552 | |
| 1553 | new_xattr = kmalloc(len, GFP_KERNEL); |
| 1554 | if (!new_xattr) |
| 1555 | return -ENOMEM; |
| 1556 | |
| 1557 | new_xattr->name = kstrdup(name, GFP_KERNEL); |
| 1558 | if (!new_xattr->name) { |
| 1559 | kfree(new_xattr); |
| 1560 | return -ENOMEM; |
| 1561 | } |
| 1562 | |
| 1563 | new_xattr->size = size; |
| 1564 | memcpy(new_xattr->value, value, size); |
| 1565 | } |
| 1566 | |
| 1567 | spin_lock(&info->lock); |
| 1568 | list_for_each_entry(xattr, &info->xattr_list, list) { |
| 1569 | if (!strcmp(name, xattr->name)) { |
| 1570 | if (flags & XATTR_CREATE) { |
| 1571 | xattr = new_xattr; |
| 1572 | err = -EEXIST; |
| 1573 | } else if (new_xattr) { |
| 1574 | list_replace(&xattr->list, &new_xattr->list); |
| 1575 | } else { |
| 1576 | list_del(&xattr->list); |
| 1577 | } |
| 1578 | goto out; |
| 1579 | } |
| 1580 | } |
| 1581 | if (flags & XATTR_REPLACE) { |
| 1582 | xattr = new_xattr; |
| 1583 | err = -ENODATA; |
| 1584 | } else { |
| 1585 | list_add(&new_xattr->list, &info->xattr_list); |
| 1586 | xattr = NULL; |
| 1587 | } |
| 1588 | out: |
| 1589 | spin_unlock(&info->lock); |
| 1590 | if (xattr) |
| 1591 | kfree(xattr->name); |
| 1592 | kfree(xattr); |
| 1593 | return err; |
| 1594 | } |
| 1595 | |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 1596 | static const struct xattr_handler *shmem_xattr_handlers[] = { |
| 1597 | #ifdef CONFIG_TMPFS_POSIX_ACL |
| 1598 | &generic_acl_access_handler, |
| 1599 | &generic_acl_default_handler, |
| 1600 | #endif |
| 1601 | NULL |
| 1602 | }; |
| 1603 | |
| 1604 | static int shmem_xattr_validate(const char *name) |
| 1605 | { |
| 1606 | struct { const char *prefix; size_t len; } arr[] = { |
| 1607 | { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, |
| 1608 | { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } |
| 1609 | }; |
| 1610 | int i; |
| 1611 | |
| 1612 | for (i = 0; i < ARRAY_SIZE(arr); i++) { |
| 1613 | size_t preflen = arr[i].len; |
| 1614 | if (strncmp(name, arr[i].prefix, preflen) == 0) { |
| 1615 | if (!name[preflen]) |
| 1616 | return -EINVAL; |
| 1617 | return 0; |
| 1618 | } |
| 1619 | } |
| 1620 | return -EOPNOTSUPP; |
| 1621 | } |
| 1622 | |
| 1623 | static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, |
| 1624 | void *buffer, size_t size) |
| 1625 | { |
| 1626 | int err; |
| 1627 | |
| 1628 | /* |
| 1629 | * If this is a request for a synthetic attribute in the system.* |
| 1630 | * namespace use the generic infrastructure to resolve a handler |
| 1631 | * for it via sb->s_xattr. |
| 1632 | */ |
| 1633 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) |
| 1634 | return generic_getxattr(dentry, name, buffer, size); |
| 1635 | |
| 1636 | err = shmem_xattr_validate(name); |
| 1637 | if (err) |
| 1638 | return err; |
| 1639 | |
| 1640 | return shmem_xattr_get(dentry, name, buffer, size); |
| 1641 | } |
| 1642 | |
| 1643 | static int shmem_setxattr(struct dentry *dentry, const char *name, |
| 1644 | const void *value, size_t size, int flags) |
| 1645 | { |
| 1646 | int err; |
| 1647 | |
| 1648 | /* |
| 1649 | * If this is a request for a synthetic attribute in the system.* |
| 1650 | * namespace use the generic infrastructure to resolve a handler |
| 1651 | * for it via sb->s_xattr. |
| 1652 | */ |
| 1653 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) |
| 1654 | return generic_setxattr(dentry, name, value, size, flags); |
| 1655 | |
| 1656 | err = shmem_xattr_validate(name); |
| 1657 | if (err) |
| 1658 | return err; |
| 1659 | |
| 1660 | if (size == 0) |
| 1661 | value = ""; /* empty EA, do not remove */ |
| 1662 | |
| 1663 | return shmem_xattr_set(dentry, name, value, size, flags); |
| 1664 | |
| 1665 | } |
| 1666 | |
| 1667 | static int shmem_removexattr(struct dentry *dentry, const char *name) |
| 1668 | { |
| 1669 | int err; |
| 1670 | |
| 1671 | /* |
| 1672 | * If this is a request for a synthetic attribute in the system.* |
| 1673 | * namespace use the generic infrastructure to resolve a handler |
| 1674 | * for it via sb->s_xattr. |
| 1675 | */ |
| 1676 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) |
| 1677 | return generic_removexattr(dentry, name); |
| 1678 | |
| 1679 | err = shmem_xattr_validate(name); |
| 1680 | if (err) |
| 1681 | return err; |
| 1682 | |
| 1683 | return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE); |
| 1684 | } |
| 1685 | |
| 1686 | static bool xattr_is_trusted(const char *name) |
| 1687 | { |
| 1688 | return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN); |
| 1689 | } |
| 1690 | |
| 1691 | static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) |
| 1692 | { |
| 1693 | bool trusted = capable(CAP_SYS_ADMIN); |
| 1694 | struct shmem_xattr *xattr; |
| 1695 | struct shmem_inode_info *info; |
| 1696 | size_t used = 0; |
| 1697 | |
| 1698 | info = SHMEM_I(dentry->d_inode); |
| 1699 | |
| 1700 | spin_lock(&info->lock); |
| 1701 | list_for_each_entry(xattr, &info->xattr_list, list) { |
| 1702 | size_t len; |
| 1703 | |
| 1704 | /* skip "trusted." attributes for unprivileged callers */ |
| 1705 | if (!trusted && xattr_is_trusted(xattr->name)) |
| 1706 | continue; |
| 1707 | |
| 1708 | len = strlen(xattr->name) + 1; |
| 1709 | used += len; |
| 1710 | if (buffer) { |
| 1711 | if (size < used) { |
| 1712 | used = -ERANGE; |
| 1713 | break; |
| 1714 | } |
| 1715 | memcpy(buffer, xattr->name, len); |
| 1716 | buffer += len; |
| 1717 | } |
| 1718 | } |
| 1719 | spin_unlock(&info->lock); |
| 1720 | |
| 1721 | return used; |
| 1722 | } |
| 1723 | #endif /* CONFIG_TMPFS_XATTR */ |
| 1724 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1725 | static const struct inode_operations shmem_symlink_inline_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | .readlink = generic_readlink, |
| 1727 | .follow_link = shmem_follow_link_inline, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 1728 | #ifdef CONFIG_TMPFS_XATTR |
| 1729 | .setxattr = shmem_setxattr, |
| 1730 | .getxattr = shmem_getxattr, |
| 1731 | .listxattr = shmem_listxattr, |
| 1732 | .removexattr = shmem_removexattr, |
| 1733 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | }; |
| 1735 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1736 | static const struct inode_operations shmem_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | .readlink = generic_readlink, |
| 1738 | .follow_link = shmem_follow_link, |
| 1739 | .put_link = shmem_put_link, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 1740 | #ifdef CONFIG_TMPFS_XATTR |
| 1741 | .setxattr = shmem_setxattr, |
| 1742 | .getxattr = shmem_getxattr, |
| 1743 | .listxattr = shmem_listxattr, |
| 1744 | .removexattr = shmem_removexattr, |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 1745 | #endif |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 1746 | }; |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 1747 | |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1748 | static struct dentry *shmem_get_parent(struct dentry *child) |
| 1749 | { |
| 1750 | return ERR_PTR(-ESTALE); |
| 1751 | } |
| 1752 | |
| 1753 | static int shmem_match(struct inode *ino, void *vfh) |
| 1754 | { |
| 1755 | __u32 *fh = vfh; |
| 1756 | __u64 inum = fh[2]; |
| 1757 | inum = (inum << 32) | fh[1]; |
| 1758 | return ino->i_ino == inum && fh[0] == ino->i_generation; |
| 1759 | } |
| 1760 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 1761 | static struct dentry *shmem_fh_to_dentry(struct super_block *sb, |
| 1762 | struct fid *fid, int fh_len, int fh_type) |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1763 | { |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1764 | struct inode *inode; |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 1765 | struct dentry *dentry = NULL; |
| 1766 | u64 inum = fid->raw[2]; |
| 1767 | inum = (inum << 32) | fid->raw[1]; |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1768 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 1769 | if (fh_len < 3) |
| 1770 | return NULL; |
| 1771 | |
| 1772 | inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), |
| 1773 | shmem_match, fid->raw); |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1774 | if (inode) { |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 1775 | dentry = d_find_alias(inode); |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1776 | iput(inode); |
| 1777 | } |
| 1778 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 1779 | return dentry; |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len, |
| 1783 | int connectable) |
| 1784 | { |
| 1785 | struct inode *inode = dentry->d_inode; |
| 1786 | |
Aneesh Kumar K.V | 5fe0c23 | 2011-01-29 18:43:25 +0530 | [diff] [blame] | 1787 | if (*len < 3) { |
| 1788 | *len = 3; |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1789 | return 255; |
Aneesh Kumar K.V | 5fe0c23 | 2011-01-29 18:43:25 +0530 | [diff] [blame] | 1790 | } |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1791 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 1792 | if (inode_unhashed(inode)) { |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1793 | /* Unfortunately insert_inode_hash is not idempotent, |
| 1794 | * so as we hash inodes here rather than at creation |
| 1795 | * time, we need a lock to ensure we only try |
| 1796 | * to do it once |
| 1797 | */ |
| 1798 | static DEFINE_SPINLOCK(lock); |
| 1799 | spin_lock(&lock); |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 1800 | if (inode_unhashed(inode)) |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1801 | __insert_inode_hash(inode, |
| 1802 | inode->i_ino + inode->i_generation); |
| 1803 | spin_unlock(&lock); |
| 1804 | } |
| 1805 | |
| 1806 | fh[0] = inode->i_generation; |
| 1807 | fh[1] = inode->i_ino; |
| 1808 | fh[2] = ((__u64)inode->i_ino) >> 32; |
| 1809 | |
| 1810 | *len = 3; |
| 1811 | return 1; |
| 1812 | } |
| 1813 | |
Christoph Hellwig | 3965516 | 2007-10-21 16:42:17 -0700 | [diff] [blame] | 1814 | static const struct export_operations shmem_export_ops = { |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1815 | .get_parent = shmem_get_parent, |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1816 | .encode_fh = shmem_encode_fh, |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 1817 | .fh_to_dentry = shmem_fh_to_dentry, |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 1818 | }; |
| 1819 | |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1820 | static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, |
| 1821 | bool remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | { |
| 1823 | char *this_char, *value, *rest; |
| 1824 | |
Hugh Dickins | b00dc3a | 2006-02-21 23:49:47 +0000 | [diff] [blame] | 1825 | while (options != NULL) { |
| 1826 | this_char = options; |
| 1827 | for (;;) { |
| 1828 | /* |
| 1829 | * NUL-terminate this option: unfortunately, |
| 1830 | * mount options form a comma-separated list, |
| 1831 | * but mpol's nodelist may also contain commas. |
| 1832 | */ |
| 1833 | options = strchr(options, ','); |
| 1834 | if (options == NULL) |
| 1835 | break; |
| 1836 | options++; |
| 1837 | if (!isdigit(*options)) { |
| 1838 | options[-1] = '\0'; |
| 1839 | break; |
| 1840 | } |
| 1841 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | if (!*this_char) |
| 1843 | continue; |
| 1844 | if ((value = strchr(this_char,'=')) != NULL) { |
| 1845 | *value++ = 0; |
| 1846 | } else { |
| 1847 | printk(KERN_ERR |
| 1848 | "tmpfs: No value for mount option '%s'\n", |
| 1849 | this_char); |
| 1850 | return 1; |
| 1851 | } |
| 1852 | |
| 1853 | if (!strcmp(this_char,"size")) { |
| 1854 | unsigned long long size; |
| 1855 | size = memparse(value,&rest); |
| 1856 | if (*rest == '%') { |
| 1857 | size <<= PAGE_SHIFT; |
| 1858 | size *= totalram_pages; |
| 1859 | do_div(size, 100); |
| 1860 | rest++; |
| 1861 | } |
| 1862 | if (*rest) |
| 1863 | goto bad_val; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1864 | sbinfo->max_blocks = |
| 1865 | DIV_ROUND_UP(size, PAGE_CACHE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | } else if (!strcmp(this_char,"nr_blocks")) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1867 | sbinfo->max_blocks = memparse(value, &rest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | if (*rest) |
| 1869 | goto bad_val; |
| 1870 | } else if (!strcmp(this_char,"nr_inodes")) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1871 | sbinfo->max_inodes = memparse(value, &rest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | if (*rest) |
| 1873 | goto bad_val; |
| 1874 | } else if (!strcmp(this_char,"mode")) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1875 | if (remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | continue; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1877 | sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | if (*rest) |
| 1879 | goto bad_val; |
| 1880 | } else if (!strcmp(this_char,"uid")) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1881 | if (remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | continue; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1883 | sbinfo->uid = simple_strtoul(value, &rest, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | if (*rest) |
| 1885 | goto bad_val; |
| 1886 | } else if (!strcmp(this_char,"gid")) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1887 | if (remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | continue; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1889 | sbinfo->gid = simple_strtoul(value, &rest, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | if (*rest) |
| 1891 | goto bad_val; |
Robin Holt | 7339ff8 | 2006-01-14 13:20:48 -0800 | [diff] [blame] | 1892 | } else if (!strcmp(this_char,"mpol")) { |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1893 | if (mpol_parse_str(value, &sbinfo->mpol, 1)) |
Robin Holt | 7339ff8 | 2006-01-14 13:20:48 -0800 | [diff] [blame] | 1894 | goto bad_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | } else { |
| 1896 | printk(KERN_ERR "tmpfs: Bad mount option %s\n", |
| 1897 | this_char); |
| 1898 | return 1; |
| 1899 | } |
| 1900 | } |
| 1901 | return 0; |
| 1902 | |
| 1903 | bad_val: |
| 1904 | printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", |
| 1905 | value, this_char); |
| 1906 | return 1; |
| 1907 | |
| 1908 | } |
| 1909 | |
| 1910 | static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) |
| 1911 | { |
| 1912 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1913 | struct shmem_sb_info config = *sbinfo; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1914 | unsigned long inodes; |
| 1915 | int error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1917 | if (shmem_parse_options(data, &config, true)) |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1918 | return error; |
| 1919 | |
| 1920 | spin_lock(&sbinfo->stat_lock); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1921 | inodes = sbinfo->max_inodes - sbinfo->free_inodes; |
Tim Chen | 7e49629 | 2010-08-09 17:19:05 -0700 | [diff] [blame] | 1922 | if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1923 | goto out; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1924 | if (config.max_inodes < inodes) |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1925 | goto out; |
| 1926 | /* |
| 1927 | * Those tests also disallow limited->unlimited while any are in |
| 1928 | * use, so i_blocks will always be zero when max_blocks is zero; |
| 1929 | * but we must separately disallow unlimited->limited, because |
| 1930 | * in that case we have no record of how much is already in use. |
| 1931 | */ |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1932 | if (config.max_blocks && !sbinfo->max_blocks) |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1933 | goto out; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1934 | if (config.max_inodes && !sbinfo->max_inodes) |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1935 | goto out; |
| 1936 | |
| 1937 | error = 0; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1938 | sbinfo->max_blocks = config.max_blocks; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1939 | sbinfo->max_inodes = config.max_inodes; |
| 1940 | sbinfo->free_inodes = config.max_inodes - inodes; |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1941 | |
| 1942 | mpol_put(sbinfo->mpol); |
| 1943 | sbinfo->mpol = config.mpol; /* transfers initial ref */ |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1944 | out: |
| 1945 | spin_unlock(&sbinfo->stat_lock); |
| 1946 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | } |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1948 | |
| 1949 | static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs) |
| 1950 | { |
| 1951 | struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb); |
| 1952 | |
| 1953 | if (sbinfo->max_blocks != shmem_default_max_blocks()) |
| 1954 | seq_printf(seq, ",size=%luk", |
| 1955 | sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); |
| 1956 | if (sbinfo->max_inodes != shmem_default_max_inodes()) |
| 1957 | seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); |
| 1958 | if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) |
| 1959 | seq_printf(seq, ",mode=%03o", sbinfo->mode); |
| 1960 | if (sbinfo->uid != 0) |
| 1961 | seq_printf(seq, ",uid=%u", sbinfo->uid); |
| 1962 | if (sbinfo->gid != 0) |
| 1963 | seq_printf(seq, ",gid=%u", sbinfo->gid); |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1964 | shmem_show_mpol(seq, sbinfo->mpol); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1965 | return 0; |
| 1966 | } |
| 1967 | #endif /* CONFIG_TMPFS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | |
| 1969 | static void shmem_put_super(struct super_block *sb) |
| 1970 | { |
Hugh Dickins | 602586a | 2010-08-17 15:23:56 -0700 | [diff] [blame] | 1971 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 1972 | |
| 1973 | percpu_counter_destroy(&sbinfo->used_blocks); |
| 1974 | kfree(sbinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | sb->s_fs_info = NULL; |
| 1976 | } |
| 1977 | |
Kay Sievers | 2b2af54 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 1978 | int shmem_fill_super(struct super_block *sb, void *data, int silent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | { |
| 1980 | struct inode *inode; |
| 1981 | struct dentry *root; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1982 | struct shmem_sb_info *sbinfo; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1983 | int err = -ENOMEM; |
| 1984 | |
| 1985 | /* Round up to L1_CACHE_BYTES to resist false sharing */ |
Pekka Enberg | 425fbf0 | 2009-09-21 17:03:50 -0700 | [diff] [blame] | 1986 | sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1987 | L1_CACHE_BYTES), GFP_KERNEL); |
| 1988 | if (!sbinfo) |
| 1989 | return -ENOMEM; |
| 1990 | |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1991 | sbinfo->mode = S_IRWXUGO | S_ISVTX; |
David Howells | 76aac0e | 2008-11-14 10:39:12 +1100 | [diff] [blame] | 1992 | sbinfo->uid = current_fsuid(); |
| 1993 | sbinfo->gid = current_fsgid(); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1994 | sb->s_fs_info = sbinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 1996 | #ifdef CONFIG_TMPFS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | /* |
| 1998 | * Per default we only allow half of the physical ram per |
| 1999 | * tmpfs instance, limiting inodes to one per page of lowmem; |
| 2000 | * but the internal instance is left unlimited. |
| 2001 | */ |
| 2002 | if (!(sb->s_flags & MS_NOUSER)) { |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 2003 | sbinfo->max_blocks = shmem_default_max_blocks(); |
| 2004 | sbinfo->max_inodes = shmem_default_max_inodes(); |
| 2005 | if (shmem_parse_options(data, sbinfo, false)) { |
| 2006 | err = -EINVAL; |
| 2007 | goto failed; |
| 2008 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | } |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 2010 | sb->s_export_op = &shmem_export_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | #else |
| 2012 | sb->s_flags |= MS_NOUSER; |
| 2013 | #endif |
| 2014 | |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 2015 | spin_lock_init(&sbinfo->stat_lock); |
Hugh Dickins | 602586a | 2010-08-17 15:23:56 -0700 | [diff] [blame] | 2016 | if (percpu_counter_init(&sbinfo->used_blocks, 0)) |
| 2017 | goto failed; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 2018 | sbinfo->free_inodes = sbinfo->max_inodes; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 2019 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 2020 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 2022 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 2023 | sb->s_magic = TMPFS_MAGIC; |
| 2024 | sb->s_op = &shmem_ops; |
Robin H. Johnson | cfd95a9 | 2006-06-12 21:50:25 +0100 | [diff] [blame] | 2025 | sb->s_time_gran = 1; |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 2026 | #ifdef CONFIG_TMPFS_XATTR |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 2027 | sb->s_xattr = shmem_xattr_handlers; |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 2028 | #endif |
| 2029 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 2030 | sb->s_flags |= MS_POSIXACL; |
| 2031 | #endif |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 2032 | |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 2033 | inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | if (!inode) |
| 2035 | goto failed; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 2036 | inode->i_uid = sbinfo->uid; |
| 2037 | inode->i_gid = sbinfo->gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | root = d_alloc_root(inode); |
| 2039 | if (!root) |
| 2040 | goto failed_iput; |
| 2041 | sb->s_root = root; |
| 2042 | return 0; |
| 2043 | |
| 2044 | failed_iput: |
| 2045 | iput(inode); |
| 2046 | failed: |
| 2047 | shmem_put_super(sb); |
| 2048 | return err; |
| 2049 | } |
| 2050 | |
Pekka Enberg | fcc234f | 2006-03-22 00:08:13 -0800 | [diff] [blame] | 2051 | static struct kmem_cache *shmem_inode_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | |
| 2053 | static struct inode *shmem_alloc_inode(struct super_block *sb) |
| 2054 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2055 | struct shmem_inode_info *info; |
| 2056 | info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); |
| 2057 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | return NULL; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2059 | return &info->vfs_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2062 | static void shmem_destroy_callback(struct rcu_head *head) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 2063 | { |
| 2064 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 2065 | INIT_LIST_HEAD(&inode->i_dentry); |
| 2066 | kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); |
| 2067 | } |
| 2068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | static void shmem_destroy_inode(struct inode *inode) |
| 2070 | { |
| 2071 | if ((inode->i_mode & S_IFMT) == S_IFREG) { |
| 2072 | /* only struct inode is valid if it's an inline symlink */ |
| 2073 | mpol_free_shared_policy(&SHMEM_I(inode)->policy); |
| 2074 | } |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2075 | call_rcu(&inode->i_rcu, shmem_destroy_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2078 | static void shmem_init_inode(void *foo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2080 | struct shmem_inode_info *info = foo; |
| 2081 | inode_init_once(&info->vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2084 | static int shmem_init_inodecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | { |
| 2086 | shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", |
| 2087 | sizeof(struct shmem_inode_info), |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2088 | 0, SLAB_PANIC, shmem_init_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | return 0; |
| 2090 | } |
| 2091 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2092 | static void shmem_destroy_inodecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | { |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 2094 | kmem_cache_destroy(shmem_inode_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 2097 | static const struct address_space_operations shmem_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | .writepage = shmem_writepage, |
Ken Chen | 7671932 | 2007-02-10 01:43:15 -0800 | [diff] [blame] | 2099 | .set_page_dirty = __set_page_dirty_no_writeback, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | #ifdef CONFIG_TMPFS |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2101 | .write_begin = shmem_write_begin, |
| 2102 | .write_end = shmem_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | #endif |
Lee Schermerhorn | 304dbdb | 2006-04-22 02:35:48 -0700 | [diff] [blame] | 2104 | .migratepage = migrate_page, |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 2105 | .error_remove_page = generic_error_remove_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | }; |
| 2107 | |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 2108 | static const struct file_operations shmem_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | .mmap = shmem_mmap, |
| 2110 | #ifdef CONFIG_TMPFS |
| 2111 | .llseek = generic_file_llseek, |
Hugh Dickins | bcd78e4 | 2008-07-23 21:27:35 -0700 | [diff] [blame] | 2112 | .read = do_sync_read, |
Hugh Dickins | 5402b97 | 2008-02-04 22:28:44 -0800 | [diff] [blame] | 2113 | .write = do_sync_write, |
Hugh Dickins | bcd78e4 | 2008-07-23 21:27:35 -0700 | [diff] [blame] | 2114 | .aio_read = shmem_file_aio_read, |
Hugh Dickins | 5402b97 | 2008-02-04 22:28:44 -0800 | [diff] [blame] | 2115 | .aio_write = generic_file_aio_write, |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 2116 | .fsync = noop_fsync, |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 2117 | .splice_read = shmem_file_splice_read, |
Hugh Dickins | ae976416 | 2007-06-04 10:00:39 +0200 | [diff] [blame] | 2118 | .splice_write = generic_file_splice_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | #endif |
| 2120 | }; |
| 2121 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 2122 | static const struct inode_operations shmem_inode_operations = { |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 2123 | .setattr = shmem_setattr, |
Badari Pulavarty | f6b3ec2 | 2006-01-06 00:10:38 -0800 | [diff] [blame] | 2124 | .truncate_range = shmem_truncate_range, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 2125 | #ifdef CONFIG_TMPFS_XATTR |
| 2126 | .setxattr = shmem_setxattr, |
| 2127 | .getxattr = shmem_getxattr, |
| 2128 | .listxattr = shmem_listxattr, |
| 2129 | .removexattr = shmem_removexattr, |
| 2130 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | }; |
| 2132 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 2133 | static const struct inode_operations shmem_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | #ifdef CONFIG_TMPFS |
| 2135 | .create = shmem_create, |
| 2136 | .lookup = simple_lookup, |
| 2137 | .link = shmem_link, |
| 2138 | .unlink = shmem_unlink, |
| 2139 | .symlink = shmem_symlink, |
| 2140 | .mkdir = shmem_mkdir, |
| 2141 | .rmdir = shmem_rmdir, |
| 2142 | .mknod = shmem_mknod, |
| 2143 | .rename = shmem_rename, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | #endif |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 2145 | #ifdef CONFIG_TMPFS_XATTR |
| 2146 | .setxattr = shmem_setxattr, |
| 2147 | .getxattr = shmem_getxattr, |
| 2148 | .listxattr = shmem_listxattr, |
| 2149 | .removexattr = shmem_removexattr, |
| 2150 | #endif |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 2151 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 2152 | .setattr = shmem_setattr, |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 2153 | #endif |
| 2154 | }; |
| 2155 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 2156 | static const struct inode_operations shmem_special_inode_operations = { |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 2157 | #ifdef CONFIG_TMPFS_XATTR |
| 2158 | .setxattr = shmem_setxattr, |
| 2159 | .getxattr = shmem_getxattr, |
| 2160 | .listxattr = shmem_listxattr, |
| 2161 | .removexattr = shmem_removexattr, |
| 2162 | #endif |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 2163 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 2164 | .setattr = shmem_setattr, |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 2165 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | }; |
| 2167 | |
Hugh Dickins | 759b977 | 2007-03-05 00:30:28 -0800 | [diff] [blame] | 2168 | static const struct super_operations shmem_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | .alloc_inode = shmem_alloc_inode, |
| 2170 | .destroy_inode = shmem_destroy_inode, |
| 2171 | #ifdef CONFIG_TMPFS |
| 2172 | .statfs = shmem_statfs, |
| 2173 | .remount_fs = shmem_remount_fs, |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 2174 | .show_options = shmem_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | #endif |
Al Viro | 1f895f7 | 2010-06-05 19:10:41 -0400 | [diff] [blame] | 2176 | .evict_inode = shmem_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | .drop_inode = generic_delete_inode, |
| 2178 | .put_super = shmem_put_super, |
| 2179 | }; |
| 2180 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 2181 | static const struct vm_operations_struct shmem_vm_ops = { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 2182 | .fault = shmem_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | #ifdef CONFIG_NUMA |
| 2184 | .set_policy = shmem_set_policy, |
| 2185 | .get_policy = shmem_get_policy, |
| 2186 | #endif |
| 2187 | }; |
| 2188 | |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 2189 | static struct dentry *shmem_mount(struct file_system_type *fs_type, |
| 2190 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | { |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 2192 | return mount_nodev(fs_type, flags, data, shmem_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2195 | static struct file_system_type shmem_fs_type = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | .owner = THIS_MODULE, |
| 2197 | .name = "tmpfs", |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 2198 | .mount = shmem_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | .kill_sb = kill_litter_super, |
| 2200 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2202 | int __init shmem_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | { |
| 2204 | int error; |
| 2205 | |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 2206 | error = bdi_init(&shmem_backing_dev_info); |
| 2207 | if (error) |
| 2208 | goto out4; |
| 2209 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2210 | error = shmem_init_inodecache(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | if (error) |
| 2212 | goto out3; |
| 2213 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2214 | error = register_filesystem(&shmem_fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | if (error) { |
| 2216 | printk(KERN_ERR "Could not register tmpfs\n"); |
| 2217 | goto out2; |
| 2218 | } |
Greg Kroah-Hartman | 95dc112 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2219 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2220 | shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER, |
| 2221 | shmem_fs_type.name, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | if (IS_ERR(shm_mnt)) { |
| 2223 | error = PTR_ERR(shm_mnt); |
| 2224 | printk(KERN_ERR "Could not kern_mount tmpfs\n"); |
| 2225 | goto out1; |
| 2226 | } |
| 2227 | return 0; |
| 2228 | |
| 2229 | out1: |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2230 | unregister_filesystem(&shmem_fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | out2: |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2232 | shmem_destroy_inodecache(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | out3: |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 2234 | bdi_destroy(&shmem_backing_dev_info); |
| 2235 | out4: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | shm_mnt = ERR_PTR(error); |
| 2237 | return error; |
| 2238 | } |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2239 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2240 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
| 2241 | /** |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2242 | * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2243 | * @inode: the inode to be searched |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2244 | * @index: the page offset to be searched |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2245 | * @pagep: the pointer for the found page to be stored |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2246 | * @swapp: the pointer for the found swap entry to be stored |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2247 | * |
| 2248 | * If a page is found, refcount of it is incremented. Callers should handle |
| 2249 | * these refcount. |
| 2250 | */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2251 | void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index, |
| 2252 | struct page **pagep, swp_entry_t *swapp) |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2253 | { |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2254 | struct shmem_inode_info *info = SHMEM_I(inode); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2255 | struct page *page = NULL; |
| 2256 | swp_entry_t swap = {0}; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2257 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2258 | if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2259 | goto out; |
| 2260 | |
| 2261 | spin_lock(&info->lock); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2262 | #ifdef CONFIG_SWAP |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2263 | swap = shmem_get_swap(info, index); |
| 2264 | if (swap.val) |
| 2265 | page = find_get_page(&swapper_space, swap.val); |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 2266 | else |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2267 | #endif |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2268 | page = find_get_page(inode->i_mapping, index); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2269 | spin_unlock(&info->lock); |
| 2270 | out: |
| 2271 | *pagep = page; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2272 | *swapp = swap; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2273 | } |
| 2274 | #endif |
| 2275 | |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2276 | #else /* !CONFIG_SHMEM */ |
| 2277 | |
| 2278 | /* |
| 2279 | * tiny-shmem: simple shmemfs and tmpfs using ramfs code |
| 2280 | * |
| 2281 | * This is intended for small system where the benefits of the full |
| 2282 | * shmem code (swap-backed and resource-limited) are outweighed by |
| 2283 | * their complexity. On systems without swap this code should be |
| 2284 | * effectively equivalent, but much lighter weight. |
| 2285 | */ |
| 2286 | |
| 2287 | #include <linux/ramfs.h> |
| 2288 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2289 | static struct file_system_type shmem_fs_type = { |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2290 | .name = "tmpfs", |
Al Viro | 3c26ff6 | 2010-07-25 11:46:36 +0400 | [diff] [blame] | 2291 | .mount = ramfs_mount, |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2292 | .kill_sb = kill_litter_super, |
| 2293 | }; |
| 2294 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2295 | int __init shmem_init(void) |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2296 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2297 | BUG_ON(register_filesystem(&shmem_fs_type) != 0); |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2298 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2299 | shm_mnt = kern_mount(&shmem_fs_type); |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2300 | BUG_ON(IS_ERR(shm_mnt)); |
| 2301 | |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2305 | int shmem_unuse(swp_entry_t swap, struct page *page) |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2306 | { |
| 2307 | return 0; |
| 2308 | } |
| 2309 | |
Hugh Dickins | 3f96b79 | 2009-09-21 17:03:37 -0700 | [diff] [blame] | 2310 | int shmem_lock(struct file *file, int lock, struct user_struct *user) |
| 2311 | { |
| 2312 | return 0; |
| 2313 | } |
| 2314 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2315 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 2316 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2317 | truncate_inode_pages_range(inode->i_mapping, lstart, lend); |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 2318 | } |
| 2319 | EXPORT_SYMBOL_GPL(shmem_truncate_range); |
| 2320 | |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2321 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
| 2322 | /** |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2323 | * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2324 | * @inode: the inode to be searched |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2325 | * @index: the page offset to be searched |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2326 | * @pagep: the pointer for the found page to be stored |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2327 | * @swapp: the pointer for the found swap entry to be stored |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2328 | * |
| 2329 | * If a page is found, refcount of it is incremented. Callers should handle |
| 2330 | * these refcount. |
| 2331 | */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2332 | void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index, |
| 2333 | struct page **pagep, swp_entry_t *swapp) |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2334 | { |
| 2335 | struct page *page = NULL; |
| 2336 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2337 | if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2338 | goto out; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2339 | page = find_get_page(inode->i_mapping, index); |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2340 | out: |
| 2341 | *pagep = page; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame^] | 2342 | *swapp = (swp_entry_t){0}; |
Daisuke Nishimura | 87946a7 | 2010-05-26 14:42:39 -0700 | [diff] [blame] | 2343 | } |
| 2344 | #endif |
| 2345 | |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 2346 | #define shmem_vm_ops generic_file_vm_ops |
| 2347 | #define shmem_file_operations ramfs_file_operations |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 2348 | #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 2349 | #define shmem_acct_size(flags, size) 0 |
| 2350 | #define shmem_unacct_size(flags, size) do {} while (0) |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2351 | |
| 2352 | #endif /* CONFIG_SHMEM */ |
| 2353 | |
| 2354 | /* common code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | |
Randy Dunlap | 4671181 | 2008-03-19 17:00:41 -0700 | [diff] [blame] | 2356 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | * shmem_file_setup - get an unlinked file living in tmpfs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
| 2359 | * @size: size to be set for the file |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 2360 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | */ |
Sergei Trofimovich | 168f5ac | 2009-06-16 15:33:02 -0700 | [diff] [blame] | 2362 | struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | { |
| 2364 | int error; |
| 2365 | struct file *file; |
| 2366 | struct inode *inode; |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 2367 | struct path path; |
| 2368 | struct dentry *root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | struct qstr this; |
| 2370 | |
| 2371 | if (IS_ERR(shm_mnt)) |
| 2372 | return (void *)shm_mnt; |
| 2373 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 2374 | if (size < 0 || size > MAX_LFS_FILESIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | return ERR_PTR(-EINVAL); |
| 2376 | |
| 2377 | if (shmem_acct_size(flags, size)) |
| 2378 | return ERR_PTR(-ENOMEM); |
| 2379 | |
| 2380 | error = -ENOMEM; |
| 2381 | this.name = name; |
| 2382 | this.len = strlen(name); |
| 2383 | this.hash = 0; /* will go */ |
| 2384 | root = shm_mnt->mnt_root; |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 2385 | path.dentry = d_alloc(root, &this); |
| 2386 | if (!path.dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | goto put_memory; |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 2388 | path.mnt = mntget(shm_mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | error = -ENOSPC; |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 2391 | inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | if (!inode) |
Al Viro | 4b42af8 | 2009-08-05 18:25:56 +0400 | [diff] [blame] | 2393 | goto put_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 2395 | d_instantiate(path.dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | inode->i_size = size; |
| 2397 | inode->i_nlink = 0; /* It is unlinked */ |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2398 | #ifndef CONFIG_MMU |
| 2399 | error = ramfs_nommu_expand_for_mapping(inode, size); |
| 2400 | if (error) |
Al Viro | 4b42af8 | 2009-08-05 18:25:56 +0400 | [diff] [blame] | 2401 | goto put_dentry; |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 2402 | #endif |
Al Viro | 4b42af8 | 2009-08-05 18:25:56 +0400 | [diff] [blame] | 2403 | |
| 2404 | error = -ENFILE; |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 2405 | file = alloc_file(&path, FMODE_WRITE | FMODE_READ, |
Al Viro | 4b42af8 | 2009-08-05 18:25:56 +0400 | [diff] [blame] | 2406 | &shmem_file_operations); |
| 2407 | if (!file) |
| 2408 | goto put_dentry; |
| 2409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | return file; |
| 2411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | put_dentry: |
Al Viro | 2c48b9c | 2009-08-09 00:52:35 +0400 | [diff] [blame] | 2413 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | put_memory: |
| 2415 | shmem_unacct_size(flags, size); |
| 2416 | return ERR_PTR(error); |
| 2417 | } |
Keith Packard | 395e0dd | 2008-06-20 00:08:06 -0700 | [diff] [blame] | 2418 | EXPORT_SYMBOL_GPL(shmem_file_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | |
Randy Dunlap | 4671181 | 2008-03-19 17:00:41 -0700 | [diff] [blame] | 2420 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | * shmem_zero_setup - setup a shared anonymous mapping |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | * @vma: the vma to be mmapped is prepared by do_mmap_pgoff |
| 2423 | */ |
| 2424 | int shmem_zero_setup(struct vm_area_struct *vma) |
| 2425 | { |
| 2426 | struct file *file; |
| 2427 | loff_t size = vma->vm_end - vma->vm_start; |
| 2428 | |
| 2429 | file = shmem_file_setup("dev/zero", size, vma->vm_flags); |
| 2430 | if (IS_ERR(file)) |
| 2431 | return PTR_ERR(file); |
| 2432 | |
| 2433 | if (vma->vm_file) |
| 2434 | fput(vma->vm_file); |
| 2435 | vma->vm_file = file; |
| 2436 | vma->vm_ops = &shmem_vm_ops; |
Hugh Dickins | bee4c36a | 2011-03-22 16:33:43 -0700 | [diff] [blame] | 2437 | vma->vm_flags |= VM_CAN_NONLINEAR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | return 0; |
| 2439 | } |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 2440 | |
| 2441 | /** |
| 2442 | * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. |
| 2443 | * @mapping: the page's address_space |
| 2444 | * @index: the page index |
| 2445 | * @gfp: the page allocator flags to use if allocating |
| 2446 | * |
| 2447 | * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", |
| 2448 | * with any new page allocations done using the specified allocation flags. |
| 2449 | * But read_cache_page_gfp() uses the ->readpage() method: which does not |
| 2450 | * suit tmpfs, since it may have pages in swapcache, and needs to find those |
| 2451 | * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. |
| 2452 | * |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 2453 | * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in |
| 2454 | * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 2455 | */ |
| 2456 | struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, |
| 2457 | pgoff_t index, gfp_t gfp) |
| 2458 | { |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 2459 | #ifdef CONFIG_SHMEM |
| 2460 | struct inode *inode = mapping->host; |
Hugh Dickins | 9276aad | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 2461 | struct page *page; |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 2462 | int error; |
| 2463 | |
| 2464 | BUG_ON(mapping->a_ops != &shmem_aops); |
| 2465 | error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL); |
| 2466 | if (error) |
| 2467 | page = ERR_PTR(error); |
| 2468 | else |
| 2469 | unlock_page(page); |
| 2470 | return page; |
| 2471 | #else |
| 2472 | /* |
| 2473 | * The tiny !SHMEM case uses ramfs without swap |
| 2474 | */ |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 2475 | return read_cache_page_gfp(mapping, index, gfp); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 2476 | #endif |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 2477 | } |
| 2478 | EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); |