blob: 469f90d56051984c674f395042029ea737608d5b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Dickins6922c0c2011-08-03 16:21:25 -07009 * Copyright (C) 2002-2011 Hugh Dickins.
10 * Copyright (C) 2011 Google Inc.
Hugh Dickins0edd73b2005-06-21 17:15:04 -070011 * Copyright (C) 2002-2005 VERITAS Software Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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 Mackall853ac432009-01-06 14:40:20 -080018 * tiny-shmem:
19 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
20 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * This file is released under the GPL.
22 */
23
Matt Mackall853ac432009-01-06 14:40:20 -080024#include <linux/fs.h>
25#include <linux/init.h>
26#include <linux/vfs.h>
27#include <linux/mount.h>
Andrew Morton250297e2013-04-29 15:06:12 -070028#include <linux/ramfs.h>
Hugh Dickinscaefba12009-04-13 14:40:12 -070029#include <linux/pagemap.h>
Matt Mackall853ac432009-01-06 14:40:20 -080030#include <linux/file.h>
31#include <linux/mm.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040032#include <linux/export.h>
Matt Mackall853ac432009-01-06 14:40:20 -080033#include <linux/swap.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070034#include <linux/aio.h>
Matt Mackall853ac432009-01-06 14:40:20 -080035
36static struct vfsmount *shm_mnt;
37
38#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * This virtual memory filesystem is heavily based on the ramfs. It
41 * extends ramfs by the ability to use swap and honor resource limits
42 * which makes it a completely usable filesystem.
43 */
44
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070045#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070046#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010047#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080048#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/string.h>
51#include <linux/slab.h>
52#include <linux/backing-dev.h>
53#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070056#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070057#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070058#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070059#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/security.h>
61#include <linux/swapops.h>
62#include <linux/mempolicy.h>
63#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000064#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070065#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070066#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080067#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040068#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070069#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070070#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070071#include <uapi/linux/memfd.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <asm/pgtable.h>
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Pretend that each entry is of this size in directory's i_size */
80#define BOGO_DIRENT_SIZE 20
81
Hugh Dickins69f07ec2011-08-03 16:21:26 -070082/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
83#define SHORT_SYMLINK_LEN 128
84
Hugh Dickins1aac1402012-05-29 15:06:42 -070085/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -070086 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
87 * inode->i_private (with i_mutex making sure that it has only one user at
88 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -070089 */
90struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -070091 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -070092 pgoff_t start; /* start of range currently being fallocated */
93 pgoff_t next; /* the next page offset to be fallocated */
94 pgoff_t nr_falloced; /* how many new pages have been fallocated */
95 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
96};
97
Hugh Dickins285b2c42011-08-03 16:21:20 -070098/* Flag allocation requirements to shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099enum sgp_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 SGP_READ, /* don't exceed i_size, don't allocate page */
101 SGP_CACHE, /* don't exceed i_size, may allocate page */
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -0800102 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700103 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
104 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Andrew Mortonb76db732008-02-08 04:21:49 -0800107#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800108static unsigned long shmem_default_max_blocks(void)
109{
110 return totalram_pages / 2;
111}
112
113static unsigned long shmem_default_max_inodes(void)
114{
115 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
116}
Andrew Mortonb76db732008-02-08 04:21:49 -0800117#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800118
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700119static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
120static int shmem_replace_page(struct page **pagep, gfp_t gfp,
121 struct shmem_inode_info *info, pgoff_t index);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700122static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
123 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
124
125static inline int shmem_getpage(struct inode *inode, pgoff_t index,
126 struct page **pagep, enum sgp_type sgp, int *fault_type)
127{
128 return shmem_getpage_gfp(inode, index, pagep, sgp,
129 mapping_gfp_mask(inode->i_mapping), fault_type);
130}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
133{
134 return sb->s_fs_info;
135}
136
137/*
138 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
139 * for shared memory and for shared anonymous (/dev/zero) mappings
140 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
141 * consistent with the pre-accounting of private mappings ...
142 */
143static inline int shmem_acct_size(unsigned long flags, loff_t size)
144{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000145 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000146 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149static inline void shmem_unacct_size(unsigned long flags, loff_t size)
150{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000151 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 vm_unacct_memory(VM_ACCT(size));
153}
154
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700155static inline int shmem_reacct_size(unsigned long flags,
156 loff_t oldsize, loff_t newsize)
157{
158 if (!(flags & VM_NORESERVE)) {
159 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
160 return security_vm_enough_memory_mm(current->mm,
161 VM_ACCT(newsize) - VM_ACCT(oldsize));
162 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
163 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
164 }
165 return 0;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*
169 * ... whereas tmpfs objects are accounted incrementally as
170 * pages are allocated, in order to allow huge sparse files.
171 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
172 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
173 */
174static inline int shmem_acct_block(unsigned long flags)
175{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000176 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000177 security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static inline void shmem_unacct_blocks(unsigned long flags, long pages)
181{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000182 if (flags & VM_NORESERVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
184}
185
Hugh Dickins759b9772007-03-05 00:30:28 -0800186static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700187static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800188static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800189static const struct inode_operations shmem_inode_operations;
190static const struct inode_operations shmem_dir_inode_operations;
191static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400192static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700194static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .ra_pages = 0, /* No readahead */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700196 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
199static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800200static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800202static int shmem_reserve_inode(struct super_block *sb)
203{
204 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
205 if (sbinfo->max_inodes) {
206 spin_lock(&sbinfo->stat_lock);
207 if (!sbinfo->free_inodes) {
208 spin_unlock(&sbinfo->stat_lock);
209 return -ENOSPC;
210 }
211 sbinfo->free_inodes--;
212 spin_unlock(&sbinfo->stat_lock);
213 }
214 return 0;
215}
216
217static void shmem_free_inode(struct super_block *sb)
218{
219 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
220 if (sbinfo->max_inodes) {
221 spin_lock(&sbinfo->stat_lock);
222 sbinfo->free_inodes++;
223 spin_unlock(&sbinfo->stat_lock);
224 }
225}
226
Randy Dunlap46711812008-03-19 17:00:41 -0700227/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700228 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * @inode: inode to recalc
230 *
231 * We have to calculate the free blocks since the mm can drop
232 * undirtied hole pages behind our back.
233 *
234 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
235 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
236 *
237 * It has to be called with the spinlock held.
238 */
239static void shmem_recalc_inode(struct inode *inode)
240{
241 struct shmem_inode_info *info = SHMEM_I(inode);
242 long freed;
243
244 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
245 if (freed > 0) {
Hugh Dickins54af60422011-08-03 16:21:24 -0700246 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
247 if (sbinfo->max_blocks)
248 percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700250 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 shmem_unacct_blocks(info->flags, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253}
254
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700255/*
256 * Replace item expected in radix tree by a new item, while holding tree lock.
257 */
258static int shmem_radix_tree_replace(struct address_space *mapping,
259 pgoff_t index, void *expected, void *replacement)
260{
261 void **pslot;
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700262 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700263
264 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700265 VM_BUG_ON(!replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700266 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700267 if (!pslot)
268 return -ENOENT;
269 item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700270 if (item != expected)
271 return -ENOENT;
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700272 radix_tree_replace_slot(pslot, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700273 return 0;
274}
275
276/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700277 * Sometimes, before we decide whether to proceed or to fail, we must check
278 * that an entry was not already brought back from swap by a racing thread.
279 *
280 * Checking page is not enough: by the time a SwapCache page is locked, it
281 * might be reused, and again be SwapCache, using the same swap as before.
282 */
283static bool shmem_confirm_swap(struct address_space *mapping,
284 pgoff_t index, swp_entry_t swap)
285{
286 void *item;
287
288 rcu_read_lock();
289 item = radix_tree_lookup(&mapping->page_tree, index);
290 rcu_read_unlock();
291 return item == swp_to_radix_entry(swap);
292}
293
294/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700295 * Like add_to_page_cache_locked, but error if expected item has gone.
296 */
297static int shmem_add_to_page_cache(struct page *page,
298 struct address_space *mapping,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700299 pgoff_t index, void *expected)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700300{
Hugh Dickinsb065b432012-07-11 14:02:48 -0700301 int error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700302
Sasha Levin309381fea2014-01-23 15:52:54 -0800303 VM_BUG_ON_PAGE(!PageLocked(page), page);
304 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700305
Hugh Dickinsb065b432012-07-11 14:02:48 -0700306 page_cache_get(page);
307 page->mapping = mapping;
308 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700309
Hugh Dickinsb065b432012-07-11 14:02:48 -0700310 spin_lock_irq(&mapping->tree_lock);
311 if (!expected)
312 error = radix_tree_insert(&mapping->page_tree, index, page);
313 else
314 error = shmem_radix_tree_replace(mapping, index, expected,
315 page);
316 if (!error) {
317 mapping->nrpages++;
318 __inc_zone_page_state(page, NR_FILE_PAGES);
319 __inc_zone_page_state(page, NR_SHMEM);
320 spin_unlock_irq(&mapping->tree_lock);
321 } else {
322 page->mapping = NULL;
323 spin_unlock_irq(&mapping->tree_lock);
324 page_cache_release(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700325 }
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700326 return error;
327}
328
329/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700330 * Like delete_from_page_cache, but substitutes swap for page.
331 */
332static void shmem_delete_from_page_cache(struct page *page, void *radswap)
333{
334 struct address_space *mapping = page->mapping;
335 int error;
336
337 spin_lock_irq(&mapping->tree_lock);
338 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
339 page->mapping = NULL;
340 mapping->nrpages--;
341 __dec_zone_page_state(page, NR_FILE_PAGES);
342 __dec_zone_page_state(page, NR_SHMEM);
343 spin_unlock_irq(&mapping->tree_lock);
344 page_cache_release(page);
345 BUG_ON(error);
346}
347
348/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700349 * Remove swap entry from radix tree, free the swap and its page cache.
350 */
351static int shmem_free_swap(struct address_space *mapping,
352 pgoff_t index, void *radswap)
353{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700354 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700355
356 spin_lock_irq(&mapping->tree_lock);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700357 old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700358 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700359 if (old != radswap)
360 return -ENOENT;
361 free_swap_and_cache(radix_to_swp_entry(radswap));
362 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700363}
364
365/*
Hugh Dickins24513262012-01-20 14:34:21 -0800366 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
367 */
368void shmem_unlock_mapping(struct address_space *mapping)
369{
370 struct pagevec pvec;
371 pgoff_t indices[PAGEVEC_SIZE];
372 pgoff_t index = 0;
373
374 pagevec_init(&pvec, 0);
375 /*
376 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
377 */
378 while (!mapping_unevictable(mapping)) {
379 /*
380 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
381 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
382 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700383 pvec.nr = find_get_entries(mapping, index,
384 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800385 if (!pvec.nr)
386 break;
387 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700388 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800389 check_move_unevictable_pages(pvec.pages, pvec.nr);
390 pagevec_release(&pvec);
391 cond_resched();
392 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700393}
394
395/*
396 * Remove range of pages and swap entries from radix tree, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700397 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700398 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700399static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
400 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700402 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700404 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700405 pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
406 unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
407 unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700408 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700409 pgoff_t indices[PAGEVEC_SIZE];
410 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700411 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700412 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700414 if (lend == -1)
415 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700416
417 pagevec_init(&pvec, 0);
418 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700419 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700420 pvec.nr = find_get_entries(mapping, index,
421 min(end - index, (pgoff_t)PAGEVEC_SIZE),
422 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700423 if (!pvec.nr)
424 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700425 for (i = 0; i < pagevec_count(&pvec); i++) {
426 struct page *page = pvec.pages[i];
427
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700428 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700429 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700430 break;
431
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700432 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700433 if (unfalloc)
434 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700435 nr_swaps_freed += !shmem_free_swap(mapping,
436 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700437 continue;
438 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700439
440 if (!trylock_page(page))
441 continue;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700442 if (!unfalloc || !PageUptodate(page)) {
443 if (page->mapping == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800444 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700445 truncate_inode_page(mapping, page);
446 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700447 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700448 unlock_page(page);
449 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700450 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800451 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700452 cond_resched();
453 index++;
454 }
455
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700456 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700457 struct page *page = NULL;
458 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
459 if (page) {
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700460 unsigned int top = PAGE_CACHE_SIZE;
461 if (start > end) {
462 top = partial_end;
463 partial_end = 0;
464 }
465 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700466 set_page_dirty(page);
467 unlock_page(page);
468 page_cache_release(page);
469 }
470 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700471 if (partial_end) {
472 struct page *page = NULL;
473 shmem_getpage(inode, end, &page, SGP_READ, NULL);
474 if (page) {
475 zero_user_segment(page, 0, partial_end);
476 set_page_dirty(page);
477 unlock_page(page);
478 page_cache_release(page);
479 }
480 }
481 if (start >= end)
482 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700483
484 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700485 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700486 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700487
488 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700489 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700490 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700491 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700492 /* If all gone or hole-punch or unfalloc, we're done */
493 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700494 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700495 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700496 index = start;
497 continue;
498 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700499 for (i = 0; i < pagevec_count(&pvec); i++) {
500 struct page *page = pvec.pages[i];
501
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700502 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700503 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700504 break;
505
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700506 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700507 if (unfalloc)
508 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700509 if (shmem_free_swap(mapping, index, page)) {
510 /* Swap was replaced by page: retry */
511 index--;
512 break;
513 }
514 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700515 continue;
516 }
517
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700518 lock_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700519 if (!unfalloc || !PageUptodate(page)) {
520 if (page->mapping == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800521 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700522 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700523 } else {
524 /* Page was replaced by swap: retry */
525 unlock_page(page);
526 index--;
527 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700528 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700529 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700530 unlock_page(page);
531 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700532 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800533 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700534 index++;
535 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700538 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 shmem_recalc_inode(inode);
540 spin_unlock(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700541}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700543void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
544{
545 shmem_undo_range(inode, lstart, lend, false);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700546 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700548EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Hugh Dickins94c1e622011-06-27 16:18:03 -0700550static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct inode *inode = dentry->d_inode;
David Herrmann40e041a2014-08-08 14:25:27 -0700553 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int error;
555
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200556 error = inode_change_ok(inode, attr);
557 if (error)
558 return error;
559
Hugh Dickins94c1e622011-06-27 16:18:03 -0700560 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
561 loff_t oldsize = inode->i_size;
562 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000563
David Herrmann40e041a2014-08-08 14:25:27 -0700564 /* protected by i_mutex */
565 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
566 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
567 return -EPERM;
568
Hugh Dickins94c1e622011-06-27 16:18:03 -0700569 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700570 error = shmem_reacct_size(SHMEM_I(inode)->flags,
571 oldsize, newsize);
572 if (error)
573 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -0700574 i_size_write(inode, newsize);
575 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
576 }
577 if (newsize < oldsize) {
578 loff_t holebegin = round_up(newsize, PAGE_SIZE);
579 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
580 shmem_truncate_range(inode, newsize, (loff_t)-1);
581 /* unmap again to remove racily COWed private pages */
582 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200586 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200587 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -0800588 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return error;
590}
591
Al Viro1f895f72010-06-05 19:10:41 -0400592static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct shmem_inode_info *info = SHMEM_I(inode);
595
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000596 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 shmem_unacct_size(info->flags, inode->i_size);
598 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000599 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800601 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800603 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Hugh Dickins69f07ec2011-08-03 16:21:26 -0700605 } else
606 kfree(info->symlink);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700607
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400608 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -0800609 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800610 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +0200611 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700614/*
615 * If swap found in inode, free it and move page from swapcache to filecache.
616 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700617static int shmem_unuse_inode(struct shmem_inode_info *info,
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700618 swp_entry_t swap, struct page **pagep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700620 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700621 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700622 pgoff_t index;
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700623 gfp_t gfp;
624 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700626 radswap = swp_to_radix_entry(swap);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700627 index = radix_tree_locate_item(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700628 if (index == -1)
Johannes Weiner00501b52014-08-08 14:19:20 -0700629 return -EAGAIN; /* tell shmem_unuse we found nothing */
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800630
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800631 /*
632 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400633 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800634 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700635 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800636 */
637 if (shmem_swaplist.next != &info->swaplist)
638 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800639
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700640 gfp = mapping_gfp_mask(mapping);
641 if (shmem_should_replace_page(*pagep, gfp)) {
642 mutex_unlock(&shmem_swaplist_mutex);
643 error = shmem_replace_page(pagep, gfp, info, index);
644 mutex_lock(&shmem_swaplist_mutex);
645 /*
646 * We needed to drop mutex to make that restrictive page
Hugh Dickins0142ef62012-06-07 14:21:09 -0700647 * allocation, but the inode might have been freed while we
648 * dropped it: although a racing shmem_evict_inode() cannot
649 * complete without emptying the radix_tree, our page lock
650 * on this swapcache page is not enough to prevent that -
651 * free_swap_and_cache() of our swap entry will only
652 * trylock_page(), removing swap from radix_tree whatever.
653 *
654 * We must not proceed to shmem_add_to_page_cache() if the
655 * inode has been freed, but of course we cannot rely on
656 * inode or mapping or info to check that. However, we can
657 * safely check if our swap entry is still in use (and here
658 * it can't have got reused for another page): if it's still
659 * in use, then the inode cannot have been freed yet, and we
660 * can safely proceed (if it's no longer in use, that tells
661 * nothing about the inode, but we don't need to unuse swap).
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700662 */
663 if (!page_swapcount(*pagep))
664 error = -ENOENT;
665 }
666
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800667 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700668 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
669 * but also to hold up shmem_evict_inode(): so inode cannot be freed
670 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800671 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700672 if (!error)
673 error = shmem_add_to_page_cache(*pagep, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700674 radswap);
Hugh Dickins48f170f2011-07-25 17:12:37 -0700675 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700676 /*
677 * Truncation and eviction use free_swap_and_cache(), which
678 * only does trylock page: if we raced, best clean up here.
679 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700680 delete_from_swap_cache(*pagep);
681 set_page_dirty(*pagep);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700682 if (!error) {
683 spin_lock(&info->lock);
684 info->swapped--;
685 spin_unlock(&info->lock);
686 swap_free(swap);
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800689 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700693 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700695int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700697 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct shmem_inode_info *info;
Johannes Weiner00501b52014-08-08 14:19:20 -0700699 struct mem_cgroup *memcg;
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700700 int error = 0;
701
702 /*
703 * There's a faint possibility that swap page was replaced before
Hugh Dickins0142ef62012-06-07 14:21:09 -0700704 * caller locked it: caller will come back later with the right page.
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700705 */
Hugh Dickins0142ef62012-06-07 14:21:09 -0700706 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700707 goto out;
Hugh Dickins778dd892011-05-11 15:13:37 -0700708
709 /*
710 * Charge page using GFP_KERNEL while we can wait, before taking
711 * the shmem_swaplist_mutex which might hold up shmem_writepage().
712 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -0700713 */
Johannes Weiner00501b52014-08-08 14:19:20 -0700714 error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg);
Hugh Dickins778dd892011-05-11 15:13:37 -0700715 if (error)
716 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700717 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Johannes Weiner00501b52014-08-08 14:19:20 -0700718 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800720 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700721 list_for_each_safe(this, next, &shmem_swaplist) {
722 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700723 if (info->swapped)
Johannes Weiner00501b52014-08-08 14:19:20 -0700724 error = shmem_unuse_inode(info, swap, &page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700725 else
726 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800727 cond_resched();
Johannes Weiner00501b52014-08-08 14:19:20 -0700728 if (error != -EAGAIN)
Hugh Dickins778dd892011-05-11 15:13:37 -0700729 break;
Johannes Weiner00501b52014-08-08 14:19:20 -0700730 /* found nothing in this: move on to search the next */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800732 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700733
Johannes Weiner00501b52014-08-08 14:19:20 -0700734 if (error) {
735 if (error != -ENOMEM)
736 error = 0;
737 mem_cgroup_cancel_charge(page, memcg);
738 } else
739 mem_cgroup_commit_charge(page, memcg, true);
Hugh Dickins778dd892011-05-11 15:13:37 -0700740out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800741 unlock_page(page);
742 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700743 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746/*
747 * Move the page from the page cache to the swap cache.
748 */
749static int shmem_writepage(struct page *page, struct writeback_control *wbc)
750{
751 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700754 swp_entry_t swap;
755 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 mapping = page->mapping;
759 index = page->index;
760 inode = mapping->host;
761 info = SHMEM_I(inode);
762 if (info->flags & VM_LOCKED)
763 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800764 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto redirty;
766
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800767 /*
768 * shmem_backing_dev_info's capabilities prevent regular writeback or
769 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700770 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800771 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700772 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800773 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700774 if (!wbc->for_reclaim) {
775 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
776 goto redirty;
777 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700778
779 /*
780 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
781 * value into swapfile.c, the only way we can correctly account for a
782 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -0700783 *
784 * That's okay for a page already fallocated earlier, but if we have
785 * not yet completed the fallocation, then (a) we want to keep track
786 * of this page in case we have to undo it, and (b) it may not be a
787 * good idea to continue anyway, once we're pushing into swap. So
788 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700789 */
790 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -0700791 if (inode->i_private) {
792 struct shmem_falloc *shmem_falloc;
793 spin_lock(&inode->i_lock);
794 shmem_falloc = inode->i_private;
795 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -0700796 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -0700797 index >= shmem_falloc->start &&
798 index < shmem_falloc->next)
799 shmem_falloc->nr_unswapped++;
800 else
801 shmem_falloc = NULL;
802 spin_unlock(&inode->i_lock);
803 if (shmem_falloc)
804 goto redirty;
805 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700806 clear_highpage(page);
807 flush_dcache_page(page);
808 SetPageUptodate(page);
809 }
810
Hugh Dickins48f170f2011-07-25 17:12:37 -0700811 swap = get_swap_page();
812 if (!swap.val)
813 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800814
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700815 /*
816 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700817 * if it's not already there. Do it now before the page is
818 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700819 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700820 * we've incremented swapped, because shmem_unuse_inode() will
821 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700822 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700823 mutex_lock(&shmem_swaplist_mutex);
824 if (list_empty(&info->swaplist))
825 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700826
Hugh Dickins48f170f2011-07-25 17:12:37 -0700827 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800828 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700829 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
830
831 spin_lock(&info->lock);
832 info->swapped++;
833 shmem_recalc_inode(inode);
Hugh Dickins826267c2011-05-28 13:14:09 -0700834 spin_unlock(&info->lock);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700835
836 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800837 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700838 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 0;
840 }
841
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700842 mutex_unlock(&shmem_swaplist_mutex);
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700843 swapcache_free(swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844redirty:
845 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800846 if (wbc->for_reclaim)
847 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
848 unlock_page(page);
849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800853#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700854static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800855{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700856 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800857
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700858 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700859 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800860
Hugh Dickinsa7a88b22013-01-02 02:04:23 -0800861 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800862
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700863 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800864}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700865
866static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
867{
868 struct mempolicy *mpol = NULL;
869 if (sbinfo->mpol) {
870 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
871 mpol = sbinfo->mpol;
872 mpol_get(mpol);
873 spin_unlock(&sbinfo->stat_lock);
874 }
875 return mpol;
876}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800877#endif /* CONFIG_TMPFS */
878
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700879static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
880 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -0800883 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800885 /* Create a pseudo vma that just contains the policy */
886 pvma.vm_start = 0;
Nathan Zimmer09c231c2012-07-31 16:46:17 -0700887 /* Bias interleave by inode number to distribute better across nodes */
888 pvma.vm_pgoff = index + info->vfs_inode.i_ino;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800889 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700890 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700891
Mel Gorman18a2f372012-12-05 14:01:41 -0800892 page = swapin_readahead(swap, gfp, &pvma, 0);
893
894 /* Drop reference taken by mpol_shared_policy_lookup() */
895 mpol_cond_put(pvma.vm_policy);
896
897 return page;
898}
899
900static struct page *shmem_alloc_page(gfp_t gfp,
901 struct shmem_inode_info *info, pgoff_t index)
902{
903 struct vm_area_struct pvma;
904 struct page *page;
905
906 /* Create a pseudo vma that just contains the policy */
907 pvma.vm_start = 0;
908 /* Bias interleave by inode number to distribute better across nodes */
909 pvma.vm_pgoff = index + info->vfs_inode.i_ino;
910 pvma.vm_ops = NULL;
911 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
912
913 page = alloc_page_vma(gfp, &pvma, 0);
914
915 /* Drop reference taken by mpol_shared_policy_lookup() */
916 mpol_cond_put(pvma.vm_policy);
917
918 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800920#else /* !CONFIG_NUMA */
921#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700922static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800923{
924}
925#endif /* CONFIG_TMPFS */
926
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700927static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
928 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700930 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Hugh Dickins02098fe2008-02-04 22:28:42 -0800933static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700934 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000936 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800938#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700940#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
941static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
942{
943 return NULL;
944}
945#endif
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700948 * When a page is moved from swapcache to shmem filecache (either by the
949 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
950 * shmem_unuse_inode()), it may have been read in earlier from swap, in
951 * ignorance of the mapping it belongs to. If that mapping has special
952 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
953 * we may need to copy to a suitable page before moving to filecache.
954 *
955 * In a future release, this may well be extended to respect cpuset and
956 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
957 * but for now it is a simple matter of zone.
958 */
959static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
960{
961 return page_zonenum(page) > gfp_zone(gfp);
962}
963
964static int shmem_replace_page(struct page **pagep, gfp_t gfp,
965 struct shmem_inode_info *info, pgoff_t index)
966{
967 struct page *oldpage, *newpage;
968 struct address_space *swap_mapping;
969 pgoff_t swap_index;
970 int error;
971
972 oldpage = *pagep;
973 swap_index = page_private(oldpage);
974 swap_mapping = page_mapping(oldpage);
975
976 /*
977 * We have arrived here because our zones are constrained, so don't
978 * limit chance of success by further cpuset and node constraints.
979 */
980 gfp &= ~GFP_CONSTRAINT_MASK;
981 newpage = shmem_alloc_page(gfp, info, index);
982 if (!newpage)
983 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700984
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700985 page_cache_get(newpage);
986 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -0700987 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700988
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700989 __set_page_locked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700990 SetPageUptodate(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700991 SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700992 set_page_private(newpage, swap_index);
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700993 SetPageSwapCache(newpage);
994
995 /*
996 * Our caller will very soon move newpage out of swapcache, but it's
997 * a nice clean interface for us to replace oldpage by newpage there.
998 */
999 spin_lock_irq(&swap_mapping->tree_lock);
1000 error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1001 newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001002 if (!error) {
1003 __inc_zone_page_state(newpage, NR_FILE_PAGES);
1004 __dec_zone_page_state(oldpage, NR_FILE_PAGES);
1005 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001006 spin_unlock_irq(&swap_mapping->tree_lock);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001007
Hugh Dickins0142ef62012-06-07 14:21:09 -07001008 if (unlikely(error)) {
1009 /*
1010 * Is this possible? I think not, now that our callers check
1011 * both PageSwapCache and page_private after getting page lock;
1012 * but be defensive. Reverse old to newpage for clear and free.
1013 */
1014 oldpage = newpage;
1015 } else {
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001016 mem_cgroup_migrate(oldpage, newpage, false);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001017 lru_cache_add_anon(newpage);
1018 *pagep = newpage;
1019 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001020
1021 ClearPageSwapCache(oldpage);
1022 set_page_private(oldpage, 0);
1023
1024 unlock_page(oldpage);
1025 page_cache_release(oldpage);
1026 page_cache_release(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001027 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001028}
1029
1030/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001031 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 *
1033 * If we allocate a new one we do not mark it dirty. That's up to the
1034 * vm. If we swap it in we mark it dirty since we also free the swap
1035 * entry since a page cannot live in both the swap and page cache
1036 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001037static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -07001038 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af60422011-08-03 16:21:24 -07001041 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct shmem_sb_info *sbinfo;
Johannes Weiner00501b52014-08-08 14:19:20 -07001043 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001044 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 swp_entry_t swap;
1046 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001047 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001048 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001050 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -07001053 swap.val = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001054 page = find_lock_entry(mapping, index);
Hugh Dickins54af60422011-08-03 16:21:24 -07001055 if (radix_tree_exceptional_entry(page)) {
1056 swap = radix_to_swp_entry(page);
1057 page = NULL;
1058 }
1059
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001060 if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
Hugh Dickins54af60422011-08-03 16:21:24 -07001061 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1062 error = -EINVAL;
1063 goto failed;
1064 }
1065
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001066 if (page && sgp == SGP_WRITE)
1067 mark_page_accessed(page);
1068
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001069 /* fallocated page? */
1070 if (page && !PageUptodate(page)) {
1071 if (sgp != SGP_READ)
1072 goto clear;
1073 unlock_page(page);
1074 page_cache_release(page);
1075 page = NULL;
1076 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001077 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001078 *pagep = page;
1079 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001083 * Fast cache lookup did not find it:
1084 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001086 info = SHMEM_I(inode);
1087 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (swap.val) {
1090 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -07001091 page = lookup_swap_cache(swap);
1092 if (!page) {
Ying Han456f9982011-05-26 16:25:38 -07001093 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -07001094 if (fault_type)
1095 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001096 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001097 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001098 error = -ENOMEM;
1099 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
1103 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -07001104 lock_page(page);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001105 if (!PageSwapCache(page) || page_private(page) != swap.val ||
Hugh Dickinsd1899222012-07-11 14:02:47 -07001106 !shmem_confirm_swap(mapping, index, swap)) {
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001107 error = -EEXIST; /* try again */
Hugh Dickinsd1899222012-07-11 14:02:47 -07001108 goto unlock;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001109 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001110 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -07001112 goto failed;
1113 }
1114 wait_on_page_writeback(page);
1115
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001116 if (shmem_should_replace_page(page, gfp)) {
1117 error = shmem_replace_page(&page, gfp, info, index);
1118 if (error)
1119 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
Johannes Weiner00501b52014-08-08 14:19:20 -07001122 error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg);
Hugh Dickinsd1899222012-07-11 14:02:47 -07001123 if (!error) {
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07001124 error = shmem_add_to_page_cache(page, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001125 swp_to_radix_entry(swap));
Hugh Dickins215c02b2012-11-16 14:15:03 -08001126 /*
1127 * We already confirmed swap under page lock, and make
1128 * no memory allocation here, so usually no possibility
1129 * of error; but free_swap_and_cache() only trylocks a
1130 * page, so it is just possible that the entry has been
1131 * truncated or holepunched since swap was confirmed.
1132 * shmem_undo_range() will have done some of the
1133 * unaccounting, now delete_from_swap_cache() will do
1134 * the rest (including mem_cgroup_uncharge_swapcache).
1135 * Reset swap.val? No, leave it so "failed" goes back to
1136 * "repeat": reading a hole and writing should succeed.
1137 */
Johannes Weiner00501b52014-08-08 14:19:20 -07001138 if (error) {
1139 mem_cgroup_cancel_charge(page, memcg);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001140 delete_from_swap_cache(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07001141 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001142 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001143 if (error)
1144 goto failed;
1145
Johannes Weiner00501b52014-08-08 14:19:20 -07001146 mem_cgroup_commit_charge(page, memcg, true);
1147
Hugh Dickins54af60422011-08-03 16:21:24 -07001148 spin_lock(&info->lock);
1149 info->swapped--;
1150 shmem_recalc_inode(inode);
1151 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001152
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001153 if (sgp == SGP_WRITE)
1154 mark_page_accessed(page);
1155
Hugh Dickins27ab7002011-07-25 17:12:36 -07001156 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001157 set_page_dirty(page);
1158 swap_free(swap);
1159
Hugh Dickins54af60422011-08-03 16:21:24 -07001160 } else {
1161 if (shmem_acct_block(info->flags)) {
1162 error = -ENOSPC;
1163 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001165 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -07001166 if (percpu_counter_compare(&sbinfo->used_blocks,
Hugh Dickins54af60422011-08-03 16:21:24 -07001167 sbinfo->max_blocks) >= 0) {
1168 error = -ENOSPC;
1169 goto unacct;
1170 }
Tim Chen7e496292010-08-09 17:19:05 -07001171 percpu_counter_inc(&sbinfo->used_blocks);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Hugh Dickins54af60422011-08-03 16:21:24 -07001174 page = shmem_alloc_page(gfp, info, index);
1175 if (!page) {
1176 error = -ENOMEM;
1177 goto decused;
1178 }
1179
Mel Gorman07a42782014-06-04 16:10:24 -07001180 __SetPageSwapBacked(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001181 __set_page_locked(page);
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001182 if (sgp == SGP_WRITE)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001183 __SetPageReferenced(page);
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001184
Johannes Weiner00501b52014-08-08 14:19:20 -07001185 error = mem_cgroup_try_charge(page, current->mm, gfp, &memcg);
Hugh Dickins54af60422011-08-03 16:21:24 -07001186 if (error)
1187 goto decused;
Jan Kara5e4c0d972013-09-11 14:26:05 -07001188 error = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
Hugh Dickinsb065b432012-07-11 14:02:48 -07001189 if (!error) {
1190 error = shmem_add_to_page_cache(page, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001191 NULL);
Hugh Dickinsb065b432012-07-11 14:02:48 -07001192 radix_tree_preload_end();
1193 }
1194 if (error) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001195 mem_cgroup_cancel_charge(page, memcg);
Hugh Dickinsb065b432012-07-11 14:02:48 -07001196 goto decused;
1197 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001198 mem_cgroup_commit_charge(page, memcg, false);
Hugh Dickins54af60422011-08-03 16:21:24 -07001199 lru_cache_add_anon(page);
1200
1201 spin_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 info->alloced++;
Hugh Dickins54af60422011-08-03 16:21:24 -07001203 inode->i_blocks += BLOCKS_PER_PAGE;
1204 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 spin_unlock(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001206 alloced = true;
Hugh Dickins54af60422011-08-03 16:21:24 -07001207
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001208 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001209 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1210 */
1211 if (sgp == SGP_FALLOC)
1212 sgp = SGP_WRITE;
1213clear:
1214 /*
1215 * Let SGP_WRITE caller clear ends if write does not fill page;
1216 * but SGP_FALLOC on a page fallocated earlier must initialize
1217 * it now, lest undo on failure cancel our earlier guarantee.
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001218 */
1219 if (sgp != SGP_WRITE) {
1220 clear_highpage(page);
1221 flush_dcache_page(page);
1222 SetPageUptodate(page);
1223 }
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001224 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -07001225 set_page_dirty(page);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001226 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001227
Hugh Dickins54af60422011-08-03 16:21:24 -07001228 /* Perhaps the file has been truncated since we checked */
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001229 if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
Hugh Dickins54af60422011-08-03 16:21:24 -07001230 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1231 error = -EINVAL;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001232 if (alloced)
1233 goto trunc;
1234 else
1235 goto failed;
Shaohua Liff36b8012010-08-09 17:19:06 -07001236 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001237 *pagep = page;
1238 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001239
Nick Piggind0217ac2007-07-19 01:47:03 -07001240 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001241 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001243trunc:
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001244 info = SHMEM_I(inode);
Hugh Dickins54af60422011-08-03 16:21:24 -07001245 ClearPageDirty(page);
1246 delete_from_page_cache(page);
1247 spin_lock(&info->lock);
1248 info->alloced--;
1249 inode->i_blocks -= BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 spin_unlock(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001251decused:
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001252 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins54af60422011-08-03 16:21:24 -07001253 if (sbinfo->max_blocks)
1254 percpu_counter_add(&sbinfo->used_blocks, -1);
1255unacct:
1256 shmem_unacct_blocks(info->flags, 1);
1257failed:
Hugh Dickinsd1899222012-07-11 14:02:47 -07001258 if (swap.val && error != -EINVAL &&
1259 !shmem_confirm_swap(mapping, index, swap))
1260 error = -EEXIST;
1261unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001262 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001263 unlock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001264 page_cache_release(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001265 }
1266 if (error == -ENOSPC && !once++) {
1267 info = SHMEM_I(inode);
1268 spin_lock(&info->lock);
1269 shmem_recalc_inode(inode);
1270 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001271 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001272 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001273 if (error == -EEXIST) /* from above or from radix_tree_insert */
Hugh Dickins54af60422011-08-03 16:21:24 -07001274 goto repeat;
1275 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1279{
Al Viro496ad9a2013-01-23 17:07:38 -05001280 struct inode *inode = file_inode(vma->vm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001282 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001284 /*
1285 * Trinity finds that probing a hole which tmpfs is punching can
1286 * prevent the hole-punch from ever completing: which in turn
1287 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001288 * faulting pages into the hole while it's being punched. Although
1289 * shmem_undo_range() does remove the additions, it may be unable to
1290 * keep up, as each new page needs its own unmap_mapping_range() call,
1291 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1292 *
1293 * It does not matter if we sometimes reach this check just before the
1294 * hole-punch begins, so that one fault then races with the punch:
1295 * we just need to make racing faults a rare case.
1296 *
1297 * The implementation below would be much simpler if we just used a
1298 * standard mutex or completion: but we cannot take i_mutex in fault,
1299 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001300 */
1301 if (unlikely(inode->i_private)) {
1302 struct shmem_falloc *shmem_falloc;
1303
1304 spin_lock(&inode->i_lock);
1305 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001306 if (shmem_falloc &&
1307 shmem_falloc->waitq &&
1308 vmf->pgoff >= shmem_falloc->start &&
1309 vmf->pgoff < shmem_falloc->next) {
1310 wait_queue_head_t *shmem_falloc_waitq;
1311 DEFINE_WAIT(shmem_fault_wait);
1312
1313 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001314 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1315 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07001316 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001317 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001318 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001319 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001320
1321 shmem_falloc_waitq = shmem_falloc->waitq;
1322 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1323 TASK_UNINTERRUPTIBLE);
1324 spin_unlock(&inode->i_lock);
1325 schedule();
1326
1327 /*
1328 * shmem_falloc_waitq points into the shmem_fallocate()
1329 * stack of the hole-punching task: shmem_falloc_waitq
1330 * is usually invalid by the time we reach here, but
1331 * finish_wait() does not dereference it in that case;
1332 * though i_lock needed lest racing with wake_up_all().
1333 */
1334 spin_lock(&inode->i_lock);
1335 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
1336 spin_unlock(&inode->i_lock);
1337 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001338 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001339 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001340 }
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1343 if (error)
1344 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001345
Ying Han456f9982011-05-26 16:25:38 -07001346 if (ret & VM_FAULT_MAJOR) {
1347 count_vm_event(PGMAJFAULT);
1348 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1349 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001350 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
1353#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001354static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Al Viro496ad9a2013-01-23 17:07:38 -05001356 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001357 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001360static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1361 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Al Viro496ad9a2013-01-23 17:07:38 -05001363 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001364 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001366 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1367 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369#endif
1370
1371int shmem_lock(struct file *file, int lock, struct user_struct *user)
1372{
Al Viro496ad9a2013-01-23 17:07:38 -05001373 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 struct shmem_inode_info *info = SHMEM_I(inode);
1375 int retval = -ENOMEM;
1376
1377 spin_lock(&info->lock);
1378 if (lock && !(info->flags & VM_LOCKED)) {
1379 if (!user_shm_lock(inode->i_size, user))
1380 goto out_nomem;
1381 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001382 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384 if (!lock && (info->flags & VM_LOCKED) && user) {
1385 user_shm_unlock(inode->i_size, user);
1386 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001387 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391out_nomem:
1392 spin_unlock(&info->lock);
1393 return retval;
1394}
1395
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001396static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 file_accessed(file);
1399 vma->vm_ops = &shmem_vm_ops;
1400 return 0;
1401}
1402
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001403static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04001404 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 struct inode *inode;
1407 struct shmem_inode_info *info;
1408 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1409
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001410 if (shmem_reserve_inode(sb))
1411 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 inode = new_inode(sb);
1414 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001415 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001416 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1419 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001420 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 info = SHMEM_I(inode);
1422 memset(info, 0, (char *)inode - (char *)info);
1423 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07001424 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001425 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001427 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04001428 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 switch (mode & S_IFMT) {
1431 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001432 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 init_special_inode(inode, mode, dev);
1434 break;
1435 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001436 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 inode->i_op = &shmem_inode_operations;
1438 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001439 mpol_shared_policy_init(&info->policy,
1440 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 break;
1442 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001443 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* Some things misbehave if size == 0 on a directory */
1445 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1446 inode->i_op = &shmem_dir_inode_operations;
1447 inode->i_fop = &simple_dir_operations;
1448 break;
1449 case S_IFLNK:
1450 /*
1451 * Must not load anything in the rbtree,
1452 * mpol_free_shared_policy will not be called.
1453 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001454 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
1456 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001457 } else
1458 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return inode;
1460}
1461
Johannes Weiner0cd61442014-04-03 14:47:46 -07001462bool shmem_mapping(struct address_space *mapping)
1463{
1464 return mapping->backing_dev_info == &shmem_backing_dev_info;
1465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001468static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001469static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001471#ifdef CONFIG_TMPFS_XATTR
1472static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1473#else
1474#define shmem_initxattrs NULL
1475#endif
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001478shmem_write_begin(struct file *file, struct address_space *mapping,
1479 loff_t pos, unsigned len, unsigned flags,
1480 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Nick Piggin800d15a2007-10-16 01:25:03 -07001482 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07001483 struct shmem_inode_info *info = SHMEM_I(inode);
Nick Piggin800d15a2007-10-16 01:25:03 -07001484 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07001485
1486 /* i_mutex is held by caller */
1487 if (unlikely(info->seals)) {
1488 if (info->seals & F_SEAL_WRITE)
1489 return -EPERM;
1490 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
1491 return -EPERM;
1492 }
1493
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001494 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
Nick Piggin800d15a2007-10-16 01:25:03 -07001495}
1496
1497static int
1498shmem_write_end(struct file *file, struct address_space *mapping,
1499 loff_t pos, unsigned len, unsigned copied,
1500 struct page *page, void *fsdata)
1501{
1502 struct inode *inode = mapping->host;
1503
Hugh Dickinsd3602442008-02-04 22:28:44 -08001504 if (pos + copied > inode->i_size)
1505 i_size_write(inode, pos + copied);
1506
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001507 if (!PageUptodate(page)) {
1508 if (copied < PAGE_CACHE_SIZE) {
1509 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1510 zero_user_segments(page, 0, from,
1511 from + copied, PAGE_CACHE_SIZE);
1512 }
1513 SetPageUptodate(page);
1514 }
Nick Piggin800d15a2007-10-16 01:25:03 -07001515 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001516 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001517 page_cache_release(page);
1518
Nick Piggin800d15a2007-10-16 01:25:03 -07001519 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
Al Viro2ba5bbe2014-04-02 20:00:02 -04001522static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Al Viro6e58e792014-02-03 17:07:03 -05001524 struct file *file = iocb->ki_filp;
1525 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001527 pgoff_t index;
1528 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001529 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02001530 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05001531 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05001532 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001533
1534 /*
1535 * Might this read be for a stacking filesystem? Then when reading
1536 * holes of a sparse file, we actually need to allocate those pages,
1537 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1538 */
1539 if (segment_eq(get_fs(), KERNEL_DS))
1540 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 index = *ppos >> PAGE_CACHE_SHIFT;
1543 offset = *ppos & ~PAGE_CACHE_MASK;
1544
1545 for (;;) {
1546 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001547 pgoff_t end_index;
1548 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 loff_t i_size = i_size_read(inode);
1550
1551 end_index = i_size >> PAGE_CACHE_SHIFT;
1552 if (index > end_index)
1553 break;
1554 if (index == end_index) {
1555 nr = i_size & ~PAGE_CACHE_MASK;
1556 if (nr <= offset)
1557 break;
1558 }
1559
Al Viro6e58e792014-02-03 17:07:03 -05001560 error = shmem_getpage(inode, index, &page, sgp, NULL);
1561 if (error) {
1562 if (error == -EINVAL)
1563 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 break;
1565 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001566 if (page)
1567 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 /*
1570 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001571 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 */
1573 nr = PAGE_CACHE_SIZE;
1574 i_size = i_size_read(inode);
1575 end_index = i_size >> PAGE_CACHE_SHIFT;
1576 if (index == end_index) {
1577 nr = i_size & ~PAGE_CACHE_MASK;
1578 if (nr <= offset) {
1579 if (page)
1580 page_cache_release(page);
1581 break;
1582 }
1583 }
1584 nr -= offset;
1585
1586 if (page) {
1587 /*
1588 * If users can be writing to this page using arbitrary
1589 * virtual addresses, take care about potential aliasing
1590 * before reading the page on the kernel side.
1591 */
1592 if (mapping_writably_mapped(mapping))
1593 flush_dcache_page(page);
1594 /*
1595 * Mark the page accessed if we read the beginning.
1596 */
1597 if (!offset)
1598 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001599 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001601 page_cache_get(page);
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 /*
1605 * Ok, we have the page, and it's up-to-date, so
1606 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04001608 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05001609 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 offset += ret;
1611 index += offset >> PAGE_CACHE_SHIFT;
1612 offset &= ~PAGE_CACHE_MASK;
1613
1614 page_cache_release(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04001615 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 break;
Al Viro6e58e792014-02-03 17:07:03 -05001617 if (ret < nr) {
1618 error = -EFAULT;
1619 break;
1620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 cond_resched();
1622 }
1623
1624 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05001625 file_accessed(file);
1626 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Hugh Dickins708e3502011-07-25 17:12:32 -07001629static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1630 struct pipe_inode_info *pipe, size_t len,
1631 unsigned int flags)
1632{
1633 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001634 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001635 unsigned int loff, nr_pages, req_pages;
1636 struct page *pages[PIPE_DEF_BUFFERS];
1637 struct partial_page partial[PIPE_DEF_BUFFERS];
1638 struct page *page;
1639 pgoff_t index, end_index;
1640 loff_t isize, left;
1641 int error, page_nr;
1642 struct splice_pipe_desc spd = {
1643 .pages = pages,
1644 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02001645 .nr_pages_max = PIPE_DEF_BUFFERS,
Hugh Dickins708e3502011-07-25 17:12:32 -07001646 .flags = flags,
1647 .ops = &page_cache_pipe_buf_ops,
1648 .spd_release = spd_release_page,
1649 };
1650
Hugh Dickins71f0e072011-07-25 17:12:33 -07001651 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001652 if (unlikely(*ppos >= isize))
1653 return 0;
1654
1655 left = isize - *ppos;
1656 if (unlikely(left < len))
1657 len = left;
1658
1659 if (splice_grow_spd(pipe, &spd))
1660 return -ENOMEM;
1661
1662 index = *ppos >> PAGE_CACHE_SHIFT;
1663 loff = *ppos & ~PAGE_CACHE_MASK;
1664 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Al Viroa786c062014-04-11 12:01:03 -04001665 nr_pages = min(req_pages, spd.nr_pages_max);
Hugh Dickins708e3502011-07-25 17:12:32 -07001666
Hugh Dickins708e3502011-07-25 17:12:32 -07001667 spd.nr_pages = find_get_pages_contig(mapping, index,
1668 nr_pages, spd.pages);
1669 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001670 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001671
Hugh Dickins708e3502011-07-25 17:12:32 -07001672 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001673 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1674 if (error)
1675 break;
1676 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001677 spd.pages[spd.nr_pages++] = page;
1678 index++;
1679 }
1680
Hugh Dickins708e3502011-07-25 17:12:32 -07001681 index = *ppos >> PAGE_CACHE_SHIFT;
1682 nr_pages = spd.nr_pages;
1683 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001684
Hugh Dickins708e3502011-07-25 17:12:32 -07001685 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1686 unsigned int this_len;
1687
1688 if (!len)
1689 break;
1690
Hugh Dickins708e3502011-07-25 17:12:32 -07001691 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1692 page = spd.pages[page_nr];
1693
Hugh Dickins71f0e072011-07-25 17:12:33 -07001694 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001695 error = shmem_getpage(inode, index, &page,
1696 SGP_CACHE, NULL);
1697 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001698 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001699 unlock_page(page);
1700 page_cache_release(spd.pages[page_nr]);
1701 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001702 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001703
1704 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001705 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1706 if (unlikely(!isize || index > end_index))
1707 break;
1708
Hugh Dickins708e3502011-07-25 17:12:32 -07001709 if (end_index == index) {
1710 unsigned int plen;
1711
Hugh Dickins708e3502011-07-25 17:12:32 -07001712 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1713 if (plen <= loff)
1714 break;
1715
Hugh Dickins708e3502011-07-25 17:12:32 -07001716 this_len = min(this_len, plen - loff);
1717 len = this_len;
1718 }
1719
1720 spd.partial[page_nr].offset = loff;
1721 spd.partial[page_nr].len = this_len;
1722 len -= this_len;
1723 loff = 0;
1724 spd.nr_pages++;
1725 index++;
1726 }
1727
Hugh Dickins708e3502011-07-25 17:12:32 -07001728 while (page_nr < nr_pages)
1729 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001730
1731 if (spd.nr_pages)
1732 error = splice_to_pipe(pipe, &spd);
1733
Eric Dumazet047fe362012-06-12 15:24:40 +02001734 splice_shrink_spd(&spd);
Hugh Dickins708e3502011-07-25 17:12:32 -07001735
1736 if (error > 0) {
1737 *ppos += error;
1738 file_accessed(in);
1739 }
1740 return error;
1741}
1742
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001743/*
1744 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
1745 */
1746static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08001747 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001748{
1749 struct page *page;
1750 struct pagevec pvec;
1751 pgoff_t indices[PAGEVEC_SIZE];
1752 bool done = false;
1753 int i;
1754
1755 pagevec_init(&pvec, 0);
1756 pvec.nr = 1; /* start small: we may be there already */
1757 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07001758 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001759 pvec.nr, pvec.pages, indices);
1760 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08001761 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001762 index = end;
1763 break;
1764 }
1765 for (i = 0; i < pvec.nr; i++, index++) {
1766 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08001767 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001768 done = true;
1769 break;
1770 }
1771 index = indices[i];
1772 }
1773 page = pvec.pages[i];
1774 if (page && !radix_tree_exceptional_entry(page)) {
1775 if (!PageUptodate(page))
1776 page = NULL;
1777 }
1778 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08001779 (page && whence == SEEK_DATA) ||
1780 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001781 done = true;
1782 break;
1783 }
1784 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001785 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001786 pagevec_release(&pvec);
1787 pvec.nr = PAGEVEC_SIZE;
1788 cond_resched();
1789 }
1790 return index;
1791}
1792
Andrew Morton965c8e52012-12-17 15:59:39 -08001793static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001794{
1795 struct address_space *mapping = file->f_mapping;
1796 struct inode *inode = mapping->host;
1797 pgoff_t start, end;
1798 loff_t new_offset;
1799
Andrew Morton965c8e52012-12-17 15:59:39 -08001800 if (whence != SEEK_DATA && whence != SEEK_HOLE)
1801 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001802 MAX_LFS_FILESIZE, i_size_read(inode));
1803 mutex_lock(&inode->i_mutex);
1804 /* We're holding i_mutex so we can access i_size directly */
1805
1806 if (offset < 0)
1807 offset = -EINVAL;
1808 else if (offset >= inode->i_size)
1809 offset = -ENXIO;
1810 else {
1811 start = offset >> PAGE_CACHE_SHIFT;
1812 end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08001813 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001814 new_offset <<= PAGE_CACHE_SHIFT;
1815 if (new_offset > offset) {
1816 if (new_offset < inode->i_size)
1817 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08001818 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001819 offset = -ENXIO;
1820 else
1821 offset = inode->i_size;
1822 }
1823 }
1824
Hugh Dickins387aae62013-08-04 11:30:25 -07001825 if (offset >= 0)
1826 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08001827 mutex_unlock(&inode->i_mutex);
1828 return offset;
1829}
1830
David Herrmann05f65b52014-08-08 14:25:36 -07001831/*
1832 * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
1833 * so reuse a tag which we firmly believe is never set or cleared on shmem.
1834 */
1835#define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
1836#define LAST_SCAN 4 /* about 150ms max */
1837
1838static void shmem_tag_pins(struct address_space *mapping)
1839{
1840 struct radix_tree_iter iter;
1841 void **slot;
1842 pgoff_t start;
1843 struct page *page;
1844
1845 lru_add_drain();
1846 start = 0;
1847 rcu_read_lock();
1848
1849restart:
1850 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
1851 page = radix_tree_deref_slot(slot);
1852 if (!page || radix_tree_exception(page)) {
1853 if (radix_tree_deref_retry(page))
1854 goto restart;
1855 } else if (page_count(page) - page_mapcount(page) > 1) {
1856 spin_lock_irq(&mapping->tree_lock);
1857 radix_tree_tag_set(&mapping->page_tree, iter.index,
1858 SHMEM_TAG_PINNED);
1859 spin_unlock_irq(&mapping->tree_lock);
1860 }
1861
1862 if (need_resched()) {
1863 cond_resched_rcu();
1864 start = iter.index + 1;
1865 goto restart;
1866 }
1867 }
1868 rcu_read_unlock();
1869}
1870
1871/*
1872 * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
1873 * via get_user_pages(), drivers might have some pending I/O without any active
1874 * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
1875 * and see whether it has an elevated ref-count. If so, we tag them and wait for
1876 * them to be dropped.
1877 * The caller must guarantee that no new user will acquire writable references
1878 * to those pages to avoid races.
1879 */
David Herrmann40e041a2014-08-08 14:25:27 -07001880static int shmem_wait_for_pins(struct address_space *mapping)
1881{
David Herrmann05f65b52014-08-08 14:25:36 -07001882 struct radix_tree_iter iter;
1883 void **slot;
1884 pgoff_t start;
1885 struct page *page;
1886 int error, scan;
1887
1888 shmem_tag_pins(mapping);
1889
1890 error = 0;
1891 for (scan = 0; scan <= LAST_SCAN; scan++) {
1892 if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
1893 break;
1894
1895 if (!scan)
1896 lru_add_drain_all();
1897 else if (schedule_timeout_killable((HZ << scan) / 200))
1898 scan = LAST_SCAN;
1899
1900 start = 0;
1901 rcu_read_lock();
1902restart:
1903 radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
1904 start, SHMEM_TAG_PINNED) {
1905
1906 page = radix_tree_deref_slot(slot);
1907 if (radix_tree_exception(page)) {
1908 if (radix_tree_deref_retry(page))
1909 goto restart;
1910
1911 page = NULL;
1912 }
1913
1914 if (page &&
1915 page_count(page) - page_mapcount(page) != 1) {
1916 if (scan < LAST_SCAN)
1917 goto continue_resched;
1918
1919 /*
1920 * On the last scan, we clean up all those tags
1921 * we inserted; but make a note that we still
1922 * found pages pinned.
1923 */
1924 error = -EBUSY;
1925 }
1926
1927 spin_lock_irq(&mapping->tree_lock);
1928 radix_tree_tag_clear(&mapping->page_tree,
1929 iter.index, SHMEM_TAG_PINNED);
1930 spin_unlock_irq(&mapping->tree_lock);
1931continue_resched:
1932 if (need_resched()) {
1933 cond_resched_rcu();
1934 start = iter.index + 1;
1935 goto restart;
1936 }
1937 }
1938 rcu_read_unlock();
1939 }
1940
1941 return error;
David Herrmann40e041a2014-08-08 14:25:27 -07001942}
1943
1944#define F_ALL_SEALS (F_SEAL_SEAL | \
1945 F_SEAL_SHRINK | \
1946 F_SEAL_GROW | \
1947 F_SEAL_WRITE)
1948
1949int shmem_add_seals(struct file *file, unsigned int seals)
1950{
1951 struct inode *inode = file_inode(file);
1952 struct shmem_inode_info *info = SHMEM_I(inode);
1953 int error;
1954
1955 /*
1956 * SEALING
1957 * Sealing allows multiple parties to share a shmem-file but restrict
1958 * access to a specific subset of file operations. Seals can only be
1959 * added, but never removed. This way, mutually untrusted parties can
1960 * share common memory regions with a well-defined policy. A malicious
1961 * peer can thus never perform unwanted operations on a shared object.
1962 *
1963 * Seals are only supported on special shmem-files and always affect
1964 * the whole underlying inode. Once a seal is set, it may prevent some
1965 * kinds of access to the file. Currently, the following seals are
1966 * defined:
1967 * SEAL_SEAL: Prevent further seals from being set on this file
1968 * SEAL_SHRINK: Prevent the file from shrinking
1969 * SEAL_GROW: Prevent the file from growing
1970 * SEAL_WRITE: Prevent write access to the file
1971 *
1972 * As we don't require any trust relationship between two parties, we
1973 * must prevent seals from being removed. Therefore, sealing a file
1974 * only adds a given set of seals to the file, it never touches
1975 * existing seals. Furthermore, the "setting seals"-operation can be
1976 * sealed itself, which basically prevents any further seal from being
1977 * added.
1978 *
1979 * Semantics of sealing are only defined on volatile files. Only
1980 * anonymous shmem files support sealing. More importantly, seals are
1981 * never written to disk. Therefore, there's no plan to support it on
1982 * other file types.
1983 */
1984
1985 if (file->f_op != &shmem_file_operations)
1986 return -EINVAL;
1987 if (!(file->f_mode & FMODE_WRITE))
1988 return -EPERM;
1989 if (seals & ~(unsigned int)F_ALL_SEALS)
1990 return -EINVAL;
1991
1992 mutex_lock(&inode->i_mutex);
1993
1994 if (info->seals & F_SEAL_SEAL) {
1995 error = -EPERM;
1996 goto unlock;
1997 }
1998
1999 if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
2000 error = mapping_deny_writable(file->f_mapping);
2001 if (error)
2002 goto unlock;
2003
2004 error = shmem_wait_for_pins(file->f_mapping);
2005 if (error) {
2006 mapping_allow_writable(file->f_mapping);
2007 goto unlock;
2008 }
2009 }
2010
2011 info->seals |= seals;
2012 error = 0;
2013
2014unlock:
2015 mutex_unlock(&inode->i_mutex);
2016 return error;
2017}
2018EXPORT_SYMBOL_GPL(shmem_add_seals);
2019
2020int shmem_get_seals(struct file *file)
2021{
2022 if (file->f_op != &shmem_file_operations)
2023 return -EINVAL;
2024
2025 return SHMEM_I(file_inode(file))->seals;
2026}
2027EXPORT_SYMBOL_GPL(shmem_get_seals);
2028
2029long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2030{
2031 long error;
2032
2033 switch (cmd) {
2034 case F_ADD_SEALS:
2035 /* disallow upper 32bit */
2036 if (arg > UINT_MAX)
2037 return -EINVAL;
2038
2039 error = shmem_add_seals(file, arg);
2040 break;
2041 case F_GET_SEALS:
2042 error = shmem_get_seals(file);
2043 break;
2044 default:
2045 error = -EINVAL;
2046 break;
2047 }
2048
2049 return error;
2050}
2051
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002052static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2053 loff_t len)
2054{
Al Viro496ad9a2013-01-23 17:07:38 -05002055 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002056 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002057 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002058 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002059 pgoff_t start, index, end;
2060 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002061
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002062 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2063 return -EOPNOTSUPP;
2064
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002065 mutex_lock(&inode->i_mutex);
2066
2067 if (mode & FALLOC_FL_PUNCH_HOLE) {
2068 struct address_space *mapping = file->f_mapping;
2069 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2070 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002071 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002072
David Herrmann40e041a2014-08-08 14:25:27 -07002073 /* protected by i_mutex */
2074 if (info->seals & F_SEAL_WRITE) {
2075 error = -EPERM;
2076 goto out;
2077 }
2078
Hugh Dickins8e205f72014-07-23 14:00:10 -07002079 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002080 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2081 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2082 spin_lock(&inode->i_lock);
2083 inode->i_private = &shmem_falloc;
2084 spin_unlock(&inode->i_lock);
2085
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002086 if ((u64)unmap_end > (u64)unmap_start)
2087 unmap_mapping_range(mapping, unmap_start,
2088 1 + unmap_end - unmap_start, 0);
2089 shmem_truncate_range(inode, offset, offset + len - 1);
2090 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002091
2092 spin_lock(&inode->i_lock);
2093 inode->i_private = NULL;
2094 wake_up_all(&shmem_falloc_waitq);
2095 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002096 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002097 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002098 }
2099
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002100 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2101 error = inode_newsize_ok(inode, offset + len);
2102 if (error)
2103 goto out;
2104
David Herrmann40e041a2014-08-08 14:25:27 -07002105 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2106 error = -EPERM;
2107 goto out;
2108 }
2109
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002110 start = offset >> PAGE_CACHE_SHIFT;
2111 end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2112 /* Try to avoid a swapstorm if len is impossible to satisfy */
2113 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2114 error = -ENOSPC;
2115 goto out;
2116 }
2117
Hugh Dickins8e205f72014-07-23 14:00:10 -07002118 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002119 shmem_falloc.start = start;
2120 shmem_falloc.next = start;
2121 shmem_falloc.nr_falloced = 0;
2122 shmem_falloc.nr_unswapped = 0;
2123 spin_lock(&inode->i_lock);
2124 inode->i_private = &shmem_falloc;
2125 spin_unlock(&inode->i_lock);
2126
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002127 for (index = start; index < end; index++) {
2128 struct page *page;
2129
2130 /*
2131 * Good, the fallocate(2) manpage permits EINTR: we may have
2132 * been interrupted because we are using up too much memory.
2133 */
2134 if (signal_pending(current))
2135 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002136 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2137 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002138 else
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002139 error = shmem_getpage(inode, index, &page, SGP_FALLOC,
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002140 NULL);
2141 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002142 /* Remove the !PageUptodate pages we added */
2143 shmem_undo_range(inode,
2144 (loff_t)start << PAGE_CACHE_SHIFT,
2145 (loff_t)index << PAGE_CACHE_SHIFT, true);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002146 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002147 }
2148
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002149 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002150 * Inform shmem_writepage() how far we have reached.
2151 * No need for lock or barrier: we have the page lock.
2152 */
2153 shmem_falloc.next++;
2154 if (!PageUptodate(page))
2155 shmem_falloc.nr_falloced++;
2156
2157 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002158 * If !PageUptodate, leave it that way so that freeable pages
2159 * can be recognized if we need to rollback on error later.
2160 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002161 * than free the pages we are allocating (and SGP_CACHE pages
2162 * might still be clean: we now need to mark those dirty too).
2163 */
2164 set_page_dirty(page);
2165 unlock_page(page);
2166 page_cache_release(page);
2167 cond_resched();
2168 }
2169
2170 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2171 i_size_write(inode, offset + len);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002172 inode->i_ctime = CURRENT_TIME;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002173undone:
2174 spin_lock(&inode->i_lock);
2175 inode->i_private = NULL;
2176 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002177out:
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002178 mutex_unlock(&inode->i_mutex);
2179 return error;
2180}
2181
David Howells726c3342006-06-23 02:02:58 -07002182static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
David Howells726c3342006-06-23 02:02:58 -07002184 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 buf->f_type = TMPFS_MAGIC;
2187 buf->f_bsize = PAGE_CACHE_SIZE;
2188 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002189 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002191 buf->f_bavail =
2192 buf->f_bfree = sbinfo->max_blocks -
2193 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002194 }
2195 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 buf->f_files = sbinfo->max_inodes;
2197 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199 /* else leave those fields 0 like simple_statfs */
2200 return 0;
2201}
2202
2203/*
2204 * File creation. Allocate an inode, and we're done..
2205 */
2206static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002207shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002209 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 int error = -ENOSPC;
2211
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002212 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002214 error = simple_acl_create(dir, inode);
2215 if (error)
2216 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002217 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002218 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002219 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002220 if (error && error != -EOPNOTSUPP)
2221 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002222
Al Viro718deb62009-12-16 19:35:36 -05002223 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 dir->i_size += BOGO_DIRENT_SIZE;
2225 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2226 d_instantiate(dentry, inode);
2227 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002230out_iput:
2231 iput(inode);
2232 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Al Viro60545d02013-06-07 01:20:27 -04002235static int
2236shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2237{
2238 struct inode *inode;
2239 int error = -ENOSPC;
2240
2241 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2242 if (inode) {
2243 error = security_inode_init_security(inode, dir,
2244 NULL,
2245 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002246 if (error && error != -EOPNOTSUPP)
2247 goto out_iput;
2248 error = simple_acl_create(dir, inode);
2249 if (error)
2250 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002251 d_tmpfile(dentry, inode);
2252 }
2253 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002254out_iput:
2255 iput(inode);
2256 return error;
Al Viro60545d02013-06-07 01:20:27 -04002257}
2258
Al Viro18bb1db2011-07-26 01:41:39 -04002259static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261 int error;
2262
2263 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2264 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002265 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return 0;
2267}
2268
Al Viro4acdaf22011-07-26 01:42:34 -04002269static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002270 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2273}
2274
2275/*
2276 * Link a file..
2277 */
2278static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2279{
2280 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002281 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 /*
2284 * No ordinary (disk based) filesystem counts links as inodes;
2285 * but each new link needs a new dentry, pinning lowmem, and
2286 * tmpfs dentries cannot be pruned until they are unlinked.
2287 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002288 ret = shmem_reserve_inode(inode->i_sb);
2289 if (ret)
2290 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 dir->i_size += BOGO_DIRENT_SIZE;
2293 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07002294 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002295 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 dget(dentry); /* Extra pinning count for the created dentry */
2297 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002298out:
2299 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
2302static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2303{
2304 struct inode *inode = dentry->d_inode;
2305
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002306 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2307 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 dir->i_size -= BOGO_DIRENT_SIZE;
2310 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002311 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 dput(dentry); /* Undo the count from "create" - this does all the work */
2313 return 0;
2314}
2315
2316static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2317{
2318 if (!simple_empty(dentry))
2319 return -ENOTEMPTY;
2320
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002321 drop_nlink(dentry->d_inode);
2322 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return shmem_unlink(dir, dentry);
2324}
2325
Miklos Szeredi37456772014-07-23 15:15:34 +02002326static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2327{
2328 bool old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2329 bool new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
2330
2331 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2332 if (old_is_dir) {
2333 drop_nlink(old_dir);
2334 inc_nlink(new_dir);
2335 } else {
2336 drop_nlink(new_dir);
2337 inc_nlink(old_dir);
2338 }
2339 }
2340 old_dir->i_ctime = old_dir->i_mtime =
2341 new_dir->i_ctime = new_dir->i_mtime =
2342 old_dentry->d_inode->i_ctime =
2343 new_dentry->d_inode->i_ctime = CURRENT_TIME;
2344
2345 return 0;
2346}
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348/*
2349 * The VFS layer already does all the dentry stuff for rename,
2350 * we just have to decrement the usage count for the target if
2351 * it exists so that the VFS layer correctly free's it when it
2352 * gets overwritten.
2353 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002354static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356 struct inode *inode = old_dentry->d_inode;
2357 int they_are_dirs = S_ISDIR(inode->i_mode);
2358
Miklos Szeredi37456772014-07-23 15:15:34 +02002359 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002360 return -EINVAL;
2361
Miklos Szeredi37456772014-07-23 15:15:34 +02002362 if (flags & RENAME_EXCHANGE)
2363 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (!simple_empty(new_dentry))
2366 return -ENOTEMPTY;
2367
2368 if (new_dentry->d_inode) {
2369 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02002370 if (they_are_dirs) {
2371 drop_nlink(new_dentry->d_inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002372 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02002373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002375 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07002376 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
2378
2379 old_dir->i_size -= BOGO_DIRENT_SIZE;
2380 new_dir->i_size += BOGO_DIRENT_SIZE;
2381 old_dir->i_ctime = old_dir->i_mtime =
2382 new_dir->i_ctime = new_dir->i_mtime =
2383 inode->i_ctime = CURRENT_TIME;
2384 return 0;
2385}
2386
2387static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2388{
2389 int error;
2390 int len;
2391 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002392 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 char *kaddr;
2394 struct shmem_inode_info *info;
2395
2396 len = strlen(symname) + 1;
2397 if (len > PAGE_CACHE_SIZE)
2398 return -ENAMETOOLONG;
2399
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002400 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (!inode)
2402 return -ENOSPC;
2403
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002404 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002405 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002406 if (error) {
2407 if (error != -EOPNOTSUPP) {
2408 iput(inode);
2409 return error;
2410 }
2411 error = 0;
2412 }
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 info = SHMEM_I(inode);
2415 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002416 if (len <= SHORT_SYMLINK_LEN) {
2417 info->symlink = kmemdup(symname, len, GFP_KERNEL);
2418 if (!info->symlink) {
2419 iput(inode);
2420 return -ENOMEM;
2421 }
2422 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 } else {
2424 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
2425 if (error) {
2426 iput(inode);
2427 return error;
2428 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07002429 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 inode->i_op = &shmem_symlink_inode_operations;
Cong Wang9b04c5f2011-11-25 23:14:39 +08002431 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 memcpy(kaddr, symname, len);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002433 kunmap_atomic(kaddr);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002434 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002436 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 page_cache_release(page);
2438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 dir->i_size += BOGO_DIRENT_SIZE;
2440 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2441 d_instantiate(dentry, inode);
2442 dget(dentry);
2443 return 0;
2444}
2445
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002446static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002448 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002449 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002452static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
2454 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002455 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
2456 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08002457 if (page)
2458 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002459 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002462static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002465 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 kunmap(page);
2467 mark_page_accessed(page);
2468 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
2470}
2471
Eric Parisb09e0fa2011-05-24 17:12:39 -07002472#ifdef CONFIG_TMPFS_XATTR
2473/*
2474 * Superblocks without xattr inode operations may get some security.* xattr
2475 * support from the LSM "for free". As soon as we have any other xattrs
2476 * like ACLs, we also need to implement the security.* handlers at
2477 * filesystem level, though.
2478 */
2479
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002480/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002481 * Callback for security_inode_init_security() for acquiring xattrs.
2482 */
2483static int shmem_initxattrs(struct inode *inode,
2484 const struct xattr *xattr_array,
2485 void *fs_info)
2486{
2487 struct shmem_inode_info *info = SHMEM_I(inode);
2488 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002489 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002490 size_t len;
2491
2492 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002493 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002494 if (!new_xattr)
2495 return -ENOMEM;
2496
2497 len = strlen(xattr->name) + 1;
2498 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
2499 GFP_KERNEL);
2500 if (!new_xattr->name) {
2501 kfree(new_xattr);
2502 return -ENOMEM;
2503 }
2504
2505 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
2506 XATTR_SECURITY_PREFIX_LEN);
2507 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
2508 xattr->name, len);
2509
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002510 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002511 }
2512
2513 return 0;
2514}
2515
Eric Parisb09e0fa2011-05-24 17:12:39 -07002516static const struct xattr_handler *shmem_xattr_handlers[] = {
2517#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002518 &posix_acl_access_xattr_handler,
2519 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002520#endif
2521 NULL
2522};
2523
2524static int shmem_xattr_validate(const char *name)
2525{
2526 struct { const char *prefix; size_t len; } arr[] = {
2527 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2528 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2529 };
2530 int i;
2531
2532 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2533 size_t preflen = arr[i].len;
2534 if (strncmp(name, arr[i].prefix, preflen) == 0) {
2535 if (!name[preflen])
2536 return -EINVAL;
2537 return 0;
2538 }
2539 }
2540 return -EOPNOTSUPP;
2541}
2542
2543static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2544 void *buffer, size_t size)
2545{
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002546 struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002547 int err;
2548
2549 /*
2550 * If this is a request for a synthetic attribute in the system.*
2551 * namespace use the generic infrastructure to resolve a handler
2552 * for it via sb->s_xattr.
2553 */
2554 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2555 return generic_getxattr(dentry, name, buffer, size);
2556
2557 err = shmem_xattr_validate(name);
2558 if (err)
2559 return err;
2560
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002561 return simple_xattr_get(&info->xattrs, name, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002562}
2563
2564static int shmem_setxattr(struct dentry *dentry, const char *name,
2565 const void *value, size_t size, int flags)
2566{
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002567 struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002568 int err;
2569
2570 /*
2571 * If this is a request for a synthetic attribute in the system.*
2572 * namespace use the generic infrastructure to resolve a handler
2573 * for it via sb->s_xattr.
2574 */
2575 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2576 return generic_setxattr(dentry, name, value, size, flags);
2577
2578 err = shmem_xattr_validate(name);
2579 if (err)
2580 return err;
2581
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002582 return simple_xattr_set(&info->xattrs, name, value, size, flags);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002583}
2584
2585static int shmem_removexattr(struct dentry *dentry, const char *name)
2586{
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002587 struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002588 int err;
2589
2590 /*
2591 * If this is a request for a synthetic attribute in the system.*
2592 * namespace use the generic infrastructure to resolve a handler
2593 * for it via sb->s_xattr.
2594 */
2595 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2596 return generic_removexattr(dentry, name);
2597
2598 err = shmem_xattr_validate(name);
2599 if (err)
2600 return err;
2601
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002602 return simple_xattr_remove(&info->xattrs, name);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002603}
2604
2605static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2606{
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002607 struct shmem_inode_info *info = SHMEM_I(dentry->d_inode);
2608 return simple_xattr_list(&info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002609}
2610#endif /* CONFIG_TMPFS_XATTR */
2611
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002612static const struct inode_operations shmem_short_symlink_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 .readlink = generic_readlink,
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002614 .follow_link = shmem_follow_short_symlink,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002615#ifdef CONFIG_TMPFS_XATTR
2616 .setxattr = shmem_setxattr,
2617 .getxattr = shmem_getxattr,
2618 .listxattr = shmem_listxattr,
2619 .removexattr = shmem_removexattr,
2620#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621};
2622
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002623static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 .readlink = generic_readlink,
2625 .follow_link = shmem_follow_link,
2626 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002627#ifdef CONFIG_TMPFS_XATTR
2628 .setxattr = shmem_setxattr,
2629 .getxattr = shmem_getxattr,
2630 .listxattr = shmem_listxattr,
2631 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002632#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002633};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002634
David M. Grimes91828a42006-10-17 00:09:45 -07002635static struct dentry *shmem_get_parent(struct dentry *child)
2636{
2637 return ERR_PTR(-ESTALE);
2638}
2639
2640static int shmem_match(struct inode *ino, void *vfh)
2641{
2642 __u32 *fh = vfh;
2643 __u64 inum = fh[2];
2644 inum = (inum << 32) | fh[1];
2645 return ino->i_ino == inum && fh[0] == ino->i_generation;
2646}
2647
Christoph Hellwig480b1162007-10-21 16:42:13 -07002648static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2649 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07002650{
David M. Grimes91828a42006-10-17 00:09:45 -07002651 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07002652 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07002653 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07002654
Christoph Hellwig480b1162007-10-21 16:42:13 -07002655 if (fh_len < 3)
2656 return NULL;
2657
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07002658 inum = fid->raw[2];
2659 inum = (inum << 32) | fid->raw[1];
2660
Christoph Hellwig480b1162007-10-21 16:42:13 -07002661 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2662 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07002663 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07002664 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07002665 iput(inode);
2666 }
2667
Christoph Hellwig480b1162007-10-21 16:42:13 -07002668 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07002669}
2670
Al Virob0b03822012-04-02 14:34:06 -04002671static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
2672 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07002673{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302674 if (*len < 3) {
2675 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09002676 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302677 }
David M. Grimes91828a42006-10-17 00:09:45 -07002678
Al Viro1d3382cb2010-10-23 15:19:20 -04002679 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07002680 /* Unfortunately insert_inode_hash is not idempotent,
2681 * so as we hash inodes here rather than at creation
2682 * time, we need a lock to ensure we only try
2683 * to do it once
2684 */
2685 static DEFINE_SPINLOCK(lock);
2686 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04002687 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07002688 __insert_inode_hash(inode,
2689 inode->i_ino + inode->i_generation);
2690 spin_unlock(&lock);
2691 }
2692
2693 fh[0] = inode->i_generation;
2694 fh[1] = inode->i_ino;
2695 fh[2] = ((__u64)inode->i_ino) >> 32;
2696
2697 *len = 3;
2698 return 1;
2699}
2700
Christoph Hellwig39655162007-10-21 16:42:17 -07002701static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07002702 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07002703 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07002704 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07002705};
2706
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002707static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2708 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
2710 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08002711 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08002712 uid_t uid;
2713 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00002715 while (options != NULL) {
2716 this_char = options;
2717 for (;;) {
2718 /*
2719 * NUL-terminate this option: unfortunately,
2720 * mount options form a comma-separated list,
2721 * but mpol's nodelist may also contain commas.
2722 */
2723 options = strchr(options, ',');
2724 if (options == NULL)
2725 break;
2726 options++;
2727 if (!isdigit(*options)) {
2728 options[-1] = '\0';
2729 break;
2730 }
2731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 if (!*this_char)
2733 continue;
2734 if ((value = strchr(this_char,'=')) != NULL) {
2735 *value++ = 0;
2736 } else {
2737 printk(KERN_ERR
2738 "tmpfs: No value for mount option '%s'\n",
2739 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08002740 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
2742
2743 if (!strcmp(this_char,"size")) {
2744 unsigned long long size;
2745 size = memparse(value,&rest);
2746 if (*rest == '%') {
2747 size <<= PAGE_SHIFT;
2748 size *= totalram_pages;
2749 do_div(size, 100);
2750 rest++;
2751 }
2752 if (*rest)
2753 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002754 sbinfo->max_blocks =
2755 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002757 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (*rest)
2759 goto bad_val;
2760 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002761 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (*rest)
2763 goto bad_val;
2764 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002765 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002767 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (*rest)
2769 goto bad_val;
2770 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002771 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08002773 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (*rest)
2775 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08002776 sbinfo->uid = make_kuid(current_user_ns(), uid);
2777 if (!uid_valid(sbinfo->uid))
2778 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002780 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08002782 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (*rest)
2784 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08002785 sbinfo->gid = make_kgid(current_user_ns(), gid);
2786 if (!gid_valid(sbinfo->gid))
2787 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002788 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08002789 mpol_put(mpol);
2790 mpol = NULL;
2791 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08002792 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 } else {
2794 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2795 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08002796 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
2798 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08002799 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return 0;
2801
2802bad_val:
2803 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2804 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08002805error:
2806 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return 1;
2808
2809}
2810
2811static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2812{
2813 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002814 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002815 unsigned long inodes;
2816 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Greg Thelen5f001102013-02-22 16:36:01 -08002818 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002819 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002820 return error;
2821
2822 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002823 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002824 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002825 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002826 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002827 goto out;
2828 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07002829 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002830 * but we must separately disallow unlimited->limited, because
2831 * in that case we have no record of how much is already in use.
2832 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002833 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002834 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002835 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002836 goto out;
2837
2838 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002839 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002840 sbinfo->max_inodes = config.max_inodes;
2841 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002842
Greg Thelen5f001102013-02-22 16:36:01 -08002843 /*
2844 * Preserve previous mempolicy unless mpol remount option was specified.
2845 */
2846 if (config.mpol) {
2847 mpol_put(sbinfo->mpol);
2848 sbinfo->mpol = config.mpol; /* transfers initial ref */
2849 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002850out:
2851 spin_unlock(&sbinfo->stat_lock);
2852 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002854
Al Viro34c80b12011-12-08 21:32:45 -05002855static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002856{
Al Viro34c80b12011-12-08 21:32:45 -05002857 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002858
2859 if (sbinfo->max_blocks != shmem_default_max_blocks())
2860 seq_printf(seq, ",size=%luk",
2861 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2862 if (sbinfo->max_inodes != shmem_default_max_inodes())
2863 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2864 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04002865 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08002866 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
2867 seq_printf(seq, ",uid=%u",
2868 from_kuid_munged(&init_user_ns, sbinfo->uid));
2869 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
2870 seq_printf(seq, ",gid=%u",
2871 from_kgid_munged(&init_user_ns, sbinfo->gid));
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002872 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002873 return 0;
2874}
David Herrmann9183df22014-08-08 14:25:29 -07002875
2876#define MFD_NAME_PREFIX "memfd:"
2877#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
2878#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
2879
2880#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
2881
2882SYSCALL_DEFINE2(memfd_create,
2883 const char __user *, uname,
2884 unsigned int, flags)
2885{
2886 struct shmem_inode_info *info;
2887 struct file *file;
2888 int fd, error;
2889 char *name;
2890 long len;
2891
2892 if (flags & ~(unsigned int)MFD_ALL_FLAGS)
2893 return -EINVAL;
2894
2895 /* length includes terminating zero */
2896 len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
2897 if (len <= 0)
2898 return -EFAULT;
2899 if (len > MFD_NAME_MAX_LEN + 1)
2900 return -EINVAL;
2901
2902 name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
2903 if (!name)
2904 return -ENOMEM;
2905
2906 strcpy(name, MFD_NAME_PREFIX);
2907 if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
2908 error = -EFAULT;
2909 goto err_name;
2910 }
2911
2912 /* terminating-zero may have changed after strnlen_user() returned */
2913 if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
2914 error = -EFAULT;
2915 goto err_name;
2916 }
2917
2918 fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
2919 if (fd < 0) {
2920 error = fd;
2921 goto err_name;
2922 }
2923
2924 file = shmem_file_setup(name, 0, VM_NORESERVE);
2925 if (IS_ERR(file)) {
2926 error = PTR_ERR(file);
2927 goto err_fd;
2928 }
2929 info = SHMEM_I(file_inode(file));
2930 file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
2931 file->f_flags |= O_RDWR | O_LARGEFILE;
2932 if (flags & MFD_ALLOW_SEALING)
2933 info->seals &= ~F_SEAL_SEAL;
2934
2935 fd_install(fd, file);
2936 kfree(name);
2937 return fd;
2938
2939err_fd:
2940 put_unused_fd(fd);
2941err_name:
2942 kfree(name);
2943 return error;
2944}
2945
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002946#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948static void shmem_put_super(struct super_block *sb)
2949{
Hugh Dickins602586a2010-08-17 15:23:56 -07002950 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2951
2952 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08002953 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07002954 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 sb->s_fs_info = NULL;
2956}
2957
Kay Sievers2b2af542009-04-30 15:23:42 +02002958int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
2960 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002961 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002962 int err = -ENOMEM;
2963
2964 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002965 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002966 L1_CACHE_BYTES), GFP_KERNEL);
2967 if (!sbinfo)
2968 return -ENOMEM;
2969
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002970 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002971 sbinfo->uid = current_fsuid();
2972 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002973 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002975#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 /*
2977 * Per default we only allow half of the physical ram per
2978 * tmpfs instance, limiting inodes to one per page of lowmem;
2979 * but the internal instance is left unlimited.
2980 */
Al Viroca4e0512013-08-31 12:57:10 -04002981 if (!(sb->s_flags & MS_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002982 sbinfo->max_blocks = shmem_default_max_blocks();
2983 sbinfo->max_inodes = shmem_default_max_inodes();
2984 if (shmem_parse_options(data, sbinfo, false)) {
2985 err = -EINVAL;
2986 goto failed;
2987 }
Al Viroca4e0512013-08-31 12:57:10 -04002988 } else {
2989 sb->s_flags |= MS_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 }
David M. Grimes91828a42006-10-17 00:09:45 -07002991 sb->s_export_op = &shmem_export_ops;
Hugh Dickins2f6e38f2012-05-29 15:06:38 -07002992 sb->s_flags |= MS_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993#else
2994 sb->s_flags |= MS_NOUSER;
2995#endif
2996
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002997 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002998 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2999 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003000 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003001
Hugh Dickins285b2c42011-08-03 16:21:20 -07003002 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 sb->s_blocksize = PAGE_CACHE_SIZE;
3004 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
3005 sb->s_magic = TMPFS_MAGIC;
3006 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003007 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003008#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003009 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003010#endif
3011#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003012 sb->s_flags |= MS_POSIXACL;
3013#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003014
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003015 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (!inode)
3017 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003018 inode->i_uid = sbinfo->uid;
3019 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003020 sb->s_root = d_make_root(inode);
3021 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003022 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return 0;
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025failed:
3026 shmem_put_super(sb);
3027 return err;
3028}
3029
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003030static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032static struct inode *shmem_alloc_inode(struct super_block *sb)
3033{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003034 struct shmem_inode_info *info;
3035 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3036 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003038 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003041static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003042{
3043 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003044 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3045}
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047static void shmem_destroy_inode(struct inode *inode)
3048{
Al Viro09208d12011-07-26 03:15:03 -04003049 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003051 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003054static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003056 struct shmem_inode_info *info = foo;
3057 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003060static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3063 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003064 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return 0;
3066}
3067
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003068static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003070 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003073static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003075 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003077 .write_begin = shmem_write_begin,
3078 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003080 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02003081 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082};
3083
Helge Deller15ad7cd2006-12-06 20:40:36 -08003084static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 .mmap = shmem_mmap,
3086#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003087 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003088 .read = new_sync_read,
Al Viro81742022014-04-03 03:17:43 -04003089 .write = new_sync_write,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003090 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003091 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003092 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07003093 .splice_read = shmem_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003094 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003095 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096#endif
3097};
3098
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003099static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07003100 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003101#ifdef CONFIG_TMPFS_XATTR
3102 .setxattr = shmem_setxattr,
3103 .getxattr = shmem_getxattr,
3104 .listxattr = shmem_listxattr,
3105 .removexattr = shmem_removexattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003106 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003107#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108};
3109
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003110static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111#ifdef CONFIG_TMPFS
3112 .create = shmem_create,
3113 .lookup = simple_lookup,
3114 .link = shmem_link,
3115 .unlink = shmem_unlink,
3116 .symlink = shmem_symlink,
3117 .mkdir = shmem_mkdir,
3118 .rmdir = shmem_rmdir,
3119 .mknod = shmem_mknod,
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003120 .rename2 = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003121 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003123#ifdef CONFIG_TMPFS_XATTR
3124 .setxattr = shmem_setxattr,
3125 .getxattr = shmem_getxattr,
3126 .listxattr = shmem_listxattr,
3127 .removexattr = shmem_removexattr,
3128#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003129#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003130 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003131 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003132#endif
3133};
3134
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003135static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003136#ifdef CONFIG_TMPFS_XATTR
3137 .setxattr = shmem_setxattr,
3138 .getxattr = shmem_getxattr,
3139 .listxattr = shmem_listxattr,
3140 .removexattr = shmem_removexattr,
3141#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003142#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003143 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003144 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003145#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146};
3147
Hugh Dickins759b9772007-03-05 00:30:28 -08003148static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 .alloc_inode = shmem_alloc_inode,
3150 .destroy_inode = shmem_destroy_inode,
3151#ifdef CONFIG_TMPFS
3152 .statfs = shmem_statfs,
3153 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003154 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155#endif
Al Viro1f895f72010-06-05 19:10:41 -04003156 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 .drop_inode = generic_delete_inode,
3158 .put_super = shmem_put_super,
3159};
3160
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003161static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003162 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003163 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164#ifdef CONFIG_NUMA
3165 .set_policy = shmem_set_policy,
3166 .get_policy = shmem_get_policy,
3167#endif
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07003168 .remap_pages = generic_file_remap_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169};
3170
Al Viro3c26ff62010-07-25 11:46:36 +04003171static struct dentry *shmem_mount(struct file_system_type *fs_type,
3172 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Al Viro3c26ff62010-07-25 11:46:36 +04003174 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003177static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 .owner = THIS_MODULE,
3179 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003180 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003182 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003185int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
3187 int error;
3188
Rob Landley16203a72013-09-11 14:26:12 -07003189 /* If rootfs called this, don't re-init */
3190 if (shmem_inode_cachep)
3191 return 0;
3192
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07003193 error = bdi_init(&shmem_backing_dev_info);
3194 if (error)
3195 goto out4;
3196
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003197 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (error)
3199 goto out3;
3200
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003201 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (error) {
3203 printk(KERN_ERR "Could not register tmpfs\n");
3204 goto out2;
3205 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003206
Al Viroca4e0512013-08-31 12:57:10 -04003207 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (IS_ERR(shm_mnt)) {
3209 error = PTR_ERR(shm_mnt);
3210 printk(KERN_ERR "Could not kern_mount tmpfs\n");
3211 goto out1;
3212 }
3213 return 0;
3214
3215out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003216 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003218 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07003220 bdi_destroy(&shmem_backing_dev_info);
3221out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 shm_mnt = ERR_PTR(error);
3223 return error;
3224}
Matt Mackall853ac432009-01-06 14:40:20 -08003225
3226#else /* !CONFIG_SHMEM */
3227
3228/*
3229 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3230 *
3231 * This is intended for small system where the benefits of the full
3232 * shmem code (swap-backed and resource-limited) are outweighed by
3233 * their complexity. On systems without swap this code should be
3234 * effectively equivalent, but much lighter weight.
3235 */
3236
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003237static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003238 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003239 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003240 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003241 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003242};
3243
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003244int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003245{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003246 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003247
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003248 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003249 BUG_ON(IS_ERR(shm_mnt));
3250
3251 return 0;
3252}
3253
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003254int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08003255{
3256 return 0;
3257}
3258
Hugh Dickins3f96b792009-09-21 17:03:37 -07003259int shmem_lock(struct file *file, int lock, struct user_struct *user)
3260{
3261 return 0;
3262}
3263
Hugh Dickins24513262012-01-20 14:34:21 -08003264void shmem_unlock_mapping(struct address_space *mapping)
3265{
3266}
3267
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003268void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003269{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003270 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003271}
3272EXPORT_SYMBOL_GPL(shmem_truncate_range);
3273
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003274#define shmem_vm_ops generic_file_vm_ops
3275#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003276#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003277#define shmem_acct_size(flags, size) 0
3278#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003279
3280#endif /* CONFIG_SHMEM */
3281
3282/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Al Viro34515382013-02-14 22:38:02 -05003284static struct dentry_operations anon_ops = {
Al Viro118b2302013-08-24 12:08:17 -04003285 .d_dname = simple_dname
Al Viro34515382013-02-14 22:38:02 -05003286};
3287
Eric Parisc7277092013-12-02 11:24:19 +00003288static struct file *__shmem_file_setup(const char *name, loff_t size,
3289 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
Al Viro6b4d0b22013-02-14 21:37:26 -05003291 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04003293 struct path path;
Al Viro34515382013-02-14 22:38:02 -05003294 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 struct qstr this;
3296
3297 if (IS_ERR(shm_mnt))
Al Viro6b4d0b22013-02-14 21:37:26 -05003298 return ERR_CAST(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Hugh Dickins285b2c42011-08-03 16:21:20 -07003300 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 return ERR_PTR(-EINVAL);
3302
3303 if (shmem_acct_size(flags, size))
3304 return ERR_PTR(-ENOMEM);
3305
Al Viro6b4d0b22013-02-14 21:37:26 -05003306 res = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 this.name = name;
3308 this.len = strlen(name);
3309 this.hash = 0; /* will go */
Al Viro34515382013-02-14 22:38:02 -05003310 sb = shm_mnt->mnt_sb;
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003311 path.mnt = mntget(shm_mnt);
Al Viro34515382013-02-14 22:38:02 -05003312 path.dentry = d_alloc_pseudo(sb, &this);
Al Viro2c48b9c2009-08-09 00:52:35 +04003313 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 goto put_memory;
Al Viro34515382013-02-14 22:38:02 -05003315 d_set_d_op(path.dentry, &anon_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Al Viro6b4d0b22013-02-14 21:37:26 -05003317 res = ERR_PTR(-ENOSPC);
Al Viro34515382013-02-14 22:38:02 -05003318 inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 if (!inode)
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003320 goto put_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
Eric Parisc7277092013-12-02 11:24:19 +00003322 inode->i_flags |= i_flags;
Al Viro2c48b9c2009-08-09 00:52:35 +04003323 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003325 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05003326 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
3327 if (IS_ERR(res))
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003328 goto put_path;
Al Viro4b42af82009-08-05 18:25:56 +04003329
Al Viro6b4d0b22013-02-14 21:37:26 -05003330 res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04003331 &shmem_file_operations);
Al Viro6b4d0b22013-02-14 21:37:26 -05003332 if (IS_ERR(res))
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003333 goto put_path;
Al Viro4b42af82009-08-05 18:25:56 +04003334
Al Viro6b4d0b22013-02-14 21:37:26 -05003335 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337put_memory:
3338 shmem_unacct_size(flags, size);
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003339put_path:
3340 path_put(&path);
Al Viro6b4d0b22013-02-14 21:37:26 -05003341 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
Eric Parisc7277092013-12-02 11:24:19 +00003343
3344/**
3345 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3346 * kernel internal. There will be NO LSM permission checks against the
3347 * underlying inode. So users of this interface must do LSM checks at a
3348 * higher layer. The one user is the big_key implementation. LSM checks
3349 * are provided at the key level rather than the inode level.
3350 * @name: name for dentry (to be seen in /proc/<pid>/maps
3351 * @size: size to be set for the file
3352 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3353 */
3354struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3355{
3356 return __shmem_file_setup(name, size, flags, S_PRIVATE);
3357}
3358
3359/**
3360 * shmem_file_setup - get an unlinked file living in tmpfs
3361 * @name: name for dentry (to be seen in /proc/<pid>/maps
3362 * @size: size to be set for the file
3363 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3364 */
3365struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3366{
3367 return __shmem_file_setup(name, size, flags, 0);
3368}
Keith Packard395e0dd2008-06-20 00:08:06 -07003369EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Randy Dunlap46711812008-03-19 17:00:41 -07003371/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3374 */
3375int shmem_zero_setup(struct vm_area_struct *vma)
3376{
3377 struct file *file;
3378 loff_t size = vma->vm_end - vma->vm_start;
3379
3380 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
3381 if (IS_ERR(file))
3382 return PTR_ERR(file);
3383
3384 if (vma->vm_file)
3385 fput(vma->vm_file);
3386 vma->vm_file = file;
3387 vma->vm_ops = &shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 return 0;
3389}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003390
3391/**
3392 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3393 * @mapping: the page's address_space
3394 * @index: the page index
3395 * @gfp: the page allocator flags to use if allocating
3396 *
3397 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3398 * with any new page allocations done using the specified allocation flags.
3399 * But read_cache_page_gfp() uses the ->readpage() method: which does not
3400 * suit tmpfs, since it may have pages in swapcache, and needs to find those
3401 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3402 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07003403 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
3404 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003405 */
3406struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
3407 pgoff_t index, gfp_t gfp)
3408{
Hugh Dickins68da9f02011-07-25 17:12:34 -07003409#ifdef CONFIG_SHMEM
3410 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003411 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07003412 int error;
3413
3414 BUG_ON(mapping->a_ops != &shmem_aops);
3415 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
3416 if (error)
3417 page = ERR_PTR(error);
3418 else
3419 unlock_page(page);
3420 return page;
3421#else
3422 /*
3423 * The tiny !SHMEM case uses ramfs without swap
3424 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003425 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07003426#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003427}
3428EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);