blob: 352d511a90d9b19943f5ee5e289a291bdf9afb5d [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>
Mimi Zohar92562922008-10-07 14:00:12 -040065#include <linux/magic.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <asm/pgtable.h>
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Pretend that each entry is of this size in directory's i_size */
74#define BOGO_DIRENT_SIZE 20
75
Hugh Dickins69f07ec2011-08-03 16:21:26 -070076/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
77#define SHORT_SYMLINK_LEN 128
78
Hugh Dickinsa62f3742014-06-23 13:22:06 -070079/*
80 * vmtruncate_range() communicates with shmem_fault via
81 * inode->i_private (with i_mutex making sure that it has only one user at
82 * a time): we would prefer not to enlarge the shmem inode just for that.
83 */
84struct shmem_falloc {
85 pgoff_t start; /* start of range currently being fallocated */
86 pgoff_t next; /* the next page offset to be fallocated */
87};
88
Eric Parisb09e0fa2011-05-24 17:12:39 -070089struct shmem_xattr {
90 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
91 char *name; /* xattr name */
92 size_t size;
93 char value[0];
94};
95
Hugh Dickins285b2c42011-08-03 16:21:20 -070096/* Flag allocation requirements to shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097enum sgp_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 SGP_READ, /* don't exceed i_size, don't allocate page */
99 SGP_CACHE, /* don't exceed i_size, may allocate page */
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -0800100 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 SGP_WRITE, /* may exceed i_size, may allocate page */
102};
103
Andrew Mortonb76db732008-02-08 04:21:49 -0800104#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800105static unsigned long shmem_default_max_blocks(void)
106{
107 return totalram_pages / 2;
108}
109
110static unsigned long shmem_default_max_inodes(void)
111{
112 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
113}
Andrew Mortonb76db732008-02-08 04:21:49 -0800114#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800115
Hugh Dickins68da9f02011-07-25 17:12:34 -0700116static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
117 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
118
119static inline int shmem_getpage(struct inode *inode, pgoff_t index,
120 struct page **pagep, enum sgp_type sgp, int *fault_type)
121{
122 return shmem_getpage_gfp(inode, index, pagep, sgp,
123 mapping_gfp_mask(inode->i_mapping), fault_type);
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
127{
128 return sb->s_fs_info;
129}
130
131/*
132 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
133 * for shared memory and for shared anonymous (/dev/zero) mappings
134 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
135 * consistent with the pre-accounting of private mappings ...
136 */
137static inline int shmem_acct_size(unsigned long flags, loff_t size)
138{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000139 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000140 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static inline void shmem_unacct_size(unsigned long flags, loff_t size)
144{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000145 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 vm_unacct_memory(VM_ACCT(size));
147}
148
149/*
150 * ... whereas tmpfs objects are accounted incrementally as
151 * pages are allocated, in order to allow huge sparse files.
152 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
153 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
154 */
155static inline int shmem_acct_block(unsigned long flags)
156{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000157 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000158 security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161static inline void shmem_unacct_blocks(unsigned long flags, long pages)
162{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000163 if (flags & VM_NORESERVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
165}
166
Hugh Dickins759b9772007-03-05 00:30:28 -0800167static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700168static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800169static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800170static const struct inode_operations shmem_inode_operations;
171static const struct inode_operations shmem_dir_inode_operations;
172static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400173static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700175static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .ra_pages = 0, /* No readahead */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700177 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
180static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800181static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800183static int shmem_reserve_inode(struct super_block *sb)
184{
185 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
186 if (sbinfo->max_inodes) {
187 spin_lock(&sbinfo->stat_lock);
188 if (!sbinfo->free_inodes) {
189 spin_unlock(&sbinfo->stat_lock);
190 return -ENOSPC;
191 }
192 sbinfo->free_inodes--;
193 spin_unlock(&sbinfo->stat_lock);
194 }
195 return 0;
196}
197
198static void shmem_free_inode(struct super_block *sb)
199{
200 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
201 if (sbinfo->max_inodes) {
202 spin_lock(&sbinfo->stat_lock);
203 sbinfo->free_inodes++;
204 spin_unlock(&sbinfo->stat_lock);
205 }
206}
207
Randy Dunlap46711812008-03-19 17:00:41 -0700208/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700209 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * @inode: inode to recalc
211 *
212 * We have to calculate the free blocks since the mm can drop
213 * undirtied hole pages behind our back.
214 *
215 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
216 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
217 *
218 * It has to be called with the spinlock held.
219 */
220static void shmem_recalc_inode(struct inode *inode)
221{
222 struct shmem_inode_info *info = SHMEM_I(inode);
223 long freed;
224
225 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
226 if (freed > 0) {
Hugh Dickins54af6042011-08-03 16:21:24 -0700227 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
228 if (sbinfo->max_blocks)
229 percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 info->alloced -= freed;
Hugh Dickins54af6042011-08-03 16:21:24 -0700231 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 shmem_unacct_blocks(info->flags, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234}
235
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700236/*
237 * Replace item expected in radix tree by a new item, while holding tree lock.
238 */
239static int shmem_radix_tree_replace(struct address_space *mapping,
240 pgoff_t index, void *expected, void *replacement)
241{
242 void **pslot;
243 void *item = NULL;
244
245 VM_BUG_ON(!expected);
246 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
247 if (pslot)
248 item = radix_tree_deref_slot_protected(pslot,
249 &mapping->tree_lock);
250 if (item != expected)
251 return -ENOENT;
252 if (replacement)
253 radix_tree_replace_slot(pslot, replacement);
254 else
255 radix_tree_delete(&mapping->page_tree, index);
256 return 0;
257}
258
259/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700260 * Like add_to_page_cache_locked, but error if expected item has gone.
261 */
262static int shmem_add_to_page_cache(struct page *page,
263 struct address_space *mapping,
264 pgoff_t index, gfp_t gfp, void *expected)
265{
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700266 int error = 0;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700267
268 VM_BUG_ON(!PageLocked(page));
269 VM_BUG_ON(!PageSwapBacked(page));
270
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700271 if (!expected)
272 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
273 if (!error) {
274 page_cache_get(page);
275 page->mapping = mapping;
276 page->index = index;
277
278 spin_lock_irq(&mapping->tree_lock);
279 if (!expected)
280 error = radix_tree_insert(&mapping->page_tree,
281 index, page);
282 else
283 error = shmem_radix_tree_replace(mapping, index,
284 expected, page);
285 if (!error) {
286 mapping->nrpages++;
287 __inc_zone_page_state(page, NR_FILE_PAGES);
288 __inc_zone_page_state(page, NR_SHMEM);
289 spin_unlock_irq(&mapping->tree_lock);
290 } else {
291 page->mapping = NULL;
292 spin_unlock_irq(&mapping->tree_lock);
293 page_cache_release(page);
294 }
295 if (!expected)
296 radix_tree_preload_end();
297 }
298 if (error)
299 mem_cgroup_uncharge_cache_page(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700300 return error;
301}
302
303/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700304 * Like delete_from_page_cache, but substitutes swap for page.
305 */
306static void shmem_delete_from_page_cache(struct page *page, void *radswap)
307{
308 struct address_space *mapping = page->mapping;
309 int error;
310
311 spin_lock_irq(&mapping->tree_lock);
312 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
313 page->mapping = NULL;
314 mapping->nrpages--;
315 __dec_zone_page_state(page, NR_FILE_PAGES);
316 __dec_zone_page_state(page, NR_SHMEM);
317 spin_unlock_irq(&mapping->tree_lock);
318 page_cache_release(page);
319 BUG_ON(error);
320}
321
322/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700323 * Like find_get_pages, but collecting swap entries as well as pages.
324 */
325static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
326 pgoff_t start, unsigned int nr_pages,
327 struct page **pages, pgoff_t *indices)
328{
329 unsigned int i;
330 unsigned int ret;
331 unsigned int nr_found;
332
333 rcu_read_lock();
334restart:
335 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
336 (void ***)pages, indices, start, nr_pages);
337 ret = 0;
338 for (i = 0; i < nr_found; i++) {
339 struct page *page;
340repeat:
341 page = radix_tree_deref_slot((void **)pages[i]);
342 if (unlikely(!page))
343 continue;
344 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700345 if (radix_tree_deref_retry(page))
346 goto restart;
347 /*
348 * Otherwise, we must be storing a swap entry
349 * here as an exceptional entry: so return it
350 * without attempting to raise page count.
351 */
352 goto export;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700353 }
354 if (!page_cache_get_speculative(page))
355 goto repeat;
356
357 /* Has the page moved? */
358 if (unlikely(page != *((void **)pages[i]))) {
359 page_cache_release(page);
360 goto repeat;
361 }
362export:
363 indices[ret] = indices[i];
364 pages[ret] = page;
365 ret++;
366 }
367 if (unlikely(!ret && nr_found))
368 goto restart;
369 rcu_read_unlock();
370 return ret;
371}
372
373/*
374 * Remove swap entry from radix tree, free the swap and its page cache.
375 */
376static int shmem_free_swap(struct address_space *mapping,
377 pgoff_t index, void *radswap)
378{
379 int error;
380
381 spin_lock_irq(&mapping->tree_lock);
382 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
383 spin_unlock_irq(&mapping->tree_lock);
384 if (!error)
385 free_swap_and_cache(radix_to_swp_entry(radswap));
386 return error;
387}
388
389/*
390 * Pagevec may contain swap entries, so shuffle up pages before releasing.
391 */
Hugh Dickins24513262012-01-20 14:34:21 -0800392static void shmem_deswap_pagevec(struct pagevec *pvec)
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700393{
394 int i, j;
395
396 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
397 struct page *page = pvec->pages[i];
398 if (!radix_tree_exceptional_entry(page))
399 pvec->pages[j++] = page;
400 }
401 pvec->nr = j;
Hugh Dickins24513262012-01-20 14:34:21 -0800402}
403
404/*
405 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
406 */
407void shmem_unlock_mapping(struct address_space *mapping)
408{
409 struct pagevec pvec;
410 pgoff_t indices[PAGEVEC_SIZE];
411 pgoff_t index = 0;
412
413 pagevec_init(&pvec, 0);
414 /*
415 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
416 */
417 while (!mapping_unevictable(mapping)) {
418 /*
419 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
420 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
421 */
422 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
423 PAGEVEC_SIZE, pvec.pages, indices);
424 if (!pvec.nr)
425 break;
426 index = indices[pvec.nr - 1] + 1;
427 shmem_deswap_pagevec(&pvec);
428 check_move_unevictable_pages(pvec.pages, pvec.nr);
429 pagevec_release(&pvec);
430 cond_resched();
431 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700432}
433
434/*
435 * Remove range of pages and swap entries from radix tree, and free them.
436 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700437void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700439 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700441 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700442 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700443 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700444 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700445 pgoff_t indices[PAGEVEC_SIZE];
446 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700447 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700448 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700450 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
451
452 pagevec_init(&pvec, 0);
453 index = start;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700454 while (index <= end) {
455 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
456 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
457 pvec.pages, indices);
458 if (!pvec.nr)
459 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700460 mem_cgroup_uncharge_start();
461 for (i = 0; i < pagevec_count(&pvec); i++) {
462 struct page *page = pvec.pages[i];
463
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700464 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700465 if (index > end)
466 break;
467
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700468 if (radix_tree_exceptional_entry(page)) {
469 nr_swaps_freed += !shmem_free_swap(mapping,
470 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700471 continue;
472 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700473
474 if (!trylock_page(page))
475 continue;
476 if (page->mapping == mapping) {
477 VM_BUG_ON(PageWriteback(page));
478 truncate_inode_page(mapping, page);
479 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700480 unlock_page(page);
481 }
Hugh Dickins24513262012-01-20 14:34:21 -0800482 shmem_deswap_pagevec(&pvec);
483 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700484 mem_cgroup_uncharge_end();
485 cond_resched();
486 index++;
487 }
488
489 if (partial) {
490 struct page *page = NULL;
491 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
492 if (page) {
493 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
494 set_page_dirty(page);
495 unlock_page(page);
496 page_cache_release(page);
497 }
498 }
499
500 index = start;
501 for ( ; ; ) {
502 cond_resched();
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700503 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
504 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
505 pvec.pages, indices);
506 if (!pvec.nr) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700507 if (index == start)
508 break;
509 index = start;
510 continue;
511 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700512 if (index == start && indices[0] > end) {
Hugh Dickins24513262012-01-20 14:34:21 -0800513 shmem_deswap_pagevec(&pvec);
514 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700515 break;
516 }
517 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)) {
526 nr_swaps_freed += !shmem_free_swap(mapping,
527 index, page);
528 continue;
529 }
530
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700531 lock_page(page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700532 if (page->mapping == mapping) {
533 VM_BUG_ON(PageWriteback(page));
534 truncate_inode_page(mapping, page);
535 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700536 unlock_page(page);
537 }
Hugh Dickins24513262012-01-20 14:34:21 -0800538 shmem_deswap_pagevec(&pvec);
539 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700540 mem_cgroup_uncharge_end();
541 index++;
542 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700545 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 shmem_recalc_inode(inode);
547 spin_unlock(&info->lock);
548
Hugh Dickins285b2c42011-08-03 16:21:20 -0700549 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700551EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Hugh Dickins94c1e622011-06-27 16:18:03 -0700553static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int error;
557
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200558 error = inode_change_ok(inode, attr);
559 if (error)
560 return error;
561
Hugh Dickins94c1e622011-06-27 16:18:03 -0700562 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
563 loff_t oldsize = inode->i_size;
564 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000565
Hugh Dickins94c1e622011-06-27 16:18:03 -0700566 if (newsize != oldsize) {
567 i_size_write(inode, newsize);
568 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
569 }
570 if (newsize < oldsize) {
571 loff_t holebegin = round_up(newsize, PAGE_SIZE);
572 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
573 shmem_truncate_range(inode, newsize, (loff_t)-1);
574 /* unmap again to remove racily COWed private pages */
575 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200579 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700580#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200581 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100582 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700583#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return error;
585}
586
Al Viro1f895f72010-06-05 19:10:41 -0400587static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700590 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000592 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 shmem_unacct_size(info->flags, inode->i_size);
594 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000595 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800597 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800599 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Hugh Dickins69f07ec2011-08-03 16:21:26 -0700601 } else
602 kfree(info->symlink);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700603
604 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
605 kfree(xattr->name);
606 kfree(xattr);
607 }
Hugh Dickinsec592422012-11-16 14:15:04 -0800608 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800609 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400610 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700613/*
614 * If swap found in inode, free it and move page from swapcache to filecache.
615 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700616static int shmem_unuse_inode(struct shmem_inode_info *info,
617 swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700619 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700620 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700621 pgoff_t index;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800622 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700624 radswap = swp_to_radix_entry(swap);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700625 index = radix_tree_locate_item(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700626 if (index == -1)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700627 return 0;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800628
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800629 /*
630 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400631 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800632 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700633 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800634 */
635 if (shmem_swaplist.next != &info->swaplist)
636 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800637
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800638 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700639 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
640 * but also to hold up shmem_evict_inode(): so inode cannot be freed
641 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800642 */
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700643 error = shmem_add_to_page_cache(page, mapping, index,
644 GFP_NOWAIT, radswap);
Hugh Dickins778dd892011-05-11 15:13:37 -0700645 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700646
Hugh Dickins48f170f2011-07-25 17:12:37 -0700647 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700648 /*
649 * Truncation and eviction use free_swap_and_cache(), which
650 * only does trylock page: if we raced, best clean up here.
651 */
Hugh Dickins73b12622008-02-04 22:28:50 -0800652 delete_from_swap_cache(page);
653 set_page_dirty(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700654 if (!error) {
655 spin_lock(&info->lock);
656 info->swapped--;
657 spin_unlock(&info->lock);
658 swap_free(swap);
659 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800660 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800662 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700666 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700668int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700670 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct shmem_inode_info *info;
672 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700673 int error;
674
675 /*
676 * Charge page using GFP_KERNEL while we can wait, before taking
677 * the shmem_swaplist_mutex which might hold up shmem_writepage().
678 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -0700679 */
680 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
681 if (error)
682 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700683 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800685 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700686 list_for_each_safe(this, next, &shmem_swaplist) {
687 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700688 if (info->swapped)
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700689 found = shmem_unuse_inode(info, swap, page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700690 else
691 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800692 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800693 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800696 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700697
Hugh Dickins778dd892011-05-11 15:13:37 -0700698 if (!found)
699 mem_cgroup_uncharge_cache_page(page);
700 if (found < 0)
701 error = found;
702out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800703 unlock_page(page);
704 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700705 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/*
709 * Move the page from the page cache to the swap cache.
710 */
711static int shmem_writepage(struct page *page, struct writeback_control *wbc)
712{
713 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700716 swp_entry_t swap;
717 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 mapping = page->mapping;
721 index = page->index;
722 inode = mapping->host;
723 info = SHMEM_I(inode);
724 if (info->flags & VM_LOCKED)
725 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800726 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 goto redirty;
728
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800729 /*
730 * shmem_backing_dev_info's capabilities prevent regular writeback or
731 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700732 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800733 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700734 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800735 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700736 if (!wbc->for_reclaim) {
737 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
738 goto redirty;
739 }
740 swap = get_swap_page();
741 if (!swap.val)
742 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800743
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700744 /*
745 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700746 * if it's not already there. Do it now before the page is
747 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700748 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700749 * we've incremented swapped, because shmem_unuse_inode() will
750 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700751 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700752 mutex_lock(&shmem_swaplist_mutex);
753 if (list_empty(&info->swaplist))
754 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700755
Hugh Dickins48f170f2011-07-25 17:12:37 -0700756 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800757 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700758 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
759
760 spin_lock(&info->lock);
761 info->swapped++;
762 shmem_recalc_inode(inode);
Hugh Dickins826267c2011-05-28 13:14:09 -0700763 spin_unlock(&info->lock);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700764
765 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800766 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700767 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769 }
770
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700771 mutex_unlock(&shmem_swaplist_mutex);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700772 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773redirty:
774 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800775 if (wbc->for_reclaim)
776 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
777 unlock_page(page);
778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800782#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700783static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800784{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700785 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800786
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700787 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700788 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800789
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700790 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800791
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700792 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800793}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700794
795static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
796{
797 struct mempolicy *mpol = NULL;
798 if (sbinfo->mpol) {
799 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
800 mpol = sbinfo->mpol;
801 mpol_get(mpol);
802 spin_unlock(&sbinfo->stat_lock);
803 }
804 return mpol;
805}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800806#endif /* CONFIG_TMPFS */
807
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700808static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
809 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct vm_area_struct pvma;
Mel Gorman78a685b2012-12-05 14:01:41 -0800812 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800814 /* Create a pseudo vma that just contains the policy */
815 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700816 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800817 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700818 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700819
Mel Gorman78a685b2012-12-05 14:01:41 -0800820 page = swapin_readahead(swap, gfp, &pvma, 0);
821
822 /* Drop reference taken by mpol_shared_policy_lookup() */
823 mpol_cond_put(pvma.vm_policy);
824
825 return page;
826}
827
828static struct page *shmem_alloc_page(gfp_t gfp,
829 struct shmem_inode_info *info, pgoff_t index)
830{
831 struct vm_area_struct pvma;
832 struct page *page;
833
834 /* Create a pseudo vma that just contains the policy */
835 pvma.vm_start = 0;
836 pvma.vm_pgoff = index;
837 pvma.vm_ops = NULL;
838 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
839
840 page = alloc_page_vma(gfp, &pvma, 0);
841
842 /* Drop reference taken by mpol_shared_policy_lookup() */
843 mpol_cond_put(pvma.vm_policy);
844
845 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800847#else /* !CONFIG_NUMA */
848#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700849static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800850{
851}
852#endif /* CONFIG_TMPFS */
853
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700854static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
855 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700857 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Hugh Dickins02098fe2008-02-04 22:28:42 -0800860static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700861 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000863 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800865#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700867#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
868static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
869{
870 return NULL;
871}
872#endif
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700875 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 *
877 * If we allocate a new one we do not mark it dirty. That's up to the
878 * vm. If we swap it in we mark it dirty since we also free the swap
879 * entry since a page cannot live in both the swap and page cache
880 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700881static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -0700882 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af6042011-08-03 16:21:24 -0700885 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700887 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 swp_entry_t swap;
889 int error;
Hugh Dickins54af6042011-08-03 16:21:24 -0700890 int once = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700892 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894repeat:
Hugh Dickins54af6042011-08-03 16:21:24 -0700895 swap.val = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700896 page = find_lock_page(mapping, index);
Hugh Dickins54af6042011-08-03 16:21:24 -0700897 if (radix_tree_exceptional_entry(page)) {
898 swap = radix_to_swp_entry(page);
899 page = NULL;
900 }
901
902 if (sgp != SGP_WRITE &&
903 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
904 error = -EINVAL;
905 goto failed;
906 }
907
908 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800909 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700910 * Once we can get the page lock, it must be uptodate:
911 * if there were an error in reading back from swap,
912 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800913 */
Hugh Dickins54af6042011-08-03 16:21:24 -0700914 BUG_ON(page && !PageUptodate(page));
915 *pagep = page;
916 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /*
Hugh Dickins54af6042011-08-03 16:21:24 -0700920 * Fast cache lookup did not find it:
921 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
Hugh Dickins54af6042011-08-03 16:21:24 -0700923 info = SHMEM_I(inode);
924 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (swap.val) {
927 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700928 page = lookup_swap_cache(swap);
929 if (!page) {
Ying Han456f9982011-05-26 16:25:38 -0700930 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700931 if (fault_type)
932 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700933 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700934 if (!page) {
Hugh Dickins54af6042011-08-03 16:21:24 -0700935 error = -ENOMEM;
936 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 /* We have to do this with page locked to prevent races */
Hugh Dickins54af6042011-08-03 16:21:24 -0700941 lock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700942 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 error = -EIO;
Hugh Dickins54af6042011-08-03 16:21:24 -0700944 goto failed;
945 }
946 wait_on_page_writeback(page);
947
948 /* Someone may have already done it for us */
949 if (page->mapping) {
950 if (page->mapping == mapping &&
951 page->index == index)
952 goto done;
953 error = -EEXIST;
954 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700957 error = mem_cgroup_cache_charge(page, current->mm,
958 gfp & GFP_RECLAIM_MASK);
959 if (!error)
960 error = shmem_add_to_page_cache(page, mapping, index,
961 gfp, swp_to_radix_entry(swap));
Hugh Dickins54af6042011-08-03 16:21:24 -0700962 if (error)
963 goto failed;
964
965 spin_lock(&info->lock);
966 info->swapped--;
967 shmem_recalc_inode(inode);
968 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700969
Hugh Dickins27ab7002011-07-25 17:12:36 -0700970 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700971 set_page_dirty(page);
972 swap_free(swap);
973
Hugh Dickins54af6042011-08-03 16:21:24 -0700974 } else {
975 if (shmem_acct_block(info->flags)) {
976 error = -ENOSPC;
977 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700979 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700980 if (percpu_counter_compare(&sbinfo->used_blocks,
Hugh Dickins54af6042011-08-03 16:21:24 -0700981 sbinfo->max_blocks) >= 0) {
982 error = -ENOSPC;
983 goto unacct;
984 }
Tim Chen7e496292010-08-09 17:19:05 -0700985 percpu_counter_inc(&sbinfo->used_blocks);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Hugh Dickins54af6042011-08-03 16:21:24 -0700988 page = shmem_alloc_page(gfp, info, index);
989 if (!page) {
990 error = -ENOMEM;
991 goto decused;
992 }
993
994 SetPageSwapBacked(page);
995 __set_page_locked(page);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700996 error = mem_cgroup_cache_charge(page, current->mm,
997 gfp & GFP_RECLAIM_MASK);
998 if (!error)
999 error = shmem_add_to_page_cache(page, mapping, index,
1000 gfp, NULL);
Hugh Dickins54af6042011-08-03 16:21:24 -07001001 if (error)
1002 goto decused;
1003 lru_cache_add_anon(page);
1004
1005 spin_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 info->alloced++;
Hugh Dickins54af6042011-08-03 16:21:24 -07001007 inode->i_blocks += BLOCKS_PER_PAGE;
1008 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 spin_unlock(&info->lock);
Hugh Dickins54af6042011-08-03 16:21:24 -07001010
Hugh Dickins27ab7002011-07-25 17:12:36 -07001011 clear_highpage(page);
1012 flush_dcache_page(page);
1013 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001014 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -07001015 set_page_dirty(page);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017done:
Hugh Dickins54af6042011-08-03 16:21:24 -07001018 /* Perhaps the file has been truncated since we checked */
1019 if (sgp != SGP_WRITE &&
1020 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1021 error = -EINVAL;
1022 goto trunc;
Shaohua Liff36b802010-08-09 17:19:06 -07001023 }
Hugh Dickins54af6042011-08-03 16:21:24 -07001024 *pagep = page;
1025 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001026
Nick Piggind0217ac2007-07-19 01:47:03 -07001027 /*
Hugh Dickins54af6042011-08-03 16:21:24 -07001028 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
Hugh Dickins54af6042011-08-03 16:21:24 -07001030trunc:
1031 ClearPageDirty(page);
1032 delete_from_page_cache(page);
1033 spin_lock(&info->lock);
1034 info->alloced--;
1035 inode->i_blocks -= BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 spin_unlock(&info->lock);
Hugh Dickins54af6042011-08-03 16:21:24 -07001037decused:
1038 if (sbinfo->max_blocks)
1039 percpu_counter_add(&sbinfo->used_blocks, -1);
1040unacct:
1041 shmem_unacct_blocks(info->flags, 1);
1042failed:
1043 if (swap.val && error != -EINVAL) {
1044 struct page *test = find_get_page(mapping, index);
1045 if (test && !radix_tree_exceptional_entry(test))
1046 page_cache_release(test);
1047 /* Have another try if the entry has changed */
1048 if (test != swp_to_radix_entry(swap))
1049 error = -EEXIST;
1050 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001051 if (page) {
Hugh Dickins54af6042011-08-03 16:21:24 -07001052 unlock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001053 page_cache_release(page);
Hugh Dickins54af6042011-08-03 16:21:24 -07001054 }
1055 if (error == -ENOSPC && !once++) {
1056 info = SHMEM_I(inode);
1057 spin_lock(&info->lock);
1058 shmem_recalc_inode(inode);
1059 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001060 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001061 }
Hugh Dickins54af6042011-08-03 16:21:24 -07001062 if (error == -EEXIST)
1063 goto repeat;
1064 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1068{
1069 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1070 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001071 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001073 /*
1074 * Trinity finds that probing a hole which tmpfs is punching can
1075 * prevent the hole-punch from ever completing: which in turn
1076 * locks writers out with its hold on i_mutex. So refrain from
1077 * faulting pages into the hole while it's being punched, and
1078 * wait on i_mutex to be released if vmf->flags permits.
1079 */
1080 if (unlikely(inode->i_private)) {
1081 struct shmem_falloc *shmem_falloc;
1082
1083 spin_lock(&inode->i_lock);
1084 shmem_falloc = inode->i_private;
1085 if (!shmem_falloc ||
1086 vmf->pgoff < shmem_falloc->start ||
1087 vmf->pgoff >= shmem_falloc->next)
1088 shmem_falloc = NULL;
1089 spin_unlock(&inode->i_lock);
1090 /*
1091 * i_lock has protected us from taking shmem_falloc seriously
1092 * once return from vmtruncate_range() went back up that stack.
1093 * i_lock does not serialize with i_mutex at all, but it does
1094 * not matter if sometimes we wait unnecessarily, or sometimes
1095 * miss out on waiting: we just need to make those cases rare.
1096 */
1097 if (shmem_falloc) {
1098 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1099 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
1100 up_read(&vma->vm_mm->mmap_sem);
1101 mutex_lock(&inode->i_mutex);
1102 mutex_unlock(&inode->i_mutex);
1103 return VM_FAULT_RETRY;
1104 }
1105 /* cond_resched? Leave that to GUP or return to user */
1106 return VM_FAULT_NOPAGE;
1107 }
1108 }
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1111 if (error)
1112 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001113
Ying Han456f9982011-05-26 16:25:38 -07001114 if (ret & VM_FAULT_MAJOR) {
1115 count_vm_event(PGMAJFAULT);
1116 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1117 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001118 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001121int vmtruncate_range(struct inode *inode, loff_t lstart, loff_t lend)
1122{
1123 /*
1124 * If the underlying filesystem is not going to provide
1125 * a way to truncate a range of blocks (punch a hole) -
1126 * we should return failure right now.
1127 * Only CONFIG_SHMEM shmem.c ever supported i_op->truncate_range().
1128 */
1129 if (inode->i_op->truncate_range != shmem_truncate_range)
1130 return -ENOSYS;
1131
1132 mutex_lock(&inode->i_mutex);
1133 {
1134 struct shmem_falloc shmem_falloc;
1135 struct address_space *mapping = inode->i_mapping;
1136 loff_t unmap_start = round_up(lstart, PAGE_SIZE);
1137 loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
1138
1139 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
1140 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
1141 spin_lock(&inode->i_lock);
1142 inode->i_private = &shmem_falloc;
1143 spin_unlock(&inode->i_lock);
1144
1145 if ((u64)unmap_end > (u64)unmap_start)
1146 unmap_mapping_range(mapping, unmap_start,
1147 1 + unmap_end - unmap_start, 0);
1148 shmem_truncate_range(inode, lstart, lend);
1149 /* No need to unmap again: hole-punching leaves COWed pages */
1150
1151 spin_lock(&inode->i_lock);
1152 inode->i_private = NULL;
1153 spin_unlock(&inode->i_lock);
1154 }
1155 mutex_unlock(&inode->i_mutex);
1156 return 0;
1157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001160static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001162 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1163 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001166static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1167 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001169 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1170 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001172 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1173 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175#endif
1176
1177int shmem_lock(struct file *file, int lock, struct user_struct *user)
1178{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001179 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct shmem_inode_info *info = SHMEM_I(inode);
1181 int retval = -ENOMEM;
1182
1183 spin_lock(&info->lock);
1184 if (lock && !(info->flags & VM_LOCKED)) {
1185 if (!user_shm_lock(inode->i_size, user))
1186 goto out_nomem;
1187 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001188 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 if (!lock && (info->flags & VM_LOCKED) && user) {
1191 user_shm_unlock(inode->i_size, user);
1192 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001193 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197out_nomem:
1198 spin_unlock(&info->lock);
1199 return retval;
1200}
1201
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001202static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 file_accessed(file);
1205 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001206 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return 0;
1208}
1209
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001210static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04001211 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct inode *inode;
1214 struct shmem_inode_info *info;
1215 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1216
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001217 if (shmem_reserve_inode(sb))
1218 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 inode = new_inode(sb);
1221 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001222 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001223 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1226 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001227 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 info = SHMEM_I(inode);
1229 memset(info, 0, (char *)inode - (char *)info);
1230 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001231 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001233 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001234 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 switch (mode & S_IFMT) {
1237 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001238 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 init_special_inode(inode, mode, dev);
1240 break;
1241 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001242 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 inode->i_op = &shmem_inode_operations;
1244 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001245 mpol_shared_policy_init(&info->policy,
1246 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
1248 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001249 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /* Some things misbehave if size == 0 on a directory */
1251 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1252 inode->i_op = &shmem_dir_inode_operations;
1253 inode->i_fop = &simple_dir_operations;
1254 break;
1255 case S_IFLNK:
1256 /*
1257 * Must not load anything in the rbtree,
1258 * mpol_free_shared_policy will not be called.
1259 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001260 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 break;
1262 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001263 } else
1264 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return inode;
1266}
1267
1268#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001269static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001270static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001272#ifdef CONFIG_TMPFS_XATTR
1273static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1274#else
1275#define shmem_initxattrs NULL
1276#endif
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001279shmem_write_begin(struct file *file, struct address_space *mapping,
1280 loff_t pos, unsigned len, unsigned flags,
1281 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Nick Piggin800d15a2007-10-16 01:25:03 -07001283 struct inode *inode = mapping->host;
1284 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -07001285 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1286}
1287
1288static int
1289shmem_write_end(struct file *file, struct address_space *mapping,
1290 loff_t pos, unsigned len, unsigned copied,
1291 struct page *page, void *fsdata)
1292{
1293 struct inode *inode = mapping->host;
1294
Hugh Dickinsd3602442008-02-04 22:28:44 -08001295 if (pos + copied > inode->i_size)
1296 i_size_write(inode, pos + copied);
1297
Nick Piggin800d15a2007-10-16 01:25:03 -07001298 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001299 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001300 page_cache_release(page);
1301
Nick Piggin800d15a2007-10-16 01:25:03 -07001302 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1306{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001307 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001309 pgoff_t index;
1310 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001311 enum sgp_type sgp = SGP_READ;
1312
1313 /*
1314 * Might this read be for a stacking filesystem? Then when reading
1315 * holes of a sparse file, we actually need to allocate those pages,
1316 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1317 */
1318 if (segment_eq(get_fs(), KERNEL_DS))
1319 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 index = *ppos >> PAGE_CACHE_SHIFT;
1322 offset = *ppos & ~PAGE_CACHE_MASK;
1323
1324 for (;;) {
1325 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001326 pgoff_t end_index;
1327 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 loff_t i_size = i_size_read(inode);
1329
1330 end_index = i_size >> PAGE_CACHE_SHIFT;
1331 if (index > end_index)
1332 break;
1333 if (index == end_index) {
1334 nr = i_size & ~PAGE_CACHE_MASK;
1335 if (nr <= offset)
1336 break;
1337 }
1338
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001339 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (desc->error) {
1341 if (desc->error == -EINVAL)
1342 desc->error = 0;
1343 break;
1344 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001345 if (page)
1346 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 /*
1349 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001350 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
1352 nr = PAGE_CACHE_SIZE;
1353 i_size = i_size_read(inode);
1354 end_index = i_size >> PAGE_CACHE_SHIFT;
1355 if (index == end_index) {
1356 nr = i_size & ~PAGE_CACHE_MASK;
1357 if (nr <= offset) {
1358 if (page)
1359 page_cache_release(page);
1360 break;
1361 }
1362 }
1363 nr -= offset;
1364
1365 if (page) {
1366 /*
1367 * If users can be writing to this page using arbitrary
1368 * virtual addresses, take care about potential aliasing
1369 * before reading the page on the kernel side.
1370 */
1371 if (mapping_writably_mapped(mapping))
1372 flush_dcache_page(page);
1373 /*
1374 * Mark the page accessed if we read the beginning.
1375 */
1376 if (!offset)
1377 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001378 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001380 page_cache_get(page);
1381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 /*
1384 * Ok, we have the page, and it's up-to-date, so
1385 * now we can copy it to user space...
1386 *
1387 * The actor routine returns how many bytes were actually used..
1388 * NOTE! This may not be the same as how much of a user buffer
1389 * we filled up (we may be padding etc), so we can only update
1390 * "pos" here (the actor routine has to update the user buffer
1391 * pointers and the remaining count).
1392 */
1393 ret = actor(desc, page, offset, nr);
1394 offset += ret;
1395 index += offset >> PAGE_CACHE_SHIFT;
1396 offset &= ~PAGE_CACHE_MASK;
1397
1398 page_cache_release(page);
1399 if (ret != nr || !desc->count)
1400 break;
1401
1402 cond_resched();
1403 }
1404
1405 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1406 file_accessed(filp);
1407}
1408
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001409static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1410 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001412 struct file *filp = iocb->ki_filp;
1413 ssize_t retval;
1414 unsigned long seg;
1415 size_t count;
1416 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001418 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1419 if (retval)
1420 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001422 for (seg = 0; seg < nr_segs; seg++) {
1423 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001425 desc.written = 0;
1426 desc.arg.buf = iov[seg].iov_base;
1427 desc.count = iov[seg].iov_len;
1428 if (desc.count == 0)
1429 continue;
1430 desc.error = 0;
1431 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1432 retval += desc.written;
1433 if (desc.error) {
1434 retval = retval ?: desc.error;
1435 break;
1436 }
1437 if (desc.count > 0)
1438 break;
1439 }
1440 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Hugh Dickins708e3502011-07-25 17:12:32 -07001443static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1444 struct pipe_inode_info *pipe, size_t len,
1445 unsigned int flags)
1446{
1447 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001448 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001449 unsigned int loff, nr_pages, req_pages;
1450 struct page *pages[PIPE_DEF_BUFFERS];
1451 struct partial_page partial[PIPE_DEF_BUFFERS];
1452 struct page *page;
1453 pgoff_t index, end_index;
1454 loff_t isize, left;
1455 int error, page_nr;
1456 struct splice_pipe_desc spd = {
1457 .pages = pages,
1458 .partial = partial,
Eric Dumazet2c07f252012-06-12 15:24:40 +02001459 .nr_pages_max = PIPE_DEF_BUFFERS,
Hugh Dickins708e3502011-07-25 17:12:32 -07001460 .flags = flags,
1461 .ops = &page_cache_pipe_buf_ops,
1462 .spd_release = spd_release_page,
1463 };
1464
Hugh Dickins71f0e072011-07-25 17:12:33 -07001465 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001466 if (unlikely(*ppos >= isize))
1467 return 0;
1468
1469 left = isize - *ppos;
1470 if (unlikely(left < len))
1471 len = left;
1472
1473 if (splice_grow_spd(pipe, &spd))
1474 return -ENOMEM;
1475
1476 index = *ppos >> PAGE_CACHE_SHIFT;
1477 loff = *ppos & ~PAGE_CACHE_MASK;
1478 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1479 nr_pages = min(req_pages, pipe->buffers);
1480
Hugh Dickins708e3502011-07-25 17:12:32 -07001481 spd.nr_pages = find_get_pages_contig(mapping, index,
1482 nr_pages, spd.pages);
1483 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001484 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001485
Hugh Dickins708e3502011-07-25 17:12:32 -07001486 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001487 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1488 if (error)
1489 break;
1490 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001491 spd.pages[spd.nr_pages++] = page;
1492 index++;
1493 }
1494
Hugh Dickins708e3502011-07-25 17:12:32 -07001495 index = *ppos >> PAGE_CACHE_SHIFT;
1496 nr_pages = spd.nr_pages;
1497 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001498
Hugh Dickins708e3502011-07-25 17:12:32 -07001499 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1500 unsigned int this_len;
1501
1502 if (!len)
1503 break;
1504
Hugh Dickins708e3502011-07-25 17:12:32 -07001505 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1506 page = spd.pages[page_nr];
1507
Hugh Dickins71f0e072011-07-25 17:12:33 -07001508 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001509 error = shmem_getpage(inode, index, &page,
1510 SGP_CACHE, NULL);
1511 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001512 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001513 unlock_page(page);
1514 page_cache_release(spd.pages[page_nr]);
1515 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001516 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001517
1518 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001519 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1520 if (unlikely(!isize || index > end_index))
1521 break;
1522
Hugh Dickins708e3502011-07-25 17:12:32 -07001523 if (end_index == index) {
1524 unsigned int plen;
1525
Hugh Dickins708e3502011-07-25 17:12:32 -07001526 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1527 if (plen <= loff)
1528 break;
1529
Hugh Dickins708e3502011-07-25 17:12:32 -07001530 this_len = min(this_len, plen - loff);
1531 len = this_len;
1532 }
1533
1534 spd.partial[page_nr].offset = loff;
1535 spd.partial[page_nr].len = this_len;
1536 len -= this_len;
1537 loff = 0;
1538 spd.nr_pages++;
1539 index++;
1540 }
1541
Hugh Dickins708e3502011-07-25 17:12:32 -07001542 while (page_nr < nr_pages)
1543 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001544
1545 if (spd.nr_pages)
1546 error = splice_to_pipe(pipe, &spd);
1547
Eric Dumazet2c07f252012-06-12 15:24:40 +02001548 splice_shrink_spd(&spd);
Hugh Dickins708e3502011-07-25 17:12:32 -07001549
1550 if (error > 0) {
1551 *ppos += error;
1552 file_accessed(in);
1553 }
1554 return error;
1555}
1556
David Howells726c3342006-06-23 02:02:58 -07001557static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
David Howells726c3342006-06-23 02:02:58 -07001559 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 buf->f_type = TMPFS_MAGIC;
1562 buf->f_bsize = PAGE_CACHE_SIZE;
1563 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001564 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001566 buf->f_bavail =
1567 buf->f_bfree = sbinfo->max_blocks -
1568 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001569 }
1570 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 buf->f_files = sbinfo->max_inodes;
1572 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 /* else leave those fields 0 like simple_statfs */
1575 return 0;
1576}
1577
1578/*
1579 * File creation. Allocate an inode, and we're done..
1580 */
1581static int
Al Viro1a67aaf2011-07-26 01:52:52 -04001582shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001584 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 int error = -ENOSPC;
1586
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001587 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001589 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001590 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001591 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001592 if (error) {
1593 if (error != -EOPNOTSUPP) {
1594 iput(inode);
1595 return error;
1596 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001597 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001598#ifdef CONFIG_TMPFS_POSIX_ACL
1599 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001600 if (error) {
1601 iput(inode);
1602 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001603 }
Al Viro718deb62009-12-16 19:35:36 -05001604#else
1605 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001606#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 dir->i_size += BOGO_DIRENT_SIZE;
1608 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1609 d_instantiate(dentry, inode);
1610 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 return error;
1613}
1614
Al Viro18bb1db2011-07-26 01:41:39 -04001615static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 int error;
1618
1619 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1620 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001621 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return 0;
1623}
1624
Al Viro4acdaf22011-07-26 01:42:34 -04001625static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 struct nameidata *nd)
1627{
1628 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1629}
1630
1631/*
1632 * Link a file..
1633 */
1634static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1635{
1636 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001637 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /*
1640 * No ordinary (disk based) filesystem counts links as inodes;
1641 * but each new link needs a new dentry, pinning lowmem, and
1642 * tmpfs dentries cannot be pruned until they are unlinked.
1643 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001644 ret = shmem_reserve_inode(inode->i_sb);
1645 if (ret)
1646 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 dir->i_size += BOGO_DIRENT_SIZE;
1649 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001650 inc_nlink(inode);
Al Viro7de9c6e2010-10-23 11:11:40 -04001651 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 dget(dentry); /* Extra pinning count for the created dentry */
1653 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001654out:
1655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
1658static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1659{
1660 struct inode *inode = dentry->d_inode;
1661
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001662 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1663 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 dir->i_size -= BOGO_DIRENT_SIZE;
1666 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001667 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 dput(dentry); /* Undo the count from "create" - this does all the work */
1669 return 0;
1670}
1671
1672static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1673{
1674 if (!simple_empty(dentry))
1675 return -ENOTEMPTY;
1676
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001677 drop_nlink(dentry->d_inode);
1678 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return shmem_unlink(dir, dentry);
1680}
1681
1682/*
1683 * The VFS layer already does all the dentry stuff for rename,
1684 * we just have to decrement the usage count for the target if
1685 * it exists so that the VFS layer correctly free's it when it
1686 * gets overwritten.
1687 */
1688static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1689{
1690 struct inode *inode = old_dentry->d_inode;
1691 int they_are_dirs = S_ISDIR(inode->i_mode);
1692
1693 if (!simple_empty(new_dentry))
1694 return -ENOTEMPTY;
1695
1696 if (new_dentry->d_inode) {
1697 (void) shmem_unlink(new_dir, new_dentry);
1698 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001699 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001701 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001702 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704
1705 old_dir->i_size -= BOGO_DIRENT_SIZE;
1706 new_dir->i_size += BOGO_DIRENT_SIZE;
1707 old_dir->i_ctime = old_dir->i_mtime =
1708 new_dir->i_ctime = new_dir->i_mtime =
1709 inode->i_ctime = CURRENT_TIME;
1710 return 0;
1711}
1712
1713static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1714{
1715 int error;
1716 int len;
1717 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001718 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 char *kaddr;
1720 struct shmem_inode_info *info;
1721
1722 len = strlen(symname) + 1;
1723 if (len > PAGE_CACHE_SIZE)
1724 return -ENAMETOOLONG;
1725
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001726 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (!inode)
1728 return -ENOSPC;
1729
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001730 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001731 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001732 if (error) {
1733 if (error != -EOPNOTSUPP) {
1734 iput(inode);
1735 return error;
1736 }
1737 error = 0;
1738 }
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 info = SHMEM_I(inode);
1741 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001742 if (len <= SHORT_SYMLINK_LEN) {
1743 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1744 if (!info->symlink) {
1745 iput(inode);
1746 return -ENOMEM;
1747 }
1748 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 } else {
1750 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1751 if (error) {
1752 iput(inode);
1753 return error;
1754 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001755 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 inode->i_op = &shmem_symlink_inode_operations;
Cong Wang9b04c5f2011-11-25 23:14:39 +08001757 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 memcpy(kaddr, symname, len);
Cong Wang9b04c5f2011-11-25 23:14:39 +08001759 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001761 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 page_cache_release(page);
1763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 dir->i_size += BOGO_DIRENT_SIZE;
1765 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1766 d_instantiate(dentry, inode);
1767 dget(dentry);
1768 return 0;
1769}
1770
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001771static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001773 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001774 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001777static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
1779 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001780 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1781 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001782 if (page)
1783 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001784 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001787static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001790 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 kunmap(page);
1792 mark_page_accessed(page);
1793 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795}
1796
Eric Parisb09e0fa2011-05-24 17:12:39 -07001797#ifdef CONFIG_TMPFS_XATTR
1798/*
1799 * Superblocks without xattr inode operations may get some security.* xattr
1800 * support from the LSM "for free". As soon as we have any other xattrs
1801 * like ACLs, we also need to implement the security.* handlers at
1802 * filesystem level, though.
1803 */
1804
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001805/*
1806 * Allocate new xattr and copy in the value; but leave the name to callers.
1807 */
1808static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
1809{
1810 struct shmem_xattr *new_xattr;
1811 size_t len;
1812
1813 /* wrap around? */
1814 len = sizeof(*new_xattr) + size;
1815 if (len <= sizeof(*new_xattr))
1816 return NULL;
1817
1818 new_xattr = kmalloc(len, GFP_KERNEL);
1819 if (!new_xattr)
1820 return NULL;
1821
1822 new_xattr->size = size;
1823 memcpy(new_xattr->value, value, size);
1824 return new_xattr;
1825}
1826
1827/*
1828 * Callback for security_inode_init_security() for acquiring xattrs.
1829 */
1830static int shmem_initxattrs(struct inode *inode,
1831 const struct xattr *xattr_array,
1832 void *fs_info)
1833{
1834 struct shmem_inode_info *info = SHMEM_I(inode);
1835 const struct xattr *xattr;
1836 struct shmem_xattr *new_xattr;
1837 size_t len;
1838
1839 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
1840 new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
1841 if (!new_xattr)
1842 return -ENOMEM;
1843
1844 len = strlen(xattr->name) + 1;
1845 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
1846 GFP_KERNEL);
1847 if (!new_xattr->name) {
1848 kfree(new_xattr);
1849 return -ENOMEM;
1850 }
1851
1852 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
1853 XATTR_SECURITY_PREFIX_LEN);
1854 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
1855 xattr->name, len);
1856
1857 spin_lock(&info->lock);
1858 list_add(&new_xattr->list, &info->xattr_list);
1859 spin_unlock(&info->lock);
1860 }
1861
1862 return 0;
1863}
1864
Eric Parisb09e0fa2011-05-24 17:12:39 -07001865static int shmem_xattr_get(struct dentry *dentry, const char *name,
1866 void *buffer, size_t size)
1867{
1868 struct shmem_inode_info *info;
1869 struct shmem_xattr *xattr;
1870 int ret = -ENODATA;
1871
1872 info = SHMEM_I(dentry->d_inode);
1873
1874 spin_lock(&info->lock);
1875 list_for_each_entry(xattr, &info->xattr_list, list) {
1876 if (strcmp(name, xattr->name))
1877 continue;
1878
1879 ret = xattr->size;
1880 if (buffer) {
1881 if (size < xattr->size)
1882 ret = -ERANGE;
1883 else
1884 memcpy(buffer, xattr->value, xattr->size);
1885 }
1886 break;
1887 }
1888 spin_unlock(&info->lock);
1889 return ret;
1890}
1891
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001892static int shmem_xattr_set(struct inode *inode, const char *name,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001893 const void *value, size_t size, int flags)
1894{
Eric Parisb09e0fa2011-05-24 17:12:39 -07001895 struct shmem_inode_info *info = SHMEM_I(inode);
1896 struct shmem_xattr *xattr;
1897 struct shmem_xattr *new_xattr = NULL;
Eric Parisb09e0fa2011-05-24 17:12:39 -07001898 int err = 0;
1899
1900 /* value == NULL means remove */
1901 if (value) {
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001902 new_xattr = shmem_xattr_alloc(value, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001903 if (!new_xattr)
1904 return -ENOMEM;
1905
1906 new_xattr->name = kstrdup(name, GFP_KERNEL);
1907 if (!new_xattr->name) {
1908 kfree(new_xattr);
1909 return -ENOMEM;
1910 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001911 }
1912
1913 spin_lock(&info->lock);
1914 list_for_each_entry(xattr, &info->xattr_list, list) {
1915 if (!strcmp(name, xattr->name)) {
1916 if (flags & XATTR_CREATE) {
1917 xattr = new_xattr;
1918 err = -EEXIST;
1919 } else if (new_xattr) {
1920 list_replace(&xattr->list, &new_xattr->list);
1921 } else {
1922 list_del(&xattr->list);
1923 }
1924 goto out;
1925 }
1926 }
1927 if (flags & XATTR_REPLACE) {
1928 xattr = new_xattr;
1929 err = -ENODATA;
1930 } else {
1931 list_add(&new_xattr->list, &info->xattr_list);
1932 xattr = NULL;
1933 }
1934out:
1935 spin_unlock(&info->lock);
1936 if (xattr)
1937 kfree(xattr->name);
1938 kfree(xattr);
1939 return err;
1940}
1941
Eric Parisb09e0fa2011-05-24 17:12:39 -07001942static const struct xattr_handler *shmem_xattr_handlers[] = {
1943#ifdef CONFIG_TMPFS_POSIX_ACL
1944 &generic_acl_access_handler,
1945 &generic_acl_default_handler,
1946#endif
1947 NULL
1948};
1949
1950static int shmem_xattr_validate(const char *name)
1951{
1952 struct { const char *prefix; size_t len; } arr[] = {
1953 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1954 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1955 };
1956 int i;
1957
1958 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1959 size_t preflen = arr[i].len;
1960 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1961 if (!name[preflen])
1962 return -EINVAL;
1963 return 0;
1964 }
1965 }
1966 return -EOPNOTSUPP;
1967}
1968
1969static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1970 void *buffer, size_t size)
1971{
1972 int err;
1973
1974 /*
1975 * If this is a request for a synthetic attribute in the system.*
1976 * namespace use the generic infrastructure to resolve a handler
1977 * for it via sb->s_xattr.
1978 */
1979 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1980 return generic_getxattr(dentry, name, buffer, size);
1981
1982 err = shmem_xattr_validate(name);
1983 if (err)
1984 return err;
1985
1986 return shmem_xattr_get(dentry, name, buffer, size);
1987}
1988
1989static int shmem_setxattr(struct dentry *dentry, const char *name,
1990 const void *value, size_t size, int flags)
1991{
1992 int err;
1993
1994 /*
1995 * If this is a request for a synthetic attribute in the system.*
1996 * namespace use the generic infrastructure to resolve a handler
1997 * for it via sb->s_xattr.
1998 */
1999 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2000 return generic_setxattr(dentry, name, value, size, flags);
2001
2002 err = shmem_xattr_validate(name);
2003 if (err)
2004 return err;
2005
2006 if (size == 0)
2007 value = ""; /* empty EA, do not remove */
2008
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002009 return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002010
2011}
2012
2013static int shmem_removexattr(struct dentry *dentry, const char *name)
2014{
2015 int err;
2016
2017 /*
2018 * If this is a request for a synthetic attribute in the system.*
2019 * namespace use the generic infrastructure to resolve a handler
2020 * for it via sb->s_xattr.
2021 */
2022 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2023 return generic_removexattr(dentry, name);
2024
2025 err = shmem_xattr_validate(name);
2026 if (err)
2027 return err;
2028
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002029 return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002030}
2031
2032static bool xattr_is_trusted(const char *name)
2033{
2034 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2035}
2036
2037static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2038{
2039 bool trusted = capable(CAP_SYS_ADMIN);
2040 struct shmem_xattr *xattr;
2041 struct shmem_inode_info *info;
2042 size_t used = 0;
2043
2044 info = SHMEM_I(dentry->d_inode);
2045
2046 spin_lock(&info->lock);
2047 list_for_each_entry(xattr, &info->xattr_list, list) {
2048 size_t len;
2049
2050 /* skip "trusted." attributes for unprivileged callers */
2051 if (!trusted && xattr_is_trusted(xattr->name))
2052 continue;
2053
2054 len = strlen(xattr->name) + 1;
2055 used += len;
2056 if (buffer) {
2057 if (size < used) {
2058 used = -ERANGE;
2059 break;
2060 }
2061 memcpy(buffer, xattr->name, len);
2062 buffer += len;
2063 }
2064 }
2065 spin_unlock(&info->lock);
2066
2067 return used;
2068}
2069#endif /* CONFIG_TMPFS_XATTR */
2070
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002071static const struct inode_operations shmem_short_symlink_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 .readlink = generic_readlink,
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002073 .follow_link = shmem_follow_short_symlink,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002074#ifdef CONFIG_TMPFS_XATTR
2075 .setxattr = shmem_setxattr,
2076 .getxattr = shmem_getxattr,
2077 .listxattr = shmem_listxattr,
2078 .removexattr = shmem_removexattr,
2079#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080};
2081
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002082static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 .readlink = generic_readlink,
2084 .follow_link = shmem_follow_link,
2085 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002086#ifdef CONFIG_TMPFS_XATTR
2087 .setxattr = shmem_setxattr,
2088 .getxattr = shmem_getxattr,
2089 .listxattr = shmem_listxattr,
2090 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002091#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002092};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002093
David M. Grimes91828a42006-10-17 00:09:45 -07002094static struct dentry *shmem_get_parent(struct dentry *child)
2095{
2096 return ERR_PTR(-ESTALE);
2097}
2098
2099static int shmem_match(struct inode *ino, void *vfh)
2100{
2101 __u32 *fh = vfh;
2102 __u64 inum = fh[2];
2103 inum = (inum << 32) | fh[1];
2104 return ino->i_ino == inum && fh[0] == ino->i_generation;
2105}
2106
Christoph Hellwig480b1162007-10-21 16:42:13 -07002107static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2108 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07002109{
David M. Grimes91828a42006-10-17 00:09:45 -07002110 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07002111 struct dentry *dentry = NULL;
Hugh Dickins530258f2012-10-07 20:32:51 -07002112 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07002113
Christoph Hellwig480b1162007-10-21 16:42:13 -07002114 if (fh_len < 3)
2115 return NULL;
2116
Hugh Dickins530258f2012-10-07 20:32:51 -07002117 inum = fid->raw[2];
2118 inum = (inum << 32) | fid->raw[1];
2119
Christoph Hellwig480b1162007-10-21 16:42:13 -07002120 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2121 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07002122 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07002123 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07002124 iput(inode);
2125 }
2126
Christoph Hellwig480b1162007-10-21 16:42:13 -07002127 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07002128}
2129
2130static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2131 int connectable)
2132{
2133 struct inode *inode = dentry->d_inode;
2134
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302135 if (*len < 3) {
2136 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07002137 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302138 }
David M. Grimes91828a42006-10-17 00:09:45 -07002139
Al Viro1d3382c2010-10-23 15:19:20 -04002140 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07002141 /* Unfortunately insert_inode_hash is not idempotent,
2142 * so as we hash inodes here rather than at creation
2143 * time, we need a lock to ensure we only try
2144 * to do it once
2145 */
2146 static DEFINE_SPINLOCK(lock);
2147 spin_lock(&lock);
Al Viro1d3382c2010-10-23 15:19:20 -04002148 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07002149 __insert_inode_hash(inode,
2150 inode->i_ino + inode->i_generation);
2151 spin_unlock(&lock);
2152 }
2153
2154 fh[0] = inode->i_generation;
2155 fh[1] = inode->i_ino;
2156 fh[2] = ((__u64)inode->i_ino) >> 32;
2157
2158 *len = 3;
2159 return 1;
2160}
2161
Christoph Hellwig39655162007-10-21 16:42:17 -07002162static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07002163 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07002164 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07002165 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07002166};
2167
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002168static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2169 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 char *this_char, *value, *rest;
2172
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00002173 while (options != NULL) {
2174 this_char = options;
2175 for (;;) {
2176 /*
2177 * NUL-terminate this option: unfortunately,
2178 * mount options form a comma-separated list,
2179 * but mpol's nodelist may also contain commas.
2180 */
2181 options = strchr(options, ',');
2182 if (options == NULL)
2183 break;
2184 options++;
2185 if (!isdigit(*options)) {
2186 options[-1] = '\0';
2187 break;
2188 }
2189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (!*this_char)
2191 continue;
2192 if ((value = strchr(this_char,'=')) != NULL) {
2193 *value++ = 0;
2194 } else {
2195 printk(KERN_ERR
2196 "tmpfs: No value for mount option '%s'\n",
2197 this_char);
2198 return 1;
2199 }
2200
2201 if (!strcmp(this_char,"size")) {
2202 unsigned long long size;
2203 size = memparse(value,&rest);
2204 if (*rest == '%') {
2205 size <<= PAGE_SHIFT;
2206 size *= totalram_pages;
2207 do_div(size, 100);
2208 rest++;
2209 }
2210 if (*rest)
2211 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002212 sbinfo->max_blocks =
2213 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002215 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (*rest)
2217 goto bad_val;
2218 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002219 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (*rest)
2221 goto bad_val;
2222 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002223 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002225 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (*rest)
2227 goto bad_val;
2228 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002229 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002231 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (*rest)
2233 goto bad_val;
2234 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002235 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002237 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (*rest)
2239 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002240 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002241 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08002242 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 } else {
2244 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2245 this_char);
2246 return 1;
2247 }
2248 }
2249 return 0;
2250
2251bad_val:
2252 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2253 value, this_char);
2254 return 1;
2255
2256}
2257
2258static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2259{
2260 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002261 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002262 unsigned long inodes;
2263 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Greg Thelen06c79762013-02-22 16:36:01 -08002265 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002266 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002267 return error;
2268
2269 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002270 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002271 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002272 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002273 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002274 goto out;
2275 /*
Hugh Dickins54af6042011-08-03 16:21:24 -07002276 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002277 * but we must separately disallow unlimited->limited, because
2278 * in that case we have no record of how much is already in use.
2279 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002280 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002281 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002282 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002283 goto out;
2284
2285 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002286 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002287 sbinfo->max_inodes = config.max_inodes;
2288 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002289
Greg Thelen06c79762013-02-22 16:36:01 -08002290 /*
2291 * Preserve previous mempolicy unless mpol remount option was specified.
2292 */
2293 if (config.mpol) {
2294 mpol_put(sbinfo->mpol);
2295 sbinfo->mpol = config.mpol; /* transfers initial ref */
2296 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002297out:
2298 spin_unlock(&sbinfo->stat_lock);
2299 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002301
Al Viro34c80b12011-12-08 21:32:45 -05002302static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002303{
Al Viro34c80b12011-12-08 21:32:45 -05002304 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002305
2306 if (sbinfo->max_blocks != shmem_default_max_blocks())
2307 seq_printf(seq, ",size=%luk",
2308 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2309 if (sbinfo->max_inodes != shmem_default_max_inodes())
2310 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2311 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04002312 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002313 if (sbinfo->uid != 0)
2314 seq_printf(seq, ",uid=%u", sbinfo->uid);
2315 if (sbinfo->gid != 0)
2316 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002317 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002318 return 0;
2319}
2320#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322static void shmem_put_super(struct super_block *sb)
2323{
Hugh Dickins602586a2010-08-17 15:23:56 -07002324 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2325
2326 percpu_counter_destroy(&sbinfo->used_blocks);
2327 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 sb->s_fs_info = NULL;
2329}
2330
Kay Sievers2b2af542009-04-30 15:23:42 +02002331int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002334 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002335 int err = -ENOMEM;
2336
2337 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002338 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002339 L1_CACHE_BYTES), GFP_KERNEL);
2340 if (!sbinfo)
2341 return -ENOMEM;
2342
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002343 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002344 sbinfo->uid = current_fsuid();
2345 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002346 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002348#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /*
2350 * Per default we only allow half of the physical ram per
2351 * tmpfs instance, limiting inodes to one per page of lowmem;
2352 * but the internal instance is left unlimited.
2353 */
2354 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002355 sbinfo->max_blocks = shmem_default_max_blocks();
2356 sbinfo->max_inodes = shmem_default_max_inodes();
2357 if (shmem_parse_options(data, sbinfo, false)) {
2358 err = -EINVAL;
2359 goto failed;
2360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
David M. Grimes91828a42006-10-17 00:09:45 -07002362 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363#else
2364 sb->s_flags |= MS_NOUSER;
2365#endif
2366
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002367 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002368 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2369 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002370 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002371
Hugh Dickins285b2c42011-08-03 16:21:20 -07002372 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 sb->s_blocksize = PAGE_CACHE_SIZE;
2374 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2375 sb->s_magic = TMPFS_MAGIC;
2376 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002377 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002378#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002379 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002380#endif
2381#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002382 sb->s_flags |= MS_POSIXACL;
2383#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002384
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002385 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (!inode)
2387 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002388 inode->i_uid = sbinfo->uid;
2389 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05002390 sb->s_root = d_make_root(inode);
2391 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05002392 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return 0;
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395failed:
2396 shmem_put_super(sb);
2397 return err;
2398}
2399
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002400static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402static struct inode *shmem_alloc_inode(struct super_block *sb)
2403{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002404 struct shmem_inode_info *info;
2405 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2406 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002408 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002411static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e32011-01-07 17:49:49 +11002412{
2413 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e32011-01-07 17:49:49 +11002414 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2415}
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417static void shmem_destroy_inode(struct inode *inode)
2418{
Al Viro09208d12011-07-26 03:15:03 -04002419 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002421 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002424static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002426 struct shmem_inode_info *info = foo;
2427 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002430static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
2432 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2433 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002434 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return 0;
2436}
2437
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002438static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002440 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002443static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002445 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002447 .write_begin = shmem_write_begin,
2448 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002450 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002451 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452};
2453
Helge Deller15ad7cd2006-12-06 20:40:36 -08002454static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 .mmap = shmem_mmap,
2456#ifdef CONFIG_TMPFS
2457 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002458 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002459 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002460 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002461 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002462 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002463 .splice_read = shmem_file_splice_read,
Hugh Dickinsae976412007-06-04 10:00:39 +02002464 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465#endif
2466};
2467
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002468static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002469 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002470 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002471#ifdef CONFIG_TMPFS_XATTR
2472 .setxattr = shmem_setxattr,
2473 .getxattr = shmem_getxattr,
2474 .listxattr = shmem_listxattr,
2475 .removexattr = shmem_removexattr,
2476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477};
2478
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002479static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480#ifdef CONFIG_TMPFS
2481 .create = shmem_create,
2482 .lookup = simple_lookup,
2483 .link = shmem_link,
2484 .unlink = shmem_unlink,
2485 .symlink = shmem_symlink,
2486 .mkdir = shmem_mkdir,
2487 .rmdir = shmem_rmdir,
2488 .mknod = shmem_mknod,
2489 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002491#ifdef CONFIG_TMPFS_XATTR
2492 .setxattr = shmem_setxattr,
2493 .getxattr = shmem_getxattr,
2494 .listxattr = shmem_listxattr,
2495 .removexattr = shmem_removexattr,
2496#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002497#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002498 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002499#endif
2500};
2501
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002502static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002503#ifdef CONFIG_TMPFS_XATTR
2504 .setxattr = shmem_setxattr,
2505 .getxattr = shmem_getxattr,
2506 .listxattr = shmem_listxattr,
2507 .removexattr = shmem_removexattr,
2508#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002509#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002510 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002511#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512};
2513
Hugh Dickins759b9772007-03-05 00:30:28 -08002514static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 .alloc_inode = shmem_alloc_inode,
2516 .destroy_inode = shmem_destroy_inode,
2517#ifdef CONFIG_TMPFS
2518 .statfs = shmem_statfs,
2519 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002520 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521#endif
Al Viro1f895f72010-06-05 19:10:41 -04002522 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 .drop_inode = generic_delete_inode,
2524 .put_super = shmem_put_super,
2525};
2526
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002527static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002528 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529#ifdef CONFIG_NUMA
2530 .set_policy = shmem_set_policy,
2531 .get_policy = shmem_get_policy,
2532#endif
2533};
2534
Al Viro3c26ff62010-07-25 11:46:36 +04002535static struct dentry *shmem_mount(struct file_system_type *fs_type,
2536 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Al Viro3c26ff62010-07-25 11:46:36 +04002538 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002541static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 .owner = THIS_MODULE,
2543 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002544 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 .kill_sb = kill_litter_super,
2546};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002548int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
2550 int error;
2551
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002552 error = bdi_init(&shmem_backing_dev_info);
2553 if (error)
2554 goto out4;
2555
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002556 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (error)
2558 goto out3;
2559
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002560 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (error) {
2562 printk(KERN_ERR "Could not register tmpfs\n");
2563 goto out2;
2564 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002565
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002566 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2567 shmem_fs_type.name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (IS_ERR(shm_mnt)) {
2569 error = PTR_ERR(shm_mnt);
2570 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2571 goto out1;
2572 }
2573 return 0;
2574
2575out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002576 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002578 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002580 bdi_destroy(&shmem_backing_dev_info);
2581out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 shm_mnt = ERR_PTR(error);
2583 return error;
2584}
Matt Mackall853ac432009-01-06 14:40:20 -08002585
2586#else /* !CONFIG_SHMEM */
2587
2588/*
2589 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2590 *
2591 * This is intended for small system where the benefits of the full
2592 * shmem code (swap-backed and resource-limited) are outweighed by
2593 * their complexity. On systems without swap this code should be
2594 * effectively equivalent, but much lighter weight.
2595 */
2596
2597#include <linux/ramfs.h>
2598
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002599static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08002600 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002601 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002602 .kill_sb = kill_litter_super,
2603};
2604
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002605int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002606{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002607 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08002608
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002609 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08002610 BUG_ON(IS_ERR(shm_mnt));
2611
2612 return 0;
2613}
2614
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002615int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08002616{
2617 return 0;
2618}
2619
Hugh Dickins3f96b792009-09-21 17:03:37 -07002620int shmem_lock(struct file *file, int lock, struct user_struct *user)
2621{
2622 return 0;
2623}
2624
Hugh Dickins24513262012-01-20 14:34:21 -08002625void shmem_unlock_mapping(struct address_space *mapping)
2626{
2627}
2628
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002629void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07002630{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002631 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07002632}
2633EXPORT_SYMBOL_GPL(shmem_truncate_range);
2634
Hugh Dickinsa62f3742014-06-23 13:22:06 -07002635int vmtruncate_range(struct inode *inode, loff_t lstart, loff_t lend)
2636{
2637 /* Only CONFIG_SHMEM shmem.c ever supported i_op->truncate_range(). */
2638 return -ENOSYS;
2639}
2640
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002641#define shmem_vm_ops generic_file_vm_ops
2642#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002643#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002644#define shmem_acct_size(flags, size) 0
2645#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002646
2647#endif /* CONFIG_SHMEM */
2648
2649/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Randy Dunlap46711812008-03-19 17:00:41 -07002651/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 * @name: name for dentry (to be seen in /proc/<pid>/maps
2654 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002655 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002657struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
2659 int error;
2660 struct file *file;
2661 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002662 struct path path;
2663 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 struct qstr this;
2665
2666 if (IS_ERR(shm_mnt))
2667 return (void *)shm_mnt;
2668
Hugh Dickins285b2c42011-08-03 16:21:20 -07002669 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return ERR_PTR(-EINVAL);
2671
2672 if (shmem_acct_size(flags, size))
2673 return ERR_PTR(-ENOMEM);
2674
2675 error = -ENOMEM;
2676 this.name = name;
2677 this.len = strlen(name);
2678 this.hash = 0; /* will go */
2679 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002680 path.dentry = d_alloc(root, &this);
2681 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002683 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002686 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002688 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Al Viro2c48b9c2009-08-09 00:52:35 +04002690 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02002692 clear_nlink(inode); /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002693#ifndef CONFIG_MMU
2694 error = ramfs_nommu_expand_for_mapping(inode, size);
2695 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002696 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002697#endif
Al Viro4b42af82009-08-05 18:25:56 +04002698
2699 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002700 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002701 &shmem_file_operations);
2702 if (!file)
2703 goto put_dentry;
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return file;
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002708 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709put_memory:
2710 shmem_unacct_size(flags, size);
2711 return ERR_PTR(error);
2712}
Keith Packard395e0dd2008-06-20 00:08:06 -07002713EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Randy Dunlap46711812008-03-19 17:00:41 -07002715/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2718 */
2719int shmem_zero_setup(struct vm_area_struct *vma)
2720{
2721 struct file *file;
2722 loff_t size = vma->vm_end - vma->vm_start;
2723
2724 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2725 if (IS_ERR(file))
2726 return PTR_ERR(file);
2727
2728 if (vma->vm_file)
2729 fput(vma->vm_file);
2730 vma->vm_file = file;
2731 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c362011-03-22 16:33:43 -07002732 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return 0;
2734}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002735
2736/**
2737 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2738 * @mapping: the page's address_space
2739 * @index: the page index
2740 * @gfp: the page allocator flags to use if allocating
2741 *
2742 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2743 * with any new page allocations done using the specified allocation flags.
2744 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2745 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2746 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2747 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002748 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2749 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002750 */
2751struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2752 pgoff_t index, gfp_t gfp)
2753{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002754#ifdef CONFIG_SHMEM
2755 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002756 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002757 int error;
2758
2759 BUG_ON(mapping->a_ops != &shmem_aops);
2760 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2761 if (error)
2762 page = ERR_PTR(error);
2763 else
2764 unlock_page(page);
2765 return page;
2766#else
2767 /*
2768 * The tiny !SHMEM case uses ramfs without swap
2769 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002770 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002771#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002772}
2773EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);