blob: 13ef2d7e912df21ea69316a0ef34371ca106cb27 [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 Dickins0edd73b2005-06-21 17:15:04 -07009 * Copyright (C) 2002-2005 Hugh Dickins.
10 * Copyright (C) 2002-2005 VERITAS Software Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Copyright (C) 2004 Andi Kleen, SuSE Labs
12 *
13 * Extended attribute support for tmpfs:
14 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
15 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
16 *
Matt Mackall853ac432009-01-06 14:40:20 -080017 * tiny-shmem:
18 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
19 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * This file is released under the GPL.
21 */
22
Matt Mackall853ac432009-01-06 14:40:20 -080023#include <linux/fs.h>
24#include <linux/init.h>
25#include <linux/vfs.h>
26#include <linux/mount.h>
Hugh Dickinscaefba12009-04-13 14:40:12 -070027#include <linux/pagemap.h>
Matt Mackall853ac432009-01-06 14:40:20 -080028#include <linux/file.h>
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/swap.h>
32
33static struct vfsmount *shm_mnt;
34
35#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * This virtual memory filesystem is heavily based on the ramfs. It
38 * extends ramfs by the ability to use swap and honor resource limits
39 * which makes it a completely usable filesystem.
40 */
41
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070042#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070043#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010044#include <linux/posix_acl.h>
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070045#include <linux/generic_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/string.h>
48#include <linux/slab.h>
49#include <linux/backing-dev.h>
50#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070053#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070054#include <linux/percpu_counter.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070055#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/security.h>
57#include <linux/swapops.h>
58#include <linux/mempolicy.h>
59#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000060#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070061#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070062#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080063#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040064#include <linux/magic.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <asm/pgtable.h>
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Pretend that each entry is of this size in directory's i_size */
73#define BOGO_DIRENT_SIZE 20
74
Eric Parisb09e0fa2011-05-24 17:12:39 -070075struct shmem_xattr {
76 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
77 char *name; /* xattr name */
78 size_t size;
79 char value[0];
80};
81
Hugh Dickins285b2c42011-08-03 16:21:20 -070082/* Flag allocation requirements to shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083enum sgp_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 SGP_READ, /* don't exceed i_size, don't allocate page */
85 SGP_CACHE, /* don't exceed i_size, may allocate page */
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -080086 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 SGP_WRITE, /* may exceed i_size, may allocate page */
88};
89
Andrew Mortonb76db7352008-02-08 04:21:49 -080090#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080091static unsigned long shmem_default_max_blocks(void)
92{
93 return totalram_pages / 2;
94}
95
96static unsigned long shmem_default_max_inodes(void)
97{
98 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
99}
Andrew Mortonb76db7352008-02-08 04:21:49 -0800100#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800101
Hugh Dickins68da9f02011-07-25 17:12:34 -0700102static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
103 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
104
105static inline int shmem_getpage(struct inode *inode, pgoff_t index,
106 struct page **pagep, enum sgp_type sgp, int *fault_type)
107{
108 return shmem_getpage_gfp(inode, index, pagep, sgp,
109 mapping_gfp_mask(inode->i_mapping), fault_type);
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
113{
114 return sb->s_fs_info;
115}
116
117/*
118 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
119 * for shared memory and for shared anonymous (/dev/zero) mappings
120 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
121 * consistent with the pre-accounting of private mappings ...
122 */
123static inline int shmem_acct_size(unsigned long flags, loff_t size)
124{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000125 return (flags & VM_NORESERVE) ?
126 0 : security_vm_enough_memory_kern(VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static inline void shmem_unacct_size(unsigned long flags, loff_t size)
130{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000131 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 vm_unacct_memory(VM_ACCT(size));
133}
134
135/*
136 * ... whereas tmpfs objects are accounted incrementally as
137 * pages are allocated, in order to allow huge sparse files.
138 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
139 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
140 */
141static inline int shmem_acct_block(unsigned long flags)
142{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000143 return (flags & VM_NORESERVE) ?
144 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147static inline void shmem_unacct_blocks(unsigned long flags, long pages)
148{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000149 if (flags & VM_NORESERVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
151}
152
Hugh Dickins759b9772007-03-05 00:30:28 -0800153static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700154static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800155static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800156static const struct inode_operations shmem_inode_operations;
157static const struct inode_operations shmem_dir_inode_operations;
158static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400159static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700161static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .ra_pages = 0, /* No readahead */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700163 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
166static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800167static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800169static int shmem_reserve_inode(struct super_block *sb)
170{
171 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
172 if (sbinfo->max_inodes) {
173 spin_lock(&sbinfo->stat_lock);
174 if (!sbinfo->free_inodes) {
175 spin_unlock(&sbinfo->stat_lock);
176 return -ENOSPC;
177 }
178 sbinfo->free_inodes--;
179 spin_unlock(&sbinfo->stat_lock);
180 }
181 return 0;
182}
183
184static void shmem_free_inode(struct super_block *sb)
185{
186 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
187 if (sbinfo->max_inodes) {
188 spin_lock(&sbinfo->stat_lock);
189 sbinfo->free_inodes++;
190 spin_unlock(&sbinfo->stat_lock);
191 }
192}
193
Randy Dunlap46711812008-03-19 17:00:41 -0700194/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700195 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * @inode: inode to recalc
197 *
198 * We have to calculate the free blocks since the mm can drop
199 * undirtied hole pages behind our back.
200 *
201 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
202 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
203 *
204 * It has to be called with the spinlock held.
205 */
206static void shmem_recalc_inode(struct inode *inode)
207{
208 struct shmem_inode_info *info = SHMEM_I(inode);
209 long freed;
210
211 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
212 if (freed > 0) {
Hugh Dickins54af60422011-08-03 16:21:24 -0700213 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
214 if (sbinfo->max_blocks)
215 percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700217 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 shmem_unacct_blocks(info->flags, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220}
221
Hugh Dickins285b2c42011-08-03 16:21:20 -0700222static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index,
223 swp_entry_t swap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700225 if (index < SHMEM_NR_DIRECT)
226 info->i_direct[index] = swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Hugh Dickins285b2c42011-08-03 16:21:20 -0700229static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700231 return (index < SHMEM_NR_DIRECT) ?
232 info->i_direct[index] : (swp_entry_t){0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700235/*
236 * Replace item expected in radix tree by a new item, while holding tree lock.
237 */
238static int shmem_radix_tree_replace(struct address_space *mapping,
239 pgoff_t index, void *expected, void *replacement)
240{
241 void **pslot;
242 void *item = NULL;
243
244 VM_BUG_ON(!expected);
245 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
246 if (pslot)
247 item = radix_tree_deref_slot_protected(pslot,
248 &mapping->tree_lock);
249 if (item != expected)
250 return -ENOENT;
251 if (replacement)
252 radix_tree_replace_slot(pslot, replacement);
253 else
254 radix_tree_delete(&mapping->page_tree, index);
255 return 0;
256}
257
258/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700259 * Like add_to_page_cache_locked, but error if expected item has gone.
260 */
261static int shmem_add_to_page_cache(struct page *page,
262 struct address_space *mapping,
263 pgoff_t index, gfp_t gfp, void *expected)
264{
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700265 int error = 0;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700266
267 VM_BUG_ON(!PageLocked(page));
268 VM_BUG_ON(!PageSwapBacked(page));
269
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700270 if (!expected)
271 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
272 if (!error) {
273 page_cache_get(page);
274 page->mapping = mapping;
275 page->index = index;
276
277 spin_lock_irq(&mapping->tree_lock);
278 if (!expected)
279 error = radix_tree_insert(&mapping->page_tree,
280 index, page);
281 else
282 error = shmem_radix_tree_replace(mapping, index,
283 expected, page);
284 if (!error) {
285 mapping->nrpages++;
286 __inc_zone_page_state(page, NR_FILE_PAGES);
287 __inc_zone_page_state(page, NR_SHMEM);
288 spin_unlock_irq(&mapping->tree_lock);
289 } else {
290 page->mapping = NULL;
291 spin_unlock_irq(&mapping->tree_lock);
292 page_cache_release(page);
293 }
294 if (!expected)
295 radix_tree_preload_end();
296 }
297 if (error)
298 mem_cgroup_uncharge_cache_page(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700299 return error;
300}
301
302/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700303 * Like find_get_pages, but collecting swap entries as well as pages.
304 */
305static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
306 pgoff_t start, unsigned int nr_pages,
307 struct page **pages, pgoff_t *indices)
308{
309 unsigned int i;
310 unsigned int ret;
311 unsigned int nr_found;
312
313 rcu_read_lock();
314restart:
315 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
316 (void ***)pages, indices, start, nr_pages);
317 ret = 0;
318 for (i = 0; i < nr_found; i++) {
319 struct page *page;
320repeat:
321 page = radix_tree_deref_slot((void **)pages[i]);
322 if (unlikely(!page))
323 continue;
324 if (radix_tree_exception(page)) {
325 if (radix_tree_exceptional_entry(page))
326 goto export;
327 /* radix_tree_deref_retry(page) */
328 goto restart;
329 }
330 if (!page_cache_get_speculative(page))
331 goto repeat;
332
333 /* Has the page moved? */
334 if (unlikely(page != *((void **)pages[i]))) {
335 page_cache_release(page);
336 goto repeat;
337 }
338export:
339 indices[ret] = indices[i];
340 pages[ret] = page;
341 ret++;
342 }
343 if (unlikely(!ret && nr_found))
344 goto restart;
345 rcu_read_unlock();
346 return ret;
347}
348
349/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700350 * Lockless lookup of swap entry in radix tree, avoiding refcount on pages.
351 */
352static pgoff_t shmem_find_swap(struct address_space *mapping, void *radswap)
353{
354 void **slots[PAGEVEC_SIZE];
355 pgoff_t indices[PAGEVEC_SIZE];
356 unsigned int nr_found;
357
358restart:
359 nr_found = 1;
360 indices[0] = -1;
361 while (nr_found) {
362 pgoff_t index = indices[nr_found - 1] + 1;
363 unsigned int i;
364
365 rcu_read_lock();
366 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
367 slots, indices, index, PAGEVEC_SIZE);
368 for (i = 0; i < nr_found; i++) {
369 void *item = radix_tree_deref_slot(slots[i]);
370 if (radix_tree_deref_retry(item)) {
371 rcu_read_unlock();
372 goto restart;
373 }
374 if (item == radswap) {
375 rcu_read_unlock();
376 return indices[i];
377 }
378 }
379 rcu_read_unlock();
380 cond_resched();
381 }
382 return -1;
383}
384
385/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700386 * Remove swap entry from radix tree, free the swap and its page cache.
387 */
388static int shmem_free_swap(struct address_space *mapping,
389 pgoff_t index, void *radswap)
390{
391 int error;
392
393 spin_lock_irq(&mapping->tree_lock);
394 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
395 spin_unlock_irq(&mapping->tree_lock);
396 if (!error)
397 free_swap_and_cache(radix_to_swp_entry(radswap));
398 return error;
399}
400
401/*
402 * Pagevec may contain swap entries, so shuffle up pages before releasing.
403 */
404static void shmem_pagevec_release(struct pagevec *pvec)
405{
406 int i, j;
407
408 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
409 struct page *page = pvec->pages[i];
410 if (!radix_tree_exceptional_entry(page))
411 pvec->pages[j++] = page;
412 }
413 pvec->nr = j;
414 pagevec_release(pvec);
415}
416
417/*
418 * Remove range of pages and swap entries from radix tree, and free them.
419 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700420void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700422 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700424 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700425 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700426 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700427 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700428 pgoff_t indices[PAGEVEC_SIZE];
429 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700430 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700431 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700433 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
434
435 pagevec_init(&pvec, 0);
436 index = start;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700437 while (index <= end) {
438 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
439 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
440 pvec.pages, indices);
441 if (!pvec.nr)
442 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700443 mem_cgroup_uncharge_start();
444 for (i = 0; i < pagevec_count(&pvec); i++) {
445 struct page *page = pvec.pages[i];
446
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700447 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700448 if (index > end)
449 break;
450
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700451 if (radix_tree_exceptional_entry(page)) {
452 nr_swaps_freed += !shmem_free_swap(mapping,
453 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700454 continue;
455 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700456
457 if (!trylock_page(page))
458 continue;
459 if (page->mapping == mapping) {
460 VM_BUG_ON(PageWriteback(page));
461 truncate_inode_page(mapping, page);
462 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700463 unlock_page(page);
464 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700465 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700466 mem_cgroup_uncharge_end();
467 cond_resched();
468 index++;
469 }
470
471 if (partial) {
472 struct page *page = NULL;
473 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
474 if (page) {
475 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
476 set_page_dirty(page);
477 unlock_page(page);
478 page_cache_release(page);
479 }
480 }
481
482 index = start;
483 for ( ; ; ) {
484 cond_resched();
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700485 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
486 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
487 pvec.pages, indices);
488 if (!pvec.nr) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700489 if (index == start)
490 break;
491 index = start;
492 continue;
493 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700494 if (index == start && indices[0] > end) {
495 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700496 break;
497 }
498 mem_cgroup_uncharge_start();
499 for (i = 0; i < pagevec_count(&pvec); i++) {
500 struct page *page = pvec.pages[i];
501
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700502 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700503 if (index > end)
504 break;
505
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700506 if (radix_tree_exceptional_entry(page)) {
507 nr_swaps_freed += !shmem_free_swap(mapping,
508 index, page);
509 continue;
510 }
511
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700512 lock_page(page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700513 if (page->mapping == mapping) {
514 VM_BUG_ON(PageWriteback(page));
515 truncate_inode_page(mapping, page);
516 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700517 unlock_page(page);
518 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700519 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700520 mem_cgroup_uncharge_end();
521 index++;
522 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700525 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 shmem_recalc_inode(inode);
527 spin_unlock(&info->lock);
528
Hugh Dickins285b2c42011-08-03 16:21:20 -0700529 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700531EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Hugh Dickins94c1e622011-06-27 16:18:03 -0700533static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int error;
537
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200538 error = inode_change_ok(inode, attr);
539 if (error)
540 return error;
541
Hugh Dickins94c1e622011-06-27 16:18:03 -0700542 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
543 loff_t oldsize = inode->i_size;
544 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000545
Hugh Dickins94c1e622011-06-27 16:18:03 -0700546 if (newsize != oldsize) {
547 i_size_write(inode, newsize);
548 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
549 }
550 if (newsize < oldsize) {
551 loff_t holebegin = round_up(newsize, PAGE_SIZE);
552 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
553 shmem_truncate_range(inode, newsize, (loff_t)-1);
554 /* unmap again to remove racily COWed private pages */
555 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200559 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700560#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200561 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100562 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700563#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return error;
565}
566
Al Viro1f895f72010-06-05 19:10:41 -0400567static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700570 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000572 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 shmem_unacct_size(info->flags, inode->i_size);
574 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000575 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800577 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800579 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 }
Eric Parisb09e0fa2011-05-24 17:12:39 -0700582
583 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
584 kfree(xattr->name);
585 kfree(xattr);
586 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700587 BUG_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800588 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400589 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700592/*
593 * If swap found in inode, free it and move page from swapcache to filecache.
594 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700595static int shmem_unuse_inode(struct shmem_inode_info *info,
596 swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700598 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700599 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700600 pgoff_t index;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800601 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700603 radswap = swp_to_radix_entry(swap);
604 index = shmem_find_swap(mapping, radswap);
605 if (index == -1)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700606 return 0;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800607
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800608 /*
609 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400610 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800611 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700612 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800613 */
614 if (shmem_swaplist.next != &info->swaplist)
615 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800616
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800617 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700618 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
619 * but also to hold up shmem_evict_inode(): so inode cannot be freed
620 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800621 */
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700622 error = shmem_add_to_page_cache(page, mapping, index,
623 GFP_NOWAIT, radswap);
Hugh Dickins778dd892011-05-11 15:13:37 -0700624 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700625
Hugh Dickins48f170f2011-07-25 17:12:37 -0700626 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700627 /*
628 * Truncation and eviction use free_swap_and_cache(), which
629 * only does trylock page: if we raced, best clean up here.
630 */
Hugh Dickins73b12622008-02-04 22:28:50 -0800631 delete_from_swap_cache(page);
632 set_page_dirty(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700633 if (!error) {
634 spin_lock(&info->lock);
635 info->swapped--;
636 spin_unlock(&info->lock);
637 swap_free(swap);
638 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800639 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800641 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700645 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700647int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700649 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct shmem_inode_info *info;
651 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700652 int error;
653
654 /*
655 * Charge page using GFP_KERNEL while we can wait, before taking
656 * the shmem_swaplist_mutex which might hold up shmem_writepage().
657 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -0700658 */
659 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
660 if (error)
661 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700662 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800664 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700665 list_for_each_safe(this, next, &shmem_swaplist) {
666 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700667 if (!info->swapped) {
668 spin_lock(&info->lock);
669 if (!info->swapped)
670 list_del_init(&info->swaplist);
671 spin_unlock(&info->lock);
672 }
673 if (info->swapped)
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700674 found = shmem_unuse_inode(info, swap, page);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800675 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800676 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700677 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800679 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700680
Hugh Dickins778dd892011-05-11 15:13:37 -0700681 if (!found)
682 mem_cgroup_uncharge_cache_page(page);
683 if (found < 0)
684 error = found;
685out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800686 unlock_page(page);
687 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700688 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691/*
692 * Move the page from the page cache to the swap cache.
693 */
694static int shmem_writepage(struct page *page, struct writeback_control *wbc)
695{
696 struct shmem_inode_info *info;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700697 swp_entry_t swap, oswap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct address_space *mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700699 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct inode *inode;
701
702 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 mapping = page->mapping;
704 index = page->index;
705 inode = mapping->host;
706 info = SHMEM_I(inode);
707 if (info->flags & VM_LOCKED)
708 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800709 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 goto redirty;
711
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800712 /*
713 * shmem_backing_dev_info's capabilities prevent regular writeback or
714 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700715 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800716 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700717 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800718 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700719 if (!wbc->for_reclaim) {
720 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
721 goto redirty;
722 }
Hugh Dickins285b2c42011-08-03 16:21:20 -0700723
724 /*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700725 * Disable even the toy swapping implementation, while we convert
726 * functions one by one to having swap entries in the radix tree.
Hugh Dickins285b2c42011-08-03 16:21:20 -0700727 */
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700728 if (index < ULONG_MAX)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700729 goto redirty;
730
Hugh Dickins48f170f2011-07-25 17:12:37 -0700731 swap = get_swap_page();
732 if (!swap.val)
733 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800734
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700735 /*
736 * Add inode to shmem_unuse()'s list of swapped-out inodes,
737 * if it's not already there. Do it now because we cannot take
738 * mutex while holding spinlock, and must do so before the page
739 * is moved to swap cache, when its pagelock no longer protects
740 * the inode from eviction. But don't unlock the mutex until
741 * we've taken the spinlock, because shmem_unuse_inode() will
742 * prune a !swapped inode from the swaplist under both locks.
743 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700744 mutex_lock(&shmem_swaplist_mutex);
745 if (list_empty(&info->swaplist))
746 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 spin_lock(&info->lock);
Hugh Dickins48f170f2011-07-25 17:12:37 -0700749 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700750
Hugh Dickins285b2c42011-08-03 16:21:20 -0700751 oswap = shmem_get_swap(info, index);
752 if (oswap.val) {
Hugh Dickins48f170f2011-07-25 17:12:37 -0700753 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700754 free_swap_and_cache(oswap);
755 shmem_put_swap(info, index, (swp_entry_t){0});
756 info->swapped--;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800757 }
758 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Hugh Dickins48f170f2011-07-25 17:12:37 -0700760 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Minchan Kim4c73b1b2011-03-22 16:32:40 -0700761 delete_from_page_cache(page);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700762 shmem_put_swap(info, index, swap);
763 info->swapped++;
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800764 swap_shmem_alloc(swap);
Hugh Dickins826267c2011-05-28 13:14:09 -0700765 spin_unlock(&info->lock);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800766 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700767 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 spin_unlock(&info->lock);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700772 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773redirty:
774 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800775 if (wbc->for_reclaim)
776 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
777 unlock_page(page);
778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800782#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700783static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800784{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700785 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800786
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700787 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700788 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800789
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700790 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800791
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700792 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800793}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700794
795static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
796{
797 struct mempolicy *mpol = NULL;
798 if (sbinfo->mpol) {
799 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
800 mpol = sbinfo->mpol;
801 mpol_get(mpol);
802 spin_unlock(&sbinfo->stat_lock);
803 }
804 return mpol;
805}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800806#endif /* CONFIG_TMPFS */
807
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700808static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
809 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700811 struct mempolicy mpol, *spol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct vm_area_struct pvma;
813
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700814 spol = mpol_cond_copy(&mpol,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700815 mpol_shared_policy_lookup(&info->policy, index));
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* Create a pseudo vma that just contains the policy */
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800818 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700819 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800820 pvma.vm_ops = NULL;
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700821 pvma.vm_policy = spol;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700822 return swapin_readahead(swap, gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Hugh Dickins02098fe2008-02-04 22:28:42 -0800825static struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700826 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 struct vm_area_struct pvma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800830 /* Create a pseudo vma that just contains the policy */
831 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700832 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800833 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700834 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700835
836 /*
837 * alloc_page_vma() will drop the shared policy reference
838 */
839 return alloc_page_vma(gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800841#else /* !CONFIG_NUMA */
842#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700843static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800844{
845}
846#endif /* CONFIG_TMPFS */
847
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700848static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
849 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700851 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Hugh Dickins02098fe2008-02-04 22:28:42 -0800854static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700855 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000857 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800859#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700861#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
862static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
863{
864 return NULL;
865}
866#endif
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700869 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 *
871 * If we allocate a new one we do not mark it dirty. That's up to the
872 * vm. If we swap it in we mark it dirty since we also free the swap
873 * entry since a page cannot live in both the swap and page cache
874 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700875static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -0700876 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af60422011-08-03 16:21:24 -0700879 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700881 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 swp_entry_t swap;
883 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -0700884 int once = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700886 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -0700889 swap.val = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700890 page = find_lock_page(mapping, index);
Hugh Dickins54af60422011-08-03 16:21:24 -0700891 if (radix_tree_exceptional_entry(page)) {
892 swap = radix_to_swp_entry(page);
893 page = NULL;
894 }
895
896 if (sgp != SGP_WRITE &&
897 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
898 error = -EINVAL;
899 goto failed;
900 }
901
902 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800903 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700904 * Once we can get the page lock, it must be uptodate:
905 * if there were an error in reading back from swap,
906 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800907 */
Hugh Dickins54af60422011-08-03 16:21:24 -0700908 BUG_ON(page && !PageUptodate(page));
909 *pagep = page;
910 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /*
Hugh Dickins54af60422011-08-03 16:21:24 -0700914 * Fast cache lookup did not find it:
915 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 */
Hugh Dickins54af60422011-08-03 16:21:24 -0700917 info = SHMEM_I(inode);
918 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (swap.val) {
921 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700922 page = lookup_swap_cache(swap);
923 if (!page) {
Ying Han456f9982011-05-26 16:25:38 -0700924 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700925 if (fault_type)
926 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700927 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700928 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -0700929 error = -ENOMEM;
930 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
934 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -0700935 lock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700936 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -0700938 goto failed;
939 }
940 wait_on_page_writeback(page);
941
942 /* Someone may have already done it for us */
943 if (page->mapping) {
944 if (page->mapping == mapping &&
945 page->index == index)
946 goto done;
947 error = -EEXIST;
948 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700951 error = mem_cgroup_cache_charge(page, current->mm,
952 gfp & GFP_RECLAIM_MASK);
953 if (!error)
954 error = shmem_add_to_page_cache(page, mapping, index,
955 gfp, swp_to_radix_entry(swap));
Hugh Dickins54af60422011-08-03 16:21:24 -0700956 if (error)
957 goto failed;
958
959 spin_lock(&info->lock);
960 info->swapped--;
961 shmem_recalc_inode(inode);
962 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700963
Hugh Dickins27ab7002011-07-25 17:12:36 -0700964 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700965 set_page_dirty(page);
966 swap_free(swap);
967
Hugh Dickins54af60422011-08-03 16:21:24 -0700968 } else {
969 if (shmem_acct_block(info->flags)) {
970 error = -ENOSPC;
971 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700973 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700974 if (percpu_counter_compare(&sbinfo->used_blocks,
Hugh Dickins54af60422011-08-03 16:21:24 -0700975 sbinfo->max_blocks) >= 0) {
976 error = -ENOSPC;
977 goto unacct;
978 }
Tim Chen7e496292010-08-09 17:19:05 -0700979 percpu_counter_inc(&sbinfo->used_blocks);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Hugh Dickins54af60422011-08-03 16:21:24 -0700982 page = shmem_alloc_page(gfp, info, index);
983 if (!page) {
984 error = -ENOMEM;
985 goto decused;
986 }
987
988 SetPageSwapBacked(page);
989 __set_page_locked(page);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700990 error = mem_cgroup_cache_charge(page, current->mm,
991 gfp & GFP_RECLAIM_MASK);
992 if (!error)
993 error = shmem_add_to_page_cache(page, mapping, index,
994 gfp, NULL);
Hugh Dickins54af60422011-08-03 16:21:24 -0700995 if (error)
996 goto decused;
997 lru_cache_add_anon(page);
998
999 spin_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 info->alloced++;
Hugh Dickins54af60422011-08-03 16:21:24 -07001001 inode->i_blocks += BLOCKS_PER_PAGE;
1002 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 spin_unlock(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001004
Hugh Dickins27ab7002011-07-25 17:12:36 -07001005 clear_highpage(page);
1006 flush_dcache_page(page);
1007 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001008 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -07001009 set_page_dirty(page);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011done:
Hugh Dickins54af60422011-08-03 16:21:24 -07001012 /* Perhaps the file has been truncated since we checked */
1013 if (sgp != SGP_WRITE &&
1014 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1015 error = -EINVAL;
1016 goto trunc;
Shaohua Liff36b8012010-08-09 17:19:06 -07001017 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001018 *pagep = page;
1019 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001020
Nick Piggind0217ac2007-07-19 01:47:03 -07001021 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001022 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001024trunc:
1025 ClearPageDirty(page);
1026 delete_from_page_cache(page);
1027 spin_lock(&info->lock);
1028 info->alloced--;
1029 inode->i_blocks -= BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 spin_unlock(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001031decused:
1032 if (sbinfo->max_blocks)
1033 percpu_counter_add(&sbinfo->used_blocks, -1);
1034unacct:
1035 shmem_unacct_blocks(info->flags, 1);
1036failed:
1037 if (swap.val && error != -EINVAL) {
1038 struct page *test = find_get_page(mapping, index);
1039 if (test && !radix_tree_exceptional_entry(test))
1040 page_cache_release(test);
1041 /* Have another try if the entry has changed */
1042 if (test != swp_to_radix_entry(swap))
1043 error = -EEXIST;
1044 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001045 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001046 unlock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001047 page_cache_release(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001048 }
1049 if (error == -ENOSPC && !once++) {
1050 info = SHMEM_I(inode);
1051 spin_lock(&info->lock);
1052 shmem_recalc_inode(inode);
1053 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001054 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001055 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001056 if (error == -EEXIST)
1057 goto repeat;
1058 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1062{
1063 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1064 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001065 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1068 if (error)
1069 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001070
Ying Han456f9982011-05-26 16:25:38 -07001071 if (ret & VM_FAULT_MAJOR) {
1072 count_vm_event(PGMAJFAULT);
1073 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1074 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001075 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
1078#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001079static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001081 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1082 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001085static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1086 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001088 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1089 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001091 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1092 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094#endif
1095
1096int shmem_lock(struct file *file, int lock, struct user_struct *user)
1097{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001098 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct shmem_inode_info *info = SHMEM_I(inode);
1100 int retval = -ENOMEM;
1101
1102 spin_lock(&info->lock);
1103 if (lock && !(info->flags & VM_LOCKED)) {
1104 if (!user_shm_lock(inode->i_size, user))
1105 goto out_nomem;
1106 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001107 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109 if (!lock && (info->flags & VM_LOCKED) && user) {
1110 user_shm_unlock(inode->i_size, user);
1111 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001112 mapping_clear_unevictable(file->f_mapping);
1113 scan_mapping_unevictable_pages(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117out_nomem:
1118 spin_unlock(&info->lock);
1119 return retval;
1120}
1121
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001122static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 file_accessed(file);
1125 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001126 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return 0;
1128}
1129
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001130static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1131 int mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 struct inode *inode;
1134 struct shmem_inode_info *info;
1135 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1136
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001137 if (shmem_reserve_inode(sb))
1138 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 inode = new_inode(sb);
1141 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001142 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001143 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1146 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001147 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 info = SHMEM_I(inode);
1149 memset(info, 0, (char *)inode - (char *)info);
1150 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001151 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001153 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001154 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 switch (mode & S_IFMT) {
1157 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001158 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 init_special_inode(inode, mode, dev);
1160 break;
1161 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001162 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 inode->i_op = &shmem_inode_operations;
1164 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001165 mpol_shared_policy_init(&info->policy,
1166 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001169 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /* Some things misbehave if size == 0 on a directory */
1171 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1172 inode->i_op = &shmem_dir_inode_operations;
1173 inode->i_fop = &simple_dir_operations;
1174 break;
1175 case S_IFLNK:
1176 /*
1177 * Must not load anything in the rbtree,
1178 * mpol_free_shared_policy will not be called.
1179 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001180 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 break;
1182 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001183 } else
1184 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return inode;
1186}
1187
1188#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001189static const struct inode_operations shmem_symlink_inode_operations;
1190static const struct inode_operations shmem_symlink_inline_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001193shmem_write_begin(struct file *file, struct address_space *mapping,
1194 loff_t pos, unsigned len, unsigned flags,
1195 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Nick Piggin800d15a2007-10-16 01:25:03 -07001197 struct inode *inode = mapping->host;
1198 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -07001199 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1200}
1201
1202static int
1203shmem_write_end(struct file *file, struct address_space *mapping,
1204 loff_t pos, unsigned len, unsigned copied,
1205 struct page *page, void *fsdata)
1206{
1207 struct inode *inode = mapping->host;
1208
Hugh Dickinsd3602442008-02-04 22:28:44 -08001209 if (pos + copied > inode->i_size)
1210 i_size_write(inode, pos + copied);
1211
Nick Piggin800d15a2007-10-16 01:25:03 -07001212 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001213 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001214 page_cache_release(page);
1215
Nick Piggin800d15a2007-10-16 01:25:03 -07001216 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1220{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001221 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001223 pgoff_t index;
1224 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001225 enum sgp_type sgp = SGP_READ;
1226
1227 /*
1228 * Might this read be for a stacking filesystem? Then when reading
1229 * holes of a sparse file, we actually need to allocate those pages,
1230 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1231 */
1232 if (segment_eq(get_fs(), KERNEL_DS))
1233 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 index = *ppos >> PAGE_CACHE_SHIFT;
1236 offset = *ppos & ~PAGE_CACHE_MASK;
1237
1238 for (;;) {
1239 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001240 pgoff_t end_index;
1241 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 loff_t i_size = i_size_read(inode);
1243
1244 end_index = i_size >> PAGE_CACHE_SHIFT;
1245 if (index > end_index)
1246 break;
1247 if (index == end_index) {
1248 nr = i_size & ~PAGE_CACHE_MASK;
1249 if (nr <= offset)
1250 break;
1251 }
1252
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001253 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (desc->error) {
1255 if (desc->error == -EINVAL)
1256 desc->error = 0;
1257 break;
1258 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001259 if (page)
1260 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 /*
1263 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001264 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 */
1266 nr = PAGE_CACHE_SIZE;
1267 i_size = i_size_read(inode);
1268 end_index = i_size >> PAGE_CACHE_SHIFT;
1269 if (index == end_index) {
1270 nr = i_size & ~PAGE_CACHE_MASK;
1271 if (nr <= offset) {
1272 if (page)
1273 page_cache_release(page);
1274 break;
1275 }
1276 }
1277 nr -= offset;
1278
1279 if (page) {
1280 /*
1281 * If users can be writing to this page using arbitrary
1282 * virtual addresses, take care about potential aliasing
1283 * before reading the page on the kernel side.
1284 */
1285 if (mapping_writably_mapped(mapping))
1286 flush_dcache_page(page);
1287 /*
1288 * Mark the page accessed if we read the beginning.
1289 */
1290 if (!offset)
1291 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001292 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001294 page_cache_get(page);
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /*
1298 * Ok, we have the page, and it's up-to-date, so
1299 * now we can copy it to user space...
1300 *
1301 * The actor routine returns how many bytes were actually used..
1302 * NOTE! This may not be the same as how much of a user buffer
1303 * we filled up (we may be padding etc), so we can only update
1304 * "pos" here (the actor routine has to update the user buffer
1305 * pointers and the remaining count).
1306 */
1307 ret = actor(desc, page, offset, nr);
1308 offset += ret;
1309 index += offset >> PAGE_CACHE_SHIFT;
1310 offset &= ~PAGE_CACHE_MASK;
1311
1312 page_cache_release(page);
1313 if (ret != nr || !desc->count)
1314 break;
1315
1316 cond_resched();
1317 }
1318
1319 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1320 file_accessed(filp);
1321}
1322
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001323static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1324 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001326 struct file *filp = iocb->ki_filp;
1327 ssize_t retval;
1328 unsigned long seg;
1329 size_t count;
1330 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001332 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1333 if (retval)
1334 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001336 for (seg = 0; seg < nr_segs; seg++) {
1337 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001339 desc.written = 0;
1340 desc.arg.buf = iov[seg].iov_base;
1341 desc.count = iov[seg].iov_len;
1342 if (desc.count == 0)
1343 continue;
1344 desc.error = 0;
1345 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1346 retval += desc.written;
1347 if (desc.error) {
1348 retval = retval ?: desc.error;
1349 break;
1350 }
1351 if (desc.count > 0)
1352 break;
1353 }
1354 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Hugh Dickins708e3502011-07-25 17:12:32 -07001357static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1358 struct pipe_inode_info *pipe, size_t len,
1359 unsigned int flags)
1360{
1361 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001362 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001363 unsigned int loff, nr_pages, req_pages;
1364 struct page *pages[PIPE_DEF_BUFFERS];
1365 struct partial_page partial[PIPE_DEF_BUFFERS];
1366 struct page *page;
1367 pgoff_t index, end_index;
1368 loff_t isize, left;
1369 int error, page_nr;
1370 struct splice_pipe_desc spd = {
1371 .pages = pages,
1372 .partial = partial,
1373 .flags = flags,
1374 .ops = &page_cache_pipe_buf_ops,
1375 .spd_release = spd_release_page,
1376 };
1377
Hugh Dickins71f0e072011-07-25 17:12:33 -07001378 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001379 if (unlikely(*ppos >= isize))
1380 return 0;
1381
1382 left = isize - *ppos;
1383 if (unlikely(left < len))
1384 len = left;
1385
1386 if (splice_grow_spd(pipe, &spd))
1387 return -ENOMEM;
1388
1389 index = *ppos >> PAGE_CACHE_SHIFT;
1390 loff = *ppos & ~PAGE_CACHE_MASK;
1391 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1392 nr_pages = min(req_pages, pipe->buffers);
1393
Hugh Dickins708e3502011-07-25 17:12:32 -07001394 spd.nr_pages = find_get_pages_contig(mapping, index,
1395 nr_pages, spd.pages);
1396 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001397 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001398
Hugh Dickins708e3502011-07-25 17:12:32 -07001399 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001400 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1401 if (error)
1402 break;
1403 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001404 spd.pages[spd.nr_pages++] = page;
1405 index++;
1406 }
1407
Hugh Dickins708e3502011-07-25 17:12:32 -07001408 index = *ppos >> PAGE_CACHE_SHIFT;
1409 nr_pages = spd.nr_pages;
1410 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001411
Hugh Dickins708e3502011-07-25 17:12:32 -07001412 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1413 unsigned int this_len;
1414
1415 if (!len)
1416 break;
1417
Hugh Dickins708e3502011-07-25 17:12:32 -07001418 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1419 page = spd.pages[page_nr];
1420
Hugh Dickins71f0e072011-07-25 17:12:33 -07001421 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001422 error = shmem_getpage(inode, index, &page,
1423 SGP_CACHE, NULL);
1424 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001425 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001426 unlock_page(page);
1427 page_cache_release(spd.pages[page_nr]);
1428 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001429 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001430
1431 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001432 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1433 if (unlikely(!isize || index > end_index))
1434 break;
1435
Hugh Dickins708e3502011-07-25 17:12:32 -07001436 if (end_index == index) {
1437 unsigned int plen;
1438
Hugh Dickins708e3502011-07-25 17:12:32 -07001439 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1440 if (plen <= loff)
1441 break;
1442
Hugh Dickins708e3502011-07-25 17:12:32 -07001443 this_len = min(this_len, plen - loff);
1444 len = this_len;
1445 }
1446
1447 spd.partial[page_nr].offset = loff;
1448 spd.partial[page_nr].len = this_len;
1449 len -= this_len;
1450 loff = 0;
1451 spd.nr_pages++;
1452 index++;
1453 }
1454
Hugh Dickins708e3502011-07-25 17:12:32 -07001455 while (page_nr < nr_pages)
1456 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001457
1458 if (spd.nr_pages)
1459 error = splice_to_pipe(pipe, &spd);
1460
1461 splice_shrink_spd(pipe, &spd);
1462
1463 if (error > 0) {
1464 *ppos += error;
1465 file_accessed(in);
1466 }
1467 return error;
1468}
1469
David Howells726c3342006-06-23 02:02:58 -07001470static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
David Howells726c3342006-06-23 02:02:58 -07001472 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 buf->f_type = TMPFS_MAGIC;
1475 buf->f_bsize = PAGE_CACHE_SIZE;
1476 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001477 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001479 buf->f_bavail =
1480 buf->f_bfree = sbinfo->max_blocks -
1481 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001482 }
1483 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 buf->f_files = sbinfo->max_inodes;
1485 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 /* else leave those fields 0 like simple_statfs */
1488 return 0;
1489}
1490
1491/*
1492 * File creation. Allocate an inode, and we're done..
1493 */
1494static int
1495shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1496{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001497 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 int error = -ENOSPC;
1499
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001500 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001502 error = security_inode_init_security(inode, dir,
1503 &dentry->d_name, NULL,
1504 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001505 if (error) {
1506 if (error != -EOPNOTSUPP) {
1507 iput(inode);
1508 return error;
1509 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001510 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001511#ifdef CONFIG_TMPFS_POSIX_ACL
1512 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001513 if (error) {
1514 iput(inode);
1515 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001516 }
Al Viro718deb62009-12-16 19:35:36 -05001517#else
1518 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001519#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 dir->i_size += BOGO_DIRENT_SIZE;
1521 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1522 d_instantiate(dentry, inode);
1523 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525 return error;
1526}
1527
1528static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1529{
1530 int error;
1531
1532 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1533 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001534 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return 0;
1536}
1537
1538static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1539 struct nameidata *nd)
1540{
1541 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1542}
1543
1544/*
1545 * Link a file..
1546 */
1547static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1548{
1549 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001550 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 /*
1553 * No ordinary (disk based) filesystem counts links as inodes;
1554 * but each new link needs a new dentry, pinning lowmem, and
1555 * tmpfs dentries cannot be pruned until they are unlinked.
1556 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001557 ret = shmem_reserve_inode(inode->i_sb);
1558 if (ret)
1559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 dir->i_size += BOGO_DIRENT_SIZE;
1562 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001563 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04001564 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 dget(dentry); /* Extra pinning count for the created dentry */
1566 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001567out:
1568 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1572{
1573 struct inode *inode = dentry->d_inode;
1574
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001575 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1576 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 dir->i_size -= BOGO_DIRENT_SIZE;
1579 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001580 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 dput(dentry); /* Undo the count from "create" - this does all the work */
1582 return 0;
1583}
1584
1585static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1586{
1587 if (!simple_empty(dentry))
1588 return -ENOTEMPTY;
1589
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001590 drop_nlink(dentry->d_inode);
1591 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return shmem_unlink(dir, dentry);
1593}
1594
1595/*
1596 * The VFS layer already does all the dentry stuff for rename,
1597 * we just have to decrement the usage count for the target if
1598 * it exists so that the VFS layer correctly free's it when it
1599 * gets overwritten.
1600 */
1601static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1602{
1603 struct inode *inode = old_dentry->d_inode;
1604 int they_are_dirs = S_ISDIR(inode->i_mode);
1605
1606 if (!simple_empty(new_dentry))
1607 return -ENOTEMPTY;
1608
1609 if (new_dentry->d_inode) {
1610 (void) shmem_unlink(new_dir, new_dentry);
1611 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001612 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001614 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001615 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
1618 old_dir->i_size -= BOGO_DIRENT_SIZE;
1619 new_dir->i_size += BOGO_DIRENT_SIZE;
1620 old_dir->i_ctime = old_dir->i_mtime =
1621 new_dir->i_ctime = new_dir->i_mtime =
1622 inode->i_ctime = CURRENT_TIME;
1623 return 0;
1624}
1625
1626static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1627{
1628 int error;
1629 int len;
1630 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001631 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 char *kaddr;
1633 struct shmem_inode_info *info;
1634
1635 len = strlen(symname) + 1;
1636 if (len > PAGE_CACHE_SIZE)
1637 return -ENAMETOOLONG;
1638
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001639 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (!inode)
1641 return -ENOSPC;
1642
Eric Paris2a7dba32011-02-01 11:05:39 -05001643 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
1644 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001645 if (error) {
1646 if (error != -EOPNOTSUPP) {
1647 iput(inode);
1648 return error;
1649 }
1650 error = 0;
1651 }
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 info = SHMEM_I(inode);
1654 inode->i_size = len-1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07001655 if (len <= SHMEM_SYMLINK_INLINE_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* do it inline */
Eric Parisb09e0fa2011-05-24 17:12:39 -07001657 memcpy(info->inline_symlink, symname, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 inode->i_op = &shmem_symlink_inline_operations;
1659 } else {
1660 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1661 if (error) {
1662 iput(inode);
1663 return error;
1664 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001665 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 inode->i_op = &shmem_symlink_inode_operations;
1667 kaddr = kmap_atomic(page, KM_USER0);
1668 memcpy(kaddr, symname, len);
1669 kunmap_atomic(kaddr, KM_USER0);
1670 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001671 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 page_cache_release(page);
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 dir->i_size += BOGO_DIRENT_SIZE;
1675 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1676 d_instantiate(dentry, inode);
1677 dget(dentry);
1678 return 0;
1679}
1680
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001681static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Eric Parisb09e0fa2011-05-24 17:12:39 -07001683 nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001684 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001687static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001690 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1691 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001692 if (page)
1693 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001694 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001697static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001700 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 kunmap(page);
1702 mark_page_accessed(page);
1703 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705}
1706
Eric Parisb09e0fa2011-05-24 17:12:39 -07001707#ifdef CONFIG_TMPFS_XATTR
1708/*
1709 * Superblocks without xattr inode operations may get some security.* xattr
1710 * support from the LSM "for free". As soon as we have any other xattrs
1711 * like ACLs, we also need to implement the security.* handlers at
1712 * filesystem level, though.
1713 */
1714
1715static int shmem_xattr_get(struct dentry *dentry, const char *name,
1716 void *buffer, size_t size)
1717{
1718 struct shmem_inode_info *info;
1719 struct shmem_xattr *xattr;
1720 int ret = -ENODATA;
1721
1722 info = SHMEM_I(dentry->d_inode);
1723
1724 spin_lock(&info->lock);
1725 list_for_each_entry(xattr, &info->xattr_list, list) {
1726 if (strcmp(name, xattr->name))
1727 continue;
1728
1729 ret = xattr->size;
1730 if (buffer) {
1731 if (size < xattr->size)
1732 ret = -ERANGE;
1733 else
1734 memcpy(buffer, xattr->value, xattr->size);
1735 }
1736 break;
1737 }
1738 spin_unlock(&info->lock);
1739 return ret;
1740}
1741
1742static int shmem_xattr_set(struct dentry *dentry, const char *name,
1743 const void *value, size_t size, int flags)
1744{
1745 struct inode *inode = dentry->d_inode;
1746 struct shmem_inode_info *info = SHMEM_I(inode);
1747 struct shmem_xattr *xattr;
1748 struct shmem_xattr *new_xattr = NULL;
1749 size_t len;
1750 int err = 0;
1751
1752 /* value == NULL means remove */
1753 if (value) {
1754 /* wrap around? */
1755 len = sizeof(*new_xattr) + size;
1756 if (len <= sizeof(*new_xattr))
1757 return -ENOMEM;
1758
1759 new_xattr = kmalloc(len, GFP_KERNEL);
1760 if (!new_xattr)
1761 return -ENOMEM;
1762
1763 new_xattr->name = kstrdup(name, GFP_KERNEL);
1764 if (!new_xattr->name) {
1765 kfree(new_xattr);
1766 return -ENOMEM;
1767 }
1768
1769 new_xattr->size = size;
1770 memcpy(new_xattr->value, value, size);
1771 }
1772
1773 spin_lock(&info->lock);
1774 list_for_each_entry(xattr, &info->xattr_list, list) {
1775 if (!strcmp(name, xattr->name)) {
1776 if (flags & XATTR_CREATE) {
1777 xattr = new_xattr;
1778 err = -EEXIST;
1779 } else if (new_xattr) {
1780 list_replace(&xattr->list, &new_xattr->list);
1781 } else {
1782 list_del(&xattr->list);
1783 }
1784 goto out;
1785 }
1786 }
1787 if (flags & XATTR_REPLACE) {
1788 xattr = new_xattr;
1789 err = -ENODATA;
1790 } else {
1791 list_add(&new_xattr->list, &info->xattr_list);
1792 xattr = NULL;
1793 }
1794out:
1795 spin_unlock(&info->lock);
1796 if (xattr)
1797 kfree(xattr->name);
1798 kfree(xattr);
1799 return err;
1800}
1801
Eric Parisb09e0fa2011-05-24 17:12:39 -07001802static const struct xattr_handler *shmem_xattr_handlers[] = {
1803#ifdef CONFIG_TMPFS_POSIX_ACL
1804 &generic_acl_access_handler,
1805 &generic_acl_default_handler,
1806#endif
1807 NULL
1808};
1809
1810static int shmem_xattr_validate(const char *name)
1811{
1812 struct { const char *prefix; size_t len; } arr[] = {
1813 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1814 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1815 };
1816 int i;
1817
1818 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1819 size_t preflen = arr[i].len;
1820 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1821 if (!name[preflen])
1822 return -EINVAL;
1823 return 0;
1824 }
1825 }
1826 return -EOPNOTSUPP;
1827}
1828
1829static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1830 void *buffer, size_t size)
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_getxattr(dentry, name, buffer, size);
1841
1842 err = shmem_xattr_validate(name);
1843 if (err)
1844 return err;
1845
1846 return shmem_xattr_get(dentry, name, buffer, size);
1847}
1848
1849static int shmem_setxattr(struct dentry *dentry, const char *name,
1850 const void *value, size_t size, int flags)
1851{
1852 int err;
1853
1854 /*
1855 * If this is a request for a synthetic attribute in the system.*
1856 * namespace use the generic infrastructure to resolve a handler
1857 * for it via sb->s_xattr.
1858 */
1859 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1860 return generic_setxattr(dentry, name, value, size, flags);
1861
1862 err = shmem_xattr_validate(name);
1863 if (err)
1864 return err;
1865
1866 if (size == 0)
1867 value = ""; /* empty EA, do not remove */
1868
1869 return shmem_xattr_set(dentry, name, value, size, flags);
1870
1871}
1872
1873static int shmem_removexattr(struct dentry *dentry, const char *name)
1874{
1875 int err;
1876
1877 /*
1878 * If this is a request for a synthetic attribute in the system.*
1879 * namespace use the generic infrastructure to resolve a handler
1880 * for it via sb->s_xattr.
1881 */
1882 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1883 return generic_removexattr(dentry, name);
1884
1885 err = shmem_xattr_validate(name);
1886 if (err)
1887 return err;
1888
1889 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
1890}
1891
1892static bool xattr_is_trusted(const char *name)
1893{
1894 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1895}
1896
1897static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1898{
1899 bool trusted = capable(CAP_SYS_ADMIN);
1900 struct shmem_xattr *xattr;
1901 struct shmem_inode_info *info;
1902 size_t used = 0;
1903
1904 info = SHMEM_I(dentry->d_inode);
1905
1906 spin_lock(&info->lock);
1907 list_for_each_entry(xattr, &info->xattr_list, list) {
1908 size_t len;
1909
1910 /* skip "trusted." attributes for unprivileged callers */
1911 if (!trusted && xattr_is_trusted(xattr->name))
1912 continue;
1913
1914 len = strlen(xattr->name) + 1;
1915 used += len;
1916 if (buffer) {
1917 if (size < used) {
1918 used = -ERANGE;
1919 break;
1920 }
1921 memcpy(buffer, xattr->name, len);
1922 buffer += len;
1923 }
1924 }
1925 spin_unlock(&info->lock);
1926
1927 return used;
1928}
1929#endif /* CONFIG_TMPFS_XATTR */
1930
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001931static const struct inode_operations shmem_symlink_inline_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 .readlink = generic_readlink,
1933 .follow_link = shmem_follow_link_inline,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001934#ifdef CONFIG_TMPFS_XATTR
1935 .setxattr = shmem_setxattr,
1936 .getxattr = shmem_getxattr,
1937 .listxattr = shmem_listxattr,
1938 .removexattr = shmem_removexattr,
1939#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940};
1941
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001942static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 .readlink = generic_readlink,
1944 .follow_link = shmem_follow_link,
1945 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001946#ifdef CONFIG_TMPFS_XATTR
1947 .setxattr = shmem_setxattr,
1948 .getxattr = shmem_getxattr,
1949 .listxattr = shmem_listxattr,
1950 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001951#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07001952};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001953
David M. Grimes91828a42006-10-17 00:09:45 -07001954static struct dentry *shmem_get_parent(struct dentry *child)
1955{
1956 return ERR_PTR(-ESTALE);
1957}
1958
1959static int shmem_match(struct inode *ino, void *vfh)
1960{
1961 __u32 *fh = vfh;
1962 __u64 inum = fh[2];
1963 inum = (inum << 32) | fh[1];
1964 return ino->i_ino == inum && fh[0] == ino->i_generation;
1965}
1966
Christoph Hellwig480b1162007-10-21 16:42:13 -07001967static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
1968 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07001969{
David M. Grimes91828a42006-10-17 00:09:45 -07001970 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07001971 struct dentry *dentry = NULL;
1972 u64 inum = fid->raw[2];
1973 inum = (inum << 32) | fid->raw[1];
David M. Grimes91828a42006-10-17 00:09:45 -07001974
Christoph Hellwig480b1162007-10-21 16:42:13 -07001975 if (fh_len < 3)
1976 return NULL;
1977
1978 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
1979 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07001980 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07001981 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07001982 iput(inode);
1983 }
1984
Christoph Hellwig480b1162007-10-21 16:42:13 -07001985 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07001986}
1987
1988static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
1989 int connectable)
1990{
1991 struct inode *inode = dentry->d_inode;
1992
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301993 if (*len < 3) {
1994 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07001995 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301996 }
David M. Grimes91828a42006-10-17 00:09:45 -07001997
Al Viro1d3382cb2010-10-23 15:19:20 -04001998 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07001999 /* Unfortunately insert_inode_hash is not idempotent,
2000 * so as we hash inodes here rather than at creation
2001 * time, we need a lock to ensure we only try
2002 * to do it once
2003 */
2004 static DEFINE_SPINLOCK(lock);
2005 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04002006 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07002007 __insert_inode_hash(inode,
2008 inode->i_ino + inode->i_generation);
2009 spin_unlock(&lock);
2010 }
2011
2012 fh[0] = inode->i_generation;
2013 fh[1] = inode->i_ino;
2014 fh[2] = ((__u64)inode->i_ino) >> 32;
2015
2016 *len = 3;
2017 return 1;
2018}
2019
Christoph Hellwig39655162007-10-21 16:42:17 -07002020static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07002021 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07002022 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07002023 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07002024};
2025
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002026static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2027 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 char *this_char, *value, *rest;
2030
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00002031 while (options != NULL) {
2032 this_char = options;
2033 for (;;) {
2034 /*
2035 * NUL-terminate this option: unfortunately,
2036 * mount options form a comma-separated list,
2037 * but mpol's nodelist may also contain commas.
2038 */
2039 options = strchr(options, ',');
2040 if (options == NULL)
2041 break;
2042 options++;
2043 if (!isdigit(*options)) {
2044 options[-1] = '\0';
2045 break;
2046 }
2047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (!*this_char)
2049 continue;
2050 if ((value = strchr(this_char,'=')) != NULL) {
2051 *value++ = 0;
2052 } else {
2053 printk(KERN_ERR
2054 "tmpfs: No value for mount option '%s'\n",
2055 this_char);
2056 return 1;
2057 }
2058
2059 if (!strcmp(this_char,"size")) {
2060 unsigned long long size;
2061 size = memparse(value,&rest);
2062 if (*rest == '%') {
2063 size <<= PAGE_SHIFT;
2064 size *= totalram_pages;
2065 do_div(size, 100);
2066 rest++;
2067 }
2068 if (*rest)
2069 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002070 sbinfo->max_blocks =
2071 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002073 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (*rest)
2075 goto bad_val;
2076 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002077 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (*rest)
2079 goto bad_val;
2080 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002081 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002083 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (*rest)
2085 goto bad_val;
2086 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002087 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002089 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (*rest)
2091 goto bad_val;
2092 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002093 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002095 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (*rest)
2097 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002098 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002099 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08002100 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 } else {
2102 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2103 this_char);
2104 return 1;
2105 }
2106 }
2107 return 0;
2108
2109bad_val:
2110 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2111 value, this_char);
2112 return 1;
2113
2114}
2115
2116static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2117{
2118 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002119 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002120 unsigned long inodes;
2121 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002123 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002124 return error;
2125
2126 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002127 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002128 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002129 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002130 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002131 goto out;
2132 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07002133 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002134 * but we must separately disallow unlimited->limited, because
2135 * in that case we have no record of how much is already in use.
2136 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002137 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002138 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002139 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002140 goto out;
2141
2142 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002143 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002144 sbinfo->max_inodes = config.max_inodes;
2145 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002146
2147 mpol_put(sbinfo->mpol);
2148 sbinfo->mpol = config.mpol; /* transfers initial ref */
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002149out:
2150 spin_unlock(&sbinfo->stat_lock);
2151 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002153
2154static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2155{
2156 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2157
2158 if (sbinfo->max_blocks != shmem_default_max_blocks())
2159 seq_printf(seq, ",size=%luk",
2160 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2161 if (sbinfo->max_inodes != shmem_default_max_inodes())
2162 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2163 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2164 seq_printf(seq, ",mode=%03o", sbinfo->mode);
2165 if (sbinfo->uid != 0)
2166 seq_printf(seq, ",uid=%u", sbinfo->uid);
2167 if (sbinfo->gid != 0)
2168 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002169 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002170 return 0;
2171}
2172#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174static void shmem_put_super(struct super_block *sb)
2175{
Hugh Dickins602586a2010-08-17 15:23:56 -07002176 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2177
2178 percpu_counter_destroy(&sbinfo->used_blocks);
2179 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 sb->s_fs_info = NULL;
2181}
2182
Kay Sievers2b2af542009-04-30 15:23:42 +02002183int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 struct inode *inode;
2186 struct dentry *root;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002187 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002188 int err = -ENOMEM;
2189
2190 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002191 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002192 L1_CACHE_BYTES), GFP_KERNEL);
2193 if (!sbinfo)
2194 return -ENOMEM;
2195
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002196 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002197 sbinfo->uid = current_fsuid();
2198 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002199 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002201#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 /*
2203 * Per default we only allow half of the physical ram per
2204 * tmpfs instance, limiting inodes to one per page of lowmem;
2205 * but the internal instance is left unlimited.
2206 */
2207 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002208 sbinfo->max_blocks = shmem_default_max_blocks();
2209 sbinfo->max_inodes = shmem_default_max_inodes();
2210 if (shmem_parse_options(data, sbinfo, false)) {
2211 err = -EINVAL;
2212 goto failed;
2213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
David M. Grimes91828a42006-10-17 00:09:45 -07002215 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216#else
2217 sb->s_flags |= MS_NOUSER;
2218#endif
2219
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002220 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002221 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2222 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002223 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002224
Hugh Dickins285b2c42011-08-03 16:21:20 -07002225 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 sb->s_blocksize = PAGE_CACHE_SIZE;
2227 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2228 sb->s_magic = TMPFS_MAGIC;
2229 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002230 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002231#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002232 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002233#endif
2234#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002235 sb->s_flags |= MS_POSIXACL;
2236#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002237
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002238 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (!inode)
2240 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002241 inode->i_uid = sbinfo->uid;
2242 inode->i_gid = sbinfo->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 root = d_alloc_root(inode);
2244 if (!root)
2245 goto failed_iput;
2246 sb->s_root = root;
2247 return 0;
2248
2249failed_iput:
2250 iput(inode);
2251failed:
2252 shmem_put_super(sb);
2253 return err;
2254}
2255
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002256static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258static struct inode *shmem_alloc_inode(struct super_block *sb)
2259{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002260 struct shmem_inode_info *info;
2261 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2262 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002264 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002267static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002268{
2269 struct inode *inode = container_of(head, struct inode, i_rcu);
2270 INIT_LIST_HEAD(&inode->i_dentry);
2271 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2272}
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274static void shmem_destroy_inode(struct inode *inode)
2275{
2276 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2277 /* only struct inode is valid if it's an inline symlink */
2278 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2279 }
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002280 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002283static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002285 struct shmem_inode_info *info = foo;
2286 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002289static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2292 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002293 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return 0;
2295}
2296
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002297static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002299 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002302static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002304 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002306 .write_begin = shmem_write_begin,
2307 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002309 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002310 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311};
2312
Helge Deller15ad7cd2006-12-06 20:40:36 -08002313static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 .mmap = shmem_mmap,
2315#ifdef CONFIG_TMPFS
2316 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002317 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002318 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002319 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002320 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002321 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002322 .splice_read = shmem_file_splice_read,
Hugh Dickinsae9764162007-06-04 10:00:39 +02002323 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324#endif
2325};
2326
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002327static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002328 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002329 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002330#ifdef CONFIG_TMPFS_XATTR
2331 .setxattr = shmem_setxattr,
2332 .getxattr = shmem_getxattr,
2333 .listxattr = shmem_listxattr,
2334 .removexattr = shmem_removexattr,
2335#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336};
2337
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002338static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339#ifdef CONFIG_TMPFS
2340 .create = shmem_create,
2341 .lookup = simple_lookup,
2342 .link = shmem_link,
2343 .unlink = shmem_unlink,
2344 .symlink = shmem_symlink,
2345 .mkdir = shmem_mkdir,
2346 .rmdir = shmem_rmdir,
2347 .mknod = shmem_mknod,
2348 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002350#ifdef CONFIG_TMPFS_XATTR
2351 .setxattr = shmem_setxattr,
2352 .getxattr = shmem_getxattr,
2353 .listxattr = shmem_listxattr,
2354 .removexattr = shmem_removexattr,
2355#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002356#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002357 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002358#endif
2359};
2360
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002361static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002362#ifdef CONFIG_TMPFS_XATTR
2363 .setxattr = shmem_setxattr,
2364 .getxattr = shmem_getxattr,
2365 .listxattr = shmem_listxattr,
2366 .removexattr = shmem_removexattr,
2367#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002368#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002369 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002370#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371};
2372
Hugh Dickins759b9772007-03-05 00:30:28 -08002373static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 .alloc_inode = shmem_alloc_inode,
2375 .destroy_inode = shmem_destroy_inode,
2376#ifdef CONFIG_TMPFS
2377 .statfs = shmem_statfs,
2378 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002379 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380#endif
Al Viro1f895f72010-06-05 19:10:41 -04002381 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 .drop_inode = generic_delete_inode,
2383 .put_super = shmem_put_super,
2384};
2385
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002386static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002387 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388#ifdef CONFIG_NUMA
2389 .set_policy = shmem_set_policy,
2390 .get_policy = shmem_get_policy,
2391#endif
2392};
2393
Al Viro3c26ff62010-07-25 11:46:36 +04002394static struct dentry *shmem_mount(struct file_system_type *fs_type,
2395 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Al Viro3c26ff62010-07-25 11:46:36 +04002397 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002400static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 .owner = THIS_MODULE,
2402 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002403 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 .kill_sb = kill_litter_super,
2405};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002407int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 int error;
2410
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002411 error = bdi_init(&shmem_backing_dev_info);
2412 if (error)
2413 goto out4;
2414
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002415 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (error)
2417 goto out3;
2418
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002419 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (error) {
2421 printk(KERN_ERR "Could not register tmpfs\n");
2422 goto out2;
2423 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002424
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002425 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2426 shmem_fs_type.name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (IS_ERR(shm_mnt)) {
2428 error = PTR_ERR(shm_mnt);
2429 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2430 goto out1;
2431 }
2432 return 0;
2433
2434out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002435 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002437 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002439 bdi_destroy(&shmem_backing_dev_info);
2440out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 shm_mnt = ERR_PTR(error);
2442 return error;
2443}
Matt Mackall853ac432009-01-06 14:40:20 -08002444
2445#else /* !CONFIG_SHMEM */
2446
2447/*
2448 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2449 *
2450 * This is intended for small system where the benefits of the full
2451 * shmem code (swap-backed and resource-limited) are outweighed by
2452 * their complexity. On systems without swap this code should be
2453 * effectively equivalent, but much lighter weight.
2454 */
2455
2456#include <linux/ramfs.h>
2457
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002458static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08002459 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002460 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002461 .kill_sb = kill_litter_super,
2462};
2463
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002464int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002465{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002466 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08002467
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002468 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08002469 BUG_ON(IS_ERR(shm_mnt));
2470
2471 return 0;
2472}
2473
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002474int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08002475{
2476 return 0;
2477}
2478
Hugh Dickins3f96b792009-09-21 17:03:37 -07002479int shmem_lock(struct file *file, int lock, struct user_struct *user)
2480{
2481 return 0;
2482}
2483
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002484void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07002485{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002486 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07002487}
2488EXPORT_SYMBOL_GPL(shmem_truncate_range);
2489
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002490#define shmem_vm_ops generic_file_vm_ops
2491#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002492#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002493#define shmem_acct_size(flags, size) 0
2494#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002495
2496#endif /* CONFIG_SHMEM */
2497
2498/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Randy Dunlap46711812008-03-19 17:00:41 -07002500/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 * @name: name for dentry (to be seen in /proc/<pid>/maps
2503 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002504 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002506struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 int error;
2509 struct file *file;
2510 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002511 struct path path;
2512 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 struct qstr this;
2514
2515 if (IS_ERR(shm_mnt))
2516 return (void *)shm_mnt;
2517
Hugh Dickins285b2c42011-08-03 16:21:20 -07002518 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return ERR_PTR(-EINVAL);
2520
2521 if (shmem_acct_size(flags, size))
2522 return ERR_PTR(-ENOMEM);
2523
2524 error = -ENOMEM;
2525 this.name = name;
2526 this.len = strlen(name);
2527 this.hash = 0; /* will go */
2528 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002529 path.dentry = d_alloc(root, &this);
2530 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002532 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002535 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002537 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Al Viro2c48b9c2009-08-09 00:52:35 +04002539 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 inode->i_size = size;
2541 inode->i_nlink = 0; /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002542#ifndef CONFIG_MMU
2543 error = ramfs_nommu_expand_for_mapping(inode, size);
2544 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002545 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002546#endif
Al Viro4b42af82009-08-05 18:25:56 +04002547
2548 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002549 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002550 &shmem_file_operations);
2551 if (!file)
2552 goto put_dentry;
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return file;
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002557 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558put_memory:
2559 shmem_unacct_size(flags, size);
2560 return ERR_PTR(error);
2561}
Keith Packard395e0dd2008-06-20 00:08:06 -07002562EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Randy Dunlap46711812008-03-19 17:00:41 -07002564/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2567 */
2568int shmem_zero_setup(struct vm_area_struct *vma)
2569{
2570 struct file *file;
2571 loff_t size = vma->vm_end - vma->vm_start;
2572
2573 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2574 if (IS_ERR(file))
2575 return PTR_ERR(file);
2576
2577 if (vma->vm_file)
2578 fput(vma->vm_file);
2579 vma->vm_file = file;
2580 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c36a2011-03-22 16:33:43 -07002581 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return 0;
2583}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002584
2585/**
2586 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2587 * @mapping: the page's address_space
2588 * @index: the page index
2589 * @gfp: the page allocator flags to use if allocating
2590 *
2591 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2592 * with any new page allocations done using the specified allocation flags.
2593 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2594 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2595 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2596 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002597 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2598 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002599 */
2600struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2601 pgoff_t index, gfp_t gfp)
2602{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002603#ifdef CONFIG_SHMEM
2604 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002605 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002606 int error;
2607
2608 BUG_ON(mapping->a_ops != &shmem_aops);
2609 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2610 if (error)
2611 page = ERR_PTR(error);
2612 else
2613 unlock_page(page);
2614 return page;
2615#else
2616 /*
2617 * The tiny !SHMEM case uses ramfs without swap
2618 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002619 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002620#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002621}
2622EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);