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