blob: 04d11f9981b123f4e772aac5170190b349d48fbf [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 {
Hugh Dickins0f5a4a02014-07-23 14:00:10 -070085 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickinsa62f3742014-06-23 13:22:06 -070086 pgoff_t start; /* start of range currently being fallocated */
87 pgoff_t next; /* the next page offset to be fallocated */
88};
89
Eric Parisb09e0fa2011-05-24 17:12:39 -070090struct shmem_xattr {
91 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
92 char *name; /* xattr name */
93 size_t size;
94 char value[0];
95};
96
Hugh Dickins285b2c42011-08-03 16:21:20 -070097/* Flag allocation requirements to shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098enum sgp_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 SGP_READ, /* don't exceed i_size, don't allocate page */
100 SGP_CACHE, /* don't exceed i_size, may allocate page */
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -0800101 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 SGP_WRITE, /* may exceed i_size, may allocate page */
103};
104
Andrew Mortonb76db732008-02-08 04:21:49 -0800105#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800106static unsigned long shmem_default_max_blocks(void)
107{
108 return totalram_pages / 2;
109}
110
111static unsigned long shmem_default_max_inodes(void)
112{
113 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
114}
Andrew Mortonb76db732008-02-08 04:21:49 -0800115#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800116
Hugh Dickins68da9f02011-07-25 17:12:34 -0700117static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
118 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
119
120static inline int shmem_getpage(struct inode *inode, pgoff_t index,
121 struct page **pagep, enum sgp_type sgp, int *fault_type)
122{
123 return shmem_getpage_gfp(inode, index, pagep, sgp,
124 mapping_gfp_mask(inode->i_mapping), fault_type);
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
128{
129 return sb->s_fs_info;
130}
131
132/*
133 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
134 * for shared memory and for shared anonymous (/dev/zero) mappings
135 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
136 * consistent with the pre-accounting of private mappings ...
137 */
138static inline int shmem_acct_size(unsigned long flags, loff_t size)
139{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000140 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000141 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144static inline void shmem_unacct_size(unsigned long flags, loff_t size)
145{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000146 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 vm_unacct_memory(VM_ACCT(size));
148}
149
150/*
151 * ... whereas tmpfs objects are accounted incrementally as
152 * pages are allocated, in order to allow huge sparse files.
153 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
154 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
155 */
156static inline int shmem_acct_block(unsigned long flags)
157{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000158 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000159 security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162static inline void shmem_unacct_blocks(unsigned long flags, long pages)
163{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000164 if (flags & VM_NORESERVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
166}
167
Hugh Dickins759b9772007-03-05 00:30:28 -0800168static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700169static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800170static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800171static const struct inode_operations shmem_inode_operations;
172static const struct inode_operations shmem_dir_inode_operations;
173static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400174static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700176static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .ra_pages = 0, /* No readahead */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700178 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179};
180
181static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800182static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800184static int shmem_reserve_inode(struct super_block *sb)
185{
186 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
187 if (sbinfo->max_inodes) {
188 spin_lock(&sbinfo->stat_lock);
189 if (!sbinfo->free_inodes) {
190 spin_unlock(&sbinfo->stat_lock);
191 return -ENOSPC;
192 }
193 sbinfo->free_inodes--;
194 spin_unlock(&sbinfo->stat_lock);
195 }
196 return 0;
197}
198
199static void shmem_free_inode(struct super_block *sb)
200{
201 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
202 if (sbinfo->max_inodes) {
203 spin_lock(&sbinfo->stat_lock);
204 sbinfo->free_inodes++;
205 spin_unlock(&sbinfo->stat_lock);
206 }
207}
208
Randy Dunlap46711812008-03-19 17:00:41 -0700209/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700210 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * @inode: inode to recalc
212 *
213 * We have to calculate the free blocks since the mm can drop
214 * undirtied hole pages behind our back.
215 *
216 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
217 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
218 *
219 * It has to be called with the spinlock held.
220 */
221static void shmem_recalc_inode(struct inode *inode)
222{
223 struct shmem_inode_info *info = SHMEM_I(inode);
224 long freed;
225
226 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
227 if (freed > 0) {
Hugh Dickins54af6042011-08-03 16:21:24 -0700228 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
229 if (sbinfo->max_blocks)
230 percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 info->alloced -= freed;
Hugh Dickins54af6042011-08-03 16:21:24 -0700232 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 shmem_unacct_blocks(info->flags, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235}
236
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700237/*
238 * Replace item expected in radix tree by a new item, while holding tree lock.
239 */
240static int shmem_radix_tree_replace(struct address_space *mapping,
241 pgoff_t index, void *expected, void *replacement)
242{
243 void **pslot;
244 void *item = NULL;
245
246 VM_BUG_ON(!expected);
247 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
248 if (pslot)
249 item = radix_tree_deref_slot_protected(pslot,
250 &mapping->tree_lock);
251 if (item != expected)
252 return -ENOENT;
253 if (replacement)
254 radix_tree_replace_slot(pslot, replacement);
255 else
256 radix_tree_delete(&mapping->page_tree, index);
257 return 0;
258}
259
260/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700261 * Like add_to_page_cache_locked, but error if expected item has gone.
262 */
263static int shmem_add_to_page_cache(struct page *page,
264 struct address_space *mapping,
265 pgoff_t index, gfp_t gfp, void *expected)
266{
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700267 int error = 0;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700268
269 VM_BUG_ON(!PageLocked(page));
270 VM_BUG_ON(!PageSwapBacked(page));
271
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700272 if (!expected)
273 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
274 if (!error) {
275 page_cache_get(page);
276 page->mapping = mapping;
277 page->index = index;
278
279 spin_lock_irq(&mapping->tree_lock);
280 if (!expected)
281 error = radix_tree_insert(&mapping->page_tree,
282 index, page);
283 else
284 error = shmem_radix_tree_replace(mapping, index,
285 expected, page);
286 if (!error) {
287 mapping->nrpages++;
288 __inc_zone_page_state(page, NR_FILE_PAGES);
289 __inc_zone_page_state(page, NR_SHMEM);
290 spin_unlock_irq(&mapping->tree_lock);
291 } else {
292 page->mapping = NULL;
293 spin_unlock_irq(&mapping->tree_lock);
294 page_cache_release(page);
295 }
296 if (!expected)
297 radix_tree_preload_end();
298 }
299 if (error)
300 mem_cgroup_uncharge_cache_page(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700301 return error;
302}
303
304/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700305 * Like delete_from_page_cache, but substitutes swap for page.
306 */
307static void shmem_delete_from_page_cache(struct page *page, void *radswap)
308{
309 struct address_space *mapping = page->mapping;
310 int error;
311
312 spin_lock_irq(&mapping->tree_lock);
313 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
314 page->mapping = NULL;
315 mapping->nrpages--;
316 __dec_zone_page_state(page, NR_FILE_PAGES);
317 __dec_zone_page_state(page, NR_SHMEM);
318 spin_unlock_irq(&mapping->tree_lock);
319 page_cache_release(page);
320 BUG_ON(error);
321}
322
323/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700324 * Like find_get_pages, but collecting swap entries as well as pages.
325 */
326static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
327 pgoff_t start, unsigned int nr_pages,
328 struct page **pages, pgoff_t *indices)
329{
330 unsigned int i;
331 unsigned int ret;
332 unsigned int nr_found;
333
334 rcu_read_lock();
335restart:
336 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
337 (void ***)pages, indices, start, nr_pages);
338 ret = 0;
339 for (i = 0; i < nr_found; i++) {
340 struct page *page;
341repeat:
342 page = radix_tree_deref_slot((void **)pages[i]);
343 if (unlikely(!page))
344 continue;
345 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700346 if (radix_tree_deref_retry(page))
347 goto restart;
348 /*
349 * Otherwise, we must be storing a swap entry
350 * here as an exceptional entry: so return it
351 * without attempting to raise page count.
352 */
353 goto export;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700354 }
355 if (!page_cache_get_speculative(page))
356 goto repeat;
357
358 /* Has the page moved? */
359 if (unlikely(page != *((void **)pages[i]))) {
360 page_cache_release(page);
361 goto repeat;
362 }
363export:
364 indices[ret] = indices[i];
365 pages[ret] = page;
366 ret++;
367 }
368 if (unlikely(!ret && nr_found))
369 goto restart;
370 rcu_read_unlock();
371 return ret;
372}
373
374/*
375 * Remove swap entry from radix tree, free the swap and its page cache.
376 */
377static int shmem_free_swap(struct address_space *mapping,
378 pgoff_t index, void *radswap)
379{
380 int error;
381
382 spin_lock_irq(&mapping->tree_lock);
383 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
384 spin_unlock_irq(&mapping->tree_lock);
385 if (!error)
386 free_swap_and_cache(radix_to_swp_entry(radswap));
387 return error;
388}
389
390/*
391 * Pagevec may contain swap entries, so shuffle up pages before releasing.
392 */
Hugh Dickins24513262012-01-20 14:34:21 -0800393static void shmem_deswap_pagevec(struct pagevec *pvec)
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700394{
395 int i, j;
396
397 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
398 struct page *page = pvec->pages[i];
399 if (!radix_tree_exceptional_entry(page))
400 pvec->pages[j++] = page;
401 }
402 pvec->nr = j;
Hugh Dickins24513262012-01-20 14:34:21 -0800403}
404
405/*
406 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
407 */
408void shmem_unlock_mapping(struct address_space *mapping)
409{
410 struct pagevec pvec;
411 pgoff_t indices[PAGEVEC_SIZE];
412 pgoff_t index = 0;
413
414 pagevec_init(&pvec, 0);
415 /*
416 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
417 */
418 while (!mapping_unevictable(mapping)) {
419 /*
420 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
421 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
422 */
423 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
424 PAGEVEC_SIZE, pvec.pages, indices);
425 if (!pvec.nr)
426 break;
427 index = indices[pvec.nr - 1] + 1;
428 shmem_deswap_pagevec(&pvec);
429 check_move_unevictable_pages(pvec.pages, pvec.nr);
430 pagevec_release(&pvec);
431 cond_resched();
432 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700433}
434
435/*
436 * Remove range of pages and swap entries from radix tree, and free them.
437 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700438void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700440 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700442 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700443 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700444 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700445 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700446 pgoff_t indices[PAGEVEC_SIZE];
447 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700448 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700449 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700451 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
452
453 pagevec_init(&pvec, 0);
454 index = start;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700455 while (index <= end) {
456 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
457 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
458 pvec.pages, indices);
459 if (!pvec.nr)
460 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700461 mem_cgroup_uncharge_start();
462 for (i = 0; i < pagevec_count(&pvec); i++) {
463 struct page *page = pvec.pages[i];
464
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700465 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700466 if (index > end)
467 break;
468
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700469 if (radix_tree_exceptional_entry(page)) {
470 nr_swaps_freed += !shmem_free_swap(mapping,
471 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700472 continue;
473 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700474
475 if (!trylock_page(page))
476 continue;
477 if (page->mapping == mapping) {
478 VM_BUG_ON(PageWriteback(page));
479 truncate_inode_page(mapping, page);
480 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700481 unlock_page(page);
482 }
Hugh Dickins24513262012-01-20 14:34:21 -0800483 shmem_deswap_pagevec(&pvec);
484 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700485 mem_cgroup_uncharge_end();
486 cond_resched();
487 index++;
488 }
489
490 if (partial) {
491 struct page *page = NULL;
492 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
493 if (page) {
494 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
495 set_page_dirty(page);
496 unlock_page(page);
497 page_cache_release(page);
498 }
499 }
500
501 index = start;
502 for ( ; ; ) {
503 cond_resched();
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700504 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
505 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
506 pvec.pages, indices);
507 if (!pvec.nr) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700508 if (index == start)
509 break;
510 index = start;
511 continue;
512 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700513 if (index == start && indices[0] > end) {
Hugh Dickins24513262012-01-20 14:34:21 -0800514 shmem_deswap_pagevec(&pvec);
515 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700516 break;
517 }
518 mem_cgroup_uncharge_start();
519 for (i = 0; i < pagevec_count(&pvec); i++) {
520 struct page *page = pvec.pages[i];
521
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700522 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700523 if (index > end)
524 break;
525
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700526 if (radix_tree_exceptional_entry(page)) {
527 nr_swaps_freed += !shmem_free_swap(mapping,
528 index, page);
529 continue;
530 }
531
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700532 lock_page(page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700533 if (page->mapping == mapping) {
534 VM_BUG_ON(PageWriteback(page));
535 truncate_inode_page(mapping, page);
536 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700537 unlock_page(page);
538 }
Hugh Dickins24513262012-01-20 14:34:21 -0800539 shmem_deswap_pagevec(&pvec);
540 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700541 mem_cgroup_uncharge_end();
542 index++;
543 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700546 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 shmem_recalc_inode(inode);
548 spin_unlock(&info->lock);
549
Hugh Dickins285b2c42011-08-03 16:21:20 -0700550 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700552EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Hugh Dickins94c1e622011-06-27 16:18:03 -0700554static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int error;
558
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200559 error = inode_change_ok(inode, attr);
560 if (error)
561 return error;
562
Hugh Dickins94c1e622011-06-27 16:18:03 -0700563 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
564 loff_t oldsize = inode->i_size;
565 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000566
Hugh Dickins94c1e622011-06-27 16:18:03 -0700567 if (newsize != oldsize) {
568 i_size_write(inode, newsize);
569 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
570 }
571 if (newsize < oldsize) {
572 loff_t holebegin = round_up(newsize, PAGE_SIZE);
573 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
574 shmem_truncate_range(inode, newsize, (loff_t)-1);
575 /* unmap again to remove racily COWed private pages */
576 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200580 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700581#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200582 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100583 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700584#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return error;
586}
587
Al Viro1f895f72010-06-05 19:10:41 -0400588static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700591 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000593 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 shmem_unacct_size(info->flags, inode->i_size);
595 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000596 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800598 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800600 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Hugh Dickins69f07ec2011-08-03 16:21:26 -0700602 } else
603 kfree(info->symlink);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700604
605 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
606 kfree(xattr->name);
607 kfree(xattr);
608 }
Hugh Dickinsec592422012-11-16 14:15:04 -0800609 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800610 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400611 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700614/*
615 * If swap found in inode, free it and move page from swapcache to filecache.
616 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700617static int shmem_unuse_inode(struct shmem_inode_info *info,
618 swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700620 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700621 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700622 pgoff_t index;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800623 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700625 radswap = swp_to_radix_entry(swap);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700626 index = radix_tree_locate_item(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700627 if (index == -1)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700628 return 0;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800629
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800630 /*
631 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400632 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800633 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700634 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800635 */
636 if (shmem_swaplist.next != &info->swaplist)
637 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800638
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800639 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700640 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
641 * but also to hold up shmem_evict_inode(): so inode cannot be freed
642 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800643 */
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700644 error = shmem_add_to_page_cache(page, mapping, index,
645 GFP_NOWAIT, radswap);
Hugh Dickins778dd892011-05-11 15:13:37 -0700646 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700647
Hugh Dickins48f170f2011-07-25 17:12:37 -0700648 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700649 /*
650 * Truncation and eviction use free_swap_and_cache(), which
651 * only does trylock page: if we raced, best clean up here.
652 */
Hugh Dickins73b12622008-02-04 22:28:50 -0800653 delete_from_swap_cache(page);
654 set_page_dirty(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700655 if (!error) {
656 spin_lock(&info->lock);
657 info->swapped--;
658 spin_unlock(&info->lock);
659 swap_free(swap);
660 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800661 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800663 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700667 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700669int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700671 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct shmem_inode_info *info;
673 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700674 int error;
675
676 /*
677 * Charge page using GFP_KERNEL while we can wait, before taking
678 * the shmem_swaplist_mutex which might hold up shmem_writepage().
679 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -0700680 */
681 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
682 if (error)
683 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700684 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800686 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700687 list_for_each_safe(this, next, &shmem_swaplist) {
688 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700689 if (info->swapped)
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700690 found = shmem_unuse_inode(info, swap, page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700691 else
692 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800693 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800694 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700695 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800697 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700698
Hugh Dickins778dd892011-05-11 15:13:37 -0700699 if (!found)
700 mem_cgroup_uncharge_cache_page(page);
701 if (found < 0)
702 error = found;
703out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800704 unlock_page(page);
705 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700706 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709/*
710 * Move the page from the page cache to the swap cache.
711 */
712static int shmem_writepage(struct page *page, struct writeback_control *wbc)
713{
714 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700717 swp_entry_t swap;
718 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 mapping = page->mapping;
722 index = page->index;
723 inode = mapping->host;
724 info = SHMEM_I(inode);
725 if (info->flags & VM_LOCKED)
726 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800727 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto redirty;
729
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800730 /*
731 * shmem_backing_dev_info's capabilities prevent regular writeback or
732 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700733 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800734 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700735 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800736 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700737 if (!wbc->for_reclaim) {
738 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
739 goto redirty;
740 }
741 swap = get_swap_page();
742 if (!swap.val)
743 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800744
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700745 /*
746 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700747 * if it's not already there. Do it now before the page is
748 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700749 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700750 * we've incremented swapped, because shmem_unuse_inode() will
751 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700752 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700753 mutex_lock(&shmem_swaplist_mutex);
754 if (list_empty(&info->swaplist))
755 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700756
Hugh Dickins48f170f2011-07-25 17:12:37 -0700757 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800758 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700759 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
760
761 spin_lock(&info->lock);
762 info->swapped++;
763 shmem_recalc_inode(inode);
Hugh Dickins826267c2011-05-28 13:14:09 -0700764 spin_unlock(&info->lock);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700765
766 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800767 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700768 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return 0;
770 }
771
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700772 mutex_unlock(&shmem_swaplist_mutex);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700773 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774redirty:
775 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800776 if (wbc->for_reclaim)
777 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
778 unlock_page(page);
779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800783#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700784static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800785{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700786 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800787
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700788 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700789 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800790
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700791 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800792
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700793 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800794}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700795
796static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
797{
798 struct mempolicy *mpol = NULL;
799 if (sbinfo->mpol) {
800 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
801 mpol = sbinfo->mpol;
802 mpol_get(mpol);
803 spin_unlock(&sbinfo->stat_lock);
804 }
805 return mpol;
806}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800807#endif /* CONFIG_TMPFS */
808
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700809static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
810 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct vm_area_struct pvma;
Mel Gorman78a685b2012-12-05 14:01:41 -0800813 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800815 /* Create a pseudo vma that just contains the policy */
816 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700817 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800818 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700819 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700820
Mel Gorman78a685b2012-12-05 14:01:41 -0800821 page = swapin_readahead(swap, gfp, &pvma, 0);
822
823 /* Drop reference taken by mpol_shared_policy_lookup() */
824 mpol_cond_put(pvma.vm_policy);
825
826 return page;
827}
828
829static struct page *shmem_alloc_page(gfp_t gfp,
830 struct shmem_inode_info *info, pgoff_t index)
831{
832 struct vm_area_struct pvma;
833 struct page *page;
834
835 /* Create a pseudo vma that just contains the policy */
836 pvma.vm_start = 0;
837 pvma.vm_pgoff = index;
838 pvma.vm_ops = NULL;
839 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
840
841 page = alloc_page_vma(gfp, &pvma, 0);
842
843 /* Drop reference taken by mpol_shared_policy_lookup() */
844 mpol_cond_put(pvma.vm_policy);
845
846 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800848#else /* !CONFIG_NUMA */
849#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700850static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800851{
852}
853#endif /* CONFIG_TMPFS */
854
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700855static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
856 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700858 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Hugh Dickins02098fe2008-02-04 22:28:42 -0800861static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700862 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000864 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800866#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700868#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
869static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
870{
871 return NULL;
872}
873#endif
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700876 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 *
878 * If we allocate a new one we do not mark it dirty. That's up to the
879 * vm. If we swap it in we mark it dirty since we also free the swap
880 * entry since a page cannot live in both the swap and page cache
881 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700882static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -0700883 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af6042011-08-03 16:21:24 -0700886 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700888 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 swp_entry_t swap;
890 int error;
Hugh Dickins54af6042011-08-03 16:21:24 -0700891 int once = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700893 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895repeat:
Hugh Dickins54af6042011-08-03 16:21:24 -0700896 swap.val = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700897 page = find_lock_page(mapping, index);
Hugh Dickins54af6042011-08-03 16:21:24 -0700898 if (radix_tree_exceptional_entry(page)) {
899 swap = radix_to_swp_entry(page);
900 page = NULL;
901 }
902
903 if (sgp != SGP_WRITE &&
904 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
905 error = -EINVAL;
906 goto failed;
907 }
908
909 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800910 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700911 * Once we can get the page lock, it must be uptodate:
912 * if there were an error in reading back from swap,
913 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800914 */
Hugh Dickins54af6042011-08-03 16:21:24 -0700915 BUG_ON(page && !PageUptodate(page));
916 *pagep = page;
917 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /*
Hugh Dickins54af6042011-08-03 16:21:24 -0700921 * Fast cache lookup did not find it:
922 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 */
Hugh Dickins54af6042011-08-03 16:21:24 -0700924 info = SHMEM_I(inode);
925 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (swap.val) {
928 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700929 page = lookup_swap_cache(swap);
930 if (!page) {
Ying Han456f9982011-05-26 16:25:38 -0700931 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700932 if (fault_type)
933 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700934 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700935 if (!page) {
Hugh Dickins54af6042011-08-03 16:21:24 -0700936 error = -ENOMEM;
937 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
941 /* We have to do this with page locked to prevent races */
Hugh Dickins54af6042011-08-03 16:21:24 -0700942 lock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700943 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 error = -EIO;
Hugh Dickins54af6042011-08-03 16:21:24 -0700945 goto failed;
946 }
947 wait_on_page_writeback(page);
948
949 /* Someone may have already done it for us */
950 if (page->mapping) {
951 if (page->mapping == mapping &&
952 page->index == index)
953 goto done;
954 error = -EEXIST;
955 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700958 error = mem_cgroup_cache_charge(page, current->mm,
959 gfp & GFP_RECLAIM_MASK);
960 if (!error)
961 error = shmem_add_to_page_cache(page, mapping, index,
962 gfp, swp_to_radix_entry(swap));
Hugh Dickins54af6042011-08-03 16:21:24 -0700963 if (error)
964 goto failed;
965
966 spin_lock(&info->lock);
967 info->swapped--;
968 shmem_recalc_inode(inode);
969 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700970
Hugh Dickins27ab7002011-07-25 17:12:36 -0700971 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700972 set_page_dirty(page);
973 swap_free(swap);
974
Hugh Dickins54af6042011-08-03 16:21:24 -0700975 } else {
976 if (shmem_acct_block(info->flags)) {
977 error = -ENOSPC;
978 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700980 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700981 if (percpu_counter_compare(&sbinfo->used_blocks,
Hugh Dickins54af6042011-08-03 16:21:24 -0700982 sbinfo->max_blocks) >= 0) {
983 error = -ENOSPC;
984 goto unacct;
985 }
Tim Chen7e496292010-08-09 17:19:05 -0700986 percpu_counter_inc(&sbinfo->used_blocks);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Hugh Dickins54af6042011-08-03 16:21:24 -0700989 page = shmem_alloc_page(gfp, info, index);
990 if (!page) {
991 error = -ENOMEM;
992 goto decused;
993 }
994
995 SetPageSwapBacked(page);
996 __set_page_locked(page);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700997 error = mem_cgroup_cache_charge(page, current->mm,
998 gfp & GFP_RECLAIM_MASK);
999 if (!error)
1000 error = shmem_add_to_page_cache(page, mapping, index,
1001 gfp, NULL);
Hugh Dickins54af6042011-08-03 16:21:24 -07001002 if (error)
1003 goto decused;
1004 lru_cache_add_anon(page);
1005
1006 spin_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 info->alloced++;
Hugh Dickins54af6042011-08-03 16:21:24 -07001008 inode->i_blocks += BLOCKS_PER_PAGE;
1009 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 spin_unlock(&info->lock);
Hugh Dickins54af6042011-08-03 16:21:24 -07001011
Hugh Dickins27ab7002011-07-25 17:12:36 -07001012 clear_highpage(page);
1013 flush_dcache_page(page);
1014 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001015 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -07001016 set_page_dirty(page);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018done:
Hugh Dickins54af6042011-08-03 16:21:24 -07001019 /* Perhaps the file has been truncated since we checked */
1020 if (sgp != SGP_WRITE &&
1021 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1022 error = -EINVAL;
1023 goto trunc;
Shaohua Liff36b802010-08-09 17:19:06 -07001024 }
Hugh Dickins54af6042011-08-03 16:21:24 -07001025 *pagep = page;
1026 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001027
Nick Piggind0217ac2007-07-19 01:47:03 -07001028 /*
Hugh Dickins54af6042011-08-03 16:21:24 -07001029 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
Hugh Dickins54af6042011-08-03 16:21:24 -07001031trunc:
1032 ClearPageDirty(page);
1033 delete_from_page_cache(page);
1034 spin_lock(&info->lock);
1035 info->alloced--;
1036 inode->i_blocks -= BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 spin_unlock(&info->lock);
Hugh Dickins54af6042011-08-03 16:21:24 -07001038decused:
1039 if (sbinfo->max_blocks)
1040 percpu_counter_add(&sbinfo->used_blocks, -1);
1041unacct:
1042 shmem_unacct_blocks(info->flags, 1);
1043failed:
1044 if (swap.val && error != -EINVAL) {
1045 struct page *test = find_get_page(mapping, index);
1046 if (test && !radix_tree_exceptional_entry(test))
1047 page_cache_release(test);
1048 /* Have another try if the entry has changed */
1049 if (test != swp_to_radix_entry(swap))
1050 error = -EEXIST;
1051 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001052 if (page) {
Hugh Dickins54af6042011-08-03 16:21:24 -07001053 unlock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001054 page_cache_release(page);
Hugh Dickins54af6042011-08-03 16:21:24 -07001055 }
1056 if (error == -ENOSPC && !once++) {
1057 info = SHMEM_I(inode);
1058 spin_lock(&info->lock);
1059 shmem_recalc_inode(inode);
1060 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001061 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001062 }
Hugh Dickins54af6042011-08-03 16:21:24 -07001063 if (error == -EEXIST)
1064 goto repeat;
1065 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1069{
1070 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1071 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001072 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001074 /*
1075 * Trinity finds that probing a hole which tmpfs is punching can
1076 * prevent the hole-punch from ever completing: which in turn
1077 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins0f5a4a02014-07-23 14:00:10 -07001078 * faulting pages into the hole while it's being punched. Although
1079 * shmem_truncate_range() does remove the additions, it may be unable to
1080 * keep up, as each new page needs its own unmap_mapping_range() call,
1081 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1082 *
1083 * It does not matter if we sometimes reach this check just before the
1084 * hole-punch begins, so that one fault then races with the punch:
1085 * we just need to make racing faults a rare case.
1086 *
1087 * The implementation below would be much simpler if we just used a
1088 * standard mutex or completion: but we cannot take i_mutex in fault,
1089 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001090 */
1091 if (unlikely(inode->i_private)) {
1092 struct shmem_falloc *shmem_falloc;
1093
1094 spin_lock(&inode->i_lock);
1095 shmem_falloc = inode->i_private;
Hugh Dickins0f5a4a02014-07-23 14:00:10 -07001096 if (shmem_falloc &&
1097 vmf->pgoff >= shmem_falloc->start &&
1098 vmf->pgoff < shmem_falloc->next) {
1099 wait_queue_head_t *shmem_falloc_waitq;
1100 DEFINE_WAIT(shmem_fault_wait);
1101
1102 ret = VM_FAULT_NOPAGE;
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001103 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1104 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins0f5a4a02014-07-23 14:00:10 -07001105 /* It's polite to up mmap_sem if we can */
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001106 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins0f5a4a02014-07-23 14:00:10 -07001107 ret = VM_FAULT_RETRY;
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001108 }
Hugh Dickins0f5a4a02014-07-23 14:00:10 -07001109
1110 shmem_falloc_waitq = shmem_falloc->waitq;
1111 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1112 TASK_UNINTERRUPTIBLE);
1113 spin_unlock(&inode->i_lock);
1114 schedule();
1115
1116 /*
1117 * shmem_falloc_waitq points into the vmtruncate_range()
1118 * stack of the hole-punching task: shmem_falloc_waitq
1119 * is usually invalid by the time we reach here, but
1120 * finish_wait() does not dereference it in that case;
1121 * though i_lock needed lest racing with wake_up_all().
1122 */
1123 spin_lock(&inode->i_lock);
1124 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
1125 spin_unlock(&inode->i_lock);
1126 return ret;
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001127 }
Hugh Dickins0f5a4a02014-07-23 14:00:10 -07001128 spin_unlock(&inode->i_lock);
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001129 }
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1132 if (error)
1133 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001134
Ying Han456f9982011-05-26 16:25:38 -07001135 if (ret & VM_FAULT_MAJOR) {
1136 count_vm_event(PGMAJFAULT);
1137 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1138 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001139 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001142int vmtruncate_range(struct inode *inode, loff_t lstart, loff_t lend)
1143{
1144 /*
1145 * If the underlying filesystem is not going to provide
1146 * a way to truncate a range of blocks (punch a hole) -
1147 * we should return failure right now.
1148 * Only CONFIG_SHMEM shmem.c ever supported i_op->truncate_range().
1149 */
1150 if (inode->i_op->truncate_range != shmem_truncate_range)
1151 return -ENOSYS;
1152
1153 mutex_lock(&inode->i_mutex);
1154 {
1155 struct shmem_falloc shmem_falloc;
1156 struct address_space *mapping = inode->i_mapping;
1157 loff_t unmap_start = round_up(lstart, PAGE_SIZE);
1158 loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
Hugh Dickins0f5a4a02014-07-23 14:00:10 -07001159 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001160
Hugh Dickins0f5a4a02014-07-23 14:00:10 -07001161 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001162 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
1163 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
1164 spin_lock(&inode->i_lock);
1165 inode->i_private = &shmem_falloc;
1166 spin_unlock(&inode->i_lock);
1167
1168 if ((u64)unmap_end > (u64)unmap_start)
1169 unmap_mapping_range(mapping, unmap_start,
1170 1 + unmap_end - unmap_start, 0);
1171 shmem_truncate_range(inode, lstart, lend);
1172 /* No need to unmap again: hole-punching leaves COWed pages */
1173
1174 spin_lock(&inode->i_lock);
1175 inode->i_private = NULL;
Hugh Dickins0f5a4a02014-07-23 14:00:10 -07001176 wake_up_all(&shmem_falloc_waitq);
Hugh Dickinsa62f3742014-06-23 13:22:06 -07001177 spin_unlock(&inode->i_lock);
1178 }
1179 mutex_unlock(&inode->i_mutex);
1180 return 0;
1181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001184static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001186 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1187 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001190static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1191 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001193 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1194 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001196 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1197 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199#endif
1200
1201int shmem_lock(struct file *file, int lock, struct user_struct *user)
1202{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001203 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct shmem_inode_info *info = SHMEM_I(inode);
1205 int retval = -ENOMEM;
1206
1207 spin_lock(&info->lock);
1208 if (lock && !(info->flags & VM_LOCKED)) {
1209 if (!user_shm_lock(inode->i_size, user))
1210 goto out_nomem;
1211 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001212 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214 if (!lock && (info->flags & VM_LOCKED) && user) {
1215 user_shm_unlock(inode->i_size, user);
1216 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001217 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221out_nomem:
1222 spin_unlock(&info->lock);
1223 return retval;
1224}
1225
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001226static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 file_accessed(file);
1229 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001230 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return 0;
1232}
1233
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001234static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04001235 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 struct inode *inode;
1238 struct shmem_inode_info *info;
1239 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1240
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001241 if (shmem_reserve_inode(sb))
1242 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 inode = new_inode(sb);
1245 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001246 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001247 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1250 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001251 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 info = SHMEM_I(inode);
1253 memset(info, 0, (char *)inode - (char *)info);
1254 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001255 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001257 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001258 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 switch (mode & S_IFMT) {
1261 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001262 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 init_special_inode(inode, mode, dev);
1264 break;
1265 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001266 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 inode->i_op = &shmem_inode_operations;
1268 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001269 mpol_shared_policy_init(&info->policy,
1270 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001273 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /* Some things misbehave if size == 0 on a directory */
1275 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1276 inode->i_op = &shmem_dir_inode_operations;
1277 inode->i_fop = &simple_dir_operations;
1278 break;
1279 case S_IFLNK:
1280 /*
1281 * Must not load anything in the rbtree,
1282 * mpol_free_shared_policy will not be called.
1283 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001284 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 break;
1286 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001287 } else
1288 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return inode;
1290}
1291
1292#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001293static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001294static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001296#ifdef CONFIG_TMPFS_XATTR
1297static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1298#else
1299#define shmem_initxattrs NULL
1300#endif
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001303shmem_write_begin(struct file *file, struct address_space *mapping,
1304 loff_t pos, unsigned len, unsigned flags,
1305 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Nick Piggin800d15a2007-10-16 01:25:03 -07001307 struct inode *inode = mapping->host;
1308 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -07001309 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1310}
1311
1312static int
1313shmem_write_end(struct file *file, struct address_space *mapping,
1314 loff_t pos, unsigned len, unsigned copied,
1315 struct page *page, void *fsdata)
1316{
1317 struct inode *inode = mapping->host;
1318
Hugh Dickinsd3602442008-02-04 22:28:44 -08001319 if (pos + copied > inode->i_size)
1320 i_size_write(inode, pos + copied);
1321
Nick Piggin800d15a2007-10-16 01:25:03 -07001322 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001323 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001324 page_cache_release(page);
1325
Nick Piggin800d15a2007-10-16 01:25:03 -07001326 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1330{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001331 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001333 pgoff_t index;
1334 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001335 enum sgp_type sgp = SGP_READ;
1336
1337 /*
1338 * Might this read be for a stacking filesystem? Then when reading
1339 * holes of a sparse file, we actually need to allocate those pages,
1340 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1341 */
1342 if (segment_eq(get_fs(), KERNEL_DS))
1343 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 index = *ppos >> PAGE_CACHE_SHIFT;
1346 offset = *ppos & ~PAGE_CACHE_MASK;
1347
1348 for (;;) {
1349 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001350 pgoff_t end_index;
1351 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 loff_t i_size = i_size_read(inode);
1353
1354 end_index = i_size >> PAGE_CACHE_SHIFT;
1355 if (index > end_index)
1356 break;
1357 if (index == end_index) {
1358 nr = i_size & ~PAGE_CACHE_MASK;
1359 if (nr <= offset)
1360 break;
1361 }
1362
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001363 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (desc->error) {
1365 if (desc->error == -EINVAL)
1366 desc->error = 0;
1367 break;
1368 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001369 if (page)
1370 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /*
1373 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001374 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
1376 nr = PAGE_CACHE_SIZE;
1377 i_size = i_size_read(inode);
1378 end_index = i_size >> PAGE_CACHE_SHIFT;
1379 if (index == end_index) {
1380 nr = i_size & ~PAGE_CACHE_MASK;
1381 if (nr <= offset) {
1382 if (page)
1383 page_cache_release(page);
1384 break;
1385 }
1386 }
1387 nr -= offset;
1388
1389 if (page) {
1390 /*
1391 * If users can be writing to this page using arbitrary
1392 * virtual addresses, take care about potential aliasing
1393 * before reading the page on the kernel side.
1394 */
1395 if (mapping_writably_mapped(mapping))
1396 flush_dcache_page(page);
1397 /*
1398 * Mark the page accessed if we read the beginning.
1399 */
1400 if (!offset)
1401 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001402 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001404 page_cache_get(page);
1405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /*
1408 * Ok, we have the page, and it's up-to-date, so
1409 * now we can copy it to user space...
1410 *
1411 * The actor routine returns how many bytes were actually used..
1412 * NOTE! This may not be the same as how much of a user buffer
1413 * we filled up (we may be padding etc), so we can only update
1414 * "pos" here (the actor routine has to update the user buffer
1415 * pointers and the remaining count).
1416 */
1417 ret = actor(desc, page, offset, nr);
1418 offset += ret;
1419 index += offset >> PAGE_CACHE_SHIFT;
1420 offset &= ~PAGE_CACHE_MASK;
1421
1422 page_cache_release(page);
1423 if (ret != nr || !desc->count)
1424 break;
1425
1426 cond_resched();
1427 }
1428
1429 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1430 file_accessed(filp);
1431}
1432
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001433static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1434 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001436 struct file *filp = iocb->ki_filp;
1437 ssize_t retval;
1438 unsigned long seg;
1439 size_t count;
1440 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001442 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1443 if (retval)
1444 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001446 for (seg = 0; seg < nr_segs; seg++) {
1447 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001449 desc.written = 0;
1450 desc.arg.buf = iov[seg].iov_base;
1451 desc.count = iov[seg].iov_len;
1452 if (desc.count == 0)
1453 continue;
1454 desc.error = 0;
1455 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1456 retval += desc.written;
1457 if (desc.error) {
1458 retval = retval ?: desc.error;
1459 break;
1460 }
1461 if (desc.count > 0)
1462 break;
1463 }
1464 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
Hugh Dickins708e3502011-07-25 17:12:32 -07001467static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1468 struct pipe_inode_info *pipe, size_t len,
1469 unsigned int flags)
1470{
1471 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001472 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001473 unsigned int loff, nr_pages, req_pages;
1474 struct page *pages[PIPE_DEF_BUFFERS];
1475 struct partial_page partial[PIPE_DEF_BUFFERS];
1476 struct page *page;
1477 pgoff_t index, end_index;
1478 loff_t isize, left;
1479 int error, page_nr;
1480 struct splice_pipe_desc spd = {
1481 .pages = pages,
1482 .partial = partial,
Eric Dumazet2c07f252012-06-12 15:24:40 +02001483 .nr_pages_max = PIPE_DEF_BUFFERS,
Hugh Dickins708e3502011-07-25 17:12:32 -07001484 .flags = flags,
1485 .ops = &page_cache_pipe_buf_ops,
1486 .spd_release = spd_release_page,
1487 };
1488
Hugh Dickins71f0e072011-07-25 17:12:33 -07001489 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001490 if (unlikely(*ppos >= isize))
1491 return 0;
1492
1493 left = isize - *ppos;
1494 if (unlikely(left < len))
1495 len = left;
1496
1497 if (splice_grow_spd(pipe, &spd))
1498 return -ENOMEM;
1499
1500 index = *ppos >> PAGE_CACHE_SHIFT;
1501 loff = *ppos & ~PAGE_CACHE_MASK;
1502 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1503 nr_pages = min(req_pages, pipe->buffers);
1504
Hugh Dickins708e3502011-07-25 17:12:32 -07001505 spd.nr_pages = find_get_pages_contig(mapping, index,
1506 nr_pages, spd.pages);
1507 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001508 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001509
Hugh Dickins708e3502011-07-25 17:12:32 -07001510 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001511 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1512 if (error)
1513 break;
1514 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001515 spd.pages[spd.nr_pages++] = page;
1516 index++;
1517 }
1518
Hugh Dickins708e3502011-07-25 17:12:32 -07001519 index = *ppos >> PAGE_CACHE_SHIFT;
1520 nr_pages = spd.nr_pages;
1521 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001522
Hugh Dickins708e3502011-07-25 17:12:32 -07001523 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1524 unsigned int this_len;
1525
1526 if (!len)
1527 break;
1528
Hugh Dickins708e3502011-07-25 17:12:32 -07001529 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1530 page = spd.pages[page_nr];
1531
Hugh Dickins71f0e072011-07-25 17:12:33 -07001532 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001533 error = shmem_getpage(inode, index, &page,
1534 SGP_CACHE, NULL);
1535 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001536 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001537 unlock_page(page);
1538 page_cache_release(spd.pages[page_nr]);
1539 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001540 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001541
1542 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001543 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1544 if (unlikely(!isize || index > end_index))
1545 break;
1546
Hugh Dickins708e3502011-07-25 17:12:32 -07001547 if (end_index == index) {
1548 unsigned int plen;
1549
Hugh Dickins708e3502011-07-25 17:12:32 -07001550 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1551 if (plen <= loff)
1552 break;
1553
Hugh Dickins708e3502011-07-25 17:12:32 -07001554 this_len = min(this_len, plen - loff);
1555 len = this_len;
1556 }
1557
1558 spd.partial[page_nr].offset = loff;
1559 spd.partial[page_nr].len = this_len;
1560 len -= this_len;
1561 loff = 0;
1562 spd.nr_pages++;
1563 index++;
1564 }
1565
Hugh Dickins708e3502011-07-25 17:12:32 -07001566 while (page_nr < nr_pages)
1567 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001568
1569 if (spd.nr_pages)
1570 error = splice_to_pipe(pipe, &spd);
1571
Eric Dumazet2c07f252012-06-12 15:24:40 +02001572 splice_shrink_spd(&spd);
Hugh Dickins708e3502011-07-25 17:12:32 -07001573
1574 if (error > 0) {
1575 *ppos += error;
1576 file_accessed(in);
1577 }
1578 return error;
1579}
1580
David Howells726c3342006-06-23 02:02:58 -07001581static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
David Howells726c3342006-06-23 02:02:58 -07001583 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 buf->f_type = TMPFS_MAGIC;
1586 buf->f_bsize = PAGE_CACHE_SIZE;
1587 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001588 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001590 buf->f_bavail =
1591 buf->f_bfree = sbinfo->max_blocks -
1592 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001593 }
1594 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 buf->f_files = sbinfo->max_inodes;
1596 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598 /* else leave those fields 0 like simple_statfs */
1599 return 0;
1600}
1601
1602/*
1603 * File creation. Allocate an inode, and we're done..
1604 */
1605static int
Al Viro1a67aaf2011-07-26 01:52:52 -04001606shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001608 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 int error = -ENOSPC;
1610
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001611 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001613 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001614 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001615 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001616 if (error) {
1617 if (error != -EOPNOTSUPP) {
1618 iput(inode);
1619 return error;
1620 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001621 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001622#ifdef CONFIG_TMPFS_POSIX_ACL
1623 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001624 if (error) {
1625 iput(inode);
1626 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001627 }
Al Viro718deb62009-12-16 19:35:36 -05001628#else
1629 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001630#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 dir->i_size += BOGO_DIRENT_SIZE;
1632 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1633 d_instantiate(dentry, inode);
1634 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636 return error;
1637}
1638
Al Viro18bb1db2011-07-26 01:41:39 -04001639static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
1641 int error;
1642
1643 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1644 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001645 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return 0;
1647}
1648
Al Viro4acdaf22011-07-26 01:42:34 -04001649static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 struct nameidata *nd)
1651{
1652 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1653}
1654
1655/*
1656 * Link a file..
1657 */
1658static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1659{
1660 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001661 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 /*
1664 * No ordinary (disk based) filesystem counts links as inodes;
1665 * but each new link needs a new dentry, pinning lowmem, and
1666 * tmpfs dentries cannot be pruned until they are unlinked.
1667 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001668 ret = shmem_reserve_inode(inode->i_sb);
1669 if (ret)
1670 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 dir->i_size += BOGO_DIRENT_SIZE;
1673 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001674 inc_nlink(inode);
Al Viro7de9c6e2010-10-23 11:11:40 -04001675 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 dget(dentry); /* Extra pinning count for the created dentry */
1677 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001678out:
1679 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
1682static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1683{
1684 struct inode *inode = dentry->d_inode;
1685
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001686 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1687 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 dir->i_size -= BOGO_DIRENT_SIZE;
1690 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001691 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 dput(dentry); /* Undo the count from "create" - this does all the work */
1693 return 0;
1694}
1695
1696static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1697{
1698 if (!simple_empty(dentry))
1699 return -ENOTEMPTY;
1700
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001701 drop_nlink(dentry->d_inode);
1702 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return shmem_unlink(dir, dentry);
1704}
1705
1706/*
1707 * The VFS layer already does all the dentry stuff for rename,
1708 * we just have to decrement the usage count for the target if
1709 * it exists so that the VFS layer correctly free's it when it
1710 * gets overwritten.
1711 */
1712static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1713{
1714 struct inode *inode = old_dentry->d_inode;
1715 int they_are_dirs = S_ISDIR(inode->i_mode);
1716
1717 if (!simple_empty(new_dentry))
1718 return -ENOTEMPTY;
1719
1720 if (new_dentry->d_inode) {
1721 (void) shmem_unlink(new_dir, new_dentry);
1722 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001723 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001725 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001726 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
1728
1729 old_dir->i_size -= BOGO_DIRENT_SIZE;
1730 new_dir->i_size += BOGO_DIRENT_SIZE;
1731 old_dir->i_ctime = old_dir->i_mtime =
1732 new_dir->i_ctime = new_dir->i_mtime =
1733 inode->i_ctime = CURRENT_TIME;
1734 return 0;
1735}
1736
1737static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1738{
1739 int error;
1740 int len;
1741 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001742 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 char *kaddr;
1744 struct shmem_inode_info *info;
1745
1746 len = strlen(symname) + 1;
1747 if (len > PAGE_CACHE_SIZE)
1748 return -ENAMETOOLONG;
1749
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001750 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (!inode)
1752 return -ENOSPC;
1753
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001754 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001755 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001756 if (error) {
1757 if (error != -EOPNOTSUPP) {
1758 iput(inode);
1759 return error;
1760 }
1761 error = 0;
1762 }
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 info = SHMEM_I(inode);
1765 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001766 if (len <= SHORT_SYMLINK_LEN) {
1767 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1768 if (!info->symlink) {
1769 iput(inode);
1770 return -ENOMEM;
1771 }
1772 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 } else {
1774 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1775 if (error) {
1776 iput(inode);
1777 return error;
1778 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001779 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 inode->i_op = &shmem_symlink_inode_operations;
Cong Wang9b04c5f2011-11-25 23:14:39 +08001781 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 memcpy(kaddr, symname, len);
Cong Wang9b04c5f2011-11-25 23:14:39 +08001783 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001785 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 page_cache_release(page);
1787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 dir->i_size += BOGO_DIRENT_SIZE;
1789 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1790 d_instantiate(dentry, inode);
1791 dget(dentry);
1792 return 0;
1793}
1794
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001795static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001797 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001798 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001801static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001804 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1805 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001806 if (page)
1807 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001808 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001811static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
1813 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001814 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 kunmap(page);
1816 mark_page_accessed(page);
1817 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819}
1820
Eric Parisb09e0fa2011-05-24 17:12:39 -07001821#ifdef CONFIG_TMPFS_XATTR
1822/*
1823 * Superblocks without xattr inode operations may get some security.* xattr
1824 * support from the LSM "for free". As soon as we have any other xattrs
1825 * like ACLs, we also need to implement the security.* handlers at
1826 * filesystem level, though.
1827 */
1828
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001829/*
1830 * Allocate new xattr and copy in the value; but leave the name to callers.
1831 */
1832static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
1833{
1834 struct shmem_xattr *new_xattr;
1835 size_t len;
1836
1837 /* wrap around? */
1838 len = sizeof(*new_xattr) + size;
1839 if (len <= sizeof(*new_xattr))
1840 return NULL;
1841
1842 new_xattr = kmalloc(len, GFP_KERNEL);
1843 if (!new_xattr)
1844 return NULL;
1845
1846 new_xattr->size = size;
1847 memcpy(new_xattr->value, value, size);
1848 return new_xattr;
1849}
1850
1851/*
1852 * Callback for security_inode_init_security() for acquiring xattrs.
1853 */
1854static int shmem_initxattrs(struct inode *inode,
1855 const struct xattr *xattr_array,
1856 void *fs_info)
1857{
1858 struct shmem_inode_info *info = SHMEM_I(inode);
1859 const struct xattr *xattr;
1860 struct shmem_xattr *new_xattr;
1861 size_t len;
1862
1863 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
1864 new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
1865 if (!new_xattr)
1866 return -ENOMEM;
1867
1868 len = strlen(xattr->name) + 1;
1869 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
1870 GFP_KERNEL);
1871 if (!new_xattr->name) {
1872 kfree(new_xattr);
1873 return -ENOMEM;
1874 }
1875
1876 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
1877 XATTR_SECURITY_PREFIX_LEN);
1878 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
1879 xattr->name, len);
1880
1881 spin_lock(&info->lock);
1882 list_add(&new_xattr->list, &info->xattr_list);
1883 spin_unlock(&info->lock);
1884 }
1885
1886 return 0;
1887}
1888
Eric Parisb09e0fa2011-05-24 17:12:39 -07001889static int shmem_xattr_get(struct dentry *dentry, const char *name,
1890 void *buffer, size_t size)
1891{
1892 struct shmem_inode_info *info;
1893 struct shmem_xattr *xattr;
1894 int ret = -ENODATA;
1895
1896 info = SHMEM_I(dentry->d_inode);
1897
1898 spin_lock(&info->lock);
1899 list_for_each_entry(xattr, &info->xattr_list, list) {
1900 if (strcmp(name, xattr->name))
1901 continue;
1902
1903 ret = xattr->size;
1904 if (buffer) {
1905 if (size < xattr->size)
1906 ret = -ERANGE;
1907 else
1908 memcpy(buffer, xattr->value, xattr->size);
1909 }
1910 break;
1911 }
1912 spin_unlock(&info->lock);
1913 return ret;
1914}
1915
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001916static int shmem_xattr_set(struct inode *inode, const char *name,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001917 const void *value, size_t size, int flags)
1918{
Eric Parisb09e0fa2011-05-24 17:12:39 -07001919 struct shmem_inode_info *info = SHMEM_I(inode);
1920 struct shmem_xattr *xattr;
1921 struct shmem_xattr *new_xattr = NULL;
Eric Parisb09e0fa2011-05-24 17:12:39 -07001922 int err = 0;
1923
1924 /* value == NULL means remove */
1925 if (value) {
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001926 new_xattr = shmem_xattr_alloc(value, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001927 if (!new_xattr)
1928 return -ENOMEM;
1929
1930 new_xattr->name = kstrdup(name, GFP_KERNEL);
1931 if (!new_xattr->name) {
1932 kfree(new_xattr);
1933 return -ENOMEM;
1934 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001935 }
1936
1937 spin_lock(&info->lock);
1938 list_for_each_entry(xattr, &info->xattr_list, list) {
1939 if (!strcmp(name, xattr->name)) {
1940 if (flags & XATTR_CREATE) {
1941 xattr = new_xattr;
1942 err = -EEXIST;
1943 } else if (new_xattr) {
1944 list_replace(&xattr->list, &new_xattr->list);
1945 } else {
1946 list_del(&xattr->list);
1947 }
1948 goto out;
1949 }
1950 }
1951 if (flags & XATTR_REPLACE) {
1952 xattr = new_xattr;
1953 err = -ENODATA;
1954 } else {
1955 list_add(&new_xattr->list, &info->xattr_list);
1956 xattr = NULL;
1957 }
1958out:
1959 spin_unlock(&info->lock);
1960 if (xattr)
1961 kfree(xattr->name);
1962 kfree(xattr);
1963 return err;
1964}
1965
Eric Parisb09e0fa2011-05-24 17:12:39 -07001966static const struct xattr_handler *shmem_xattr_handlers[] = {
1967#ifdef CONFIG_TMPFS_POSIX_ACL
1968 &generic_acl_access_handler,
1969 &generic_acl_default_handler,
1970#endif
1971 NULL
1972};
1973
1974static int shmem_xattr_validate(const char *name)
1975{
1976 struct { const char *prefix; size_t len; } arr[] = {
1977 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1978 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1979 };
1980 int i;
1981
1982 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1983 size_t preflen = arr[i].len;
1984 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1985 if (!name[preflen])
1986 return -EINVAL;
1987 return 0;
1988 }
1989 }
1990 return -EOPNOTSUPP;
1991}
1992
1993static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1994 void *buffer, size_t size)
1995{
1996 int err;
1997
1998 /*
1999 * If this is a request for a synthetic attribute in the system.*
2000 * namespace use the generic infrastructure to resolve a handler
2001 * for it via sb->s_xattr.
2002 */
2003 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2004 return generic_getxattr(dentry, name, buffer, size);
2005
2006 err = shmem_xattr_validate(name);
2007 if (err)
2008 return err;
2009
2010 return shmem_xattr_get(dentry, name, buffer, size);
2011}
2012
2013static int shmem_setxattr(struct dentry *dentry, const char *name,
2014 const void *value, size_t size, int flags)
2015{
2016 int err;
2017
2018 /*
2019 * If this is a request for a synthetic attribute in the system.*
2020 * namespace use the generic infrastructure to resolve a handler
2021 * for it via sb->s_xattr.
2022 */
2023 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2024 return generic_setxattr(dentry, name, value, size, flags);
2025
2026 err = shmem_xattr_validate(name);
2027 if (err)
2028 return err;
2029
2030 if (size == 0)
2031 value = ""; /* empty EA, do not remove */
2032
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002033 return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002034
2035}
2036
2037static int shmem_removexattr(struct dentry *dentry, const char *name)
2038{
2039 int err;
2040
2041 /*
2042 * If this is a request for a synthetic attribute in the system.*
2043 * namespace use the generic infrastructure to resolve a handler
2044 * for it via sb->s_xattr.
2045 */
2046 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2047 return generic_removexattr(dentry, name);
2048
2049 err = shmem_xattr_validate(name);
2050 if (err)
2051 return err;
2052
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002053 return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
Eric Parisb09e0fa2011-05-24 17:12:39 -07002054}
2055
2056static bool xattr_is_trusted(const char *name)
2057{
2058 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2059}
2060
2061static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2062{
2063 bool trusted = capable(CAP_SYS_ADMIN);
2064 struct shmem_xattr *xattr;
2065 struct shmem_inode_info *info;
2066 size_t used = 0;
2067
2068 info = SHMEM_I(dentry->d_inode);
2069
2070 spin_lock(&info->lock);
2071 list_for_each_entry(xattr, &info->xattr_list, list) {
2072 size_t len;
2073
2074 /* skip "trusted." attributes for unprivileged callers */
2075 if (!trusted && xattr_is_trusted(xattr->name))
2076 continue;
2077
2078 len = strlen(xattr->name) + 1;
2079 used += len;
2080 if (buffer) {
2081 if (size < used) {
2082 used = -ERANGE;
2083 break;
2084 }
2085 memcpy(buffer, xattr->name, len);
2086 buffer += len;
2087 }
2088 }
2089 spin_unlock(&info->lock);
2090
2091 return used;
2092}
2093#endif /* CONFIG_TMPFS_XATTR */
2094
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002095static const struct inode_operations shmem_short_symlink_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 .readlink = generic_readlink,
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002097 .follow_link = shmem_follow_short_symlink,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002098#ifdef CONFIG_TMPFS_XATTR
2099 .setxattr = shmem_setxattr,
2100 .getxattr = shmem_getxattr,
2101 .listxattr = shmem_listxattr,
2102 .removexattr = shmem_removexattr,
2103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104};
2105
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002106static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 .readlink = generic_readlink,
2108 .follow_link = shmem_follow_link,
2109 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002110#ifdef CONFIG_TMPFS_XATTR
2111 .setxattr = shmem_setxattr,
2112 .getxattr = shmem_getxattr,
2113 .listxattr = shmem_listxattr,
2114 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002115#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002116};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002117
David M. Grimes91828a42006-10-17 00:09:45 -07002118static struct dentry *shmem_get_parent(struct dentry *child)
2119{
2120 return ERR_PTR(-ESTALE);
2121}
2122
2123static int shmem_match(struct inode *ino, void *vfh)
2124{
2125 __u32 *fh = vfh;
2126 __u64 inum = fh[2];
2127 inum = (inum << 32) | fh[1];
2128 return ino->i_ino == inum && fh[0] == ino->i_generation;
2129}
2130
Christoph Hellwig480b1162007-10-21 16:42:13 -07002131static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2132 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07002133{
David M. Grimes91828a42006-10-17 00:09:45 -07002134 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07002135 struct dentry *dentry = NULL;
Hugh Dickins530258f2012-10-07 20:32:51 -07002136 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07002137
Christoph Hellwig480b1162007-10-21 16:42:13 -07002138 if (fh_len < 3)
2139 return NULL;
2140
Hugh Dickins530258f2012-10-07 20:32:51 -07002141 inum = fid->raw[2];
2142 inum = (inum << 32) | fid->raw[1];
2143
Christoph Hellwig480b1162007-10-21 16:42:13 -07002144 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2145 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07002146 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07002147 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07002148 iput(inode);
2149 }
2150
Christoph Hellwig480b1162007-10-21 16:42:13 -07002151 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07002152}
2153
2154static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2155 int connectable)
2156{
2157 struct inode *inode = dentry->d_inode;
2158
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302159 if (*len < 3) {
2160 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07002161 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302162 }
David M. Grimes91828a42006-10-17 00:09:45 -07002163
Al Viro1d3382c2010-10-23 15:19:20 -04002164 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07002165 /* Unfortunately insert_inode_hash is not idempotent,
2166 * so as we hash inodes here rather than at creation
2167 * time, we need a lock to ensure we only try
2168 * to do it once
2169 */
2170 static DEFINE_SPINLOCK(lock);
2171 spin_lock(&lock);
Al Viro1d3382c2010-10-23 15:19:20 -04002172 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07002173 __insert_inode_hash(inode,
2174 inode->i_ino + inode->i_generation);
2175 spin_unlock(&lock);
2176 }
2177
2178 fh[0] = inode->i_generation;
2179 fh[1] = inode->i_ino;
2180 fh[2] = ((__u64)inode->i_ino) >> 32;
2181
2182 *len = 3;
2183 return 1;
2184}
2185
Christoph Hellwig39655162007-10-21 16:42:17 -07002186static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07002187 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07002188 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07002189 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07002190};
2191
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002192static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2193 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 char *this_char, *value, *rest;
2196
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00002197 while (options != NULL) {
2198 this_char = options;
2199 for (;;) {
2200 /*
2201 * NUL-terminate this option: unfortunately,
2202 * mount options form a comma-separated list,
2203 * but mpol's nodelist may also contain commas.
2204 */
2205 options = strchr(options, ',');
2206 if (options == NULL)
2207 break;
2208 options++;
2209 if (!isdigit(*options)) {
2210 options[-1] = '\0';
2211 break;
2212 }
2213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (!*this_char)
2215 continue;
2216 if ((value = strchr(this_char,'=')) != NULL) {
2217 *value++ = 0;
2218 } else {
2219 printk(KERN_ERR
2220 "tmpfs: No value for mount option '%s'\n",
2221 this_char);
2222 return 1;
2223 }
2224
2225 if (!strcmp(this_char,"size")) {
2226 unsigned long long size;
2227 size = memparse(value,&rest);
2228 if (*rest == '%') {
2229 size <<= PAGE_SHIFT;
2230 size *= totalram_pages;
2231 do_div(size, 100);
2232 rest++;
2233 }
2234 if (*rest)
2235 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002236 sbinfo->max_blocks =
2237 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002239 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (*rest)
2241 goto bad_val;
2242 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002243 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (*rest)
2245 goto bad_val;
2246 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002247 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002249 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (*rest)
2251 goto bad_val;
2252 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002253 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002255 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (*rest)
2257 goto bad_val;
2258 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002259 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002261 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (*rest)
2263 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002264 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002265 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08002266 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 } else {
2268 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2269 this_char);
2270 return 1;
2271 }
2272 }
2273 return 0;
2274
2275bad_val:
2276 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2277 value, this_char);
2278 return 1;
2279
2280}
2281
2282static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2283{
2284 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002285 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002286 unsigned long inodes;
2287 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Greg Thelen06c79762013-02-22 16:36:01 -08002289 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002290 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002291 return error;
2292
2293 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002294 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002295 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002296 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002297 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002298 goto out;
2299 /*
Hugh Dickins54af6042011-08-03 16:21:24 -07002300 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002301 * but we must separately disallow unlimited->limited, because
2302 * in that case we have no record of how much is already in use.
2303 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002304 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002305 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002306 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002307 goto out;
2308
2309 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002310 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002311 sbinfo->max_inodes = config.max_inodes;
2312 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002313
Greg Thelen06c79762013-02-22 16:36:01 -08002314 /*
2315 * Preserve previous mempolicy unless mpol remount option was specified.
2316 */
2317 if (config.mpol) {
2318 mpol_put(sbinfo->mpol);
2319 sbinfo->mpol = config.mpol; /* transfers initial ref */
2320 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002321out:
2322 spin_unlock(&sbinfo->stat_lock);
2323 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002325
Al Viro34c80b12011-12-08 21:32:45 -05002326static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002327{
Al Viro34c80b12011-12-08 21:32:45 -05002328 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002329
2330 if (sbinfo->max_blocks != shmem_default_max_blocks())
2331 seq_printf(seq, ",size=%luk",
2332 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2333 if (sbinfo->max_inodes != shmem_default_max_inodes())
2334 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2335 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04002336 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002337 if (sbinfo->uid != 0)
2338 seq_printf(seq, ",uid=%u", sbinfo->uid);
2339 if (sbinfo->gid != 0)
2340 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002341 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002342 return 0;
2343}
2344#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346static void shmem_put_super(struct super_block *sb)
2347{
Hugh Dickins602586a2010-08-17 15:23:56 -07002348 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2349
2350 percpu_counter_destroy(&sbinfo->used_blocks);
2351 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 sb->s_fs_info = NULL;
2353}
2354
Kay Sievers2b2af542009-04-30 15:23:42 +02002355int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
2357 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002358 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002359 int err = -ENOMEM;
2360
2361 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002362 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002363 L1_CACHE_BYTES), GFP_KERNEL);
2364 if (!sbinfo)
2365 return -ENOMEM;
2366
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002367 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002368 sbinfo->uid = current_fsuid();
2369 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002370 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002372#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 /*
2374 * Per default we only allow half of the physical ram per
2375 * tmpfs instance, limiting inodes to one per page of lowmem;
2376 * but the internal instance is left unlimited.
2377 */
2378 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002379 sbinfo->max_blocks = shmem_default_max_blocks();
2380 sbinfo->max_inodes = shmem_default_max_inodes();
2381 if (shmem_parse_options(data, sbinfo, false)) {
2382 err = -EINVAL;
2383 goto failed;
2384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
David M. Grimes91828a42006-10-17 00:09:45 -07002386 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387#else
2388 sb->s_flags |= MS_NOUSER;
2389#endif
2390
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002391 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002392 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2393 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002394 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002395
Hugh Dickins285b2c42011-08-03 16:21:20 -07002396 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 sb->s_blocksize = PAGE_CACHE_SIZE;
2398 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2399 sb->s_magic = TMPFS_MAGIC;
2400 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002401 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002402#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002403 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002404#endif
2405#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002406 sb->s_flags |= MS_POSIXACL;
2407#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002408
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002409 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (!inode)
2411 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002412 inode->i_uid = sbinfo->uid;
2413 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05002414 sb->s_root = d_make_root(inode);
2415 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05002416 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return 0;
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419failed:
2420 shmem_put_super(sb);
2421 return err;
2422}
2423
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002424static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426static struct inode *shmem_alloc_inode(struct super_block *sb)
2427{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002428 struct shmem_inode_info *info;
2429 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2430 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002432 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002435static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e32011-01-07 17:49:49 +11002436{
2437 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e32011-01-07 17:49:49 +11002438 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2439}
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441static void shmem_destroy_inode(struct inode *inode)
2442{
Al Viro09208d12011-07-26 03:15:03 -04002443 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002445 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002448static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002450 struct shmem_inode_info *info = foo;
2451 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002454static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
2456 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2457 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002458 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return 0;
2460}
2461
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002462static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002464 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002467static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002469 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002471 .write_begin = shmem_write_begin,
2472 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002474 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002475 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476};
2477
Helge Deller15ad7cd2006-12-06 20:40:36 -08002478static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 .mmap = shmem_mmap,
2480#ifdef CONFIG_TMPFS
2481 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002482 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002483 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002484 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002485 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002486 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002487 .splice_read = shmem_file_splice_read,
Hugh Dickinsae976412007-06-04 10:00:39 +02002488 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489#endif
2490};
2491
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002492static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002493 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002494 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002495#ifdef CONFIG_TMPFS_XATTR
2496 .setxattr = shmem_setxattr,
2497 .getxattr = shmem_getxattr,
2498 .listxattr = shmem_listxattr,
2499 .removexattr = shmem_removexattr,
2500#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501};
2502
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002503static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504#ifdef CONFIG_TMPFS
2505 .create = shmem_create,
2506 .lookup = simple_lookup,
2507 .link = shmem_link,
2508 .unlink = shmem_unlink,
2509 .symlink = shmem_symlink,
2510 .mkdir = shmem_mkdir,
2511 .rmdir = shmem_rmdir,
2512 .mknod = shmem_mknod,
2513 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002515#ifdef CONFIG_TMPFS_XATTR
2516 .setxattr = shmem_setxattr,
2517 .getxattr = shmem_getxattr,
2518 .listxattr = shmem_listxattr,
2519 .removexattr = shmem_removexattr,
2520#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002521#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002522 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002523#endif
2524};
2525
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002526static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002527#ifdef CONFIG_TMPFS_XATTR
2528 .setxattr = shmem_setxattr,
2529 .getxattr = shmem_getxattr,
2530 .listxattr = shmem_listxattr,
2531 .removexattr = shmem_removexattr,
2532#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002533#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002534 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002535#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536};
2537
Hugh Dickins759b9772007-03-05 00:30:28 -08002538static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 .alloc_inode = shmem_alloc_inode,
2540 .destroy_inode = shmem_destroy_inode,
2541#ifdef CONFIG_TMPFS
2542 .statfs = shmem_statfs,
2543 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002544 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545#endif
Al Viro1f895f72010-06-05 19:10:41 -04002546 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 .drop_inode = generic_delete_inode,
2548 .put_super = shmem_put_super,
2549};
2550
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002551static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002552 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553#ifdef CONFIG_NUMA
2554 .set_policy = shmem_set_policy,
2555 .get_policy = shmem_get_policy,
2556#endif
2557};
2558
Al Viro3c26ff62010-07-25 11:46:36 +04002559static struct dentry *shmem_mount(struct file_system_type *fs_type,
2560 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
Al Viro3c26ff62010-07-25 11:46:36 +04002562 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002565static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 .owner = THIS_MODULE,
2567 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002568 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 .kill_sb = kill_litter_super,
2570};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002572int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
2574 int error;
2575
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002576 error = bdi_init(&shmem_backing_dev_info);
2577 if (error)
2578 goto out4;
2579
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002580 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 if (error)
2582 goto out3;
2583
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002584 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (error) {
2586 printk(KERN_ERR "Could not register tmpfs\n");
2587 goto out2;
2588 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002589
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002590 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2591 shmem_fs_type.name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (IS_ERR(shm_mnt)) {
2593 error = PTR_ERR(shm_mnt);
2594 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2595 goto out1;
2596 }
2597 return 0;
2598
2599out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002600 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002602 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002604 bdi_destroy(&shmem_backing_dev_info);
2605out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 shm_mnt = ERR_PTR(error);
2607 return error;
2608}
Matt Mackall853ac432009-01-06 14:40:20 -08002609
2610#else /* !CONFIG_SHMEM */
2611
2612/*
2613 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2614 *
2615 * This is intended for small system where the benefits of the full
2616 * shmem code (swap-backed and resource-limited) are outweighed by
2617 * their complexity. On systems without swap this code should be
2618 * effectively equivalent, but much lighter weight.
2619 */
2620
2621#include <linux/ramfs.h>
2622
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002623static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08002624 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002625 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002626 .kill_sb = kill_litter_super,
2627};
2628
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002629int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002630{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002631 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08002632
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002633 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08002634 BUG_ON(IS_ERR(shm_mnt));
2635
2636 return 0;
2637}
2638
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002639int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08002640{
2641 return 0;
2642}
2643
Hugh Dickins3f96b792009-09-21 17:03:37 -07002644int shmem_lock(struct file *file, int lock, struct user_struct *user)
2645{
2646 return 0;
2647}
2648
Hugh Dickins24513262012-01-20 14:34:21 -08002649void shmem_unlock_mapping(struct address_space *mapping)
2650{
2651}
2652
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002653void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07002654{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002655 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07002656}
2657EXPORT_SYMBOL_GPL(shmem_truncate_range);
2658
Hugh Dickinsa62f3742014-06-23 13:22:06 -07002659int vmtruncate_range(struct inode *inode, loff_t lstart, loff_t lend)
2660{
2661 /* Only CONFIG_SHMEM shmem.c ever supported i_op->truncate_range(). */
2662 return -ENOSYS;
2663}
2664
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002665#define shmem_vm_ops generic_file_vm_ops
2666#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002667#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002668#define shmem_acct_size(flags, size) 0
2669#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002670
2671#endif /* CONFIG_SHMEM */
2672
2673/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Randy Dunlap46711812008-03-19 17:00:41 -07002675/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 * @name: name for dentry (to be seen in /proc/<pid>/maps
2678 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002679 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002681struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
2683 int error;
2684 struct file *file;
2685 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002686 struct path path;
2687 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 struct qstr this;
2689
2690 if (IS_ERR(shm_mnt))
2691 return (void *)shm_mnt;
2692
Hugh Dickins285b2c42011-08-03 16:21:20 -07002693 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return ERR_PTR(-EINVAL);
2695
2696 if (shmem_acct_size(flags, size))
2697 return ERR_PTR(-ENOMEM);
2698
2699 error = -ENOMEM;
2700 this.name = name;
2701 this.len = strlen(name);
2702 this.hash = 0; /* will go */
2703 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002704 path.dentry = d_alloc(root, &this);
2705 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002707 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002710 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002712 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Al Viro2c48b9c2009-08-09 00:52:35 +04002714 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02002716 clear_nlink(inode); /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002717#ifndef CONFIG_MMU
2718 error = ramfs_nommu_expand_for_mapping(inode, size);
2719 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002720 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002721#endif
Al Viro4b42af82009-08-05 18:25:56 +04002722
2723 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002724 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002725 &shmem_file_operations);
2726 if (!file)
2727 goto put_dentry;
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return file;
2730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002732 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733put_memory:
2734 shmem_unacct_size(flags, size);
2735 return ERR_PTR(error);
2736}
Keith Packard395e0dd2008-06-20 00:08:06 -07002737EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Randy Dunlap46711812008-03-19 17:00:41 -07002739/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2742 */
2743int shmem_zero_setup(struct vm_area_struct *vma)
2744{
2745 struct file *file;
2746 loff_t size = vma->vm_end - vma->vm_start;
2747
2748 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2749 if (IS_ERR(file))
2750 return PTR_ERR(file);
2751
2752 if (vma->vm_file)
2753 fput(vma->vm_file);
2754 vma->vm_file = file;
2755 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c362011-03-22 16:33:43 -07002756 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return 0;
2758}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002759
2760/**
2761 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2762 * @mapping: the page's address_space
2763 * @index: the page index
2764 * @gfp: the page allocator flags to use if allocating
2765 *
2766 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2767 * with any new page allocations done using the specified allocation flags.
2768 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2769 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2770 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2771 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002772 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2773 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002774 */
2775struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2776 pgoff_t index, gfp_t gfp)
2777{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002778#ifdef CONFIG_SHMEM
2779 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002780 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002781 int error;
2782
2783 BUG_ON(mapping->a_ops != &shmem_aops);
2784 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2785 if (error)
2786 page = ERR_PTR(error);
2787 else
2788 unlock_page(page);
2789 return page;
2790#else
2791 /*
2792 * The tiny !SHMEM case uses ramfs without swap
2793 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002794 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002795#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002796}
2797EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);