blob: 1c702f6f1241fadb04e83ac4e1c0aecab4d7de4b [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>
31#include <linux/module.h>
32#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
Eric Parisb09e0fa2011-05-24 17:12:39 -070079struct shmem_xattr {
80 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
81 char *name; /* xattr name */
82 size_t size;
83 char value[0];
84};
85
Hugh Dickins285b2c42011-08-03 16:21:20 -070086/* Flag allocation requirements to shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087enum sgp_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 SGP_READ, /* don't exceed i_size, don't allocate page */
89 SGP_CACHE, /* don't exceed i_size, may allocate page */
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -080090 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 SGP_WRITE, /* may exceed i_size, may allocate page */
92};
93
Andrew Mortonb76db7352008-02-08 04:21:49 -080094#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080095static unsigned long shmem_default_max_blocks(void)
96{
97 return totalram_pages / 2;
98}
99
100static unsigned long shmem_default_max_inodes(void)
101{
102 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
103}
Andrew Mortonb76db7352008-02-08 04:21:49 -0800104#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800105
Hugh Dickins68da9f02011-07-25 17:12:34 -0700106static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
107 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
108
109static inline int shmem_getpage(struct inode *inode, pgoff_t index,
110 struct page **pagep, enum sgp_type sgp, int *fault_type)
111{
112 return shmem_getpage_gfp(inode, index, pagep, sgp,
113 mapping_gfp_mask(inode->i_mapping), fault_type);
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
117{
118 return sb->s_fs_info;
119}
120
121/*
122 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
123 * for shared memory and for shared anonymous (/dev/zero) mappings
124 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
125 * consistent with the pre-accounting of private mappings ...
126 */
127static inline int shmem_acct_size(unsigned long flags, loff_t size)
128{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000129 return (flags & VM_NORESERVE) ?
130 0 : security_vm_enough_memory_kern(VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static inline void shmem_unacct_size(unsigned long flags, loff_t size)
134{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000135 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 vm_unacct_memory(VM_ACCT(size));
137}
138
139/*
140 * ... whereas tmpfs objects are accounted incrementally as
141 * pages are allocated, in order to allow huge sparse files.
142 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
143 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
144 */
145static inline int shmem_acct_block(unsigned long flags)
146{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000147 return (flags & VM_NORESERVE) ?
148 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151static inline void shmem_unacct_blocks(unsigned long flags, long pages)
152{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000153 if (flags & VM_NORESERVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
155}
156
Hugh Dickins759b9772007-03-05 00:30:28 -0800157static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700158static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800159static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800160static const struct inode_operations shmem_inode_operations;
161static const struct inode_operations shmem_dir_inode_operations;
162static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400163static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700165static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .ra_pages = 0, /* No readahead */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700167 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168};
169
170static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800171static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800173static int shmem_reserve_inode(struct super_block *sb)
174{
175 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
176 if (sbinfo->max_inodes) {
177 spin_lock(&sbinfo->stat_lock);
178 if (!sbinfo->free_inodes) {
179 spin_unlock(&sbinfo->stat_lock);
180 return -ENOSPC;
181 }
182 sbinfo->free_inodes--;
183 spin_unlock(&sbinfo->stat_lock);
184 }
185 return 0;
186}
187
188static void shmem_free_inode(struct super_block *sb)
189{
190 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
191 if (sbinfo->max_inodes) {
192 spin_lock(&sbinfo->stat_lock);
193 sbinfo->free_inodes++;
194 spin_unlock(&sbinfo->stat_lock);
195 }
196}
197
Randy Dunlap46711812008-03-19 17:00:41 -0700198/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700199 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * @inode: inode to recalc
201 *
202 * We have to calculate the free blocks since the mm can drop
203 * undirtied hole pages behind our back.
204 *
205 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
206 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
207 *
208 * It has to be called with the spinlock held.
209 */
210static void shmem_recalc_inode(struct inode *inode)
211{
212 struct shmem_inode_info *info = SHMEM_I(inode);
213 long freed;
214
215 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
216 if (freed > 0) {
Hugh Dickins54af60422011-08-03 16:21:24 -0700217 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
218 if (sbinfo->max_blocks)
219 percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700221 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 shmem_unacct_blocks(info->flags, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224}
225
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700226/*
227 * Replace item expected in radix tree by a new item, while holding tree lock.
228 */
229static int shmem_radix_tree_replace(struct address_space *mapping,
230 pgoff_t index, void *expected, void *replacement)
231{
232 void **pslot;
233 void *item = NULL;
234
235 VM_BUG_ON(!expected);
236 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
237 if (pslot)
238 item = radix_tree_deref_slot_protected(pslot,
239 &mapping->tree_lock);
240 if (item != expected)
241 return -ENOENT;
242 if (replacement)
243 radix_tree_replace_slot(pslot, replacement);
244 else
245 radix_tree_delete(&mapping->page_tree, index);
246 return 0;
247}
248
249/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700250 * Like add_to_page_cache_locked, but error if expected item has gone.
251 */
252static int shmem_add_to_page_cache(struct page *page,
253 struct address_space *mapping,
254 pgoff_t index, gfp_t gfp, void *expected)
255{
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700256 int error = 0;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700257
258 VM_BUG_ON(!PageLocked(page));
259 VM_BUG_ON(!PageSwapBacked(page));
260
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700261 if (!expected)
262 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
263 if (!error) {
264 page_cache_get(page);
265 page->mapping = mapping;
266 page->index = index;
267
268 spin_lock_irq(&mapping->tree_lock);
269 if (!expected)
270 error = radix_tree_insert(&mapping->page_tree,
271 index, page);
272 else
273 error = shmem_radix_tree_replace(mapping, index,
274 expected, page);
275 if (!error) {
276 mapping->nrpages++;
277 __inc_zone_page_state(page, NR_FILE_PAGES);
278 __inc_zone_page_state(page, NR_SHMEM);
279 spin_unlock_irq(&mapping->tree_lock);
280 } else {
281 page->mapping = NULL;
282 spin_unlock_irq(&mapping->tree_lock);
283 page_cache_release(page);
284 }
285 if (!expected)
286 radix_tree_preload_end();
287 }
288 if (error)
289 mem_cgroup_uncharge_cache_page(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700290 return error;
291}
292
293/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700294 * Like delete_from_page_cache, but substitutes swap for page.
295 */
296static void shmem_delete_from_page_cache(struct page *page, void *radswap)
297{
298 struct address_space *mapping = page->mapping;
299 int error;
300
301 spin_lock_irq(&mapping->tree_lock);
302 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
303 page->mapping = NULL;
304 mapping->nrpages--;
305 __dec_zone_page_state(page, NR_FILE_PAGES);
306 __dec_zone_page_state(page, NR_SHMEM);
307 spin_unlock_irq(&mapping->tree_lock);
308 page_cache_release(page);
309 BUG_ON(error);
310}
311
312/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700313 * Like find_get_pages, but collecting swap entries as well as pages.
314 */
315static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
316 pgoff_t start, unsigned int nr_pages,
317 struct page **pages, pgoff_t *indices)
318{
319 unsigned int i;
320 unsigned int ret;
321 unsigned int nr_found;
322
323 rcu_read_lock();
324restart:
325 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
326 (void ***)pages, indices, start, nr_pages);
327 ret = 0;
328 for (i = 0; i < nr_found; i++) {
329 struct page *page;
330repeat:
331 page = radix_tree_deref_slot((void **)pages[i]);
332 if (unlikely(!page))
333 continue;
334 if (radix_tree_exception(page)) {
335 if (radix_tree_exceptional_entry(page))
336 goto export;
337 /* radix_tree_deref_retry(page) */
338 goto restart;
339 }
340 if (!page_cache_get_speculative(page))
341 goto repeat;
342
343 /* Has the page moved? */
344 if (unlikely(page != *((void **)pages[i]))) {
345 page_cache_release(page);
346 goto repeat;
347 }
348export:
349 indices[ret] = indices[i];
350 pages[ret] = page;
351 ret++;
352 }
353 if (unlikely(!ret && nr_found))
354 goto restart;
355 rcu_read_unlock();
356 return ret;
357}
358
359/*
360 * Remove swap entry from radix tree, free the swap and its page cache.
361 */
362static int shmem_free_swap(struct address_space *mapping,
363 pgoff_t index, void *radswap)
364{
365 int error;
366
367 spin_lock_irq(&mapping->tree_lock);
368 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
369 spin_unlock_irq(&mapping->tree_lock);
370 if (!error)
371 free_swap_and_cache(radix_to_swp_entry(radswap));
372 return error;
373}
374
375/*
376 * Pagevec may contain swap entries, so shuffle up pages before releasing.
377 */
378static void shmem_pagevec_release(struct pagevec *pvec)
379{
380 int i, j;
381
382 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
383 struct page *page = pvec->pages[i];
384 if (!radix_tree_exceptional_entry(page))
385 pvec->pages[j++] = page;
386 }
387 pvec->nr = j;
388 pagevec_release(pvec);
389}
390
391/*
392 * Remove range of pages and swap entries from radix tree, and free them.
393 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700394void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700396 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700398 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700399 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700400 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700401 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700402 pgoff_t indices[PAGEVEC_SIZE];
403 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700404 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700405 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700407 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
408
409 pagevec_init(&pvec, 0);
410 index = start;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700411 while (index <= end) {
412 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
413 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
414 pvec.pages, indices);
415 if (!pvec.nr)
416 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700417 mem_cgroup_uncharge_start();
418 for (i = 0; i < pagevec_count(&pvec); i++) {
419 struct page *page = pvec.pages[i];
420
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700421 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700422 if (index > end)
423 break;
424
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700425 if (radix_tree_exceptional_entry(page)) {
426 nr_swaps_freed += !shmem_free_swap(mapping,
427 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700428 continue;
429 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700430
431 if (!trylock_page(page))
432 continue;
433 if (page->mapping == mapping) {
434 VM_BUG_ON(PageWriteback(page));
435 truncate_inode_page(mapping, page);
436 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700437 unlock_page(page);
438 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700439 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700440 mem_cgroup_uncharge_end();
441 cond_resched();
442 index++;
443 }
444
445 if (partial) {
446 struct page *page = NULL;
447 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
448 if (page) {
449 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
450 set_page_dirty(page);
451 unlock_page(page);
452 page_cache_release(page);
453 }
454 }
455
456 index = start;
457 for ( ; ; ) {
458 cond_resched();
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700459 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
460 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
461 pvec.pages, indices);
462 if (!pvec.nr) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700463 if (index == start)
464 break;
465 index = start;
466 continue;
467 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700468 if (index == start && indices[0] > end) {
469 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700470 break;
471 }
472 mem_cgroup_uncharge_start();
473 for (i = 0; i < pagevec_count(&pvec); i++) {
474 struct page *page = pvec.pages[i];
475
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700476 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700477 if (index > end)
478 break;
479
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700480 if (radix_tree_exceptional_entry(page)) {
481 nr_swaps_freed += !shmem_free_swap(mapping,
482 index, page);
483 continue;
484 }
485
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700486 lock_page(page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700487 if (page->mapping == mapping) {
488 VM_BUG_ON(PageWriteback(page));
489 truncate_inode_page(mapping, page);
490 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700491 unlock_page(page);
492 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700493 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700494 mem_cgroup_uncharge_end();
495 index++;
496 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700499 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 shmem_recalc_inode(inode);
501 spin_unlock(&info->lock);
502
Hugh Dickins285b2c42011-08-03 16:21:20 -0700503 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700505EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Hugh Dickins94c1e622011-06-27 16:18:03 -0700507static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int error;
511
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200512 error = inode_change_ok(inode, attr);
513 if (error)
514 return error;
515
Hugh Dickins94c1e622011-06-27 16:18:03 -0700516 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
517 loff_t oldsize = inode->i_size;
518 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000519
Hugh Dickins94c1e622011-06-27 16:18:03 -0700520 if (newsize != oldsize) {
521 i_size_write(inode, newsize);
522 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
523 }
524 if (newsize < oldsize) {
525 loff_t holebegin = round_up(newsize, PAGE_SIZE);
526 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
527 shmem_truncate_range(inode, newsize, (loff_t)-1);
528 /* unmap again to remove racily COWed private pages */
529 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200533 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700534#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200535 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100536 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700537#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return error;
539}
540
Al Viro1f895f72010-06-05 19:10:41 -0400541static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700544 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000546 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 shmem_unacct_size(info->flags, inode->i_size);
548 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000549 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800551 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800553 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Hugh Dickins69f07ec2011-08-03 16:21:26 -0700555 } else
556 kfree(info->symlink);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700557
558 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
559 kfree(xattr->name);
560 kfree(xattr);
561 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700562 BUG_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800563 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400564 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700567/*
568 * If swap found in inode, free it and move page from swapcache to filecache.
569 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700570static int shmem_unuse_inode(struct shmem_inode_info *info,
571 swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700573 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700574 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700575 pgoff_t index;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800576 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700578 radswap = swp_to_radix_entry(swap);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700579 index = radix_tree_locate_item(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700580 if (index == -1)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700581 return 0;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800582
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800583 /*
584 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400585 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800586 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700587 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800588 */
589 if (shmem_swaplist.next != &info->swaplist)
590 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800591
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800592 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700593 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
594 * but also to hold up shmem_evict_inode(): so inode cannot be freed
595 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800596 */
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700597 error = shmem_add_to_page_cache(page, mapping, index,
598 GFP_NOWAIT, radswap);
Hugh Dickins778dd892011-05-11 15:13:37 -0700599 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700600
Hugh Dickins48f170f2011-07-25 17:12:37 -0700601 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700602 /*
603 * Truncation and eviction use free_swap_and_cache(), which
604 * only does trylock page: if we raced, best clean up here.
605 */
Hugh Dickins73b12622008-02-04 22:28:50 -0800606 delete_from_swap_cache(page);
607 set_page_dirty(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700608 if (!error) {
609 spin_lock(&info->lock);
610 info->swapped--;
611 spin_unlock(&info->lock);
612 swap_free(swap);
613 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800614 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800616 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700620 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700622int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700624 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct shmem_inode_info *info;
626 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700627 int error;
628
629 /*
630 * Charge page using GFP_KERNEL while we can wait, before taking
631 * the shmem_swaplist_mutex which might hold up shmem_writepage().
632 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -0700633 */
634 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
635 if (error)
636 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700637 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800639 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700640 list_for_each_safe(this, next, &shmem_swaplist) {
641 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700642 if (info->swapped)
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700643 found = shmem_unuse_inode(info, swap, page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700644 else
645 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800646 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800647 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700648 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800650 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700651
Hugh Dickins778dd892011-05-11 15:13:37 -0700652 if (!found)
653 mem_cgroup_uncharge_cache_page(page);
654 if (found < 0)
655 error = found;
656out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800657 unlock_page(page);
658 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700659 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/*
663 * Move the page from the page cache to the swap cache.
664 */
665static int shmem_writepage(struct page *page, struct writeback_control *wbc)
666{
667 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700670 swp_entry_t swap;
671 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 mapping = page->mapping;
675 index = page->index;
676 inode = mapping->host;
677 info = SHMEM_I(inode);
678 if (info->flags & VM_LOCKED)
679 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800680 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto redirty;
682
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800683 /*
684 * shmem_backing_dev_info's capabilities prevent regular writeback or
685 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700686 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800687 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700688 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800689 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700690 if (!wbc->for_reclaim) {
691 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
692 goto redirty;
693 }
694 swap = get_swap_page();
695 if (!swap.val)
696 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800697
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700698 /*
699 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700700 * if it's not already there. Do it now before the page is
701 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700702 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700703 * we've incremented swapped, because shmem_unuse_inode() will
704 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700705 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700706 mutex_lock(&shmem_swaplist_mutex);
707 if (list_empty(&info->swaplist))
708 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700709
Hugh Dickins48f170f2011-07-25 17:12:37 -0700710 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800711 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700712 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
713
714 spin_lock(&info->lock);
715 info->swapped++;
716 shmem_recalc_inode(inode);
Hugh Dickins826267c2011-05-28 13:14:09 -0700717 spin_unlock(&info->lock);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700718
719 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800720 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700721 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
723 }
724
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700725 mutex_unlock(&shmem_swaplist_mutex);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700726 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727redirty:
728 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800729 if (wbc->for_reclaim)
730 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
731 unlock_page(page);
732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800736#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700737static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800738{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700739 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800740
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700741 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700742 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800743
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700744 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800745
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700746 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800747}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700748
749static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
750{
751 struct mempolicy *mpol = NULL;
752 if (sbinfo->mpol) {
753 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
754 mpol = sbinfo->mpol;
755 mpol_get(mpol);
756 spin_unlock(&sbinfo->stat_lock);
757 }
758 return mpol;
759}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800760#endif /* CONFIG_TMPFS */
761
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700762static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
763 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700765 struct mempolicy mpol, *spol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct vm_area_struct pvma;
767
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700768 spol = mpol_cond_copy(&mpol,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700769 mpol_shared_policy_lookup(&info->policy, index));
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* Create a pseudo vma that just contains the policy */
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800772 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700773 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800774 pvma.vm_ops = NULL;
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700775 pvma.vm_policy = spol;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700776 return swapin_readahead(swap, gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Hugh Dickins02098fe2008-02-04 22:28:42 -0800779static struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700780 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct vm_area_struct pvma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800784 /* Create a pseudo vma that just contains the policy */
785 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700786 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800787 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700788 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700789
790 /*
791 * alloc_page_vma() will drop the shared policy reference
792 */
793 return alloc_page_vma(gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800795#else /* !CONFIG_NUMA */
796#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700797static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800798{
799}
800#endif /* CONFIG_TMPFS */
801
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700802static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
803 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700805 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Hugh Dickins02098fe2008-02-04 22:28:42 -0800808static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700809 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000811 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800813#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700815#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
816static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
817{
818 return NULL;
819}
820#endif
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700823 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 *
825 * If we allocate a new one we do not mark it dirty. That's up to the
826 * vm. If we swap it in we mark it dirty since we also free the swap
827 * entry since a page cannot live in both the swap and page cache
828 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700829static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -0700830 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af60422011-08-03 16:21:24 -0700833 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700835 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 swp_entry_t swap;
837 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -0700838 int once = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700840 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -0700843 swap.val = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700844 page = find_lock_page(mapping, index);
Hugh Dickins54af60422011-08-03 16:21:24 -0700845 if (radix_tree_exceptional_entry(page)) {
846 swap = radix_to_swp_entry(page);
847 page = NULL;
848 }
849
850 if (sgp != SGP_WRITE &&
851 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
852 error = -EINVAL;
853 goto failed;
854 }
855
856 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800857 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700858 * Once we can get the page lock, it must be uptodate:
859 * if there were an error in reading back from swap,
860 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800861 */
Hugh Dickins54af60422011-08-03 16:21:24 -0700862 BUG_ON(page && !PageUptodate(page));
863 *pagep = page;
864 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /*
Hugh Dickins54af60422011-08-03 16:21:24 -0700868 * Fast cache lookup did not find it:
869 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 */
Hugh Dickins54af60422011-08-03 16:21:24 -0700871 info = SHMEM_I(inode);
872 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (swap.val) {
875 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700876 page = lookup_swap_cache(swap);
877 if (!page) {
Ying Han456f9982011-05-26 16:25:38 -0700878 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700879 if (fault_type)
880 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700881 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700882 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -0700883 error = -ENOMEM;
884 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
888 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -0700889 lock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700890 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -0700892 goto failed;
893 }
894 wait_on_page_writeback(page);
895
896 /* Someone may have already done it for us */
897 if (page->mapping) {
898 if (page->mapping == mapping &&
899 page->index == index)
900 goto done;
901 error = -EEXIST;
902 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700905 error = mem_cgroup_cache_charge(page, current->mm,
906 gfp & GFP_RECLAIM_MASK);
907 if (!error)
908 error = shmem_add_to_page_cache(page, mapping, index,
909 gfp, swp_to_radix_entry(swap));
Hugh Dickins54af60422011-08-03 16:21:24 -0700910 if (error)
911 goto failed;
912
913 spin_lock(&info->lock);
914 info->swapped--;
915 shmem_recalc_inode(inode);
916 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700917
Hugh Dickins27ab7002011-07-25 17:12:36 -0700918 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700919 set_page_dirty(page);
920 swap_free(swap);
921
Hugh Dickins54af60422011-08-03 16:21:24 -0700922 } else {
923 if (shmem_acct_block(info->flags)) {
924 error = -ENOSPC;
925 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700927 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700928 if (percpu_counter_compare(&sbinfo->used_blocks,
Hugh Dickins54af60422011-08-03 16:21:24 -0700929 sbinfo->max_blocks) >= 0) {
930 error = -ENOSPC;
931 goto unacct;
932 }
Tim Chen7e496292010-08-09 17:19:05 -0700933 percpu_counter_inc(&sbinfo->used_blocks);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Hugh Dickins54af60422011-08-03 16:21:24 -0700936 page = shmem_alloc_page(gfp, info, index);
937 if (!page) {
938 error = -ENOMEM;
939 goto decused;
940 }
941
942 SetPageSwapBacked(page);
943 __set_page_locked(page);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700944 error = mem_cgroup_cache_charge(page, current->mm,
945 gfp & GFP_RECLAIM_MASK);
946 if (!error)
947 error = shmem_add_to_page_cache(page, mapping, index,
948 gfp, NULL);
Hugh Dickins54af60422011-08-03 16:21:24 -0700949 if (error)
950 goto decused;
951 lru_cache_add_anon(page);
952
953 spin_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 info->alloced++;
Hugh Dickins54af60422011-08-03 16:21:24 -0700955 inode->i_blocks += BLOCKS_PER_PAGE;
956 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 spin_unlock(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -0700958
Hugh Dickins27ab7002011-07-25 17:12:36 -0700959 clear_highpage(page);
960 flush_dcache_page(page);
961 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -0800962 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -0700963 set_page_dirty(page);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965done:
Hugh Dickins54af60422011-08-03 16:21:24 -0700966 /* Perhaps the file has been truncated since we checked */
967 if (sgp != SGP_WRITE &&
968 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
969 error = -EINVAL;
970 goto trunc;
Shaohua Liff36b8012010-08-09 17:19:06 -0700971 }
Hugh Dickins54af60422011-08-03 16:21:24 -0700972 *pagep = page;
973 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -0700974
Nick Piggind0217ac2007-07-19 01:47:03 -0700975 /*
Hugh Dickins54af60422011-08-03 16:21:24 -0700976 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
Hugh Dickins54af60422011-08-03 16:21:24 -0700978trunc:
979 ClearPageDirty(page);
980 delete_from_page_cache(page);
981 spin_lock(&info->lock);
982 info->alloced--;
983 inode->i_blocks -= BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 spin_unlock(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -0700985decused:
986 if (sbinfo->max_blocks)
987 percpu_counter_add(&sbinfo->used_blocks, -1);
988unacct:
989 shmem_unacct_blocks(info->flags, 1);
990failed:
991 if (swap.val && error != -EINVAL) {
992 struct page *test = find_get_page(mapping, index);
993 if (test && !radix_tree_exceptional_entry(test))
994 page_cache_release(test);
995 /* Have another try if the entry has changed */
996 if (test != swp_to_radix_entry(swap))
997 error = -EEXIST;
998 }
Hugh Dickins27ab7002011-07-25 17:12:36 -0700999 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001000 unlock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001001 page_cache_release(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001002 }
1003 if (error == -ENOSPC && !once++) {
1004 info = SHMEM_I(inode);
1005 spin_lock(&info->lock);
1006 shmem_recalc_inode(inode);
1007 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001008 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001009 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001010 if (error == -EEXIST)
1011 goto repeat;
1012 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1016{
1017 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1018 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001019 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1022 if (error)
1023 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001024
Ying Han456f9982011-05-26 16:25:38 -07001025 if (ret & VM_FAULT_MAJOR) {
1026 count_vm_event(PGMAJFAULT);
1027 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1028 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001029 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001033static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001035 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1036 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001039static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1040 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001042 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1043 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001045 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1046 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048#endif
1049
1050int shmem_lock(struct file *file, int lock, struct user_struct *user)
1051{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001052 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct shmem_inode_info *info = SHMEM_I(inode);
1054 int retval = -ENOMEM;
1055
1056 spin_lock(&info->lock);
1057 if (lock && !(info->flags & VM_LOCKED)) {
1058 if (!user_shm_lock(inode->i_size, user))
1059 goto out_nomem;
1060 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001061 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 if (!lock && (info->flags & VM_LOCKED) && user) {
1064 user_shm_unlock(inode->i_size, user);
1065 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001066 mapping_clear_unevictable(file->f_mapping);
1067 scan_mapping_unevictable_pages(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071out_nomem:
1072 spin_unlock(&info->lock);
1073 return retval;
1074}
1075
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001076static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 file_accessed(file);
1079 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001080 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 0;
1082}
1083
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001084static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1085 int mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 struct inode *inode;
1088 struct shmem_inode_info *info;
1089 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1090
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001091 if (shmem_reserve_inode(sb))
1092 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 inode = new_inode(sb);
1095 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001096 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001097 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1100 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001101 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 info = SHMEM_I(inode);
1103 memset(info, 0, (char *)inode - (char *)info);
1104 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001105 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001107 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001108 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 switch (mode & S_IFMT) {
1111 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001112 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 init_special_inode(inode, mode, dev);
1114 break;
1115 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001116 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 inode->i_op = &shmem_inode_operations;
1118 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001119 mpol_shared_policy_init(&info->policy,
1120 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 break;
1122 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001123 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /* Some things misbehave if size == 0 on a directory */
1125 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1126 inode->i_op = &shmem_dir_inode_operations;
1127 inode->i_fop = &simple_dir_operations;
1128 break;
1129 case S_IFLNK:
1130 /*
1131 * Must not load anything in the rbtree,
1132 * mpol_free_shared_policy will not be called.
1133 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001134 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 break;
1136 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001137 } else
1138 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return inode;
1140}
1141
1142#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001143static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001144static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001147shmem_write_begin(struct file *file, struct address_space *mapping,
1148 loff_t pos, unsigned len, unsigned flags,
1149 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Nick Piggin800d15a2007-10-16 01:25:03 -07001151 struct inode *inode = mapping->host;
1152 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -07001153 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1154}
1155
1156static int
1157shmem_write_end(struct file *file, struct address_space *mapping,
1158 loff_t pos, unsigned len, unsigned copied,
1159 struct page *page, void *fsdata)
1160{
1161 struct inode *inode = mapping->host;
1162
Hugh Dickinsd3602442008-02-04 22:28:44 -08001163 if (pos + copied > inode->i_size)
1164 i_size_write(inode, pos + copied);
1165
Nick Piggin800d15a2007-10-16 01:25:03 -07001166 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001167 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001168 page_cache_release(page);
1169
Nick Piggin800d15a2007-10-16 01:25:03 -07001170 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1174{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001175 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001177 pgoff_t index;
1178 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001179 enum sgp_type sgp = SGP_READ;
1180
1181 /*
1182 * Might this read be for a stacking filesystem? Then when reading
1183 * holes of a sparse file, we actually need to allocate those pages,
1184 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1185 */
1186 if (segment_eq(get_fs(), KERNEL_DS))
1187 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 index = *ppos >> PAGE_CACHE_SHIFT;
1190 offset = *ppos & ~PAGE_CACHE_MASK;
1191
1192 for (;;) {
1193 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001194 pgoff_t end_index;
1195 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 loff_t i_size = i_size_read(inode);
1197
1198 end_index = i_size >> PAGE_CACHE_SHIFT;
1199 if (index > end_index)
1200 break;
1201 if (index == end_index) {
1202 nr = i_size & ~PAGE_CACHE_MASK;
1203 if (nr <= offset)
1204 break;
1205 }
1206
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001207 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (desc->error) {
1209 if (desc->error == -EINVAL)
1210 desc->error = 0;
1211 break;
1212 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001213 if (page)
1214 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /*
1217 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001218 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
1220 nr = PAGE_CACHE_SIZE;
1221 i_size = i_size_read(inode);
1222 end_index = i_size >> PAGE_CACHE_SHIFT;
1223 if (index == end_index) {
1224 nr = i_size & ~PAGE_CACHE_MASK;
1225 if (nr <= offset) {
1226 if (page)
1227 page_cache_release(page);
1228 break;
1229 }
1230 }
1231 nr -= offset;
1232
1233 if (page) {
1234 /*
1235 * If users can be writing to this page using arbitrary
1236 * virtual addresses, take care about potential aliasing
1237 * before reading the page on the kernel side.
1238 */
1239 if (mapping_writably_mapped(mapping))
1240 flush_dcache_page(page);
1241 /*
1242 * Mark the page accessed if we read the beginning.
1243 */
1244 if (!offset)
1245 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001246 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001248 page_cache_get(page);
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /*
1252 * Ok, we have the page, and it's up-to-date, so
1253 * now we can copy it to user space...
1254 *
1255 * The actor routine returns how many bytes were actually used..
1256 * NOTE! This may not be the same as how much of a user buffer
1257 * we filled up (we may be padding etc), so we can only update
1258 * "pos" here (the actor routine has to update the user buffer
1259 * pointers and the remaining count).
1260 */
1261 ret = actor(desc, page, offset, nr);
1262 offset += ret;
1263 index += offset >> PAGE_CACHE_SHIFT;
1264 offset &= ~PAGE_CACHE_MASK;
1265
1266 page_cache_release(page);
1267 if (ret != nr || !desc->count)
1268 break;
1269
1270 cond_resched();
1271 }
1272
1273 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1274 file_accessed(filp);
1275}
1276
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001277static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1278 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001280 struct file *filp = iocb->ki_filp;
1281 ssize_t retval;
1282 unsigned long seg;
1283 size_t count;
1284 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001286 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1287 if (retval)
1288 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001290 for (seg = 0; seg < nr_segs; seg++) {
1291 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001293 desc.written = 0;
1294 desc.arg.buf = iov[seg].iov_base;
1295 desc.count = iov[seg].iov_len;
1296 if (desc.count == 0)
1297 continue;
1298 desc.error = 0;
1299 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1300 retval += desc.written;
1301 if (desc.error) {
1302 retval = retval ?: desc.error;
1303 break;
1304 }
1305 if (desc.count > 0)
1306 break;
1307 }
1308 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Hugh Dickins708e3502011-07-25 17:12:32 -07001311static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1312 struct pipe_inode_info *pipe, size_t len,
1313 unsigned int flags)
1314{
1315 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001316 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001317 unsigned int loff, nr_pages, req_pages;
1318 struct page *pages[PIPE_DEF_BUFFERS];
1319 struct partial_page partial[PIPE_DEF_BUFFERS];
1320 struct page *page;
1321 pgoff_t index, end_index;
1322 loff_t isize, left;
1323 int error, page_nr;
1324 struct splice_pipe_desc spd = {
1325 .pages = pages,
1326 .partial = partial,
1327 .flags = flags,
1328 .ops = &page_cache_pipe_buf_ops,
1329 .spd_release = spd_release_page,
1330 };
1331
Hugh Dickins71f0e072011-07-25 17:12:33 -07001332 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001333 if (unlikely(*ppos >= isize))
1334 return 0;
1335
1336 left = isize - *ppos;
1337 if (unlikely(left < len))
1338 len = left;
1339
1340 if (splice_grow_spd(pipe, &spd))
1341 return -ENOMEM;
1342
1343 index = *ppos >> PAGE_CACHE_SHIFT;
1344 loff = *ppos & ~PAGE_CACHE_MASK;
1345 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1346 nr_pages = min(req_pages, pipe->buffers);
1347
Hugh Dickins708e3502011-07-25 17:12:32 -07001348 spd.nr_pages = find_get_pages_contig(mapping, index,
1349 nr_pages, spd.pages);
1350 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001351 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001352
Hugh Dickins708e3502011-07-25 17:12:32 -07001353 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001354 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1355 if (error)
1356 break;
1357 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001358 spd.pages[spd.nr_pages++] = page;
1359 index++;
1360 }
1361
Hugh Dickins708e3502011-07-25 17:12:32 -07001362 index = *ppos >> PAGE_CACHE_SHIFT;
1363 nr_pages = spd.nr_pages;
1364 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001365
Hugh Dickins708e3502011-07-25 17:12:32 -07001366 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1367 unsigned int this_len;
1368
1369 if (!len)
1370 break;
1371
Hugh Dickins708e3502011-07-25 17:12:32 -07001372 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1373 page = spd.pages[page_nr];
1374
Hugh Dickins71f0e072011-07-25 17:12:33 -07001375 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001376 error = shmem_getpage(inode, index, &page,
1377 SGP_CACHE, NULL);
1378 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001379 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001380 unlock_page(page);
1381 page_cache_release(spd.pages[page_nr]);
1382 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001383 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001384
1385 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001386 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1387 if (unlikely(!isize || index > end_index))
1388 break;
1389
Hugh Dickins708e3502011-07-25 17:12:32 -07001390 if (end_index == index) {
1391 unsigned int plen;
1392
Hugh Dickins708e3502011-07-25 17:12:32 -07001393 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1394 if (plen <= loff)
1395 break;
1396
Hugh Dickins708e3502011-07-25 17:12:32 -07001397 this_len = min(this_len, plen - loff);
1398 len = this_len;
1399 }
1400
1401 spd.partial[page_nr].offset = loff;
1402 spd.partial[page_nr].len = this_len;
1403 len -= this_len;
1404 loff = 0;
1405 spd.nr_pages++;
1406 index++;
1407 }
1408
Hugh Dickins708e3502011-07-25 17:12:32 -07001409 while (page_nr < nr_pages)
1410 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001411
1412 if (spd.nr_pages)
1413 error = splice_to_pipe(pipe, &spd);
1414
1415 splice_shrink_spd(pipe, &spd);
1416
1417 if (error > 0) {
1418 *ppos += error;
1419 file_accessed(in);
1420 }
1421 return error;
1422}
1423
David Howells726c3342006-06-23 02:02:58 -07001424static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
David Howells726c3342006-06-23 02:02:58 -07001426 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 buf->f_type = TMPFS_MAGIC;
1429 buf->f_bsize = PAGE_CACHE_SIZE;
1430 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001431 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001433 buf->f_bavail =
1434 buf->f_bfree = sbinfo->max_blocks -
1435 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001436 }
1437 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 buf->f_files = sbinfo->max_inodes;
1439 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441 /* else leave those fields 0 like simple_statfs */
1442 return 0;
1443}
1444
1445/*
1446 * File creation. Allocate an inode, and we're done..
1447 */
1448static int
1449shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1450{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001451 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 int error = -ENOSPC;
1453
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001454 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001456 error = security_inode_init_security(inode, dir,
1457 &dentry->d_name, NULL,
1458 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001459 if (error) {
1460 if (error != -EOPNOTSUPP) {
1461 iput(inode);
1462 return error;
1463 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001464 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001465#ifdef CONFIG_TMPFS_POSIX_ACL
1466 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001467 if (error) {
1468 iput(inode);
1469 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001470 }
Al Viro718deb62009-12-16 19:35:36 -05001471#else
1472 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001473#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 dir->i_size += BOGO_DIRENT_SIZE;
1475 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1476 d_instantiate(dentry, inode);
1477 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479 return error;
1480}
1481
1482static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1483{
1484 int error;
1485
1486 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1487 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001488 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return 0;
1490}
1491
1492static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1493 struct nameidata *nd)
1494{
1495 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1496}
1497
1498/*
1499 * Link a file..
1500 */
1501static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1502{
1503 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001504 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 /*
1507 * No ordinary (disk based) filesystem counts links as inodes;
1508 * but each new link needs a new dentry, pinning lowmem, and
1509 * tmpfs dentries cannot be pruned until they are unlinked.
1510 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001511 ret = shmem_reserve_inode(inode->i_sb);
1512 if (ret)
1513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 dir->i_size += BOGO_DIRENT_SIZE;
1516 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001517 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04001518 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 dget(dentry); /* Extra pinning count for the created dentry */
1520 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001521out:
1522 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1526{
1527 struct inode *inode = dentry->d_inode;
1528
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001529 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1530 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 dir->i_size -= BOGO_DIRENT_SIZE;
1533 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001534 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 dput(dentry); /* Undo the count from "create" - this does all the work */
1536 return 0;
1537}
1538
1539static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1540{
1541 if (!simple_empty(dentry))
1542 return -ENOTEMPTY;
1543
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001544 drop_nlink(dentry->d_inode);
1545 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return shmem_unlink(dir, dentry);
1547}
1548
1549/*
1550 * The VFS layer already does all the dentry stuff for rename,
1551 * we just have to decrement the usage count for the target if
1552 * it exists so that the VFS layer correctly free's it when it
1553 * gets overwritten.
1554 */
1555static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1556{
1557 struct inode *inode = old_dentry->d_inode;
1558 int they_are_dirs = S_ISDIR(inode->i_mode);
1559
1560 if (!simple_empty(new_dentry))
1561 return -ENOTEMPTY;
1562
1563 if (new_dentry->d_inode) {
1564 (void) shmem_unlink(new_dir, new_dentry);
1565 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001566 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001568 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001569 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571
1572 old_dir->i_size -= BOGO_DIRENT_SIZE;
1573 new_dir->i_size += BOGO_DIRENT_SIZE;
1574 old_dir->i_ctime = old_dir->i_mtime =
1575 new_dir->i_ctime = new_dir->i_mtime =
1576 inode->i_ctime = CURRENT_TIME;
1577 return 0;
1578}
1579
1580static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1581{
1582 int error;
1583 int len;
1584 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001585 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 char *kaddr;
1587 struct shmem_inode_info *info;
1588
1589 len = strlen(symname) + 1;
1590 if (len > PAGE_CACHE_SIZE)
1591 return -ENAMETOOLONG;
1592
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001593 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!inode)
1595 return -ENOSPC;
1596
Eric Paris2a7dba32011-02-01 11:05:39 -05001597 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
1598 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001599 if (error) {
1600 if (error != -EOPNOTSUPP) {
1601 iput(inode);
1602 return error;
1603 }
1604 error = 0;
1605 }
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 info = SHMEM_I(inode);
1608 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001609 if (len <= SHORT_SYMLINK_LEN) {
1610 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1611 if (!info->symlink) {
1612 iput(inode);
1613 return -ENOMEM;
1614 }
1615 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 } else {
1617 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1618 if (error) {
1619 iput(inode);
1620 return error;
1621 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001622 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 inode->i_op = &shmem_symlink_inode_operations;
1624 kaddr = kmap_atomic(page, KM_USER0);
1625 memcpy(kaddr, symname, len);
1626 kunmap_atomic(kaddr, KM_USER0);
1627 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001628 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 page_cache_release(page);
1630 }
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);
1635 return 0;
1636}
1637
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001638static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001640 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001641 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001644static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001647 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1648 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001649 if (page)
1650 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001651 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001654static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001657 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 kunmap(page);
1659 mark_page_accessed(page);
1660 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662}
1663
Eric Parisb09e0fa2011-05-24 17:12:39 -07001664#ifdef CONFIG_TMPFS_XATTR
1665/*
1666 * Superblocks without xattr inode operations may get some security.* xattr
1667 * support from the LSM "for free". As soon as we have any other xattrs
1668 * like ACLs, we also need to implement the security.* handlers at
1669 * filesystem level, though.
1670 */
1671
1672static int shmem_xattr_get(struct dentry *dentry, const char *name,
1673 void *buffer, size_t size)
1674{
1675 struct shmem_inode_info *info;
1676 struct shmem_xattr *xattr;
1677 int ret = -ENODATA;
1678
1679 info = SHMEM_I(dentry->d_inode);
1680
1681 spin_lock(&info->lock);
1682 list_for_each_entry(xattr, &info->xattr_list, list) {
1683 if (strcmp(name, xattr->name))
1684 continue;
1685
1686 ret = xattr->size;
1687 if (buffer) {
1688 if (size < xattr->size)
1689 ret = -ERANGE;
1690 else
1691 memcpy(buffer, xattr->value, xattr->size);
1692 }
1693 break;
1694 }
1695 spin_unlock(&info->lock);
1696 return ret;
1697}
1698
1699static int shmem_xattr_set(struct dentry *dentry, const char *name,
1700 const void *value, size_t size, int flags)
1701{
1702 struct inode *inode = dentry->d_inode;
1703 struct shmem_inode_info *info = SHMEM_I(inode);
1704 struct shmem_xattr *xattr;
1705 struct shmem_xattr *new_xattr = NULL;
1706 size_t len;
1707 int err = 0;
1708
1709 /* value == NULL means remove */
1710 if (value) {
1711 /* wrap around? */
1712 len = sizeof(*new_xattr) + size;
1713 if (len <= sizeof(*new_xattr))
1714 return -ENOMEM;
1715
1716 new_xattr = kmalloc(len, GFP_KERNEL);
1717 if (!new_xattr)
1718 return -ENOMEM;
1719
1720 new_xattr->name = kstrdup(name, GFP_KERNEL);
1721 if (!new_xattr->name) {
1722 kfree(new_xattr);
1723 return -ENOMEM;
1724 }
1725
1726 new_xattr->size = size;
1727 memcpy(new_xattr->value, value, size);
1728 }
1729
1730 spin_lock(&info->lock);
1731 list_for_each_entry(xattr, &info->xattr_list, list) {
1732 if (!strcmp(name, xattr->name)) {
1733 if (flags & XATTR_CREATE) {
1734 xattr = new_xattr;
1735 err = -EEXIST;
1736 } else if (new_xattr) {
1737 list_replace(&xattr->list, &new_xattr->list);
1738 } else {
1739 list_del(&xattr->list);
1740 }
1741 goto out;
1742 }
1743 }
1744 if (flags & XATTR_REPLACE) {
1745 xattr = new_xattr;
1746 err = -ENODATA;
1747 } else {
1748 list_add(&new_xattr->list, &info->xattr_list);
1749 xattr = NULL;
1750 }
1751out:
1752 spin_unlock(&info->lock);
1753 if (xattr)
1754 kfree(xattr->name);
1755 kfree(xattr);
1756 return err;
1757}
1758
Eric Parisb09e0fa2011-05-24 17:12:39 -07001759static const struct xattr_handler *shmem_xattr_handlers[] = {
1760#ifdef CONFIG_TMPFS_POSIX_ACL
1761 &generic_acl_access_handler,
1762 &generic_acl_default_handler,
1763#endif
1764 NULL
1765};
1766
1767static int shmem_xattr_validate(const char *name)
1768{
1769 struct { const char *prefix; size_t len; } arr[] = {
1770 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1771 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1772 };
1773 int i;
1774
1775 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1776 size_t preflen = arr[i].len;
1777 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1778 if (!name[preflen])
1779 return -EINVAL;
1780 return 0;
1781 }
1782 }
1783 return -EOPNOTSUPP;
1784}
1785
1786static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1787 void *buffer, size_t size)
1788{
1789 int err;
1790
1791 /*
1792 * If this is a request for a synthetic attribute in the system.*
1793 * namespace use the generic infrastructure to resolve a handler
1794 * for it via sb->s_xattr.
1795 */
1796 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1797 return generic_getxattr(dentry, name, buffer, size);
1798
1799 err = shmem_xattr_validate(name);
1800 if (err)
1801 return err;
1802
1803 return shmem_xattr_get(dentry, name, buffer, size);
1804}
1805
1806static int shmem_setxattr(struct dentry *dentry, const char *name,
1807 const void *value, size_t size, int flags)
1808{
1809 int err;
1810
1811 /*
1812 * If this is a request for a synthetic attribute in the system.*
1813 * namespace use the generic infrastructure to resolve a handler
1814 * for it via sb->s_xattr.
1815 */
1816 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1817 return generic_setxattr(dentry, name, value, size, flags);
1818
1819 err = shmem_xattr_validate(name);
1820 if (err)
1821 return err;
1822
1823 if (size == 0)
1824 value = ""; /* empty EA, do not remove */
1825
1826 return shmem_xattr_set(dentry, name, value, size, flags);
1827
1828}
1829
1830static int shmem_removexattr(struct dentry *dentry, const char *name)
1831{
1832 int err;
1833
1834 /*
1835 * If this is a request for a synthetic attribute in the system.*
1836 * namespace use the generic infrastructure to resolve a handler
1837 * for it via sb->s_xattr.
1838 */
1839 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1840 return generic_removexattr(dentry, name);
1841
1842 err = shmem_xattr_validate(name);
1843 if (err)
1844 return err;
1845
1846 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
1847}
1848
1849static bool xattr_is_trusted(const char *name)
1850{
1851 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1852}
1853
1854static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1855{
1856 bool trusted = capable(CAP_SYS_ADMIN);
1857 struct shmem_xattr *xattr;
1858 struct shmem_inode_info *info;
1859 size_t used = 0;
1860
1861 info = SHMEM_I(dentry->d_inode);
1862
1863 spin_lock(&info->lock);
1864 list_for_each_entry(xattr, &info->xattr_list, list) {
1865 size_t len;
1866
1867 /* skip "trusted." attributes for unprivileged callers */
1868 if (!trusted && xattr_is_trusted(xattr->name))
1869 continue;
1870
1871 len = strlen(xattr->name) + 1;
1872 used += len;
1873 if (buffer) {
1874 if (size < used) {
1875 used = -ERANGE;
1876 break;
1877 }
1878 memcpy(buffer, xattr->name, len);
1879 buffer += len;
1880 }
1881 }
1882 spin_unlock(&info->lock);
1883
1884 return used;
1885}
1886#endif /* CONFIG_TMPFS_XATTR */
1887
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001888static const struct inode_operations shmem_short_symlink_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 .readlink = generic_readlink,
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001890 .follow_link = shmem_follow_short_symlink,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001891#ifdef CONFIG_TMPFS_XATTR
1892 .setxattr = shmem_setxattr,
1893 .getxattr = shmem_getxattr,
1894 .listxattr = shmem_listxattr,
1895 .removexattr = shmem_removexattr,
1896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897};
1898
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001899static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 .readlink = generic_readlink,
1901 .follow_link = shmem_follow_link,
1902 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001903#ifdef CONFIG_TMPFS_XATTR
1904 .setxattr = shmem_setxattr,
1905 .getxattr = shmem_getxattr,
1906 .listxattr = shmem_listxattr,
1907 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001908#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07001909};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001910
David M. Grimes91828a42006-10-17 00:09:45 -07001911static struct dentry *shmem_get_parent(struct dentry *child)
1912{
1913 return ERR_PTR(-ESTALE);
1914}
1915
1916static int shmem_match(struct inode *ino, void *vfh)
1917{
1918 __u32 *fh = vfh;
1919 __u64 inum = fh[2];
1920 inum = (inum << 32) | fh[1];
1921 return ino->i_ino == inum && fh[0] == ino->i_generation;
1922}
1923
Christoph Hellwig480b1162007-10-21 16:42:13 -07001924static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
1925 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07001926{
David M. Grimes91828a42006-10-17 00:09:45 -07001927 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07001928 struct dentry *dentry = NULL;
1929 u64 inum = fid->raw[2];
1930 inum = (inum << 32) | fid->raw[1];
David M. Grimes91828a42006-10-17 00:09:45 -07001931
Christoph Hellwig480b1162007-10-21 16:42:13 -07001932 if (fh_len < 3)
1933 return NULL;
1934
1935 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
1936 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07001937 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07001938 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07001939 iput(inode);
1940 }
1941
Christoph Hellwig480b1162007-10-21 16:42:13 -07001942 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07001943}
1944
1945static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
1946 int connectable)
1947{
1948 struct inode *inode = dentry->d_inode;
1949
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301950 if (*len < 3) {
1951 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07001952 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301953 }
David M. Grimes91828a42006-10-17 00:09:45 -07001954
Al Viro1d3382cb2010-10-23 15:19:20 -04001955 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07001956 /* Unfortunately insert_inode_hash is not idempotent,
1957 * so as we hash inodes here rather than at creation
1958 * time, we need a lock to ensure we only try
1959 * to do it once
1960 */
1961 static DEFINE_SPINLOCK(lock);
1962 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04001963 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07001964 __insert_inode_hash(inode,
1965 inode->i_ino + inode->i_generation);
1966 spin_unlock(&lock);
1967 }
1968
1969 fh[0] = inode->i_generation;
1970 fh[1] = inode->i_ino;
1971 fh[2] = ((__u64)inode->i_ino) >> 32;
1972
1973 *len = 3;
1974 return 1;
1975}
1976
Christoph Hellwig39655162007-10-21 16:42:17 -07001977static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07001978 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07001979 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07001980 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07001981};
1982
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001983static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
1984 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 char *this_char, *value, *rest;
1987
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00001988 while (options != NULL) {
1989 this_char = options;
1990 for (;;) {
1991 /*
1992 * NUL-terminate this option: unfortunately,
1993 * mount options form a comma-separated list,
1994 * but mpol's nodelist may also contain commas.
1995 */
1996 options = strchr(options, ',');
1997 if (options == NULL)
1998 break;
1999 options++;
2000 if (!isdigit(*options)) {
2001 options[-1] = '\0';
2002 break;
2003 }
2004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (!*this_char)
2006 continue;
2007 if ((value = strchr(this_char,'=')) != NULL) {
2008 *value++ = 0;
2009 } else {
2010 printk(KERN_ERR
2011 "tmpfs: No value for mount option '%s'\n",
2012 this_char);
2013 return 1;
2014 }
2015
2016 if (!strcmp(this_char,"size")) {
2017 unsigned long long size;
2018 size = memparse(value,&rest);
2019 if (*rest == '%') {
2020 size <<= PAGE_SHIFT;
2021 size *= totalram_pages;
2022 do_div(size, 100);
2023 rest++;
2024 }
2025 if (*rest)
2026 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002027 sbinfo->max_blocks =
2028 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002030 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (*rest)
2032 goto bad_val;
2033 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002034 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (*rest)
2036 goto bad_val;
2037 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002038 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002040 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (*rest)
2042 goto bad_val;
2043 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002044 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002046 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (*rest)
2048 goto bad_val;
2049 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002050 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002052 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (*rest)
2054 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002055 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002056 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08002057 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 } else {
2059 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2060 this_char);
2061 return 1;
2062 }
2063 }
2064 return 0;
2065
2066bad_val:
2067 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2068 value, this_char);
2069 return 1;
2070
2071}
2072
2073static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2074{
2075 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002076 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002077 unsigned long inodes;
2078 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002080 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002081 return error;
2082
2083 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002084 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002085 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002086 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002087 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002088 goto out;
2089 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07002090 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002091 * but we must separately disallow unlimited->limited, because
2092 * in that case we have no record of how much is already in use.
2093 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002094 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002095 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002096 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002097 goto out;
2098
2099 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002100 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002101 sbinfo->max_inodes = config.max_inodes;
2102 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002103
2104 mpol_put(sbinfo->mpol);
2105 sbinfo->mpol = config.mpol; /* transfers initial ref */
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002106out:
2107 spin_unlock(&sbinfo->stat_lock);
2108 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002110
2111static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2112{
2113 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2114
2115 if (sbinfo->max_blocks != shmem_default_max_blocks())
2116 seq_printf(seq, ",size=%luk",
2117 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2118 if (sbinfo->max_inodes != shmem_default_max_inodes())
2119 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2120 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2121 seq_printf(seq, ",mode=%03o", sbinfo->mode);
2122 if (sbinfo->uid != 0)
2123 seq_printf(seq, ",uid=%u", sbinfo->uid);
2124 if (sbinfo->gid != 0)
2125 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002126 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002127 return 0;
2128}
2129#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131static void shmem_put_super(struct super_block *sb)
2132{
Hugh Dickins602586a2010-08-17 15:23:56 -07002133 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2134
2135 percpu_counter_destroy(&sbinfo->used_blocks);
2136 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 sb->s_fs_info = NULL;
2138}
2139
Kay Sievers2b2af542009-04-30 15:23:42 +02002140int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 struct inode *inode;
2143 struct dentry *root;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002144 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002145 int err = -ENOMEM;
2146
2147 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002148 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002149 L1_CACHE_BYTES), GFP_KERNEL);
2150 if (!sbinfo)
2151 return -ENOMEM;
2152
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002153 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002154 sbinfo->uid = current_fsuid();
2155 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002156 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002158#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /*
2160 * Per default we only allow half of the physical ram per
2161 * tmpfs instance, limiting inodes to one per page of lowmem;
2162 * but the internal instance is left unlimited.
2163 */
2164 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002165 sbinfo->max_blocks = shmem_default_max_blocks();
2166 sbinfo->max_inodes = shmem_default_max_inodes();
2167 if (shmem_parse_options(data, sbinfo, false)) {
2168 err = -EINVAL;
2169 goto failed;
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
David M. Grimes91828a42006-10-17 00:09:45 -07002172 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173#else
2174 sb->s_flags |= MS_NOUSER;
2175#endif
2176
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002177 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002178 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2179 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002180 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002181
Hugh Dickins285b2c42011-08-03 16:21:20 -07002182 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 sb->s_blocksize = PAGE_CACHE_SIZE;
2184 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2185 sb->s_magic = TMPFS_MAGIC;
2186 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002187 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002188#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002189 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002190#endif
2191#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002192 sb->s_flags |= MS_POSIXACL;
2193#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002194
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002195 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (!inode)
2197 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002198 inode->i_uid = sbinfo->uid;
2199 inode->i_gid = sbinfo->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 root = d_alloc_root(inode);
2201 if (!root)
2202 goto failed_iput;
2203 sb->s_root = root;
2204 return 0;
2205
2206failed_iput:
2207 iput(inode);
2208failed:
2209 shmem_put_super(sb);
2210 return err;
2211}
2212
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002213static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215static struct inode *shmem_alloc_inode(struct super_block *sb)
2216{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002217 struct shmem_inode_info *info;
2218 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2219 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002221 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002224static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002225{
2226 struct inode *inode = container_of(head, struct inode, i_rcu);
2227 INIT_LIST_HEAD(&inode->i_dentry);
2228 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2229}
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231static void shmem_destroy_inode(struct inode *inode)
2232{
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002233 if ((inode->i_mode & S_IFMT) == S_IFREG)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002235 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002238static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002240 struct shmem_inode_info *info = foo;
2241 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002244static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
2246 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2247 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002248 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return 0;
2250}
2251
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002252static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002254 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002257static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002259 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002261 .write_begin = shmem_write_begin,
2262 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002264 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002265 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266};
2267
Helge Deller15ad7cd2006-12-06 20:40:36 -08002268static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 .mmap = shmem_mmap,
2270#ifdef CONFIG_TMPFS
2271 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002272 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002273 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002274 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002275 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002276 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002277 .splice_read = shmem_file_splice_read,
Hugh Dickinsae9764162007-06-04 10:00:39 +02002278 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279#endif
2280};
2281
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002282static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002283 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002284 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002285#ifdef CONFIG_TMPFS_XATTR
2286 .setxattr = shmem_setxattr,
2287 .getxattr = shmem_getxattr,
2288 .listxattr = shmem_listxattr,
2289 .removexattr = shmem_removexattr,
2290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291};
2292
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002293static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294#ifdef CONFIG_TMPFS
2295 .create = shmem_create,
2296 .lookup = simple_lookup,
2297 .link = shmem_link,
2298 .unlink = shmem_unlink,
2299 .symlink = shmem_symlink,
2300 .mkdir = shmem_mkdir,
2301 .rmdir = shmem_rmdir,
2302 .mknod = shmem_mknod,
2303 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002305#ifdef CONFIG_TMPFS_XATTR
2306 .setxattr = shmem_setxattr,
2307 .getxattr = shmem_getxattr,
2308 .listxattr = shmem_listxattr,
2309 .removexattr = shmem_removexattr,
2310#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002311#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002312 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002313#endif
2314};
2315
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002316static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002317#ifdef CONFIG_TMPFS_XATTR
2318 .setxattr = shmem_setxattr,
2319 .getxattr = shmem_getxattr,
2320 .listxattr = shmem_listxattr,
2321 .removexattr = shmem_removexattr,
2322#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002323#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002324 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002325#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326};
2327
Hugh Dickins759b9772007-03-05 00:30:28 -08002328static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 .alloc_inode = shmem_alloc_inode,
2330 .destroy_inode = shmem_destroy_inode,
2331#ifdef CONFIG_TMPFS
2332 .statfs = shmem_statfs,
2333 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002334 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335#endif
Al Viro1f895f72010-06-05 19:10:41 -04002336 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 .drop_inode = generic_delete_inode,
2338 .put_super = shmem_put_super,
2339};
2340
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002341static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002342 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343#ifdef CONFIG_NUMA
2344 .set_policy = shmem_set_policy,
2345 .get_policy = shmem_get_policy,
2346#endif
2347};
2348
Al Viro3c26ff62010-07-25 11:46:36 +04002349static struct dentry *shmem_mount(struct file_system_type *fs_type,
2350 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Al Viro3c26ff62010-07-25 11:46:36 +04002352 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002355static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 .owner = THIS_MODULE,
2357 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002358 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 .kill_sb = kill_litter_super,
2360};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002362int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
2364 int error;
2365
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002366 error = bdi_init(&shmem_backing_dev_info);
2367 if (error)
2368 goto out4;
2369
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002370 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (error)
2372 goto out3;
2373
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002374 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (error) {
2376 printk(KERN_ERR "Could not register tmpfs\n");
2377 goto out2;
2378 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002379
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002380 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2381 shmem_fs_type.name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (IS_ERR(shm_mnt)) {
2383 error = PTR_ERR(shm_mnt);
2384 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2385 goto out1;
2386 }
2387 return 0;
2388
2389out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002390 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002392 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002394 bdi_destroy(&shmem_backing_dev_info);
2395out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 shm_mnt = ERR_PTR(error);
2397 return error;
2398}
Matt Mackall853ac432009-01-06 14:40:20 -08002399
2400#else /* !CONFIG_SHMEM */
2401
2402/*
2403 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2404 *
2405 * This is intended for small system where the benefits of the full
2406 * shmem code (swap-backed and resource-limited) are outweighed by
2407 * their complexity. On systems without swap this code should be
2408 * effectively equivalent, but much lighter weight.
2409 */
2410
2411#include <linux/ramfs.h>
2412
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002413static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08002414 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002415 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002416 .kill_sb = kill_litter_super,
2417};
2418
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002419int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002420{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002421 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08002422
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002423 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08002424 BUG_ON(IS_ERR(shm_mnt));
2425
2426 return 0;
2427}
2428
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002429int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08002430{
2431 return 0;
2432}
2433
Hugh Dickins3f96b792009-09-21 17:03:37 -07002434int shmem_lock(struct file *file, int lock, struct user_struct *user)
2435{
2436 return 0;
2437}
2438
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002439void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07002440{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002441 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07002442}
2443EXPORT_SYMBOL_GPL(shmem_truncate_range);
2444
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002445#define shmem_vm_ops generic_file_vm_ops
2446#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002447#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002448#define shmem_acct_size(flags, size) 0
2449#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002450
2451#endif /* CONFIG_SHMEM */
2452
2453/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Randy Dunlap46711812008-03-19 17:00:41 -07002455/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 * @name: name for dentry (to be seen in /proc/<pid>/maps
2458 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002459 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002461struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 int error;
2464 struct file *file;
2465 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002466 struct path path;
2467 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 struct qstr this;
2469
2470 if (IS_ERR(shm_mnt))
2471 return (void *)shm_mnt;
2472
Hugh Dickins285b2c42011-08-03 16:21:20 -07002473 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return ERR_PTR(-EINVAL);
2475
2476 if (shmem_acct_size(flags, size))
2477 return ERR_PTR(-ENOMEM);
2478
2479 error = -ENOMEM;
2480 this.name = name;
2481 this.len = strlen(name);
2482 this.hash = 0; /* will go */
2483 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002484 path.dentry = d_alloc(root, &this);
2485 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002487 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002490 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002492 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Al Viro2c48b9c2009-08-09 00:52:35 +04002494 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 inode->i_size = size;
2496 inode->i_nlink = 0; /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002497#ifndef CONFIG_MMU
2498 error = ramfs_nommu_expand_for_mapping(inode, size);
2499 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002500 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002501#endif
Al Viro4b42af82009-08-05 18:25:56 +04002502
2503 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002504 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002505 &shmem_file_operations);
2506 if (!file)
2507 goto put_dentry;
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return file;
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002512 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513put_memory:
2514 shmem_unacct_size(flags, size);
2515 return ERR_PTR(error);
2516}
Keith Packard395e0dd2008-06-20 00:08:06 -07002517EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Randy Dunlap46711812008-03-19 17:00:41 -07002519/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2522 */
2523int shmem_zero_setup(struct vm_area_struct *vma)
2524{
2525 struct file *file;
2526 loff_t size = vma->vm_end - vma->vm_start;
2527
2528 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2529 if (IS_ERR(file))
2530 return PTR_ERR(file);
2531
2532 if (vma->vm_file)
2533 fput(vma->vm_file);
2534 vma->vm_file = file;
2535 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c36a2011-03-22 16:33:43 -07002536 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return 0;
2538}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002539
2540/**
2541 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2542 * @mapping: the page's address_space
2543 * @index: the page index
2544 * @gfp: the page allocator flags to use if allocating
2545 *
2546 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2547 * with any new page allocations done using the specified allocation flags.
2548 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2549 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2550 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2551 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002552 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2553 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002554 */
2555struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2556 pgoff_t index, gfp_t gfp)
2557{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002558#ifdef CONFIG_SHMEM
2559 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002560 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002561 int error;
2562
2563 BUG_ON(mapping->a_ops != &shmem_aops);
2564 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2565 if (error)
2566 page = ERR_PTR(error);
2567 else
2568 unlock_page(page);
2569 return page;
2570#else
2571 /*
2572 * The tiny !SHMEM case uses ramfs without swap
2573 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002574 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002575#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002576}
2577EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);