blob: bf771f4288cd10e0633667284ac6221c416bfe2c [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>
Hugh Dickinscaefba12009-04-13 14:40:12 -070028#include <linux/pagemap.h>
Matt Mackall853ac432009-01-06 14:40:20 -080029#include <linux/file.h>
30#include <linux/mm.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040031#include <linux/export.h>
Matt Mackall853ac432009-01-06 14:40:20 -080032#include <linux/swap.h>
33
34static struct vfsmount *shm_mnt;
35
36#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * This virtual memory filesystem is heavily based on the ramfs. It
39 * extends ramfs by the ability to use swap and honor resource limits
40 * which makes it a completely usable filesystem.
41 */
42
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070043#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070044#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010045#include <linux/posix_acl.h>
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070046#include <linux/generic_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/string.h>
49#include <linux/slab.h>
50#include <linux/backing-dev.h>
51#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070054#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070055#include <linux/percpu_counter.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070056#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/security.h>
58#include <linux/swapops.h>
59#include <linux/mempolicy.h>
60#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000061#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070062#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070063#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080064#include <linux/seq_file.h>
David Herrmannd9bba3a2014-08-08 14:25:29 -070065#include <linux/syscalls.h>
Mimi Zohar92562922008-10-07 14:00:12 -040066#include <linux/magic.h>
David Herrmannd9bba3a2014-08-08 14:25:29 -070067#include <uapi/linux/memfd.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <asm/pgtable.h>
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* Pretend that each entry is of this size in directory's i_size */
76#define BOGO_DIRENT_SIZE 20
77
Hugh Dickins69f07ec2011-08-03 16:21:26 -070078/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
79#define SHORT_SYMLINK_LEN 128
80
Hugh Dickinsc4a92842014-06-23 13:22:06 -070081/*
82 * vmtruncate_range() communicates with shmem_fault via
83 * inode->i_private (with i_mutex making sure that it has only one user at
84 * a time): we would prefer not to enlarge the shmem inode just for that.
85 */
86struct shmem_falloc {
Hugh Dickins757e96f2014-07-23 14:00:10 -070087 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickinsc4a92842014-06-23 13:22:06 -070088 pgoff_t start; /* start of range currently being fallocated */
89 pgoff_t next; /* the next page offset to be fallocated */
90};
91
Eric Parisb09e0fa2011-05-24 17:12:39 -070092struct shmem_xattr {
93 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
94 char *name; /* xattr name */
95 size_t size;
96 char value[0];
97};
98
Hugh Dickins285b2c42011-08-03 16:21:20 -070099/* Flag allocation requirements to shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100enum sgp_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 SGP_READ, /* don't exceed i_size, don't allocate page */
102 SGP_CACHE, /* don't exceed i_size, may allocate page */
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -0800103 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 SGP_WRITE, /* may exceed i_size, may allocate page */
105};
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 Dickins68da9f02011-07-25 17:12:34 -0700119static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
120 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
121
122static inline int shmem_getpage(struct inode *inode, pgoff_t index,
123 struct page **pagep, enum sgp_type sgp, int *fault_type)
124{
125 return shmem_getpage_gfp(inode, index, pagep, sgp,
126 mapping_gfp_mask(inode->i_mapping), fault_type);
127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
130{
131 return sb->s_fs_info;
132}
133
134/*
135 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
136 * for shared memory and for shared anonymous (/dev/zero) mappings
137 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
138 * consistent with the pre-accounting of private mappings ...
139 */
140static inline int shmem_acct_size(unsigned long flags, loff_t size)
141{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000142 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000143 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146static inline void shmem_unacct_size(unsigned long flags, loff_t size)
147{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000148 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 vm_unacct_memory(VM_ACCT(size));
150}
151
152/*
153 * ... whereas tmpfs objects are accounted incrementally as
154 * pages are allocated, in order to allow huge sparse files.
155 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
156 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
157 */
158static inline int shmem_acct_block(unsigned long flags)
159{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000160 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000161 security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164static inline void shmem_unacct_blocks(unsigned long flags, long pages)
165{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000166 if (flags & VM_NORESERVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
168}
169
Hugh Dickins759b9772007-03-05 00:30:28 -0800170static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700171static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800172static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800173static const struct inode_operations shmem_inode_operations;
174static const struct inode_operations shmem_dir_inode_operations;
175static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400176static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700178static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .ra_pages = 0, /* No readahead */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700180 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
183static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800184static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800186static int shmem_reserve_inode(struct super_block *sb)
187{
188 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
189 if (sbinfo->max_inodes) {
190 spin_lock(&sbinfo->stat_lock);
191 if (!sbinfo->free_inodes) {
192 spin_unlock(&sbinfo->stat_lock);
193 return -ENOSPC;
194 }
195 sbinfo->free_inodes--;
196 spin_unlock(&sbinfo->stat_lock);
197 }
198 return 0;
199}
200
201static void shmem_free_inode(struct super_block *sb)
202{
203 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
204 if (sbinfo->max_inodes) {
205 spin_lock(&sbinfo->stat_lock);
206 sbinfo->free_inodes++;
207 spin_unlock(&sbinfo->stat_lock);
208 }
209}
210
Randy Dunlap46711812008-03-19 17:00:41 -0700211/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700212 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * @inode: inode to recalc
214 *
215 * We have to calculate the free blocks since the mm can drop
216 * undirtied hole pages behind our back.
217 *
218 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
219 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
220 *
221 * It has to be called with the spinlock held.
222 */
223static void shmem_recalc_inode(struct inode *inode)
224{
225 struct shmem_inode_info *info = SHMEM_I(inode);
226 long freed;
227
228 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
229 if (freed > 0) {
Hugh Dickins54af6042011-08-03 16:21:24 -0700230 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
231 if (sbinfo->max_blocks)
232 percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 info->alloced -= freed;
Hugh Dickins54af6042011-08-03 16:21:24 -0700234 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 shmem_unacct_blocks(info->flags, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237}
238
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700239/*
240 * Replace item expected in radix tree by a new item, while holding tree lock.
241 */
242static int shmem_radix_tree_replace(struct address_space *mapping,
243 pgoff_t index, void *expected, void *replacement)
244{
245 void **pslot;
246 void *item = NULL;
247
248 VM_BUG_ON(!expected);
249 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
250 if (pslot)
251 item = radix_tree_deref_slot_protected(pslot,
252 &mapping->tree_lock);
253 if (item != expected)
254 return -ENOENT;
255 if (replacement)
256 radix_tree_replace_slot(pslot, replacement);
257 else
258 radix_tree_delete(&mapping->page_tree, index);
259 return 0;
260}
261
262/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700263 * Like add_to_page_cache_locked, but error if expected item has gone.
264 */
265static int shmem_add_to_page_cache(struct page *page,
266 struct address_space *mapping,
267 pgoff_t index, gfp_t gfp, void *expected)
268{
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700269 int error = 0;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700270
271 VM_BUG_ON(!PageLocked(page));
272 VM_BUG_ON(!PageSwapBacked(page));
273
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700274 if (!expected)
275 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
276 if (!error) {
277 page_cache_get(page);
278 page->mapping = mapping;
279 page->index = index;
280
281 spin_lock_irq(&mapping->tree_lock);
282 if (!expected)
283 error = radix_tree_insert(&mapping->page_tree,
284 index, page);
285 else
286 error = shmem_radix_tree_replace(mapping, index,
287 expected, page);
288 if (!error) {
289 mapping->nrpages++;
290 __inc_zone_page_state(page, NR_FILE_PAGES);
291 __inc_zone_page_state(page, NR_SHMEM);
292 spin_unlock_irq(&mapping->tree_lock);
293 } else {
294 page->mapping = NULL;
295 spin_unlock_irq(&mapping->tree_lock);
296 page_cache_release(page);
297 }
298 if (!expected)
299 radix_tree_preload_end();
300 }
301 if (error)
302 mem_cgroup_uncharge_cache_page(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700303 return error;
304}
305
306/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700307 * Like delete_from_page_cache, but substitutes swap for page.
308 */
309static void shmem_delete_from_page_cache(struct page *page, void *radswap)
310{
311 struct address_space *mapping = page->mapping;
312 int error;
313
314 spin_lock_irq(&mapping->tree_lock);
315 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
316 page->mapping = NULL;
317 mapping->nrpages--;
318 __dec_zone_page_state(page, NR_FILE_PAGES);
319 __dec_zone_page_state(page, NR_SHMEM);
320 spin_unlock_irq(&mapping->tree_lock);
321 page_cache_release(page);
322 BUG_ON(error);
323}
324
325/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700326 * Like find_get_pages, but collecting swap entries as well as pages.
327 */
328static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
329 pgoff_t start, unsigned int nr_pages,
330 struct page **pages, pgoff_t *indices)
331{
332 unsigned int i;
333 unsigned int ret;
334 unsigned int nr_found;
335
336 rcu_read_lock();
337restart:
338 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
339 (void ***)pages, indices, start, nr_pages);
340 ret = 0;
341 for (i = 0; i < nr_found; i++) {
342 struct page *page;
343repeat:
344 page = radix_tree_deref_slot((void **)pages[i]);
345 if (unlikely(!page))
346 continue;
347 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700348 if (radix_tree_deref_retry(page))
349 goto restart;
350 /*
351 * Otherwise, we must be storing a swap entry
352 * here as an exceptional entry: so return it
353 * without attempting to raise page count.
354 */
355 goto export;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700356 }
357 if (!page_cache_get_speculative(page))
358 goto repeat;
359
360 /* Has the page moved? */
361 if (unlikely(page != *((void **)pages[i]))) {
362 page_cache_release(page);
363 goto repeat;
364 }
365export:
366 indices[ret] = indices[i];
367 pages[ret] = page;
368 ret++;
369 }
370 if (unlikely(!ret && nr_found))
371 goto restart;
372 rcu_read_unlock();
373 return ret;
374}
375
376/*
377 * Remove swap entry from radix tree, free the swap and its page cache.
378 */
379static int shmem_free_swap(struct address_space *mapping,
380 pgoff_t index, void *radswap)
381{
382 int error;
383
384 spin_lock_irq(&mapping->tree_lock);
385 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
386 spin_unlock_irq(&mapping->tree_lock);
387 if (!error)
388 free_swap_and_cache(radix_to_swp_entry(radswap));
389 return error;
390}
391
392/*
393 * Pagevec may contain swap entries, so shuffle up pages before releasing.
394 */
Hugh Dickins24513262012-01-20 14:34:21 -0800395static void shmem_deswap_pagevec(struct pagevec *pvec)
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700396{
397 int i, j;
398
399 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
400 struct page *page = pvec->pages[i];
401 if (!radix_tree_exceptional_entry(page))
402 pvec->pages[j++] = page;
403 }
404 pvec->nr = j;
Hugh Dickins24513262012-01-20 14:34:21 -0800405}
406
407/*
408 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
409 */
410void shmem_unlock_mapping(struct address_space *mapping)
411{
412 struct pagevec pvec;
413 pgoff_t indices[PAGEVEC_SIZE];
414 pgoff_t index = 0;
415
416 pagevec_init(&pvec, 0);
417 /*
418 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
419 */
420 while (!mapping_unevictable(mapping)) {
421 /*
422 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
423 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
424 */
425 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
426 PAGEVEC_SIZE, pvec.pages, indices);
427 if (!pvec.nr)
428 break;
429 index = indices[pvec.nr - 1] + 1;
430 shmem_deswap_pagevec(&pvec);
431 check_move_unevictable_pages(pvec.pages, pvec.nr);
432 pagevec_release(&pvec);
433 cond_resched();
434 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700435}
436
437/*
438 * Remove range of pages and swap entries from radix tree, and free them.
439 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700440void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700442 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700444 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700445 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700446 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700447 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700448 pgoff_t indices[PAGEVEC_SIZE];
449 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700450 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700451 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700453 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
454
455 pagevec_init(&pvec, 0);
456 index = start;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700457 while (index <= end) {
458 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
459 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
460 pvec.pages, indices);
461 if (!pvec.nr)
462 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700463 mem_cgroup_uncharge_start();
464 for (i = 0; i < pagevec_count(&pvec); i++) {
465 struct page *page = pvec.pages[i];
466
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700467 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700468 if (index > end)
469 break;
470
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700471 if (radix_tree_exceptional_entry(page)) {
472 nr_swaps_freed += !shmem_free_swap(mapping,
473 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700474 continue;
475 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700476
477 if (!trylock_page(page))
478 continue;
479 if (page->mapping == mapping) {
480 VM_BUG_ON(PageWriteback(page));
481 truncate_inode_page(mapping, page);
482 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700483 unlock_page(page);
484 }
Hugh Dickins24513262012-01-20 14:34:21 -0800485 shmem_deswap_pagevec(&pvec);
486 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700487 mem_cgroup_uncharge_end();
488 cond_resched();
489 index++;
490 }
491
492 if (partial) {
493 struct page *page = NULL;
494 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
495 if (page) {
496 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
497 set_page_dirty(page);
498 unlock_page(page);
499 page_cache_release(page);
500 }
501 }
502
503 index = start;
Hugh Dickinscf655052014-07-23 14:00:13 -0700504 while (index <= end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700505 cond_resched();
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700506 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
507 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
508 pvec.pages, indices);
509 if (!pvec.nr) {
Hugh Dickinscf655052014-07-23 14:00:13 -0700510 /* If all gone or hole-punch, we're done */
511 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700512 break;
Hugh Dickinscf655052014-07-23 14:00:13 -0700513 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700514 index = start;
515 continue;
516 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700517 mem_cgroup_uncharge_start();
518 for (i = 0; i < pagevec_count(&pvec); i++) {
519 struct page *page = pvec.pages[i];
520
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700521 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700522 if (index > end)
523 break;
524
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700525 if (radix_tree_exceptional_entry(page)) {
Hugh Dickinscf655052014-07-23 14:00:13 -0700526 if (shmem_free_swap(mapping, index, page)) {
527 /* Swap was replaced by page: retry */
528 index--;
529 break;
530 }
531 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700532 continue;
533 }
534
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700535 lock_page(page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700536 if (page->mapping == mapping) {
537 VM_BUG_ON(PageWriteback(page));
538 truncate_inode_page(mapping, page);
Hugh Dickinscf655052014-07-23 14:00:13 -0700539 } else {
540 /* Page was replaced by swap: retry */
541 unlock_page(page);
542 index--;
543 break;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700544 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700545 unlock_page(page);
546 }
Hugh Dickins24513262012-01-20 14:34:21 -0800547 shmem_deswap_pagevec(&pvec);
548 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700549 mem_cgroup_uncharge_end();
550 index++;
551 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700554 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 shmem_recalc_inode(inode);
556 spin_unlock(&info->lock);
557
Hugh Dickins285b2c42011-08-03 16:21:20 -0700558 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700560EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Hugh Dickins94c1e622011-06-27 16:18:03 -0700562static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int error;
566
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200567 error = inode_change_ok(inode, attr);
568 if (error)
569 return error;
570
Hugh Dickins94c1e622011-06-27 16:18:03 -0700571 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
572 loff_t oldsize = inode->i_size;
573 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000574
Hugh Dickins94c1e622011-06-27 16:18:03 -0700575 if (newsize != oldsize) {
576 i_size_write(inode, newsize);
577 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
578 }
579 if (newsize < oldsize) {
580 loff_t holebegin = round_up(newsize, PAGE_SIZE);
581 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
582 shmem_truncate_range(inode, newsize, (loff_t)-1);
583 /* unmap again to remove racily COWed private pages */
584 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200588 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700589#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200590 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100591 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700592#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return error;
594}
595
Al Viro1f895f72010-06-05 19:10:41 -0400596static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700599 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000601 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 shmem_unacct_size(info->flags, inode->i_size);
603 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000604 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800606 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800608 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Hugh Dickins69f07ec2011-08-03 16:21:26 -0700610 } else
611 kfree(info->symlink);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700612
613 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
614 kfree(xattr->name);
615 kfree(xattr);
616 }
Hugh Dickins49917cc2012-11-16 14:15:04 -0800617 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800618 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400619 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700622/*
623 * If swap found in inode, free it and move page from swapcache to filecache.
624 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700625static int shmem_unuse_inode(struct shmem_inode_info *info,
626 swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700628 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700629 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700630 pgoff_t index;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800631 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700633 radswap = swp_to_radix_entry(swap);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700634 index = radix_tree_locate_item(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700635 if (index == -1)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700636 return 0;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800637
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800638 /*
639 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400640 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800641 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700642 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800643 */
644 if (shmem_swaplist.next != &info->swaplist)
645 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800646
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800647 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700648 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
649 * but also to hold up shmem_evict_inode(): so inode cannot be freed
650 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800651 */
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700652 error = shmem_add_to_page_cache(page, mapping, index,
653 GFP_NOWAIT, radswap);
Hugh Dickins778dd892011-05-11 15:13:37 -0700654 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700655
Hugh Dickins48f170f2011-07-25 17:12:37 -0700656 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700657 /*
658 * Truncation and eviction use free_swap_and_cache(), which
659 * only does trylock page: if we raced, best clean up here.
660 */
Hugh Dickins73b12622008-02-04 22:28:50 -0800661 delete_from_swap_cache(page);
662 set_page_dirty(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700663 if (!error) {
664 spin_lock(&info->lock);
665 info->swapped--;
666 spin_unlock(&info->lock);
667 swap_free(swap);
668 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800669 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800671 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700675 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700677int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700679 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct shmem_inode_info *info;
681 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700682 int error;
683
684 /*
685 * Charge page using GFP_KERNEL while we can wait, before taking
686 * the shmem_swaplist_mutex which might hold up shmem_writepage().
687 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -0700688 */
689 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
690 if (error)
691 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700692 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800694 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700695 list_for_each_safe(this, next, &shmem_swaplist) {
696 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700697 if (info->swapped)
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700698 found = shmem_unuse_inode(info, swap, page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700699 else
700 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800701 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800702 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700703 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800705 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700706
Hugh Dickins778dd892011-05-11 15:13:37 -0700707 if (!found)
708 mem_cgroup_uncharge_cache_page(page);
709 if (found < 0)
710 error = found;
711out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800712 unlock_page(page);
713 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700714 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717/*
718 * Move the page from the page cache to the swap cache.
719 */
720static int shmem_writepage(struct page *page, struct writeback_control *wbc)
721{
722 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700725 swp_entry_t swap;
726 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 mapping = page->mapping;
730 index = page->index;
731 inode = mapping->host;
732 info = SHMEM_I(inode);
733 if (info->flags & VM_LOCKED)
734 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800735 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 goto redirty;
737
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800738 /*
739 * shmem_backing_dev_info's capabilities prevent regular writeback or
740 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700741 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800742 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700743 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800744 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700745 if (!wbc->for_reclaim) {
746 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
747 goto redirty;
748 }
749 swap = get_swap_page();
750 if (!swap.val)
751 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800752
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700753 /*
754 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700755 * if it's not already there. Do it now before the page is
756 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700757 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700758 * we've incremented swapped, because shmem_unuse_inode() will
759 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700760 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700761 mutex_lock(&shmem_swaplist_mutex);
762 if (list_empty(&info->swaplist))
763 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700764
Hugh Dickins48f170f2011-07-25 17:12:37 -0700765 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800766 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700767 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
768
769 spin_lock(&info->lock);
770 info->swapped++;
771 shmem_recalc_inode(inode);
Hugh Dickins826267c2011-05-28 13:14:09 -0700772 spin_unlock(&info->lock);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700773
774 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800775 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700776 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778 }
779
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700780 mutex_unlock(&shmem_swaplist_mutex);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700781 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782redirty:
783 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800784 if (wbc->for_reclaim)
785 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
786 unlock_page(page);
787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800791#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700792static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800793{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700794 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800795
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700796 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700797 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800798
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700799 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800800
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700801 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800802}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700803
804static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
805{
806 struct mempolicy *mpol = NULL;
807 if (sbinfo->mpol) {
808 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
809 mpol = sbinfo->mpol;
810 mpol_get(mpol);
811 spin_unlock(&sbinfo->stat_lock);
812 }
813 return mpol;
814}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800815#endif /* CONFIG_TMPFS */
816
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700817static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
818 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct vm_area_struct pvma;
Mel Gorman3d1170f2012-12-05 14:01:41 -0800821 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800823 /* Create a pseudo vma that just contains the policy */
824 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700825 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800826 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700827 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700828
Mel Gorman3d1170f2012-12-05 14:01:41 -0800829 page = swapin_readahead(swap, gfp, &pvma, 0);
830
831 /* Drop reference taken by mpol_shared_policy_lookup() */
832 mpol_cond_put(pvma.vm_policy);
833
834 return page;
835}
836
837static struct page *shmem_alloc_page(gfp_t gfp,
838 struct shmem_inode_info *info, pgoff_t index)
839{
840 struct vm_area_struct pvma;
841 struct page *page;
842
843 /* Create a pseudo vma that just contains the policy */
844 pvma.vm_start = 0;
845 pvma.vm_pgoff = index;
846 pvma.vm_ops = NULL;
847 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
848
849 page = alloc_page_vma(gfp, &pvma, 0);
850
851 /* Drop reference taken by mpol_shared_policy_lookup() */
852 mpol_cond_put(pvma.vm_policy);
853
854 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800856#else /* !CONFIG_NUMA */
857#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700858static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800859{
860}
861#endif /* CONFIG_TMPFS */
862
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700863static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
864 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700866 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Hugh Dickins02098fe2008-02-04 22:28:42 -0800869static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700870 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000872 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800874#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700876#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
877static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
878{
879 return NULL;
880}
881#endif
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700884 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 *
886 * If we allocate a new one we do not mark it dirty. That's up to the
887 * vm. If we swap it in we mark it dirty since we also free the swap
888 * entry since a page cannot live in both the swap and page cache
889 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700890static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -0700891 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af6042011-08-03 16:21:24 -0700894 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700896 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 swp_entry_t swap;
898 int error;
Hugh Dickins54af6042011-08-03 16:21:24 -0700899 int once = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700901 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903repeat:
Hugh Dickins54af6042011-08-03 16:21:24 -0700904 swap.val = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700905 page = find_lock_page(mapping, index);
Hugh Dickins54af6042011-08-03 16:21:24 -0700906 if (radix_tree_exceptional_entry(page)) {
907 swap = radix_to_swp_entry(page);
908 page = NULL;
909 }
910
911 if (sgp != SGP_WRITE &&
912 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
913 error = -EINVAL;
914 goto failed;
915 }
916
917 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800918 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700919 * Once we can get the page lock, it must be uptodate:
920 * if there were an error in reading back from swap,
921 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800922 */
Hugh Dickins54af6042011-08-03 16:21:24 -0700923 BUG_ON(page && !PageUptodate(page));
924 *pagep = page;
925 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /*
Hugh Dickins54af6042011-08-03 16:21:24 -0700929 * Fast cache lookup did not find it:
930 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
Hugh Dickins54af6042011-08-03 16:21:24 -0700932 info = SHMEM_I(inode);
933 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (swap.val) {
936 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700937 page = lookup_swap_cache(swap);
938 if (!page) {
Ying Han456f9982011-05-26 16:25:38 -0700939 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700940 if (fault_type)
941 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700942 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700943 if (!page) {
Hugh Dickins54af6042011-08-03 16:21:24 -0700944 error = -ENOMEM;
945 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
949 /* We have to do this with page locked to prevent races */
Hugh Dickins54af6042011-08-03 16:21:24 -0700950 lock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700951 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 error = -EIO;
Hugh Dickins54af6042011-08-03 16:21:24 -0700953 goto failed;
954 }
955 wait_on_page_writeback(page);
956
957 /* Someone may have already done it for us */
958 if (page->mapping) {
959 if (page->mapping == mapping &&
960 page->index == index)
961 goto done;
962 error = -EEXIST;
963 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700966 error = mem_cgroup_cache_charge(page, current->mm,
967 gfp & GFP_RECLAIM_MASK);
968 if (!error)
969 error = shmem_add_to_page_cache(page, mapping, index,
970 gfp, swp_to_radix_entry(swap));
Hugh Dickins54af6042011-08-03 16:21:24 -0700971 if (error)
972 goto failed;
973
974 spin_lock(&info->lock);
975 info->swapped--;
976 shmem_recalc_inode(inode);
977 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700978
Hugh Dickins27ab7002011-07-25 17:12:36 -0700979 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700980 set_page_dirty(page);
981 swap_free(swap);
982
Hugh Dickins54af6042011-08-03 16:21:24 -0700983 } else {
984 if (shmem_acct_block(info->flags)) {
985 error = -ENOSPC;
986 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700988 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700989 if (percpu_counter_compare(&sbinfo->used_blocks,
Hugh Dickins54af6042011-08-03 16:21:24 -0700990 sbinfo->max_blocks) >= 0) {
991 error = -ENOSPC;
992 goto unacct;
993 }
Tim Chen7e496292010-08-09 17:19:05 -0700994 percpu_counter_inc(&sbinfo->used_blocks);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Hugh Dickins54af6042011-08-03 16:21:24 -0700997 page = shmem_alloc_page(gfp, info, index);
998 if (!page) {
999 error = -ENOMEM;
1000 goto decused;
1001 }
1002
1003 SetPageSwapBacked(page);
1004 __set_page_locked(page);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07001005 error = mem_cgroup_cache_charge(page, current->mm,
1006 gfp & GFP_RECLAIM_MASK);
1007 if (!error)
1008 error = shmem_add_to_page_cache(page, mapping, index,
1009 gfp, NULL);
Hugh Dickins54af6042011-08-03 16:21:24 -07001010 if (error)
1011 goto decused;
1012 lru_cache_add_anon(page);
1013
1014 spin_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 info->alloced++;
Hugh Dickins54af6042011-08-03 16:21:24 -07001016 inode->i_blocks += BLOCKS_PER_PAGE;
1017 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 spin_unlock(&info->lock);
Hugh Dickins54af6042011-08-03 16:21:24 -07001019
Hugh Dickins27ab7002011-07-25 17:12:36 -07001020 clear_highpage(page);
1021 flush_dcache_page(page);
1022 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001023 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -07001024 set_page_dirty(page);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026done:
Hugh Dickins54af6042011-08-03 16:21:24 -07001027 /* Perhaps the file has been truncated since we checked */
1028 if (sgp != SGP_WRITE &&
1029 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1030 error = -EINVAL;
1031 goto trunc;
Shaohua Liff36b802010-08-09 17:19:06 -07001032 }
Hugh Dickins54af6042011-08-03 16:21:24 -07001033 *pagep = page;
1034 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001035
Nick Piggind0217ac2007-07-19 01:47:03 -07001036 /*
Hugh Dickins54af6042011-08-03 16:21:24 -07001037 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 */
Hugh Dickins54af6042011-08-03 16:21:24 -07001039trunc:
1040 ClearPageDirty(page);
1041 delete_from_page_cache(page);
1042 spin_lock(&info->lock);
1043 info->alloced--;
1044 inode->i_blocks -= BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 spin_unlock(&info->lock);
Hugh Dickins54af6042011-08-03 16:21:24 -07001046decused:
1047 if (sbinfo->max_blocks)
1048 percpu_counter_add(&sbinfo->used_blocks, -1);
1049unacct:
1050 shmem_unacct_blocks(info->flags, 1);
1051failed:
1052 if (swap.val && error != -EINVAL) {
1053 struct page *test = find_get_page(mapping, index);
1054 if (test && !radix_tree_exceptional_entry(test))
1055 page_cache_release(test);
1056 /* Have another try if the entry has changed */
1057 if (test != swp_to_radix_entry(swap))
1058 error = -EEXIST;
1059 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001060 if (page) {
Hugh Dickins54af6042011-08-03 16:21:24 -07001061 unlock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001062 page_cache_release(page);
Hugh Dickins54af6042011-08-03 16:21:24 -07001063 }
1064 if (error == -ENOSPC && !once++) {
1065 info = SHMEM_I(inode);
1066 spin_lock(&info->lock);
1067 shmem_recalc_inode(inode);
1068 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001069 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001070 }
Hugh Dickins54af6042011-08-03 16:21:24 -07001071 if (error == -EEXIST)
1072 goto repeat;
1073 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1077{
1078 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1079 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001080 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001082 /*
1083 * Trinity finds that probing a hole which tmpfs is punching can
1084 * prevent the hole-punch from ever completing: which in turn
1085 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins757e96f2014-07-23 14:00:10 -07001086 * faulting pages into the hole while it's being punched. Although
1087 * shmem_truncate_range() does remove the additions, it may be unable to
1088 * keep up, as each new page needs its own unmap_mapping_range() call,
1089 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1090 *
1091 * It does not matter if we sometimes reach this check just before the
1092 * hole-punch begins, so that one fault then races with the punch:
1093 * we just need to make racing faults a rare case.
1094 *
1095 * The implementation below would be much simpler if we just used a
1096 * standard mutex or completion: but we cannot take i_mutex in fault,
1097 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001098 */
1099 if (unlikely(inode->i_private)) {
1100 struct shmem_falloc *shmem_falloc;
1101
1102 spin_lock(&inode->i_lock);
1103 shmem_falloc = inode->i_private;
Hugh Dickins757e96f2014-07-23 14:00:10 -07001104 if (shmem_falloc &&
1105 vmf->pgoff >= shmem_falloc->start &&
1106 vmf->pgoff < shmem_falloc->next) {
1107 wait_queue_head_t *shmem_falloc_waitq;
1108 DEFINE_WAIT(shmem_fault_wait);
1109
1110 ret = VM_FAULT_NOPAGE;
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001111 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1112 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins757e96f2014-07-23 14:00:10 -07001113 /* It's polite to up mmap_sem if we can */
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001114 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins757e96f2014-07-23 14:00:10 -07001115 ret = VM_FAULT_RETRY;
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001116 }
Hugh Dickins757e96f2014-07-23 14:00:10 -07001117
1118 shmem_falloc_waitq = shmem_falloc->waitq;
1119 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1120 TASK_UNINTERRUPTIBLE);
1121 spin_unlock(&inode->i_lock);
1122 schedule();
1123
1124 /*
1125 * shmem_falloc_waitq points into the vmtruncate_range()
1126 * stack of the hole-punching task: shmem_falloc_waitq
1127 * is usually invalid by the time we reach here, but
1128 * finish_wait() does not dereference it in that case;
1129 * though i_lock needed lest racing with wake_up_all().
1130 */
1131 spin_lock(&inode->i_lock);
1132 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
1133 spin_unlock(&inode->i_lock);
1134 return ret;
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001135 }
Hugh Dickins757e96f2014-07-23 14:00:10 -07001136 spin_unlock(&inode->i_lock);
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001137 }
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1140 if (error)
1141 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001142
Ying Han456f9982011-05-26 16:25:38 -07001143 if (ret & VM_FAULT_MAJOR) {
1144 count_vm_event(PGMAJFAULT);
1145 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1146 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001147 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001150int vmtruncate_range(struct inode *inode, loff_t lstart, loff_t lend)
1151{
1152 /*
1153 * If the underlying filesystem is not going to provide
1154 * a way to truncate a range of blocks (punch a hole) -
1155 * we should return failure right now.
1156 * Only CONFIG_SHMEM shmem.c ever supported i_op->truncate_range().
1157 */
1158 if (inode->i_op->truncate_range != shmem_truncate_range)
1159 return -ENOSYS;
1160
1161 mutex_lock(&inode->i_mutex);
1162 {
1163 struct shmem_falloc shmem_falloc;
1164 struct address_space *mapping = inode->i_mapping;
1165 loff_t unmap_start = round_up(lstart, PAGE_SIZE);
1166 loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
Hugh Dickins757e96f2014-07-23 14:00:10 -07001167 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001168
Hugh Dickins757e96f2014-07-23 14:00:10 -07001169 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001170 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
1171 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
1172 spin_lock(&inode->i_lock);
1173 inode->i_private = &shmem_falloc;
1174 spin_unlock(&inode->i_lock);
1175
1176 if ((u64)unmap_end > (u64)unmap_start)
1177 unmap_mapping_range(mapping, unmap_start,
1178 1 + unmap_end - unmap_start, 0);
1179 shmem_truncate_range(inode, lstart, lend);
1180 /* No need to unmap again: hole-punching leaves COWed pages */
1181
1182 spin_lock(&inode->i_lock);
1183 inode->i_private = NULL;
Hugh Dickins757e96f2014-07-23 14:00:10 -07001184 wake_up_all(&shmem_falloc_waitq);
Hugh Dickinsc4a92842014-06-23 13:22:06 -07001185 spin_unlock(&inode->i_lock);
1186 }
1187 mutex_unlock(&inode->i_mutex);
1188 return 0;
1189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001192static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001194 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1195 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001198static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1199 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001201 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1202 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001204 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1205 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207#endif
1208
1209int shmem_lock(struct file *file, int lock, struct user_struct *user)
1210{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001211 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 struct shmem_inode_info *info = SHMEM_I(inode);
1213 int retval = -ENOMEM;
1214
1215 spin_lock(&info->lock);
1216 if (lock && !(info->flags & VM_LOCKED)) {
1217 if (!user_shm_lock(inode->i_size, user))
1218 goto out_nomem;
1219 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001220 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 if (!lock && (info->flags & VM_LOCKED) && user) {
1223 user_shm_unlock(inode->i_size, user);
1224 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001225 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229out_nomem:
1230 spin_unlock(&info->lock);
1231 return retval;
1232}
1233
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001234static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 file_accessed(file);
1237 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001238 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return 0;
1240}
1241
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001242static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04001243 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 struct inode *inode;
1246 struct shmem_inode_info *info;
1247 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1248
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001249 if (shmem_reserve_inode(sb))
1250 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 inode = new_inode(sb);
1253 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001254 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001255 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1258 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001259 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 info = SHMEM_I(inode);
1261 memset(info, 0, (char *)inode - (char *)info);
1262 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001263 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001265 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001266 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 switch (mode & S_IFMT) {
1269 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001270 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 init_special_inode(inode, mode, dev);
1272 break;
1273 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001274 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 inode->i_op = &shmem_inode_operations;
1276 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001277 mpol_shared_policy_init(&info->policy,
1278 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001281 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /* Some things misbehave if size == 0 on a directory */
1283 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1284 inode->i_op = &shmem_dir_inode_operations;
1285 inode->i_fop = &simple_dir_operations;
1286 break;
1287 case S_IFLNK:
1288 /*
1289 * Must not load anything in the rbtree,
1290 * mpol_free_shared_policy will not be called.
1291 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001292 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
1294 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001295 } else
1296 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return inode;
1298}
1299
1300#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001301static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001302static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001304#ifdef CONFIG_TMPFS_XATTR
1305static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1306#else
1307#define shmem_initxattrs NULL
1308#endif
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001311shmem_write_begin(struct file *file, struct address_space *mapping,
1312 loff_t pos, unsigned len, unsigned flags,
1313 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Nick Piggin800d15a2007-10-16 01:25:03 -07001315 struct inode *inode = mapping->host;
1316 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -07001317 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1318}
1319
1320static int
1321shmem_write_end(struct file *file, struct address_space *mapping,
1322 loff_t pos, unsigned len, unsigned copied,
1323 struct page *page, void *fsdata)
1324{
1325 struct inode *inode = mapping->host;
1326
Hugh Dickinsd3602442008-02-04 22:28:44 -08001327 if (pos + copied > inode->i_size)
1328 i_size_write(inode, pos + copied);
1329
Nick Piggin800d15a2007-10-16 01:25:03 -07001330 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001331 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001332 page_cache_release(page);
1333
Nick Piggin800d15a2007-10-16 01:25:03 -07001334 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1338{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001339 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001341 pgoff_t index;
1342 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001343 enum sgp_type sgp = SGP_READ;
1344
1345 /*
1346 * Might this read be for a stacking filesystem? Then when reading
1347 * holes of a sparse file, we actually need to allocate those pages,
1348 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1349 */
1350 if (segment_eq(get_fs(), KERNEL_DS))
1351 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 index = *ppos >> PAGE_CACHE_SHIFT;
1354 offset = *ppos & ~PAGE_CACHE_MASK;
1355
1356 for (;;) {
1357 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001358 pgoff_t end_index;
1359 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 loff_t i_size = i_size_read(inode);
1361
1362 end_index = i_size >> PAGE_CACHE_SHIFT;
1363 if (index > end_index)
1364 break;
1365 if (index == end_index) {
1366 nr = i_size & ~PAGE_CACHE_MASK;
1367 if (nr <= offset)
1368 break;
1369 }
1370
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001371 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (desc->error) {
1373 if (desc->error == -EINVAL)
1374 desc->error = 0;
1375 break;
1376 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001377 if (page)
1378 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /*
1381 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001382 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
1384 nr = PAGE_CACHE_SIZE;
1385 i_size = i_size_read(inode);
1386 end_index = i_size >> PAGE_CACHE_SHIFT;
1387 if (index == end_index) {
1388 nr = i_size & ~PAGE_CACHE_MASK;
1389 if (nr <= offset) {
1390 if (page)
1391 page_cache_release(page);
1392 break;
1393 }
1394 }
1395 nr -= offset;
1396
1397 if (page) {
1398 /*
1399 * If users can be writing to this page using arbitrary
1400 * virtual addresses, take care about potential aliasing
1401 * before reading the page on the kernel side.
1402 */
1403 if (mapping_writably_mapped(mapping))
1404 flush_dcache_page(page);
1405 /*
1406 * Mark the page accessed if we read the beginning.
1407 */
1408 if (!offset)
1409 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001410 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001412 page_cache_get(page);
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /*
1416 * Ok, we have the page, and it's up-to-date, so
1417 * now we can copy it to user space...
1418 *
1419 * The actor routine returns how many bytes were actually used..
1420 * NOTE! This may not be the same as how much of a user buffer
1421 * we filled up (we may be padding etc), so we can only update
1422 * "pos" here (the actor routine has to update the user buffer
1423 * pointers and the remaining count).
1424 */
1425 ret = actor(desc, page, offset, nr);
1426 offset += ret;
1427 index += offset >> PAGE_CACHE_SHIFT;
1428 offset &= ~PAGE_CACHE_MASK;
1429
1430 page_cache_release(page);
1431 if (ret != nr || !desc->count)
1432 break;
1433
1434 cond_resched();
1435 }
1436
1437 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1438 file_accessed(filp);
1439}
1440
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001441static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1442 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001444 struct file *filp = iocb->ki_filp;
1445 ssize_t retval;
1446 unsigned long seg;
1447 size_t count;
1448 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001450 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1451 if (retval)
1452 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001454 for (seg = 0; seg < nr_segs; seg++) {
1455 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001457 desc.written = 0;
1458 desc.arg.buf = iov[seg].iov_base;
1459 desc.count = iov[seg].iov_len;
1460 if (desc.count == 0)
1461 continue;
1462 desc.error = 0;
1463 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1464 retval += desc.written;
1465 if (desc.error) {
1466 retval = retval ?: desc.error;
1467 break;
1468 }
1469 if (desc.count > 0)
1470 break;
1471 }
1472 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Hugh Dickins708e3502011-07-25 17:12:32 -07001475static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1476 struct pipe_inode_info *pipe, size_t len,
1477 unsigned int flags)
1478{
1479 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001480 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001481 unsigned int loff, nr_pages, req_pages;
1482 struct page *pages[PIPE_DEF_BUFFERS];
1483 struct partial_page partial[PIPE_DEF_BUFFERS];
1484 struct page *page;
1485 pgoff_t index, end_index;
1486 loff_t isize, left;
1487 int error, page_nr;
1488 struct splice_pipe_desc spd = {
1489 .pages = pages,
1490 .partial = partial,
Eric Dumazet7ba56a52012-06-12 15:24:40 +02001491 .nr_pages_max = PIPE_DEF_BUFFERS,
Hugh Dickins708e3502011-07-25 17:12:32 -07001492 .flags = flags,
1493 .ops = &page_cache_pipe_buf_ops,
1494 .spd_release = spd_release_page,
1495 };
1496
Hugh Dickins71f0e072011-07-25 17:12:33 -07001497 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001498 if (unlikely(*ppos >= isize))
1499 return 0;
1500
1501 left = isize - *ppos;
1502 if (unlikely(left < len))
1503 len = left;
1504
1505 if (splice_grow_spd(pipe, &spd))
1506 return -ENOMEM;
1507
1508 index = *ppos >> PAGE_CACHE_SHIFT;
1509 loff = *ppos & ~PAGE_CACHE_MASK;
1510 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1511 nr_pages = min(req_pages, pipe->buffers);
1512
Hugh Dickins708e3502011-07-25 17:12:32 -07001513 spd.nr_pages = find_get_pages_contig(mapping, index,
1514 nr_pages, spd.pages);
1515 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001516 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001517
Hugh Dickins708e3502011-07-25 17:12:32 -07001518 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001519 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1520 if (error)
1521 break;
1522 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001523 spd.pages[spd.nr_pages++] = page;
1524 index++;
1525 }
1526
Hugh Dickins708e3502011-07-25 17:12:32 -07001527 index = *ppos >> PAGE_CACHE_SHIFT;
1528 nr_pages = spd.nr_pages;
1529 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001530
Hugh Dickins708e3502011-07-25 17:12:32 -07001531 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1532 unsigned int this_len;
1533
1534 if (!len)
1535 break;
1536
Hugh Dickins708e3502011-07-25 17:12:32 -07001537 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1538 page = spd.pages[page_nr];
1539
Hugh Dickins71f0e072011-07-25 17:12:33 -07001540 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001541 error = shmem_getpage(inode, index, &page,
1542 SGP_CACHE, NULL);
1543 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001544 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001545 unlock_page(page);
1546 page_cache_release(spd.pages[page_nr]);
1547 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001548 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001549
1550 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001551 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1552 if (unlikely(!isize || index > end_index))
1553 break;
1554
Hugh Dickins708e3502011-07-25 17:12:32 -07001555 if (end_index == index) {
1556 unsigned int plen;
1557
Hugh Dickins708e3502011-07-25 17:12:32 -07001558 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1559 if (plen <= loff)
1560 break;
1561
Hugh Dickins708e3502011-07-25 17:12:32 -07001562 this_len = min(this_len, plen - loff);
1563 len = this_len;
1564 }
1565
1566 spd.partial[page_nr].offset = loff;
1567 spd.partial[page_nr].len = this_len;
1568 len -= this_len;
1569 loff = 0;
1570 spd.nr_pages++;
1571 index++;
1572 }
1573
Hugh Dickins708e3502011-07-25 17:12:32 -07001574 while (page_nr < nr_pages)
1575 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001576
1577 if (spd.nr_pages)
1578 error = splice_to_pipe(pipe, &spd);
1579
Eric Dumazet7ba56a52012-06-12 15:24:40 +02001580 splice_shrink_spd(&spd);
Hugh Dickins708e3502011-07-25 17:12:32 -07001581
1582 if (error > 0) {
1583 *ppos += error;
1584 file_accessed(in);
1585 }
1586 return error;
1587}
1588
David Howells726c3342006-06-23 02:02:58 -07001589static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
David Howells726c3342006-06-23 02:02:58 -07001591 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 buf->f_type = TMPFS_MAGIC;
1594 buf->f_bsize = PAGE_CACHE_SIZE;
1595 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001596 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001598 buf->f_bavail =
1599 buf->f_bfree = sbinfo->max_blocks -
1600 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001601 }
1602 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 buf->f_files = sbinfo->max_inodes;
1604 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606 /* else leave those fields 0 like simple_statfs */
1607 return 0;
1608}
1609
1610/*
1611 * File creation. Allocate an inode, and we're done..
1612 */
1613static int
Al Viro1a67aaf2011-07-26 01:52:52 -04001614shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001616 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int error = -ENOSPC;
1618
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001619 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001621 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001622 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001623 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001624 if (error) {
1625 if (error != -EOPNOTSUPP) {
1626 iput(inode);
1627 return error;
1628 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001629 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001630#ifdef CONFIG_TMPFS_POSIX_ACL
1631 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001632 if (error) {
1633 iput(inode);
1634 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001635 }
Al Viro718deb62009-12-16 19:35:36 -05001636#else
1637 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 dir->i_size += BOGO_DIRENT_SIZE;
1640 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1641 d_instantiate(dentry, inode);
1642 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 return error;
1645}
1646
Al Virob0d54302013-06-07 01:20:27 -04001647static int
1648shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
1649{
1650 struct inode *inode;
1651 int error = -ENOSPC;
1652
1653 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
1654 if (inode) {
1655 error = security_inode_init_security(inode, dir,
1656 NULL,
1657 shmem_initxattrs, NULL);
1658 if (error) {
1659 if (error != -EOPNOTSUPP) {
1660 iput(inode);
1661 return error;
1662 }
1663 }
1664#ifdef CONFIG_TMPFS_POSIX_ACL
1665 error = generic_acl_init(inode, dir);
1666 if (error) {
1667 iput(inode);
1668 return error;
1669 }
1670#else
1671 error = 0;
1672#endif
1673 d_tmpfile(dentry, inode);
1674 }
1675 return error;
1676}
1677
Al Viro18bb1db2011-07-26 01:41:39 -04001678static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 int error;
1681
1682 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1683 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001684 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return 0;
1686}
1687
Al Viro4acdaf22011-07-26 01:42:34 -04001688static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro33b02b82012-06-10 18:05:36 -04001689 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1692}
1693
1694/*
1695 * Link a file..
1696 */
1697static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1698{
1699 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001700 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 /*
1703 * No ordinary (disk based) filesystem counts links as inodes;
1704 * but each new link needs a new dentry, pinning lowmem, and
1705 * tmpfs dentries cannot be pruned until they are unlinked.
1706 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001707 ret = shmem_reserve_inode(inode->i_sb);
1708 if (ret)
1709 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 dir->i_size += BOGO_DIRENT_SIZE;
1712 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001713 inc_nlink(inode);
Al Viro7de9c6e2010-10-23 11:11:40 -04001714 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 dget(dentry); /* Extra pinning count for the created dentry */
1716 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001717out:
1718 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
1721static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1722{
1723 struct inode *inode = dentry->d_inode;
1724
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001725 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1726 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 dir->i_size -= BOGO_DIRENT_SIZE;
1729 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001730 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 dput(dentry); /* Undo the count from "create" - this does all the work */
1732 return 0;
1733}
1734
1735static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1736{
1737 if (!simple_empty(dentry))
1738 return -ENOTEMPTY;
1739
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001740 drop_nlink(dentry->d_inode);
1741 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return shmem_unlink(dir, dentry);
1743}
1744
1745/*
1746 * The VFS layer already does all the dentry stuff for rename,
1747 * we just have to decrement the usage count for the target if
1748 * it exists so that the VFS layer correctly free's it when it
1749 * gets overwritten.
1750 */
1751static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1752{
1753 struct inode *inode = old_dentry->d_inode;
1754 int they_are_dirs = S_ISDIR(inode->i_mode);
1755
1756 if (!simple_empty(new_dentry))
1757 return -ENOTEMPTY;
1758
1759 if (new_dentry->d_inode) {
1760 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredi1b526fb2014-09-24 17:56:17 +02001761 if (they_are_dirs) {
1762 drop_nlink(new_dentry->d_inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001763 drop_nlink(old_dir);
Miklos Szeredi1b526fb2014-09-24 17:56:17 +02001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001766 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001767 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769
1770 old_dir->i_size -= BOGO_DIRENT_SIZE;
1771 new_dir->i_size += BOGO_DIRENT_SIZE;
1772 old_dir->i_ctime = old_dir->i_mtime =
1773 new_dir->i_ctime = new_dir->i_mtime =
1774 inode->i_ctime = CURRENT_TIME;
1775 return 0;
1776}
1777
1778static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1779{
1780 int error;
1781 int len;
1782 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001783 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 char *kaddr;
1785 struct shmem_inode_info *info;
1786
1787 len = strlen(symname) + 1;
1788 if (len > PAGE_CACHE_SIZE)
1789 return -ENAMETOOLONG;
1790
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001791 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (!inode)
1793 return -ENOSPC;
1794
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001795 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001796 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001797 if (error) {
1798 if (error != -EOPNOTSUPP) {
1799 iput(inode);
1800 return error;
1801 }
1802 error = 0;
1803 }
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 info = SHMEM_I(inode);
1806 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001807 if (len <= SHORT_SYMLINK_LEN) {
1808 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1809 if (!info->symlink) {
1810 iput(inode);
1811 return -ENOMEM;
1812 }
1813 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 } else {
1815 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1816 if (error) {
1817 iput(inode);
1818 return error;
1819 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001820 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 inode->i_op = &shmem_symlink_inode_operations;
Cong Wang9b04c5f2011-11-25 23:14:39 +08001822 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 memcpy(kaddr, symname, len);
Cong Wang9b04c5f2011-11-25 23:14:39 +08001824 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001826 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 page_cache_release(page);
1828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 dir->i_size += BOGO_DIRENT_SIZE;
1830 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1831 d_instantiate(dentry, inode);
1832 dget(dentry);
1833 return 0;
1834}
1835
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001836static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001838 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001839 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001842static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001845 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1846 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001847 if (page)
1848 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001849 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001852static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001855 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 kunmap(page);
1857 mark_page_accessed(page);
1858 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860}
1861
Eric Parisb09e0fa2011-05-24 17:12:39 -07001862#ifdef CONFIG_TMPFS_XATTR
1863/*
1864 * Superblocks without xattr inode operations may get some security.* xattr
1865 * support from the LSM "for free". As soon as we have any other xattrs
1866 * like ACLs, we also need to implement the security.* handlers at
1867 * filesystem level, though.
1868 */
1869
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001870/*
1871 * Allocate new xattr and copy in the value; but leave the name to callers.
1872 */
1873static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
1874{
1875 struct shmem_xattr *new_xattr;
1876 size_t len;
1877
1878 /* wrap around? */
1879 len = sizeof(*new_xattr) + size;
1880 if (len <= sizeof(*new_xattr))
1881 return NULL;
1882
1883 new_xattr = kmalloc(len, GFP_KERNEL);
1884 if (!new_xattr)
1885 return NULL;
1886
1887 new_xattr->size = size;
1888 memcpy(new_xattr->value, value, size);
1889 return new_xattr;
1890}
1891
1892/*
1893 * Callback for security_inode_init_security() for acquiring xattrs.
1894 */
1895static int shmem_initxattrs(struct inode *inode,
1896 const struct xattr *xattr_array,
1897 void *fs_info)
1898{
1899 struct shmem_inode_info *info = SHMEM_I(inode);
1900 const struct xattr *xattr;
1901 struct shmem_xattr *new_xattr;
1902 size_t len;
1903
1904 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
1905 new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
1906 if (!new_xattr)
1907 return -ENOMEM;
1908
1909 len = strlen(xattr->name) + 1;
1910 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
1911 GFP_KERNEL);
1912 if (!new_xattr->name) {
1913 kfree(new_xattr);
1914 return -ENOMEM;
1915 }
1916
1917 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
1918 XATTR_SECURITY_PREFIX_LEN);
1919 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
1920 xattr->name, len);
1921
1922 spin_lock(&info->lock);
1923 list_add(&new_xattr->list, &info->xattr_list);
1924 spin_unlock(&info->lock);
1925 }
1926
1927 return 0;
1928}
1929
Eric Parisb09e0fa2011-05-24 17:12:39 -07001930static int shmem_xattr_get(struct dentry *dentry, const char *name,
1931 void *buffer, size_t size)
1932{
1933 struct shmem_inode_info *info;
1934 struct shmem_xattr *xattr;
1935 int ret = -ENODATA;
1936
1937 info = SHMEM_I(dentry->d_inode);
1938
1939 spin_lock(&info->lock);
1940 list_for_each_entry(xattr, &info->xattr_list, list) {
1941 if (strcmp(name, xattr->name))
1942 continue;
1943
1944 ret = xattr->size;
1945 if (buffer) {
1946 if (size < xattr->size)
1947 ret = -ERANGE;
1948 else
1949 memcpy(buffer, xattr->value, xattr->size);
1950 }
1951 break;
1952 }
1953 spin_unlock(&info->lock);
1954 return ret;
1955}
1956
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001957static int shmem_xattr_set(struct inode *inode, const char *name,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001958 const void *value, size_t size, int flags)
1959{
Eric Parisb09e0fa2011-05-24 17:12:39 -07001960 struct shmem_inode_info *info = SHMEM_I(inode);
1961 struct shmem_xattr *xattr;
1962 struct shmem_xattr *new_xattr = NULL;
Eric Parisb09e0fa2011-05-24 17:12:39 -07001963 int err = 0;
1964
1965 /* value == NULL means remove */
1966 if (value) {
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001967 new_xattr = shmem_xattr_alloc(value, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001968 if (!new_xattr)
1969 return -ENOMEM;
1970
1971 new_xattr->name = kstrdup(name, GFP_KERNEL);
1972 if (!new_xattr->name) {
1973 kfree(new_xattr);
1974 return -ENOMEM;
1975 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001976 }
1977
1978 spin_lock(&info->lock);
1979 list_for_each_entry(xattr, &info->xattr_list, list) {
1980 if (!strcmp(name, xattr->name)) {
1981 if (flags & XATTR_CREATE) {
1982 xattr = new_xattr;
1983 err = -EEXIST;
1984 } else if (new_xattr) {
1985 list_replace(&xattr->list, &new_xattr->list);
1986 } else {
1987 list_del(&xattr->list);
1988 }
1989 goto out;
1990 }
1991 }
1992 if (flags & XATTR_REPLACE) {
1993 xattr = new_xattr;
1994 err = -ENODATA;
1995 } else {
1996 list_add(&new_xattr->list, &info->xattr_list);
1997 xattr = NULL;
1998 }
1999out:
2000 spin_unlock(&info->lock);
2001 if (xattr)
2002 kfree(xattr->name);
2003 kfree(xattr);
2004 return err;
2005}
2006
Eric Parisb09e0fa2011-05-24 17:12:39 -07002007static const struct xattr_handler *shmem_xattr_handlers[] = {
2008#ifdef CONFIG_TMPFS_POSIX_ACL
2009 &generic_acl_access_handler,
2010 &generic_acl_default_handler,
2011#endif
2012 NULL
2013};
2014
2015static int shmem_xattr_validate(const char *name)
2016{
2017 struct { const char *prefix; size_t len; } arr[] = {
2018 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2019 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2020 };
2021 int i;
2022
2023 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2024 size_t preflen = arr[i].len;
2025 if (strncmp(name, arr[i].prefix, preflen) == 0) {
2026 if (!name[preflen])
2027 return -EINVAL;
2028 return 0;
2029 }
2030 }
2031 return -EOPNOTSUPP;
2032}
2033
2034static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2035 void *buffer, size_t size)
2036{
2037 int err;
2038
2039 /*
2040 * If this is a request for a synthetic attribute in the system.*
2041 * namespace use the generic infrastructure to resolve a handler
2042 * for it via sb->s_xattr.
2043 */
2044 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2045 return generic_getxattr(dentry, name, buffer, size);
2046
2047 err = shmem_xattr_validate(name);
2048 if (err)
2049 return err;
2050
2051 return shmem_xattr_get(dentry, name, buffer, size);
2052}
2053
2054static int shmem_setxattr(struct dentry *dentry, const char *name,
2055 const void *value, size_t size, int flags)
2056{
2057 int err;
2058
2059 /*
2060 * If this is a request for a synthetic attribute in the system.*
2061 * namespace use the generic infrastructure to resolve a handler
2062 * for it via sb->s_xattr.
2063 */
2064 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2065 return generic_setxattr(dentry, name, value, size, flags);
2066
2067 err = shmem_xattr_validate(name);
2068 if (err)
2069 return err;
2070
2071 if (size == 0)
2072 value = ""; /* empty EA, do not remove */
2073
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002074 return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002075
2076}
2077
2078static int shmem_removexattr(struct dentry *dentry, const char *name)
2079{
2080 int err;
2081
2082 /*
2083 * If this is a request for a synthetic attribute in the system.*
2084 * namespace use the generic infrastructure to resolve a handler
2085 * for it via sb->s_xattr.
2086 */
2087 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2088 return generic_removexattr(dentry, name);
2089
2090 err = shmem_xattr_validate(name);
2091 if (err)
2092 return err;
2093
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002094 return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002095}
2096
2097static bool xattr_is_trusted(const char *name)
2098{
2099 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2100}
2101
2102static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2103{
2104 bool trusted = capable(CAP_SYS_ADMIN);
2105 struct shmem_xattr *xattr;
2106 struct shmem_inode_info *info;
2107 size_t used = 0;
2108
2109 info = SHMEM_I(dentry->d_inode);
2110
2111 spin_lock(&info->lock);
2112 list_for_each_entry(xattr, &info->xattr_list, list) {
2113 size_t len;
2114
2115 /* skip "trusted." attributes for unprivileged callers */
2116 if (!trusted && xattr_is_trusted(xattr->name))
2117 continue;
2118
2119 len = strlen(xattr->name) + 1;
2120 used += len;
2121 if (buffer) {
2122 if (size < used) {
2123 used = -ERANGE;
2124 break;
2125 }
2126 memcpy(buffer, xattr->name, len);
2127 buffer += len;
2128 }
2129 }
2130 spin_unlock(&info->lock);
2131
2132 return used;
2133}
2134#endif /* CONFIG_TMPFS_XATTR */
2135
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002136static const struct inode_operations shmem_short_symlink_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 .readlink = generic_readlink,
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002138 .follow_link = shmem_follow_short_symlink,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002139#ifdef CONFIG_TMPFS_XATTR
2140 .setxattr = shmem_setxattr,
2141 .getxattr = shmem_getxattr,
2142 .listxattr = shmem_listxattr,
2143 .removexattr = shmem_removexattr,
2144#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145};
2146
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002147static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 .readlink = generic_readlink,
2149 .follow_link = shmem_follow_link,
2150 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002151#ifdef CONFIG_TMPFS_XATTR
2152 .setxattr = shmem_setxattr,
2153 .getxattr = shmem_getxattr,
2154 .listxattr = shmem_listxattr,
2155 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002156#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002157};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002158
David M. Grimes91828a42006-10-17 00:09:45 -07002159static struct dentry *shmem_get_parent(struct dentry *child)
2160{
2161 return ERR_PTR(-ESTALE);
2162}
2163
2164static int shmem_match(struct inode *ino, void *vfh)
2165{
2166 __u32 *fh = vfh;
2167 __u64 inum = fh[2];
2168 inum = (inum << 32) | fh[1];
2169 return ino->i_ino == inum && fh[0] == ino->i_generation;
2170}
2171
Christoph Hellwig480b1162007-10-21 16:42:13 -07002172static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2173 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07002174{
David M. Grimes91828a42006-10-17 00:09:45 -07002175 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07002176 struct dentry *dentry = NULL;
Hugh Dickinsa76d9802012-10-07 20:32:51 -07002177 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07002178
Christoph Hellwig480b1162007-10-21 16:42:13 -07002179 if (fh_len < 3)
2180 return NULL;
2181
Hugh Dickinsa76d9802012-10-07 20:32:51 -07002182 inum = fid->raw[2];
2183 inum = (inum << 32) | fid->raw[1];
2184
Christoph Hellwig480b1162007-10-21 16:42:13 -07002185 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2186 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07002187 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07002188 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07002189 iput(inode);
2190 }
2191
Christoph Hellwig480b1162007-10-21 16:42:13 -07002192 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07002193}
2194
2195static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2196 int connectable)
2197{
2198 struct inode *inode = dentry->d_inode;
2199
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302200 if (*len < 3) {
2201 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07002202 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302203 }
David M. Grimes91828a42006-10-17 00:09:45 -07002204
Al Viro1d3382c2010-10-23 15:19:20 -04002205 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07002206 /* Unfortunately insert_inode_hash is not idempotent,
2207 * so as we hash inodes here rather than at creation
2208 * time, we need a lock to ensure we only try
2209 * to do it once
2210 */
2211 static DEFINE_SPINLOCK(lock);
2212 spin_lock(&lock);
Al Viro1d3382c2010-10-23 15:19:20 -04002213 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07002214 __insert_inode_hash(inode,
2215 inode->i_ino + inode->i_generation);
2216 spin_unlock(&lock);
2217 }
2218
2219 fh[0] = inode->i_generation;
2220 fh[1] = inode->i_ino;
2221 fh[2] = ((__u64)inode->i_ino) >> 32;
2222
2223 *len = 3;
2224 return 1;
2225}
2226
Christoph Hellwig39655162007-10-21 16:42:17 -07002227static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07002228 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07002229 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07002230 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07002231};
2232
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002233static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2234 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
2236 char *this_char, *value, *rest;
2237
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00002238 while (options != NULL) {
2239 this_char = options;
2240 for (;;) {
2241 /*
2242 * NUL-terminate this option: unfortunately,
2243 * mount options form a comma-separated list,
2244 * but mpol's nodelist may also contain commas.
2245 */
2246 options = strchr(options, ',');
2247 if (options == NULL)
2248 break;
2249 options++;
2250 if (!isdigit(*options)) {
2251 options[-1] = '\0';
2252 break;
2253 }
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (!*this_char)
2256 continue;
2257 if ((value = strchr(this_char,'=')) != NULL) {
2258 *value++ = 0;
2259 } else {
2260 printk(KERN_ERR
2261 "tmpfs: No value for mount option '%s'\n",
2262 this_char);
2263 return 1;
2264 }
2265
2266 if (!strcmp(this_char,"size")) {
2267 unsigned long long size;
2268 size = memparse(value,&rest);
2269 if (*rest == '%') {
2270 size <<= PAGE_SHIFT;
2271 size *= totalram_pages;
2272 do_div(size, 100);
2273 rest++;
2274 }
2275 if (*rest)
2276 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002277 sbinfo->max_blocks =
2278 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002280 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (*rest)
2282 goto bad_val;
2283 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002284 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (*rest)
2286 goto bad_val;
2287 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002288 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002290 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (*rest)
2292 goto bad_val;
2293 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002294 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002296 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (*rest)
2298 goto bad_val;
2299 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002300 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002302 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (*rest)
2304 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002305 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002306 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08002307 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 } else {
2309 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2310 this_char);
2311 return 1;
2312 }
2313 }
2314 return 0;
2315
2316bad_val:
2317 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2318 value, this_char);
2319 return 1;
2320
2321}
2322
2323static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2324{
2325 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002326 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002327 unsigned long inodes;
2328 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Greg Thelen5bff19b2013-02-22 16:36:01 -08002330 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002331 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002332 return error;
2333
2334 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002335 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002336 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002337 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002338 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002339 goto out;
2340 /*
Hugh Dickins54af6042011-08-03 16:21:24 -07002341 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002342 * but we must separately disallow unlimited->limited, because
2343 * in that case we have no record of how much is already in use.
2344 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002345 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002346 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002347 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002348 goto out;
2349
2350 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002351 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002352 sbinfo->max_inodes = config.max_inodes;
2353 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002354
Greg Thelen5bff19b2013-02-22 16:36:01 -08002355 /*
2356 * Preserve previous mempolicy unless mpol remount option was specified.
2357 */
2358 if (config.mpol) {
2359 mpol_put(sbinfo->mpol);
2360 sbinfo->mpol = config.mpol; /* transfers initial ref */
2361 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002362out:
2363 spin_unlock(&sbinfo->stat_lock);
2364 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002366
Al Viro34c80b12011-12-08 21:32:45 -05002367static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002368{
Al Viro34c80b12011-12-08 21:32:45 -05002369 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002370
2371 if (sbinfo->max_blocks != shmem_default_max_blocks())
2372 seq_printf(seq, ",size=%luk",
2373 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2374 if (sbinfo->max_inodes != shmem_default_max_inodes())
2375 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2376 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04002377 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002378 if (sbinfo->uid != 0)
2379 seq_printf(seq, ",uid=%u", sbinfo->uid);
2380 if (sbinfo->gid != 0)
2381 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002382 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002383 return 0;
2384}
David Herrmannd9bba3a2014-08-08 14:25:29 -07002385
2386#define MFD_NAME_PREFIX "memfd:"
2387#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
2388#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
2389
2390#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
2391
2392SYSCALL_DEFINE2(memfd_create,
2393 const char __user *, uname,
2394 unsigned int, flags)
2395{
2396 struct shmem_inode_info *info;
2397 struct file *file;
2398 int fd, error;
2399 char *name;
2400 long len;
2401
2402 if (flags & ~(unsigned int)MFD_ALL_FLAGS)
2403 return -EINVAL;
2404
2405 /* length includes terminating zero */
2406 len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
2407 if (len <= 0)
2408 return -EFAULT;
2409 if (len > MFD_NAME_MAX_LEN + 1)
2410 return -EINVAL;
2411
2412 name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
2413 if (!name)
2414 return -ENOMEM;
2415
2416 strcpy(name, MFD_NAME_PREFIX);
2417 if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
2418 error = -EFAULT;
2419 goto err_name;
2420 }
2421
2422 /* terminating-zero may have changed after strnlen_user() returned */
2423 if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
2424 error = -EFAULT;
2425 goto err_name;
2426 }
2427
2428 fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
2429 if (fd < 0) {
2430 error = fd;
2431 goto err_name;
2432 }
2433
2434 file = shmem_file_setup(name, 0, VM_NORESERVE);
2435 if (IS_ERR(file)) {
2436 error = PTR_ERR(file);
2437 goto err_fd;
2438 }
2439 info = SHMEM_I(file->f_path.dentry->d_inode);
2440 file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
2441 file->f_flags |= O_RDWR | O_LARGEFILE;
2442
2443 fd_install(fd, file);
2444 kfree(name);
2445 return fd;
2446
2447err_fd:
2448 put_unused_fd(fd);
2449err_name:
2450 kfree(name);
2451 return error;
2452}
2453
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002454#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456static void shmem_put_super(struct super_block *sb)
2457{
Hugh Dickins602586a2010-08-17 15:23:56 -07002458 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2459
2460 percpu_counter_destroy(&sbinfo->used_blocks);
2461 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 sb->s_fs_info = NULL;
2463}
2464
Kay Sievers2b2af542009-04-30 15:23:42 +02002465int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
2467 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002468 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002469 int err = -ENOMEM;
2470
2471 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002472 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002473 L1_CACHE_BYTES), GFP_KERNEL);
2474 if (!sbinfo)
2475 return -ENOMEM;
2476
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002477 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002478 sbinfo->uid = current_fsuid();
2479 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002480 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002482#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 /*
2484 * Per default we only allow half of the physical ram per
2485 * tmpfs instance, limiting inodes to one per page of lowmem;
2486 * but the internal instance is left unlimited.
2487 */
2488 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002489 sbinfo->max_blocks = shmem_default_max_blocks();
2490 sbinfo->max_inodes = shmem_default_max_inodes();
2491 if (shmem_parse_options(data, sbinfo, false)) {
2492 err = -EINVAL;
2493 goto failed;
2494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
David M. Grimes91828a42006-10-17 00:09:45 -07002496 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497#else
2498 sb->s_flags |= MS_NOUSER;
2499#endif
2500
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002501 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002502 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2503 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002504 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002505
Hugh Dickins285b2c42011-08-03 16:21:20 -07002506 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 sb->s_blocksize = PAGE_CACHE_SIZE;
2508 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2509 sb->s_magic = TMPFS_MAGIC;
2510 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002511 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002512#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002513 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002514#endif
2515#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002516 sb->s_flags |= MS_POSIXACL;
2517#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002518
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002519 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (!inode)
2521 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002522 inode->i_uid = sbinfo->uid;
2523 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05002524 sb->s_root = d_make_root(inode);
2525 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05002526 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return 0;
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529failed:
2530 shmem_put_super(sb);
2531 return err;
2532}
2533
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002534static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536static struct inode *shmem_alloc_inode(struct super_block *sb)
2537{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002538 struct shmem_inode_info *info;
2539 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2540 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002542 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002545static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e32011-01-07 17:49:49 +11002546{
2547 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e32011-01-07 17:49:49 +11002548 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2549}
2550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551static void shmem_destroy_inode(struct inode *inode)
2552{
Al Viro09208d12011-07-26 03:15:03 -04002553 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002555 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002558static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002560 struct shmem_inode_info *info = foo;
2561 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002564static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2567 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002568 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return 0;
2570}
2571
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002572static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002574 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002577static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002579 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002581 .write_begin = shmem_write_begin,
2582 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002584 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002585 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586};
2587
Helge Deller15ad7cd2006-12-06 20:40:36 -08002588static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 .mmap = shmem_mmap,
2590#ifdef CONFIG_TMPFS
2591 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002592 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002593 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002594 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002595 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002596 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002597 .splice_read = shmem_file_splice_read,
Hugh Dickinsae976412007-06-04 10:00:39 +02002598 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599#endif
2600};
2601
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002602static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002603 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002604 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002605#ifdef CONFIG_TMPFS_XATTR
2606 .setxattr = shmem_setxattr,
2607 .getxattr = shmem_getxattr,
2608 .listxattr = shmem_listxattr,
2609 .removexattr = shmem_removexattr,
2610#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611};
2612
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002613static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614#ifdef CONFIG_TMPFS
2615 .create = shmem_create,
2616 .lookup = simple_lookup,
2617 .link = shmem_link,
2618 .unlink = shmem_unlink,
2619 .symlink = shmem_symlink,
2620 .mkdir = shmem_mkdir,
2621 .rmdir = shmem_rmdir,
2622 .mknod = shmem_mknod,
2623 .rename = shmem_rename,
Al Virob0d54302013-06-07 01:20:27 -04002624 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002626#ifdef CONFIG_TMPFS_XATTR
2627 .setxattr = shmem_setxattr,
2628 .getxattr = shmem_getxattr,
2629 .listxattr = shmem_listxattr,
2630 .removexattr = shmem_removexattr,
2631#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002632#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002633 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002634#endif
2635};
2636
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002637static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002638#ifdef CONFIG_TMPFS_XATTR
2639 .setxattr = shmem_setxattr,
2640 .getxattr = shmem_getxattr,
2641 .listxattr = shmem_listxattr,
2642 .removexattr = shmem_removexattr,
2643#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002644#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002645 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002646#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647};
2648
Hugh Dickins759b9772007-03-05 00:30:28 -08002649static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 .alloc_inode = shmem_alloc_inode,
2651 .destroy_inode = shmem_destroy_inode,
2652#ifdef CONFIG_TMPFS
2653 .statfs = shmem_statfs,
2654 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002655 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656#endif
Al Viro1f895f72010-06-05 19:10:41 -04002657 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 .drop_inode = generic_delete_inode,
2659 .put_super = shmem_put_super,
2660};
2661
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002662static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002663 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664#ifdef CONFIG_NUMA
2665 .set_policy = shmem_set_policy,
2666 .get_policy = shmem_get_policy,
2667#endif
2668};
2669
Al Viro3c26ff62010-07-25 11:46:36 +04002670static struct dentry *shmem_mount(struct file_system_type *fs_type,
2671 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Al Viro3c26ff62010-07-25 11:46:36 +04002673 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002676static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 .owner = THIS_MODULE,
2678 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002679 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 .kill_sb = kill_litter_super,
2681};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002683int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
2685 int error;
2686
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002687 error = bdi_init(&shmem_backing_dev_info);
2688 if (error)
2689 goto out4;
2690
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002691 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (error)
2693 goto out3;
2694
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002695 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (error) {
2697 printk(KERN_ERR "Could not register tmpfs\n");
2698 goto out2;
2699 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002700
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002701 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2702 shmem_fs_type.name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (IS_ERR(shm_mnt)) {
2704 error = PTR_ERR(shm_mnt);
2705 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2706 goto out1;
2707 }
2708 return 0;
2709
2710out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002711 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002713 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002715 bdi_destroy(&shmem_backing_dev_info);
2716out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 shm_mnt = ERR_PTR(error);
2718 return error;
2719}
Matt Mackall853ac432009-01-06 14:40:20 -08002720
2721#else /* !CONFIG_SHMEM */
2722
2723/*
2724 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2725 *
2726 * This is intended for small system where the benefits of the full
2727 * shmem code (swap-backed and resource-limited) are outweighed by
2728 * their complexity. On systems without swap this code should be
2729 * effectively equivalent, but much lighter weight.
2730 */
2731
2732#include <linux/ramfs.h>
2733
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002734static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08002735 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002736 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002737 .kill_sb = kill_litter_super,
2738};
2739
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002740int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002741{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002742 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08002743
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002744 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08002745 BUG_ON(IS_ERR(shm_mnt));
2746
2747 return 0;
2748}
2749
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002750int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08002751{
2752 return 0;
2753}
2754
Hugh Dickins3f96b792009-09-21 17:03:37 -07002755int shmem_lock(struct file *file, int lock, struct user_struct *user)
2756{
2757 return 0;
2758}
2759
Hugh Dickins24513262012-01-20 14:34:21 -08002760void shmem_unlock_mapping(struct address_space *mapping)
2761{
2762}
2763
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002764void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07002765{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002766 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07002767}
2768EXPORT_SYMBOL_GPL(shmem_truncate_range);
2769
Hugh Dickinsc4a92842014-06-23 13:22:06 -07002770int vmtruncate_range(struct inode *inode, loff_t lstart, loff_t lend)
2771{
2772 /* Only CONFIG_SHMEM shmem.c ever supported i_op->truncate_range(). */
2773 return -ENOSYS;
2774}
2775
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002776#define shmem_vm_ops generic_file_vm_ops
2777#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002778#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002779#define shmem_acct_size(flags, size) 0
2780#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002781
2782#endif /* CONFIG_SHMEM */
2783
2784/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Randy Dunlap46711812008-03-19 17:00:41 -07002786/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 * @name: name for dentry (to be seen in /proc/<pid>/maps
2789 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002790 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002792struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
2794 int error;
2795 struct file *file;
2796 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002797 struct path path;
2798 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 struct qstr this;
2800
2801 if (IS_ERR(shm_mnt))
2802 return (void *)shm_mnt;
2803
Hugh Dickins285b2c42011-08-03 16:21:20 -07002804 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return ERR_PTR(-EINVAL);
2806
2807 if (shmem_acct_size(flags, size))
2808 return ERR_PTR(-ENOMEM);
2809
2810 error = -ENOMEM;
2811 this.name = name;
2812 this.len = strlen(name);
2813 this.hash = 0; /* will go */
2814 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002815 path.dentry = d_alloc(root, &this);
2816 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002818 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002821 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002823 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Al Viro2c48b9c2009-08-09 00:52:35 +04002825 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02002827 clear_nlink(inode); /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002828#ifndef CONFIG_MMU
2829 error = ramfs_nommu_expand_for_mapping(inode, size);
2830 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002831 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002832#endif
Al Viro4b42af82009-08-05 18:25:56 +04002833
2834 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002835 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002836 &shmem_file_operations);
2837 if (!file)
2838 goto put_dentry;
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return file;
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002843 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844put_memory:
2845 shmem_unacct_size(flags, size);
2846 return ERR_PTR(error);
2847}
Keith Packard395e0dd2008-06-20 00:08:06 -07002848EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
John Stultzd92aa582011-12-15 18:51:04 -08002850void shmem_set_file(struct vm_area_struct *vma, struct file *file)
2851{
2852 if (vma->vm_file)
2853 fput(vma->vm_file);
2854 vma->vm_file = file;
2855 vma->vm_ops = &shmem_vm_ops;
2856 vma->vm_flags |= VM_CAN_NONLINEAR;
2857}
2858
Randy Dunlap46711812008-03-19 17:00:41 -07002859/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2862 */
2863int shmem_zero_setup(struct vm_area_struct *vma)
2864{
2865 struct file *file;
2866 loff_t size = vma->vm_end - vma->vm_start;
2867
2868 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2869 if (IS_ERR(file))
2870 return PTR_ERR(file);
2871
John Stultzd92aa582011-12-15 18:51:04 -08002872 shmem_set_file(vma, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 return 0;
2874}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002875
2876/**
2877 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2878 * @mapping: the page's address_space
2879 * @index: the page index
2880 * @gfp: the page allocator flags to use if allocating
2881 *
2882 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2883 * with any new page allocations done using the specified allocation flags.
2884 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2885 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2886 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2887 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002888 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2889 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002890 */
2891struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2892 pgoff_t index, gfp_t gfp)
2893{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002894#ifdef CONFIG_SHMEM
2895 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002896 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002897 int error;
2898
2899 BUG_ON(mapping->a_ops != &shmem_aops);
2900 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2901 if (error)
2902 page = ERR_PTR(error);
2903 else
2904 unlock_page(page);
2905 return page;
2906#else
2907 /*
2908 * The tiny !SHMEM case uses ramfs without swap
2909 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002910 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002911#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002912}
2913EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);