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